Coder Social home page Coder Social logo

luatable's Introduction

LuaTable

This is a simple implementation of Lua table parser and generator for Python 2.7 and Python 3.5. It is pure Python code with no dependencies.

Usage

Basic parsing:

>>> from luatable import fromlua
>>> src = '{"foo", {["bar"] = {"baz", nil, 1.0, 2}}}'
>>> fromlua(src)
['foo', {'bar': ['baz', None, 1.0, 2]}]

Basic generating:

>>> from luatable import tolua
>>> obj = ['foo', {'bar': ['baz', None, 1.0, 2]}]
>>> tolua(obj)
'{"foo",{["bar"]={"baz",nil,1.0,2,},},}'

Put it together:

>>> from pprint import pprint
>>> from luatable import fromlua, tolua
>>> src = """
...     {
...         list = {
...             3141.6e-3,                  -- decimal floating-point number
...             0xA23p-4;                   -- binary floating-point number
...             '\\97lo\\10\\04923"',               -- single-quoted string
...             "\\x61\\x6c\\x6f\\x0a123\\x22",     -- double-quoted string
...             [==[\nalo\n123"]==],                -- multi-line string
...         },
...         dict = {
...             [ [[kikyo]]] = true,                -- long string as key
...             ["kagome"] = false,                 -- short string as key
...             inuyasha = nil;                     --[[ name as key
...                                                      will be ignored ]]
...             19961113.E-4,               -- positive, empty fraction part
...             -.20080618e4,               -- negative, empty integer part
...         }
...     }
... """
>>> pprint(fromlua(src))
{'dict': {1: 1996.1113, 2: -2008.0618, 'kagome': False, 'kikyo': True},
 'list': [3.1416, 162.1875, 'alo\n123"', 'alo\n123"', 'alo\n123"']}
>>> pprint(tolua(fromlua(src)))
'{["list"]={3.1416,162.1875,"alo\\n123\\"","alo\\n123\\"","alo\\n123\\"",},["dict"]={[1]=1996.1113,["kikyo"]=true,["kagome"]=false,[2]=-2008.0618,},}'
>>> pprint(fromlua(tolua(fromlua(src))))
{'dict': {1: 1996.1113, 2: -2008.0618, 'kagome': False, 'kikyo': True},
 'list': [3.1416, 162.1875, 'alo\n123"', 'alo\n123"', 'alo\n123"']}
>>> assert fromlua(src) == fromlua(tolua(fromlua(src)))
>>>

Implementation Details

The parser performs the following translations.

Lua Python
table (w/ keys) dict
table (w/o keys) list
string str
number (int) int
number (real) float
true True
false False
nil None

The generator performs the following translations.

Python Lua
dict table (record style)
list table (array style)
str string
int number
float number
True true
False false
None nil

luatable's People

Contributors

tarmylan avatar zauonlok avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.