This repository has been archived by the owner on Feb 1, 2019. It is now read-only.
forked from madduck/reclass
-
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.
Mutators allow the definition of raw python functions to manipulate entities before returning them to the caller. The Mutators implementation obsoletes function interpolation
- Loading branch information
Showing
13 changed files
with
192 additions
and
362 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
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
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,62 @@ | ||
# | ||
# -*- coding: utf-8 -*- | ||
# | ||
# This file is part of reclass (http://github.com/madduck/reclass) | ||
# | ||
# Copyright © 2007–14 martin f. krafft <[email protected]> | ||
# Released under the terms of the Artistic Licence 2.0 | ||
# | ||
|
||
from collections import deque | ||
|
||
class Mutators(object): | ||
''' | ||
A class to maintain a push-only stack of python callables, with the following specialty: | ||
+ If a callable is being pushed onto the stack which is already there, | ||
it is removed from its previous position and placed back on top | ||
Class also provides a static method for building lists of callables from iterables | ||
of module paths | ||
''' | ||
@staticmethod | ||
def load_callables(iterable): | ||
callables = [] | ||
for path in iterable: | ||
modname, callname = path.rsplit('.', 1) | ||
module = __import__(modname, globals(), locals(), [callname], -1) | ||
call = getattr(module, callname) | ||
callables.append(call) | ||
return callables | ||
def __init__(self, iterable=None): | ||
self._stack = deque() | ||
if iterable is not None: | ||
self.push(iterable) | ||
def __repr__(self): | ||
return '%s(%r)' % (self.__class__.__name__, | ||
self.as_list()) | ||
def __len__(self): | ||
return len(self._stack) | ||
def __eq__(self, rhs): | ||
if isinstance(rhs, deque): | ||
return self._stack == rhs | ||
if isinstance(rhs, list): | ||
return list(self._stack) == rhs | ||
else: | ||
try: | ||
return self._stack == rhs._stack | ||
except AttributeError: | ||
return False | ||
def __ne__(self, rhs): | ||
return not self.__eq__(rhs) | ||
def push(self, iterable): | ||
if isinstance(iterable, self.__class__): | ||
iterable = iterable.as_list() | ||
for item in iterable: | ||
if item in self._stack: | ||
self._stack.remove(item) | ||
self._stack.appendleft(item) | ||
def as_list(self): | ||
return list(self._stack) | ||
def as_deque(self): | ||
return deque(self._stack) |
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
Oops, something went wrong.
fb12475
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you explain how to use mutators compared to function interpolation?
fb12475
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function interpolation feature which was not merged differs from this approach on two key usage points:
PYTHONPATH
.There is a usage example available in the PR (which still needs some work done before it can be merged)
fb12475
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your explanation, that makes sense.