On Aug 13, 5:16 pm, johnmfis...@com cast.net (John Fisher) wrote:

I am working on a framework for data acquisition in Python 2.5, am

trying to get a structure going more like this:

mark start time

start event

event finishes

count time until next interval

start second event...

rather than this:

start event

event finishes

sleep for interval

start second event

Do you see the difference? I get a true fixed interval from the first,

including the time to accomplish the event task(s). In the second case,

the sleep just gets tacked on at the end of the events, not very

deterministic (timing-wise).

So how do I accomplish this in Python with a minimum of labour?

Thanks for any light you can shed on my darkness...

wave_man

In the first example, you know when to start the second interval,

right? In the second case, you are assuming the second task is

ready to run after the sleep interval.

Here's an example that times how long an external program takes,

the os.popen() returns to the Python script when the external

C program completes, so no need for any sleep interval.

Because of the poorly written factor program, some composites

are falsely reported as unfactorable. This example keeps

re-calling the factor!.exe program until all composites are

factored or marked intractable, and the time to execute each

iteration of factor!.exe is printed after each call.

import os

import time

factor_program = 'factor! -d200 '

the_composites =

[['COMPOSITE_FACT OR','5081842980 034330599302211 433031103327124 931395791904635 267920626220458 934262381123664 798988914517309 8650749']]

the_primes = []

the_intractable s = []

phase = 1

the_times = []

while the_composites:

print "="*40

print 'Phase',phase

the_comp = the_composites. pop(0)

print the_comp

print

the_times.appen d(time.time()) # time how long it takes to run

factor!.exe

the_output = os.popen(factor _program+the_co mp[1]).readlines()

the_times.appen d(time.time())

new_factors = [i.split() for i in the_output]

for i in new_factors: print i

print

if len(new_factors ) == 1:

# it's prime or intractable

if new_factors[0][0] == 'PRIME_FACTOR':

the_primes.appe nd([new_factors[0][0],long(new_facto rs[0][1])])

else:

the_intractable s.append([new_factors[0][0],long(new_facto rs[0]

[1])])

new_factors.pop ()

while new_factors:

j = new_factors.pop (0)

if j[0] == 'PRIME_FACTOR':

the_primes.appe nd([j[0],long(j[1])])

else:

the_composites. append(j)

print the_times[phase] - the_times[phase-1],'seconds'

phase += 1

print "="*40

print

print 'Factoring complete'

print

the_primes.sort ()

the_intractable s.sort()

the_primes.exte nd(the_intracta bles)

for i in the_primes:

print i[0],i[1]

print

print "="*40

## =============== =============== ==========

## Phase 1

## ['COMPOSITE_FACT OR',

'50818429800343 305993022114330 311033271249313 957919046352679 206262204589342 623811236647989 889145173098650 749']

##

## ['PRIME_FACTOR', '37']

## ['PRIME_FACTOR', '43']

## ['PRIME_FACTOR', '167']

## ['COMPOSITE_FACT OR', '507787751']

## ['PRIME_FACTOR', '69847']

## ['PRIME_FACTOR', '30697']

## ['PRIME_FACTOR', '89017']

## ['PRIME_FACTOR', '3478697']

## ['PRIME_FACTOR', '434593']

## ['PRIME_FACTOR', '49998841']

## ['PRIME_FACTOR', '16161070459714 3']

## ['PRIME_FACTOR', '14064370273']

## ['COMPOSITE_FACT OR', '96303939470359 8565337297']

## ['PRIME_FACTOR', '11927295803']

##

## 0.860000133514 seconds

## =============== =============== ==========

## Phase 2

## ['COMPOSITE_FACT OR', '507787751']

##

## ['PRIME_FACTOR', '29819']

## ['PRIME_FACTOR', '17029']

##

## 0.0780000686646 seconds

## =============== =============== ==========

## Phase 3

## ['COMPOSITE_FACT OR', '96303939470359 8565337297']

##

## ['PRIME_FACTOR', '518069464441']

## ['PRIME_FACTOR', '1858900129817']

##

## 0.0469999313354 seconds

## =============== =============== ==========

##

## Factoring complete

##

## PRIME_FACTOR 37

## PRIME_FACTOR 43

## PRIME_FACTOR 167

## PRIME_FACTOR 17029

## PRIME_FACTOR 29819

## PRIME_FACTOR 30697

## PRIME_FACTOR 69847

## PRIME_FACTOR 89017

## PRIME_FACTOR 434593

## PRIME_FACTOR 3478697

## PRIME_FACTOR 49998841

## PRIME_FACTOR 11927295803

## PRIME_FACTOR 14064370273

## PRIME_FACTOR 518069464441

## PRIME_FACTOR 1858900129817

## PRIME_FACTOR 161610704597143

##

## =============== =============== ==========