MiscUtils.Funcs | index /usr/local/share/webware/MiscUtils/Funcs.py

`Funcs.py
Funcs.py, a member of MiscUtils, holds functions that don't fit in anywhere else.`

Modules
`` |  |
os random | sys tempfile | time |

Functions
`` |  | CharWrap(s, width, hanging=0) Commas(number) charWrap(s, width, hanging=0) `Word wrap a string. Return a new version of the string word wrapped with the given width and hanging indent. The font is assumed to be monospaced. This can be useful for including text between <pre> </pre> tags, since <pre> will not word wrap, and for lengthly lines, will increase the width of a web page. It can also be used to help delineate the entries in log-style output by passing hanging=4.` commas(number) `Insert commas in a number. Return the given number as a string with commas to separate the thousands positions. The number can be a float, int, long or string. Returns None for None.` dateForEmail(now=None) `Return a properly formatted date/time string for email messages.` excstr(e) `Return a string for the exception. The string will be in the format that Python normally outputs in interactive shells and such: <ExceptionName>: <message> AttributeError: 'object' object has no attribute 'bar' Neither str(e) nor repr(e) do that.` hostName() `Return the host name. The name is taken first from the os environment and failing that, from the 'hostname' executable. May return None if neither attempt succeeded. The environment keys checked are HOST and HOSTNAME, both upper and lower case.` localIP(remote=('www.yahoo.com', 80), useCache=1) `Get the "public" address of the local machine. This is the address which is connected to the general Internet. This function connects to a remote HTTP server the first time it is invoked (or every time it is invoked with useCache=0). If that is not acceptable, pass remote=None, but be warned that the result is less likely to be externally visible. Getting your local ip is actually quite complex. If this function is not serving your needs then you probably need to think deeply about what you really want and how your network is really set up. Search comp.lang.python for "local ip" for more information. http://groups.google.com/groups?q=%22local+ip%22+group:comp.lang.python.*` md5 = openssl_md5(...) `Returns a md5 hash object; optionally initialized with a string` positive_id(obj) `Return id(obj) as a non-negative integer.` safeDescription(x, what='what') `Return the repr() of x and its class (or type) for help in debugging. A major benefit here is that exceptions from repr() are consumed. This is important in places like "assert" where you don't want to lose the assertion exception in your attempt to get more information. Example use: assert isinstance(foo, Foo), safeDescription(foo) print "foo:", safeDescription(foo) # won't raise exceptions # better output format: assert isinstance(foo, Foo), safeDescription(foo, 'foo') print safeDescription(foo, 'foo')` sha1 = openssl_sha1(...) `Returns a sha1 hash object; optionally initialized with a string` timestamp(numSecs=None) `Return a dictionary whose keys give different versions of the timestamp. The dictionary will contain the following timestamp versions: 'numSecs': the number of seconds 'tuple': (year, month, day, hour, min, sec) 'pretty': 'YYYY-MM-DD HH:MM:SS' 'condensed': 'YYYYMMDDHHMMSS' 'dashed': 'YYYY-MM-DD-HH-MM-SS' The focus is on the year, month, day, hour and second, with no additional information such as timezone or day of year. This form of timestamp is often ideal for print statements, logs and filenames. If the current number of seconds is not passed, then the current time is taken. The 'pretty' format is ideal for print statements, while the 'condensed' and 'dashed' formats are generally more appropriate for filenames.` uniqueId(forObject=None, sha=False) `Generate an opaque, identifier string. The string is practically guaranteed to be unique If an object is passed, then its id() is incorporated into the generation. Returns a 32 character long string relying on md5 or, if sha is True, a 40 character long string relying on sha-1.` valueForString(s) `Return value for a string. For a given string, returns the most appropriate Pythonic value such as None, a long, an int, a list, etc. If none of those make sense, then returns the string as-is. "None", "True" and "False" are case-insensitive because there is already too much case sensitivity in computing, damn it!` wordWrap(s, width=78) `Return a version of the string word wrapped to the given width. Respects existing newlines in the string. Taken from: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/148061`