-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
attic.py
167 lines (118 loc) · 4.82 KB
/
attic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# encoding: utf-8
"""
Older utilities that are not being used.
WARNING: IF YOU NEED TO USE ONE OF THESE FUNCTIONS, PLEASE FIRST MOVE IT
TO ANOTHER APPROPRIATE MODULE IN IPython.utils.
"""
#-----------------------------------------------------------------------------
# Copyright (C) 2008-2011 The IPython Development Team
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
import sys
import warnings
from IPython.utils.warn import warn
#-----------------------------------------------------------------------------
# Code
#-----------------------------------------------------------------------------
def mutex_opts(dict,ex_op):
"""Check for presence of mutually exclusive keys in a dict.
Call: mutex_opts(dict,[[op1a,op1b],[op2a,op2b]...]"""
for op1,op2 in ex_op:
if op1 in dict and op2 in dict:
raise ValueError('\n*** ERROR in Arguments *** '\
'Options '+op1+' and '+op2+' are mutually exclusive.')
class EvalDict:
"""
Emulate a dict which evaluates its contents in the caller's frame.
Usage:
>>> number = 19
>>> text = "python"
>>> print "%(text.capitalize())s %(number/9.0).1f rules!" % EvalDict()
Python 2.1 rules!
"""
# This version is due to sismex01@hebmex.com on c.l.py, and is basically a
# modified (shorter) version of:
# http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66018 by
# Skip Montanaro (skip@pobox.com).
def __getitem__(self, name):
frame = sys._getframe(1)
return eval(name, frame.f_globals, frame.f_locals)
EvalString = EvalDict # for backwards compatibility
def all_belong(candidates,checklist):
"""Check whether a list of items ALL appear in a given list of options.
Returns a single 1 or 0 value."""
return 1-(0 in [x in checklist for x in candidates])
def belong(candidates,checklist):
"""Check whether a list of items appear in a given list of options.
Returns a list of 1 and 0, one for each candidate given."""
return [x in checklist for x in candidates]
def with_obj(object, **args):
"""Set multiple attributes for an object, similar to Pascal's with.
Example:
with_obj(jim,
born = 1960,
haircolour = 'Brown',
eyecolour = 'Green')
Credit: Greg Ewing, in
http://mail.python.org/pipermail/python-list/2001-May/040703.html.
NOTE: up until IPython 0.7.2, this was called simply 'with', but 'with'
has become a keyword for Python 2.5, so we had to rename it."""
object.__dict__.update(args)
def map_method(method,object_list,*argseq,**kw):
"""map_method(method,object_list,*args,**kw) -> list
Return a list of the results of applying the methods to the items of the
argument sequence(s). If more than one sequence is given, the method is
called with an argument list consisting of the corresponding item of each
sequence. All sequences must be of the same length.
Keyword arguments are passed verbatim to all objects called.
This is Python code, so it's not nearly as fast as the builtin map()."""
out_list = []
idx = 0
for object in object_list:
try:
handler = getattr(object, method)
except AttributeError:
out_list.append(None)
else:
if argseq:
args = map(lambda lst:lst[idx],argseq)
#print 'ob',object,'hand',handler,'ar',args # dbg
out_list.append(handler(args,**kw))
else:
out_list.append(handler(**kw))
idx += 1
return out_list
def import_fail_info(mod_name,fns=None):
"""Inform load failure for a module."""
if fns == None:
warn("Loading of %s failed." % (mod_name,))
else:
warn("Loading of %s from %s failed." % (fns,mod_name))
class NotGiven: pass
def popkey(dct,key,default=NotGiven):
"""Return dct[key] and delete dct[key].
If default is given, return it if dct[key] doesn't exist, otherwise raise
KeyError. """
try:
val = dct[key]
except KeyError:
if default is NotGiven:
raise
else:
return default
else:
del dct[key]
return val
def wrap_deprecated(func, suggest = '<nothing>'):
def newFunc(*args, **kwargs):
warnings.warn("Call to deprecated function %s, use %s instead" %
( func.__name__, suggest),
category=DeprecationWarning,
stacklevel = 2)
return func(*args, **kwargs)
return newFunc