Skip to content

Instantly share code, notes, and snippets.

@mtholder
Created January 26, 2020 22:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mtholder/70c0c5b8f10176ca2723340ddaf0399f to your computer and use it in GitHub Desktop.
Save mtholder/70c0c5b8f10176ca2723340ddaf0399f to your computer and use it in GitHub Desktop.
example of calling dimus' gnparser from python3
#!/usr/env python
""" Example of wrapping a call to gnparser from python
See https://gitlab.com/gogna/gnparser#command-line
Adapted based on instructions on
https://medium.com/learning-the-go-programming-language/calling-go-functions-from-other-languages-4c7d8bcc69bf
"""
import ctypes
import json
lib = ctypes.cdll.LoadLibrary("./libgnparser.so")
lib.ParseToString.argtypes = [ctypes.c_char_p, ctypes.c_char_p]
lib.ParseToString.restype = ctypes.c_char_p
_compact_arg = ctypes.c_char_p("compact".encode('utf-8'))
def name_to_dict(name):
cn = ctypes.c_char_p(name.encode('utf-8'))
x = lib.ParseToString(cn, _compact_arg).decode('utf-8')
return json.loads(x)
if __name__ == '__main__':
import sys
for arg in sys.argv[1:]:
x = name_to_dict(arg)
print(json.dumps(x, ensure_ascii=True, indent=2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment