[Python-ideas] Re: Create a @deprecated decorator (annotation)

2021-08-15 Thread Marco Sulla
@Deprecated is used in Java and I find it very easy to use. Remember
you can also use parameters in decorators.
___
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/OBTRKWOMEE3TUF2MZKWVZSAAV4OFQQ47/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: copy-paste code snippets to the python console

2021-08-15 Thread Marco Sulla
You can use the Jupiter console: https://jupyter.org/

On Fri, 11 Jun 2021 at 15:13, Raymond Bisdorff  wrote:
>
> Dear Python developers,
>
> It would be helpful, if the following issue with copy-pasting python
> code-snippets into the standard shell console, could be investigated and
> corrected.
>
> https://stackoverflow.com/questions/2501208/copying-and-pasting-code-into-the-python-interpreter
>
> In particular, copying and pasting from sphinx python and pycon
> code-blocks with copy button (only >>> and ... lines), is at present not
> generally working due to the  "the shell-s de-indent cmd" need for empty
> lines.
>
> See https://digraph3.readthedocs.io/en/latest/index.html
>
> Thank you in advance for you attention,
>
> Best Regards,
>
> Raymond Bisdorff
>
> --
> Raymond Bisdorff
> Emeritus Professor of Computer Science and Applied Mathematics
> University of Luxembourg
> http://rbisdorff.github.io/
>
> ___
> 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/6X6G52BYEDQYTZPX2FV5JJ4P4JH4VIL3/
> Code of Conduct: http://python.org/psf/codeofconduct/
___
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/JXG3ETPULMYJUT4RQA6JUFUJMGENIZO7/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] dict.sort()?

2021-05-29 Thread Marco Sulla
Since `dict` now is ordered, how about a `sort()` method?
It could have the same signature of list.sort(), with an optional
parameter "by" that can be "keys" or "values" ("keys" could be the
default).
___
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/XXA2E5ILMAEMFLPWZIZN3T67FERJPBFF/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Add support for private variables, methods and functions in Python

2021-05-24 Thread Marco Sulla
On Sun, 23 May 2021 at 19:50, Chris Angelico  wrote:
>
> On Mon, May 24, 2021 at 3:38 AM Marco Sulla
>  wrote:
> > > > > Do you yearn for actual refactoring tools - which do exist?
> > > >
> > > > Renaming tools of IDE do not work in 100% of the cases. For example,
> > > > if you have _variable in an eval string, it's not replaced.
> > >
> > > Another reason not to use eval.
> >
> > I completely agree, but the world is not perfect. Another example is
> > the use of the _variable in a f-string.
> >
>
> f-strings are code, and should be treated as code by any decent
> refactoring tool.

Ok, but the point is that renaming a variable is always a problem. I
check the result of renaming twice even when I use Java with Eclipse.
It seems to me that modern languages and programming strategies tend
to make refactoring as easy as possible.

>
> Language features shouldn't be dictated by poor tools, nor by poor codebases.
>
> ChrisA
> ___
> 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/6O6NDZBOWAI6DK7623HHIC7OESQ46KWV/
> Code of Conduct: http://python.org/psf/codeofconduct/
___
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/ZEQUNJFDFCPFXH5OMSK3Z2NYN76HUAUK/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: dict.get_deep()

2021-05-24 Thread Marco Sulla
On Sun, 23 May 2021 at 19:41, Todd  wrote:
>
> The pytoolz/cytoolz project already has this: 
> https://toolz.readthedocs.io/en/latest/api.html#toolz.dicttoolz.get_in

It seems a project that is used by many people. I think that JSON is
so much used that that function could be added to the builtin dict.

>
> On Sun, May 23, 2021, 11:44 Chris Angelico  wrote:
>>
>> On Mon, May 24, 2021 at 1:24 AM MRAB  wrote:
>> > Also, if the first lookup returns a list or a tuple, and an argument can
>> > be an index of that list, would be make sense to add a similar method to
>> > lists and tuples?
>>
>> Or, better: make it a stand-alone function, not a method of anything.
>> Although that kinda takes it out of python-ideas territory because it
>> could easily be a personal library function instead:
>>
>> _sentinel = object() # or see other proposals
>> def get_deep(obj, *keys, default=_sentinel):
>> if len(keys) == 1: keys = list(keys[0])
>> try:
>> for key in keys:
>> obj = obj[key]
>> except (LookupError, TypeError): # the OP did include TypeError
>> if default is not _sentinel: return default
>> raise
>> return obj
>>
>> Done. Personally, I'd just go with "except LookupError:", but this is
>> another advantage of personal library functions: you don't have to
>> bikeshed them with everyone else :)
>>
>> ChrisA
>> ___
>> 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/YM6KCRUJV2ROYV2TC44DWECFJV6TG4A6/
>> Code of Conduct: http://python.org/psf/codeofconduct/
>
> ___
> 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/MEQM2SZQD5KPVZHUM655DRR4EV42U63Y/
> Code of Conduct: http://python.org/psf/codeofconduct/
___
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/3VAIGV6CPLOMEY7K2Z4XWIXQ7HXBJXQ6/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Add support for private variables, methods and functions in Python

2021-05-23 Thread Marco Sulla
On Sun, 23 May 2021 at 17:43, <2qdxy4rzwzuui...@potatochowder.com> wrote:
>
> On 2021-05-23 at 16:30:35 +0200,
> Marco Sulla  wrote:
>
> > On Sun, 23 May 2021 at 14:50, Chris Angelico  wrote:
> > >
> > > On Sun, May 23, 2021 at 10:42 PM Marco Sulla
> > >  wrote:
> > > >
> > > > On Sun, 23 May 2021 at 14:35, Chris Angelico  wrote:
> > > > >
> > > > > On Sun, May 23, 2021 at 10:30 PM Marco Sulla
> > > > >  wrote:
> > > > > >
> > > > > > I think the only reason to introduce something like `private` is
> > > > > > refactoring. If you added a `_variable` and later you decided to
> > > > > > expose it, you have to change it to `variable`. This is something 
> > > > > > that
> > > > > > in languages like Java is not necessary, you have only to change the
> > > > > > variable from private to public. This sometimes bothered me in 
> > > > > > Python.
> > > > >
> > > > > Since you started with it private, you should be able to solve this
> > > > > with a simple search-and-replace within the class's own definition.
> > > > > Nothing outside the class should be affected. If it's that hard to
> > > > > replace "self._variable" with "self.variable"
> > > >
> > > > And, in non-trivial cases, it is :)
> > >
> > > I'm curious as to what sort of non-trivial cases you have to deal
> > > with, where you have something harder than search-and-replace
> >
> > For example, having _variable in another class.
>
> Then it isn't/wasn't private.

I'm not saying _variable of the class A I want to rename is used by
the class B. I'm saying that there could exist another class B that
has a _variable with the same name. In this case, a simple search and
replace all will fail.

>
> > > Do you yearn for actual refactoring tools - which do exist?
> >
> > Renaming tools of IDE do not work in 100% of the cases. For example,
> > if you have _variable in an eval string, it's not replaced.
>
> Another reason not to use eval.

I completely agree, but the world is not perfect. Another example is
the use of the _variable in a f-string.

>
> > > Or maybe most Python programmers don't think in terms of private
> > > members, and it's easier to just make something public from the start.
> >
> > N, I see private vars used a lot. It's @property that, it seems to
> > me, is used sporadically.
>
> I suspect that I am not "most Python programmers,", but I always think
> of state (aka members, variables, attributes) as private and internal.
> If I think I need to reach into an application level object for its
> state (to read it or to write it), or that I need a getter, let alone a
> setter, then I'm doing (or have done) somthing else wrong.
> ___
> 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/MDCRGZBOHLTC5DGJFYXGOIJZ2WECT5YL/
> Code of Conduct: http://python.org/psf/codeofconduct/
___
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/RICX4FO3XOEEVVBUZCC6UZB2VUBWUMLG/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: dict.get_deep()

2021-05-23 Thread Marco Sulla
On Sun, 23 May 2021 at 17:22, MRAB  wrote:
>
> On 2021-05-23 13:37, Marco Sulla wrote:
> > I propose to add a get_deep(*args, default=_sentinel) method to dict.
> >
> > It can accept a single argument, that must be an iterable, or multiple
> > arguments.
> >
> > The first element must be a key of the dict. If there's not a second
> > element, the value is returned. If it's present, it tries to use it as
> > an argument for the eventual __getitem__() of the value object, and so
> > on.
> >
> > In this process, if a KeyError, an IndexError or a TypeError is
> > raised, if default is set its value is returned, otherwise the
> > exception will be raised.
> >
> > Example:
> >
> > d = {1: [42]}
> > d.get_deep(1, 0)
> > # 42
> > d.get_deep(range(3), default=1981)
> > # 1981
> > d.get_deep((1, 1))
> > # IndexError: list index out of range
> >
> A slight problem here: a tuple (1, 1) can be a key of the dict.

This problem can raise only if you want to get the first level, but
for this purpose there's __getitem__ already.

You can have:

d = {(1, 1): [5, 7]}

and probably you will write

d.get_deep((1, 1), 1)

or

d.get_deep([(1, 1), 1])

>
> Also, if the first lookup returns a list or a tuple, and an argument can
> be an index of that list, would be make sense to add a similar method to
> lists and tuples?
> ___
> 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/XQAFJEZ5PABLKRF7FLDOEUDKNDGF7D34/
> Code of Conduct: http://python.org/psf/codeofconduct/
___
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/EJLZHRMRKPD6WUDFABH6ODFBIUWCRKDF/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Add support for private variables, methods and functions in Python

2021-05-23 Thread Marco Sulla
On Sun, 23 May 2021 at 14:50, Chris Angelico  wrote:
>
> On Sun, May 23, 2021 at 10:42 PM Marco Sulla
>  wrote:
> >
> > On Sun, 23 May 2021 at 14:35, Chris Angelico  wrote:
> > >
> > > On Sun, May 23, 2021 at 10:30 PM Marco Sulla
> > >  wrote:
> > > >
> > > > I think the only reason to introduce something like `private` is
> > > > refactoring. If you added a `_variable` and later you decided to
> > > > expose it, you have to change it to `variable`. This is something that
> > > > in languages like Java is not necessary, you have only to change the
> > > > variable from private to public. This sometimes bothered me in Python.
> > >
> > > Since you started with it private, you should be able to solve this
> > > with a simple search-and-replace within the class's own definition.
> > > Nothing outside the class should be affected. If it's that hard to
> > > replace "self._variable" with "self.variable"
> >
> > And, in non-trivial cases, it is :)
>
> I'm curious as to what sort of non-trivial cases you have to deal
> with, where you have something harder than search-and-replace

For example, having _variable in another class.

> Do you yearn for actual refactoring tools - which do exist?

Renaming tools of IDE do not work in 100% of the cases. For example,
if you have _variable in an eval string, it's not replaced.

> Or maybe most Python programmers don't think in terms of private
> members, and it's easier to just make something public from the start.

N, I see private vars used a lot. It's @property that, it seems to
me, is used sporadically.

>
> ChrisA
> ___
> 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/OT54AL5JEPPOK3PK5WACHAMZ6KNFGRKO/
> Code of Conduct: http://python.org/psf/codeofconduct/
___
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/YLBB7WAM2YCEFSCURUHY7TTNWL3NRXTE/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: dict.get_deep()

2021-05-23 Thread Marco Sulla
On Sun, 23 May 2021 at 15:30, Thomas Grainger  wrote:
>
> seems a bit like https://www.python.org/dev/peps/pep-0505/
>
> eg `d?[1]?[0]`

No, I do not want to suppress the exception, only to have a way to
access a nested object in a complicate dict, for example a dict
generated by a JSON.

In your example,

d = {1: [42]}
d.get_deep(2, 0)
# KeyError: 2
d = None
d.get_deep(1, 0)
# AttributeError: 'NoneType' object has no attribute 'get_deep'


> ___
> 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/ZKYLSYU7U673RX6XQLYBSC2HGEAO5NES/
> Code of Conduct: http://python.org/psf/codeofconduct/
___
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/5CPPV3VADIF57Y4PPVZROOHGX45I7U6D/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Add support for private variables, methods and functions in Python

2021-05-23 Thread Marco Sulla
On Sun, 23 May 2021 at 14:35, Chris Angelico  wrote:
>
> On Sun, May 23, 2021 at 10:30 PM Marco Sulla
>  wrote:
> >
> > I think the only reason to introduce something like `private` is
> > refactoring. If you added a `_variable` and later you decided to
> > expose it, you have to change it to `variable`. This is something that
> > in languages like Java is not necessary, you have only to change the
> > variable from private to public. This sometimes bothered me in Python.
>
> Since you started with it private, you should be able to solve this
> with a simple search-and-replace within the class's own definition.
> Nothing outside the class should be affected. If it's that hard to
> replace "self._variable" with "self.variable"

And, in non-trivial cases, it is :)

> then you can always
> create a property to make it available under both names.

I use property a lot, but I noticed that the majority of programmers
do not use them. Also asyncio, written by Guido himself, uses normal
getters and setters. Maybe @property slows down the code?

>
> ChrisA
> ___
> 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/L33KIIKBY4ZT7CVJXLGHXMXDCAM7ZQWH/
> Code of Conduct: http://python.org/psf/codeofconduct/
___
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/ZHWEL7FV7HD2QAHQVXGLE4BTKIPU27VX/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] dict.get_deep()

2021-05-23 Thread Marco Sulla
I propose to add a get_deep(*args, default=_sentinel) method to dict.

It can accept a single argument, that must be an iterable, or multiple
arguments.

The first element must be a key of the dict. If there's not a second
element, the value is returned. If it's present, it tries to use it as
an argument for the eventual __getitem__() of the value object, and so
on.

In this process, if a KeyError, an IndexError or a TypeError is
raised, if default is set its value is returned, otherwise the
exception will be raised.

Example:

d = {1: [42]}
d.get_deep(1, 0)
# 42
d.get_deep(range(3), default=1981)
# 1981
d.get_deep((1, 1))
# IndexError: list index out of range
___
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/O53OTTDKWPQ7HZCODZSM7ZVNAQ5HVZG6/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Add support for private variables, methods and functions in Python

2021-05-23 Thread Marco Sulla
I think the only reason to introduce something like `private` is
refactoring. If you added a `_variable` and later you decided to
expose it, you have to change it to `variable`. This is something that
in languages like Java is not necessary, you have only to change the
variable from private to public. This sometimes bothered me in Python.
___
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/BCYFOTTKO5MO7Q4P72V47Z3QDHJBCOVA/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Enum: determining if a value is valid

2021-03-16 Thread Marco Sulla
On Mon, 15 Mar 2021 at 20:49, Ethan Furman  wrote:
> Everything considered, I think I like allowing `__contains__` to verify both 
> names and values

What about Enum.values()?

> adding `default=` to the constructor for the value-based "gimme an 
> Enum or None" case

What's the use case, apart checking if the value is a "member" of the enum?
___
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/QMCYEAAZ3UELOL5G3T2T3G72KVKNAJV3/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Enum: determining if a value is valid

2021-03-16 Thread Marco Sulla
On Tue, 16 Mar 2021 at 05:38, Matt Wozniski  wrote:
> Color.from_value(1)  # returns Color.RED

What if I have an alias?
___
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/NQ535PUFCWRBBN5QVTGB7QOBNJNJJEPO/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Add an __exclude_all__ complement to __all__

2021-03-05 Thread Marco Sulla
On Wed, 3 Mar 2021 at 23:59, Brendan Barnwell  wrote:
>  But usually you want to define it at the beginning as a sort of
> documentation aid ("this is the public API").

This is a little off-topic, but I'm curious, since usually, for public
functions and classes, I do

__all__ = (Class.__name__, func.__name__, ...)

So I have to put it at the end of the module. I do this because if I
change the class or function name and I forget to change it in
__all__, I get an exception.

Furthermore, if there's a module composed by submodules, I usually do

from .a import *
from .b import *

__all__ = a.__all__ + b.__all__

In your opinion, these are good or bad practices?
___
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/AZLCRXYWZUS63RDSAUEQ52SGRXGKY3KE/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Deprecate/change the behaviour of ~bool

2021-02-22 Thread Marco Sulla
On Tue, 23 Feb 2021 at 02:13, Soni L.  wrote:
>
> Currently ~False is -1 and ~True is -2. Would be nicer if ~bool was the
> same as not bool.

I suspect this is more for NumPy people. I remember that one of my
bosses used `~a` on a bool because he started with NumPy and had a
limited knowledge of Python.
___
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/ISG7IASKULB4SP7TW5GC7I5BUIEDYO4E/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Option to not raise if file does not exists for os.remove()?

2021-01-01 Thread Marco Sulla
On Tue, 29 Dec 2020 at 22:40, Eelke van den Bos  wrote:
>
> Hi Sergio,
>
> The pathlib module includes this feature:
> https://docs.python.org/3/library/pathlib.html#pathlib.Path.unlink
>
> Best,
>
> Eelke

I add that it's quite common to skip FileNotFoundError in removing a
file. I think it's the same because exist_ok was added to
os.makedirs().

Can I try: a PR?
___
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/MUEIT2RPERM2IH52EWRBY3UILL4SWH2E/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Standard tool for iterating over recursive data structures?

2021-01-01 Thread Marco Sulla
On Fri, 1 Jan 2021 at 06:38, Steven D'Aprano  wrote:
> Relevant: https://bugs.python.org/issue42801

Can't reproduce on the latest trunk (3.10). I get 1989 as a result.
___
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/2FFECDJEXGYY5UDLPNOUOMOCDIEBXWLZ/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Option to not raise if file does not exists for os.remove()?

2020-12-29 Thread Marco Sulla
What about a parameter, false by default, that suppresses the
FileNotFoundError exception if true for os.remove()? Something similar
to exist_ok for os.makedirs().
___
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/3SGQVZ4AVZX33NFVPDDELLUOAYQ5IZGG/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Possibility to decorate single code line or code block?

2020-12-18 Thread Marco Sulla
Maybe it's a crazy idea, but what if we could decorate a single line
of code? For example:

@Timer
a = time_consuming_function()

This will be equivalent to using Steven's context manager, but the
decorator is more simple to comment and uncomment.

Maybe it could be possible to comment also code blocks:

@parallel
for x in y:
do_something(x)
now_something_completely_different(x)

This could be a shortcut for multiprocessing.
___
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/EQKDYPHSMEL6QO6DN4GQGJKHKOGUBMBJ/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Using explicit parenthesization to convey aspects of semantic meaning?

2020-12-16 Thread Marco Sulla
On Wed, 16 Dec 2020 at 20:18, Paul Sokolovsky  wrote:
> But still, are there Python implementations which compile "(a.b)()"
> faithfully, with its baseline semantic meaning? Of course there're.

OK, Paul, why don't you propose a PR and a bug report about it?
___
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/5HIBV5PZAJ4VB65D5YZSIQOIMLLVAPMZ/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: [Feature Request] Member variable as member function default argument values

2020-12-16 Thread Marco Sulla
On Wed, 16 Dec 2020 at 19:52, Abdulla Al Kathiri
 wrote:
>
> Or more concise
> def method(self, spam, eggs, cheese, *args):
> spam = spam or self.spam
> eggs = eggs or self.eggs
> #etc., The above is equivelent to the following:
> spam = spam if spam else self.spam
> eggs = eggs if eggs else self.eggs
> # I prefer the first approach..

What if spam, for example, is zero?
___
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/2R5BBCBNA6ITHLNSSZYUQPCSGJLY44RT/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Using explicit parenthesization to convey aspects of semantic meaning?

2020-12-15 Thread Marco Sulla
On Tue, 15 Dec 2020 at 17:41, Chris Angelico  wrote:
> I learned BOMDAS - Brackets, O (varies in expansion but always minor
> things you don't often see), Multiplication, Division, Addition,
> Subtraction. For some reason it's also written BODMAS, which has the
> exact same meaning (since multiplication and division have the same
> precedence) but is harder to pronounce. PEMDAS uses "parentheses"
> instead of "brackets" (so it's probably an American English vs British
> English thing), and "exponentiation" in place of the first vowel.

This is the most interesting thing in the whole discussion, IMHO.
___
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/EBTHAXKGD3VZ3VSZIBOULATR23CKUXGI/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Function for fetching what months between two dates

2020-12-15 Thread Marco Sulla
On Tue, 15 Dec 2020 at 15:32, Steven D'Aprano  wrote:
>
> On Tue, Dec 15, 2020 at 03:08:51AM -, qaidjoharbarbh...@gmail.com wrote:
> > Hello,
> >
> > Greetings!
> >
> > I have the following idea of adding a function in Lib/datetime.py
> > which when called with the datetime.date object returns the months
> > between the object datetime.date and the object datetime.date passed
> > as the date_to argument in function.
>
> What is this function used for?
>
> I see it returns a list of (year, month_number) tuples. Once I have
> collected those (year, month) tuples, what do I do with them?

It will be more useful if the returned values are datetime.date
objects. The library arrow has arrow.Arrow.range("month", startdate,
enddate)
___
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/X55X3DWMYPV6NQYOBI6PL7PRSSNSLWV2/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Making the for statement work better with nested functions

2020-12-02 Thread Marco Sulla
On Wed, 2 Dec 2020 at 09:27, Chris Angelico  wrote:
> But you can make your own private research project without asking
> anyone else for information. Why try to synchronize with anyone else?
> Why not just make your own thing and find out what constness can do
> for Python?

I agree. I think it will be very interesting some macro-benchmarks
(read: pyperformance) before proposing this big change.

Anyway, personally I think a "const" can be useful for developers to
avoid changing or rebinding the object by mistake, not primarily for
speeding up things.
___
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/FNXQZQN6OML7KCS43NBLSSYACWOS3S45/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: [RFC] "Strict execution mode" (TL;DR version)

2020-12-01 Thread Marco Sulla
On Tue, 1 Dec 2020 at 23:49, Paul Sokolovsky  wrote:
> On Wed, 2 Dec 2020 09:16:56 +1100
> Chris Angelico  wrote:
> > If the restricted execution model is incompatible with most Python
> > scripts, why would anyone bother to use it?
>
> E.g. because someone who would want to experiment with JIT, would need
> to apply similar restrictions anyway.

So do you think that the strict mode can help people to create a JIT for Python?
Why can't this be done in a separate project, like PyPy or Pycopy?
___
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/ML3FAKTM67DK6GJWJIJZ5WWYQQFM6FV3/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: [RFC] "Strict execution mode" (TL;DR version)

2020-12-01 Thread Marco Sulla
I think that what you want is another language, that already exists
and it's RPython:

https://rpython.readthedocs.io/en/latest/rpython.html

See constants paragraph.

RPython is used to create PyPy, not to limit normal Python programming :-)
___
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/H3HOXPVCGA6BSGUG3BNNWM2JT7ATZ4OF/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Making the for statement work better with nested functions

2020-12-01 Thread Marco Sulla
On Tue, 1 Dec 2020 at 13:16, Paul Sokolovsky  wrote:
> If you want immutable dict you [know whom to thank for the lack of it -
> stroked thru] can use types.MappingProxyType, as was explained (it's
> all about PEP603).
>
> The last case with immutable dict also shows that proliferation of both
> mutable and immutable type counterparts doesn't scale. What we need is
> some generic types.roproxy (yes, all lower-case, to emphasize its
> fundementalness) which can be applied to any object, and will filter
> out __setitem__ and __setattr__ (and del counterparts, and custom list
> of mutator methods, you get an idea).

Is it not more simple for the moment to have it only as a sort of
warning for the developer? I mean, if the object is mutated, a warning
is raised at runtime. If the object is rebound, a SyntaxError is
emitted.

I suppose that this is possible for dicts, since they have an internal
attribute ma_version_tag, that is increased every time the dict
mutates. Classes have __dict__. Don't know for the other builtin
types.

Speed optimizations could be done later, if they are possible.
___
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/4GIKATDJM6BOJO3S5WZXP27RYMXGU4I3/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Making the for statement work better with nested functions

2020-12-01 Thread Marco Sulla
"Immutable", like C. Not rebound has another keyword, in Java for
example (final). If we want to only avoid rebounding, I think const
will be a bit confusing for C/C++ people.

(I wrote "immutable" because in C constness can be removed)

On Tue, 1 Dec 2020 at 01:12, Chris Angelico  wrote:
>
> On Tue, Dec 1, 2020 at 10:25 AM Marco Sulla
>  wrote:
> >
> > On Mon, 30 Nov 2020 at 23:26, David Mertz  wrote:
> > > Somehow "dire" doesn't strike me as the right word Maybe you were 
> > > looking for "conceivably useful in niche cases."?
> >
> > Well, I think const can be useful for:
> > * multiprocessing. Now, for example, dict is passed between processes
> > using MappingProxyType, which is slow.
> > * avoid side effects. I expect that my object will not change and I
> > want to be sure I'll not change it by mistake. Mistake that I made a
> > lot of times.
> > * contract. A function marks a parameter as const will guarantee that
> > the object will not be changed. It's something complementar to
> > annotations.
> > * possible future speed improvements. For example, if an iterable is
> > const, you can skip a lot of checks about mutability on iteration and
> > make it more fast.
>
> Are you assuming that "const" means "will not be rebound" or "is
> immutable"? Or both?
>
> ChrisA
> ___
> 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/YFDQCVIIJAXAUJ54C7C4D7L6WQFKJ3EI/
> Code of Conduct: http://python.org/psf/codeofconduct/
___
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/ZCZOWSLNZSPQ6H46XDB6C76CX64ZKSON/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Making the for statement work better with nested functions

2020-11-30 Thread Marco Sulla
On Mon, 30 Nov 2020 at 23:26, David Mertz  wrote:
> Somehow "dire" doesn't strike me as the right word Maybe you were looking 
> for "conceivably useful in niche cases."?

Well, I think const can be useful for:
* multiprocessing. Now, for example, dict is passed between processes
using MappingProxyType, which is slow.
* avoid side effects. I expect that my object will not change and I
want to be sure I'll not change it by mistake. Mistake that I made a
lot of times.
* contract. A function marks a parameter as const will guarantee that
the object will not be changed. It's something complementar to
annotations.
* possible future speed improvements. For example, if an iterable is
const, you can skip a lot of checks about mutability on iteration and
make it more fast.
___
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/GVYJJCPRBYTTXFJ7GYTUMSDVYUWJZUJU/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: adding a timeit.Timer context manager and decorator to time code/functions

2020-11-30 Thread Marco Sulla
On Mon, 30 Nov 2020 at 03:39, Chris Angelico  wrote:
>
> No, Serhiy meant that you can pass a function to timeit.

Aaah, didn't know this.


On Mon, 30 Nov 2020 at 12:21, Steven D'Aprano  wrote:
>
> On Mon, Nov 30, 2020 at 12:11:01AM +0100, Marco Sulla wrote:
>
> > You can get the code of a function as a string using `inspect`.
>
> *Sometimes*.
>
>
> >>> import inspect
> >>> def func():
> ... return 1
> ...
> >>> inspect.getsource(func)
> Traceback (most recent call last):
>   [...]
> OSError: could not get source code

So `inspect.getsource()` does not work in the REPL. It does make sense.

Anyway, usually I use cProfile, I find the "most slow" function and
then I measure the more suspicious lines. So personally I'm more
interested in a context manager for timeit than a decorator.

I know that PyCharm can report you the speed line per line, but not
the free version.


On Mon, 30 Nov 2020 at 12:21, Steven D'Aprano  wrote:
> I have been using a variant of this for years now:
>
>
> https://code.activestate.com/recipes/577896-benchmark-code-with-the-with-statement/

I think it's not a bad idea to have it in timeit.
___
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/KGNVKZEOTGAYPW5CNUU6BVGVGYCW3XKJ/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Making the for statement work better with nested functions

2020-11-29 Thread Marco Sulla
On Sun, 29 Nov 2020 at 23:34, Guido van Rossum  wrote:
> OTOH if we were to introduce 'let' or 'const' in the language, it would 
> surely be usable to solve a whole bunch of other problems, in addition to 
> giving us a cleaner way to solve the value capture problem.

Well, IMHO let could help the famous misspelled var problem:

myvar = 5
[...]
myvvar = 7  # Whops!

but to work, if let is used somewhere in the scope, all the variables
declared in the scope must be declared with let:

let myvar = 5
myvvar = 7  # SyntaxError

But I'm really unsure that this is what you want.
___
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/WZ3LWBIXYKT33IFSFPVQVTERANOK74AH/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Add decorator_with_params function to functools module

2020-11-29 Thread Marco Sulla
You can use classes as decorators, it's quite more simple:
https://towardsdatascience.com/using-class-decorators-in-python-2807ef52d273?gi=ea5091974462
___
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/I6GUPJDYILR7SBSWATAULHYL2X4GGBRL/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: adding a timeit.Timer context manager and decorator to time code/functions

2020-11-29 Thread Marco Sulla
On Sun, 29 Nov 2020 at 21:45,  wrote:
> To use timeit (or the current Timer class), one has to write the stmt as a 
> string which is not convenient (yet I understand that if you want to time a 
> code snippet by running it more than once there may be not alternative than 
> using stmt as strings)

You can get the code of a function as a string using `inspect`. Don't
know about generic code, maybe with `ast`? Or you can use the
`globals` parameter of timeit and pass the function name, if I
understood what Serhiy meant:


def timefunc(func, *args, name="f", stmt=None, **kwargs):
try:
globs = kwargs.pop("globals")
except KeyError:
globs = {}

globs[name] = func

if stmt is None:
stmt = f"{name}()"

timeit.timeit(stmt, *args, globals=globs, **kwargs)

(not tested)
___
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/42DTPO2SRVAQYAX3XLVBXTYKFJL5WVL7/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: adding a timeit.Timer context manager and decorator to time code/functions

2020-11-28 Thread Marco Sulla
On Fri, 27 Nov 2020 at 17:11,  wrote:
>
> When optimizing code, I often need to timeit a part of code or a function.
> I am using then the following class
> [...]
> that I can use either as a context manager in
> ```
> with Timer("how long does this take?") as t:
> time.sleep(1)
> # output: DEBUG:Timer:'how long does this take?' ran in 1.000 seconds
> ```
> or as a decorator
> ```
> @Timer()
> def my_function():
> """this is my function"""
> time.sleep(2)
> my_function()
> # output: DEBUG:Timer:'my_function' ran in 2.000 seconds
> ```
>
> This class could be added to the timeit module as it fits well its functional 
> scope.

timeit already has a Timer class that runs the code n times, and other
features. I suppose a decorator for timeit should have all the
features of timeit.Timer and the same overhead (not more).
___
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/XZWSB4NLJHWBI3N4AIXTNJORGMEF2NUF/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Enable subscription operator for generator expressions

2020-11-18 Thread Marco Sulla
On Wed, 18 Nov 2020 at 00:31, Oscar Benjamin 
wrote:

> I can write many things myself. That doesn't mean that it wouldn't be
> good if someone already wrote it for me (and for everyone else).
>

What about more_itertools?
___
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/UL46MUFGY53RS6UFJVFTQ6WRRVFIPFAQ/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Experimental syntax proposal

2020-10-24 Thread Marco Sulla
On Sat, 24 Oct 2020 at 21:49, André Roberge  wrote:

> No, it does not. It proposes actual changes to the Python interpreter.
>
> Under my proposal, something like what is proposed there would first be
> implemented as a third party package.
>

Not sure, but yes, the PEP proposes a change to the interpreter, but in
such a way that you can define your own macro, ie custom keyword(s) in a
third party package that can be import!ed.
___
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/LIS4HZL6B7T7PG2AKO2UCL66PHYTDYH4/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Conditional function/method/dict arguments assignments

2020-10-24 Thread Marco Sulla
I had many times the same idea: why can't we just "say" to the called
function "use your own default"? I'm quite sure this is possible in a
tricky way, since defaults are stored in the function object.

Anyway, honestly I don't like your syntax proposal.
___
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/GFLFFS2JL4S7UKDF2OK57VCFVYA6O6RD/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Experimental syntax proposal

2020-10-24 Thread Marco Sulla
See PEP 638:
https://www.python.org/dev/peps/pep-0638/

If I have understood well, it proposes what you want
___
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/44NCSH4MCBVMTP6SI2LUI5A3GXHMDYVT/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Dict unpacking assignment

2020-10-23 Thread Marco Sulla
On Fri, 23 Oct 2020 at 09:39, Steven D'Aprano  wrote:

> Using PEP 634 syntax, I could write:
>
>
> def method(self, **kwargs):
> {'spam': spam, 'eggs': eggs, **kw} = **kwargs
> process(spam, eggs)
> super().method(**kw)
>

I like that.
___
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/E4XRNYEARYNGXQGL3XKTTJSSE2ZU3NKW/
Code of Conduct: http://python.org/psf/codeofconduct/


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

2020-10-23 Thread Marco Sulla
On Fri, 23 Oct 2020 at 08:06, Random832  wrote:

> On Thu, Oct 22, 2020, at 21:00, Steven D'Aprano wrote:
> I suspect that calling this particular syntax ugly is picking at a bit of
> an open wound in the history f-string implementation... consider precisely
> why the escaping syntax is {{}} instead of \{\}, and all the implications
> of that.
>

It's the same syntax of format()
___
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/RYFSESES4JQKGW4FQ6OAAPM5PAWX6WUX/
Code of Conduct: http://python.org/psf/codeofconduct/


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

2020-10-22 Thread Marco Sulla
On Thu, 22 Oct 2020 at 12:36, Hans Ginzel  wrote:

> 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.
>

It's not ugly for me too, but if you want another workaround:

d = {}
f"INSERT INTO {table} VALUES (1, '{d}');"
___
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/F63STJCC3MFLMYSS7KTKSRO3VGZOP27V/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: New feature

2020-10-17 Thread Marco Sulla
On Sat, 17 Oct 2020 at 21:50, Ethan Furman  wrote:
>
> On 10/17/20 10:54 AM, Marco Sulla wrote:
>
> > I think that in this case `clear` simply writes N enter chars, until
> > the terminal is "cleared". IMHO this is the safest option.
>
> 'clear' should also leave the cursor in the upper-left position, which cannot 
> be gotten by writing a bunch of line feeds.

Because someone wants to support `cls` or `clear` inside a statement?
I suppose it can  be done, but I don't see the use case.
___
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/VYT5DK26YJQA55FOQ75NF7A2DSQ6TIVI/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: New feature

2020-10-17 Thread Marco Sulla
On Sat, 17 Oct 2020 at 03:37, Steven D'Aprano  wrote:
>
> On Fri, Oct 16, 2020 at 11:30:56PM +0200, Marco Sulla wrote:
>
> > Well, in terminals like bash, `clear` does not really delete the
> > previous input. It simply move the scroll so the first line of the
> > input is the current input.
>
> That's not actually correct: in bash, `clear` actually deletes the
> scrollback buffer too.
>
> In modern Linuxes, `clear` takes an option `-x` which suppresses that
> behaviour. Perhaps you have an alias?
>
> alias clear='clear -x'

Well, this is interesting. From `man clear`:

clear  clears  your screen if this is possible, including its
scrollback buffer (if the extended “E3” capability is defined).
-x   do not attempt to clear the terminal's scrollback buffer using
the extended “E3” capability.

So probably I always used pseudoterminal without E3 capability.

I think that in this case `clear` simply writes N enter chars, until
the terminal is "cleared". IMHO this is the safest option.
___
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/M6XXYVQAQNTHFT7F33452P6E62CO5GAM/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: New feature

2020-10-16 Thread Marco Sulla
Well, in terminals like bash, `clear` does not really delete the
previous input. It simply move the scroll so the first line of the
input is the current input.

Maybe the REPL can emulate this in some way.
___
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/LQBOO75BLG4CCVHNIFNK5K752ZKOHKON/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Fwd: Re: Experimenting with dict performance, and an immutable dict

2020-10-15 Thread Marco Sulla
I forgot: I noticed that creating dict from a matrix N*2 is not
optimized for lists and tuples. Is this because creation from a
sequence2 is not much used?
___
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/UPVENYZLEDIBKCPF5W2EBEOPC3DK2XG2/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Fwd: Re: Experimenting with dict performance, and an immutable dict

2020-10-15 Thread Marco Sulla
Well, it seems to work now, but the creation from a combined, without
holes, dict, is definitively faster no more. On the contrary, it is 2x
slower.

This is probably because

1. combined dicts needs only one memcpy
2. probably I wrong something in my code, since I need TWO Py_INCREF
for keys and values:
https://github.com/Marco-Sulla/cpython/blob/2eea9ff796685127fc03fcc30ff6c652ed18f5db/Objects/dictobject.c
(It's frozendict_clone and it's used in frozendict_merge)

Iteration continues to be faster. Probably also creation from dict
with holes, I did not test it.

I suppose frozendict can improve memory space using shared keys and
shared frozendicts.

Probably I'll try to write a C extension, even if I'll need a lot of
help, in another mailing list.

I have some random remarks about possible improvements to dict performance:

A. lookdict functions that are unicode only could return zero
immediately if the searched key is not a string, instead of using the
basic lookdict
B. every time the dict is changed, the keys could be checked if they
are all unicode with an internal version of _PyDict_HasOnlyStringKeys
(I created it for frozendict, it's dict_has_only_unicode_keys_exact)
and change the dk_lookup accordingly. If the function changes only one
value, it's sufficient to check if the value if unicode or not, and
the original lookup func
C. can USABLE_FRACTION be substituted with mp->ma_keys->dk_usable?
___
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/NK5QUSRGXQLNT62LIY5KXV4PURQNPEVV/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Exact decimal multiplication and division operations

2020-10-11 Thread Marco Sulla
On Sun, 11 Oct 2020 at 19:53, Wes Turner  wrote:
> Arbitrary-precision multiple-precision floats in Python: mpmath, gmpy, sympy 
> .evalf() / N()

mpmath has always a global precision:
http://mpmath.org/doc/current/basics.html#setting-the-precision

About SymPy, I worked a little with it within Sage, and it was really
amazing, but I think it's too much for the current goal.
___
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/BSYDEZOIK4DOJFGTPS3X6WO53T5KMGW3/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Exact decimal multiplication and division operations

2020-10-10 Thread Marco Sulla
On Sat, 10 Oct 2020 at 19:28, Tim Peters  wrote:
> Try to spell out what you mean - precisely! - by "this". I can't do
> that for you. For any plausible way of fleshing it out I've thought
> of, the answer is "no".

Well, please, don't be so harsh. I'm trying to discuss to someone that
co-created Python itself, it's not simple to me :-P

> The closest you can get to BigDecimal's behavior "by magic" in Python
> is to set the context precision to its maximum allowed value.

I think there's another "trick" to get the BigDecimal behaviour.
If you read the Javadoc, it says that each operation has a default
precision. For example, multiplication a*b has precision = a_scale +
b_scale. So, in reality, also BigDecimal has a context with finite
precision. The difference is that the default context has a variable
precision, depending on the operation.

Could Python decimal have something similar, maybe by setting prec = -1?
___
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/O7KUARDPTMJ36FEYJTKGEUPHFG3A5BOG/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Exact decimal multiplication and division operations

2020-10-10 Thread Marco Sulla
On Fri, 9 Oct 2020 at 23:41, Tim Peters  wrote:
> But the decimal spec takes a different approach, which Python's docs
> don't explain at all:  the otherwise-mysterious ROUND_05UP rounding
> mode.  Quoting from the spec:
>
> http://speleotrove.com/decimal/damodel.html
> ...
> The rounding mode round-05up permits arithmetic at shorter
> lengths to be emulated in a fixed-precision environment without
> double rounding. For example, a multiplication at a precision of 9
> can be effected by carrying out the multiplication at (say) 16
> digits using round-05up and then rounding to the required length
> using the desired rounding algorithm.
>
> In your original example,  1.01 * 1.46 rounds to 4-digit 1.474 under
> ROUND_05UP. and then `quantize()` can be used to round that back to 1,
> 2, or 3 digits under any rounding mode you like.
>
> Or, with your last example,
>
> >>> with decimal.localcontext() as ctx:
> ... ctx.rounding = decimal.ROUND_05UP
> ... r = D('1.01')*D('1.46')
> >>> r
> Decimal('1.474')
> >>> r.quantize(D('.01'))
> Decimal('1.47')

And can't be this the default of decimal? For what I know, this is the
default of BigDecimal in Java:

> public BigDecimal multiply(BigDecimal multiplicand)
>
> Returns a BigDecimal whose value is (this × multiplicand), and whose scale is 
> (this.scale() + multiplicand.scale()).
> Parameters:multiplicand - value to be multiplied by this 
> BigDecimal.Returns:this * multiplicand
>
>
> public BigDecimal multiply(BigDecimal multiplicand,
>MathContext mc)
>
> Returns a BigDecimal whose value is (this × multiplicand), with rounding 
> according to the context settings.

Example online:  http://tpcg.io/5axMxUQb
___
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/VPAV3T2XN4DARL5RNBNCYFP5IP4BGYAX/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Exact decimal multiplication and division operations

2020-10-09 Thread Marco Sulla
On Thu, 8 Oct 2020 at 22:10, Tim Peters  wrote:
> Again, the concept of a _fixed_ (albeit user-settable) working
> precision is deep in the module's bones.

That is, for what I know, how also BigDecimal in Java works... and
float in any architecture.
___
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/N77ZZEV7QUP3DNATAKSAAUVD3KSVZKLZ/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: CPP Namespaces For Python

2020-10-06 Thread Marco Sulla
On Tue, 6 Oct 2020 at 15:33, Alperen Keleş  wrote:
> Cars have different states, MovingCar, IdleCar, ParkingCar...

Well, IMHO the solution is quite more simple:

class Car:
def __init__(self):
self.state = "parking"

def move(self):
if self.state != "moving":
raise StateException("car can't move!")

[...]
___
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/5P2WVGDX6HFUY2JMKJZ7OQP5YGJ5F6PL/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Method to efficiently advance iterators for sequences that support random access

2020-10-06 Thread Marco Sulla
What I do not understand is why you need to use the iterator instead
of using the iterable itself. This way you can jump to whatever
position without slicing.
___
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/QZ5QL3HNNDJYV67RPVATVDRD5R63AOUM/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Fwd: Re: Experimenting with dict performance, and an immutable dict

2020-10-06 Thread Marco Sulla
In the meanwhile, I updated the code of frozendict to the new 3.10
code. And here I need some help.

As you can see by the new benchs:
https://github.com/Marco-Sulla/cpython/blob/frozendict/frozendict/test/bench.txt

creation of frozendict is not faster anymore. This is because Inada
introduced memcpy to do a fast init of a dict from a "good" dict.

I tried to copy the code for frozendict, but I get a good old memory
corruption. I passed several hours to understand what I'm doing wrong,
without success.

The code is the one commented out at lines 1084 and 2978, starting
with a "it does not work" comment...
https://github.com/Marco-Sulla/cpython/blob/frozendict/Objects/dictobject.c
___
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/CJZMIAHIBY4D2MOAAKPCHTKJDCGFOGF6/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Method to efficiently advance iterators for sequences that support random access

2020-10-05 Thread Marco Sulla
You can use slice:

new_iterator = iterator[50001:]
it2 = iter(new_iterator)

or range:

for i in range(50001, len(iterator)):
x = iterator[i]
___
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/KP6DAY4YQVA6LOX2XCRMIAEK5W3JZVG5/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: A new suggestion for Python

2020-09-30 Thread Marco Sulla
On Wed, 30 Sep 2020 at 20:02, Steven D'Aprano  wrote:
> There's also the factor that the dot operator is not very visually
> distinctive.

I completely agree.
___
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/KZ73L7H7247DXXCGM5QCTOK4BKTEWEED/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Regular Expression | re - Support String List Input Type List[str]

2020-09-29 Thread Marco Sulla
On Tue, 29 Sep 2020 at 04:39, <2qdxy4rzwzuui...@potatochowder.com> wrote:
>
> On 2020-09-28 at 23:10:24 -,
> Giang Le  wrote:
>
> > I would like to propose an idea for the regular expression module
> > re.search(Pattern, Input) to support List[str] input type.
>
> > So it will return a matched object list if the input is a string
> > list. Otherwise, it will return a normal matched object, if the input
> > is a normal string
>
> How would that change be better than a new function:
>
> def regex_search_list(regex, pattern, list_of_inputs):
> [regex.search(pattern, input) for input in list_of_inputs]
>
> (or possibly an equivalent method of regexen)?

I suppose that another advantage is that the list is passed to C, so
it's faster.
Not sure such an overloading is desirable.
___
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/LDYHQEKW776JISFA6FNBGETVUCUW2KQT/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Trash bin

2020-09-25 Thread Marco Sulla
That I hope it's not the place where this proposal will be sent.

My idea is apparently simple: what if, anytime we create an object,
instead of deleting it, we send it in a trash bin? If the object is,
for some reason, recreated, we can take it from the trash bin. If
there is no more memory, the trash bin will be empty.

The problem is that, probably, args and kwargs used by object creation
must be stored. Maybe also copied? This could slow down the object
creation instead of speed it up? Could this be done by a separate
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/AI5ALV7D64DJCVSJPARZQEI2G6BPKTX6/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Suggestion: annotated exceptions

2020-09-25 Thread Marco Sulla
On Fri, 25 Sep 2020 at 14:44, Samuel Colvin  wrote:
>
> Sorry I probably wasn't clear enough in what I was suggesting.
>
>>
>> The main question here is why using a hint or a decorator should be
>> better than a simple documentation.
>
>
> For the same reason type hints are better than documentation

Type hints help an IDE to check if you're potentially passing a bad
parameter to your function.
What does an "exception hint" will do in an IDE? Alerts you that you
are not catching that exception, and, if you really want it to bubble
up, silencing that warning? Again, no thanks :-)
___
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/CFVT7ICR7A5C5AFT2U4JEO2XSCMEHS2M/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Suggestion: annotated exceptions

2020-09-25 Thread Marco Sulla
On Fri, 25 Sep 2020 at 11:58, Samuel Colvin  wrote:
> I first found myself wanting this when I came back to python
> having been writing rust. The Result type in rust is somewhat
> similar to what's being suggested here. See
> https://doc.rust-lang.org/std/result/

I do not know Rust and I'm not sure I understood 100% the code. But,
if I'm not wrong, Rust does not use try-catch, but pattern matching.
It seems to me that Rust has a good exception handling system, but
Python does not (yet) have pattern matching.

The main question here is why using a hint or a decorator should be
better than a simple documentation.

If the goal is to force people to manage the exception, no, thanks. As
Serhiy Storchaka already said, it was historically proven as bad. And
even if I've not a great knowledge in Java (3 years), I can assure you
checked exceptions are a really bad idea.
___
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/BRU5WUAFQWMKIJ5I6GWGKTRPSYAGTLRS/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Naming Accepted PEPs as PAPs

2020-09-22 Thread Marco Sulla
For me, the only good thing to differentiate between accepted and
rejected PEPs is to get a look at the old rejected ones, to see if
there's some ideas I can stea... take inspiration. But
https://www.python.org/dev/peps/ already does it.

PS: PAP remembers me the PAP test.
___
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/FUESTX7GSBUP6VVZXRECKAF5L7UD4MUM/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Fwd: Re: Experimenting with dict performance, and an immutable dict

2020-09-21 Thread Marco Sulla
I've done a PR:
https://github.com/python/cpython/pull/22346

As you can see, changes are not dramatical, if you improve only kw
creation. Furthermore, IMHO insert_to_emptydict() can be removed,
since it speeds up the insertion of the first element, but slows down
all the others. I do something similar in insertdict_init, but in
"bulk mode".

On Thu, 17 Sep 2020 at 16:49, Marco Sulla  wrote:
>
> On Thu, 17 Sep 2020 at 05:31, Inada Naoki  wrote:
> >
> > On Thu, Sep 17, 2020 at 8:03 AM Marco Sulla
> >  wrote:
> > >
> > > python -m timeit -n 2000  --setup "from uuid import uuid4 ; o =
> > > {str(uuid4()).replace('-', '') : str(uuid4()).replace('-', '') for i
> > > in range(1)}" "dict(**o)"
> > >
> >
> > I don't think this use case is worth to optimize, because `dict(o)` or
> > `o.copy()` is Pythonic.
>
> Well, also {**dict1, **dict2} is pythonic. Anyway, I used **dict as a
> shortcut for testing keyword assignment.
> For doing this I "only" cloned PyDict_SetItem and insertdict.
>
> I do not like code duplication, but dictobject.c has already a lot of
> duplicated copies of the same function for optimization (see
> lookdict).
___
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/SPLALWGOKFSKXJL6ZA5LPY3TZXD6JQQH/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Fwd: Re: Experimenting with dict performance, and an immutable dict

2020-09-17 Thread Marco Sulla
On Thu, 17 Sep 2020 at 05:31, Inada Naoki  wrote:
>
> On Thu, Sep 17, 2020 at 8:03 AM Marco Sulla
>  wrote:
> >
> > python -m timeit -n 2000  --setup "from uuid import uuid4 ; o =
> > {str(uuid4()).replace('-', '') : str(uuid4()).replace('-', '') for i
> > in range(1)}" "dict(**o)"
> >
>
> I don't think this use case is worth to optimize, because `dict(o)` or
> `o.copy()` is Pythonic.

Well, also {**dict1, **dict2} is pythonic. Anyway, I used **dict as a
shortcut for testing keyword assignment.
For doing this I "only" cloned PyDict_SetItem and insertdict.

I do not like code duplication, but dictobject.c has already a lot of
duplicated copies of the same function for optimization (see
lookdict).
___
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/IID5E4TYV2ZLAXWSCQL6YJWZN4XO54BA/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: f-strings as assignment targets

2020-09-17 Thread Marco Sulla
It seems that the variables come out magically.

What about something like:

a, b = "hello world".extract("{} {}")

PS: I do not like extract, it's the first name that comes in my mind
___
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/MLU5AWDXKOFWACLGJZHJL6TDFHVLDEH5/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Fwd: Re: Experimenting with dict performance, and an immutable dict

2020-09-16 Thread Marco Sulla
Well, it seems ok now:
https://github.com/python/cpython/compare/master...Marco-Sulla:master

I've done a quick speed test and speedup is quite high for a creation
using keywods or a dict with "holes": about 30%:

python -m timeit -n 2000  --setup "from uuid import uuid4 ; o =
{str(uuid4()).replace('-', '') : str(uuid4()).replace('-', '') for i
in range(1)}" "dict(**o)"

python -m timeit -n 1  --setup "from uuid import uuid4 ; o =
{str(uuid4()).replace('-', '') : str(uuid4()).replace('-', '') for i
in range(1)} ; it = iter(o) ; key0 = next(it) ; o.pop(key0)"
"dict(o)"

Can I do a PR?
___
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/QWXD2D4SC6XHZLV3QA4TMGMI7Z7SAJ2R/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Fwd: Re: Experimenting with dict performance, and an immutable dict

2020-09-16 Thread Marco Sulla
Well, I simply forgot vectorcall.

I've done a test and it seems the speedup is about 25%.
Unluckily, now I abandoned definitively the hope of a big big resize,
but it seems I don't resize correctly, since now I have again an
assert error on dk_usable.

What is the difference between dk_usable and USABLE_FRACTION?
___
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/W6PWONGCMIMFILYWLLT75OVR673O33GB/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: A shortcut to load a JSON file into a dict : json.loadf

2020-09-15 Thread Marco Sulla
On Tue, 15 Sep 2020 at 18:10, Wes Turner  wrote:
>
> json.loads and json.dumps exist only because there was no way to distinguish 
> between a string containing JSON and a file path string.
> (They probably should've been .loadstr and .dumpstr, but it's too late for 
> that now)

Well, if you see the code of msutils.jsonLoad I linked before, it does
a simple try. Not very elegant, but effective.
___
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/2LSXRUEBP2MEE7M3EHCHIGGI2NZ3NROL/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Fwd: Re: Experimenting with dict performance, and an immutable dict

2020-09-15 Thread Marco Sulla
On Tue, 15 Sep 2020 at 09:22, Inada Naoki  wrote:
>
> On Tue, Sep 15, 2020 at 5:08 AM Marco Sulla
>  wrote:
> >
> > 1. How can we check the size of an object only if it's an iterable
> > using the Python C API?
>
> There is no good way. Additionally, we need to know distinct count if
> we want to preallocate hash table.
> For example, `len(dict(["foo"]*1000))` is 1, not 1000.
> [...]
> We have "one big resize" logic in dict_merge already.
> And I use dummy empty dictkeys for new empty dict.
> So we don't allocate any temporary, intermediate dictkey object.

Well, yes, but only for the first positional argument and if it's a map.
I would be able to resize to the maximum possible size, that is
len(arg) + len(kwarg). Of course the size can be overestimated, but I
suppose the overlaps are very rare and small.

The problem is that if I do this resize in dict_new, when the compilation does

python -E -S -m sysconfig --generate-posix-vars

a segfault happens. If I reintroduce the temporary dummy keys, it works.
___
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/AK35PRDPACXMHQ443CPB7GTYZXHTUQYV/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Custom keywords (from: Decorators for class non function properties)

2020-09-14 Thread Marco Sulla
Little errata: change

Cython, for example, uses its parser to compile Python code

to

Cython, for example, uses its parser to compile Cython code
___
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/5JOT2U52QBWMUQQEPQB42ABARNLNVYNH/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Custom keywords (from: Decorators for class non function properties)

2020-09-14 Thread Marco Sulla
On Sun, 23 Aug 2020 at 02:42, Guido van Rossum  wrote:
> IMO this is just too large of a step to expect either redradrist or Marco 
> Sulla to take.

You're quite right, I have proposed it to have an opinion by experts.
Indeed I have no knowledge about how a parser works. This is why I
asked if this is possible and desirable.
Anyway, I was not thinking about a macro system, but about custom
keywords. Maybe a macro is more simple, but was not my idea.
My idea is to be able to delegate the parsing of a piece of code to a
custom parser, defined in a third-party module.

Cython, for example, uses its parser to compile Python code to C code.
Cython uses custom keywords like cdef to speed up the code introducing
C static typing.

My idea and question is if it's possible to have something like this:


from cython import *

@int a = 0


About Jython and other implementations, as far as I know they can't
use C extensions. Not without many troubles.
___
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/75N4W2NQMC3LZOPY5TU7357D5YYZO3D3/
Code of Conduct: http://python.org/psf/codeofconduct/


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

2020-09-14 Thread Marco Sulla
I just had an idea: we have mp->ma_used and keys->dk_nentries.

holes = mp->ma_keys->dk_nentries - mp->ma_used

is the number of holes in the dict.
So, if holes == 0, you have O(1). But, if the dict has holes, you
can't have O(1), but you can speedup the check of the entry by
counting the NULLs in the for loop. When the number of NULLs reaches
holes, you can jump safely to the dict entry you need.

In this case, O(n) can happen only if the items are removed from the
end, as with popitem().
___
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/G4VO262RMJALR35G74B72PFCTXT524LQ/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: A shortcut to load a JSON file into a dict : json.loadf

2020-09-14 Thread Marco Sulla
Well, I didn't read the entire discussion... but I wrote in
unsuspicious times a stupid little module, msutils, with two stupid
little functions, jsonLoad and jsonDump:

https://github.com/Marco-Sulla/msutils/blob/master/msutils/jsonutil.py#L20
___
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/SDILJ67J5HF5P5SLR4WXYXGQGI2ACCUS/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Fwd: Re: Experimenting with dict performance, and an immutable dict

2020-09-14 Thread Marco Sulla
@Inada (senpai?):

In the meanwhile I tried to apply some tricks to speedup the dict
creation without success.
This is my effort:
https://github.com/Marco-Sulla/cpython/blob/master/Objects/dictobject.c

As you can see, I simply "cloned" some functions that create dict, and
removed some checks that are not needed at creation:

1. the "override" flag
2. the check for resizing, since in theory I would do one big resize
at start (but I have some problems, see below)
3. the introduction of an "empty" parameter, that is false only if you
have to insert key-value pairs AND you also have a positional
parameter
4. some random const, when the code permits it
5. changing of ma_used, ma_version_tag, dk_usable, dk_nentries only
one time, after the for loop that insert all the items
6. some little shortcut (for example, PyDictKeysObject *keys =
mp->ma_keys; and using keys in the rest of the code)
7. changing some checks in asserts
8. calling directly the internal static functions

I tested it with a dict with an "hole", since I noticed that you
already improved the creation of dict when the source it's another
dict and it's "perfect".
The speedup is a mere 2.5%...

Furthermore, I failed to do one big resize with dict. I was ok with
frozendict because I always used dicts or seq2 as parameters. But
defaultdict passes a class as first argument. So PyObject_Length fails
miserably, and I had to remove the resize. Anyway, I didn't
reintroduce the resize, so in theory the code should be faster (even
if one odict test fails, of course).

Some questions:

1. How can we check the size of an object only if it's an iterable
using the Python C API?
2. Why, in your opinion, no relevant speedup was done?

PS: My next and final try is to improve the speed of lookups, the only
other field where frozendict is faster. I hope to have better luck
here.
___
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/CVJWH4CFUA3RDKDPY74LQXFKN6NZS4B5/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Custom keywords (from: Decorators for class non function properties)

2020-08-17 Thread Marco Sulla
On Mon, 17 Aug 2020 at 22:10, Guido van Rossum  wrote:
>
> Please try to learn how to write a good proposal. A few examples, like you 
> did here, just isn't enough. It's worth doing right, and it's worth 
> *learning* how to do it right.

Well, I'll try.

In pure python, you can change everything. It seems that the only
things you can't really modify are the keywords. And this is for
obvious reasons, since if you change the keywords, the code will be
absolutely unreadable.

We could add a way to define new keywords using a third-party module;
but what if the keyword is already presented as variable, class,
function etc. name?

So my idea is that these custom keywords must start with a
non-alphanumeric character (and underscore). For now, I'll call this
char "@"

For example, many py programmers desider to make their objects a
constant. This could be done in the example I wrote before:

from mykeywords import @const
@const a = 1

The new `@const` will be added as a hook to the PEG parser. if the PEG
parser finds a `@const`, it will invoke the miniparser of `mykeywords`
module inherent to `@const`. In this case, it will simply transform
`@const a = 1` in `const PyObject* a = PyLong_FromSsize_t((Py_ssize_t)
1)`

A problem is that, since `a` now is const, it can't be reassigned. I'm
not an expert about parsers, so I ask for info.
___
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/UI65FU6A246H3ZOWWAOFZOVSTMQBHRXE/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Custom keywords (from: Decorators for class non function properties)

2020-08-17 Thread Marco Sulla
On Mon, 17 Aug 2020 at 11:17, Steven D'Aprano  wrote:
> Did you have a specific question to be answered or are you just hoping
> to get people interested in the proposal?

It seems I have to repost the proposal I already wrote...

My proposal is to add a way for third party modules to add custom
keywords. Example:

from mykeywords import @const
@const a = 1

Notice that I choose "@" but I hope another non c-alphanumeric
character will be chosen.

On Mon, 17 Aug 2020 at 12:46, Chris Angelico  wrote:
> A proposal that requires syntactic changes [...]

No syntactic changes are needed. Custom keywords *must* be prefixed
with a non-alphanumeric character (@ for example).


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


[Python-ideas] Re: Custom keywords (from: Decorators for class non function properties)

2020-08-17 Thread Marco Sulla
Well, "up" it's used in forums to draw attention :)
___
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/CLW5R62WLGTJKWMS2HYSA5Y6WTSRAUE4/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Custom keywords (from: Decorators for class non function properties)

2020-08-16 Thread Marco Sulla
...up?

On Thu, 6 Aug 2020 at 04:41, Marco Sulla  wrote:
> Think if you're able to do:
>
>
> from aenum import const
> @const a = 5
>
>
> Notice that I'm using the "at" char because I can't find a better character.
>
> I give the rest to your imagination:
>
>
> # previous declarations of x in the same scope are a SyntaxError
> @var x = None
>
> # simulate electrical circuit
> c = a @nand b
>
> # Java style
> @final class A:
> @protected _x = 0
>
> # ...maybe someday
> @int a = 1
___
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/NEWIZGCZFUJ23FYNX54PEPP3LCDGK7JB/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Tracebacks for C code in Python

2020-08-16 Thread Marco Sulla
You probably wants also the python extension for gdb, to go faster to
the interesting code:
https://stackoverflow.com/q/41160447/1763602
___
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/IT7PX2W7XL2CSB55OOIQ7N6FTIQC6VTO/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Syntactic sugar for checking if a variable is an iterable but not a string?

2020-08-15 Thread Marco Sulla
On Fri, 14 Aug 2020 at 11:10, Chris Angelico  wrote:
> IMO the collections module isn't really right for something like that,
> but if you really need a helper function for this one-liner, then I'd
> say having one in your own code is perfectly reasonable.

In my experience I used it a lot, and I saw many devs online that
implemented their solution or asked about it. Not sure if collections
module is appropriate, but IMO an helper function will be very useful.

@Steven: I continue the discussion with your objections in private.
___
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/EH4QKKZGVVVJJT2HETVCSLCXNJRJEEF2/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: basic matrix object

2020-08-14 Thread Marco Sulla
Excuse me, I sent this message from the wrong email address:

On Fri, 14 Aug 2020 at 09:24, Steven D'Aprano  wrote:
> Tensors generalise matrices to an arbitrary number of
> dimensions, 3D and above.
>
> https://medium.com/@quantumsteinke/whats-the-difference-between-a-matrix-and-a-tensor-4505fbdc576c
>
> I have never studied tensors and wouldn't know where to even begin a
> tensor library :-)

Another big problem with tensors is the covariance and contravariance.
Usually in informatic you denote the covariance with the subscript
operator, that on paper is written as subscript. Contravariant index
on the contrary is a superscript. Not sure how a programming language
can easily represent it. Maybe PEP 472 can help here?
___
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/O3PEI2IGWIO37FD35JXGI3Z4HKEC4Z5T/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: basic matrix object

2020-08-14 Thread Marco Sulla
LaTeX for example uses the underscore for subscripts and the ^ for the
superscript. This is not acceptable, since  Python (and almost all
languages) uses [] for subscript, and ^ in Python is the XOR logical
operator.
___
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/JQRHVSFZTEHQHFTEILKV5DJUGMK4OI4I/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Syntactic sugar for checking if a variable is an iterable but not a string?

2020-08-14 Thread Marco Sulla
Many times I want a function parameter that is an iterable but not a
string. Usually I do:

try:
var.__iter__
except AttributeError:
# not an iterable
else:
try:
var.isascii
except AttributeError:
# put yuour code here


or


from collections.abc import Iterable

if isinstance(var, Iterable) and not isinstance(var, str):
# put yuour code here


The first example uses duck typing but it's more verbose. I use the
first method in an home-made utility function.

I think it could be interesting to add a syntactic sugar to do this. Maybe a
collections.notTextIterable()
collections.nonTextIterable()
collections.notStrIterable()
collections.iterableNotStr()
?
___
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/P74I55RJX4S2FUEFI6PIIKVP64F2NQJR/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: basic matrix object

2020-08-13 Thread Marco Sulla
On Fri, 14 Aug 2020 at 03:16, Steven D'Aprano  wrote:
> Funny you mention this, I have been working on a Matrix object for
> precisely the use-case you discuss (secondary school maths), where
> performance is not critical and the dimensions of the matrix is
> typically single digits.

This is really good. I think that numpy is a practical project, but I
feel it hard to understand for people that come from Python and not
from Matlab.
Maybe the API for such a module can be released initially as a
provisional API, as at the pathlib and asyncio beginnings?
___
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/3AVVWCTRD675SM3HF755RMLQLO5INXTX/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Package kwkey and PEP 472 -- Support for indexing with keyword arguments

2020-08-07 Thread Marco Sulla
On Fri, 7 Aug 2020 at 14:14, Jonathan Fine  wrote:
> At present
> d[1, 2]
> d[(1, 2)]
> are semantically equivalent.
>
> There is a proposal, that
> d[1, 2, a=3, b=4]
> d[(1, 2), a=3, b=4]
> be semantically equivalent.
>
> I find this troubling, for example because
>fn(1, 2, a=3, b=4)
>fn((1, 2), a=3, b=4)
> are semantically different.

I think I've understood your point. To be sure, what's the Steven proposal?
___
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/QDYCO5FOS26YYUTX4RV4LTPBAMCW6C3P/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Propouse add context to json module.

2020-08-07 Thread Marco Sulla
What about __json__()?
___
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/555MJQ4I5VTPFHZ5AJMG7VMUCCV52HS2/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Inline Try-Except Clause

2020-08-07 Thread Marco Sulla
On Thu, 6 Aug 2020 at 14:57, Jonathan Grant
 wrote:
> Instead of writing this:
>
> try:
> return my_dict[“a”][“b”][“c”][“d”]
> except:
> return “some default”
>
> [...]
>
> I propose we allow for an inline exception handler, like `eor`:
>
> return my_dict[“a”][“b”][“c”][“d”] eor “some default”

For this behaviour, you can use the kwkey module:
https://pypi.org/project/kwkey/

About PEP 463, the examples in the PEP seem to me less readable. Not
sure where the advantage is.
___
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/4DQHYP5VKMDNXMAQOBWEQ4XC7JJ65GIK/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Custom keywords (from: Decorators for class non function properties)

2020-08-05 Thread Marco Sulla
On Wed, 5 Aug 2020 at 20:10, Ethan Furman  wrote:
>  --> from aenum import Constant
>
>  --> class K(Constant):
>  ...   a = 5
>  ...

This is exactly what I intended. Think if you're able to do:


from aenum import const
@const a = 5


Notice that I'm using the "at" char because I can't find a better character.

I give the rest to your imagination:


# previous declarations of x in the same scope are a SyntaxError
@var x = None

# simulate electrical circuit
c = a @nand b

