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


Groups > comp.lang.python > #97214

Re: Create a .lua fle from Python

Path csiph.com!news.swapon.de!newsreader4.netcologne.de!news.netcologne.de!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'resulting': 0.04; 'subject:Python': 0.05; '"""': 0.05; '(python': 0.05; 'fetch': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.10; 'template': 0.11; 'code?': 0.16; 'help?': 0.16; 'jmp': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'temp': 0.16; 'wrote:': 0.16; 'instance,': 0.18; 'thanks.': 0.18; 'library': 0.20; 'prevent': 0.20; 'parse': 0.22; 'parser': 0.22; 'subject: .': 0.22; 'code.': 0.23; 'import': 0.24; 'xml': 0.24; 'script': 0.25; 'header:User-Agent:1': 0.26; 'example': 0.26; 'header:X-Complaints-To:1': 0.26; 'executing': 0.27; 'yield': 0.27; 'this.': 0.28; 'dictionary': 0.29; 'print': 0.30; 'skip:[ 10': 0.31; 'anyone': 0.32; 'table': 0.32; 'file': 0.34; 'so,': 0.35; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'suggestion': 0.37; 'hi,': 0.38; 'data': 0.39; 'does': 0.39; 'subject:from': 0.39; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'called': 0.40; 'some': 0.40; 'save': 0.60; 'your': 0.60; 'skip:u 10': 0.61; 'engine': 0.62; '30,': 0.63; 'city': 0.65; 'post,': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Create a .lua fle from Python
Date Tue, 29 Sep 2015 19:28:24 +0200
Organization None
References <CAMxmM6bhyHUpQXxnwO-OiesppP6mRM74=dA37VJwV212MZGyUw@mail.gmail.com> <mueeef$d31$1@ger.gmane.org>
Mime-Version 1.0
Content-Type text/plain; charset="UTF-8"
Content-Transfer-Encoding 8Bit
X-Gmane-NNTP-Posting-Host p57bd976a.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.244.1443547713.28679.python-list@python.org> (permalink)
Lines 70
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1443547713 news.xs4all.nl 23757 [2001:888:2000:d::a6]:50732
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:97214

Show key headers only | 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