63 Reusable Code Snippet Topics

Remove Filter
Member Avatar for Gribouillis

This snippet defines a function [b]chmod[/b] which uses symbolic strings to represent the mode (eg [b]u+rwx[/b]) like the shell command 'chmod'.

0
509
Member Avatar for Gribouillis

Joining together ordered sample points (xi, yi) when ths xi's are different yields a piecewise linear continuous (P1) function. This snippet defines a handy class to manipulate these functions. It allows computing the value and the slope of the function at each point, arithmetic operations, absolute value, truncation and linear …

Member Avatar for Gribouillis
1
598
Member Avatar for Gribouillis

This snippet defines a context to execute a block of statements in a different working directory (with the help of the with statement). After the block is exited, the initial working directory is restored, even if an exception has occurred in the with block.

0
592
Member Avatar for Gribouillis

Printing a convenient [url=http://en.wikipedia.org/wiki/ANSI_escape_sequence]ansi escape sequence[/url] clears the terminal (if you're running python from a terminal). Similar techniques could be used to print in colors.

Member Avatar for Gribouillis
0
278
Member Avatar for Gribouillis

This snippet shows a fast and efficient way to cut strings in substrings with a fixed length (note that the last substring may be smaller).

Member Avatar for bumsfeld
1
621
Member Avatar for Gribouillis

When several producer threads put items in a queue.Queue, and a consumer thread gets items from the queue, there is no way for the consumer thread to tell the producers that they should stop feeding the queue. This snippet defines a subclass of Queue with a close method. After the …

Member Avatar for Gribouillis
0
320
Member Avatar for Gribouillis

This snippet defines a cachedProperty decorator. A cached property differs from a property in that it's value is only computed the first time that the property is accessed and then stored in the object's dict for later use. If the object's attribute is explicitely deleted, it will be computed again …

0
494
Member Avatar for Gribouillis

This snippet defines a function [icode]patfilter(pattern, rule, sequence)[/icode] which implements filtering a sequence of strings according to various criteria. The output is either a subsequence of strings, or a sequence of match objects.

Member Avatar for Gribouillis
0
402
Member Avatar for Gribouillis

Many threads in daniweb's python forum deal with menu based programs which run in a terminal. This snippet defines a handy class, MenuCrawler, to help writing such programs. Subclassing from this base classes and adding methods corresponding to a given set of menus allows one to build the menu-based application. …

0
3K
Member Avatar for Gribouillis

This snippet defines a class which can group a collection of strings according to a given set of prefixes. Each string goes in the group of the longest prefix it contains.

0
748
Member Avatar for Gribouillis

This snippet allows your code to use the Mendeleiev's periodic table of elements. It defines a single function [icode]mendeleiev_table()[/icode] which returns the table as a python list of lists.

Member Avatar for Gribouillis
1
813
Member Avatar for Gribouillis

If you're not used to work with the standard module logging, this snippet will allow you to incorporate a logger in your application without effort. It gives you a starting point before you try to use more sophisticated features of the logging module.

0
185
Member Avatar for Gribouillis

This is a command line utility which lists the names of all functions defined in a python source file with a [icode]def <name>[/icode] statement

2
214

The End.