Skip to content

Commit bed0829

Browse files
authored
Merge pull request #3308 from apache/TINKERPOP-3119
TINKERPOP-3119 Added better pythonic access for properties
2 parents afb0882 + 8f4182e commit bed0829

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

CHANGELOG.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ image::https://raw.githubusercontent.com/apache/tinkerpop/master/docs/static/ima
2323
[[release-4-0-0]]
2424
=== TinkerPop 4.0.0 (NOT OFFICIALLY RELEASED YET)
2525
26+
* Added `__contains__` and `keys()` to `Element` in `gremlin-python`.
2627
* Added `subgraph()` support for `gremlin-python` so that results are stored in a detached `Graph` object.
2728
* Modified grammar to make `discard()` usage more consistent as a filter step where it can now be used to chain additional traversal steps and be used anonymously.
2829
* Removed `Meta` field from `ResponseResult` struct in `gremlin-go`

gremlin-python/src/main/python/gremlin_python/structure/graph.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ def __getitem__(self, key):
4141
return p.value
4242
raise KeyError(key)
4343

44+
def __contains__(self, key):
45+
for p in self.properties:
46+
if p.key == key:
47+
return True
48+
return False
49+
50+
def keys(self):
51+
return set(p.key for p in self.properties)
52+
4453
def values(self, *property_keys):
4554
if len(property_keys) == 0:
4655
return [p.value for p in self.properties]

gremlin-python/src/main/python/tests/unit/structure/test_graph.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,24 @@ def test_element_value_values(self):
161161
assert vp["acl"] == "public"
162162
assert vp.values("acl") == ["public"]
163163
assert vp.values() == ["public"]
164+
165+
def test_element_contains_and_keys(self):
166+
v = Vertex(1, "person", [VertexProperty(10, "name", "marko", Vertex(1)),
167+
VertexProperty(11, "age", 29, Vertex(1))])
168+
assert "name" in v
169+
assert "age" in v
170+
assert "nonexistent" not in v
171+
assert v.keys() == {"name", "age"}
172+
173+
e = Edge(2, Vertex(1), "knows", Vertex(3), [Property("weight", 0.5, None)])
174+
assert "weight" in e
175+
assert "missing" not in e
176+
assert e.keys() == {"weight"}
177+
178+
empty_v = Vertex(99)
179+
assert "anything" not in empty_v
180+
assert empty_v.keys() == set()
181+
182+
# supports the pattern: vertex[key] if key in vertex else None
183+
assert v["name"] if "name" in v else None == "marko"
184+
assert v["missing"] if "missing" in v else None is None

0 commit comments

Comments
 (0)