[Python-ideas] Re: allow initial comma

2021-04-16 Thread Hans Ginzel

On Tue, Mar 16, 2021 at 08:28:05AM -0700, Guido van Rossum wrote:

Personally, I'd like to remind you that when I designed Python my ideal was
to use punctuation in ways that are similar to the way it is used in plain
English, with exceptions only for forms commonly found in many other
programming languages such as foo.bar. Leading with a comma is most
definitely not something one would do in English.


Why not continue evolving from natural language to a programming one?
It was good approach at beginning but is nowadays the argument still relevant?
Why not to integrate programming experiences into programming language?

H.

PS:
Larry Wall was also designing Perl by natural English language.
https://bigthink.com/videos/why-perl-is-like-a-human-language
He has brought e.g. the practical
statement if condition
syntax, which is natural and
emphases the statement not the (special) condition.
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/JQ24HUYKHN3IYZCSE2DX4EU3IMVRONAV/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: allow initial comma

2021-04-16 Thread Hans Ginzel

Thank you Roland, for that idea!

On Tue, Mar 16, 2021 at 01:52:48PM +0100, Roland Puntaier via Python-ideas 
wrote:

On Mon 21Mar15 22:24, Stephen J. Turnbull wrote:

Roland Puntaier via Python-ideas writes:


Aesthetic Concern: No
=

It might seem an aesthetic concern,
but I thought a bit about it,
and I don't think it is.


I has been also conservative about leading commas.
But with huge using and generating of sql files I came to „taste“ of it.
In SQL it is very common and aesthetic and practical to write the comma
on the beginning of a line:

SELECT  …
FROM(
 SELECT column1
 ,  column2
 ,  column3
 FROM   t1
JOIN t2 ON …
 )  AS q1
 …

You can easily add or remove columns.
In addition you see the relation corresponding SELECT and FROM keywords in 
(indented) subqueries.
The possibility of leading comma before the first column is also very missing.


As you hinted, possibly add an option line above/before.
One would use it only for lines, of course. One would not write `[,1,2]`,
but one possibly would write

def long_fun(
   , long_option = 'long_expression_for_default'.split('_'),
   , option1=1
   )
   :pass


Nice. Also useful for huge dict/mappings definitions.


Can the proposal be generalized to other operators?
===

Yes.


I find the case for allowing logical line continuation by a leading
operator stronger than allowing a leading comma in a sequence display.
But I don't particularly *want* it, so I'll leave it to you to make
that argument if you want to.

Allowing operators to start an expression already has a meaning in
some cases, but I don't see a good argument for establishing a general
rule that a missing operand is the identity, even though that works
for leading "+" and "-".  It's already not the case for "*" and "**",
and "~" has no binary counterpart.


This is not my proposal, but I would welcome it.


I also.

H.
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/24KMKOC6UQIOODXWGCJGEJLIRZPW745U/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Iterable scalar values returning itself ones?

2021-04-16 Thread Hans Ginzel

On Wed, Apr 14, 2021 at 09:05:17AM -0700, Christopher Barker wrote:

so that one could write:
for i in 23:
  ...


I am proposing this ill run the cycle ones with i=23.


iter(5)
Should return the same thing as:
iter((5,))


Yes.  As I wrote in the "traverse" example below

iter(s)

should return the same thing as:

from collections.abc import Iterable
iter(s if isinstance(s, Iterable) else (s,))


which is really odd. It makes some small amount of sense if you assume that
all sequences are "flat". But even then, the distinction between a single
item and a sequence with one item in it a critical distinction that should
not be masked.


Why, please? Can you give more (concrete) examples, please?


Also - if you do this for integers, do you do it for all numbers?
what about any other single object?


As I wrote for any (not iterable) scalar/single value/object.


(and THAT would get really strange with strings!)


Strings are lists by definition.  Yes, the behaviour for strings would be
surprising.  But why to discriminate all other types, because strings are lists?


Error? Perhaps feature! You will get the desired behaviour and don't need
to handle corner case like this.

from collections.abc import Iterable
def traverse(s):
   for x in s if isinstance(s, Iterable) else (s,):
 print(f"{x=}")

traverse(5)
traverse(range(3))


H.
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/CJPRESBD7WYK6JD5DWYP4HGDWPG3KFLK/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Iterable scalar values returning itself ones?

2021-04-14 Thread Hans Ginzel

