prevent memory leak in C+Python

2006-03-20 Thread Uwe Mayer
Hi,

I am wrapping a C function returning large amount of binary data back to
Python using SWIG. 

I have the data malloc()ed or new()ed on the heap and buffer objects seem a
good way to wrap it in python
(http://docs.python.org/api/bufferObjects.html)

>From the documentation it seems PyBuffer_FromReadWriteObject() will not
release the allocated shared memory. How do I prevent a memory leak in this
case?

Thanks,
Ciao
Uwe
-- 
Everything you read in newspapers is absolutely true, except for that
rare story of which you happen to have first-hand knowledge.
-- Erwin Knoll

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


Re: Bug on Python2.3.4 [FreeBSD]?

2005-08-13 Thread Uwe Mayer
Saturday 13 August 2005 06:18 am Donn Cave wrote:

> Of course, but the question was, where do reads start?  I would
> guess the GNU C library "innovated" on this point.  But in the
> end it doesn't really matter unless Python is going to try to
> square that all up and make open() consistent across platforms.

Personally, I think Python should unify this difference across plattforms
and explicitly document the behaviour in the library reference.
But I don't know how a decision on that is going to be formed.

I will forward a request for taking this point up into the library
reference's file() function, perhaps a footnote pointing out the
end-of-file position on BSD and start-of-file on Linux.

Uwe

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


Re: Bug on Python2.3.4 [FreeBSD]?

2005-08-12 Thread Uwe Mayer
Friday 12 August 2005 22:12 pm David Bolen wrote:
> Which version of FreeBSD are you running?  I thought it might be a
> dependency on needing to seek between reads and writes on a duplex
> stream (which is ANSI), but FreeBSD doesn't require that, at least
> back as far as a 4.7 system I have, and I assume much earlier than
> that.
> 
> One dumb question - are you absolutely sure it wasn't appending?  As
> written, there's no trailing newline on the file, so your final "cat
> test" would produce output where the "testing" was on the same line as
> your next command prompt, and can sometimes be missed visually.
> 
>> Can anyone confirm that? Is there any other way of opening a file for
>> appending instead of a+?
> 
> Well, if you really just want appending, I'd just use "a".  It creates
> the file if necessary but always appends to the end.  Of course, it's
> not set up for reading, but you wouldn't need that for appending.

I was debugging an application I released, on a forreign FreeBSD system:
$ uname -a
FreeBSD hephaistos 4.11-RELEASE FreeBSD 4.11-RELEASE #0: Sun Feb 27 21:09:39
CET 2005 [EMAIL PROTECTED]:/storage/obj-4.11/usr/src/sys/OLYMPUS  i386

The application as able to create and write to files, but not to open and
read them.  It seems the file poitner is positioned at the end of a file
when using:

>>> f = open('', 'a+')

on FreeBSD, but at the beginning of the file on Linux (at least on my Debian
unstable, Python 2.3.4 and 2.3.5; its running on Gentoo Linux and Suse,
too, though I don't know the Python version numbers).
I was assuming the fp was set to 0 in this mode, thus the app was not
working on above FreeBSD and the owner set up a login for me to debug the
situation.

The problem is easyly fixed by appending a f.seek(0) after opening the file,
but the position of the fp is strange.

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


Re: Bug on Python2.3.4 [FreeBSD]?

2005-08-12 Thread Uwe Mayer
Friday 12 August 2005 22:12 pm paolino wrote:
[...]
>f = open('test', 'a+')
>f.read()
>> 
>> ''
>> 
>> -> append mode does not read from file, *not ok*
>> 
>> 
> This is right IMO 'a' is appending so seek(-1)

True, thank you.
f.tell() shows the file pointer is at EOF. On my Debian Linux (unstable),
Python 2.3.4 +2.3.5, however, the file pointer is at the beginning of the
file.
Is that behaviour intended?

Ciao
Uwe

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


Bug on Python2.3.4 [FreeBSD]?

2005-08-12 Thread Uwe Mayer


Hi,

AFAICT there seems to be a bug on FreeBSD's Python 2.3.4 open function. The
documentation states:

> Modes 'r+', 'w+' and 'a+' open the file for updating (note that 'w+'
> truncates the file). Append 'b' to the mode to open the file in binary
> mode, on systems that differentiate between binary and text files (else it
> is ignored). If the file cannot be opened, IOError is raised.   

Consider:

$ cat test
lalala

$ python2.3
Python 2.3.4 (#2, Jan  4 2005, 04:42:43)
[GCC 2.95.4 20020320 [FreeBSD]] on freebsd4
Type "help", "copyright", "credits" or "license" for more information.
>>> f = open('test', 'r+')
>>> f.read()
'lalala\n'
>>> f.write('testing')
>>> f.close()
>>>
[1]+  Stopped python2.3
$ cat test
lalala

-> write did not work; ok

$ fg
python2.3
>>> f = open('test', 'a+')
>>> f.read()
''

-> append mode does not read from file, *not ok*

>>> f = open('test', 'w+')
>>> f.read()
''
>>>
$ cat test

-> w+ truncated file, ok


Can anyone confirm that? Is there any other way of opening a file for
appending instead of a+? 

Thanks,
Ciao
Uwe

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


super problem

2005-06-25 Thread Uwe Mayer
Hi,

I have a diamond-shaped multiple inheritanc chain with new style classes,
but super() does not call the parent class correctly:

-- snip --
from qtcanvas import *

class B2(QCanvasItem):
def move(self, x,y):
super(B2, self).move(0,0)
print "B2"

class C2(QCanvasItem):
def move(self, x,y):
super(C2, self).move(0,0)
print "C2"

class D2(QCanvasPolygonalItem, B2, C2):
def move(self, x,y):
super(D2, self).move(0,0)
print "D2"

d2 = D2(None)
d2.move(0,0)
-- snip --
returns:

Traceback (most recent call last):
  File "tmp.py", line 46, in ?
d2.move(0,0)
  File "tmp.py", line 42, in move
super(D2, self).move(0,0)
  File "tmp.py", line 32, in move
super(B2, self).move(0,0)
  File "tmp.py", line 37, in move
super(C2, self).move(0,0)
AttributeError: 'super' object has no attribute 'move'

super(C2, self).move(0,0) should resolve to QCanvasItem.move
AFAIK super only works with new-style classes, so I checked:

>>> from qtcanvas import *
>>> isinstance(QCanvasItem, object)
True


A way to fix this is to introduce a dummy 

class A2(QCanvasItem):
def move(self,x,y): QCanvasItem.move(self,x,y)

This is stupid, as it only delays execution by one intermediary call.


Is this a bug?

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


Life of Python

2005-06-25 Thread Uwe Mayer
Hi,

I have come across the following statement a number of times:

http://mail.python.org/pipermail/python-list/2003-July/171805.html
[... how to enforce pure abstract class ...]
> Python, in general, doesn't try to stop the programmer doing things, the
> way many other languages do.  This is known in the community as the
> "we're all consenting adults" philosophy.

I have a split opinion on that: 

pro: If you're writing smaller apps and everything stays rather clearly laid
out you don't need to care about abstract, virtual, accessor functions,
private, public, interfaces, etc

con: If you are planning larger applications (for a reasonable value of
"large") you have to discipline yourself to write well structured code.
Then you will want to specify interfaces, accessor functions with different
read /write access, ...
Unless you have designed the software interactions completely bevorehand
(which never works out) this is the only way to incorporate changes without
refactoring your source all the time.

Therefore I come to the conclusion that a general purpose language like
Python may well allow tampering with name-mangeling, dynamic method
resolution, whatever, BUT it should also provide facilities to allow
enforcing a more structured approach.

This is why I welcome i.e. the decorator support in Python 2.4. And I think
this should be expanded further, i.e. more build-in decorators for
interfaces, abstract classes, parameter and return value restrictions.
You can, but you must not; and I use both, depending on the project I'm
working on.

IMO this is a problem i.e. Perl is faceing: you can do even more rubbish
with Perl than with Python and since there is no way of forcing a
programmer to do it a certain way, its often easyer to rewrite Perl
programs over 400 lines than to fix them.

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


Re: Favorite non-python language trick?

2005-06-24 Thread Uwe Mayer
Friday 24 June 2005 09:18 am Enrico wrote:

[...]
>> --This code will, because the first two dashes make the rest a comment,
>> breaking the block
>> ---[[
>> print(10)
>> --]]
[...]

> python:
> 
> """
> print 10
> """
> 
> and
> 
> #"""
> print 10
> #"""
> 
> C++:
> 
> /*
> print(10);
> */
> 
> and
> 
> ///*
> print(10);
> //*/
> 
> ?

I think the *trick* here was that if you had larger blocks you'd only have
to change one side of the comment, i.e. the opening line, to de-comment the
block without searching the end of it and commenting that out aswell.

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


Re: don't understand MRO

2005-06-23 Thread Uwe Mayer
Thursday 23 June 2005 19:22 pm Terry Reedy wrote:

[...]
> In the absence of other information, I would presume that none of the
> other classes have a move() method.

move() is implemented in the class qtcanvas.QCanvasItem
I checked the pyqt sources and it is linked via sip to the C++ object file.
In C++, QCanvasItem.move is delegated to QCanvasItem.moveBy. 

-- snip: C++ sources --
void QCanvasItem::move( double x, double y ){
moveBy( x-myx, y-myy );
}

void QCanvasItem::moveBy( double dx, double dy ){
if ( dx || dy ) {
removeFromChunks();
myx += dx;
myy += dy;
addToChunks();
}
}
-- snip --

> Are you sure that QCanvasItem has a move method?  What results from
 print qtcanvas.QCanvasItem.move # ?
> If so, I would need to see its code to try to answer.

>>> import qtcanvas
>>> qtcanvas.QCanvasItem.move


Here is a working portion which recreates the strange output:

-- snip --
from qtcanvas import *

class Node(object):
def move(self, x,y):
print "Node: move(%d,%d)"%(x,y)

class Rhomb(QCanvasPolygon, Node):
def __init__(self, parent):
QCanvasPolygon.__init__(self, parent)
Node.__init__(self)

print Rhomb.mro()
r = Rhomb(None)
r.move(1,2)
-- snip --

This prints:

[, , , , , , , ]
Node: move(1,2)

Ciao
Uwe

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


don't understand MRO

2005-06-23 Thread Uwe Mayer
Hi,

I have a subclassed PyQt class: 

class Node(object):
def move(self, x,y): pass

class CRhomb(QCanvasPolygon, Node): pass

$ python
v2.4.1
>>> CRhomb.mro()
[, , , , , , , ]

>>> a = CRhomb()
>>> a.move(1,2)

This executes also Node.move(a, 1,2)
Why?

Because even QCanvasItem.move delegates the call to the derived object? But
qt.Qt does not have a move() method... how does it get passed on to Node?

Thanks in advance,
Ciao
Uwe 
-- 
http://mail.python.org/mailman/listinfo/python-list


AM_PATH_PYTHON - version problem

2005-06-01 Thread Uwe Mayer
Hi,

I use the GNU autotools for packaging my python applications. My problem is
that as I have both python2.3 and python2.4 installed the automake macros
always detects the newest version and sets it path variables accordingly. 

How can I package the program for different versions of Python? 
(i.e. for generating binary packages)

Any ideas?

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


Re: anonymous function objects?

2005-04-29 Thread Uwe Mayer
Friday 29 April 2005 05:40 am Michele Simionato wrote:

> Uwe Mayer:
>> Why does the "print" statement return a syntax error here?
> 
> Google for "Python regrets" where Guido admits that
> 'print' should have been a function.

:) Will this change in future - and if not, why not?

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


Re: anonymous function objects?

2005-04-28 Thread Uwe Mayer
Friday 29 April 2005 00:06 am Paul Rubin wrote:
> Closest you can come is:
> 
>f = lambda: sys.stdout.write("hello world\n")
 
Ah. :))
Why does the "print" statement return a syntax error here?

>>> lambda: print("hallo")
  File "", line 1
lambda: print("hallo")
^
SyntaxError: invalid syntax

> Of course if you're trying to capture the function in a named variable
> like f, just use a def statement.

Unfortunately I want to assign a handler function to an object and something
like this does not work:

>>> class Foobar(object): pass
...
>>> a = Foobar()
>>> def a.handler():
  File "", line 1
def a.handler():
 ^
SyntaxError: invalid syntax

But wrapping print into a myprint function works :)


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


anonymous function objects?

2005-04-28 Thread Uwe Mayer
Is it possible to specify anonymous functions, something like:

>>> f = {print "hello world"}
>>> f()
hello world


in Pyton?
Lambda expressions don't work here.

Thanks,
Uwe

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


sscanf needed

2005-04-17 Thread Uwe Mayer
Hi,

I've got a ISO 8601 formatted date-time string which I need to read into a
datetime object.
Is there a shorter way than using regular expressions? Is there a sscanf
function as in C?

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


Re: singleton objects with decorators

2005-04-12 Thread Uwe Mayer
Tuesday 12 April 2005 18:51 pm Michele Simionato wrote:

> Uhm? If I pass different parameters I want to have
> different instances. The Singleton behaviour is recovered
> only when I pass always the same arguments, in
> particular when I pass 0-arguments:
> 
 class Foobar:
> ... __metaclass__ = Memoize
> ...
 Foobar()
> <__main__.Foobar object at 0xb7defbcc>
 Foobar()
> <__main__.Foobar object at 0xb7defbcc>
 Foobar()
> <__main__.Foobar object at 0xb7defbcc>
> 
> Of course if for Singleton you mean "whatever I pass
> to the constructor it must always return the same
> instance" then this pattern is not a Singleton.
> This is why I call it memoize ;)

:)

I guess it depends on what you want to do with the instance and the
constructor, wether it satisfies the condition to be a "singleton":

If you pass i.e. the timestamp of object creation in the constructor, or the
class name that instanciates the object, a memoized implementation would
not suffice.

On the other hand if you need parameterized implementations of a singleton,
i.e. for each colour 'red', 'green', 'blue' - then a memoized
implementation would be better.

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


Re: singleton objects with decorators

2005-04-12 Thread Uwe Mayer
Tuesday 12 April 2005 17:00 pm Michele Simionato wrote:

> I did not put memoize on __new__. I put it on the metaclass __call__.
> Here is my memoize:
> 
>  def memoize(func):
>  memoize_dic = {}
>  def wrapped_func(*args):
>  if args in memoize_dic:
>  return memoize_dic[args]
>  else:
>  result = func(*args)
>  memoize_dic[args] = result
>  return result
>  wrapped_func.__name__ = func.__name__
>  wrapped_func.__doc__ = func.__doc__
>  wrapped_func.__dict__ = func.__dict__
>  return wrapped_func
> 
>  class Memoize(type): # Singleton is a special case of Memoize
>  @memoize
>  def __call__(cls, *args):
>  return super(Memoize, cls).__call__(*args)

I tried it out and found the following "inconsistency":

>>> class Foobar(object):
... __metaclass__ = Memoize
... def __init__(self, *args): pass
...
>>> Foobar(1)
<__main__.Foobar object at 0x4006f7ec>
>>> Foobar(2)
<__main__.Foobar object at 0x4006f7cc>
>>> Foobar(3)
<__main__.Foobar object at 0x4006f82c>

Unless I'm using it incorrectly (I haven't done much metaclass programming
yet) this is the same problem was with using @memoize on __new__ and
__init__.

Talking so much about singletons I'm not even sure what the definition on
calling a singleton with different constructor parameter values is.

Anyways, a fix for that could be:

class SingletonFactory(type):
single = {}
def __call__(cls, *args):
if (cls not in SingletonFactory.single):
SingletonFactory.single[cls] = super(SingletonFactory,
cls).__call__(*args)
return SingletonFactory.single[cls]


i.e. not caching the parameter values or types.

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


Re: singleton objects with decorators

2005-04-12 Thread Uwe Mayer
Tuesday 12 April 2005 17:00 pm Michele Simionato wrote:

> I did not put memoize on __new__. I put it on the metaclass __call__.
> Here is my memoize:
[...] 

Clever, thanks! :)

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


Re: singleton objects with decorators

2005-04-12 Thread Uwe Mayer
Tuesday 12 April 2005 14:51 pm Michele Simionato wrote:

> No. Not everybody knows about Singleton. It is an acquired knowledge.

Well, what isn't? 
What I ment to say, but failed to do so more explicitly, was that it is a
term I felt which was generally known to "the programming society". Or that
it is a term that might pop up in a comunity where software design is an
issue.
This of course does not mean that you could not have used it without knowing
that someone had thought of a fancy name for it. 

[...]
> It was only after many months that I understood that there was really
> nothing
> more to it. 

I agree. There really isn't much to Singletons. There was a time when I also
did not know about Singletons or design pattern in general. But thats
always the case somehow, somewhere. 

[...] 
> In my lectures at Oxford next week I have carefully expunged anything
> referring to Singletons ;)

Perhapts you shouldn't. Otherwise people might stumble over the same problem
you did. 

> IMNSHO, the property "there is only one" is not important enough to
> deserves
> a name. 

The problem here is: it has already a name. It is the probably most simple
pattern there is. And if only it serves as a mind-boggingly example of a
design pattern, why not call it a "singleton". Why call it a "class that
can only be instanciated once"?
I do not yet feel there is being made too much fuss about it. People do not
start name-dropping on singletons. Design pattern in general, perhaps. 

> The property "if you have already called this callable with the 
> same arguments you don't need to recompute it" instead is quite worthy
> and subsumes the other concept as a special case. It also gives you
> plenty of use case to illustrate it, even to beginner programmers.

To step in your argument, you could also call that "caching a function
call".

BTW: @memoize on the __new__ method isn't quite enough. You'll have to call
it on __init__ as well, otherwise it is executed again on the already
initialised object.
Also calling @memoize on the __init__ does not suffice either, because can
call it with different parameters...

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


Re: singleton objects with decorators

2005-04-12 Thread Uwe Mayer
Tuesday 12 April 2005 12:09 pm Michele Simionato wrote:

> Steven Bethard:
>> It strikes me that I've never wanted or needed a singleton object.
>> Would you mind sharing your use case?  I'm just curious.
> 
> "Singleton" is the most idiotic pattern ever. If you want an instance,
> just
> instantiate your class once. If a class should have only one instance,
> you can just document it. What I find usuful is "memoize", which
> contains "Singleton" as a special case. So I use memoize even
> for singleton would-be, i.e. logfiles and databases connections
> (memoizing the connections, if I try to open a database twice with the
> same parameters, I am returned an instance of the already opened
> database).

"Singleton" is simple (like the wheel), but that does not make it stupid.
There are two aspects that are important:

1. a Singleton has one, very simple property and virtually everyone knows
what you talk about when you explain that you used a "Singleton". In this
case its just a technical term. We need technical terms.

2. the property of a Singleton, i.e. there is only one, is important - you
use it yourself through memoize. That is just a more flexible
implementation of having one instance of whatever you memoize.

Using @memoize on the __new__ method works very well and is flexible enough
to be used with any function call. Thanks for the tip.

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


Re: singleton objects with decorators

2005-04-12 Thread Uwe Mayer
Tuesday 12 April 2005 10:01 am Steven Bethard wrote:
>> I am using a class to manage configuration settings in an application.
>> This object should only existe once so that when the user
>> changes a setting through a configuration dialog the change imminent in
>> all locations where access to config settings are needed.
 
> Ahh, I see.  I would typically just use a module in this situation,
> where the configuration settings were just names global to the module.
> Is there a benefit to using a singleton object over using just a module?

Basically I am using a module. The config file is stored in
$HOME/./.conf where the user can go and edit it. It is a working
python program which globally declares variables.
I cannot simply import this module as it does not lie in the path and I am
not too fond of dynamically cluttering sys.path to my needs. My intend was
to make it look as little complicated as possible (for the user) while
retaining maximum flexibility.

Therefore I wrote a class, subclassing a dict, which executes the config
file and takes up the variables into its namespace. Afterwards I update the
dict with default-values (in case the user kicked some vars out) or
manipulate the read-in config variables, in case the format changed. 

When the application closes the Singleton is written back to the config file
in the user home by using the PrettyPrinter and substituting variables in a
rather longer descriptive text. That way the user always has a nicely
formatted, descriptive configuration file - I thought that was rather
straight forward. :/

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


Re: singleton objects with decorators

2005-04-12 Thread Uwe Mayer
Tuesday 12 April 2005 06:36 am Steven Bethard wrote:
> Uwe Mayer wrote:
>> I've been looking into ways of creating singleton objects.
> 
> It strikes me that I've never wanted or needed a singleton object.
> Would you mind sharing your use case?  I'm just curious.

I am using a class to manage configuration settings in an application. This
object should only existe once so that when the user
changes a setting through a configuration dialog the change imminent in all
locations where access to config settings are needed.

I was using a factory function bevore, but since I am starting to use Python
2.4 and I remembered having read about a singleton-decorator I went to look
deeper into the possibilities.

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


singleton objects with decorators

2005-04-11 Thread Uwe Mayer
Hi,

I've been looking into ways of creating singleton objects. With Python2.3 I
usually used a module-level variable and a factory function to implement
singleton objects.

With Python2.4 I was looking into decorators. The examples from PEP 318
http://www.python.org/peps/pep-0318.html#examples

don't work - AFAIK because:
- you cannot decorate class definitions (why was it left out?)
- __init__ must return None


However, you can use the decorator:

def singleton(f):
instances = {}
def new_f(*args, **kwargs):
if (f not in instances):
instances[f] = f(*args, **kwargs)
return instances[f]
new_f.func_name = f.func_name
new_f.func_doc = f.func_doc   
return new_f

with a class that overwrites the __new__ methof of new-style classes:

class Foobar(object):
def __init__(self):
print self

@singleton
def __new__(self):
return object.__new__(Foobar)


Is this particularly ugly or bad?

Thanks for comments,
Ciao
Uwe 
-- 
http://mail.python.org/mailman/listinfo/python-list


unknown encoding problem

2005-04-08 Thread Uwe Mayer
Hi,

I need to read in a text file which seems to be stored in some unknown
encoding. Opening and reading the files content returns:

>>> f.read()
'\x00 \x00 \x00<\x00l\x00o\x00g\x00E\x00n\x00t\x00r\x00y\x00...

Each character has a \x00 prepended to it. I suspect its some kind of
unicode - how do I get rid of it? 

str.replace('\x00', '') "works" but is not really nice. I don't quite get
the hang of str.encode /str.decode

Any Ideas?
Thanks
Ciao
Uwe 
-- 
http://mail.python.org/mailman/listinfo/python-list


changing from python2.3 to python2.4

2005-04-08 Thread Uwe Mayer
I've written a little application which uses the bang-line 
#!/usr/bin/python

to call the interpreter. As python 2.4 comes distributed with other distroes
this breaks my application as its modules are installed
in /usr/local/lib/python2.3/site-packages/... and python2.4 does not look
there.

How do you suggest dealing with this:
- is calling /usr/bin/python2.3 in the bang-line problematic?
- installing into both python2.3 and python2.4 
- rebuilding (re- ./configure, make, make install) the app solves the
problem

Whats the usual way to deal with this?

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


high resolution time needed

2005-02-21 Thread Uwe Mayer
Hi,

I need a function returning a time value with a higher resolution that the
standard 1sec unix timestamp. I found the clock() function in the time
module, but that seems to return the same value (in the Python shell) each
time I call it (Debian Linux speaking here). 

Any ideas?

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


Re: example needed: sip + Qt

2005-01-28 Thread Uwe Mayer
Saturday 29 January 2005 00:23 am Phil Thompson wrote:

> So have you created the library you are trying to wrap? The documentation
> is describing how to wrap a library and describes a fictional library as
> the basis for the example.

No, the extracts from before are from the code example of the fictional
library with no further implementation. 

By now I also found the problem: the poor python programmer did not care to
create the makefile (for building hello.cpp and hello.h) with qmake and
thus there were no moc files and that stupid programmer (me) could not link
against them. 

It compiles and loads in python now. 
Thanks
Uwe 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: example needed: sip + Qt

2005-01-28 Thread Uwe Mayer
Friday 28 January 2005 23:39 pm Uwe Mayer wrote:

> Friday 28 January 2005 23:18 pm Uwe Mayer wrote:
>> Traceback (most recent call last):
>>   File "", line 1, in ?
>> ImportError: ./hello.so: undefined symbol: _ZTV5Hello
>> 
>> $ c++filt _ZTV5Hello
>> vtable for Hello
>> 
>> The compilation did not give any warnings or error messages. Any ideas?
> 
> $ ldd -d hello.so
> libqt-mt.so.3 => /usr/lib/libqt-mt.so.3 (0x40057000)
> libXext.so.6 => /usr/X11R6/lib/libXext.so.6 (0x4190c000)
> libX11.so.6 => /usr/X11R6/lib/libX11.so.6 (0x4154e000)
> libpthread.so.0 => /lib/tls/libpthread.so.0 (0x41173000)
> libstdc++.so.5 => /usr/lib/libstdc++.so.5 (0x40743000)
> libm.so.6 => /lib/tls/libm.so.6 (0x4114f000)
> libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x407fd000)
> libc.so.6 => /lib/tls/libc.so.6 (0x41019000)
> libfontconfig.so.1 => /usr/lib/libfontconfig.so.1 (0x418d8000)
> libaudio.so.2 => /usr/lib/libaudio.so.2 (0x40807000)
> libXt.so.6 => /usr/X11R6/lib/libXt.so.6 (0x4191c000)
> libpng12.so.0 => /usr/lib/libpng12.so.0 (0x4081c000)
> libz.so.1 => /usr/lib/libz.so.1 (0x41856000)
> libXrender.so.1 => /usr/lib/libXrender.so.1 (0x41749000)
> libXrandr.so.2 => /usr/X11R6/lib/libXrandr.so.2 (0x40842000)
> libXcursor.so.1 => /usr/lib/libXcursor.so.1 (0x41734000)
> libXft.so.2 => /usr/lib/libXft.so.2 (0x416e1000)
> libfreetype.so.6 => /usr/lib/libfreetype.so.6 (0x42487000)
> libSM.so.6 => /usr/X11R6/lib/libSM.so.6 (0x42583000)
> libICE.so.6 => /usr/X11R6/lib/libICE.so.6 (0x4256a000)
> libdl.so.2 => /lib/tls/libdl.so.2 (0x41184000)
> /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x8000)
> libexpat.so.1 => /usr/lib/libexpat.so.1 (0x40847000)
> undefined symbol: _ZTV5Hello(./hello.so)
> undefined symbol: _ZNK5Hello9classNameEv(./hello.so)
> undefined symbol: _ZN5Hello7qt_castEPKc (./hello.so)
> undefined symbol: _ZN5Hello9qt_invokeEiP8QUObject   (./hello.so)
> undefined symbol: _ZN5Hello7qt_emitEiP8QUObject (./hello.so)
> undefined symbol: _ZN5Hello11qt_propertyEiiP8QVariant   (./hello.so)
> undefined symbol: _ZTI5Hello(./hello.so)

> undefined symbol: _Py_NoneStruct(./hello.so)
> undefined symbol: PyCObject_Type(./hello.so)

Those last two can be removed if you link against libpython2.3.so and by
appending -lpython2.3 to the libs in the Makefile.

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


Re: example needed: sip + Qt

2005-01-28 Thread Uwe Mayer
Friday 28 January 2005 23:18 pm Uwe Mayer wrote:
> Traceback (most recent call last):
>   File "", line 1, in ?
> ImportError: ./hello.so: undefined symbol: _ZTV5Hello
> 
> $ c++filt _ZTV5Hello
> vtable for Hello
> 
> The compilation did not give any warnings or error messages. Any ideas?

$ ldd -d hello.so
libqt-mt.so.3 => /usr/lib/libqt-mt.so.3 (0x40057000)
libXext.so.6 => /usr/X11R6/lib/libXext.so.6 (0x4190c000)
libX11.so.6 => /usr/X11R6/lib/libX11.so.6 (0x4154e000)
libpthread.so.0 => /lib/tls/libpthread.so.0 (0x41173000)
libstdc++.so.5 => /usr/lib/libstdc++.so.5 (0x40743000)
libm.so.6 => /lib/tls/libm.so.6 (0x4114f000)
libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x407fd000)
libc.so.6 => /lib/tls/libc.so.6 (0x41019000)
libfontconfig.so.1 => /usr/lib/libfontconfig.so.1 (0x418d8000)
libaudio.so.2 => /usr/lib/libaudio.so.2 (0x40807000)
libXt.so.6 => /usr/X11R6/lib/libXt.so.6 (0x4191c000)
libpng12.so.0 => /usr/lib/libpng12.so.0 (0x4081c000)
libz.so.1 => /usr/lib/libz.so.1 (0x41856000)
libXrender.so.1 => /usr/lib/libXrender.so.1 (0x41749000)
libXrandr.so.2 => /usr/X11R6/lib/libXrandr.so.2 (0x40842000)
libXcursor.so.1 => /usr/lib/libXcursor.so.1 (0x41734000)
libXft.so.2 => /usr/lib/libXft.so.2 (0x416e1000)
libfreetype.so.6 => /usr/lib/libfreetype.so.6 (0x42487000)
libSM.so.6 => /usr/X11R6/lib/libSM.so.6 (0x42583000)
libICE.so.6 => /usr/X11R6/lib/libICE.so.6 (0x4256a000)
libdl.so.2 => /lib/tls/libdl.so.2 (0x41184000)
/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x8000)
libexpat.so.1 => /usr/lib/libexpat.so.1 (0x40847000)
undefined symbol: _ZTV5Hello(./hello.so)
undefined symbol: _ZNK5Hello9classNameEv(./hello.so)
undefined symbol: _ZN5Hello7qt_castEPKc (./hello.so)
undefined symbol: _ZN5Hello9qt_invokeEiP8QUObject   (./hello.so)
undefined symbol: _ZN5Hello7qt_emitEiP8QUObject (./hello.so)
undefined symbol: _ZN5Hello11qt_propertyEiiP8QVariant   (./hello.so)
undefined symbol: _ZTI5Hello(./hello.so)
undefined symbol: _Py_NoneStruct(./hello.so)
undefined symbol: PyCObject_Type(./hello.so)

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


Re: example needed: sip + Qt

2005-01-28 Thread Uwe Mayer
On Friday 28 January 2005 14:24 pm, Craig Ringer wrote:
> Out of curiosity, would this be for an extension module used in an
> embedded Python interpreter, or for plain extension module for use with
> a standalone interpreter?

I am writing an application program using Python and PyQt:
https://savannah.nongnu.org/projects/lmc/

At one point I am using a QListView but need my items to be sorted 
numerically. There is no function to set the sort-criterea and the solution 
to this is subclassing QListViewItem and overwrite the comparison functions. 
However, doing so in Python was awfully slow for a greater number of items, 
so I want to rewrite this in C++ and wrap it back to python.

Ciao
Uwe
-- 
Kilroe hic erat!


pgp3PoEGtLKbc.pgp
Description: PGP signature
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: example needed: sip + Qt

2005-01-28 Thread Uwe Mayer
On Friday 28 January 2005 14:47 pm, Phil Thompson wrote:
> > The QLabel example in the SIP reference manual yields syntax errors for
> > me.
>
> What syntax errors? If there is a documentation bug then I'll fix it.

I'll come to that a little later, that happened in the simple c++ word 
example. 

I am currently fiddling around with the QLabel example. I copied the example 
and tried it out. First problem was that the generated makefile does not 
attempt to compile the original hello.cpp sources. 

So I wrote a configure script and a Makefile.am for that. 
It was not clear to me what type of archive or object was needed, so I first 
tried creating a normal object file, then libtool archives and then normal 
archives. It turned out I needed a normal archive an the library search path 
has to be extended to include the current directory (-L.), i.e. where the 
libhello.a lies.
My only way of doing so was edditing the Makefile generated by configure.py. 
Is there a way of telling that  to configure.py?

So now the hello.cpp example compiles. I fire up python:
>>> import hello
Traceback (most recent call last):
  File "", line 1, in ?
ImportError: ./hello.so: undefined symbol: _ZTV5Hello

$ c++filt _ZTV5Hello
vtable for Hello

The compilation did not give any warnings or error messages. Any ideas?

Thanks
Uwe
-- 
It would seem that evil retreats when forcibly confronted.
  -- Yarnek of Excalbia, "The Savage Curtain", stardate 5906.5


pgprunF10Sowx.pgp
Description: PGP signature
-- 
http://mail.python.org/mailman/listinfo/python-list

example needed: sip + Qt

2005-01-28 Thread Uwe Mayer
Hi,

can someone provide me with a running example for subclassing QWidget (or
something similarly simple) in C++ and then creating SIP (4.x+) bindings
for in for Python (2.3+)?

I am looking for something I can start of with and work my way towards more
complicated stuff (with Qt). 

The QLabel example in the SIP reference manual yields syntax errors for me.
Same with:
http://www.pegasus.rutgers.edu/~elflord/unix/siptute/subclass_example.tgz

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


Re: make install with python

2005-01-21 Thread Uwe Mayer
Friday 21 January 2005 20:47 pm Reinhold Birkenfeld wrote:

[...]
> The regular way is to use distutils and a setup.py file (google for
> documentation).

Ok, thanks. The  document
described just what I was looking for. 

Any suggestions how I handle uninstallation? This was provided by automake
rather mechanically. I didn't find a section on that in the distutils
documentation... :(

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


make install with python

2005-01-21 Thread Uwe Mayer
Hi,

I am writing a Python application and use the GNU auto-tools to compile what
needs compilation (i.e. Qt's .ui files).
However, I don't know how to write an automake file that installs the main
file (lmc.py) and some library files (i.e. ClassA.py, ClassB.py) into the
appropriate directories. 

Any tips there?

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


accessing class variables of private classes

2005-01-16 Thread Uwe Mayer
Hi,

I need to access class variables of a class I'd like to make private:

i.e.
class __Bar(object):
  pass

class __Foo(__Bar):
  def __init__(self):
super(__Foo, self).__init__()

>>> __Foo()
Name Error: global name '_Foo__Foo' is not defined

Here I want to prevent the user of instanciating __Foo from outside of the
module.


i.e.
class __A:
  a_list = []
  def __init__(self):
__A.a_list.append(self)

>>> __A()
NameError: global name '_A__A' is not defined

Here I want to keep a list of instanciated objects of class __A, so I can
update internal values if they are changed.


Any ideas?

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


deleting from tarfile

2005-01-15 Thread Uwe Mayer
Hi,

is it possible to delete a file from a tar-archive using the tarfile module?

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


missing sys.setappdefaultencoding

2005-01-07 Thread Uwe Mayer
Hi,

well, I wrote a nice python program which won't work if the default encoding
has not been set from ascii to latin-1 or latin-15.

However, the command sys.setappdefaultencoding is missing on a Python
installation with Python 2.3.4 on Gentoo where it is present on Debian.

Any ideas how to deal with this?

Thanks in advance,
Ciao
Uwe 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: UserDict deprecated

2005-01-02 Thread Uwe Mayer
Saturday 01 January 2005 23:34 pm Steven Bethard wrote:

[...]
> If you implemented the file interface functions yourself, why do you
> want to inherit from file?

[...]

> But just inheriting from list won't make this work, will it?  Don't you
> want to do something like:

[...]

Right. I guess the thing I was looking for is an interface specification as
in Java's Interfaces. 
I could write an abstract base class, where all methods just raise
NotImplemented().

I don't know wether this is a good idea after all...:/

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


Re: UserDict deprecated

2005-01-01 Thread Uwe Mayer
Saturday 01 January 2005 22:48 pm Hans Nowak wrote:

> Uwe Mayer wrote:
> 
>> Why is the UserDict module is deprecated after Python 2.2. The
>> application of it I have in mind is, i.e. multiple inheritance from
>> "file" and "dic" - which is not possible.
 
> I am curious, what would you do with a class that derives from both file
> and dict?

I was writing a class that read /writes some binary file format. I
implemented the functions from the file interface such that they are
refering to records. However, the file format has some header fields and
I'd wanted to grant access to those via the dict-interface.

Another example: working with PyQt I have an instance of a QListView and
wanted to use the list-interface to get and set individual records.

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


UserDict deprecated

2005-01-01 Thread Uwe Mayer
Hi,

Why is the UserDict module is deprecated after Python 2.2. The application
of it I have in mind is, i.e. multiple inheritance from "file" and "dic" -
which is not possible. 
If I used UserDict I would not need to specify all methods UserDict provides
alreay, anyway.

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


emulating python shell

2004-12-28 Thread Uwe Mayer
Hi,

in an application I want to provide direct access to the python interpreter
of the running program.
I use rawinput() and exec() to read an input string and a self-made function
to check wether the inputed block is closed and then execute it.

When running python interactively the result of the previous statement is
assigned to the variable "_" and commands like dir() just output their
results to stdout. 
However, executions of the sort:

exec("_ = "+command)

don't work.

Any ideas how to mimick python -i 's behaviour?

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


subclassing list

2004-12-23 Thread Uwe Mayer
Hi,

I want to subclass "list". The documentation states to prefer subclassing
list instead of UserList. How to you clear the contents of a list subclass
without creating a new object?

Thanks in advance
Uwe 
-- 
http://mail.python.org/mailman/listinfo/python-list


non blocking read()

2004-12-01 Thread Uwe Mayer
Hi,

I use select() to wait for a file object (stdin) to become readable. In that
situation I wanted to read everything available from stdin and return to
the select statement to wait for more.

However, the file object's read method blocks if the number of bytes is 0 or
negative. 

Is there no way to read everything a channel's got currently got without
blocking?

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