Skip to content

Instantly share code, notes, and snippets.

@skanev
Created August 24, 2012 11:45

Revisions

  1. skanev revised this gist Aug 24, 2012. 2 changed files with 21 additions and 4 deletions.
    8 changes: 5 additions & 3 deletions set_attributes.py
    Original 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
    self.__dict__.update(dict(zip(names[1:], args)))
    self.__dict__.update(kwargs)
    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
    return wrapped
    17 changes: 16 additions & 1 deletion set_attributes_test.py
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@
    from set_attributes import set_attributes
    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)
  2. skanev revised this gist Aug 24, 2012. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion set_attributes.py
    Original 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
    return wrapped
  3. skanev revised this gist Aug 24, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion set_attributes.py
    Original 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)
    self.__dict__.update(kwargs)
    return constructor(self, *args, **kwargs)
    return wrapped
  4. skanev revised this gist Aug 24, 2012. 2 changed files with 30 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions set_attributes.py
    Original 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
    21 changes: 21 additions & 0 deletions set_attributes_test.py
    Original 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)
  5. skanev created this gist Aug 24, 2012.
    10 changes: 10 additions & 0 deletions simple.py
    Original 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)