Skip to content

Commit

Permalink
indent fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Robbie1977 committed Sep 18, 2024
1 parent a232432 commit 773663e
Showing 1 changed file with 40 additions and 40 deletions.
80 changes: 40 additions & 40 deletions src/uk/ac/ebi/vfb/neo4j/neo4j_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,46 +74,46 @@ def __init__(self, base_uri, usr, pwd):
if not self.test_connection():
raise Exception("Failed to connect to Neo4j.")

def commit_list(self, statements, return_graphs=False, max_retries=3):
"""Commit a list of statements to Neo4j DB via REST API.
Prints requests status and warnings if any problems with commit.
- statements: list of Cypher statements as strings
- return_graphs: optionally specify graphs to be returned in JSON results.
Errors prompt warnings, not exceptions, and cause return = False.
Returns results list of results or False if any errors are encountered."""

cstatements = []
if return_graphs:
for s in statements:
cstatements.append({'statement': s, "resultDataContents": ["row", "graph"]})
else:
for s in statements:
cstatements.append({'statement': s}) # Rows and columns are returned by default.
payload = {'statements': cstatements}
headers = self.headers

attempt = 0
while attempt < max_retries:
try:
response = requests.post(
url=self.base_uri + self.commit,
auth=(self.usr, self.pwd),
data=json.dumps(payload),
headers=headers
)
if self.rest_return_check(response):
return response.json()['results']
else:
return False
except requests.exceptions.ConnectionError as e:
attempt += 1
print(f"ConnectionError on attempt {attempt}: {e}")
if attempt == max_retries:
print("Max retries reached. Operation failed.")
return False
wait_time = 2 ** attempt # Exponential backoff
print(f"Retrying in {wait_time} seconds...")
time.sleep(wait_time)
def commit_list(self, statements, return_graphs=False, max_retries=3):
"""Commit a list of statements to Neo4j DB via REST API.
Prints requests status and warnings if any problems with commit.
- statements: list of Cypher statements as strings
- return_graphs: optionally specify graphs to be returned in JSON results.
Errors prompt warnings, not exceptions, and cause return = False.
Returns results list of results or False if any errors are encountered."""
cstatements = []
if return_graphs:
for s in statements:
cstatements.append({'statement': s, "resultDataContents": ["row", "graph"]})
else:
for s in statements:
cstatements.append({'statement': s}) # Rows and columns are returned by default.
payload = {'statements': cstatements}
headers = self.headers

attempt = 0
while attempt < max_retries:
try:
response = requests.post(
url=self.base_uri + self.commit,
auth=(self.usr, self.pwd),
data=json.dumps(payload),
headers=headers
)
if self.rest_return_check(response):
return response.json()['results']
else:
return False
except requests.exceptions.ConnectionError as e:
attempt += 1
print(f"ConnectionError on attempt {attempt}: {e}")
if attempt == max_retries:
print("Max retries reached. Operation failed.")
return False
wait_time = 2 ** attempt # Exponential backoff
print(f"Retrying in {wait_time} seconds...")
time.sleep(wait_time)

def commit_list_in_chunks(self, statements, verbose=False, chunk_length=1000):
"""Commit a list of statements to neo4J DB via REST API, split into chunks.
Expand Down

0 comments on commit 773663e

Please sign in to comment.