# Java style
@final class A:
@protected _x = 0

# ...maybe someday
@int a = 1
___
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/KRJSKUZQF5JWK7QA2TNLFQTONUS6FC5R/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Decorators for class non function properties

2020-08-05 Thread Marco Sulla
On Wed, 5 Aug 2020 at 15:53, Ricky Teachey  wrote:
> How about this?
>
> @print(sep="\n")
> 1, 2, 3

If you consider @print as a function decorator, it's quite a problem.
But I was thinking mainly about a sort of "decorator" for the parser
and/or the AST compiler.
This could allow you to have, for example:

from mypython import *
@const a = 5

with a C extension.
More than an idea is a question, since I've no knowledge about AST and
code parsers.
___
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/GVB3GZOY3VYLOFXJ2WSPA5AUTJSQUYII/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Decorators for class non function properties

2020-08-05 Thread Marco Sulla
On Wed, 5 Aug 2020 at 13:29, Dominik Vilsmeier  wrote:
>
> On 05.08.20 12:40, Greg Ewing wrote:
> > A considerable number of moons ago, I suggested that
> >
> > @my_property
> > fred = 42
> >
> > should expand to
> >
> > fred = my_property("fred", 42)
> >
> > The point being to give the descriptor access to the name of
> > the attribute, without having to repeat yourself.
> >
> That should be possible by doing `fred = my_property(42)` and defining
> `__set_name__` on the `my_property` class.

I suppose that what Greg Ewing suggests is a way to define a sort of
custom simple statement.

For example, instead of the old
print "Hello"

and the "new"
print("Hello")

you could write

@print
"Hello"
___
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/4H2JPEDJBAJD7CUPITAI7GECJ5XMUN6X/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: How to prevent shared memory from being corrupted ?

2020-08-04 Thread Marco Sulla
I forgot that there's also Ray:
https://github.com/ray-project/ray

Ray uses Apache Arrow (and Plasma) under the hood. It seems Plasma was
originally developed by Ray team.

Don't know how they solve the GC problem. Maybe they disable it.
___
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/HEPNSXWOFIA6NJ2M2E5NP6MT3F2ZEVIX/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: How to prevent shared memory from being corrupted ?

2020-08-02 Thread Marco Sulla
There's also the possibility to use shared ctypes:
https://docs.python.org/3/library/multiprocessing.html#shared-ctypes-objects

Operations like += which involve a read and write are not atomic. So if,
> for instance, you want to atomically increment a shared value it is
> insufficient to just do
>
> counter.value += 1
>
> Assuming the associated lock is recursive (which it is by default)
> you can instead do
> with counter.get_lock():
> counter.value += 1
>
> Notice that they use a lock anyway. Maybe the solution of Wes Turner is 
> better. See also RLock:
https://docs.python.org/3/library/multiprocessing.html#multiprocessing.RLock

On Sat, 1 Aug 2020 at 22:42, Eric V. Smith  wrote:

> While they're immutable at the Python level, strings (and all other
> objects) are mutated at the C level, due to reference count updates. You
> need to consider this if you're sharing objects without locking or other
> synchronization.
>
>
This is interesting. What if you want to have a language that uses only
immutable objects and garbage collection? Could smart pointers address this
problem?
___
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/QVQWSNWJMMMRVFA6BTXDNJHIGAUBPWVX/
Code of Conduct: http://python.org/psf/codeofconduct/


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

2020-08-02 Thread Marco Sulla
On Sat, 1 Aug 2020 at 20:25, Stestagg  wrote:

> I wrote some (better than the previously shared) benchmarks for this
> change a while ago.
>

I think that you could speed up the algorithm if you check if
dict->ma_keys->dk_lookup
== lookdict_unicode_nodummy. If so, the dict is a combined dict with only
string keys (quite common), and no deletion was done before, so there's no
hole in ma_keys.
___
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/ZP3QVG4ZPALST5OXEVKXNHGCRC57J34G/
Code of Conduct: http://python.org/psf/codeofconduct/


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

2020-08-02 Thread Marco Sulla
On Sat, 1 Aug 2020 at 19:34, Christopher Barker  wrote:

> I would think the goal here would be to re-order once in a while to remove
> the holes. But that would take time, of course, so you wouldn't want to do
> it on every deletion. But when?
>

You should have a separate process that does this, so "normal" operations
will be not blocked. The process could also cache objects and do garbage
collection. A sort of domestic worker. Don't know if this is possible.


> > About the hole, I was thinking that in theory the problem can be
> circumvented using a modified version of lookdict.
>>
>> lookdict searches for a key and returns its position in the ma_keys
>> array. I suppose it's possible to do the contrary: search for the index and
>> return the key.
>> What do you think (theoretically speaking)?
>>
>
> but isn't searching for the index going to require iterating through the
> array until you find it? i.e. that O(N) operation we're trying to avoid?
>

O(n) is an+b. If a and b are small, the algorithm is fast for not-so-big n.

To sum up the alternatives:
1.  rearrange the items array at positional lookup. As Inada said, it will
mutate the dict while reading, that is quite unexpected
2. rearrange the items array at deletion. It will slow down deletion.
Probably not an option... a NAO
3. no positional indexing. It seems the more simple solution. A number of
alternatives are offered (by Tim Peters too)
4. something else.
___
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/VPMKJEP4A4SN6QTYWVZRR36AIDTOLSPN/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: How to prevent shared memory from being corrupted ?

2020-08-01 Thread Marco Sulla
You don't need locks with immutable objects. Since they're immutable, any
operation that usually will mutate the object, generate another immutable
instead. The most common example is str: the sum of two strings in Python
(and in many other languages) produces a new string.

This is usually slower than modifying a mutable object (as atomic types),
but they allow you to remove the bottleneck of a lock.

See also immutables.Map: https://github.com/MagicStack/immutables
___
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/HC5MA4SHEYLLQ7X5KL7C7QWMKKJZPAVB/
Code of Conduct: http://python.org/psf/codeofconduct/


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

2020-08-01 Thread Marco Sulla
On Sat, 1 Aug 2020 at 03:00, Inada Naoki  wrote:

> Please teach me if you know any algorithm which has no hole, O(1)
> deletion, preserving insertion order, and efficient and fast as array.
>

:)

About the hole, I was thinking that in theory the problem can be
circumvented using a modified version of lookdict.
lookdict searches for a key and returns its position in the ma_keys array.
I suppose it's possible to do the contrary: search for the index and return
the key.
What do you think (theoretically speaking)?
___
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/57BDPRYVMMALKERYPRJMQO4AH33FWOV4/
Code of Conduct: http://python.org/psf/codeofconduct/


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

2020-07-31 Thread Marco Sulla
Ok... I wrong. The array of items have a NULL key and value if one item is
deleted. Sorry for the confusion.

On Sat, 1 Aug 2020 at 01:09, Guido van Rossum  wrote:

> the key and items views already implement the Set ABC, and I'd rather
> refrain from having them *also* implement the Sequence ABC.
>

I'm not pro or against the proposal, but maybe count it's not useful at all
for keys and items. Furthermore, dict implements __reversed__, that is not
a Mapping method, but it's a Sequence method.

I think that it's useful to implement methods of other APIs without
changing their name or implement the full other API... if it's useful ^^
But maybe I'm missing something in the general picture.
___
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/LXPWDPYTFT3SP2XFHG2CXNSTHACSLFVA/
Code of Conduct: http://python.org/psf/codeofconduct/


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

2020-07-31 Thread Marco Sulla
On Sat, 1 Aug 2020 at 02:30, Stestagg  wrote:

> The dict keys is compact only *until* you delete an item, at which point,
> a hole is left in the array
>

No, the array of items has no hole. The hole is inserted in the hashtable.
___
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/YNL6HGGXI7HVEX7QW2YM7WCEVGXNLKSL/
Code of Conduct: http://python.org/psf/codeofconduct/


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

2020-07-31 Thread Marco Sulla
On Fri, 31 Jul 2020 at 22:14, Chris Angelico  wrote:

>  So, constructing a tuple or list from the keys or items WILL give you a
> sequence.
>

Yes. Since now dicts are ordered by insertion, also keys, values and items
are ordered the same way.

It seems to me more simple to add some sequence methods to dict views, like
subscript, slicing and index(), instead of creating other 3 methods and 3
data types.

What I have not understood well is when you need to index a dict by
position or slice it.
___
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/YMXPFPXWOJXZXIYXC6ZX3KQQ2YFTERBD/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: How to prevent shared memory from being corrupted ?

2020-07-31 Thread Marco Sulla
On Thu, 30 Jul 2020 at 12:57, Vinay Sharma via Python-ideas <
python-ideas@python.org> wrote:

> Python has support for atomic types, I guess:
> Atomic Int:
> https://github.com/python/cpython/blob/master/Include/internal/pycore_atomic.h#L80
> Atomic Store:
> https://github.com/python/cpython/blob/master/Include/internal/pycore_atomic.h#L94
>
>

You could also use immutables:
https://nextjournal.com/schmudde/adventures-in-immutable-python
___
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/URLHX7IEK6NRCUCN3K647JTDCIRK5ZAT/
Code of Conduct: http://python.org/psf/codeofconduct/


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

2020-07-31 Thread Marco Sulla
Is it not more simple to add some sequence methods to the dict views (if
there's a real need)?
If you do tuple(dict.keys()), you get the sequence of keys in the same
insertion order of the dict. It seems to me that the duck already exists
and quacks.
___
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/MFS55TWPHYTKNNJMYZACAEKOEQ2XALUE/
Code of Conduct: http://python.org/psf/codeofconduct/


  1   2   >