adds tutorials and documentation for file readers and writers.

This commit is contained in:
Romain Dorgueil
2017-04-30 11:14:34 +02:00
parent 8018f63457
commit 4ee3fd3be9
11 changed files with 151 additions and 47 deletions

View File

@ -10,6 +10,7 @@ __all__ += ['Bag', 'Graph']
# Filesystem. This is a shortcut from the excellent filesystem2 library, that we make available there for convenience.
from fs import open_fs as _open_fs
open_fs = lambda url, *args, **kwargs: _open_fs(str(url), *args, **kwargs)
__all__ += ['open_fs']
@ -38,7 +39,11 @@ def get_examples_path(*pathsegments):
return str(pathlib.Path(os.path.dirname(__file__), 'examples', *pathsegments))
def open_examples_fs(*pathsegments):
return open_fs(get_examples_path(*pathsegments))
__all__.append(get_examples_path.__name__)
__all__.append(open_examples_fs.__name__)
def _is_interactive_console():

View File

@ -0,0 +1,5 @@
from bonobo import open_examples_fs
def get_services():
return {'fs': open_examples_fs('datasets')}

View File

@ -1,15 +1,11 @@
import bonobo
from bonobo.commands.run import get_default_services
graph = bonobo.Graph(
bonobo.FileReader(path='datasets/coffeeshops.txt'),
bonobo.FileReader(path='coffeeshops.txt'),
print,
)
def get_services():
return {'fs': bonobo.open_fs(bonobo.get_examples_path())}
if __name__ == '__main__':
bonobo.run(graph, services=get_default_services(__file__, get_services()))
bonobo.run(graph, services={
'fs': bonobo.open_examples_fs('datasets')
})

View File

@ -0,0 +1,17 @@
import bonobo
def split_one(line):
return line.split(', ', 1)
graph = bonobo.Graph(
bonobo.FileReader(path='coffeeshops.txt'),
split_one,
bonobo.JsonWriter(path='coffeeshops.json'),
)
if __name__ == '__main__':
bonobo.run(graph, services={
'fs': bonobo.open_examples_fs('datasets')
})

View File

@ -0,0 +1,27 @@
import bonobo, json
def split_one_to_map(line):
k, v = line.split(', ', 1)
return {k: v}
class MyJsonWriter(bonobo.JsonWriter):
prefix, suffix = '{', '}'
def write(self, fs, file, lineno, row):
return bonobo.FileWriter.write(
self, fs, file, lineno, json.dumps(row)[1:-1]
)
graph = bonobo.Graph(
bonobo.FileReader(path='coffeeshops.txt'),
split_one_to_map,
MyJsonWriter(path='coffeeshops.json'),
)
if __name__ == '__main__':
bonobo.run(graph, services={
'fs': bonobo.open_examples_fs('datasets')
})

View File

@ -10,6 +10,7 @@ __all__ = [
class JsonHandler:
eol = ',\n'
prefix, suffix = '[', ']'
class JsonReader(JsonHandler, FileReader):
@ -24,9 +25,9 @@ class JsonReader(JsonHandler, FileReader):
class JsonWriter(JsonHandler, FileWriter):
@ContextProcessor
def envelope(self, context, fs, file, lineno):
file.write('[\n')
file.write(self.prefix)
yield
file.write('\n]')
file.write(self.suffix)
def write(self, fs, file, lineno, row):
"""