Skip to content

Commit 3c747f1

Browse files
authored
Merge pull request #118 from hubmapconsortium/shirey/py-to-str-method
Added string_helper.convert_py_obj_to_string method
2 parents 70c4f42 + 799a572 commit 3c747f1

File tree

2 files changed

+50
-40
lines changed

2 files changed

+50
-40
lines changed

hubmap_commons/string_helper.py

Lines changed: 49 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -105,54 +105,64 @@ def convert_str_literal(data_str):
105105
return data_str
106106

107107
"""
108-
Build the property key-value pairs to be used in the Cypher clause for node creation/update
108+
Convert a List of dictionaries to the format used in the database to store a
109+
List or dictionary as a string.
109110
110111
Parameters
111112
----------
112-
entity_data_dict : dict
113-
The target Entity node to be created
114-
115-
This was copied from:
116-
https://github.com/hubmapconsortium/entity-api/blob/1aa6c868df25514f8ac2130005d8080f3fbe229a/src/schema/schema_neo4j_queries.py#L1361
113+
value_obj : a List or a dict
114+
The value to be converted to the Python string format used in the database
115+
to store dictionaries or Lists as a string.
117116
118117
Returns
119118
-------
120119
str
121-
A string representation of the node properties map containing
122-
key-value pairs to be used in Cypher clause
120+
A string representation of the dictionary or List that can be stored as a string in the database
123121
"""
124-
def build_properties_map(entity_data_dict):
122+
def convert_py_obj_to_string(value_obj):
125123
separator = ', '
126124
node_properties_list = []
127125

128-
for key, value in entity_data_dict.items():
129-
if isinstance(value, (int, bool)):
130-
# Treat integer and boolean as is
131-
key_value_pair = f"{key}: {value}"
132-
elif isinstance(value, str):
133-
# Special case is the value is 'TIMESTAMP()' string
134-
# Remove the quotes since neo4j only takes TIMESTAMP() as a function
135-
if value == 'TIMESTAMP()':
136-
key_value_pair = f"{key}: {value}"
126+
if isinstance(value_obj, list):
127+
str_val = '['
128+
first = True
129+
for ent in value_obj:
130+
if not first:
131+
str_val = str_val + separator
137132
else:
138-
# Escape single quote
139-
escaped_str = value.replace("'", r"\'")
140-
# Quote the value
141-
key_value_pair = f"{key}: '{escaped_str}'"
142-
else:
143-
# Convert list and dict to string, retain the original data without removing any control characters
144-
# Will need to call schema_manager.convert_str_literal() to convert the list/dict literal back to object
145-
# Note that schema_manager.convert_str_literal() removes any control characters to avoid SyntaxError
146-
# Must also escape single quotes in the string to build a valid Cypher query
147-
escaped_str = str(value).replace("'", r"\'")
148-
# Also need to quote the string value
149-
key_value_pair = f"{key}: '{escaped_str}'"
150-
151-
# Add to the list
152-
node_properties_list.append(key_value_pair)
153-
154-
# Example: {uuid: 'eab7fd6911029122d9bbd4d96116db9b', rui_location: 'Joe <info>', lab_tissue_sample_id: 'dadsadsd'}
155-
# Note: all the keys are not quoted, otherwise Cypher syntax error
156-
node_properties_map = f"{{ {separator.join(node_properties_list)} }}"
157-
158-
return node_properties_map
133+
first = False
134+
str_val = str_val + convert_py_obj_to_string(ent)
135+
str_val = str_val + ']'
136+
return str_val
137+
else:
138+
for key, value in value_obj.items():
139+
if isinstance(value, (int, bool)):
140+
# Treat integer and boolean as is
141+
key_value_pair = f"'{key}': {value}"
142+
elif isinstance(value, str):
143+
# Special case is the value is 'TIMESTAMP()' string
144+
# Remove the quotes since neo4j only takes TIMESTAMP() as a function
145+
if value == 'TIMESTAMP()':
146+
key_value_pair = f"'{key}': {value}"
147+
else:
148+
# Escape single quote
149+
escaped_str = value.replace("'", r"\'")
150+
# Quote the value
151+
key_value_pair = f"'{key}': '{escaped_str}'"
152+
else:
153+
# Convert list and dict to string, retain the original data without removing any control characters
154+
# Will need to call schema_manager.convert_str_literal() to convert the list/dict literal back to object
155+
# Note that schema_manager.convert_str_literal() removes any control characters to avoid SyntaxError
156+
# Must also escape single quotes in the string to build a valid Cypher query
157+
escaped_str = str(value).replace("'", r"\'")
158+
# Also need to quote the string value
159+
key_value_pair = f"'{key}': '{escaped_str}'"
160+
161+
# Add to the list
162+
node_properties_list.append(key_value_pair)
163+
164+
# Example: {uuid: 'eab7fd6911029122d9bbd4d96116db9b', rui_location: 'Joe <info>', lab_tissue_sample_id: 'dadsadsd'}
165+
# Note: all the keys are not quoted, otherwise Cypher syntax error
166+
node_properties_map = f"{{ {separator.join(node_properties_list)} }}"
167+
168+
return node_properties_map

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setup(
77
name="hubmap-commons",
8-
version="2.1.11",
8+
version="2.1.12",
99
author="HuBMAP Consortium",
1010
author_email="[email protected]",
1111
description="The common utilities used by the HuMBAP web services",

0 commit comments

Comments
 (0)