Skip to content

Instantly share code, notes, and snippets.

@FRex
Created September 9, 2019 12:31
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 FRex/51f73862efd916221a5df6b6baca9db1 to your computer and use it in GitHub Desktop.
Save FRex/51f73862efd916221a5df6b6baca9db1 to your computer and use it in GitHub Desktop.
get = (sum and __import__ or require)('requests').get
url = "http://translate.googleapis.com/translate_a/single"
params = {}
params['client'] = 'gtx'
params['sl'] = 'en'
params['tl'] = 'de'
params['dt'] = 't'
params['q'] = 'hello!'
params['params'] = params
r = get(url, params)
print(r.text)
@FRex
Copy link
Author

FRex commented Sep 9, 2019

Tricks/kludges used:

  1. Not using Lua requests easier Python-like call syntax of using a single arg new table like get{url, params=params} that I proposed in JakobGreen/lua-requests/issues/14 and that later got added since it'd fail as Python.
  2. Following from above, params['params'] = params because in Lua requests without using {} the second argument is a table of all params (GET params, POST data, etc.), not table of GET params themselves.
  3. The ?: tertiary operator-like behavior using and and or which works in both, to pick right module importing function since only in Python sum is a global function (truthy), in Lua it doesn't exist so it's nil - falsy, but okay and not an exception or error to access.
  4. Using Python's __import__ function instead of the import statement.
  5. Not using Lua's shortcut of calling a function like require 'requests' since in Python parens on function calls are mandatory unlike Lua where they can be skipped for single arg call with new table or literal string.
  6. Not using table (Lua)/dict (Python) initializer since they have different syntax in each, but normal assigment can have same syntax.
  7. Related to above, not using Lua's shortcut of doing params.q instead of params['q'] since it doesn't work in Python (it's attribute access, not shortcut for access by string literal key).
  8. All the variables in Lua are globals, adding local would make it fail as Python.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment