[Python-ideas] Re: Allow kwargs in __{get|set|del|}item__

2019-10-04 Thread Todd
On Fri, Oct 4, 2019 at 3:59 PM Caleb Donovick 
wrote:

> While there is no restriction on passing dicts to getitem.  Doing so tends
> to be a bit ugly.  I have two main use cases in mind for this syntax.
>
> The first  and perhaps the most obvious, is doing relational queries.
> ```
> where_x_1 = db[x=1]
> ```
> is more beautiful than
> ```
> where_x_1 = db[dict(x=1)]
> where_x_1 = db[{'x': 1}]
> # or by abusing slices
> where_x_1 = db['x':1]
> # or in the style of Pandas
> where_x_1 = db[db['x'] == 1]
> ```
>
> Beyond relational queries my own personal use case is a shorthand for
> dataclasses / protocols.
> ```
> foo: ProtoRecord[x=int, y=int] = DataRecord[x=int, y=int](0, 1)
> ```
> where `DataRecord[field0=T0, ..., fieldk=Tk]` generates
> ```
> @dataclass
> class Record:
>   field0: T0
>   ...
>   fieldk: Tk
> ```
> and `ProtoRecord[field0=T0, ..., fieldk=Tk]` generates a similar protocol.
>
> Allowing key value pairs in geitem need not change the interface of
> getitem.   All the key value pairs could be collected as a dict and passed
> to getitem as the index. Similar to how the all the positional arguments
> are gather into a single tuple.
> ```
> class Foo:
>   def __getitem__(self, idx):
>  print(idx)
>
> f = Foo()
> f[x=1, y=2] # {'x': 1, 'y': 2}
> ```
> This would make any legacy code using normal dicts as keys (I don't know
> how prevalent that is) automatically work with  the new syntax.
>
> There doesn't necessarily need to be support for mixing of tuple based
> indexing and keyword indexing. i.e.
> ```
> obj[0, x=1] # SyntaxError
> ```
>
> I don't really know anything about parsers but I think the grammar could
> be extended without issue with the following rule:
> ```
> subscriptlist: ... | kwargsubscript (','  kwargsubscript )* [',']
> kwargsubscript: NAME '=' test
> ```
> if `NAME '=' test` would result in ambiguity similar to argument it could
> be `test '=' test` with a block in ast.c
>
>
>-  Caleb Donovick
>
>
I thought about this in terms of labelled arrays like pandas and xarray,
but I haven't talked to them about whether they would actually want to use
such a feature so I hesitated to post it here.

One possible approach I thought of for backwards-compatibility would be to
be to create a new "kwslice" object, that would be API-incompatible and not
derived from "slice".  Anything that isn't explicitly programmed to deal
with it would simply choke on the unknown object.  The downside is the
error message would be somewhat cryptic.
___
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/FL3F7MWZKHDVV6DJVWKARAG5X4VLAZY7/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Allow kwargs in __{get|set|del|}item__

2019-10-04 Thread Caleb Donovick
While there is no restriction on passing dicts to getitem.  Doing so tends
to be a bit ugly.  I have two main use cases in mind for this syntax.

The first  and perhaps the most obvious, is doing relational queries.
```
where_x_1 = db[x=1]
```
is more beautiful than
```
where_x_1 = db[dict(x=1)]
where_x_1 = db[{'x': 1}]
# or by abusing slices
where_x_1 = db['x':1]
# or in the style of Pandas
where_x_1 = db[db['x'] == 1]
```

Beyond relational queries my own personal use case is a shorthand for
dataclasses / protocols.
```
foo: ProtoRecord[x=int, y=int] = DataRecord[x=int, y=int](0, 1)
```
where `DataRecord[field0=T0, ..., fieldk=Tk]` generates
```
@dataclass
class Record:
  field0: T0
  ...
  fieldk: Tk
```
and `ProtoRecord[field0=T0, ..., fieldk=Tk]` generates a similar protocol.

Allowing key value pairs in geitem need not change the interface of
getitem.   All the key value pairs could be collected as a dict and passed
to getitem as the index. Similar to how the all the positional arguments
are gather into a single tuple.
```
class Foo:
  def __getitem__(self, idx):
 print(idx)

f = Foo()
f[x=1, y=2] # {'x': 1, 'y': 2}
```
This would make any legacy code using normal dicts as keys (I don't know
how prevalent that is) automatically work with  the new syntax.

There doesn't necessarily need to be support for mixing of tuple based
indexing and keyword indexing. i.e.
```
obj[0, x=1] # SyntaxError
```

I don't really know anything about parsers but I think the grammar could be
extended without issue with the following rule:
```
subscriptlist: ... | kwargsubscript (','  kwargsubscript )* [',']
kwargsubscript: NAME '=' test
```
if `NAME '=' test` would result in ambiguity similar to argument it could
be `test '=' test` with a block in ast.c


   -  Caleb Donovick
___
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/EUGDRTRFIY36K4RM3QRR52CKCI7MIR2M/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Start argument for itertools.accumulate() [Was: Proposal: A Reduce-Map Comprehension and a "last" builtin]

2019-10-04 Thread Jonathan Goble
On Fri, Oct 4, 2019 at 7:41 AM  wrote:
> [spam removed]

Why is this 18-month-old thread in particular attracting spammers? The
last four messages to this thread have been three spam links and one
complaint about spam. Is there any reason for this thread to be bumped
in the future with legitimate discussion posts, and if not, would it
make sense for the list admins to just shut this thread down, and is
there a way to do that (e.g. by blacklisting or auto-rejecting future
messages to the thread)?
___
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/NSG26MRCQEVCLGR7BIIPSQKC2UBPIIFG/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Start argument for itertools.accumulate() [Was: Proposal: A Reduce-Map Comprehension and a "last" builtin]

2019-10-04 Thread trekha89
Historical gloss:  Lehman surprisingly transformed Fermat's O(n) method
into an O(cube_root(n)) method.  I believe that was the first deterministic
factoring method known beating trial division's O(sqrt(n)) time.  Alas for
Lehman, Pollard very soon after published his rho method, which runs in
probabilistic O(sqrt(p)) time where p is n's smallest prime factor, so in
worst-case probabilistic O(fourth_root(n)) time.
https://mobileappdevelopmentcompanyindia.co
___
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/5XRLJZEAXAQJISCSLTZWJBIO6XOMZHJJ/
Code of Conduct: http://python.org/psf/codeofconduct/