Re: [Tutor] Request for help with os.walk() combining os.path.ismount() in Linux
On 21 August 2015 at 02:57, Alan Gauld wrote: > On 20/08/15 09:51, Srihari Vijayaraghavan wrote: > >> In general I agree, but this is what the os.walk() document states: >> "... When topdown is True, the caller can modify the dirnames list >> in-place (perhaps using del or slice assignment)..." > > > That's true so far as the os.walk call goes. > That is, altering dirs does not have any nagative > impact on the subsequent iterations of os.walk. > So even if you delete items from dirs os.walk will > continue to iterate over those deleted directories > in subsequent calls to os.walk() Thanks Alan. That's now practically learnt :-). > But altering dirs within the current iteration does affect > dirs within that iteration, just like any other collection. > So your for loop inside the os.walk loop is affected by the > deletions even if the outer os.walk loop is not. Yes, it was a bad idea to update dirs list while iterating over it. I was confused/mislead by the documentation, perhaps by misinterpreting it. Never mind, now it's quite clear that it's a no-no. Thanks your quick & perfect analysis. Srihari Vijayaraghavan ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] problem with code
Op 20-08-15 om 16:49 schreef Aravind Jaya: time = input("How long on average do you spend on the computer per day?") if time <= 2: print "Message1" else: print "Message2" This will raise 2 errors: - time will be a string. So time <= 2 is invalid. - print is a function. Updated code: time = int(input("How long on average do you spend on the computer per day?")) if time <= 2: print("Message1") else: print("Message2") Also the question is ambiguous. There is no time unit. What is "long"? Timo On Thu, Aug 20, 2015 at 3:20 PM, Nathan Clark <26110...@gmail.com> wrote: I have written a basic program out of python and it is not functioning, please could you proof read my code and tell me how to fix it.It is in python 3.3 time=int(input("How long on average do you spend on the computer per day?") (print("that seems reasonable")) if time<=2 else print ("get a life") ___ 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 maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] problem with code
On 20/08/2015 15:49, Aravind Jaya wrote: time = input("How long on average do you spend on the computer per day?") if time <= 2: print "Message1" else: print "Message2" If you insist on top posting you could at least get your response correct. The comparison will fail in Python 3.3 as time will be a string, you've missed the conversion to int, and print should be a function, not a statement. On Thu, Aug 20, 2015 at 3:20 PM, Nathan Clark <26110...@gmail.com> wrote: I have written a basic program out of python and it is not functioning, please could you proof read my code and tell me how to fix it.It is in python 3.3 time=int(input("How long on average do you spend on the computer per day?") (print("that seems reasonable")) if time<=2 else print ("get a life") -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Do not understand why test is running.
On 2015-08-20 23:16, Peter Otten wrote: Yea, breaking things is an art form ;) $ python3 -m unittest -h usage: python3 -m unittest [-h] [-v] [-q] [-f] [-c] [-b] [tests [tests ...]] . For test discovery all test modules must be importable from the top level directory of the project. How is "top level directory of the project" defined in this context? Is it as far up as one can travel while passing through directories containing an __init__.py file? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Do not understand why test is running.
On Fri, Aug 21, 2015 at 1:16 AM, Peter Otten <__pete...@web.de> wrote: > boB Stepp wrote: > >> On Thu, Aug 20, 2015 at 10:13 PM, Steven D'Aprano >> wrote: >>> On Thu, Aug 20, 2015 at 09:01:50PM -0500, boB Stepp wrote: >>> >>> import unittest # import modules to be tested: import mcm.db.manager class ManagerTestCase(unittest.TestCase): def setUp(self): # Insert setup code here... pass def test_open_db(self): pass def tearDown(self): # Insert tear-down code here... pass #if __name__ == "__main__": #unittest.main() >>> >>> >>> The two commented out lines at the end would, if uncommented, run >>> unittest.main() if and only if you are running this specific module as a >>> thread. In other words, if you were to run: >>> >>> python /path/to/the/test.py >>> >>> then __name__ would be set to the string "__main__", the if clause would >>> trigger, and unittest.main() would run. Since those lines are commented >>> out, that cannot happen. >> >> Okay, I uncommented those two lines and got: >> >> E:\Projects\mcm>py -m unittest ./mcm/test/db/test_manager.py In the cold light of morning, I see that in this invocation, the path is wrong. But even if I correct it, I get the same results: e:\Projects\mcm>py -m unittest ./test/db/test_manager.py Traceback (most recent call last): File "C:\Python34\lib\runpy.py", line 170, in _run_module_as_main "__main__", mod_spec) File "C:\Python34\lib\runpy.py", line 85, in _run_code exec(code, run_globals) File "C:\Python34\lib\unittest\__main__.py", line 18, in main(module=None) File "C:\Python34\lib\unittest\main.py", line 92, in __init__ self.parseArgs(argv) File "C:\Python34\lib\unittest\main.py", line 139, in parseArgs self.createTests() File "C:\Python34\lib\unittest\main.py", line 146, in createTests self.module) File "C:\Python34\lib\unittest\loader.py", line 146, in loadTestsFromNames suites = [self.loadTestsFromName(name, module) for name in names] File "C:\Python34\lib\unittest\loader.py", line 146, in suites = [self.loadTestsFromName(name, module) for name in names] File "C:\Python34\lib\unittest\loader.py", line 105, in loadTestsFromName module = __import__('.'.join(parts_copy)) ValueError: Empty module name > > Yea, breaking things is an art form ;) Alas! I am still artless because if I do as you suggest > If you want to trigger the > > if __name__ == "__main__": ... > > you have to invoke the test script with > > py ./mcm/test/db/test_manager.py > > the same way you would invoke any other script. e:\Projects\mcm>py ./test/db/test_manager.py Traceback (most recent call last): File "./test/db/test_manager.py", line 16, in import mcm.db.manager ImportError: No module named 'mcm' This is using the path correction I mention above. (But just to be certain, I also did the exact path Peter gave with the same results.) > py -m unittest > > runs the unittest module which is free to do what it wants with its > arguments. Let's see: The reason that I have been trying to invoke this individual test module the way I have is because in the docs it says: 26.3.2. Command-Line Interface The unittest module can be used from the command line to run tests from modules, classes or even individual test methods: python -m unittest test_module1 test_module2 python -m unittest test_module.TestClass python -m unittest test_module.TestClass.test_method You can pass in a list with any combination of module names, and fully qualified class or method names. Test modules can be specified by file path as well: python -m unittest tests/test_something.py This allows you to use the shell filename completion to specify the test module. The file specified must still be importable as a module. The path is converted to a module name by removing the ‘.py’ and converting path separators into ‘.’ So I am still quite confused! Uh, oh, I have to leave for work. Peter, I have not made it to the rest of your comments yet. That will have to wait till this evening, but many thanks for the assistance! If the answer is in your remaining comments, then I apologize for the additional noise in advance!! boB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Do not understand why test is running.
In a message of Fri, 21 Aug 2015 06:26:11 -0700, Alex Kleider writes: >On 2015-08-20 23:16, Peter Otten wrote: > > >> Yea, breaking things is an art form ;) > > >> $ python3 -m unittest -h >> usage: python3 -m unittest [-h] [-v] [-q] [-f] [-c] [-b] [tests [tests >> ...]] >> >. >> >> For test discovery all test modules must be importable from the top >> level >> directory of the project. > >How is "top level directory of the project" defined in this context? >Is it as far up as one can travel while passing through directories >containing an __init__.py file? With python3 you don't need an __init__.py file to have a module, see: https://www.python.org/dev/peps/pep-0420/ ... scratching head So I suppose you could make a test module that is importable from the top level directory, but doesn't have an __init__.py file. ... is that true? ... more scratching ... Laura ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Can someone explain this to me please
This code: import sys x = sys.maxsize print ("Max size is: ", x) y = (x + 1) print ("y is", type(y), "with a value of", y) Produces this result: Max size is: 9223372036854775807 y is with a value of 9223372036854775808 I was expecting it to error out but instead it produces a value greeter than the supposed maximum while still keeping it as an int. I’m confused. If sys.maxsize _isn’t_ the largest possible value then how do I determine what is? Jon Paris jon.f.pa...@gmail.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Can someone explain this to me please
In a message of Fri, 21 Aug 2015 14:04:18 -0400, Jon Paris writes: >This code: > >import sys >x = sys.maxsize >print ("Max size is: ", x) >y = (x + 1) >print ("y is", type(y), "with a value of", y) > >Produces this result: > >Max size is: 9223372036854775807 >y is with a value of 9223372036854775808 > >I was expecting it to error out but instead it produces a value greeter than >the supposed maximum while still keeping it as an int. I’m confused. If >sys.maxsize _isn’t_ the largest possible value then how do I determine what is? > > >Jon Paris >jon.f.pa...@gmail.com If you go over sys.maxsize, Python will just get you a long. The idea is to do away with the distinction between long and int. see: https://www.python.org/dev/peps/pep-0237/ Laura ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Can someone explain this to me please
On 21/08/15 19:04, Jon Paris wrote: Max size is: 9223372036854775807 y is with a value of 9223372036854775808 I was expecting it to error out The documentation says: --- sys.maxsize An integer giving the maximum value a variable of type Py_ssize_t can take. It’s usually 2**31 - 1 on a 32-bit platform and 2**63 - 1 on a 64-bit platform. --- So from your output we can deduce that Python ints (in version 3) are not stored in a Py_ssize_t variable. (In Python 2 they are so what you expect is almost what you would get - except Python converts int to long int automatically.) Python 3 ints are long ints, which the documentation says means they have "infinite precision" In practice their max size depends on how much memory you have! There are others here who can give you the inner details of how it is all done, but for most programmers that's all you normally need to know. Where you can run into issues is where you use modules that, in turn, use the underlying C libraries to do calculations. Passing a long int to them can result in errors. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Complications Take Two (Long) Frustrations.
Joseph Gulizia schreef op 2015-08-20 05:12: Assume that the grader defines two variables A and B for you. Write a program which prints out the value min(A, B) However, there is a catch: your program is not allowed to use the min function. Instead, use max in a clever way to simulate min. -- Code that gave best results but didn't work for negative numbers... -- Original = abs(max (-A, -B)) print (Original) -- Did not pass tests. You change the sign of A and B before passing them to max(). To revert that afterwards, you just need to change the sign of the result again. abs() indeed doesn't work for that: it only changes the sign on negative numbers, not on positive numbers. Regards, Roel -- The saddest aspect of life right now is that science gathers knowledge faster than society gathers wisdom. -- Isaac Asimov Roel Schroeven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Complications Take Two (Long) Frustrations.
On Fri, Aug 21, 2015 at 4:58 PM, Roel Schroeven wrote: > Joseph Gulizia schreef op 2015-08-20 05:12: >> >> Assume that the grader defines two variables A and B for you. Write a >> program which prints out the value >> min(A, B) >> >> However, there is a catch: your program is not allowed to use the min >> function. Instead, use max in a clever way to simulate min. >> -- >> Code that gave best results but didn't work for negative numbers... >> -- >> >> Original = abs(max (-A, -B)) >> print (Original) >> >> -- >> Did not pass tests. > > > You change the sign of A and B before passing them to max(). To revert that > afterwards, you just need to change the sign of the result again. > > abs() indeed doesn't work for that: it only changes the sign on negative > numbers, not on positive numbers. > > > Regards, > Roel > > -- > The saddest aspect of life right now is that science gathers knowledge > faster than society gathers wisdom. > -- Isaac Asimov > > Roel Schroeven > so: print -max(-A, -B) > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor -- Joel Goldstick http://joelgoldstick.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Complications Take Two (Long) Frustrations.
Joel Goldstick schreef op 2015-08-21 23:22: so: print -max(-A, -B) That's what I mean, yes. I haven't tried it, but I don't see why it wouldn't work. Regards, Roel -- The saddest aspect of life right now is that science gathers knowledge faster than society gathers wisdom. -- Isaac Asimov Roel Schroeven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor