Created
December 18, 2010 19:21
-
-
Save khaije1/746773 to your computer and use it in GitHub Desktop.
fun w/ Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Pythonic Switch Statement | |
i'm going to have fun using this to dynamically construct a database-backend abstraction using my atomicnode concept... Yay! | |
Dictionary trees own. HAHA I love them! | |
""" | |
class switchdict(): | |
""" | |
a pythonic form of the switch statement, more traditionally expressed as... | |
""" | |
def __init__(self , initdict = None, default = None): | |
self.index = { None : default } | |
if initdict: | |
self.index.update(initdict) | |
def addentry(self, key, action): | |
try: | |
self.index[ key ] = action | |
except: | |
#<<: | |
print "unable to add entry" | |
def eval(self,key): | |
try: | |
if 'function' != type(self.index[key]): | |
self.index[key]() | |
except: | |
#<<: | |
print "key not found: returning default" | |
self.index[None] | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env python | |
# -*- coding: utf-8 -*- | |
""" | |
template.py | |
""" | |
#------------------------------------------------------------------------------ | |
# imports (system) | |
#------------------------------------------------------------------------------ | |
import sys, os | |
import time, logging | |
#------------------------------------------------------------------------------ | |
# imports (custom) | |
#------------------------------------------------------------------------------ | |
#------------------------------------------------------------------------------ | |
# classes | |
#------------------------------------------------------------------------------ | |
#------------------------------------------------------------------------------ | |
# functions | |
#------------------------------------------------------------------------------ | |
## handle environment | |
def setup_environ(): | |
""" | |
define different behavior based on environment (http://stackoverflow.com/questions/9850220/how-does-a-python-program-tell-if-its-being-run-within-emacs) can be abstracted for dev/lab/test/prod handling | |
""" | |
for e in os.environ: | |
if 'EMACS' in e: | |
print e, os.environ[e] | |
print "context: dev" | |
## handle input | |
def setup_output(infile, outfile): | |
if len(args) > 1: | |
parser.print_help() | |
sys.exit(1) | |
elif len(args) == 0 or args[0] == "-": | |
infilename = "" | |
if sys.stdin.isatty(): | |
# True if the file is connected to a tty device, and False | |
# otherwise (pipeline or file redirection). | |
parser.print_help() | |
sys.exit(1) | |
else: | |
# Handle piped input ($ cat ehv3.raw | pylon | rst2pdf -o ans.pdf). | |
infile = sys.stdin | |
else: | |
infilename = args[0] | |
infile = open(infilename) | |
## handle ouput | |
def setup_output(infile, outfile): | |
if options.output: | |
outfile = options.output | |
if outfile == "-": | |
outfile = sys.stdout | |
logger.info("logger level set to critical as output is to stdout") | |
logger.setLevel(logging.CRITICAL) # we must stay quiet | |
else: | |
outfile = sys.stdout | |
logger.info("logger level set to critical as output is to stdout") | |
logger.setLevel(logging.CRITICAL) # we must stay quiet | |
## handle options | |
def setup_options(): | |
optParser = optparse.OptionParser("usage: program [options] input_file") | |
optParser.add_option("-o", "--output", dest="output", metavar="FILE", help="Write the solution report to FILE.") | |
optParser.add_option("-q", "--quiet", action="store_true", dest="quiet", default=False, help="Print less information.") | |
optParser.add_option("-d", "--debug", action="store_true", dest="debug", default=False, help="Print debug information.") | |
return optParser | |
## handle logging | |
def setup_logging( logname ): | |
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG, format="%(levelname)s: %(message)s") | |
logger = logging.getLogger( logname ) | |
return logger | |
## | |
def main2(infile, outfile): | |
psat(infile).write(open(outfile,"w")) | |
#------------------------------------------------------------------------------ | |
# "main" function: | |
#------------------------------------------------------------------------------ | |
def main(): | |
""" Parses the command line and call with the correct data. | |
""" | |
setup_options() | |
setup_logging( __name__ ) | |
(options, args) = parser.parse_args() | |
if options.quiet: | |
logger.info("setting logger level to critical") | |
logger.setLevel(logging.CRITICAL) | |
elif options.debug: | |
logger.info("setting logger level to debug") | |
logger.setLevel(logging.DEBUG) | |
else: | |
logger.info("setting logger level to info") | |
logger.setLevel(logging.INFO) | |
# Output. | |
if options.output: | |
outfile = options.output | |
if outfile == "-": | |
outfile = sys.stdout | |
logger.info("logger level set to critical as output is to stdout") | |
logger.setLevel(logging.CRITICAL) # we must stay quiet | |
else: | |
outfile = sys.stdout | |
logger.info("logger level set to critical as output is to stdout") | |
logger.setLevel(logging.CRITICAL) # we must stay quiet | |
# Input. | |
if len(args) > 1: | |
parser.print_help() | |
sys.exit(1) | |
elif len(args) == 0 or args[0] == "-": | |
infilename = "" | |
if sys.stdin.isatty(): | |
# True if the file is connected to a tty device, and False | |
# otherwise (pipeline or file redirection). | |
parser.print_help() | |
sys.exit(1) | |
else: | |
# Handle piped input ($ cat ehv3.raw | pylon | rst2pdf -o ans.pdf). | |
infile = sys.stdin | |
else: | |
infilename = args[0] | |
infile = open(infilename) | |
logger.info("Running Program with: %s" % infilename) | |
main2(infile, outfile) | |
if __name__ == "__main__": | |
main() | |
else: | |
logger.info("Running as Module") | |
sys.exit(0) | |
# EOF ------------------------------------------------------------------------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment