Re: Like c enumeration in python3

2020-03-23 Thread Paulo da Silva
Thank you very much for all your responses!
Now I have too much ideas :-)
I'm saving your answers and I'll see what is more
appropriate/comfortable in my case.

Best regards.
Paulo
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Like c enumeration in python3 [ERRATA]

2020-03-23 Thread Terry Reedy

On 3/22/2020 11:37 PM, DL Neil via Python-list wrote:

On 23/03/20 3:32 PM, Paulo da Silva wrote:

Às 02:18 de 23/03/20, Paulo da Silva escreveu:

Hi!


Olá,



Suppose a class C.
I want something like this:

class C:
KA=0
KB=1

KC=2

...
Kn=n

def __init__ ...
    ...


These constants come from an enum in a .h (header of C file).
They are many and may change from time to time.
Is there a way to somehow define them from inside __init__ giving for
example a list of names as strings?
There is an additional problem: C is not recognized inside __init__!



Please read the PSL docs carefully, because Python's enums seem to 
differ from those in other languages - sometimes in significant ways, 
and sometimes in a subtle manner!



I have been asking similar questions recently - particularly wanting to 
(also) use the "strings", because of the effort of upgrading many 
modules of code at the same time (the manual, or was it the PEP(?) 
refers to the need to upgrade the PSL to use enums, and to the effort 
that might cost - I suggest there has been little/v.slow take-up, but 
then keeping the PSL updated is a subject of a great many conversations, 
elsewhere).


Python's enums cannot be changed. Once set, C.KA cannot be changed to 1 


I am not sure what you are saying.  The OP's class is not an enum in the 
enum module sense.  Class attributues are usually mutable.


>>> class C:
K = 0


>>> C.K = 1
>>> C.K
1


--
Terry Jan Reedy


--
https://mail.python.org/mailman/listinfo/python-list


Re: Like c enumeration in python3

2020-03-23 Thread Barry Scott



> On 23 Mar 2020, at 11:52, Rhodri James  wrote:
> 
> On 23/03/2020 02:18, Paulo da Silva wrote:
>> Hi!
>> Suppose a class C.
>> I want something like this:
>> class C:
>>  KA=0
>>  KB=1
>>  KC=1
>>  ...
>>  Kn=n
>>  def __init__ ...
>>  ...
>> These constants come from an enum in a .h (header of C file).
>> They are many and may change from time to time.
>> Is there a way to somehow define them from inside __init__ giving for
>> example a list of names as strings?
> 
> It sounds like what you want is a separate script to process your .h file 
> into a Python module containing an enum, then import that module into your 
> program.

web search for "python parse C header" yields some interesting results.

Barry


> 
> -- 
> Rhodri James *-* Kynesim Ltd
> -- 
> https://mail.python.org/mailman/listinfo/python-list 
> 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Like c enumeration in python3

2020-03-23 Thread Rhodri James

On 23/03/2020 02:18, Paulo da Silva wrote:

Hi!

Suppose a class C.
I want something like this:

class C:
KA=0
KB=1
KC=1
...
Kn=n

def __init__ ...
...


These constants come from an enum in a .h (header of C file).
They are many and may change from time to time.
Is there a way to somehow define them from inside __init__ giving for
example a list of names as strings?


It sounds like what you want is a separate script to process your .h 
file into a Python module containing an enum, then import that module 
into your program.


--
Rhodri James *-* Kynesim Ltd
--
https://mail.python.org/mailman/listinfo/python-list


Re: Like c enumeration in python3

2020-03-23 Thread Skip Montanaro
Does the enum module help?

https://docs.python.org/3/library/enum.html

Note that within __init__ you can reference your enumerated constants
using (for example) self.KA.

Skip
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Like c enumeration in python3 [ERRATA]

2020-03-23 Thread Peter Otten
Paulo da Silva wrote:

