| Reply | « Previous Thread | Next Thread » |
|
Sorry for quick question: I have a generated enum, need to use it "automatically" for python code. How to do it most automatically? Yep, manually it takes less than 5 minutes, but I really would like to automate this process. Less debugging to be done afterwards
![]() Code:
// kredit.h
// Generated by BitmapCompiler
// Copyright (c) 1998-2001 Symbian Ltd. All rights reserved.
//
enum TMbmKredit
{
EMbmKreditMask,
EMbmKreditEu
};
Cheers, --jouni |
|
Why don't you use a dictionary? you could store the values of your var there.
If you want something more C-like, I found this on google: http://code.activestate.com/recipes/67107/ http://igordsm.googlepages.com |
|
I use this so I can copy and paste C++ enums into it:
Code:
import re
#Enum class for c++ style enums
#
# Inherit from object to create a 'new style' class
class Enum(object):
_values = {} # Store our names/values
_exp = re.compile(r'(([a-zA-Z][0-9a-zA-Z]+)\s*(=\s*((0x)?-?[0-9]+))?)', re.S)
def __init__(self, enum):
self._parse(enum)
def _parse(self, enumdata):
matches = self._exp.findall(enumdata)
cur = 0
for match in matches:
name = match[1]
value = match[3]
if not value == '':
value = int(value, 0) # Using a 0 radix makes python guess it
cur = value
self._values[name] = cur
cur += 1
# Where the magic happens!
# This intercepts _all_ attribute requests
def __getattribute__(self, attr):
if not attr.startswith('_'): # If variable isn't private
return self._values[attr] # Get it from our dictionary
else:
return super(Enum, self).__getattribute__(attr) # Otherwise get it from our class
# This is a luxury function to allow iterating
def __len__(self):
return len(self._values)
Code:
TMbmKredit = Enum ("""
EMbmKreditMask=5,
EMbmKreditEu
""")
print TMbmKredit.EMbmKreditMask #5
print TMbmKredit.EMbmKreditEu #6
Edit: Seeing as I just wrote it, I've updated it since originally posting!
Last edited by mikezs : 2009-01-30 at 14:57.
Reason: Updated to enable hex!
|
|
Quote:
![]() ...however this time I'll just do some boring excel stuff to manually convert enum to python code. I need it to update listbox icons and I figure faster version should be preferable - even if it's not as nice looking as your code! ![]() Thanx, --jouni who's thinking how things really should be timed instead of just deciding which is faster code |
|
I suggest you use lazy initialization design pattern here, since running code during import is the major fact affecting Python application start up speed.
Mikko Ohtamaa Twinapex Research http://www.twinapex.com |
|
Quote:
Have to think about lazy init... Cheers, --jouni |
| Reply | « Previous Thread | Next Thread » |
| Thread Tools | Search this Thread |
|---|---|
| Rate This Thread | |
| Thread | Thread Starter | Forum | Replies | Last Post |
|---|---|---|---|---|
| Please help installing Python libraries on S60 | ericroijen | Python | 11 | 2009-07-18 11:43 |
| Python for S60 1.9.1 released | tvijayan | Python | 30 | 2009-02-26 07:16 |
| Python for S60 1.9.0 released | tvijayan | Python | 48 | 2009-01-27 16:39 |
| New to Python for S60 (PyS60)? Read this first! | croozeus | Python | 2 | 2008-10-17 20:23 |
| [announce] Mobile Python book soon in Sept 2007 ! | cyke64 | Python | 11 | 2007-10-03 20:46 |