Logging to HTTP or File

2008-02-27 Thread Robert Rawlins - Think Blue
Hello Chaps,

 

I'm after some advice on the best way to handle a logging job. Essentially I
have data which I'm looking to log to a webservice, it would make good sense
I think to use something like the HTTP handler for the python logging API.

 

The problem comes when the application isn't 'online' as it were, with no
network connection or if the webservice isn't currently available. In this
case I don't want to simply loose all the log data, I want it stored into a
file, I can then run hourly batch/cron tasks to upload to the service when
the system comes back online.

 

How would you manage this? I'm thinking about building my own custom log
handler for the task, which if unable to resolve the webservice will just
log to the file instead, I'm just not sure if there is a better way of
achieving this, what are your thoughts?

 

In addition to this, does anyone know which libararys the HTTP handler uses
to make its call? Is it a good one which will recognise if the system has no
network connection or if the server is temporarily unavailable? Is it
asynchronous?

 

Thanks for any advice guys,

 

Robert

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

Last 4 Letters of String

2008-02-21 Thread Robert Rawlins - Think Blue
Hello Guys,

 

I'm looking for a function which will give me the last 4 characters of a
given string. I'm sure it's a very simple task but I couldn't find anything
of it.

 

Any ideas?

 

Rob

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

Memory Issue

2008-02-17 Thread Robert Rawlins - Think Blue
Afternoon Guys,

 

I've got what I would consider to be a strange memory leak within an
application I've been working on. When the application is running the
consumed system memory creeps up slowly, getting higher and higher.

 

However, when looking at 'top' to display the memory allocation for all the
applications, the actual assigned memory to the 'python' process doesn't
appear to change, it consistently looks like:

 

2414 root  20   0  9516 6316 3148 S  0.0  2.5   0:03.62 python

 

However the currently used memory for the entire system, shown here:

 

Mem:256760k total,35588k used,   221172k free, 2096k buffers

 

Continues to escalate until the system runs out of memory. I can be pretty
sure that this is something to do with my application as whenever the app
isn't running then the memory usage doesn't increase.

 

An additional strangeness to this is that when my application is killed, the
memory doesn't drop back down again? Is that normal? My understanding of a
memory leak was that when the application dies the memory will be freed back
up again.

 

Can any offer any suggestions as to what is causing this problem? Is it
perhaps not my actual application but something I'm calling from within it?

 

Cheers,

 

Robert

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

IoC and MVC Frameworks

2008-02-01 Thread Robert Rawlins - Think Blue
Hello Guys,

 

I've been working on a little pet project for a while now, using it as a
learning exercise for Python and its starting to grow nicely, I'm now
looking into refactoring it a little in a more OO fashion and I've always
been a real fan of design patterns and all those fun kind of things.

 

I'm looking for your recommendations on some good IoC and MVC frameworks,
specifically those that don't just apply to web projects, as this is not
one. I've taken a look at SpringPython which seems fairly tidy, from my
experience with the original Spring framework in JAVA I always liked the way
in which it implemented, what are your thoughts on this? I'm most interested
in the dependency management elements and AOP opposed to the database side,
but I'd be interested to get your opinions on them all.

 

I'll be interested to hear from you guys,

 

Rob

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

RE: Get Available Functions

2008-01-28 Thread Robert Rawlins - Think Blue
Thanks for that Tim,

I'll have a play around with these functions later today and see what
happens, hopefully it'll shed some light on this API for me.

Thanks mate, I appreciate it.

Rob

-Original Message-
From: Tim Chase [mailto:[EMAIL PROTECTED] 
Sent: 28 January 2008 17:02
To: Robert Rawlins - Think Blue
Cc: python-list@python.org
Subject: Re: Get Available Functions

> I'm working with a python module which isn't part of the core
> Python API and it also isn't very documented or supported, is
> there any way that I can easily dump/view the available
> classes and methods within the package from within python?

Most of the time, the dir(), type() and help() functions can be 
your friend:

 >>> import somelib
 >>> dir(somelib)
['Foo', 'thing', 'whatever']
 >>> type(somelib.Foo)

 >>> dir(somelib.Foo)
['method1', 'method2']
 >>> type(somelib.thing)

 >>> print somelib.thing
'I didn't expect the Spanish Inquisition!'
 >>> type(somelib.whatever)

 >>> help(somelib.whatever)
Help on function whatever in module somelib:

whatever(param1, param2, *args, **kwargs)
 >>>


I've had a couple cases where the underlying module was written 
in C (mod_python in particular...don't know if it still has this 
problem) where dir() wouldn't actually tell you about the object, 
but for most cases, dir() will get you pointed in the right 
dir-ection. :)

-tkc




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


Get Available Functions

2008-01-28 Thread Robert Rawlins - Think Blue
Hello Guys,

 

I'm working with a python module which isn't part of the core Python API and
it also isn't very documented or supported, is there any way that I can
easily dump/view the available classes and methods within the package from
within python?

 

Thanks guys,

 

Rob

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

Logging Help

2008-01-02 Thread Robert Rawlins - Think Blue
Hello Guys,

 

I'm having a little trouble modifying my logging so that the log files are
rotated. I'm using the standard python logging module and can find some
decent documentation on how to create and add a rotatingFileHandler to my
logger but I've been unable to find out how to disable the standard File
handler.

 

I currently create my logger like this:

 

logging.basicConfig(level=logging.DEBUG,

format='%(asctime)s %(levelname)-8s %(message)s',

datefmt='%a, %d %b %Y %H:%M:%S',

filename='/var/log/ThinkBlue.log',

filemode='a')

 

What's the best way to change this so that ThinkBlue.log grows no larger
than let's say 5mb and that we keep 5 archives of the log, I know I can
create a new handler and add it to the logger like this:

 

newlevel = logging.handlers.RotatingFileHandler('/var/log/ThinkBlue.log',

'a',

5000,

5)

 

logging.getLogger('').addHandler(newlevel)

 

But fear this will mean I'm writing to the log file twice, is that right? I
need some help, am I going about this the correct way of is there a better
way of doing this?

 

Cheers guys,

 

Rob

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

Eclipse Plugins

2007-10-26 Thread Robert Rawlins - Think Blue
Hello Chaps,

 

I'm not sure what you IDE you all generally use for your python development.
I've been somewhat lazy and always used a plain text editor, however this
technique is starting to take its toll, and it's purely been out of laziness
not finding myself a decent IDE to develop in. So this morning task is to
pull my thumb out of my arse and set something up properly.

 

For my other languages, such as HTML, ColdFusion, JAVA etc I make good use
of the Eclipse SDK, and I'm looking for some advice on the best and most
popular python plug-ins available, what would you suggest? I downloaded one
called PyDev which looked ok but nothing too exciting.

 

Thanks guys,

 

Rob

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

RE: Check File Change Every 10 Seconds

2007-10-22 Thread Robert Rawlins - Think Blue
Spot on Tim, I'm running Linux, I totally forgot to mention... more detail 
coming in a reply to Gabriel's post.

-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Tim Golden
Sent: 22 October 2007 15:40
Cc: python-list@python.org
Subject: Re: Check File Change Every 10 Seconds

Gabriel Genellina wrote:
> En Mon, 22 Oct 2007 06:56:52 -0300, Robert Rawlins - Think Blue  
> <[EMAIL PROTECTED]> escribi�:
> 
>> I've got a requirement to check a file for a change every 10 seconds or  
>> so,
>> and if the file has been modified since the last time I parsed its  
>> content
>> into the application then I need to parse it in again. However, I need  
>> this
>> process to not interrupt the rest of my application flow.
> 
> See this article by Tim Golden:
> http://timgolden.me.uk/python/win32_how_do_i/watch_directory_for_changes.html
> 
>> What is the best way to handle this? Is there some form of file watcher
>> module for python which can watch the file for me and then parse any  
>> changes
>> into the application memory? Or should I be spawning and unjoined thread
>> which contains and infinite loop which checks a date/time the file was
>> modified against an internal date/time variable for when the application
>> last parsed the file into memory?
> 
> I would use a different thread waiting for notifications from  
> ReadDirectoryChangesW (third option in the link above)
> See http://msdn2.microsoft.com/en-us/library/aa365465.aspx for more info  
> on ReadDirectoryChangesW
> 

Although Robert doesn't say so here, I seem to remember that his
past posts have indicated a *nix setting. Robert?

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

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

RE: Check File Change Every 10 Seconds

2007-10-22 Thread Robert Rawlins - Think Blue
Thanks for your time Gabriel,

That certainly looks to be the type of thing that I'm looking to achieve, 
however, I forgot to mention I'm running this on a Linux platform and not a 
Win32 one :-( Sorry.

I'm sure similar things are achievable, I've used os.stat before now to get the 
time stamp for when a file was last changed, if I perhaps combine this with the 
while 1: inside a thread I can achieve the same end result? Sound like a good 
idea?

I just wasn’t sure how safe it was to spawn a thread like this which contains 
an infinite loop.

Thanks again for your time,

Rob

-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Gabriel Genellina
Sent: 22 October 2007 15:29
To: python-list@python.org
Subject: Re: Check File Change Every 10 Seconds

En Mon, 22 Oct 2007 06:56:52 -0300, Robert Rawlins - Think Blue  
<[EMAIL PROTECTED]> escribi�:

> I've got a requirement to check a file for a change every 10 seconds or  
> so,
> and if the file has been modified since the last time I parsed its  
> content
> into the application then I need to parse it in again. However, I need  
> this
> process to not interrupt the rest of my application flow.

See this article by Tim Golden:
http://timgolden.me.uk/python/win32_how_do_i/watch_directory_for_changes.html

> What is the best way to handle this? Is there some form of file watcher
> module for python which can watch the file for me and then parse any  
> changes
> into the application memory? Or should I be spawning and unjoined thread
> which contains and infinite loop which checks a date/time the file was
> modified against an internal date/time variable for when the application
> last parsed the file into memory?

I would use a different thread waiting for notifications from  
ReadDirectoryChangesW (third option in the link above)
See http://msdn2.microsoft.com/en-us/library/aa365465.aspx for more info  
on ReadDirectoryChangesW

-- 
Gabriel Genellina

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

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

Check File Change Every 10 Seconds

2007-10-22 Thread Robert Rawlins - Think Blue
Hello Chaps,

 

I've got a requirement to check a file for a change every 10 seconds or so,
and if the file has been modified since the last time I parsed its content
into the application then I need to parse it in again. However, I need this
process to not interrupt the rest of my application flow.

 

What is the best way to handle this? Is there some form of file watcher
module for python which can watch the file for me and then parse any changes
into the application memory? Or should I be spawning and unjoined thread
which contains and infinite loop which checks a date/time the file was
modified against an internal date/time variable for when the application
last parsed the file into memory?

 

I'll be interested to hear your ideas guys,

 

Thanks,

 

Rob

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

Control Log File Size

2007-10-19 Thread Robert Rawlins - Think Blue
Hello Chaps,

 

I've got an application running on linux which writes log files using the
python logging module. I'm looking for some help and advice to cap the size
which the file will grow too, something reasonably like 2Mb would be great.

 

What is the best way to handle this kind of thing? Can this be done using a
setting in the logging module? Or is it something I'll have to get the FS to
handle for me?

 

Thanks chaps,

 

Rob

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

Subprocess Running Slowly

2007-10-12 Thread Robert Rawlins - Think Blue
Hello Guys,

 

I'm looking for a little advice on spawning a sub process to run a command
line application for transferring a file over Bluetooth. Now the spawned
process runs from start to finish exactly as I would expect it too, however
its very slow. 

 

Now the main reason this is so odd, is that If I have my python application
running as usual, and I start the command line application manually myself
from the command line, then it'll transfer the file to my phone about 3 or 4
times faster than when the app is launch using sub process. Take a look
below for a quick snippet example of my process launch code.

 

send = 'ussp-push --debug --dev hci%s [EMAIL PROTECTED] /pblue/new/Media/%s %s' 
% (

adapter, 

mac, 

result, 

media[3], 

media[0]

)

process = subprocess.Popen(

send, 

shell=True, 

stdout=subprocess.PIPE, stderr=subprocess.PIPE).stderr

 

Do you have any ideas what might be causing this slow behaviour? Do spawned
sub processes generally run at a much slower rate than expected? Or is this
something specific to my application?

 

I'd love to work this out as when transferring larger files this becomes a
real pain, especial when I know there is no real physical reason for it
running so slowly.

 

Thanks guys for any help you can offer.

 

Rob

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

RE: Almost There - os.kill()

2007-09-24 Thread Robert Rawlins - Think Blue
Woops,

 

Spoke too soon, just wrapped them in int() and it works a charm :-D

 

Rob

 

From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]
On Behalf Of Robert Rawlins - Think Blue
Sent: 24 September 2007 12:23
To: python-list@python.org
Subject: Almost There - os.kill()

 

Hello Guys,

 

Finally got around to sitting down to tidy up this script this morning but
I'm having a small syntax problem with os.kill() to kill a process on my
system. Here is the code:

 

def killApplication():

 

   pid = file('/var/lock/MyApplication.lock').read().strip()

   

   logging.info('Killing Application Process: %s' % pid)

 

   os.kill(pid, 15)

 

   logging.info('Application %s Killed' % pid)

 

As you can see if rips the PID from the lock file, which works fine as I get
log data which says 'Killing Application Process: 3079' exactly as I would
expect it too. However when I run this script I get the following error:

 

Traceback (most recent call last):

  File "/pblue/new/Alive.py", line 50, in ?

main()

  File "/pblue/new/Alive.py", line 47, in main

doCheck()

  File "/pblue/new/Alive.py", line 43, in doCheck

killApplication()

  File "/pblue/new/Alive.py", line 28, in killApplication

os.kill(pid, 15)

TypeError: an integer is required

 

Which would suggest its passing those arguments in as the wrong data types
to the kill command. What is the best way to convert these?

 

Thanks guys,

 

Rob

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

Almost There - os.kill()

2007-09-24 Thread Robert Rawlins - Think Blue
Hello Guys,

 

Finally got around to sitting down to tidy up this script this morning but
I'm having a small syntax problem with os.kill() to kill a process on my
system. Here is the code:

 

def killApplication():

 

   pid = file('/var/lock/MyApplication.lock').read().strip()

   

   logging.info('Killing Application Process: %s' % pid)

 

   os.kill(pid, 15)

 

   logging.info('Application %s Killed' % pid)

 

As you can see if rips the PID from the lock file, which works fine as I get
log data which says 'Killing Application Process: 3079' exactly as I would
expect it too. However when I run this script I get the following error:

 

Traceback (most recent call last):

  File "/pblue/new/Alive.py", line 50, in ?

main()

  File "/pblue/new/Alive.py", line 47, in main

doCheck()

  File "/pblue/new/Alive.py", line 43, in doCheck

killApplication()

  File "/pblue/new/Alive.py", line 28, in killApplication

os.kill(pid, 15)

TypeError: an integer is required

 

Which would suggest its passing those arguments in as the wrong data types
to the kill command. What is the best way to convert these?

 

Thanks guys,

 

Rob

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

Killing A Process By PID

2007-09-20 Thread Robert Rawlins - Think Blue
Hello Guys,

 

I've got some code here which I'm using to kill a process if it freezes.
However I have no real way of testing it as I can't force a freeze on my
application. So thought I would run the code past you lot to see if anything
jumps out as not being quite right.

 

Now, my main application when it starts creates a lock file containing its
process ID using the following code:

 

file('/var/lock/Application.lock', 'w').write(str(os.getpid()))

 

Which to be honest appears to run just fine, when I look in that file it
always contains the correct process ID, for instance, 3419 or something like
that. This application also writes a date stamp to a log file every 10
seconds or so, and it's this date stamp that I use to determine if the
application has crashed.

 

def doCheck():

try:

f = open('/pblue/new/Logs/Alive.log','r')

 

try:

timestamp = f.read()

finally:

f.close()

except Exception, e:

logging.error(str(e))

nowdatetime = timestuff.now()

 

filedatetime = timestuff.strptime(timestamp, '%Y-%m-%d %H:%M:%S')

 

if timestuff.RelativeDateTimeDiff(nowdatetime,filedatetime).minutes > 1:

killApplication()

 

def killApplication():

  pid = file('/var/lock/Application.lock').read().strip()

  

  if file('/proc/%s/cmdline' % pid).read().endswith('python'):

os.system('kill %s' % pid)

 

Now, this second script is run every minute on the system, the idea is to
check that time stamp file, and if it hasn't been updated in the past
minute, then kill the process, inittab can then pick it back up and it will
be as right as rain again. Now the problem is that I'm unable to test it
properly, and from what I've seen so far, when the app does freeze, the
process isn't being killed, which leads me to believe something isn't quite
right in my second script above.

 

Does anyone spot anything that sticks out? Or perhaps something I could be
doing a little better?

 

Thanks guys,

 

Rob

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

Wait For Application Start

2007-09-18 Thread Robert Rawlins - Think Blue
Hello Guys,

 

I'm kick starting my application using the inittab to ensure its re-spawned
if it dies. However I need to ensure several other applications and service
are up and running before my application is started, things such as dbus and
a couple of other hardware stacks.

 

A concept I've been advised to use is that I should make the application
wait for the existence of a file before it starts, then create that file on
the FS using the rc.local file which is called at the end of all other start
items. Only once that file exists will my application commence with its job.

 

This seems like a very logical method, but I'm not sure how to implement it
into my python code? Is there a simple way to make it wait for that file?
Without the need to build my own conditional loop?

 

Can anyone perhaps help with a simple code example?

 

Thanks guys,

 

Rob

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

Launching App

2007-08-09 Thread Robert Rawlins - Think Blue
Hello Guys,

 

I'm looking for the best method to launch my python app when my Linux system
boots up. At the moment I just have an entry like this in my rc.local file:

 

CD /myfolder/anotherfolder

./myapp.py

 

Is this the best way to do this? Or is there a better way? I feel like a bit
of a dummy asking this question, I just wanted to ensure the best method.

 

Thanks guys,

 

Rob

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

Devloper Wanted For Debugging / Optimizing

2007-08-03 Thread Robert Rawlins - Think Blue
Hello Chaps,

 

I've been writing (with your help) a small application over the past couple
of months but I'm really struggling to iron out all of the creases and its
seems to spring a few memory leaks that I'm unable to find and plug. It's
only a small app, around 500 lines of code I would guess, aside from the
usual python type things it deals with dbus and XML parsing using element
tree, But it's for an embedded Linux platform so keeping it lightweight and
memory leak free is an absolute must.

 

Is anyone interested to take this small project on to have a look at my
code? Perhaps give me a little bit of advice on where I'm going wrong?

 

I would be willing to pay someone for their much needed time to get the
final few creases ironed out, this isn't really stuff we can deal with on
the boards so something private is a must, hence I'm exploring the idea of a
paid contract.

 

If anyone is interested then please feel free to contact me off list.

 

Thanks again guys,

 

Rob

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

RE: Replacing _xmlplus.dom.minidom with xml.dom.minidom

2007-08-03 Thread Robert Rawlins - Think Blue
Just as a heads up, minidom is pretty inefficient and difficult to work with
too.

On someone else's advice I switched over to ElementTree and have been really
pleased with the results, its much simpler to work with and more efficient
too.

Rob

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]
On Behalf Of [EMAIL PROTECTED]
Sent: 03 August 2007 14:39
To: python-list@python.org
Subject: Replacing _xmlplus.dom.minidom with xml.dom.minidom

Hi,

I'm working with a number of scripts which were written years ago for
my company for Python 2.2, and I'd like to update for Python 2.5. I
have written a script to add # -*- coding: cp1252 -*- to the beginning
of all my scripts, and that has fixed the encoding issues.

Another issue was the use of -

from _xmlplus.dom import minidom

http://sourceforge.net/project/showfiles.php?group_id=6473

I couldn't get a version of this module for  2.5, so I changed the
above to -

from xml.dom import minidom

The scripts I work with are all working now correctly for 2.5. But I
haven't been able to test the whole system as not all of it concerns
me. Anyway, my colleges are interested in updating also if it will be
reasonably hassle free.

So my main concern now is the use of  _xmlplus.dom.minidom. Why was it
used and what differences should  I look out for with regard to
xml.dom.mididom

Thanks very much for your help,

Barry.

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

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


RE: File Handling & TRY/EXCEPT

2007-08-03 Thread Robert Rawlins - Think Blue
Thanks for your ideas guys,

I'm unfortunately tied to 2.4 so don't have the full try except status, but
I'm now working with the following code:

def addApp(self, event):
try:
logfile =
open('/pblue/new/Logs/Application.csv','a')

now = datetime.datetime.now()
logstring = '%s,%s \n' % (event, str(now))

try:
logfile.write(logstring)
finally:
logfile.close()
except:
self.addApp(event)

I'm trying to slowly debug my app and get rid of all the memory leaks, but
its pain staking work, any help you can offer on that stuff would be a god
send, I'm a little reluctant about posting all my app code on the lists as
I'd like to keep some of it private.

How does that new version look? A little tidier?

Thanks guys,

Rob

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]
On Behalf Of Steve Holden
Sent: 03 August 2007 14:20
To: python-list@python.org
Subject: Re: File Handling & TRY/EXCEPT

Robert Rawlins - Think Blue wrote:
> Hello Guys,
> 
>  
> 
> I'm looking for some advice on how best to handle file read/write errors 
> with try/except as i'm a little vague on this, I have a small memory 
> leak in my app and I'm starting to think its generated by my log file 
> write. For an example of the function look below.
> 
>  
> 
>def addAppLog(self, event):
>   try:
>  logfile = open('/pblue/new/Logs/Application.csv','a')
>  now = datetime.datetime.now()
>  logstring = '%s,%s \n' % (event, str(now))
>  logfile.write(logstring)
>   except:
>  self.addAppLog(event)
> 
It seems somewhat perverse, when the logging code raises an exception, 
to recursively log - presumably the same exception will be raised again?

>   else:
> 
>  logfile.close()
> 
Remember that in 2.5 you can use try ... except ... finally

> 
> Now I'm looking for some help to sort this out as I'm sure it's pretty 
> untidy, I want to make it as air tight as possible. The basic concept 
> was that if it tries writing to the log file and it fails, then it needs 
> to reattempt it, right?
> 
Wrong. The one thing you can't log is a logging attempt error!
>  
> 
> What's the best way to handle this to ensure that there are not any 
> memory leaks caused when the file is open() but not followed by a 
> close(). I'm running 2.4 and I know some of these newer versions don't 
> need you to explicitly close() the file objects, but I would certainly 
> feel a lot better.
> 
>  
> 
> Any advice?
> 
This is just a quick on-the-fly look at what you did, I am sure you will 
receive other,  probably more helpful, comments.

regards
  Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC/Ltd   http://www.holdenweb.com
Skype: holdenweb  http://del.icio.us/steve.holden
--- Asciimercial --
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
--- Thank You for Reading -

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

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


File Handling & TRY/EXCEPT

2007-08-03 Thread Robert Rawlins - Think Blue
Hello Guys,

 

I'm looking for some advice on how best to handle file read/write errors
with try/except as i'm a little vague on this, I have a small memory leak in
my app and I'm starting to think its generated by my log file write. For an
example of the function look below.

 

   def addAppLog(self, event):

  try:

 logfile = open('/pblue/new/Logs/Application.csv','a')

 

 now = datetime.datetime.now()

 

 logstring = '%s,%s \n' % (event, str(now))

 

 logfile.write(logstring)

  except:

 self.addAppLog(event)

  else:

 logfile.close()

 

Now I'm looking for some help to sort this out as I'm sure it's pretty
untidy, I want to make it as air tight as possible. The basic concept was
that if it tries writing to the log file and it fails, then it needs to
reattempt it, right?

 

What's the best way to handle this to ensure that there are not any memory
leaks caused when the file is open() but not followed by a close(). I'm
running 2.4 and I know some of these newer versions don't need you to
explicitly close() the file objects, but I would certainly feel a lot
better.

 

Any advice?

 

Thanks guys,

 

Rob

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

DateTime Differance

2007-07-20 Thread Robert Rawlins - Think Blue
Hello Guys,

 

I've used the eGenix date time module to create two date time objects which
I'm now looking to compare like so:

 

If date 1 is more than 5 minutes older than date 2:

Do something here...

 

After reading through the egenix documentation I'm still a little confused
as the best way to do this. Can anyone offer any advice?

 

Thanks guys,

 

Rob

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

Log Memory Usage

2007-07-19 Thread Robert Rawlins - Think Blue
Hello Guys,

 

I have an embedded application with a suspected memory leak which I'm trying
to confirm. You see, the application seems to crash unexpectedly and when
looking at the resource consumption after the crash system memory has crept
up to nearly 100%.

 

However this takes quite a long time to occur, so sitting and watching the
application run isn't a very effective way of doing it, so I'm looking for
the best way to log the system memory every minute or two.

 

I have a scheduled event which occurs every minute, i just need a code
solution to give me the systems current memory consumptions details, is
there perhaps something in the os module?

 

Thanks for any suggestions guys,

 

Rob

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

Copy List

2007-07-18 Thread Robert Rawlins - Think Blue
Hello Guys,

 

What's the best way to create a copy of a list? I've seen several method and
I'm not sure what to use. This will be in a class and one method creates a
list which I then want to move to the self scope, like so:

 

__Init__(self):

Self.myList = []

 

regenerateList(self):

 

newList = []

 

for somthing in somthing:

newList.append('SomeStuff')

 

self.Mylist = newList.copy()

 

See, The iteration and rebuilding of the list could be quite slow, during
which time the application could be reading from the self.mylist variable so
i think simply doing this:

 

regenerateList(self):

 

self.myList = []

 

for somthing in somthing:

self.myList.append('SomeStuff')

 

Might cause a few inconsistencies in the list if it's being read from whilst
I'm appending too it, so I'm guessing that the top example is the best
method, I'm just looking for the best way to copy() that list into the self
scope, should i be using copy(), deepcopy() or simply self.mylist = newlist?

 

Thanks guys,

 

Rob 

 

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

Subprocess Poll

2007-07-18 Thread Robert Rawlins - Think Blue
Hello Guys,

 

I have an application which launches a sub-process using the sub
process/Popen module, which seems to work great. However on occasion the
launched process will crash, it doesn't appear to full terminate though as
when I run a 'top' command from the Linux system the process is still
displayed in the list, it just appears to be dormant and the TIME+ for the
application stops incrementing.

 

Can I use the poll() method from the sub-process object to test for this? I
would love to test it however the crash is so rare it's very difficult to
simulate, so thought I'd try and see if anyone has any experience of this.

 

Thanks chaps,

 

Rob

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

Subprocess Poll() to sport Application Crash

2007-07-18 Thread Robert Rawlins - Think Blue
Hello Guys,

 

I have an application which launches a sub-process using the sub
process/Popen module, which seems to work great. However on occasion the
launched process will crash, it doesn't appear to full terminate though as
when I run a 'top' command from the Linux system the process is still
displayed in the list, it just appears to be dormant and the TIME+ for the
application stops incrementing.

 

Can I use the poll() method from the sub-process object to test for this? I
would love to test it however the crash is so rare it's very difficult to
simulate, so thought I'd try and see if anyone has any experience of this.

 

Thanks chaps,

 

Rob

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

RE: Dict Help

2007-07-17 Thread Robert Rawlins - Think Blue
Morning Gabriel,

I'm looking for a little more advice on this dictionary/list to
defaultdict/set conversion that we were talking about, there were a few
things I was looking to clarify. Firstly, what is the difference between a
standard dict and a default dict? Is it purely a performance issue? 

This dict is likely to grow pretty large and is read/written on a very
regular basis so the better performing one is going to work best for me.
Also, am I still able to iterate over a set in the same way I can a list?

Here is an example of my add function at the moment, how can that be
converted to a defaultdict/set instead of the dict/list approach?

self.History = {}

def addHistory(self, address, media):
if address not in self.History:
self.History[address] = []

self.History[address].append(media)

Thanks Gabriel,

Rob

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]
On Behalf Of Robert Rawlins - Think Blue
Sent: 15 July 2007 20:47
To: 'Gabriel Genellina'; python-list@python.org
Subject: RE: Dict Help

Thanks Gabriel,

That all works a charm, Iteration on this isn’t important for me so the SET
will work much better I think, I had read about them before but have never
used them, I'm glad to see it works so simply.

Thanks,

Rob

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]
On Behalf Of Gabriel Genellina
Sent: 15 July 2007 20:33
To: python-list@python.org
Subject: Re: Dict Help

En Sun, 15 Jul 2007 11:39:24 -0300, Robert Rawlins  
<[EMAIL PROTECTED]> escribió:

> I'm looking for some help expanding on a dictionary I've got and storing  
> multiple values per key and the best way to do it. I'm guessing that I  
> need to store a list inside the value of the dictionary, but I'm not  
> quite sure how that can be achieved, and also how to check for values in  
> that list.
> Here is a brief example of what I want to be able to build:
> Key  Value
> 00:0F:DE:A8:AB:6F  
>
6C66F14B-FED6-D1A4-62EAAD881E9133F0.jpg,01DBB592-F7EB-B000-7F250FD8A2CE158F.
gif,533EAE0F-B211-B2D8-4C2DB662CCECFBD7.3gp
> 00:17:B0:A0:E7:09  
>
6C66F14B-FED6-D1A4-62EAAD881E9133F0.jpg,01DBB592-F7EB-B000-7F250FD8A2CE158F.
gif
> 00:1B:98:16:21:E4  
> 6C66F14B-FED6-D1A4-62EAAD881E9133F0.jpg

There are a few ways to do it. Just translating your own words into  
Python, use a dictionary with a list of values.

> Now I really need to have two functions, one of which appends a value to  
> the list for a specified key, so I have the key and the new value as  
> strings, I then want to locate that key and append the list with the new  
> value.

The simplest way:

d = {}
# add a pair key, value
if key not in d:
   d[key] = [value]
else:
   d[key].append(value)

> The second function I need, is to check for the existence of a value in  
> the list of a particular key, again I have the key and the value and I  
> want to do something like:
> if value in list of values for key:
>do something here...

# check for a key, value
exists = key in d and value in d[key]

> The final function that would be helpful to me would to be able to  
> remove a value from the list for a specific key.

# remove value from key's values
if key in d and value in d[key]:
   d[key].remove(value)

> I'm not sure if a list as the value in a dict is possible, or if its the  
> best way to achieve this, It just made logic sense to me so thought I'd  
> come and get your thoughts on this. If anyone has any better suggestions

It's perfectly possible as you can see. Now, depending on how many values  
you have, or how often do you test, you might replace the list with a set.  
Sets are unordered collections (you will loose the insertion order) but  
are better suited for a "contains" test (O(1) instead of O(n) for a list)
And you might replace the dictionary with a defaultdict. The insertion  
would become:

 from collections import defaultdict
d = defaultdict(set)
# add a pair key, value
d[key].add(value)

The existence check and remove method do not change.

Dicts, lists and sets are documented here:  
<http://docs.python.org/lib/types.html> and defaultdict  
<http://docs.python.org/lib/defaultdict-objects.html>

-- 
Gabriel Genellina

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

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

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


RE: Access Object From 2 Applications or Fix Scheduler

2007-07-16 Thread Robert Rawlins - Think Blue
Also Hendrik,

I should probably mention that the second application is a constant running
application, it's nothing something that can just be 'started' by the first
one, its running non-stop and just needs data passed into it regularly when
the first application finds it.

Application 1   Application 2
Work with dict
Work with dict
Work with dict
Work with dict
New XML Found, Parse Into Dict -->  Work with new dict
Work with new dict
Work with new dict
Work with new dict

You see how that works? Application 1 has a function that runs every minute
and _may_ find a new xml file, if it does then I need it to parse that file
into a list of dictionary and then pass that into application 2, which then
starts using it :-)

Now we may be able to avoid this if there is some type of file watcher
function available in python, my second application could then just watch
the XML file and as soon as a new one is available parse it itself. Is that
something you've heard of?

Thanks,

Rob


-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]
On Behalf Of Hendrik van Rooyen
Sent: 16 July 2007 07:29
To: python-list@python.org
Subject: Re: Access Object From 2 Applications or Fix Scheduler

 Robert Rawlins - Think Blue  wrote:


>Hello Guys,
>
>I have two applications which I need to get talking and sharing an 
>object, what
s the best way to do this? Basically my >first application parses an XML
document into a bunch of lists and tuples etc, and I need to access the data
in these lists >and tuples from my second application. Is this possible? And
whats the simplest method?

>
>This problem all evolves from a hack/work around Ive had to place 
>together as
I have been having problems with >sched, these two applications used to be a
single one to start with but for some reason when scheduling tasks using the
>sched module they wouldnt ever run when supposed too, so Id schedule 
>it to
run every 2 minutes and it would run >between 3 and 15 minutes :-s so I
ripped it out into its own application and it now runs every 2 minutes
exactly.
>
>I either need to fix my sched issues or the memory share, either way 
>Im quite
happy, just need to get it working >smoothly.
>
>Any ideas?
>

You have already split the thing, so I would:

schedule the XML parser and make it do its stuff.
then pickle the results using cPickle
then from this first thing, either:
write the pickles to temp files and start the second thing using eg
os.system() or start the second thing and use a named pipe to pass the
pickles over for unpickling and processing, or use one of the popens, or
have a look at Pyro, or start the second thing as a thread and use a Queue..

of course this whole scheme will fall apart if the combined processing takes
longer than the scheduling interval.

HTH - Hendrik



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


RE: Access Object From 2 Applications or Fix Scheduler

2007-07-16 Thread Robert Rawlins - Think Blue
Thanks Hendrik & Alex for your suggestions,

I'm glad to see that there are lots of options for me here, and I'll give
them all of a decent exploration to see what my best option is, starting the
second app in a named pipe sounds like a fair solution, but I've not don't
this before, how easy is it to have the second application ready to receive
the new dictionary data?

Just out of interest though I'd still like to know why my first application
schedules didn't work properly, should I be starting them all in their own
thread? Would that work better?

Thanks Guys,

Rob

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]
On Behalf Of Hendrik van Rooyen
Sent: 16 July 2007 07:29
To: python-list@python.org
Subject: Re: Access Object From 2 Applications or Fix Scheduler

 Robert Rawlins - Think Blue  wrote:


>Hello Guys,
>
>I have two applications which I need to get talking and sharing an 
>object, what
s the best way to do this? Basically my >first application parses an XML
document into a bunch of lists and tuples etc, and I need to access the data
in these lists >and tuples from my second application. Is this possible? And
whats the simplest method?

>
>This problem all evolves from a hack/work around Ive had to place 
>together as
I have been having problems with >sched, these two applications used to be a
single one to start with but for some reason when scheduling tasks using the
>sched module they wouldnt ever run when supposed too, so Id schedule 
>it to
run every 2 minutes and it would run >between 3 and 15 minutes :-s so I
ripped it out into its own application and it now runs every 2 minutes
exactly.
>
>I either need to fix my sched issues or the memory share, either way 
>Im quite
happy, just need to get it working >smoothly.
>
>Any ideas?
>

You have already split the thing, so I would:

schedule the XML parser and make it do its stuff.
then pickle the results using cPickle
then from this first thing, either:
write the pickles to temp files and start the second thing using eg
os.system() or start the second thing and use a named pipe to pass the
pickles over for unpickling and processing, or use one of the popens, or
have a look at Pyro, or start the second thing as a thread and use a Queue..

of course this whole scheme will fall apart if the combined processing takes
longer than the scheduling interval.

HTH - Hendrik



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


Access Object From 2 Applications or Fix Scheduler

2007-07-15 Thread Robert Rawlins - Think Blue
Hello Guys,

 

I have two applications which I need to get talking and sharing an object,
what's the best way to do this? Basically my first application parses an XML
document into a bunch of lists and tuples etc, and I need to access the data
in these lists and tuples from my second application. Is this possible? And
what's the simplest method?

 

This problem all evolves from a hack/work around I've had to place together
as I have been having problems with sched, these two applications used to be
a single one to start with but for some reason when scheduling tasks using
the sched module they wouldn't ever run when supposed too, so I'd schedule
it to run every 2 minutes and it would run between 3 and 15 minutes :-s so I
ripped it out into its own application and it now runs every 2 minutes
exactly.

 

I either need to fix my sched issues or the memory share, either way I'm
quite happy, just need to get it working smoothly.

 

Any ideas?

 

Thanks guys,

 

Rob

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

RE: Dict Help

2007-07-15 Thread Robert Rawlins - Think Blue
Thanks Gabriel,

That all works a charm, Iteration on this isn’t important for me so the SET
will work much better I think, I had read about them before but have never
used them, I'm glad to see it works so simply.

Thanks,

Rob

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]
On Behalf Of Gabriel Genellina
Sent: 15 July 2007 20:33
To: python-list@python.org
Subject: Re: Dict Help

En Sun, 15 Jul 2007 11:39:24 -0300, Robert Rawlins  
<[EMAIL PROTECTED]> escribió:

> I'm looking for some help expanding on a dictionary I've got and storing  
> multiple values per key and the best way to do it. I'm guessing that I  
> need to store a list inside the value of the dictionary, but I'm not  
> quite sure how that can be achieved, and also how to check for values in  
> that list.
> Here is a brief example of what I want to be able to build:
> Key  Value
> 00:0F:DE:A8:AB:6F  
>
6C66F14B-FED6-D1A4-62EAAD881E9133F0.jpg,01DBB592-F7EB-B000-7F250FD8A2CE158F.
gif,533EAE0F-B211-B2D8-4C2DB662CCECFBD7.3gp
> 00:17:B0:A0:E7:09  
>
6C66F14B-FED6-D1A4-62EAAD881E9133F0.jpg,01DBB592-F7EB-B000-7F250FD8A2CE158F.
gif
> 00:1B:98:16:21:E4  
> 6C66F14B-FED6-D1A4-62EAAD881E9133F0.jpg

There are a few ways to do it. Just translating your own words into  
Python, use a dictionary with a list of values.

> Now I really need to have two functions, one of which appends a value to  
> the list for a specified key, so I have the key and the new value as  
> strings, I then want to locate that key and append the list with the new  
> value.

The simplest way:

d = {}
# add a pair key, value
if key not in d:
   d[key] = [value]
else:
   d[key].append(value)

> The second function I need, is to check for the existence of a value in  
> the list of a particular key, again I have the key and the value and I  
> want to do something like:
> if value in list of values for key:
>do something here...

# check for a key, value
exists = key in d and value in d[key]

> The final function that would be helpful to me would to be able to  
> remove a value from the list for a specific key.

# remove value from key's values
if key in d and value in d[key]:
   d[key].remove(value)

> I'm not sure if a list as the value in a dict is possible, or if its the  
> best way to achieve this, It just made logic sense to me so thought I'd  
> come and get your thoughts on this. If anyone has any better suggestions

It's perfectly possible as you can see. Now, depending on how many values  
you have, or how often do you test, you might replace the list with a set.  
Sets are unordered collections (you will loose the insertion order) but  
are better suited for a "contains" test (O(1) instead of O(n) for a list)
And you might replace the dictionary with a defaultdict. The insertion  
would become:

 from collections import defaultdict
d = defaultdict(set)
# add a pair key, value
d[key].add(value)

The existence check and remove method do not change.

Dicts, lists and sets are documented here:  
 and defaultdict  


-- 
Gabriel Genellina

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

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


Launch One Application From Another

2007-07-12 Thread Robert Rawlins - Think Blue
Hello Guys,

 

I'm looking for the best method to launch one Python application from
another? Is something like sub process going to be my best bet? Or is there
something more specific for launching one python app from another?

 

Thanks guys,

 

Rob

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

Time A Script

2007-07-11 Thread Robert Rawlins - Think Blue
Hello Guys,

 

What's the best way to time how long it takes a script to run, from top to
bottom and then have it print that execution time at the end?

 

Thanks guys,

 

Rob

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

Parsing Help

2007-07-10 Thread Robert Rawlins - Think Blue
Hello Guys,

 

I'm looking for some help building a function which can parse some XML for
me using ElementTree. The document is of a very consistent format and I've
copied an example of the document below.

 



 









 











 







 





























 











 

















 







 





















 

Now, the piece of information I'm looking to retrieve is inside the
 element and is, in this example , however I want the function to return the standard integer value and not
the unit8 encoded version, so instead of my function returning '0x05' it
just needs to return '5' which is the standard integer version.

 

I will be passing this XML into the function as a string, so the function
will be formed something like this:

 

Def myFunction(XmlAsString):

Pass the xml and exract my value



Return the value as an integer...

 

I'm not sure on the best method to do this, I just want something nice and
quick, lightweight and that's not resource hungry. Can anyone offer some
advice on this?

 

Thanks guys,

 

Rob

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

GObject and Python 2.5

2007-07-04 Thread Robert Rawlins - Think Blue
Hello Guys,

 

Firstly I should apologise for all the mails that keep landing on the list,
my SMTP server (supplied by my ISP) seems to be playing silly buggers and
sending multiples.

 

I've just installed Python 2.5 on my Debian system and I'm trying to run my
application and I get 'ImportError: No module named gobject' thrown back at
me. I think I've successfully install the glib library using apt-get but the
problem still persists.

 

There is obviously a module I'm missing, any ideas what it might be?

 

Thanks guys,

 

Rob

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

Which Python Version

2007-07-04 Thread Robert Rawlins - Think Blue
Hello Chaps,

 

Is there a command I can run to confirm which version of python I'm running?

 

Another thing I've always wondered, should i be running my applications
using './MyFile.py' or 'Python MyFile.Py' what are the benefits of each
method? One thing I have noticed is that when I used 'Python MyFile.Py' my
processor usage was a lot higher, is that normal?

 

Thanks guys,

 

Rob

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

Which Python Version

2007-07-04 Thread Robert Rawlins - Think Blue
Hello Chaps,

 

Is there a command I can run to confirm which version of python I'm running?

 

Another thing I've always wondered, should i be running my applications
using './MyFile.py' or 'Python MyFile.Py' what are the benefits of each
method? One thing I have noticed is that when I used 'Python MyFile.Py' my
processor usage was a lot higher, is that normal?

 

Thanks guys,

 

Rob

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

Which Python Version

2007-07-04 Thread Robert Rawlins - Think Blue
Hello Chaps,

 

Is there a command I can run to confirm which version of python I'm running?

 

Another thing I've always wondered, should i be running my applications
using './MyFile.py' or 'Python MyFile.Py' what are the benefits of each
method? One thing I have noticed is that when I used 'Python MyFile.Py' my
processor usage was a lot higher, is that normal?

 

Thanks guys,

 

Rob

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

RE: Which Python Version

2007-07-04 Thread Robert Rawlins - Think Blue
Thanks Tim,

Greatly appreciated, I've been having a few problems with one of my apps
recently crashing at all sorts of odd intervals without throwing an error or
anything like that, So I'm upgrading to 2.5 to see if they'll make life any
simpler.

Thanks mate,

Rob

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]
On Behalf Of Tim Golden
Sent: 04 July 2007 11:36
Cc: python-list@python.org
Subject: Re: Which Python Version

Robert Rawlins - Think Blue wrote:
> Is there a command I can run to confirm which version of python I'm
running?

 From outside Python:

   python -V

(that's a capital V)

 From inside Python:

   import sys
   print sys.version

(and a couple of more easily parseable alternatives; look at the sys module
docs)

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

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


Which Python Version

2007-07-04 Thread Robert Rawlins - Think Blue
Hello Chaps,

 

Is there a command I can run to confirm which version of python I'm running?

 

Another thing I've always wondered, should i be running my applications
using './MyFile.py' or 'Python MyFile.Py' what are the benefits of each
method? One thing I have noticed is that when I used 'Python MyFile.Py' my
processor usage was a lot higher, is that normal?

 

Thanks guys,

 

Rob

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

Which Python Version

2007-07-04 Thread Robert Rawlins - Think Blue
Hello Chaps,

 

Is there a command I can run to confirm which version of python I'm running?

 

Another thing I've always wondered, should i be running my applications
using './MyFile.py' or 'Python MyFile.Py' what are the benefits of each
method? One thing I have noticed is that when I used 'Python MyFile.Py' my
processor usage was a lot higher, is that normal?

 

Thanks guys,

 

Rob

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

POpen - Wait For Process To Complete

2007-07-03 Thread Robert Rawlins - Think Blue
Hello guys,

 

Quite a simple one I'm hoping. I've got a process that I run using popen
which writes to a file, I'm then going to read the file and parse its
contents. However, getting the application to wait until the process is
complete and finished writing before it reads and parses the file is
becoming complicated.

 

Popen is meant to have a wait() function along with a poll() function
(according to the docs), both of which can be used to make the app wait for
the process to finished, but for some reason I can't get them working, when
I try this:



Import popen2



  Process = popen2.popen4('This is my command to run from cmd')

 

  Process.wait()

 

  f = open('path/to/output/file.txt')

  new = f.read()

  f.close()

 

It throws back the following error to me:

 

push.wait()

AttributeError: 'tuple' object has no attribute 'wait'

 

Which is fair enough as popen4 returns a tuple containing the output streams
from the cmd command, but how the hell can I have it wait so I can read the
physical files?

 

I'll be interested to hear your thoughts guys,

 

Rob

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

RE: DatePart From String

2007-07-03 Thread Robert Rawlins - Think Blue
Hello Dave,

Thanks for getting back to me, I had been trying to use it like this:

time.strptime('2007-01-01 00:00:00','%H:%M:%S')

I was getting an error thrown at me: ValueError: time data did not match 
format:  data=2007-01-01 00:00:00  fmt=%H:%M:%S

I see how your solution works, but I'm hoping to trip is down a little bit as 
this has to be used in a conditional statement such as the one below:

If time.strftime('%H:%M:%S') > time.strptime('2007-01-01 
00:00:00','%H:%M:%S')
Print 'Later In The Day'

You see how that works? I'm basically trying to check if the current time is 
later that the one defined in my string.

Are you able to give me a working example of how I might do this with my 
conditional?

Thanks Dave,

Rob

-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Dave
Sent: 03 July 2007 10:46
To: python-list@python.org
Subject: Re: DatePart From String

Robert Rawlins - Think Blue  thinkbluemedia.co.uk> writes:


> I’ve tried using the time.strptime() function without much success so
> thought I’d come and ask your advice.
> 

How exactly does it not work?? This works for me:

from time import mktime, strptime
from datetime import datetime

datetime_string = '2007-02-01 00:00:00'
datetime_object = datetime.fromtimestamp(
  mktime(strptime(datetime_string,'%Y-%m-%d %H:%M:%S')))

print datetime_object.year
print datetime_object.month
print datetime_object.day
print datetime_object.hour
print datetime_object.minute
print datetime_object.second

HTH,
Dave








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

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

DatePart From String

2007-07-03 Thread Robert Rawlins - Think Blue
Hello Guys,

 

I have a date/time as a string which looks like this: 2007-02-01 00:00:00

 

I'm trying to get my hands on the different date parts of that string in the
following formats:

 

Time Only: 00:00:00

Day As Number: 01

Month As Number: 02

Day As Word: Monday

 

I've tried using the time.strptime() function without much success so
thought I'd come and ask your advice.

 

Thanks guys for any advice,

 

Rob

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

Application Crashing

2007-06-29 Thread Robert Rawlins - Think Blue
Hello Guys,

 

I find my application is freezing/crashing every now and then and it
becoming a bit of a frustration, so this morning I sat down to try and
locate the problem. After doing some debugging I think I've found the line
of code that causes my problem.

 

Print 'Read Results'

New = e.read()

Print 'Results Read'

 

The last thing the application does is print the words 'Read Results' which
leads me to think that the troublesome line is e.read(). This line is
reading the contents of a file object created by a popen command. This runs
without fail most of the time, but on a reasonably regular occurrence it
seems to crash my app.

 

I've tried wrapping it in a try/except, which I don't know much about and
that doesn't appear to make any difference, the app still crashes.

 

  print 'Reading Push'

  

  try:

 new = e.read()

  except:

 new = 'fault'

 

Print 'Results Read'

 

I'm not sure if this makes any difference, but this is all being run from
within a thread.

 

I need some help on handling this a little better to avoid my application
from dying on me, if anyone can offer some advice on what can be done it
would be greatly appreciated, I'm hoping just a small code solution will be
available.

 

Thanks guys, I look forward to hearing from you,

 

Rob

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

XML Parsing Help,

2007-06-29 Thread Robert Rawlins - Think Blue
Hello Chaps,

 

I'm looking for some help with XML parsing, I've been playing around with
this over the past few days and the only solution I can come up with seems
to be a little slow and also leaves what I think is a memory leak in my
application, which causes all kinds of problems. 

 

I have a very simple XML file which I need to loop over the elements and
extract the attribute information from, but the loop must be conditional as
the attributes must meet a certain criteria.

 

My current solution is using minidom, which I've read isn't one of the
better parsers, if anyone knows of any that are better for the task I would
love to hear it, the content is extracted regularly so whatever we chose
needs to be quick. Take a look at this brief example of the XML we're
dealing with:

 













 

Now what I need to do is loop through the 'event' elements and locate the
first one which has a type of '2' and return the name and location, if it is
unable to find any events with the type of 2 then I want it to return the
default event which is defined by the attributes of the schedules element.

 

I'm pretty inexperienced with parsing XML in python and could really use
some help selecting a parser and writing the code, I'm sure it's all quite
simple I'm just looking for the best solution.

 

Thanks guys,

 

Rob

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

Threads Dying?

2007-06-28 Thread Robert Rawlins - Think Blue
Hello Guys,

 

I've got an application that seems to be a little bit unstable and freezes
quite a bit, and I'm suspecting it's something in one of my threads that's
causing the problem, when does a thread die? And how can I be sure that its
dyeing when its mean to be?

 

Thanks guys,

 

Rob

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

RE: Zip File Woes

2007-06-27 Thread Robert Rawlins - Think Blue
Just as an update guys:

 

Before my zip code featured below I have another piece of code that creates
the zip file from a binary string, like this:

 

  #f3 = open("Media/Media.zip", "wb")

  #f3.write(base64.decodestring(MediaBinary))

  #f3.close

 

Now, with that code commented out like that so the unzip code is running on
the file generated last time the code was run it works fine, but if I try
and unzip after the file has been freshly created I get that error, I've
even tried placing a 2 second sleep command in between them and still get
the problems.

 

Thanks guys,

 

Rob

 

From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]
On Behalf Of Robert Rawlins - Think Blue
Sent: 27 June 2007 15:10
To: python-list@python.org
Subject: Zip File Woes

 

Hello Guys,

 

I'm having a MASSIVE headache today with zip files, I had it working a while
ago but it all seems to have stopped in the past 30 minutes and I can't
figure out why.

 

I'm simply trying to write a function that will unzip a file, simple as
that. I've currently got this code:

 

Import zipfile

 

  zip = zipfile.ZipFile('Media/Media.zip', 'r')

  unzip(zip)

  zip.close()

 

def unzip(zip):

   for name in zip.namelist():

  newname = 'Media/%s' % (name)

  file(newname, 'wb').write(zip.read(name))

 

Now when I try and run this i get the following error message:

 

  File "/usr/lib/python2.4/zipfile.py", line 242, in _RealGetContents

raise BadZipfile, "File is not a zip file"

zipfile.BadZipfile: File is not a zip file

 

However, if I copy the zip file off the unit client onto my windows box and
unzip it and it works absolutely perfectly, all the files are extracted as I
would expect, which leads me to think the zip file is fine, and i must just
be missing something in my code.

 

Any ideas guys? I'm tearing my hair out here.

 

Rob

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

Zip File Woes

2007-06-27 Thread Robert Rawlins - Think Blue
Hello Guys,

 

I'm having a MASSIVE headache today with zip files, I had it working a while
ago but it all seems to have stopped in the past 30 minutes and I can't
figure out why.

 

I'm simply trying to write a function that will unzip a file, simple as
that. I've currently got this code:

 

Import zipfile

 

  zip = zipfile.ZipFile('Media/Media.zip', 'r')

  unzip(zip)

  zip.close()

 

def unzip(zip):

   for name in zip.namelist():

  newname = 'Media/%s' % (name)

  file(newname, 'wb').write(zip.read(name))

 

Now when I try and run this i get the following error message:

 

  File "/usr/lib/python2.4/zipfile.py", line 242, in _RealGetContents

raise BadZipfile, "File is not a zip file"

zipfile.BadZipfile: File is not a zip file

 

However, if I copy the zip file off the unit client onto my windows box and
unzip it and it works absolutely perfectly, all the files are extracted as I
would expect, which leads me to think the zip file is fine, and i must just
be missing something in my code.

 

Any ideas guys? I'm tearing my hair out here.

 

Rob

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

Strange Thread Issue

2007-06-23 Thread Robert Rawlins - Think Blue
Hello Guys,

 

I'm having an issue with a thread which I've not come across before and it
has be baffled. The thread doesn't really do a lot, it simple contains a
popen command to run something from cmd, now then i trigger the thread form
my main application using the .start() method nothing happens, the command
prompt program isn't triggered, yet as soon as a ctrl+c to close my
application the thread then seems to kick into life and work.

 

Any ideas what is causing this?

 

Thanks guys,

 

Rob

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

Binary / SOAPpy

2007-06-08 Thread Robert Rawlins - Think Blue
Hello Guys,

 

I have a WebService call which returns an array, the first element in that
array is the binary for a zip file, however I'm having trouble writing that
binary string into an actual file when it arrives, I've tried the following
method.

 

Result = call to the webservice that returns the array.

 

file = open("Zips/1.zip", "wb")

file.write(result[0])

file.close()

 

But this throws the error message: 

 

file.write(result[0])

TypeError: argument 1 must be string or read-only buffer, not instance

 

Does anyone know what I might be doing wrong? Once I've resaved this file
can then unzip it and get at all its lovely content.

 

Thanks for any input guys,

 

Rob

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

Minimize Bandwidth

2007-06-08 Thread Robert Rawlins - Think Blue
Hello Chaps,

 

I have a python application that hits a web service on a regular basis to
post a string of CSV log data and I'm looking to minimize the amount of
bandwidth that the application uses to send the log data.

 

Is there any way to encode the string into base64 or something, that I can
then post and decode at the other end? Is that likely to save me bandwidth
perhaps? I don't really know much about this encoding stuff, but the smaller
I can compress the string the better.

 

Thanks,

 

Rob

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

Shared Memory Space - Accross Apps & Network

2007-05-22 Thread Robert Rawlins - Think Blue
Hello Guys,

 

I've got an application that runs on an embedded system, the application
uses a whole bunch or dicts and other data types to store state and other
important information.

 

I'm looking to build a small network of these embedded systems, and I'd love
to have them all share the same set or data. Is it possible to share the
applications variables across multiple applications, so certain lists are
like a 'pool' written to by the different systems? I'm sure I could cobble
something together by writing the lists to shared files instead of keeping
them in RAM, but that feels a little inefficient. I'd like to try and
configure some form of master/slave relationship between my applications if
possible.

 

Thanks for any ideas you guys might have.

 

Rob

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

Restart Linux System

2007-05-22 Thread Robert Rawlins - Think Blue
Hello Guys,

 

I'm looking to restart a Linux system from my python application. What's the
best way to achieve this, is there something in the OS module?

 

Thanks,

 

Rob

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

SOAPpy My Class

2007-05-21 Thread Robert Rawlins - Think Blue
Hello Chaps,

 

I've used SOAPpy in its very basic form for consuming web services on my web
server, but I'm now looking to publish a web service from my application.
Basically I'm looking to create a public 'proxy' of an object I have in the
application. The idea being that my application will be able to use the
instance of the object, and other applications will also be able to access
this same object, using the web service.

 

I've got my object built, and it has a few basic functions inside of it,
most of them just serving as wrappers for other simple python functions,
working on dicts and lists. What's the best way to go about publishing a web
service that calls on that instance?

 

I've done simple publishing of services in a standard script before, but
I've not worked on this idea of publishing a public facade for an existing
class.

 

Thanks,

 

Rob

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

App Leaving 'sh ' Everywhere

2007-05-18 Thread Robert Rawlins - Think Blue
Hello Guys,

 

I've got an application that seems to leave 'sh ' in my os
processes list. I'm running it on Debian, albeit a stripped down embedded
version. I'm not sure what the cause of this is, My application starts
several threads and also uses popen2.popen3() to run a few CMD commands.

 

Any ideas why I'm getting this, or if it's even something to be concerned
about.

 

Thanks,

 

Rob

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

Code Explanation

2007-05-17 Thread Robert Rawlins - Think Blue
Hello Guys,

 

I'm currently working on a non-python project, and I'm trying to overcome a
task of parsing a text file into a database and/or xml file. I've managed to
find a parser example written in python, and I'm hoping to deconstruct the
code methodology a bit so I can write it in another language. So I'm hoping
someone can explain to me what these following bits of code are doing.

 

lines = range(data.count("\n"))

lined_data = data.split("\n")

print "Read %i vendors, now processing" % data.count("(hex)")

 

I've not used the split() function before, but it partly makes sense to me.
What is that piece of code doing? 'Data' is the content of the text file,
presumably the first line there is counting the number of lines in the file,
but I don't get the rest of it.

 

The rest of the code seems like a relatively simple set of loops, but it's
just this splitting stuff that's got me confused.

 

Thanks,

 

Rob

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

Spotting Crashed Application

2007-05-09 Thread Robert Rawlins - Think Blue
Hello Guys,

 

I've got an application that I've written, and it sits in an embedded
system, from time to time the application will crash, I'm not quite sure
what's causing this, but as we test it more and more we'll grasp a better
understanding and fix the issues.

 

However, until then I need a quick solution which can spot the crash and
reboot the system. Is there any generic way of writing a separate
application that'll spot the crash in my main application? If not then i was
thinking about having my core application log itself as 'alive' every 5
minutes or so. My new 'spotter' application can check this log, if it's not
been written too in say 6 minutes then the main app must have crashed, and
it can reboot.

 

Any suggestions on how best to handle this? Obviously finding the bug in my
main app is paramount, but a failsafe will never hurt.

 

Thanks again guys,

 

Rob

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

RE: Killing Threads

2007-05-02 Thread Robert Rawlins - Think Blue
Thanks for this Diez and Tim,

I'll take a look into this today, sorry for not posting any code fragments,
its not so much a fragment as it is a bloody great big monster :-D

Rob

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]
On Behalf Of Diez B. Roggisch
Sent: 02 May 2007 09:05
To: python-list@python.org
Subject: Re: Killing Threads

> You probably need to setDaemon (True) on your threads
> after you've created them and before they run. That
> tells the OS: don't bother waiting for these ones to
> finish if the program exits. (At least I think that's
> what it does; I don't use threads all that much)

Actually all it does is to tell the at_exit-handler that it's ok to 
terminate even though there are still some threads. The OS isn't 
concerned with this.

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

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


Dynamic File Name Open()

2007-05-01 Thread Robert Rawlins - Think Blue
Chaps,

 

I'm trying to open a file using open() but the name of the file is created
dynamically as a variable, but also has part of a static path. For instance,
the file may be called 'dave' and will always be in '/my/files/here/'.

 

Now I've tried a few combinations of getting this to work, such as.

 

Path = '/my/files/here/%s' % (name)

 

Open(Path, 'r')

 

But that does work, can anyone enlighten me on the best way to do this?

 

Thanks,

 

Rob

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

RE: test

2007-05-01 Thread Robert Rawlins - Think Blue
Test response :-D

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]
On Behalf Of Ray
Sent: 02 May 2007 04:05
To: python-list@python.org
Subject: test

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

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


Killing Threads

2007-05-01 Thread Robert Rawlins - Think Blue
Hello Guys,

 

I've got an application which I've fearfully placed a couple of threads into
however when these threads are running it seems as if I try and quite the
application from the bash prompt it just seems to freeze the SSH client.
I've also seen that if I have my app running in the background on the system
then my 'reboot' no longer works, it just hangs after saying it's going to
shut down.

 

Is there anything specific i should be doing to kill my threads?

 

Thanks,

 

Rob

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

RE: Dict Copy & Compare

2007-04-30 Thread Robert Rawlins - Think Blue
No that makes sense Tim,

Thanks again for all your help on this one, it'll all prove invaluable I'm
sure. I managed to crack all the big troubles last week with my reoccurring
tasks, it's just a case of tidying up a few of these loose ends, then
that'll be python project no.1 over and done with :-D

Thanks,

Rob

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]
On Behalf Of Tim Golden
Sent: 30 April 2007 12:54
Cc: python-list@python.org
Subject: Re: Dict Copy & Compare

Robert Rawlins - Think Blue wrote:
> On[e] quick question, how can I order a dict by 
 > the 'values' (not keys) before looping? Is that possible?

Depends on what you want to do. You can loop on
the sorted values very easily:


d1 = dict (a=2, b=1)
for value in sorted (d1.values):
   print value



but inferring the equivalent key is, in effect,
not possible since more than one key might correspond
to that value. Depending on what you're after, the
following technique might be of use:


import operator

d = dict (a=2, b=1, c=-1, d=4)
for k, v in sorted (
   d.items (),
   key=operator.itemgetter (1)
):
   print k, "=>", v



It may look a bit hairy, but break it down:

d.items () returns a list of 2-tuples, each one
corresponding to a key-value pair from the dict.
In our case, that'll be:

[('a', 2), ('b', 1), ('c', -1), ('d', 4)]

Although I've written them out like that,
the order they'll come in is undefined.

sorted () will return a sorted version of
whatever iterable you chuck at it. Under
normal Python semantics, sorted() on the
list above will return no change since I've
listed things out in alphanumeric order.

The extra key= parameter tells the sorted
routine to call the function you provide
against each of the items in the list (in
our case that means against each of the
2-tuples) and using the result of that
function as the sorting order.

The operation.itemgetter (1) bit is a touch
complicated unless you're already familiar with
partial functions, but it basically returns
*another* function which takes the item you
give it and returns the -- in this case --
1st item. Just believe me: it works.

So, in summary:

+ Get a list of key-value pairs
+ Sort them according to the 1st item (Python-style)
which in this case is the value.
+ Do something with the result

Before the key= param was introduced into
sort/sorted, people used to do the same thing
with what's often called DSU (short for
decorate-sort-undecorate), a technique which
here would look something like this:

items = d.items ()
sortable_items = [(i[1], i) for i in items]
sortable_items.sort ()
sorted_items = [i[-1] for i in sortable_items]

I mention this because (a) you still see it around
a fair bit and (b) there are occasions where it's
still useful, for example where a simple function
call can't really cope.

(Did I answer the question, or was I just rambling?)

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

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


RE: Dict Copy & Compare

2007-04-30 Thread Robert Rawlins - Think Blue
Thanks for that Tim,

Don't feel guilty mate, I've learned a little something from you anyway,
whether its applied here or not.

On quick question, how can I order a dict by the 'values' (not keys) before
looping? Is that possible?

Thanks,

Rob

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]
On Behalf Of Tim Golden
Sent: 30 April 2007 11:27
Cc: python-list@python.org
Subject: Re: Dict Copy & Compare

Robert Rawlins - Think Blue wrote:
> Hello Tim,
> 
> Sorry, that 'value' was a slip up on my part, we're just dealing with keys
> here.
> 
> I get that a dict stores unique keys only but we're comparing the two
dicts,
> so when I say 'unique keys in dict 1' I basically mean all those keys that
> are in dict one but not in dict 2. So imagine my 2 dicts with the
following
> keys.
> 
> Dict 1Dict 2
> -----
> 00:00:00:00   00:00:00:00
> 11:11:11:11   11:11:11:11
> 22:22:22:22   33:33:33:33
> 44:44:44:44   44:44:44:44
> 55:55:55:55
> 
> Now, 22:22:22:22 and 55:55:55:55 is unique to dict one, and 33:33:33:33 is
> unique to dict 2, does that make sense? Sorry for not explaining this
stuff
> very well, being so new to dicts its easy to get confused with my terms.
> 
> I then want to pass those keys as a string value into my function as an
> argument, like.
> 
> thisFunction('22:22:22:22')
> thisFunction('55:55:55:55')
> 
> thatFunction('33:33:33:33')
> 
> I'm hoping that your method will work for me, I've just got to spend my
time
> understanding what each step of it does.

Well I feel a bit guilty now I look back at your
original post, because I've probably given
you a more complex solution than you really need. Your
initial approach is probably quite adequate. Python
dicts are highly tuned beasts so unless you're doing
something really big or bizarre, you can sensibly do:


d1 = {
   "00:00:00:00" : None,
   "11:11:11:11" : None,
   "22:22:22:22" : None,
   "44:44:44:44" : None,
   "55:55:55:55" : None
}

d2 = {
   "00:00:00:00" : None,
   "11:11:11:11" : None,
   "33:33:33:33" : None,
   "44:44:44:44" : None
}

for k in d1:
   if d1 not in d2:
 thisFunction (d1)

for k in d2
   if d2 not in d1:
thatFunction (k)



But even if this is adequate for your purposes,
it's always good to be aware of what's in your
programming toolbox and there's always the danger
you'll end up implementing sets in dicts (which
is what everyone did before Python 2.3).

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

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


RE: Dict Copy & Compare

2007-04-30 Thread Robert Rawlins - Think Blue
Hello Tim,

Sorry, that 'value' was a slip up on my part, we're just dealing with keys
here.

I get that a dict stores unique keys only but we're comparing the two dicts,
so when I say 'unique keys in dict 1' I basically mean all those keys that
are in dict one but not in dict 2. So imagine my 2 dicts with the following
keys.

Dict 1  Dict 2
--  ---
00:00:00:00 00:00:00:00
11:11:11:11 11:11:11:11
22:22:22:22 33:33:33:33
44:44:44:44 44:44:44:44
55:55:55:55

Now, 22:22:22:22 and 55:55:55:55 is unique to dict one, and 33:33:33:33 is
unique to dict 2, does that make sense? Sorry for not explaining this stuff
very well, being so new to dicts its easy to get confused with my terms.

I then want to pass those keys as a string value into my function as an
argument, like.

thisFunction('22:22:22:22')
thisFunction('55:55:55:55')

thatFunction('33:33:33:33')

I'm hoping that your method will work for me, I've just got to spend my time
understanding what each step of it does.

Thanks again for all your help Tim,

Rob

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]
On Behalf Of Tim Golden
Sent: 30 April 2007 10:15
Cc: python-list@python.org
Subject: Re: Dict Copy & Compare

Robert Rawlins - Think Blue wrote:
> I have two dicts, one named 'this' and the other named 'that'.
> 
> I want to get all the unique keys from 'this' and log them into a file, I
> then want to take all the unique values from 'that' and log them into a
> separate file.

Couple of points which are confusing me:

1) Any dict can *only* have unique keys, ie you can't have
a key appearing more than once in a dictionary by
definition.

2) You speak of unique keys in "this" but unique values
in "that". Is that deliberate on your part? Might be, but
I'm not quite clear.

> I have functions set up for the logging, so I can call it like
> logThis(uniquekey) and logThat(uniquekey).

Here you refer to "uniquekey" in both cases, so maybe a
mistake above?

> So it's just a case of firstly returning a list of all keys that are in
> 'this' but NOT in 'that' and then visa versa, then loop over them
performing
> the function.

OK, well following by example earlier:


d1 = dict (a=1, b=2, c=3)
d2 = dict (b=4, c=5, d=6)

s1 = set (d1) # => set of 'a', 'b', 'c'
s2 = set (d2) # => set of 'b', 'c', 'd'

s1_not_in_s2 = s1 - s2 # => set of 'a'
s2_not_in_s1 = s2 - s1 # => set of 'd'

for key in s1_not_in_s2:
   print key, "=>", d1[key]

for key in s2_not_in_s1:
   print key, "=>", d2[key]



Obviously there are more concise ways of representing
this; I'm just spelling the whole thing out to make it
clearer (I hope). If this approach seems fruitful, have
a look at the set typeit's a recentish addition to
Python but very useful for this kind of thing:

   http://docs.python.org/lib/types-set.html

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

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


RE: Dict Copy & Compare

2007-04-30 Thread Robert Rawlins - Think Blue
Thanks for that Tim,

The first part for copying the dict seems to work nicely but I'm struggling
to get the second part working properly. Let me explain a little more
specifically what I'm trying to do.

I have two dicts, one named 'this' and the other named 'that'.

I want to get all the unique keys from 'this' and log them into a file, I
then want to take all the unique values from 'that' and log them into a
separate file.

I have functions set up for the logging, so I can call it like
logThis(uniquekey) and logThat(uniquekey).

So it's just a case of firstly returning a list of all keys that are in
'this' but NOT in 'that' and then visa versa, then loop over them performing
the function.

Rob

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]
On Behalf Of Tim Golden
Sent: 30 April 2007 09:41
Cc: python-list@python.org
Subject: Re: Dict Copy & Compare

Robert Rawlins - Think Blue wrote:
> I'm looking for a little advice on dicts, firstly I need to learn how to
> copy a dict, I suppose I could just something like.

> Self.newdict = self.olddict

> But I fear that this only creates a reference rather than an actual copy,
> this means that as soon as I clear out the old one, the new one will
> effectively be empty. What's the best way to ACTUALY copy a dict into a
new
> variable?

Unless you have specialised needs, you can just say:

d2 = dict (d1)

which will initialise d2 from d1's key/value pairs:


d1 = dict (a=1, b=2)
d2 = dict (d1)
d2['a'] = 5
print d1
print d2




> Next up I'm looking to compare two different dictionaries, then loop
through
> the unique results that are in each and print them out. Is there a more
> efficient way of doing this other than a loop with an if/else statement?

This comes up not infrequently on the list. I think there's
even a few recipes in the cookbook. One (fairly recent)
technique is to use set versions of your dictionary keys,
but it depends on what you want to do next. From my example
above:


# relies on the fact that dictionary iterators
# iterate over the keys of the dict.

s1 = set (d1)
s2 = set (d2)

# do whatever set-ops you want, eg

s3 = s1 | s2

for key in s3:
   print "Key:", key
   print "d1 =>", d1[key]
   print "d2 =>", d2[key]



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

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


Dict Copy & Compare

2007-04-30 Thread Robert Rawlins - Think Blue
Hello Guys,

 

I'm looking for a little advice on dicts, firstly I need to learn how to
copy a dict, I suppose I could just something like.

 

Self.newdict = self.olddict

 

But I fear that this only creates a reference rather than an actual copy,
this means that as soon as I clear out the old one, the new one will
effectively be empty. What's the best way to ACTUALY copy a dict into a new
variable?

 

Next up I'm looking to compare two different dictionaries, then loop through
the unique results that are in each and print them out. Is there a more
efficient way of doing this other than a loop with an if/else statement?

 

For a in self.dict1:

If a not in self.dict2:

Print 'Found %s' % (a)

 

For b in self.dict2:

If b not in self.dict1:

Print 'Found %s'  % (b)

 

That would firstly loop through the first dict and output any of its unique
values, it then loops through the second dict and output any of its unique
values, is this the best way of doing this? Or is there something more
efficient?

 

Thanks,

 

Rob Rawlins

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

RE: what python technology for my app?

2007-04-27 Thread Robert Rawlins - Think Blue
Haha, no Troll, just a shameless plug for my life's one true love ;-)

Rob

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]
On Behalf Of Bruno Desthuilliers
Sent: 27 April 2007 14:35
To: python-list@python.org
Subject: Re: what python technology for my app?

Robert Rawlins - Think Blue a écrit :
> Just thought I'd make a little suggestion about this, I don’t know how
> strict you want to be with the web development side of things, but I'm a
web
> developer by trade and have recently started using python for my non-web
> type applications.
> 
> If you're looking for a web based server side solution, then you can't go
> wrong with Adobe ColdFusion.

Err... is this a troll ?
-- 
http://mail.python.org/mailman/listinfo/python-list

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


RE: Re-ocurring Events

2007-04-27 Thread Robert Rawlins - Think Blue
Thanks for that tip Jarek, It worked a charm, I just created a format time
string and used that in the compare against my XML and it work perfectly.

Thank you,

Rob

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]
On Behalf Of Jarek Zgoda
Sent: 27 April 2007 13:01
To: python-list@python.org
Subject: Re: Re-ocurring Events

Robert Rawlins - Think Blue napisał(a):

> and I need to do and conditional that ensures start is before today's
> date/time and end is after today's date/time.
> 
> The string looks like: '2007-01-01 00:00:00'
> 
> Do I have to convert this to a proper time object to do the comparison? Or
> can I do it as a string?

In the very specific case of string formatted as above, the string
comparison will give the same results as in the case of datetime objects
comparison. You just have to compare the same kinds of things. ;)

-- 
Jarek Zgoda

"We read Knuth so you don't have to."
-- 
http://mail.python.org/mailman/listinfo/python-list

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


RE: what python technology for my app?

2007-04-27 Thread Robert Rawlins - Think Blue
Just thought I'd make a little suggestion about this, I don’t know how
strict you want to be with the web development side of things, but I'm a web
developer by trade and have recently started using python for my non-web
type applications.

If you're looking for a web based server side solution, then you can't go
wrong with Adobe ColdFusion. Its and incredibly powerful and scalable
development platform and yet supports a simple tag based language which
makes life very easy when getting started, and yet will support much more
complex OOP methodology with a whole myriad of frameworks, orm's and object
factories.

For a quick 'run down' of its features take a look at the simple tutorials
on the adobe site, they'll demonstrate how easy it is to achieve even quite
complex tasks like PDF creation, animated statistical charts an all sorts.
As CF is a JAVA based technology you get the power of J2EE platform and its
perfectly multi platform playing nicely with linux, unix, sun, windows or
whatever server environment you wish to run it on.

Just my two pence, I'm a big CF fan so my opinions are no doubt bias, but
might be worth you taking a look.

http://www.adobe.com/uk/products/coldfusion/

Pay attention to the 'ColdFusion Demos' at the bottom right of the main
column.

Rob

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]
On Behalf Of Bruno Desthuilliers
Sent: 27 April 2007 12:19
To: python-list@python.org
Subject: Re: what python technology for my app?

Chris a écrit :
> I have an database containing lots of numerical data. I want to write a 
> browser based interface that will allow selection of various key 
> parameters and yield tables, plots and/or printouts of the data 
> according to the selections. Ultimately I want this to run on an 
> intranet so that others can get access via their browsers.
> 
> The application is for in-house use only and not likely to have more 
> than a few users at any one time. I've managed to hack out enough 
> understanding of sql and sqlAlchemy over the last couple of days to 
> create and access an sqlite3 based database for my data. This seems to 
> suit my purposes for now. Now for a front end and some simple 
> distribution over a network.
> 
> I have written some small Wxpython & matplotlib apps for data analysis 
> and display but I'm not sure how these would work in a browser based 
> world, or even if they are appropriate.

wxPython being a GUI toolkit, it's of course not really appropriate for 
a web-based solution (and yes, this is an understatement).

I don't have any experience with matplotlib, but according to the 
project's FAQ, this shouldn't be a problem:
http://matplotlib.sourceforge.net/faq.html#BATCHMODE

> 
> Any advice on what technologies I should be looking at for this? Python 
> based naturally, and hopefully simple and lightweight. I'm not a 
> programmer by trade and that's not what I really get paid for but I've 
> learned to love python and its myriad of modules for all the data 
> analysis work I need to do. If I can throw something moderately 
> functional together in a week or two (along with all the learning that 
> entails)

If you don't have any experience with web programming, it might take a 
bit more time.

> I'll be happy.

Pylons (http://pylonshq.com) and turbogears are two great web 
frameworks. Both support SQLAlchemy. My own favourite is Pylons, but you 
should try both and choose the one that better fits your brain.

> btw - Platform needs to be windows because that's what on my desk.

Python is mostly platform independant.

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

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


RE: Re-ocurring Events

2007-04-27 Thread Robert Rawlins - Think Blue
Ok, Almost done now, it seems to be working a real charm at the moment.

I need a little help on performing a quick if statement against a date
string. I have couple of date strings returned by

s.attributes.getNamedItem("start").nodeValue
s.attributes.getNamedItem("end").nodeValue

and I need to do and conditional that ensures start is before today's
date/time and end is after today's date/time.

The string looks like: '2007-01-01 00:00:00'

Do I have to convert this to a proper time object to do the comparison? Or
can I do it as a string?

Thanks,

Rob

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]
On Behalf Of Robert Rawlins - Think Blue
Sent: 27 April 2007 09:55
To: 'Laurent Pointal'
Cc: python-list@python.org
Subject: RE: Re-ocurring Events

Thanks for getting back to me Laurent.

I've now made some pretty tidy progress on this and -think- it's going to
shape up nicely, I'm just working on converting my date strings from the XML
into a date_struct and we should be good to go.

I'll keep you all posted.

Rob

-Original Message-
From: Laurent Pointal [mailto:[EMAIL PROTECTED] 
Sent: 27 April 2007 09:49
To: Robert Rawlins - Think Blue
Subject: Re: Re-ocurring Events

Robert Rawlins - Think Blue a écrit :
> Thank you guys for your suggestions.
> 
> I've been having a look at that launchd stuff from apple but couldn’t
really
> see how that applies to my requirements.

I was not thinking about launchd itself, but about its XML configuration 
files which manage timed events, as you need.

> I've been putting some serious thought into how this should work as its
> essentially the final part of the puzzle for my application, I'll have a
> fully working model for my application.
> 
> I've been thinking about the possibility of using a combination of xpath
to
> search the XML with some loops which change the date. As events won't ever
> be set to start before 01-01-2007 I can set that as the ceiling for my
loop.
> So when I'm searching for weekly events, I 'simply' take today's date and
> time and loop from now until 01-01-2007 decrementing the date by a week
each
> iteration of the loop and then search the XML for events in that date,
make
> sense?
> 
> I know that's a fairly intensive way of doing it, but if it works it
works.
> 
> Now, the loop is where I'm really struggling, I've not done any looping
with
> dates, can anyone give me a hand with this? How can I loop back in time
from
> now to beginning of 07 a week at a time? Do we have some form of dateAdd()
I
> can use with a while loop? Perhaps.

See datetime module, eventually third party mxDatetime.

> Date = (now)
> While date > 2007-01-01:
>   Date = dateAdd(date, -1, w)
> 
> Something to that effect? Then I can quickly xpath for every iteration of
> the loop.

A+

Laurent.

-- 
Laurent POINTAL
CNRS-LIMSI dépt. CHM, groupes AMI et PS
Courriel: [EMAIL PROTECTED](prof)
   [EMAIL PROTECTED] (perso)
Ouebe: http://www.limsi.fr/Individu/pointal/
Tél. 01 69 85 81 06 (prof)
Fax. 01 69 85 80 88



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

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


RE: Re-ocurring Events

2007-04-27 Thread Robert Rawlins - Think Blue
Thanks for getting back to me Laurent.

I've now made some pretty tidy progress on this and -think- it's going to
shape up nicely, I'm just working on converting my date strings from the XML
into a date_struct and we should be good to go.

I'll keep you all posted.

Rob

-Original Message-
From: Laurent Pointal [mailto:[EMAIL PROTECTED] 
Sent: 27 April 2007 09:49
To: Robert Rawlins - Think Blue
Subject: Re: Re-ocurring Events

Robert Rawlins - Think Blue a écrit :
> Thank you guys for your suggestions.
> 
> I've been having a look at that launchd stuff from apple but couldn’t
really
> see how that applies to my requirements.

I was not thinking about launchd itself, but about its XML configuration 
files which manage timed events, as you need.

> I've been putting some serious thought into how this should work as its
> essentially the final part of the puzzle for my application, I'll have a
> fully working model for my application.
> 
> I've been thinking about the possibility of using a combination of xpath
to
> search the XML with some loops which change the date. As events won't ever
> be set to start before 01-01-2007 I can set that as the ceiling for my
loop.
> So when I'm searching for weekly events, I 'simply' take today's date and
> time and loop from now until 01-01-2007 decrementing the date by a week
each
> iteration of the loop and then search the XML for events in that date,
make
> sense?
> 
> I know that's a fairly intensive way of doing it, but if it works it
works.
> 
> Now, the loop is where I'm really struggling, I've not done any looping
with
> dates, can anyone give me a hand with this? How can I loop back in time
from
> now to beginning of 07 a week at a time? Do we have some form of dateAdd()
I
> can use with a while loop? Perhaps.

See datetime module, eventually third party mxDatetime.

> Date = (now)
> While date > 2007-01-01:
>   Date = dateAdd(date, -1, w)
> 
> Something to that effect? Then I can quickly xpath for every iteration of
> the loop.

A+

Laurent.

-- 
Laurent POINTAL
CNRS-LIMSI dépt. CHM, groupes AMI et PS
Courriel: [EMAIL PROTECTED](prof)
   [EMAIL PROTECTED] (perso)
Ouebe: http://www.limsi.fr/Individu/pointal/
Tél. 01 69 85 81 06 (prof)
Fax. 01 69 85 80 88



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


RE: : Re-ocurring Events

2007-04-27 Thread Robert Rawlins - Think Blue
Just as another quick update, I've found this module for python that may be
able to help me, its designed to spot date patterns for things like
recurring events I guess,

http://www.biostat.wisc.edu/~annis/creations/period.py.html

Quite how I can integrate it into my project I'm not sure but if I can loop
through each of the events in my XML and see if they are meant to be
recurring today then that's fantastic.

If anyone has any rough ideas on how this might work for me I'd love to hear
some rough concepts.

Thanks,

Rob

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]
On Behalf Of Robert Rawlins - Think Blue
Sent: 27 April 2007 08:24
To: python-list@python.org
Subject: RE:: Re-ocurring Events

Thank you guys for your suggestions.

I've been having a look at that launchd stuff from apple but couldn’t really
see how that applies to my requirements.

I've been putting some serious thought into how this should work as its
essentially the final part of the puzzle for my application, I'll have a
fully working model for my application.

I've been thinking about the possibility of using a combination of xpath to
search the XML with some loops which change the date. As events won't ever
be set to start before 01-01-2007 I can set that as the ceiling for my loop.
So when I'm searching for weekly events, I 'simply' take today's date and
time and loop from now until 01-01-2007 decrementing the date by a week each
iteration of the loop and then search the XML for events in that date, make
sense?

I know that's a fairly intensive way of doing it, but if it works it works.

Now, the loop is where I'm really struggling, I've not done any looping with
dates, can anyone give me a hand with this? How can I loop back in time from
now to beginning of 07 a week at a time? Do we have some form of dateAdd() I
can use with a while loop? Perhaps.

Date = (now)
While date > 2007-01-01:
Date = dateAdd(date, -1, w)

Something to that effect? Then I can quickly xpath for every iteration of
the loop.

Thanks guys for any help.

Rob

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]
On Behalf Of Laurent Pointal
Sent: 26 April 2007 15:33
To: python-list@python.org
Subject: Re: Re-ocurring Events

Daniel Nogradi a écrit :
>> A bit more of a complex one this time, and I thought I'd get your 
>> opinions
>> on the best way to achieve this. Basically I'm looking for a way to 
>> describe
>> a re-occurring event, like a calendar event or appointment I guess. I'm
>> likely to use an XML file for the definition of the events, but 
>> imagine I've
>> got an event that looks something like this.
>>
>> > repeat="daily"
>> />

May take a look at launchd (Apple) XML dialect, it may have at least the 
semantic for your need.

For other parts... elementtree, yes.

>> Now what I want to do is be able to build a class which has a function 
>> like
>> 'getCurrentEvent()' which will return any events that should be 
>> occurring at
>> that time. So if the current system time and date is 2007-01-03 13:00:00
>> then it will return THAT event to me, but if it was say 2007-01-03 
>> 16:00:00
>> then it would not, as the event isn't 'due' to occur at that time. Make
>> sense?
>>
>> What's the best way of handling this? I'm really a little lost as to 
>> how I
>> might get started, checking a static date time isn't a problem, it's 
>> when it
>> comes to these re-occurring events that I struggle a little. The idea is
>> that I would have 5 core repetitions, none, daily, weekly, monthly and
>> annually.
> 
> This will not solve all your problems, but a very convenient way of
> handling XML is the element tree module (that comes with python 2.5):
> http://docs.python.org/lib/module-xml.etree.ElementTree.html
> 
> HTH,
> Daniel
-- 
http://mail.python.org/mailman/listinfo/python-list

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

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


RE:: Re-ocurring Events

2007-04-27 Thread Robert Rawlins - Think Blue
Thank you guys for your suggestions.

I've been having a look at that launchd stuff from apple but couldn’t really
see how that applies to my requirements.

I've been putting some serious thought into how this should work as its
essentially the final part of the puzzle for my application, I'll have a
fully working model for my application.

I've been thinking about the possibility of using a combination of xpath to
search the XML with some loops which change the date. As events won't ever
be set to start before 01-01-2007 I can set that as the ceiling for my loop.
So when I'm searching for weekly events, I 'simply' take today's date and
time and loop from now until 01-01-2007 decrementing the date by a week each
iteration of the loop and then search the XML for events in that date, make
sense?

I know that's a fairly intensive way of doing it, but if it works it works.

Now, the loop is where I'm really struggling, I've not done any looping with
dates, can anyone give me a hand with this? How can I loop back in time from
now to beginning of 07 a week at a time? Do we have some form of dateAdd() I
can use with a while loop? Perhaps.

Date = (now)
While date > 2007-01-01:
Date = dateAdd(date, -1, w)

Something to that effect? Then I can quickly xpath for every iteration of
the loop.

Thanks guys for any help.

Rob

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]
On Behalf Of Laurent Pointal
Sent: 26 April 2007 15:33
To: python-list@python.org
Subject: Re: Re-ocurring Events

Daniel Nogradi a écrit :
>> A bit more of a complex one this time, and I thought I'd get your 
>> opinions
>> on the best way to achieve this. Basically I'm looking for a way to 
>> describe
>> a re-occurring event, like a calendar event or appointment I guess. I'm
>> likely to use an XML file for the definition of the events, but 
>> imagine I've
>> got an event that looks something like this.
>>
>> > repeat="daily"
>> />

May take a look at launchd (Apple) XML dialect, it may have at least the 
semantic for your need.

For other parts... elementtree, yes.

>> Now what I want to do is be able to build a class which has a function 
>> like
>> 'getCurrentEvent()' which will return any events that should be 
>> occurring at
>> that time. So if the current system time and date is 2007-01-03 13:00:00
>> then it will return THAT event to me, but if it was say 2007-01-03 
>> 16:00:00
>> then it would not, as the event isn't 'due' to occur at that time. Make
>> sense?
>>
>> What's the best way of handling this? I'm really a little lost as to 
>> how I
>> might get started, checking a static date time isn't a problem, it's 
>> when it
>> comes to these re-occurring events that I struggle a little. The idea is
>> that I would have 5 core repetitions, none, daily, weekly, monthly and
>> annually.
> 
> This will not solve all your problems, but a very convenient way of
> handling XML is the element tree module (that comes with python 2.5):
> http://docs.python.org/lib/module-xml.etree.ElementTree.html
> 
> HTH,
> Daniel
-- 
http://mail.python.org/mailman/listinfo/python-list

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


Re-ocurring Events

2007-04-26 Thread Robert Rawlins - Think Blue
Hello Chaps,

 

A bit more of a complex one this time, and I thought I'd get your opinions
on the best way to achieve this. Basically I'm looking for a way to describe
a re-occurring event, like a calendar event or appointment I guess. I'm
likely to use an XML file for the definition of the events, but imagine I've
got an event that looks something like this.

 



 

Now what I want to do is be able to build a class which has a function like
'getCurrentEvent()' which will return any events that should be occurring at
that time. So if the current system time and date is 2007-01-03 13:00:00
then it will return THAT event to me, but if it was say 2007-01-03 16:00:00
then it would not, as the event isn't 'due' to occur at that time. Make
sense?

 

What's the best way of handling this? I'm really a little lost as to how I
might get started, checking a static date time isn't a problem, it's when it
comes to these re-occurring events that I struggle a little. The idea is
that I would have 5 core repetitions, none, daily, weekly, monthly and
annually.

 

Or perhaps you guys have a more full proof method of describing the event in
the XML?

 

Any suggestions are more than welcome guys, thanks again for all your help
recently.

 

Rob

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

Scheduler Module Help

2007-04-26 Thread Robert Rawlins - Think Blue
Hello Guys,

 

I'm using the sched module to create a set of functions that run every 10 or
20 minutes whilst the application is running, however it would seem that the
moment I run scheduler.run() it prevents any other operations in my
application from running, its sits dormant until it runs the scheduled
functions.

 

Any ideas on what might be the root of this issue?

 

Thanks,

 

Rob

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

Now()

2007-04-25 Thread Robert Rawlins - Think Blue
Hello Guys,

 

I'm using the following function 'str (now)' to place a date time stamp into
a log file, which works fine, however it writes the stamp like this.

 

2007-04-25 11:06:53.873029

 

But I need to expel those extra decimal places as they're causing problems
with the application that parses the log data, all I need is something like
this:

 

2007-04-25 11:06:53

 

With time depicted to the nearest second, from my background in ColdFusion
development we used to have a datetimeformat() function that I could use as

 

DateTimeFormat(now(), "-mm-dd HH:mm:ss")

 

Which would give the current time a mask.

 

Any equivalent for this in python?

 

Thanks,

 

Rob

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

popen2 results

2007-04-25 Thread Robert Rawlins - Think Blue
Hello guys,

 

I've recently ported my application from bash to python, however there are
still a few bash line utilities I -have- to use in the application as there
isn't any alternative available to me. In the old days of bash I would have
grep'd the output from these commands to determine the outcome.

 

I'm now using popen2 to run the command which works a charm, but I'm
struggling to parse the results of the function, has anyone had any
experience with this? I've found a few suggested solutions dotted around,
such as this one.

 

 import os

 

 def filtered(command, source):

 dest, result = os.popen2(command)

 dest.write(source)

 dest.close()

 try:

  return result.read()

 finally:

  result.close()

 

But to be honest I'm struggling to get it to do anything as it doesn't
states what the 'source' object is or should be.

 

Thanks for any help guys, I'm just looking to capture the output from the
command and then I can go about a little REGEX on it.

 

Thanks,

 

Rob

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

If Dict Contains...

2007-04-25 Thread Robert Rawlins - Think Blue
Hello Guys,

 

Looking to build a quick if/else statement that checks a dictionary for a
key like follows.

 

If myDict contains ThisKey:

Do this...

Else

Do that...

 

Thats the best way of doing this?

 

Thanks,

 

Rob

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

RE: If Dict Contains a particular key

2007-04-25 Thread Robert Rawlins - Think Blue
Thanks guys for this, glad it was so simple.

 

I used mikes solution in the end, and it worked a charm.

 

Thanks again,

 

Rob

 

From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]
On Behalf Of Michael Bentley
Sent: 24 April 2007 18:37
To: python-list@python.org
Subject: Re: If Dict Contains a particular key

 

 

On Apr 24, 2007, at 12:28 PM, Robert Rawlins - Think Blue wrote:





Hello Guys,

 

I'm Looking to build a quick if/else statement that checks a dictionary for
a key like follows.

 

If myDict contains ThisKey:

Do this...

Else

Do that...

 

Thats the best way of doing this?

if ThisKey in myDict:

  pass # do this

else:

  pass # do that

 

 

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

If Dict Contains a particular key

2007-04-24 Thread Robert Rawlins - Think Blue
Hello Guys,

 

I'm Looking to build a quick if/else statement that checks a dictionary for
a key like follows.

 

If myDict contains ThisKey:

Do this...

Else

Do that...

 

Thats the best way of doing this?

 

Thanks,

 

Rob

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

Minidom Help

2007-04-24 Thread Robert Rawlins - Think Blue
Hello guys,

 

Not got much experience with working with minidom, but I'm looking to parse
this XML and retrieve the 'name' value from the xml.

 







Think Blue

0x1002

1





 

I've got as far as parsing the XML using self.doc =
xml.dom.minidom.parse(self.filepath) but that's as far as I've managed to
get. I've spent a little time googling around but it's all double Dutch to
me.

 

Any ideas?

 

Thanks,

 

Rob

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

Styled Output

2007-04-21 Thread Robert Rawlins - Think Blue
Chaps,

 

Hope you're all having a good weekend, I'm sure it'll only be the more 'hard
core' of you reading this, anyone with any sanity would be out in the
sunshine right now.

 

I'm running a program of mine from Linux bash script and it currently
outputs status messages to the command prompt, but they all look a little
boring to say the least, it'll be like this.

 

Application Started

Sucess

Application Configured

Failed

Retry

Application Configured

Success

 

What I'd ideally like to do it give it a little bit of format, so it perhaps
looks something like this.

 

Application Started
[Success]

Application Configured
[Success]

 

And perhaps have the word 'success' in green, or red for 'failed' and things
like that, just to make it a little more presentable to the user.

 

Any ideas on the best way to achieve this?

 

Thanks,

 

Rob

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

Class Not Auto-Init On Import

2007-04-21 Thread Robert Rawlins - Think Blue
Hello Guys,

 

>From my understanding of what I've read, the 'import' is meant to auto Init
my class ready for me to access its methods, but it doesn't appear too, I'm
having to init them myself before I can access them, like this.

 

import LocationService

 

Location = LocationService.LocationService()

 

LocationService.setIP('192.168.1.1')

 

Why is this the case? Should i not just be able to access the setIP() method
by doing LocationService.setIP('192.168.1.1') Without having to create my
own inited reference?

 

Thanks,

 

Rob

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

Schedule Task

2007-04-21 Thread Robert Rawlins - Think Blue
Hello Guys,

 

I've got a method in my app that I want to run every 20 minutes that the
application it running, what's the best way of achieving this? At the moment
I'm using this kind of method.

 

import sched

import time, sys

scheduler = sched.scheduler(time.time, time.sleep)

 

scheduler.enter(1.0, 0, sys.stdout.write, ("one\n",))

scheduler.run()

 

Now when the function runs after 20 minutes I just have it schedule itself
to run again in 20 minutes time, which seems to work for me quite nicely, it
just seems a little 'hacky' to keep the task reoccurring like that.

 

Is there any other method? I've spent some time on Google but couldn't
really find anything. Not sure if it makes any difference but I'm running
Linux.

 

Thanks,

 

Rob

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

RE: Byte-Array to String

2007-04-20 Thread Robert Rawlins - Think Blue
Thanks for that Carsten,

I've given that a go and I still get similar results to what I've seen in the 
past whereby it prints a couple of elements I would expect to see but other 
which I'm not sure about, and what seems to be ALOT of blank characters.

6e
555 5   5   5   en  j   
5   %OBEX Object Push   ÿ

That's what your suggestion returns to me, I'm using the API to scan a 
Bluetooth device for services, one of which is OBEX Object Push, but the rest 
of that output is apparent junk.

Unfortunately the API doesn’t really give any suggestions at all, take a look 
here http://bluez.cvs.sourceforge.net/*checkout*/bluez/utils/hcid/dbus-api.txt 
if you look down under the 'adapter' section, I'm running the method called 
GetRemoteServiceRecord() which returns the byte array but there really isn’t 
any suggestion as to how you extract the information from what's returned.

I have no idea if we're barking up the right tree here or if we're going about 
this the wrong way.

Thanks again for all your helps guys,

Rob

