What is the Easiest Way to Add Property in Python using dict?

762 views
Skip to first unread message

이의담

unread,
May 30, 2021, 11:51:01 PM5/30/21
to Gremlin-users
Hi, I'm trying to add properties in gremlin-python.

I know that I can add one by one using ".property('a', 'a'),property().property().."

However, I want to know if there is a way that can map entire dictionary into properties.

For example,

prop_dict = {
    'id' : '1',
    'name' : 'Jake',
    'age' : '24',
    'city' : 'Seoul'
}

Some pythonic operation with prop_didt would do the same operation as below.

V().addV('Student').
     .property(id_, '1')
     .property('name', 'Jake')
     .property('age', '24')
     .property('city', 'Seoul')

Is there any way I can handle it?

Really appreciate your help :)

Thanks.

AMIYA KUMAR SAHOO

unread,
May 31, 2021, 12:45:38 AM5/31/21
to gremli...@googlegroups.com
Hi,


There was a conversation to create single step for this, I don't know latest status on that.



--
You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/ecbf0629-5d49-4793-b0b6-9d2551544ff0n%40googlegroups.com.

이의담

unread,
May 31, 2021, 1:39:33 AM5/31/21
to Gremlin-users
Thanks, I could solve the issue with your answer :)

Here's how I solved it.


from gremlin_python.process.traversal import Column
from gremlin_python.process.graph_traversal import select
from gremlin_python.process.graph_traversal import property

l = [{'name' : 'Jake', 'age' : '24', 'city' : 'Seoul'}]

g.inject(l).unfold().as_('properties')\
    .addV('Person').as_('vertex')\
    .sideEffect(
        select('properties').unfold().as_('kv')
        .select('vertex')
        .property(select('kv').by(Column.keys), select('kv').by(Column.values))
    ).next()

2021년 5월 31일 월요일 오후 1시 45분 38초 UTC+9에 Amiya님이 작성:

Thirumal M

unread,
May 31, 2021, 12:19:22 PM5/31/21
to Gremlin-users
Hi,

I am trying to implement your solution but I got problem with `T.id`

"TypeError: unhashable type: 'dict'"

Any solution?

이의담

unread,
May 31, 2021, 9:09:18 PM5/31/21
to Gremlin-users
Same here.

Replacing list 'l' with below one raises error.

l = [{T.id : '1', 'name' : 'Jake', 'age' : '24', 'city' : 'Seoul'}]

 498: {"detailedMessage":"org.apache.tinkerpop.gremlin.structure.T$2 cannot be cast to java.lang.String","code":"UnsupportedOperationException","requestId":"efe46769-7ea5-4e8e-b248-ba7bcc5c5563"}

2021년 6월 1일 화요일 오전 1시 19분 22초 UTC+9에 m.thi...@hotmail.com님이 작성:

Stephen Mallette

unread,
Jun 1, 2021, 8:08:54 AM6/1/21
to gremli...@googlegroups.com
Setting the T.id can't be set by way of a sideEffect() as the calls to property() are not associated with the initial call to addV() that way. Since T values are immutable, the missing association prevents them from being set at the time the vertex is created. There are actually other problems with T that are annoying, like not being able to select(T) which doesn't let you pick apart the Map easily and you have to filter() the Map entries which just complicates the Gremlin - here it is with all the filters and includes T.label:

gremlin> m = [name:'jake',age:24,city:'seoul',(T.label):'person',(T.id):321]
==>name=jake
==>age=24
==>city=seoul
==>label=person
==>id=321
gremlin> g.withSideEffect('properties',m).
......1>   addV(select('properties').unfold().filter(select(keys).is(T.label)).select(values)).as('vertex').
......2>   property(T.id, select('properties').unfold().filter(select(keys).is(T.id)).select(values)).
......3>   sideEffect(select('properties').
......4>   unfold().filter(select(keys).is(without(T.label,T.id))).as('kv').
......5>   select('vertex').
......6>   property(select('kv').by(Column.keys), select('kv').by(Column.values)))
==>v[321]
gremlin> g.V().elementMap()
==>[id:321,label:person,city:seoul,name:jake,age:24]


If you need to set the "id" perhaps you could create your own unique indexed property rather than relying on the actual T.id itself, then you could avoid all the filtering. Also, if you're passing just a single Map then it might be better to deal with the label/id outside of your Map. 

gremlin> m = [name:'jake',age:24,city:'seoul',(T.label):'person',(T.id):321]
==>name=jake
==>age=24
==>city=seoul
==>label=person
==>id=321
gremlin> g.withSideEffect('properties',m).
......1>   addV(m[T.label]).as('vertex').
......2>   property(T.id, m[T.id]).
......3>   sideEffect(select('properties').
......4>   unfold().filter(select(keys).is(without(T.label,T.id))).as('kv').
......5>   select('vertex').
......6>   property(select('kv').by(Column.keys), select('kv').by(Column.values)))
==>v[321]
gremlin> g.V().elementMap()
==>[id:321,label:person,city:seoul,name:jake,age:24]



--
You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.

Thirumal M

unread,
Jun 1, 2021, 8:44:50 AM6/1/21
to Gremlin-users
Got it. Thank you for your answer & time.

Much Appreciated.

Dave Bechberger

unread,
Jun 1, 2021, 8:54:00 AM6/1/21
to gremli...@googlegroups.com
There is no way to pass a dictionary to A Gremlin step to have it save the properties so the best method is to save them one at a time like you have found. 

Dave Bechberger


On May 30, 2021, at 7:51 PM, 이의담 <ud8...@gmail.com> wrote:

Hi, I'm trying to add properties in gremlin-python.
--
You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.

이의담

unread,
Jun 1, 2021, 10:22:29 AM6/1/21
to Gremlin-users
Thanks for all !!

Really appreciate your efforts and nice answers :)

It really helped me a lot in graph implementation.


2021년 6월 1일 화요일 오후 9시 54분 0초 UTC+9에 David Bechberger님이 작성:
Reply all
Reply to author
Forward
0 new messages