Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Equivalent to C bitmasks and enumerations

Reply
Thread Tools

Equivalent to C bitmasks and enumerations

 
 
Ulrich Eckhardt
Guest
Posts: n/a
 
      04-15-2009
Greetings!

I'm currently using Python to implement a set of tests for code that is
otherwise written in C. This code was wrapped using Boost.Python and is
then loaded into Python as module.

What I often have in C is this:

// bitfield (several flags combined)
#define STATUS_OVERTEMP 1u
#define STATUS_ON_FIRE 2u
#define STATUS_BORED 4u
unsigned get_status(void);

// enumeration (distinct values from a set)
enum color { color_red=1, color_green=13, color_mauve=42 };
enum color get_color(void);

What I'm looking for is a suggestion how to handle this in Python. Note that
technically, this works without problem, I'm rather looking for stylistic
advise. What I currently have is these (note: I'm retyping this, so ignore
any syntax errors, please):

STATUS_OVERTEMP = 1
STATUS_ON_FIRE = 2
STATUS_BORED = 4
def status_as_string(st):
tmp = []
if st&STATUS_OVERTEMP:
tmp.append("OVERTEMP")
if st&STATUS_ON_FIRE:
tmp.append("ON_FIRE")
if st&STATUS_BORED:
tmp.append("BORED")
return "|".join(tmp)

COLOR_RED = 1
COLOR_GREEN = 13
COLOR_MAUVE = 42
def color_as_string(c):
names = {
COLOR_RED:"RED",
COLOR_GREEN:"GREEN",
COLOR_MAUVE:"MAUVE"}
return names[c];

Further, I also tried defining a separate class:

class Color(int):
RED = 1
GREEN = 13
MAUVE = 42
names = { RED:"RED", GREEN:"GREEN", MAUVE:"MAUVE"}

def __str__(self):
return names[c];
...


To be a bit more explicit about what I would like, here is an example how I
would like to use it:

c = Color.RED
type(c) # should yield class Color
str(c) # should yield "RED"
Color(999) # should signal the invalid color value


Any other suggestions how to structure this or rewrite this?

thanks!

Uli


--
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

 
Reply With Quote
 
 
 
 
Dave Angel
Guest
Posts: n/a
 
      04-16-2009
Ulrich Eckhardt wrote:
> Greetings!
>
> I'm currently using Python to implement a set of tests for code that is
> otherwise written in C. This code was wrapped using Boost.Python and is
> then loaded into Python as module.
>
> What I often have in C is this:
>
> // bitfield (several flags combined)
> #define STATUS_OVERTEMP 1u
> #define STATUS_ON_FIRE 2u
> #define STATUS_BORED 4u
> unsigned get_status(void);
>
> // enumeration (distinct values from a set)
> enum color { color_red=1, color_green=13, color_mauve=42 };
> enum color get_color(void);
>
> What I'm looking for is a suggestion how to handle this in Python. Note that
> technically, this works without problem, I'm rather looking for stylistic
> advise. What I currently have is these (note: I'm retyping this, so ignore
> any syntax errors, please):
>
> STATUS_OVERTEMP = 1
> STATUS_ON_FIRE = 2
> STATUS_BORED = 4
> def status_as_string(st):
> tmp = []
> if st&STATUS_OVERTEMP:
> tmp.append("OVERTEMP")
> if st&STATUS_ON_FIRE:
> tmp.append("ON_FIRE")
> if st&STATUS_BORED:
> tmp.append("BORED")
> return "|".join(tmp)
>
> COLOR_RED = 1
> COLOR_GREEN = 13
> COLOR_MAUVE = 42
> def color_as_string(c):
> names = {
> COLOR_RED:"RED",
> COLOR_GREEN:"GREEN",
> COLOR_MAUVE:"MAUVE"}
> return names[c];
>
> Further, I also tried defining a separate class:
>
> class Color(int):
> RED = 1
> GREEN = 13
> MAUVE = 42
> names = { RED:"RED", GREEN:"GREEN", MAUVE:"MAUVE"}
>
> def __str__(self):
> return names[c];
> ...
>
>
> To be a bit more explicit about what I would like, here is an example how I
> would like to use it:
>
> c = Color.RED
> type(c) # should yield class Color
> str(c) # should yield "RED"
> Color(999) # should signal the invalid color value
>
>
> Any other suggestions how to structure this or rewrite this?
>
> thanks!
>
> Uli
>
>

For the Color example, how's this for a starting place:


class Color(object):
def __init__(self, name, enum):
self.enum = enum
self.name = name
setattr(Color, name, self)

@staticmethod
def seal():
del Color.__init__
del Color.seal

def __str__(self):
return self.name



Color("RED", 4)
Color("GREEN", 11)
Color.seal() #prevent any new instances from being created


b = Color.RED
print type(b)
print str(b)

a = Color.GREEN
print a
print a.enum

k = Color
xx = Color("aaa", 42) #error



>
>

 
Reply With Quote
 
 
 
 
Ulrich Eckhardt
Guest
Posts: n/a
 
      04-20-2009
Ulrich Eckhardt wrote:
[how to handle bitfields and enumerations in Python]

Thanks to all that answered. The important lessons I learned:
* You can modify classes, other than in C++ where they are statically
defined. This allows e.g. adding constants.
* __repr__ should provide output suitable as input to the Python
interpreter if possible.

Very interesting!

Uli

--
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Forward decleration of enumerations and typedefs in a NameSpace kasiyil C++ 3 08-21-2006 08:33 PM
file io and enumerations cpisz@austin.rr.com C++ 1 06-28-2006 11:03 PM
#defines and enumerations in Java - How ? exquisitus Java 12 02-19-2005 03:27 PM
reinterpret_cast and enumerations Christopher Benson-Manica C++ 5 11-03-2004 02:49 PM
creating bitmasks (for bloom filters) Aaron Straup Cope Python 0 06-26-2004 02:56 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57