[Tutor] how to redirect input from pipe

2017-03-22 Thread Yosef Levy
Hello All,

I am running with Python 2.7
I have to run script that could have get arguments in two ways:
1. argument + file name.
2. argument + input from pipe.

example for 1:
./my_script.py -t 1,2,3 file_name.txt

example for 2:
grep snd file_name.txt | ./my_script.py -t 1,2,3

I am using "parse_known_args" to parse the rest of args when pipe exists.
and capture with: "fileinput".

My problem is that this does not run always, for second option.
Any idea how could I get standard input with additional flag?
for example, running with pipe option will be like this:
grep snd file_name.txt | ./my_script.py -t 1,2,3 -
where the additional "-" at the end will indicate script to get standard
input.

Rgds.


my script:
---
#!/usr/bin/python

import fileinput
import argparse


class TAG(object):
   pass




tag = TAG ()
parser = argparse.ArgumentParser()
parser.add_argument('-t', help = "tags,\n for example: -t 35,150,39")
args, unk = parser.parse_known_args(namespace=tag)

tag_list = tag.t.split(',')


for line in fileinput.input(unk):
 print line
---
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to redirect input from pipe

2017-03-30 Thread Yosef Levy
Thank you, it was very helpful.

בתאריך 23 במרץ 2017 02:39,‏ "Peter Otten" <__pete...@web.de> כתב:

> Yosef Levy wrote:
>
> > Hello All,
> >
> > I am running with Python 2.7
> > I have to run script that could have get arguments in two ways:
> > 1. argument + file name.
> > 2. argument + input from pipe.
> >
> > example for 1:
> > ./my_script.py -t 1,2,3 file_name.txt
> >
> > example for 2:
> > grep snd file_name.txt | ./my_script.py -t 1,2,3
> >
> > I am using "parse_known_args" to parse the rest of args when pipe exists.
> > and capture with: "fileinput".
> >
> > My problem is that this does not run always, for second option.
> > Any idea how could I get standard input with additional flag?
> > for example, running with pipe option will be like this:
> > grep snd file_name.txt | ./my_script.py -t 1,2,3 -
> > where the additional "-" at the end will indicate script to get standard
> > input.
>
> It's not clear to me why you use parse_known_args(). The examples you give
> above seem to be covered by
>
> $ cat tmp.py
> #!/usr/bin/env python
> import argparse
> import fileinput
>
> parser = argparse.ArgumentParser()
> parser.add_argument("--tags", "-t")
> parser.add_argument("files", metavar="file", nargs="*")
> args = parser.parse_args()
>
> print args.tags
> for line in fileinput.input(args.files):
> print line.strip()
> $ cat greek.txt
> alpha
> beta
> gamma
> delta
> $ cat numbers.txt
> ONE
> TWO
>
> Reading from a file:
>
> $ ./tmp.py -t 1,2,3 numbers.txt
> 1,2,3
> ONE
> TWO
>
> Reading from two files:
>
> $ ./tmp.py -t 1,2,3 numbers.txt greek.txt
> 1,2,3
> ONE
> TWO
> alpha
> beta
> gamma
> delta
>
> Reading from stdin:
>
> $ grep ^[ab] greek.txt | ./tmp.py -t 1,2,3
> 1,2,3
> alpha
> beta
>
> Reading from a file, then stdin, then another file:
>
> $ grep ^[ab] greek.txt | ./tmp.py -t 1,2,3 numbers.txt - numbers.txt
> 1,2,3
> ONE
> TWO
> alpha
> beta
> ONE
> TWO
> $
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] redirecting testing output to file, using two different options

2018-05-16 Thread Yosef Levy
Hello All,

I have testing environment.
Code is written under Python 2.6.
testing output should be redirected to log file.
I am using two different file handling, in order to log output:



1. logging module:

import unittest2
import logging
import sys

class TestMyEnvClass (unittest2.TestCase):
def setUp (self):
pass
def testMyEnv (self):
logging.debug  ( "In testMyEnv.." )
def tearDown (self):
pass

if __name__ == "__main__":
logging.basicConfig( filename='log_file.txt', stream=sys.stderr,
level=logging.DEBUG)
logging.getLogger( "TestMyEnvClass.testMyEnv" ).setLevel( logging.DEBUG
)
unittest2.main()


running:
python test.py

output:
# cat log_file.txt
DEBUG:root:In testMyEnv..
#





2. but if I want to redirect assert messages to file, I have to:

import unittest2

class TestMyEnvClass (unittest2.TestCase):
def setUp (self):
pass
def testMyEnv (self):
res = True
self.assertEqual(res, True, 'Testing my environment..')
def tearDown (self):
pass

if __name__ == "__main__":
f = open('log_file.txt', "a")
runner = unittest2.TextTestRunner(f)
unittest2.main(testRunner=runner)
f.close ()



running:
# python test2.py

output:
# cat log_file.txt
.
--
Ran 1 test in 0.000s

OK



Question:
Is the a way to 'merge' the above two different ways output to file?
Or, is there a way to redirect assert messages to logging module methods?


Rgds.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] redirecting testing output to file, using two different options

2018-05-17 Thread Yosef Levy
As I understand from documentation reading, pytest does not support
advanced file handling like logging module..
for example: log file rotate, limit file size etc.

I am using OS Centos6

2018-05-16 21:48 GMT+03:00 Mats Wichmann :

> On 05/16/2018 02:53 AM, Yosef Levy wrote:
> > Hello All,
> >
> > I have testing environment.
> > Code is written under Python 2.6.
>
> Why? 2.6 was retired in October 2013.  For long life of your code you
> should be using Python 3, but at least 2.7 is still current and getting
> updates (for another 18 months or so).
>
>
> > testing output should be redirected to log file.
> > I am using two different file handling, in order to log output:
> >
> >
> >
> > 1. logging module:
>
> > 2. but if I want to redirect assert messages to file, I have to:
> >
> > import unittest2
>
> > Question:
> > Is the a way to 'merge' the above two different ways output to file?
> > Or, is there a way to redirect assert messages to logging module methods?
>
> If you use pytest, this should be fairly straightforward. It normally
> captures stdout and stderr (the latter should have your asserts), and
> with a plugin, puts logging output on the same basis, so you get get
> everything going the same place:
>
> https://pypi.org/project/pytest-catchlog/#description
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor