-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move base functor implemetation into lighty package from lighty.db. R…
…ewrite monads implementation using functor.
- Loading branch information
Showing
4 changed files
with
85 additions
and
120 deletions.
There are no files selected for viewing
This file contains 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 characters
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
'''Class implements base functor operations | ||
''' | ||
import functools | ||
|
||
|
||
def create_operator(operator): | ||
'''Create operator function | ||
''' | ||
@functools.wraps(operator) | ||
def wrap(self, operand): | ||
return self.create_copy(operator, operand) | ||
return wrap | ||
|
||
|
||
class FunctorBase(type): | ||
'''Metaclass used to create a classes includes method to generate lazy | ||
methods to access | ||
''' | ||
|
||
def __new__(mcls, name, bases, attrs): | ||
'''Create new class includes lazy methods to override an operators | ||
''' | ||
super_new = super(FunctorBase, mcls).__new__ | ||
parents = [b for b in bases if isinstance(b, FunctorBase)] | ||
if not parents: | ||
# If this isn't a subclass of Model, don't do anything special. | ||
return super_new(mcls, name, bases, attrs) | ||
# Analyse an attributes | ||
new_attrs = {'_lazy': set(attrs['_lazy']) if '_lazy' in attrs | ||
else set()} | ||
for parent in parents: | ||
if hasattr(parent, '_lazy'): | ||
new_attrs['_lazy'] |= set(parent._lazy) | ||
for attr in new_attrs['_lazy']: | ||
attrs[attr.__name__] = create_operator(attr) | ||
new_attrs.update(attrs) | ||
return super_new(mcls, name, bases, new_attrs) | ||
|
||
|
||
class BaseFunctor(object): | ||
'''Base lazy object class | ||
''' | ||
__metaclass__ = FunctorBase | ||
|
||
def create_copy(self, operator, operand): | ||
'''Create object's copy | ||
''' | ||
raise NotImplemented('"%s" does not overrides create_copy() method' % | ||
self.__class__.__name__) |
This file contains 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 characters
This file contains 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 characters