[Python-ideas] Re: Add a decorators called @staticproperty

2021-12-18 Thread Chen Jintao
Thank everyone! 
I just want to use a class that holds global variables and has a value that can 
be easily calculated. So I thought it would be more elegant to use 
"@staticproperty".
These is just my personal thought and not necessary. Now you've given me the 
solution.
___
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/A22ZY2SRYQ4ED47LIJ7KLL2O3QV45WVD/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Add a decorators called @staticproperty

2021-12-18 Thread Chris Angelico
On Sun, Dec 19, 2021 at 8:10 AM Ethan Furman  wrote:
>
> By way of correcting misconceptions:
>
> On 12/18/21 8:39 AM, Chris Angelico wrote:
>  >
>  > I'm not sure that this is actually possible the way you're doing it.
>  > The descriptor protocol (which is what makes properties work) won't
>  > apply when you're looking up statically.
>
> On 12/18/21 9:19 AM, Christopher Barker wrote:
>  >
>  > Anyway, the thing is that both staticmethod and property are implimented 
> using descriptors, which I think can only be
>  > invoked by instance attribute lookup. That is, the class attribute IS a 
> descriptor instance.
>
> While it is true that a descriptor does not get the chance to run its 
> `__set__` and `__delete__` methods when called on
> the class, it does get to run its `__get__` method.

Ah, thanks for clarifying that.

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


[Python-ideas] Re: Add a decorators called @staticproperty

2021-12-18 Thread Ethan Furman

By way of correcting misconceptions:

On 12/18/21 8:39 AM, Chris Angelico wrote:
>
> I'm not sure that this is actually possible the way you're doing it.
> The descriptor protocol (which is what makes properties work) won't
> apply when you're looking up statically.

On 12/18/21 9:19 AM, Christopher Barker wrote:
>
> Anyway, the thing is that both staticmethod and property are implimented 
using descriptors, which I think can only be
> invoked by instance attribute lookup. That is, the class attribute IS a 
descriptor instance.

While it is true that a descriptor does not get the chance to run its `__set__` and `__delete__` methods when called on 
the class, it does get to run its `__get__` method.  In code:


class descriptor_example:
#
def __get__(self, instance, owner=None):
if owner is not None:
print('called directly from the class, have a cookie!')
return 'chocolate-chip cookie'
#
def __set__(self, instance, value):
# never called from the class
raise TypeError
#
def __delete__(self, instance, value):
# never called from the class
raise TypeError

class Test:
huh = descriptor_example()

>>> Test.huh
called directly from the class, have a cookie!
chocolate-chip cookie

>>> Test.huh = 7 # change `huh`
>>> Test.huh
7

>>> Test.huh = descriptor_example()  # put it back
>>> Test.huh
called directly from the class, have a cookie!
chocolate-chip cookie

>>> del Test.huh # remove huh
>>> Test.huh
AttributeError

Having said that, I don't think it's needs direct support in the stdlib.

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


[Python-ideas] Re: set arbitrary hash random seed to ensure reproducible results

2021-12-18 Thread Hao Hu

On 12/18/21 08:44, Stephen J. Turnbull wrote:


Hao Hu writes:
  > > On 17 Dec 2021, at 15:28, Chris Angelico  wrote:

  > > The built-in hash() function is extremely generic, so it can't really
  > > work that way. Adding a parameter to it would require (a) adding the
  > > parameter to every __hash__ method of every object, including
  > > user-defined objects;
  >
  > I would not say the opposite, however maybe it appears to be more
  > complicated than it is really is. Probably it is worth a small
  > analysis?

It's the user-defined objects that are the killer here.  We don't want
to go wrecking dozens of projects' objects.

  > >> For instance, if we create a caching programming interface that
  > >> relies on a distributed kv store,

I would be very suspicious of using Python's hash builtin for such a
purpose.  The Python hash functions are very carefully tuned for high
performance in one application only: equality testing in Python,
especially for dicts.  Many __hash__ methods omit much of the object
being hashed; if the variation in your keys depends only on those
attributes, you'll get a lot of collisions.  Others are extremely
predictable.  E.g., most integers and other numbers equal to integers
hash to themselves mod 2**61 - 1, I believe -1 is only exception.
Being predictable as such may not be a problem for your kv store
cache, but predictable == pattern, and if your application happens to
match that pattern, you could again end up with a massive collision
problem.  I imagine this is much less likely to be a problem than the
case where keys depend on omitted attributes, since presumably the
__hash__ method is designed to cover the whole range.  And numbers are
the only case I know of offhand.


