Create a .lua fle from Python

jmp jeanmichel at sequans.com
Wed Sep 30 11:21:07 CEST 2015


On 09/29/2015 07:28 PM, Peter Otten wrote:
> jmp wrote:
>> 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?

It does not. Like it doesn't fulfill the millions of possible 
requirements the OP could have written but did not. What if the OP want 
a thread safe, super fast, multi core solution distributed on multiple 
remote hosts ?

jm




More information about the Python-list mailing list