[Python-ideas] Re: len(Enum) should raise TypeError

2023-04-03 Thread Benedict Verhegghe
Well, it's not an object of type 'type' like the list or dict mentioned 
by the OP, for which len() give a

TypeError: object of type 'type' has no len()

Any derived class of Enum will also return True for
isinstance(..., type). Should the derived classes then neither have a 
meaningful result for len()? Enum and derived classes hold a container 
with a fixed set of values. It makes perfectly sense to ask for the 
number of possible values, even when there are none.



Op 3/04/2023 om 08:53 schreef Chris Angelico:

On Mon, 3 Apr 2023 at 15:54, Benedict Verhegghe  wrote:


Enum is not a type, like the OP suggested:



Well, it is:


isinstance(Enum, type)

True

but it has a metaclass, meaning that the type of Enum is a subclass of type.


type(Enum)



type(Enum).__bases__

(,)

I mean, we wouldn't be subclassing it if it weren't a type.

The __len__ method is implemented on the metaclass to allow all Enum
subclasses to have lengths (ditto __iter__ to make them iterable), and
as a consequence of that, Enum itself has all the attributes that it
wants to give to its subclasses.

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


[Python-ideas] Re: len(Enum) should raise TypeError

2023-04-02 Thread Benedict Verhegghe

Enum is not a type, like the OP suggested:

from enum import Enum
class Color(Enum):
WHITE = 1
type(Color)

type(Enum)

type(enum.EnumMeta)

Enum.__len__
>

So to me it looks like Enum is just a generic, empty enum. The __len__ 
method is implemented in EnumMeta.



Op 3/04/2023 om 00:18 schreef Greg Ewing:

On 2/04/23 6:36 pm, Benedict Verhegghe wrote:
An Enum is functionally a container with a limited set of constants. 
Why should it not be legitimate to ask for the number of constants in it?


But Enum itself isn't an enum, it's a constructor for enums.
I think len() just happens to work on it as a side effect
of the way enums are implemented.


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


[Python-ideas] Re: len(Enum) should raise TypeError

2023-04-01 Thread Benedict Verhegghe
An Enum is functionally a container with a limited set of constants. Why 
should it not be legitimate to ask for the number of constants in it?



Op 1/04/2023 om 16:45 schreef Richard Hajek:

# Feature or enhancement

`len(Enum)` has no useful meaning and should therefore raise the appropriate 
error

# Pitch

I cannot figure out any scenario, in which len(Enum) should not raise. Enum is 
a type and as any other types, such as `len(list)` or `len(dict)`, and should 
not give any meaningful result on len.

Thoughts?
___
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/TQB2HRGRJZ64DDWULU6N7RPIPDDJ72GI/
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/SYT5QW335KRXJMAG5HSIKKIELE7JFXNP/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Better (?) PRNG - follow up

2022-12-06 Thread Benedict Verhegghe
The difference is probably because I count 4 repeated values as 2 
triples, etc..., while the 5.382 value counts them only once?


Benedict

 Original Message 
From: Benedict Verhegghe
Sent: Tuesday, December 6, 2022 at 10:38 UTC
To: python-ideas@python.org
Subject: [Python-ideas] Re: Better (?) PRNG - follow up

I used a brute force method to check the probability. Counted the number 
of triples in 600 random numbers 0-9, repeated that 1 times and took 
the mean: 5.99

So it looks like Chris's number is more accurate.

Benedict

Op 6/12/2022 om 09:25 schreef Alex Prengère:
@Chris Indeed the true figure, if my math is correct, is a bit under 
5.98 because of the "non-independence" of triplets.

I computed it and found 5.382, so finding 6 is entirely normal.

For the details: calling L = 600 and n = 3
* number of possible sequence of L digits: 10^L
* if a specific digit appears in a specific position n times, the rest 
of the sequence has 9*10^(L-n-1) possibilities [9 because after the 
repetition you have a different digit]

     * we need to multiply that number by 10 as we have 10 digits
     * we need to multiply it again by L-n+1 as the digit can start in 
any place until the L-n+1 place
     * so the total number of sequence of L digits with at least 1 
repetition of length n is: 9*10^(L-n-1) * 10 * (L-n+1)


Dividing the 2 to compute the probability:
P = 9*10^(L-n-1) * 10 * (L-n+1) / 10^L = 9*10^(L-n) * (L-n+1) / 10^L

$ python3 -c "L = 600; n = 3; print(9*10**(L-n) * (L-n+1) / 10**L)"
5.382

Regards,
Alex