It is pretty much the same use case as python's dictionary though, the 
goal is just to generalize it to use with a distributed kv store. 
Another big advantage is that it is more user friendly to apply *hash* 
directly on a type.




  > > I'd recommend hashlib:

+1

  > Otherwise, would that be useful to add siphash24 or fnv in the
  > hashlib as well?

I think that is a good idea.  To me, it seems relatively likely to be
accepted quickly.  However, many cryptographic algorithms are delicate
(eg, to avoid timing attacks), so I could be wrong about that.  Folks
like Christian Heimes might be very concerned about the implementation
as well as the algorithm.

Note that Python/pyhash.c seems to have implementations of both of
these algorithms, although I don't know if these implementations
satisfy cryptographic needs.


According to the doc, there seems to be 2 categories of hash function. 
One is for cryptographic purpose, another one is for message 
authentication code.


The algorithms mentioned above could be mostly put into the second category.


Steve


smime.p7s
Description: S/MIME Cryptographic Signature
___
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/FBYX6MPTZGQUPQICYGYOPMLGAELUVF2H/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Add a decorators called @staticproperty

2021-12-18 Thread Danilo J. S. Bellini
If the goal is to create a "property" that behaves like such but doesn't
receive "self" as a parameter, the straightforward way to do so would be a
simple descriptor.

class StaticProperty:
def __init__(self, fget=None, fset=None, fdel=None):
self.fget = fget
self.fset = fset
self.fdel = fdel
def setter(self, fset):
self.fset = fset
return self
def deleter(self, fdel):
self.fdel = fdel
return self
def __get__(self, instance, owner=None):
return self.fget()
def __set__(self, instance, value):
return self.fset(value)
def __delete__(self, instance):
return self.fdel()

Example:

class A:
@StaticProperty
def number():
return 10
@number.setter
def number(value):
if value != 10:
raise ValueError("Invalid assignment")
@number.deleter
def number():
raise AttributeError("Can't remove it!")

For reading it, both from class and instance would work (A.number ==
a.number). However it behaves like a common property for attribute
setting/deleting in the sense that an assignment to the class (e.g. A.number
= 11) would replace it and a removal (del A.number) would indeed delete it
from the class namespace. To avoid this, the change must happen in the
metaclass, so it's no longer that simple. Is there an use case for this
scenario, where an attribute assignment (and/or an attribute removal)
should do the same thing on both class and instance?

On Sat, 18 Dec 2021 at 14:41, Joao S. O. Bueno 
wrote:

> All that is needed is a descriptor which calls the decorated functiosn
> without any parameters.
>
> This could serve to use classes as namespaces for "live" globals - if one
> will do some sort
> of reactive programing, it might even find some use.
>
> A descriptor with a  for that would be something like:
>
>
> class staticproperty:
>  def __init__(self, func, setter=None):
>self.func = func
>self.setter = setter
>  def __get__(self, instance, owner):
>return self.func
>
>  # setter code (won't work statically)
>  def __set__(self, instance, value):
>return self.setter(value)
>  def setter(self, func):
>return type(self)(self.func, func)
>
> I wrote the "setter"  code above, but actually it won't work  statically -
> for the descriptor protocol to call __set__ it must be operating with an
> instance of the class.  It could still work involving metaclasses  - but
> if you want the
> setter only, the above snippet should work.
>
> Should it be on the stdlib? I don't think so - its usage would be too
> specific, and
> there are differing interpretations to what it should actually do.
>
>
> On Sat, 18 Dec 2021 at 14:23, Christopher Barker 
> wrote:
>
>> I'm confused about what a staticproperty would even be.
>>
>> Usually, properties are a way to provide an  interface that "looks like"
>> a simple attribute, but does some computation under the hood. But that
>> computation usually requires instance data to do its thing -- so a static
>> one wouldn't be useful.
>>
>> In fact, in Python, a staticmethod is not very useful at all anyway, all
>> it is is a function that lives in the class namespace. Making it a property
>> would make it look like a class attribute.
>>
>> Hmm, I guess one use case would be to make a read only class attribute.
>>
>> Anyway, the thing is that both staticmethod and property are implimented
>> using descriptors, which I think can only be invoked by instance attribute
>> lookup. That is, the class attribute IS a descriptor instance.
>>
>> And Chris A says -- there may be a way to get a similar effect with
>> Metaclasses, but we'd have to know what your goal is to advise on how to do
>> that.
>>
>> Note: you can put a descriptor on class, and the __get__ will be called,
>> to get part of what I think you want:
>>
>> In [52]: class Ten:
>> ...: def __get__(self, obj, objtype=None):
>> ...: return 10
>> ...: def __set__(self, obj, value):
>> ...: raise AttributeError("attribute can not be set")
>> ...:
>>
>> In [53]: class A:
>> ...: y = Ten()
>> ...:
>>
>> # attribute access does call the descriptor's __get__:
>>
>> In [54]: A.y
>> Out[54]: 10
>>
>> But setting the attribute replaces the descriptor, rather than raising an
>> exception:
>>
>> In [55]: A.y = 12
>>
>> In [56]: A.y
>> Out[56]: 12
>>
>> Honestly, I don't quite "get" how all this works, but the usual thing is
>> for Descriptors to be invoked on instance attribute access.
>>
>> -CHB
>>
>>
>> On Sat, Dec 18, 2021 at 8:30 AM  wrote:
>>
>>> In the following situations:
>>>
>>>
>>> class Data(object):
>>> @staticmethod
>>> @property
>>> def imagesTotal():
>>> return 10
>>>
>>> print(Data.imagesTotal)
>>>
>>>
>>> The "print(Data.imagesTotal)" can't print "10", it print ">> object at 0x...>".
>>>
>>> It might be a good idea to use 

[Python-ideas] Re: Add a decorators called @staticproperty

2021-12-18 Thread Joao S. O. Bueno
All that is needed is a descriptor which calls the decorated functiosn
without any parameters.

This could serve to use classes as namespaces for "live" globals - if one
will do some sort
of reactive programing, it might even find some use.

A descriptor with a  for that would be something like:


class staticproperty:
 def __init__(self, func, setter=None):
   self.func = func
   self.setter = setter
 def __get__(self, instance, owner):
   return self.func

 # setter code (won't work statically)
 def __set__(self, instance, value):
   return self.setter(value)
 def setter(self, func):
   return type(self)(self.func, func)

I wrote the "setter"  code above, but actually it won't work  statically -
for the descriptor protocol to call __set__ it must be operating with an
instance of the class.  It could still work involving metaclasses  - but if
you want the
setter only, the above snippet should work.

Should it be on the stdlib? I don't think so - its usage would be too
specific, and
there are differing interpretations to what it should actually do.


On Sat, 18 Dec 2021 at 14:23, Christopher Barker 
wrote:

> I'm confused about what a staticproperty would even be.
>
> Usually, properties are a way to provide an  interface that "looks like" a
> simple attribute, but does some computation under the hood. But that
> computation usually requires instance data to do its thing -- so a static
> one wouldn't be useful.
>
> In fact, in Python, a staticmethod is not very useful at all anyway, all
> it is is a function that lives in the class namespace. Making it a property
> would make it look like a class attribute.
>
> Hmm, I guess one use case would be to make a read only class attribute.
>
> Anyway, the thing is that both staticmethod and property are implimented
> using descriptors, which I think can only be invoked by instance attribute
> lookup. That is, the class attribute IS a descriptor instance.
>
> And Chris A says -- there may be a way to get a similar effect with
> Metaclasses, but we'd have to know what your goal is to advise on how to do
> that.
>
> Note: you can put a descriptor on class, and the __get__ will be called,
> to get part of what I think you want:
>
> In [52]: class Ten:
> ...: def __get__(self, obj, objtype=None):
> ...: return 10
> ...: def __set__(self, obj, value):
> ...: raise AttributeError("attribute can not be set")
> ...:
>
> In [53]: class A:
> ...: y = Ten()
> ...:
>
> # attribute access does call the descriptor's __get__:
>
> In [54]: A.y
> Out[54]: 10
>
> But setting the attribute replaces the descriptor, rather than raising an
> exception:
>
> In [55]: A.y = 12
>
> In [56]: A.y
> Out[56]: 12
>
> Honestly, I don't quite "get" how all this works, but the usual thing is
> for Descriptors to be invoked on instance attribute access.
>
> -CHB
>
>
> On Sat, Dec 18, 2021 at 8:30 AM  wrote:
>
>> In the following situations:
>>
>>
>> class Data(object):
>> @staticmethod
>> @property
>> def imagesTotal():
>> return 10
>>
>> print(Data.imagesTotal)
>>
>>
>> The "print(Data.imagesTotal)" can't print "10", it print "> object at 0x...>".
>>
>> It might be a good idea to use "@staticproperty" to solve this problem.
>> "@staticproperty" is a decorators, it mix the @staticmethod and @property.
>> Then the static property has getter and setter.
>> ___
>> 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/DEC3AA2NN5KTI5LQ6M7FIRLPDYLNSP7G/
>> Code of Conduct: http://python.org/psf/codeofconduct/
>>
>
>
> --
> Christopher Barker, PhD (Chris)
>
> Python Language Consulting
>   - Teaching
>   - Scientific Software Development
>   - Desktop GUI and Web Development
>   - wxPython, numpy, scipy, Cython
> ___
> 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/7WACW25ZZELEMVWE6CAX7DICPIJUXGYC/
> 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/BHHMB75QGOVCA5SU6QU57EDQHY4RN2TQ/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Add a decorators called @staticproperty

2021-12-18 Thread Christopher Barker
I'm confused about what a staticproperty would even be.

Usually, properties are a way to provide an  interface that "looks like" a
simple attribute, but does some computation under the hood. But that
computation usually requires instance data to do its thing -- so a static
one wouldn't be useful.

In fact, in Python, a staticmethod is not very useful at all anyway, all it
is is a function that lives in the class namespace. Making it a property
would make it look like a class attribute.

Hmm, I guess one use case would be to make a read only class attribute.

Anyway, the thing is that both staticmethod and property are implimented
using descriptors, which I think can only be invoked by instance attribute
lookup. That is, the class attribute IS a descriptor instance.

And Chris A says -- there may be a way to get a similar effect with
Metaclasses, but we'd have to know what your goal is to advise on how to do
that.

Note: you can put a descriptor on class, and the __get__ will be called, to
get part of what I think you want:

In [52]: class Ten:
...: def __get__(self, obj, objtype=None):
...: return 10
...: def __set__(self, obj, value):
...: raise AttributeError("attribute can not be set")
...:

In [53]: class A:
...: y = Ten()
...:

# attribute access does call the descriptor's __get__:

In [54]: A.y
Out[54]: 10

But setting the attribute replaces the descriptor, rather than raising an
exception:

In [55]: A.y = 12

In [56]: A.y
Out[56]: 12

Honestly, I don't quite "get" how all this works, but the usual thing is
for Descriptors to be invoked on instance attribute access.

-CHB


On Sat, Dec 18, 2021 at 8:30 AM  wrote:

> In the following situations:
>
>
> class Data(object):
> @staticmethod
> @property
> def imagesTotal():
> return 10
>
> print(Data.imagesTotal)
>
>
> The "print(Data.imagesTotal)" can't print "10", it print " at 0x...>".
>
> It might be a good idea to use "@staticproperty" to solve this problem.
> "@staticproperty" is a decorators, it mix the @staticmethod and @property.
> Then the static property has getter and setter.
> ___
> 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/DEC3AA2NN5KTI5LQ6M7FIRLPDYLNSP7G/
> Code of Conduct: http://python.org/psf/codeofconduct/
>


-- 
Christopher Barker, PhD (Chris)

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython
___
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/7WACW25ZZELEMVWE6CAX7DICPIJUXGYC/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Add a decorators called @staticproperty

2021-12-18 Thread Chris Angelico
On Sun, Dec 19, 2021 at 3:31 AM  wrote:
>
> In the following situations:
>
>
> class Data(object):
> @staticmethod
> @property
> def imagesTotal():
> return 10
>
> print(Data.imagesTotal)
>
>
> The "print(Data.imagesTotal)" can't print "10", it print " 0x...>".
>
> It might be a good idea to use "@staticproperty" to solve this problem.
> "@staticproperty" is a decorators, it mix the @staticmethod and @property.
> Then the static property has getter and setter.

I'm not sure that this is actually possible the way you're doing it.
The descriptor protocol (which is what makes properties work) won't
apply when you're looking up statically.

Your best bet, if you really need this sort of thing, would probably
be to make a metaclass. But I can't advise further without a good
use-case (and the example you've shown above looks extremely
unpythonic).

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


[Python-ideas] Add a decorators called @staticproperty

2021-12-18 Thread me
In the following situations: 


class Data(object):
@staticmethod
@property
def imagesTotal():
return 10

print(Data.imagesTotal)


The "print(Data.imagesTotal)" can't print "10", it print "".

It might be a good idea to use "@staticproperty" to solve this problem.
"@staticproperty" is a decorators, it mix the @staticmethod and @property.
Then the static property has getter and setter.
___
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/DEC3AA2NN5KTI5LQ6M7FIRLPDYLNSP7G/
Code of Conduct: http://python.org/psf/codeofconduct/