You Are Here:

Community: Developer Discussion Boards

#1 Old How to use ENUM in python? - 2009-01-30, 12:20

Join Date: Mar 2003
Posts: 937
Location: Espoo, Finland
JOM's Avatar
JOM
Offline
Forum Nokia Champion
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
	};
Right now it's only 2 items and I'm using it successfully. Next version will have 20+ items, for testing something more. Then first released version could have about 100 and finally almost 300 items. Debugging is not fun, so any tips about how to use enum inside python code would be most welcome!

Cheers,

--jouni
Reply With Quote

#2 Old Re: How to use ENUM in python? - 2009-01-30, 13:21

Join Date: Jul 2008
Posts: 41
igordsm
Offline
Registered User
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
Reply With Quote

#3 Old Re: How to use ENUM in python? - 2009-01-30, 14:46

Join Date: Sep 2008
Posts: 36
mikezs
Offline
Registered User
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)
Then you can just do this:

Code:
TMbmKredit = Enum ("""
	EMbmKreditMask=5,
	EMbmKreditEu
	""")
print TMbmKredit.EMbmKreditMask #5
print TMbmKredit.EMbmKreditEu #6
Looks pretty tidy-ish too

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!
Reply With Quote

#4 Old Re: How to use ENUM in python? - 2009-01-30, 20:22

Join Date: Mar 2003
Posts: 937
Location: Espoo, Finland
JOM's Avatar
JOM
Offline
Forum Nokia Champion
Quote:
Originally Posted by mikezs View Post
I use this so I can copy and paste C++ enums into it:
...
Then you can just do this:

Code:
TMbmKredit = Enum ("""
	EMbmKreditMask=5,
	EMbmKreditEu
	""")
print TMbmKredit.EMbmKreditMask #5
print TMbmKredit.EMbmKreditEu #6
Looks pretty tidy-ish too
This looks pretty much what I was looking for, just drag and drop enum into python code and it just works! Thanx, I'll save this into my archives!

...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
Reply With Quote

#5 Old Re: How to use ENUM in python? - 2009-02-01, 01:18

Join Date: Jan 2004
Posts: 369
Location: Helsinki
Send a message via Skype™ to miohtama
miohtama's Avatar
miohtama
Offline
Regular Contributor
Quote:
Originally Posted by mikezs View Post
I use this so I can copy and paste C++ enums into it:

Code:
import re

	def __init__(self, enum):
		self._parse(enum)
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
Reply With Quote

#6 Old Re: How to use ENUM in python? - 2009-02-01, 13:35

Join Date: Mar 2003
Posts: 937
Location: Espoo, Finland
JOM's Avatar
JOM
Offline
Forum Nokia Champion
Quote:
Originally Posted by miohtama View Post
I suggest you use lazy initialization design pattern here, since running code during import is the major fact affecting Python application start up speed.
I noticed! Current implementation is built-in hardcoded dictionary and list, both only some 100+ items, and startup is too slow. Second launch is much faster for some reason.. Is emulator doing some graphics icon caching?

Have to think about lazy init...

Cheers,

--jouni
Reply With Quote
Reply « Previous Thread | Next Thread »
Display Modes
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules

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

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump
Similar Threads
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

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia