Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'forgive': 0.05; 'output': 0.05; 'aggregate': 0.07; 'json': 0.07; 'string': 0.09; '[1,': 0.09; 'strings.': 0.09; 'subject:How': 0.10; 'python': 0.11; 'def': 0.12; 'array.': 0.16; 'attempted.': 0.16; 'dict': 0.16; 'downside': 0.16; 'lisp': 0.16; 'newlines': 0.16; 'url:json': 0.16; 'yaml': 0.16; 'elements': 0.16; 'ignore': 0.16; 'module': 0.19; 'split': 0.19; '8bit%:5': 0.22; 'input': 0.22; 'fairly': 0.24; 'file.': 0.24; "i've": 0.25; 'pass': 0.26; 'header :In-Reply-To:1': 0.27; "doesn't": 0.30; 'message- id:@mail.gmail.com': 0.30; "skip:' 10": 0.31; 'comments,': 0.31; 'loads': 0.31; 'strip': 0.31; 'allows': 0.31; 'there.': 0.32; 'open': 0.33; 'url:python': 0.33; 'cases': 0.33; 'comment': 0.34; 'could': 0.34; 'knowledge': 0.35; 'convert': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'google': 0.35; 'really': 0.36; 'done,': 0.36; 'edge': 0.36; 'module.': 0.36; 'method': 0.36; 'subject:?': 0.36; 'url:org': 0.36; 'should': 0.36; 'example,': 0.37; 'turn': 0.37; 'list': 0.37; 'easily': 0.37; 'skip:& 10': 0.38; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'anything': 0.39; 'obtain': 0.39; 'to:addr:python.org': 0.39; 'enough': 0.39; 'either': 0.39; '8bit%:6': 0.40; 'how': 0.40; 'remove': 0.60; 'read': 0.60; 'hope': 0.61; 'helps': 0.61; 'url:3': 0.61; 'simple': 0.61; 'further': 0.61; 'real': 0.63; 'great': 0.65; 'subject:read': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:mime-version:in-reply-to:references:from:date:message-id :subject:to:content-type; bh=mxDwPc7JheNPhdh0J7jJgCbp20cf4X72B0Nzx6rKROE=; b=CHQ0KM/6+pRQXVsL1qP/iYXapLx/OLCZbh7qEnXgnD5SirZgvnRDT/C+y85rXZ1mPg njsWDA+RQyyjIVKEQM9hQmbFulPmGoiUca/Ku0oE5BJ4lXaPbXQV7NBRdlIssXkJurNG HIxPZXrRZnHEp1h68SUn01ohgpXOvOZULaMgz1hlXsXkv9EMC3qSdI07KGYDRhDcb9Ug K8yKO/hvOfWieG6Muwkr8oM++2nX2YnyznwD8rJKXVhw5BGu4s6QiVSURWoThpc8hS27 3TzkSPRJHq9AN73i7mtu7FUFqOw9MfeKocxCY3cfcaYBJGjxE9KAVMU37PmycPrVri4/ vyHw== X-Received: by 10.182.205.169 with SMTP id lh9mr14642888obc.86.1365409097406; Mon, 08 Apr 2013 01:18:17 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <4ff75708-f8e4-4ec9-8a4b-e71eb1dc2892@googlegroups.com> References: <4ff75708-f8e4-4ec9-8a4b-e71eb1dc2892@googlegroups.com> From: Barrett Lewis Date: Mon, 8 Apr 2013 01:17:57 -0700 Subject: Re: How to do a Lispy-esque read? To: Python Mailing List Content-Type: multipart/alternative; boundary=001a11c2c5c039628504d9d51423 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 110 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1365409105 news.xs4all.nl 6868 [2001:888:2000:d::a6]:36398 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:43044 --001a11c2c5c039628504d9d51423 Content-Type: text/plain; charset=ISO-8859-1 > For example, if the input stream contained the text: > [1, # python should ignore this comment > 2] > > and I do a "read" on it, I should obtain the result > [1, 2] > -- > I don't know much about lisp but given that input and the desired output you can write functions like the following def strtolist(l): if l.startswith('['): l = l[1:] if l.endswith(']'): l = l[:-1] # treat newlines as if they are commas so we can easily split l = l.replace('\n', ',').split(',') # list comprehension # strip to remove whitespace and aggregate all elements without the comment # you can further refine this to create ints by using the int() method return [x for x in l if not x.strip().startswith('#')] you would have to use input() to read the input or open a file. Either way you can pass that value to strtolist and it will convert it to a list of strings. However, this implementation is not very robust and doesn't cover a lot of edge cases (more a demo of how it might be done, I don't know your python experience so forgive me if this is all self evident). The real solution that I would use would be to use the json module. Docs: http://docs.python.org/3.3/library/json.html It allows you to take a string and turn it into a native dict or list in python. The great part is that it is fairly robust and it has a simple interface. You could take input() and send it to loads and it will return an array. The downside is json doesn't allow comments. If you really want comments, you could look into some of the yaml libraries, a quick google search shoes PyYaml is out there. I however don't have any knowledge of that module or other yaml modules as I find json is enough for anything I've ever attempted. I hope that helps --001a11c2c5c039628504d9d51423 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable

=
For example, if the input stream contained the text:
[1, # python should ignore this comment
2]

and I do a "read" on it, I should obtain the result
[1, 2]
--

I don't know much about lisp but given that input and the desir= ed output you can write functions like the following

def strtolist(l):
=A0 =A0 if l.startswith('['):=
=A0 =A0 =A0 =A0 l =3D l[1:]
=A0 =A0 if l.endswith('= ;]'):
=A0 =A0 =A0 =A0 l =3D l[:-1]

= =A0 =A0 # treat newlines as if they are commas so we can easily split
=A0 =A0 l =3D l.replace('\n', ',').split(',')<= /div>
=A0 =A0 # list comprehension
=A0 =A0 # strip to remove = whitespace and=A0aggregate=A0all elements without the comment
=A0= =A0 # you can further refine this to create ints by using the int() method=
=A0 =A0 return [x for x in l if not x.strip().startswith('#')]=

you would have to use input() to read= the input or open a file. Either way you can pass that value to strtolist = and it will convert it to a list of strings.
However, this implementation is not very robust and doesn't = cover a lot of edge cases (more a demo of how it might be done, I don't= know your python experience so forgive me if this is all self evident).

The real solution that I would use would be= to use the json module.
It allows you to take a string and turn it into a native dict or= list in python. The great part is that it is fairly robust and it has a si= mple interface. You could take input() and send it to loads and it will ret= urn an array. The downside is json doesn't allow comments.

If you really want comments, you could look= into some of the yaml libraries, a quick google search shoes PyYaml is out= there. I however don't have any knowledge of that module or other yaml= modules as I find json is enough for anything=A0I've=A0ever attempted.=

I hope that helps
--001a11c2c5c039628504d9d51423--