Skip to content

Commit

Permalink
trim target names
Browse files Browse the repository at this point in the history
  • Loading branch information
Robbie1977 authored Oct 23, 2024
1 parent 0fbd4d6 commit f312382
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/uk/ac/ebi/vfb/neo4j/flybase2neo/fb_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,18 +132,25 @@ def cypher_escape(value):
# For numbers and booleans
return str(value).lower()

# **Preprocess the statement_template to remove spaces inside placeholders**
# **Preprocess the statement_template to remove spaces inside placeholders and within placeholder names**
import re
statement_template = re.sub(r'{\s*([^}]+?)\s*}', r'{\1}', statement_template)

def clean_placeholders(template):
# Match placeholders and remove spaces inside braces and within placeholder names
def replacer(match):
placeholder = match.group(1)
cleaned_placeholder = placeholder.strip()
return '{' + cleaned_placeholder + '}'
return re.sub(r'{([^}]*)}', replacer, template)

statement_template = clean_placeholders(statement_template)

batch_size = 1000 # Adjust batch size as needed
for batch in chunks(dict_list, batch_size):
statements = []
for line in batch:
# Escape and substitute values into the statement template
escaped_values = {key: cypher_escape(value) for key, value in line.items()}
# Create a new dictionary with 'line.' prefixed to keys
escaped_line = {'line.' + key: value for key, value in escaped_values.items()}
escaped_line = {key.strip(): cypher_escape(value) for key, value in line.items()}

try:
statement = statement_template.format(**escaped_line)
Expand All @@ -161,6 +168,7 @@ def cypher_escape(value):
self.nc.commit_list(statements)



def close(self):
self.conn.close() # Investigate implementing using with statement. Then method not required.

Expand Down

0 comments on commit f312382

Please sign in to comment.