Created
August 24, 2012 11:45
Revisions
-
skanev revised this gist
Aug 24, 2012 . 2 changed files with 21 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,7 +5,9 @@ def set_attributes(constructor): @wraps(constructor) def wrapped(self, *args, **kwargs): names = inspect.getargspec(constructor).args for (key, value) in dict(zip(names[1:], args)).items(): setattr(self, key, value) for (key, value) in kwargs.items(): setattr(self, key, value) return constructor(self, *args, **kwargs) return wrapped This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,3 @@ import unittest class SetAttributesTest(unittest.TestCase): @@ -19,3 +18,19 @@ def __init__(self, **kwargs): pass self.assertEqual('John Doe', Person(name='John Doe').name) def test_invoke_setters(self): class Person: @set_attributes def __init__(self, age): pass @property def age(self): return self._age @age.setter def age(self, age): self._age = age * 2 self.assertEqual(20, Person(10).age) -
skanev revised this gist
Aug 24, 2012 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,9 +1,11 @@ import inspect from functools import wraps def set_attributes(constructor): @wraps(constructor) def wrapped(self, *args, **kwargs): names = inspect.getargspec(constructor).args self.__dict__.update(dict(zip(names[1:], args))) self.__dict__.update(kwargs) return constructor(self, *args, **kwargs) return wrapped -
skanev revised this gist
Aug 24, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,6 +4,6 @@ def set_attributes(constructor): def wrapped(self, *args, **kwargs): names = inspect.getargspec(constructor).args self.__dict__.update(dict(zip(names[1:], args))) self.__dict__.update(kwargs) return constructor(self, *args, **kwargs) return wrapped -
skanev revised this gist
Aug 24, 2012 . 2 changed files with 30 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,9 @@ import inspect def set_attributes(constructor): def wrapped(self, *args, **kwargs): names = inspect.getargspec(constructor).args self.__dict__.update(dict(zip(names[1:], args))) self.__dict__.update(**kwargs) return constructor(self, *args, **kwargs) return wrapped This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,21 @@ from set_attributes import set_attributes import unittest class SetAttributesTest(unittest.TestCase): def test_positional_arguments(self): class Person: @set_attributes def __init__(self, name, age, gender): pass self.assertEqual('John Doe', Person('John Doe', None, None).name) self.assertEqual(33, Person(None, 33, None).age) self.assertEqual('male', Person(None, None, 'male').gender) def test_keyword_arguments(self): class Person: @set_attributes def __init__(self, **kwargs): pass self.assertEqual('John Doe', Person(name='John Doe').name) -
skanev created this gist
Aug 24, 2012 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,10 @@ # This is a fairly simple way to do it, but I dislike it: class Person: def __init__(self, name, age, location): self.__dict__.update({k: v for (k, v) in locals().items() if k != 'self'}) person = Person('Stefan', 26, 'Sofia') print(person.name) print(person.age) print(person.location)