本文整理匯總了Python中types.BuiltinMethodType方法的典型用法代碼示例。如果您正苦於以下問題:Python types.BuiltinMethodType方法的具體用法?Python types.BuiltinMethodType怎麽用?Python types.BuiltinMethodType使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類types
的用法示例。
在下文中一共展示了types.BuiltinMethodType方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: fileopen
# 需要導入模塊: import types [as 別名]
# 或者: from types import BuiltinMethodType [as 別名]
def fileopen(self, file):
import types
if repr(type(file)) != "<type 'file'>":
raise TypeError, 'posixfile.fileopen() arg must be file object'
self._file_ = file
# Copy basic file methods
for maybemethod in dir(file):
if not maybemethod.startswith('_'):
attr = getattr(file, maybemethod)
if isinstance(attr, types.BuiltinMethodType):
setattr(self, maybemethod, attr)
return self
#
# New methods
#
示例2: short_repr
# 需要導入模塊: import types [as 別名]
# 或者: from types import BuiltinMethodType [as 別名]
def short_repr(obj):
if isinstance(obj, (type, types.ModuleType, types.BuiltinMethodType,
types.BuiltinFunctionType)):
return obj.__name__
if isinstance(obj, types.MethodType):
try:
if obj.__self__ is not None:
return obj.__func__.__name__ + ' (bound)'
else:
return obj.__func__.__name__
except AttributeError:
# Python < 2.6 compatibility
if obj.im_self is not None:
return obj.im_func.__name__ + ' (bound)'
else:
return obj.im_func.__name__
if isinstance(obj, types.FrameType):
return '%s:%s' % (obj.f_code.co_filename, obj.f_lineno)
if isinstance(obj, (tuple, list, dict, set)):
return '%d items' % len(obj)
return repr(obj)[:40]
示例3: __new__
# 需要導入模塊: import types [as 別名]
# 或者: from types import BuiltinMethodType [as 別名]
def __new__(cls, name, bases, attrs):
if '__tainted__' in attrs:
raise AttributeError("__taint__ attribute is a special method for Taint metaclass")
if '__tainttags__' in attrs:
raise AttributeError("__tainttags__ attribute is a special method for Taint metaclass")
def taint_dec(func):
def _taint_dec(*args, **kwds):
tainted = 0
tags = []
for arg in args:
tainted = tainted or getattr(arg, '__tainted__', 0)
tags.extend(getattr(arg, '__tainttags__', ()))
for arg in kwds.itervalues():
tainted = tainted or getattr(arg, '__tainted__', 0)
tags.extend(getattr(arg, '__tainttags__', ()))
res = func(*args, **kwds)
res.__tainted__ = tainted
res.__tags__ = tags
return res
return _taint_dec
newattrs = {key: (taint_dec(val) if isinstance(val, (types.BuiltinMethodType, types.MethodType, types.FunctionType)) else val)
for key, val in attrs.iteritems()}
newattrs['__tainted__'] = 0
newattrs['__tainttags__'] = []
return super(Taint, cls).__new__(cls, name, bases, attrs)
示例4: _compute_fields_for_operation
# 需要導入模塊: import types [as 別名]
# 或者: from types import BuiltinMethodType [as 別名]
def _compute_fields_for_operation(self, fields, to_compute):
row = OpRow(self)
for name, tup in iteritems(fields):
field, value = tup
if isinstance(
value,
(
types.LambdaType,
types.FunctionType,
types.MethodType,
types.BuiltinFunctionType,
types.BuiltinMethodType,
),
):
value = value()
row.set_value(name, value, field)
for name, field in to_compute:
try:
row.set_value(name, field.compute(row), field)
except (KeyError, AttributeError):
# error silently unless field is required!
if field.required and name not in fields:
raise RuntimeError("unable to compute required field: %s" % name)
return row
示例5: safe_pickle
# 需要導入模塊: import types [as 別名]
# 或者: from types import BuiltinMethodType [as 別名]
def safe_pickle(the_object):
if type(the_object) is list:
return [safe_pickle(x) for x in the_object]
if type(the_object) is dict:
new_dict = dict()
for key, value in the_object.items():
new_dict[key] = safe_pickle(value)
return new_dict
if type(the_object) is set:
new_set = set()
for sub_object in the_object:
new_set.add(safe_pickle(sub_object))
return new_set
if type(the_object) in [types.ModuleType, types.FunctionType, TypeType, types.BuiltinFunctionType, types.BuiltinMethodType, types.MethodType, types.ClassType, FileType]:
return None
return the_object
示例6: short_repr
# 需要導入模塊: import types [as 別名]
# 或者: from types import BuiltinMethodType [as 別名]
def short_repr(obj):
if isinstance(obj, (type, types.ModuleType, types.BuiltinMethodType,
types.BuiltinFunctionType)):
return obj.__name__
if isinstance(obj, types.MethodType):
if obj.im_self is not None:
return obj.im_func.__name__ + ' (bound)'
else:
return obj.im_func.__name__
if isinstance(obj, (tuple, list, dict, set)):
return '%d items' % len(obj)
if isinstance(obj, weakref.ref):
return 'all_weakrefs_are_one'
return repr(obj)[:40]
示例7: _randbelow
# 需要導入模塊: import types [as 別名]
# 或者: from types import BuiltinMethodType [as 別名]
def _randbelow(self, n, int=int, maxsize=1<<BPF, type=type,
Method=_MethodType, BuiltinMethod=_BuiltinMethodType):
"Return a random int in the range [0,n). Raises ValueError if n==0."
getrandbits = self.getrandbits
# Only call self.getrandbits if the original random() builtin method
# has not been overridden or if a new getrandbits() was supplied.
if type(self.random) is BuiltinMethod or type(getrandbits) is Method:
k = n.bit_length() # don't use (n-1) here because n can be 1
r = getrandbits(k) # 0 <= r < 2**k
while r >= n:
r = getrandbits(k)
return r
# There's an overriden random() method but no new getrandbits() method,
# so we can only use random() from here.
random = self.random
if n >= maxsize:
_warn("Underlying random() generator does not supply \n"
"enough bits to choose from a population range this large.\n"
"To remove the range limitation, add a getrandbits() method.")
return int(random() * n)
rem = maxsize % n
limit = (maxsize - rem) / maxsize # int(limit * maxsize) % n == 0
r = random()
while r >= limit:
r = random()
return int(r*maxsize) % n
## -------------------- sequence methods -------------------
示例8: inspect_format_method
# 需要導入模塊: import types [as 別名]
# 或者: from types import BuiltinMethodType [as 別名]
def inspect_format_method(callable):
if not isinstance(callable, (types.MethodType,
types.BuiltinMethodType)) or \
callable.__name__ not in ('format', 'format_map'):
return None
obj = callable.__self__
if isinstance(obj, string_types):
return obj
示例9: inspect_format_method
# 需要導入模塊: import types [as 別名]
# 或者: from types import BuiltinMethodType [as 別名]
def inspect_format_method(callable):
if not isinstance(callable, (types.MethodType,
types.BuiltinMethodType)) or \
callable.__name__ != 'format':
return None
obj = callable.__self__
if isinstance(obj, string_types):
return obj
示例10: __getattr__
# 需要導入模塊: import types [as 別名]
# 或者: from types import BuiltinMethodType [as 別名]
def __getattr__(self, attr):
real = getattr(self.repo, attr)
if type(real) in [
types.FunctionType,
types.BuiltinFunctionType,
types.BuiltinMethodType,
]:
def fake(*args, **kwargs):
resp = real(*args, **kwargs)
if isinstance(resp, _pygit2.Walker):
resp = FakeWalker(resp)
elif isinstance(resp, _pygit2.Diff):
resp = FakeDiffer(resp)
return resp
return fake
elif isinstance(real, dict):
real_getitem = real.__getitem__
def fake_getitem(self, item):
return real_getitem(item)
real.__getitem__ = fake_getitem
return real
else:
return real
示例11: _randbelow
# 需要導入模塊: import types [as 別名]
# 或者: from types import BuiltinMethodType [as 別名]
def _randbelow(self, n, _log=_log, int=int, _maxwidth=1L<<BPF,
_Method=_MethodType, _BuiltinMethod=_BuiltinMethodType):
示例12: _randbelow
# 需要導入模塊: import types [as 別名]
# 或者: from types import BuiltinMethodType [as 別名]
def _randbelow(self, n, _log=_log, _int=int, _maxwidth=1L<<BPF,
_Method=_MethodType, _BuiltinMethod=_BuiltinMethodType):
示例13: _randbelow
# 需要導入模塊: import types [as 別名]
# 或者: from types import BuiltinMethodType [as 別名]
def _randbelow(self, n, int=int, maxsize=1<<BPF, type=type,
Method=_MethodType, BuiltinMethod=_BuiltinMethodType):
"Return a random int in the range [0,n). Raises ValueError if n==0."
random = self.random
getrandbits = self.getrandbits
# Only call self.getrandbits if the original random() builtin method
# has not been overridden or if a new getrandbits() was supplied.
if type(random) is BuiltinMethod or type(getrandbits) is Method:
k = n.bit_length() # don't use (n-1) here because n can be 1
r = getrandbits(k) # 0 <= r < 2**k
while r >= n:
r = getrandbits(k)
return r
# There's an overridden random() method but no new getrandbits() method,
# so we can only use random() from here.
if n >= maxsize:
_warn("Underlying random() generator does not supply \n"
"enough bits to choose from a population range this large.\n"
"To remove the range limitation, add a getrandbits() method.")
return int(random() * n)
rem = maxsize % n
limit = (maxsize - rem) / maxsize # int(limit * maxsize) % n == 0
r = random()
while r >= limit:
r = random()
return int(r*maxsize) % n
## -------------------- sequence methods -------------------
示例14: __iter__
# 需要導入模塊: import types [as 別名]
# 或者: from types import BuiltinMethodType [as 別名]
def __iter__(self):
""" iterator generator for public values/properties
It only returns the properties that are public.
"""
attributes = [attr for attr in dir(self)
if not attr.startswith('__') and \
not attr.startswith('_') and \
not isinstance(getattr(self, attr), (types.MethodType,
types.BuiltinFunctionType,
types.BuiltinMethodType))
]
for att in attributes:
yield (att, getattr(self, att))
#----------------------------------------------------------------------