def python2lua(lua, obj, max_depth=100, encoding='utf8', keep_tuples=True):

    def p2l(obj, depth):
        if depth <= 0:
            raise ValueError("Can't convert Python object to Lua: depth limit is reached")

        if isinstance(obj, dict):
            return lua.table_from({
                p2l(key, depth-1): p2l(value, depth-1)
                for key, value in six.iteritems(obj)
            })

        if isinstance(obj, tuple) and keep_tuples:
            return tuple(p2l(el, depth-1) for el in obj)

        if isinstance(obj, (list, tuple)):
            tbl = lua.table_from([p2l(el, depth-1) for el in obj])
            return _mark_table_as_array(lua, tbl)

        if isinstance(obj, six.text_type):
            return obj.encode(encoding)

        if isinstance(obj, datetime.datetime):
            return to_bytes(obj.isoformat() + 'Z', encoding)
            # XXX: maybe return datetime encoded to Lua standard? E.g.:

            # tm = obj.timetuple()
            # return python2lua(lua, {
            #     '_jstype': 'Date',
            #     'year': tm.tm_year,
            #     'month': tm.tm_mon,
            #     'day': tm.tm_mday,
            #     'yday': tm.tm_yday,
            #     'wday': tm.tm_wday,  # fixme: in Lua Sunday is 1, in Python Monday is 0
            #     'hour': tm.tm_hour,
            #     'min': tm.tm_min,
            #     'sec': tm.tm_sec,
            #     'isdst': tm.tm_isdst,  # fixme: isdst can be -1 in Python
            # }, max_depth)

        return obj

    return p2l(obj, depth=max_depth)
Recursively convert Python object to a Lua data structure.
dataset/ETHPy150Open scrapinghub/splash/splash/lua.py/python2lua