Working toward sqlalchemy extension. Better ability to extend context. Still needs a lot of cleanup.
This commit is contained in:
@ -1,47 +1,116 @@
|
||||
""" Various simple utilities. """
|
||||
|
||||
import functools
|
||||
import pprint
|
||||
from pprint import pprint as _pprint
|
||||
|
||||
import blessings
|
||||
|
||||
from .tokens import NOT_MODIFIED
|
||||
from .helpers import run, console_run, jupyter_run
|
||||
from .tokens import NOT_MODIFIED
|
||||
|
||||
__all__ = [
|
||||
'NOT_MODIFIED',
|
||||
'console_run',
|
||||
'head',
|
||||
'jupyter_run',
|
||||
'limit',
|
||||
'log',
|
||||
'noop',
|
||||
'pprint',
|
||||
'run',
|
||||
'tee',
|
||||
]
|
||||
|
||||
|
||||
def head(n=10):
|
||||
def identity(x):
|
||||
return x
|
||||
|
||||
|
||||
def limit(n=10):
|
||||
i = 0
|
||||
|
||||
def _head(x):
|
||||
def _limit(*args, **kwargs):
|
||||
nonlocal i, n
|
||||
i += 1
|
||||
if i <= n:
|
||||
yield x
|
||||
yield NOT_MODIFIED
|
||||
|
||||
_head.__name__ = 'head({})'.format(n)
|
||||
return _head
|
||||
_limit.__name__ = 'limit({})'.format(n)
|
||||
return _limit
|
||||
|
||||
|
||||
def tee(f):
|
||||
@functools.wraps(f)
|
||||
def wrapped(x):
|
||||
def wrapped(*args, **kwargs):
|
||||
nonlocal f
|
||||
f(x)
|
||||
return x
|
||||
f(*args, **kwargs)
|
||||
return NOT_MODIFIED
|
||||
|
||||
return wrapped
|
||||
|
||||
|
||||
log = tee(pprint.pprint)
|
||||
log = tee(_pprint)
|
||||
|
||||
|
||||
def pprint(title_keys=('title', 'name', 'id'), print_values=True, sort=True):
|
||||
term = blessings.Terminal()
|
||||
|
||||
def _pprint(*args, **kwargs):
|
||||
nonlocal title_keys, term, sort, print_values
|
||||
|
||||
row = args[0]
|
||||
for key in title_keys:
|
||||
if key in row:
|
||||
print(term.bold(row.get(key)))
|
||||
break
|
||||
|
||||
if print_values:
|
||||
for k in sorted(row) if sort else row:
|
||||
print(
|
||||
' • {t.blue}{k}{t.normal} : {t.black}({tp}){t.normal} {v}{t.clear_eol}'.format(
|
||||
k=k, v=repr(row[k]), t=term, tp=type(row[k]).__name__
|
||||
)
|
||||
)
|
||||
|
||||
yield NOT_MODIFIED
|
||||
|
||||
_pprint.__name__ = 'pprint'
|
||||
|
||||
return _pprint
|
||||
|
||||
|
||||
'''
|
||||
|
||||
def writehr(self, label=None):
|
||||
width = t.width or 80
|
||||
|
||||
if label:
|
||||
label = str(label)
|
||||
sys.stderr.write(t.black('·' * 4) + shade('{') + label + shade('}') + t.black('·' * (width - (6+len(label)) - 1)) + '\n')
|
||||
else:
|
||||
sys.stderr.write(t.black('·' * (width-1) + '\n'))
|
||||
|
||||
|
||||
def writeln(self, s):
|
||||
"""Output method."""
|
||||
sys.stderr.write(self.format(s) + '\n')
|
||||
|
||||
def initialize(self):
|
||||
self.lineno = 0
|
||||
|
||||
def transform(self, hash, channel=STDIN):
|
||||
"""Actual transformation."""
|
||||
self.lineno += 1
|
||||
if not self.condition or self.condition(hash):
|
||||
hash = hash.copy()
|
||||
hash = hash if not isinstance(self.field_filter, collections.Callable) else hash.restrict(self.field_filter)
|
||||
if self.clean:
|
||||
hash = hash.restrict(lambda k: len(k) and k[0] != '_')
|
||||
self.writehr(self.lineno)
|
||||
self.writeln(hash)
|
||||
self.writehr()
|
||||
sys.stderr.write('\n')
|
||||
yield hash
|
||||
'''
|
||||
|
||||
|
||||
def noop(*args, **kwargs): # pylint: disable=unused-argument
|
||||
|
||||
@ -1,20 +1,20 @@
|
||||
def run(*chain, plugins=None):
|
||||
def run(*chain, plugins=None, strategy=None):
|
||||
from bonobo import Graph, ThreadPoolExecutorStrategy
|
||||
|
||||
graph = Graph()
|
||||
graph.add_chain(*chain)
|
||||
|
||||
executor = ThreadPoolExecutorStrategy()
|
||||
executor = (strategy or ThreadPoolExecutorStrategy)()
|
||||
return executor.execute(graph, plugins=plugins or [])
|
||||
|
||||
|
||||
def console_run(*chain, output=True, plugins=None):
|
||||
def console_run(*chain, output=True, plugins=None, strategy=None):
|
||||
from bonobo.ext.console import ConsoleOutputPlugin
|
||||
|
||||
return run(*chain, plugins=(plugins or []) + [ConsoleOutputPlugin()] if output else [])
|
||||
return run(*chain, plugins=(plugins or []) + [ConsoleOutputPlugin()] if output else [], strategy=strategy)
|
||||
|
||||
|
||||
def jupyter_run(*chain, plugins=None):
|
||||
def jupyter_run(*chain, plugins=None, strategy=None):
|
||||
from bonobo.ext.jupyter import JupyterOutputPlugin
|
||||
|
||||
return run(*chain, plugins=(plugins or []) + [JupyterOutputPlugin()])
|
||||
return run(*chain, plugins=(plugins or []) + [JupyterOutputPlugin()], strategy=strategy)
|
||||
|
||||
Reference in New Issue
Block a user