You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by co...@apache.org on 2023/06/05 22:23:16 UTC

[tinkerpop] branch 3.6-dev updated: (new) (gremlin-python) improve translation (#2076)

This is an automated email from the ASF dual-hosted git repository.

colegreer pushed a commit to branch 3.6-dev
in repository https://gitbox.apache.org/repos/asf/tinkerpop.git


The following commit(s) were added to refs/heads/3.6-dev by this push:
     new e5f819cad0 (new) (gremlin-python) improve translation (#2076)
e5f819cad0 is described below

commit e5f819cad03f9765b16c0670112156dbc2d3f76b
Author: Alex <90...@users.noreply.github.com>
AuthorDate: Mon Jun 5 16:23:11 2023 -0600

    (new) (gremlin-python) improve translation (#2076)
    
    This commit improves the python Translator class in a few ways:
    
        handle TextP predicates in addition to P
        handle quoting for subclasses of str
        handle None -> null
    
    Tests have been added to verify the changes.
    
    * (fix) translate text predicates
    
    * (fix) translate str subclasses
    
    * (test) verify translation changes
    
    * (new) fixup str subclass
    
    * (test) verify translation of text predicate with str subclass
    
    * (fix) translate None -> null
---
 .../python/gremlin_python/process/translator.py    |  8 +++++---
 .../main/python/tests/process/test_translator.py   | 23 ++++++++++++++++++++++
 2 files changed, 28 insertions(+), 3 deletions(-)

diff --git a/gremlin-python/src/main/python/gremlin_python/process/translator.py b/gremlin-python/src/main/python/gremlin_python/process/translator.py
index 199f76e8fb..efbc4b42e3 100755
--- a/gremlin-python/src/main/python/gremlin_python/process/translator.py
+++ b/gremlin-python/src/main/python/gremlin_python/process/translator.py
@@ -67,7 +67,7 @@ class Translator:
     # Do any needed special processing for the representation
     # of strings and dates.
     def fixup(self, v):
-        if type(v) == str:
+        if isinstance(v, str):
             return f'\'{v}\''
         elif type(v) == datetime:
             return self.process_date(v)
@@ -133,7 +133,7 @@ class Translator:
                   script += f'WithOptions.{self.options[p]}'
                 elif type(p) == Bytecode:
                     script += self.translate(p, True)
-                elif type(p) == P:
+                elif isinstance(p, P):
                     script += self.process_predicate(p)
                 elif type(p) in [Cardinality, Pop, Operator]:
                     tmp = str(p)
@@ -146,10 +146,12 @@ class Translator:
                 elif p == WithOptions.tokens:
                     script += 'WithOptions.tokens'
                     with_opts = True
-                elif type(p) == str:
+                elif isinstance(p, str):
                     script += f'\'{p}\''
                 elif type(p) == bool:
                     script += 'true' if p else 'false'
+                elif p is None:
+                    script += 'null'
                 else:
                     script += str(p)
                 c += 1
diff --git a/gremlin-python/src/main/python/tests/process/test_translator.py b/gremlin-python/src/main/python/tests/process/test_translator.py
index e0230d6885..2afa148372 100644
--- a/gremlin-python/src/main/python/tests/process/test_translator.py
+++ b/gremlin-python/src/main/python/tests/process/test_translator.py
@@ -351,6 +351,29 @@ class TestTraversalStrategies(object):
         tests.append([g.withComputer().V().shortestPath().with_(ShortestPath.target, __.has('name','peter')),
                      "g.withStrategies(new VertexProgramStrategy()).V().shortestPath().with('~tinkerpop.shortestPath.target',__.has('name','peter'))"])
 
+        # 99
+        tests.append([g.V().has("p1", starting_with("foo")),
+                     "g.V().has('p1',startingWith('foo'))"])
+
+        # 100
+        tests.append([g.V().has("p1", ending_with("foo")),
+                     "g.V().has('p1',endingWith('foo'))"])
+
+        # 101
+        class SuperStr(str):
+            pass
+        tests.append([g.V(SuperStr("foo_id")),
+                     "g.V('foo_id')"])
+
+        # 102
+        tests.append([g.V().has("p1", containing(SuperStr("foo"))),
+                     "g.V().has('p1',containing('foo'))"])
+
+        # 103
+        tests.append([g.V().has("p1", None),
+                     "g.V().has('p1',null)"])
+
+
         tlr = Translator().of('g')
 
         for t in range(len(tests)):