RE: Tuple Comprehension ???

2023-02-20 Thread avi.e.gross
Tuples are immutable and sort of have to be created all at once. This does
not jive well wth being made incrementally in a comprehension. And, as
noted, the use of parentheses I too many contexts means that what looks like
a comprehension in parentheses is used instead as a generator.

If you really want a tuple made using a comprehension, you have some options
that are indirect.

One is to create a list using the comprehension and copy/convert that into a
tuple as in:

mytuple = tuple( [x for x in range(10) ] )

I think an alternative is to use a generator in a similar way that keeps
being iterated till done.

mytuple = tuple( (x for x in range(10) ) )

And similarly, you can use a set comprehension and convert that to a tuple
but only if nothing is repeated and perhaps order does not matter, albeit in
recent python versions, I think it remains ordered by insertion order!

mytuple = tuple( {x for x in range(10) } )

There are other more obscure and weird ways, of course but generally no
need.

Realistically, in many contexts, you do not have to store or use things in
tuples, albeit some sticklers think it is a good idea to use a tuple when
you want to make clear the data is to be immutable. There can be other
benefits such as storage space used. And in many ways, tuples are supposed
to be faster than lists.

-Original Message-
From: Python-list  On
Behalf Of Michael Torrie
Sent: Monday, February 20, 2023 10:57 PM
To: python-list@python.org
Subject: Re: Tuple Comprehension ???

On 2/20/23 20:36, Hen Hanna wrote:
> For a while,  i've been curious about a  [Tuple   Comprehension] 

I've never heard of a "Tuple comprehension."  No such thing exists as far as
I know.

> So  finally   i tried it, and the result was a bit surprising...
> 
> 
> X= [ x for x in range(10) ]
> X= ( x for x in range(10) )
> print(X)
> a= list(X)
> print(a)

What was surprising? Don't keep us in suspense!

Using square brackets is a list comprehension. Using parenthesis creates a
generator expression. It is not a tuple. A generator expression can be
perhaps thought of as a lazy list.  Instead of computing each member ahead
of time, it returns a generator object which, when iterated over, produces
the members one at a time.  This can be a tremendous optimization in terms
of resource usage.  See
https://docs.python.org/3/reference/expressions.html#generator-expressions.
 Also you can search google for "generator expression" for other examples.

--
https://mail.python.org/mailman/listinfo/python-list

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: why is a search thru a Tuple slower ? ---- (meaningless indentations)

2023-02-20 Thread Michael Torrie
On 2/20/23 18:01, Hen Hanna wrote:
> is Comp.Lang.Python very active
Fairly.  Apparently the cool kids are using the Python Discourse forum.

> why is a linear search  thru  a Tuple  slower 
>  (than  thru a  (corresponding)   List ) ???

I cannot say, unfortunately.  Perhaps doing some analysis of the byte
code with the disasm module could tell you what the interpreter is doing
and why it is slower.  Since tuples are read only, I cannot think of any
reason to use them for large, generated structures. A list is far better
in my opinion.  I use tuples mainly for bundling small amounts of
information together, such as coordinates, or returning multiple values
from a function.

> sometimes,  i 'd  like to put  meaningless indentations
> like  i do (twice)  below
>  ( how can it do this ?)

Fortunately you cannot.  Such indents are syntax errors.

And I have to say it makes your emails very hard to read and understand
when you indent your sentences as you do.  Looks poetic but hard to read.

Also your python example code was not run-able either thanks to those
two extra indents which are syntax errors.  It's always helpful to post
complete and working code examples when asking for help or wanting to
get discussion on a piece of code.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: is [comprehension] the right word???

2023-02-20 Thread Michael Torrie
On 2/20/23 18:06, Hen Hanna wrote:
> is [comprehension]   the right word???
>
> i swear  i never heard the word  before
>   getting into Python  a few years ago.

Seems as though the term was borrowed from formal mathematics set theory.

A simple search reveals that the term "list comprehension" predates
Python.  Back to 1977 to be exact. The term was first coined by Phil
Wadler in the late 70s or early 80s.

https://en.wikipedia.org/wiki/List_comprehension
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Tuple Comprehension ???

2023-02-20 Thread Michael Torrie
On 2/20/23 20:36, Hen Hanna wrote:
> For a while,  i've been curious about a  [Tuple   Comprehension] 

I've never heard of a "Tuple comprehension."  No such thing exists as
far as I know.

> So  finally   i tried it, and the result was a bit surprising...
> 
> 
> X= [ x for x in range(10) ]
> X= ( x for x in range(10) )
> print(X)
> a= list(X)
> print(a)

What was surprising? Don't keep us in suspense!

Using square brackets is a list comprehension. Using parenthesis creates
a generator expression. It is not a tuple. A generator expression can be
perhaps thought of as a lazy list.  Instead of computing each member
ahead of time, it returns a generator object which, when iterated over,
produces the members one at a time.  This can be a tremendous
optimization in terms of resource usage.  See
https://docs.python.org/3/reference/expressions.html#generator-expressions.
 Also you can search google for "generator expression" for other examples.

-- 
https://mail.python.org/mailman/listinfo/python-list


