TechnologyOctober 25, 2017

Gremlin DSLs in Python with DSE Graph

Stephen Mallette
Stephen Mallette
Gremlin DSLs in Python with DSE Graph
killr.movies() # equivalent to g.V().hasLabel('movie') killr.users() # equivalent to g.V().hasLabel('user') killr.persons() # equivalent to g.V().hasLabel('person') killr.genres() # equivalent to g.V().hasLabel('genre')
killr.movies('Young Guns') # equivalent to g.V().has('movie', 'title', 'Young Guns') killr.users('u460') # equivalent to g.V().has('user', 'userId', 'u460')
killr.movies('Young Guns', 'Hot Shots!') # equivalent to g.V().has('movie', 'title', within('Young Guns', 'Hot Shots!')) killr.users('u460', 'u461', 'u462') # equivalent to g.V().has('user', 'userId', within('u460', 'u461', 'u462'))
class KillrVideoTraversalSource(GraphTraversalSource):
      return traversal
   def users(self, *args): traversal = self.get_graph_traversal().V().hasLabel("user")
   return traversal
killr.movies('Young Guns').out('actor') # equivalent to g.V().has('movie','title','Young Guns').out('actor') killr.movies('Young Guns').inE('rated') # equivalent to g.V().has('movie','title','Young Guns').inE('rated')
killr.movies('Young Guns').actors() # equivalent to g.V().has('movie','title','Young Guns').out('actor') killr.movies('Young Guns').ratings() # equivalent to g.V().has('movie','title','Young Guns').inE('rated')
        return self.out("actor").hasLabel('person') # extra verification that a 'person' is obtained
    return self.inE('rated')
killr.users('u460').recommend()
   def ratings(self): return self.inE('rated')
   def recommend(self, recommendations, minimum_rating, include):
killr.users('u460').recommend(5, 7, has('country','USA').has('duration',gt(90))) # recommend movies from 'USA' longer than 90 minutes
killr.users('u460').recommend(5, 7, out('belongsTo').has('name', 'Comedy')) # recommend movies that are comedies
killr.users('u460').recommend(5, 7, genre('Comedy')) # recommend movies that are comedies
   def recommend(self, recommendations, minimum_rating, include):
class KillrVideoTraversal(GraphTraversal):
# ... omitted other DSL steps to focus on recommend() step
def recommend(self, recommendations, minimum_rating, include, recommender):
class Recommender(Enum):
      return switcher.get(self)
class KillrVideoTraversal(GraphTraversal):
# ... omitted other DSL steps to focus on recommend() step
def recommend(self, recommendations, minimum_rating, include, recommender):
class KillrVideoTraversal(GraphTraversal):
# ... omitted other DSL steps to focus on recommend() step
killr.users('u460').recommend(5, 7) killr.users('u460').recommend(5, 7, has('country','USA')) killr.users('u460').recommend(5, 7, has('country','USA'), Recommender.LARGE_SAMPLE)
from aenum import Enum
class KillrVideoTraversal(GraphTraversal):
# ... omitting other DSL methods to focus on genre()
killr.users('u460').recommend(5, 7, genre(Genre.COMEDY, Genre.ANIMATION)) # recommend movies that are comedies or animations
Discover more
GremlinPythonDSE Graph
Share

NoSQL and Vector DB
for Generative AI,
Instantly, At Scale

Vector search capabilities on Astra DB enable complex, context-sensitive searches across diverse data formats for use in Generative AI applications, powered by Apache Cassandra®.