Skip to content

Commit 85161de

Browse files
authored
Merge pull request #168 from technige/1.2-example-fixes
Idiomaticised examples
2 parents 3964711 + 1830e7b commit 85161de

16 files changed

+101
-89
lines changed

test/examples/autocommit_transaction_example.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,18 @@
1818
# See the License for the specific language governing permissions and
1919
# limitations under the License.
2020

21-
# tag::autocommit-transaction-import[]
22-
from neo4j.v1 import Session;
2321
from test.examples.base_application import BaseApplication
22+
23+
# tag::autocommit-transaction-import[]
2424
# end::autocommit-transaction-import[]
2525

26+
2627
class AutocommitTransactionExample(BaseApplication):
2728
def __init__(self, uri, user, password):
2829
super(AutocommitTransactionExample, self).__init__(uri, user, password)
2930

3031
# tag::autocommit-transaction[]
3132
def add_person(self, name):
32-
session = self._driver.session()
33-
session.run( "CREATE (a:Person {name: $name})", {"name": name} )
33+
with self._driver.session() as session:
34+
session.run("CREATE (a:Person {name: $name})", name=name)
3435
# end::autocommit-transaction[]

test/examples/base_application.py

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

2424
class BaseApplication(object):
2525
def __init__(self, uri, user, password):
26-
self._driver = GraphDatabase.driver(uri, auth=( user, password ), max_retry_time=0)
26+
self._driver = GraphDatabase.driver(uri, auth=(user, password), max_retry_time=0)
2727

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

test/examples/basic_auth_example.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,5 @@ def close(self):
3333
self._driver.close()
3434

3535
def can_connect(self):
36-
record_list = list(self._driver.session().run("RETURN 1"))
37-
return int(record_list[0][0]) == 1
36+
result = self._driver.session().run("RETURN 1")
37+
return result.single()[0] == 1

test/examples/config_connection_timeout_example.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@
2222
from neo4j.v1 import GraphDatabase
2323
# end::config-connection-timeout-import[]
2424

25+
2526
class ConfigConnectionTimeoutExample:
2627
# tag::config-connection-timeout[]
2728
def __init__(self, uri, user, password):
28-
self._driver = GraphDatabase.driver( uri, auth=( user, password ),
29-
Config.build().withConnectionTimeout( 15, SECONDS ).toConfig() )
29+
self._driver = GraphDatabase.driver(uri, auth=(user, password), connection_timeout=15)
3030
# end::config-connection-timeout[]
3131

3232
def close(self):
33-
self._driver.close();
33+
self._driver.close()

test/examples/config_max_retry_time_example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ def __init__(self, uri, user, password):
3030
# end::config-max-retry-time[]
3131

3232
def close(self):
33-
self._driver.close();
33+
self._driver.close()

test/examples/config_trust_example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ def __init__(self, uri, user, password):
3030
# end::config-trust[]
3131

3232
def close(self):
33-
self._driver.close();
33+
self._driver.close()

test/examples/custom_auth_example.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@ def close(self):
3333
self._driver.close()
3434

3535
def can_connect(self):
36-
record_list = list(self._driver.session().run("RETURN 1"))
37-
return int(record_list[0][0]) == 1
36+
with self._driver.session() as session:
37+
result = session.run("RETURN 1")
38+
return result.single()[0] == 1

test/examples/cypher_error_example.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,28 @@
1818
# See the License for the specific language governing permissions and
1919
# limitations under the License.
2020

21-
# tag::cypher-error-import[]
22-
from neo4j.v1 import GraphDatabase, ClientError
2321
from test.examples.base_application import BaseApplication
22+
23+
# tag::cypher-error-import[]
24+
from neo4j.v1 import ClientError
2425
# end::cypher-error-import[]
2526

27+
2628
class CypherErrorExample(BaseApplication):
2729
def __init__(self, uri, user, password):
2830
super(CypherErrorExample, self).__init__(uri, user, password)
2931

3032
# tag::cypher-error[]
3133
def get_employee_number(self, name):
32-
session = self._driver.session()
33-
session.read_transaction(lambda tx: self.select_employee(tx, name))
34+
with self._driver.session() as session:
35+
return session.read_transaction(self.select_employee, name)
3436

35-
def select_employee(self, tx, name):
37+
@staticmethod
38+
def select_employee(tx, name):
3639
try:
37-
record_list = list(tx.run("SELECT * FROM Employees WHERE name = $name", {"name": name}))
38-
return int(record_list[0]["employee_number"])
39-
except ClientError as e:
40-
print(e.message)
40+
result = tx.run("SELECT * FROM Employees WHERE name = $name", name=name)
41+
return result.single()["employee_number"]
42+
except ClientError as error:
43+
print(error.message)
4144
return -1
4245
# end::cypher-error[]

test/examples/driver_lifecycle_example.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@
2222
from neo4j.v1 import GraphDatabase
2323
# end::driver-lifecycle-import[]
2424

25+
2526
# tag::driver-lifecycle[]
2627
class DriverLifecycleExample:
2728
def __init__(self, uri, user, password):
2829
self._driver = GraphDatabase.driver(uri, auth=(user, password))
2930

3031
def close(self):
31-
self._driver.close();
32+
self._driver.close()
3233
# end::driver-lifecycle[]

test/examples/hello_world_example.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,29 @@
2020

2121
# tag::hello-world-import[]
2222
from neo4j.v1 import GraphDatabase
23-
from test.examples.base_application import BaseApplication
2423
# end::hello-world-import[]
2524

25+
2626
# tag::hello-world[]
27-
class HelloWorldExample(BaseApplication):
27+
class HelloWorldExample(object):
28+
2829
def __init__(self, uri, user, password):
29-
super(HelloWorldExample, self).__init__(uri, user, password)
30-
31-
def _create_and_return_greeting(self, tx, message):
32-
record_list = list(tx.run("CREATE (a:Greeting) " +
33-
"SET a.message = $message " +
34-
"RETURN a.message + ', from node ' + id(a)",
35-
{"message": message}))
36-
return str(record_list[0][0])
37-
30+
self._driver = GraphDatabase.driver(uri, auth=(user, password))
31+
32+
def close(self):
33+
self._driver.close()
34+
3835
def print_greeting(self, message):
3936
with self._driver.session() as session:
40-
greeting = session.write_transaction(lambda tx: self._create_and_return_greeting(tx, message))
37+
greeting = session.write_transaction(self._create_and_return_greeting, message)
4138
print(greeting)
39+
40+
@staticmethod
41+
def _create_and_return_greeting(tx, message):
42+
result = tx.run("CREATE (a:Greeting) "
43+
"SET a.message = $message "
44+
"RETURN a.message + ', from node ' + id(a)", message=message)
45+
return result.single()[0]
4246
# end::hello-world[]
4347

4448
# tag::hello-world-output[]

0 commit comments

Comments
 (0)