Skip to content

update documentation #152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Apr 27, 2017
6 changes: 3 additions & 3 deletions neo4j/v1/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from collections import deque
from random import random
from threading import RLock
from time import clock, sleep
from time import time, sleep
from warnings import warn

from neo4j.bolt import ProtocolError, ServiceUnavailable
Expand Down Expand Up @@ -425,7 +425,7 @@ def _run_transaction(self, access_mode, unit_of_work, *args, **kwargs):
RETRY_DELAY_MULTIPLIER,
RETRY_DELAY_JITTER_FACTOR)
last_error = None
t0 = t1 = clock()
t0 = t1 = time()
while t1 - t0 <= self._max_retry_time:
try:
self._create_transaction()
Expand All @@ -436,7 +436,7 @@ def _run_transaction(self, access_mode, unit_of_work, *args, **kwargs):
except (ServiceUnavailable, SessionExpired) as error:
last_error = error
sleep(next(retry_delay))
t1 = clock()
t1 = time()
raise last_error

def read_transaction(self, unit_of_work, *args, **kwargs):
Expand Down
7 changes: 5 additions & 2 deletions neo4j/v1/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,11 @@ def __run__(self, statement, parameters):
result.statement = statement
result.parameters = parameters

self._connection.append(RUN, (statement, parameters), response=run_response)
self._connection.append(PULL_ALL, response=pull_all_response)
try:
self._connection.append(RUN, (statement, parameters), response=run_response)
self._connection.append(PULL_ALL, response=pull_all_response)
except AttributeError:
pass

return result

Expand Down
25 changes: 25 additions & 0 deletions test/examples/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env python
# -*- encoding: utf-8 -*-

# Copyright (c) 2002-2017 "Neo Technology,"
# Network Engine for Objects in Lund AB [http://neotechnology.com]
#
# This file is part of Neo4j.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


if __name__ == "__main__":
from os.path import dirname
from test.tools import run_tests
run_tests(dirname(__file__))
34 changes: 34 additions & 0 deletions test/examples/autocommit_transaction_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env python
# -*- encoding: utf-8 -*-

# Copyright (c) 2002-2017 "Neo Technology,"
# Network Engine for Objects in Lund AB [http://neotechnology.com]
#
# This file is part of Neo4j.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# tag::autocommit-transaction-import[]
from neo4j.v1 import Session;
from base_application import BaseApplication
# end::autocommit-transaction-import[]

class AutocommitTransactionExample(BaseApplication):
def __init__(self, uri, user, password):
super(AutocommitTransactionExample, self).__init__(uri, user, password)

# tag::autocommit-transaction[]
def add_person(self, name):
session = self._driver.session()
session.run( "CREATE (a:Person {name: $name})", {"name": name} )
# end::autocommit-transaction[]
28 changes: 28 additions & 0 deletions test/examples/base_application.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env python
# -*- encoding: utf-8 -*-

# Copyright (c) 2002-2017 "Neo Technology,"
# Network Engine for Objects in Lund AB [http://neotechnology.com]
#
# This file is part of Neo4j.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from neo4j.v1 import GraphDatabase

class BaseApplication(object):
def __init__(self, uri, user, password):
self._driver = GraphDatabase.driver( uri, auth=( user, password ) )

def close(self):
self._driver.close();
36 changes: 36 additions & 0 deletions test/examples/basic_auth_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env python
# -*- encoding: utf-8 -*-

# Copyright (c) 2002-2017 "Neo Technology,"
# Network Engine for Objects in Lund AB [http://neotechnology.com]
#
# This file is part of Neo4j.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# tag::basic-auth-import[]
from neo4j.v1 import GraphDatabase
# end::basic-auth-import[]

class BasicAuthExample:
# tag::basic-auth[]
def __init__(self, uri, user, password):
self._driver = GraphDatabase.driver(uri, auth=(user, password))
# end::basic-auth[]

def close(self):
self._driver.close()

def can_connect(self):
record_list = list(self._driver.session().run("RETURN 1"))
return int(record_list[0][0]) == 1
33 changes: 33 additions & 0 deletions test/examples/config_connection_timeout_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env python
# -*- encoding: utf-8 -*-

# Copyright (c) 2002-2017 "Neo Technology,"
# Network Engine for Objects in Lund AB [http://neotechnology.com]
#
# This file is part of Neo4j.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# tag::config-connection-timeout-import[]
from neo4j.v1 import GraphDatabase
# end::config-connection-timeout-import[]

