Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #97214

Re: Create a .lua fle from Python

From Peter Otten <__peter__@web.de>
Subject Re: Create a .lua fle from Python
Date 2015-09-29 19:28 +0200
Organization None
References <CAMxmM6bhyHUpQXxnwO-OiesppP6mRM74=dA37VJwV212MZGyUw@mail.gmail.com> <mueeef$d31$1@ger.gmane.org>
Newsgroups comp.lang.python
Message-ID <mailman.244.1443547713.28679.python-list@python.org> (permalink)

Show all headers | View raw


jmp wrote:

> On 09/28/2015 11:41 PM, Ariel ArgaƱaraz wrote:
>> Hi,
>> This is my first post, I would like to know if a library that can help
>> me with this.
>>
>>
>> I want to parse a XML fle with Python and save the data into a Lua table
>> called for example "newTable", then I want to create a "table.lua" fle
>> with the "newTable" write on it.
> 
>> And  I want to create a cities_temp.lua file
>>
>> cities_temps ={
>> ["Buenos Aires"] = 30,
>> ["Seatle"] = 25,
>> }
>> Can anyone give some help?
>>
>> Thanks.
>>
>> --
>> Ariel ArgaƱaraz
> 
> Use an xml parser to fetch the data from the xml file and use a template
> engine to generate the lua code.
> 
> for instance, using bs4 (beautifulsoup) for xml and jinja2 for the
> template engine:
> 
> import bs4
> import jinja2
> 
> xml = """<cities>
>       <city>
>                <name>BuenosAires</name>
>                <temperature>30</temperature>
>        </city>
> <city>
>        <name>Seatle</name>
>        <temperature>25</temperature>
> </city>
> </cities>"""
> 
> lua_template = """
> cities_temps ={
> {%- for city, temp in cities.iteritems() %}
> ["{{city}}"] = {{temp}},
> {%- endfor %}
> }"""
> 
> xmlp = bs4.BeautifulSoup(xml, 'xml')
> # from xml to python dictionary
> data = {city.find('name').string:city.find('temperature').string for
> city in xmlp.findAll('city')}
> # from python dictionary to lua
> print jinja2.Template(lua_template).render(cities=data)
> 
> 
> will yield (python 2.7):
> 
> cities_temps ={
> ["BuenosAires"] = 30,
> ["Seatle"] = 25,
> }

Is Ariel's xml file user-supplied? If so, how does your suggestion prevent 
the resulting lua script from executing arbitrary code?

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Create a .lua fle from Python Peter Otten <__peter__@web.de> - 2015-09-29 19:28 +0200

csiph-web