本文整理汇总了Python中pyrsistent.PClass方法的典型用法代码示例。如果您正苦于以下问题:Python pyrsistent.PClass方法的具体用法?Python pyrsistent.PClass怎么用?Python pyrsistent.PClass使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyrsistent
的用法示例。
在下文中一共展示了pyrsistent.PClass方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_backend_selection
# 需要导入模块: import pyrsistent [as 别名]
# 或者: from pyrsistent import PClass [as 别名]
def test_backend_selection(self):
"""
``AgentService.get_api`` returns an object constructed by the
factory corresponding to the agent's ``backend_description``,
supplying ``api_args``.
"""
class API(PClass):
a = field()
b = field()
agent_service = self.agent_service.set(
"backend_description",
BackendDescription(
name=u"foo", needs_reactor=False, needs_cluster_id=False,
api_factory=API, deployer_type=DeployerType.p2p,
),
).set(
"api_args", {"a": "x", "b": "y"},
)
api = agent_service.get_api()
self.assertEqual(
API(a="x", b="y"),
api,
)
示例2: test_needs_cluster_id
# 需要导入模块: import pyrsistent [as 别名]
# 或者: from pyrsistent import PClass [as 别名]
def test_needs_cluster_id(self):
"""
If the flag for needing a cluster id as an extra argument is set in the
backend_description, the ``AgentService`` passes the cluster id
extracted from the node certificate when ``AgentService.get_api`` calls
the backend factory.
"""
class API(PClass):
cluster_id = field()
agent_service = self.agent_service.set(
"backend_description",
BackendDescription(
name=u"foo",
needs_reactor=False, needs_cluster_id=True,
api_factory=API, deployer_type=DeployerType.p2p,
),
)
api = agent_service.get_api()
self.assertEqual(
API(cluster_id=self.ca_set.node.cluster_uuid),
api,
)
示例3: new_record_type
# 需要导入模块: import pyrsistent [as 别名]
# 或者: from pyrsistent import PClass [as 别名]
def new_record_type(name, headings):
return type(
name,
(PClass,),
{heading: field(type=unicode, mandatory=True) for heading in headings}
)
示例4: filter_for_pclass
# 需要导入模块: import pyrsistent [as 别名]
# 或者: from pyrsistent import PClass [as 别名]
def filter_for_pclass(pclass_type, pclass_kwargs):
pclass_fields = pclass_type._pclass_fields
# Does the pclass support all the proposed kwargs
unexpected_keys = set(
pclass_kwargs.keys()
).difference(
set(pclass_fields.keys())
)
if unexpected_keys:
raise ValueError(
"Unexpected keyword arguments for '{}'. "
"Found: {}. "
"Expected: {}.".format(
pclass_type.__name__,
list(unexpected_keys),
pclass_fields.keys(),
)
)
fields_to_validate = {
key: field
for key, field
in pclass_fields.items()
if key in pclass_kwargs
}
filter_type = type(
"Filter_{}".format(pclass_type.__name__),
(PClass,),
fields_to_validate
)
return filter_type(**pclass_kwargs)
示例5: _to_serializables
# 需要导入模块: import pyrsistent [as 别名]
# 或者: from pyrsistent import PClass [as 别名]
def _to_serializables(obj):
"""
This function turns assorted types into serializable objects (objects that
can be serialized by the default JSON encoder). Note that this is done
shallowly for containers. For example, ``PClass``es will be turned into
dicts, but the values and keys of the dict might still not be serializable.
It is up to higher layers to traverse containers recursively to achieve
full serialization.
:param obj: The object to serialize.
:returns: An object that is shallowly JSON serializable.
"""
if isinstance(obj, PRecord):
result = dict(obj)
result[_CLASS_MARKER] = obj.__class__.__name__
return result
elif isinstance(obj, PClass):
result = obj._to_dict()
result[_CLASS_MARKER] = obj.__class__.__name__
return result
elif isinstance(obj, PMap):
return {_CLASS_MARKER: u"PMap", u"values": dict(obj).items()}
elif isinstance(obj, (PSet, PVector, set)):
return list(obj)
elif isinstance(obj, FilePath):
return {_CLASS_MARKER: u"FilePath",
u"path": obj.path.decode("utf-8")}
elif isinstance(obj, UUID):
return {_CLASS_MARKER: u"UUID",
"hex": unicode(obj)}
elif isinstance(obj, datetime):
if obj.tzinfo is None:
raise ValueError(
"Datetime without a timezone: {}".format(obj))
return {_CLASS_MARKER: u"datetime",
"seconds": timegm(obj.utctimetuple())}
return obj
示例6: _is_pyrsistent
# 需要导入模块: import pyrsistent [as 别名]
# 或者: from pyrsistent import PClass [as 别名]
def _is_pyrsistent(obj):
"""
Boolean check if an object is an instance of a pyrsistent object.
"""
return isinstance(obj, (PRecord, PClass, PMap, PSet, PVector))
示例7: make_generation_hash
# 需要导入模块: import pyrsistent [as 别名]
# 或者: from pyrsistent import PClass [as 别名]
def make_generation_hash(x):
"""
Creates a ``GenerationHash`` for a given argument.
Simple helper to call ``generation_hash`` and wrap it in the
``GenerationHash`` ``PClass``.
:param x: The object to hash.
:returns: The ``GenerationHash`` for the object.
"""
return GenerationHash(
hash_value=generation_hash(x)
)
示例8: test_needs_reactor
# 需要导入模块: import pyrsistent [as 别名]
# 或者: from pyrsistent import PClass [as 别名]
def test_needs_reactor(self):
"""
If the flag for needing a reactor as an extra argument is set in the
backend_description, the ``AgentService`` passes its own reactor when
``AgentService.get_api`` calls the backend factory.
"""
reactor = MemoryCoreReactor()
class API(PClass):
reactor = field()
agent_service = self.agent_service.set(
"backend_description",
BackendDescription(
name=u"foo",
needs_reactor=True, needs_cluster_id=False,
api_factory=API, deployer_type=DeployerType.p2p,
),
).set(
"reactor", reactor,
)
api = agent_service.get_api()
self.assertEqual(
API(reactor=reactor),
api,
)
示例9: test_required_config
# 需要导入模块: import pyrsistent [as 别名]
# 或者: from pyrsistent import PClass [as 别名]
def test_required_config(self):
"""
A ``UsageError`` is raised if the loaded configuration for the API
factory does not contain a key in the corresponding backend's required
configuration keys.
"""
class API(PClass):
region = field()
api_key = field()
agent_service = self.agent_service.set(
"backend_description",
BackendDescription(
name=u"foo",
needs_reactor=False, needs_cluster_id=False,
api_factory=API, deployer_type=DeployerType.p2p,
required_config={u"region", u"api_key"},
),
)
agent_service = agent_service.set("api_args", {
"region": "abc",
})
error = self.assertRaises(UsageError, agent_service.get_api)
self.assertEqual(
error.message,
u'Configuration error: Required key api_key is missing.'
)
示例10: interface_field
# 需要导入模块: import pyrsistent [as 别名]
# 或者: from pyrsistent import PClass [as 别名]
def interface_field(interfaces, **field_kwargs):
"""
A ``PClass`` field which checks that the assigned value provides all the
``interfaces``.
:param tuple interfaces: The ``Interface`` that a value must provide.
"""
if not isinstance(interfaces, tuple):
raise TypeError(
"The ``interfaces`` argument must be a tuple. "
"Got: {!r}".format(interfaces)
)
original_invariant = field_kwargs.pop("invariant", None)
def invariant(value):
error_messages = []
if original_invariant is not None:
(original_invariant_result,
_original_invariant_message) = original_invariant(value)
if original_invariant_result:
error_messages.append(original_invariant_result)
missing_interfaces = []
for interface in interfaces:
if not interface.providedBy(value):
missing_interfaces.append(interface.getName())
if missing_interfaces:
error_messages.append(
"The value {!r} "
"did not provide these required interfaces: {}".format(
value,
", ".join(missing_interfaces)
)
)
if error_messages:
return (False, "\n".join(error_messages))
else:
return (True, "")
field_kwargs["invariant"] = invariant
return field(**field_kwargs)