__cmp__ method
Hi Can anyone explain to me why the following codes do not work? I want to try out using __cmp__ method to change the sorting order. I subclass the str and override the __cmp__ method so the strings can be sorted by the lengh. I expect the shortest string should be in the front. Thanks >>> class myStr(str): def __init__(self, s): str.__init__(self, s) # Ensure super class is initialized def __cmp__(self, other): return cmp(len(self), len(other)) >>> a = myStr('abc') >>> b = myStr('Personal') >>> c = myStr('Personal firewall') >>> sorted([c, b, a]) ['Personal', 'Personal firewall', 'abc'] >>> -- http://mail.python.org/mailman/listinfo/python-list
Regular Expression pattern group
Hi I am a fussy learner. Could someone explain to me why the following inconsistency exists between methods? How can it be justified if it is considered all right? There are three groups in pattern. However, match object shows 3 groups in collection, but group has to be indexed from one because the m.group(0) is the implicit group for whole pattern. Are these just some things to remember? "Group counting is from one". >>> p = sre.compile('abc(.(.).)d(ef)') >>> s 'xxxabc???defxxx' >>> m = p.search(s) >>> m.groups() ('???', '?', 'ef') >>> m.group(0) 'abc???def' >>> m.group(1) '???' >>> m.group(2) '?' >>> m.group(3) 'ef' >>> m.group(4) Traceback (most recent call last): File "", line 1, in -toplevel- m.group(4) IndexError: no such group >>> p.findall(s) [('???', '?', 'ef')] >>> -- http://mail.python.org/mailman/listinfo/python-list
Is type object an instance or class?
Hi I found that a type/class are both a subclass and a instance of base type "object". It conflicts to my understanding that: 1.) a type/class object is created from class statement 2.) a instance is created by "calling" a class object. A object should not be both a class and an instance at the same time. Further I found out there is a special type call "type" that is a subclass of base type "object". All other types are instances of this type. Even base type "object" is an instance of this special type. What is role of this type "type" in object creation? Could someone there straighten this concept a little? For example (Python2.5): >>> issubclass(int, object) True >>> isinstance(int, object) True >>> print type(int) >>> isinstance(int, type) True >>> issubclass(int, type) False >>> issubclass(type, object) True >>> isinstance(type, object) True >>> isinstance(object, type) True >>> -- http://mail.python.org/mailman/listinfo/python-list
Re: how to get the path of a module (myself) ?
Stef Mientki gmail.com> writes: > I don't seem to have pkg_utils, > only pkgutil, which doesn't have an apropiate function. > > But I found another way, don't know if that's reliable: > import inspect > print inspect.currentframe().f_code.co_filenameE Eeek. > head -999 foo/* ==> foo/__init__.py <== import pkg_resources print pkg_resources.resource_string(__name__, "bar") ==> foo/bar <== bar > python -c "import foo" bar Make a "def load_resource" from that print line and you're done. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python IDE
On the subject can somebody here who uses SPE (or just has some python knowledge) help me out with the installation process? I tried following http://spe.pycs.net/extra/manual/manual.html#windows but end up with the error: python /c/system/python24/Lib/site-packages/_spe/winInstall.py Traceback (most recent call last): File "/c/system/python24/Lib/site-packages/_spe/winInstall.py", line 1, in ? import info File "/c/system/python24/Lib/site-packages/_spe/info.py", line 1, in ? import os,sys,sm.osx ImportError: No module named sm.osx interestingly enough there is an osx.py in PYTHON_DIR/Lib/site-packages/sm so I figure I may just have python configured poorly somehow. Any ideas on where I should look? -- http://mail.python.org/mailman/listinfo/python-list
Re: Python IDE
On the subject can somebody here who uses SPE (or just has some python knowledge) help me out with the installation process? I tried following http://spe.pycs.net/extra/manual/manual.html#windows but end up with the error: python /c/system/python24/Lib/site-packages/_spe/winInstall.py Traceback (most recent call last): File "/c/system/python24/Lib/site-packages/_spe/winInstall.py", line 1, in ? import info File "/c/system/python24/Lib/site-packages/_spe/info.py", line 1, in ? import os,sys,sm.osx ImportError: No module named sm.osx interestingly enough there is an osx.py in PYTHON_DIR/Lib/site-packages/sm so I figure I may just have python configured poorly somehow. Any ideas on where I should look? -- http://mail.python.org/mailman/listinfo/python-list