Created
January 26, 2020 22:27
-
-
Save mtholder/70c0c5b8f10176ca2723340ddaf0399f to your computer and use it in GitHub Desktop.
example of calling dimus' gnparser from python3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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