Fictional list of recipes for a fictional cookbook on Python packaging.
- Package a single, pure Python module
| from collections import namedtuple | |
| import functools | |
| import math | |
| from avro import schema, datafile, io | |
| SCHEMA_STR = """{ | |
| "type": "record", | |
| "name": "Feature", | |
| "fields": [ |
| """REPL enhancements for Python interactive interpreter. | |
| This module adds: | |
| - tab completion (requires readline support) | |
| - command history between sessions | |
| - syntactic sugar for 'help(stmt)' via 'stmt?' | |
| - output history | |
| To enable set the environment variables: |
| """Calculate the number of leading zeros on an unsigned integer. | |
| Uses (or abuses) floating-point post-normalization to calculate the number of | |
| leading zeros on an unsigned integer of 32 bits or less. | |
| Adapted from figure 5-12 of Hacker's Delight by Henry Warren. | |
| http://www.hackersdelight.org/HDcode/nlz.c.txt | |
| """ | |
| import ctypes | |
| import sys |
| "Usage example of InitProtocl metaclass" | |
| import magic | |
| class Base(object, metaclass=magic.InitProtocol): | |
| """Example of class using init protocol constructor. | |
| The Base method (Which is named to match the class name.) will be renamed | |
| to __init__ by the metaclass. | |
| """ |
| "Usage example of InitProtocl metaclass" | |
| import magic | |
| class Base(object, metaclass=magic.InitProtocol): | |
| """Empty Base class. | |
| Implements the getattr protocol for a 'null' attribute. | |
| """ |
| """REPL enhancements for Python interactive interpreter. | |
| This module adds: | |
| - tab completion (requires readline support) | |
| - command history between sessions | |
| - syntactic sugar for 'help(stmt)' via 'stmt?' | |
| - user 'more' as pager command | |
| - output history |
| "collections.namedtuple implementation without using exec." | |
| from collections import OrderedDict | |
| from keyword import iskeyword | |
| from operator import itemgetter | |
| import itertools | |
| import sys | |
| __all__ = ['NamedTuple', 'namedtuple'] | |
| """Demonstrate self extraction of data from a Python script. | |
| **YOU PROBABLY DON'T WANT TO DO THIS** | |
| If run with any command line arguments, this script will extract the Zen of | |
| Python from the embedded data at the end of the script and print it to standard | |
| out. | |
| $ python self_extract.py | |
| The Zen of Python, by Time Peters |
| 0:d=0 hl=4 l= 380 cons: SEQUENCE | |
| 4:d=1 hl=4 l= 294 cons: SEQUENCE | |
| 8:d=2 hl=2 l= 9 prim: INTEGER :D5628EC478688A35 | |
| 19:d=2 hl=2 l= 13 cons: SEQUENCE | |
| 21:d=3 hl=2 l= 9 prim: OBJECT :sha1WithRSAEncryption | |
| 32:d=3 hl=2 l= 0 prim: NULL | |
| 34:d=2 hl=2 l= 69 cons: SEQUENCE | |
| 36:d=3 hl=2 l= 11 cons: SET | |
| 38:d=4 hl=2 l= 9 cons: SEQUENCE | |
| 40:d=5 hl=2 l= 3 prim: OBJECT :countryName |