Skip to content

Commit 11de5b8

Browse files
committed
Docs for async
1 parent 510496a commit 11de5b8

15 files changed

+749
-146
lines changed

bin/make-unasync

Lines changed: 72 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020

2121
import collections
2222
import errno
23-
from functools import reduce
2423
import os
2524
from pathlib import Path
25+
import re
2626
import sys
2727
import tokenize as std_tokenize
2828

@@ -103,20 +103,86 @@ class CustomRule(unasync.Rule):
103103
super(CustomRule, self).__init__(*args, **kwargs)
104104
self.out_files = []
105105

106-
def _unasync_name(self, name):
107-
# copy from unasync to customize renaming rules
106+
def _unasync_tokens(self, tokens):
107+
# copy from unasync to hook into string handling
108108
# https://github.com/python-trio/unasync
109109
# License: MIT and Apache2
110-
if name in self.token_replacements:
111-
return self.token_replacements[name]
110+
# TODO __await__, ...?
111+
used_space = None
112+
for space, toknum, tokval in tokens:
113+
if tokval in ["async", "await"]:
114+
# When removing async or await, we want to use the whitespace
115+
# that was before async/await before the next token so that
116+
# `print(await stuff)` becomes `print(stuff)` and not
117+
# `print( stuff)`
118+
used_space = space
119+
else:
120+
if toknum == std_tokenize.NAME:
121+
tokval = self._unasync_name(tokval)
122+
elif toknum == std_tokenize.STRING:
123+
if tokval[0] == tokval[1] and len(tokval) > 2:
124+
# multiline string (`"""..."""` or `'''...'''`)
125+
left_quote, name, right_quote = \
126+
tokval[:3], tokval[3:-3], tokval[-3:]
127+
else:
128+
# simple string (`"..."` or `'...'`)
129+
left_quote, name, right_quote = \
130+
tokval[:1], tokval[1:-1], tokval[-1:]
131+
tokval = \
132+
left_quote + self._unasync_string(name) + right_quote
133+
if used_space is None:
134+
used_space = space
135+
yield (used_space, tokval)
136+
used_space = None
137+
138+
def _unasync_string(self, name):
139+
start = 0
140+
end = 1
141+
out = ""
142+
while end < len(name):
143+
sub_name = name[start:end]
144+
if sub_name.isidentifier():
145+
end += 1
146+
else:
147+
if end == start + 1:
148+
out += sub_name
149+
start += 1
150+
end += 1
151+
else:
152+
out += self._unasync_prefix(name[start:(end - 1)])
153+
start = end - 1
154+
155+
sub_name = name[start:]
156+
if sub_name.isidentifier():
157+
out += self._unasync_prefix(name[start:])
158+
else:
159+
out += sub_name
160+
161+
# very boiled down unasync version that removes "async" and "await"
162+
# substrings.
163+
out = re.subn(r"(^|\s+|(?<=\W))(?:async|await)\s+", r"\1", out,
164+
flags=re.MULTILINE)[0]
165+
# Convert doc-reference names from 'async-xyz' to 'xyz'
166+
out = re.subn(r":ref:`async-", ":ref:`", out)[0]
167+
return out
168+
169+
def _unasync_prefix(self, name):
112170
# Convert class names from 'AsyncXyz' to 'Xyz'
113-
elif len(name) > 5 and name.startswith("Async") and name[5].isupper():
171+
if len(name) > 5 and name.startswith("Async") and name[5].isupper():
114172
return name[5:]
115173
# Convert variable/method/function names from 'async_xyz' to 'xyz'
116174
elif len(name) > 6 and name.startswith("async_"):
117175
return name[6:]
118176
return name
119177

178+
def _unasync_name(self, name):
179+
# copy from unasync to customize renaming rules
180+
# https://github.com/python-trio/unasync
181+
# License: MIT and Apache2
182+
if name in self.token_replacements:
183+
return self.token_replacements[name]
184+
return self._unasync_prefix(name)
185+
120186
def _unasync_file(self, filepath):
121187
# copy from unasync to append file suffix to out path
122188
# https://github.com/python-trio/unasync

docs/source/api.rst

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ GraphDatabase
1111
Driver Construction
1212
===================
1313

