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


Groups > comp.lang.python > #43042 > unrolled thread

How to do a Lispy-esque read?

Started byzeta.convex@gmail.com
First post2013-04-08 00:45 -0700
Last post2013-04-08 18:46 +0100
Articles 4 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  How to do a Lispy-esque read? zeta.convex@gmail.com - 2013-04-08 00:45 -0700
    Re: How to do a Lispy-esque read? Barrett Lewis <musikal.fusion@gmail.com> - 2013-04-08 01:17 -0700
    Re: How to do a Lispy-esque read? Benjamin Kaplan <benjamin.kaplan@case.edu> - 2013-04-08 01:32 -0700
    Re: How to do a Lispy-esque read? Arnaud Delobelle <arnodel@gmail.com> - 2013-04-08 18:46 +0100

#43042 — How to do a Lispy-esque read?

Fromzeta.convex@gmail.com
Date2013-04-08 00:45 -0700
SubjectHow to do a Lispy-esque read?
Message-ID<4ff75708-f8e4-4ec9-8a4b-e71eb1dc2892@googlegroups.com>
Suppose I want to read an object from some stream. How do I do it?

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]

[toc] | [next] | [standalone]


#43044

FromBarrett Lewis <musikal.fusion@gmail.com>
Date2013-04-08 01:17 -0700
Message-ID<mailman.271.1365409105.3114.python-list@python.org>
In reply to#43042

[Multipart message — attachments visible in raw view] — view raw

> 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

[toc] | [prev] | [next] | [standalone]


#43046

FromBenjamin Kaplan <benjamin.kaplan@case.edu>
Date2013-04-08 01:32 -0700
Message-ID<mailman.272.1365410360.3114.python-list@python.org>
In reply to#43042
There is no "read in a stream until it's a valid literal" function as
far as I know, but ast.literal_eval will turn your string into an
object.

On Mon, Apr 8, 2013 at 12:45 AM,  <zeta.convex@gmail.com> wrote:
> Suppose I want to read an object from some stream. How do I do it?
>
> 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]
> --
> http://mail.python.org/mailman/listinfo/python-list

[toc] | [prev] | [next] | [standalone]


#43085

FromArnaud Delobelle <arnodel@gmail.com>
Date2013-04-08 18:46 +0100
Message-ID<mailman.298.1365443184.3114.python-list@python.org>
In reply to#43042
On 8 April 2013 08:45,  <zeta.convex@gmail.com> wrote:
> Suppose I want to read an object from some stream. How do I do it?
>
> 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]

You might be interested in code.compile_command()
(http://docs.python.org/2/library/code.html)

-- 
Arnaud

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web