-Original Message-
From: Carsten Haese [mailto:[EMAIL PROTECTED] 
Sent: 20 April 2007 14:00
To: Robert Rawlins - Think Blue
Cc: python-list@python.org
Subject: RE: Byte-Array to String

On Fri, 2007-04-20 at 09:51 +0100, Robert Rawlins - Think Blue wrote:
> Morning Steve,
> 
>  
> 
> That stuff looks mighty promising, I did play around with the
> toString() function yesterday but couldn’t get the damned thing
> working.

That's because Steven seems to have given you suboptimal advice. The
thing you're working with is a dbus.Array of dbus.Byte, not a Python
array, so Python's standard array module is not going to help you.

When you work with an API function and you don't know what to do with
the results of calling that function, the documentation for that API
should be your first place to look for help. I don't know what you're
referencing, but Google tells me that the python-dbus API is documented
here: http://dbus.freedesktop.org/doc/dbus-python/api/

So, what do you do with a dbus.Array full of instances of dbus.Byte?
Let's look at their documentation,
http://dbus.freedesktop.org/doc/dbus-python/api/dbus.Array-class.html
and
http://dbus.freedesktop.org/doc/dbus-python/api/dbus.Byte-class.html,
respectively.

The page about dbus.Array tells us that it inherits from list, so you
should be able to do indexed access into it and iterate over it.
dbus.Byte inherits from int, and the documentation says that a dbus.Byte
can be converted to a character (string of length 1) by calling str or
chr on it.

So, to get a string from your dbus.Array, you could do something like
this:

s = ""
for b in myDbusArray:
  s += chr(b)

However, it's better (faster) to use the join idiom for building a
string char-by-char:

s = "".join(chr(b) for b in myDbusArray)

That ought to give you the string you're asking for.

HTH,

Carsten.



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

RE: Byte-Array to String

2007-04-20 Thread Robert Rawlins - Think Blue
Morning Steve,

 

That stuff looks mighty promising, I did play around with the toString()
function yesterday but couldn't get the damned thing working. The syntax has
me a little muddled, perhaps you can help out on this by taking a look at my
code.

 

#!/usr/bin/python

import dbus

 

bus = dbus.SystemBus()

obj = bus.get_object('org.bluez', '/org/bluez')

obj = bus.get_object('org.bluez', '/org/bluez/hci0')

adapter = dbus.Interface(obj, 'org.bluez.Adapter')

 

# Search For Obex Push Protocol

result = adapter.GetRemoteServiceHandles('00:17:B0:A0:E7:09', 'opp')

 

result2 = adapter.GetRemoteServiceRecord('00:17:B0:A0:E7:09', result[0])

 

Now 'result2' is basically that byte array, Unfortunately the API doesn't
give any more information other than that the function returns a array{byte}
so I can't really shed any more light on that however if I can just get it
to output all the elements from that array then I will know which one it is
I'm looking for.

 

I tried to implement your suggestions below as they look pretty safe, but I
couldn't get it to work, my syntax is pretty confused as I'm so new to the
language. Like I say, there is only one element of the array I'm interested
in, I just need to see them all before I know which it is.

 

Thanks again Steve,

 

Rob

 

From: Steven Howe [mailto:[EMAIL PROTECTED] 
Sent: 19 April 2007 16:51
To: Robert Rawlins - Think Blue
Cc: python-list@python.org
Subject: Re: Byte-Array to String

 

Robert Rawlins - Think Blue wrote: 

Hello Guys,
 
 
 
I have a byte array passed to me by dbus and I'm looking to convert it into
a string? Is that possible? Sorry for seeming like a putts with these
questions, I'm not used to all these complex data types :-D
 
 
 
The byte array looks something like this when printed to screen.
 
 
 
dbus.Array([dbus.Byte(54), dbus.Byte(0), dbus.Byte(24), dbus.Byte(9),
dbus.Byte(0), dbus.Byte(0), dbus.Byte(10), dbus.Byte(0), dbu
s.Byte(0), dbus.Byte(0), dbus.Byte(0), dbus.Byte(9), dbus.Byte(0),
dbus.Byte(1), dbus.Byte(53), dbus.Byte(3), dbus.Byte(25), dbus.
Byte(16), dbus.Byte(0), dbus.Byte(9), dbus.Byte(2), dbus.Byte(0),
dbus.Byte(53), dbus.Byte(3), dbus.Byte(9), dbus.Byte(1), dbus.By
te(1)], signature=dbus.Signature('y'))
 
 
 
Thanks again,
 
 
 
Rob
 

When reading about array, I wondered what the hell it was good for. Now I
see. It's a tool to build objects 
to pass to the Operating System or other applications. Something like
ctypes. The OS might store data from left to right, or right to left, or not
use IEEE standards (which VMS certainly doesn't). So the data you give/get
from the system call must be massaged by the application before it's usable.

python/lib/module-array.html (5.14 array -- Efficient arrays of numeric
values)
array.tostring( )
Convert the array to an array of machine values and return the string
representation (the same sequence of bytes that would be written to a file
by the tofile() method.)

I wonder if this is the method you are looking for. 
So you have an object dbus.Array, which, obviously is from a call to the
dbus (another application's data) that contains 28 byte arrays. I would
assume you know which you want, say the first one.

myDbusString01 = dbus.Array[0].tostring() 

or to get the lot:

myDbusStrings = []  #create a new empty list
for array in dbus.Array:
myDbusStrings.append( array.tostring() )

At this point you should have the array converted. But you will still need a
reference as to what you have. The call to the dbus should have some
documentation abut what it's returning. 
Also I'd expect the second example to be very bad programming, as some of
the array elements are probably not going to be characters. They could be
integers, floats or booleans. So creating a dictionary to handle specific
array element handling is probably a better, less error prone, method of
attack.

Not have the contents and defination of your dbus.array handy, I can't test
this, but the approach seems reasonable.

Steven Howe

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

RE: Byte-Array to String

2007-04-19 Thread Robert Rawlins - Think Blue
Thanks for getting back to me on this Tim, *pauses pulling his hair out for
a moment.

I'm back on this damn API and its driving me crazy I get three options
really for returning a service record from the API, they look like this.

array{uint32} GetRemoteServiceHandles(string address, string
match)

This method will request the SDP database of a
remote
device and retrieve the service record handles. To
request service browse send an empty match string.

Possible errors: org.bluez.Error.InvalidArguments
 org.bluez.Error.InProgress

org.bluez.Error.ConnectionAttemptFailed
 org.bluez.Error.Failed

array{byte} GetRemoteServiceRecord(string address, uint32
handle)

This method will request the SDP database of a
remote
device for a service record and return the binary
stream of it.

Possible errors: org.bluez.Error.InvalidArguments
 org.bluez.Error.InProgress
 org.bluez.Error.Failed

string GetRemoteServiceRecordAsXML(string address, uint32
handle)

This method will request the SDP database of a
remote
device for a service record and return its data in
XML
format.

Possible errors: org.bluez.Error.InvalidArguments
 org.bluez.Error.InProgress
 org.bluez.Error.Failed

The first method, when I print its results just gives me
dbus.Array([dbus.UInt32(65547L)], signature=dbus.Signature('u')) the method
gives me that byte array and the third doesn't appear to work at all :-D

Unfortunately nowhere seems to document what exactly those arrays contain,
so I'm trying to crack one open and find out, but as you can see, I'm not
having much luck. Perhaps this is a DBus thing, they do have a mailing list
so I'll give them a go in a while.

If you have any brain waves let me know mate, I'll add another beer to the
tab,

Rob


-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]
On Behalf Of Tim Golden
Sent: 19 April 2007 15:54
Cc: python-list@python.org
Subject: Re: Byte-Array to String

Robert Rawlins - Think Blue wrote:
> I have a byte array passed to me by dbus and I'm looking to convert it
into
> a string? Is that possible? Sorry for seeming like a putts with these
> questions, I'm not used to all these complex data types :-D
> 
> dbus.Array([dbus.Byte(54), dbus.Byte(0), dbus.Byte(24), dbus.Byte(9),
> dbus.Byte(0), dbus.Byte(0), dbus.Byte(10), dbus.Byte(0), dbu
> s.Byte(0), dbus.Byte(0), dbus.Byte(0), dbus.Byte(9), dbus.Byte(0),
> dbus.Byte(1), dbus.Byte(53), dbus.Byte(3), dbus.Byte(25), dbus.
> Byte(16), dbus.Byte(0), dbus.Byte(9), dbus.Byte(2), dbus.Byte(0),
> dbus.Byte(53), dbus.Byte(3), dbus.Byte(9), dbus.Byte(1), dbus.By
> te(1)], signature=dbus.Signature('y'))

No idea, but what happens when you iterate over it?

for i in array_thingy:
   print i

Or does it support the buffer interface?

for i in buffer (array_thingy):
   print i

If it did then you can at least get access to its
innards and construct some kind of string representation.
I've no idea what the numbers are supposed to represent,
so I don't know what "convert it to a string" is likely
to imply. This looks rather more like a dbus question than
a strictly Python one. Is there a DBus mailing list or
what-have-you?

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

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


Byte-Array to String

2007-04-19 Thread Robert Rawlins - Think Blue
Hello Guys,

 

I have a byte array passed to me by dbus and I'm looking to convert it into
a string? Is that possible? Sorry for seeming like a putts with these
questions, I'm not used to all these complex data types :-D

 

The byte array looks something like this when printed to screen.

 

dbus.Array([dbus.Byte(54), dbus.Byte(0), dbus.Byte(24), dbus.Byte(9),
dbus.Byte(0), dbus.Byte(0), dbus.Byte(10), dbus.Byte(0), dbu
s.Byte(0), dbus.Byte(0), dbus.Byte(0), dbus.Byte(9), dbus.Byte(0),
dbus.Byte(1), dbus.Byte(53), dbus.Byte(3), dbus.Byte(25), dbus.
Byte(16), dbus.Byte(0), dbus.Byte(9), dbus.Byte(2), dbus.Byte(0),
dbus.Byte(53), dbus.Byte(3), dbus.Byte(9), dbus.Byte(1), dbus.By
te(1)], signature=dbus.Signature('y'))

 

Thanks again,

 

Rob

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

RE: Signals

2007-04-18 Thread Robert Rawlins - Think Blue
Thanks Carsten,

Sorry for not mentioning the dbus before, I thought that the signals were a 
general python thing, I didn’t realize it was a specific dbus thing.

I've now modified the code so it looks like this.

#!/usr/bin/python

import dbus
from gobject import MainLoop, idle_add
from dbus.mainloop.glib import DBusGMainLoop

def main_function():
bus = dbus.SystemBus()
manager = dbus.Interface(bus.get_object('org.bluez', 
'/org/bluez'),'org.bluez.Manager')
adapter = dbus.Interface(bus.get_object('org.bluez', 
manager.DefaultAdapter()),'org.bluez.Adapter')

def remote_device_found(addr, class_, rssi):
print 'Found:', addr

adapter.connect_to_signal('RemoteDeviceFound', remote_device_found)

adapter.DiscoverDevices()

if __name__ == '__main__':
idle_add(main_function)
dbus_mainloop_wrapper = DBusGMainLoop(set_as_default=True)
mainloop = MainLoop()
mainloop.run()

Which should implement the main loop and keep the application alive, however 
when running the program I get the following error thrown at me.

File "./scan4.py", line 5, in ?
from dbus.mainloop.glib import DBusGMainLoop
ImportError: No module named mainloop.glib

Any ideas what might be causing this? My guess is that I don’t have one of the 
dependency installed properly, but I have no idea which ones I need.

Thanks for any more help you can offer, its greatly appreciated.

Rob 
-Original Message-----
From: Carsten Haese [mailto:[EMAIL PROTECTED] 
Sent: 18 April 2007 13:43
To: Robert Rawlins - Think Blue
Cc: python-list@python.org
Subject: Re: Signals

On Wed, 2007-04-18 at 08:39 +0100, Robert Rawlins - Think Blue wrote:
> Hello Chaps,
> 
>  
> 
> I posted about this the other day but I’m still struggling to get any
> form of result from it. Basically I have the following piece of code,
> and according to its API is produces singals when particular events
> occur, but i have no idea how to list for events, I’ve tried all sorts
> of combinations using the signal module but haven’t been able to get
> it working.
> [...]
> import dbus, signal
> [...]
>
> Now here is a copy of the API documentation that lists the signals
> thrown by the API, this is the one I’m trying to listen for.
> 
>  
> 
> void RemoteDeviceFound(string address, uint32 class, int16
> rssi)
> 
>  
> 
>This signal will be send every time an inquiry result
> 
>has been found by the service daemon. In general they
> 
>only appear during a device discovery.
> 
>  
> 
> Basically I’m just looking to run a function that will print those
> details to screen. Can anyone help with working out how to listen for
> this signal?

The signal module is for handling process signals the operating system
sends to your python process. The API you're using (dbus, a crucial
detail you neglected to mention before) doesn't send this kind of
signal.

Googling for 'python dbus documentation' brings up
http://dbus.freedesktop.org/doc/dbus-python/api/ , which seems to
explain everything you need. You need to get an instance of
dbus.Interface, which you seem to have done, and call its
connect_to_signal() method, which you don't seem to have done.

>  I’d also like to know how i can keep the application running until I
> decided to stop it manually, as the DiscoverDevices() can take a while
> to complete and send out several RemoteDeviceFound() signals in that
> period.

The very first section of the above mentioned documentation talks about
needing a main loop for receiving signals, and it provides example
boilerplate code for how to set up a main loop.

Hope this helps,

Carsten.



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

array{uint32}

2007-04-18 Thread Robert Rawlins - Think Blue
Guys,

 

I have a function that returns a array{uint32}, is there any way to output
that to screen in a more user friendly format? At the moment when I print it
by itself it appears as [65541L], which isn't much used to anyone.

 

I know this is probably a REALLY dumb question but I'm a little lost with
this API, its driving me nuts.

 

Thanks again guys,

 

Rob

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

Signals

2007-04-18 Thread Robert Rawlins - Think Blue
Hello Chaps,

 

I posted about this the other day but I'm still struggling to get any form
of result from it. Basically I have the following piece of code, and
according to its API is produces singals when particular events occur, but i
have no idea how to list for events, I've tried all sorts of combinations
using the signal module but haven't been able to get it working.

 

#!/usr/bin/python

 

import dbus, signal

 

bus = dbus.SystemBus()

obj = bus.get_object('org.bluez', '/org/bluez')

obj = bus.get_object('org.bluez', '/org/bluez/hci0')

adapter = dbus.Interface(obj, 'org.bluez.Adapter')

 

adapter.DiscoverDevices()

 

Now here is a copy of the API documentation that lists the signals thrown by
the API, this is the one I'm trying to listen for.

 

void RemoteDeviceFound(string address, uint32 class, int16 rssi)

 

   This signal will be send every time an inquiry result

   has been found by the service daemon. In general they

   only appear during a device discovery.

 

Basically I'm just looking to run a function that will print those details
to screen. Can anyone help with working out how to listen for this signal?
I'd also like to know how i can keep the application running until I decided
to stop it manually, as the DiscoverDevices() can take a while to complete
and send out several RemoteDeviceFound() signals in that period.

 

Thanks guys

 

Rob

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

Deleting or Empty a File

2007-04-17 Thread Robert Rawlins - Think Blue
I'm looking to clear those log files we were discussing earlier chaps,

 

What the best method to do this? Delete the file completely? Or just empty
its content?

 

Thanks,

 

Rob

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

RE: file resume

2007-04-16 Thread Robert Rawlins - Think Blue
Do the same again, but change that 'wb' to 'a' for append :-D should sort
you out.

Rob

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]
On Behalf Of luca72
Sent: 16 April 2007 16:57
To: python-list@python.org
Subject: file resume

Hello at all:
if i have one file written in binary mode, how can i append others
binary data to this file after the its closure.
ex
my_file = open('blabal', 'wb')
then i write something and then
my_file.close()
now if i need to open it again and append other binary data how can i
proceed?


Regards

Luca

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

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


RE: Writing Log CSV (Efficiently)

2007-04-16 Thread Robert Rawlins - Think Blue

Thanks for that Tim,

I could use a little more help with this CSV stuff this afternoon and I
can't get it to write the output I want for the life of me. I'm trying to
write a method for my logging class that receives a string as an argument,
and then writes a row to the CSV with the string and a date/time stamp.

''' Add Application Log Entry '''
def addApp(self, event):
writer = csv.writer(open("some.csv", "a"))
writer.writerow(event)

Now if I do something like this; addApp('Application Started') then it
writes to the CSV file somthing like.

A,p,p,l,i,c,a,t,i,o,n, ,S,t,a,r,t,e,d

Which isn't much use to me :-D any ideas how I can get something like this:

2007-01-01,13:00:00,Application Started

Thanks,

Rob
-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]
On Behalf Of Tim Golden
Sent: 16 April 2007 15:28
Cc: python-list@python.org
Subject: Re: Writing Log CSV (Efficiently)

Robert Rawlins - Think Blue wrote:
> The log at its highest rate of write may be looking at an operation a
> second

I think I can probably type stuff in faster than that if
I try :) You probably don't have a performance issue there.

, I've not got much experience with this kind of thing so 
I'm not sure
> if that's 'a lot' or not, it just seems like it at the moment. It might
not
> get as busy as that, I'm not sure and its difficult to simulate as this
> isn't likely to be a steady flow of traffic, they'll come in big fat lumps
> every now and then.

Sounds like you don't really need to profile that, but if
you did, Python's a great language for knocking together
that kind of test harness; combine the time, random and
csv modules and you've got a "big fat lumps every now and
then" simulation. (At which point I get jumped on by the
serious model types for being so blase with their discipline!)

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

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


RE: Writing Log CSV (Efficiently)

2007-04-16 Thread Robert Rawlins - Think Blue
Hello Guys,

Thanks for the advice on this one. I'm running Debian Linux as an OS, if
that makes any major differences. That inbuilt CSV stuff looked pretty tidy,
as does the logging. I'd be keen to learn a little more about that
performance wise though.

The log at its highest rate of write may be looking at an operation a
second, I've not got much experience with this kind of thing so I'm not sure
if that's 'a lot' or not, it just seems like it at the moment. It might not
get as busy as that, I'm not sure and its difficult to simulate as this
isn't likely to be a steady flow of traffic, they'll come in big fat lumps
every now and then.

Thanks,

Rob

-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] 
Sent: 16 April 2007 15:12
To: Dave Borne
Cc: Robert Rawlins - Think Blue; python-list@python.org
Subject: Re: Writing Log CSV (Efficiently)


Dave> Python has built in logging support. It's pretty flexible as far
Dave> as formatting output. I can get a bit complicated to set up, but
Dave> it will handle traffic well.

Really?  I've found it to be a dog in heavy logging situations.

Skip

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


Writing Log CSV (Efficiently)

2007-04-16 Thread Robert Rawlins - Think Blue
Hello Guys,

 

I'm looking to write a Log file which will be CSV based, and there is a good
possibility that it'll get quite busy once its up and running, so I'm
looking for the most efficient way to achieve it. Whilst I'm sure i could do
something like this.

 

  myfile = open("Logs/Application.txt", "w")

  myfile.write('col1, col2, col3, col4, col5')

  myfile.close

 

But I'm a little apprehensive that open() and close() on a very regular
basis is just going to cause issues. I'm also a little worried that we'll
end up with 'race' type conditions and things going missing.

 

So my next thought was to just have an open object for the file, and then
perform multiple rights, until I need to send the report file somewhere
else, at which point I would close it. This in itself causes more issues as
we're running in a buffer which is just going to eat my memory, and as this
is on an embedded system which may lose power, we'd be kissing good bye to
all the logs until that point.

 

What would you guys suggest to be the most efficient way to handle this?

 

Thanks,

 

Rob

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

Import From SubFolder

2007-04-16 Thread Robert Rawlins - Think Blue
Chaps,

 

Is it possible to import a module from a subdirectory in my application? I
can only seem to get it to import from a single directory, but keeping all
my files and classes in the same directory becomes a little sloppy.

 

Thanks,

 

Rob 

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

Signal Handlers

2007-04-16 Thread Robert Rawlins - Think Blue
Hello Guys,

 

Thanks again for all the help you've given me this past week, things are
moving briskly and my class library is building nicely and seems to be
working well :-D

 

Now, I'm working with an API for another piece of software today, and the
API documentation details the signals that are broadcast upon particular
events, which I need to listen for and then handle.

 

Here is an example:

 

   void RemoteDeviceFound(string address, uint32 class, int16
rssi)

 

   This signal will be send every time an inquiry result

   has been found by the service daemon. In general they

   only appear during a device discovery.

 

Now, how do I listen for this signal? I've seen a couple of examples where
by they import the signal module, and then to
signal.signal(RemoteDeviceFound, myFunctionToCall) but the python
documentation seemed to think that this would be limited to 2 arguments,
when I've obviously got 3 of them.

 

Perhaps I've got my wires crossed, if it's really that simple then great.
However, where about in my application should I declare that signal
listener?

 

Thanks,

 

Rob

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

  1   2   >