Re: [Tutor] Request for help with os.walk() combining os.path.ismount() in Linux
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() 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. -- 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] problem with code
On 20/08/15 10:50, Nathan Clark 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) When you are programming you have to follow the rules of the language very carefully. Its not like English, say, where you have a lot of flexibility in the order that you say things. Also you have to get the punctuation (the syntax) exactly right, any missing commas, or brackets, or quotes or, in your case, colons will stop your code working. Taking your last two lines, you need to spell them like this: if time=2: print(that seems reasonable) else: print (get a life) The if must come first. There must be a colon after the if expression and the thing you want executed. There must also be a colon after the else and the thing you want executed. There is an alternative way of writing what you want but it's not very commonly used: print(that seems reasonable if time =2 else get a life) Notice that the if/else are all inside the print's parentheses. The expression basically passes a single string to print depending on the test result. You could expand it like this instead: message = that seems reasonable if time =2 else get a life print(message) HTH -- 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] iteration help
On 20/08/15 14:27, richard kappler wrote: While for and if loops always seem to give me trouble. A picky point, but it is conceptually very important. 'while' and 'for' are loops - because the loop back and repeat code. 'if' is not a loop. It is a selector. It only executes its code once but selects one of several options. Thee are basically only three(*) concepts in programming so far as code structure goes: 1) sequence - one instruction after another 2) repetition - code that repeats or loops 3) selection - code that chooses to go down one of many possible paths. With those three structures you can write any program. So it is very important that you keep those concepts separate in your mind. They do very different things. As to your specific issue I see Joel has given you some pointers there. (*) Some people like to include a fourth: modularity. The ability to create reusable code blocks such as functions. But technically it is not needed, its just a nice to have. -- 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] Request for help with os.walk() combining os.path.ismount() in Linux
Laura Creighton wrote: In a message of Thu, 20 Aug 2015 09:25:29 +0100, Alan Gauld writes: aside It always amazes me how often the same issues come up in rapid succession. We must have had 4 or 5 of these types of errors in the last couple of months, and other times we go for 6 months or more without it being raised! :-) /aside There are only 17 people in the world. We simulate the rest with mirrors. :) You have that backwards: there are only 17 mirrors in the world; we simulate the rest with people. ;) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Test discovery not locating module to test
boB Stepp wrote: W7 64-bit. Py 3.4.3 unittest result: E:\Projects\mcmpython -m unittest E == ERROR: test.db.test_mcm_db_mgr (unittest.loader.ModuleImportFailure) -- Traceback (most recent call last): File C:\Python34\lib\unittest\case.py, line 58, in testPartExecutor yield File C:\Python34\lib\unittest\case.py, line 577, in run testMethod() File C:\Python34\lib\unittest\loader.py, line 32, in testFailure raise exception ImportError: Failed to import test module: test.db.test_mcm_db_mgr Traceback (most recent call last): File C:\Python34\lib\unittest\loader.py, line 312, in _find_tests module = self._get_module_from_name(name) File C:\Python34\lib\unittest\loader.py, line 290, in _get_module_from_name __import__(name) File E:\Projects\mcm\test\db\test_mcm_db_mgr.py, line 22, in module import mcm_db_mgr ImportError: No module named 'mcm_db_mgr' -- Ran 1 test in 0.000s FAILED (errors=1) Relevant code in test_mcm_db_mgr.py: import unittest # import modules to be tested: import mcm_db_mgr class MCMDBMgrTestCase(unittest.TestCase): def setUp(self): # Insert setup code here... pass def test_open_mcm_db(self): pass def tearDown(self): # Insert tear-down code here... pass I suspect that there is something wrong with my project structure. Currently it is as follows: Projects/ --mcm/ .git/ doc/ src/ --db/ __init__.py mcm_db_mgr.py --ui/ __init__.py test/ --db/ __init__.py test_mcm_db_mgr.py --ui/ __init__.py .gitignore LICENSE.txt README.txt All __init__.py files are currently empty. Alex had asked a question very similar to this situation, and I thought I had understood the answer Laura had given, but apparently I do not understand. Where am I going wrong this time? Assuming E:/Projects/mcm/src is in the PYTHONPATH mcm_db_mgr.py (what an alphabet soup!) is part of your db package and has to be imported with import db.mcm_db_mgr or from db import mcm_db_mgr by modules outside the db package. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Request for help with os.walk() combining os.path.ismount() in Linux
On 20/08/15 04:03, Srihari Vijayaraghavan wrote: out = sys.stdout.write for root, dirs, files in os.walk(/): out(The dirs before removing mount points: %s\n % dirs) for d in dirs: dname = os.path.join(root, d) if os.path.ismount(dname): dirs.remove(d) It's never a good idea to remove items from the thing you are iterating over. Create a copy of dirs (dirs[:]) to iterate on then remove the items from the original dirs. aside It always amazes me how often the same issues come up in rapid succession. We must have had 4 or 5 of these types of errors in the last couple of months, and other times we go for 6 months or more without it being raised! :-) /aside -- 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
[Tutor] Request for help with os.walk() combining os.path.ismount() in Linux
Hello Folks, Please consider the following 2 scripts: 1. os_path_ismount.py: import os import sys out = sys.stdout.write out(%s\n % os.path.ismount(sys.argv[1])) 2. os_walk.py: import sys import os out = sys.stdout.write for root, dirs, files in os.walk(/): out(The dirs before removing mount points: %s\n % dirs) for d in dirs: dname = os.path.join(root, d) if os.path.ismount(dname): dirs.remove(d) out(The dirs after removing mount points: %s\n % dirs) sys.exit() Am at a loss to understand this quirky behaviour on Fedora 22 (x86-64; both stock-standard python 2.7.10 3.4.2): [srihari@laptop ~]$ python2 ./os_path_ismount.py /proc True [srihari@laptop ~]$ python3 ./os_path_ismount.py /proc True [srihari@laptop ~]$ python2 ./os_walk.py The dirs before removing mount points: ['run', 'dev', 'srv', 'fedora', 'root', 'bin', 'lib', 'opt', 'lost+found', 'etc', 'sbin', 'var', 'sys', 'media', 'backup', 'home', 'usr', 'tmp', 'proc', 'mnt', 'boot', 'lib64'] The dirs after removing mount points: ['dev', 'srv', 'fedora', 'root', 'bin', 'lib', 'opt', 'lost+found', 'etc', 'sbin', 'var', 'media', 'backup', 'usr', 'proc', 'mnt', 'lib64'] [srihari@laptop ~]$ python3 ./os_walk.py The dirs before removing mount points: ['run', 'dev', 'srv', 'fedora', 'root', 'bin', 'lib', 'opt', 'lost+found', 'etc', 'sbin', 'var', 'sys', 'media', 'backup', 'home', 'usr', 'tmp', 'proc', 'mnt', 'boot', 'lib64'] The dirs after removing mount points: ['dev', 'srv', 'fedora', 'root', 'bin', 'lib', 'opt', 'lost+found', 'etc', 'sbin', 'var', 'media', 'backup', 'usr', 'proc', 'mnt', 'lib64'] Undoubtedly proc (to be precise /proc) (same for /dev as well) is indeed a mount point, yet it's somehow not being evicted. So am I doing something silly? Could somebody please explain what am I doing wrong here? However, /run /sys are being evicted, which is good, of course. (Interestingly, I get different results on different platforms: CentOS6.7 gives some results, CentOS7.1 something else, i.e., am totally baffled!) Thank you. Srihari Vijayaraghavan PS: Of course, I could add an exclusion list to take out the known mount points, but that won't be elegant I reckon. Certainly, it won't be for my real usage scenario. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Request for help with os.walk() combining os.path.ismount() in Linux
In a message of Thu, 20 Aug 2015 09:25:29 +0100, Alan Gauld writes: aside It always amazes me how often the same issues come up in rapid succession. We must have had 4 or 5 of these types of errors in the last couple of months, and other times we go for 6 months or more without it being raised! :-) /aside There are only 17 people in the world. We simulate the rest with mirrors. :) Laura ___ 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 Thu, Aug 20, 2015 at 10:13 PM, Steven D'Aprano st...@pearwood.info 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\mcmpy -m unittest ./mcm/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 module 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 listcomp 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 Now what? Scratching my sleepy head... boB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Do not understand why test is running.
I adopted Peter's suggestions (I think!) and now have these two paths that are pertinent to my questions: Projects/mcm/mcm/db/manager.py # The module I am beginning to write tests for. Projects/mcm/test/db/test_manager.py# The file for my module tests. The test code currently is: 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() Out of curiosity, I changed the last two lines to comments, as I am still feeling my way around this package structure and how things work. I was surprised when I ran my test now: E:\Projects\mcmpy -m unittest discover -v test_open_db (test.db.test_manager.ManagerTestCase) ... ok -- Ran 1 test in 0.000s OK Obviously I was not expecting this! Why did the test run? I thought it would not happen without those final two lines. -- boB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] problem with code
time = input(How long on average do you spend on the computer per day?) if time = 2: print Message1 else: print Message2 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
Re: [Tutor] iteration help
On 20/08/2015 14:27, richard kappler wrote: Running python 2.7 on Linux While for and if loops always seem to give me trouble. They seem obvious but I often don't get the result I expect and I struggle to figure out why. Appended below is a partial script. Ultimately, this script will read a log, parse out two times from each line of the log, a time the line was written to the lg (called serverTime in the script) and an action time from elsewhere in the line, then get the difference between the two. I don't want every difference, but rather the average per hour, so I have a line count. The script will output the average time difference for each hour. I've got most of the pieces working in test scripts, but I'm stymied with the single output bit. How do you write an if loop? The idea is that the script takes the hour from the server time of the first line of the log and sets that as the initial serverHr. That works, has been tested. Next the script is supposed to iterate through each line of the log (for line in f1) and then check that there is a time in the line (try), and if not skip to the next line. That works, has been tested. As each line is iterated over, my intent was that the variable newServerHr (read from the current line) is compared to serverHr and if they are the same, the script will increase the count by one and add the difference to a cummulative total then go to the next line. If the newServerHr and serverHr are not the same, then we have entered a new clock hour, and the script should calculate averages and output those, zero all counts and cummulative totals, then carry on. The idea being that out of 117,000 ish lines of log (the test file) that have inputs from 0200 to 0700, I would get 6 lines of output. I've got everything working properly in a different script except I get 25 lines of output instead of 6, writing something like 16 different hours instead of 02 - 07. In trying to chase down my bug, I wrote the appended script, but it outputs 117,000 ish lines (times 02-07, so that bit is better), not 6. Can someone tell me what I'm misunderstanding? #!/usr/bin/env python import re f1 = open('ATLA_PS4_red5.log', 'r') f2 = open('recurseOut.log', 'a') # read server time of first line to get hour first_line = f1.readline() q = re.search(r'\d\d:\d\d:\d\d', first_line) q2 = q.start() serverHr = (first_line[q2:q2+2]) Are you absolutely certain that this will always be set correctly? for line in f1: try: s = line The line above does nothing effective so remove it. #read server time a = re.search(r'\d\d:\d\d:\d\d', s) # find server time in line b = a.start()# find 1st position of srvTime newServerHr = (s[b:b+2]) # what hour is it now? if newServerHr != serverHr: f2.write('hour ' + newServerHr + '\n') else: serverHr == newServerHr Is it possible that lines don't contain a valid time in which case b will be -1? Your slice will run from -1, the last character in the line, to +1, i.e. an empty string . except: pass Remove the try and plain except as it'll mask any problems that you get in the code. It also prevents CTRL-C or similar from breaking infinite loops that you've accidentally written. -- 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
[Tutor] problem with code (Alan Gauld)
Hello, I am trying to make Hangman. But main part of the game i.e. comparing user input with secret word is not working properly. Can you have a look where am I going wrong? Thank you Attachment: Python code file. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Request for help with os.walk() combining os.path.ismount() in Linux
On 20 August 2015 at 18:51, Srihari Vijayaraghavan linux.bug.report...@gmail.com wrote: On 20 August 2015 at 18:25, Alan Gauld alan.ga...@btinternet.com wrote: On 20/08/15 04:03, Srihari Vijayaraghavan wrote: out = sys.stdout.write for root, dirs, files in os.walk(/): out(The dirs before removing mount points: %s\n % dirs) for d in dirs: dname = os.path.join(root, d) if os.path.ismount(dname): dirs.remove(d) It's never a good idea to remove items from the thing you are iterating over. Create a copy of dirs (dirs[:]) to iterate on then remove the items from the original dirs. 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)... (Yes, the topdown argument of os.walk() is True by default, until manually modified.) Therefore I see no problem with in-place modification of dirnames. I've made a copy of dirs iterated over it, which made no difference in my case. Sorry to reply to my own email. I stand corrected. Indeed, while iterating over dirs (in the above example) doing in-place modification was the source of the problem, giving unpredictable outcome. While iterating over its copy updating the original dirs, gives expected results. Perhaps my interpretation of the document wasn't correct. Anyway, it's working now. Thank you folks, especially Alan. -- Srihari Vijayaraghavan ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Test discovery not locating module to test
boB Stepp wrote: On Thu, Aug 20, 2015 at 2:37 AM, Peter Otten __pete...@web.de wrote: Assuming E:/Projects/mcm/src is in the PYTHONPATH mcm_db_mgr.py (what an alphabet soup!)... Written out in full, this would be montessori_classroom_manager_database_manager.py Hmm. Perhaps just db_manager.py? How about manager.py? If you increase the nesting level by introducing an mcm toplevel package you can access the module formerly known as db.mcm_db_mgr with import mcm.db.manager or from mcm.db import manager and avoid the mixture of logical _ and physical . separators. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] problem with code
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
Re: [Tutor] Request for help with os.walk() combining os.path.ismount() in Linux
On 20 August 2015 at 18:25, Alan Gauld alan.ga...@btinternet.com wrote: On 20/08/15 04:03, Srihari Vijayaraghavan wrote: out = sys.stdout.write for root, dirs, files in os.walk(/): out(The dirs before removing mount points: %s\n % dirs) for d in dirs: dname = os.path.join(root, d) if os.path.ismount(dname): dirs.remove(d) It's never a good idea to remove items from the thing you are iterating over. Create a copy of dirs (dirs[:]) to iterate on then remove the items from the original dirs. 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)... (Yes, the topdown argument of os.walk() is True by default, until manually modified.) Therefore I see no problem with in-place modification of dirnames. I've made a copy of dirs iterated over it, which made no difference in my case. My issue is that some of the virtual or in memory file systems' mount points (like /proc, /dev) are failing to be recognised properly under os.path.ismount(). Perhaps this quirkiness is irresolvable?? Thank you. 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
On 8/20/2015 2:50 AM, Nathan Clark 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) Check your parens -- they're mismatched. Emile ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] iteration help
On Thu, Aug 20, 2015 at 9:27 AM, richard kappler richkapp...@gmail.com wrote: Running python 2.7 on Linux While for and if loops always seem to give me trouble. They seem obvious but I often don't get the result I expect and I struggle to figure out why. Appended below is a partial script. Ultimately, this script will read a log, parse out two times from each line of the log, a time the line was written to the lg (called serverTime in the script) and an action time from elsewhere in the line, then get the difference between the two. I don't want every difference, but rather the average per hour, so I have a line count. The script will output the average time difference for each hour. I've got most of the pieces working in test scripts, but I'm stymied with the single output bit. The idea is that the script takes the hour from the server time of the first line of the log and sets that as the initial serverHr. That works, has been tested. Next the script is supposed to iterate through each line of the log (for line in f1) and then check that there is a time in the line (try), and if not skip to the next line. That works, has been tested. As each line is iterated over, my intent was that the variable newServerHr (read from the current line) is compared to serverHr and if they are the same, the script will increase the count by one and add the difference to a cummulative total then go to the next line. If the newServerHr and serverHr are not the same, then we have entered a new clock hour, and the script should calculate averages and output those, zero all counts and cummulative totals, then carry on. The idea being that out of 117,000 ish lines of log (the test file) that have inputs from 0200 to 0700, I would get 6 lines of output. I've got everything working properly in a different script except I get 25 lines of output instead of 6, writing something like 16 different hours instead of 02 - 07. In trying to chase down my bug, I wrote the appended script, but it outputs 117,000 ish lines (times 02-07, so that bit is better), not 6. Can someone tell me what I'm misunderstanding? #!/usr/bin/env python import re f1 = open('ATLA_PS4_red5.log', 'r') f2 = open('recurseOut.log', 'a') # read server time of first line to get hour first_line = f1.readline() q = re.search(r'\d\d:\d\d:\d\d', first_line) q2 = q.start() serverHr = (first_line[q2:q2+2]) for line in f1: try: s = line #read server time a = re.search(r'\d\d:\d\d:\d\d', s) # find server time in line b = a.start()# find 1st position of srvTime newServerHr = (s[b:b+2]) # what hour is it now? if newServerHr != serverHr: f2.write('hour ' + newServerHr + '\n') else: serverHr == newServerHr except: pass 1. You don't need s, you can use line directly. 2. In your else: code, you want = not == since you want to assign the new value to the serverHr. That line does nothing now since it is comparing two values, but making no decision based on the comparison. 3. I'm guessing you are coming from another language. In python people generally use lower case names with underscores between words. -- 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] Test discovery not locating module to test
On Thu, Aug 20, 2015 at 2:37 AM, Peter Otten __pete...@web.de wrote: Assuming E:/Projects/mcm/src is in the PYTHONPATH mcm_db_mgr.py (what an alphabet soup!)... Written out in full, this would be montessori_classroom_manager_database_manager.py Hmm. Perhaps just db_manager.py? ...is part of your db package and has to be imported with import db.mcm_db_mgr or from db import mcm_db_mgr by modules outside the db package. I'm bad! So clear in the morning, so obscure for me just before bed. In my defense, I have intellectually *known* about this, but have not used the standard library much to this point, and this is the first time I have tried this *package* structure of a project, so that aspect is all new to me. Thanks, Peter! boB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] iteration help
Running python 2.7 on Linux While for and if loops always seem to give me trouble. They seem obvious but I often don't get the result I expect and I struggle to figure out why. Appended below is a partial script. Ultimately, this script will read a log, parse out two times from each line of the log, a time the line was written to the lg (called serverTime in the script) and an action time from elsewhere in the line, then get the difference between the two. I don't want every difference, but rather the average per hour, so I have a line count. The script will output the average time difference for each hour. I've got most of the pieces working in test scripts, but I'm stymied with the single output bit. The idea is that the script takes the hour from the server time of the first line of the log and sets that as the initial serverHr. That works, has been tested. Next the script is supposed to iterate through each line of the log (for line in f1) and then check that there is a time in the line (try), and if not skip to the next line. That works, has been tested. As each line is iterated over, my intent was that the variable newServerHr (read from the current line) is compared to serverHr and if they are the same, the script will increase the count by one and add the difference to a cummulative total then go to the next line. If the newServerHr and serverHr are not the same, then we have entered a new clock hour, and the script should calculate averages and output those, zero all counts and cummulative totals, then carry on. The idea being that out of 117,000 ish lines of log (the test file) that have inputs from 0200 to 0700, I would get 6 lines of output. I've got everything working properly in a different script except I get 25 lines of output instead of 6, writing something like 16 different hours instead of 02 - 07. In trying to chase down my bug, I wrote the appended script, but it outputs 117,000 ish lines (times 02-07, so that bit is better), not 6. Can someone tell me what I'm misunderstanding? #!/usr/bin/env python import re f1 = open('ATLA_PS4_red5.log', 'r') f2 = open('recurseOut.log', 'a') # read server time of first line to get hour first_line = f1.readline() q = re.search(r'\d\d:\d\d:\d\d', first_line) q2 = q.start() serverHr = (first_line[q2:q2+2]) for line in f1: try: s = line #read server time a = re.search(r'\d\d:\d\d:\d\d', s) # find server time in line b = a.start()# find 1st position of srvTime newServerHr = (s[b:b+2]) # what hour is it now? if newServerHr != serverHr: f2.write('hour ' + newServerHr + '\n') else: serverHr == newServerHr except: pass -- All internal models of the world are approximate. ~ Sebastian Thrun ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] About using list in a function
On 19/08/2015 18:25, Alan Gauld wrote: On 19/08/15 17:09, Michelle Meiduo Wu wrote: Hi there, I'm trying to use List in a function. But it doesn't work. Here are sample code not work: ---def getResult():ls = []ls= ls.append(100)ls= ls.append(200) return ls reList = []reList = getResult()lsLength = len(reList)print '\n The length of the list is:' + str(lsLength)-I ran the above code, there is an error message: AttributeError: 'NoneType' object has no attribute 'append' But the code below not using list in a function works.--### This works:ls = []ls.append(100)ls.append(200)lsLength = len(ls)print '\n list length is: ' + str(lsLength)- Do you know the reason? Thank you,Michelle As you can (hopefully!) see above, this message is completely scrambled. Normally that means HTML. But the headers suggest it is plain text. Also, I see that Steve replied with a correctly formatted inclusion. Did anyone else get the scrambled version? And does anyone have any clues why I did? (Using Thunderbird v31.8 and normally not having major issues.) Scrambled using Thunderbird 38.2.0 on Windows 10. -- 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 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. But that's not the only way to run unit tests. Another way to run unit tests is to tell the unittest module to run whatever tests it discovers inside a module or package. Which is what you have done: Out of curiosity, I changed the last two lines to comments, as I am still feeling my way around this package structure and how things work. I was surprised when I ran my test now: E:\Projects\mcmpy -m unittest discover -v test_open_db (test.db.test_manager.ManagerTestCase) ... ok -- Ran 1 test in 0.000s OK Obviously I was not expecting this! Why did the test run? I thought it would not happen without those final two lines. No, although they both involve unittest, the method of invoking unittest are different. What you actually did was: Hey unittest, see what tests you can discover, and run those tests. instead of: Hey Python, run this script. (script says) Hey unittest, run the tests you find inside me. -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] problem with code (Alan Gauld)
On Thu, Aug 20, 2015 at 2:12 PM, Freddy thobhani.fre...@gmail.com wrote: Hello, I am trying to make Hangman. But main part of the game i.e. comparing user input with secret word is not working properly. Can you have a look where am I going wrong? Thank you Attachment: Python code file. No attachments allowed here. Paste your code in your message 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] problem with code
On 20/08/15 18:07, Alan Gauld wrote: (print(that seems reasonable)) if time=2 else print (get a life) There is an alternative way of writing what you want but it's not very commonly used: print(that seems reasonable if time =2 else get a life) I just realized that the OP's version would actually work in Python v3 because print is a function. It effectively evaluates as None if time = 2 else None But can I suggest that it's not a good style to get into. Either of the options that i suggested in my last mail would be more idiomatic in the Python community. -- 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