Re: How to convert a number to hex number?

2005-11-08 Thread Daniel Evers
Hi!

Try hex:
>>> hex(120)
'0x78'

Consider converting string -> int using the int()-function:
>>> print int.__doc__
int(x[, base]) -> integer

Convert a string or number to an integer, if possible.  A floating point
argument will be truncated towards zero (this does not include a string
representation of a floating point number!)  When converting a string, use
the optional base.  It is an error to supply a base when converting a
non-string. If the argument is outside the integer range a long object
will be returned instead.

Daniel
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: iterate over class variables

2005-11-10 Thread Daniel Evers
Hi!

You can iterate over the internal dictionary:

>>> class Test:
... def __init__(self):
... self.x = 5
... self.y = 6
... self.z = "Hallo"
... 
>>> x = Test()
>>> print x.__dict__
{'y': 6, 'x': 5, 'z': 'Hallo'}
>>> for key, value in x.__dict__.items():
... print key
... print value
... 
y
6
x
5
z
Hallo
>>> 

Consider using iteritems() instead of items() when you have a loop.

Hope that helps :)
Daniel
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What do you use as symbols for Python ?

2005-11-11 Thread Daniel Evers
Hi!

Never would have thought of this...
I mixed this with the class-version and created a new class derived from
"str" for easier printing and added an iterator:

---

class Enum:
class Type(str):
def __init__(self, name):
self.__name = name
def __str__(self):
return self.__name

def __init__(self, *keys):
self.__keys = []
for key in keys:
mytype = self.Type(key)
self.__dict__[key] = mytype
self.__keys.append(mytype)
self.__index = -1
self.__count = len(keys)

def __iter__(self):
return self

def next(self):
self.__index = self.__index + 1
if (self.__index >= self.__count):
self.__index = -1
raise StopIteration
return self.__keys[self.__index]

friends = Enum("Eric", "Kyle", "Stan", "Kenny")
print "These are my friends:",
print ", ".join([kid for kid in friends])
for kid in friends:
print kid,
if kid is friends.Kenny:
print "dead"
else:
print "alive"
---

Daniel
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Internal Variables

2005-11-11 Thread Daniel Evers
Hi!

The sys module provides some useful information, e.g.:

builtin_module_names -- tuple of module names built into this interpreter
version -- the version of this interpreter as a string
version_info -- version information as a tuple
hexversion -- version information encoded as a single integer
copyright -- copyright notice pertaining to this interpreter
platform -- platform identifier
executable -- pathname of this Python interpreter
prefix -- prefix used to find the Python library
exec_prefix -- prefix used to find the machine-specific Python library

(from sys.__doc__)
Maybe you can start searching there.

Daniel
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What do you use as symbols for Python ?

2005-11-12 Thread Daniel Evers
Peter Otten wrote:
> 
> You should ditch what follows and instead add just
> 
>   def __iter__(self):
>   return  iter(self.__keys)

Mhh. I should start learning the builtins ... :)

> To see the problem with your original code (an object serving as its own
> iterator) try the following example:
> 
> friends = Enum("Eric", "Kyle", "Stan", "Kenny")
> if "Kyle" in friends:
> print "Hi Kyle"
> print "My friends:", ", ".join(friends)
> 
> Only Stan and Kenny show up in the last print statement because the
> containment test did not iterate over all friends.

Never would have thought of this... Makes it even easier.

> Also, Type.__name seems redundant. Just
> 
> class Type(str): pass
> 
> should do the job.

Right, works just fine. I'm not used to types, meanwhile I replaced
Type(str) by a simple class Type, works as well.

Thanks a lot, I was starting to integrate this solution in my code, now I
can fix it before it's even used :)

Daniel
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: combine doxygen and doc-strings?

2005-11-18 Thread Daniel Evers
You're maybe searching for epydoc: http://epydoc.sourceforge.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to determine an object is "scriptable"

2006-03-30 Thread Daniel Evers
Richard Brodie wrote:

> subscriptable: supports an indexing operator, like a list does.

Right. You can check this e.g. with

hasattr(x, "__getitem__")

because the __getitem__ method is used for indexing.

Daniel
-- 
http://mail.python.org/mailman/listinfo/python-list