Adding warnings to transformation factory, and code formating.

This commit is contained in:
Romain Dorgueil
2017-09-30 11:58:37 +02:00
parent b26dbc83cb
commit ef938c9970
4 changed files with 30 additions and 19 deletions

View File

@ -44,8 +44,9 @@ def resolve_factory(name, filename, factory_type):
if not name in REGISTRY: if not name in REGISTRY:
raise RuntimeError( raise RuntimeError(
'Could not resolve {factory_type} factory for {filename} ({name}). Try providing it explicitely using -{opt} <format>.'.format( 'Could not resolve {factory_type} factory for {filename} ({name}). Try providing it explicitely using -{opt} <format>.'.
name=name, filename=filename, factory_type=factory_type, opt=factory_type[0])) format(name=name, filename=filename, factory_type=factory_type, opt=factory_type[0])
)
if factory_type == READER: if factory_type == READER:
return REGISTRY[name][0] return REGISTRY[name][0]
@ -62,9 +63,11 @@ def execute(input, output, reader=None, reader_options=None, writer=None, writer
graph = bonobo.Graph() graph = bonobo.Graph()
graph.add_chain(reader, writer) graph.add_chain(reader, writer)
return bonobo.run(graph, services={ return bonobo.run(
'fs': bonobo.open_fs(), graph, services={
}) 'fs': bonobo.open_fs(),
}
)
def register(parser): def register(parser):

View File

@ -48,7 +48,6 @@ def normalize(row):
return result return result
def display(row): def display(row):
print(Style.BRIGHT, row.get('name'), Style.RESET_ALL, sep='') print(Style.BRIGHT, row.get('name'), Style.RESET_ALL, sep='')
@ -69,15 +68,15 @@ def display(row):
print( print(
' - {}address{}: {address}'. ' - {}address{}: {address}'.
format(Fore.BLUE, Style.RESET_ALL, address=', '.join(address)) format(Fore.BLUE, Style.RESET_ALL, address=', '.join(address))
) )
print( print(
' - {}links{}: {links}'. ' - {}links{}: {links}'.
format(Fore.BLUE, Style.RESET_ALL, links=', '.join(row['links'])) format(Fore.BLUE, Style.RESET_ALL, links=', '.join(row['links']))
) )
print( print(
' - {}geometry{}: {geometry}'. ' - {}geometry{}: {geometry}'.
format(Fore.BLUE, Style.RESET_ALL, **row) format(Fore.BLUE, Style.RESET_ALL, **row)
) )
print( print(
' - {}source{}: {source}'.format( ' - {}source{}: {source}'.format(
@ -87,7 +86,9 @@ def display(row):
graph = bonobo.Graph( graph = bonobo.Graph(
OpenDataSoftAPI(dataset=API_DATASET, netloc=API_NETLOC, timezone='Europe/Paris'), OpenDataSoftAPI(
dataset=API_DATASET, netloc=API_NETLOC, timezone='Europe/Paris'
),
normalize, normalize,
bonobo.Filter(filter=lambda row: row.get('country') == 'France'), bonobo.Filter(filter=lambda row: row.get('country') == 'France'),
bonobo.JsonWriter(path='fablabs.txt', ioformat='arg0'), bonobo.JsonWriter(path='fablabs.txt', ioformat='arg0'),

View File

@ -8,6 +8,7 @@ from bonobo.config import Configurable
from bonobo.nodes.factory import Factory from bonobo.nodes.factory import Factory
from bonobo.nodes.io.json import JsonDictReader from bonobo.nodes.io.json import JsonDictReader
@Factory @Factory
def Normalize(self): def Normalize(self):
self[0].str().title() self[0].str().title()
@ -15,11 +16,11 @@ def Normalize(self):
self.move(0, 'address') self.move(0, 'address')
class PrettyPrinter(Configurable): class PrettyPrinter(Configurable):
def call(self, *args, **kwargs): def call(self, *args, **kwargs):
for i, (item, value) in enumerate(itertools.chain(enumerate(args), kwargs.items())): for i, (
item, value
) in enumerate(itertools.chain(enumerate(args), kwargs.items())):
print(' ' if i else '', item, '=', value) print(' ' if i else '', item, '=', value)

View File

@ -1,8 +1,9 @@
import functools import functools
import warnings
from functools import partial from functools import partial
from bonobo import Bag from bonobo import Bag
from bonobo.config import Configurable, Method from bonobo.config import Configurable
_isarg = lambda item: type(item) is int _isarg = lambda item: type(item) is int
_iskwarg = lambda item: type(item) is str _iskwarg = lambda item: type(item) is str
@ -110,7 +111,10 @@ class Cursor():
setattr(self, item, partial(_operation, self)) setattr(self, item, partial(_operation, self))
return getattr(self, item) return getattr(self, item)
raise AttributeError('Unknown operation {}.{}().'.format(type(self).__name__, item, )) raise AttributeError('Unknown operation {}.{}().'.format(
type(self).__name__,
item,
))
CURSOR_TYPES['default'] = Cursor CURSOR_TYPES['default'] = Cursor
@ -139,12 +143,15 @@ CURSOR_TYPES['str'] = StringCursor
class Factory(Configurable): class Factory(Configurable):
setup = Method()
def __init__(self): def __init__(self):
warnings.warn(
__file__ +
' is experimental, API may change in the future, use it as a preview only and knowing the risks.',
FutureWarning
)
super(Factory, self).__init__()
self.default_cursor_type = 'default' self.default_cursor_type = 'default'
self.operations = [] self.operations = []
self.setup()
@factory_operation @factory_operation
def move(self, _from, _to, *args, **kwargs): def move(self, _from, _to, *args, **kwargs):
@ -186,7 +193,6 @@ if __name__ == '__main__':
print('operations:', f.operations) print('operations:', f.operations)
print(f({'foo': 'bisou'}, foo='blah')) print(f({'foo': 'bisou'}, foo='blah'))
''' '''
specs: specs: