Skip to content

Commit e7adabd

Browse files
committed
Add support for insert Path obj to csv
1 parent e339d46 commit e7adabd

File tree

3 files changed

+26
-19
lines changed

3 files changed

+26
-19
lines changed

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,15 @@ important DataJoint schema or records.
112112

113113
### API docs
114114

115-
The API documentation can be built using sphinx by running
115+
The API documentation can be built with mkdocs using the docker compose file in
116+
`docs/` with the following command:
116117

117118
``` bash
118-
pip install sphinx sphinx_rtd_theme
119-
(cd docs-api/sphinx && make html)
119+
MODE="LIVE" PACKAGE=datajoint UPSTREAM_REPO=https://github.com/datajoint/datajoint-python.git HOST_UID=$(id -u) docker compose -f docs/docker-compose.yaml up --build
120120
```
121121

122-
Generated docs are written to `docs-api/docs/html/index.html`.
123-
More details in [docs-api/README.md](docs-api/README.md).
122+
The site will then be available at `http://localhost/`. When finished, be sure to run
123+
the same command as above, but replace `up --build` with `down`.
124124

125125
## Running Tests Locally
126126
<details>
@@ -141,11 +141,11 @@ HOST_GID=1000
141141
* Add entry in `/etc/hosts` for `127.0.0.1 fakeservices.datajoint.io`
142142
* Run desired tests. Some examples are as follows:
143143

144-
| Use Case | Shell Code |
145-
| ---------------------------- | ------------------------------------------------------------------------------ |
146-
| Run all tests | `nosetests -vsw tests --with-coverage --cover-package=datajoint` |
147-
| Run one specific class test | `nosetests -vs --tests=tests.test_fetch:TestFetch.test_getattribute_for_fetch1` |
148-
| Run one specific basic test | `nosetests -vs --tests=tests.test_external_class:test_insert_and_fetch` |
144+
| Use Case | Shell Code |
145+
| ---------------------------- | ------------------------------------------------------------------------------ |
146+
| Run all tests | `nosetests -vsw tests --with-coverage --cover-package=datajoint` |
147+
| Run one specific class test | `nosetests -vs --tests=tests.test_fetch:TestFetch.test_getattribute_for_fetch1` |
148+
| Run one specific basic test | `nosetests -vs --tests=tests.test_external_class:test_insert_and_fetch` |
149149

150150

151151
### Launch Docker Terminal

datajoint/table.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import pandas
77
import logging
88
import uuid
9+
import csv
910
import re
1011
from pathlib import Path
1112
from .settings import config
@@ -345,13 +346,17 @@ def insert(
345346
"""
346347
Insert a collection of rows.
347348
348-
:param rows: An iterable where an element is a numpy record, a dict-like object, a
349-
pandas.DataFrame, a sequence, or a query expression with the same heading as self.
349+
:param rows: Either (a) an iterable where an element is a numpy record, a
350+
dict-like object, a pandas.DataFrame, a sequence, or a query expression with
351+
the same heading as self, or (b) a pathlib.Path object specifying a path
352+
relative to the current directory with a CSV file, the contents of which
353+
will be inserted.
350354
:param replace: If True, replaces the existing tuple.
351355
:param skip_duplicates: If True, silently skip duplicate inserts.
352-
:param ignore_extra_fields: If False, fields that are not in the heading raise error.
353-
:param allow_direct_insert: applies only in auto-populated tables. If False (default),
354-
insert are allowed only from inside the make callback.
356+
:param ignore_extra_fields: If False, fields that are not in the heading raise
357+
error.
358+
:param allow_direct_insert: applies only in auto-populated tables. If False
359+
(default), insert are allowed only from inside the make callback.
355360
356361
Example:
357362
@@ -366,6 +371,10 @@ def insert(
366371
drop=len(rows.index.names) == 1 and not rows.index.names[0]
367372
).to_records(index=False)
368373

374+
if isinstance(rows, Path):
375+
with open(rows, newline="") as data_file:
376+
rows = list(csv.DictReader(data_file, delimiter=","))
377+
369378
# prohibit direct inserts into auto-populated tables
370379
if not allow_direct_insert and not getattr(self, "_allow_insert", True):
371380
raise DataJointError(

tests/test_university.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,9 @@ def test_activate():
3333
Enroll,
3434
Grade,
3535
):
36-
import csv
36+
from pathlib import Path
3737

38-
with open("./data/" + table.__name__ + ".csv") as f:
39-
reader = csv.DictReader(f)
40-
table().insert(reader)
38+
table().insert(Path("./data/" + table.__name__ + ".csv"))
4139

4240

4341
def test_fill():

0 commit comments

Comments
 (0)