Re: Need help in python program

2016-10-29 Thread Veek M
id_1, clk, val = foo_function()
id_2, key, units, delay = bar_function()

if id_1 == id_2:
  print id_1, clk, val, key, units, delay


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


Re: Need help in python program

2016-07-01 Thread Christian Gollwitzer

Am 01.07.16 um 12:26 schrieb Archana Sonavane:

Hello Everyone,

I am doing python code by using API.

My first API giving fields - Itemid, clock and value
second API giving fields - Itemid, key, units and delay

using for loops for both API.

Could you please tell me how to compare both id by using equal operator.

My output should be :

Itemid, clock, value, key, units and delay



I think you want a "join" operation from relational algebra. pandas can 
do that:


http://pandas.pydata.org/pandas-docs/stable/merging.html#database-style-dataframe-joining-merging

or you just store the one table in a dict with Itemd as the key and loop 
over the second, while looking up corresponding values from the dict.


Christin
--
https://mail.python.org/mailman/listinfo/python-list


Re: Need help in python program

2016-07-01 Thread Bob Gailer
On Jul 1, 2016 6:30 AM, "Archana Sonavane" 
wrote:
>
> Hello Everyone,
>
> I am doing python code by using API.
>
> My first API giving fields - Itemid, clock and value
> second API giving fields - Itemid, key, units and delay
>
> using for loops for both API.
>
> Could you please tell me how to compare both id by using equal operator.
>
> My output should be :
>
> Itemid, clock, value, key, units and delay

Please provide some sample input and the corresponding output. What do you
mean by API?
-- 
https://mail.python.org/mailman/listinfo/python-list


Need help in python program

2016-07-01 Thread Archana Sonavane
Hello Everyone,

I am doing python code by using API. 

My first API giving fields - Itemid, clock and value
second API giving fields - Itemid, key, units and delay

using for loops for both API.

Could you please tell me how to compare both id by using equal operator. 

My output should be :

Itemid, clock, value, key, units and delay
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Need help with a program

2010-02-03 Thread Nobody
On Tue, 02 Feb 2010 15:07:05 -0800, Aahz wrote:

If you have a problem and you think that regular expressions are the
solution then now you have two problems.  Regex is really overkill for
the OP's problem and it certainly doesn't improve readability.
 
 If you're going to use a quote, it works better if you use the exact
 quote and attribute it:
 
 'Some people, when confronted with a problem, think I know, I'll use
 regular expressions.  Now they have two problems.' --Jamie Zawinski

He may have mixed that one up with a different (and more generic) saying:

If you think that X is the solution to your problem, then you don't
understand X and you don't understand your problem.

For most values of X.

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


Re: Need help with a program

2010-02-02 Thread Aahz
In article mailman.1551.1264701475.28905.python-l...@python.org,
D'Arcy J.M. Cain da...@druid.net wrote:

If you have a problem and you think that regular expressions are the
solution then now you have two problems.  Regex is really overkill for
the OP's problem and it certainly doesn't improve readability.

If you're going to use a quote, it works better if you use the exact
quote and attribute it:

'Some people, when confronted with a problem, think I know, I'll use
regular expressions.  Now they have two problems.' --Jamie Zawinski
-- 
Aahz (a...@pythoncraft.com)   * http://www.pythoncraft.com/

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


Re: Need help with a program

2010-01-29 Thread Johann Spies
On Thu, Jan 28, 2010 at 07:07:04AM -0800, evilweasel wrote:
 Hi folks,
 
 I am a newbie to python, and I would be grateful if someone could
 point out the mistake in my program. Basically, I have a huge text
 file similar to the format below:
 
 AGACTCGAGTGCGCGGA 0
 AGATAAGCTAATTAAGCTACTGG   0
 AGATAAGCTAATTAAGCTACTGGGTT 1
 AGCTCACAATAT   1
 AGGTCGCCTGACGGCTGC0

I know this is a python list but if you really want to get the job
done quickly this is one method without writing python code:

$ cat /tmp/y
AGACTCGAGTGCGCGGA   0
AGATAAGCTAATTAAGCTACTGG 0
AGATAAGCTAATTAAGCTACTGGGTT   1
AGCTCACAATAT 1
AGGTCGCCTGACGGCTGC  0
$ grep -v 0 /tmp/y  tmp/z
$ cat /tmp/z
AGATAAGCTAATTAAGCTACTGGGTT   1
AGCTCACAATAT 1

Regards
Johann
-- 
Johann Spies  Telefoon: 021-808 4599
Informasietegnologie, Universiteit van Stellenbosch

 My son, if sinners entice thee, consent thou not.
Proverbs 1:10 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Need help with a program

2010-01-29 Thread Steven D'Aprano
On Fri, 29 Jan 2010 11:23:54 +0200, Johann Spies wrote:

 On Thu, Jan 28, 2010 at 07:07:04AM -0800, evilweasel wrote:
 Hi folks,
 
 I am a newbie to python, and I would be grateful if someone could point
 out the mistake in my program. Basically, I have a huge text file
 similar to the format below:
 
 AGACTCGAGTGCGCGGA0
 AGATAAGCTAATTAAGCTACTGG  0
 AGATAAGCTAATTAAGCTACTGGGTT1
 AGCTCACAATAT  1
 AGGTCGCCTGACGGCTGC   0
 
 I know this is a python list but if you really want to get the job done
 quickly this is one method without writing python code:
 
 $ cat /tmp/y
 AGACTCGAGTGCGCGGA   0
 AGATAAGCTAATTAAGCTACTGG 0
 AGATAAGCTAATTAAGCTACTGGGTT   1
 AGCTCACAATAT 1
 AGGTCGCCTGACGGCTGC  0
 $ grep -v 0 /tmp/y  tmp/z
 $ cat /tmp/z
 AGATAAGCTAATTAAGCTACTGGGTT   1
 AGCTCACAATAT 1

That will do the wrong thing for lines like:

AGATAAGCTAATTAAGCTACTGGGTT   10


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


Re: Need help with a program

2010-01-29 Thread Johann Spies
On Fri, Jan 29, 2010 at 10:04:33AM +, Steven D'Aprano wrote:
  I know this is a python list but if you really want to get the job done
  quickly this is one method without writing python code:
  
  $ cat /tmp/y
  AGACTCGAGTGCGCGGA   0
  AGATAAGCTAATTAAGCTACTGG 0
  AGATAAGCTAATTAAGCTACTGGGTT   1
  AGCTCACAATAT 1
  AGGTCGCCTGACGGCTGC  0
  $ grep -v 0 /tmp/y  tmp/z
  $ cat /tmp/z
  AGATAAGCTAATTAAGCTACTGGGTT   1
  AGCTCACAATAT 1
 
 That will do the wrong thing for lines like:
 
 AGATAAGCTAATTAAGCTACTGGGTT   10

In that case change the grep to ' 0$'  then only the lines with a
singel digit '0' at the end of the line will be excluded.

One can do the same using regulare expressions in Python and it will
probably a lot slower on large files.

Regards
Johann
-- 
Johann Spies  Telefoon: 021-808 4599
Informasietegnologie, Universiteit van Stellenbosch

 My son, if sinners entice thee, consent thou not.
Proverbs 1:10 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Need help with a program