14-
The :class:`neo4j.Driver` construction is via a `classmethod` on the :class:`neo4j.GraphDatabase` class.
14+
The :class:`neo4j.Driver` construction is done via a `classmethod` on the :class:`neo4j.GraphDatabase` class.
1515

1616
.. autoclass:: neo4j.GraphDatabase
1717
:members: driver
@@ -24,18 +24,19 @@ Example, driver creation:
2424
from neo4j import GraphDatabase
2525
2626
uri = "neo4j://example.com:7687"
27-
driver = GraphDatabase.driver(uri, auth=("neo4j", "password"), max_connection_lifetime=1000)
27+
driver = GraphDatabase.driver(uri, auth=("neo4j", "password"))
2828
2929
driver.close() # close the driver object
3030
3131
32-
For basic auth, this can be a simple tuple, for example:
32+
For basic authentication, `auth` can be a simple tuple, for example:
3333

3434
.. code-block:: python
3535
3636
auth = ("neo4j", "password")
3737
38-
This will implicitly create a :class:`neo4j.Auth` with a ``scheme="basic"``
38+
This will implicitly create a :class:`neo4j.Auth` with a ``scheme="basic"``.
39+
Other authentication methods are described under :ref:`auth-ref`.
3940

4041

4142
Example, with block context:
@@ -345,11 +346,9 @@ BoltDriver
345346
URI schemes:
346347
``bolt``, ``bolt+ssc``, ``bolt+s``
347348

348-
Driver subclass:
349-
:class:`neo4j.BoltDriver`
349+
Will result in:
350350

351-
..
352-
.. autoclass:: neo4j.BoltDriver
351+
.. autoclass:: neo4j.BoltDriver
353352

354353

355354
.. _neo4j-driver-ref:
@@ -360,11 +359,9 @@ Neo4jDriver
360359
URI schemes:
361360
``neo4j``, ``neo4j+ssc``, ``neo4j+s``
362361

363-
Driver subclass:
364-
:class:`neo4j.Neo4jDriver`
362+
Will result in:
365363

366-
..
367-
.. autoclass:: neo4j.Neo4jDriver
364+
.. autoclass:: neo4j.Neo4jDriver
368365

369366

370367
***********************
@@ -604,7 +601,8 @@ Example:
604601
605602
def create_person(driver, name):
606603
with driver.session(default_access_mode=neo4j.WRITE_ACCESS) as session:
607-
result = session.run("CREATE (a:Person { name: $name }) RETURN id(a) AS node_id", name=name)
604+
query = "CREATE (a:Person { name: $name }) RETURN id(a) AS node_id"
605+
result = session.run(query, name=name)
608606
record = result.single()
609607
return record["node_id"]
610608
@@ -665,13 +663,15 @@ Example:
665663
tx.close()
666664
667665
def create_person_node(tx):
666+
query = "CREATE (a:Person { name: $name }) RETURN id(a) AS node_id"
668667
name = "default_name"
669-
result = tx.run("CREATE (a:Person { name: $name }) RETURN id(a) AS node_id", name=name)
668+
result = tx.run(query, name=name)
670669
record = result.single()
671670
return record["node_id"]
672671
673672
def set_person_name(tx, node_id, name):
674-
result = tx.run("MATCH (a:Person) WHERE id(a) = $id SET a.name = $name", id=node_id, name=name)
673+
query = "MATCH (a:Person) WHERE id(a) = $id SET a.name = $name"
674+
result = tx.run(query, id=node_id, name=name)
675675
info = result.consume()
676676
# use the info for logging etc.
677677
@@ -698,7 +698,8 @@ Example:
698698
node_id = session.write_transaction(create_person_tx, name)
699699
700700
def create_person_tx(tx, name):
701-
result = tx.run("CREATE (a:Person { name: $name }) RETURN id(a) AS node_id", name=name)
701+
query = "CREATE (a:Person { name: $name }) RETURN id(a) AS node_id"
702+
result = tx.run(query, name=name)
702703
record = result.single()
703704
return record["node_id"]
704705
@@ -708,12 +709,6 @@ To exert more control over how a transaction function is carried out, the :func:
708709

709710

710711

711-
712-
713-
714-
715-
716-
717712
******
718713
Result
719714
******

0 commit comments

Comments
 (0)