Skip to content

Fix examples #160

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 4 commits into from
May 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion neo4j/bolt/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,8 @@ def connect(address, ssl_context=None, **config):
raise ServiceUnavailable("Failed to establish connection to {!r}".format(address))
else:
raise
except ConnectionResetError:
raise ServiceUnavailable("Failed to establish connection to {!r}".format(address))

# Secure the connection if an SSL context has been provided
if ssl_context and SSL_AVAILABLE:
Expand Down Expand Up @@ -500,7 +502,10 @@ def connect(address, ssl_context=None, **config):
ready_to_read, _, _ = select((s,), (), (), 0)
while not ready_to_read:
ready_to_read, _, _ = select((s,), (), (), 0)
data = s.recv(4)
try:
data = s.recv(4)
except ConnectionResetError:
raise ServiceUnavailable("Failed to read any data from server {!r} after connected".format(address))
data_size = len(data)
if data_size == 0:
# If no data is returned after a successful select
Expand Down
8 changes: 4 additions & 4 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 @@ -422,11 +422,11 @@ 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()
self._connect(access_mode)
self._create_transaction()
self.__begin__()
with self._transaction as tx:
return unit_of_work(tx, *args, **kwargs)
Expand All @@ -438,7 +438,7 @@ def _run_transaction(self, access_mode, unit_of_work, *args, **kwargs):
else:
raise 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 test.examples.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()
Loading