Skip to content

Commit

Permalink
added feature to allow access of attributes which are on underlying s…
Browse files Browse the repository at this point in the history
…equence but do not exist on FunctionalSequence using __getattr__
  • Loading branch information
Pedro Rodriguez committed Mar 19, 2015
1 parent 14fb813 commit 0ec00cf
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
10 changes: 10 additions & 0 deletions functional/chain.py
Expand Up @@ -41,6 +41,16 @@ def _get_base_sequence(self):
else:
return self.sequence

def __getattr__(self, item):
"""
Extends attribute access to any attributes that the underlying sequence may have. Since
__getattr__ is used instead of __getattribute__, attributes in FunctionalSequence take precedent
:param item: attribute to get
:return: either result of getting the item attribute from the sequence or an error
"""
return getattr(self.sequence, item)

def __eq__(self, other):
"""
Checks for equality with the sequence's equality operator
Expand Down
10 changes: 10 additions & 0 deletions test/test_functional.py
@@ -1,4 +1,5 @@
import unittest
from collections import namedtuple

from functional.chain import seq, FunctionalSequence, _wrap

Expand All @@ -17,6 +18,15 @@ def test_base_sequence(self):
self.assertType(seq(seq(l)))
self.assertNotType(seq(seq(l)).sequence)

def test_get_attr(self):
CustomTuple = namedtuple("CustomTuple", 'x y')
t = CustomTuple(1, 2)
s = seq(t)
self.assertType(s)
self.assertEqual(s.sum(), 3)
self.assertEqual(s.x, 1)
self.assertEqual(s.y, 2)

def test_eq(self):
l = [1, 2, 3]
self.assertEqual(seq(l), seq(l))
Expand Down

0 comments on commit 0ec00cf

Please sign in to comment.