Strings  ¶

  • Replace accentuated characters by their ASCII equivalent in a unicode string:

    import unicodedata
    
    unicodedata.normalize("NFKD", "éèàçÇÉȲ³¼ÀÁÂÃÄÅËÍÑÒÖÜÝåïš™").encode("ascii", "ignore")
    
  • Cleanest way I found to produce slugified / tokenized strings, based on boltons.strutils  :

    >>> from boltons import strutils
    >>> strutils.slugify(" aBc De F   1 23 4! -- ! 56--78 - -9- %$& +eée-", "-", ascii=True)
    b'abc-de-f-1-23-4-56-78-9-eee'
    

    Alternative: use awesome-slugify  package.

Sorting  ¶

  • Sort a list of dicts by dict-key ( source  ):

    import operator
    
    [dict(a=1, b=2, c=3), dict(a=2, b=2, c=2), dict(a=3, b=2, c=1)].sort(
        key=operator.itemgetter("c")
    )
    

Date & Time  ¶

I recommend using Arrow . But if you can’t, here are some pure-python snippets.

  • Add a month to the current date:

    import datetime
    import dateutil
    
    datetime.date.today() + dateutil.relativedelta(months=1)
    

Network  ¶

  • Set urllib2 timeout ( source  ):

    import socket
    
    socket.setdefaulttimeout(10)
    
  • Start a dumb HTTP server on port 8000 ( source  ):

    $ python -m SimpleHTTPServer 8000
    

Debug  ¶

  • Add a Python’s debugger break point:

    import pdb
    
    pdb.set_trace()
    
  • Delete all .pyc and .pyo files in the system:

    $ find / -name "*.py[co]" -print -delete
    

Version  ¶

  • Print Python’s 3-elements version number:

    $ python -c "from __future__ import print_function; import sys; print('.'.join(map(str, sys.version_info[:3])))"
    2.7.6
    
  • Compare Python version for use in shell scripts:

    $ python -c "import sys; exit(sys.version_info[:3] < (2, 7, 9))"
    $ if [[ $? != 0 ]]; then
    >     echo "Old Python detected.";
    > fi
    Old Python detected.
    

Style  ¶

  • Use autopep8 to apply PEP8’s coding style on all Python files:

    $ find ./ -iname "*.py" -print -exec autopep8 --in-place "{}" \;
    

Configuration  ¶

I maintain a set of default configuration files in my dotfiles repository  :

Package Management  ¶

  • Generate a binary distribution of the current package:

    $ python ./setup.py sdist
    
  • Register, generate and upload to PyPi the current package as a source package, an egg and a dumb binary:

    $ python ./setup.py register sdist bdist_egg bdist_dumb upload
    
  • Download Pygments’ source distribution from PyPi, without dependencies ( source  ):

    $ pip download --no-binary=:all: --no-deps pygments==2.14.0
    
  • Hackish way to execute the CLI above with Pip’s internal (tested with pip==22.1 ), inspired by pip._internal.cli.base_command.Command._main()  :

    from pathlib import Path
    
    from pip._internal.cli.status_codes import SUCCESS
    from pip._internal.commands.download import DownloadCommand
    from pip._internal.utils.temp_dir import global_tempdir_manager, tempdir_registry
    
    
    tmp_path = Path("/tmp")
    
    # Emulate the following CLI call:
    #   $ pip download --no-binary=:all: --no-deps pygments==2.14.0
    cmd = DownloadCommand(name="dummy_name", summary="dummy_summary")
    
    with cmd.main_context():
        cmd.tempdir_registry = cmd.enter_context(tempdir_registry())
        cmd.enter_context(global_tempdir_manager())
        options, args = cmd.parse_args(
            [
                "--no-binary=:all:",
                "--no-deps",
                "--dest",
                f"{tmp_path}",
                f"pygments==2.14.0",
            ]
        )
        cmd.verbosity = options.verbose
        outcome = cmd.run(options, args)
        assert outcome == SUCCESS
    
    package_path = tmp_path.joinpath("Pygments-2.14.0.tar.gz")
    assert package_path.is_file()
    

Jinja  ¶

To generate curly braces:

>>> from jinja2 import Template
>>> Template(""" Yo! """).render()
u' Yo! '
>>> Template(""" {{'{{'}} """).render()
u' {{ '
>>> Template(""" {{'{'}} """).render()
u' { '
>>> Template(""" {{'{'}}machin{{'}'}} """).render()
u' {machin} '

Data  ¶