Skip to content
This repository was archived by the owner on Jun 15, 2024. It is now read-only.
Merged
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
78 changes: 49 additions & 29 deletions src/python/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@


def create_experiments_and_inventories(parsed_experiments, collection):
"""Compiles a dictionary of cript Experiment objects. If a parsed experiment is able to be turned
into an Experiment object it is added to an experiments dictionary and that dictionary is returned.
"""Compiles dictionaries of CRIPT Experiment objects and CRIPT Inventory objects. If a parsed experiment/inventory is able to be turned
into an Experiment/Inventory object it is added to an experiments dictionary and that dictionary is returned.
parsed_...-dict of dicts
group-object
collection-object
Expand All @@ -25,13 +25,13 @@ def create_experiments_and_inventories(parsed_experiments, collection):
cell_type = parsed_cell["type"]
cell_key = parsed_cell["key"]
cell_value = parsed_cell["value"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend stripping it of white spaces as well

# Only attribute types should be in Experiment
# Only attribute and that specific identifier should be in experiment
if cell_type == "attribute":
experiment_dict[cell_key] = cell_value
inventory_dict[cell_key] = cell_value
if cell_type == "identifier":
elif cell_type == "identifier":
if cell_key == "Experiment or Inventory":
if cell_value == "I":
if cell_value.lower() == "i":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is fine, but if we wanted to make the code shorter we could do

inventory = cell_value.lower() == "i"

inventory = True
if inventory:
invObj = _create_object(cript.Inventory, inventory_dict, parsed_cell)
Expand All @@ -49,8 +49,8 @@ def create_experiments_and_inventories(parsed_experiments, collection):
def create_citations(parsed_citations, group):
"""Compiles dictionaries with Data and File cript objects.
parsed_...-dict of dicts
group-obj
returns-tuple of dicts of objs
group-cript Group node
returns-tuple of dicts of reference nodes and citation nodes
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good docstrings!

"""

references = {}
Expand Down Expand Up @@ -176,7 +176,7 @@ def create_materials(parsed_materials, project, data, citations):

elif cell_type == "property":
if parsed_cell["key"] == "use_existing":
use_existing = cellToBool(parsed_cell["value"])
use_existing = is_cell_true(parsed_cell["value"])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

beautiful!

continue
property = _create_property(parsed_cell, data, citations)
material_dict["properties"].append(property)
Expand All @@ -189,11 +189,11 @@ def create_materials(parsed_materials, project, data, citations):

try:
# try to get the material using its name
name_ = parsed_material["name"]["value"]
newProject = cript.Project.get(
mat_name = parsed_material["name"]["value"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not have known what mat_name if it wasn't for the comment. How come we didn't go with material_name ?

new_project = cript.Project.get(
name=parsed_material["use_existing"]["value"]
)
material = cript.Material.get(name=name_, project=newProject.uid)
material = cript.Material.get(name=mat_name, project=new_project.uid)

# If there is a get error add it to the errors sheet
except ValueError as e:
Expand All @@ -205,23 +205,11 @@ def create_materials(parsed_materials, project, data, citations):
# If the material had a successful GET request, add properties, identifiers,
# and select attributes as written in the excel
else:
if newProject.name != project.name:
material.project = project
material.url = None
material.uid = None
if material.group.name != project.group.name:
for property in material.properties:
property.citations = []
material.group = project.group

newProperties = []
for property in material.properties:
if "+" not in property.key:
newProperties.append(property)
material.properties = newProperties

for property in material_dict["properties"]:
material = copyMaterial(material, new_project, project)

# Add properties,identifiers, and attributes to material
for property in material_dict["properties"]:
material.add_property(property)
for identifier in material_dict["identifiers"]:
material.add_identifier(identifier)
Expand All @@ -236,6 +224,7 @@ def create_materials(parsed_materials, project, data, citations):
material.notes += material_dict["notes"]
else:
material.notes = material_dict["notes"]

# create new material object otherwise
else:
material = _create_object(cript.Material, material_dict, parsed_cell)
Expand Down Expand Up @@ -481,7 +470,7 @@ def _create_property(parsed_property, data, citations):
property_dict["conditions"].append(condition)

elif cell_type == "method":
if cellToBool(parsed_cell["value"]):
if is_cell_true(parsed_cell["value"]):
property_dict["method"] = parsed_cell["key"]

elif cell_type == "relation":
Expand Down Expand Up @@ -578,6 +567,37 @@ def _get_relation(related_objs, cell_value, parsed_cell):
return None


def cellToBool(val):
def is_cell_true(val):
"""Converts a cell value to a useable boolean"""
return True if str(val).lower() != "false" else False
return str(val).lower() != "false"


def copyMaterial(material, new_project, project):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

beautiful function!
✅ great docstrings!
✅ great comments!
✅ clear self documenting code!

great job all around!

The only thing that I could think to make it even better would be if we had python typing and docstrings in either Google or Pep style

"""
Takes a material node and adjusts values to get rid of legacy code and incompatible features
inputs:
material - cript material node
new_project - cript project node
project - cript project node

returns - cript material node
"""
if new_project.name != project.name:
# Sets new project and gets rid of url and uid to make new node object
material.project = project
material.url = None
material.uid = None
# Gets rid of citations that would cause permissions errors
if material.group.name != project.group.name:
for property in material.properties:
property.citations = []
material.group = project.group

newProperties = []
# Gets rid of any legacy properties/custom that won't upload
for property in material.properties:
if "+" not in property.key:
newProperties.append(property)
material.properties = newProperties

return material