Skip to content

Commit

Permalink
Fix code smell
Browse files Browse the repository at this point in the history
  • Loading branch information
Marius Conjeaud committed Aug 24, 2023
1 parent 9998c45 commit b138b55
Showing 1 changed file with 15 additions and 25 deletions.
40 changes: 15 additions & 25 deletions neomodel/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
from neo4j import DEFAULT_DATABASE, GraphDatabase, basic_auth
from neo4j.api import Bookmarks
from neo4j.exceptions import ClientError, ServiceUnavailable, SessionExpired
from neo4j.graph import Node, Relationship
from neo4j.graph import Path
from neo4j.graph import Node, Path, Relationship

from neomodel import config, core
from neomodel.exceptions import (
Expand All @@ -22,9 +21,9 @@
UniqueProperty,
)


logger = logging.getLogger(__name__)


# make sure the connection url has been set prior to executing the wrapped function
def ensure_connection(func):
def wrapper(self, *args, **kwargs):
Expand Down Expand Up @@ -99,10 +98,7 @@ def set_connection(self, url):
"neo4j+ssc",
]

if (
parsed_url.netloc.find("@") > -1
and parsed_url.scheme in valid_schemas
):
if parsed_url.netloc.find("@") > -1 and parsed_url.scheme in valid_schemas:
credentials, hostname = parsed_url.netloc.rsplit("@", 1)
username, password = credentials.split(":")
password = unquote(password)
Expand Down Expand Up @@ -134,9 +130,7 @@ def set_connection(self, url):
self.url = url
self._pid = os.getpid()
self._active_transaction = None
self._database_name = (
DEFAULT_DATABASE if database_name == "" else database_name
)
self._database_name = DEFAULT_DATABASE if database_name == "" else database_name

# Getting the information about the database version requires a connection to the database
self._database_version = None
Expand Down Expand Up @@ -261,7 +255,7 @@ def _object_resolution(self, object_to_resolve):
returned by cypher_query.
The function operates recursively in order to be able to resolve Nodes
within nested list structures and Path objects. Not meant to be called
within nested list structures and Path objects. Not meant to be called
directly, used primarily by _result_resolution.
:param object_to_resolve: A result as returned by cypher_query.
Expand All @@ -271,7 +265,7 @@ def _object_resolution(self, object_to_resolve):
"""
# Below is the original comment that came with the code extracted in
# this method. It is not very clear but I decided to keep it just in
# case
# case
#
#
# For some reason, while the type of `a_result_attribute[1]`
Expand All @@ -293,11 +287,12 @@ def _object_resolution(self, object_to_resolve):

if isinstance(object_to_resolve, Path):
from .path import NeomodelPath

return NeomodelPath(object_to_resolve)

if isinstance(object_to_resolve, list):
return self._result_resolution([object_to_resolve])

return object_to_resolve

def _result_resolution(self, result_list):
Expand Down Expand Up @@ -408,9 +403,7 @@ def _run_cypher_query(
# Retrieve the data
start = time.time()
response = session.run(query, params)
results, meta = [
list(r.values()) for r in response
], response.keys()
results, meta = [list(r.values()) for r in response], response.keys()
end = time.time()

if resolve_objects:
Expand Down Expand Up @@ -482,9 +475,7 @@ def list_constraints(self) -> Sequence[dict]:
Sequence[dict]: List of dictionaries, each entry being a constraint definition
"""
constraints, meta_constraints = self.cypher_query("SHOW CONSTRAINTS")
constraints_as_dict = [
dict(zip(meta_constraints, row)) for row in constraints
]
constraints_as_dict = [dict(zip(meta_constraints, row)) for row in constraints]

return constraints_as_dict

Expand All @@ -506,12 +497,11 @@ def __exit__(self, exc_type, exc_value, traceback):
if exc_value:
self.db.rollback()

if exc_type is ClientError:
if (
exc_value.code
== "Neo.ClientError.Schema.ConstraintValidationFailed"
):
raise UniqueProperty(exc_value.message)
if (
exc_type is ClientError
and exc_value.code == "Neo.ClientError.Schema.ConstraintValidationFailed"
):
raise UniqueProperty(exc_value.message)

if not exc_value:
self.last_bookmark = self.db.commit()
Expand Down

0 comments on commit b138b55

Please sign in to comment.