本文整理汇总了Python中functools.partialmethod方法的典型用法代码示例。如果您正苦于以下问题:Python functools.partialmethod方法的具体用法?Python functools.partialmethod怎么用?Python functools.partialmethod使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类functools
的用法示例。
在下文中一共展示了functools.partialmethod方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: contribute_to_class
# 需要导入模块: import functools [as 别名]
# 或者: from functools import partialmethod [as 别名]
def contribute_to_class(self, cls, *args, **kwargs):
super().contribute_to_class(cls, *args, **kwargs)
# Add order related methods to model
# Applying partialmethod() to already bound methods will retain self and add the model_instance bound to
subs = {'name': self.name, 'model': self.model.__name__.lower()}
setattr(cls, self.func_local_next % subs, partialmethod(self.get_next_or_previous_in_order, is_next=True))
setattr(cls, self.func_local_previous % subs, partialmethod(self.get_next_or_previous_in_order, is_next=False))
setattr(cls, self.func_local_get_set % subs, partialmethod(self.get_group_order))
setattr(cls, self.func_local_set_set % subs, partialmethod(self.set_group_order))
if self.unique_for_fields:
# Declare that this field has dependencies
self.has_dependencies = True
# Queue rest of work for when model is fully loaded
cls._meta.apps.lazy_model_operation(
self._lazy_contribute_to_class,
(cls._meta.app_label, cls._meta.model_name))
示例2: load_instruction
# 需要导入模块: import functools [as 别名]
# 或者: from functools import partialmethod [as 别名]
def load_instruction(cls, instruction, opcode, implementation):
try:
impl_name = implementation.__name__
except AttributeError:
if isinstance(implementation, functools.partialmethod):
rand = os.urandom(4).hex()
try:
impl_name = f"{implementation.func.__name__}_{rand}"
except AttributeError:
# TODO: WTF is happening here? Eg. when wrapping a MagicMock
impl_name = rand
else:
# TODO: WTF is happening here? Eg. with a MagicMock
impl_name = os.urandom(4).hex()
concrete_name = f"i_{instruction.__name__}_{impl_name}"
while concrete_name in cls.concrete_names:
concrete_name += os.urandom(4).hex()
cls.concrete_names.append(concrete_name)
setattr(cls, concrete_name, implementation)
cls.opcodes_names.setdefault(opcode, []).append(concrete_name)
示例3: __init__
# 需要导入模块: import functools [as 别名]
# 或者: from functools import partialmethod [as 别名]
def __init__(self):
self.opcodes = {
**{
0xD8F0 + i: P(self.fdiv, i=i, reverse=True)
for i in range(8)
},
**{
0xDCF8 + i: P(self.fdiv, i=i, reverse=False)
for i in range(8)
},
**{
0xDEF8 + i: P(self.fdivp, i=i)
for i in range(8)
},
}
示例4: __init__
# 需要导入模块: import functools [as 别名]
# 或者: from functools import partialmethod [as 别名]
def __init__(self):
self.opcodes = {
**{
o: self.r
for o in range(0x50, 0x58)
},
0xFF : self.rm,
0x6A : P(self.imm, _8bit=True),
0x68 : P(self.imm, _8bit=False),
0x0E : P(self.sreg, 'CS'),
0x16 : P(self.sreg, 'SS'),
0x1E : P(self.sreg, 'DS'),
0x06 : P(self.sreg, 'ES'),
0x0FA0: P(self.sreg, 'FS'),
0x0FA8: P(self.sreg, 'GS')
}
示例5: __init__
# 需要导入模块: import functools [as 别名]
# 或者: from functools import partialmethod [as 别名]
def __init__(self):
self.opcodes = {
0xEB: P(self.rel, _8bit=True, jump=_JMP),
0xE9: P(self.rel, _8bit=False, jump=_JMP),
0xFF: self.rm_m,
0xEA: P(self.ptr, _8bit=False),
0xE3: P(self.rel, _8bit=True, jump=JCXZ),
**{
opcode: P(self.rel, _8bit=True, jump=JUMPS[opcode % 0x70])
for opcode in range(0x70, 0x80)
},
**{
opcode: P(self.rel, _8bit=False, jump=JUMPS[opcode % 0x0F80])
for opcode in range(0x0F80, 0x0F90)
}
}
示例6: _get_function_source
# 需要导入模块: import functools [as 别名]
# 或者: from functools import partialmethod [as 别名]
def _get_function_source(func):
if _PY34:
func = inspect.unwrap(func)
elif hasattr(func, '__wrapped__'):
func = func.__wrapped__
if inspect.isfunction(func):
code = func.__code__
return (code.co_filename, code.co_firstlineno)
if isinstance(func, functools.partial):
return _get_function_source(func.func)
if _PY34 and isinstance(func, functools.partialmethod):
return _get_function_source(func.func)
return None
示例7: test_partialmethod_basic
# 需要导入模块: import functools [as 别名]
# 或者: from functools import partialmethod [as 别名]
def test_partialmethod_basic(check_lru, loop):
class Obj:
async def _coro(self, val):
return val
coro = alru_cache(partialmethod(_coro, 2), loop=loop)
obj = Obj()
coros = [obj.coro() for _ in range(5)]
check_lru(obj.coro, hits=0, misses=0, cache=0, tasks=0)
ret = await asyncio.gather(*coros, loop=loop)
check_lru(obj.coro, hits=4, misses=1, cache=1, tasks=0)
assert ret == [2, 2, 2, 2, 2]
示例8: test_partialmethod_partial
# 需要导入模块: import functools [as 别名]
# 或者: from functools import partialmethod [as 别名]
def test_partialmethod_partial(check_lru, loop):
class Obj:
def __init__(self):
self.coro = alru_cache(partial(self._coro, 2), loop=loop)
async def __coro(self, val1, val2):
return val1 + val2
_coro = partialmethod(__coro, 1)
obj = Obj()
coros = [obj.coro() for _ in range(5)]
check_lru(obj.coro, hits=0, misses=0, cache=0, tasks=0)
ret = await asyncio.gather(*coros, loop=loop)
check_lru(obj.coro, hits=4, misses=1, cache=1, tasks=0)
assert ret == [3, 3, 3, 3, 3]
示例9: test_partialmethod_cls_loop
# 需要导入模块: import functools [as 别名]
# 或者: from functools import partialmethod [as 别名]
def test_partialmethod_cls_loop(check_lru, loop):
class Obj:
def __init__(self, loop):
self._loop = loop
async def _coro(self, val):
return val
coro = alru_cache(partialmethod(_coro, 2), cls=True, loop='_loop')
obj = Obj(loop=loop)
coros = [obj.coro() for _ in range(5)]
check_lru(obj.coro, hits=0, misses=0, cache=0, tasks=0)
ret = await asyncio.gather(*coros, loop=loop)
check_lru(obj.coro, hits=4, misses=1, cache=1, tasks=0)
assert ret == [2, 2, 2, 2, 2]
示例10: test_partialmethod_kwargs_loop
# 需要导入模块: import functools [as 别名]
# 或者: from functools import partialmethod [as 别名]
def test_partialmethod_kwargs_loop(check_lru, loop):
class Obj:
async def _coro(self, val, *, _loop):
return val
coro = alru_cache(partialmethod(_coro, 2), kwargs=True, loop='_loop')
obj = Obj()
coros = [obj.coro(_loop=loop) for _ in range(5)]
check_lru(obj.coro, hits=0, misses=0, cache=0, tasks=0)
ret = await asyncio.gather(*coros, loop=loop)
check_lru(obj.coro, hits=4, misses=1, cache=1, tasks=0)
assert ret == [2, 2, 2, 2, 2]
示例11: contribute_to_related_class
# 需要导入模块: import functools [as 别名]
# 或者: from functools import partialmethod [as 别名]
def contribute_to_related_class(self, cls, field):
subs = {'name': self.name, 'model': self.model.__name__.lower(), 'remote_name': field.name}
setattr(cls, self.func_remote_get_set % subs, partialmethod(self.get_group_order, field=field))
setattr(cls, self.func_remote_set_set % subs, partialmethod(self.set_group_order, field=field))
示例12: make_foreign_order_accessors
# 需要导入模块: import functools [as 别名]
# 或者: from functools import partialmethod [as 别名]
def make_foreign_order_accessors(model, related_model):
setattr(
related_model,
'get_%s_order' % model.__name__.lower(),
partialmethod(method_get_order, model)
)
setattr(
related_model,
'set_%s_order' % model.__name__.lower(),
partialmethod(method_set_order, model)
)
########
# MISC #
########
示例13: partialclass
# 需要导入模块: import functools [as 别名]
# 或者: from functools import partialmethod [as 别名]
def partialclass(cls, *args, **kwds):
class NewCls(cls):
__init__ = functools.partialmethod(cls.__init__, *args, **kwds)
return NewCls
示例14: partialclass
# 需要导入模块: import functools [as 别名]
# 或者: from functools import partialmethod [as 别名]
def partialclass(cls, *args, **kwargs):
class PartialClass(cls):
__init__ = partialmethod(cls.__init__, *args, **kwargs)
return PartialClass
示例15: test_download_single
# 需要导入模块: import functools [as 别名]
# 或者: from functools import partialmethod [as 别名]
def test_download_single(run_cli, api, tmpdir, smallest_online_products, monkeypatch):
# Change default arguments for quicker test.
# Also, vcrpy is not threadsafe, so only one worker is used.
monkeypatch.setattr(
"sentinelsat.SentinelAPI.download_all",
partialmethod(SentinelAPI.download_all, n_concurrent_dl=1, max_attempts=2),
)
product_id = smallest_online_products[0]["id"]
command = ["--uuid", product_id, "--download", "--path", str(tmpdir)]
run_cli(*command)
# The file already exists, should not be re-downloaded
run_cli(*command)
# clean up
for f in tmpdir.listdir():
f.remove()
# Prepare a response with an invalid checksum
url = "https://scihub.copernicus.eu/apihub/odata/v1/Products('%s')?$format=json" % product_id
json = api.session.get(url).json()
json["d"]["Checksum"]["Value"] = "00000000000000000000000000000000"
# Force the download to fail by providing an incorrect checksum
with requests_mock.mock(real_http=True) as rqst:
rqst.get(url, json=json)
# md5 flag set (implicitly), should raise an exception
run_cli(*command, must_raise=InvalidChecksumError)
# clean up
tmpdir.remove()