class ConfigConnectionTimeoutExample:
# tag::config-connection-timeout[]
def __init__(self, uri, user, password):
self._driver = GraphDatabase.driver( uri, auth=( user, password ),
Config.build().withConnectionTimeout( 15, SECONDS ).toConfig() )
# end::config-connection-timeout[]

def close(self):
self._driver.close();
33 changes: 33 additions & 0 deletions test/examples/config_max_retry_time_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env python
# -*- encoding: utf-8 -*-

# Copyright (c) 2002-2017 "Neo Technology,"
# Network Engine for Objects in Lund AB [http://neotechnology.com]
#
# This file is part of Neo4j.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# tag::config-max-retry-time-import[]
from neo4j.v1 import GraphDatabase
# end::config-max-retry-time-import[]

class ConfigMaxRetryTimeExample:
# tag::config-max-retry-time[]
def __init__(self, uri, user, password):
self._driver = GraphDatabase.driver(uri, auth=(user, password),
Config.build().withMaxTransactionRetryTime( 15, SECONDS ).toConfig() )
# end::config-max-retry-time[]

def close(self):
self._driver.close();
33 changes: 33 additions & 0 deletions test/examples/config_trust_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env python
# -*- encoding: utf-8 -*-

# Copyright (c) 2002-2017 "Neo Technology,"
# Network Engine for Objects in Lund AB [http://neotechnology.com]
#
# This file is part of Neo4j.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# tag::config-trust-import[]
from neo4j.v1 import GraphDatabase
# end::config-trust-import[]

class ConfigTrustExample:
# tag::config-trust[]
def __init__(self, uri, user, password):
self._driver = GraphDatabase.driver(uri, auth=(user, password),
Config.build().withTrustStrategy( Config.TrustStrategy.trustSystemCertificates() ).toConfig() )
# end::config-trust[]

def close(self):
self._driver.close();
32 changes: 32 additions & 0 deletions test/examples/config_unencrypted_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env python
# -*- encoding: utf-8 -*-

# Copyright (c) 2002-2017 "Neo Technology,"
# Network Engine for Objects in Lund AB [http://neotechnology.com]
#
# This file is part of Neo4j.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# tag::config-unencrypted-import[]
from neo4j.v1 import GraphDatabase
# end::config-unencrypted-import[]

class ConfigUnencryptedExample:
# tag::config-unencrypted[]
def __init__(self, uri, user, password):
self._driver = GraphDatabase.driver(uri, auth=(user, password), encrypted=False)
# end::config-unencrypted[]

def close(self):
self._driver.close();
32 changes: 32 additions & 0 deletions test/examples/custom_auth_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env python
# -*- encoding: utf-8 -*-

# Copyright (c) 2002-2017 "Neo Technology,"
# Network Engine for Objects in Lund AB [http://neotechnology.com]
#
# This file is part of Neo4j.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# tag::custom-auth-import[]
from neo4j.v1 import GraphDatabase;
# end::custom-auth-import[]

class CustomAuthExample:
# tag::custom-auth[]
def __init__(self, uri, principal, credentials, realm, scheme, parameters):
self._driver = GraphDatabase.driver( uri, auth=(principal, credentials, realm, scheme, parameters))
# end::custom-auth[]

def close(self):
self._driver.close()
42 changes: 42 additions & 0 deletions test/examples/cypher_error_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env python
# -*- encoding: utf-8 -*-

# Copyright (c) 2002-2017 "Neo Technology,"
# Network Engine for Objects in Lund AB [http://neotechnology.com]
#
# This file is part of Neo4j.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# tag::cypher-error-import[]
from neo4j.v1 import GraphDatabase, ClientError
from base_application import BaseApplication
# end::cypher-error-import[]

class CypherErrorExample(BaseApplication):
def __init__(self, uri, user, password):
super(CypherErrorExample, self).__init__(uri, user, password)

# tag::cypher-error[]
def get_employee_number(self, name):
session = self._driver.session()
session.read_transaction(lambda tx: self.select_employee(tx, name))

def select_employee(self, tx, name):
try:
record_list = list(tx.run("SELECT * FROM Employees WHERE name = $name", {"name": name}))
return int(record_list[0]["employee_number"])
except ClientError as e:
print(e.message)
return -1
# end::cypher-error[]
Loading