D3 updating setup file
Keys must be immutable objects: ints, strings, tuples, ... import sys class Writer: def __init__(self, file_name): self.out_file = file(file_name, 'a') def write(self, msg): self.out_file.write('%s' % msg) def close(self): self.out_file.close() def test(): writer = Writer('outputfile.txt') save_stdout = sys.stdout sys.stdout = writer print 'hello' print 'goodbye' writer.close() # Show the output.
Literals for constructing dictionaries: In : f = open('mylog.txt', 'w') In : f.write('message #1\n') In : f.write('message #2\n') In : f.write('message #3\n') In : f.close() In : f = file('mylog.txt', 'r') In : for line in f: ....: print line, ....: message #1 message #2 message #3 In : f.close() In : f = open('mylog.txt', 'a') In : f.write('message #4\n') In : f.close() In : f = file('mylog.txt', 'r') In : for line in f: ....: print line, ....: message #1 message #2 message #3 message #4 In : f.close() In : import zipfile In : outfile = open('tmp1.zip', 'wb') In : zfile = zipfile. ZIP_DEFLATED) In : zfile.writestr('entry1', 'my heroes have always been cowboys') In : zfile.writestr('entry2', 'and they still are it seems') In : zfile.writestr('entry3', 'sadly in search of and') In : zfile.writestr('entry4', 'on step in back of') In : In : zfile.writestr('entry4', 'one step in back of') In : zfile.writestr('entry5', 'themselves and their slow moving ways') In : zfile.close() In : outfile.close() In : $ $ unzip -lv tmp1Archive: tmp1Length Method Size Ratio Date Time CRC-32 Name -------- ------ ------- ----- ---- ---- ------ ---- 34 Defl: N 36 -6% 05-29-08 f6b7d921 entry1 27 Defl: N 29 -7% 05-29-08 10da8f3d entry2 22 Defl: N 24 -9% 05-29-08 3fd17fda entry3 18 Defl: N 20 -11% 05-29-08 d55182e6 entry4 19 Defl: N 21 -11% 05-29-08 1a892acd entry4 37 Defl: N 39 -5% 05-29-08 e213708c entry5 -------- ------- --- ------- 157 169 -8% 6 files A set is an unordered collection of immutable objects. Sets support several set operations, for example: union, intersection, difference, ... tmp_file = file('outputfile.txt') sys.stdout = save_stdout content = tmp_file.read() tmp_file.close() print content test() outfile, 'Message #3' In : outfile.close() In : In : infile = open('tmp.log', 'r') In : for line in infile: ...: print 'Line:', line.rstrip('\n') ...: Line: Message #1 Line: Message #2 Line: Message #3 In : infile.close() But, note that in the previous exercise, a generator expression would often be better.
There are also tools that extract and format doc strings, for example: Understanding the Python execution model -- How Python evaluates and executes your code. Creating names/variables -- Binding -- The following all create names (variables) and bind values (objects) to them: (1) assignment, (2) function definition, (3) class definition, (4) function and method call, (5) importing a module, ...
First class objects -- Almost all objects in Python are first class.
Preface This book is a collection of materials that I've used when conducting Python training and also materials from my Web site that are intended for self-instruction.]]
Therefore, a frozenset is hash-able and can be used as a key in a dictionary, and it can be added to a set. Examples: Structured code -- Python programs are made up of expressions, statements, functions, classes, modules, and packages. Catching an exception by exception class catches exceptions of that class and all its subclasses.You will find a script to start up IDLE in the Tools/scripts directory of your Python distribution. In addition, there are tools that will give you a more powerful and fancy Python interactive interpreter. Also, editors with support for Python often provide the ability to comment out selected blocks of code, usually with "##". A doc string is written as a quoted string that is at the top of a module or the first lines after the header line of a function or class.We can use triple-quoting to create doc strings that span multiple lines.If the upgrade includes two sets of firmware (A and B), upgrade the A firmware first and then repeat steps 4–5 below to upgrade the B firmware.There is no need to repeat these steps if the upgrade includes only one set of firmware (A or B).