Skip to content
This repository was archived by the owner on Aug 25, 2024. It is now read-only.

Commit 64a42ba

Browse files
mhash1mpdxjohnny
authored andcommitted
source: Doctests for BaseSource using MemorySource
Signed-off-by: Hashim.git <[email protected]>
1 parent 0971cbc commit 64a42ba

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1919
`dffml.dataflow.run` operation.
2020
- Support for operations without inputs.
2121
- Partial doctestable examples to `features.py`
22+
- Doctestable examples for `BaseSource`
2223
### Fixed
2324
- New model tutorial mentions file paths that should be edited.
2425
- DataFlow is no longer a dataclass to prevent it from being exported

dffml/source/source.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,64 @@ def __init__(self, parent: "BaseSource") -> None:
2929
async def update(self, record: Record):
3030
"""
3131
Updates a record for a source
32+
33+
Examples
34+
--------
35+
>>> async def main():
36+
... async with MemorySource(records=[]) as source:
37+
... # Open, update, and close
38+
... async with source() as ctx:
39+
... example = Record("one", data=dict(features=dict(feed="face")))
40+
... # ... Update one into our records ...
41+
... await ctx.update(example)
42+
... # Let's check out our records after calling `record` and `update`.
43+
... async for record in ctx.records():
44+
... print(record.export())
45+
>>>
46+
>>> asyncio.run(main())
47+
{'key': 'one', 'features': {'feed': 'face'}, 'extra': {}}
3248
"""
3349

3450
@abc.abstractmethod
3551
async def records(self) -> AsyncIterator[Record]:
3652
"""
3753
Returns a list of records retrieved from self.src
54+
55+
Examples
56+
--------
57+
>>> async def main():
58+
... async with MemorySource(records=[Record("example", data=dict(features=dict(dead="beef")))]) as source:
59+
... async with source() as ctx:
60+
... async for record in ctx.records():
61+
... print(record.export())
62+
>>>
63+
>>> asyncio.run(main())
64+
{'key': 'example', 'features': {'dead': 'beef'}, 'extra': {}}
3865
"""
3966
# mypy ignores AsyncIterator[Record], therefore this is needed
4067
yield Record("") # pragma: no cover
4168

4269
@abc.abstractmethod
4370
async def record(self, key: str):
4471
"""
45-
Get a record from the source or add it if it doesn't exist
72+
Get a record from the source or add it if it doesn't exist.
73+
74+
Examples
75+
--------
76+
>>> async def main():
77+
... async with MemorySource(records=[Record("example", data=dict(features=dict(dead="beef")))]) as source:
78+
... # Open, update, and close
79+
... async with source() as ctx:
80+
... example = await ctx.record("example")
81+
... # Let's also try calling `record` for a record that doesnt exist.
82+
... one = await ctx.record("one")
83+
... await ctx.update(one)
84+
... async for record in ctx.records():
85+
... print(record.export())
86+
>>>
87+
>>> asyncio.run(main())
88+
{'key': 'example', 'features': {'dead': 'beef'}, 'extra': {}}
89+
{'key': 'one', 'extra': {}}
4690
"""
4791

4892

docs/doctest_header.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@
1717
os.chdir(DOCTEST_TEMPDIR)
1818

1919
from dffml_model_scikit import *
20-
from dffml.record import RecordPrediction, RecordData, Record
2120
from dffml import *
2221
from dffml.base import *
22+
from dffml.record import *
2323
from dffml.df.base import *
2424
from dffml.df.types import *
2525
from dffml.df.memory import *
2626
from dffml.util.net import *
2727
from dffml.operation.output import *
2828
from dffml.operation.dataflow import *
29+
from dffml.source.memory import *

0 commit comments

Comments
 (0)