forwardable
Advanced tools
+4
-0
@@ -0,1 +1,5 @@ | ||
| * 0.3.0 | ||
| - Support attr plucking. | ||
| * 0.2.1 | ||
@@ -2,0 +6,0 @@ |
| Metadata-Version: 1.1 | ||
| Name: forwardable | ||
| Version: 0.2.1 | ||
| Version: 0.3.0 | ||
| Summary: Forwardable as in Ruby's stdlib | ||
@@ -15,2 +15,8 @@ Home-page: https://github.com/5long/forwardable | ||
| Requirements | ||
| ------------ | ||
| Python 2.7 or 3.3 w/ standard library. Might work on other version of | ||
| Python, too. | ||
| Installation | ||
@@ -24,22 +30,30 @@ ------------ | ||
| Most Common Use Case | ||
| ~~~~~~~~~~~~~~~~~~~~ | ||
| The ``@forwardable.forwardable()`` decorator enables you to use | ||
| ``def_delegator`` in a class definition block. | ||
| ``def_delegator()`` and ``def_delegators()`` in a class definition block. | ||
| Use ``def_delegators()`` to define multiple attr forwarding: | ||
| .. code-block:: python | ||
| from forwardable import forwardable | ||
| from forwardable import forwardable | ||
| @forwardable() # Note the () here, which is required. | ||
| class Foo(object): | ||
| def_delegators('bar', ('add', '__len__')) | ||
| @forwardable() # Note the () here, which is required. | ||
| class Foo(object): | ||
| def_delegators('bar', ('add', '__len__')) | ||
| def __init__(self) | ||
| self.bar = set() | ||
| def __init__(self): | ||
| self.bar = set() | ||
| foo = Foo() | ||
| foo.add(1) # Delegates to foo.bar.add() | ||
| assert len(foo) == 1 | ||
| foo = Foo() | ||
| foo.add(1) # Delegates to foo.bar.add() | ||
| assert len(foo) == 1 # Magic methods works, too | ||
| Easy, heh? | ||
| Define a Single Forwarding | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| In case you only need to delegate one method to a delegatee, just | ||
@@ -50,19 +64,43 @@ use ``def_delegator``: | ||
| from forwardable import forwardable | ||
| from forwardable import forwardable | ||
| @forwardable() | ||
| class Foo(object): | ||
| def_delegator('bar', '__len__') | ||
| @forwardable() | ||
| class Foo(object): | ||
| def_delegator('bar', '__len__') | ||
| def __init__(self) | ||
| self.bar = set() | ||
| def __init__(self): | ||
| self.bar = set() | ||
| assert len(Foo()) == 0 | ||
| assert len(Foo()) == 0 | ||
| And it should work just fine. | ||
| And it should work just fine. Actually, ``def_delegators()`` calls | ||
| ``def_delegator()`` under the hood. | ||
| Plucking | ||
| ~~~~~~~~ | ||
| .. code-block:: python | ||
| from forwardable import forwardable | ||
| @forwardable | ||
| class MyDict(object): | ||
| def_delegator('dct.get', '__call__') | ||
| def __init__(self): | ||
| self.dct = {'foo', 42} | ||
| d = MyDict() | ||
| # Equivlant to d.dct.get('foo') | ||
| assert d('foo') == 42 | ||
| Less Magical Usage | ||
| ~~~~~~~~~~~~~~~~~~ | ||
| If you hesitate to touch the ``@forwardable()`` injection magic, just | ||
| The ``@forwardable()`` decorator injects ``def_delegator{,s}()`` into the | ||
| module scope temorarily, which is why you don't have to import them | ||
| explicitly. This is admittedly magical but discourages the usage | ||
| of ``import *``. And it's always nice to type less characters whenever | ||
| unnecessary. | ||
| If you hesitate to utilize this injection magic, just explicitly say | ||
| ``from forwardable import def_delegator, def_delegators``, use them in | ||
@@ -85,1 +123,2 @@ a class definition and you'll be fine. | ||
| Classifier: Programming Language :: Python :: 2.7 | ||
| Classifier: Programming Language :: Python :: 3.3 |
@@ -18,3 +18,3 @@ """ | ||
| __version__ = '0.2.1' | ||
| __version__ = '0.3.0' | ||
@@ -24,2 +24,3 @@ __all__ = ["forwardable", "def_delegator", "def_delegators"] | ||
| import sys | ||
| from operator import attrgetter | ||
@@ -43,4 +44,3 @@ class NotCalledInModuleScope(Exception): pass | ||
| def get_wrapped_obj(self): | ||
| return getattr(self, wrapped) | ||
| get_wrapped_obj = attrgetter(wrapped) | ||
@@ -47,0 +47,0 @@ def getter(self): |
@@ -19,3 +19,3 @@ from unittest import TestCase | ||
| self.assertTrue(hasattr(foo, "keys")) | ||
| self.assertEqual(foo.keys(), ['key']) | ||
| self.assertEqual(list(foo.keys()), ['key']) | ||
@@ -30,4 +30,4 @@ | ||
| self.assertEqual(foo.keys(), ['key']) | ||
| self.assertEqual(foo.values(), [42]) | ||
| self.assertEqual(list(foo.keys()), ['key']) | ||
| self.assertEqual(list(foo.values()), [42]) | ||
@@ -82,1 +82,13 @@ def test_called_in_non_class_scope(self): | ||
| self.assertTrue(foo.bar.baz, 42) | ||
| def test_attr_plucking(self): | ||
| class Foo(object): | ||
| def_delegator("bar.baz", "qoox") | ||
| foo = Foo() | ||
| foo.bar = Object() | ||
| foo.bar.baz = Object() | ||
| foo.bar.baz.qoox = 42 | ||
| self.assertEqual(foo.qoox, 42) |
@@ -22,5 +22,5 @@ from unittest import TestCase | ||
| self.assertEqual(foo.keys(), ['key']) | ||
| self.assertEqual(foo.values(), [42]) | ||
| self.assertEqual(foo.items(), [('key', 42)]) | ||
| self.assertEqual(list(foo.keys()), ['key']) | ||
| self.assertEqual(list(foo.values()), [42]) | ||
| self.assertEqual(list(foo.items()), [('key', 42)]) | ||
@@ -27,0 +27,0 @@ self.assertFalse(hasattr(foo, "get")) |
+59
-20
| Metadata-Version: 1.1 | ||
| Name: forwardable | ||
| Version: 0.2.1 | ||
| Version: 0.3.0 | ||
| Summary: Forwardable as in Ruby's stdlib | ||
@@ -15,2 +15,8 @@ Home-page: https://github.com/5long/forwardable | ||
| Requirements | ||
| ------------ | ||
| Python 2.7 or 3.3 w/ standard library. Might work on other version of | ||
| Python, too. | ||
| Installation | ||
@@ -24,22 +30,30 @@ ------------ | ||
| Most Common Use Case | ||
| ~~~~~~~~~~~~~~~~~~~~ | ||
| The ``@forwardable.forwardable()`` decorator enables you to use | ||
| ``def_delegator`` in a class definition block. | ||
| ``def_delegator()`` and ``def_delegators()`` in a class definition block. | ||
| Use ``def_delegators()`` to define multiple attr forwarding: | ||
| .. code-block:: python | ||
| from forwardable import forwardable | ||
| from forwardable import forwardable | ||
| @forwardable() # Note the () here, which is required. | ||
| class Foo(object): | ||
| def_delegators('bar', ('add', '__len__')) | ||
| @forwardable() # Note the () here, which is required. | ||
| class Foo(object): | ||
| def_delegators('bar', ('add', '__len__')) | ||
| def __init__(self) | ||
| self.bar = set() | ||
| def __init__(self): | ||
| self.bar = set() | ||
| foo = Foo() | ||
| foo.add(1) # Delegates to foo.bar.add() | ||
| assert len(foo) == 1 | ||
| foo = Foo() | ||
| foo.add(1) # Delegates to foo.bar.add() | ||
| assert len(foo) == 1 # Magic methods works, too | ||
| Easy, heh? | ||
| Define a Single Forwarding | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| In case you only need to delegate one method to a delegatee, just | ||
@@ -50,19 +64,43 @@ use ``def_delegator``: | ||
| from forwardable import forwardable | ||
| from forwardable import forwardable | ||
| @forwardable() | ||
| class Foo(object): | ||
| def_delegator('bar', '__len__') | ||
| @forwardable() | ||
| class Foo(object): | ||
| def_delegator('bar', '__len__') | ||
| def __init__(self) | ||
| self.bar = set() | ||
| def __init__(self): | ||
| self.bar = set() | ||
| assert len(Foo()) == 0 | ||
| assert len(Foo()) == 0 | ||
| And it should work just fine. | ||
| And it should work just fine. Actually, ``def_delegators()`` calls | ||
| ``def_delegator()`` under the hood. | ||
| Plucking | ||
| ~~~~~~~~ | ||
| .. code-block:: python | ||
| from forwardable import forwardable | ||
| @forwardable | ||
| class MyDict(object): | ||
| def_delegator('dct.get', '__call__') | ||
| def __init__(self): | ||
| self.dct = {'foo', 42} | ||
| d = MyDict() | ||
| # Equivlant to d.dct.get('foo') | ||
| assert d('foo') == 42 | ||
| Less Magical Usage | ||
| ~~~~~~~~~~~~~~~~~~ | ||
| If you hesitate to touch the ``@forwardable()`` injection magic, just | ||
| The ``@forwardable()`` decorator injects ``def_delegator{,s}()`` into the | ||
| module scope temorarily, which is why you don't have to import them | ||
| explicitly. This is admittedly magical but discourages the usage | ||
| of ``import *``. And it's always nice to type less characters whenever | ||
| unnecessary. | ||
| If you hesitate to utilize this injection magic, just explicitly say | ||
| ``from forwardable import def_delegator, def_delegators``, use them in | ||
@@ -85,1 +123,2 @@ a class definition and you'll be fine. | ||
| Classifier: Programming Language :: Python :: 2.7 | ||
| Classifier: Programming Language :: Python :: 3.3 |
+57
-19
@@ -7,2 +7,8 @@ Forwardable | ||
| Requirements | ||
| ------------ | ||
| Python 2.7 or 3.3 w/ standard library. Might work on other version of | ||
| Python, too. | ||
| Installation | ||
@@ -16,22 +22,30 @@ ------------ | ||
| Most Common Use Case | ||
| ~~~~~~~~~~~~~~~~~~~~ | ||
| The ``@forwardable.forwardable()`` decorator enables you to use | ||
| ``def_delegator`` in a class definition block. | ||
| ``def_delegator()`` and ``def_delegators()`` in a class definition block. | ||
| Use ``def_delegators()`` to define multiple attr forwarding: | ||
| .. code-block:: python | ||
| from forwardable import forwardable | ||
| from forwardable import forwardable | ||
| @forwardable() # Note the () here, which is required. | ||
| class Foo(object): | ||
| def_delegators('bar', ('add', '__len__')) | ||
| @forwardable() # Note the () here, which is required. | ||
| class Foo(object): | ||
| def_delegators('bar', ('add', '__len__')) | ||
| def __init__(self) | ||
| self.bar = set() | ||
| def __init__(self): | ||
| self.bar = set() | ||
| foo = Foo() | ||
| foo.add(1) # Delegates to foo.bar.add() | ||
| assert len(foo) == 1 | ||
| foo = Foo() | ||
| foo.add(1) # Delegates to foo.bar.add() | ||
| assert len(foo) == 1 # Magic methods works, too | ||
| Easy, heh? | ||
| Define a Single Forwarding | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| In case you only need to delegate one method to a delegatee, just | ||
@@ -42,19 +56,43 @@ use ``def_delegator``: | ||
| from forwardable import forwardable | ||
| from forwardable import forwardable | ||
| @forwardable() | ||
| class Foo(object): | ||
| def_delegator('bar', '__len__') | ||
| @forwardable() | ||
| class Foo(object): | ||
| def_delegator('bar', '__len__') | ||
| def __init__(self) | ||
| self.bar = set() | ||
| def __init__(self): | ||
| self.bar = set() | ||
| assert len(Foo()) == 0 | ||
| assert len(Foo()) == 0 | ||
| And it should work just fine. | ||
| And it should work just fine. Actually, ``def_delegators()`` calls | ||
| ``def_delegator()`` under the hood. | ||
| Plucking | ||
| ~~~~~~~~ | ||
| .. code-block:: python | ||
| from forwardable import forwardable | ||
| @forwardable | ||
| class MyDict(object): | ||
| def_delegator('dct.get', '__call__') | ||
| def __init__(self): | ||
| self.dct = {'foo', 42} | ||
| d = MyDict() | ||
| # Equivlant to d.dct.get('foo') | ||
| assert d('foo') == 42 | ||
| Less Magical Usage | ||
| ~~~~~~~~~~~~~~~~~~ | ||
| If you hesitate to touch the ``@forwardable()`` injection magic, just | ||
| The ``@forwardable()`` decorator injects ``def_delegator{,s}()`` into the | ||
| module scope temorarily, which is why you don't have to import them | ||
| explicitly. This is admittedly magical but discourages the usage | ||
| of ``import *``. And it's always nice to type less characters whenever | ||
| unnecessary. | ||
| If you hesitate to utilize this injection magic, just explicitly say | ||
| ``from forwardable import def_delegator, def_delegators``, use them in | ||
@@ -61,0 +99,0 @@ a class definition and you'll be fine. |
+1
-0
@@ -30,3 +30,4 @@ #!/usr/bin/env python | ||
| 'Programming Language :: Python :: 2.7', | ||
| 'Programming Language :: Python :: 3.3', | ||
| ] | ||
| ) |
Alert delta unavailable
Currently unable to show alert delta for PyPI packages.
18386
29.69%191
4.95%