Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

BOFH excuse #151: Some one needed the powerstrip, so they pulled the switch plug.


comp / comp.lang.python / Re: Lprint = ( Lisp-style printing ( of lists and strings (etc.) ) in Python )

SubjectAuthor
* Lprint = ( Lisp-style printing ( of lists and strings (etc.) ) in Python )HenHanna
+- Re: Lprint = ( Lisp-style printing ( of lists and strings (etc.) ) in Python )Peter J. Holzer
+- Re: Lprint = ( Lisp-style printing ( of lists and strings (etc.) ) in Python )Gilmeh Serda
`- Re: Lprint = ( Lisp-style printing ( of lists and strings (etc.) ) in Python )Sebastian Wells

1
Subject: Lprint = ( Lisp-style printing ( of lists and strings (etc.) ) in Python )
From: HenHanna
Newsgroups: comp.lang.python, comp.lang.lisp, comp.lang.scheme
Organization: A noiseless patient Spider
Date: Fri, 31 May 2024 04:47 UTC
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: HenHanna@devnull.tb (HenHanna)
Newsgroups: comp.lang.python,comp.lang.lisp,comp.lang.scheme
Subject: Lprint = ( Lisp-style printing ( of lists and strings (etc.) ) in
Python )
Date: Thu, 30 May 2024 21:47:14 -0700
Organization: A noiseless patient Spider
Lines: 22
Message-ID: <v3bkoj$23bck$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 31 May 2024 06:47:15 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="512d4da52372882ea9ed0f897c428d2f";
logging-data="2207124"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/5ulEZE+GyhAhJ2d2KszZcSeWnKsObUNI="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:ZcI1L5Avh6iv8URcX8Qy2CRJuG8=
Content-Language: en-US
View all headers

;;; Pls tell me about little tricks you use in Python or Lisp.

[('the', 36225), ('and', 17551), ('of', 16759), ('i', 16696), ('a',
15816), ('to', 15722), ('that', 11252), ('in', 10743), ('it', 10687)]

((the 36225) (and 17551) (of 16759) (i 16696) (a 15816) (to 15722) (that
11252) (in 10743) (it 10687))

i think the latter is easier-to-read, so i use this code
(by Peter Norvig)

def lispstr(exp):
# "Convert a Python object back into a Lisp-readable string."
if isinstance(exp, list):
return '(' + ' '.join(map(lispstr, exp)) + ')'
else:
return str(exp)

def Lprint(x): print(lispstr(x))

Subject: Re: Lprint = ( Lisp-style printing ( of lists and strings (etc.) ) in Python )
From: Peter J. Holzer
Newsgroups: comp.lang.python
Date: Sat, 1 Jun 2024 08:24 UTC
References: 1 2
Attachments: signature.asc (application/pgp-signature)
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!fu-berlin.de!uni-berlin.de!not-for-mail
From: hjp-python@hjp.at (Peter J. Holzer)
Newsgroups: comp.lang.python
Subject: Re: Lprint = ( Lisp-style printing ( of lists and strings (etc.) )
in Python )
Date: Sat, 1 Jun 2024 10:24:14 +0200
Lines: 82
Message-ID: <mailman.79.1717230255.2909.python-list@python.org>
References: <v3bkoj$23bck$1@dont-email.me>
<20240601082414.zmn5vpklreqrnirr@hjp.at>
Mime-Version: 1.0
Content-Type: multipart/signed; micalg=pgp-sha512;
protocol="application/pgp-signature"; boundary="mlfggnv4wyanenuo"
X-Trace: news.uni-berlin.de 9yghq1V61FpDswbwgye26AIyad3LeXPQzezZllvLUtEQ==
Cancel-Lock: sha1:xURQrh69D2ePPSeBDypU30asG/g= sha256:h7uv/xKyJj4B1WDJ3qBWuvZhofd4WCLPq0XV2diW2Rc=
Return-Path: <hjp-python@hjp.at>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=none reason="no signature";
dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.000
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'def': 0.04; 'content-
type:multipart/signed': 0.05; '(to': 0.07; '-0700,': 0.09;
'breaks': 0.09; 'content-type:application/pgp-signature': 0.09;
'else:': 0.09; 'filename:fname piece:asc': 0.09; 'filename:fname
piece:signature': 0.09; 'filename:fname:signature.asc': 0.09;
'types.': 0.09; 'subject:Python': 0.12; '"creative': 0.16; '(it':
0.16; '__/': 0.16; 'challenge!"': 0.16; 'from:addr:hjp-python':
0.16; 'from:addr:hjp.at': 0.16; 'from:name:peter j. holzer': 0.16;
'hjp@hjp.at': 0.16; 'holzer': 0.16; 'instance': 0.16;
'precision.': 0.16; 'readability': 0.16; 'reality.': 0.16;
'stross,': 0.16; 'subject:lists': 0.16; 'subject:printing': 0.16;
'tuple': 0.16; 'tuples,': 0.16; 'url-ip:212.17.106/24': 0.16;
'url-ip:212.17/16': 0.16; 'url:hjp': 0.16; '|_|_)': 0.16;
'wrote:': 0.16; 'python': 0.16; 'to:addr:python-list': 0.20;
'code': 0.23; '(and': 0.25; 'classes': 0.26; 'object': 0.26;
'output': 0.28; 'sense': 0.28; 'example,': 0.28; 'think': 0.32;
"doesn't": 0.32; 'python-list': 0.32; 'but': 0.32; 'able': 0.34;
'header:In-Reply-To:1': 0.34; 'lists': 0.37; "skip:' 10": 0.37;
"it's": 0.37; 'handle': 0.39; 'list': 0.39; 'use': 0.39; '(with':
0.39; 'should': 0.40; 'method': 0.61; 'received:212': 0.62;
'between': 0.63; 'your': 0.64; 'well': 0.65; 'received:userid':
0.66; 'back': 0.67; 'latter': 0.69; 'url-ip:212/8': 0.69;
'formatting': 0.76; '(that': 0.84; 'compromise': 0.84;
'received:at': 0.84; 'strings': 0.84; 'subject: ( ': 0.84;
'subject: )': 0.84
Mail-Followup-To: python-list@python.org
Content-Disposition: inline
In-Reply-To: <v3bkoj$23bck$1@dont-email.me>
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
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: <https://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>
X-Mailman-Original-Message-ID: <20240601082414.zmn5vpklreqrnirr@hjp.at>
X-Mailman-Original-References: <v3bkoj$23bck$1@dont-email.me>
View all headers

On 2024-05-30 21:47:14 -0700, HenHanna via Python-list wrote:
> [('the', 36225), ('and', 17551), ('of', 16759), ('i', 16696), ('a', 15816),
> ('to', 15722), ('that', 11252), ('in', 10743), ('it', 10687)]
>
> ((the 36225) (and 17551) (of 16759) (i 16696) (a 15816) (to 15722) (that
> 11252) (in 10743) (it 10687))
>
>
> i think the latter is easier-to-read, so i use this code
> (by Peter Norvig)

This doesn't work well if your strings contain spaces:

Lprint(
[
["Just", "three", "words"],
["Just", "three words"],
["Just three", "words"],
["Just three words"],
]
)

prints:

((Just three words) (Just three words) (Just three words) (Just three words))

Output is often a compromise between readability and precision.

> def lispstr(exp):
> # "Convert a Python object back into a Lisp-readable string."
> if isinstance(exp, list):

This won't work for your example, since you have a list of tuples, not a
list of lists and a tuple is not an instance of a list.

> return '(' + ' '.join(map(lispstr, exp)) + ')'
> else:
> return str(exp)
>
> def Lprint(x): print(lispstr(x))

I like to use pprint, but it's lacking support for user-defined types. I
should be able to add a method (maybe __pprint__?) to my classes which
handle proper formatting (with line breaks and indentation).

hp
--
_ | Peter J. Holzer | Story must make more sense than reality.
|_|_) | |
| | | hjp@hjp.at | -- Charles Stross, "Creative writing
__/ | http://www.hjp.at/ | challenge!"

Attachments: signature.asc (application/pgp-signature)
Subject: Re: Lprint = ( Lisp-style printing ( of lists and strings (etc.) ) in Python )
From: Gilmeh Serda
Newsgroups: comp.lang.python
Organization: Easynews - www.easynews.com
Date: Tue, 4 Jun 2024 17:01 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!news.gegeweb.eu!gegeweb.org!usenet-fr.net!feeder1-2.proxad.net!proxad.net!feeder1-1.proxad.net!193.141.40.65.MISMATCH!npeer.as286.net!npeer-ng0.as286.net!peer02.ams1!peer.ams1.xlned.com!news.xlned.com!peer03.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!fx12.ams4.POSTED!not-for-mail
From: gilmeh.serda@nothing.here.invalid (Gilmeh Serda)
Subject: Re: Lprint = ( Lisp-style printing ( of lists and strings (etc.) ) in
Python )
Newsgroups: comp.lang.python
References: <v3bkoj$23bck$1@dont-email.me>
MIME-Version: 1.0
x-no-archive: yes
User-Agent: Pan/0.157 (Mariinka; 7c3c6087)
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Lines: 13
Message-ID: <RLH7O.49528$MLdb.33526@fx12.ams4>
X-Complaints-To: abuse@easynews.com
Organization: Easynews - www.easynews.com
X-Complaints-Info: Please be sure to forward a copy of ALL headers otherwise we will be unable to process your complaint properly.
Date: Tue, 04 Jun 2024 17:01:37 GMT
X-Received-Bytes: 1164
View all headers

On Thu, 30 May 2024 21:47:14 -0700, HenHanna wrote:

> Newsgroups: comp.lang.python,comp.lang.lisp,comp.lang.scheme

Stupid idea.

*PLONK* + all references to any replies.

--
Gilmeh

Isn't it interesting that the same people who laugh at science fiction
listen to weather forecasts and economists? -- Kelvin Throop III

Subject: Re: Lprint = ( Lisp-style printing ( of lists and strings (etc.) ) in Python )
From: Sebastian Wells
Newsgroups: comp.lang.python, comp.lang.lisp, comp.lang.scheme
Organization: A noiseless patient Spider
Date: Mon, 24 Jun 2024 05:38 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: sebastian@here.com.invalid (Sebastian Wells)
Newsgroups: comp.lang.python,comp.lang.lisp,comp.lang.scheme
Subject: Re: Lprint = ( Lisp-style printing ( of lists and strings (etc.) ) in
Python )
Date: Mon, 24 Jun 2024 05:38:07 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 42
Message-ID: <v5b0nv$7182$2@dont-email.me>
References: <v3bkoj$23bck$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 24 Jun 2024 07:38:09 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="2787d93bb44b66d3fe4dceb9b5d2d9b9";
logging-data="230658"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19nWfyBbmhvNTwzRsJsvg95qXPQLQZoZpY="
User-Agent: Pan/0.154 (Izium; 517acf4)
Cancel-Lock: sha1:rY5rTbmDHhSEpwBJxSIfuFOT+1A=
View all headers

On Thu, 30 May 2024 21:47:14 -0700, HenHanna wrote:

> ;;; Pls tell me about little tricks you use in Python or Lisp.
>
>
> [('the', 36225), ('and', 17551), ('of', 16759), ('i', 16696), ('a',
> 15816), ('to', 15722), ('that', 11252), ('in', 10743), ('it', 10687)]
>
> ((the 36225) (and 17551) (of 16759) (i 16696) (a 15816) (to 15722) (that
> 11252) (in 10743) (it 10687))
>

The direct Lispification of the original expression would probably be
something like this:

#(#("the" 36225) #("and" 17551) #("of" 16759)
#("i" 16696) #("a" 15816))

...etc, taking into account that Python "lists" are really
arrays, and there's no real Lisp equivalent to tuples,
but they're essentially arrays also. And there's a distinction
between strings and symbols in Lisp that could be approximated
in Python by defining an empty class for each desired symbol.
But since strings are used in the Python example, they should
be used in the Lisp one, too.

That written, there's not much benefit in doing this in a Python
program, and you actually lose one of the advantages you started
out with: Like Lisp, the Python syntax is readable Python. Unlike
Lisp, there's no reader that will give you the original structure
from its string representation without having to also evaluate it
as code. Lispifying it doesn't bring that advantage unless you
also implement a reader, and even then you might be better off
convincing some insider to endorse a Python analogue to JSON. But
that insider would probably tell you to use JSON, ignoring
the lack of distinct array/tuple types in JSON, or he'd tell you
to use Pickle, ignoring the fact that it's a binary format.

Norvig was coping big time. He even called Python an "acceptable"
compromise between what Lisp delivers and whatever it is that's
supposed to be good about Python that it didn't directly copy
from Lisp.

1

rocksolid light 0.9.8
clearnet tor