Re: How to get/set class attributes in Python

2005-06-12 Thread Axel Straschil
Hello!

 You can 'hide' you getsetters using a property attribute[1]:

For me, the property attribute is a beast:

class A(object):
def getP(self): return 'A'
p = property(getP)
class A2(A):
def getP(self): return 'A2'
a = A()
a2 = A2()
print a.getP(), a2.getP()
print a.p, a2.p

So, property is instance-level super() tool ;-)

Lg,
AXEL.
-- 
Gentoo? Debian? RedHat? SuSE? *BSD? Stop the distri-war, make little user!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Reusing object methods?

2005-04-30 Thread Axel Straschil
Hello!

Why not:

 class A:
  def a_lengthy_method(self, params):
   # do some work depending only on data in self and params

 class B(A): pass

?

Lg,
AXEL.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Multiple inheritance: Interface problem workaround, please comment this

2005-04-11 Thread Axel Straschil
Hallo!

 Look at the comment in the code! I have posted the decorate module in

Uuups, sorry, I'll RTFM myselfe *g*

Lg,
AXEL.
-- 
Aber naja, ich bin eher der Forentyp. Wolfibolfi's outing in 
http://www.informatik-forum.at/showpost.php?p=206342postcount=10
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Multiple inheritance: Interface problem workaround, please comment this

2005-04-09 Thread Axel Straschil
Hello!

 from decorate import decorate # see today thread on decorators for this

Gives me an ImportError: No module named decorate. I've got to donwload
that? (python 2.4)

Thanks,
AXEL.
-- 
Aber naja, ich bin eher der Forentyp. Wolfibolfi's outing in 
http://www.informatik-forum.at/showpost.php?p=206342postcount=10
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Multiple inheritance: Interface problem workaround, please comment this

2005-04-08 Thread Axel Straschil
Hello!

 (1) make A or B a mixin class that doesn't need __init__ called, or

Would be a solution for classes that just give functionality, no
data-structures. In that case, i would use functions, no classes ;-)

I've seen code where there are classes without init and the hope that
self has what they expect, not realy the blackbox oop should produce!

 (2) make class AB inherit from A and delegate to B (or vice versa)

As every multiple inheritance can be solved as single inheritance, if no
better solution for my hungry monster problem is found, this would be
the best way. Don't take my AB example as a real word example ;-)

Also think of to libraries with classes which have inheritance depth
five or more (that's what I've got). Somtimes you want to stick things
together without knowing the inheritance tree.

So, if there are ready class-modules, and I want to use them for
multiple inheritance, I've to rewrite the init's of classes in the
module!

For me, python is the best solution in oop-webprogramming (tried perl,
got ugly code, tried php, got php ;-), tried C++, takes too long for web
projects). The only thing I'm dislike is the super thing with multiple
inheritance, maby I shoud see pythons multiple inheritance as a nice to
have and not to use thing ;-)

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


Re: Can dictionary values access their keys?

2005-04-08 Thread Axel Straschil
Hello!

 If I have a simple dictionary, where the value is a class or function,
 is there an interface through which it can discover what its key is?

The key of a value may not be unique, so you can also get a tupe of
keys, like dict(a=1, b=1), the key's of 1 are a and b.

For unique values, I did something like that couple of weeks ago, the
thing you would need is the getKey thing, it's fast, but needs much
memory for big structures becouse I use two dicts.
If speed does not matter:

class ReverseDict(dict):
def get_keys(self, value):
keys = []
for k, v in self.items():
if v == value: keys.append(k)
return keys

class UniqueReverseDict(dict):

A dictionary that can resolute reverse: A object to a key. Both, the 
Key and
the Value must be unique.


def __init__(self, **kws):
super(UniqueReverseDict, self).__init__(kws)
self.__reverse = {}

def __setitem__(self, k, v):
super(UniqueReverseDict, self).__setitem__(k, v)
self.__reverse[v] = k

def __update_reverse(self):
self.__reverse.clear()
for k, v in self.items():
self.__reverse[v] == k

def has_value(self, v):
return self.__reverse.has_key(v)

def __delitem__(self, k):
self.__reverse[self[k]] 
super(UniqueReverseDict, self).__delitem__(k)

def clear(self):
self.__reverse.clear()
super(UniqueReverseDict, self).clear()

def copy(self):
return UniqueReverseDict(self)

def pop(self, k):
del self.__reverse[self[k]]
return self.pop(k)

def popitem(self, **kws): 
raise 'AxsPy.Misc.Structures.UniqueReverseDict', \
'NotImplemented'

def setdefault(self, **kws): 
raise 'AxsPy.Misc.Structures.UniqueReverseDict', \
'NotImplemented'

def update(self, **kws):
super(UniqueReverseDict, self).update(**kws)
self.__update_reverse()

def getKey(self, v): return self.__reverse[v]

Lg,
AXEL
-- 
Aber naja, ich bin eher der Forentyp. Wolfibolfi's outing in 
http://www.informatik-forum.at/showpost.php?p=206342postcount=10
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Can dictionary values access their keys?

2005-04-08 Thread Axel Straschil
Hello!

 thousands more entries. So we're talking about maybe a million+ total
 nested key:values. I don't know if that counts as large or not. I can't
 even guess how much k memory that is.

Mhh, maybe you should use a SQL-Database ;-)

Lg,
AXEL.
-- 
Aber naja, ich bin eher der Forentyp. Wolfibolfi's outing in 
http://www.informatik-forum.at/showpost.php?p=206342postcount=10
-- 
http://mail.python.org/mailman/listinfo/python-list


Multiple inheritance: Interface problem workaround, please comment this

2005-04-07 Thread Axel Straschil
Hello!

I'm working on an HTML/Cgi widget's class where multiple inheritance
well be sometime a great thing.

I solved all my problems for pythons multiple inheritance with this ng,
thaks to all again, but there is one think I still dislike:

class A(object):
def __init__(self, a=None, **__eat): 
print A
super(A, self).__init__()
class B(object):

def __init__(self, b=None, **__eat): 
print B
super(B, self).__init__()

class AB(A, B):
def __init__(self, a=None, b=None): 
super(AB, self).__init__(a=a, b=b)

ab = AB()

This looks (and I think is) correct, but I realy dislike the **__eat
stuff. As in python everything is virtual, I found no better solution to
do that. In my real world, i've got constucts like:
class A(object)
class B(A)
class AB(A,B)
(not realy  so ugly like that ;-), just to say I can work only with super to 
call __init__).
 
My problem: If you make a coding mistake, and the mistake does not give
a runtime error becouse **__eat is a hungry evil beast, it would be very
hard to debug ... think of a wrong written parameter!

So, here is my workaround, please comment this, if someone has a better
solution I would be glad:

class A(object):
def __init__(self, a=None, _do_eat=False, **__eat): 
if __eat and not _do_eat: raise I'm not hungry
print A
super(A, self).__init__()

class B(object):
def __init__(self, b=None, _do_eat=False, **__eat): 
if __eat and not _do_eat: raise I'm not hungry
print B
super(B, self).__init__()

class AB(A, B):
def __init__(self, a=None, b=None): 
super(AB, self).__init__(a=a, b=b, _do_eat=True)

ab = AB()

Thanks, 
AXEL.
-- 
Aber naja, ich bin eher der Forentyp. Wolfibolfi's outing in 
http://www.informatik-forum.at/showpost.php?p=206342postcount=10
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python IDE like NetBeans/Delphi IDE

2005-04-06 Thread Axel Straschil
Hello!

 WingIDE (commercial, slower than PythonWin but has many features)

You can use and reactivate a trial licence for WingIDE for a realy long
term, give it a try, i bought a licence last week and realy love it!

Lg,
AXEL.
-- 
Aber naja, ich bin eher der Forentyp. Wolfibolfi's outing in 
http://www.informatik-forum.at/showpost.php?p=206342postcount=10
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Generating RTF with Python

2005-03-31 Thread Axel Straschil
Hello!

 does anyone know of a high-level solution to produce RTF from Python=20
 (something similar to
 Reportlab for producing PDF)?

Spend hours of googeling and searching, also in this NG, about two
months ago. My conclusion is: On windwos, maybe you can include some
hacks with dll's, under linux, linux support for generating rtf is none,
and so is python's.

My workaround was: 
http://www.research.att.com/sw/download/
This includes an html2rtf converter, which I access from python via
popen and temporary files. Not high-level, not very sexy ... ;-(

Lg,
AXEL.
-- 
Aber naja, ich bin eher der Forentyp. Wolfibolfi's outing in 
http://www.informatik-forum.at/showpost.php?p=206342postcount=10
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Generating RTF with Python

2005-03-31 Thread Axel Straschil
Hello!

 I looked at this a while ago, which might be a starter.
 http://pyrtf.sourceforge.net/

Don't remember why I didn't spent much time on that. Sombody has
experience with pyrtf on an production project (is it stable ;-))

Lg,
AXEL.
-- 
Aber naja, ich bin eher der Forentyp. Wolfibolfi's outing in 
http://www.informatik-forum.at/showpost.php?p=206342postcount=10
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Calling __init__ with multiple inheritance

2005-03-29 Thread Axel Straschil
Hello!

 Also, lool at that:
 class Mother(object):
 def __init__(self, param_mother='optional', **eat):
 print 'Mother'
 class Father(object):
 def __init__(self, param_father='optional', **eat):
 print 'Father'
 class Child(Mother, Father):
 def __init__(self, **ham):
 super(Child, self).__init__(**ham)
 child = Child(param_mother=1, param_father=1)
 Father's init will not be called.
 Change Father/Mother.__init__() to call the superclass initializer. It may
 be counterintuitive, but it works.

OK, thanks, with the super(...).__init__() in Father/Mother it workes
and makes sense.

So, the last thing a *realy* don't like ist the
__init__(self, param, **ignore_the_rest) thing. 

Anyone had troubles with that, or should I cust take this as a python
way of thinking ... ;-), and getting used to that?

Thanks,
AXEL.
-- 
Aber naja, ich bin eher der Forentyp. Wolfibolfi's outing in 
http://www.informatik-forum.at/showpost.php?p=206342postcount=10
-- 
http://mail.python.org/mailman/listinfo/python-list


Calling __init__ with multiple inheritance

2005-03-28 Thread Axel Straschil
Hello!

Im working with new (object) classes and normaly call init of ther
motherclass with callin super(...), workes fine.

No, I've got a case with multiple inherance and want to ask if this is
the right and common case to call init:

class Mother(object):
def __init__(self, param_mother): print 'Mother'
class Father(object):
def __init__(self, param_father): print 'Father'
class Child(Mother, Father):
def __init__(self, param_mother, param_father): 
Mother.__init__(self, param_mother)
Father.__init__(self, param_mother)
child = Child(1, 2)

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


Re: Calling __init__ with multiple inheritance

2005-03-28 Thread Axel Straschil
Hello!

Thanks to all for the very interesting postings!

I came to the following:

For single inheritance, super is a nice tool if you will recfactoring
the class later. 

For multiple inheritance, if you want to use super, you have to have
very much knowledge of the classes you inheritance. For me, OOP is to
not have to have the deep inner knowledge of the classes I inheritance
from. 

Also, for multiple inheritance, if think 
Mother1.__init__(self, ...)
Mother2.__init__(self, ...)
Mother3.__init__(self, ...)
would be more clear to read then
super(MyClass).__init__(Maby this will do some magic thing)

Also, I realy dislike 
__init__(self, param, **eat_some_needless_stuff)

If I later extend my class and also add some parameters to __init__,
what will happen to the classes using the baseclass?

Also, lool at that:
class Mother(object):
def __init__(self, param_mother='optional', **eat): 
print 'Mother'
class Father(object):
def __init__(self, param_father='optional', **eat): 
print 'Father'
class Child(Mother, Father):
def __init__(self, **ham): 
super(Child, self).__init__(**ham)
child = Child(param_mother=1, param_father=1)

Father's init will not be called.

Thanks,
AXEL.
-- 
Aber naja, ich bin eher der Forentyp. Wolfibolfi's outing in 
http://www.informatik-forum.at/showpost.php?p=206342postcount=10
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Generating modul classes with eval

2005-02-03 Thread Axel Straschil
Hello!

 Note that we don't need eval anywhere.

Uuups, that looks realy cool! Thanks for that!

Im fooling around with generating html-tags. As there are only two kind
of html tags, one who can nest chields, and one who cant, i wantet to
play arround with something like:

I've got two base classes, _Tag and _ContainerTag (for tags which can
nest tags). Instead of getting an htmltag with _Tag(name='html'), I
want to have a class for each html-tag. So, I thought of creating that
classes dynamicly.

my now (nearly) working code is:

class _Tag(object):
def __init__(self, name, flags=None, **props):
[...]

class _ContainerTag(_Tag):
def __init__(self, name, contents=None, flags=None, **props):
super(_ContainerTag, self).__init__(name=name, flags=flags, 
**props)
self._contents = coalesce(contents, [])


_module_name = sys.modules[__name__]

class_dic = {}
class_dic['Br'] = _Tag
class_dic['Hr'] = _Tag
class_dic['Html'] = _ContainerTag
class_dic['Table'] = _ContainerTag

for class_name, class_base in class_dic.items():
class TmpClass(class_base):
def __init__(self, **props):
name = class_name.lower()
#super(TmpClass, self).__init__(name=name, **props)
class_base.__init__(self, name=name, **props)
setattr(_module_name, class_name, TmpClass)

br = Br()
print br
table = Table()
print table

br is printed OK, but for table, I get:
AttributeError: 'TmpClass' object has no attribute '_contents'
so, it seems that __init__ of _Tag is not called. 
If I try to do the commented line
super(TmpClass, self).__init__(name=name, **props)
instead of
class_base.__init__(self, name=name, **props)
I get:
 TypeError: super(type, obj): obj must be an instance or subtype of
 type
for print table, print br ist processed OK.


Thanks for help and your perfekt examples,
AXEL.

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


Re: Generating modul classes with eval

2005-02-03 Thread Axel Straschil
Hello!

 After the loop has finished, the global variable TmpClass will be bound to
 whatever class was created last, and the variable class_base will be bound
 to that the base class of that same TmpClass. Therefore only this last class
 is guaranteed to work as expected.

Great, now it workes!

_module_name = sys.modules[__name__]
def _tag_class_factory(name, base):
class T(base):
def __init__(self, **props):
super(T, self).__init__(name=name.lower(), **props)
setattr(_module_name, name, T)
class_dic = {}
class_dic['Br'] = _Tag
class_dic['Hr'] = _Tag
class_dic['Html'] = _ContainerTag
class_dic['Table'] = _ContainerTag
class_dic['Td'] = _ContainerTag
class_dic['Tr'] = _ContainerTag
for name, base in class_dic.items():
_tag_class_factory(name, base)
print Table(contents=[Tr(contents=[Br()])])

gives: tabletrbr//tr/table

Thanks,
AXEL.

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


Generating modul classes with eval

2005-02-02 Thread Axel Straschil
Hello!

I was fooling around with creating classes for a module with eval,
something like:

MyModule.py:

class Base:
init(self, name):
self._name = name

for myclass in ['A', 'B', 'C']:
code=class %s(Base):\n\tinit(self, name='%s')\n\t\tsuper(%s,
self).__init(name=name)\n%dict(myclass, myclass.lower(), myclass())
... codeop and eval stuff ...
a=A()
print a

that gives: class '__main__.A', but I want MyModule.A ;-)

Can someone give me a hint how to create classes in a module with eval
and codeop so that they exist like the code was written in?

Thanks, 
AXEL.

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


Re: Html or Pdf to Rtf (Linux) with Python

2004-12-17 Thread Axel Straschil
Hello!

 You might take a look at PyRTF in PyPI. It's still in beta,

I think PyRTF would be the right choice, thanks. Yust had a short look
at it.

Lg,
AXEL.
-- 
The key words MUST, MUST NOT, REQUIRED, SHALL, SHALL NOT, SHOULD,
SHOULD NOT, RECOMMENDED, MAY, and OPTIONAL in this document are to be
interpreted as described in RFC 2119 [http://ietf.org/rfc/rfc2119.txt]

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


Re: Html or Pdf to Rtf (Linux) with Python

2004-12-16 Thread Axel Straschil
Hello!

 I've been able to successfully get konqueror to generate a pdf from a
 html file via dcop. It's something along the lines of:

For that stuff, I'm using htmloc (http://www.htmldoc.org/).

Lg,
AXEL.
-- 
The key words MUST, MUST NOT, REQUIRED, SHALL, SHALL NOT, SHOULD,
SHOULD NOT, RECOMMENDED, MAY, and OPTIONAL in this document are to be
interpreted as described in RFC 2119 [http://ietf.org/rfc/rfc2119.txt]

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


Re: Html or Pdf to Rtf (Linux) with Python

2004-12-15 Thread Axel Straschil
Hallo!
However, our company's product, PDFTextStream does do a phenomenal job of 
extracting text and metadata out of PDF documents.  It's crazy-fast, has a 
clean API, and in general gets the job done very nicely.  It presents two 
points of compromise from your idea situation:
1. It only produces text, so you would have to take the text it provides and 
write it out as an RTF yourself (there are tons of packages and tools that do 
this).  Since the RTF format has pretty weak formatting capabilities compared
I've got the Input Source in HTML, the Problem ist converting from any to 
RTF. Please give me a hint where the tons of packages are.

Thanks,
AXEL.
--
The key words MUST, MUST NOT, REQUIRED, SHALL, SHALL NOT, SHOULD,
SHOULD NOT, RECOMMENDED, MAY, and OPTIONAL in this document are to be
interpreted as described in RFC 2119 [http://ietf.org/rfc/rfc2119.txt]
--
http://mail.python.org/mailman/listinfo/python-list


Re: Html or Pdf to Rtf (Linux) with Python

2004-12-14 Thread Axel Straschil
Hello!

Sorry Cameron, I was replying, now my folloup ;-):

 Are you trying to convert one document in particular, or automate the
 process of conveting arbitrary HTML documents?

I have an small CMS System where the customer has the posibility to view
certain Html-Pages as Pdf, the CMS ist Python based. I also thought
about
passing the Url to an external converter Script, but found nothing ;-(


 What computing host is available to you--Win*?  Linux?  MacOS?
 Solaris!?

Linux

 Is Word installed?

No.

 OpenOffice?

Yes.

 Why have you specified Python?

Becouse I like Python ;-)
The System behind generating the HTML-Code is written in Python.

Thanks,
AXEL.

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