def handle_animation(self, node): frames = [] map_obj = self.root tileset = self.parent for frame_node in node.findall('frame'): tileid = to_python('tileid', frame_node.get('tileid')) gid = tileset.firstgid + tileid # add tile required to display animation if gid not in map_obj.tiles: tileset.add_tile(None, gid=gid, width=tileset.tilewidth, height=tileset.tileheight) duration = to_python('duration', frame_node.get('duration')) frames.append(AnimationFrame(gid, duration, map_obj)) self.set_property('animation_frames', tuple(frames))
def __getitem__(self, key): if not isinstance(key, (str, unicode)): raise TypeError('Primary key must be a string, got %s "%s"' % (type(key).__name__, key)) try: elem = self.proto.get(key, self.literal) return utils.to_python(elem, self.db_type, self.separator) except exceptions.TyrantError: raise KeyError(key)
def expired(self): """return True if membership is expired """ date = self.membershipExpirationDate if isinstance(date, datetime.date): date = to_python(to_string(date), datetime.datetime) if date > datetime.datetime.now(): return False return True
def __get__(self, instance, klass): if instance is None: return self try: data = instance.normalized_data() except ValueError: # new node return '' value = data.get(self.name) return utils.to_python(value, self.klass)
def __getattr__(self, attr): """get a node attribute""" try: value = self.normalized_data()[attr] except KeyError: raise AttributeError('%r as no attribute %s' % (self, attr)) type = self._field_types.get(attr, None) if type: return utils.to_python(value, type) return value
def updateExpirationDate(user): payments = [p for p in user.payments if p.paymentAmount] last = None if payments: last = payments[-1] expire = to_python(to_string(last.paymentDate), datetime.datetime) + datetime.timedelta(400) user.membershipExpirationDate = expire user.save() return last
def set_attr(self, attr_name, value): # omit all attributes that aren't explicitly set on instance if not hasattr(self, attr_name): return try: # do some custom processing if required prepare = getattr(self, 'prepare_attr_{}'.format(attr_name)) except AttributeError: # or just cast to the predefined type if custom method wasn't provided value = to_python(attr_name, value) else: value = prepare(value) setattr(self, attr_name, value)
def multi_get(self, keys): """ Returns records that match given keys. Missing keys are silently ignored, i.e. the number of results may be lower than the number of requested keys. The records are returned as key/value pairs. Usage:: >>> g = t.multi_get(['foo', 'bar', 'galakteko opasnoste']) >>> g [('foo', {'one': 'one'}), ('bar', {'two': 'two'})] :param keys: the list of keys. """ # TODO: write better documentation: why would user need the no_update_log param? assert hasattr(keys, '__iter__'), 'expected iterable, got %s' % keys prep_val = lambda v: utils.to_python(v, self.db_type, self.separator) keys = list(keys) data = self.proto.misc('getlist', keys, 0) data_keys = data[::2] data_vals = (prep_val(x) for x in data[1::2]) return zip(data_keys, data_vals)
def _to_python(self, key): elem = wait(self._proto.get(key, self.literal)) elem = to_python(elem, self._dbtype) return key, elem
def get(self, attr, default=None): value = self.normalized_data().get(attr, default) type = self._field_types.get(attr, None) if type: return utils.to_python(value, type) return value
def _to_python(self, elem): return utils.to_python(elem, self._db_type)