> Às 02:18 de 23/03/20, Paulo da Silva escreveu:
>> Hi!
>> 
>> Suppose a class C.
>> I want something like this:
>> 
>> class C:
>> KA=0
>> KB=1
> KC=2
>> ...
>> Kn=n
>> 
>> def __init__ ...
>> ...
>> 
>> 
>> These constants come from an enum in a .h (header of C file).
>> They are many and may change from time to time.
>> Is there a way to somehow define them from inside __init__ giving for
>> example a list of names as strings?
>> There is an additional problem: C is not recognized inside __init__!
> Of course I'm talking about C class name, not C language.

Use enum:

>>> import enum
>>> C = enum.IntEnum("C", ["KA", "KB", "KC"], start=0)
>>> C.KB

>>> C.KB == 1
True

https://docs.python.org/3/library/enum.html#functional-api

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Like c enumeration in python3 [ERRATA]

2020-03-22 Thread DL Neil via Python-list

On 23/03/20 3:32 PM, Paulo da Silva wrote:

Às 02:18 de 23/03/20, Paulo da Silva escreveu:

Hi!


Olá,



Suppose a class C.
I want something like this:

class C:
KA=0
KB=1

KC=2

...
Kn=n

def __init__ ...
...


These constants come from an enum in a .h (header of C file).
They are many and may change from time to time.
Is there a way to somehow define them from inside __init__ giving for
example a list of names as strings?
There is an additional problem: C is not recognized inside __init__!



Please read the PSL docs carefully, because Python's enums seem to 
differ from those in other languages - sometimes in significant ways, 
and sometimes in a subtle manner!



I have been asking similar questions recently - particularly wanting to 
(also) use the "strings", because of the effort of upgrading many 
modules of code at the same time (the manual, or was it the PEP(?) 
refers to the need to upgrade the PSL to use enums, and to the effort 
that might cost - I suggest there has been little/v.slow take-up, but 
then keeping the PSL updated is a subject of a great many conversations, 
elsewhere).


Python's enums cannot be changed. Once set, C.KA cannot be changed to 1 
(for example). We may not add element Km (where m>n) at some later 
occasion after class definition-time (in fact the __new__() constructor 
is 're-wired' in order to prohibit its use post-definition). It is not 
permitted to sub-class an existing enum class, eg C(), perhaps to 
enlarge it with more members, if the 'super class' contains "members".


There is usually no need for an __init__. Plus, I've had a distinct lack 
of satisfaction playing with that - but YMMV!


Sub-classing, and even attempting to use the meta-class seems to be a 
major challenge (see StackOverflow).


It is possible to list the elements using the built-in iterator:

for c in C: print( c )

or iterator-conversion:

list( C )

and/or accessing the class's 'data-dict' directly:

for name, value in C.__members__.items():
--
Regards =dn
--
https://mail.python.org/mailman/listinfo/python-list


Re: Like c enumeration in python3 [ERRATA]

2020-03-22 Thread Paulo da Silva
Às 02:18 de 23/03/20, Paulo da Silva escreveu:
> Hi!
> 
> Suppose a class C.
> I want something like this:
> 
> class C:
>   KA=0
>   KB=1
KC=2
>   ...
>   Kn=n
> 
>   def __init__ ...
>   ...
> 
> 
> These constants come from an enum in a .h (header of C file).
> They are many and may change from time to time.
> Is there a way to somehow define them from inside __init__ giving for
> example a list of names as strings?
> There is an additional problem: C is not recognized inside __init__!
Of course I'm talking about C class name, not C language.
> 
> Thanks.
> 

-- 
https://mail.python.org/mailman/listinfo/python-list


Like c enumeration in python3

2020-03-22 Thread Paulo da Silva
Hi!

Suppose a class C.
I want something like this:

class C:
KA=0
KB=1
KC=1
...
Kn=n

def __init__ ...
...


These constants come from an enum in a .h (header of C file).
They are many and may change from time to time.
Is there a way to somehow define them from inside __init__ giving for
example a list of names as strings?
There is an additional problem: C is not recognized inside __init__!

Thanks.
-- 
https://mail.python.org/mailman/listinfo/python-list