{"id":91,"date":"2016-06-08T15:31:40","date_gmt":"2016-06-08T19:31:40","guid":{"rendered":"http:\/\/williamkennerly.com\/blog\/?p=91"},"modified":"2016-06-17T16:07:32","modified_gmt":"2016-06-17T20:07:32","slug":"python-single-point-geometry-energy-script-update","status":"publish","type":"post","link":"http:\/\/williamkennerly.com\/blog\/python-single-point-geometry-energy-script-update\/","title":{"rendered":"Python Single-Point Geometry Energy Script Update"},"content":{"rendered":"<p>Continued from a <a href=\"http:\/\/williamkennerly.com\/blog\/python-script-for-single-point-energy-calculations\/\">previous post<\/a>.<\/p>\n<p>As I created my first completed version of the python script, I learned several things about python.<\/p>\n<p>One thing I learned was how file handles work. With the function:<\/p>\n<p><code>def find_parameters(file_handle):<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;for l in file_handle:<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if l.startswith(\" #\"):<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return l.rstrip()<\/code><\/p>\n<p>I was running into an error in which I received the message, Nonetype has no attribute&#8230;, meaning that my code was trying to use nothing as its input for functions that were looking for strings. That was after I looped through the file handle for a find function. I learned that looping through a file handle leaves the &#8220;cursor&#8221; at the end of the file handle, so that the very next use\u00a0of the file handle would start at the end of the file handle. To reset the &#8220;cursor&#8221; at the beginning of the file handle, I need to call <code>file_handle.seek(0)<\/code> after each iteration, as follows:<\/p>\n<p><code>def find_parameters(file_handle):<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;file_handle.seek(0)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;for l in file_handle:<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if l.startswith(\" #\"):<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return l.rstrip()<\/code><\/p>\n<p>Here is a <a href=\"http:\/\/stackoverflow.com\/questions\/3906137\/why-cant-i-call-read-twice-on-an-open-file\">Stack Overflow thread<\/a> with an explanation.<\/p>\n<p>Another thing I learned had to do with the difference between the <code>.append()<\/code> and <code>.extend()<\/code> functions.  After some searching and consultation with Prof. Kennerly, I realized that that was because I called <code>.extend()<\/code> rather than <code>.append()<\/code>. I was trying to add a string as an item to an empty list, and the <code>.extend()<\/code> function treated the string itself as some sort of list. Using the <code>.append()<\/code> function instead kept the string as a string and simply added it as a value to the list. Here is a <a href=\"http:\/\/stackoverflow.com\/questions\/252703\/python-append-vs-extend\">Stack Overflow thread<\/a> with more discussion of the difference between <code>.append()<\/code> and <code>.extend()<\/code>.<\/p>\n<p>One more error that I do not understand but have since overcome has to do with Unicode. I had an interesting hour or so in which I got long text files of Sanskrit and other unexpected languages instead of my output data, when I opened my results text file in Wordpad. Other text editors, such as Microsoft Word, had no trouble reading the text files. I think that this error had something to do with the <code>.extend()<\/code> function, but I&#8217;m not sure. I recall that the error went away after I corrected my code to use <code>.append()<\/code> instead of <code>.extend()<\/code>, but that may not have been the direct source of the error. Further investigation of the Unicode issue is needed for a better understanding of it.<\/p>\n<p>Often error messages may not describe the cause of the problem, but give clues of where the problem is. That is why many different error messages popped up when I had a more fundamental issue somewhere else in the script. In addition, as Prof. Kennerly says, computers always do what you tell them. So, the Sanskrit output made sense, given the code I wrote. To fix problems with the script, I needed more understanding of what certain functions did and how python and Wordpad read files.<\/p>\n<p>I used IDLE on a Windows computer to edit and run my script.<\/p>\n<p>P.S. WordPress is not good at allowing indentations or extra spaces. I have to use a keyword &amp;+nbsp; (without the +) to create spaces in the text editor, and all instances of it disappear if I switch to the visual editor. I think this is an issue with HTML, and I read that I would have to use CSS to allow indents in WordPress. This is an annoying problem, and I am trying to get around it for future posts where I show snippets of code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Continued from a previous post. As I created my first completed version of the python script, I learned several things about python. One thing I learned was how file handles work. With the function: def find_parameters(file_handle): &nbsp;&nbsp;&nbsp;&nbsp;for l in file_handle: &hellip; <a href=\"http:\/\/williamkennerly.com\/blog\/python-single-point-geometry-energy-script-update\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[14],"class_list":["post-91","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-python"],"_links":{"self":[{"href":"http:\/\/williamkennerly.com\/blog\/wp-json\/wp\/v2\/posts\/91","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/williamkennerly.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/williamkennerly.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/williamkennerly.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/williamkennerly.com\/blog\/wp-json\/wp\/v2\/comments?post=91"}],"version-history":[{"count":8,"href":"http:\/\/williamkennerly.com\/blog\/wp-json\/wp\/v2\/posts\/91\/revisions"}],"predecessor-version":[{"id":112,"href":"http:\/\/williamkennerly.com\/blog\/wp-json\/wp\/v2\/posts\/91\/revisions\/112"}],"wp:attachment":[{"href":"http:\/\/williamkennerly.com\/blog\/wp-json\/wp\/v2\/media?parent=91"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/williamkennerly.com\/blog\/wp-json\/wp\/v2\/categories?post=91"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/williamkennerly.com\/blog\/wp-json\/wp\/v2\/tags?post=91"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}