yglukhov / nimpy

Nim - Python bridge
MIT License
1.48k stars 60 forks source link

How to unpack PPyObject (dict) to kwargs in Nim? #287

Closed Ethosa closed 12 months ago

Ethosa commented 1 year ago

Python function with arguments

def myFunc(some, required, arguments = "asda", here = 5):
  pass

How I can build JsonNode and send it as unpacked keyword arguments?

var kwargs = %*{"some": 5, "required": "hello", "arguments": 123}
var pyDict = nimValueToPy(kwargs)
myFunc( ... ? ... )
Ethosa commented 1 year ago

Solution for me:

  1. Convert Nim object to Python dict kwargs
  2. Create empty Python dict locals
  3. Add myFunc and kwargs to locals
  4. Call via eval and got value