On Tue, Apr 13, 2021 at 05:39:42PM -, Dennis Sweeney wrote:

Whenever you extend the definition of an operation (`__iter__` in this case) to 
more existing objects, you lose a little bit of the ability to catch errors 
early. Consider the function:

   def traverse(something):
   for x in something:
   # do stuff
   ...

If you accidentally call `traverse(42)`, then right now, you catch it 
immediately with a TypeError on the `for x in something:` line. Under your 
proposal, you might just get a strange answer and not realize anything is wrong.


Error? Perhaps feature! You will get the desired behaviour and don't need to 
handle corner case like this.

from collections.abc import Iterable
def traverse(s):
  for x in s if isinstance(s, Iterable) else (s,):
print(f"{x=}")

traverse(5)
traverse(range(3))

H.
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/UIJRGCU4LUEZCNO4OUESKFVHPCSKHKKH/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Iterable scalar values returning itself ones?

2021-04-13 Thread Hans Ginzel

Are there any reasons not to make scalar types iterable returning the value 
ones?
Should each function check if it has got one value or a list/tuple before 
iteration over the argument?
What is wrong on scalars for iteration, please?
There is even legal to iterate over an empty set – an empty cycle.

Python 3.8.5 (default, Jan 27 2021, 15:41:15)

def chain(*iterables):

...   for iterable in iterables:
... yield from iterable
...

a = 5
b = range(3)
for i in chain(a, b):

...   print(i)
...
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 3, in chain
TypeError: 'int' object is not iterable

HG
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/UTVIGXBPAWOAPOFMN526KKSUELRXAKXC/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: class(obj) could return obj.__class__

2021-03-03 Thread Hans Ginzel

Thank you, type(o) is sufficient.
It is possible to use class properties:


type(o).__name__

'c'

On Wed, Mar 03, 2021 at 10:03:03PM +, Paul Bryan wrote:

Since class is a keyword, this is unlikely. Why is type(o)
insufficient?

On Wed, 2021-03-03 at 22:59 +0100, Hans Ginzel wrote:

>>> class c: pass
>>> o = c()
>>> o.__class__

>>> class(o)
    File "", line 1
  class(o)
   ^
SyntaxError: invalid syntax



___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/X6FJM5IBF6XBIIZM5CVISCWLND5ESXHD/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] class(obj) could return obj.__class__

2021-03-03 Thread Hans Ginzel

Please, consider class(obj) to return obj.__class__
consistenly with dir(), vars(), repr(), str(),…


class c: pass
o = c()
o.__class__



class(o)

   File "", line 1
 class(o)
  ^
SyntaxError: invalid syntax

Thank you in advance,
H.
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/HYHCN5RUW76BXSAW22UFLFBK6DVOGFJV/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] list.extend() should return self

2021-03-03 Thread Hans Ginzel

Please, is there a reason why extend() method does not return self?


a = [1,2].extend((3,4))
a
type(a)



b = [1,2]
b.extend((3,4))
b

[1, 2, 3, 4]
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/5BAHV7VURBOSFUCTPFPQ646CGMHB7RTP/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] class(obj) could return obj.__class__

2021-02-08 Thread Hans Ginzel

Please, consider class(obj) to return obj.__class__
consistenly with dir(), vars(), repr(), str(),…


class c: pass
o = c()
o.__class__



class(o)

  File "", line 1
class(o)
 ^
SyntaxError: invalid syntax

H.
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/TMFUKID6KMTEAZAS4ILBHSG23GGYNCS4/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] unix filter, one liners, awk

2020-11-05 Thread Hans Ginzel

Is there a reason, why not to make python useful for practical one liners
to replace perl and awk?
There is page Powerful Python One-Liners,
https://wiki.python.org/moin/Powerful%20Python%20One-Liners.
But almost none of them handles files, behaves like unix filter
or is ugly – must import modules, etc.

It seems that the command line options like -a, -n, -p, -F are still free. :-)
https://docs.python.org/3/using/cmdline.html

Why not use them almost the same way as in Perl?
https://perldoc.perl.org/perlrun#-n
https://perldoc.perl.org/perlrun#-p
https://perldoc.perl.org/perlrun#-a
https://perldoc.perl.org/perlrun#-Fpattern

E.g. -n would be almost equivalent to

import sys,os,re
from fileinput import *
for line in input():
<-c code comes here>
close()

