Re: [Tutor] redirecting testing output to file, using two different options
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
Re: [Tutor] redirecting testing output to file, using two different options
Yosef Levy wrote: > 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? You can either write a file-like object that uses Logger methods under the hood or write your own test runner that uses Logger methods. A basic example targeting Python 3: $ cat log_unittest.py import logging import unittest import os logger = logging.getLogger() class TestMyEnvClass (unittest.TestCase): def setUp(self): pass def testMyEnv(self): res = os.environ.get("res") == "1" self.assertEqual(res, True, 'Testing my environment..') def tearDown(self): pass class LogStream: def __init__(self, logger): self.logger = logger def writeln(self, s=""): self.logger.info(s) def write(self, s): self.logger.info(s) def flush(self): pass class MyTextTestRunner(unittest.TextTestRunner): def __init__(self, logger): super().__init__(None) self.stream = LogStream(logger) if __name__ == "__main__": logging.basicConfig(level=logging.INFO) logging.info("hello world") runner = MyTextTestRunner(logger) unittest.main(testRunner=runner) $ res=1 python3 log_unittest.py INFO:root:hello world INFO:root:. INFO:root: INFO:root:-- INFO:root:Ran 1 test in 0.000s INFO:root: INFO:root:OK INFO:root: $ res=0 python3 log_unittest.py INFO:root:hello world INFO:root:F INFO:root: INFO:root:== INFO:root:FAIL: testMyEnv (__main__.TestMyEnvClass) INFO:root:-- INFO:root:Traceback (most recent call last): File "log_unittest.py", line 14, in testMyEnv self.assertEqual(res, True, 'Testing my environment..') AssertionError: False != True : Testing my environment.. INFO:root:-- INFO:root:Ran 1 test in 0.001s INFO:root: INFO:root:FAILED INFO:root: (failures=1) If you want more control you have to rewrite more code ;) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Scraping old twitter posts
Hi guys ! I didn't use Python for c. 2 years hence my skills are a little bit rusty. I will really appreciate some hints or a guideline. I would like to search my Twitter feed and search/scrape for some "specific words" and create a list for further analysis. Can someone recommend me some quality resources on this topic? Thanks Matej ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Scraping old twitter posts
On 05/17/2018 04:47 AM, matej taferner wrote: > Hi guys ! > I didn't use Python for c. 2 years hence my skills are a little bit rusty. > I will really appreciate some hints or a guideline. > I would like to search my Twitter feed and search/scrape for some "specific > words" and create a list for further analysis. > Can someone recommend me some quality resources on this topic? Don't know if anyone has enough experience to comment on "quality" - we'll wait and see. But it's not really the kind of question this list is set up for. Getting data out of a website like Twitter should be done using their API, not by scraping, if at all possible: the API delivers data in a way that Twitter is okay with; scraping is against the terms of service unless you have explicit permission - and don't take that too lightly, LinkedIn has been busy suing people for scraping their site, for example. There are several existing projects that get data out of Twitter which you can search for (look on pypi.org for example, or github). If your objective is a Python learning project you might want to look for some of the tutorials on the subject and avoid ready-made projects. Python Tutor list will be more useful to you if you come with specific coding questions - I tried this and it doesn't work; I don't understand this; etc. Best of luck! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor