We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
feature: add a load function in high level
Add a load function in `high_level.py` so the data from sources can be loaded.
After completing this, #555 can be worked on.

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `dffml/high_level.py`
Content:
```
1 """
2 High level abstraction interfaces to DFFML. These are probably going to be used
3 in a lot of quick and dirty python files.
4 """
5 import pathlib
6 from typing import Union, Dict, Any
7 
8 from .record import Record
9 from .source.source import Sources, BaseSource
10 from .source.memory import MemorySource, MemorySourceConfig
11 
12 
13 def _records_to_sources(*args):
14     """
15     Create a memory source out of any records passed as a variable length list.
16     Add all sources found in the variable length list to a list of sources, and
17     the created source containing records, and return that list of sources.
18     """
19     # If the first arg is an instance of sources, append the rest to that.
20     if args and isinstance(args[0], Sources):
21         sources = args[0]
22     else:
23         sources = Sources(
24             *[arg for arg in args if isinstance(arg, BaseSource)]
25         )
26     # Records to add to memory source
27     records = []
28     # Make args mutable
29     args = list(args)
30     # Convert dicts to records
31     for i, arg in enumerate(args):
32         if isinstance(arg, dict):
33             arg = Record(i, data={"features": arg})
34         if isinstance(arg, Record):
35             records.append(arg)
36         if isinstance(arg, str) and "." in arg:
37             filepath = pathlib.Path(arg)
38             source = BaseSource.load(filepath.suffix.replace(".", ""))
39             sources.append(source(filename=arg))
40     # Create memory source if there are any records
41     if records:
42         sources.append(MemorySource(MemorySourceConfig(records=records)))
43     return sources
44 
45 
46 async def save(source: BaseSource, *args: Record) -> None:
47     """
48     Update a source's knowledge about given records.
49 
50     For each record given, call
51     :py:func:`update <dffml.source.source.BaseSourceContext.update>` on the
52     source. Effectively saving all the records to the source.
53 
54     Parameters
55     ----------
56     source : BaseSource
57         Data source to use. See :doc:`/plugins/dffml_source` for sources and
58         options.
59     *args : list
60         Records to be saved.
61 
62     Examples
63     --------
64 
65     >>> source = CSVSource(filename="save.csv", allowempty=True, readwrite=True)
66     >>>
67     >>> async def main():
68     ...     await save(
69     ...         source,
70     ...         Record(
71     ...             "myrecord",
72     ...             data={
73     ...                 "features": {"Years": 0, "Expertise": 1, "Trust": 0.1},
74     ...                 "prediction": {"Salary": {"value": 10, "confidence": 1.0}},
75     ...             }
76     ...         )
77     ...     )
78     ...     print(pathlib.Path("save.csv").read_text().strip())
79     >>>
80     >>> asyncio.run(main())
81     key,tag,Expertise,Trust,Years,prediction_Salary,confidence_Salary
82     myrecord,untagged,1,0.1,0,10,1.0
83     """
84     async with source:
85         async with source() as sctx:
86             for record in args:
87                 await sctx.update(record)
88 
89 
90 async def train(model, *args: Union[BaseSource, Record, Dict[str, Any]]):
91     """
92     Train a machine learning model.
93 
94     Provide records to the model to train it. The model should be already
95     instantiated.
96 
97     Parameters
98     ----------
99     model : Model
100         Machine Learning model to use. See :doc:`/plugins/dffml_model` for
101         models options.
102     *args : list
103         Input data for training. Could be a ``dict``, :py:class:`Record`,
104         filename, one of the data :doc:`/plugins/dffml_source`, or a filename
105         with the extension being one of the data sources.
106 
107     Examples
108     --------
109 
110     >>> model = LinearRegressionModel(
111     ...     features=Features(
112     ...         DefFeature("Years", int, 1),
113     ...         DefFeature("Expertise", int, 1),
114     ...         DefFeature("Trust", float, 1),
115     ...     ),
116     ...     predict=DefFeature("Salary", int, 1),
117     ... )
118     >>>
119     >>> async def main():
120     ...     await train(
121     ...         model,
122     ...         {"Years": 0, "Expertise": 1, "Trust": 0.1, "Salary": 10},
123     ...         {"Years": 1, "Expertise": 3, "Trust": 0.2, "Salary": 20},
124     ...         {"Years": 2, "Expertise": 5, "Trust": 0.3, "Salary": 30},
125     ...         {"Years": 3, "Expertise": 7, "Trust": 0.4, "Salary": 40},
126     ...     )
127     >>>
128     >>> asyncio.run(main())
129     """
130     sources = _records_to_sources(*args)
131     async with sources as sources, model as model:
132         async with sources() as sctx, model() as mctx:
133             return await mctx.train(sctx)
134 
135 
136 async def accuracy(
137     model, *args: Union[BaseSource, Record, Dict[str, Any]]
138 ) -> float:
139     """
140     Assess the accuracy of a machine learning model.
141 
142     Provide records to the model to assess the percent accuracy of its
143     prediction abilities. The model should be already instantiated and trained.
144 
145     Parameters
146     ----------
147     model : Model
148         Machine Learning model to use. See :doc:`/plugins/dffml_model` for
149         models options.
150     *args : list
151         Input data for training. Could be a ``dict``, :py:class:`Record`,
152         filename, one of the data :doc:`/plugins/dffml_source`, or a filename
153         with the extension being one of the data sources.
154 
155     Returns
156     -------
157     float
158         A decimal value representing the percent of the time the model made the
159         correct prediction. For some models this has another meaning. Please see
160         the documentation for the model your using for further details.
161 
162     Examples
163     --------
164 
165     >>> model = LinearRegressionModel(
166     ...     features=Features(
167     ...         DefFeature("Years", int, 1),
168     ...         DefFeature("Expertise", int, 1),
169     ...         DefFeature("Trust", float, 1),
170     ...     ),
171     ...     predict=DefFeature("Salary", int, 1),
172     ... )
173     >>>
174     >>> async def main():
175     ...     print(
176     ...         "Accuracy:",
177     ...         await accuracy(
178     ...             model,
179     ...             {"Years": 4, "Expertise": 9, "Trust": 0.5, "Salary": 50},
180     ...             {"Years": 5, "Expertise": 11, "Trust": 0.6, "Salary": 60},
181     ...         ),
182     ...     )
183     >>>
184     >>> asyncio.run(main())
185     Accuracy: 1.0
186     """
187     sources = _records_to_sources(*args)
188     async with sources as sources, model as model:
189         async with sources() as sctx, model() as mctx:
190             return float(await mctx.accuracy(sctx))
191 
192 
193 async def predict(
194     model,
195     *args: Union[BaseSource, Record, Dict[str, Any]],
196     update: bool = False,
197     keep_record: bool = False,
198 ):
199     """
200     Make a prediction using a machine learning model.
201 
202     The model must be trained before using it to make a prediction.
203 
204     Parameters
205     ----------
206     model : Model
207         Machine Learning model to use. See :doc:`/plugins/dffml_model` for
208         models options.
209     *args : list
210         Input data for prediction. Could be a ``dict``, :py:class:`Record`,
211         filename, or one of the data :doc:`/plugins/dffml_source`.
212     update : boolean, optional
213         If ``True`` prediction data within records will be written back to all
214         sources given. Defaults to ``False``.
215     keep_record : boolean, optional
216         If ``True`` the results will be kept as their ``Record`` objects instead
217         of being converted to a ``(record.key, features, predictions)`` tuple.
218         Defaults to ``False``.
219 
220     Returns
221     -------
222     asynciterator
223         ``Record`` objects or ``(record.key, features, predictions)`` tuple.
224 
225     Examples
226     --------
227 
228     >>> model = LinearRegressionModel(
229     ...     features=Features(
230     ...         DefFeature("Years", int, 1),
231     ...         DefFeature("Expertise", int, 1),
232     ...         DefFeature("Trust", float, 1),
233     ...     ),
234     ...     predict=DefFeature("Salary", int, 1),
235     ... )
236     >>>
237     >>> async def main():
238     ...     async for i, features, prediction in predict(
239     ...         model,
240     ...         {"Years": 6, "Expertise": 13, "Trust": 0.7},
241     ...         {"Years": 7, "Expertise": 15, "Trust": 0.8},
242     ...     ):
243     ...         features["Salary"] = round(prediction["Salary"]["value"])
244     ...         print(features)
245     >>>
246     >>> asyncio.run(main())
247     {'Years': 6, 'Expertise': 13, 'Trust': 0.7, 'Salary': 70.0}
248     {'Years': 7, 'Expertise': 15, 'Trust': 0.8, 'Salary': 80.0}
249     """
250     sources = _records_to_sources(*args)
251     async with sources as sources, model as model:
252         async with sources() as sctx, model() as mctx:
253             async for record in mctx.predict(sctx.records()):
254                 yield record if keep_record else (
255                     record.key,
256                     record.features(),
257                     record.predictions(),
258                 )
259                 if update:
260                     await sctx.update(record)
261
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/dffml/high_level.py b/dffml/high_level.py
--- a/dffml/high_level.py
+++ b/dffml/high_level.py
@@ -3,7 +3,7 @@
 in a lot of quick and dirty python files.
 """
 import pathlib
-from typing import Union, Dict, Any
+from typing import Union, Dict, Any, AsyncIterator
 
 from .record import Record
 from .source.source import Sources, BaseSource
@@ -258,3 +258,60 @@
                 )
                 if update:
                     await sctx.update(record)
+
+
+async def load(source: BaseSource, *args: str) -> AsyncIterator[Record]:
+    """
+    Yields records from a source.
+
+    Yields all the records from the source, if record keys are given then only
+    those records are yielded.
+
+    Parameters
+    ----------
+    source : BaseSource
+        Data source to use. See :doc:`/plugins/dffml_source` for sources and
+        options.
+    *args : str
+        Records to be returned. If empty, all the records in a source will be returned.
+
+    Returns
+    -------
+    asynciterator
+        :py:class:`Record <dffml.record.Record>` object
+
+    Examples
+    --------
+
+    >>> source = CSVSource(filename="save.csv", allowempty=True, readwrite=True)
+    >>>
+    >>> async def main():
+    ...     await save(
+    ...         source,
+    ...         Record("1", data={"features": {"A": 0, "B": 1}}),
+    ...         Record("2", data={"features": {"A": 3, "B": 4}}),
+    ...     )
+    ...
+    ...     # All records in source
+    ...     async for record in load(source):
+    ...         print(record.export())
+    ...
+    ...     # For specific records in a source
+    ...     async for record in load(source, "1"):
+    ...         print(record.export())
+    >>>
+    >>> asyncio.run(main())
+    {'key': '1', 'features': {'A': 0, 'B': 1}, 'extra': {}}
+    {'key': '2', 'features': {'A': 3, 'B': 4}, 'extra': {}}
+    {'key': '1', 'features': {'A': 0, 'B': 1}, 'extra': {}}
+    """
+    async with source:
+        async with source() as sctx:
+            if args:
+                # If specific records are to be loaded
+                for record in args:
+                    yield await sctx.record(record)
+            else:
+                # All the records are loaded
+                async for record in sctx.records():
+                    yield record

{"golden_diff": "diff --git a/dffml/high_level.py b/dffml/high_level.py\n--- a/dffml/high_level.py\n+++ b/dffml/high_level.py\n@@ -3,7 +3,7 @@\n in a lot of quick and dirty python files.\n \"\"\"\n import pathlib\n-from typing import Union, Dict, Any\n+from typing import Union, Dict, Any, AsyncIterator\n \n from .record import Record\n from .source.source import Sources, BaseSource\n@@ -258,3 +258,60 @@\n                 )\n                 if update:\n                     await sctx.update(record)\n+\n+\n+async def load(source: BaseSource, *args: str) -> AsyncIterator[Record]:\n+    \"\"\"\n+    Yields records from a source.\n+\n+    Yields all the records from the source, if record keys are given then only\n+    those records are yielded.\n+\n+    Parameters\n+    ----------\n+    source : BaseSource\n+        Data source to use. See :doc:`/plugins/dffml_source` for sources and\n+        options.\n+    *args : str\n+        Records to be returned. If empty, all the records in a source will be returned.\n+\n+    Returns\n+    -------\n+    asynciterator\n+        :py:class:`Record <dffml.record.Record>` object\n+\n+    Examples\n+    --------\n+\n+    >>> source = CSVSource(filename=\"save.csv\", allowempty=True, readwrite=True)\n+    >>>\n+    >>> async def main():\n+    ...     await save(\n+    ...         source,\n+    ...         Record(\"1\", data={\"features\": {\"A\": 0, \"B\": 1}}),\n+    ...         Record(\"2\", data={\"features\": {\"A\": 3, \"B\": 4}}),\n+    ...     )\n+    ...\n+    ...     # All records in source\n+    ...     async for record in load(source):\n+    ...         print(record.export())\n+    ...\n+    ...     # For specific records in a source\n+    ...     async for record in load(source, \"1\"):\n+    ...         print(record.export())\n+    >>>\n+    >>> asyncio.run(main())\n+    {'key': '1', 'features': {'A': 0, 'B': 1}, 'extra': {}}\n+    {'key': '2', 'features': {'A': 3, 'B': 4}, 'extra': {}}\n+    {'key': '1', 'features': {'A': 0, 'B': 1}, 'extra': {}}\n+    \"\"\"\n+    async with source:\n+        async with source() as sctx:\n+            if args:\n+                # If specific records are to be loaded\n+                for record in args:\n+                    yield await sctx.record(record)\n+            else:\n+                # All the records are loaded\n+                async for record in sctx.records():\n+                    yield record\n", "issue": "feature: add a load function in high level\nAdd a load function in `high_level.py` so the data from sources can be loaded.\r\nAfter completing this, #555 can be worked on.\n", "before_files": [{"content": "\"\"\"\nHigh level abstraction interfaces to DFFML. These are probably going to be used\nin a lot of quick and dirty python files.\n\"\"\"\nimport pathlib\nfrom typing import Union, Dict, Any\n\nfrom .record import Record\nfrom .source.source import Sources, BaseSource\nfrom .source.memory import MemorySource, MemorySourceConfig\n\n\ndef _records_to_sources(*args):\n    \"\"\"\n    Create a memory source out of any records passed as a variable length list.\n    Add all sources found in the variable length list to a list of sources, and\n    the created source containing records, and return that list of sources.\n    \"\"\"\n    # If the first arg is an instance of sources, append the rest to that.\n    if args and isinstance(args[0], Sources):\n        sources = args[0]\n    else:\n        sources = Sources(\n            *[arg for arg in args if isinstance(arg, BaseSource)]\n        )\n    # Records to add to memory source\n    records = []\n    # Make args mutable\n    args = list(args)\n    # Convert dicts to records\n    for i, arg in enumerate(args):\n        if isinstance(arg, dict):\n            arg = Record(i, data={\"features\": arg})\n        if isinstance(arg, Record):\n            records.append(arg)\n        if isinstance(arg, str) and \".\" in arg:\n            filepath = pathlib.Path(arg)\n            source = BaseSource.load(filepath.suffix.replace(\".\", \"\"))\n            sources.append(source(filename=arg))\n    # Create memory source if there are any records\n    if records:\n        sources.append(MemorySource(MemorySourceConfig(records=records)))\n    return sources\n\n\nasync def save(source: BaseSource, *args: Record) -> None:\n    \"\"\"\n    Update a source's knowledge about given records.\n\n    For each record given, call\n    :py:func:`update <dffml.source.source.BaseSourceContext.update>` on the\n    source. Effectively saving all the records to the source.\n\n    Parameters\n    ----------\n    source : BaseSource\n        Data source to use. See :doc:`/plugins/dffml_source` for sources and\n        options.\n    *args : list\n        Records to be saved.\n\n    Examples\n    --------\n\n    >>> source = CSVSource(filename=\"save.csv\", allowempty=True, readwrite=True)\n    >>>\n    >>> async def main():\n    ...     await save(\n    ...         source,\n    ...         Record(\n    ...             \"myrecord\",\n    ...             data={\n    ...                 \"features\": {\"Years\": 0, \"Expertise\": 1, \"Trust\": 0.1},\n    ...                 \"prediction\": {\"Salary\": {\"value\": 10, \"confidence\": 1.0}},\n    ...             }\n    ...         )\n    ...     )\n    ...     print(pathlib.Path(\"save.csv\").read_text().strip())\n    >>>\n    >>> asyncio.run(main())\n    key,tag,Expertise,Trust,Years,prediction_Salary,confidence_Salary\n    myrecord,untagged,1,0.1,0,10,1.0\n    \"\"\"\n    async with source:\n        async with source() as sctx:\n            for record in args:\n                await sctx.update(record)\n\n\nasync def train(model, *args: Union[BaseSource, Record, Dict[str, Any]]):\n    \"\"\"\n    Train a machine learning model.\n\n    Provide records to the model to train it. The model should be already\n    instantiated.\n\n    Parameters\n    ----------\n    model : Model\n        Machine Learning model to use. See :doc:`/plugins/dffml_model` for\n        models options.\n    *args : list\n        Input data for training. Could be a ``dict``, :py:class:`Record`,\n        filename, one of the data :doc:`/plugins/dffml_source`, or a filename\n        with the extension being one of the data sources.\n\n    Examples\n    --------\n\n    >>> model = LinearRegressionModel(\n    ...     features=Features(\n    ...         DefFeature(\"Years\", int, 1),\n    ...         DefFeature(\"Expertise\", int, 1),\n    ...         DefFeature(\"Trust\", float, 1),\n    ...     ),\n    ...     predict=DefFeature(\"Salary\", int, 1),\n    ... )\n    >>>\n    >>> async def main():\n    ...     await train(\n    ...         model,\n    ...         {\"Years\": 0, \"Expertise\": 1, \"Trust\": 0.1, \"Salary\": 10},\n    ...         {\"Years\": 1, \"Expertise\": 3, \"Trust\": 0.2, \"Salary\": 20},\n    ...         {\"Years\": 2, \"Expertise\": 5, \"Trust\": 0.3, \"Salary\": 30},\n    ...         {\"Years\": 3, \"Expertise\": 7, \"Trust\": 0.4, \"Salary\": 40},\n    ...     )\n    >>>\n    >>> asyncio.run(main())\n    \"\"\"\n    sources = _records_to_sources(*args)\n    async with sources as sources, model as model:\n        async with sources() as sctx, model() as mctx:\n            return await mctx.train(sctx)\n\n\nasync def accuracy(\n    model, *args: Union[BaseSource, Record, Dict[str, Any]]\n) -> float:\n    \"\"\"\n    Assess the accuracy of a machine learning model.\n\n    Provide records to the model to assess the percent accuracy of its\n    prediction abilities. The model should be already instantiated and trained.\n\n    Parameters\n    ----------\n    model : Model\n        Machine Learning model to use. See :doc:`/plugins/dffml_model` for\n        models options.\n    *args : list\n        Input data for training. Could be a ``dict``, :py:class:`Record`,\n        filename, one of the data :doc:`/plugins/dffml_source`, or a filename\n        with the extension being one of the data sources.\n\n    Returns\n    -------\n    float\n        A decimal value representing the percent of the time the model made the\n        correct prediction. For some models this has another meaning. Please see\n        the documentation for the model your using for further details.\n\n    Examples\n    --------\n\n    >>> model = LinearRegressionModel(\n    ...     features=Features(\n    ...         DefFeature(\"Years\", int, 1),\n    ...         DefFeature(\"Expertise\", int, 1),\n    ...         DefFeature(\"Trust\", float, 1),\n    ...     ),\n    ...     predict=DefFeature(\"Salary\", int, 1),\n    ... )\n    >>>\n    >>> async def main():\n    ...     print(\n    ...         \"Accuracy:\",\n    ...         await accuracy(\n    ...             model,\n    ...             {\"Years\": 4, \"Expertise\": 9, \"Trust\": 0.5, \"Salary\": 50},\n    ...             {\"Years\": 5, \"Expertise\": 11, \"Trust\": 0.6, \"Salary\": 60},\n    ...         ),\n    ...     )\n    >>>\n    >>> asyncio.run(main())\n    Accuracy: 1.0\n    \"\"\"\n    sources = _records_to_sources(*args)\n    async with sources as sources, model as model:\n        async with sources() as sctx, model() as mctx:\n            return float(await mctx.accuracy(sctx))\n\n\nasync def predict(\n    model,\n    *args: Union[BaseSource, Record, Dict[str, Any]],\n    update: bool = False,\n    keep_record: bool = False,\n):\n    \"\"\"\n    Make a prediction using a machine learning model.\n\n    The model must be trained before using it to make a prediction.\n\n    Parameters\n    ----------\n    model : Model\n        Machine Learning model to use. See :doc:`/plugins/dffml_model` for\n        models options.\n    *args : list\n        Input data for prediction. Could be a ``dict``, :py:class:`Record`,\n        filename, or one of the data :doc:`/plugins/dffml_source`.\n    update : boolean, optional\n        If ``True`` prediction data within records will be written back to all\n        sources given. Defaults to ``False``.\n    keep_record : boolean, optional\n        If ``True`` the results will be kept as their ``Record`` objects instead\n        of being converted to a ``(record.key, features, predictions)`` tuple.\n        Defaults to ``False``.\n\n    Returns\n    -------\n    asynciterator\n        ``Record`` objects or ``(record.key, features, predictions)`` tuple.\n\n    Examples\n    --------\n\n    >>> model = LinearRegressionModel(\n    ...     features=Features(\n    ...         DefFeature(\"Years\", int, 1),\n    ...         DefFeature(\"Expertise\", int, 1),\n    ...         DefFeature(\"Trust\", float, 1),\n    ...     ),\n    ...     predict=DefFeature(\"Salary\", int, 1),\n    ... )\n    >>>\n    >>> async def main():\n    ...     async for i, features, prediction in predict(\n    ...         model,\n    ...         {\"Years\": 6, \"Expertise\": 13, \"Trust\": 0.7},\n    ...         {\"Years\": 7, \"Expertise\": 15, \"Trust\": 0.8},\n    ...     ):\n    ...         features[\"Salary\"] = round(prediction[\"Salary\"][\"value\"])\n    ...         print(features)\n    >>>\n    >>> asyncio.run(main())\n    {'Years': 6, 'Expertise': 13, 'Trust': 0.7, 'Salary': 70.0}\n    {'Years': 7, 'Expertise': 15, 'Trust': 0.8, 'Salary': 80.0}\n    \"\"\"\n    sources = _records_to_sources(*args)\n    async with sources as sources, model as model:\n        async with sources() as sctx, model() as mctx:\n            async for record in mctx.predict(sctx.records()):\n                yield record if keep_record else (\n                    record.key,\n                    record.features(),\n                    record.predictions(),\n                )\n                if update:\n                    await sctx.update(record)\n", "path": "dffml/high_level.py"}], "after_files": [{"content": "\"\"\"\nHigh level abstraction interfaces to DFFML. These are probably going to be used\nin a lot of quick and dirty python files.\n\"\"\"\nimport pathlib\nfrom typing import Union, Dict, Any, AsyncIterator\n\nfrom .record import Record\nfrom .source.source import Sources, BaseSource\nfrom .source.memory import MemorySource, MemorySourceConfig\n\n\ndef _records_to_sources(*args):\n    \"\"\"\n    Create a memory source out of any records passed as a variable length list.\n    Add all sources found in the variable length list to a list of sources, and\n    the created source containing records, and return that list of sources.\n    \"\"\"\n    # If the first arg is an instance of sources, append the rest to that.\n    if args and isinstance(args[0], Sources):\n        sources = args[0]\n    else:\n        sources = Sources(\n            *[arg for arg in args if isinstance(arg, BaseSource)]\n        )\n    # Records to add to memory source\n    records = []\n    # Make args mutable\n    args = list(args)\n    # Convert dicts to records\n    for i, arg in enumerate(args):\n        if isinstance(arg, dict):\n            arg = Record(i, data={\"features\": arg})\n        if isinstance(arg, Record):\n            records.append(arg)\n        if isinstance(arg, str) and \".\" in arg:\n            filepath = pathlib.Path(arg)\n            source = BaseSource.load(filepath.suffix.replace(\".\", \"\"))\n            sources.append(source(filename=arg))\n    # Create memory source if there are any records\n    if records:\n        sources.append(MemorySource(MemorySourceConfig(records=records)))\n    return sources\n\n\nasync def save(source: BaseSource, *args: Record) -> None:\n    \"\"\"\n    Update a source's knowledge about given records.\n\n    For each record given, call\n    :py:func:`update <dffml.source.source.BaseSourceContext.update>` on the\n    source. Effectively saving all the records to the source.\n\n    Parameters\n    ----------\n    source : BaseSource\n        Data source to use. See :doc:`/plugins/dffml_source` for sources and\n        options.\n    *args : list\n        Records to be saved.\n\n    Examples\n    --------\n\n    >>> source = CSVSource(filename=\"save.csv\", allowempty=True, readwrite=True)\n    >>>\n    >>> async def main():\n    ...     await save(\n    ...         source,\n    ...         Record(\n    ...             \"myrecord\",\n    ...             data={\n    ...                 \"features\": {\"Years\": 0, \"Expertise\": 1, \"Trust\": 0.1},\n    ...                 \"prediction\": {\"Salary\": {\"value\": 10, \"confidence\": 1.0}},\n    ...             }\n    ...         )\n    ...     )\n    ...     print(pathlib.Path(\"save.csv\").read_text().strip())\n    >>>\n    >>> asyncio.run(main())\n    key,tag,Expertise,Trust,Years,prediction_Salary,confidence_Salary\n    myrecord,untagged,1,0.1,0,10,1.0\n    \"\"\"\n    async with source:\n        async with source() as sctx:\n            for record in args:\n                await sctx.update(record)\n\n\nasync def train(model, *args: Union[BaseSource, Record, Dict[str, Any]]):\n    \"\"\"\n    Train a machine learning model.\n\n    Provide records to the model to train it. The model should be already\n    instantiated.\n\n    Parameters\n    ----------\n    model : Model\n        Machine Learning model to use. See :doc:`/plugins/dffml_model` for\n        models options.\n    *args : list\n        Input data for training. Could be a ``dict``, :py:class:`Record`,\n        filename, one of the data :doc:`/plugins/dffml_source`, or a filename\n        with the extension being one of the data sources.\n\n    Examples\n    --------\n\n    >>> model = LinearRegressionModel(\n    ...     features=Features(\n    ...         DefFeature(\"Years\", int, 1),\n    ...         DefFeature(\"Expertise\", int, 1),\n    ...         DefFeature(\"Trust\", float, 1),\n    ...     ),\n    ...     predict=DefFeature(\"Salary\", int, 1),\n    ... )\n    >>>\n    >>> async def main():\n    ...     await train(\n    ...         model,\n    ...         {\"Years\": 0, \"Expertise\": 1, \"Trust\": 0.1, \"Salary\": 10},\n    ...         {\"Years\": 1, \"Expertise\": 3, \"Trust\": 0.2, \"Salary\": 20},\n    ...         {\"Years\": 2, \"Expertise\": 5, \"Trust\": 0.3, \"Salary\": 30},\n    ...         {\"Years\": 3, \"Expertise\": 7, \"Trust\": 0.4, \"Salary\": 40},\n    ...     )\n    >>>\n    >>> asyncio.run(main())\n    \"\"\"\n    sources = _records_to_sources(*args)\n    async with sources as sources, model as model:\n        async with sources() as sctx, model() as mctx:\n            return await mctx.train(sctx)\n\n\nasync def accuracy(\n    model, *args: Union[BaseSource, Record, Dict[str, Any]]\n) -> float:\n    \"\"\"\n    Assess the accuracy of a machine learning model.\n\n    Provide records to the model to assess the percent accuracy of its\n    prediction abilities. The model should be already instantiated and trained.\n\n    Parameters\n    ----------\n    model : Model\n        Machine Learning model to use. See :doc:`/plugins/dffml_model` for\n        models options.\n    *args : list\n        Input data for training. Could be a ``dict``, :py:class:`Record`,\n        filename, one of the data :doc:`/plugins/dffml_source`, or a filename\n        with the extension being one of the data sources.\n\n    Returns\n    -------\n    float\n        A decimal value representing the percent of the time the model made the\n        correct prediction. For some models this has another meaning. Please see\n        the documentation for the model your using for further details.\n\n    Examples\n    --------\n\n    >>> model = LinearRegressionModel(\n    ...     features=Features(\n    ...         DefFeature(\"Years\", int, 1),\n    ...         DefFeature(\"Expertise\", int, 1),\n    ...         DefFeature(\"Trust\", float, 1),\n    ...     ),\n    ...     predict=DefFeature(\"Salary\", int, 1),\n    ... )\n    >>>\n    >>> async def main():\n    ...     print(\n    ...         \"Accuracy:\",\n    ...         await accuracy(\n    ...             model,\n    ...             {\"Years\": 4, \"Expertise\": 9, \"Trust\": 0.5, \"Salary\": 50},\n    ...             {\"Years\": 5, \"Expertise\": 11, \"Trust\": 0.6, \"Salary\": 60},\n    ...         ),\n    ...     )\n    >>>\n    >>> asyncio.run(main())\n    Accuracy: 1.0\n    \"\"\"\n    sources = _records_to_sources(*args)\n    async with sources as sources, model as model:\n        async with sources() as sctx, model() as mctx:\n            return float(await mctx.accuracy(sctx))\n\n\nasync def predict(\n    model,\n    *args: Union[BaseSource, Record, Dict[str, Any]],\n    update: bool = False,\n    keep_record: bool = False,\n):\n    \"\"\"\n    Make a prediction using a machine learning model.\n\n    The model must be trained before using it to make a prediction.\n\n    Parameters\n    ----------\n    model : Model\n        Machine Learning model to use. See :doc:`/plugins/dffml_model` for\n        models options.\n    *args : list\n        Input data for prediction. Could be a ``dict``, :py:class:`Record`,\n        filename, or one of the data :doc:`/plugins/dffml_source`.\n    update : boolean, optional\n        If ``True`` prediction data within records will be written back to all\n        sources given. Defaults to ``False``.\n    keep_record : boolean, optional\n        If ``True`` the results will be kept as their ``Record`` objects instead\n        of being converted to a ``(record.key, features, predictions)`` tuple.\n        Defaults to ``False``.\n\n    Returns\n    -------\n    asynciterator\n        ``Record`` objects or ``(record.key, features, predictions)`` tuple.\n\n    Examples\n    --------\n\n    >>> model = LinearRegressionModel(\n    ...     features=Features(\n    ...         DefFeature(\"Years\", int, 1),\n    ...         DefFeature(\"Expertise\", int, 1),\n    ...         DefFeature(\"Trust\", float, 1),\n    ...     ),\n    ...     predict=DefFeature(\"Salary\", int, 1),\n    ... )\n    >>>\n    >>> async def main():\n    ...     async for i, features, prediction in predict(\n    ...         model,\n    ...         {\"Years\": 6, \"Expertise\": 13, \"Trust\": 0.7},\n    ...         {\"Years\": 7, \"Expertise\": 15, \"Trust\": 0.8},\n    ...     ):\n    ...         features[\"Salary\"] = round(prediction[\"Salary\"][\"value\"])\n    ...         print(features)\n    >>>\n    >>> asyncio.run(main())\n    {'Years': 6, 'Expertise': 13, 'Trust': 0.7, 'Salary': 70.0}\n    {'Years': 7, 'Expertise': 15, 'Trust': 0.8, 'Salary': 80.0}\n    \"\"\"\n    sources = _records_to_sources(*args)\n    async with sources as sources, model as model:\n        async with sources() as sctx, model() as mctx:\n            async for record in mctx.predict(sctx.records()):\n                yield record if keep_record else (\n                    record.key,\n                    record.features(),\n                    record.predictions(),\n                )\n                if update:\n                    await sctx.update(record)\n\n\nasync def load(source: BaseSource, *args: str) -> AsyncIterator[Record]:\n    \"\"\"\n    Yields records from a source.\n\n    Yields all the records from the source, if record keys are given then only\n    those records are yielded.\n\n    Parameters\n    ----------\n    source : BaseSource\n        Data source to use. See :doc:`/plugins/dffml_source` for sources and\n        options.\n    *args : str\n        Records to be returned. If empty, all the records in a source will be returned.\n\n    Returns\n    -------\n    asynciterator\n        :py:class:`Record <dffml.record.Record>` object\n\n    Examples\n    --------\n\n    >>> source = CSVSource(filename=\"save.csv\", allowempty=True, readwrite=True)\n    >>>\n    >>> async def main():\n    ...     await save(\n    ...         source,\n    ...         Record(\"1\", data={\"features\": {\"A\": 0, \"B\": 1}}),\n    ...         Record(\"2\", data={\"features\": {\"A\": 3, \"B\": 4}}),\n    ...     )\n    ...\n    ...     # All records in source\n    ...     async for record in load(source):\n    ...         print(record.export())\n    ...\n    ...     # For specific records in a source\n    ...     async for record in load(source, \"1\"):\n    ...         print(record.export())\n    >>>\n    >>> asyncio.run(main())\n    {'key': '1', 'features': {'A': 0, 'B': 1}, 'extra': {}}\n    {'key': '2', 'features': {'A': 3, 'B': 4}, 'extra': {}}\n    {'key': '1', 'features': {'A': 0, 'B': 1}, 'extra': {}}\n    \"\"\"\n    async with source:\n        async with source() as sctx:\n            if args:\n                # If specific records are to be loaded\n                for record in args:\n                    yield await sctx.record(record)\n            else:\n                # All the records are loaded\n                async for record in sctx.records():\n                    yield record\n", "path": "dffml/high_level.py"}]}