def edit_venue(venue_id): form = VenueForm() raw_venue = Venue.query.filter_by(id=venue_id).first() attribs = list(filter(lambda a: not a.startswith('_'), dir(raw_venue))) for attrib in attribs: if hasattr(form, attrib): magicattr.set(form, attrib + '.data', getattr(raw_venue, attrib, '')) venue = vars(raw_venue) return render_template('forms/edit_venue.html', form=form, venue=venue)
def edit_artist(artist_id): form = ArtistForm() raw_artist = Artist.query.filter_by(id=artist_id).first() attribs = list(filter(lambda a: not a.startswith('_'), dir(raw_artist))) for attrib in attribs: if hasattr(form, attrib): magicattr.set(form, attrib + '.data', getattr(raw_artist, attrib, '')) artist = vars(raw_artist) return render_template('forms/edit_artist.html', form=form, artist=artist)
def SetLibConfigValue(self, name, value): """ libconf wrote its own version of AttrDict, which doesn't have the propper setters for certain types. The code here is mostly due to trial and error of what works on each type. """ if name[-1] == ']': magicattr.set(self.cfg, name, value) return pos = name.rfind('.') if pos >= 0: pos = name.rfind('.') prop = magicattr.get(self.cfg, name[:pos]) prop[name[pos+1:]] = value else: self.cfg[name] = value
def test_person_example(): bob = Person(name="Bob", age=31, friends=[]) jill = Person(name="Jill", age=29, friends=[bob]) jack = Person(name="Jack", age=28, friends=[bob, jill]) # Nothing new assert magicattr.get(bob, 'age') == 31 # Default value (optional) with pytest.raises(AttributeError) as e: magicattr.get(bob, 'weight') assert magicattr.get(bob, 'weight', default=75) == 75 # Lists assert magicattr.get(jill, 'friends[0].name') == 'Bob' assert magicattr.get(jack, 'friends[-1].age') == 29 # Dict lookups assert magicattr.get(jack, 'settings["style"]["width"]') == 200 # Combination of lookups assert magicattr.get(jack, 'settings["themes"][-2]') == 'light' assert magicattr.get(jack, 'friends[-1].settings["themes"][1]') == 'dark' # Setattr magicattr.set(bob, 'settings["style"]["width"]', 400) assert magicattr.get(bob, 'settings["style"]["width"]') == 400 # Nested objects magicattr.set(bob, 'friends', [jack, jill]) assert magicattr.get(jack, 'friends[0].friends[0]') == jack magicattr.set(jill, 'friends[0].age', 32) assert bob.age == 32 # Deletion magicattr.delete(jill, 'friends[0]') assert len(jill.friends) == 0 magicattr.delete(jill, 'age') assert not hasattr(jill, 'age') magicattr.delete(bob, 'friends[0].age') assert not hasattr(jack, 'age') # Unsupported with pytest.raises(NotImplementedError) as e: magicattr.get(bob, 'friends[0+1]') # Nice try, function calls are not allowed with pytest.raises(ValueError): magicattr.get(bob, 'friends.pop(0)') # Must be an expression with pytest.raises(ValueError): magicattr.get(bob, 'friends = []') # Must be an expression with pytest.raises(SyntaxError): magicattr.get(bob, 'friends..') # Must be an expression with pytest.raises(KeyError): magicattr.get(bob, 'settings["DoesNotExist"]') # Must be an expression with pytest.raises(IndexError): magicattr.get(bob, 'friends[100]')