Tuple Comprehension ???

2023-02-20 Thread Hen Hanna
For a while,  i've been curious about a  [Tuple   Comprehension] 

So  finally   i tried it, and the result was a bit surprising...


X= [ x for x in range(10) ]
X= ( x for x in range(10) )
print(X)
a= list(X)
print(a)



-- 
https://mail.python.org/mailman/listinfo/python-list


Re: is [comprehension] the right word???

2023-02-20 Thread Hen Hanna
On Monday, February 20, 2023 at 5:45:39 PM UTC-8, Paul Rubin wrote:
> Hen Hanna  writes: 
> > is [comprehension] the right word???
> Yes, it comes from math, particularly set theory. An expression like 
> 
> { n | n:integer, n mod 2 = 0 } 
> 
> is called a set comprehension, and then one there denotes the set of all 
> even integers. Axioms saying that the above denotes a legitimate set 
> are called comprehension axioms. In ZFC (an axiomitization of set 
> theory widely used in math), there is an infinite schema of such axioms. 
> 
> The Haskell language used a notation inspired by this for "list 
> comprehensions", and Python list (and later dictionary etc.) 
> comprehensions were inspired by Haskell's version.


thanks...   my curiosity was   re-aroused today
 when i learned that

In Italian  they say: [compresi]   as in...


  Ho 5 libri di Eco, compresi quei 3 che vedete lì.

  Ho 5 libri di Eco, inclusi 3 che non sono disponibili nella 
traduzione giapponese.



-- 
https://mail.python.org/mailman/listinfo/python-list


why is a search thru a Tuple slower ? ---- (meaningless indentations)

2023-02-20 Thread Hen Hanna
is Comp.Lang.Python very active


why is a linear search  thru  a Tuple  slower 
 (than  thru a  (corresponding)   List ) ???


sometimes,  i 'd  like to put  meaningless indentations
like  i do (twice)  below
 ( how can it do this ?)

___

import time

X= [x for x in range(1) ]
  TupX= tuple(X)
  SetX= set(X)

Items=[20, 30, 100, 200, 1000,   5000,7000,   8000, 9000 ]
Count=1000

Time = time.time() 
for _ in range(Count):
for i in X:test= i*i; test= i*i *i
print( ' \t ({0:.4f})  X'.format(time.time()-Time) )

Time = time.time() 
for _ in range(Count):
for i in TupX:test= i*i; test= i*i *i
print( ' \t ({0:.4f})  TupX'.format(time.time()-Time) )



Time = time.time() 
for _ in range(Count):
for i in Items:   test= i in X
print( ' \t ({0:.4f}) i in X'.format(time.time()-Time) )

Time = time.time() 
for _ in range(Count):
for i in Items:   test= i in TupX
print( ' \t ({0:.4f}) i in TupX'.format(time.time()-Time) )

-- 
https://mail.python.org/mailman/listinfo/python-list


is [comprehension] the right word???

2023-02-20 Thread Hen Hanna
is [comprehension]   the right word???

i swear  i never heard the word  before
  getting into Python  a few years ago.


What was it called in the Lisp (Scheme) world 

ok...  in Common Lisp,  (and MacLisp)   it was  Loop and Collect

https://en.wikipedia.org/wiki/List_comprehension#History


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Add angle brackets for required args in argparse

2023-02-20 Thread Mark Bourne

scruel tao wrote:

If we have the following code:
```
parser = argparse.ArgumentParser(description="test")
parser.add_argument('path')
```

Run it without args, will get error message:
```
usage: test.py [-h] path
test.py: error: the following arguments are required: path
```

However, I hope the message can be as the following:
```
usage: test.py [-h] 
test.py: error: the following arguments are required: path
```


The `metavar` argument to `add_argument` can be used to control how an 
argument is represented in the usage text:

```
import argparse
parser = argparse.ArgumentParser(description='test')
parser.add_argument('path', metavar='')
parser.parse_args()
```

Which results in:
```
usage: test.py [-h] 
test.py: error: the following arguments are required: 
```


Or might can consider to provide a way to let user have there own style, like:
```
usage: test.py [-h] path
```


It's also possible to create a custom help formatter, overriding 
appropriate methods to control the formatting.  For example:

```
import argparse

class CustomHelpFormatter(argparse.HelpFormatter):
def _get_default_metavar_for_positional(self, action):
default = super()._get_default_metavar_for_positional(action)
return f'<{default}>'

parser = argparse.ArgumentParser(
description='test',
formatter_class=CustomHelpFormatter)
parser.add_argument('path')
parser.parse_args()
```

Which results in:
```
usage: test.py [-h] 
test.py: error: the following arguments are required: path
```

That's a bit closer to what you asked for, since the required argument 
shown in the error message doesn't include the angle brackets.  It also 
avoids needing to specify a `metavar` for every positional argument. 
However, it is overriding a non-public method of the `HelpFormatter` 
class, so might not work across all Python versions if the name or 
signature of that method changes (even if it does work with all current 
versions, it might break in future).


--
Mark.
--
https://mail.python.org/mailman/listinfo/python-list