Документ взят из кэша поисковой машины. Адрес оригинального документа : http://kodomo.fbb.msu.ru/hg/allpy/file/8074b60331a6/allpy/util.py
Дата изменения: Unknown
Дата индексирования: Sun Feb 3 21:31:58 2013
Кодировка:
allpy: 8074b60331a6 allpy/util.py

allpy

view allpy/util.py @ 1070:8074b60331a6

pair-cores/web: Alignment is now required input field
author Boris Nagaev <bnagaev@gmail.com>
date Thu, 17 May 2012 02:02:33 +0400
parents 7b2a37ff1662
children 08d892230e8c 87ba56d634cf
line source
1 """Miscellanous utilities.
2 """
3 import sys
4 import warnings
6 def unzip(seq):
7 """The oppozite of zip() builtin."""
8 a, b = [], []
9 for x, y in seq:
10 a.append(x)
11 b.append(y)
12 return a, b
14 def open(filename, mode='r'):
15 """Open file. Return stdin/stdout for filename '-'."""
16 if filename == '-':
17 if 'w' in mode or 'a' in mode or '+' in mode:
18 return sys.stdout
19 if 'r' in mode:
20 return sys.stdin
21 raise AssertionError("Unknown file mode: %s" % mode)
22 else:
23 return file(filename, mode)
25 def remove_each(string, substrings):
26 """Remove each of substrings from string."""
27 for sub in substrings:
28 string = string.replace(sub, "")
29 return string
31 class UserDict(dict):
32 """Clone of dict that user may add attributes to."""
33 pass
35 class UserList(list):
36 """Clone of list that user may add attributes to."""
37 pass
39 class UserString(str):
40 """Clone of str that user may add attributes to."""
41 pass
43 def deprecated(message):
44 """Warn about function being deprecated."""
45 warnings.warn(message, DeprecationWarning, stacklevel=2)
47 class lazy_import(object):
48 """Lazy module import to help breaking bad import loops."""
49 def __init__(self, name, globals={}, locals={}, fromlist=[], level=-1):
50 self.name = name
51 self.globals = globals
52 self.locals = locals
53 self.fromlist = fromlist
54 self.level = level
55 def __getattr__(self, attr):
56 self.module = __import__(
57 self.name, self.globals, self.locals, self.fromlist, self.level
58 )
59 self.__dict__ = self.module.__dict__
60 return self.__dict__[attr]
62 class Binder(object):
63 def __init__(self, func, *args, **kwargs):
64 self.func = func
65 self.args = args
66 self.kwargs = kwargs
67 def __call__(self):
68 try:
69 self.func(*self.args, **self.kwargs)
70 except Exception, e:
71 sys.stderr.write("%s: %s\n" % (e.__class__, e))
73 # vim: set et ts=4 sts=4 sw=4: