Re: [Tutor] Topic focus of ‘python-tutor’ (was: mySQL and Python)
Long-ish reply, but please bear with me. To quote the list description "This list is for folks who want to ask questions regarding how to learn computer programming with the Python language and its standard library." While MySQL modules are not part of the standard library, consider the following scenario. A person, let's call him Xerxes, is interesting in learning to program in Python. So he does a whole lot of reading, asks a load of questions on this list, he learns the basics. He now thinks to himself, wow, it would be really cool if I could actually do something really useful with my new skill set, maybe he has a job where building an interface to a database would save him and his company a lot of effort, but as MySQL is not part of the standard library he's not really sure how to go about it. Since people on the tutor list have always been willing to help and offer assistance, he thinks, I'll ask there. He asks his question and almost immediately gets told that what he's asking is off topic for the list. Let's jump back to the list description for a minute there. "This list is for folks who want to *ask questions regarding how to learn computer programming with the Python language* and its standard library." Is the installation and usage of 3rd party modules not related to learning to program with python? By responding in that manner, you're discouraging the asking of questions. No one should *_EVER_ *be discouraged to ask a question they do not know the answer to. That response that I referred to in my initial response is very common on this list and I think we (collectively) could and should come up with a better way of pointing people in the direction of satisfactory resolution. I also don't think we should immediately point people else where when the question is outside our comfort zone. OO, inheritance and multi-byte string processing, to name a few, are part of learning to program but are easily more advanced topics than which MySQL module to use. Creating C data structures in python, while part of the standard library, is an example of a topic that is probably too advanced for this list, and even then, I managed to find someone on this list able to help with that topic. If we discourage people from asking more interesting questions then I suspect that many subscribers will stop subscribing and that knowledge will be lost. I subscribe to quite a few programming related mailing lists and let me tell you, this one is _BY_FAR_ the most willing to help. That question would very likely have been ignored on other "more advanced" lists. Finally, I realise I've offered some criticism but not a suggestion for an alternative, so here is an example of a response to a question that would be considered "outside the scope" of this list: Hi Xerxes Great question. I know there are many options when it comes to MySQL but I've not had any experience with any. Someone else on this list might be able to provide a satisfactory answer, but, as the main focus of this list is learning to program in python using the standard library you might not get an answer, and if you do, the answer might not be very knowledgeable. You could try asking your question on forum X or mailing list Y as these types of topics are often covered there. James ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] How to do a PyQt VTK application in Python26
Is it possible to make a PyQt VTK application using Python26? My first sample Qt VTK application is not running!! And my VTK compiled Bin folder is python 26 compiled dll files and Win32 bit The error is Traceback (most recent call last): File "EmbedInPyQt.py", line 5, in from PyQt4 import QtCore, QtGui ImportError: DLL load failed: %1 is not a valid Win32 application. Due to this bug I searched in internet for PyQt4 Installer downloading versions and those are supporting Python34 and higher as PyQt4 exe (http://www.riverbankcomputing.com/software/pyqt/download) Is there any way can I run my sample QT-VTK application using python 26? OR should I go after python 34?? could anyone please help me soon? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Topic focus of ‘python-tutor’
On 19/02/2015 09:51, James Chapman wrote: Long-ish reply, but please bear with me. To quote the list description "This list is for folks who want to ask questions regarding how to learn computer programming with the Python language and its standard library." While MySQL modules are not part of the standard library, consider the following scenario. A person, let's call him Xerxes, is interesting in learning to program in Python. So he does a whole lot of reading, asks a load of questions on this list, he learns the basics. He now thinks to himself, wow, it would be really cool if I could actually do something really useful with my new skill set, maybe he has a job where building an interface to a database would save him and his company a lot of effort, but as MySQL is not part of the standard library he's not really sure how to go about it. Since people on the tutor list have always been willing to help and offer assistance, he thinks, I'll ask there. He asks his question and almost immediately gets told that what he's asking is off topic for the list. Let's jump back to the list description for a minute there. "This list is for folks who want to *ask questions regarding how to learn computer programming with the Python language* and its standard library." Is the installation and usage of 3rd party modules not related to learning to program with python? By responding in that manner, you're discouraging the asking of questions. No one should *_EVER_ *be discouraged to ask a question they do not know the answer to. That response that I referred to in my initial response is very common on this list and I think we (collectively) could and should come up with a better way of pointing people in the direction of satisfactory resolution. I also don't think we should immediately point people else where when the question is outside our comfort zone. OO, inheritance and multi-byte string processing, to name a few, are part of learning to program but are easily more advanced topics than which MySQL module to use. Creating C data structures in python, while part of the standard library, is an example of a topic that is probably too advanced for this list, and even then, I managed to find someone on this list able to help with that topic. If we discourage people from asking more interesting questions then I suspect that many subscribers will stop subscribing and that knowledge will be lost. I subscribe to quite a few programming related mailing lists and let me tell you, this one is _BY_FAR_ the most willing to help. That question would very likely have been ignored on other "more advanced" lists. Finally, I realise I've offered some criticism but not a suggestion for an alternative, so here is an example of a response to a question that would be considered "outside the scope" of this list: Hi Xerxes Great question. I know there are many options when it comes to MySQL but I've not had any experience with any. Someone else on this list might be able to provide a satisfactory answer, but, as the main focus of this list is learning to program in python using the standard library you might not get an answer, and if you do, the answer might not be very knowledgeable. You could try asking your question on forum X or mailing list Y as these types of topics are often covered there. James ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor IMHO you're talking crap, now can we please move on? -- 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] Topic focus of ‘python-tutor’
On 19/02/15 09:51, James Chapman wrote: While MySQL modules are not part of the standard library, consider the following scenario. skill set, maybe he has a job where building an interface to a database would save him and his company a lot of effort, but as MySQL is not part of the standard library he's not really sure how to go about it. If it were a new database he could use Sqlite which is part of the standard library... But that's not really the point :-) on the tutor list have always been willing to help and offer assistance, he thinks, I'll ask there. He asks his question and almost immediately gets told that what he's asking is off topic for the list. Which is exactly the right answer. as a beginner he needs to learn to find the most appropriate forum and ask there. Its part of learning to program. It should be done politely and ideally we should tell him what that other forum is. But any programmer is going to need to learn how to research and locate appropriate information resources. It's just as important as learning a language, and in that sense it's part of this list's remit. One of the things that gives me greatest satisfaction is when I see someone who started out on the tutor list migrate to the main python list and eventually stop participating on this one. It means they have progressed as a programmer. Ideally everyone who starts using this list should, within a couple of years, have stopped asking questions here and moved on to another forum (or become one of the participating 'tutors' perhaps). the list description for a minute there. "This list is for folks who want to *ask questions regarding how to learn computer programming with the Python language* and its standard library." Is the installation and usage of 3rd party modules not related to learning to program with python? Not really. You can learn to program with Python effectively without ever using a third party package. In fact in my 20 years of Python I've only really used 4 third party packages - PyWin32, wxPython, BeautifulSoup and Pil/Pillow. Of these I only really use PyWin32 regularly. But it depends on what kind of programming you do. By responding in that manner, you're discouraging the asking of questions. No, you are encouraging the asking of appropriate questions, and the searching for appropriate groups. A new and important skill for the programmer. No one should *_EVER_ *be discouraged to ask a question they do not know the answer to. That's true, but the answer may well be, "this is the wrong place to get the answer" very common on this list and I think we (collectively) could and should come up with a better way of pointing people in the direction of satisfactory resolution. If the phrasing can be improved then lets have a go. Pointing them to a more effective solution should be the aim. people else where when the question is outside our comfort zone. OO, inheritance and multi-byte string processing, to name a few, are part of learning to program but are easily more advanced topics than which MySQL module to use. But they are appropriate to the tutor list because they are about learning to program. Specific database modules are not. Generic SQL or specific SQLite queries are appropriate because SQL is general programming and SQLite is in the standard library. However, if someone wants to delve into deep detail of how a feature is implemented in Python we would probably point them to the main list too, since it's not about learning how to program at that point. Creating C data structures in python, while part of the standard library, is an example of a topic that is probably too advanced for this list, and even then, I managed to find someone on this list able to help with that topic. Yes, that is probably borderline, since it is arguably part of using ctypes. And its true that we do have several people who can answer wider questions, depending on the question. But it is still better to ask a pool of 100 experts than ask a pool of perhaps 10. And in particular asking a beginner group about any topic can be risky since you often get answers which might work but are not the best way to do it. (This is a big issue on forums like stack overflow where you often see horrible hacks presented when a much better solution is available.) If we discourage people from asking more interesting questions then I suspect that many subscribers will stop subscribing and that knowledge will be lost. The knowledge will be on the forums that specialise in those topics. That's where it should be. As I said above this list is not in the business of acquiring lifetime members, we want people to outgrow it and move on. It's a good sign. A bit like children leaving home. I subscribe to quite a few programming related mailing lists and let me tell you, this one is _BY_FAR_ the most willing to help. That question would very likely have been ignored on other "more advanc
Re: [Tutor] unittest for: Raises an exception
On 18/02/2015 20:40, Ben Finney wrote: Sydney Shall writes: My test code is the following: def test_func_getSurplusLabourTime_Exc(self): self.assertRaises(ValueError,self.cwp.getSurplusLabourTime(self.cwp.ww,self.cwp.uvc)) [This last line should indented, but it refuses to do so!] What is “it” which refuses to indent your text? You might need to use a better message composition tool. If you're typing into a Web application, please see the discussion happening in this forum about appropriate email clients for posting program code. So I'll reformat that code for readability:: def test_func_getSurplusLabourTime_Exc(self): self.assertRaises( ValueError, self.cwp.getSurplusLabourTime(self.cwp.ww, self.cwp.uvc)) The traceback is as follows: == ERROR: test_func_getSurplusLabourTime_Exc (__main__.Testcwp) -- Traceback (most recent call last): […] "/Applications/Canopy.app/appdata/canopy-1.5.1.2730.macosx-x86_64/Canopy.app/Contents/lib/python2.7/unittest/case.py", line 475, in assertRaises callableObj(*args, **kwargs) TypeError: 'float' object is not callable The error message is correct: the ‘assertRaises’ method expects a callable object in the second parameter, but you've supplied an object which is not callable (a float). Have a closer look at the documentation for ‘TestCase.assertRaises’:: assertRaises(exception, callable, *args, **kwds) Test that an exception is raised when callable is called with any positional or keyword arguments that are also passed to assertRaises(). https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertRaises> It's unfortunate the documentation doesn't give an example. But note that clause “when `callable` is called with any […] arguments that *are also passed to assertRaises*”. So you don't call the function yourself; if you do, you get back its return value (in your case, a float object) and *that's* your argument to ‘assertRaises’ — not the function you're trying to test! Instead of calling the function and getting its return value, you pass *the function itself* to ‘assertRaises’, along with any arguments you want *the test method* to call it with. This indirection is a little confusing, and again I'm unhappy the documentation doesn't show an example. Here's one:: def test_func_getSurplusLabourTime_Exc(self): self.assertRaises( ValueError, self.cwp.getSurplusLabourTime, self.cwp.ww, self.cwp.uvc) What the documentation does show as an example, though, is a new-ish feature that might suit you better. You can now make your test code more comprehensible by instead using the ‘assertRaises’ method as a context manager, and then you just call your function normally. Like this:: def test_func_getSurplusLabourTime_Exc(self): with self.assertRaises(ValueError): self.cwp.getSurplusLabourTime(self.cwp.ww, self.cwp.uvc) Context managers are a very helpful feature that can make code more elegant and readable. They might seem a little magic for now if you haven't learned about them yet, but this is a good demonstration of the improvement they can make. Raul and Ben, Many thanks for the advice. The use of this test is much clearer to me now, and I think it is now working with your help. But I will practice my use of it. As for my formatting, this problem of indentation has not occurred before. I use Thunderbird and Firefox and I have not had this problem up to now. I shall examine the problem and see what is wrong and correct it. Thanks, Sydney -- Sydney ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to do a PyQt VTK application in Python26
On 19/02/15 09:54, John R wrote: Is it possible to make a PyQt VTK application using Python26? No idea. What is a PyQt VTK application? I know what PyQt is but never heard of VTK Is there a web site? Does it have a support area? Perhaps they can help? The error is Traceback (most recent call last): File "EmbedInPyQt.py", line 5, in from PyQt4 import QtCore, QtGui ImportError: DLL load failed: %1 is not a valid Win32 application. It sounds like you may have a dependency issue and need to install something. But since its not Python or its standard library I can't guess what. The best place to ask will be a PyQt and/or a VTK forum. Due to this bug I searched in internet for PyQt4 Installer downloading versions and those are supporting Python34 and higher as PyQt4 exe (http://www.riverbankcomputing.com/software/pyqt/download) Is there any way can I run my sample QT-VTK application using python 26? OR should I go after python 34?? Its unlikely that a version for 3.4 will work on 2.6. There are too many incompatible differences. Try using Python 3.4. -- 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] text file help
Forwarding to tutor, please use Reply All when responding to tutor emails. On 19/02/15 11:17, Tihomir Zjajic wrote: formular doznake.txt red_broj = 1 vrs_drv = 21 prs_prec = 20 teh_kl = 2 red_broj = 2 vrs_drv = 21 prs_prec = 40 teh_kl = 3 I assume red_broj indicates the start of a new record? So you can read three lines from the file after you find a red_broj entry? Something like this? (untested) kl_numbers = [] with open('doznake.txt') as doznake for line in doznake: if line.startswith('red_broj') num = makeNumber(next(doznake),next(doznake),next(doznake)) kl_numbers.append(num) def makeNumber(l1,l2,l3): nums = [] for line in (s1,s2,s3): nums.append(line.rstrip().split()[-1]) return ''.join(nums) > def makeNumber(l1, l2, l3): >nums = [] >for lines in (s1, s2, s3): >nums.append(line.rstrip().split() [-1]) >return ".join(nums) " > > kl_number = [] > open("formular_doznake.txt") You need to either assign the open result to a variable or use the 'with' style (as in my example above). > for line in "formular_doznake.txt": > line startswith("red_broj") startswith is a method of string so you need a dot between line and startswith > num = makeNumber(next("formular_doznake"), next("formular_doznake"), next("formular_doznake")) next takes an iterator as an argument not a file name. This needs to be the alias from the with statement or the variable you assign if using open. example: >>> myfile = open("formular_doznake.txt") >>> print(next(myfile)) >kl_number.append(num) > > lpthw> python answer2.py formular_doznake.txt > File "answer2.py" line 13 > kl_number.append(num) > SyntaxError: invalid sintax You should copy and paste the code and errors not re-type them. its easier and more reliable. BTW If using the open styule above you should close the file at the end: myfile.close() One of the advantages of the with... style is that it auto-closes the file for you. -- 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] Topic focus of ‘python-tutor’
On Feb 19, 2015 2:35 AM, Mark Lawrence wrote: > IMHO you're talking crap, now can we please move on Agree with second part, Disagree with first ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Topic focus of ‘python-tutor’ (was: mySQL and Python)
On Feb 19, 2015 1:51 AM, James Chapman wrote: > > .. this one is _BY_FAR_ the most willing to help. That question > Here, here! ak ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Topic focus of ‘python-tutor’
On 2/19/2015 1:51 AM, James Chapman wrote: No one should *_EVER_ *be discouraged to ask a question they do not know have not found the answer to. Learning where to look and how to ask are likely more important skills for a programmer than learning any one specific language. Emile ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to do a PyQt VTK application in Python26
Thanks Alan for suggesting Python 3.4, that seems to be correct but I am searching for any possibilities with python 2.6 Thank you guys for responding, I should try some other forum. On 19 Feb 2015 16:37, "Alan Gauld" wrote: > On 19/02/15 09:54, John R wrote: > >> Is it possible to make a PyQt VTK application using Python26? >> > > No idea. What is a PyQt VTK application? > I know what PyQt is but never heard of VTK > > Is there a web site? Does it have a support area? > Perhaps they can help? > > The error is >> >> Traceback (most recent call last): >>File "EmbedInPyQt.py", line 5, in >> from PyQt4 import QtCore, QtGui >> ImportError: DLL load failed: %1 is not a valid Win32 application. >> > > It sounds like you may have a dependency issue and need to > install something. But since its not Python or its standard > library I can't guess what. The best place to ask will be > a PyQt and/or a VTK forum. > > Due to this bug I searched in internet for PyQt4 Installer downloading >> versions and those are supporting >> Python34 and higher as PyQt4 exe >> (http://www.riverbankcomputing.com/software/pyqt/download) >> >> Is there any way can I run my sample QT-VTK application using python 26? >> OR should I go after python 34?? >> > > Its unlikely that a version for 3.4 will work on 2.6. There are too many > incompatible differences. Try using Python 3.4. > > > -- > 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 maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] (no subject)
Greetings !! Hi all, what the meaning of the line at the start of the python file __author__ = "user" thanks in advancerakesh ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] (no subject)
On 2/19/2015 9:23 AM, rakesh sharma wrote: Greetings !! Hi all, what the meaning of the line at the start of the python file __author__ = "user" Googling __author__ provides lots of relevant info. Emile ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] (no subject)
On Thu, Feb 19, 2015 at 9:23 AM, rakesh sharma wrote: > Greetings !! > Hi all, > what the meaning of the line at the start of the python file > __author__ = "user" Hi Rakesh, It's assigning a variable '__author__' with the value "user". Python libraries typically have some kind of "metadata" that talks about the library, and one way to express this metadata is to assign to certain variables like what you're seeing here. I can look at a particular module, and see if it has an '__author__': >>> import tarfile >>> tarfile.__author__ 'Lars Gust\xe4bel (l...@gustaebel.de)' >>> import io >>> io.__author__ "Guido van Rossum , Mike Verdone , Mark Russell , Antoine Pitrou , Amaury Forgeot d'Arc , Benjamin Peterson " That being said, it's not a requirement, so not all library modules have this. >>> import xml >>> xml.__author__ Traceback (most recent call last): File "", line 1, in AttributeError: 'module' object has no attribute '__author__' And as a beginner, you probably don't need to worry about assigning authorship to your own programs: you should know that you wrote them. :P When you start working with larger teams of people, it's helpful because you want to direct development questions to the folks who own those files. Authorship is a rough approximation to find the right person in charge. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Fwd: Re: Fwd: Need help with find error
> 2015-02-18 8:25 GMT+02:00 Danny Yoo : >> >> I do not have time to help you right now. Please continue correspondence >> with the main tutor list. On Wed, Feb 18, 2015 at 11:34 AM, Андрей Пугачев wrote: > ok, thanks, I will Following up: have you been helped? I was watching the mailing list, and it looks like you might have gotten skipped on accident. Unfortunately, I am still overloaded, and can not help much at the moment. I would recommend asking your question again. But please summarize what you've learned so far, what parts worked, and what parts aren't working yet. Write the question so that someone can read just your new message without having to remember what you wrote earlier. The issue, I think, is that it's been such a long time that it's possible that people do not remember your question. Other tips: If people have asked you questions, try to reply back to those questions. Make sure to Reply All so that it's a conversation with the group. Also, avoid email attachments if possible. If I remember rightly, your programs were short enough that they could be inlined into the email message. You'll get more eyes to see your program if you reduce the friction for people to inspect and help. Good luck to you! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] updating a dictionary
Hello List, I have a dictionary that I would like to update/add rows to it as I read a file line by line. The dictionary format looks like: format = {'Cell': '','7':'','8':'','9':'','2':''} For each line read in I would simply like to check to see if a Cell key;value exists and if it does update the correct key==band(7,8,9,2) within the dictionary. If the Cell doesn't exist do the same thing as above only make sure to update the Cell key:value with it's value form the file so it can check to see if it exists later. There are duplicate Cell:values in the file so when there is a duplicate it will need to look at band to see what key:value to update. Below is what I have attempted thus far. I can provide sample data if needed. Thank you in advance. import datetime import string import pprint from datetime import datetime # Open a files for reading inFileOne = open('PRB_utilization.txt', "r") iDas = "DB" oDas = "D" suffix = (iDas,oDas) dict = {'Cell': '','7':'','8':'','9':'','2':''} for line in inFileOne.readlines(): index = line.rstrip("\n").split("\t") cell = index[1] if cell.endswith(suffix, 14, 16) is False: eNb = cell[0:8] sector = cell[10:11] band = cell[9:10] dl_prb_utl = index[60] site = eNb + "_" + sector if site in dict: dict['Cell'] = site dict[band] = dl_prb_utl else: dict['Cell'] = site dict[band] = dl_prb_utl inFileOne.close(); ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] updating a dictionary
On 2/19/2015 1:19 PM, Chris Stinemetz wrote: Hello List, I have a dictionary that I would like to update/add rows to it as I read a file line by line. The dictionary format looks like: format = {'Cell': '','7':'','8':'','9':'','2':''} For each line read in I would simply like to check to see if a Cell key;value exists and if it does update the correct key==band(7,8,9,2) within the dictionary. If the Cell doesn't exist do the same thing as above only make sure to update the Cell key:value with it's value form the file so it can check to see if it exists later. There are duplicate Cell:values in the file so when there is a duplicate it will need to look at band to see what key:value to update. Below is what I have attempted thus far. I can provide sample data if needed. Thank you in advance. I've added comments interspersed below as I try to grok what you've got here... import datetime don't need this as you replace the value below... import string import pprint from datetime import datetime ''' here. # Open a files for reading inFileOne = open('PRB_utilization.txt', "r") iDas = "DB" oDas = "D" suffix = (iDas,oDas) dict = {'Cell': '','7':'','8':'','9':'','2':''} it's not good form to shadow python types for line in inFileOne.readlines(): index = line.rstrip("\n").split("\t") you now have a list of string values... cell = index[1] if cell.endswith(suffix, 14, 16) is False: ... so they'll never end with numeric values. Further, "".endswith() accepts only one argument so you ought to get an error on this line. eNb = cell[0:8] sector = cell[10:11] band = cell[9:10] dl_prb_utl = index[60] site = eNb + "_" + sector if site in dict: this tests if site is a valid key in dict -- but the only key value you've assigned to if 'Cell' so this will always be False... dict['Cell'] = site dict[band] = dl_prb_utl else: ... and the following will always execute. dict['Cell'] = site dict[band] = dl_prb_utl inFileOne.close(); Perhaps if you provide a sample of what the contents of inFileOne look like and what you want dict to look like after each iteration we'd get a better idea of what you're trying to accomplish. As it is, it'd likely take someone who recognizes the problem domain to shed light on this. Emile ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] updating a dictionary
On 19/02/15 21:19, Chris Stinemetz wrote: The dictionary format looks like: format = {'Cell': '','7':'','8':'','9':'','2':''} For each line read in I would simply like to check to see if a Cell key;value exists and if it does update the correct key==band(7,8,9,2) within the dictionary. If the Cell doesn't exist do the same thing as above only make sure to update the Cell key:value with it's value form the file so it can check to see if it exists later. I'm not sure what the difference is here? If you update the dictionary or add a new key it will automatically have that key going forward. There are duplicate Cell:values in the file so when there is a duplicate it will need to look at band to see what key:value to update. I'm not sure what 'band' is? You have not mentioned it up until now. Below is what I have attempted thus far. I can provide sample data if needed. I think a short same of your data would be useful, both input and output. import datetime import string > import pprint You don't use string. Do you really need it? It's quite rarely used these days. And you don't use pprint either. Do you plan on having a use for it later? from datetime import datetime # Open a files for reading inFileOne = open('PRB_utilization.txt', "r") You should get used to using the 'with' construct for file handling, it's usually shorter and much more reliable. iDas = "DB" oDas = "D" suffix = (iDas,oDas) suffix = ("DB","D") would be shorter and more obvious, since you don't use the single value variables anywhere in the code.. dict = {'Cell': '','7':'','8':'','9':'','2':''} Using dict as a name is a bad idea because it hides the built in dict type conversion function. for line in inFileOne.readlines(): You don't need readlines() any more you can just do: for line in inFileOne: index = line.rstrip("\n").split("\t") cell = index[1] if cell.endswith(suffix, 14, 16) is False: eNb = cell[0:8] sector = cell[10:11] band = cell[9:10] dl_prb_utl = index[60] site = eNb + "_" + sector I'll assume that's all OK since we can't see the data format. if site in dict: dict['Cell'] = site dict[band] = dl_prb_utl else: dict['Cell'] = site dict[band] = dl_prb_utl Both 'if' and 'else' clauses do the same thing so there is no point in doing a test. Just use: dict['Cell'] = site dict[band] = dl_prb_utl inFileOne.close(); You don't need this if you use the with... form. Other than the minor tweaks I've suggested I'm not sure what your problem is? I think we need to see the data to understand the issue. -- 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] updating a dictionary
On 19/02/15 22:50, Emile van Sebille wrote: if cell.endswith(suffix, 14, 16) is False: ... so they'll never end with numeric values. Further, "".endswith() accepts only one argument so you ought to get an error on this line. Sorry Emile, The OP is correct. ## >>> help(''.endswith) endswith(...) method of builtins.str instance S.endswith(suffix[, start[, end]]) -> bool Return True if S ends with the specified suffix, False otherwise. With optional start, test S beginning at that position. With optional end, stop comparing S at that position. suffix can also be a tuple of strings to try. ### The tuple is the set of suffices and the numbers are the start/end points. Its not often seen like that but the OP is quite correct. The test against False is unusual it woyuld normally look like if not cell.endswith(suffix, 14, 16): but that is just a style issue. -- 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] updating a dictionary
On 2/19/2015 3:10 PM, Alan Gauld wrote: On 19/02/15 22:50, Emile van Sebille wrote: if cell.endswith(suffix, 14, 16) is False: ... so they'll never end with numeric values. Further, "".endswith() accepts only one argument so you ought to get an error on this line. Sorry Emile, The OP is correct. ## >>> help(''.endswith) endswith(...) method of builtins.str instance S.endswith(suffix[, start[, end]]) -> bool You're right of course. That should teach me to read closer: TypeError: endswith() takes at least 1 argument (0 given) I skipped the 'at least' part of that. the-deeper-the-corners-the-thicker-the-cobwebs-ly y'rs, Emile ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] updating a dictionary
> > > > > Other than the minor tweaks I've suggested I'm not sure what your problem > is? I think we need to see the data to understand the issue. Here is a sample of the input data, it is tab delimited and I chopped it down for example purposes: KSL03502_7A_1 11.5921 KSL03502_7B_1 46.4997 KSL03502_7C_1 13.5839 KSL03505_7A_1 12.8684 KSL03505_7B_1 16.5311 KSL03505_7C_1 18.9926 KSL03509_7A_1 3.4104 KSL03509_7B_1 40.6244 KSL03509_7C_1 51.0597 KSL03511_7A_1 7.128 KSL03511_7B_1 53.4401 KSL03511_7C_1 66.2584 KSL03514_2A_1 25.6476 KSL03514_2B_1 53.17 KSL03514_2C_1 11.6469 KSL03514_7A_1 39.2292 KSL03514_7B_1 65.675 KSL03514_7C_1 3.4937 I would like to parse it buy using a dictionary structure. Where each row would be something like: name 7,8,9,2 KSL03514_C,3.4937,,,11.6469 KSL03514_B,65.675,,,53.17 I am just showing an example of what KSL03514_7C_1, KSL03514_2C_1, KSL03514_7B_1, KSL03514_2B_1 would parse. Hope this helps explain what I am trying to accomplish. Thank you in advance. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] updating a dictionary
On 20/02/2015 00:55, Chris Stinemetz wrote: Other than the minor tweaks I've suggested I'm not sure what your problem is? I think we need to see the data to understand the issue. Here is a sample of the input data, it is tab delimited and I chopped it down for example purposes: KSL03502_7A_1 11.5921 KSL03502_7B_1 46.4997 KSL03502_7C_1 13.5839 KSL03505_7A_1 12.8684 KSL03505_7B_1 16.5311 KSL03505_7C_1 18.9926 KSL03509_7A_1 3.4104 KSL03509_7B_1 40.6244 KSL03509_7C_1 51.0597 KSL03511_7A_1 7.128 KSL03511_7B_1 53.4401 KSL03511_7C_1 66.2584 KSL03514_2A_1 25.6476 KSL03514_2B_1 53.17 KSL03514_2C_1 11.6469 KSL03514_7A_1 39.2292 KSL03514_7B_1 65.675 KSL03514_7C_1 3.4937 I would like to parse it buy using a dictionary structure. Where each row would be something like: name 7,8,9,2 KSL03514_C,3.4937,,,11.6469 KSL03514_B,65.675,,,53.17 I am just showing an example of what KSL03514_7C_1, KSL03514_2C_1, KSL03514_7B_1, KSL03514_2B_1 would parse. Hope this helps explain what I am trying to accomplish. Thank you in advance. I think you need a defaultdict rather than a plain dict, otherwise you'll always be overwriting your values with new data. I believe this https://docs.python.org/3/library/collections.html#defaultdict-examples is exactly what you need. Am I close? -- 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