Extrait de code Python

Formating

Use variable name to format strings

prefix = "HAHA"
attr = "HOHO"
print "{prefix}{attr}".format(**locals())

In python 3.6+ you can do this simply with:

prefix = "HAHA"
attr = "HOHO"
print(f"{prefix}{attr}")

Iterator

Here is a list of useful iterators not present in the itertools module.

Iterate over packets of N elements in the given list

This is useful for frame range generation when creating renderfarm jobs (eg: RIB genration).

Pattern is:ABCDEF -> AB, CD, EF

def chunck(l, n):
    return (l[i:i+n] for i in xrange(0, len(l), n))
>>> list(chunck(range(16),3))
[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11], [12, 13, 14], [15]]

Pair sliding

Usefull to organize a list of connected nodes inside a graph.

Pattern is:ABCDEF -> AB, BC, CD, DE, EF

import itertools

def pair_slide(l):
    return itertools.izip(l, l[1:])
>>> list(pair_slide("ABCDEF"))
[('A', 'B'), ('B', 'C'), ('C', 'D'), ('D', 'E'), ('E', 'F')]

Systeme

psutil alternative (Linux)

import os

def process_info():

    for out in os.popen('ps --no-headers -eo pid:1,user,command'):

        pid, user, cmd = out.rstrip('\n').split(' ', 2)

        pid = int(pid)

        yield pid, user, cmd

Update NFS cache

def update_nfs_cache(path):
    """Update NFS cache to given file path.

    Args:
        path (str): Path to update
    """
    splitted = path.replace('\\', '/').split('/')

    path_aggregated = splitted.pop(0)

    for item in splitted:

        path_aggregated = '/'.join((path_aggregated, item))

        try:
            os.listdir(path_aggregated)
        except OSError:
            os.stat(path_aggregated)

Other

Profiling

import cProfile, pstats, StringIO
pr = cProfile.Profile()
pr.enable()

# your code

pr.disable()
s = StringIO.StringIO()
ps = pstats.Stats(pr, stream=s).sort_stats('cumulative')
ps.print_stats(0.1)  # 0.1 for only first 10% lines
print(s.getvalue())

More information on pstats.Stats() documentation.

Generate and execute dynamic Bash commands in Python

This can be very usefull to dynamically create Bash functions in Python. A good example is to dynamically set environment variables after getting them in Python.

Your Bash file need this:

eval "$(python print_bash_stuff.py)"

print_bash_stuff.py file just need to dynamically print bash commands:

print 'function test {\n    echo "toto"\n}\n'