2010-01-29 Thread D'Arcy J.M. Cain
On Fri, 29 Jan 2010 11:23:54 +0200
Johann Spies jsp...@sun.ac.za wrote:
 I know this is a python list but if you really want to get the job
 done quickly this is one method without writing python code:
 [...]
 $ grep -v 0 /tmp/y  tmp/z

There's plenty of ways to do it without writing Python.  C, C++, Perl,
Forth, Awk, BASIC, Intercal, etc.  So what?  Besides, your solution
doesn't work.  You want grep -vw 0 /tmp/y  tmp/z and even then it
doesn't meet the requirements.  It extracts the lines the OP wants but
doesn't reformat them.  It also assumes a Unix system or at least
something with grep installed so it isn't portable.

If you want to see how the same task can be done in many different
languages see http://www.roesler-ac.de/wolfram/hello.htm.

-- 
D'Arcy J.M. Cain da...@druid.net |  Democracy is three wolves
http://www.druid.net/darcy/|  and a sheep voting on
+1 416 425 1212 (DoD#0082)(eNTP)   |  what's for dinner.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Need help with a program

2010-01-29 Thread nn


Johann Spies wrote:
 On Thu, Jan 28, 2010 at 07:07:04AM -0800, evilweasel wrote:
  Hi folks,
 
  I am a newbie to python, and I would be grateful if someone could
  point out the mistake in my program. Basically, I have a huge text
  file similar to the format below:
 
  AGACTCGAGTGCGCGGA   0
  AGATAAGCTAATTAAGCTACTGG 0
  AGATAAGCTAATTAAGCTACTGGGTT   1
  AGCTCACAATAT 1
  AGGTCGCCTGACGGCTGC  0

 I know this is a python list but if you really want to get the job
 done quickly this is one method without writing python code:

 $ cat /tmp/y
 AGACTCGAGTGCGCGGA   0
 AGATAAGCTAATTAAGCTACTGG 0
 AGATAAGCTAATTAAGCTACTGGGTT   1
 AGCTCACAATAT 1
 AGGTCGCCTGACGGCTGC  0
 $ grep -v 0 /tmp/y  tmp/z
 $ cat /tmp/z
 AGATAAGCTAATTAAGCTACTGGGTT   1
 AGCTCACAATAT 1

 Regards
 Johann
 --
 Johann Spies  Telefoon: 021-808 4599
 Informasietegnologie, Universiteit van Stellenbosch

  My son, if sinners entice thee, consent thou not.
 Proverbs 1:10

I would rather use awk for this:

awk 'NF==2  $2!~/^0$/ {printf(seq%s\n%s\n,NR,$1)}' dnain.dat

but I think that is getting a bit off topic...
-- 
http://mail.python.org/mailman/listinfo/python-list


Need help with a program

2010-01-28 Thread evilweasel
Hi folks,

I am a newbie to python, and I would be grateful if someone could
point out the mistake in my program. Basically, I have a huge text
file similar to the format below:

AGACTCGAGTGCGCGGA   0
AGATAAGCTAATTAAGCTACTGG 0
AGATAAGCTAATTAAGCTACTGGGTT   1
AGCTCACAATAT 1
AGGTCGCCTGACGGCTGC  0

The text is nothing but DNA sequences, and there is a number next to
it. What I will have to do is, ignore those lines that have 0 in it,
and print all other lines (excluding the number) in a new text file
(in a particular format called as FASTA format). This is the program I
wrote for that:

seq1 = []
list1 = []
lister = []
listers = []
listers1 = []
a = []
d = []
i = 0
j = 0
num = 0

file1 = open(sys.argv[1], 'r')
for line in file1:
if not line.startswith('\n'):
seq1 = line.split()
if len(seq1) == 0:
continue

a = seq1[0]
list1.append(a)

d = seq1[1]
lister.append(d)


b = len(lister)
for j in range(0, b):
if lister[j] == 0:
listers.append(j)
else:
listers1.append(j)


print listers1
resultsfile = open(sequences1.txt, 'w')
for i in listers1:
resultsfile.write('\nseq' + str(i) + '\n' + list1[i] + '\n')

But this isn't working. I am not able to find the bug in this. I would
be thankful if someone could point it out. Thanks in advance!

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


Re: Need help with a program

2010-01-28 Thread Alf P. Steinbach

* evilweasel:

Hi folks,

I am a newbie to python, and I would be grateful if someone could
point out the mistake in my program. Basically, I have a huge text
file similar to the format below:

AGACTCGAGTGCGCGGA   0
AGATAAGCTAATTAAGCTACTGG 0
AGATAAGCTAATTAAGCTACTGGGTT   1
AGCTCACAATAT 1
AGGTCGCCTGACGGCTGC  0

The text is nothing but DNA sequences, and there is a number next to
it. What I will have to do is, ignore those lines that have 0 in it,
and print all other lines (excluding the number) in a new text file
(in a particular format called as FASTA format). This is the program I
wrote for that:

seq1 = []
list1 = []
lister = []
listers = []
listers1 = []
a = []
d = []
i = 0
j = 0
num = 0

file1 = open(sys.argv[1], 'r')
for line in file1:
if not line.startswith('\n'):
seq1 = line.split()
if len(seq1) == 0:
continue

a = seq1[0]
list1.append(a)

d = seq1[1]
lister.append(d)


b = len(lister)
for j in range(0, b):
if lister[j] == 0:
listers.append(j)
else:
listers1.append(j)


print listers1
resultsfile = open(sequences1.txt, 'w')
for i in listers1:
resultsfile.write('\nseq' + str(i) + '\n' + list1[i] + '\n')

But this isn't working.


What do you mean by isn't working?



I am not able to find the bug in this. I would
be thankful if someone could point it out. Thanks in advance!


What do you expect as output, and what do you actually get as output?


Cheers,

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


Re: Need help with a program

2010-01-28 Thread Mark Dickinson
On Jan 28, 3:07 pm, evilweasel karthikramaswam...@gmail.com wrote:
 Hi folks,

 I am a newbie to python, and I would be grateful if someone could
 point out the mistake in my program.

snip

 for j in range(0, b):
     if lister[j] == 0:

At a guess, this line should be:

if lister[j] == '0':
...

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


Re: Need help with a program

2010-01-28 Thread Krister Svanlund
On Thu, Jan 28, 2010 at 4:07 PM, evilweasel
karthikramaswam...@gmail.com wrote:
 Hi folks,

 I am a newbie to python, and I would be grateful if someone could
 point out the mistake in my program. Basically, I have a huge text
 file similar to the format below:

 AGACTCGAGTGCGCGGA   0
 AGATAAGCTAATTAAGCTACTGG     0
 AGATAAGCTAATTAAGCTACTGGGTT   1
 AGCTCACAATAT     1
 AGGTCGCCTGACGGCTGC  0

 The text is nothing but DNA sequences, and there is a number next to
 it. What I will have to do is, ignore those lines that have 0 in it,
 and print all other lines (excluding the number) in a new text file
 (in a particular format called as FASTA format). This is the program I
 wrote for that:

 seq1 = []
 list1 = []
 lister = []
 listers = []
 listers1 = []
 a = []
 d = []
 i = 0
 j = 0
 num = 0

 file1 = open(sys.argv[1], 'r')
 for line in file1:
    if not line.startswith('\n'):
        seq1 = line.split()
        if len(seq1) == 0:
            continue

        a = seq1[0]
        list1.append(a)

        d = seq1[1]
        lister.append(d)


 b = len(lister)
 for j in range(0, b):
    if lister[j] == 0:
        listers.append(j)
    else:
        listers1.append(j)


 print listers1
 resultsfile = open(sequences1.txt, 'w')
 for i in listers1:
    resultsfile.write('\nseq' + str(i) + '\n' + list1[i] + '\n')

 But this isn't working. I am not able to find the bug in this. I would
 be thankful if someone could point it out. Thanks in advance!

 Cheers!

I'm not totaly sure what you want to do but try this (python2.6+):

newlines = []

with open(sys.argv[1], 'r') as f:
text = f.read();
for line in text.splitlines():
if not line.strip() and line.strip().endswith('1'):
newlines.append('seq'+line)

with open(sys.argv[2], 'w') as f:
f.write('\n'.join(newlines))
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Need help with a program

2010-01-28 Thread Krister Svanlund
On Thu, Jan 28, 2010 at 4:28 PM, Krister Svanlund
krister.svanl...@gmail.com wrote:
 On Thu, Jan 28, 2010 at 4:07 PM, evilweasel
 karthikramaswam...@gmail.com wrote:
 Hi folks,

 I am a newbie to python, and I would be grateful if someone could
 point out the mistake in my program. Basically, I have a huge text
 file similar to the format below:

 AGACTCGAGTGCGCGGA   0
 AGATAAGCTAATTAAGCTACTGG     0
 AGATAAGCTAATTAAGCTACTGGGTT   1
 AGCTCACAATAT     1
 AGGTCGCCTGACGGCTGC  0

 The text is nothing but DNA sequences, and there is a number next to
 it. What I will have to do is, ignore those lines that have 0 in it,
 and print all other lines (excluding the number) in a new text file
 (in a particular format called as FASTA format). This is the program I
 wrote for that:

 seq1 = []
 list1 = []
 lister = []
 listers = []
 listers1 = []
 a = []
 d = []
 i = 0
 j = 0
 num = 0

 file1 = open(sys.argv[1], 'r')
 for line in file1:
    if not line.startswith('\n'):
        seq1 = line.split()
        if len(seq1) == 0:
            continue

        a = seq1[0]
        list1.append(a)

        d = seq1[1]
        lister.append(d)


 b = len(lister)
 for j in range(0, b):
    if lister[j] == 0:
        listers.append(j)
    else:
        listers1.append(j)


 print listers1
 resultsfile = open(sequences1.txt, 'w')
 for i in listers1:
    resultsfile.write('\nseq' + str(i) + '\n' + list1[i] + '\n')

 But this isn't working. I am not able to find the bug in this. I would
 be thankful if someone could point it out. Thanks in advance!

 Cheers!

 I'm not totaly sure what you want to do but try this (python2.6+):

 newlines = []

 with open(sys.argv[1], 'r') as f:
    text = f.read();
    for line in text.splitlines():
        if not line.strip() and line.strip().endswith('1'):
            newlines.append('seq'+line.strip()[:-1].strip())

 with open(sys.argv[2], 'w') as f:
    f.write('\n'.join(newlines))


Gah, made some errors
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Need help with a program

2010-01-28 Thread D'Arcy J.M. Cain
On Thu, 28 Jan 2010 07:07:04 -0800 (PST)
evilweasel karthikramaswam...@gmail.com wrote:
 I am a newbie to python, and I would be grateful if someone could

Welcome.

 point out the mistake in my program. Basically, I have a huge text
 file similar to the format below:

You don't say how it isn't working.  As a first step you should read
http://catb.org/~esr/faqs/smart-questions.html.

 The text is nothing but DNA sequences, and there is a number next to
 it. What I will have to do is, ignore those lines that have 0 in it,

Your code doesn't completely ignore them.  See below.

 and print all other lines (excluding the number) in a new text file
 (in a particular format called as FASTA format). This is the program I
 wrote for that:
 
 seq1 = []
 list1 = []
 lister = []
 listers = []
 listers1 = []
 a = []
 d = []
 i = 0
 j = 0
 num = 0

This seems like an awful lot of variables for such a simple task.

 
 file1 = open(sys.argv[1], 'r')
 for line in file1:

This is good.  You aren't trying to load the whole file into memory at
once.  If the file is huge as you say then that would have been bad.  I
would have made one small optimization that saves one assignment and
one extra variable.

  for line in open(sys.argv[1], 'r'):

 if not line.startswith('\n'):
 seq1 = line.split()
 if len(seq1) == 0:
 continue

This is redundant and perhaps not even correct at the end of the file.
It assumes that the last line ends with a newline.  Look at what
'\n'.split() gives you and see if you can't improve the above code.

Another small optimization - if seq1 is better than if len(seq1).

 
 a = seq1[0]
   list1.append(a)

Aha!  I may have found your bug.  Are you mixing tabs and spaces?
Don't do that.  Either always use spaces or always use tabs.  My
suggestion is to use spaces and choose a short indent such as three or
even two but that's a religious issue.

 
   d = seq1[1]
   lister.append(d)

You can also do a, d = seq1.  Of course you must be sure that you
have two fields.  Perhaps that's guaranteed for your input but a quick
sanity test wouldn't hurt here.

However, I don't understand all of the above.  It may also be a source
of problems.  You say the files are huge.  Are you filling up memory
here?  You did the smart thing reading the file but you lose it here.
In any case, see below.

 b = len(lister)
 for j in range(0, b):

Go lookup zip()

 if lister[j] == 0:

I think that you will find that lister[j] is 0, not 0.

 listers.append(j)
 else:
 listers1.append(j)

Why are you collecting the input?  Just toss the '0' ones and write the
others lines directly to the output.

Hope this helps with this script and in further understanding the power
and simplicity of Python.  Good luck.

-- 
D'Arcy J.M. Cain da...@druid.net |  Democracy is three wolves
http://www.druid.net/darcy/|  and a sheep voting on
+1 416 425 1212 (DoD#0082)(eNTP)   |  what's for dinner.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Need help with a program

2010-01-28 Thread Krister Svanlund
On Thu, Jan 28, 2010 at 4:31 PM, Krister Svanlund
krister.svanl...@gmail.com wrote:
 On Thu, Jan 28, 2010 at 4:28 PM, Krister Svanlund
 krister.svanl...@gmail.com wrote:
 On Thu, Jan 28, 2010 at 4:07 PM, evilweasel
 karthikramaswam...@gmail.com wrote:
 Hi folks,

 I am a newbie to python, and I would be grateful if someone could
 point out the mistake in my program. Basically, I have a huge text
 file similar to the format below:

 AGACTCGAGTGCGCGGA   0
 AGATAAGCTAATTAAGCTACTGG     0
 AGATAAGCTAATTAAGCTACTGGGTT   1
 AGCTCACAATAT     1
 AGGTCGCCTGACGGCTGC  0

 The text is nothing but DNA sequences, and there is a number next to
 it. What I will have to do is, ignore those lines that have 0 in it,
 and print all other lines (excluding the number) in a new text file
 (in a particular format called as FASTA format). This is the program I
 wrote for that:

 seq1 = []
 list1 = []
 lister = []
 listers = []
 listers1 = []
 a = []
 d = []
 i = 0
 j = 0
 num = 0

 file1 = open(sys.argv[1], 'r')
 for line in file1:
    if not line.startswith('\n'):
        seq1 = line.split()
        if len(seq1) == 0:
            continue

        a = seq1[0]
        list1.append(a)

        d = seq1[1]
        lister.append(d)


 b = len(lister)
 for j in range(0, b):
    if lister[j] == 0:
        listers.append(j)
    else:
        listers1.append(j)


 print listers1
 resultsfile = open(sequences1.txt, 'w')
 for i in listers1:
    resultsfile.write('\nseq' + str(i) + '\n' + list1[i] + '\n')

 But this isn't working. I am not able to find the bug in this. I would
 be thankful if someone could point it out. Thanks in advance!

 Cheers!

I'm trying this again:

newlines = []

with open(sys.argv[1], 'r') as f:
   text = f.read();
   for line in (l.strip() for l in text.splitlines()):
       if line:
           line_elem = line.split()
           if len(line_elem) == 2 and line_elem[1] == '1':
               newlines.append('seq'+line_elem[0])

with open(sys.argv[2], 'w') as f:
   f.write('\n'.join(newlines))
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Need help with a program

2010-01-28 Thread evilweasel
I will make my question a little more clearer. I have close to 60,000
lines of the data similar to the one I posted. There are various
numbers next to the sequence (this is basically the number of times
the sequence has been found in a particular sample). So, I would need
to ignore the ones containing '0' and write all other sequences
(excluding the number, since it is trivial) in a new text file, in the
following format:

seq59902
TTTATTATATAGT

seq59903
TTTATTTCTTGGCGTTGT

seq59904
TTTGGTTGCCCTGCGTGG

seq59905
TTTGTTTATGGG

The number next to 'seq' is the line number of the sequence. When I
run the above program, what I expect is an output file that is similar
to the above output but with the ones containing '0' ignored. But, I
am getting all the sequences printed in the file.

Kindly excuse the 'newbieness' of the program. :) I am hoping to
improve in the next few months. Thanks to all those who replied. I
really appreciate it. :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Need help with a program

2010-01-28 Thread nn
On Jan 28, 10:50 am, evilweasel karthikramaswam...@gmail.com wrote:
 I will make my question a little more clearer. I have close to 60,000
 lines of the data similar to the one I posted. There are various
 numbers next to the sequence (this is basically the number of times
 the sequence has been found in a particular sample). So, I would need
 to ignore the ones containing '0' and write all other sequences
 (excluding the number, since it is trivial) in a new text file, in the
 following format:

 seq59902

 TTTATTATATAGT

 seq59903

 TTTATTTCTTGGCGTTGT

 seq59904

 TTTGGTTGCCCTGCGTGG

 seq59905

 TTTGTTTATGGG

 The number next to 'seq' is the line number of the sequence. When I
 run the above program, what I expect is an output file that is similar
 to the above output but with the ones containing '0' ignored. But, I
 am getting all the sequences printed in the file.

 Kindly excuse the 'newbieness' of the program. :) I am hoping to
 improve in the next few months. Thanks to all those who replied. I
 really appreciate it. :)

People have already given you some pointers to your problem. In the
end you will have to tweak the details because only you have access
to the data not us.

Just as example here is another way to do what you are doing:

with open('dnain.dat') as infile, open('dnaout.dat','w') as outfile:
   partgen=(line.split() for line in infile)
   dnagen=(str(i+1)+'\n'+part[0]+'\n'
   for i,part in enumerate(partgen)
   if len(part)1 and part[1]!='0')
   outfile.writelines(dnagen)

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


Re: Need help with a program

2010-01-28 Thread Arnaud Delobelle
nn prueba...@latinmail.com writes:

 On Jan 28, 10:50 am, evilweasel karthikramaswam...@gmail.com wrote:
 I will make my question a little more clearer. I have close to 60,000
 lines of the data similar to the one I posted. There are various
 numbers next to the sequence (this is basically the number of times
 the sequence has been found in a particular sample). So, I would need
 to ignore the ones containing '0' and write all other sequences
 (excluding the number, since it is trivial) in a new text file, in the
 following format:

 seq59902

 TTTATTATATAGT

 seq59903

 TTTATTTCTTGGCGTTGT

 seq59904

 TTTGGTTGCCCTGCGTGG

 seq59905

 TTTGTTTATGGG

 The number next to 'seq' is the line number of the sequence. When I
 run the above program, what I expect is an output file that is similar
 to the above output but with the ones containing '0' ignored. But, I
 am getting all the sequences printed in the file.

 Kindly excuse the 'newbieness' of the program. :) I am hoping to
 improve in the next few months. Thanks to all those who replied. I
 really appreciate it. :)

 People have already given you some pointers to your problem. In the
 end you will have to tweak the details because only you have access
 to the data not us.

 Just as example here is another way to do what you are doing:

 with open('dnain.dat') as infile, open('dnaout.dat','w') as outfile:
partgen=(line.split() for line in infile)
dnagen=(str(i+1)+'\n'+part[0]+'\n'
for i,part in enumerate(partgen)
if len(part)1 and part[1]!='0')
outfile.writelines(dnagen)

I think that generator expressions are overrated :) What's wrong with:

with open('dnain.dat') as infile, open('dnaout.dat','w') as outfile:
for i, line in enumerate(infile):
parts = line.split()
if len(parts)  1 and parts[1] != '0':
outfile.write(seq%s\n%s\n % (i+1, parts[0]))

(untested)

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


Re: Need help with a program

2010-01-28 Thread John Posner

On 1/28/2010 10:50 AM, evilweasel wrote:

I will make my question a little more clearer. I have close to 60,000
lines of the data similar to the one I posted. There are various
numbers next to the sequence (this is basically the number of times
the sequence has been found in a particular sample). So, I would need
to ignore the ones containing '0' and write all other sequences
(excluding the number, since it is trivial) in a new text file, in the
following format:


seq59902

TTTATTATATAGT


seq59903

TTTATTTCTTGGCGTTGT


seq59904

TTTGGTTGCCCTGCGTGG


seq59905

TTTGTTTATGGG

The number next to 'seq' is the line number of the sequence. When I
run the above program, what I expect is an output file that is similar
to the above output but with the ones containing '0' ignored. But, I
am getting all the sequences printed in the file.

Kindly excuse the 'newbieness' of the program. :) I am hoping to
improve in the next few months. Thanks to all those who replied. I
really appreciate it. :)


Your program is a good first try. It contains a newbie error (looking 
for the number 0 instead of the string 0). But more importantly, 
you're doing too much work yourself, rather than letting Python do the 
heavy lifting for you. These practices and tools make life a lot easier:


* As others have noted, don't accumulate output in a list. Just write 
data to the output file line-by-line.


* You don't need to initialize every variable at the beginning of the 
program. But there's no harm in it.


* Use the enumerate() function to provide a line counter:

  for counter, line in enumerate(file1):

This eliminates the need to accumulate output data in a list, then use 
the index variable j as the line counter.


* Use string formatting. Each chunk of output is a two-line string, with 
the line-counter and the DNA sequence as variables:


  outformat = seq%05d
  %s
  

  ... later, inside your loop ...

  resultsfile.write(outformat % (counter, sequence))

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


Re: Need help with a program

2010-01-28 Thread D'Arcy J.M. Cain
On Thu, 28 Jan 2010 18:49:02 +0100
Jean-Michel Pichavant jeanmic...@sequans.com wrote:
 Using regexp may increase readability (if you are familiar with it). 

If you have a problem and you think that regular expressions are the
solution then now you have two problems.  Regex is really overkill for
the OP's problem and it certainly doesn't improve readability.

-- 
D'Arcy J.M. Cain da...@druid.net |  Democracy is three wolves
http://www.druid.net/darcy/|  and a sheep voting on
+1 416 425 1212 (DoD#0082)(eNTP)   |  what's for dinner.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Need help with a program

2010-01-28 Thread Jean-Michel Pichavant

evilweasel wrote:

I will make my question a little more clearer. I have close to 60,000
lines of the data similar to the one I posted. There are various
numbers next to the sequence (this is basically the number of times
the sequence has been found in a particular sample). So, I would need
to ignore the ones containing '0' and write all other sequences
(excluding the number, since it is trivial) in a new text file, in the
following format:

  

seq59902


TTTATTATATAGT

  

seq59903


TTTATTTCTTGGCGTTGT

  

seq59904


TTTGGTTGCCCTGCGTGG

  

seq59905


TTTGTTTATGGG

The number next to 'seq' is the line number of the sequence. When I
run the above program, what I expect is an output file that is similar
to the above output but with the ones containing '0' ignored. But, I
am getting all the sequences printed in the file.

Kindly excuse the 'newbieness' of the program. :) I am hoping to
improve in the next few months. Thanks to all those who replied. I
really appreciate it. :)
  
Using regexp may increase readability (if you are familiar with it). 
What about


import re

output = open(sequences1.txt, 'w')

for index, line in enumerate(open(sys.argv[1], 'r')):
   match = re.match('(?Psequence[GATC]+)\s+1')
   if match:
   output.write('seq%s\n%s\n' % (index, match.group('sequence')))


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


Re: Need help with a program

2010-01-28 Thread Jean-Michel Pichavant

D'Arcy J.M. Cain wrote:

On Thu, 28 Jan 2010 18:49:02 +0100
Jean-Michel Pichavant jeanmic...@sequans.com wrote:
  
Using regexp may increase readability (if you are familiar with it). 



If you have a problem and you think that regular expressions are the
solution then now you have two problems.  Regex is really overkill for
the OP's problem and it certainly doesn't improve readability.

  

It depends on the reader ability to understand a *simple* regexp.
It is also strange to get such answer after taking so much precautions, 
so let me quote myself:


Using regexp *may* increase readability (*if* you are *familiar* with it). 

I honestly find it quite readable in the sample code I provided and 
spares all the if-len-startwith-strip logic, but If the OP does not 
agree, fine with me. But there's no need to get certain that I'm 
completly wrong.


JM


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


Re: Need help with a program

2010-01-28 Thread Steven Howe

On 01/28/2010 09:49 AM, Jean-Michel Pichavant wrote:

evilweasel wrote:

I will make my question a little more clearer. I have close to 60,000
lines of the data similar to the one I posted. There are various
numbers next to the sequence (this is basically the number of times
the sequence has been found in a particular sample). So, I would need
to ignore the ones containing '0' and write all other sequences
(excluding the number, since it is trivial) in a new text file, in the
following format:


seq59902

TTTATTATATAGT


seq59903

TTTATTTCTTGGCGTTGT


seq59904

TTTGGTTGCCCTGCGTGG


seq59905

TTTGTTTATGGG

The number next to 'seq' is the line number of the sequence. When I
run the above program, what I expect is an output file that is similar
to the above output but with the ones containing '0' ignored. But, I
am getting all the sequences printed in the file.

Kindly excuse the 'newbieness' of the program. :) I am hoping to
improve in the next few months. Thanks to all those who replied. I
really appreciate it. :)
Using regexp may increase readability (if you are familiar with it). 
What about


import re

output = open(sequences1.txt, 'w')

for index, line in enumerate(open(sys.argv[1], 'r')):
   match = re.match('(?Psequence[GATC]+)\s+1')
   if match:
   output.write('seq%s\n%s\n' % (index, match.group('sequence')))


Jean-Michel


Finally!

After ready 8 or 9 messages about find a line ending with '1', someone 
suggests Regex.

It was my first thought.

Steven

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


Re: Need help with a program

2010-01-28 Thread Mensanator
On Jan 28, 12:28 pm, Steven Howe howe.ste...@gmail.com wrote:
 On 01/28/2010 09:49 AM, Jean-Michel Pichavant wrote:





  evilweasel wrote:
  I will make my question a little more clearer. I have close to 60,000
  lines of the data similar to the one I posted. There are various
  numbers next to the sequence (this is basically the number of times
  the sequence has been found in a particular sample). So, I would need
  to ignore the ones containing '0' and write all other sequences
  (excluding the number, since it is trivial) in a new text file, in the
  following format:

  seq59902
  TTTATTATATAGT

  seq59903
  TTTATTTCTTGGCGTTGT

  seq59904
  TTTGGTTGCCCTGCGTGG

  seq59905
  TTTGTTTATGGG

  The number next to 'seq' is the line number of the sequence. When I
  run the above program, what I expect is an output file that is similar
  to the above output but with the ones containing '0' ignored. But, I
  am getting all the sequences printed in the file.

  Kindly excuse the 'newbieness' of the program. :) I am hoping to
  improve in the next few months. Thanks to all those who replied. I
  really appreciate it. :)
  Using regexp may increase readability (if you are familiar with it).
  What about

  import re

  output = open(sequences1.txt, 'w')

  for index, line in enumerate(open(sys.argv[1], 'r')):
     match = re.match('(?Psequence[GATC]+)\s+1')
     if match:
         output.write('seq%s\n%s\n' % (index, match.group('sequence')))

  Jean-Michel

 Finally!

 After ready 8 or 9 messages about find a line ending with '1', someone
 suggests Regex.
 It was my first thought.

And as a first thought, it is, of course, wrong.

You don't want lines ending in '1', you want ANY non-'0' amount.

Likewise, you don't want to exclude lines ending in '0' because
you'll end up excluding counts of 10, 20, 30, etc.

You need a regex that extracts ALL the numeric characters at the end
of the
line and exclude those that evaluate to 0.


 Steven

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


Re: Need help with a program

2010-01-28 Thread MRAB

Steven Howe wrote:

On 01/28/2010 09:49 AM, Jean-Michel Pichavant wrote:

evilweasel wrote:

I will make my question a little more clearer. I have close to 60,000
lines of the data similar to the one I posted. There are various
numbers next to the sequence (this is basically the number of times
the sequence has been found in a particular sample). So, I would need
to ignore the ones containing '0' and write all other sequences
(excluding the number, since it is trivial) in a new text file, in the
following format:


seq59902

TTTATTATATAGT


seq59903

TTTATTTCTTGGCGTTGT


seq59904

TTTGGTTGCCCTGCGTGG


seq59905

TTTGTTTATGGG

The number next to 'seq' is the line number of the sequence. When I
run the above program, what I expect is an output file that is similar
to the above output but with the ones containing '0' ignored. But, I
am getting all the sequences printed in the file.

Kindly excuse the 'newbieness' of the program. :) I am hoping to
improve in the next few months. Thanks to all those who replied. I
really appreciate it. :)
Using regexp may increase readability (if you are familiar with it). 
What about


import re

output = open(sequences1.txt, 'w')

for index, line in enumerate(open(sys.argv[1], 'r')):
   match = re.match('(?Psequence[GATC]+)\s+1')
   if match:
   output.write('seq%s\n%s\n' % (index, match.group('sequence')))


Jean-Michel


Finally!

After ready 8 or 9 messages about find a line ending with '1', someone 
suggests Regex.

It was my first thought.


I'm a great fan of regexes, but I never though of using them for this
because it doesn't look like a regex type of problem to me.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Need help with a program

2010-01-28 Thread nn


Arnaud Delobelle wrote:
 nn prueba...@latinmail.com writes:

  On Jan 28, 10:50 am, evilweasel karthikramaswam...@gmail.com wrote:
  I will make my question a little more clearer. I have close to 60,000
  lines of the data similar to the one I posted. There are various
  numbers next to the sequence (this is basically the number of times
  the sequence has been found in a particular sample). So, I would need
  to ignore the ones containing '0' and write all other sequences
  (excluding the number, since it is trivial) in a new text file, in the
  following format:
 
  seq59902
 
  TTTATTATATAGT
 
  seq59903
 
  TTTATTTCTTGGCGTTGT
 
  seq59904
 
  TTTGGTTGCCCTGCGTGG
 
  seq59905
 
  TTTGTTTATGGG
 
  The number next to 'seq' is the line number of the sequence. When I
  run the above program, what I expect is an output file that is similar
  to the above output but with the ones containing '0' ignored. But, I
  am getting all the sequences printed in the file.
 
  Kindly excuse the 'newbieness' of the program. :) I am hoping to
  improve in the next few months. Thanks to all those who replied. I
  really appreciate it. :)
 
  People have already given you some pointers to your problem. In the
  end you will have to tweak the details because only you have access
  to the data not us.
 
  Just as example here is another way to do what you are doing:
 
  with open('dnain.dat') as infile, open('dnaout.dat','w') as outfile:
 partgen=(line.split() for line in infile)
 dnagen=(str(i+1)+'\n'+part[0]+'\n'
 for i,part in enumerate(partgen)
 if len(part)1 and part[1]!='0')
 outfile.writelines(dnagen)

 I think that generator expressions are overrated :) What's wrong with:

 with open('dnain.dat') as infile, open('dnaout.dat','w') as outfile:
 for i, line in enumerate(infile):
 parts = line.split()
 if len(parts)  1 and parts[1] != '0':
 outfile.write(seq%s\n%s\n % (i+1, parts[0]))

 (untested)

 --
 Arnaud

Nothing really,
After posting I was thinking I should have posted a more
straightforward version like the one you wrote. Now there is! It
probably is more efficient too. I just have a tendency to think in
terms of pipes: pipe this junk in here, then in here, get output.
Probably damage from too much Unix scripting.Since I can't resist the
urge to post crazy code here goes the bonus round (don't do this at
work):

open('dnaout.dat','w').writelines(
   'seq%s\n%s\n'%(i+1,part[0])
   for i,part in enumerate(line.split() for line in open('dnain.dat'))
   if len(part)1 and part[1]!='0')
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Need help with a program

2010-01-28 Thread Arnaud Delobelle
nn prueba...@latinmail.com writes:
 After posting I was thinking I should have posted a more
 straightforward version like the one you wrote. Now there is! It
 probably is more efficient too. I just have a tendency to think in
 terms of pipes: pipe this junk in here, then in here, get output.
 Probably damage from too much Unix scripting.

This is funny, I did think *exactly* this when I saw your code :)

-- 
Arnaud

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


Need help with first program to connect to mysql database via apache and python.

2008-02-06 Thread pythonbrian
I am just learning python and I am trying to create a simple
connection to a mysql table via Python and Apache, using a Python
program
Unfortunately I keep getting an internal server error (50), when I
bring it up in my browser ... information attached.
Any help would be appreciated ...
Thx, [EMAIL PROTECTED]

Information #1
error in /var/log/apache2/error.log

[Wed Feb 06 20:04:31 2008] [error] [client 127.0.0.1] (2)No such file
or directory: exec of '/var/www/cgi-bin/fig17_27.py' failed

[Wed Feb 06 20:04:31 2008] [error] [client 127.0.0.1] Premature end of
script headers: fig17_27.py


---
Information #2
directory information

[EMAIL PROTECTED]:/var/log/apache2$ cd /var/www/cgi-bin

[EMAIL PROTECTED]:/var/www/cgi-bin$ ls -al

total 24

drwxr-xr-x 2 root root 4096 2008-02-06 15:03 .

drwxr-xr-x 4 root root 4096 2008-02-02 20:53 ..

-rwxr-xr-x 1 root root 1569 2008-02-02 21:02 fig06_03.py

-rwxr-xr-x 1 root root 2067 2008-02-02 21:05 fig06_05.py

-rwxr-xr-x 1 root root 2031 2008-02-02 21:19 fig06_06.py

-rwxr-xr-x 1 root root 3489 2008-02-06 15:03 fig17_27.py


-
Web Error

http://localhost/cgi-bin/fig17_27.py

Internal Server Error
The server encountered an internal error or misconfiguration and was
unable to complete your request.
Please contact the server administrator, [EMAIL PROTECTED] and
inform them of the time the error occurred, and anything you might
have done that may have caused the error.
More information about this error may be available in the server error
log.

Apache/2.2.4 (Ubuntu) mod_python/3.3.1 Python/2.5.1 PHP/
5.2.3-1ubuntu6.3 Server at localhost Port 80

--
Program File
[EMAIL PROTECTED]:/var/www/cgi-bin$ cat fig17_27.py


#!/usr/local/bin/python

# Fig. 17.27: fig17_27.py

# Displays contents of the Authors table,

# ordered by a specified field.



import MySQLdb

import cgi

import sys



def printHeader( title ):

   print Content-type: text/html



?xml version = 1.0 encoding = UTF-8?

!DOCTYPE html PUBLIC

   -//W3C//DTD XHTML 1.0 Transitional//EN

   DTD/xhtml1-transitional.dtd

html xmlns = http://www.w3.org/1999/xhtml;

   xml:lang = en lang = en

headtitle%s/title/head



body % title



# obtain user query specifications

form = cgi.FieldStorage()



# get sortBy value

if form.has_key( sortBy ):

   sortBy = form[ sortBy ].value

else:

   sortBy = firstName



# get sortOrder value

if form.has_key( sortOrder ):

   sortOrder = form[ sortOrder ].value

else:

   sortOrder = ASC



printHeader( Authors table from Books )



# connect to database and retrieve a cursor

try:

   connection = MySQLdb.connect( db = Books, user = root )





# error connecting to database

except MySQLdb.OperationalError, error:

   print Error:, error

   sys.exit( 1 )



# retrieve cursor

else:

   cursor = connection.cursor()



# query all records from Authors table

cursor.execute( SELECT * FROM Authors ORDER BY %s %s %

   ( sortBy, sortOrder ) )



allFields = cursor.description  # get field names

allRecords = cursor.fetchall()  # get records



# close cursor and connection

cursor.close()

connection.close()



# output results in a table

print \ntable border = 1 cellpadding = 3 

  tr bgcolor = silver 



# create table header

for field in allFields:

   print td%s/td % field[ 0 ]



print /tr



# display each record as a row

for author in allRecords:

   print tr



   for item in author:

  print td%s/td % item



   print /tr



print /table



# obtain sorting method from user

print 

  \nform method = post action = /cgi-bin/fig17_27.py

  Sort By:br /



# display sorting options

for field in allFields:

   print input type = radio name = sortBy

  value = %s / % field[ 0 ]

   print field[ 0 ]

   print br /



print br /\nSort Order:br /

  input type = radio name = sortOrder

  value = ASC checked = checked /

  Ascending

  input type = radio name = sortOrder

  value = DESC /

  Descending

  br /br /\ninput type = submit value = SORT /

  /form\n\n/body\n/html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Need help with first program to connect to mysql database via apache and python.

2008-02-06 Thread Steve Holden
pythonbrian wrote:
 I am just learning python and I am trying to create a simple
 connection to a mysql table via Python and Apache, using a Python
 program
 Unfortunately I keep getting an internal server error (50), when I
 bring it up in my browser ... information attached.
 Any help would be appreciated ...
 Thx, [EMAIL PROTECTED]
 
 Information #1
 error in /var/log/apache2/error.log
 
 [Wed Feb 06 20:04:31 2008] [error] [client 127.0.0.1] (2)No such file
 or directory: exec of '/var/www/cgi-bin/fig17_27.py' failed
 
 [Wed Feb 06 20:04:31 2008] [error] [client 127.0.0.1] Premature end of
 script headers: fig17_27.py
 
 
 ---
 Information #2
 directory information
 
 [EMAIL PROTECTED]:/var/log/apache2$ cd /var/www/cgi-bin
 
 [EMAIL PROTECTED]:/var/www/cgi-bin$ ls -al
 
 total 24
 
 drwxr-xr-x 2 root root 4096 2008-02-06 15:03 .
 
 drwxr-xr-x 4 root root 4096 2008-02-02 20:53 ..
 
 -rwxr-xr-x 1 root root 1569 2008-02-02 21:02 fig06_03.py
 
 -rwxr-xr-x 1 root root 2067 2008-02-02 21:05 fig06_05.py
 
 -rwxr-xr-x 1 root root 2031 2008-02-02 21:19 fig06_06.py
 
 -rwxr-xr-x 1 root root 3489 2008-02-06 15:03 fig17_27.py
 
 
 -
 Web Error
 
 http://localhost/cgi-bin/fig17_27.py
 
 Internal Server Error
 The server encountered an internal error or misconfiguration and was
 unable to complete your request.
 Please contact the server administrator, [EMAIL PROTECTED] and
 inform them of the time the error occurred, and anything you might
 have done that may have caused the error.
 More information about this error may be available in the server error
 log.
 
 Apache/2.2.4 (Ubuntu) mod_python/3.3.1 Python/2.5.1 PHP/
 5.2.3-1ubuntu6.3 Server at localhost Port 80
 
 --
 Program File
[EMAIL PROTECTED]:/var/www/cgi-bin$ cat fig17_27.py


#!/usr/local/bin/python


[...]

I've snipped the code, because the error message indicates it isn't 
being executed: your problem is more fundamental than a Python error.

I take it you do have an executable /usr/local/bin/python?

One thing that can trip you up is if you have carriage returns in tour 
script: then Apache fails to find the program from the shebang line 
because it takes the DR before the LF as part of the filename.

regards
  Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

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


Re: Need help running external program

2005-03-03 Thread Rigga
Thanks to all for your help it is now working, I rant he code through a
debugger and found that the input file I was using to create my list of
addresses to wget had newlines in them and were therefore breaking my
command line.

All your advice has been appreciated.

RiGGa

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


Re: Need help running external program

2005-03-02 Thread Rigga
Brian van den Broek wrote:

 Rigga said unto the world upon 2005-02-27 15:04:
 Tim Jarman wrote:
 
 SNIP
 
No, the r was the point - it's there to tell Python not to do any
escaping on the string. Try it again with the r and see what happens.

 
 Brilliant!!!  that works a treat thankyou!!, where on earth did you find
 out
 about the 'r'  any pointers to documentation appreciated.
 
 Thanks
 
 RiGGa
 
 http://www.python.org/doc/current/ref/strings.html
 http://www.python.org/doc/current/lib/module-re.html
 
 Best,
 
 Brian vdB
Thanks for all your help with this it is appreciated, one further question
though, how do I pass a variable to the external program while using the
r

Thanks

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


Re: Need help running external program

2005-03-02 Thread Tim Jarman
Rigga wrote:

 Brian van den Broek wrote:
 
 Rigga said unto the world upon 2005-02-27 15:04:

(snip stuff about raw strings)

 Thanks for all your help with this it is appreciated, one further question
 though, how do I pass a variable to the external program while using the
 r
 
 Thanks
 
 RiGGa

I'm not sure I understand the question. Say you have:

parameter = rmy \funky \text

then surely you just pass it to your external program using whichever method
you like, e.g.

import os
os.execl(your_external_prog, parameter) # replaces the current process

or some variant of:

return_code = os.spawnl(os.P_WAIT, your_external_prog, parameter) 

or you can build a command line:

command = your_external_prog %s % parameter
return_code = os.system(command)

(see docs on the os module for more variations on this theme than you can
shack a stick at)

It's just a string, after all.



-- 
Website: www DOT jarmania FULLSTOP com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Need help running external program

2005-03-02 Thread Tim Jarman
Rigga wrote:

(snip)

 
 This is the command I am trying to run:
 
 feed is a list of web addresses
 
 output, input = popen2(wget -q %s -O - | tr '\r' '\n' | tr \' \ | sed -n
 's/.*url=\([^]*\).*/\1/p' % feed[counter])
 
 But it does not work, if I escape the string using r and hard code in
 the web address rather than use %s and feed[counter] it works, my question
 is how do I escape the string to get it to work with the %s and
 feed[counter]
 
 Im new to python as you can tell :-)

Disclaimer: I know nothing about wget beyond what just having typed 'man
wget' told me! ;)

1. What *exactly* does it does not work mean? Do you get a traceback? If
so, post it - that will help others to help you. What results are you
expecting?

2. Are you sure feed contains what you think it contains at this point in
your program? What do you see if you do:

for thing in feed: print thing

?

A good strategy in these cases is to go in small steps. before you try
getting fancy with popen2, have your program just print the command-line
correctly. Then maybe try it with something like echo just to see that
you're passing what you think you're passing. And so on.

We were all new once - no blame! :)

-- 
Website: www DOT jarmania FULLSTOP com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Need help running external program

2005-03-02 Thread Steve Holden
Rigga wrote:
Tim Jarman wrote:

Rigga wrote:

Brian van den Broek wrote:

Rigga said unto the world upon 2005-02-27 15:04:
(snip stuff about raw strings)

Thanks for all your help with this it is appreciated, one further
question though, how do I pass a variable to the external program while
using the r
Thanks
RiGGa
I'm not sure I understand the question. Say you have:
parameter = rmy \funky \text
then surely you just pass it to your external program using whichever
method you like, e.g.
import os
os.execl(your_external_prog, parameter) # replaces the current process
or some variant of:
return_code = os.spawnl(os.P_WAIT, your_external_prog, parameter)
or you can build a command line:
command = your_external_prog %s % parameter
return_code = os.system(command)
(see docs on the os module for more variations on this theme than you can
shack a stick at)
It's just a string, after all.

This is the command I am trying to run:
feed is a list of web addresses
output, input = popen2(wget -q %s -O - | tr '\r' '\n' | tr \' \ | sed -n
's/.*url=\([^]*\).*/\1/p' % feed[counter])
But it does not work, if I escape the string using r and hard code in the
web address rather than use %s and feed[counter] it works, my question is
how do I escape the string to get it to work with the %s and feed[counter]
Im new to python as you can tell :-)
Right, using raw strings (r ... ) makes sure that backslashes in the 
literal are retained rather than used as escapes. Socould you show us an 
example where the expression (using  ... % feed[counter]) gives you a 
different value from hard coding the web address?

I suspect if you use a raw string for the format then that will be 
enough - in other words, does

output, input = popen2(rwget -q %s -O - | tr '\r' '\n' | tr \' \ | 
sed -n 's/.*url=\([^]*\).*/\1/p' % feed[counter])

work?
regards
 Steve
--
Meet the Python developers and your c.l.py favorites March 23-25
Come to PyCon DC 2005  http://www.pycon.org/
Steve Holden   http://www.holdenweb.com/
--
http://mail.python.org/mailman/listinfo/python-list


Re: Need help running external program

2005-02-27 Thread Pink
Rigga wrote:

 Hi,
 
 I am running the line of code below from a shell script and it works fine,
 however I am at a total loss on how i can run it from within a Python
 script as every option I have tried fails and it appears to be down to the
 escaping of certain characters.
 
 wget -q www.anywebpage.com -O - | tr '\r' '\n' | tr \' \ | sed -n
 's/.*url=\([^]*\).*/\1/p'
If your problem is getting a python string without worrying about how to
escape the escape sequences, try:

rwget -q www.anywebpage.com -O - | tr '\r' '\n' | tr \' \ | sed -n
's/.*url=\([^]*\).*/\1/p'

You should be able to pass this directly to a popen() function.

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


Re: Need help running external program

2005-02-27 Thread Rigga
Pink wrote:

 Rigga wrote:
 
 Hi,
 
 I am running the line of code below from a shell script and it works
 fine, however I am at a total loss on how i can run it from within a
 Python script as every option I have tried fails and it appears to be
 down to the escaping of certain characters.
 
 wget -q www.anywebpage.com -O - | tr '\r' '\n' | tr \' \ | sed -n
 's/.*url=\([^]*\).*/\1/p'
 If your problem is getting a python string without worrying about how to
 escape the escape sequences, try:
 
 rwget -q www.anywebpage.com -O - | tr '\r' '\n' | tr \' \ | sed -n
 's/.*url=\([^]*\).*/\1/p'
 
 You should be able to pass this directly to a popen() function.

Hi,

Thanks for replying however I have just tried that and it does not seem to
work, it doesnt return any results (i take it the r was a typo)

Thanks

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


Re: Need help running external program

2005-02-27 Thread Leif B. Kristensen
I'm using wget from Python to get extactly one line from a reports page.
I made this function that works for me:

def wgetline(exp):  # see Python Cookbook p. 228
print Getting result from server ...
command = 'wget -q -O - \
http://www.foobar.com/report.pl\?UserID=xxx\UserPW=xxx \
| grep ' + exp
child = os.popen(command)
data = child.read()
return data

I had to escape the ? and  in the url, or the CGI script at the other
end would refuse to cooperate with Invalid UserID or UserPW.
-- 
Leif Biberg Kristensen
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Need help running external program

2005-02-27 Thread Tim Jarman
Rigga wrote:

 Pink wrote:
 
 Rigga wrote:
 
 Hi,
 
 I am running the line of code below from a shell script and it works
 fine, however I am at a total loss on how i can run it from within a
 Python script as every option I have tried fails and it appears to be
 down to the escaping of certain characters.
 
 wget -q www.anywebpage.com -O - | tr '\r' '\n' | tr \' \ | sed -n
 's/.*url=\([^]*\).*/\1/p'
 If your problem is getting a python string without worrying about how to
 escape the escape sequences, try:
 
 rwget -q www.anywebpage.com -O - | tr '\r' '\n' | tr \' \ | sed -n
 's/.*url=\([^]*\).*/\1/p'
 
 You should be able to pass this directly to a popen() function.
 
 Hi,
 
 Thanks for replying however I have just tried that and it does not seem to
 work, it doesnt return any results (i take it the r was a typo)
 
 Thanks
 
 RiGGa

No, the r was the point - it's there to tell Python not to do any escaping
on the string. Try it again with the r and see what happens.

-- 
Website: www DOT jarmania FULLSTOP com
-- 
http://mail.python.org/mailman/listinfo/python-list



Re: Need help running external program

2005-02-27 Thread Rigga
Tim Jarman wrote:

 Rigga wrote:
 
 Pink wrote:
 
 Rigga wrote:
 
 Hi,
 
 I am running the line of code below from a shell script and it works
 fine, however I am at a total loss on how i can run it from within a
 Python script as every option I have tried fails and it appears to be
 down to the escaping of certain characters.
 
 wget -q www.anywebpage.com -O - | tr '\r' '\n' | tr \' \ | sed -n
 's/.*url=\([^]*\).*/\1/p'
 If your problem is getting a python string without worrying about how to
 escape the escape sequences, try:
 
 rwget -q www.anywebpage.com -O - | tr '\r' '\n' | tr \' \ | sed -n
 's/.*url=\([^]*\).*/\1/p'
 
 You should be able to pass this directly to a popen() function.
 
 Hi,
 
 Thanks for replying however I have just tried that and it does not seem
 to work, it doesnt return any results (i take it the r was a typo)
 
 Thanks
 
 RiGGa
 
 No, the r was the point - it's there to tell Python not to do any escaping
 on the string. Try it again with the r and see what happens.
 
Brilliant!!!  that works a treat thankyou!!, where on earth did you find out
about the 'r'  any pointers to documentation appreciated.

Thanks 

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


Re: Need help running external program

2005-02-27 Thread Brian van den Broek
Rigga said unto the world upon 2005-02-27 15:04:
Tim Jarman wrote:
SNIP
No, the r was the point - it's there to tell Python not to do any escaping
on the string. Try it again with the r and see what happens.
Brilliant!!!  that works a treat thankyou!!, where on earth did you find out
about the 'r'  any pointers to documentation appreciated.
Thanks 

RiGGa
http://www.python.org/doc/current/ref/strings.html
http://www.python.org/doc/current/lib/module-re.html
Best,
Brian vdB
--
http://mail.python.org/mailman/listinfo/python-list


Re: Need help running external program

2005-02-27 Thread Pink
Rigga wrote:
 No, the r was the point - it's there to tell Python not to do any
 escaping on the string. Try it again with the r and see what happens.
 
 Brilliant!!!  that works a treat thankyou!!, where on earth did you find
 out
 about the 'r'  any pointers to documentation appreciated.
This is a pretty common problem when working with regular expression (which
usually contain many backslashes) - that's where I saw this syntax for the
first time (e.g. http://docs.python.org/lib/match-objects.html).
The official reference for string literals is here:
http://docs.python.org/ref/strings.html

c ya

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