Skip to content

Commit 61744ee

Browse files
4.0 neo4j import object exposure (#364)
* explicit and nice imports * test first unit tests for imports of public neo4j objects and functions * enabled public api from neo4j import Moved READ_ACCESS and WRITE_ACCESS to live in neo4j.api due to circular import issue. * updated test READ_ACCESS and WRITE_ACCESS import test is now ok * fixed import issues with external dependencies the setup.py imports neo4j.meta that means that dependencies have to be internally imported made the pytz imports internal
1 parent af386ac commit 61744ee

File tree

9 files changed

+254
-31
lines changed

9 files changed

+254
-31
lines changed

neo4j/__init__.py

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121

2222
__all__ = [
2323
"__version__",
24-
"READ_ACCESS",
25-
"WRITE_ACCESS",
2624
"GraphDatabase",
2725
"Driver",
2826
"BoltDriver",
@@ -32,18 +30,50 @@
3230
]
3331

3432
from logging import getLogger
35-
from urllib.parse import urlparse, parse_qs
36-
37-
from neo4j.addressing import Address
38-
from neo4j.api import *
39-
from neo4j.conf import Config, PoolConfig
33+
from urllib.parse import (
34+
urlparse,
35+
parse_qs,
36+
)
37+
38+
from neo4j.addressing import (
39+
Address,
40+
IPv4Address,
41+
IPv6Address,
42+
)
43+
from neo4j.api import (
44+
Auth,
45+
AuthToken,
46+
basic_auth,
47+
kerberos_auth,
48+
custom_auth,
49+
Bookmark,
50+
ServerInfo,
51+
Version,
52+
READ_ACCESS,
53+
WRITE_ACCESS,
54+
)
55+
from neo4j.conf import (
56+
Config,
57+
PoolConfig,
58+
)
59+
from neo4j.meta import (
60+
experimental,
61+
get_user_agent,
62+
version as __version__,
63+
)
64+
from neo4j.data import (
65+
Record,
66+
)
67+
from neo4j.work.simple import (
68+
Transaction,
69+
Result,
70+
ResultSummary,
71+
Query,
72+
Session,
73+
SessionConfig,
74+
unit_of_work,
75+
)
4076
from neo4j.exceptions import ServiceUnavailable
41-
from neo4j.meta import experimental, get_user_agent, version as __version__
42-
43-
44-
READ_ACCESS = "READ"
45-
WRITE_ACCESS = "WRITE"
46-
4777

4878
log = getLogger("neo4j")
4979

neo4j/api.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,7 @@ def from_bytes(cls, b):
159159
if b[0] != 0 or b[1] != 0:
160160
raise ValueError("First two bytes must contain zero")
161161
return Version(b[-1], b[-2])
162+
163+
164+
READ_ACCESS = "READ"
165+
WRITE_ACCESS = "WRITE"

neo4j/io/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ def ensure_routing_table_is_fresh(self, access_mode):
662662
663663
:return: `True` if an update was required, `False` otherwise.
664664
"""
665-
from neo4j import READ_ACCESS
665+
from neo4j.api import READ_ACCESS
666666
if self.routing_table.is_fresh(readonly=(access_mode == READ_ACCESS)):
667667
return False
668668
with self.refresh_lock:
@@ -676,7 +676,7 @@ def ensure_routing_table_is_fresh(self, access_mode):
676676
return True
677677

678678
def _select_address(self, access_mode=None):
679-
from neo4j import READ_ACCESS
679+
from neo4j.api import READ_ACCESS
680680
""" Selects the address with the fewest in-use connections.
681681
"""
682682
self.ensure_routing_table_is_fresh(access_mode)
@@ -695,7 +695,8 @@ def _select_address(self, access_mode=None):
695695
return choice(addresses_by_usage[min(addresses_by_usage)])
696696

697697
def acquire(self, access_mode=None, timeout=None):
698-
from neo4j import READ_ACCESS, WRITE_ACCESS
698+
from neo4j.api import WRITE_ACCESS
699+
from neo4j.api import READ_ACCESS
699700
if access_mode is None:
700701
access_mode = WRITE_ACCESS
701702
if access_mode not in (READ_ACCESS, WRITE_ACCESS):

neo4j/time/hydration.py

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,32 @@
1919
# limitations under the License.
2020

2121

22-
from datetime import time, datetime, timedelta
23-
24-
from pytz import FixedOffset, timezone, utc
22+
from datetime import (
23+
time,
24+
datetime,
25+
timedelta,
26+
)
2527

2628
from neo4j.packstream import Structure
27-
from neo4j.time import Duration, Date, Time, DateTime
29+
from neo4j.time import (
30+
Duration,
31+
Date,
32+
Time,
33+
DateTime,
34+
)
35+
36+
37+
def get_date_unix_epoch():
38+
return Date(1970, 1, 1)
39+
40+
41+
def get_date_unix_epoch_ordinal():
42+
return get_date_unix_epoch().to_ordinal()
2843

2944

30-
UNIX_EPOCH_DATE = Date(1970, 1, 1)
31-
UNIX_EPOCH_DATE_ORDINAL = UNIX_EPOCH_DATE.to_ordinal()
32-
UNIX_EPOCH_DATETIME_UTC = DateTime(1970, 1, 1, 0, 0, 0, utc)
45+
def get_datetime_unix_epoch_utc():
46+
from pytz import utc
47+
return DateTime(1970, 1, 1, 0, 0, 0, utc)
3348

3449

3550
def hydrate_date(days):
@@ -38,7 +53,7 @@ def hydrate_date(days):
3853
:param days:
3954
:return: Date
4055
"""
41-
return Date.from_ordinal(UNIX_EPOCH_DATE_ORDINAL + days)
56+
return Date.from_ordinal(get_date_unix_epoch_ordinal() + days)
4257

4358

4459
def dehydrate_date(value):
@@ -48,7 +63,7 @@ def dehydrate_date(value):
4863
:type value: Date
4964
:return:
5065
"""
51-
return Structure(b"D", value.toordinal() - UNIX_EPOCH_DATE.toordinal())
66+
return Structure(b"D", value.toordinal() - get_date_unix_epoch().toordinal())
5267

5368

5469
def hydrate_time(nanoseconds, tz=None):
@@ -58,6 +73,7 @@ def hydrate_time(nanoseconds, tz=None):
5873
:param tz:
5974
:return: Time
6075
"""
76+
from pytz import FixedOffset
6177
seconds, nanoseconds = map(int, divmod(nanoseconds, 1000000000))
6278
minutes, seconds = map(int, divmod(seconds, 60))
6379
hours, minutes = map(int, divmod(minutes, 60))
@@ -98,11 +114,12 @@ def hydrate_datetime(seconds, nanoseconds, tz=None):
98114
:param tz:
99115
:return: datetime
100116
"""
117+
from pytz import FixedOffset, timezone
101118
minutes, seconds = map(int, divmod(seconds, 60))
102119
hours, minutes = map(int, divmod(minutes, 60))
103120
days, hours = map(int, divmod(hours, 24))
104121
seconds = (1000000000 * seconds + nanoseconds) / 1000000000
105-
t = DateTime.combine(Date.from_ordinal(UNIX_EPOCH_DATE_ORDINAL + days), Time(hours, minutes, seconds))
122+
t = DateTime.combine(Date.from_ordinal(get_date_unix_epoch_ordinal() + days), Time(hours, minutes, seconds))
106123
if tz is None:
107124
return t
108125
if isinstance(tz, int):
@@ -131,6 +148,7 @@ def seconds_and_nanoseconds(dt):
131148
tz = value.tzinfo
132149
if tz is None:
133150
# without time zone
151+
from pytz import utc
134152
value = utc.localize(value)
135153
seconds, nanoseconds = seconds_and_nanoseconds(value)
136154
return Structure(b"d", seconds, nanoseconds)

neo4j/work/simple.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from time import perf_counter, sleep
2626
from warnings import warn
2727

28-
from neo4j import READ_ACCESS, WRITE_ACCESS
28+
from neo4j.api import READ_ACCESS, WRITE_ACCESS
2929
from neo4j.conf import DeprecatedAlias
3030
from neo4j.data import DataHydrator, DataDehydrator
3131
from neo4j.exceptions import (

tests/integration/test_bookmarking.py

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

2222
from uuid import uuid4
2323

24-
from neo4j import WRITE_ACCESS, READ_ACCESS
24+
from neo4j.api import READ_ACCESS, WRITE_ACCESS
2525
from neo4j.graph import Node
2626

2727

tests/stub/test_bookmarking.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323

2424
from neo4j import (
2525
GraphDatabase,
26-
READ_ACCESS,
2726
)
27+
from neo4j.api import READ_ACCESS
2828

2929
from tests.stub.conftest import StubCluster
3030

tests/stub/test_routingdriver.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@
2323

2424
from neo4j import (
2525
GraphDatabase,
26-
READ_ACCESS,
27-
WRITE_ACCESS,
2826
Neo4jDriver,
2927
)
28+
from neo4j.api import READ_ACCESS, WRITE_ACCESS
3029

3130
from neo4j._exceptions import BoltRoutingError
3231

0 commit comments

Comments
 (0)