本文整理匯總了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)