Retain reference to a struct

2005-11-02 Thread andychambers2002
Hi All,

A C library I'm using has a number of functions that all require a
struct as an argument.  The example module shows how to make a new
Python Object from C code and I've seen other posts that recommend this
way of doing it.

In this case though, it would seem easier if I could create the object
in the Python code.  This would require storing a pointer to an
instance of the struct until a certain function is called.

I can get the pointer into the python code, but whenever I try to use
it to call another function, the module segfaults.  Can anyone suggest
why this is?

static PyObject *
libpyq_PQconnectdb(PyObject *self, PyObject *args)
{
PGconn *conn = PyMem_New(PGconn, sizeof(PGconn *));
conn = (PGconn *)PQconnectdb((const char*)
PyString_AS_STRING(args));

printf("%p", conn);
return PyLong_FromVoidPtr(conn) ;
}

static PyObject *
libpyq_PQfinish(PyObject *self, PyObject *args)
{
printf("%p", args);
return 1;
}

>>> import libpyq#works fine
>>> conn = libpyq.PQconnectdb#conn now a pointer
>>> libpyq.PQfinish(conn)#segfaults

I'm new to C but relatively experienced with Python.  I have a sneaky
suspiscion there's a good reason for not doing it this way but I
haven't seen a good explanation of why not yet.  If you know one,
please tell me.

Thanks,
Andy

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


DB API specification of .rowcount and/or execute

2005-11-14 Thread andychambers2002
Hi,

Should execute() be allowed to execute multiple operations?

e.g.

from dbi import *

conn = connect(database="test")
curs = conn.cursor()
curs.execute("""
   INSERT INTO test_table VALUES (1, 'one');
   INSERT INTO test_table VALUES(2, 'two');
   """)

If so, then given this execution should rowcount contain the value 2?

What if both a select statement and an insert/update statement exist in
a single call to execute?

Perhaps only the rows affected/returned by the last operation in a
given execute() should be given by rowcount?

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


Re: sqlite3 or mysqldb?

2006-08-17 Thread andychambers2002
> I was using mysqldb just because MySQL seems to be a pretty big
> standard, but now that sqlite3 is coming with Python 2.5, I might
> switch, since it seems to be easier to use.

Yes and No.  Sqlite takes less to configure and manage but you have to
consider your needs for concurrent processing.  If memory/disk space is
no object then I would stick to mysql.

If its learning SQL that you want, you should try postgres.  It has a
very
interesting "RULE" system that you can play with.

Regards,
Andy

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


Re: couple more questions about sqlite

2006-08-18 Thread andychambers2002

> 2. What's the difference between sqlite and pysqlite? Do you need both,
> just one, or is one an older version of the same thing?

To access your database from python you need both (or some alternative
to pysqlite)

> 3. What's the difference between the command line program called sqlite3
> and the module you would use with Python? (I know that the former let's
> you do normal database things without dealing with Python, but is it
> necessary if you are using Python to interact with the DB?)

slqite comes with a library which other programs can call, and a
command line interface
that you can use from the shell.  pysqlite needs the library but I
don't think it is possible
to get one without the other.

Regards,
Andy

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


Force sleep to ignore interrupts

2006-09-14 Thread andychambers2002
I've written a simple Timer class that allows you to extend it
and then implement onMinuteChange, onHourChange etc methods
which will be executed on each new minute/hour respectively.

It works as I want when used in the main application thread.
That is, when you hit Ctr + C, it stops running.  However, if
the class that subclasses it, also subclasses Thread, it breaks
in that hitting Ctrl + C interrupts the call to sleep which puts
the event loop out of sync with real time.

How can I force the sleep to stay asleep for a whole second?  The
source code is below.


from threading import Thread
from datetime import datetime
from time import sleep

new_minute = lambda t: t.second == 0
new_hour = lambda t: t.minute == 0 and new_minute(t)
new_day = lambda t: t.hour == 0 and new_hour(t)
new_week = lambda t: t.weekday() == 0 and new_day(t)
new_month = lambda t: t.day == 0 and new_day(t)

class Timer:

def run(self):
t = datetime.now()
diff = t.microsecond
delay = 1.00 - (0.01 * diff)
sleep(delay)
self.onSecondChange()
while True:
sleep(1)
t = datetime.now()
self.onSecondChange()
if new_minute(t): self.onMinuteChange()
if new_hour(t): self.onHourChange()
if new_day(t): self.onDayChange()
if new_week(t): self.onWeekChange()
if new_month(t): self.onMonthChange()

def onSecondChange(self): pass
def onMinuteChange(self): pass
def onHourChange(self): pass
def onDayChange(self): pass
def onWeekChange(self): pass
def onMonthChange(self): pass
def onYearChange(self): pass


if __name__ == '__main__':
class TestTimer(Timer, Thread):
def onSecondChange(self):
print "second elapse: %s" % datetime.now()
def onMinuteChange(self):
print "minute elapse : %s" % datetime.now()
def onHourChange(self):
print "hour elapse : %s" % datetime.now()
t = TestTimer()
t.start()

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


Re: Force sleep to ignore interrupts

2006-09-14 Thread andychambers2002
Jeremy Sanders wrote:

> [EMAIL PROTECTED] wrote:
>
> > It works as I want when used in the main application thread.
> > That is, when you hit Ctr + C, it stops running.  However, if
> > the class that subclasses it, also subclasses Thread, it breaks
> > in that hitting Ctrl + C interrupts the call to sleep which puts
> > the event loop out of sync with real time.
>
> Maybe you could install a signal handler to ignore ctrl+c, when required
>
> import signal
> signal.signal(signal.SIGINT, signal.SIG_IGN)

That worked nicely.  Thanks

Andy

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


Re: PyOpenGL pour python 2.5 ???

2006-09-25 Thread andychambers2002

Sébastien Ramage wrote:
> oh!
> sorry, I made some search on comp.lang.python and fr.comp.lang.python
> and finally I forgot where I was...
>
> My question is :
> how use pyopengl with python 2.5 ??
> it seems that pyopengl was stop on 2005

Have a look at this thread.  I think with pyopengl you'll have to get
the source and compile to get it to work on Python 2.5.  I'd be
surprised if development of this package has stopped completely though.
 If you give them some time, they will come up with a 2.5 release soon
enough.

http://groups.google.com/group/comp.lang.python/browse_thread/thread/4d100920de4f271a/09216ab1c36cf898?lnk=gst&q=upgrade+package+to+2.5&rnum=1#09216ab1c36cf898

Regards,
Andy

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


Re: Daemonizing python

2006-09-25 Thread andychambers2002

Andi Clemens wrote:
> Hi,
>
> what is the main difference of running a python program as a daemon or
> as a cronjob?
>
> I have written a program at work that checks all internet connections of
> our failover sites and saves the results in a MySQL-database.
> The whole program is made with django (a webframework) so I had to be
> sure that the "checking procedure" is done every 30 minutes.

With a daemon, you might be able to flag up problems earlier than the
periodic check would allow.  On the other hand, you *know* that cron
will run every 30 minutes.  Do you *know* that your daemon will not
stop running somehow?  I suppose I put more trust in cron than I do in
myself.

Regards,
Andy

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


classmethod and instance method

2006-02-02 Thread andychambers2002
Hi,

I'm trying to write a method that needs to know both the class name and
the  instance details

class A:

@classmethod
def meth(cls, self):
print cls
print self

a = A()
a.meth(a)

The above code seems to work as intended.  Could the same effect be
achieved using a second decorator in addition to the @classmethod.  I
tried

def instancemethod(self):
return self.meth(self)

@instancemethod
@classmethod
def meth(cls, self):
#do stuff

but it didn't work.  Any suggestions?

Regards,
Andy

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


Re: classmethod and instance method

2006-02-02 Thread andychambers2002
Yes that's better.  Didn't know about the __class__ attribute.  I
thought there must be a way to access this but couldn't find it in the
docs.

Thanks,

Andy

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


Re: Natural Language Date Processing.

2006-02-07 Thread andychambers2002
>From the docs for PHP's 'strtotime'

Parameters

time
The string to parse, according to the GNU Date Input Formats syntax.
Before PHP 5.0, microseconds weren't allowed in the time, since PHP 5.0
they are allowed but ignored.
...

It seems that the string to be parsed has to be provided in the GNU
date format.  One option would be to provide a function that calls out
to the the GNU date program with whatever string you want to parse.

I'm not aware of an existing function in Python that does this.

Regards,
Andy

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


Re: Running External Commands + Seeing when they are Finished

2006-05-27 Thread andychambers2002
from os import *

for cmd in ['adaware', 'spybot']:
system(cmd)

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


dynamic type changing

2006-05-27 Thread andychambers2002
I'm working on a "TempFile" class that stores the data in memory until
it gets larger than a specified threshold (as per PEP 42).  Whilst
trying to implement it, I've come across some strange behaviour.  Can
anyone explain this?

The test case at the bottom starts a TempFile at size 50 and prints its
type.  It then increases the size to the threshold at which point
"self" is changed to being a TemporaryFile.  It seems that the next
call correctly uses the write() method of TemporaryFile (since we don't
see "changing type" in the output).  However, type(tmp) still equals
TempFile.  Not only that, tmp can still access the method dummy() that
exists only in TempFile.

#!/usr/bin/env python
from StringIO import StringIO
import tempfile

class TempFile(StringIO, object):
"""A temporary file implementation that uses memory unless
   either capacity is breached or fileno is requested, at which
   point a real temporary file will be created and the relevant
   details returned
"""
def __init__(self, buffer, capacity):
"""Creates a TempFile object containing the specified buffer.
If capacity is specified, we use a real temporary file once the

file gets larger than that size.  Otherwise, the data is stored

in memory.
"""
self.capacity = capacity
if len(buffer) > capacity:
self = tempfile.TemporaryFile()
self.write(buffer)
else:
super(TempFile, self).__init__(buffer)

def dummy(self):
pass

def write(self, str):
self.seek(0, 2)  # find end of file
if((self.tell() + len(str)) >= self.capacity):
print "changing type"
flo = tempfile.TemporaryFile()
flo.write(self.getvalue())
self = flo
print type(self)
else:
super(TempFile, self).write(str)


print "testing tempfile:"
tmp = TempFile("", 100)
ten_chars = "1234567890"
tmp.write(ten_chars * 5)
tmp.dummy()
print "tmp < 100: " + str(type(tmp))
tmp.write(ten_chars * 5)
tmp.dummy()
print "tmp == 100: " + str(type(tmp))
tmp.write("the last straw")
tmp.dummy()
print "tmp > 100: " + str(type(tmp))

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


Re: dynamic type changing

2006-05-28 Thread andychambers2002
>> I'm working on a "TempFile" class that stores the data in memory until
>> it gets larger than a specified threshold (as per PEP 42).  Whilst
>> trying to implement it, I've come across some strange behaviour.  Can
>> anyone explain this?

>> The test case at the bottom starts a TempFile at size 50 and prints its
>> type.  It then increases the size to the threshold at which point
>> "self" is changed to being a TemporaryFile.

> Changed how ?-)

Just by being assigned with a TemporaryFile object.  I thought that if
you do

instance = TempFile()

that "instance" and "self" defined in the Class were the same thing so
that if you changed the class of self, the class of instance would also
change.

Thanks very much for your example.  It has solved my problem and helped
me understand a new pattern at the same time.

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


Re: generating random passwords ... for a csv file with user details

2006-05-28 Thread andychambers2002
import random

def rand_str(len):
chars = ''.join(['abcdefghijklmnopqrstuvwxyz',
 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
 '1234567890',
 '_+']) # plus whatever additional characters you
want
return ''.join([random.choice(chars) for i in range(len)])

print rand_str(10)

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