Re: In an inherited class, embedded classes is referenced?

2007-12-21 Thread Christian Joergensen
Diez B. Roggisch [EMAIL PROTECTED] writes:

[...]

 The more important question is: what do you need C for? Do you have by
 any chance a Java-background and think of C as inner/nested class as
 in Java? This feature doesn't exist in Python.

I was working on some framework code where each class would have a 
nested class containing meta information about the outer class 
(because all properties would be treated in a special way by the 
framework). 

I needed to inherit two classes and got confused when they didn't 
inherit their meta information - but referenced it :)

 Your workaround might be implementable using a metaclass in a more
 conveinient way, but I'm not sure-footed enough with metaclasses to
 provide a solution out of my head now.

That would be fun ;-)

-- 
Christian Joergensen | Linux, programming or web consultancy
http://www.razor.dk  | Visit us at: http://www.gmta.info
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: In an inherited class, embedded classes is referenced?

2007-12-21 Thread Christian Joergensen
Carl Banks [EMAIL PROTECTED] writes:

[...]

 No, when a class inherits a class member from a subclass, both classes
 reference the same object.  This is true of any object: classes,
 lists, sets, etc.  For instance, if you were to do this,

 class A(object):
 class C(object): pass
 d = [1,2,3,4]
 e = set((a,b,c,d))

 class B(A):
 pass


 Then you would find that

 A.C is B.C
 A.d is B.d
 A.e is B.e

 They are all the same object.

I see.

 Perhaps you are misled by the example methods?  Even them, the same
 function object is referenced by both classes.  The difference is,
 when accessing a method, a class doesn't return the function object
 itself, but a method object instead (which is a binding between a
 function and a class, used to set the value of self when calling
 it).

 But only functions do something like that, not classes.

Great explanation. This makes sense. I didn't think of it that way. 

[...]

 Metaclass programming, or at least some clever properties, would be
 required to do it automatically.  You could try something like this
 (untested) to automatically subclass any class variables that are
 instances of type:


 class AutoSubclassMetaclass(type):
 def __new__(cls,name,bases,clsdict):
 for key,value in clsdict.iteritems():
 if isinstance(value,type):
 clsdict[key] = type(value.__name__,(value,),{})
 type.__new__(cls,name,bases,clsdict)


 class A(object):
 __metaclasS__ = AutoSubclassMetaclass
 class C(object):
 foobar = 40

 class B(A):
 pass

Intriguing :-)

Thank you for your timed,

-- 
Christian Joergensen | Linux, programming or web consultancy
http://www.razor.dk  | Visit us at: http://www.gmta.info
-- 
http://mail.python.org/mailman/listinfo/python-list


In an inherited class, embedded classes is referenced?

2007-12-19 Thread Christian Joergensen
Hello

I stumpled upon this feature during my work tonight, and found it 
a bit confusing:

 class A(object):
... class C: 
... foobar = 42
... 
 class B(A): pass
... 
 A.C   
class __main__.C at 0xb7cf735c
 B.C
class __main__.C at 0xb7cf735c
 B.C.foobar = 60
 A.C.foobar  
60  

When I inherit B from A, I would expect that A.C and B.C would be two
different classes? But apparently not.

Can anyone give me an explanation? Or a better workaround than 
something along the line of:

 B.C = type(C, (object,), {'foobar': 60})

Instead of:

 B.C.foobar = 60

Thanks,

-- 
Christian Joergensen | Linux, programming or web consultancy
http://www.razor.dk  | Visit us at: http://www.gmta.info
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python FTP server down

2007-03-04 Thread Christian Joergensen
John Nagle [EMAIL PROTECTED] writes:

 ftp://ftp.python.org/pub/; is returning Connection Refused today.

True.

 I need FTP access to download onto a colocated server.

Is HTTP firewalled? If you have SSH-access, you could just do the HTTP 
download at your workstation and scp/sftp it to the remote server.

-- 
Christian Joergensen | Linux, programming or web consultancy
http://www.razor.dk  | Visit us at: http://www.gmta.info
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Superclass for Errors?

2006-12-27 Thread Christian Joergensen
tac-tics [EMAIL PROTECTED] writes:

 I have a program which has a GUI front-end which that runs a separate
 thread to handle all the important stuff. However, if there is a
 problem with the important stuff, I want the GUI to raise a MessageBox
 alert to indicate this.

 For exceptions, I can simply use a catch-all except statement like:

 try:
 ...
 except Exception, error:
 JOptionPane.showMessageDialog(self, Error: %s % error)

 Only, I want it to catch Errors as well. Right now, I'm using:

 try:
 ...
 except (Exception, TypeError, NameError, RuntimeError, AttributeError),
 error:
 JOptionPane.showMessageDialog(self, Error: %s % error)

 I was wondering if there is a superclass for TypeError, NameError,
 RuntimeError, AttributeError, etc.

See http://rgruet.free.fr/PQR25/PQR2.5.html#BuiltInExc

I would guess you're looking for StandardError.

-- 
Christian Joergensen | Linux, programming or web consultancy
http://www.razor.dk  | Visit us at: http://www.gmta.info
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is there a commas-in-between idiom?

2006-11-05 Thread Christian Joergensen
Ernesto García García [EMAIL PROTECTED] writes:

 Hi experts,

 it's very common that I have a list and I want to print it with commas
 in between. How do I do this in an easy manner, whithout having the
 annoying comma in the end?

 list = [1,2,3,4,5,6]
 print ','.join(map(str, list))
1,2,3,4,5,6

-- 
Christian Joergensen | Linux, programming or web consultancy
http://www.razor.dk  | Visit us at: http://www.gmta.info
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: finding the list of the matched strings

2006-11-05 Thread Christian Joergensen
[EMAIL PROTECTED] [EMAIL PROTECTED] writes:

 Hi, I have a list of strings. And I want to find the subset which
 matches a particular regular expression.

 import re
 ll = ('a', 'b', 's1', 's2', '3s')
 p = re.compile('^s.*')
 newList = filter(lambda s: p.match(s), ll)

 I suppose there should be simple function to do this in re module. Is
 there any?

 I searched google, but could not find one, may be for keywords were not
 perfect.

I dont believe there exists such a function. I would have written
it using a list comprehension.

 import re
 ll = ('a', 'b', 's1', 's2', '3s')
 p = re.compile('^s.*')
 newList = [s for s in ll if p.match(s)]
 newList
['s1', 's2']

-- 
Christian Joergensen | Linux, programming or web consultancy
http://www.razor.dk  | Visit us at: http://www.gmta.info
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Cannot import a module from a variable

2006-10-15 Thread Christian Joergensen
Jia Lu [EMAIL PROTECTED] writes:

 Hi all:

 I try to do things below:
import sys
 for i in sys.modules.keys():
   import i
 Traceback (most recent call last):
   File pyshell#67, line 2, in module
 import i
 ImportError: No module named i

 But it seems that import donot know what is i ? why?

Try using __import__(i) instead.

-- 
Christian Joergensen | Linux, programming or web consultancy
http://www.razor.dk  | Visit us at: http://www.gmta.info
-- 
http://mail.python.org/mailman/listinfo/python-list