Cryptographic helper
Time related methods, also includes time zone handling some misc methods
Bases: datetime.tzinfo
Fixed offset in minutes east from UTC.
Bases: datetime.tzinfo
A ‘local’ time zone implementation
Calculate time delta between two datetime objects. (the result is somewhat imprecise, only use for prettyprinting).
If either time1 or time2 is None, the current time will be used instead.
Convenience method for formatting the date part of a datetime object. See format_datetime for more details.
Format the datetime object t into an unicode string
If t is None, the current time will be used.
The formatting will be done using the given format, which consist of conventional strftime keys. In addition the format can be ‘iso8601’ to specify the international date format.
tzinfo will default to the local timezone if left to None.
Convenience method for formatting the time part of a datetime object. See format_datetime for more details.
Present the default format used by format_date in a human readable form. This is a format that will be recognized by parse_date when reading a date.
Present the default format used by format_datetime in a human readable form. This is a format that will be recognized by parse_date when reading a date.
Fetch timezone instance by name or return None
Convert t into a datetime object, using the following rules:
- If t is already a datetime object, it is simply returned.
- If t is None, the current time will be used.
- If t is a number, it is interpreted as a timestamp.
If no tzone is given, the local timezone will be used.
Any other input will trigger a TypeError.
File handling utils
Recursively copy a directory tree using copy2() (from shutil.copytree.)
Added a skip parameter consisting of absolute paths which we don’t want to copy.
Create directory and add a nice git safe file in so we will handle the folder in version control
Create a new file. An index is added if the path exists
Purge a whole dir, including all it’s containing files and subfolders!
Method that just. Inpired by: http://stackoverflow.com/questions/458436/adding-folders-to-a-zip-file-using-python @param source_dir: The folder that will be zipped @param dest_file: Destination zip file. If not set source_dir base name will be used @param exclude_dirs: List of directories that should be excluded @param include_root: If True source_dir will be included in the resulting zip
Returns True if the file name is pointing to a zip file. Otherwise False
Simple hashing module, used to handle different type of hashes for clients, clusters, discover servers, etc...
msgpackjson.py - JSON loads and dumps proxy defenitions and implemented using msgpack instead
Copyright (C) 2014, Moritz Wundke. Released under MIT license.
A Python Singleton mixin class that makes use of some of the ideas found at http://c2.com/cgi/wiki?PythonSingleton. Just inherit from it and you have a singleton. No code is required in subclasses to create singleton behavior – inheritance from Singleton is all that is needed.
Singleton creation is threadsafe.
USAGE:
Just inherit from Singleton. If you need a constructor, include an __init__() method in your class as you usually would. However, if your class is S, you instantiate the singleton using S.getInstance() instead of S(). Repeated calls to S.getInstance() return the originally-created instance.
For example:
class S(Singleton):
- def __init__(self, a, b=1):
- pass
S1 = S.getInstance(1, b=3)
Most of the time, that’s all you need to know. However, there are some other useful behaviors. Read on for a full description:
Getting the singleton:
S.getInstance()
returns the instance of S. If none exists, it is created.
The usual idiom to construct an instance by calling the class, i.e.
S()
is disabled for the sake of clarity.
For one thing, the S() syntax means instantiation, but getInstance() usually does not cause instantiation. So the S() syntax would be misleading.
Because of that, if S() were allowed, a programmer who didn’t happen to notice the inheritance from Singleton (or who wasn’t fully aware of what a Singleton pattern does) might think he was creating a new instance, which could lead to very unexpected behavior.
So, overall, it is felt that it is better to make things clearer by requiring the call of a class method that is defined in Singleton. An attempt to instantiate via S() will result in a SingletonException being raised.
3) Use __S.__init__() for instantiation processing, since S.getInstance() runs S.__init__(), passing it the args it has received.
If no data needs to be passed in at instantiation time, you don’t need S.__init__().
4) If S.__init__(.) requires parameters, include them ONLY in the first call to S.getInstance(). If subsequent calls have arguments, a SingletonException is raised by default.
If you find it more convenient for subsequent calls to be allowed to have arguments, but for those argumentsto be ignored, just include ‘ignoreSubsequent = True’ in your class definition, i.e.:
class S(Singleton):
ignoreSubsequent = True
- def __init__(self, a, b=1):
- pass
5) For testing, it is sometimes convenient for all existing singleton instances to be forgotten, so that new instantiations can occur. For that reason, a forgetAllSingletons() function is included. Just call
forgetAllSingletons()
and it is as if no earlier instantiations have occurred.
6) As an implementation detail, classes that inherit from Singleton may not have their own __new__ methods. To make sure this requirement is followed, an exception is raised if a Singleton subclass includ es __new__. This happens at subclass instantiation time (by means of the MetaSingleton metaclass.
By Gary Robinson, grobinson@flyfi.com. No rights reserved – placed in the public domain – which is only reasonable considering how much it owes to other people’s code and ideas which are in the public domain. The idea of using a metaclass came from a comment on Gary’s blog (see http://www.garyrobinson.net/2004/03/python_singleto.html#comments). Other improvements came from comments and email from other people who saw it online. (See the blog post and comments for further credits.)
Not guaranteed to be fit for any particular purpose. Use at your own risk.
Bases: object
Call this to instantiate an instance or retrieve the existing instance. If the singleton requires args to be instantiated, include them the first time you call getInstance.
Bases: exceptions.Exception
Simple stats helper handling min, max, avg and callnumber stats. The Stats class is thread safe using a ReadWrite lock
Bases: concurrent.core.util.stats.StatsGatherer, concurrent.core.util.singletonmixin.Singleton
Singleton version of StatsGatherer class
Bases: object
Thread safe Stats gathere, handels average, min, max and call counts. Useful for benchmarking.
Usage:
# Just add a stat stats = Stats.getInstance() stats.add_avg(‘MyStat’,1)
# This will just give you a copy of the current stat print(stats.MyStat) {‘avg’:1,’min’:1,’max’:1,’n’:1}
usefull stuff for text conversations, unicode, tags, etc etc...
Low level console print method
Turn an exception to a unicode string
Convert a str object to an unicode object.
If charset is given, we simply assume that encoding for the text, but we’ll use the “replace” mode so that the decoding will always succeed. If charset is ‘’not’’ specified, we’ll make some guesses, first trying the UTF-8 encoding, then trying the locale preferred encoding, in “replace” mode. This differs from the unicode builtin, which by default uses the locale preferred encoding, in ‘strict’ mode, and is therefore prompt to raise `UnicodeDecodeError`s.
Because of the “replace” mode, the original content might be altered. If this is not what is wanted, one could map the original byte content by using an encoding which maps each byte of the input to an unicode character, e.g. by doing unicode(text, ‘iso-8859-1’).
Convert a string to UTF-8, assuming the encoding is either UTF-8, ISO Latin-1, or as specified by the optional charset parameter.
A unicode aware version of urllib.quote
A unicode aware version of urllib.quote
A unicode aware version of urllib.unquote.
Take str value previously obtained by unicode_quote.
Some usefull base methods and helpers
errorlevel, out (a string) and err (a string).
The optional input, which must be a str object, is first written to a temporary file from which the process will read.
(capturestderr may not work under Windows 9x.)
Example: print Popen3(‘grep spam’,’
here spam
‘).out
Bases: object
Holds information about ranges parsed from a string
>>> x = Ranges("1,2,9-15")
>>> 1 in x
True
>>> 5 in x
False
>>> 10 in x
True
>>> 16 in x
False
>>> [i for i in range(20) if i in x]
[1, 2, 9, 10, 11, 12, 13, 14, 15]
Also supports iteration, which makes that last example a bit simpler:
>>> list(x)
[1, 2, 9, 10, 11, 12, 13, 14, 15]
Note that it automatically reduces the list and short-circuits when the desired ranges are a relatively small portion of the entire set:
>>> x = Ranges("99")
>>> 1 in x # really fast
False
>>> x = Ranges("1, 2, 1-2, 2") # reduces this to 1-2
>>> x.pairs
[(1, 2)]
>>> x = Ranges("1-9,2-4") # handle ranges that completely overlap
>>> list(x)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
The members ‘a’ and ‘b’ refer to the min and max value of the range, and are None if the range is empty:
>>> x.a
1
>>> x.b
9
>>> e = Ranges()
>>> e.a, e.b
(None, None)
Empty ranges are ok, and ranges can be constructed in pieces, if you so choose:
>>> x = Ranges()
>>> x.appendrange("1, 2, 3")
>>> x.appendrange("5-9")
>>> x.appendrange("2-3") # reduce'd away
>>> list(x)
[1, 2, 3, 5, 6, 7, 8, 9]
‘’Code contributed by Tim Hatch’‘
Generate a properly escaped Content-Disposition header
Comparison function for natural order sorting based on http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/214202.
Return content number of lines before and after the specified lineno from the file identified by filename.
Returns a (lines_before, line, lines_after) tuple.
Get a dictionary containing package information for a package
dist can be either a Distribution instance or, as a shortcut, directly the module instance, if one can safely infer a Distribution instance from it.
Always returns a dictionary but it will be empty if no Distribution instance can be created for the given module.
Safe imports: rollback after a failed import.
Initially inspired from the RollbackImporter in PyUnit, but it’s now much simpler and works better for our needs.