Python Libraries II By Michael Valle.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

UFCE8V-20-3 Information Systems Development 3 (SHAPE HK) Lecture 3 PHP (2) : Functions, User Defined Functions & Environment Variables.
PYTHON’S STANDARD LIBRARY – PART II. Repr() Similar use to str() Used to return Python object representation that evaluates to such objects Useful for.
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
And PyLNP for the RCX By: Eric Cranmer and Nick Blake.
Builtins, namespaces, functions. There are objects that are predefined in Python Python built-ins When you use something without defining it, it means.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
 socket.socket() returns socket object: _socketobject  Most of socket API are methods on socket objects or functions  Value result arguments (e.g.,
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Functions.
WHAT IS A DATABASE? A DATABASE IS A COLLECTION OF DATA RELATED TO A PARTICULAR TOPIC OR PURPOSE OR TO PUT IT SIMPLY A GENERAL PURPOSE CONTAINER FOR STORING.
With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
5 1 Data Files CGI/Perl Programming By Diane Zak.
Chapter Four I/O Redirection1 System Programming Shell Operators.
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
FUNCTIONS. Topics Introduction to Functions Defining and Calling a Void Function Designing a Program to Use Functions Local Variables Passing Arguments.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Data Types and Conversions, Input from the Keyboard CS303E: Elements of Computers and Programming.
N ATIONAL E NERGY R ESEARCH S CIENTIFIC C OMPUTING C ENTER Charles Leggett Colors in the Message Service ATLAS software workshop March Architecture.
SIDHANT GARG.  Python comes with a bevy of pre-written pieces of programming code for you to use in your programs. The individual files are called modules,
Variables, Types, Expressions Intro2CS – week 1b 1.
Python’s Standard Library Part 2 Francis Ryan. Output formatting, 1 repr module – When using large or deeply nested containers, allows better printing.
Python’s Standard Library Part II Dennis Tran. Output Formatting The repr module provides a version of repr() customized for abbreviated displays of large.
Lists/Dictionaries. What we are covering Data structure basics Lists Dictionaries Json.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Automation Testing- QTP Rajesh Charles Batch No: Date: jan
Introduction to C++ (Extensions to C)
Topics Designing a Program Input, Processing, and Output
Topics Introduction to Functions Defining and Calling a Void Function
Input from STDIN STDIN, standard input, comes from the keyboard.
CHP - 9 File Structures.
Chapter 7 Text Input/Output Objectives
Chapter 7 Text Input/Output Objectives
Module 5 Working with Data
Prof. Leonardo Mostarda University of Camerino
Containers and Lists CIS 40 – Introduction to Programming in Python
CSc 120 Introduction to Computer Programing II
Chapter 7 Text Input/Output Objectives
Department of Computer Science,
Introduction to C CSE 2031 Fall /3/ :33 AM.
Other Kinds of Arrays Chapter 11
Overview of Hadoop MapReduce MapReduce is a soft work framework for easily writing applications which process vast amounts of.
Programming in C Input / Output.
Programming in C Input / Output.
CHAPTER FOUR Functions.
Optimizing Malloc and Free
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 3 Introduction to Classes, Objects Methods and Strings
Python Primer 2: Functions and Control Flow
Topics Introduction to File Input and Output
Chapter 7 Files and Exceptions
Bits and Bytes Topics Representing information as bits
Python’s Standard Library Part II
Bits and Bytes Topics Representing information as bits
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
Bits and Bytes Topics Representing information as bits
File Input and Output.
Perl I/O Learning Objectives:
Python Primer 1: Types and Operators
Programming in C Input / Output.
Introduction to Data Structure
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Introduction to C EECS May 2019.
Topics Introduction to File Input and Output
Python’s STD Library Part II.
 A function is a named sequence of statement(s) that performs a computation. It contains  line of code(s) that are executed sequentially from top.
Professor Jodi Neely-Ritz University of Florida
Abstract Data Types (ADTs)
Introduction to Computer Science
Presentation transcript:

Python Libraries II By Michael Valle

Output Formatting use repr module to print brief string representations of large container structures import repr repr.repr(set('facetious')) "set(['f', 'a', 'c', 'e', 't', 'i', 'o','u',s'])"

Output Formatting (continued) Use the pprint module to print built in or user defined structures that can be read by the interpreter. The width parameter specifies how long the printed line may be, and if the printed line is too long, the string is printed on multiple lines. >>> import pprint >>> t = [[[['black', 'cyan'], 'white', ['green', 'red']], [['magenta', ... 'yellow'], 'blue']]] ... >>> pprint.pprint(t, width=30) [[[['black', 'cyan'], 'white', ['green', 'red']], [['magenta', 'yellow'], 'blue']]]

Output Formatting (continued) Use textwrap module to split a string into a list of strings and print them. Each string is no larger than the ‘width’ parameter in length, but may be smaller. Works just like Fill method only it produces a list of strings instead of one string separated by new line characters. >>> import textwrap >>> doc = ""“If I have seen farther than other men, it is only because I stood ... on the shoulders of giants – Isaac Newton""" ... >>> print textwrap.fill(doc, width=24) If I have seen farther than other men, it is only because I stood on the shoulders of giants – Isaac Newton

Output Formatting (continued) Use locale module to access a data base of string format conventions for different countries or locales. Use locale.localeconv() to get a mapping from particular items in this format >>> import locale >>> locale.setlocale(locale.LC_ALL, 'English_United States.1252') 'English_United States.1252‘ >>> conv = locale.localeconv() # get a mapping of conventions >>> x = 1234567.8 >>> locale.format("%d", x, grouping=True) '1,234,567' >>> locale.format_string("%s%.*f", (conv['currency_symbol'], ... conv['frac_digits'], x), grouping=True) '$1,234,567.80'

Templating Use the Template class from the string model to create generic strings that take parameters, marked by ‘$’ signs. The substitute method allows you to pass in parameters to the string template, but raises a KeyError when a parameter is not supplied. The safe substitute method allows for missing parameters. Subclasses of Template class can specify their own delimiters, other than ‘$’ >>> from string import Template >>> t = Template(‘The people of ${villageName} village are indebted to you, ${playerName}.') >>> t.substitute(village=‘Kakariko', playerName=‘Link') ‘The people of Kakariko village are indebted to you, Link’ >>> t = Template('Return the $item to $place.') >>> d = dict(item=‘Master Sword') >>> t.substitute(d) Traceback (most recent call last): . . . KeyError: 'owner' >>> t.safe_substitute(d) 'Return the Master Sword to $place.'

Working with Binary Data Record Layouts The struct module provides methods ‘pack’ and ‘unpack’ for packing and unpacking binary files in binary format. First parameter to unpack is the format string. Character ‘<‘ means standard size and little endian byte order. Character ‘H’ represents a field of 2 bytes, character ‘I’ represents a field of 4 bytes. The data is returned in list form. import struct data = open('myfile.zip', 'rb').read() start = 0 for i in range(3): # show the first 3 file headers start += 14 fields = struct.unpack('<IIIHH', data[start:start+16]) crc32, comp_size, uncomp_size, filenamesize, extra_size = fields start +=16 filename = data[start:start+filenamesize] start += filenamesize Extra=data[start:start+extra_size] print filename, hex(crc32), comp_size, uncomp_size start += extra_size + comp_size # skip to the next header

Multi-threading Use the threading module for multithreaded processes. Define a subclass of the ‘threading’ class, and implement its ‘run’ function to define the process . Use start() to begin execution of a thread, and join() to wait for a thread to finish execution. import threading, zipfile class AsyncZip(threading.Thread): def __init__(self, infile, outfile): threading.Thread.__init__(self) self.infile = infile self.outfile = outfile def run(self): f = zipfile.ZipFile(self.outfile, 'w', zipfile.ZIP_DEFLATED) f.write(self.infile) f.close() print 'Finished background zip of: ', self.infile background = AsyncZip('mydata.txt', 'myarchive.zip') background.start() Print 'The main program continues to run in foreground.‘ background.join() # Wait for the background task to finish print 'Main program waited until background was done.'

Logging Use the logging module to log messages about debugging, errors, warnings, and other information By default, debugging and information messages are not printed, and error messages are sent to stderr The logging system can be reconfigured to send the messages to email, datagrams, sockets and servers. Reconfiguration can be done through Python, or configuration files. import logging logging.debug('Debugging information') logging.info('Informational message') logging.warning('Warning:config file %s not found', 'server.conf') logging.error('Error occurred') logging.critical('Critical error – shutting down')

Weak references Use the weakref module to create weak references. Automatic garbage collection removes an object when no reference to it exists. Having a reference keeps an object alive. Having a weak reference, does not. >>> import weakref, gc >>> a = A(10) # create a reference >>> d = weakref.WeakValueDictionary() >>> d['primary'] = a # does not create a reference >>> d['primary'] # fetch the object if it is still alive 10 >>> del a # remove the one reference >>> gc.collect() # run garbage collection right away >>> d['primary'] # entry was automatically removed Traceback (most recent call last): File "<stdin>", line 1, in <module> d['primary'] # entry was automatically removed File "C:/python26/lib/weakref.py", line 46, in __getitem__ o = self.data[key]() KeyError: 'primary'

Tools for working with Lists Alternative data structures with different performance properties exist. Use the ‘array’ module for creation of more compact lists. For instance, an integer array using 2 bytes per entry instead of 16. Use the ‘collections’ module to create deques with faster append and pop functions at the ends, but slower lookup functions for elements in the middle of the list. Use the ‘bisect’ module for functions optimized for sorted lists. Use the ‘heapq’ module for list based implementations of heaps, and heap related functions.

Decimal Floating Point Arithmetic Use the decimal module to create Decimal data types which allow you to save and operate on decimal numbers to any level of precision. >>> from decimal import * >>> Decimal('0.70') * Decimal('1.05') Decimal('0.7350') >>> .70 * 1.05 0.73499999999999999 >>> Decimal('1.00') % Decimal('.10') Decimal('0.00') >>> 1.00 % 0.10 0.09999999999999995 >>> sum([Decimal('0.1')]*10) == Decimal('1.0') True >>> sum([0.1]*10) == 1.0 False >>> getcontext().prec = 36 >>> Decimal(1) / Decimal(7) Decimal('0.142857142857142857142857142857142857')

Questions?