Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
get = (sum and __import__ or require)('requests').get
url = ""
params = {}
params['client'] = 'gtx'
params['sl'] = 'en'
params['tl'] = 'de'
params['dt'] = 't'
params['q'] = 'hello!'
params['params'] = params
r = get(url, params)

This comment has been minimized.

Copy link
Owner Author

@FRex 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