This document discusses six Python packages that are useful to know:
1. First - A utility for selecting the first successful result from a sequence of functions.
2. Parse - A library for parsing Python format strings and extracting values.
3. Filecmp - A module for comparing files and directories.
4. Bitrot - A tool for detecting silent data corruption in files.
5. Docopt - A tool for generating command-line interfaces from a docstring.
6. Six - A library for writing code that is compatible with both Python 2 and Python 3.
11. +
pip install first
s = 'abc'
m = re1.match(s)
if m:
print('re1', m.group(1))
else:
m = re2.match(s)
if m:
print('re2', m.group(1))
else:
m = re3.match(s)
if m:
print('re3', m.group(1))
else:
print('no match!')
12. +
pip install first
s = 'abc'
m = first(r.match(s)
for r in [re1, re2, re3])
if not m:
print('no match!’)
elif m.re is re1:
print('re1', m.group(1))
elif m.re is re2:
print('re2', m.group(1))
elif m.re is re3:
print('re2', m.group(1))
31. +
pip install docopt
$ rename --help
usage: rename [-h] [-c] [-I] [-l] [-q] [-U] [-v EXCEPT_REGEX] [-t]
[--index-first INDEX_FIRST] [--index-step INDEX_STEP]
[--index-digits INDEX_DIGITS] [--index-pad-with INDEX_PAD_WITH]
[-s] [--selftest [use_directory]]
regex target
positional arguments:
regex regular expression to match files with
target target pattern using references to groups in the
regular expression
optional arguments:
-h, --help show this help message and exit
-c, --copy copy files instead of renaming
-I, --case-insensitive
treat the regular expression as case-insensitive
-l, --lower translate all letters to lower-case
-q, --quiet don't print anything, just return status codes
-U, --upper translate all letters to upper-case
-v EXCEPT_REGEX, --except EXCEPT_REGEX
exclude files matching the following regular
expression
-t, --test test only, don't actually rename anything
-s, --simple invokes the simple mode. For more help on its
positional arguments: rename -s –help
--selftest [use_directory]
run internal unit tests
32. +
pip install docopt
stparser = argparse.ArgumentParser(prog='rename', add_help=False)
classic = argparse.ArgumentParser(prog='rename')
simple = argparse.ArgumentParser(prog='rename')
invocator = Proxy(classic, stparser)
simple.add_argument('-s', '--simple', action='store_true', help='invokes '
'the simple mode', required=True)
common = Proxy(classic, simple)
common.add_argument('-c', '--copy', action='store_true',
help='copy files instead of renaming')
common.add_argument('-I', '--case-insensitive', action='store_true',
help='treat the regular expression as case-insensitive')
common.add_argument('-l', '--lower', action='store_const', dest='xform',
const='lower', help='translate all letters to lower-case')
common.add_argument('-q', '--quiet', action='store_true',
help='don't print anything, just return status codes')
common.add_argument('-U', '--upper', action='store_const', dest='xform',
const='upper', help='translate all letters to upper-case')
common.add_argument('-v', '--except', dest='except_regex', action='store',
default="", help='exclude files matching the following '
'regular expression')
common.add_argument('-t', '--test', action='store_true', help='test only, '
'don't actually rename anything')
group = classic.add_argument_group('Configuration for the special '
'(index) reference')
group.add_argument('--index-first', default=1, help='specifies '
'what number will the first (index) substitution contain. Default: 1')
group.add_argument('--index-step', default=1, help='specifies '
'what number will be added with each step to the first value. Negative '
'numbers allowed. Default: 1')
group.add_argument('--index-digits', default='auto',
help='specifies how many digits will be used in each (index) '
'substitution. If a number has fewer digits, they will be prefixed by '
'leading zeroes (or another character, see --index-pad-with). Default: '
'auto (e.g. path enough digits so that each number uses the same amount'
' of characters)')
group.add_argument('--index-pad-with', default='0',
help='specifies what character will be used for padding. Default: "0"')
invocator.add_argument('-s', '--simple', action='store_true', help='invokes '
'the simple mode. For more help on its positional arguments: '
'rename -s --help')
invocator.add_argument('--selftest', nargs='?', const=True,
metavar='use_directory', help='run internal unit tests')
classic.add_argument('regex', help='regular expression to match '
'files with')
classic.add_argument('target', help='target pattern using '
'references to groups in the regular expression')
simple.add_argument('substring_from', help='simple (raw) substring that '
'should be found within the filename')
simple.add_argument('substring_to', help='the replacement string')
simple.add_argument('regex', help='regular expression to match '
'files with')
args = stparser.parse_known_args()
if args[0].selftest:
selftest(args[0].selftest)
elif args[0].simple:
args = simple.parse_args()
args.regex = "".join(args.regex)
args.substring_from = "".join(args.substring_from)
args.substring_to = "".join(args.substring_to)
sys.exit(Renamer(**args.__dict__).rename_simple(**args.__dict__))
else:
args = classic.parse_args()
args.regex = "".join(args.regex)
args.except_regex = "".join(args.except_regex)
args.target = "".join(args.target)
33. +
pip install docopt
$ httproxy --help
Tiny HTTP Proxy.
This module implements GET, HEAD, POST, PUT, DELETE and CONNECT
methods on BaseHTTPServer.
Usage:
httproxy [options]
httproxy [options] <allowed-client> ...
Options:
-h, --help Show this screen.
--version Show version and exit.
-H, --host HOST Host to bind to [default: 127.0.0.1].
-p, --port PORT Port to bind to [default: 8000].
-l, --logfile PATH Path to the logfile [default: STDOUT].
-i, --pidfile PIDFILE Path to the pidfile [default: httproxy.pid].
-d, --daemon Daemonize (run in the background). The
default logfile path is httproxy.log in
this case.
-c, --configfile CONFIGFILE Path to a configuration file.
-v, --verbose Log headers.
34. +
pip install docopt
"""Tiny HTTP Proxy.
This module implements GET, HEAD, POST, PUT, DELETE and CONNECT
methods on BaseHTTPServer.
Usage:
httproxy [options]
httproxy [options] <allowed-client> ...
Options:
-h, --help Show this screen.
--version Show version and exit.
-H, --host HOST Host to bind to [default: 127.0.0.1].
-p, --port PORT Port to bind to [default: 8000].
-l, --logfile PATH Path to the logfile [default: STDOUT].
-i, --pidfile PIDFILE Path to the pidfile [default: httproxy.pid].
-d, --daemon Daemonize (run in the background). The
default logfile path is httproxy.log in
this case.
-c, --configfile CONFIGFILE Path to a configuration file.
-v, --verbose Log headers.
"""
37. +
pip install six
Have a Python 2.x project?
1. Write those tests, seriously.
2. Target 2.7 with __futures__:
division
print_function
unicode_literals
3. Run tests on 3.3. Use six to fix leftovers.
4. There is no step 4! *
38. +
pip install six
* Well, to have all bases covered, read:
http://docs.python.org/3/
howto/pyporting.html
39. +
pip install six
Have a Python 2.x project?
1. Tests.
2. 2.7 + three futures.
3. Run tests on both 2.7 and 3.3.
Fix whatever doesn’t work using six.
40. +
pip install six
six.text_type / six.binary_type