Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

88: add entities methods create_many, delete, merge #93

Merged
merged 4 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,48 @@
A script that uses CSV data to create an entity list and populate it with entities.
"""

import csv
from csv import DictReader
from pathlib import Path
from uuid import uuid4

from pyodk import Client

if __name__ == "__main__":
project_id = 1
entity_list_name = f"previous_survey_{uuid4()}"
entity_label_field = "first_name"
entity_properties = ("age", "location")
csv_path = Path("./imported_answers.csv")
project_id = 1
entity_label_field = "first_name"
entity_properties = ("age", "location")
csv_path = Path("./imported_answers.csv")


def create_one_at_a_time():
with Client(project_id=project_id) as client, open(csv_path) as csv_file:
# Create the entity list.
client.entity_lists.create(entity_list_name=entity_list_name)
entity_list = client.entity_lists.create(
entity_list_name=f"previous_survey_{uuid4()}"
)
for prop in entity_properties:
client.entity_lists.add_property(name=prop, entity_list_name=entity_list_name)
client.entity_lists.add_property(name=prop, entity_list_name=entity_list.name)

# Create the entities from the CSV data.
for row in csv.DictReader(csv_file):
for row in DictReader(csv_file):
client.entities.create(
label=row[entity_label_field],
data={k: str(v) for k, v in row.items() if k in entity_properties},
entity_list_name=entity_list_name,
entity_list_name=entity_list.name,
)


def create_with_merge():
with Client(project_id=project_id) as client, open(csv_path) as csv_file:
client.entity_lists.default_entity_list_name = f"previous_survey_{uuid4()}"
entity_list = client.entity_lists.create()
client.entities.merge(
data=DictReader(csv_file),
entity_list_name=entity_list.name,
source_label_key=entity_label_field,
source_keys=(entity_label_field, *entity_properties),
)


if __name__ == "__main__":
# create_one_at_a_time()
create_with_merge()
Loading
Loading