Skip to content

Documentation example doesn't work #84

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

Closed
PabloCastellano opened this issue Jun 28, 2016 · 3 comments
Closed

Documentation example doesn't work #84

PabloCastellano opened this issue Jun 28, 2016 · 3 comments

Comments

@PabloCastellano
Copy link

From https://neo4j.com/docs/api/python-driver/current/#example

This line:

>>> for friend, in session.run("MATCH (a:Person {name:'Alice'})-[:KNOWS]->(x) RETURN x"):
...     print('Alice says, "hello, %s"' % friend["name"])
... 
Traceback (most recent call last):
  File "<input>", line 2, in <module>
    print('Alice says, "hello, %s"' % friend["name"])
TypeError: string indices must be integers
@EmilStenstrom
Copy link

This problem is likely just an extra comma after friend:

for friend, in session.run(...)

@liutec
Copy link
Contributor

liutec commented Nov 2, 2016

The comma is not extra.

for friend, in session.run("MATCH (a:Person {name:'Alice'})-[:KNOWS]->(x) RETURN x"):
    print('Alice says, "hello, %s"' % friend["name"])

is equivalent to:

for record in session.run("MATCH (a:Person {name:'Alice'})-[:KNOWS]->(x) RETURN a, x"):
    friend, = record
    print('Alice says, "hello, %s"' % friend["name"])

Record.__iter__ will be called in order to assign the first returned value (x) to friend.

It's use is to extract returned values like so:

for a, x in session.run("MATCH (a:Person {name:'Alice'})-[:KNOWS]->(x) RETURN a,x"):
    print('%s says, "hello, %s"' % (a["name"], x["name"]))

instead of having to address them explicitly in the record:

for record in session.run("MATCH (a:Person {name:'Alice'})-[:KNOWS]->(x) RETURN a,x"):
    print('%s says, "hello, %s"' % (record["a"]["name"], record["x"]["name"]))

The problem is that Record.__iter__ returns iter(self._keys) instead of iter(self._values).

I believe this is a flaw in the implementation of the Record class.

return iter(self._keys)

I may be wrong, I just started to evaluate this library, but I see no point in providing iteration support to fetch the already known variable names instead of the actual values.

@technige
Copy link
Contributor

Fixed in #111

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants