[Assorted-commits] SF.net SVN: assorted:[984] python-commons/trunk/src/commons/misc.py
Brought to you by:
yangzhang
From: <yan...@us...> - 2008-10-02 23:22:13
|
Revision: 984 http://assorted.svn.sourceforge.net/assorted/?rev=984&view=rev Author: yangzhang Date: 2008-10-02 22:18:03 +0000 (Thu, 02 Oct 2008) Log Message: ----------- reorganized/cleaned; fixed some minor bugs; added sendmail Modified Paths: -------------- python-commons/trunk/src/commons/misc.py Modified: python-commons/trunk/src/commons/misc.py =================================================================== --- python-commons/trunk/src/commons/misc.py 2008-10-02 19:31:12 UTC (rev 983) +++ python-commons/trunk/src/commons/misc.py 2008-10-02 22:18:03 UTC (rev 984) @@ -6,27 +6,50 @@ """ __all__ = ''' +TerminalController days +default_if_none generate_bit_fields +remove_colors +run +sendmail +seq +settimeout +tc +timeout_exception wall_clock -default_if_none -seq -run -TerminalController -remove_colors wrap_color '''.split() -from contextlib import * -from subprocess import CalledProcessError, PIPE, Popen -from time import * +# +# Email +# + +import smtplib + +def sendmail(sender, to, subj, body): + msg = "From: %s\r\nTo: %s\r\nSubject:%s\r\n\r\n%s" % (sender, to, subj, body) + server = smtplib.SMTP('localhost') + try: + server.set_debuglevel(False) + server.sendmail(sender, to, msg) + finally: + server.quit() + +# +# Date/Time +# + from datetime import timedelta -import sys, re def days(td): """Returns the ceil(days in the timedelta L{td}).""" return td.days + (1 if td - timedelta(days = td.days) > timedelta() else 0) +# +# Bit fields +# + def generate_bit_fields(count): """ A generator of [2^i] for i from 0 to (count - 1). Useful for, @@ -44,6 +67,14 @@ yield j j <<= 1 +# +# Timing +# + +from time import * +from contextlib import * +from signal import * + @contextmanager def wall_clock(output): """ @@ -66,6 +97,21 @@ end = time() output[0] = end - start +class timeout_exception(Exception): pass + +def settimeout(secs): + """ + Set the signal handler for SIGALRM to raise timeout_exception, and call + alarm(secs). + """ + def handle(sig, frame): raise timeout_exception() + signal(SIGALRM, handle) + alarm(secs) + +# +# Functional +# + def default_if_none(x, d): """ Returns L{x} if it's not None, otherwise returns L{d}. @@ -80,17 +126,30 @@ f() return g() +# +# Processes +# + +from subprocess import CalledProcessError, PIPE, Popen + def run(cmd): """ Run the given command (a list of program and argument strings) and return the stdout as a string, raising a L{CalledProcessError} if the program exited - with a non-zero status. + with a non-zero status. Different from check_call because I return the + (piped) stdout. """ p = Popen(cmd, stdout=PIPE) stdout = p.communicate()[0] if p.returncode != 0: raise CalledProcessError(p.returncode, cmd) return stdout +# +# Terminal ANSI coloring +# + +import sys, re + class TerminalController: """ From U{http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/475116}. @@ -243,11 +302,12 @@ 'Removes ANSI escape codes (e.g. those for terminal colors).' return remove_colors_pat.sub('', s) -def wrap_color(s, color): +tc = TerminalController() +def wrap_color(s, color, tc = tc): """ Wraps L{s} in L{color} (resets color to NORMAL at the end). """ - return '${%s}%s${NORMAL}' % (s, color) + return tc.render( '${%s}%s${NORMAL}' % (color, s) ) import unittest This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |