python/ruby question..

Mensanator mensanator at aol.com
Thu Jun 19 22:47:13 CEST 2008


On Jun 19, 5:14 am, Matt Nordhoff <mnordh... at mattnordhoff.com> wrote:
> Mensanator wrote:

> You're supposed to use the subprocess module.

Yeah, I know, but I couldn't get it to work the last
time I tried it.

>
> In this case, something like:
>
> import subprocess
> factor_program = ['factor!', '-d200']
>
> ...
>
> p = subprocess.Popen(factor_program + [n], stdout=subprocess.PIPE)
> p.wait() # wait for it to finish; not sure how necessary it is
> the_output = p.stdout.readlines()

Just like how this doesn't work either:

Traceback (most recent call last):
  File "C:\Program Files\PyGTK\Python\user\factor_timing.py", line 26,
in <module>
    p = subprocess.Popen(factor_program + [the_comp[1]],
stdout=subprocess.PIPE)
  File "C:\Program Files\PyGTK\Python\lib\subprocess.py", line 586, in
__init__
    errread, errwrite) = self._get_handles(stdin, stdout, stderr)
  File "C:\Program Files\PyGTK\Python\lib\subprocess.py", line 681, in
_get_handles
    p2cread = self._make_inheritable(p2cread)
  File "C:\Program Files\PyGTK\Python\lib\subprocess.py", line 722, in
_make_inheritable
    DUPLICATE_SAME_ACCESS)
TypeError: an integer is required

>
> See subprocess's documentation [1], which includes guides on replacing
> os.popen* and other functions with it.

I have seen it - it's utterly incomprehensible.

What do you suppose you did wrong?

The complete program (with the deprecated code commented out -
which works, BTW):


#import os
import time
import subprocess

#factor_program = 'factor! -d200 '
factor_program = ['factor!','-d200']

the_composites =
[['COMPOSITE_FACTOR','50818429800343305993022114330311033271249313957919046352679206262204589342623811236647989889145173098650749']]

the_primes = []
the_intractables = []

phase = 1
the_times = []
while the_composites:
  print "="*40
  print 'Phase',phase
  the_comp = the_composites.pop(0)
  print the_comp
  print
  the_times.append(time.time())  # time how long it takes to run
factor!.exe

  #the_output = os.popen(factor_program+the_comp[1]).readlines()
  #  change to subprocess
  p = subprocess.Popen(factor_program + [the_comp[1]],
stdout=subprocess.PIPE)
  p.wait()
  the_output = p.stdout.readlines()

  the_times.append(time.time())
  new_factors = [i.split() for i in the_output]
  for i in new_factors: print i
  print
  if len(new_factors) == 1:
    if new_factors[0][0] == 'PRIME_FACTOR':
      the_primes.append([new_factors[0][0],long(new_factors[0][1])])
    else:
      the_intractables.append([new_factors[0][0],long(new_factors[0]
[1])])
    new_factors.pop()
  while new_factors:
    j = new_factors.pop(0)
    if j[0] == 'PRIME_FACTOR':
      the_primes.append([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_intractables.sort()
the_primes.extend(the_intractables)

for i in the_primes:
  print i[0],i[1]
print
print "="*40





When working, it produces:


##  ========================================
##  Phase 1
##  ['COMPOSITE_FACTOR',
'50818429800343305993022114330311033271249313957919046352679206262204589342623811236647989889145173098650749']
##
##  ['PRIME_FACTOR', '37']
##  ['PRIME_FACTOR', '43']
##  ['PRIME_FACTOR', '167']
##  ['COMPOSITE_FACTOR', '507787751']
##  ['PRIME_FACTOR', '69847']
##  ['PRIME_FACTOR', '30697']
##  ['PRIME_FACTOR', '89017']
##  ['PRIME_FACTOR', '3478697']
##  ['PRIME_FACTOR', '434593']
##  ['PRIME_FACTOR', '49998841']
##  ['PRIME_FACTOR', '161610704597143']
##  ['PRIME_FACTOR', '14064370273']
##  ['COMPOSITE_FACTOR', '963039394703598565337297']
##  ['PRIME_FACTOR', '11927295803']
##
##  0.860000133514 seconds
##  ========================================
##  Phase 2
##  ['COMPOSITE_FACTOR', '507787751']
##
##  ['PRIME_FACTOR', '29819']
##  ['PRIME_FACTOR', '17029']
##
##  0.0780000686646 seconds
##  ========================================
##  Phase 3
##  ['COMPOSITE_FACTOR', '963039394703598565337297']
##
##  ['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
##
##  ========================================






More information about the Python-list mailing list