Прикреплённый файл «nargs.py»

Загрузка

   1 #!/usr/bin/env python
   2 '''famework for program arguments passing via filename.
   3 '''
   4 import re, sys, os.path
   5 
   6 __Version=0.01
   7 __Types={
   8     "str":	r"'(?:[^']|\')*'|"+'"(?:[^"]|\")*"',
   9     "int":	'''-?[0-9]+''',
  10     "float":	'''-?[0-9]+[.][0-9]*(?:[Ee][+-][0-9]+)?''',
  11     }
  12 __Types["simple"]="|".join(__Types.values())
  13 __Tests={
  14     "str":	( re.compile(__Types["str"]),
  15       		  None ),
  16     "int":	( re.compile(__Types["int"]),
  17       		  None ),
  18     "float":	( re.compile(__Types["float"]),
  19       		  None ),
  20     "list":	( re.compile("("+__Types["simple"]+")+,("+__Types["simple"]+")?"),
  21       		  None ),
  22     "range":	( re.compile("("+__Types["int"]+")-("+__Types["int"]+")"),
  23       		  r"range(\1,\2)" ),
  24     }
  25 
  26 def SaferEval(Value):
  27   '''Check if Value can be converted through eval() safely
  28   Supported types:
  29   - Strings (single or double quoted)
  30   - Integers and fixed doubles
  31   - Floats
  32   - Comma separated lists
  33   - Dash separated ranges of integers (tuples)
  34   Return converted Value, Value itself otherwise'''
  35   for ex in __Tests:
  36     if __Tests[ex][0].match(Value):
  37       if __Tests[ex][1]:
  38 	return eval(__Tests[ex][0].replace(__Tests[ex][1],Value))
  39       else:
  40 	return eval(Value)
  41   else:
  42     return Value
  43 
  44 def SetVar(Globs, Val, Value):
  45   '''global Var = eval(Value) if it's safe, Value otherwise'''
  46   Globs[Val]=SaferEval(Value)
  47   return Value
  48 
  49 def SetVars(Globs, *pairs):
  50   '''Take sequence of tuples ((Variable, Value), ...)
  51   and set global variables to their values'''
  52   return [SetVar(Globs, *pair) for pair in pairs]
  53 
  54 def SetEqVars(Globs, pairs, Eq="="):
  55   '''Take sequence of strings ("Variable=Value",...) ("=" overrided by Eq) 
  56   and set global variables to their values'''
  57   valid = filter(lambda s: Eq in s, pairs)
  58   SetVars(Globs, *[pair.split(Eq)[:2] for pair in valid])
  59   return filter(lambda s: Eq not in s, pairs)
  60 
  61 def UseProgName(Globs, Sep=";", Eq="="):
  62   '''Parse program name for [<Sep>]Var<Eq>Value[<Sep>] entries and use them'''
  63   name = re.sub(r"[.][Pp][Yy].?$","",os.path.basename(sys.argv[0]))
  64   return Sep.join(SetEqVars(Globs, name.split(Sep), Eq))
  65 
  66 def UseProgArgs(Globs, Sep=";", Eq="="):
  67   '''Parse commandline for [<Sep>]Var<Eq>Value[<Sep>] entries and use them'''
  68   return SetEqVars(Globs, sys.argv[1:], Eq)
  69   
  70 def UseSpecialVars(Globs):
  71   '''Check for special variables in Globs and perform corresponded ations:
  72   OUT=filename		redirect standard output to filename
  73   ERR=filename		redirect standard error output to filename
  74   '''
  75   if "OUT" in Globs:	sys.stdout=file(Globs["OUT"],"w")
  76   if "ERR" in Globs:	sys.stderr=file(Globs["ERR"],"w")

Прикреплённые файлы

Для ссылки на прикреплённый файл в тексте страницы напишите attachment:имяфайла, как показано ниже в списке файлов. Не используйте URL из ссылки «[получить]», так как он чисто внутренний и может измениться.

Вам нельзя прикреплять файлы к этой странице.