http://docs.python.org/lib/module-os.html
import os # Vykdome komanda print os.system ('ls -la') # Rodo listing'a, gale - 0 # Kuriame kataloge esame? print os.getcwd() # Keichiame kataloga? os.chdir ('.') # Rodome visas funkcijas, esanchias modulyje print dir(os) # Rodome modulio funkcijos dokumentacija # Galima ir: help(os) print help(os.getcwd)
http://docs.python.org/lib/module-shutil.html
import shutil shutil.copyfile ('/tmp/test1', '/tmp/test10') shutil.move ('/tmp/test10', '/tmp/test11')
http://docs.python.org/lib/module-glob.html
import glob # Rodome tik kai kuriuos failus print glob.glob('*.py')
Dar žiūrėti: http://docs.python.org/lib/module-getopt.html ir http://docs.python.org/lib/module-optparse.html
import sys print sys.argv
import sys # I STDERR ishvedame klaida sys.stderr.write ('Klaida, klaida!\n')
import sys sys.exit()
http://docs.python.org/lib/module-re.html
import re print re.findall(r'\bf[a-z]*', 'which foot or hand fell fastest') # ['foot', 'fell', 'fastest'] print re.sub(r'(\b[a-z]+) \1', r'\1', 'cat in the the hat') # 'cat in the hat' # Eilutes keisti galima papraschiau eilute = 'vienas keturi trys' print eilute.replace ('keturi', 'du')
http://docs.python.org/lib/module-math.html
import math print math.cos(math.pi / 4.0) # 0.70710678118654757 print math.log(1024, 2) # 10.0
http://docs.python.org/lib/module-random.html
import random print random.choice(['apple', 'pear', 'banana']) # 'apple' print random.sample(xrange(100), 10) # sampling without replacement # [30, 83, 16, 4, 8, 81, 41, 50, 18, 33] print random.random() # random float # 0.17970987693706186 arba kitas print random.randrange(6) # random integer chosen from range(6) # 4
http://docs.python.org/lib/module-urllib2.html
import urllib2 for line in urllib2.urlopen('http://tycho.usno.navy.mil/cgi-bin/timer.pl'): if 'EST' in line or 'EDT' in line: # look for Eastern Time print line
http://docs.python.org/lib/module-smtplib.html
import smtplib server = smtplib.SMTP('localhost') server.sendmail('soothsayer@example.org', 'jcaesar@example.org', """To: jcaesar@example.org From: soothsayer@example.org Beware the Ides of March. """) server.quit()
http://docs.python.org/lib/module-datetime.html
from datetime import date now = date.today() print now # datetime.date(2003, 12, 2) print now.strftime("%m-%d-%y. %d %b %Y is a %A on the %d day of %B.") # '12-02-03. 02 Dec 2003 is a Tuesday on the 02 day of December.' # dates support calendar arithmetic birthday = date(1964, 7, 31) age = now - birthday print age.days # 14368
http://docs.python.org/lib/module-zlib.html
import zlib s = 'witch which has which witches wrist watch' print len(s) # 41 t = zlib.compress(s) print len(t) # 37 print zlib.decompress(t) # 'witch which has which witches wrist watch' print zlib.crc32(s) # 226805979
http://docs.python.org/lib/module-timeit.html
from timeit import Timer print Timer('t=a; a=b; b=t', 'a=1; b=2').timeit() # 0.57535828626024577 print Timer('a,b = b,a', 'a=1; b=2').timeit() # 0.54962537085770791
In contrast to timeit
's fine level of granularity, the profile
(http://docs.python.org/lib/module-profile.html) and pstats
modules provide tools for identifying time critical sections in larger blocks of code.
http://docs.python.org/lib/module-doctest.html
The doctest
module provides a tool for scanning a module and validating tests embedded in a program's docstrings. Test construction is as simple as cutting-and-pasting a typical call along with its results into the docstring
. This improves the documentation by providing the user with an example and it allows the doctest module to make sure the code remains true to the documentation:
def average(values): """Computes the arithmetic mean of a list of numbers. print average([20, 30, 70]) 40.0 """ return sum(values, 0.0) / len(values) import doctest doctest.testmod() # automatically validate the embedded tests
http://docs.python.org/lib/module-unittest.html
The unittest
module is not as effortless as the doctest
module, but it allows a more comprehensive set of tests to be maintained in a separate file:
pavyzdys kažkodėl neveikia.
import unittest class TestStatisticalFunctions(unittest.TestCase): def test_average(self): self.assertEqual(average([20, 30, 70]), 40.0) self.assertEqual(round(average([1, 5, 7]), 1), 4.3) self.assertRaises(ZeroDivisionError, average, []) self.assertRaises(TypeError, average, 20, 30, 70) unittest.main() # Calling from the command line invokes all tests
http://docs.python.org/lib/module-repr.html
import repr print repr.repr(set('supercalifragilisticexpialidocious')) # "set(['a', 'c', 'd', 'e', 'f', 'g', ...])"
http://docs.python.org/lib/module-pprint.html
Grazhiai spausdina ivairias strukturas, o jos lieka skaitomos parser'iui.
import pprint t = [[[['black', 'cyan'], 'white', ['green', 'red']], [['magenta', 'yellow'], 'blue']]] print pprint.pprint (t, width=30) # [[[['black', 'cyan'], # 'white', # ['green', 'red']], # [['magenta', 'yellow'], # 'blue']]]
http://docs.python.org/lib/module-textwrap.html
import textwrap doc = """The wrap() method is just like fill() except that it returns a list of strings instead of one big string with newlines to separate the wrapped lines.""" print textwrap.fill(doc, width=40) # The wrap() method is just like fill() # except that it returns a list of strings # instead of one big string with newlines # to separate the wrapped lines.
http://docs.python.org/lib/module-locale.html
import locale print locale.setlocale(locale.LC_ALL, 'lt_LT.UTF-8') # 'English_United States.1252' conv = locale.localeconv() # get a mapping of conventions x = 1234567.8 print locale.format("%d", x, grouping=True) # '1,234,567' print locale.format("%s%.*f", (conv['currency_symbol'], conv['frac_digits'], x), grouping=True) # '$1,234,567.80'
Ish string
modulio (http://docs.python.org/lib/module-string.html)
The format uses placeholder names formed by „$
“ with valid Python identifiers (alphanumeric characters and underscores). Surrounding the placeholder with braces allows it to be followed by more alphanumeric letters with no intervening spaces. Writing „$$
“ creates a single escaped „$“:
from string import Template t = Template('${village}folk send $$10 to $cause.') print t.substitute(village='Nottingham', cause='the ditch fund') # 'Nottinghamfolk send $10 to the ditch fund.'
The substitute
method raises a KeyError
when a placeholder is not supplied in a dictionary or a keyword argument. For mail-merge style applications, user supplied data may be incomplete and the safe_substitute
method may be more appropriate – it will leave placeholders unchanged if data is missing.
Galima nustatyti ir kitoki delimiter'i:
import time, os.path photofiles = ['img_1074.jpg', 'img_1076.jpg', 'img_1077.jpg'] class BatchRename(Template): delimiter = '%' fmt = raw_input('Enter rename style (%d-date %n-seqnum %f-format): ') t = BatchRename(fmt) date = time.strftime('%d%b%y') for i, filename in enumerate(photofiles): base, ext = os.path.splitext(filename) newname = t.substitute(d=date, n=i, f=ext) print '%s --> %s' % (filename, newname)
http://docs.python.org/lib/module-struct.html
The following example shows how to loop through header information in a ZIP file (with pack codes „H
“ and „L
“ representing two and four byte unsigned numbers respectively):
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('LLLHH', 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
http://docs.python.org/lib/module-threading.html
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.'
While those tools are powerful, minor design errors can result in problems that are difficult to reproduce. So, the preferred approach to task coordination is to concentrate all access to a resource in a single thread and then use the Queue
module (http://docs.python.org/lib/module-Queue.html) to feed that thread with requests from other threads. Applications using Queue
objects for inter-thread communication and coordination are easier to design, more readable, and more reliable.
http://docs.python.org/lib/module-logging.html
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') # WARNING:root:Warning:config file server.conf not found # ERROR:root:Error occurred # CRITICAL:root:Critical error -- shutting down
http://docs.python.org/lib/module-weakref.html
Python does automatic memory management (reference counting for most objects and garbage collection to eliminate cycles). The memory is freed shortly after the last reference to it has been eliminated.
This approach works fine for most applications but occasionally there is a need to track objects only as long as they are being used by something else. Unfortunately, just tracking them creates a reference that makes them permanent. The weakref
module provides tools for tracking objects without creating a reference. When the object is no longer needed, it is automatically removed from a weakref
table and a callback is triggered for weakref
objects. Typical applications include caching objects that are expensive to create:
import weakref, gc class A: def __init__(self, value): self.value = value def __repr__(self): return str(self.value) a = A(10) # create a reference d = weakref.WeakValueDictionary() d['primary'] = a # does not create a reference print d['primary'] # fetch the object if it is still alive # 10 del a # remove the one reference print gc.collect() # run garbage collection right away # 0 d['primary'] # entry was automatically removed # Raise'ina exception'a
http://docs.python.org/lib/module-array.html
The array
module provides an array()
object that is like a list that stores only homogenous data and stores it more compactly. The following example shows an array of numbers stored as two byte unsigned binary numbers (typecode „H“) rather than the usual 16 bytes per entry for regular lists of python int
objects:
from array import array a = array('H', [4000, 10, 700, 22222]) print sum(a) # 26932 print a[1:3] # array('H', [10, 700])
http://docs.python.org/lib/module-collections.html
The collections
module provides a deque()
object that is like a list with faster appends and pops from the left side but slower lookups in the middle. These objects are well suited for implementing queues and breadth first tree searches:
from collections import deque d = deque(["task1", "task2", "task3"]) d.append("task4") print "Handling", d.popleft() # Handling task1 unsearched = deque([starting_node]) def breadth_first_search(unsearched): node = unsearched.popleft() for m in gen_moves(node): if is_goal(m): return m unsearched.append(m)
http://docs.python.org/lib/module-bisect.html
In addition to alternative list implementations, the library also offers other tools such as the bisect
module with functions for manipulating sorted lists:
import bisect scores = [(100, 'perl'), (200, 'tcl'), (400, 'lua'), (500, 'python')] bisect.insort(scores, (300, 'ruby')) print scores # [(100, 'perl'), (200, 'tcl'), (300, 'ruby'), (400, 'lua'), (500, 'python')]
http://docs.python.org/lib/module-heapq.html
The heapq
module provides functions for implementing heaps based on regular lists. The lowest valued entry is always kept at position zero. This is useful for applications which repeatedly access the smallest element but do not want to run a full list sort:
from heapq import heapify, heappop, heappush data = [1, 3, 5, 7, 9, 2, 4, 6, 8, 0] heapify(data) # rearrange the list into heap order heappush(data, -5) # add a new entry [heappop(data) for i in range(3)] # fetch the three smallest entries # [-5, 0, 1]
http://docs.python.org/lib/module-decimal.html
The decimal
module offers a Decimal
datatype for decimal floating point arithmetic. Compared to the built-in float
implementation of binary floating point, the new class is especially helpful for financial applications and other uses which require exact decimal representation, control over precision, control over rounding to meet legal or regulatory requirements, tracking of significant decimal places, or for applications where the user expects the results to match calculations done by hand.
from decimal import * print Decimal('0.70') * Decimal('1.05') # Decimal("0.7350") print .70 * 1.05 # 0.73499999999999999
Exact representation enables the Decimal
class to perform modulo calculations and equality tests that are unsuitable for binary floating point:
print Decimal('1.00') % Decimal('.10') # Decimal("0.00") print 1.00 % 0.10 # 0.09999999999999995 print sum([Decimal('0.1')]*10) == Decimal('1.0') # True print sum([0.1]*10) == 1.0 # False
The decimal module provides arithmetic with as much precision as needed:
getcontext().prec = 36 print Decimal(1) / Decimal(7) # Decimal("0.142857142857142857142857142857142857")