Some languages don't even have a standard library.
It's got some great stuff.
It's got some not so great stuff.
Must in 2.x or 3.x standard library.
Python version added will be noted if recent.
Fun or just jolly useful counts.
>>> import argparse
>>> parser = argparse.ArgumentParser()
...[snip]
>>> parser.parse_args(['--sum', '7', '-1', '42'])
Namespace(accumulate=, integers=[7, -1, 42])
>>> import atexit, os
>>> os.path.exists('confoo.txt')
True
>>> def cleanup(): os.system('rm confoo.txt')
...
>>> atexit.register(cleanup)
>>> ^D
$ ls confoo.txt
ls: confoo.txt: No such file or directory
>>> import codecs
>>> codecs.open('example.txt', 'r',
... 'utf-8', 'strict').read()
u'\u0540\u0561\u0575\u0561\u057d\u057f\u0561\u0576\n'
>>> from contextlib import contextmanager
>>> @contextmanager
... def tag(name):
... print "<%s>" % name,
... yield
... print "</%s>" % name
...
>>> with tag('h1'): print 'hello',
...
'<h1> hello </h1>'
>>> from collections import defaultdict
>>> beer = defaultdict(lambda: defaultdict(dict))
>>> beer['guinness']['colour'] = 'black'
>>> beer['guinness']['colour']
'black'
>>> from collections import Counter
>>> food = Counter(['eggs', 'bacon'])
>>> food['eggs']
1
>>> food['ham']
0
>>> food += Counter(['eggs', 'ham'])
>>> food['eggs']
2
>>> food['ham']
1
>>> from collections import OrderedDict
>>> o = OrderedDict()
>>> o['x'] = 'X'
>>> o['y'] = 'Y'
>>> o.keys()
['x', 'y']
>>> from collections import namedtuple
>>> Point = namedtuple('Point', ['x', 'y'])
>>> p = Point(3, 4)
>>> p.x
3
>>> from ctypes import *
>>> print windll.kernel32
<WinDLL 'kernel32', handle ... at ...>
>>> from decimal import Decimal
>>> Decimal('3.14214').quantize(Decimal('.01'))
Decimal('3.14')
>>> from urllib import urlopen
>>> import dis
>>> dis.dis(urlopen)
73 0 LOAD_CONST 1 (-1)
3 LOAD_CONST 2 (('warnpy3k',))
6 IMPORT_NAME 0 (warnings)
>>> import functools, os
>>> os.path.join('/some/root', 'bar')
'/some/root/bar'
>>> join = functools.partial(
... os.path.join, '/some/root')
>>> join('bar')
'/some/root/bar'
>>> from functools import total_ordering
>>> @total_ordering
... class Person:
... def __init__(self, age):
... self.age = age
... def __lt__(self, other):
... return self.age < other.age
...
>>> print Person(45) >= Person(5)
True
>>> from functools import wraps
>>> def my_decorator(f):
... @wraps(f)
... def wrapper(*args, **kwds):
... return f(*args, **kwds)
... return wrapper
>>> from glob import glob
>>> glob('apps/*/*.py')
['apps/abuse/__init__.py',
'apps/access/__init__.py', ...]
>>> import imaplib
>>> imap = imaplib.IMAP4('mail.mozilla.com', 993)
>>> imap.login('user', 'pass')
>>> imap.select()
>>> typ, data = imap.search(None, 'ALL')
>>> for num in data[0].split():
... typ, data = imap.fetch(num, '(RFC822)')
... print 'Message %s\n%s\n' % (num, data[0][1])
>>> imap.close()
>>> imap.logout()
>>> from importlib import import_module
>>> import_module('urllib')
<module 'urllib' from ...>
>>> import inspect
>>> inspect.getmoduleinfo('../settings_local.pyc')
ModuleInfo(name='settings_local', suffix='.pyc',
mode='rb', module_type=2)
>>> import inspect, urllib
>>> inspect.getsource(urllib.urlopen)
'def urlopen(url, data=None, proxies=None):\n...'
>>> import inspect
>>> inspect.stack()
[(<frame object at 0x7fdd21cbf490>, '<stdin>', 1,
'<module>', None, None)]
>>> from itertools import groupby
>>> x = [1,1,3,3,3,3,3,4,5,6]
>>> for x, g in groupby(x):
... print x; print [y for y in g]
...
1
[1, 1]
3
[3, 3, 3, 3, 3]
>>> from itertools import chain
>>> for x in chain('a', 'bc'):
... print x
...
a
b
c
>>> from itertools import imap
>>> for x in itertools.imap(str.upper, ['a','b']):
... print x
...
A
B
$ echo '{"foo": "lorem", "bar": "ipsum"}'\
| python -m json.tool
{
"bar": "ipsum",
"foo": "lorem"
}
>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'fr_FR')
'fr_FR'
>>> locale.currency(10)
'10,00 Eu'
>>> import operator
>>> map(operator.mul, [1,2,3], [4,5,6])
[4, 10, 18]
$ python -m pdb foo.py
> foo.py(1)()
-> import smtplib
(Pdb)
>>> import pdb; pdb.set_trace()
--Return--
> (1)()->None
(Pdb)
>>> import pdb
>>> import mymodule
>>> mymodule.test()
Traceback (most recent call last):
File "./mymodule.py", line 3, in test2
print spam
NameError: spam
>>> pdb.pm()
> ./mymodule.py(3)test2()
-> print spam
(Pdb)
$ python -m profile foo.py -s ncalls
5961 function calls (5896 primitive calls) in 0.076...
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
13 0.000 0.000 0.000 0.000 :0(__contains__)
2 0.000 0.000 0.000 0.000 :0(_getframe)
11 0.000 0.000 0.000 0.000 :0(add)
13 0.000 0.000 0.001 0.000 :0(all)
327 0.001 0.000 0.001 0.000 :0(append)
>>> import re
>>> match = re.match('(?P<first>\w+) (?P<last>\w+)',
'Isaac Newton')
>>> match.expand('\g<last> \g<first>')
'Newton Isaac'
>>> set([1,2,3]).intersection(set([3,4,5]))
set([3])
$ python -m SimpleHTTPServer
Serving HTTP on 0.0.0.0 port 8000 ...
$ python -m smtpd -n -c DebuggingServer localhost:2000
---------- MESSAGE FOLLOWS ----------
From: bob@site.com
To: jane@site.com
X-Peer: 127.0.0.1
ConFoo
------------ END MESSAGE ------------
>>> res = subprocess.check_output(["ls", "-l"])
>>> res
'total 296\n-rw-r--r-- 1 andy staff...'
>>> import os
>>> from tempfile import NamedTemporaryFile
>>> tmp = NamedTemporaryFile()
>>> tmp.name
'/var/folders/g_/d41tpd...'
>>> os.path.exists(tmp.name)
True
>>> tmp.close()
>>> os.path.exists(tmp.name)
False
>>> import timeit
>>> timeit.timeit(
... '",".join(str(n) for n in range(100))',
... number=10000)
0.41231489181518555
$ python -m trace --trace foo.py
--- modulename: threading, funcname: settrace
threading.py(90): _trace_hook = func
--- modulename: foo, funcname:
foo.py(1): import smtplib
--- modulename: smtplib, funcname:
smtplib.py(33): '''
smtplib.py(44): import socket
--- modulename: socket, funcname:
socket.py(44): """
socket.py(46): import _socket
$ python -c "import unicodedata;\
print unicodedata.lookup('SNOWMAN')"
>>> from unittest.mock import Mock
>>> thing = Mock()
>>> thing.method()
<Mock name='mock.method()' id='4467254992'>
>>> thing.method.return_value = 3
>>> thing.method()
3
@patch('requests.post')
def test_auth_fails(self, post):
post.side_effect = AuthError
with self.assertRaises(AuthError):
self.paypal.call('get-pay-key', {})
>>> from urllib.parse import urlparse, urlunparse
>>> urlparse('http://mozilla.org')
ParseResult(scheme='http', netloc='mozilla.org',
path='', params='', query='',
fragment='')
>>> urlunparse(urlparse('http://mozilla.org'))
'http://mozilla.org'
>>> import uuid
>>> uuid.getnode()
123516846201053
>>> str(uuid.uuid1())
'd4547447-8047-11e2-be45-705681bdd8dd'
>>> import venv
>>> venv.create('/tmp/venv')
>>> from wsgiref.simple_server import make_server
>>> def hello_world_app(environ, start_response):
... start_response(
'200 OK',
[('Content-type', 'text/plain')])
... return ['Hello world']
...
>>> httpd = make_server('', 8000, hello_world_app)
>>> httpd.serve_forever()
>>> from wsgiref import handlers
>>> handlers.BaseHandler.error_status
'500 Dude, this is whack!'
>>> import xml.etree.ElementTree
>>> tree = ElementTree.parse('country_data.xml')
>>> tree.getroot().findall('country')
[<Element country at 1084a6248>, ...]
>>> import this
The Zen of Python, by Tim Peters
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
... [snip]
>>> from __future__ import braces
File "", line 1
SyntaxError: not a chance
>>> from __future__ import barry_as_FLUFL
>>> print(3!=4)
File "", line 1
print(3!=4)
^
SyntaxError: invalid syntax
>>> print(3<>4)
True
>>> import antigravity
httplib, urllib, urllib2 etc. Use requests.
xml etc (mostly). Use lxml.
threading, asyncore etc. Relevant?
Use gevent, tornadio, twisted (maybe).