-p will print(line) as last command of the for cycle.

Why to learn Perl/awk/datamash/mlr/…, for “one line like” tasks?

Thank you in advance
Hans

PS:
https://blogs.oracle.com/linux/the-top-10-tricks-of-perl-one-liners-v2
https://gist.github.com/joyrexus/7328094
https://stackoverflow.com/questions/1589994/how-do-i-write-a-unix-filter-in-python
Why not to have an elegant (one line) answer in python for questions like this?
https://stackoverflow.com/questions/40708370/drop-duplicates-and-keep-first-in-a-csv-file-in-unix
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/IT5MXIZB3LSMZTHJHVFCFADSGZOW4S2Y/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: f-string: empty expression should be allowed

2020-10-22 Thread Hans Ginzel

On Thu, Oct 22, 2020 at 11:32:36PM +1100, Chris Angelico wrote:

My recommendation here would be to separate the part where you insert
a table name from the rest of the statement:
cursor.execute(f"INSERT INTO {table} "
   "VALUES (1, '{}')")
That way, you aren't at risk of SQL injection in the rest of the
statement, and you have a very clear separation saying "hey this bit
is doing something really unusual and using interpolation in SQL".


Thank you, that is the best suggestion.

--
H.
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/RGX7KRJUUJ6O73H25FMBWEQWEKCX2QCV/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: f-string: empty expression should be allowed

2020-10-22 Thread Hans Ginzel

On Thu, Oct 22, 2020 at 08:31:34PM +1100, Steven D'Aprano wrote:

cursor.execute(f"INSERT INTO {table} VALUES (1, '{}');")
SyntaxError: f-string: empty expression not allowed


Escape the braces by doubling them:
   f"INSERT INTO {table} VALUES (1, '{{}}');"


Thank you for (ugly) workaorund.


The problem here is with the f-string, not the call to cursor.execute.
We can simplify the example to this:
   f'{}'
and avoid the distraction of JSON, SQL, databases, etc.


Technically we can, but the context give us a useful example.
Strings like f"Use braces ({}) for {something}."
are also a useful example.


Empty expression in f-string should
* (silently) expand as '{}' (opening and closing braces),
* generate a (compile time) warning if requested, e.g. with -W.


We could do that, but this is more likely to just hide bugs in the
f-string than be useful.


Thank you, that would be great and useful.
Users will be warned before potentially bug two times:
1) They will see the {} in output (which should be tested).
2) By the compiler when they ask for (-W), which is a better behaviour than 
forced carefulness.

--
BR, H.
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/2B5KEM34SUO3TXDMMZE6A332L3SKVLEF/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] f-string: empty expression should be allowed

2020-10-22 Thread Hans Ginzel

Hello,

consider this snippet please

cursor.execute(f"INSERT INTO {table} VALUES (1, '{}');")
SyntaxError: f-string: empty expression not allowed

It is (absolutely) correct to insert empty json into database table field.
Empty expression in f-string should
* (silently) expand as '{}' (opening and closing braces),
* generate a (compile time) warning if requested, e.g. with -W.

Thank you in advance
Hans
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/P2JVICNTXT5WEHGVLBGZASKBOC2CNSVF/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Missing link to git repo on the “Source code” page

2020-07-16 Thread Hans Ginzel

there are links to each releases on the “Source code” page,
https://www.python.org/downloads/source/.


Even for documentation. Each page should contain link to its source in git repo
to easily correct typos via pull requests.

E.g., where does source of the Glossary page live,
https://docs.python.org/release/3.8.3/glossary.html?

H.
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/6QUHXD3SAJNA7QKACLWW7ZJL5BBHY3VT/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Missing link to git repo on the “Source code” page

2020-07-16 Thread Hans Ginzel

Hello,

there are links to each releases on the “Source code” page,
https://www.python.org/downloads/source/.

But I am missing a ling to the (official) git repository,
e.g. https://github.com/python or which one is it.

Thank you in advance,
Hans
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/LYREGIU6LYSA34WDK7WHNEIZJ6AJMU7D/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: multidimensional lists

2020-07-08 Thread Hans Ginzel

On Wed, Jul 08, 2020 at 11:32:32PM +1000, Chris Angelico wrote:

>>> T = [[11, 12, 5, 2], [15, 6, 10], [10, 8, 12, 5], [12, 15, 8, 6]]
>>> print(T[1, 2])
TypeError: list indices must be integers or slices, not tuple

If you want a helper function, it's not hard to write it:

def fetch(collection, indices):
   for index in indices:
   collection = collection[index]
   return collection

Job done. This doesn't belong on the list type, because you might want
the exact same behaviour starting with (or incorporating) a dict, or
any other type that can be subscripted.


Thank you. Great idea. Such function could be made default method for
any subscriptable. :-)

H.
PS: For dicts there is a better solution, Munch, munchify(),
https://github.com/Infinidat/munch
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/ROFOALBTTFB5BQ2MITHH4OZWYLXNH5ES/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] multidimensional lists

2020-07-08 Thread Hans Ginzel

Why not to allow tuple as a list index?


T = [[11, 12, 5, 2], [15, 6, 10], [10, 8, 12, 5], [12, 15, 8, 6]]
print(T[1][2])

10

print(T[1, 2])

Traceback (most recent call last):
  File "", line 1, in 
TypeError: list indices must be integers or slices, not tuple

https://stackoverflow.com/questions/40361743/python-using-tuples-as-list-indices
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/DFPPECRZRQHT2WXOTYPRUHAYMYH2XQ6U/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-06-29 Thread Hans Ginzel

Thank you.

On Fri, Jun 26, 2020 at 02:50:22PM -0300, Joao S. O. Bueno wrote:

On Fri, 26 Jun 2020 at 14:30, Hans Ginzel  wrote:

thank you for making dict ordered.
Is it planned to access key,value pair(s) by index? See
https://stackoverflow.com/a/44687752/2556118 for example. Both for
reading and (re)writing?
Is it planned to insert pair(s) on exact index? Or generally to slice? See
splice() in Perl, https://perldoc.perl.org/functions/splice.html.
…


These are odd requirements.

No - Python dictionaries are ordered, by order of insertion only, but one
can't generally do any manipulation by the numeric index of
a dictionary entry - and it will stay that way.


That is fully corret to respect the _insertion_ order.


If you need such an hybrid data structure, you could just have
a list of tuples as data structure, and use collections.abc.MutableMapping
to provide a dict-like interface to it (an index for better than linear search).

I could create such a data structure if you want,


Thank you, I will write it myself.
H.
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/BWHRHYFTPJVHXKER5OUKARBS3N3OCSNK/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-06-29 Thread Hans Ginzel

Tahnk you,

On Fri, Jun 26, 2020 at 10:45:07AM -0700, Brett Cannon wrote:

Why can't you do `tuple(dict.items())` to get your indexable pairs?


of course, I can.
But how it is expensive/effective?
What are the reasons, why object dict.items() is not subscriptable – 
dict.items()[0]?



Otherwise there are no plans as you would have to introduce a new method as
you can't assume e.g. `0` is being used as a dictionary key.


I see.
What about to use the items() method – dict.items(1) for the second pair
or items(1:3) or … ?

H.


>…

___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/YP4IEBQTUNFJQQD53BIRDFSWUVG2YPFO/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Access (ordered) dict by index; insert slice

2020-06-26 Thread Hans Ginzel

Date: Fri, 26 Jun 2020 18:47:44 +0200
From: Hans Ginzel 
To: Hans Ginzel 
Subject: Access (ordered) dict by index; insert slice

Hello,

thank you for making dict ordered.
Is it planned to access key,value pair(s) by index? See 
https://stackoverflow.com/a/44687752/2556118 for example. Both for reading and 
(re)writing?
Is it planned to insert pair(s) on exact index? Or generally to slice? See 
splice() in Perl, https://perldoc.perl.org/functions/splice.html.

Use case: Represent database table metadata (columns). It is useful as to 
access columns both by name and by index as to insert column on specific 
position, https://dev.mysql.com/doc/refman/8.0/en/alter-table.html, “ALTER 
TABLE ADD COLUMN [FIRST |AFTER col]” (consider default order or table storage 
size optimisation by aligning).

Thank you in advance,
Hans
PS1: Named tuples cannot be used, are immutable.
PS2: See 
https://metacpan.org/pod/perlref#Pseudo-hashes:-Using-an-array-as-a-hash
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/S7UMTWK65X6BJDYZ3SSU7I7HOIASDMMJ/
Code of Conduct: http://python.org/psf/codeofconduct/