本文整理汇总了Python中types.LambdaType方法的典型用法代码示例。如果您正苦于以下问题:Python types.LambdaType方法的具体用法?Python types.LambdaType怎么用?Python types.LambdaType使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类types
的用法示例。
在下文中一共展示了types.LambdaType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _get_calculated_size
# 需要导入模块: import types [as 别名]
# 或者: from types import LambdaType [as 别名]
def _get_calculated_size(self, size, data):
"""
Get's the final size of the field and runs the lambda functions
recursively until a final size is derived. If size is None then it
will just return the length of the data as it is assumed it is the
final field (None should only be set on size for the final field).
:param size: The size to calculate/expand
:param data: The data that the size is being calculated for
:return: The final size
"""
# if the size is derived from a lambda function, run it now; otherwise
# return the value we passed in or the length of the data if the size
# is None (last field value)
if size is None:
return len(data)
elif isinstance(size, types.LambdaType):
expanded_size = size(self.structure)
return self._get_calculated_size(expanded_size, data)
else:
return size
示例2: _parse_value
# 需要导入模块: import types [as 别名]
# 或者: from types import LambdaType [as 别名]
def _parse_value(self, value):
if value is None:
int_value = 0
elif isinstance(value, types.LambdaType):
int_value = value
elif isinstance(value, bytes):
format = self._get_struct_format(self.size, self.unsigned)
struct_string = "%s%s"\
% ("<" if self.little_endian else ">", format)
int_value = struct.unpack(struct_string, value)[0]
elif isinstance(value, integer_types):
int_value = value
else:
raise TypeError("Cannot parse value for field %s of type %s to "
"an int" % (self.name, type(value).__name__))
return int_value
示例3: __init__
# 需要导入模块: import types [as 别名]
# 或者: from types import LambdaType [as 别名]
def __init__(self, submit_config: submit.SubmitConfig, config_module: types.ModuleType = None, max_epoch: Any = None):
self.submit_config = submit_config
self.should_stop_flag = False
self.has_closed = False
self.start_time = time.time()
self.last_update_time = time.time()
self.last_update_interval = 0.0
self.max_epoch = max_epoch
# pretty print the all the relevant content of the config module to a text file
if config_module is not None:
with open(os.path.join(submit_config.run_dir, "config.txt"), "w") as f:
filtered_dict = {k: v for k, v in config_module.__dict__.items() if not k.startswith("_") and not isinstance(v, (types.ModuleType, types.FunctionType, types.LambdaType, submit.SubmitConfig, type))}
pprint.pprint(filtered_dict, stream=f, indent=4, width=200, compact=False)
# write out details about the run to a text file
self.run_txt_data = {"task_name": submit_config.task_name, "host_name": submit_config.host_name, "start_time": datetime.datetime.now().isoformat(sep=" ")}
with open(os.path.join(submit_config.run_dir, "run.txt"), "w") as f:
pprint.pprint(self.run_txt_data, stream=f, indent=4, width=200, compact=False)
示例4: __init__
# 需要导入模块: import types [as 别名]
# 或者: from types import LambdaType [as 别名]
def __init__(self,
interpreter, # type: NaturalLanguageInterpreter
policy_ensemble, # type: PolicyEnsemble
domain, # type: Domain
tracker_store, # type: TrackerStore
max_number_of_predictions=10, # type: int
message_preprocessor=None, # type: Optional[LambdaType]
on_circuit_break=None # type: Optional[LambdaType]
):
self.interpreter = interpreter
self.policy_ensemble = policy_ensemble
self.domain = domain
self.tracker_store = tracker_store
self.max_number_of_predictions = max_number_of_predictions
self.message_preprocessor = message_preprocessor
self.on_circuit_break = on_circuit_break
示例5: get_field_type
# 需要导入模块: import types [as 别名]
# 或者: from types import LambdaType [as 别名]
def get_field_type(self,field,name = None):
m = {
IntegerField : Integer,
FloatField : Float,
CharField : lambda field: String(length = field.length),
EnumField : lambda field: Enum(*field.enums,name = name,native_enum = field.native_enum),
TextField : Text,
BooleanField: Boolean,
BinaryField: LargeBinary,
DateField: Date,
DateTimeField: DateTime
}
for cls,t in m.items():
if isinstance(field,cls):
if isinstance(t,LambdaType):
return t(field)
return t
raise AttributeError("Invalid field type: %s" % field)
示例6: get_config
# 需要导入模块: import types [as 别名]
# 或者: from types import LambdaType [as 别名]
def get_config(self):
if isinstance(self.function, python_types.LambdaType):
function = func_dump(self.function)
function_type = 'lambda'
else:
function = self.function.__name__
function_type = 'function'
if isinstance(self._output_shape, python_types.LambdaType):
output_shape = func_dump(self._output_shape)
output_shape_type = 'lambda'
elif callable(self._output_shape):
output_shape = self._output_shape.__name__
output_shape_type = 'function'
else:
output_shape = self._output_shape
output_shape_type = 'raw'
config = {'function': function,
'function_type': function_type,
'output_shape': output_shape,
'output_shape_type': output_shape_type,
'arguments': self.arguments}
base_config = super(Lambda, self).get_config()
return dict(list(base_config.items()) + list(config.items()))
示例7: __init__
# 需要导入模块: import types [as 别名]
# 或者: from types import LambdaType [as 别名]
def __init__(self,
interpreter: NaturalLanguageInterpreter,
policy_ensemble: PolicyEnsemble,
domain: Domain,
tracker_store: TrackerStore,
generator: NaturalLanguageGenerator,
action_endpoint: Optional[EndpointConfig] = None,
max_number_of_predictions: int = 10,
message_preprocessor: Optional[LambdaType] = None,
on_circuit_break: Optional[LambdaType] = None,
):
self.interpreter = interpreter
self.nlg = generator
self.policy_ensemble = policy_ensemble
self.domain = domain
self.tracker_store = tracker_store
self.max_number_of_predictions = max_number_of_predictions
self.message_preprocessor = message_preprocessor
self.on_circuit_break = on_circuit_break
self.action_endpoint = action_endpoint
示例8: new_looper
# 需要导入模块: import types [as 别名]
# 或者: from types import LambdaType [as 别名]
def new_looper(a, arg=None):
"""Helper function for nest()
determines what sort of looper to make given a's type"""
if isinstance(a,types.TupleType):
if len(a) == 2:
return RangeLooper(a[0],a[1])
elif len(a) == 3:
return RangeLooper(a[0],a[1],a[2])
elif isinstance(a, types.BooleanType):
return BooleanLooper(a)
elif isinstance(a,types.IntType) or isinstance(a, types.LongType):
return RangeLooper(a)
elif isinstance(a, types.StringType) or isinstance(a, types.ListType):
return ListLooper(a)
elif isinstance(a, Looper):
return a
elif isinstance(a, types.LambdaType):
return CalcField(a, arg)
示例9: test_set_lambda
# 需要导入模块: import types [as 别名]
# 或者: from types import LambdaType [as 别名]
def test_set_lambda(self):
structure = self.StructureTest()
field = structure['field']
field.name = "field"
field.structure = self.StructureTest
field.set_value(lambda s: 4567)
expected = 4567
actual = field.get_value()
assert isinstance(field.value, types.LambdaType)
assert actual == expected
assert len(field) == 4
示例10: test_set_lambda_as_bytes
# 需要导入模块: import types [as 别名]
# 或者: from types import LambdaType [as 别名]
def test_set_lambda_as_bytes(self):
structure = self.StructureTest()
field = structure['field']
field.name = "field"
field.structure = self.StructureTest
field.set_value(lambda s: b"\x10\x11\x12\x13")
expected = [b"\x10\x11", b"\x12\x13"]
actual = field.get_value()
assert isinstance(field.value, types.LambdaType)
assert actual == expected
assert len(field) == 4
示例11: test_set_lambda_as_list
# 需要导入模块: import types [as 别名]
# 或者: from types import LambdaType [as 别名]
def test_set_lambda_as_list(self):
structure = self.StructureTest()
field = structure['field']
field.name = "field"
field.structure = self.StructureTest
field.set_value(lambda s: [b"\x10\x11", b"\x12\x13"])
expected = [b"\x10\x11", b"\x12\x13"]
actual = field.get_value()
assert isinstance(field.value, types.LambdaType)
assert actual == expected
assert len(field) == 4
示例12: __init__
# 需要导入模块: import types [as 别名]
# 或者: from types import LambdaType [as 别名]
def __init__(self, little_endian=True, default=None, size=None):
"""
The base class of a Field object. This contains the framework that a
field SHOULD implement in regards to packing and unpacking a value.
There should be little need to call this particular object as it is
designed to be a base class for *Type classes.
:param little_endian: When converting an int/uuid to bytes, the byte
order to pack as, False means it will be big endian
:param default: The default value of the field, this can be any
supported value such as as well as a lambda function or None
(default).
:param size: The size of the field, this can be an int, lambda function
or None (for variable length end field) unless overridden in Class
definition.
"""
field_type = self.__class__.__name__
self.little_endian = little_endian
if not (size is None or isinstance(size, integer_types) or
isinstance(size, types.LambdaType)):
raise InvalidFieldDefinition("%s size for field must be an int or "
"None for a variable length"
% field_type)
self.size = size
self.default = default
self.value = None
示例13: _get_struct_format
# 需要导入模块: import types [as 别名]
# 或者: from types import LambdaType [as 别名]
def _get_struct_format(self, size, unsigned=True):
"""
Get's the format specified for use in struct. This is only designed
for 1, 2, 4, or 8 byte values and will throw an exception if it is
anything else.
:param size: The size as an int
:return: The struct format specifier for the size specified
"""
if isinstance(size, types.LambdaType):
size = size(self.structure)
struct_format = {
1: 'B',
2: 'H',
4: 'L',
8: 'Q'
}
if size not in struct_format.keys():
raise InvalidFieldDefinition("Cannot struct format of size %s"
% size)
format_char = struct_format[size]
if not unsigned:
format_char = format_char.lower()
return format_char
示例14: _create_list_from_bytes
# 需要导入模块: import types [as 别名]
# 或者: from types import LambdaType [as 别名]
def _create_list_from_bytes(self, list_count, list_type, value):
# calculate the list_count and rerun method if a lambda
if isinstance(list_count, types.LambdaType):
list_count = list_count(self.structure)
return self._create_list_from_bytes(list_count, list_type, value)
list_value = []
for idx in range(0, list_count):
new_field = copy.deepcopy(list_type)
value = new_field.unpack(value)
list_value.append(new_field)
return list_value
示例15: __init__
# 需要导入模块: import types [as 别名]
# 或者: from types import LambdaType [as 别名]
def __init__(self, lambd):
assert isinstance(lambd, types.LambdaType)
self.lambd = lambd