Le mar. 6 déc. 2022 à 07:34, Greg Ewing <mailto:gcew...@snap.net.nz>> a écrit :


    On 6/12/22 3:58 pm, James Johnson wrote:
 > I came back to this thread looking for the list of randomness
    tests, and
 > I keep missing them somehow.

    If you're interested in testing a PRNG really thoroughly, check
    out TestU01:
    http://simul.iro.umontreal.ca/testu01/tu01.html
    <http://simul.iro.umontreal.ca/testu01/tu01.html>

    --     Greg

    ___
    Python-ideas mailing list -- python-ideas@python.org
    <mailto:python-ideas@python.org>
    To unsubscribe send an email to python-ideas-le...@python.org
    <mailto:python-ideas-le...@python.org>
    https://mail.python.org/mailman3/lists/python-ideas.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/JNK52FQDWCDRFNWB2PXTGTXVWUDYKLVJ/ <https://mail.python.org/archives/list/python-ideas@python.org/message/JNK52FQDWCDRFNWB2PXTGTXVWUDYKLVJ/>

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

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


[Python-ideas] Re: Better (?) PRNG - follow up

2022-12-06 Thread Benedict Verhegghe
I used a brute force method to check the probability. Counted the number 
of triples in 600 random numbers 0-9, repeated that 1 times and took 
the mean: 5.99

So it looks like Chris's number is more accurate.

Benedict

Op 6/12/2022 om 09:25 schreef Alex Prengère:
@Chris Indeed the true figure, if my math is correct, is a bit under 
5.98 because of the "non-independence" of triplets.

I computed it and found 5.382, so finding 6 is entirely normal.

For the details: calling L = 600 and n = 3
* number of possible sequence of L digits: 10^L
* if a specific digit appears in a specific position n times, the rest 
of the sequence has 9*10^(L-n-1) possibilities [9 because after the 
repetition you have a different digit]

     * we need to multiply that number by 10 as we have 10 digits
     * we need to multiply it again by L-n+1 as the digit can start in 
any place until the L-n+1 place
     * so the total number of sequence of L digits with at least 1 
repetition of length n is: 9*10^(L-n-1) * 10 * (L-n+1)


Dividing the 2 to compute the probability:
P = 9*10^(L-n-1) * 10 * (L-n+1) / 10^L = 9*10^(L-n) * (L-n+1) / 10^L

$ python3 -c "L = 600; n = 3; print(9*10**(L-n) * (L-n+1) / 10**L)"
5.382

Regards,
Alex

Le mar. 6 déc. 2022 à 07:34, Greg Ewing > a écrit :


On 6/12/22 3:58 pm, James Johnson wrote:
 > I came back to this thread looking for the list of randomness
tests, and
 > I keep missing them somehow.

If you're interested in testing a PRNG really thoroughly, check
out TestU01:
http://simul.iro.umontreal.ca/testu01/tu01.html


-- 
Greg


___
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/JNK52FQDWCDRFNWB2PXTGTXVWUDYKLVJ/
 

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


[Python-ideas] Re: Enhancing variable scope control

2022-11-30 Thread Benedict Verhegghe
A context manager could be used for this. On exit it should delete the 
variables created inside it. Someting like this:


class Scope:
def __enter__(self):
self._globals = list(globals().keys())
return self
def __exit__(self, exc_type, exc_value, traceback):
del_vars = [var for var in globals()
if var not in self._globals]
for var in del_vars:
del globals()[var]

Then one can write:

with Scope():
a = 1
with Scope():
b = 2
# no more b here
# no more a or b here

Maybe such an (optimized) context manager could be added to Python?


Op 29/11/2022 om 02:49 schreef Anony Mous:

As it stands now, to create a local scope, you must define a function.

However, there are many cases where various things can reasonably be 
done inline. Preparatory code is often done this way.


Good coding practice is generally accepted to be that variables are 
local if at all possible. However, in direct, inline Python code, we're 
inherently creating variables with a global scope.


We don't actually need a function for this kind of code; but a local 
scope would often be valuable (as we see with lambda.) Moving things off 
into a function can create a circumstance where we have to go looking 
for the function. When something is a "one-shot" as in global 
preparatory code, that's doing more work, and creating more indirection, 
than needs actually be done. But global operations have their own 
pitfalls, one of which is variable collisions. So Python sort of drives 
us to make "one-use" functions where lambdas are insufficient to the 
case in order to control locality.


You can end up writing things like...

def PrepOne():
     for MyCount in range(0,10):
         pass

PrepOne()

...which achieves the locality desired for variable MyCount, but is 
clunky. It also clutters the global namespace with prepOne, which has no 
good reason to exist.


So I'm thinking of something along these lines:

local:    # <== or whatever syntax makes sense
     for MyCount in range(0,10):
         pass

So in that example, the code is inline - runs as part of the global, 
sequential code execution - but the variable MyCount is local and goes 
"poof" once the local: scope is exited.


This is the scoping equivalent of a pair of curly braces in c and c++. I 
would like to see it be nestable, as scopes are in c/c++:


local:    # <== or whatever syntax makes sense
     for MyCount in range(0,10):
         local:
             for YourCount in range(0,MyCount+1)
                 pass

There, MyCount is available in both scopes (the first local: is an 
enclosing scope) and YourCount is available only in the second local: scope.


Or, local: could be very strict, and require further syntax to 
incorporate a previous scope, something like this:


local:    # <== or whatever syntax makes sense
     for MyCount in range(0,10):
         local:
             incorporate MyCount # <== or whatever syntax makes sense
             for YourCount in range(0,MyCount+1)
                 pass

Lastly, I suggest that the global keyword be used to expose global scope 
variables, just as it is within def functions:


TheirCount = 10
local:    # <== or whatever syntax makes sense
     global TheirCount
     for MyCount in range(0,TheirCount):
         local:
             for YourCount in range(0,TheirCount)
                 pass

This would also be useful within normal functions, and again allows the 
program flow to be linear rather than calling a function-within-a-function.


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


[Python-ideas] Re: range() manipulations

2022-08-03 Thread Benedict Verhegghe

These kind of problems can easily be handled with numpy:

>>> import numpy as np
>>> print(np.union1d(np.arange(3,15,2), np.arange(8,12,2)))
[ 3  5  7  8  9 10 11 13]


Op 3/08/2022 om 21:23 schreef Random832:

On Mon, Aug 1, 2022, at 09:19, Paul Moore wrote:

There are a lot of complex cases you'd need to consider. What would the
value of range(3, 15, 2) + range(8, 12, 2) be? It's not a range in the
sense of being describable as (start, end, step). And simply saying
"that's not allowed" wouldn't really work, as it would be far too hard
to work with if operations could fail unexpectedly like this. In
reality, this feels more like you're after set algebra, which Python
already has.


Maybe it would make sense for the type of the result to devolve into a sorted 
set (do we have a sorted set type now?) if it's not representable as a 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/PKGCHWYKC5P27FTKZH5LGVQ5RCZMMXUS/
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/ZZQTL4M6RRFBMNFOQFT7Z3GNLKHKLLSB/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Addition to fnmatch.py

2022-06-06 Thread Benedict Verhegghe




Op 6/06/2022 om 18:07 schreef Mathew Elman:

oops, you're right, I have ended up with an unnecessary "not" in there, should 
be:

from collections import deque


def partition(pred, iterable):
 results = deque([]), deque([])

 def gen_split(only):
 for thing in iterable:
 if results[only]:
 yield results[only].popleft()
 results[pred(thing)].append(thing)
 for thing in results[only]:
 yield thing

 return gen_split(True), gen_split(False)


There still is something wrong. I get the second list twice:

odd, even = partition(lambda i: i % 2, range(20))
print(list(odd))
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
print(list(even))
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

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


[Python-ideas] Re: Addition to fnmatch.py

2022-06-05 Thread Benedict Verhegghe

Op 5/06/2022 om 18:47 schreef David Mertz, Ph.D.:
Sure, that's nice enough code and has the same big-O complexity. I 
suspect set difference is a little faster (by a constant multiple) 
because it hits C code more, but I haven't benchmarked.


The OP said the elements were from fnmatch though, which explicitly does 
not promise order. So really it's just whether you like your code or 
this better aesthetically:


     list(set(b) - set(a))


I benchmarked it and indeed the list difference is a little faster.
>>> timeit.timeit('s=set(b); [x for x in a if x not in s]', setup='a = 
list(range(1)); b = list(range(1000))', number=1000)

0.6156670850032242
>>> timeit.timeit('list(set(a)-set(b))', setup='a = list(range(1)); 
b = list(range(1000))', number=1000)

0.43649216600169893

And what's more, it seems to also preserve order. I guess because a set 
is implemented like a dict, and will preserve order of you only remove 
some elements from the set.


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


[Python-ideas] Re: Whitespace handling for indexing

2022-05-24 Thread Benedict Verhegghe

Op 24/05/2022 om 18:46 schreef Chris Angelico:


(foo["bar"]
   ["baz"]
   ["eggs"]
   ["spam"] = 1)



Not for assignment, unfortunately. And you can't cheat with := either,
since only simple names are permitted.


This would work:

(foo["bar"]
["baz"]
["eggs"]
["spam"]) = 1

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