This repository was archived by the owner on Jan 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 600
/
Copy pathmapper.py
49 lines (38 loc) · 1.55 KB
/
mapper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from .input import BSONInput, KeyValueBSONInput
from .output import BSONOutput, KeyValueBSONOutput
class BSONMapper(object):
"""Wraps BSONInput to allow writing mapper functions
as generators.
"""
def __init__(self, target, **kwargs):
"""`target` should be a generator function that accepts a
single argument which will be an instance of :class:`BSONInput`,
and which yields dictionaries to be emitted. The yielded
dictionaries should conform to the format expected by
:class:`BSONInput` (i.e. they should have the key defined
in a field named `_id`).
Keyword arguments are passed directly to the underlying
:class:`BSONInput`.
"""
output = BSONOutput()
input = BSONInput(**kwargs)
generator = target(input)
for mapped in generator:
output.write(mapped)
class KeyValueBSONMapper(object):
"""Wraps KeyValueBSONInput to allow writing mapper functions
as generators.
"""
def __init__(self, target, **kwargs):
"""`target` should be a generator function that accepts a
single argument which will be an instance of
:class:`KeyValueBSONInput`, and which yields tuples of
(key, value) to be emitted.
Keyword arguments are passed directly to the underlying
:class:`KeyValueBSONInput`.
"""
output = KeyValueBSONOutput()
input = KeyValueBSONInput(**kwargs)
generator = target(input)
for key_and_value in generator:
output.write(key_and_value)