[newbie] using ElementTree, how to add doctype and xml pi

2008-03-19 Thread dave berk
Hi all

I have an svg file i'm creating on the fly. How do I add the doctype and xml
pi? They're not an element per se, and there is no function to add them. Am
I suppose to add them as elements after all?

I have something like this:

self.svgRoot = ET.Element("svg", xmlns=r'http://www.w3.org/2000/svg')
ET.SubElement(self.svgRoot, "g", transform="scale(1,-1)")



self.tree = ET.ElementTree(self.svgRoot)
...

thanks
dave
-- 
http://mail.python.org/mailman/listinfo/python-list

Regarding __slots__ and other stuff: A couple of questions

2008-03-27 Thread dave berk
I'm new to Python. I'm trying to understand the type/class/object(instance)
model. I have read here:
http://www.cafepy.com/article/python_types_and_objects/python_types_and_objects.html
and here:
http://www.cafepy.com/article/python_attributes_and_methods/python_attributes_and_methods.html
as well as here:
http://users.rcn.com/python/download/Descriptor.htm

There are still a couple of issues I'm unclear about. I'll be very happy if
one of you could help me.
*first:*
Why doesn't this works?

>>> class C(object):
... __slots__ = ['foo', 'bar']
... class_attr = 'class attribute'
...
>>> c = C()
>>> c.foo = 'foo'
>>> c.bar = 'bar'
>>> c.bar, c.foo
('bar', 'foo')
>>> c.__slots__
['foo', 'bar']
>>> c.__slots__.append('eggs')
>>> c.__slots__
['foo', 'bar', 'eggs']
>>> c.eggs = 'eggs'
Traceback (innermost last):
File "", line 1, in 
AttributeError: 'C' object has no attribute 'eggs'


*second:*
what happens here? Why can I write to spam using the class object but not
the using the instance?

>>> class C(object):
... __slots__ = ['foo', 'bar']
... class_attr = 'class attribute'
...
>>> C.spam = 50
>>> C.spam
50
>>> C.spam = 56
>>> c = C()
>>> c.spam = 55
Traceback (innermost last):
File "", line 1, in 
AttributeError: 'C' object attribute 'spam' is read-only

>>> C.spam = 55
>>> C.spam
55


*Third:*

class RevealAccess(object):
"""A data descriptor that sets and returns values
normally and prints a message logging their access.
"""

def __init__(self, initval=None, name='var'):
self.val = initval
self.name = name

def __get__(self, obj, objtype):
print 'Retrieving', self.name
return self.val

def __set__(self, obj, val):
print 'Updating' , self.name
self.val = val
class A(object):
def __init__(self):
self.x = RevealAccess(10, 'var "x"')
self.y = 5
class B(object):
x = RevealAccess(10, 'var "x"')
y = 5

>>> a = A()
>>> b = B()
>>> a.x
<__main__.RevealAccess object at 0x00BAC730>
>>> a.x = 55
>>> b.x
Retrieving var "x"
10
>>> b.x = 55
Updating var "x"
>>>

Why the descriptor works only when created as a static variable and not
as an instance variable?

I'm sure all this questions stem from my lack of understanding python object
model. But, please, what am I missing?

Thanks
Dave
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Regarding __slots__ and other stuff: A couple of questions

2008-03-27 Thread dave berk
sorry, forget all I wrote: I should have read this more thoroughly, it
explained it all
http://www.cafepy.com/article/python_attributes_and_methods/python_attributes_and_methods.htm

Descriptors
only works with type objects, not instance. and you can't write from an
instance object to a
class variable. You can only hide it. Also __slots__ works by creating
descriptors in the class and preventing creation of any atrributes in
the class instance, so everything is clear now

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

Re: Regarding __slots__ and other stuff: A couple of questions

2008-03-27 Thread dave berk
Didn't see your answer, Gabriel. Thanks, now I understand it even better.

On 3/27/08, dave berk <[EMAIL PROTECTED]> wrote:
>
> sorry, forget all I wrote: I should have read this more thoroughly, it
> explained it all
>
> http://www.cafepy.com/article/python_attributes_and_methods/python_attributes_and_methods.htm
>
>
> <http://www.cafepy.com/article/python_attributes_and_methods/python_attributes_and_methods.html>Descriptors
> only works with type objects, not instance. and you can't write from an
> instance object to a
> class variable. You can only hide it. Also __slots__ works by creating 
> descriptors in the class and preventing creation of any atrributes in the 
> class instance, so everything is clear now
>
> Dave
>
-- 
http://mail.python.org/mailman/listinfo/python-list