How to use property_factory method in autotest

Best Python code snippet using autotest_python

__init__.py

Source:__init__.py Github

copy

Full Screen

...81 data = appstruct.copy()82 property_factory = content._property_factory83 for i, node in enumerate(schema):84 name = node.name85 props[name] = prop = property_factory(content, node)86 try:87 prop.__set__(self, data.pop(name, colander.null))88 except colander.Invalid, e:89 if error is None:90 error = colander.Invalid(schema)91 error.add(e, i)92 if error is not None:93 raise error94 if data:95 raise TypeError(96 "Unexpected keyword argument(s): %s" % repr(data))97 self.__dict__['__schema__'] = schema98 self.__dict__['_props'] = props99 def __getattr__(self, name):100 prop = self._props.get(name, None)101 if prop is None:102 raise AttributeError(name)103 return prop.__get__(self)104 def __setattr__(self, name, value):105 props = self.__dict__.get('_props')106 if props is not None:107 prop = props.get(name)108 if prop is not None:109 return prop.__set__(self, value)110 return super(_MappingNode, self).__setattr__(name, value)111 def appstruct(self):112 return dict([(name, _appstruct_node(prop.__get__(self))) for113 name, prop in self._props.items()])114class _SequenceNodeProperty(_LeafNodeProperty):115 def _validate(self, content, value):116 if value is colander.null:117 value = []118 return content._SequenceNode(content, self.node, value)119class _SequenceNode(object):120 _data_type = list121 def __init__(self, content, schema, appstruct):122 # XXX calls private colander api.123 self.__content__ = content124 schema.typ._validate(schema, appstruct, schema.typ.accept_scalar)125 self.__schema__ = schema126 property_factory = content._property_factory127 self._prop = prop = property_factory(content, schema.children[0])128 data = self._data_type()129 error = None130 for i, item in enumerate(appstruct):131 try:132 data.append(content._SequenceItem(content, prop, item))133 except colander.Invalid, e:134 if error is None:135 error = colander.Invalid(schema)136 error.add(e, i)137 if error is not None:138 raise error139 self._data = data140 def __getitem__(self, index):141 return self._data[index].get()142 def __setitem__(self, index, value):143 content = self.__content__144 self._data[index] = content._SequenceItem(content, self._prop, value)145 def __delitem__(self, index):146 del self._data[index]147 def __iter__(self):148 for item in self._data:149 yield item.get()150 def __cmp__(self, right):151 return cmp(list(self), right)152 def __repr__(self):153 return repr(list(self))154 def append(self, item):155 content = self.__content__156 self._data.append(157 content._SequenceItem(content, self._prop, item))158 def extend(self, items):159 prop = self._prop160 data = self._data161 content = self.__content__162 for item in items:163 data.append(content._SequenceItem(content, prop, item))164 def count(self, item):165 n = 0166 for x in self:167 if x == item:168 n += 1169 return n170 def index(self, item, start=0, stop=None):171 if stop is None:172 stop = len(self)173 data = self._data174 for index in xrange(start, stop):175 x = data[index].get()176 if x == item:177 return index178 raise ValueError("'%s' not in list" % item)179 def __len__(self):180 return len(self._data)181 def insert(self, index, item):182 content = self.__content__183 self._data.insert(index, content._SequenceItem(184 content, self._prop, item))185 def pop(self, index=-1):186 return self._data.pop(index).get()187 def remove(self, item):188 del self[self.index(item)]189 def reverse(self):190 self._data.reverse()191 def __getslice__(self, i, j):192 return [item.get() for item in self._data[i:j]]193 def __setslice__(self, i, j, s):194 error = None195 items = []196 prop = self._prop197 content = self.__content__198 for index, item in enumerate(s):199 try:200 items.append(201 content._SequenceItem(content, prop, item))202 except colander.Invalid, e:203 if error is None:204 error = colander.Invalid(self.__schema__)205 error.add(e, i + index)206 if error is not None:207 raise error208 self._data[i:j] = items209 def __delslice__(self, i, j):210 del self._data[i:j]211 def appstruct(self):212 return [_appstruct_node(item) for item in self]213class _SequenceItem(object):214 def __init__(self, content, prop, value):215 self.__content__ = content216 self._prop = prop217 prop.__set__(self, value)218 def get(self):219 return self._prop.__get__(self)220class _TupleNodeProperty(_LeafNodeProperty):221 def __init__(self, content, node):222 super(_TupleNodeProperty, self).__init__(content, node)223 property_factory = content._property_factory224 self._props = tuple(property_factory(content, child) for child in node)225 def _validate(self, content, value):226 node = self.node227 node.typ._validate(node, value) # XXX private colander api228 items = []229 error = None230 for i, (item, prop) in enumerate(zip(value, self._props)):231 try:232 items.append(233 content._SequenceItem(content, prop, item))234 except colander.Invalid, e:235 if error is None:236 error = colander.Invalid(node)237 error.add(e, i)238 if error is not None:239 raise error240 return tuple(item.get() for item in items)241class PropertyFactory(object):242 def __init__(self):243 self.registry = {244 colander.Mapping: _MappingNodeProperty,245 colander.Sequence: _SequenceNodeProperty,246 colander.Tuple: _TupleNodeProperty,247 colander.SchemaType: _LeafNodeProperty,248 }249 def __call__(self, content, node):250 registry = self.registry251 for cls in type(node.typ).mro():252 prop_cls = registry.get(cls)253 if prop_cls is not None:254 return prop_cls(content, node)255property_factory = PropertyFactory()256class _ContentSchemaDecorator(object):257 """258 Decorator for turning a Colander schema into a content type.259 """260 def __init__(self, meta=type, property_factory=property_factory):261 self.meta = meta262 self.property_factory = property_factory263 def __call__(self, schema):264 ct = make_content_type(265 schema, schema.__name__, schema.__module__, meta=self.meta,266 property_factory=self.property_factory267 )268 def callback(scanner, name, ob):269 scanner.limone.register_content_type(ct)270 venusian.attach(ct, callback, category='limone')271 return ct272content_schema = _ContentSchemaDecorator()273class _ContentTypeDecorator(object):274 """275 Decorator for turning a class into a content type using the passed in276 Colander schema.277 """278 def __init__(self, meta=type, property_factory=property_factory):279 self.meta = meta280 self.property_factory = property_factory281 def __call__(self, schema):282 def decorator(cls):283 ct = make_content_type(284 schema, cls.__name__, cls.__module__, (cls,), self.meta,285 property_factory=self.property_factory286 )287 def callback(scanner, name, ob):288 scanner.limone.register_content_type(ct)289 venusian.attach(ct, callback, category='limone')290 return ct291 return decorator292content_type = _ContentTypeDecorator()293def make_content_type(schema, name, module=None, bases=(object,), meta=type,294 property_factory=property_factory):295 """296 Generate a content type class from a Colander schema.297 """298 if isinstance(schema, type):299 schema = schema()300 if type(getattr(schema, 'typ', None)) != colander.Mapping:301 raise TypeError('Schema must be a colander mapping schema.')302 class MetaType(meta):303 def __new__(cls, throw, away, members):304 return meta.__new__(cls, name, bases, members)305 def __init__(cls, throw, away, members):306 meta.__init__(cls, name, bases, members)307 cls.__module__ = module308 cls.__content_type__ = cls309 class ContentType(object):310 __metaclass__ = MetaType311 __schema__ = schema312 _property_factory = property_factory313 _MappingNode = _MappingNode314 _SequenceNode = _SequenceNode315 _SequenceItem = _SequenceItem316 @classmethod317 def deserialize(cls, cstruct):318 appstruct = cls.__schema__.deserialize(cstruct)319 return cls(**appstruct)320 @classmethod321 def from_appstruct(cls, appstruct):322 return cls(**appstruct)323 def __init__(self, **kw):324 try:325 super(ContentType, self).__init__()326 except TypeError:327 # Substitute error message more pertinent to situation at hand.328 raise TypeError(329 'Limone content types may only extend types with no-arg '330 'constructors.')331 self.__content__ = self332 kw = self._update_from_dict(kw, skip_missing=False)333 if kw:334 raise TypeError(335 "Unexpected keyword argument(s): %s" % repr(kw))336 def deserialize_update(self, cstruct):337 error = None338 schema = self.__schema__339 appstruct = {}340 for i, (name, value) in enumerate(cstruct.items()):341 node = schema[name]342 try:343 appstruct[name] = node.deserialize(value)344 except colander.Invalid, e:345 if error is None:346 error = colander.Invalid(schema)347 error.add(e, i)348 if error is not None:349 raise error350 self._update_from_dict(appstruct, skip_missing=True)351 def update_from_appstruct(self, appstruct):352 self._update_from_dict(appstruct, skip_missing=True)353 def serialize(self):354 return self.__schema__.serialize(self.appstruct())355 def _update_from_dict(self, data, skip_missing):356 error = None357 schema = self.__schema__358 for i, node in enumerate(schema.children):359 name = node.name360 try:361 value = data.pop(name, colander.null)362 if value is colander.null and skip_missing:363 continue364 setattr(self, name, value)365 except colander.Invalid, e:366 if error is None:367 error = colander.Invalid(schema)368 error.add(e, i)369 if error is not None:370 raise error371 return data372 def appstruct(self):373 return dict([(node.name, _appstruct_node(getattr(self, node.name)))374 for node in self.__schema__])375 property_factory = ContentType._property_factory376 for node in schema:377 setattr(ContentType, node.name, property_factory(ContentType, node))378 return ContentType379def _appstruct_node(value):380 get_appstruct = getattr(value, 'appstruct', None)381 if get_appstruct is not None:382 return get_appstruct()383 return value384class _FinderLoader(object):385 def __init__(self, limone, module):386 self.limone = limone387 self.module = module388 sys.meta_path.append(self)389 def find_module(self, module, package_path):390 if module == self.module:391 return self...

Full Screen

Full Screen

test_properties.py

Source:test_properties.py Github

copy

Full Screen

...9"""10import pytest11from nominatim.db import properties12@pytest.fixture13def property_factory(property_table, temp_db_cursor):14 """ A function fixture that adds a property into the property table.15 """16 def _add_property(name, value):17 temp_db_cursor.execute("INSERT INTO nominatim_properties VALUES(%s, %s)",18 (name, value))19 return _add_property20def test_get_property_existing(property_factory, temp_db_conn):21 property_factory('foo', 'bar')22 assert properties.get_property(temp_db_conn, 'foo') == 'bar'23def test_get_property_unknown(property_factory, temp_db_conn):24 property_factory('other', 'bar')25 assert properties.get_property(temp_db_conn, 'foo') is None26@pytest.mark.parametrize("prefill", (True, False))27def test_set_property_new(property_factory, temp_db_conn, temp_db_cursor, prefill):28 if prefill:29 property_factory('something', 'bar')30 properties.set_property(temp_db_conn, 'something', 'else')31 assert temp_db_cursor.scalar("""SELECT value FROM nominatim_properties32 WHERE property = 'something'""") == 'else'...

Full Screen

Full Screen

utils.py

Source:utils.py Github

copy

Full Screen

1from __future__ import absolute_import2def lazy_property(property_name, property_factory, doc=None):3 def get(self):4 if not hasattr(self, property_name):5 setattr(self, property_name, property_factory(self))6 return getattr(self, property_name)...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful