From PyTutor at DancesWithMice.info Sat Feb 1 03:42:59 2020 From: PyTutor at DancesWithMice.info (David L Neil) Date: Sat, 1 Feb 2020 21:42:59 +1300 Subject: [Tutor] Corrupt file(s) likelihood and prevention? In-Reply-To: <20200201020521.GA6289@cskk.homeip.net> References: <20200201020521.GA6289@cskk.homeip.net> Message-ID: <441b640b-fa91-42f3-7bd2-4f44119d8ae3@DancesWithMice.info> On 1/02/20 3:05 PM, Cameron Simpson wrote: > On 01Feb2020 00:20, Alan Gauld wrote: >> On 01/02/2020 00:04, Adam Slowley wrote: >>> What is this ? I never asked about corrupt files >>> ________________________________ >>> From: Tutor on >>> behalf of DL Neil via Tutor >> >> You are subscribed to the list which means you get all of the >> tutor emails, not just the ones you send/receive. That's how >> we learn from each other - by being exposed to issues we may >> not even have considered before. >> >> If the traffic is too high for you then you have the option >> of choosing to receive the digest instead if individual messages. >> All of the same material but in a single message instead of >> a stream. > > BTW, I discourage this approach. Far better to have your mailer filter > the tutor messages off into their own folder. Replying to digests brings > much pain for all, because replies are not direct replies to a specific > message. +1 and digest replies do not thread correctly - neither on client PCs nor in the Archives. -- Regards =dn From __peter__ at web.de Sat Feb 1 05:24:54 2020 From: __peter__ at web.de (Peter Otten) Date: Sat, 01 Feb 2020 11:24:54 +0100 Subject: [Tutor] Printing Bold Text References: Message-ID: Adam Slowley wrote: > So I?m a beginner, learning Python for the first time so this might be a > dumb question but here goes. I?m trying to print a text in bold. For > example: > > def main(): > name=input("Enter your name:") > print("You entered for name a value of",name) > > I want whatever the user inputs as his/her name to be printed in Bold It will only work with terminals that understand ANSI escape codes, but if you can live with that limitiation you can install termcolor: https://pypi.org/project/termcolor/ Example: (1) installation with pip: $ pip install termcolor [...] Successfully installed termcolor Cleaning up... (2) $ python [...] >>> import termcolor >>> termcolor.cprint("all bold", attrs=["bold"]) all bold >>> print("this is", termcolor.colored("red", color="red")) this is red Though it's hard ;) to see in a text-only mail "all bold" is printed bold and the word "red" is shown in the color red. From alan.gauld at yahoo.co.uk Sat Feb 1 09:26:20 2020 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 1 Feb 2020 14:26:20 +0000 Subject: [Tutor] Printing Bold Text In-Reply-To: References: Message-ID: On 31/01/2020 18:30, Adam Slowley wrote: I was bored so I built this demo of how to do what you want using curses. ######### bold_input.py ##### import curses def getInput(win, prompt): cur.echo() # turn echo on, just in case... win.addstr(prompt) win.refresh() win.attrset(cur.A_BOLD) value = win.getstr().decode('utf-8') win.attrset(cur.A_NORMAL) return value def test_bold(scr): name = getInput(scr,"What is your name? ") scr.addstr(4,2,"Hello " + name) scr.refresh() scr.getkey() # pause to view screen if __name__ == "__main__": cur.wrapper(test_bold) ################################# You can import the module and use getInput() - but you need to use curses for the rest of your app. -- 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 From robertvstepp at gmail.com Sat Feb 1 17:16:54 2020 From: robertvstepp at gmail.com (boB Stepp) Date: Sat, 1 Feb 2020 16:16:54 -0600 Subject: [Tutor] Corrupt file(s) likelihood and prevention? In-Reply-To: References: Message-ID: While reading Alan's response I found myself having a sense of d?j? vu. Looking back at last year's Tutor archives I found https://www.mail-archive.com/tutor at python.org/msg80574.html where I was asking about the pluses/minuses of storing calculated values versus recalculating them upon a rerunning of a program. The topic of data corruption came up there. Alan's responses then and more pointedly now that _eventually_ data corruption will happen is bothersome in my mind even though he pointed out that this, in his experience, was rarely a problem. But ... On Fri, Jan 31, 2020 at 5:20 PM DL Neil via Tutor wrote: > So, if I've interpreted your interest correctly, the pertinent part of > that (perhaps less apparent in your question) is: what can we do about > the risk of corruption, when files which may have been updated, have > not been/cannot be, closed properly? This along with other possible sources of problems seems to demand some sort of defensive programming. Should every file in a program, both data files and source code files have an associated checksum? I mentioned that one of the applications I use at work makes extensive use of checksums. Every file in the application I have ever opened ends in a checksum value. The other mentioned solution for data is to use a robust database application to store the data which presumably manages, as best as these possibilities can be managed, prevention of data corruption. But what about the source code itself? Maybe a corrupt file will crash, but maybe it won't and will subtly lead to erroneous behaviors? I don't know; I'm speculating at this point. But even if corruption is detected, unless actually retrievable backups exist, per Alan, one is stuck in one's tracks! -- boB From alan.gauld at yahoo.co.uk Sat Feb 1 18:05:06 2020 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 1 Feb 2020 23:05:06 +0000 Subject: [Tutor] Corrupt file(s) likelihood and prevention? In-Reply-To: References: Message-ID: On 01/02/2020 22:16, boB Stepp wrote: > some sort of defensive programming. Should every file in a program, > both data files and source code files have an associated checksum? Source code should be backed up and in a version control system. That way even if a file gets corrupted you can get back to a very recent ancestor. But for non-executing files multiple backups are the correct solution. > ends in a checksum value. The other mentioned solution for data is to > use a robust database application to store the data which presumably > manages, as best as these possibilities can be managed, prevention of > data corruption. Databases should also have a backup strategy but RDBMS systems have built in redundancy and recovery mechanisms plus they usually run on servers with RAID storage so even a corrupt disk won't lose anything. > But even if corruption is detected, unless actually retrievable > backups exist, per Alan, one is stuck in one's tracks! Yes. Absolutely. Backups are not an optional activity if you care about your data. And ideally stored off site and to at least 3 generations. -- 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 From mats at wichmann.us Sat Feb 1 19:03:32 2020 From: mats at wichmann.us (Mats Wichmann) Date: Sat, 1 Feb 2020 17:03:32 -0700 Subject: [Tutor] Input is Dictionary1, Output is Dictionary2 (using keys and values of Dictionary1) In-Reply-To: References: Message-ID: <016d87d5-9df1-7cac-fab1-3ba166acd2ab@wichmann.us> On 1/30/20 2:47 PM, Mats Wichmann wrote: > On 1/30/20 1:23 PM, Panchanathan Suresh wrote: >> Hi Everyone, >> >> I spent a lot of time on this, trying out many things, but I am unable to >> create a new dictionary with the users as keys and a list of their groups >> as values. >> >> Input Dictionary is {"local": ["admin", "userA"],"public": ["admin", >> "userB"],"administrator": ["admin"] } >> Expected Output Dictionary is {"userA": ["admin","public"], "userB": >> ["admin"], "administrator": ["admin"]} >> >> --- How to create an unique group for a particular user? >> --- And then how to add this user key and the user's value (which will be >> the list of groups the user belongs to) >> >> ***** >> def groups_per_user(group_dictionary): >> user_groups = {} >> groups = [] >> # Go through group_dictionary >> for group,user in group_dictionary.items(): >> # Now go through the users in the group >> for user1 in user: >> groups.append(group) >> print(user1,groups) >> >> return(user_groups) >> >> print(groups_per_user({"local": ["admin", "userA"],"public": ["admin", >> "userB"],"administrator": ["admin"] })) >> >> ***** > > Hopefully this isn't a homework problem :) > > You're not adding anything to your new dictionary. What you want to do > is use the user as a key, and a list of groups as the value; the trick > is that you may need to add several times to a user's entry, so you need > to handle both the case of "user not yet in dictionary" and the case of > "user has a grouplist, add to that list". As a quick and dirty hack, > like this (I changed a name a bit for clarity): > > for group, userlist in group_dictionary.items(): > # Now go through the users in the group > for user in userlist: > try: > user_groups[user].append(group) > except KeyError: > user_groups[user] = [group] Now some time has passed, I wanted to add, as a somewhat advanced topic for those who may read this in the archives, that there are more concise approaches. One way is to use, for the new calculated dictionary, a derived class of dict, the defaultdict. With defaultdict, the behavior of accessing a key that doesn't exist changes: instead of raising a KeyError, the key is automatically inserted with an empty value of the type specified when instantiating the defaultdict, and then things proceed as normal. In this case we want a list, so we can rewrite the above stuff like this: from collections import defaultdict def groups_per_user(group_dictionary): user_groups = defaultdict(list) # Go through group_dictionary for group, userlist in group_dictionary.items(): # Now go through the users in the group for user in userlist: user_groups[user].append(group) return user_groups From PyTutor at DancesWithMice.info Sat Feb 1 22:10:32 2020 From: PyTutor at DancesWithMice.info (David L Neil) Date: Sun, 2 Feb 2020 16:10:32 +1300 Subject: [Tutor] Corrupt file(s) likelihood and prevention? In-Reply-To: References: Message-ID: <5869c195-a856-66e1-9c01-de728776cd2a@DancesWithMice.info> On 2/02/20 12:05 PM, Alan Gauld via Tutor wrote: > On 01/02/2020 22:16, boB Stepp wrote: > >> some sort of defensive programming. Should every file in a program, >> both data files and source code files have an associated checksum? ... > Databases should also have a backup strategy but RDBMS systems > have built in redundancy and recovery mechanisms plus they usually > run on servers with RAID storage so even a corrupt disk won't lose anything. ... > Yes. Absolutely. Backups are not an optional activity if you > care about your data. And ideally stored off site and to at > least 3 generations. ...stored in multiple locations (define "multiple" according to your level of risk/paranoia/caution/$depth-of-pockets. All these things are two-edged swords, eg this morning it took me longer to perform a precautionary (SSD to HDD) backup of my system settings and /home partition, than it did to perform the actual system-upgrade! (one of the beauties of Linux, and one of the pains of behaving conservatively) Back in the ?good, old days, when amending a DB, we would (first) output the command+data to a separate file (likely on a separate drive) and only after that, perform the DB-operation. The file became known as a "journal". When things (were realised to have) went awry, we would restore an assured backup of the DB, and then re-run the journal. These days, RDBMS-es keep their own journals. Thank you! I haven't built a system with manual/application journaling for at least one, and possibly two, decades! (even with MySQL) Similarly, the ext4 FS - a popular/common file system under Linux, is a journaling FS. If something goes wrong, one is invited/instructed to run fsck ("file system consistency check" - not what one of my circle called it after substituting letters!). Such checks are performed periodically, and/or after a set number of reboots. In such an environment, the need to maintain separate/embedded check-sums would appear to have also passed. However, greater minds might have something else to say... and, as always, YMMV! NB I can't comment about MS-Windows' ability to maintain file consistency, nor (necessarily) other FS on other systems... -- Regards =dn From alan.gauld at yahoo.co.uk Sun Feb 2 03:58:15 2020 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 2 Feb 2020 08:58:15 +0000 Subject: [Tutor] Corrupt file(s) likelihood and prevention? In-Reply-To: <5869c195-a856-66e1-9c01-de728776cd2a@DancesWithMice.info> References: <5869c195-a856-66e1-9c01-de728776cd2a@DancesWithMice.info> Message-ID: On 02/02/2020 03:10, David L Neil via Tutor wrote: > Similarly, the ext4 FS - a popular/common file system under Linux, is a > journaling FS. If something goes wrong, one is invited/instructed to run >... > In such an environment, the need to maintain separate/embedded > check-sums would appear to have also passed. However, greater minds > might have something else to say... and, as always, YMMV! Checksums are not needed as often with a JFS but there are still cases where they are useful. The most obvious is a shared file where a running process needs to know if a file has changed between accesses due to the action of another program (or user). Also in a world of pluggable storage, most of which is still using FAT, checksums are still important. And of course, if the data is being shipped via a network a checksum is still a useful integrity check. -- 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 From antonklem8 at gmail.com Mon Feb 3 02:28:48 2020 From: antonklem8 at gmail.com (anton klem) Date: Sun, 2 Feb 2020 23:28:48 -0800 Subject: [Tutor] Course for free on 100 Python Challenges to Boost Your Python Skills Message-ID: [image: Capture.PNG] What you'll learn - Learn Python 3 by solving Python problems on your own - Solve data structures and algorithm exercises to help on your data science journey - Create 50+different functions - Learn the basics of Python classes - Read, write, and extract information from files - Extract data from text - Optimize your code in terms of processing speed and memory efficiency - Introspect your code - Learn Python tricks Now the course is 100% free From this link http://gestyy.com/w7L3H0 Requirements - To make the best of this course you should be familiar with Python basics: loops, conditionals, functions, and datatypes. Description Video tutorials are great to learn the basics of Python. You can try out and experiment with the code that the teacher demonstrates in the video. However, never did anyone became a programmer by just watching videos. That is because your brain hardly learns anything when it's not in problem solving mode. When you put your mind in problem solving mode your mind will devour every piece of information it finds in an effort to solve the given problem. Therefore, if you want to learn Python programming your only way is to sit and solve Python problems on your own. That is what this course is all about. Here you will find an interactive Python problem solving interface where you will try to solve more than 100 Python exercises and quizzes. Moreover, you will find timed exams that will rigorously test the knowledge you gained when you were solving the preceding exercises. The problems cover different areas of Python such as functions, data structures and algorithms, files, classes, code introspection, code optimization, and more. The section of data structures and algorithms is the biggest one and it's especially useful for those who are interested in data science. You can write and run the code directly on the Udemy interface using your browser and get the answer instantly. That means you can even do this course on your mobile phone while traveling on a bus or train. The course assumes you have some basic knowledge of Python so if you know loops, functions, conditionals, and datatypes, then this course is for you. If you don't have any knowledge of Python you can still take the course, but you will have a hard time solving the exercises. If you think you're ready to learn Python by challenging yourself, this program was carefully designed just for you. Python is both fun and extremely useful. Don't miss the chance to learn it. Who this course is for: - Beginner Python Developers From PyTutor at DancesWithMice.info Sun Feb 2 20:29:15 2020 From: PyTutor at DancesWithMice.info (DL Neil) Date: Mon, 3 Feb 2020 14:29:15 +1300 Subject: [Tutor] WARNING: Course for free on 100 Python Challenges to Boost Your Python Skills In-Reply-To: References: Message-ID: <256d7a00-48a9-27ed-56a7-021ee1aedd57@DancesWithMice.info> On 3/02/20 8:28 PM, anton klem wrote: > [image: Capture.PNG] > Now the course is 100% free From this link > http://gestyy.com/w7L3H0 Maybe my computer's security system is too strong, but after following three levels of invitation to 'click-here', even the "Download" button didn't deliver! (except loads of advertising pop-ups) I was unable to find the course on Udacity (not that they make that task v.easy), therefore was not able to establish either the authenticity of the claims, or the authority of the sender to make the course-materials available. Not having seen this name on the list before, and noting the huge number of recipients, be aware that this 'invitation' is probably mass-marketing, or worse. -- Regards =dn From akleider at sonic.net Sun Feb 2 22:21:09 2020 From: akleider at sonic.net (Alex Kleider) Date: Sun, 02 Feb 2020 19:21:09 -0800 Subject: [Tutor] WARNING: Course for free on 100 Python Challenges to Boost Your Python Skills In-Reply-To: <256d7a00-48a9-27ed-56a7-021ee1aedd57@DancesWithMice.info> References: <256d7a00-48a9-27ed-56a7-021ee1aedd57@DancesWithMice.info> Message-ID: <6f6eba6b973346d5395059d88b40c49a@sonic.net> On 2020-02-02 17:29, DL Neil via Tutor wrote: > On 3/02/20 8:28 PM, anton klem wrote: >> [image: Capture.PNG] >> Now the course is 100% free From this link >> http://gestyy.com/w7L3H0 > > > Maybe my computer's security system is too strong, but after following > three levels of invitation to 'click-here', even the "Download" button > didn't deliver! (except loads of advertising pop-ups) > > I was unable to find the course on Udacity (not that they make that > task v.easy), therefore was not able to establish either the > authenticity of the claims, or the authority of the sender to make the > course-materials available. > > Not having seen this name on the list before, and noting the huge > number of recipients, be aware that this 'invitation' is probably > mass-marketing, or worse. I also tried to follow the link and also had a rather unsatisfactory experience. From Richard at Damon-Family.org Mon Feb 3 06:58:00 2020 From: Richard at Damon-Family.org (Richard Damon) Date: Mon, 3 Feb 2020 06:58:00 -0500 Subject: [Tutor] WARNING: Course for free on 100 Python Challenges to Boost Your Python Skills In-Reply-To: <256d7a00-48a9-27ed-56a7-021ee1aedd57@DancesWithMice.info> References: <256d7a00-48a9-27ed-56a7-021ee1aedd57@DancesWithMice.info> Message-ID: <13006d4c-a7ff-cd2d-7694-095b60d857ab@Damon-Family.org> On 2/2/20 8:29 PM, DL Neil via Tutor wrote: > On 3/02/20 8:28 PM, anton klem wrote: >> ? [image: Capture.PNG] >> Now the course is 100% free From this link >> ? http://gestyy.com/w7L3H0 > > > Maybe my computer's security system is too strong, but after following > three levels of invitation to 'click-here', even the "Download" button > didn't deliver! (except loads of advertising pop-ups) > > I was unable to find the course on Udacity (not that they make that > task v.easy), therefore was not able to establish either the > authenticity of the claims, or the authority of the sender to make the > course-materials available. > > Not having seen this name on the list before, and noting the huge > number of recipients, be aware that this 'invitation' is probably > mass-marketing, or worse. And if you look at the list of recipients, it includes some that are clearly invalid (example.com) and some that appear to be file names that just happened to have a @ in them (I don't know of a .png top level domain), so the list appears to have come from some form of crude web scraping with no thought to edit. -- Richard Damon From colinkimble82 at gmail.com Tue Feb 4 16:07:08 2020 From: colinkimble82 at gmail.com (Colin Kimble) Date: Tue, 4 Feb 2020 13:07:08 -0800 Subject: [Tutor] Windows err code 2 Message-ID: Afternoon, my name is Colin Kimble. I have been having trouble with regards to running code. I have run it from the command prompt. I had received two error messages. C:\Users\pc\Desktop\Programming\autologinbot-master\autologinbot-master>python l ogin.py Traceback (most recent call last): File "C:\Users\pc\AppData\Local\Programs\Python\Python38-32\lib\site-packages\ selenium\webdriver\common\service.py", line 72, in start self.process = subprocess.Popen(cmd, env=self.env, File "C:\Users\pc\AppData\Local\Programs\Python\Python38-32\lib\subprocess.py" , line 854, in __init__ self._execute_child(args, executable, preexec_fn, close_fds, File "C:\Users\pc\AppData\Local\Programs\Python\Python38-32\lib\subprocess.py" , line 1307, in _execute_child hp, ht, pid, tid = _winapi.CreateProcess(executable, args, FileNotFoundError: [WinError 2] The system cannot find the file specified During handling of the above exception, another exception occurred: Traceback (most recent call last): File "login.py", line 9, in browser = webdriver.Chrome() File "C:\Users\pc\AppData\Local\Programs\Python\Python38-32\lib\site-packages\ selenium\webdriver\chrome\webdriver.py", line 73, in __init__ self.service.start() File "C:\Users\pc\AppData\Local\Programs\Python\Python38-32\lib\site-packages\ selenium\webdriver\common\service.py", line 81, in start raise WebDriverException( selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executabl e needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chrome driver/home It cannot find my .py file and it says that Chromedriver needs to be in the path, but I am not to sure how this is to be done. I hope you are able to assist me. I wish you well, and I hope to hear from you soon. Sincerley Colin Kimble From mats at wichmann.us Tue Feb 4 14:59:56 2020 From: mats at wichmann.us (Mats Wichmann) Date: Tue, 4 Feb 2020 12:59:56 -0700 Subject: [Tutor] Windows err code 2 In-Reply-To: References: Message-ID: <5bfd687c-a1ad-5d31-7d44-8c3ac2bfacd4@wichmann.us> On 2/4/20 2:07 PM, Colin Kimble wrote: > Afternoon, my name is Colin Kimble. I have been having trouble with regards > to running code. I have run it from the command prompt. I had received two > error messages. there's only one error, it's just it compounds. > It cannot find my .py file and it says that Chromedriver needs to be in the > path, but I am not to sure how this is to be done. I hope you are able to > assist me. I wish you well, and I hope to hear from you soon. it's not having trouble finding your Python file, it's having trouble finding a program it needs to run. [extra explanation: the subprocess module is used by Python code to call an external command, and you can see from the error the system can't find that command. Trimmed down a bit: > "C:\Users\pc\AppData\Local\Programs\Python\Python38-32\lib\site-packages\ > selenium\webdriver\common\service.py", line 72, in start > self.process = subprocess.Popen(cmd, env=self.env, ... > FileNotFoundError: [WinError 2] The system cannot find the file specified Unfortunately, there it's not telling you what the value of "cmd", the command to run, is. This should tell you what to do next: > selenium.common.exceptions.WebDriverException: Message: 'chromedriver' > executable needs to be in PATH. Please see > https://sites.google.com/a/chromium.org/chromedriver/home Probably a subsidiary page is what you want: https://sites.google.com/a/chromium.org/chromedriver/getting-started suggest you try the "Sample Test" listed there after you've followed the installation instructions. For anything further, you need to work with the Selenium community, I'd imagine - this isn't a Python problem per se, looks like just getting the setup of a bunch of dependencies right. From Abhinava.saha at outlook.com Wed Feb 5 00:34:52 2020 From: Abhinava.saha at outlook.com (Abhinava Saha) Date: Wed, 5 Feb 2020 05:34:52 +0000 Subject: [Tutor] Need help - getting Tuple index out of range Message-ID: Hi all, I am a beginner in python, Someone please help me fix the program below on tuple, getting error touple index out of range. def full_email(peoples): result = [] for index, (name, email) in enumerate(peoples): result.append("{} <{}>".format("name, email")) return result print(full_email([('Abhinava', 'abhinava at abc.com'), ('Manoj', 'manoj at xyz.com')])) Regards, Abhinava Sent from Mail for Windows 10 From akleider at sonic.net Wed Feb 5 12:25:49 2020 From: akleider at sonic.net (Alex Kleider) Date: Wed, 05 Feb 2020 09:25:49 -0800 Subject: [Tutor] Need help - getting Tuple index out of range In-Reply-To: References: Message-ID: <461dbae74938ef18d8c58cf0eb90f6ac@sonic.net> On 2020-02-04 21:34, Abhinava Saha wrote: > Hi all, > > I am a beginner in python, Someone please help me fix the program > below on tuple, getting error touple index out of range. > > def full_email(peoples): > result = [] > for index, (name, email) in enumerate(peoples): > result.append("{} <{}>".format("name, email")) > return result > > print(full_email([('Abhinava', 'abhinava at abc.com'), ('Manoj', > 'manoj at xyz.com')])) You are not using the index provided by enumerate so I suggest you get rid of it. The error you are getting is because your format string requires two parameters (one for each {}) and you've only provided one ("name, email"). Change "name, email" which is one string to the two variables you want: name and email. The following works: """ def full_email(peoples): result = [] for (name, email) in peoples: result.append("{} <{}>".format(name, email)) return result print(full_email([('Abhinava', 'abhinava at abc.com'), ('Manoj', 'manoj at xyz.com')])) """ From mats at wichmann.us Wed Feb 5 13:18:43 2020 From: mats at wichmann.us (Mats Wichmann) Date: Wed, 5 Feb 2020 11:18:43 -0700 Subject: [Tutor] Need help - getting Tuple index out of range In-Reply-To: <461dbae74938ef18d8c58cf0eb90f6ac@sonic.net> References: <461dbae74938ef18d8c58cf0eb90f6ac@sonic.net> Message-ID: <70e82f98-90ce-4931-882c-12d3bf574f65@wichmann.us> On 2/5/20 10:25 AM, Alex Kleider wrote: > On 2020-02-04 21:34, Abhinava Saha wrote: > You are not using the index provided by enumerate so I suggest you get > rid of it. > The error you are getting is because your format string requires two > parameters (one for each {}) and you've only provided one ("name, email"). > Change "name, email" which is one string to the two variables you want: > name and email. > The following works: > """ > def full_email(peoples): > ??? result = [] > ??? for (name, email) in peoples: > ??????? result.append("{} <{}>".format(name, email)) > ??? return result > > print(full_email([('Abhinava', 'abhinava at abc.com'), ('Manoj', > 'manoj at xyz.com')])) > """ And if you are able to ensure a recent Python version, you can use f-strings to write it in a way there isn't likely to be any confusion: result.append(f"{name} <{email}>") From breamoreboy at gmail.com Wed Feb 5 13:35:30 2020 From: breamoreboy at gmail.com (Mark Lawrence) Date: Wed, 5 Feb 2020 18:35:30 +0000 Subject: [Tutor] Need help - getting Tuple index out of range In-Reply-To: References: Message-ID: On 05/02/2020 05:34, Abhinava Saha wrote: > Hi all, > > I am a beginner in python, Someone please help me fix the program below on tuple, getting error touple index out of range. > > def full_email(peoples): > result = [] > for index, (name, email) in enumerate(peoples): > result.append("{} <{}>".format("name, email")) > return result > > print(full_email([('Abhinava', 'abhinava at abc.com'), ('Manoj', 'manoj at xyz.com')])) > > > Regards, > Abhinava > As you've all ready had answers I'll just point out that you can use list comprehensions https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions instead of the for loop to achieve the same result. Writing the code is left as an exercise for any reader :) -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From hanckokruger at gmail.com Sat Feb 8 10:35:24 2020 From: hanckokruger at gmail.com (Hancko Kruger) Date: Sat, 8 Feb 2020 17:35:24 +0200 Subject: [Tutor] AttributeError Message-ID: <5e3ed53b.1c69fb81.fc2e7.9a5d@mx.google.com> Dear sir/madam I have been working on a program in which to solve the following problem: I need to write a program which can count how many times a certain word (?bob?) occurs within any string. The starting letter of the following ?bob? must also be able to be the last ?b? of the first ?bob?. E.g: if I am given a string: ?bobob?, the number of times ?bob? occurs within the string is: 2 times. Currently I am using a for loop with the range being the length of the given string (s). I have the following code: # enter a string for 's' s = 'aabobobaa' countBob = 0 # the starting position for the computer to search for 'b' start = 0 # find the position of the first 'b' within the string. # LOOP START for s in range(start,len(s)): if s[s.find('b'):(s.find('b') + 3)] == 'bob': countBob = countBob + 1 start = start + s.find('b') print('Number of times bob occurs is: ' + str(countBob)) #LOOP END If I run the code the console keeps on giving me an AttributeError. I have even tried the index function instead of find yet I have no luck. I do not understand what I am doing wrong. Please help. Kind regards Hancko From alan.gauld at yahoo.co.uk Sat Feb 8 12:32:21 2020 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 8 Feb 2020 17:32:21 +0000 Subject: [Tutor] AttributeError In-Reply-To: <5e3ed53b.1c69fb81.fc2e7.9a5d@mx.google.com> References: <5e3ed53b.1c69fb81.fc2e7.9a5d@mx.google.com> Message-ID: On 08/02/2020 15:35, Hancko Kruger wrote: > If I run the code the console keeps on giving me an AttributeError. Please, ALWAYS post the full error trace not a summary. That would tell us which line/object and name is causing the error. Without it we have to try to work it out. tedious! -- 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 From mats at wichmann.us Sat Feb 8 12:36:36 2020 From: mats at wichmann.us (Mats Wichmann) Date: Sat, 8 Feb 2020 10:36:36 -0700 Subject: [Tutor] AttributeError In-Reply-To: <5e3ed53b.1c69fb81.fc2e7.9a5d@mx.google.com> References: <5e3ed53b.1c69fb81.fc2e7.9a5d@mx.google.com> Message-ID: <0854fba2-20d3-56ba-c565-5daee8f13147@wichmann.us> On 2/8/20 8:35 AM, Hancko Kruger wrote: > Dear sir/madam > I have been working on a program in which to solve the following problem: I need to write a program which can count how many times a certain word (?bob?) occurs within any string. The starting letter of the following ?bob? must also be able to be the last ?b? of the first ?bob?. E.g: if I am given a string: ?bobob?, the number of times ?bob? occurs within the string is: 2 times. > > Currently I am using a for loop with the range being the length of the given string (s). > > I have the following code: > > > # enter a string for 's' > s = 'aabobobaa' > countBob = 0 > # the starting position for the computer to search for 'b' > start = 0 > # find the position of the first 'b' within the string. > # LOOP START > for s in range(start,len(s)): > if s[s.find('b'):(s.find('b') + 3)] == 'bob': > countBob = countBob + 1 > start = start + s.find('b') > print('Number of times bob occurs is: ' + str(countBob)) > #LOOP END > > > If I run the code the console keeps on giving me an AttributeError. I have even tried the index function instead of find yet I have no luck. I do not understand what I am doing wrong. Please help. It really helps if you post the actual error message, because it contains real clues. In this case I can see what's wrong without it, but keep that in mind... when you loop over a range object you are getting a number which you assign to s. Then you do s.find ... I'll bet the AttributeError is that an int has no find method. The real problem here is you're reusing 's': it holds the string you're trying to look up in, and then you throw that away by assigning something different to s in the for loop. There are better ways to solve this problem, but let's talk about that later, when you've fixed this one up first. From alfielee21 at outlook.com Mon Feb 10 14:36:05 2020 From: alfielee21 at outlook.com (Alfie Lee) Date: Mon, 10 Feb 2020 19:36:05 +0000 Subject: [Tutor] (no subject) Message-ID: Sent from Mail for Windows 10 If say I had a variable called ?username?, and a list of usernames called ?usernames?; how would I write it so that the programme will check if that username is in the usernames list? this would be using an if statement if possible. From mats at wichmann.us Mon Feb 10 14:56:24 2020 From: mats at wichmann.us (Mats Wichmann) Date: Mon, 10 Feb 2020 12:56:24 -0700 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: <9ad7af83-b6d5-8eed-5d91-dbea38e8df92@wichmann.us> On 2/10/20 12:36 PM, Alfie Lee wrote: > > > Sent from Mail for Windows 10 > > If say I had a variable called ?username?, and a list of usernames called ?usernames?; how would I write it so that the programme will check if that username is in the usernames list? > this would be using an if statement if possible. > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > if username in usernames: From theclancampbell at outlook.com Mon Feb 10 15:07:31 2020 From: theclancampbell at outlook.com (David Campbell) Date: Mon, 10 Feb 2020 20:07:31 +0000 Subject: [Tutor] FW: Python Beginner In-Reply-To: References: Message-ID: Hi, I am trying to seek some help with the thread below Regards David ________________________________ From: David Campbell Sent: Friday, February 7, 2020 6:19:17 PM To: python-list at python.org Subject: Python Beginner Hello, Please could you direct me to the novice beginner Python user stupid asking stupid questions department please I am trying to learn Python via Cisco Academy and there tutorial states that you should click File etc to create a new file [cid:image004.png at 01D5DDE3.1A95B070] I have downloaded version 3.8 of python and when I run it has no option or menu. I am running Windows 10 (FYI) [cid:image005.png at 01D5DDE3.1A95B070] Hope you can help Regards David Campbell 07841 705421 From alan.gauld at yahoo.co.uk Mon Feb 10 16:06:51 2020 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 10 Feb 2020 21:06:51 +0000 Subject: [Tutor] FW: Python Beginner In-Reply-To: References: Message-ID: On 10/02/2020 20:07, David Campbell wrote: > Please could you direct me to the novice beginner Python user stupid asking stupid questions department please Hmm, that would probably be us! Any question on Python or its standard library or programming in general is welcome. > I am trying to learn Python via Cisco Academy I don;t know that one but,... > and there tutorial states that you should click File etc to create a new file They obviously expect you to use an IDE of some sort - IDLE comes with Python but there are many others. They all have a standard menu with File->New or File->Open items. If in doubt gicve us a shout for recommendations. But as a beginner, IDLE (sometimes called "Python GUI" on Windows for some reason!) should be fine. > [cid:image004.png at 01D5DDE3.1A95B070] The mailing list is text only so non text attachments get stripped by the server. Always cut n paste code and error messages into your mails. -- 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 From alan.gauld at yahoo.co.uk Mon Feb 10 18:26:13 2020 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 10 Feb 2020 23:26:13 +0000 Subject: [Tutor] FW: Python Beginner In-Reply-To: References: Message-ID: <6caeb9dd-512e-ec81-459f-92e58c97cdb0@yahoo.co.uk> Please always use reply All or Reply List when responding to tutor list mail. Otherwise it only goes to the individual not the list. I've CCd this to the list. On 10/02/2020 21:10, David Campbell wrote: > Thanks > > Can I rephrase my question. I am learning Python via Cisco academy and they directed > me to the Python.org website to download the required software which I did however > the option to open a new file is not present ? The python.exe file is the interpreter used to run your Python code. If you run it you will get the Python interactive prompt (>>>) in a command console but no option to edit files. But, in your download somewhere you should find a file called idle.bat(?) to which you can create a shortcut (on your desktop or menu). IDLE is an Integrated Development Environment (IDE) for building and executing Python programs. It is a GUI (itself built in Python) with the usual File menu options. I assume this is what your tutorial is referring to and expecting you to use. Do a search in File Manager under your Python install for idle.* You should find it somewhere there (It's a while since I used Windows so I can't recall the details of its file type or location) -- 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 From PyTutor at danceswithmice.info Mon Feb 10 18:54:38 2020 From: PyTutor at danceswithmice.info (DL Neil) Date: Tue, 11 Feb 2020 12:54:38 +1300 Subject: [Tutor] FW: Python Beginner In-Reply-To: <6caeb9dd-512e-ec81-459f-92e58c97cdb0@yahoo.co.uk> References: <6caeb9dd-512e-ec81-459f-92e58c97cdb0@yahoo.co.uk> Message-ID: On 11/02/20 12:26 PM, Alan Gauld via Tutor wrote: > On 10/02/2020 21:10, David Campbell wrote: >> Can I rephrase my question. I am learning Python via Cisco academy and they directed >> me to the Python.org website to download the required software which I did however >> the option to open a new file is not present ? (Happily) I do not use MS-Windows either. Perhaps these web.refs will help? 3. Using Python on Windows: https://docs.python.org/3/using/windows.html Python on Windows documentation: https://docs.microsoft.com/en-us/windows/python/ -- Regards =dn From collisio at net-devops.com Tue Feb 11 01:56:43 2020 From: collisio at net-devops.com (Collisio Adolebitque) Date: Tue, 11 Feb 2020 06:56:43 +0000 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: username = 'user3' usernames = ['user1', 'user2', 'user3', 'user4'] def check_list_for_pattern(user: str, users: list) -> bool: return True if user in users else False print(check_list_for_pattern(username, usernames)) ________________________________ From: Tutor on behalf of Alfie Lee Sent: Monday, February 10, 2020 7:36:05 PM To: tutor at python.org Subject: [Tutor] (no subject) Sent from Mail for Windows 10 If say I had a variable called ?username?, and a list of usernames called ?usernames?; how would I write it so that the programme will check if that username is in the usernames list? this would be using an if statement if possible. _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fmail.python.org%2Fmailman%2Flistinfo%2Ftutor&data=02%7C01%7C%7Ce5bb51c2eace41581d6b08d7ae6301d5%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637169612558658843&sdata=5mOwzWcFQD1FEwF5DQ%2BXHI8FrGeETMFzEIU%2BjZkyY5U%3D&reserved=0 From mudgilakash66 at gmail.com Mon Feb 10 19:18:04 2020 From: mudgilakash66 at gmail.com (Akash Mudgil) Date: Tue, 11 Feb 2020 05:48:04 +0530 Subject: [Tutor] (no subject) In-Reply-To: <9ad7af83-b6d5-8eed-5d91-dbea38e8df92@wichmann.us> References: <9ad7af83-b6d5-8eed-5d91-dbea38e8df92@wichmann.us> Message-ID: Let's say if you wanted to search for a username in a, list where 'username' variable is predefined. Example: username='BOB' usernames=['BOB','Alice','David'] for i in range(len(usernames)): if usernames[i]==username: print("User exists in the list") This program doea a linear search on every index in the list, and if the value matches, returns the output. The len function gets the total length of the list, and the range function defines the scope of the search from beginning to end of the list. Hope this helps. On Tue, 11 Feb, 2020, 1:27 AM Mats Wichmann, wrote: > On 2/10/20 12:36 PM, Alfie Lee wrote: > > > > > > Sent from Mail for > Windows 10 > > > > If say I had a variable called ?username?, and a list of usernames > called ?usernames?; how would I write it so that the programme will check > if that username is in the usernames list? > > this would be using an if statement if possible. > > > > _______________________________________________ > > Tutor maillist - Tutor at python.org > > To unsubscribe or change subscription options: > > https://mail.python.org/mailman/listinfo/tutor > > > > > if username in usernames: > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From joel.goldstick at gmail.com Tue Feb 11 06:27:24 2020 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Tue, 11 Feb 2020 06:27:24 -0500 Subject: [Tutor] (no subject) In-Reply-To: References: <9ad7af83-b6d5-8eed-5d91-dbea38e8df92@wichmann.us> Message-ID: On Tue, Feb 11, 2020 at 4:19 AM Akash Mudgil wrote: > > Let's say if you wanted to search for a username in a, list where > 'username' variable is predefined. > > Example: > > > username='BOB' > > usernames=['BOB','Alice','David'] > > for i in range(len(usernames)): > if usernames[i]==username: > print("User exists in the list") > > This program doea a linear search on every index in the list, and if the > value matches, returns the output. The len function gets the total length > of the list, and the range function defines the scope of the search from > beginning to end of the list. > > Hope this helps. While the above method might work, it is a very convoluted way to use python. Finding the length of the list and iterating over the indices is not pythonic. Using the 'if name in names' construct is much more in the spirit of the language, because it doesn't need the user to think about indexes and lengths, which are artifacts, not the information wanted > > On Tue, 11 Feb, 2020, 1:27 AM Mats Wichmann, wrote: > > > On 2/10/20 12:36 PM, Alfie Lee wrote: > > > > > > > > > Sent from Mail for > > Windows 10 > > > > > > If say I had a variable called ?username?, and a list of usernames > > called ?usernames?; how would I write it so that the programme will check > > if that username is in the usernames list? > > > this would be using an if statement if possible. > > > > > > _______________________________________________ > > > Tutor maillist - Tutor at python.org > > > To unsubscribe or change subscription options: > > > https://mail.python.org/mailman/listinfo/tutor > > > > > > > > > if username in usernames: > > > > > > _______________________________________________ > > Tutor maillist - Tutor at python.org > > To unsubscribe or change subscription options: > > https://mail.python.org/mailman/listinfo/tutor > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor -- Joel Goldstick http://joelgoldstick.com/blog http://cc-baseballstats.info/stats/birthdays From __peter__ at web.de Tue Feb 11 04:59:06 2020 From: __peter__ at web.de (Peter Otten) Date: Tue, 11 Feb 2020 10:59:06 +0100 Subject: [Tutor] Searching a list for a value, was Re: (no subject) References: Message-ID: Collisio Adolebitque wrote: > username = 'user3' > usernames = ['user1', 'user2', 'user3', 'user4'] > > > def check_list_for_pattern(user: str, users: list) -> bool: > return True if user in users else False - Note that user in users # use this gives the same result as True if user in users else False # redundant; don't use it - I find your function name misleading. "pattern" usually denotes a regex or glob rather than a string that has to be matched exactly. - I've avoided type annotations so far, but if you do it -- shouldn't users be marked as a list of strings? (A web search suggests List[str]) > print(check_list_for_pattern(username, usernames)) Compared to the clean and simple print(username in usernames) your suggestion looks a bit like overengineering ;) From breamoreboy at gmail.com Tue Feb 11 04:58:18 2020 From: breamoreboy at gmail.com (Mark Lawrence) Date: Tue, 11 Feb 2020 09:58:18 +0000 Subject: [Tutor] (no subject) In-Reply-To: References: <9ad7af83-b6d5-8eed-5d91-dbea38e8df92@wichmann.us> Message-ID: On 11/02/2020 00:18, Akash Mudgil wrote: > Let's say if you wanted to search for a username in a, list where > 'username' variable is predefined. > > Example: > > > username='BOB' > > usernames=['BOB','Alice','David'] > > for i in range(len(usernames)): > if usernames[i]==username: > print("User exists in the list") > > This program doea a linear search on every index in the list, and if the > value matches, returns the output. The len function gets the total length > of the list, and the range function defines the scope of the search from > beginning to end of the list. > > Hope this helps. > Not really as it's poor python. There is no need for the 'range(len(usernames))' construct, simply, as has all ready been said:- for user in usernames: if user == username: print('found) -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From PyTutor at DancesWithMice.info Tue Feb 11 16:45:40 2020 From: PyTutor at DancesWithMice.info (David L Neil) Date: Wed, 12 Feb 2020 10:45:40 +1300 Subject: [Tutor] Searching a list for a value, was Re: (no subject) In-Reply-To: References: Message-ID: Apologies to OP, in that somewhat different topical interest. On 11/02/20 10:59 PM, Peter Otten wrote: ... > your suggestion looks a bit like overengineering ;) It's a common problem in training. On the one hand we (trainers) try to gently guide trainees 'into' a solution - IIRC the OP even said 'has to use an if-statement (albeit your (advanced Python) solution both cleaner and more efficient!). On the other, this sort of 'hinting' (and likely the use of published rubrics) does seem to 'steer' literal-minded people to limit their review of the material covered to-date. (Nothing new/out-of-the-ordinary: as students, many of us develop(ed) the strategy to trying to double-guess what the tutor/examiner was 'expecting', and customising our answers accordingly!) Thus, thinking along such lines as: 'I should include an if, a for, and show how advanced I am with a context manager...' These days, and not necessarily in-agreement with colleagues, I'm inclined against offering specific "use an if" advice - in the same way that when starting to develop a system one is wise not to impose artificial limits or pre-judge 'the best' solution (as in "must involved 'xyz' package/system/hardware/latest-shiny-object")! If trainees exhibit anxiety, then the course is to ask some questions which invite thinking about the material-covered. (and part of the 'learning contract' is to NEVER set a challenge which requires 'stuff' that hasn't been covered - although within that guideline, many trainees are surprised to discover that "reading" is part of their learning, and therefore "covered"!) Do you have other ideas or advice? I'll be interested to consider... Disclaimer: my training is in topics other than Python. -- Regards =dn From david at graniteweb.com Tue Feb 11 17:10:55 2020 From: david at graniteweb.com (David Rock) Date: Tue, 11 Feb 2020 16:10:55 -0600 Subject: [Tutor] Searching a list for a value, was Re: (no subject) In-Reply-To: References: Message-ID: > On Feb 11, 2020, at 15:45, David L Neil via Tutor wrote: > > These days, and not necessarily in-agreement with colleagues, I'm inclined against offering specific "use an if" advice - in the same way that when starting to develop a system one is wise not to impose artificial limits or pre-judge 'the best' solution (as in "must involved 'xyz' package/system/hardware/latest-shiny-object?)! While that?s a nice idealistic viewpoint, it doesn?t change the fact you have to start somewhere. Starting with ?use an if? is no better/worse than any other option when you have to produce something. The very nature of writing code implies pre-judgement; you _must_ pick something, or nothing gets written. In the case of the OP, "using an if" was part of the requirements, for better or worse; hence the "suggestions.? ? David Rock david at graniteweb.com From PyTutor at danceswithmice.info Tue Feb 11 17:24:48 2020 From: PyTutor at danceswithmice.info (DL Neil) Date: Wed, 12 Feb 2020 11:24:48 +1300 Subject: [Tutor] Searching a list for a value, was Re: (no subject) In-Reply-To: References: Message-ID: <1f0ff990-d43c-01ac-d659-58f0abfeec10@DancesWithMice.info> On 12/02/20 11:10 AM, David Rock wrote: > >> On Feb 11, 2020, at 15:45, David L Neil via Tutor wrote: >> >> These days, and not necessarily in-agreement with colleagues, I'm inclined against offering specific "use an if" advice - in the same way that when starting to develop a system one is wise not to impose artificial limits or pre-judge 'the best' solution (as in "must involved 'xyz' package/system/hardware/latest-shiny-object?)! > > While that?s a nice idealistic viewpoint, it doesn?t change the fact you have to start somewhere. Starting with ?use an if? is no better/worse than any other option when you have to produce something. The very nature of writing code implies pre-judgement; you _must_ pick something, or nothing gets written. Training is largely idealistic - at least its educational component. If you've just completed a learning session entitled or involving "if", doesn't it follow that the session's assignment will involve same? > In the case of the OP, "using an if" was part of the requirements, for better or worse; hence the "suggestions.? The intent was not to debate the OP's constraints - out of his/her control; but to ask if such 'advice' is actually helpful, because of the very nature of writing code. NB some posts/suggested-solutions did not include 'if', which raises the question: if a trainee does not use 'if', is that a bad thing (wrt to the objectives of that learning-session), or is it a great thing ((s)he has figured-out a perfectly valid solution to the problem)? -- Regards =dn From david at graniteweb.com Tue Feb 11 17:33:56 2020 From: david at graniteweb.com (David Rock) Date: Tue, 11 Feb 2020 16:33:56 -0600 Subject: [Tutor] Searching a list for a value, was Re: (no subject) In-Reply-To: <1f0ff990-d43c-01ac-d659-58f0abfeec10@DancesWithMice.info> References: <1f0ff990-d43c-01ac-d659-58f0abfeec10@DancesWithMice.info> Message-ID: <46D10569-9ADC-4079-A37F-81BD0D9F5413@graniteweb.com> > On Feb 11, 2020, at 16:24, DL Neil via Tutor wrote: > > Training is largely idealistic - at least its educational component. > > If you've just completed a learning session entitled or involving "if", doesn't it follow that the session's assignment will involve same? yes. >> In the case of the OP, "using an if" was part of the requirements, for better or worse; hence the "suggestions.? > > The intent was not to debate the OP's constraints - out of his/her control; but to ask if such 'advice' is actually helpful, because of the very nature of writing code. > > NB some posts/suggested-solutions did not include 'if', which raises the question: if a trainee does not use 'if', is that a bad thing (wrt to the objectives of that learning-session), or is it a great thing ((s)he has figured-out a perfectly valid solution to the problem)? The answer to that is largely based on context. In the context of learning something specific (eg, use ?if?), it is a failure if the task is not completed as directed. Following directions is at least as important as ?finding another way to do something.? You are testing a _specific_ skill, not looking for ?out of the box? creativity. If you complete the task and do not use the prescribed method, there is no way to tell if you understand the lesson. ? David Rock david at graniteweb.com From __peter__ at web.de Wed Feb 12 04:36:11 2020 From: __peter__ at web.de (Peter Otten) Date: Wed, 12 Feb 2020 10:36:11 +0100 Subject: [Tutor] Searching a list for a value, was Re: (no subject) References: Message-ID: David L Neil via Tutor wrote: > Apologies to OP, in that somewhat different topical interest. > > > On 11/02/20 10:59 PM, Peter Otten wrote: > ... > >> your suggestion looks a bit like overengineering ;) > > It's a common problem in training. > > On the one hand we (trainers) try to gently guide trainees 'into' a > solution - IIRC the OP even said 'has to use an if-statement (albeit > your (advanced Python) solution both cleaner and more efficient!). I was addressing Collisio, not the OP. IMO Collisio's solution is deficient, and a beginner like the OP is unlikely to recognize that, under its pomp and circumstance ;) Had Collisio suggested something like def list_contains_value(items, value): for item in items: if item == value: return True return False that would have been fine by me. It uses basic constructs like if... and for..., and even if the OP has not been introduced to functions yet they will probably follow soon. You can see pythonic iteration and can argue about what happens as the list grows (big O). You can also have a look at duck typing: what happens if you pass a string or set instead of a list? Is it a good idea to use the function with sets? In short, a beginner can learn a lot from code that no one who is a bit more advanced would write or use. > On the other, this sort of 'hinting' (and likely the use of published > rubrics) does seem to 'steer' literal-minded people to limit their > review of the material covered to-date. > > (Nothing new/out-of-the-ordinary: as students, many of us develop(ed) > the strategy to trying to double-guess what the tutor/examiner was > 'expecting', and customising our answers accordingly!) > > Thus, thinking along such lines as: 'I should include an if, a for, and > show how advanced I am with a context manager...' > > > These days, and not necessarily in-agreement with colleagues, I'm > inclined against offering specific "use an if" advice - in the same way > that when starting to develop a system one is wise not to impose > artificial limits or pre-judge 'the best' solution (as in "must involved > 'xyz' package/system/hardware/latest-shiny-object")! > > If trainees exhibit anxiety, then the course is to ask some questions > which invite thinking about the material-covered. > (and part of the 'learning contract' is to NEVER set a challenge which > requires 'stuff' that hasn't been covered - although within that > guideline, many trainees are surprised to discover that "reading" is > part of their learning, and therefore "covered"!) > > Do you have other ideas or advice? I'll be interested to consider... > > > Disclaimer: my training is in topics other than Python. From phua26565 at yahoo.com.sg Tue Feb 11 21:17:47 2020 From: phua26565 at yahoo.com.sg (Phua KT) Date: Wed, 12 Feb 2020 02:17:47 +0000 (UTC) Subject: [Tutor] Question regarding import References: <15711354.1377476.1581473867992.ref@mail.yahoo.com> Message-ID: <15711354.1377476.1581473867992@mail.yahoo.com> Hi Sir, I was trying to work on a tutorial that requires to import time_series module. But I have the error message 'ModuleNotFoundError' generated when i run my scripts. Can you kindly advise?Thank you. Phua KT From alan.gauld at yahoo.co.uk Wed Feb 12 05:53:29 2020 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 12 Feb 2020 10:53:29 +0000 Subject: [Tutor] Question regarding import In-Reply-To: <15711354.1377476.1581473867992@mail.yahoo.com> References: <15711354.1377476.1581473867992.ref@mail.yahoo.com> <15711354.1377476.1581473867992@mail.yahoo.com> Message-ID: On 12/02/2020 02:17, Phua KT via Tutor wrote: > Hi Sir, > I was trying to work on a tutorial that requires to import time_series module. That's not part of the standard library so you need to install it. It might be part of a bigger package such as numpy, but you need to install something beyond the basic standard library. Please post the full error trace BTW as it holds a lot of useful details that might help diagnose the issue. > But I have the error message 'ModuleNotFoundError' generated when i run my scripts. It says it can't find the module, possibly because it is not installed? I did a quick Google search for time_series but it came up blank. Does your tutorial provide the code? If so you may have to manually install the module somewhere that python can see it - somewhere in your sys.path or PYTHONPATH settings. -- 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 From nathan.delboux at gmail.com Thu Feb 13 20:02:50 2020 From: nathan.delboux at gmail.com (Nathan D'Elboux) Date: Fri, 14 Feb 2020 12:02:50 +1100 Subject: [Tutor] Monitoring directories Message-ID: ________________________________ Hi Pythonistas! I have a little script below that copies files from a directory and all of its sub directories over to a target dir. The target dir doesnt have any sub dir's and is exactly how i want it. The copy checks if the file in the source dir exists in the target dir and then copies if it doesnt. Problem is is that i have another utility monitoring that target dir moving the files out to process them further so as it processes them the target dir will always be empty thus coping every file from source folder over and over depending on interval i run the script What i would like to do is change my below script from checking if sourcedir filename exists in target dir, is to change it so it monitors the source dir and copies the newest files over to the target dir. As this script is invoked currently via a cron job im not familiar with what i would need to do to make this python script monitor in daemon mode or how it would know the newest files and the fact it haunt already copied them over. The script i have already is below. Just looking for feedback on how i should improve this. Would using something like Pyinotify work best in this situation? import shutil import os targetdir = "/home/target_dir" sourcedir = "/home/source/" dirlist = os.listdir(sourcedir) for x in dirlist : directory = sourcedir + x + '/' filelist = os.listdir(directory) for file in filelist : if not os.path.exists(os.path.join(targetdir,file)): shutil.copy(directory+file,targetdir) Thanks in advance for any advice Nathan From alan.gauld at yahoo.co.uk Fri Feb 14 03:11:52 2020 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 14 Feb 2020 08:11:52 +0000 Subject: [Tutor] Monitoring directories In-Reply-To: References: Message-ID: On 14/02/2020 01:02, Nathan D'Elboux wrote: > As this script is invoked currently via a cron job im not familiar > with what i would need to do to make this python script monitor in > daemon mode or how it would know the newest files and the fact it > haunt already copied them over. Store the copied filenames in a file perhaps? Either that or just store the time of the last run in a file. Then check the file creation date against the last run time and if newer copy the file. -- 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 From mats at wichmann.us Fri Feb 14 06:29:23 2020 From: mats at wichmann.us (Mats Wichmann) Date: Fri, 14 Feb 2020 04:29:23 -0700 Subject: [Tutor] Monitoring directories In-Reply-To: References: Message-ID: <65d469eb-c54d-3f28-9fcb-f0da9c60667b@wichmann.us> On 2/14/20 1:11 AM, Alan Gauld via Tutor wrote: > On 14/02/2020 01:02, Nathan D'Elboux wrote: > >> As this script is invoked currently via a cron job im not familiar >> with what i would need to do to make this python script monitor in >> daemon mode or how it would know the newest files and the fact it >> haunt already copied them over. > > Store the copied filenames in a file perhaps? > Either that or just store the time of the last run in a file. Then check > the file creation date against the last run time and if newer copy the file. > If you're going to stay with a cron job, the latter is the common trick... you don't need to store anything in your timestamp file, just "touch" it every time you've run and look only at files that have a later modification time than the stamp file. You may want to take a look at the underappreciated pathlib module if you're going to be doing this kind of twiddling with paths - it has support for a lot of useful things,like getting the stat of a file, doing a touch, globbing, path joining, etc. For example, off the top of my head, probably riddled with errors: from pathlib import Path trg = Path("/home/target_dir") src = Path("/home/source/") stampfile = src / ".lastrun" try: stamp = stampfile.stat().st_mtime except FileNotFoundError: stamp = 0 # make a list of all the files in src, descending the tree: filelist = [f for f in src.glob('**/*') if f.is_file()] for f in filelist: if f.stat().st_mtime > stamp: # copy the file stampfile.touch() If you do want to go with a daemon, and use inotify, there's a PEP for the daemon stuff (3143) and a sample implementation: https://pypi.org/project/python-daemon/ Often it turns out you don't need a daemon. But... it depends. From cs at cskk.id.au Fri Feb 14 16:36:25 2020 From: cs at cskk.id.au (Cameron Simpson) Date: Sat, 15 Feb 2020 08:36:25 +1100 Subject: [Tutor] Monitoring directories In-Reply-To: References: Message-ID: <20200214213625.GA50328@cskk.homeip.net> On 14Feb2020 12:02, Nathan D'Elboux wrote: >I have a little script below that copies files from a directory and >all of its sub directories over to a target dir. The target dir doesnt >have any sub dir's and is exactly how i want it. The copy checks if >the file in the source dir exists in the target dir and then copies if >it doesnt. > >Problem is is that i have another utility monitoring that target dir >moving the files out to process them further so as it processes them >the target dir will always be empty thus coping every file from source >folder over and over depending on interval i run the script > >What i would like to do is change my below script from checking if >sourcedir filename exists in target dir, is to change it so it >monitors the source dir and copies the newest files over to the target >dir. Might I suggest that you rename the files in the source directly when they have been copied? This light be as simple as moving them (shutil.move) from the source directory to a parallel "processed" directory. If the source directory is your serious read only main repository of source files and you do not want to modify it, perhapsyou should maintain a parallel "to process" source tree beside it; have the thing which puts things into the main source directory _also_ hard link them into the "to process" (or "spool") tree, which you are free to remove things from after the copy. Either of these removes your need to keep extra state or play guesswork with file timestamps. >As this script is invoked currently via a cron job im not familiar >with what i would need to do to make this python script monitor in >daemon mode or how it would know the newest files and the fact it >haunt already copied them over. Because cron runs repeatedly you tend not to run things in "daemon" mode from them - daemons start once and continue indefinitely to provide whatever service they perform. >The script i have already is below. Just looking for feedback on how i >should improve this. Would using something like Pyinotify work best >in this situation? Pyinotify would suit a daemon mode script (started not from cron but maybe the "at" command) as it relies on reporting changes. It is perfectly valid and reasonable though, you just wouldn't start it from cron (unless the crontab were to start it if it was no longer running, which would require an "is it running?" check). >import shutil >import os > >targetdir = "/home/target_dir" >sourcedir = "/home/source/" > >dirlist = os.listdir(sourcedir) >for x in dirlist : > >directory = sourcedir + x + '/' Consider using os.path.join for the "+ x" bit. >filelist = os.listdir(directory) > for file in filelist : > if not os.path.exists(os.path.join(targetdir,file)): >shutil.copy(directory+file,targetdir) The other component missing here is that scripts like this are prone to copying a source file before it is complete i.e. as soon as its name shows in the source tree, not _after_ all the data have been copied into it. You will have a similar problem in the target directory with whatever is processing things there. Tools like rsync perform this process by copying new files to a temporary name in the target tree beginning with a '.', eg .tempfile-blah, and only renaming the file to the real name when the copy of data is complete. You should consider adapting your script to use a similar mode, both for the copy to the target ("copy to a tempfile, then rename") and also for the scan of the source (ignore filenames which start with a '.', thus supporting such a scheme for the delivery to the source directory). Cheers, Cameron Simpson From martin at linux-ip.net Sat Feb 15 04:08:41 2020 From: martin at linux-ip.net (Martin A. Brown) Date: Sat, 15 Feb 2020 01:08:41 -0800 Subject: [Tutor] Monitoring directories In-Reply-To: References: Message-ID: Hello, Filesystems are wonderful and powerful. (Hardware disclaimer [0].) Filesystems can also be tricky. There are conventions for non-racy interactions with filesystems. Fundamental items: * writing to a filesystem can have all sorts of problems * there are a few filesystem calls that are atomic So, if you are doing lots of work with a filesystem, learn how to take advantage of the atomic operations to minimize your exposure to races and significantly reduce your risk of data loss (due to software). Let's say I run a family grocery for hungry giraffes. >I have a little script below that copies files from a directory and >all of its sub directories over to a target dir. The target dir doesnt >have any sub dir's and is exactly how i want it. The copy checks if >the file in the source dir exists in the target dir and then copies if >it doesnt. This seems deceptively simple. So, maybe this is ./conveyer.py (because it conveys files from the source tree of subdirectories to a single target directory....) Problem 1: What happens if a file under source/a/monkey.txt exists and there's also file source/b/monkey.txt ? It's possible that you know your data and/or applications and this would never happen,...but it is certainly possible from a filesystem perspective. Problem 2: How do you know the input file in sourcedir is complete? Has it been written successfully? (Is shelf-stocker.py writing files atomically?, see below) (Cameron Simpson mentioned this, problem, too.) >Problem is is that i have another utility monitoring that target dir >moving the files out to process them further so as it processes them >the target dir will always be empty thus coping every file from source >folder over and over depending on interval i run the script Yes! And, that makes the job of the program feeding data into the target dir even a bit harder, as you have already figured out. What happens if hungry-giraffe.py consumes the file out of target directory. How is conveyer.py going to know not to copy from sourcedir into target dir? Problem 3: How do you know if you have copied the file already, especially if there's a hungry-giraffe.py eating the files out of the target directory? >What i would like to do is change my below script from checking if >sourcedir filename exists in target dir, is to change it so it >monitors the source dir and copies the newest files over to the >target dir. A filesystem makes some guarantees that can be quite useful. There are some operations which are atomic on filesystems. The Python call os.link() and os.unlink() are my favorites. These are the equivalent of renaming (link) and deleting (unlink) files. For example, if an os.link() call returns success, your file is now available with the new name. And, if an os.unlink() file returns successfully, you have removed the file. Safely handling files in a non-racy way requires care, but pretty reliable software can be written by trusting the filesystem atomicity guarantees afforded by these functions. In copying data from one file (or filesystem) to another, writing directly memory from memory to disk, or generating output programmatically can often yield an error. Maybe the disk gets full, and you can't write any more. Maybe, you write too many bytes to a single file, and it can't get larger. Maybe, you cannot open the file to write because of permissions issue or inode exhaustion. (My inodes just flew in from Singapore, and they are very tired.) In these (admittedly rare cases), you don't know where in the attempted filesystem operation, f.write() or f.sync() whether the data you just put into the file are correct. So, what's a concerned software developer to do? Here's an old technique (30+ years?) for taking advantage of filesystem atomicity to make sure you safely create / copy files. - open up a temporary file, with a temporary name in your output directory (either in the same directory, or if you are paranoid / writing large numbers of files simultaneously, in a directory on the same filesystem; see Maildir description for details [1]) (If error: delete tempfile, bail, crash, try again or start over) - write every bit of data from memory (and check for errors on your f.write() or print() calls) or, copy the data from the input file to the tempfile (If error: delete tempfile, bail, crash, try again or start over) - close the tempfile (If error: delete tempfile, bail, crash, try again or start over) - os.link() the tempfile to the full name (If error: delete tempfile, bail, crash, try again or start over) - os.unlink() the tempfile (Programmer judgement: log the error and ... ignore or crash, depending on what your preference is; if you have a separate directory for your tempfiles, you can often just log and ignore this and have a separate sweeping agent clean up the cruft) So, putting that together (conceptually, only, at this point), and assuming that the shelf-stocker.py (which puts things into the input directory) and conveyer.py (which puts things into the target directory for the giraffes) both use the above filesystem handling techniques, then you have solved Problem 2. If you write the file with a tempfile name, you'll sidestep problem 1. If you want to keep the original name, but use a tempfile technique, you can do: # -- write data, avoid errors, if errors, bail and delete # log and crash or maybe just loop and try again later ... # whatever your application wants to do # target_tempfile.close() # -- get a guaranteed, non-colliding, new filename # tf = tempfile.NamedTemporaryFile(dir=".", prefix=fname, delete=False) tf.close() # -- now link right over top of the just created tf # os.link(target_tempfile, tf.name) # -- remove the target_tempfile # os.unlink(target_tempfile) The downside to solving problem 2 as I suggest above is that you can't tell by the presence of a file in target_dir, whether a file has been copied. In order to detect, you would absolutely have to use some state file. So, as you may imagine, now, it's safer to either A) duplicate the source directory hierarchy exactly into the target directory (so you don't have to worry about name collisions) or B) settle on a single directory for each source and target directory. Problem 3 can be solved (as Alan Gauld pointed out) by storing the filenames you have seen in a file. Or, you can try to take advantage of the filesystem's atomicity guarantees for this too, so that you know which files your conveyer.py has copied from the source directory to the target directory for the hungry-giraffe.py. How? source: dir with input files (sub-tree complicates) target: dir for the giraffes to browse on state: dir to track which input files have been copied Since the os.symlink() command is an atomic filesystem operation, too, you can use that to create a file in the state directory to that points to any file your conveyer.py program has already handled. (Note, this does leave a very small race in the program.) - copy file from source to temfile in target, then os.link() to final name in target - use os.symlink() to create symlink in state that points to file in source dir When you introduce the state directory, though, you have introduced another race, because it's easy to imagine the conveyer.py program crashing after it has completed feeding the giraffes, but before it has updated its state directory. So, upon next execution, conveyer.py will move the same file to the target_dir again. >As this script is invoked currently via a cron job im not familiar >with what i would need to do to make this python script monitor in >daemon mode or how it would know the newest files and the fact it >haunt already copied them over. Or loop forever? while True: run_conveyer_logic( ... ) if not loop: break time.sleep(30) # -- sleep 30 seconds, and run loop again >The script i have already is below. Just looking for feedback on how i >should improve this. Would using something like Pyinotify work best >in this situation? > >import shutil >import os > >targetdir = "/home/target_dir" >sourcedir = "/home/source/" > >dirlist = os.listdir(sourcedir) >for x in dirlist : > directory = sourcedir + x + '/' > > filelist = os.listdir(directory) > for file in filelist : > if not os.path.exists(os.path.join(targetdir,file)): > shutil.copy(directory+file,targetdir) Note that shutil is already taking care of a great deal of the heavy lifting of safely interacting with the filesystem for you, so you you could get away with simply using it as you have. Anytime you are writing a file (including using shutil.copy), there's a risk that there will be a problem completely writing the file to disk. If so, the safest thing to do is to delete the file and start over. This is why the pattern of write to a temporary file exists -- and then once you are sure the data are written safely in that file, you can atomically link the file into place (with os.link()). Anytime you are working with a filesystem, it's also good to review the entire sequence of open(), f.write(), f.close(), os.link() calls for racy interactions. (What happens if two instances of the same program are running at the same time? What happens if somebody accidentally blows away a state directory? What happens if the target directory is not there? Are temporary files cleaned up after encountering an error? Many of us have filled up disks with programs that ran amok generating temporary files in loops.) That is all. I wish you good luck with your grocery business and hope that it conveys you to your goals, and keeps your giraffes as happy as mine, -Martin [0] There was a recent discussion here about the eventually inevitable failure or corruption of data, files, RAM on any computer. However, for the purposes of my answer, I'll assume that the physical constraints of our modest universe do not apply, which is to say, that I'll answer your question as though this hardware failure or data corruption is not possible. By making this assumption, we can remain squarely in the realm of high quality hardware. Accurate: https://mail.python.org/pipermail/tutor/2020-January/115993.html Pragmatic: https://mail.python.org/pipermail/tutor/2020-January/115992.html [1] https://en.wikipedia.org/wiki/Maildir -- Martin A. Brown http://linux-ip.net/ From deepakdixit0001 at gmail.com Sat Feb 15 10:08:15 2020 From: deepakdixit0001 at gmail.com (Deepak Dixit) Date: Sat, 15 Feb 2020 20:38:15 +0530 Subject: [Tutor] How to know the internal execution flow of class Message-ID: Hi Tutors, What happens when we create a class or function. In other way, I want to know the life-cycle of class. Lets see an example: ######### Script Start ######### class MClass(type): def __new__(cls, *args): print 'New from MClass', args[0] return super(MClass, cls).__new__(cls, *args) class A: print 'Class A defined' __metaclass__ = MClass def __init__(self, *args): print 'Init from A', args def __new__(cls, *args): print 'New from A', args return super(A, cls).__new__(cls, *args) print 'Class A definition end' a = A() print 'Object created: A' class B: print 'Class B defined' __metaclass__ = MClass def __init__(self, *args): print 'Init from B', args def __new__(cls, *args): print 'New from B', args print 'Class B definition end' b = B() print 'Object created: B' ######### Script End ######### ################# Output ############ Class A defined New from MClass A Class A definition end New from A () Init from A () Object created: A Class B defined New from MClass B Class B definition end New from B () Object created: B ################# Output End ############ >From this example , we can see that when we are defining any class ( not creating object), it call some methods from the metaclass. If I am returning something from __new__ then it call __init__ of current class otherwise it skips the calling even when I am creating object. Now suppose that If I never assigned any metaclass manually then I will not be aware that something is happening when I am defining the class. How can I know these internals? -- *With Regards,* *Deepak Kumar Dixit* From mats at wichmann.us Sat Feb 15 12:45:41 2020 From: mats at wichmann.us (Mats Wichmann) Date: Sat, 15 Feb 2020 10:45:41 -0700 Subject: [Tutor] How to know the internal execution flow of class In-Reply-To: References: Message-ID: <787c3ca3-c366-2582-9b08-a3724279c7da@wichmann.us> On 2/15/20 8:08 AM, Deepak Dixit wrote: > Hi Tutors, > > What happens when we create a class or function. In other way, I want to > know the life-cycle of class. Lets see an example: > > ######### Script Start ######### > > class MClass(type): > def __new__(cls, *args): > print 'New from MClass', args[0] > return super(MClass, cls).__new__(cls, *args) > > class A: > print 'Class A defined' > __metaclass__ = MClass > > def __init__(self, *args): > print 'Init from A', args > > def __new__(cls, *args): > print 'New from A', args > return super(A, cls).__new__(cls, *args) > > print 'Class A definition end' > a = A() > print 'Object created: A' > > class B: > print 'Class B defined' > __metaclass__ = MClass > > def __init__(self, *args): > print 'Init from B', args > > def __new__(cls, *args): > print 'New from B', args > > print 'Class B definition end' > b = B() > print 'Object created: B' > > ######### Script End ######### > > ################# Output ############ > Class A defined > New from MClass A > Class A definition end > New from A () > Init from A () > Object created: A > Class B defined > New from MClass B > Class B definition end > New from B () > Object created: B > > ################# Output End ############ > From this example , we can see that when we are defining any class ( not > creating object), it call some methods from the metaclass. If I am > returning something from __new__ then it call __init__ of current class > otherwise it skips the calling even when I am creating object. Now suppose > that If I never assigned any metaclass manually then I will not be aware > that something is happening when I am defining the class. > > How can I know these internals? First off here's a moderately famous quote from one of the most famous Python developers: ?Metaclasses are deeper magic than 99% of users should ever worry about. If you wonder whether you need them, you don?t (the people who actually need them know with certainty that they need them, and don?t need an explanation about why).? ? Tim Peters Ignoring any niggles about Python 2 and old-style classes, if you don't define a metaclass, your class has one anyway: it's "type". So this same flow happens for every class statement. Every callable object has a special method __call__ which is invoked when you "call" that object, a metaclass is no different. A metaclass is a template for creating a class similar to how a class is a template for creating an object - you call it and you get the object back. Thus when you create class A or B, the metaclass is called - by invoking __call__, which invokes the __new__ and __init__ methods, and you see your message from the metaclass's init. It you don't provide a metaclass (which inherits from type), or don't provide those methods, then the ones from type are called as with any inheritance relationship. Since you can't muck with type for safety reasons, inserting a custom metaclass is useful for experimentation, as you've done. For the rest, see Tim's quote :) Seriously, did that clear any of the confusion up? Btw. the more preferred form now is: class A(metaclass=MClass): print 'Class A defined' as opposed to defining __metaclass__ P.S. - stuff you didn't ask for: You can create a class by calling type directly, by the way; the magic of the class statement is you don't have to do that, since it automatically calls type (or any metaclass you supply). Here's something I had sitting around from a tutorial I did for someone. Maybe it will amplify a little more? Here are the pieces of what we think of as a Python class: # some methods def transact(acct, amount): acct.balance += amount def pay_interest(acct): acct.balance += acct.balance * acct.interest_rate def account_init(acct, num, name, bal, rate): acct.acct_number = num acct.acct_holder = name acct.balance = bal acct.interest_rate = rate # a mapping for methods and attributes: account = { "acct_number": "", "acct_holder": "", "balance": 0.0, "interest_rate": 0.0, "transact": transact, "pay_interest": pay_interest, "__init__": account_init, } # and create it with name, parents (none), mapping: AccountType = type("AccountType", (), account) # that gives the same result as this: class AccountType(): def transact(self, amount): self.balance += amount def pay_interest(self): self.balance += self.balance * self.interest_rate def __init__(self, num, name, bal, rate): self.acct_number = num self.acct_holder = name self.balance = bal self.interest_rate = rate # you can call either AccountType to create an instance - they're the same thing! From phillor9 at gmail.com Sat Feb 15 19:42:22 2020 From: phillor9 at gmail.com (Phil) Date: Sun, 16 Feb 2020 11:12:22 +1030 Subject: [Tutor] Executing shell commands Message-ID: <238b9830-4b0d-cefc-e322-44eef43e184b@gmail.com> Thank you for reading this. Python3 on Linux. I'm looking for ideas on how I might collect some .gpx files and then upload them to a serial device as a batch via a for-loop mechanism. Uploading the files one at a time is quite straight forward, no problems there. I thought I might start with something like this and then collect the file names in a list and then upload them one by one. However, I'm missing a step from the listing of the file names and then collecting them. Plus the following fails with a directory does not exist error even though executing the same command "ls /home/phil/Downloads/*.gpx" from the command line shows that there isn't a problem. import subprocess subprocess.call(["ls", "/home/phil/Downloads/*.gpx"]) The above fails but the following is OK. subprocess.call(["ls", "/home/phil/Downloads/GC1BPG4.gpx"]) Any suggestion on how I might at least make a start will be greatly appreciated. -- Regards, Phil From robertvstepp at gmail.com Sat Feb 15 21:17:53 2020 From: robertvstepp at gmail.com (boB Stepp) Date: Sat, 15 Feb 2020 20:17:53 -0600 Subject: [Tutor] Executing shell commands In-Reply-To: <238b9830-4b0d-cefc-e322-44eef43e184b@gmail.com> References: <238b9830-4b0d-cefc-e322-44eef43e184b@gmail.com> Message-ID: On Sat, Feb 15, 2020 at 6:42 PM Phil wrote: > I thought I might start with something like this and then collect the > file names in a list and then upload them one by one. However, I'm > missing a step from the listing of the file names and then collecting > them. Plus the following fails with a directory does not exist error > even though executing the same command "ls /home/phil/Downloads/*.gpx" > from the command line shows that there isn't a problem. > > import subprocess > subprocess.call(["ls", "/home/phil/Downloads/*.gpx"]) If I am interpreting the docs correctly (https://docs.python.org/3/library/subprocess.html#frequently-used-arguments) in order to expand filename wildcards you have to add "shell=True": subprocess.call("ls", "/home/phil/Downloads/*.gpx", shell=True) # I don't think you need the square brackets. Also, if you have Python 3.5 or greater the docs recommend using subprocess.run(). The link that I provided is using subprocess.run(), though I think adding "shell=True" will work for subprocess.call() as well. HTH! -- boB From phillor9 at gmail.com Sat Feb 15 22:46:28 2020 From: phillor9 at gmail.com (Phil) Date: Sun, 16 Feb 2020 14:16:28 +1030 Subject: [Tutor] Executing shell commands In-Reply-To: References: <238b9830-4b0d-cefc-e322-44eef43e184b@gmail.com> Message-ID: <34bbe45a-2df8-85fd-b063-3b417bbbffd8@gmail.com> On 16/2/20 12:47 pm, boB Stepp wrote: Thank you Bob for your reply. > subprocess.call("ls", "/home/phil/Downloads/*.gpx", shell=True) Adding shell=True to .call() or .run() generates the following: Traceback (most recent call last): File "/home/phil/Python/waypoints.py", line 6, in subprocess.run("ls", "/home/phil/Downloads/*.gpx", shell=True) File "/usr/lib/python3.7/subprocess.py", line 488, in run with Popen(*popenargs, **kwargs) as process: File "/usr/lib/python3.7/subprocess.py", line 702, in __init__ raise TypeError("bufsize must be an integer") TypeError: bufsize must be an integer Anyway, you've given me something to think about, I'll experiment further. -- Regards, Phil From robertvstepp at gmail.com Sat Feb 15 23:14:39 2020 From: robertvstepp at gmail.com (boB Stepp) Date: Sat, 15 Feb 2020 22:14:39 -0600 Subject: [Tutor] Executing shell commands In-Reply-To: <34bbe45a-2df8-85fd-b063-3b417bbbffd8@gmail.com> References: <238b9830-4b0d-cefc-e322-44eef43e184b@gmail.com> <34bbe45a-2df8-85fd-b063-3b417bbbffd8@gmail.com> Message-ID: On Sat, Feb 15, 2020 at 9:46 PM Phil wrote: > > On 16/2/20 12:47 pm, boB Stepp wrote: > Thank you Bob for your reply. > > subprocess.call("ls", "/home/phil/Downloads/*.gpx", shell=True) > Adding shell=True to .call() or .run() generates the following: > > Traceback (most recent call last): > File "/home/phil/Python/waypoints.py", line 6, in > subprocess.run("ls", "/home/phil/Downloads/*.gpx", shell=True) > File "/usr/lib/python3.7/subprocess.py", line 488, in run > with Popen(*popenargs, **kwargs) as process: > File "/usr/lib/python3.7/subprocess.py", line 702, in __init__ > raise TypeError("bufsize must be an integer") > TypeError: bufsize must be an integer > > Anyway, you've given me something to think about, I'll experiment further. Apparently your original approach of making a list of arguments is the correct one, and I was sadly mistaken. You *do* need the square brackets. Try that and hopefully all will be well. -- boB From mats at wichmann.us Sat Feb 15 23:34:56 2020 From: mats at wichmann.us (Mats Wichmann) Date: Sat, 15 Feb 2020 21:34:56 -0700 Subject: [Tutor] Executing shell commands In-Reply-To: <34bbe45a-2df8-85fd-b063-3b417bbbffd8@gmail.com> References: <238b9830-4b0d-cefc-e322-44eef43e184b@gmail.com> <34bbe45a-2df8-85fd-b063-3b417bbbffd8@gmail.com> Message-ID: <7E297906-6267-46F7-B46C-36FEE17B207C@wichmann.us> On February 15, 2020 8:46:28 PM MST, Phil wrote: >On 16/2/20 12:47 pm, boB Stepp wrote: >Thank you Bob for your reply. >> subprocess.call("ls", "/home/phil/Downloads/*.gpx", shell=True) >Adding shell=True to .call() or .run() generates the following: > >Traceback (most recent call last): > File "/home/phil/Python/waypoints.py", line 6, in > subprocess.run("ls", "/home/phil/Downloads/*.gpx", shell=True) > File "/usr/lib/python3.7/subprocess.py", line 488, in run > with Popen(*popenargs, **kwargs) as process: > File "/usr/lib/python3.7/subprocess.py", line 702, in __init__ > raise TypeError("bufsize must be an integer") >TypeError: bufsize must be an integer > >Anyway, you've given me something to think about, I'll experiment >further. you dont need to run an external command to collect the files, look at the glob module and even more apropos the pathlib module. -- Sent from a mobile device with K-9 Mail. Please excuse my brevity. From phillor9 at gmail.com Sun Feb 16 01:37:41 2020 From: phillor9 at gmail.com (Phil) Date: Sun, 16 Feb 2020 17:07:41 +1030 Subject: [Tutor] Executing shell commands In-Reply-To: <7E297906-6267-46F7-B46C-36FEE17B207C@wichmann.us> References: <238b9830-4b0d-cefc-e322-44eef43e184b@gmail.com> <34bbe45a-2df8-85fd-b063-3b417bbbffd8@gmail.com> <7E297906-6267-46F7-B46C-36FEE17B207C@wichmann.us> Message-ID: On 16/2/20 3:04 pm, Mats Wichmann wrote: > On February 15, 2020 8:46:28 PM MST, Phil wrote: > you dont need to run an external command to collect the files, look at > the glob module and even more apropos the pathlib module. OK, thank you Mats. I had a brief look at the pathlib module but didn't think that it suited my needs, I'll have another look. I only want the file names at this stage, not the contents. -- Regards, Phil From phillor9 at gmail.com Sun Feb 16 02:54:50 2020 From: phillor9 at gmail.com (Phil) Date: Sun, 16 Feb 2020 18:24:50 +1030 Subject: [Tutor] Executing shell commands In-Reply-To: <7E297906-6267-46F7-B46C-36FEE17B207C@wichmann.us> References: <238b9830-4b0d-cefc-e322-44eef43e184b@gmail.com> <34bbe45a-2df8-85fd-b063-3b417bbbffd8@gmail.com> <7E297906-6267-46F7-B46C-36FEE17B207C@wichmann.us> Message-ID: On 16/2/20 3:04 pm, Mats Wichmann wrote: > you dont need to run an external command to collect the files, look at > the glob module and even more apropos the pathlib module. It turned out to be easier that I had expected and this is what I came up with: from pathlib2 import Path from glob import glob path = Path("/home/phil/Downloads/") for x in path.glob("*.gpx"): ??? print (x) I think that all I need to do now is to insert each file name into a subprocess.run() string which will then send each .gpx file to my navigator. A task for a later time. -- Regards, Phil From deepakdixit0001 at gmail.com Sun Feb 16 05:17:38 2020 From: deepakdixit0001 at gmail.com (Deepak Dixit) Date: Sun, 16 Feb 2020 15:47:38 +0530 Subject: [Tutor] How to know the internal execution flow of class In-Reply-To: <787c3ca3-c366-2582-9b08-a3724279c7da@wichmann.us> References: <787c3ca3-c366-2582-9b08-a3724279c7da@wichmann.us> Message-ID: Thanks Mats, I am agree with *Tim Peters *statement but I don't want to be one who know to write programs only. I am curious to know the internals of python :) BTW, Thanks for your well explanation, it helps me a lot to understand, specially __call__. Is there anything happening also when we are defining a *function* like the class ? One question regarding type, see this script- # Python 2.7.16 on Linux >>> type(dict) >>> type(type) >>> type(object) >>> Here I am getting *type(type) = * which confuses me. Is there any relation between 'type' and base/parent class of any object? what is the benefit of 'type' for a programmer? On Sat, Feb 15, 2020 at 11:21 PM Mats Wichmann wrote: > On 2/15/20 8:08 AM, Deepak Dixit wrote: > > Hi Tutors, > > > > What happens when we create a class or function. In other way, I want to > > know the life-cycle of class. Lets see an example: > > > > ######### Script Start ######### > > > > class MClass(type): > > def __new__(cls, *args): > > print 'New from MClass', args[0] > > return super(MClass, cls).__new__(cls, *args) > > > > class A: > > print 'Class A defined' > > __metaclass__ = MClass > > > > def __init__(self, *args): > > print 'Init from A', args > > > > def __new__(cls, *args): > > print 'New from A', args > > return super(A, cls).__new__(cls, *args) > > > > print 'Class A definition end' > > a = A() > > print 'Object created: A' > > > > class B: > > print 'Class B defined' > > __metaclass__ = MClass > > > > def __init__(self, *args): > > print 'Init from B', args > > > > def __new__(cls, *args): > > print 'New from B', args > > > > print 'Class B definition end' > > b = B() > > print 'Object created: B' > > > > ######### Script End ######### > > > > ################# Output ############ > > Class A defined > > New from MClass A > > Class A definition end > > New from A () > > Init from A () > > Object created: A > > Class B defined > > New from MClass B > > Class B definition end > > New from B () > > Object created: B > > > > ################# Output End ############ > > From this example , we can see that when we are defining any class ( not > > creating object), it call some methods from the metaclass. If I am > > returning something from __new__ then it call __init__ of current class > > otherwise it skips the calling even when I am creating object. Now > suppose > > that If I never assigned any metaclass manually then I will not be aware > > that something is happening when I am defining the class. > > > > How can I know these internals? > > First off here's a moderately famous quote from one of the most famous > Python developers: > > ?Metaclasses are deeper magic than 99% of users should ever worry > about. If you wonder whether you need them, you don?t (the people who > actually need them know with certainty that they need them, and don?t > need an explanation about why).? > > ? Tim Peters > > Ignoring any niggles about Python 2 and old-style classes, if you don't > define a metaclass, your class has one anyway: it's "type". So this same > flow happens for every class statement. Every callable object has a > special method __call__ which is invoked when you "call" that object, a > metaclass is no different. A metaclass is a template for creating a > class similar to how a class is a template for creating an object - you > call it and you get the object back. Thus when you create class A or B, > the metaclass is called - by invoking __call__, which invokes the > __new__ and __init__ methods, and you see your message from the > metaclass's init. It you don't provide a metaclass (which inherits from > type), or don't provide those methods, then the ones from type are > called as with any inheritance relationship. Since you can't muck with > type for safety reasons, inserting a custom metaclass is useful for > experimentation, as you've done. For the rest, see Tim's quote :) > > Seriously, did that clear any of the confusion up? > > Btw. the more preferred form now is: > > class A(metaclass=MClass): > print 'Class A defined' > > as opposed to defining __metaclass__ > > > P.S. - stuff you didn't ask for: > > You can create a class by calling type directly, by the way; the magic > of the class statement is you don't have to do that, since it > automatically calls type (or any metaclass you supply). Here's > something I had sitting around from a tutorial I did for someone. Maybe > it will amplify a little more? Here are the pieces of what we think of > as a Python class: > > # some methods > def transact(acct, amount): > acct.balance += amount > > def pay_interest(acct): > acct.balance += acct.balance * acct.interest_rate > > def account_init(acct, num, name, bal, rate): > acct.acct_number = num > acct.acct_holder = name > acct.balance = bal > acct.interest_rate = rate > > # a mapping for methods and attributes: > account = { > "acct_number": "", > "acct_holder": "", > "balance": 0.0, > "interest_rate": 0.0, > "transact": transact, > "pay_interest": pay_interest, > "__init__": account_init, > } > > # and create it with name, parents (none), mapping: > AccountType = type("AccountType", (), account) > > > # that gives the same result as this: > class AccountType(): > def transact(self, amount): > self.balance += amount > > def pay_interest(self): > self.balance += self.balance * self.interest_rate > > def __init__(self, num, name, bal, rate): > self.acct_number = num > self.acct_holder = name > self.balance = bal > self.interest_rate = rate > > # you can call either AccountType to create an instance - they're the > same thing! > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- *With Regards,* *Deepak Kumar Dixit* From mats at wichmann.us Sun Feb 16 08:54:23 2020 From: mats at wichmann.us (Mats Wichmann) Date: Sun, 16 Feb 2020 06:54:23 -0700 Subject: [Tutor] Executing shell commands In-Reply-To: References: <238b9830-4b0d-cefc-e322-44eef43e184b@gmail.com> <34bbe45a-2df8-85fd-b063-3b417bbbffd8@gmail.com> <7E297906-6267-46F7-B46C-36FEE17B207C@wichmann.us> Message-ID: On 2/16/20 12:54 AM, Phil wrote: > On 16/2/20 3:04 pm, Mats Wichmann wrote: >> you dont need to run an external command to collect the files, look at >> the glob module and even more apropos the pathlib module. > > It turned out to be easier that I had expected and this is what I came > up with: > > from pathlib2 import Path > from glob import glob > > path = Path("/home/phil/Downloads/") > > for x in path.glob("*.gpx"): > ??? print (x) just note: you don't need the glob import line here, you're asking for (and getting because you use the qualified name path.glob) the glob method of the Path object from pathlib. From __peter__ at web.de Sun Feb 16 06:28:07 2020 From: __peter__ at web.de (Peter Otten) Date: Sun, 16 Feb 2020 12:28:07 +0100 Subject: [Tutor] Executing shell commands References: <238b9830-4b0d-cefc-e322-44eef43e184b@gmail.com> <34bbe45a-2df8-85fd-b063-3b417bbbffd8@gmail.com> Message-ID: boB Stepp wrote: > On Sat, Feb 15, 2020 at 9:46 PM Phil wrote: >> >> On 16/2/20 12:47 pm, boB Stepp wrote: >> Thank you Bob for your reply. >> > subprocess.call("ls", "/home/phil/Downloads/*.gpx", shell=True) >> Adding shell=True to .call() or .run() generates the following: >> >> Traceback (most recent call last): >> File "/home/phil/Python/waypoints.py", line 6, in >> subprocess.run("ls", "/home/phil/Downloads/*.gpx", shell=True) >> File "/usr/lib/python3.7/subprocess.py", line 488, in run >> with Popen(*popenargs, **kwargs) as process: >> File "/usr/lib/python3.7/subprocess.py", line 702, in __init__ >> raise TypeError("bufsize must be an integer") >> TypeError: bufsize must be an integer >> >> Anyway, you've given me something to think about, I'll experiment >> further. > > Apparently your original approach of making a list of arguments is the > correct one, and I was sadly mistaken. You *do* need the square > brackets. Try that and hopefully all will be well. There are basically two ways to invoke call(): (1) with a list and shell=False. >>> from subprocess import call >>> call(["ls", "*.py"], shell=False) ls: cannot access *.py: No such file or directory 2 As the shell is not involved '*.py' is passed directly, and thus 'ls' will look for a file '*.py' which usually doesn't exist. This is the best option as there's no need to consider argument escaping and shell injection. If you want wildcards you can emulate them in Python: >>> import glob >>> call(["ls"] + glob.glob("*.py")) a.py b.py c.py 0 (2) with a string and shell=True. >>> call("ls *.py", shell=True) a.py b.py c.py 0 The shell will process the string according to its rules as if you had typed it on the command line, i. e. it will split args and expand wildcards and then issue a command equivalent to Python's call(["ls", "a.py", "b.py", "c.py"], shell=False) This option may seem convenient, but can be a constant source of problems (filenames with spaces have to be escaped, user input has to be sanitized etc.) As has been mentioned in this thread glob and pathlib offer alternatives that are easier to control. (3) Other combinations are usually a programming error. From phillor9 at gmail.com Sun Feb 16 23:01:54 2020 From: phillor9 at gmail.com (Phil) Date: Mon, 17 Feb 2020 14:31:54 +1030 Subject: [Tutor] Executing shell commands In-Reply-To: References: <238b9830-4b0d-cefc-e322-44eef43e184b@gmail.com> <34bbe45a-2df8-85fd-b063-3b417bbbffd8@gmail.com> <7E297906-6267-46F7-B46C-36FEE17B207C@wichmann.us> Message-ID: On 17/2/20 12:24 am, Mats Wichmann wrote: > On 16/2/20 3:04 pm, Mats Wichmann wrote: >> from pathlib2 import Path >> from glob import glob >> >> path = Path("/home/phil/Downloads/") >> >> for x in path.glob("*.gpx"): >> ??? print (x) > just note: you don't need the glob import line here, you're asking for > (and getting because you use the qualified name path.glob) the glob > method of the Path object from pathlib. Thanks Mats, It was something that I was experimenting with and I meant to delete that line before pressing "send". -- Regards, Phil From phillor9 at gmail.com Sun Feb 16 23:05:19 2020 From: phillor9 at gmail.com (Phil) Date: Mon, 17 Feb 2020 14:35:19 +1030 Subject: [Tutor] Executing shell commands In-Reply-To: References: <238b9830-4b0d-cefc-e322-44eef43e184b@gmail.com> <34bbe45a-2df8-85fd-b063-3b417bbbffd8@gmail.com> Message-ID: On 16/2/20 9:58 pm, Peter Otten wrote: > There are basically two ways to invoke call(): > (1) with a list and shell=False. Thank you Peter, lots of useful information there. -- Regards, Phil From lindsaydarling2020 at gmail.com Mon Feb 17 11:19:39 2020 From: lindsaydarling2020 at gmail.com (Lindsay towndrow) Date: Mon, 17 Feb 2020 09:19:39 -0700 Subject: [Tutor] Type Error in Python Message-ID: Hi team, I?m using python for a psychology project and I have a problem when inserting my histogram. It says Type error: string indices must be integers. I have looked on every single website I could find and can?t figure out what I did wrong. Also wondering if it has something to do with my data? I?ve attached a photo! Thank you in advance, Lindsay From joel.goldstick at gmail.com Mon Feb 17 15:57:08 2020 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Mon, 17 Feb 2020 15:57:08 -0500 Subject: [Tutor] Type Error in Python In-Reply-To: References: Message-ID: On Mon, Feb 17, 2020 at 2:39 PM Lindsay towndrow wrote: > > Hi team, > I?m using python for a psychology project and I have a problem when inserting my histogram. It says Type error: string indices must be integers. I have looked on every single website I could find and can?t figure out what I did wrong. Also wondering if it has something to do with my data? I?ve attached a photo! > Thank you in advance, > Lindsay > HI Lindsay, This is a text only list. copy and paste the complete error message, and the relevant code into the body of your message to give us a chance to see what is going wrong The photo gets removed here > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor -- Joel Goldstick http://joelgoldstick.com/blog http://cc-baseballstats.info/stats/birthdays From mats at wichmann.us Mon Feb 17 19:04:23 2020 From: mats at wichmann.us (Mats Wichmann) Date: Mon, 17 Feb 2020 17:04:23 -0700 Subject: [Tutor] How to know the internal execution flow of class In-Reply-To: References: <787c3ca3-c366-2582-9b08-a3724279c7da@wichmann.us> Message-ID: On 2/16/20 3:17 AM, Deepak Dixit wrote: > Thanks Mats, I am agree with *Tim Peters *statement but I don't want to > be one who know to write programs only. I am curious to know the > internals of python :) > BTW, Thanks for your well explanation, it helps me a lot to understand, > specially __call__. Is there anything happening also when we are > defining a *function* like the class ? One question regarding type, see > this script- > > # Python 2.7.16 on Linux > >>>> type(dict) > >>>> type(type) > >>>> type(object) > >>>>? > > Here I am getting *type(type) =??*?which confuses me. Is > there any relation between 'type' and base/parent class of any > object??what?is the benefit of 'type' for a programmer? I was busy and hoping someone else would pick up this advanced topic, where there's a risk of saying something that isn't quite right. Maybe as a next step read here? https://docs.python.org/3/reference/datamodel.html and for the topic that started this: https://docs.python.org/3/reference/datamodel.html#metaclasses can't say I've memorized it, perhaps I'll read again. Also some interesting stuff here: https://docs.python.org/3/library/types.html From deepakdixit0001 at gmail.com Mon Feb 17 21:27:49 2020 From: deepakdixit0001 at gmail.com (Deepak Dixit) Date: Tue, 18 Feb 2020 07:57:49 +0530 Subject: [Tutor] How to know the internal execution flow of class In-Reply-To: References: <787c3ca3-c366-2582-9b08-a3724279c7da@wichmann.us> Message-ID: Thanks, I'll check it out. On Tue, Feb 18, 2020, 5:34 AM Mats Wichmann wrote: > On 2/16/20 3:17 AM, Deepak Dixit wrote: > > Thanks Mats, I am agree with *Tim Peters *statement but I don't want to > > be one who know to write programs only. I am curious to know the > > internals of python :) > > BTW, Thanks for your well explanation, it helps me a lot to understand, > > specially __call__. Is there anything happening also when we are > > defining a *function* like the class ? One question regarding type, see > > this script- > > > > # Python 2.7.16 on Linux > > > >>>> type(dict) > > > >>>> type(type) > > > >>>> type(object) > > > >>>> > > > > Here I am getting *type(type) = * which confuses me. Is > > there any relation between 'type' and base/parent class of any > > object? what is the benefit of 'type' for a programmer? > > I was busy and hoping someone else would pick up this advanced topic, > where there's a risk of saying something that isn't quite right. > > Maybe as a next step read here? > > https://docs.python.org/3/reference/datamodel.html > > and for the topic that started this: > > https://docs.python.org/3/reference/datamodel.html#metaclasses > > can't say I've memorized it, perhaps I'll read again. > > Also some interesting stuff here: > > https://docs.python.org/3/library/types.html > > > From breamoreboy at gmail.com Mon Feb 17 15:47:38 2020 From: breamoreboy at gmail.com (Mark Lawrence) Date: Mon, 17 Feb 2020 20:47:38 +0000 Subject: [Tutor] Type Error in Python In-Reply-To: References: Message-ID: On 17/02/2020 16:19, Lindsay towndrow wrote: > Hi team, > I?m using python for a psychology project and I have a problem when inserting my histogram. It says Type error: string indices must be integers. I have looked on every single website I could find and can?t figure out what I did wrong. Also wondering if it has something to do with my data? I?ve attached a photo! > Thank you in advance, > Lindsay > Please show us your code and the full traceback leading up to the TypeError. This text only list has stripped your photo. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From contactmikecole at gmail.com Tue Feb 18 15:42:29 2020 From: contactmikecole at gmail.com (Michael Cole) Date: Tue, 18 Feb 2020 14:42:29 -0600 Subject: [Tutor] need help parsing multiple log files to create a timeline. what am I doing wrong?? Message-ID: I am working on parsing a bunch of log files to construct a timeline to represent changes on a network so that I can view events on different machines in parallel with matching timestamps. However, my program is getting bogged down and doesn't seem to be giving the right output. expected behavior: each row contains the name of the file it came from, and each cell in the row either is blank or contains a log with a timestamp in the same column as the matching timestamp from the timestamp row. observed behavior: readlines() reads the lines correctly. The timelines and headers are all built correctly. the data in each row is not populated fully, with many entries still left blank. some entries are filled, but only a tiny fraction. I am seeing that python is taking 99% of the CPU and that a lot of data is missing from the csv file generated. import globimport osimport csvimport sysimport datetime def createCSV(headers, rows, timestamps, csvFileName): with open (csvFileName, 'w+') as csvfile: csvwriter = csv.writer(csvfile) timestamps.insert(0, "timestamps") csvwriter.writerow(timestamps) for row in rows: row.insert(0, headers[rows.index(row)]) csvwriter.writerow(row) # checks to see if beginning of line matches format DD-MM HH:MM:SS.MSXdef getDateTime(line): if(line.strip()): if(line != '\n'): time = line.split(' ')[1] if(len(time.split(":")) == 4): hour = time.split(":")[0] minute = time.split(":")[1] second = time.split(":")[2].split(".")[0] microsecond = time.split(":")[2].split(".")[1] if(datetime.time(int(hour), int(minute), int(second), int(microsecond))): stamp = line.split(' ')[0] + " " + line.split(' ')[1] return stamp else: return 0 else: return 0 return 0 def listToString(s): str1 = "" return (str1.join(s)) def parseLogs(logFilePaths, csvFileName): rows = [] headers = [] timestamps = [] # parse files to get headers and timestamps for logFilePath in logFilePaths: logFile = open(logFilePath, 'r') fileName = logFilePath.split('/')[-3:] fileNameStr = listToString(fileName).split("-")[2].split(" ")[0].split("20")[1] headers.append(fileNameStr) lines = logFile.readlines() for line in lines: stamp = getDateTime(line) # append all valid timestamps to the array if(stamp != 0): timestamps.append(stamp) logFile.close() # remove duplicate timestamps and sort timestamps = list(dict.fromkeys(timestamps)) timestamps.sort() # parse files again to get data and put it in the right slot in the row for logFilePath in logFilePaths: logFile = open(logFilePath, "r") lines = logFile.readlines() row = [] # zero fill row, ensuring row array is same size as timestamps array for timestamp in timestamps: row.append("") # find the index of the corresponding timestamp for each line in the file linecount = 0 for line in lines: while linecount < len(timestamps): if line.strip() and getDateTime(line) != 0: try: index = timestamps.index(getDateTime(line)) row[index] = line linecount += 1 break except ValueError: row[linecount] = "!!XX!!" linecount += 1 pass rows.append(row) logFile.close() createCSV(headers, rows, timestamps, csvFileName) def main (): # default logfile base path and csv file name logFileBasePath = "" csvFileName = "Log.csv" # parsing command line args numArgs = len (sys.argv) - 1 if(numArgs == 0): print ("ERROR: You must specify the path to a directory of logfiles") return elif(numArgs > 1): print ("ERROR: Too many arguments") return else: # appending slash to path if not already there arg = sys.argv[1] if(arg[len (arg) - 1] != "/"): arg = arg + "/" logFileBasePath = arg # csv file will be placed adjacent to the logs, and will be named the name of its containing folder csvFileName = logFileBasePath + os.path.splitext (os.path.basename (os.path.dirname (logFileBasePath)))[0] + ".csv" logFilePaths = glob.glob (logFileBasePath + "*.log") if(len (logFilePaths) == 0): print ("ERROR: No logfiles found at: ", logFileBasePath) return else: parseLogs(logFilePaths, csvFileName ) main () Example log format: ---------- 2020-02-13 18:06:45 -0600: Logging Started ---------- 02-13 18:18:24.370: 00:12:42: INFO [XMOS] Media clock unlocked! reason: unlocked on zeroing 02-13 18:18:24.421: XMOS clock update. state:0 source:ff, rate:ff 02-13 18:18:24.656: 00:12:43: INFO [XMOS] out of sequence error. this seq: 16 last seq: 41 timestamp: fceb397f 02-13 18:18:24.709: 00:12:43: INFO [XMOS] out of sequence error. this seq: 57 last seq: 80 timestamp: fd3a1012 02-13 18:18:31.830: XMOS clock update. state:1 source:ff, rate:ff 02-13 18:46:41.844: 00:41:00: INFO [XMOS] Media clock unlocked! reason: unlocked on zeroing 02-13 18:46:41.896: XMOS clock update. state:0 source:ff, rate:ff 02-13 18:46:42.131: 00:41:00: INFO [XMOS] out of sequence error. this seq: 86 last seq: 111 timestamp: 38052b81 02-13 18:46:42.183: 00:41:00: INFO [XMOS] out of sequence error. this seq: 126 last s From PyTutor at DancesWithMice.info Tue Feb 18 17:51:05 2020 From: PyTutor at DancesWithMice.info (David L Neil) Date: Wed, 19 Feb 2020 11:51:05 +1300 Subject: [Tutor] need help parsing multiple log files to create a timeline. what am I doing wrong?? In-Reply-To: References: Message-ID: On 19/02/20 9:42 AM, Michael Cole wrote: > I am working on parsing a bunch of log files to construct a timeline to > represent changes on a network so that I can view events on different > machines in parallel with matching timestamps. However, my program is > getting bogged down and doesn't seem to be giving the right output. > > expected behavior: > > each row contains the name of the file it came from, and each cell in the > row either is blank or contains a log with a timestamp in the same column > as the matching timestamp from the timestamp row. > > observed behavior: > > readlines() reads the lines correctly. The timelines and headers are all > built correctly. > > the data in each row is not populated fully, with many entries still left > blank. some entries are filled, but only a tiny fraction. > > I am seeing that python is taking 99% of the CPU and that a lot of data is > missing from the csv file generated. ... code ... > Example log format: > > ---------- 2020-02-13 18:06:45 -0600: Logging Started ---------- > 02-13 18:18:24.370: 00:12:42: INFO [XMOS] Media clock unlocked! > reason: unlocked on zeroing > 02-13 18:18:24.421: XMOS clock update. state:0 source:ff, rate:ff > 02-13 18:18:24.656: 00:12:43: INFO [XMOS] out of sequence error. this > seq: 16 last seq: 41 timestamp: fceb397f > 02-13 18:18:24.709: 00:12:43: INFO [XMOS] out of sequence error. this > seq: 57 last seq: 80 timestamp: fd3a1012 > 02-13 18:18:31.830: XMOS clock update. state:1 source:ff, rate:ff > 02-13 18:46:41.844: 00:41:00: INFO [XMOS] Media clock unlocked! > reason: unlocked on zeroing > 02-13 18:46:41.896: XMOS clock update. state:0 source:ff, rate:ff > 02-13 18:46:42.131: 00:41:00: INFO [XMOS] out of sequence error. this > seq: 86 last seq: 111 timestamp: 38052b81 > 02-13 18:46:42.183: 00:41:00: INFO [XMOS] out of sequence error. this > seq: 126 last s Couple of things not directly answering the question: - if using Linux, are you aware that there are facilities to centralise multiple machine's logs, which might ease your task? - why spreadsheet? If your logs are all in txt format(?) and commence with a time-stamp, why not use directly? - are you aware of the many Python implementations and tools which 'attack' log-related problems Would you please show some sample log-entries, in over-lapping time-ranges (ie which should be interleaved)? Do any of these log entries span multiple lines? (lines terminated by your OpSys' line-separator(s) - not lines on a screen) > each row contains the name of the file it came from, and each cell in the > row either is blank or contains a log with a timestamp in the same column > as the matching timestamp from the timestamp row. - might one row contain more than one "log"? - is "each row" in a different worksheet from "the timestamp row" - why two "row"s? - why does the above description imply that the worksheet is not uniformly organised? eg firstCol = fileNM secondCol = timestamp thirdCol = log-entry > the data in each row is not populated fully, with many entries still left > blank. some entries are filled, but only a tiny fraction. Please show examples. Have you tried extracting some of these identified-faulty log-entries or worksheet rows, into a testable sub-set, which you will more easily analyse (than if there are hundreds/thousands/tens of... records to sift-through)? The multi-pass methodology may be unnecessarily complex - unless I've misunderstood some aspect. The problem is a class "merge" operation (once the staple of mainframe batch-computing). If one can assume that each of the input-files is sorted (into the same sequence), then the logic is to look at the 'current-record' from each, choose the 'earliest', output that, and replenish that file's current-record (rinse and repeat - plus watch-out for EOF conditions! How do you like working with databases? Assuming (>=) basic skills, another option is to write the logs to an RDBMS (or to make step-1 the copying from 'standard' logs into one), and then use SQL to do all the 'heavy-lifting' of the merge-and-sort, and reduce 'output' to a single retrieval-query! -- Regards =dn From flaviasnow86 at gmail.com Thu Feb 20 15:15:16 2020 From: flaviasnow86 at gmail.com (Flavee Gee Garcia) Date: Thu, 20 Feb 2020 12:15:16 -0800 Subject: [Tutor] IDLE Editor Message-ID: Hi there, I am trying to get better at using the Python IDLE terminal, specifically navigation without a mouse. I found this article but none of the commands correspond to what they say they should in the article. For example, ?F12 for File Structure Popup just makes the volume icon pop up on my Mac. I am in the IDLE editor but the Mac OS seems to be in the mix which is not my intention. Thanks! From alan.gauld at yahoo.co.uk Thu Feb 20 17:01:43 2020 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 20 Feb 2020 22:01:43 +0000 Subject: [Tutor] IDLE Editor In-Reply-To: References: Message-ID: On 20/02/2020 20:15, Flavee Gee Garcia wrote: > Hi there, > > I am trying to get better at using the Python IDLE terminal, specifically > navigation without a mouse. I found this > > article but none of the commands correspond to what they say they should Notice that this is for Jetbrains' own IDE - Pycharm. The keyboard bindings will be quite different. There is a page of keyboard bindings for IDLE in the IDLE configure dialog, and the help page has a section on bindings too. It is not a complete list though... Note that IDLE is due to get some major enhancements in the next couple of IDLE releases. The IDLE devs have been doing a lot of work, its quite exciting... -- 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 From sbanks1125 at gmail.com Sat Feb 22 04:20:06 2020 From: sbanks1125 at gmail.com (sean banks) Date: Sat, 22 Feb 2020 17:20:06 +0800 Subject: [Tutor] Sean Banks basic python help Message-ID: Hello I need a bit of help with this question, please. Use Python to calculate how many different passwords can be formed with 6 lower case English letters. For a 1 letter password, there would be 26 possibilities. For a 2 letter password, each letter is independent of the other, so there would be 26 times 26 possibilities. Using this information, print the amount of possible passwords that can be formed with 6 letters. From alan.gauld at yahoo.co.uk Sat Feb 22 04:31:36 2020 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 22 Feb 2020 09:31:36 +0000 Subject: [Tutor] Sean Banks basic python help In-Reply-To: References: Message-ID: On 22/02/2020 09:20, sean banks wrote: > Hello I need a bit of help with this question, please. > > Use Python to calculate how many different passwords can be formed with 6 > lower case English letters. For a 1 letter password, there would be 26 > possibilities. For a 2 letter password, each letter is independent of the > other, so there would be 26 times 26 possibilities. Using this information, > print the amount of possible passwords that can be formed with 6 letters. We won;ty do your homework for you but we can offer hints. The assignment tells you that 1 letter = 26 2 letters = 26*26 so we can extend that 3 letters = 26*26*26 So 6 letters = 26*26*26*26*26*26 All you need to do is calculate that using Python. If you need more help let us know what exactly puzzles 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 From mikael.lenander1 at gmail.com Sat Feb 22 12:33:36 2020 From: mikael.lenander1 at gmail.com (Mikael Lenander) Date: Sat, 22 Feb 2020 19:33:36 +0200 Subject: [Tutor] Pygame mixer music Message-ID: I created a music player with tkinter and pygame 1.9.6 mixer. Yesterday it worked flawlessly with mp3 files. Today, when I tried to play an mp3 file, the music player doesn't work anymore, though I haven't changed the code since yesterday. To check if there's something wrong with my code, I created this simple program. from tkinter import * from pygame import mixer root = Tk() mixer.init() mixer.music.load("journey.wav") mixer.music.play() root.mainloop() This program plays the music as it should. However, when I run the program using an mp3 file instead of wav, I get this error message. Traceback (most recent call last): File "C:/Users/Mikael/PycharmProjects/peli3/music.py", line 6, in mixer.music.load("miugemusaa.mp3") pygame.error I tried this with several mp3 files. All the music files are in the Pycharm project folder. From alan.gauld at yahoo.co.uk Sat Feb 22 17:31:17 2020 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 22 Feb 2020 22:31:17 +0000 Subject: [Tutor] Pygame mixer music In-Reply-To: References: Message-ID: On 22/02/2020 17:33, Mikael Lenander wrote: > created this simple program. > > from pygame import mixer > > mixer.init() > mixer.music.load("journey.wav") > mixer.music.play() > This program plays the music as it should. However, when I run the program > using an mp3 file instead of wav, I get this error message. > > Traceback (most recent call last): > File "C:/Users/Mikael/PycharmProjects/peli3/music.py", line 6, in > mixer.music.load("miugemusaa.mp3") > pygame.error As it says it is a pygame error, so you will probably get more advice on the pygame support forum. However things to check are whether you need to install or configure specific codecs to play mp3s. There are (or used to be) licensing issues with mp3 files that meant not all software supported it by default. You might have to do something to tell pygame about lame (or whatever decoder you use) -- 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 From Jon_Davies17 at hotmail.co.uk Sun Feb 23 08:10:02 2020 From: Jon_Davies17 at hotmail.co.uk (Jon Davies) Date: Sun, 23 Feb 2020 13:10:02 +0000 Subject: [Tutor] Pay-at-Pump Use Case - Tkinter GUI Message-ID: Hi all, I'm completely new to programming, Python and the like. I've researched various modules and frameworks, and am beginning to feel overwhelmed with the hundreds of ways in which to create a (hopefully) basic functioning application. In short, my aim is to create a GUI app that mimics a Pay-At-Pump software application which navigates multiple screens depending on options selected, following a simple flow e.g. Select Language > Select Payment Method > Insert Card > Select Currency > Begin Fuelling > Fuelling In Progress > Total and Goodbye. Semantics wise, I'd love to be able to make it look fantastic, but for now, I want to make sure I can develop something that works. Ideally, this is being built to deploy onto a Raspberry Pi, as I was planning to (hopefully) use a touchscreen to provide input to the GUI, and a breadboard with a push button to provide the input (simulate a fuel pump) ? however, this is not a priority as such! I?ve been following some video tutorials and other online resources to get to a stage where I have created a VERY basic GUI which can navigate through multiple pages based on clicking a button, however I need to enhance this with some of the other features. The key parts of functionality for me concern the language translation of the entire app, and the calculation of fuel price based on an input multiplied by a set of currency exchange rates (depending on which is selected). For translation, I am struggling to find the right way in which to present the rest of the app in a different language, based on the button pressed on the first frame. Having googled, there were a couple of different solutions but I couldn?t seem to apply them to my code. My baseline code is below: class Oleum(tk.Tk): def __init__(self, *args, **kwargs): # tk.Tk.__init__(self, *args, **kwargs) container = tk.Frame(self) container.pack(side="top", fill="both", expand = True) container.grid_rowconfigure(0, weight=1) container.grid_columnconfigure(0, weight=1) self.frames = {} for F in (SelectLanguagePage, SelectPaymentPage, InsertCardPage, SelectCurrencyPage, BeginFuellingPage, FuellingInProgressPage, TotalGoodbyePage): frame = F(container, self) self.frames[F] = frame frame.grid(row=0, column = 0, sticky = "nsew") self.show_frame(SelectLanguagePage) def show_frame(self, cont): frame = self.frames[cont] frame.tkraise() I tried to create lists containing the string input for every single label and button for each frame. At the moment, I have put prompt labels on the first frame (SelectLanguagePage) in all languages with a button to select each respective language. #LANGUAGE DICTIONARY - 17 ITEMS TOTAL ENGLISH = ['WELCOME!', 'SELECT YOUR LANGUAGE', 'SELECT PAYMENT METHOD', 'PAY-AT-PUMP', 'PAY-AT-KIOSK', 'PLEASE INSERT CARD', 'PLEASE SELECT CURRENCY', '? GBP', '$ USD', '? EUR', 'BEGIN FUELLING', 'TOTAL FUEL', 'TOTAL COST', 'FUELLING IN PROGRESS', 'FINISH', 'THANK YOU, GOODBYE'] GERMAN = ['DAS WILLKOMMEN!', 'W?HLE DEINE SPRACHE', 'ZAHLUNG AUSW?HLEN METHODE', 'PAY-AT-PUMP', 'PAY-AT-KIOSK', 'BITTE KARTE EINF?GEN', 'BITTE W?HLEN SIE IHRE W?HRUNG', '? GBP', '$ USD', '? EUR', 'TANKEN BEGINNEN', 'GESAMTKRAFTSTOFF', 'GESAMTKOSTEN', 'TREIBEN IM FORTSCHRITT', 'FERTIG', 'DANKE. AUF WIEDERSEHEN'] SPANISH = ['?BIENVENIDA!', 'ELIGE TU IDIOMA', 'SELECCIONAR PAGO M?TODO', 'PAGO EN BOMBA', 'PAGO EN EL KIOSCO', 'INSERTE LA TARJETA', 'POR FAVOR SELECCIONE SU MONEDA', '? GBP', '$ USD', '? EUR', 'COMENZAR COMBUSTIBLE', 'COMBUSTIBLE TOTAL', 'COSTE TOTAL', 'COMBUSTIBLE EN PROGRESO', 'TERMINAR', 'GRACIAS, ADIOS'] FRENCH = ['BIENVENUE!', 'CHOISISSEZ VOTRE LANGUE', 'CHOISIR LE PAIEMENT M?THODE', 'PAYER ? LA POMPE', 'PAYER AU KIOSQUE', 'VEUILLEZ INS?RER LA CARTE', 'VEUILLEZ S?LECTIONNER VOTRE MONNAIE', '? GBP', '$ USD', '? EUR', 'COMMENCER LE CARBURANT', 'CARBURANT TOTAL', 'CO?T TOTAL', 'RAVITAILLEMENT EN COURS', 'TERMINER', 'MERCI, AU REVOIR'] class SelectLanguagePage(tk.Frame): def __init__(self, parent, controller): tk.Frame.__init__(self, parent) EN_lang_prompt = tk.Label(self, text = "PLEASE SELECT LANGUAGE", font = LARGE_FONT) #tk.Label is the class, the object is label EN_lang_prompt.pack(pady = 10, padx = 10) DE_lang_prompt = tk.Label(self, text = "W?HLE DEINE SPRACHE", font = LARGE_FONT) DE_lang_prompt.pack(pady = 10, padx = 10) ES_lang_prompt = tk.Label(self, text = "ELIGE TU IDIOMA", font = LARGE_FONT) ES_lang_prompt.pack(pady = 10, padx = 10) FR_lang_prompt = tk.Label(self, text = "CHOISISSEZ VOTRE LANGUE", font = LARGE_FONT) FR_lang_prompt.pack(pady = 10, padx = 10) EN_lang_button = tk.Button(self, text = "ENGLISH", command = lambda: controller.show_frame(SelectPaymentPage)) EN_lang_button.pack() DE_lang_button = tk.Button(self, text = "GERMAN", command = lambda: controller.show_frame(SelectPaymentPage)) DE_lang_button.pack() ES_lang_button = tk.Button(self, text = "SPANISH", command = lambda: controller.show_frame(SelectPaymentPage)) ES_lang_button.pack() FR_lang_button = tk.Button(self, text = "FRENCH", command = lambda: controller.show_frame(SelectPaymentPage)) FR_lang_button.pack() The aim is to show SelectPaymentPage (and essentially all subsequent pages as defined in frames (F) in baseline code) in the desired language as selected via the buttons in 1st Frame. Again, I assume this would involve passing multiple commands through lambda to show the 2nd frame, pull in the values from my language lists etc.) How best would I achieve this, in a way that I can then replicate for each frame? My current basic code for 2nd frame is below. class SelectPaymentPage(tk.Frame): def __init__(self, parent, controller): tk.Frame.__init__(self, parent) label = tk.Label(self, text = "SELECT PAYMENT OPTION", font = LARGE_FONT) label.pack(pady = 10, padx = 10) pap_button = tk.Button(self, text = "PAY-AT-PUMP", command = lambda: controller.show_frame(InsertCardPage)) pap_button.pack() pap_button = tk.Button(self, text = "PAY-AT-KIOSK", command = lambda: controller.show_frame(SelectCurrencyPage)) pap_button.pack() button1 = tk.Button(self, text = "RESTART", command = lambda: controller.show_frame(SelectLanguagePage)) button1.pack() I?d found the following online, but as mentioned above I have struggled to apply this in my scenario: Changing the language is as simple as modifying each Tk widget depending on the selected language. For example, def change_language(lang): if lang == 'English': root.title('Program') menuButton.config(text='Menu') elif lang == 'Spanish': root.title('Programa') menuButton.config(text='Men?') To make it easier to write the code, you could store your language data in a file (e.g. csv), parse it into lists or dictionaries, and have something like this: english = ['Program', 'Menu'] spanish = ['Programa', 'Men?'] def change_language_2(lang): root.title(lang[0]) menuButton.config(text=lang[1]) Any help would be greatly appreciated, as in theory once I know the specific code structure/command/variables required, I should be able to then take this an apply it where necessary. Kind regards, Jon From alan.gauld at yahoo.co.uk Sun Feb 23 13:52:37 2020 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 23 Feb 2020 18:52:37 +0000 Subject: [Tutor] Pay-at-Pump Use Case - Tkinter GUI In-Reply-To: References: Message-ID: On 23/02/2020 13:10, Jon Davies wrote: > For translation, I am struggling to find the right way There is a well understood solution for this that is used in real world programs regardless of language and is supported in Python. Do not reinvent the wheel! Search for terms like "internationalisation", "localisation", "locale" "i18n". The industry standard tool is called "gettext" and python has a gettext module - check its documentation. It is non trivial but it does mean that your code will be easily extendible to any language. The basic process looks like this: 1) Write your python code with a gettext preamble and mark the strings that need to be translated(ie the ones displayed by the UI) for use with the gettext functions. In Python that usually means things like: print(_("Hello world")) # _() marks string for gettext 2) Run the xgettext tool (non-python) to extract those strings into a template file, usually "messages.po" 3) Create translation files providing the equivalent strings in the languages you support. Google translate can give a helpful start! 4) Use another tool - msgfmt - from the gettext suite to translate the .po files into the format used by gettext, usually ending in.mo 5) Ship the folder containing these files with your application. (On Windows the tools are found in Tools/i18n of the python distribution. On Linux the OS has the tools already.) If you need a longer example then ask again. This is described in Chapter 4 of my book "Python Projects"... -- 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 From alan.gauld at yahoo.co.uk Sun Feb 23 13:55:08 2020 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 23 Feb 2020 18:55:08 +0000 Subject: [Tutor] Pay-at-Pump Use Case - Tkinter GUI In-Reply-To: References: Message-ID: On 23/02/2020 18:52, Alan Gauld via Tutor wrote: > There is a well understood solution for this that is used > in real world programs regardless of language ...regardless of programming language... is what I meant! -- 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 From phillor9 at gmail.com Sun Feb 23 20:32:32 2020 From: phillor9 at gmail.com (Phil) Date: Mon, 24 Feb 2020 12:02:32 +1030 Subject: [Tutor] Basic ElementTree xml question Message-ID: Thank you for reading this, I'm trying to create an xml file with an output that looks like this" ? and this is the closest that I have so far achieved: ? with this code snippet: from xml.etree import ElementTree as ET gpx = ET.Element("gpx") wpt = ET.SubElement(gpx, "wpt") So my question is, how to I include the lat and lon text after Message-ID: Phil wrote: > Thank you for reading this, > > I'm trying to create an xml file with an output that looks like this" > > > > > > and this is the closest that I have so far achieved: > > > > > > with this code snippet: > > from xml.etree import ElementTree as ET > > gpx = ET.Element("gpx") > > wpt = ET.SubElement(gpx, "wpt") > > So my question is, how to I include the lat and lon text after >> gpx = ET.Element("gpx") >>> wpt = ET.SubElement(gpx, "wpt", lat="-32.5", lon="137.7") >>> ET.tostring(gpx) b'' If that's not possible: >>> wpt.attrib["not-a-valid-python-identifier"] = "spam" >>> ET.tostring(gpx) b'' or >>> ET.tostring(ET.Element("foo", attrib={"ham-spam": "jam", "foo-bar": "baz"})) b'' PS: > I've searched the Internet for example code but haven't found anything > that allows strings after " the information in the Etree manual into anything close to what I need. Goggling for "create elementtree element with attribute" finds https://stackoverflow.com/questions/25807414/how-do-i-add-attributes-to-subelement-in-elementtree-python From jon_davies17 at hotmail.co.uk Mon Feb 24 06:05:59 2020 From: jon_davies17 at hotmail.co.uk (Jon Davies) Date: Mon, 24 Feb 2020 11:05:59 +0000 Subject: [Tutor] Pay-at-Pump Use Case - Tkinter GUI In-Reply-To: References: , Message-ID: Thank you Alan much appreciated. I?ll have a dog and a play, will let you know how I get on! Kind regards, Jon Get Outlook for iOS ________________________________ From: Tutor on behalf of Alan Gauld via Tutor Sent: Sunday, February 23, 2020 6:55:08 PM To: tutor at python.org Subject: Re: [Tutor] Pay-at-Pump Use Case - Tkinter GUI On 23/02/2020 18:52, Alan Gauld via Tutor wrote: > There is a well understood solution for this that is used > in real world programs regardless of language ...regardless of programming language... is what I meant! -- 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 at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From phillor9 at gmail.com Mon Feb 24 18:36:57 2020 From: phillor9 at gmail.com (Phil) Date: Tue, 25 Feb 2020 10:06:57 +1030 Subject: [Tutor] Basic ElementTree xml question In-Reply-To: References: Message-ID: On 24/2/20 6:30 pm, Peter Otten wrote: > wpt = ET.SubElement(gpx, "wpt", lat="-32.5", lon="137.7") Thank you Peter, I had tried what I thought was every combination. What I didn't try was not including lat and lon within the inverted comas. I now see the error of my ways. And thank you for the link. -- Regards, Phil From cranky.frankie at gmail.com Mon Feb 24 19:03:50 2020 From: cranky.frankie at gmail.com (Cranky Frankie) Date: Mon, 24 Feb 2020 19:03:50 -0500 Subject: [Tutor] big props for Python Projects Wrox 2015 Message-ID: Hi I just wanted to say I'm only now getting to "Python Projects" from Wrox in 2015 and I understand Anal Gauld who co-wrote it is moderating this list. For anyone on here who has not seen or heard of this book, let me say it is 100% awesome. The GUI content alone is worth the price, and that's only one chapter. They cover everything -databases, web frameworks, and much more. I especially like the instructions on how to organize modules to make one program. That's something I struggle with on larger programs. Anyway, just wanted to say both this book and this list rock! Thanks Alan very, very much for your hard work for the Python community. -- Frank L. "Cranky Frankie" Palmeri, Risible Riding Raconteur & Writer "Thankfully, perseverance is a great substitute for talent." - Steve Martin From alan.gauld at yahoo.co.uk Mon Feb 24 19:27:32 2020 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 25 Feb 2020 00:27:32 +0000 Subject: [Tutor] big props for Python Projects Wrox 2015 In-Reply-To: References: Message-ID: On 25/02/2020 00:03, Cranky Frankie wrote: > Anyway, just wanted to say both this book and this list rock! Thanks Alan > very, very much for your hard work for the Python community. Ah shucks ;-/ Thanks. -- 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 From phillor9 at gmail.com Mon Feb 24 19:54:07 2020 From: phillor9 at gmail.com (Phil) Date: Tue, 25 Feb 2020 11:24:07 +1030 Subject: [Tutor] Basic ElementTree - another question In-Reply-To: References: Message-ID: On 24/2/20 6:30 pm, Peter Otten wrote: I thought that I would now be able to proceed to the next step but unfortunately that's not the case despite extensive Internet searching. This is what I want to achieve: ? ??? 1 ??? 1 ??? 1 ? and then repeat as above to the next destination, with a different name. This gives me the first line but, of course, the next line is not correct. ? gpx = ET.Element("gpx") ? wpt = ET.SubElement(gpx, "wpt", lat="-32.506533000", lon= "137.740017000") ? ET.SubElement(wpt, "name=1") I've found all sorts of examples but nothing specific to my current needs. -- Regards, Phil From dfjennings at gmail.com Mon Feb 24 21:30:37 2020 From: dfjennings at gmail.com (Don Jennings) Date: Mon, 24 Feb 2020 21:30:37 -0500 Subject: [Tutor] Basic ElementTree - another question In-Reply-To: References: Message-ID: > On Feb 24, 2020, at 7:54 PM, Phil wrote: > > On 24/2/20 6:30 pm, Peter Otten wrote: > > I thought that I would now be able to proceed to the next step but unfortunately that's not the case despite extensive Internet searching. > > This is what I want to achieve: > > > 1 > 1 > 1 > > > and then repeat as above to the next destination, with a different name. > > This gives me the first line but, of course, the next line is not correct. > > gpx = ET.Element("gpx") > > wpt = ET.SubElement(gpx, "wpt", lat="-32.506533000", lon= "137.740017000") > ET.SubElement(wpt, "name=1?) Hi, Phil. When you add keyword arguments (e.g. lat and lon), those become attributes in the resulting start-tag(s). Instead, assign the value to the text attribute of the element: >>> import xml.etree.ElementTree as ET >>> root = ET.Element("root") >>> child = ET.SubElement(root, "child", attr="some attribute") >>> child.text = ?content" >>> ET.tostring(root) b'content? Best, Don From PyTutor at DancesWithMice.info Mon Feb 24 23:11:56 2020 From: PyTutor at DancesWithMice.info (David L Neil) Date: Tue, 25 Feb 2020 17:11:56 +1300 Subject: [Tutor] big props for Python Projects Wrox 2015 In-Reply-To: References: Message-ID: <84746971-d37e-e245-4776-4c0a6350ca1a@DancesWithMice.info> On 25/02/20 1:03 PM, Cranky Frankie wrote: > Hi I just wanted to say I'm only now getting to "Python Projects" from Wrox > in 2015 and I understand Anal Gauld who co-wrote it is moderating this > list. For anyone on here who has not seen or heard of this book, let me say > it is 100% awesome. The GUI content alone is worth the price, and that's > only one chapter. They cover everything -databases, web frameworks, and > much more. I especially like the instructions on how to organize modules to > make one program. That's something I struggle with on larger programs. > Anyway, just wanted to say both this book and this list rock! Thanks Alan > very, very much for your hard work for the Python community. +1 Is (s)he saying that your book gets to the bottom of things? -- Regards =dn From phillor9 at gmail.com Tue Feb 25 00:45:49 2020 From: phillor9 at gmail.com (Phil) Date: Tue, 25 Feb 2020 16:15:49 +1030 Subject: [Tutor] Basic ElementTree - another question In-Reply-To: References: Message-ID: On 25/2/20 1:00 pm, Don Jennings wrote: >> On Feb 24, 2020, at 7:54 PM, Phil wrote: >> >> >> This is what I want to achieve: >> >> >> 1 >> 1 >> 1 >> >> >> Hi, Phil. When you add keyword arguments (e.g. lat and lon), those become attributes in the resulting start-tag(s). Instead, assign the value to the text attribute of the element: >> >>>> import xml.etree.ElementTree as ET >>>> root = ET.Element("root") >>>> child = ET.SubElement(root, "child", attr="some attribute") >>>> child.text = ?content" >>>> ET.tostring(root) > b'content? Thanks Don, I appreciate your reply but after experimenting with what you have provided I still cannot get the result that I need. Would it be possible for you to provide code for the two lines that start with "1"? Hopefully that will enable me to proceed with this section of the project. -- Regards, Phil From dfjennings at gmail.com Tue Feb 25 06:34:01 2020 From: dfjennings at gmail.com (Don Jennings) Date: Tue, 25 Feb 2020 06:34:01 -0500 Subject: [Tutor] Basic ElementTree - another question In-Reply-To: References: Message-ID: > On Feb 25, 2020, at 12:45 AM, Phil wrote: > > On 25/2/20 1:00 pm, Don Jennings wrote: >>> On Feb 24, 2020, at 7:54 PM, Phil wrote: >>> >>> > >>> This is what I want to achieve: >>> >>> >>> 1 >>> 1 >>> 1 >>> >>> >>> Hi, Phil. When you add keyword arguments (e.g. lat and lon), those become attributes in the resulting start-tag(s). Instead, assign the value to the text attribute of the element: >>> >>>>> import xml.etree.ElementTree as ET >>>>> root = ET.Element("root") >>>>> child = ET.SubElement(root, "child", attr="some attribute") >>>>> child.text = ?content" >>>>> ET.tostring(root) >> b'content? > > Thanks Don, I appreciate your reply but after experimenting with what you have provided I still cannot get the result that I need. Would it be possible for you to provide code for the two lines that start with "1"? Hopefully that will enable me to proceed with this section of the project. Please paste the code which you?ve tried, so we can see what?s happening, thanks. Best, Don From yichen.liu at igeollc.com Tue Feb 25 12:01:41 2020 From: yichen.liu at igeollc.com (Will Liu) Date: Tue, 25 Feb 2020 17:01:41 +0000 Subject: [Tutor] Control MS Access with Python Message-ID: Hello. I am new to Python. I am wondering if it is possible to control MS Access with Python? I have a MS Access database. Every month, I need to export 100+ excel files from this database manually based on different criteria. I am wondering if it is possible to let Python distinguish, pick, and export data from MS Access automatically? I already have pyodbc installed and have MS Access connected with Python IDLE. Now I am able to read data through Python IDLE. Am I on the right way? If not, please explain. Thanks! Will Liu Engineer Assistant | iGeo LLC 3000 Wilcrest Drive, Suite 240 | Houston, Texas 77042 United States Phone: +1 281 857 2091 E-mail: yichen.liu at igeollc.com From mdrieder at msn.com Tue Feb 25 09:48:03 2020 From: mdrieder at msn.com (daniel rieder) Date: Tue, 25 Feb 2020 14:48:03 +0000 Subject: [Tutor] How the shell relates to programs in idle Message-ID: I am new to Python and am going through a guide at anh.cs.edu/handsonPythonTutorial. I am learning how to create dictionaries PLUS learning how, when a program is run, the variables and, as I understand it, dictionaries created in a program that is run from idle are retained so they can be accessed via the shell. For example, I created a program that, among other things, creates a variable x=3. After I ran the program, I used the shell and entered print(x) and it dutifully printed 3. Well and good. But supposedly if I create a dictionary in a program and run that program, the dictionary is still ?stored? by python such that if I enter a print line in the shell referencing the dictionary, it should print the contents I indexed. Here is the program: def createDictionary(): '''Returns a tiny Spanish dictionary''' spanish = dict() spanish['hello'] = 'hola' spanish['yes'] = 'si' spanish['one'] = 'uno' spanish['two'] = 'dos' spanish['three'] = 'tres' spanish['red'] = 'rojo' spanish['black'] = 'negro' spanish['green'] = 'verde' spanish['blue'] = 'azul' return spanish def main(): dictionary = createDictionary() print(dictionary['two']) print(dictionary['red']) main() When I ran the program, it displayed ?dos? and ?rojo? as intended. Then immediately afterward, using the shell, I typed in: print (dictionary[?two?]) hoping it would access the dictionary and print ?dos,? but it returned an error message: Traceback (most recent call last): File "", line 1, in print (spanish['two']) NameError: name 'dictionary' is not defined Did I make a mistake in how I referenced the dictionary? Or is the guidance I am following somehow outdated such that NOW python no longer stores dictionaries after the program is run like it seems to store variables?like it did x in the above example? Thanks, Dan From alan.gauld at yahoo.co.uk Tue Feb 25 17:44:14 2020 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 25 Feb 2020 22:44:14 +0000 Subject: [Tutor] Control MS Access with Python In-Reply-To: References: Message-ID: On 25/02/2020 17:01, Will Liu wrote: > I already have pyodbc installed and have MS Access connected with Python IDLE. > Now I am able to read data through Python IDLE. Am I on the right way? For your purposes yes. You can read the data you require from Access using SQL commands and store the results in a Python collection - a list of tuples say? Then use the csv module to write those tuples into a CSV file that can be read from excel. If you need to be even cleverer with Excel (multi tabbed spreadsheet for example) then there are 3rd party modules that can do it. But for most purposes CSV is the easiest option. You can finesse the queries in IDLE and then once you have them exactly as you need transfer the code into a standalone script that you can run as a monthly batch job generating all the files automatically. -- 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 From alan.gauld at yahoo.co.uk Tue Feb 25 18:04:20 2020 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 25 Feb 2020 23:04:20 +0000 Subject: [Tutor] How the shell relates to programs in idle In-Reply-To: References: Message-ID: On 25/02/2020 14:48, daniel rieder wrote: > when a program is run, the variables ... created in a program that is run from idle are retained We need to be careful about the terminology here. When you run a program in IDLE it is executed in the IDLE shell. When the program stops the shell still runs so any data objects created by the program will still exist and you can inspect them. But only the objects that still live at the end of the program, not all the objects that the program created. > Here is the program: > > > def createDictionary(): > > '''Returns a tiny Spanish dictionary''' > > spanish = dict() > spanish['hello'] = 'hola' > spanish['yes'] = 'si' > spanish['one'] = 'uno' ... > spanish['blue'] = 'azul' > > return spanish An easier way to do this is to create the dictionary in place: spanish = { 'hello' : 'hola', 'yes' : 'si', 'one' : 'uno', ... 'blue' : 'azul' } return spanish Saves typing and is marginally faster to execute. But thats a side issue... Here is the key point. > def main(): > > dictionary = createDictionary() > print(dictionary['two']) > print(dictionary['red']) When you create an object inside a function the object only exists within that function, unless you return it to the caller of the function. So when you call createDictionary() you create spanish inside the function. But because you return spanish, the object is passed out to the caller(main) when the function terminates. So the dictionary is now referenced by the variable name 'dictionary' which exists inside main. But because you don't return dictionary to a variable outside main, it dies when main ends. So when the program finishes and you try to access the data it is no longer there. The only objects left are the two functions createDictionary() and main(). > When I ran the program, it displayed ?dos? and ?rojo? as intended. Good. The program works. > Then immediately afterward, using the shell, I typed in: > print (dictionary[?two?]) hoping it would access the dictionary > and print ?dos,? but it returned an error message: > > Traceback (most recent call last): > File "", line 1, in > print (spanish['two']) > NameError: name 'dictionary' is not defined That's because the dictionary variable only exists inside main(). You cannot see it outside of main. > Did I make a mistake in how I referenced the dictionary? No, you did everything correctly. Its just how Python (and most other programming languages) work. >...after the program is run like it seems to store variables?like it did x I suspect the x was not inside a main() function, but just typed at the top level. If you had run the same program as above but left out the def main() you would have seen the dictionary at the end. It was just because you put it inside main that you lost it. But I need to stress that you should not build programs where you expect to access data after the program is finished. Think of that purely as a debugging aid in IDLE. If you rely on it you will only ever be able to run your programs inside IDLE and that is not a good thing. Instead you will learn to build programs that run continuously until you tell them to exit. Then inside the program you will create facilities for accessing the data as needed. That is a much more user friendly way to write code and also more efficient. If you want to read more about this topic (which is technically called "scope" or "namespaces") and how to deal with it check out the "What's in a name?" advanced topic in my tutorial (see .sig below) -- 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 From PyTutor at DancesWithMice.info Tue Feb 25 18:35:11 2020 From: PyTutor at DancesWithMice.info (DL Neil) Date: Wed, 26 Feb 2020 12:35:11 +1300 Subject: [Tutor] How the shell relates to programs in idle In-Reply-To: References: Message-ID: <03280127-df4a-349e-624f-66514141ad5a@DancesWithMice.info> On 26/02/20 3:48 AM, daniel rieder wrote: ... > > def main(): > > dictionary = createDictionary() > > print(dictionary['two']) > > print(dictionary['red']) > > > > main() ... Further to earlier explanation (and slightly wary because I don't use Idle), instead of declaring a main function, consider another common Python idiom: if __name__ == "__main__": dictionary = createDictionary() print(dictionary['two']) print(dictionary['red']) (should) continue to work, plus leave access to dictionary thereafter. WebRef: https://docs.python.org/3/library/__main__.html -- Regards =dn From alan.gauld at yahoo.co.uk Tue Feb 25 19:59:02 2020 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 26 Feb 2020 00:59:02 +0000 Subject: [Tutor] How the shell relates to programs in idle In-Reply-To: References: Message-ID: On 25/02/2020 23:04, Alan Gauld via Tutor wrote: > On 25/02/2020 14:48, daniel rieder wrote: > >> when a program is run, the variables ... created in a program that is run from idle are retained > > We need to be careful about the terminology here. Actually, reading it back, I was being sloppy with terminology myself. Below is the same answer but expanded in places to correct my own misinformation! Sorry. > When you run a program in IDLE it is executed in the IDLE shell. > When the program stops the shell still runs so any data objects created > by the program will still exist and you can inspect them. But only the > objects that still live at the end of the program, not all the objects > that the program created. Notice that I use the word object here (or you could use 'value' if you prefer). Python has a clear distinction between variable names and values. A variable in Python (unlike most other languages) is just a name which refers to an object(or value). The same object can be referred to by multiple names (and a single name can refer to multiple values over its lifespan) > Here is the key point. > >> def main(): >> >> dictionary = createDictionary() >> print(dictionary['two']) >> print(dictionary['red']) > > When you create an object inside a function the object > only exists within that function, unless you return it > to the caller of the function. Actually that should say "when you create a variable name inside a function the name only exists within the function. Now we need to talk about objects and their lifetimes. When an object is created it exists for as long as a name refers to it. Thus an object created inside a function will die when the variable inside the function dies unless another name refers to it. This is true when the object is returned from the function to the caller, provided the caller assigns it to a value. So returned values live on after the function that created them ceases to run. Here is a short example: def create999(): x = 999 # create a new name, x and value return x # returns the VALUE of x to the caller def create7000(): x = 7000 # create a new name, x and a value return x # returns the VALUE of x to the caller def main(): y = create999() print(y) print (create7000()) print(x) # error! x is not known inside main. Note that main() prints both values successfully, but the 7000 value gets destroyed immediately after the print because it was not assigned to a name (ie a variable). The only way to get 7000 printed again would be to call create7000() again. However, the 999 value is stored in a variable named y and so we can print it out as often as we want without calling create999 again, simply by calling print(y). If we try to print x we get an error because x only exists inside the create functions. It is destroyed when the functions end. So it is the object/value that is returned from the function not the name. And the object is only stored if it is assigned to a variable (such as y) in the calling function. > So when you call createDictionary() you create spanish > inside the function. But because you return spanish, > the object is passed out to the caller(main) when > the function terminates. So the dictionary is now referenced > by the variable name 'dictionary' which exists inside main. > > But because you don't return dictionary to a variable > outside main, it dies when main ends. > > So when the program finishes and you try to access the > data it is no longer there. The only objects left are > the two functions createDictionary() and main(). Note that Python treats functions as objects too - this is an advanced concept you can ignore for now! >> Traceback (most recent call last): >> File "", line 1, in >> print (spanish['two']) >> NameError: name 'dictionary' is not defined > > That's because the dictionary variable only exists inside main(). > You cannot see it outside of main. Note that it was a NAME error not a Value error. It was the name 'dictionary' that it did not recognise. So even if the dictionary object had been returned it would need to be referenced by whatever name the receiver used. Not the name inside main. I hope that has clarified rather than further confused matters! -- 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 From phillor9 at gmail.com Wed Feb 26 00:43:01 2020 From: phillor9 at gmail.com (Phil) Date: Wed, 26 Feb 2020 16:13:01 +1030 Subject: [Tutor] Basic ElementTree - another question In-Reply-To: References: Message-ID: <5a40a5d6-54f2-e934-7aa2-16a720da394b@gmail.com> On 25/2/20 10:04 pm, Don Jennings wrote: > Please paste the code which you?ve tried, so we can see what?s happening, thanks. I'm a little embarrassed to say, Don, that I cannot relate the example that you provided with what I'm trying to achieve. I cannot get past the code snippet that I showed when I first asked my question and I cannot make hear nor tail of the ElementTree manual. In short, I'm at a total loss. -- Regards, Phil From __peter__ at web.de Wed Feb 26 03:50:00 2020 From: __peter__ at web.de (Peter Otten) Date: Wed, 26 Feb 2020 09:50 +0100 Subject: [Tutor] Basic ElementTree - another question References: <5a40a5d6-54f2-e934-7aa2-16a720da394b@gmail.com> Message-ID: Phil wrote: > On 25/2/20 10:04 pm, Don Jennings wrote: > >> Please paste the code which you?ve tried, so we can see what?s happening, >> thanks. > > I'm a little embarrassed Don't be. > to say, Don, that I cannot relate the example > that you provided with what I'm trying to achieve. I cannot get past the > code snippet that I showed when I first asked my question and I cannot > make hear nor tail of the ElementTree manual. In short, I'm at a total > loss. Basic xml consists of nodes xml: ... (an empty node can also be written ) etree: Element("stuff") that can have attributes xml: ... etree: Element("stuff", foo="bar" ham="spam") that can include text xml: yadda etree: e = Element("stuff") e.text = "yadda" or children 1st childwhatever e = Element("stuff") first = SubElement(e, "first") first.text = "1st child" second = SubElement(e, "second") second.text = "whatever" These building blocks should be enough to generate the desired xml. If you find that too hard, and if you want to learn Python rather than have someone else solve your current problem it's probably better to step back and work through a Python tutorial, tackle some basic problems (we'll be happy to help with those, too) and revisit xml generation with element tree once you are a bit more comfortable with the language. PS: Regarding element tree I like lxml and its documentation https://lxml.de/tutorial.html but as there are differences between lxml and the stdlib I'm not sure whether you will get even more confused if you read it right now. From kb at kbojens.de Wed Feb 26 04:46:39 2020 From: kb at kbojens.de (Kai Bojens) Date: Wed, 26 Feb 2020 10:46:39 +0100 Subject: [Tutor] RC5 in Python Message-ID: <20200226094639.GA29829@mail.kbojens.de> Is there any way to use RC5 for decryption purposes in Python? I even looked on page 2 of the Google results and all I found were deprecations notices, removals due to patents and some old github repos with python 2 code. None of the available crypto modules support RC5 at the moment. There are of course very good reasons to not use RC5 today, but there is still some data I need to decrypt ? (And of course I don't want to code my own encryption as we all know that this leads to bigger problems) :) From alan.gauld at yahoo.co.uk Wed Feb 26 11:56:04 2020 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 26 Feb 2020 16:56:04 +0000 Subject: [Tutor] RC5 in Python In-Reply-To: <20200226094639.GA29829@mail.kbojens.de> References: <20200226094639.GA29829@mail.kbojens.de> Message-ID: On 26/02/2020 09:46, Kai Bojens wrote: > Is there any way to use RC5 for decryption purposes in Python? I even looked on > page 2 of the Google results and all I found were deprecations notices, removals > due to patents and some old github repos with python 2 code. I found https://github.com/tbb/pyRC5 On my first page and it seems fairly recent. The last update was May 2019, and a quick glance at the code looks like it is Python 3 compatible. I have no idea if it will do what you want but it might be a good starting point at least. -- 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 From narasimha928 at gmail.com Wed Feb 26 11:44:44 2020 From: narasimha928 at gmail.com (Narasimharao Nelluri) Date: Wed, 26 Feb 2020 08:44:44 -0800 Subject: [Tutor] overlapping tuples Message-ID: Hi I got stuck at below problem for couple of days, can you guys help tp solve this problem. i need find overlapping tuples for various list of tuples, my current solution doest solve all the cases. I am failing at last input list3 . please check below #!/usr/bin/env python3 def tuple_overlap(old_list,overlap = None): old_list.sort() if overlap is None: overlap = [] for fir,sec in zip(old_list,old_list[1:]): if fir[1] >= sec[0] and fir[1] <=sec[1]: overlap.append(fir) if sec[0] >= fir[0] and sec[1] <= fir[1]: overlap.append(sec) overlap = sorted(overlap) print(overlap) list1 = [(1,10),(15,20),(101,110)] tuple_overlap(list1) list2 = [(1,20),(15,20),(101,110)] tuple_overlap(list2) list3 = [(1,10),(15,20),(1,10),(1,10),(101,110)] tuple_overlap(list3) NNELLURI-M-L13P:google_class nnelluri$ ./overlap.py [] [(1, 20), (15, 20)] [(1, 10), (1, 10), (1, 10), (1, 10)] NNELLURI-M-L13P:google_class nnelluri$ From phillor9 at gmail.com Wed Feb 26 19:12:12 2020 From: phillor9 at gmail.com (Phil) Date: Thu, 27 Feb 2020 10:42:12 +1030 Subject: [Tutor] Basic ElementTree - another question In-Reply-To: References: <5a40a5d6-54f2-e934-7aa2-16a720da394b@gmail.com> Message-ID: <615a51f4-7aaf-a3ae-8a7f-491736b7147b@gmail.com> On 26/2/20 7:19 pm, Peter Otten wrote: > These building blocks should be enough to generate the desired xml. Thank you for taking the time to answer Peter and you are correct I do have enough information to build the file but for some unknown reason I have not been successful. I have an IT degree and that compounds the embarrassment caused by this matter. In my defence I think it's an age thing; I've forgotten more that I've ever learnt. -- Regards, Phil From mats at wichmann.us Wed Feb 26 19:22:06 2020 From: mats at wichmann.us (Mats Wichmann) Date: Wed, 26 Feb 2020 17:22:06 -0700 Subject: [Tutor] Basic ElementTree - another question In-Reply-To: <615a51f4-7aaf-a3ae-8a7f-491736b7147b@gmail.com> References: <5a40a5d6-54f2-e934-7aa2-16a720da394b@gmail.com> <615a51f4-7aaf-a3ae-8a7f-491736b7147b@gmail.com> Message-ID: <8e5e60d4-8969-0b72-fdd0-9459d240fa52@wichmann.us> On 2/26/20 5:12 PM, Phil wrote: > On 26/2/20 7:19 pm, Peter Otten wrote: >> These building blocks should be enough to generate the desired xml. > > Thank you for taking the time to answer Peter and you are correct I do > have enough information to build the file but for some unknown reason I > have not been successful. > > I have an IT degree and that compounds the embarrassment caused by this > matter. In my defence I think it's an age thing; I've forgotten more > that I've ever learnt. Don't be embarrassed. That "forgotten much" thing applies to many of us!. You can only retain what you use, and those bits would come back if you needed them, the mind is an interesting thing. We'll continue to try to help as we can. There's also: XML Is Evil. There are more concised ways to build textual representations of data that actually work quite well without exploding our brains. Taking 100-ish bytes of data and wrapping it in tags to blow the amount up to 300+ bytes is, well... if only machines have to deal with it, it doesn't matter too much. Supposedly. Just sayin' (and I know, not actually helpin'). From dfjennings at gmail.com Wed Feb 26 20:14:06 2020 From: dfjennings at gmail.com (Don Jennings) Date: Wed, 26 Feb 2020 20:14:06 -0500 Subject: [Tutor] Basic ElementTree - another question In-Reply-To: <615a51f4-7aaf-a3ae-8a7f-491736b7147b@gmail.com> References: <5a40a5d6-54f2-e934-7aa2-16a720da394b@gmail.com> <615a51f4-7aaf-a3ae-8a7f-491736b7147b@gmail.com> Message-ID: <2D6906C7-7AF6-4C82-A055-1DF70D076401@gmail.com> > On Feb 26, 2020, at 7:12 PM, Phil wrote: > > On 26/2/20 7:19 pm, Peter Otten wrote: >> These building blocks should be enough to generate the desired xml. > > Thank you for taking the time to answer Peter and you are correct I do have enough information to build the file but for some unknown reason I have not been successful. The lack of success can take many forms. I encourage you to paste: - code which you have tried - the full traceback of any errors so we can be more helpful. Best, Don From PyTutor at DancesWithMice.info Wed Feb 26 21:01:35 2020 From: PyTutor at DancesWithMice.info (David L Neil) Date: Thu, 27 Feb 2020 15:01:35 +1300 Subject: [Tutor] overlapping tuples In-Reply-To: References: Message-ID: <0a6ff5ca-46e7-4562-cce3-d49adc66236d@DancesWithMice.info> On 27/02/20 5:44 AM, Narasimharao Nelluri wrote: > I got stuck at below problem for couple of days, can you guys help tp solve > this problem. This seems like a 'homework' assignment. Please declare it as such, so that we know to help you along your Python learning path... > if overlap is None: > overlap = [] Good technique! - although question need for a second parameter at all? > for fir,sec in zip(old_list,old_list[1:]): > > if fir[1] >= sec[0] and fir[1] <=sec[1]: > overlap.append(fir) > > if sec[0] >= fir[0] and sec[1] <= fir[1]: > overlap.append(sec) Please write your understanding of the above, more-or-less in English as if you were instructing another person (pseudo-code). Second question (after you've tackled the above): are you preferring to use different words in the explanation from those used for variable names in the code? Room for code-improvement there? > print(overlap) Recommend instead of printing 'inside', returning the result of the function. Then your main-line can change from - to -: > list1 = [(1,10),(15,20),(101,110)] > tuple_overlap(list1) overlaps = tuple_overlap( [(1,10),(15,20),(101,110)] ) print( "Overlap 1 =", overlaps ) Plus, free bonus! Should you ever wish to use tuple_overlap() within some other function, it will be ready to deliver... In fact (free ultra-bonus!) you have expected results in-mind (which you worked-out 'manually' before-hand - another good practice!), so you could ask Python to check/test for you: assert overlaps == [(1, 20), (15, 20)] # second test Will leave it to you to note what happens when the assert-ion ('this is what I believe to be true') is correct, and what when it proves wrong... (there are whole Python libraries to accomplish 'automated testing', which you could research later - but for now, simple is sweet!) -- Regards =dn From phillor9 at gmail.com Thu Feb 27 00:59:12 2020 From: phillor9 at gmail.com (Phil) Date: Thu, 27 Feb 2020 16:29:12 +1030 Subject: [Tutor] Basic ElementTree - another question In-Reply-To: <2D6906C7-7AF6-4C82-A055-1DF70D076401@gmail.com> References: <5a40a5d6-54f2-e934-7aa2-16a720da394b@gmail.com> <615a51f4-7aaf-a3ae-8a7f-491736b7147b@gmail.com> <2D6906C7-7AF6-4C82-A055-1DF70D076401@gmail.com> Message-ID: <4cbbe636-48f3-5112-d665-536837413de0@gmail.com> On 27/2/20 11:44 am, Don Jennings wrote: > The lack of success can take many forms. I encourage you to paste: > - code which you have tried > - the full traceback of any errors OK Don, thank you for your patience. This is what I need the file to look like: ?? ??? 1 ? and this is what I've currently got: ? ? This is the code that generates the above file: from xml.etree import ElementTree as ET ? gpx = ET.Element("gpx") ? wpt = ET.SubElement(gpx, "wpt", lat="-32.506533000", lon= "137.740017000") ? name = ET.SubElement(gpx, "name", name="1") There aren't any syntax errors in the code, only logic errors. The logic errors generate? the wpt block ending symbol at the end of the first line and the name entry is completely wrong. The pasted code is just a sample of what I've tried. I've attempted to adapt every piece of example code that I can find but without success. Getting lat and lon on the same line, without the block closing symbol, has been the source of the problem. -- Regards, Phil From iamsatyabrata428 at gmail.com Thu Feb 27 00:09:27 2020 From: iamsatyabrata428 at gmail.com (SATYABRATA DATTA) Date: Thu, 27 Feb 2020 10:39:27 +0530 Subject: [Tutor] =?utf-8?q?I_don=E2=80=99t_know_how_to_generate_a_number_?= =?utf-8?q?of_data_points_from_this_part_of_code?= Message-ID: I have a package and this part is written to calculate the quantity called ?action? at a output temperature T. But now I need a set of ?action? at slightly different temperatures say T+1e-3,T+3*1e-3,T+4*1e-3? Etc.Since I am a beginner at python I can?t figure out where in. This definition I have to put a loop to print different values of action at slightly different temperatures. The part of code is attached as below def _tunnelFromPhaseAtT(T, phases, start_phase, V, dV, phitol, overlapAngle, nuclCriterion, fullTunneling_params, verbose, outdict):""" Find the lowest action tunneling solution. Return ``nuclCriterion(S,T)``, and store a dictionary describing the transition in outdict for key `T`. """try: T = T[0] # need this when the function is run from optimize.fminexcept: passif T in outdict: return nuclCriterion(outdict[T]['action'], T) def fmin(x): return optimize.fmin(V, x, args=(T,), xtol=phitol, ftol=np.inf, disp=False) # Loop through all the phases, adding acceptable minima x0 = fmin(start_phase.valAt(T)) V0 = V(x0, T) tunnel_list = []for key in phases.keys(): if key == start_phase.key: continue p = phases[key] if (p.T[0] > T or p.T[-1] < T): continue x1 = fmin(p.valAt(T)) V1 = V(x1, T) if V1 >= V0: continue tdict = dict(low_vev=x1, high_vev=x0, Tnuc=T, low_phase=key, high_phase=start_phase.key) tunnel_list.append(tdict)# Check for overlapif overlapAngle > 0: excluded = [] cos_overlap = np.cos(overlapAngle * np.pi/180) for i in xrange(1, len(tunnel_list)): for j in xrange(i): xi = tunnel_list[i]['low_vev'] xj = tunnel_list[j]['low_vev'] xi2 = np.sum((xi-x0)**2) xj2 = np.sum((xj-x0)**2) dotij = np.sum((xj-x0)*(xi-x0)) if dotij >= np.sqrt(xi2*xj2) * cos_overlap: excluded.append(i if xi2 > xj2 else j) for i in sorted(excluded)[::-1]: del tunnel_list[i]# Get rid of the T parameter for V and dVdef V_(x,T=T,V=V): return V(x,T)def dV_(x,T=T,dV=dV): return dV(x,T)# For each item in tunnel_list, try tunneling lowest_action = np.inf lowest_tdict = dict(action=np.inf)for tdict in tunnel_list: x1 = tdict['low_vev'] try: print("Tunneling from phase %s to phase %s at T=%0.4g" % (tdict['high_phase'], tdict['low_phase'], T)) print("high_vev =", tdict['high_vev']) print("low_vev =", tdict['low_vev']) tobj = pathDeformation.fullTunneling( [x1,x0], V_, dV_, callback_data=T, **fullTunneling_params) tdict['instanton'] = tobj tdict['action'] = tobj.action tdict['trantype'] = 1 except tunneling1D.PotentialError as err: if err.args[1] == "no barrier": tdict['trantype'] = 0 tdict['action'] = 0.0 elif err.args[1] == "stable, not metastable": tdict['trantype'] = 0 tdict['action'] = np.inf else: print("Unexpected error message.") raise if tdict['action'] <= lowest_action: lowest_action = tdict['action'] lowest_tdict = tdict outdict[T] = lowest_tdictreturn nuclCriterion(lowest_action, T) From alan.gauld at yahoo.co.uk Thu Feb 27 12:41:41 2020 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 27 Feb 2020 17:41:41 +0000 Subject: [Tutor] =?utf-8?q?I_don=E2=80=99t_know_how_to_generate_a_number_?= =?utf-8?q?of_data_points_from_this_part_of_code?= In-Reply-To: References: Message-ID: On 27/02/2020 05:09, SATYABRATA DATTA wrote: > I have a package and this part is written to calculate the quantity called > ?action? at a output temperature T. But now I need a set of ?action? at > slightly different temperatures say T+1e-3,T+3*1e-3,T+4*1e-3? Etc.Since I > am a beginner at python I can?t figure out where in. This definition I have > to put a loop to print different values of action at slightly different > temperatures. The part of code is attached as below Unfortunately your code seems to have been mangled by the mail system so the indentation is all messed up. Can you repost the code (pasted into the message, not attached) ensuring you post using Plain Text format and not HTML or rich text. Thanks. -- 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 From alan.gauld at yahoo.co.uk Thu Feb 27 12:44:06 2020 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 27 Feb 2020 17:44:06 +0000 Subject: [Tutor] Basic ElementTree - another question In-Reply-To: <4cbbe636-48f3-5112-d665-536837413de0@gmail.com> References: <5a40a5d6-54f2-e934-7aa2-16a720da394b@gmail.com> <615a51f4-7aaf-a3ae-8a7f-491736b7147b@gmail.com> <2D6906C7-7AF6-4C82-A055-1DF70D076401@gmail.com> <4cbbe636-48f3-5112-d665-536837413de0@gmail.com> Message-ID: On 27/02/2020 05:59, Phil wrote: > > > ?? > ??? 1 > ? > > > and this is what I've currently got: > > > ? > ? > > > This is the code that generates the above file: > > from xml.etree import ElementTree as ET > > ? gpx = ET.Element("gpx") > > ? wpt = ET.SubElement(gpx, "wpt", lat="-32.506533000", lon= > "137.740017000") > ? name = ET.SubElement(gpx, "name", name="1") Notice you said name was a sub-element of gpx. That's what you got. But you want name to be a sub-element of wpt... -- 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 From akleider at sonic.net Thu Feb 27 12:55:56 2020 From: akleider at sonic.net (Alex Kleider) Date: Thu, 27 Feb 2020 09:55:56 -0800 Subject: [Tutor] =?utf-8?q?I_don=E2=80=99t_know_how_to_generate_a_number_?= =?utf-8?q?of_data_points_from_this_part_of_code?= In-Reply-To: References: Message-ID: <0dc0ccc215bc9e248fec2687803edb06@sonic.net> On 2020-02-26 21:09, SATYABRATA DATTA wrote: > I have a package and this part is written to calculate the quantity > called > ?action? at a output temperature T. But now I need a set of ?action? at > slightly different temperatures say T+1e-3,T+3*1e-3,T+4*1e-3? Etc.Since > I > am a beginner at python I can?t figure out where in. This definition I > have > to put a loop to print different values of action at slightly different > temperatures. The part of code is attached as below I don't pretend to understand (or to even have looked very hard) at the code you submitted but based only on what you've written above: Assuming you have a function that calculates something (you are calling it 'action') based on a quantity, in your case it seems to be a temperature _you_ can create a function: def action_at_t(temp): .... return action Once you have a collection of temperatures: collection_of_temperatures: results = [] # results = set() for temp in collection_of_temperatures: results.append(action_at_t(temp)) # use 'add' vs 'append' if using a set for res in results: print(res) If you know about list comprehension that would be even better. You might be able to generate your collection_of_temperatures using the range function. Comments are in case you really want a set rather than a list (which I doubt.) A dict keyed by temperature would seem to me to be what would be best. From iamsatyabrata428 at gmail.com Thu Feb 27 15:53:57 2020 From: iamsatyabrata428 at gmail.com (SATYABRATA DATTA) Date: Fri, 28 Feb 2020 02:23:57 +0530 Subject: [Tutor] Need help to print outputs on seperate files and avoid some unwanted error messages which can stop my code at some mid point Message-ID: There is some code *import math class Vector(): def __init__(self,vx,vy,vz): self.x=vx self.y=vy self.z=vz def norm(self): xx=self.x**2 yy=self.y**2 zz=self.z**2 return math.sqrt(xx+yy+zz)* Now in the run file *import math* *import numpy as np* *from Desktop import Test* *def random_range(n, min, max):* *return min + np.random.random(n) * (max - min)* *filenumber = 0* *x=random_range(20,2,9)* *y=random_range(20,2,9)* *z=random_range(20,2,9)* *trial_args = np.stack((x, y, z), axis=-1)* *for x, y, z in trial_args:* * model=Test.Vector(x,y,z)* * if model.norm() > 5:* * filenumber += 1* * filename = str(filenumber)* * with open(filename+'.txt', 'w') as f:* * print(x, y, z, '=>', model.norm(), * Now let say I have a similar program which need two operator ouptput means model.operation1 #that produces some output which is used by operation2 model.operation2 Not I want to print print(x,y,z,?=>?,model.operation1,\n model.operation2,file=(my specified folder)f How to write that(In case of my original program the model.operation1 and model.operation2 when given print command gives ouput in python shell). But since there happens to be some error at some specific points(x,y,z) which causes my program to stop at some intermediate point. So my trick is to print each output to seperate files wherether errorful or right and than I can manually search which outputs are ok for me From PyTutor at DancesWithMice.info Thu Feb 27 17:42:17 2020 From: PyTutor at DancesWithMice.info (David L Neil) Date: Fri, 28 Feb 2020 11:42:17 +1300 Subject: [Tutor] Need help to print outputs on seperate files and avoid some unwanted error messages which can stop my code at some mid point In-Reply-To: References: Message-ID: <46176525-1d1d-d22d-814a-8f80d88091a4@DancesWithMice.info> On 28/02/20 9:53 AM, SATYABRATA DATTA wrote: > There is some code > *import math class Vector(): def __init__(self,vx,vy,vz): self.x=vx > self.y=vy self.z=vz def norm(self): xx=self.x**2 yy=self.y**2 zz=self.z**2 > return math.sqrt(xx+yy+zz)* > > Now in the run file > *import math* > *import numpy as np* > > *from Desktop import Test* > *def random_range(n, min, max):* > *return min + np.random.random(n) * (max - min)* > *filenumber = 0* > *x=random_range(20,2,9)* > *y=random_range(20,2,9)* > *z=random_range(20,2,9)* > > *trial_args = np.stack((x, y, z), axis=-1)* > *for x, y, z in trial_args:* > * model=Test.Vector(x,y,z)* > * if model.norm() > 5:* > * filenumber += 1* > * filename = str(filenumber)* > * with open(filename+'.txt', 'w') as f:* > * print(x, y, z, '=>', model.norm(), * > > Now let say I have a similar program which need two operator ouptput means > model.operation1 #that produces some output which is used by operation2 > model.operation2 > Not I want to print > print(x,y,z,?=>?,model.operation1,\n model.operation2,file=(my specified > folder)f > How to write that(In case of my original program the model.operation1 and > model.operation2 when given print command gives ouput in python shell). But > since there happens to be some error at some specific points(x,y,z) which > causes my program to stop at some intermediate point. So my trick is to > print each output to seperate files wherether errorful or right and than I > can manually search which outputs are ok for me Firstly, are you aware of Python's exception handling features? An "exception" may be the result of some fault, but does not have to be an "error". For example, 'raising an exception' (which is not a fault) is a common way to 'escape' from multiple layers of program-logic, ie loops within loops within loops... Accordingly, exception handling may be a good way to deal with those "some specific points(x,y,z)". As well as reporting the 'discovery' (and relevant source-data as the cause), it might also be possible to continue processing the rest of the data-set! Speaking personally, (from the users' perspective) I prefer to find 'all' such errors during a single run of the input phase, rather than to be forced to run once, find one 'error', correct it, run again, find another/later 'error', correct that, run again... This is efficiently accomplished by 'trapping' each error, as above, and setting a flag. At the end of all input processing, the flag can be checked, and if it is showing 'data is good' status, then the main process/analysis can proceed. Suggested reading/WebRef should appear here* Are you aware that we are permitted to have more than one file open at a time? Previously we coded three steps: open the file, read-from/write-to the file, and close the file. People quickly comprehended that because each file had an identifier (a "file descriptor"), it was quite possible to have multiple files (and fd-s), and therefore to output different types of data according to the purpose of each file. Recommend reading about files* These days we have an elegant and powerful construct at our disposal: the Context Manager (ie the with... construct). Interestingly, it is a common misunderstanding that only one entity can be handled (by one with... statement), at a time. In this case (which is perhaps the most common example of using a Context Manager!), that thinking leads to the self-imposed idea that one may only access a single file at a time. (which may or may not be the case for you - just something I've noticed with other learner-coders) There is a version of with ... which allows for multiple 'contexts' within a single code-block. (see manual) Accordingly, no reason why you shouldn't code multiple file-objects to be used in a single code-context! Recommend reading about Context Managers and the with... statement* That said, please review another discussion 'here' on the list, answered a few minutes ago: "Logging all requests...". For a while now, I have been making heavy use of the Python Standard Library's logging facility in all of my statistical/big-data projects - and not just for "logging" in its narrowest, ComSc, sense! The logging library performs a lot of 'file management' functions on my behalf. I will often have three 'logs' (better termed "output files"), recording the activities of the three phases, eg data-cleaning/input/selection, analysis, and reporting (yes, much of the time, even the 'output report' has also been produced as if it were a "log"! In the case of the input- and analytical-phases, 'messages' are highly uniform in format. Once a format is devised, the logger will happily churn-out line-after-line! The beauty of this approach, combined with some thoughts expressed above (IMHO), is that once the data/selection process runs 'clean', that entire log file can be comfortably ignored by the users, who are more interested in checking the analysis against their hypothesis! Plus, 'printing' to a log (actually a disk file) is *much* faster than having myriad debug-print statements cluttering the console (which is often very slow compared to the time required for the actual statistical analysis!) Recommend reading about the logging library* Lastly, am not sure what happened when the code-example was copy-pasted into the original post. All the extra asterisks (*), letter-fs, etc, present a severe challenge to (old) eyes... * Plus, I apologise for not providing Web.Refs - my 'broadband' connection is operating at tens of KB/sec, so waiting for 'heavy' web pages to display is more than mildly painful! I trust you will be able to find your way around the comprehensive Python 'docs' documentation web site! -- Regards =dn From dfjennings at gmail.com Thu Feb 27 18:06:55 2020 From: dfjennings at gmail.com (Don Jennings) Date: Thu, 27 Feb 2020 18:06:55 -0500 Subject: [Tutor] Basic ElementTree - another question In-Reply-To: References: <5a40a5d6-54f2-e934-7aa2-16a720da394b@gmail.com> <615a51f4-7aaf-a3ae-8a7f-491736b7147b@gmail.com> <2D6906C7-7AF6-4C82-A055-1DF70D076401@gmail.com> <4cbbe636-48f3-5112-d665-536837413de0@gmail.com> Message-ID: > On Feb 27, 2020, at 12:44 PM, Alan Gauld via Tutor wrote: > > On 27/02/2020 05:59, Phil wrote: > >> >> >> >> 1 >> >> >> >> and this is what I've currently got: >> >> >> >> >> >> >> This is the code that generates the above file: >> >> from xml.etree import ElementTree as ET >> >> gpx = ET.Element("gpx") >> >> wpt = ET.SubElement(gpx, "wpt", lat="-32.506533000", lon= >> "137.740017000") >> name = ET.SubElement(gpx, "name", name="1") > > Notice you said name was a sub-element of gpx. > That's what you got. But you want name to be a > sub-element of wpt? Adding to what Alan wrote, notice that you added the keyword argument name=?1?, so you get it as an attribute of the element ; however, what you want is for it to be the content of the element. In the latter case, assign it to the text attribute of the element as Peter pointed out: first = SubElement(e, "first") first.text = "1st child? Try fixing those 2 things, then post back your code if you run into difficulty, thanks. Best, Don From alan.gauld at yahoo.co.uk Thu Feb 27 18:59:52 2020 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 27 Feb 2020 23:59:52 +0000 Subject: [Tutor] Need help to print outputs on seperate files and avoid some unwanted error messages which can stop my code at some mid point In-Reply-To: References: Message-ID: On 27/02/2020 20:53, SATYABRATA DATTA wrote: > There is some code > *import math class Vector(): def __init__(self,vx,vy,vz): self.x=vx > self.y=vy self.z=vz def norm(self): xx=self.x**2 yy=self.y**2 zz=self.z**2 > return math.sqrt(xx+yy+zz)* > Please always post in Plain text to the tutor list - especially if including code. Otherwise, the whitespace gets mangled as above and it all becomes a meaningless mess. > Now in the run file > *import math* > *import numpy as np* > > *from Desktop import Test* This is probably a bad idea. I assume you have not actually created a package called Desktop but rather have just saved your Test module to your GUI desktop? While it may work its likely to go horribly wrong in the future, especially if anyone else tries to run your code. Its much better to create a project directory and save your modules there (and run your code from there) Nothing to do with your immediate issue but I thought it worth pointing out! > *def random_range(n, min, max):* > *return min + np.random.random(n) * (max - min)* > *filenumber = 0* > *x=random_range(20,2,9)* > *y=random_range(20,2,9)* > *z=random_range(20,2,9)* > > *trial_args = np.stack((x, y, z), axis=-1)* > *for x, y, z in trial_args:* > * model=Test.Vector(x,y,z)* > * if model.norm() > 5:* > * filenumber += 1* > * filename = str(filenumber)* > * with open(filename+'.txt', 'w') as f:* > * print(x, y, z, '=>', model.norm(), * > > causes my program to stop at some intermediate point. So my trick is to > print each output to seperate files wherether errorful or right and than I > can manually search which outputs are ok for me There are numerous ways to do this but the easiest is probably just to open two output files and write the strings(*) (rather than using print) to whichever file is appropriate. file.write() requires strings so you will need to convert x,y,z etc to a string using str(). Thats one advantage of using print, it does that 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 From mats at wichmann.us Thu Feb 27 19:06:58 2020 From: mats at wichmann.us (Mats Wichmann) Date: Thu, 27 Feb 2020 17:06:58 -0700 Subject: [Tutor] Need help to print outputs on seperate files and avoid some unwanted error messages which can stop my code at some mid point In-Reply-To: References: Message-ID: <7e0f7820-ed75-b962-72d7-bfeba157a53c@wichmann.us> On 2/27/20 4:59 PM, Alan Gauld via Tutor wrote: > On 27/02/2020 20:53, SATYABRATA DATTA wrote: >> There is some code >> *import math class Vector(): def __init__(self,vx,vy,vz): self.x=vx >> self.y=vy self.z=vz def norm(self): xx=self.x**2 yy=self.y**2 zz=self.z**2 >> return math.sqrt(xx+yy+zz)* >> > > Please always post in Plain text to the tutor list - especially if > including code. Otherwise, the whitespace gets mangled as above > and it all becomes a meaningless mess. indeed, because indentation is part of the Python language syntax this is crucially important, more so than other languages. I pasted the above snip into a Python code reformatter to see if it could swallow it and it said: error: cannot format: Cannot parse: From phillor9 at gmail.com Fri Feb 28 00:01:38 2020 From: phillor9 at gmail.com (Phil) Date: Fri, 28 Feb 2020 15:31:38 +1030 Subject: [Tutor] Basic ElementTree - another question - Solved In-Reply-To: References: <5a40a5d6-54f2-e934-7aa2-16a720da394b@gmail.com> <615a51f4-7aaf-a3ae-8a7f-491736b7147b@gmail.com> <2D6906C7-7AF6-4C82-A055-1DF70D076401@gmail.com> <4cbbe636-48f3-5112-d665-536837413de0@gmail.com> Message-ID: <336f8f90-f10a-b1dd-500d-c72a304cc4f8@gmail.com> On 28/2/20 4:14 am, Alan Gauld via Tutor wrote: > Notice you said name was a sub-element of gpx. > That's what you got. But you want name to be a > sub-element of wpt... Thank you Alan, that was indeed the problem and I don't know why I couldn't see that earlier. Don, I had already tried ".text =" but because of the error in the first line I had discounted that as a solution. I misunderstood Peter's offering and kept trying to apply that to the error in the first line. -- Regards, Phil From abdurauf.isokjonov at gmail.com Fri Feb 28 04:02:18 2020 From: abdurauf.isokjonov at gmail.com (Abdurauf Isokjonov) Date: Fri, 28 Feb 2020 18:02:18 +0900 Subject: [Tutor] (no subject) Message-ID: Even i?ve installed bs4, python says ?no module named bs4?. How to solve this? From narasimha928 at gmail.com Thu Feb 27 23:03:43 2020 From: narasimha928 at gmail.com (Narasimharao Nelluri) Date: Thu, 27 Feb 2020 20:03:43 -0800 Subject: [Tutor] overlapping tuples In-Reply-To: <0a6ff5ca-46e7-4562-cce3-d49adc66236d@DancesWithMice.info> References: <0a6ff5ca-46e7-4562-cce3-d49adc66236d@DancesWithMice.info> Message-ID: Hi David , Thanks for your feedback. Yes here i am solving Home work from one of my assignments. please check below i added your comments. NNELLURI-M-L13P:google_class nnelluri$ cat overlap.py #!/usr/bin/env python3 def tuple_overlap(old_list,overlap = None): old_list.sort() if overlap is None: overlap = [] for fir,sec in zip(old_list,old_list[1:]): if fir[1] >= sec[0] and fir[1] <=sec[1]:=======>Here i am validating 2nd element in first variablae is in-between seconds variable if it is there is a overlap overlap.append(fir) if sec[0] >= fir[0] and sec[1] <= fir[1]:=====> Here i am checking if first element in second variable is in-between second variable , there is a oberlap overlap.append(sec) overlap = sorted(overlap) return overlap overlaps = tuple_overlap( [(1,10),(15,20),(101,110)] ) print( "Overlap 1 =", overlaps ) assert overlaps == [] overlaps = tuple_overlap( [(1,20),(15,20),(101,110)]) print( "Overlap 2 =", overlaps ) assert overlaps ==[(1, 20), (15, 20)] overlaps = tuple_overlap( [(1,10),(15,20),(1,10),(1,10),(101,110)]) print( "Overlap 3 =", overlaps ) assert overlaps == [(1, 10), (1, 10),(1,10)] NNELLURI-M-L13P:google_class nnelluri$ Please let know if you know correct solution. Thanks Narasimha On Wed, Feb 26, 2020 at 6:02 PM David L Neil via Tutor wrote: > On 27/02/20 5:44 AM, Narasimharao Nelluri wrote: > > I got stuck at below problem for couple of days, can you guys help tp > solve > > this problem. > > This seems like a 'homework' assignment. Please declare it as such, so > that we know to help you along your Python learning path... > > > > if overlap is None: > > overlap = [] > > Good technique! > - although question need for a second parameter at all? > > > > for fir,sec in zip(old_list,old_list[1:]): > > > > if fir[1] >= sec[0] and fir[1] <=sec[1]: > > overlap.append(fir) > > > > if sec[0] >= fir[0] and sec[1] <= fir[1]: > > overlap.append(sec) > > Please write your understanding of the above, more-or-less in English as > if you were instructing another person (pseudo-code). > > Second question (after you've tackled the above): are you preferring to > use different words in the explanation from those used for variable > names in the code? Room for code-improvement there? > > > > print(overlap) > > Recommend instead of printing 'inside', returning the result of the > function. Then your main-line can change from - to -: > > > > list1 = [(1,10),(15,20),(101,110)] > > tuple_overlap(list1) > > overlaps = tuple_overlap( [(1,10),(15,20),(101,110)] ) > print( "Overlap 1 =", overlaps ) > > > Plus, free bonus! Should you ever wish to use tuple_overlap() within > some other function, it will be ready to deliver... > > > In fact (free ultra-bonus!) you have expected results in-mind (which you > worked-out 'manually' before-hand - another good practice!), so you > could ask Python to check/test for you: > > assert overlaps == [(1, 20), (15, 20)] # second test > > Will leave it to you to note what happens when the assert-ion ('this is > what I believe to be true') is correct, and what when it proves wrong... > > (there are whole Python libraries to accomplish 'automated testing', > which you could research later - but for now, simple is sweet!) > -- > Regards =dn > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From alan.gauld at yahoo.co.uk Fri Feb 28 05:13:01 2020 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 28 Feb 2020 10:13:01 +0000 Subject: [Tutor] overlapping tuples In-Reply-To: References: <0a6ff5ca-46e7-4562-cce3-d49adc66236d@DancesWithMice.info> Message-ID: On 28/02/2020 04:03, Narasimharao Nelluri wrote: > if fir[1] >= sec[0] and fir[1] <=sec[1]:=======>Here i am validating > 2nd element in first variablae is in-between seconds variable if it is > there is a overlap In python you can write that more concisely as: if sec[0] <= fir[1] <= sec[1]: > if sec[0] >= fir[0] and sec[1] <= fir[1]:=====> Here i am checking if > first element in second variable is in-between second variable , there is a > oberlap In that case shouldn't the index to sec be the same in both comparisons? The comparison style above would have eliminated that error... -- 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 From alan.gauld at yahoo.co.uk Fri Feb 28 05:15:20 2020 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 28 Feb 2020 10:15:20 +0000 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: On 28/02/2020 09:02, Abdurauf Isokjonov wrote: > Even i?ve installed bs4, python says ?no module named bs4?. How to solve > this? We need quite a bit more information. First what OS and Python versions are you using? Next, how did you install bs4 - the actual sequence of commands you typed. And can you show us the full error trace, not just a summary. There is usually a lot of useful information in the error message. -- 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 From iamsatyabrata428 at gmail.com Fri Feb 28 10:52:47 2020 From: iamsatyabrata428 at gmail.com (SATYABRATA DATTA) Date: Fri, 28 Feb 2020 21:22:47 +0530 Subject: [Tutor] Facing problem in printing a large number of points for my problem Message-ID: I previously explained my problem that I want to print each output to specific .txt file and than repeatedly do it for several points and even if there some error which generally stops the program at some midway , can't happen in this case For this I have used the simple line which is giving each numbered output to specific folder for %i in (1,5) do python.exe test_model.py > tests/TestModel_%i.txt But I am getting only 1 and 5 as output in the above example. So I need to print nearly 100 s of such i by such simple command without using additional for loops I think here I can get a suitable answer From mats at wichmann.us Fri Feb 28 13:52:36 2020 From: mats at wichmann.us (Mats Wichmann) Date: Fri, 28 Feb 2020 11:52:36 -0700 Subject: [Tutor] Facing problem in printing a large number of points for my problem In-Reply-To: References: Message-ID: <68c9aa76-d36c-d61b-f37b-013f1dafd611@wichmann.us> On 2/28/20 8:52 AM, SATYABRATA DATTA wrote: > I previously explained my problem that I want to print each output to > specific .txt file and than repeatedly do it for several points and even if > there some error which generally stops the program at some midway , can't > happen in this case > > For this I have used the simple line which is giving each numbered output > to specific folder > for %i in (1,5) do python.exe test_model.py > tests/TestModel_%i.txt > > But I am getting only 1 and 5 as output in the above example. what language is that? some kind of shell? you seem to be expecting that construct to generate "all the integers from 1 to 5" and you've already observed that you're getting just 1 and 5. From __peter__ at web.de Fri Feb 28 11:46:02 2020 From: __peter__ at web.de (Peter Otten) Date: Fri, 28 Feb 2020 17:46:02 +0100 Subject: [Tutor] overlapping tuples References: <0a6ff5ca-46e7-4562-cce3-d49adc66236d@DancesWithMice.info> Message-ID: Narasimharao Nelluri wrote: > Hi David , > Thanks for your feedback. Yes here i am solving Home work from one of my > assignments. > please check below i added your comments. > > NNELLURI-M-L13P:google_class nnelluri$ cat overlap.py > #!/usr/bin/env python3 > > > def tuple_overlap(old_list,overlap = None): > > old_list.sort() > if overlap is None: > overlap = [] > for fir,sec in zip(old_list,old_list[1:]): > > if fir[1] >= sec[0] and fir[1] <=sec[1]:=======>Here i am validating > 2nd element in first variablae is in-between seconds variable if it is > there is a overlap > overlap.append(fir) > > if sec[0] >= fir[0] and sec[1] <= fir[1]:=====> Here i am checking if > first element in second variable is in-between second variable , there is > a oberlap > overlap.append(sec) > > overlap = sorted(overlap) > return overlap > > > overlaps = tuple_overlap( [(1,10),(15,20),(101,110)] ) > print( "Overlap 1 =", overlaps ) > assert overlaps == [] > > overlaps = tuple_overlap( [(1,20),(15,20),(101,110)]) > print( "Overlap 2 =", overlaps ) > assert overlaps ==[(1, 20), (15, 20)] > > overlaps = tuple_overlap( [(1,10),(15,20),(1,10),(1,10),(101,110)]) > print( "Overlap 3 =", overlaps ) > assert overlaps == [(1, 10), (1, 10),(1,10)] > > > NNELLURI-M-L13P:google_class nnelluri$ > > > Please let know if you know correct solution. I would have to work it out myself ;) but I think I can give you a few hints: If you add some debugging information >>> tuple_overlap( [(1,10, "a"),(15,20, "b"),(1,10, "c"),(1,10, "d"), (101,110, "e")]) [(1, 10, 'a'), (1, 10, 'c'), (1, 10, 'c'), (1, 10, 'd')] you can see that (1, 10, "c") occurs twice. Your code checks adjacent tuples, and if three tuples overlap the tuple in the middle may be added twice. In this situation you need to ensure that every tuple is added only once. But not only that: >>> tuple_overlap([(0, 10), (1, 3)]) [(1, 3)] Hm, one overlapping tuple? with what? with itself? If tuple A overlaps with tuple B, tuple B overlaps with A, i. e. instead of two independent checks you can use a single one, and add both tuples if there is an overlap. That single check may be easier to implement if you ask yourself the opposite question: when do the tuples not overlap? Also consider [(0, 10), (1, 3), (5, 20)]. Clearly you need to decide what to do if t1 overlaps with t2 and t3, but t2 not with t2. Finally, does your task say something about independent overlaps, e. g. what should be returned by tuple_overlap([(0, 2), (1, 3), (5, 7), (6, 8)]) ? From breamoreboy at gmail.com Fri Feb 28 13:57:29 2020 From: breamoreboy at gmail.com (Mark Lawrence) Date: Fri, 28 Feb 2020 18:57:29 +0000 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: On 28/02/2020 09:02, Abdurauf Isokjonov wrote: > Even i?ve installed bs4, python says ?no module named bs4?. How to solve > this? The odds are you've installed beautifulsoup in one version of python but are trying to run it from another. To check try running things like the following from the command line:- python -c "import bs4" python2.7 -c "import bs4" python3.6 -c "import bs4" python3.7 -c "import bs4" What happens? -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From mats at wichmann.us Fri Feb 28 14:54:34 2020 From: mats at wichmann.us (Mats Wichmann) Date: Fri, 28 Feb 2020 12:54:34 -0700 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: On 2/28/20 11:57 AM, Mark Lawrence wrote: > On 28/02/2020 09:02, Abdurauf Isokjonov wrote: >> Even i?ve installed bs4, python says ?no module named bs4?. How to solve >> this? > > The odds are you've installed beautifulsoup in one version of python but > are trying to run it from another.? To check try running things like the > following from the command line:- > > python -c "import bs4" > python2.7 -c "import bs4" > python3.6 -c "import bs4" > python3.7 -c "import bs4" > > What happens? > Indeed, to tie this up a little more with what both Alan and Mark have said: This has a very simple explanation: it's *always* a problem with the path, the Python you're running is looking in a set of places, and the install put it in a place that wasn't in that set. The path your Python interpreter searches you can get like this: >>> import sys >>> print(sys.path) Here is the most reliable way to make sure you don't get a mismatch - for the Python you're going to run (note this may happen to be named python, or python3, or py - the latter if you're using the Python Launcher on Windows - use the name you're going to use to run it): python -m pip install --user bs4 we pretty much *never* know that pip, if called as a standalone command, matches exactly the Python we're going to use. === Sadly, this story gets more complicated if you're using an advanced editor like PyCharm or VS Code or other - they may helpfully try to construct a virtualenv for you for your project, so you on the command line don't necessarily *know* which Python to invoke. In that case, you should install through the environment in the IDE, so it has a chance of getting it right. And if you're using Anconda (or Miniconda), that has its own installation environment that you should make use of (the Conda world likes to construct virtualenvs too, by the way): conda install bs4 From PyTutor at DancesWithMice.info Fri Feb 28 15:31:26 2020 From: PyTutor at DancesWithMice.info (David L Neil) Date: Sat, 29 Feb 2020 09:31:26 +1300 Subject: [Tutor] overlapping tuples In-Reply-To: References: <0a6ff5ca-46e7-4562-cce3-d49adc66236d@DancesWithMice.info> Message-ID: Hi, On 28/02/20 5:03 PM, Narasimharao Nelluri wrote: > Hi David , > Thanks for your feedback. Yes here i am solving Home work?from one of my > assignments. Are you attempting one of the Google certificates on-line - I saw one (I cannot recall its exact name, but something about) computer support using Python? > ? ? if fir[1] >= sec[0] and fir[1] <=sec[1]:=======>Here i am > validating 2nd element in first variablae?is in-between seconds variable > if it is there is a overlap > ? ? ? overlap.append(fir) > > ? ? if sec[0] >= fir[0] and sec[1] <= fir[1]:=====> Here i am checking > if first element in second variable is in-between second variable , > there is a oberlap > ? ? ? overlap.append(sec) When writing 'pseudo-code' try to avoid using IT-terminology, eg "variable"; because it 'puts on blinkers' - channels our brains to think (only) along certain lines! I appreciate that in this case, it is made more difficult, because the assignment has (probably) been phrased as an academic exercise. Can you think of example data/circumstances for such data and algorithm in 'real life'? Now you can use 'real words', even if pseudo-code is more logical than 'correct English grammar'. (in fact, apart from the fact that I may not understand you 'here'; there's no rule that disallows the expression of "pseudo-code" in any other language - whichever you prefer. If it helps you to produce better code, then that's the 'correct' answer!) At the risk of stating the obvious, when solving a logic problem, it pays to be methodical, ie be equally-logical in one's approach. Please note how the current approach first examines the *second* element in the first tuple, and *later* moves to the first. Wouldn't it be more logical to work 'from left to right' and 'first to second'? (whereas English is written left-to-right and top-to-bottom, please forgive me if your preferred language is otherwise - 'your logic' won't align with my view) Perhaps even consider both elements of the first tuple against the first element of the second tuple, and subsequently against the second element of the second tuple... Lets design some simple tests: 1 no overlap, (second tuple is completely 'below the range') 2 no overlap, 'above' 3 (second tuple's) first element is 'inside' but second is not 4 ... first element is 'outside' but second is 'in' 5 ... both elements are 'inside' the first tuple's range. First question: are there other possible situations that I didn't notice? Second question (and it's very important): how is "overlap" defined? In other words, which of these five tests should result in True (overlap), and which will be considered False (no overlap)? NB I see two understandings of the word. (You seem to use one, whereas I (without seeing the exact question, but checking against the sample data) prefer the other) Consideration/a question of design-approach: Without understanding *exactly* what is required, how can we write a correct program/answer? Was the sample data provided as part of the question, or did you make it up? If it was provided, does it help us understand "overlap"? If you made it up, did you consider *all* the (reasonable) possible cases? Regardless, it is a good idea to make your own set of test data to check your own code - let's call that "Unit Testing". Once all is working to our satisfaction, we then to use the provided-data as part of delivering the assignment-answer - we can call that "Acceptance Testing". Let's follow-on from the logic-tests (developed above) and create some sample data: first tuple: ( 4, 7 ) second tuples: 1 ( 1, 3 ) 2 ( 8, 9 ) 3 ( 5, 9 ) 4 ( 1, 6 ) 5 ( 5, 6 ) Depending upon the answers, above (and any realisations (learning!)), by this point, the current-code might already be consigned to the 'bit-bucket of cyber-space'. However, *before* you are tempted to start re-coding, let's continue 'on paper', by working-through the newly-developed test scenarios ("unit tests") against the first if-statement: if fir[1] >= sec[0] and fir[1] <=sec[1] 1 7 >= 1 and 7 <= 3 2 7 >= 8 and 7 <= 9 3 7 >= 5 and 7 <= 9 4 7 >= 6 and 7 <= 6 5 7 >= 5 and 7 <= 6 Which of these is True, and how does that meet the "specification"? (ie the assignment's requirements) Once again, ignoring any fresh thoughts about 'the solution', let's repeat the exercise for the second if-statement: if sec[0] >= fir[0] and sec[1] <= fir[1]: 1 1 >= 4 and 3 <= 7 2 8 >= 4 and 9 <= 7 3 5 >= 4 and 9 <= 7 4 1 >= 4 and 6 <= 7 5 5 >= 4 and 6 <= 7 Again, please consider: which of these is True? Remember the earlier suggestion of using assert? Could you now build five (or more) assert statements, one for each unit test? Ahah!. So, that's how we carry-through all the preparatory work 'on paper' into work-on-the-machine! Now, before charging head-long towards the 'finish line', let's take another step, even further back: where did the problem "my current solution doest solve all the cases" actually arise? a) understanding the assignment (in IT we call that a "specification") b) understanding the meaning/definition of words in the spec c) understanding/possessing sufficient skills in Python d) understanding of Boolean logic/algebra (True/False, and, or, not...) e) developing test-data f) somewhere else along the way I expect that you are primarily focused on completing your assignment, so if you're confident you'll be wanting to 'get it done' and 'hand it in'. Let us know how you get on... Nevertheless, to succeed in the course, may I recommend that you try answering these questions in a reply 'here', and list-members will likely respond with helpful advice which will help you, not only to learn Python, but to become a better professional programmer... -- Regards =dn From PyTutor at DancesWithMice.info Fri Feb 28 17:21:25 2020 From: PyTutor at DancesWithMice.info (David L Neil) Date: Sat, 29 Feb 2020 11:21:25 +1300 Subject: [Tutor] Facing problem in printing a large number of points for my problem In-Reply-To: References: Message-ID: On 29/02/20 4:52 AM, SATYABRATA DATTA wrote: > I previously explained my problem that I want to print each output to > specific .txt file and than repeatedly do it for several points and even if > there some error which generally stops the program at some midway , can't > happen in this case > > For this I have used the simple line which is giving each numbered output > to specific folder > for %i in (1,5) do python.exe test_model.py > tests/TestModel_%i.txt > > But I am getting only 1 and 5 as output in the above example. > So I need to print nearly 100 s of such i by such simple command without > using additional for loops > I think here I can get a suitable answer Would it be reasonable to finish one conversation ("previously explained") before starting another? How do we know if that is solved? ...if we are actually helping? ...if you are learning anything? -- Regards =dn From narasimha928 at gmail.com Fri Feb 28 15:06:49 2020 From: narasimha928 at gmail.com (Narasimharao Nelluri) Date: Fri, 28 Feb 2020 12:06:49 -0800 Subject: [Tutor] overlapping tuples In-Reply-To: References: <0a6ff5ca-46e7-4562-cce3-d49adc66236d@DancesWithMice.info> Message-ID: Hi peter, Thanks a lot for your inputs, i will work your inputs and let you know. For the last question if there is independent over lap for ex: in below case python should return [(5,7),(6,8)] tuple_overlap([(0, 2), (1, 3), (5, 7), (6, 8)]) I tried different combinations as of i didn't get perfect solution. Thanks Narasimha. On Fri, Feb 28, 2020 at 11:40 AM Peter Otten <__peter__ at web.de> wrote: > Narasimharao Nelluri wrote: > > > Hi David , > > Thanks for your feedback. Yes here i am solving Home work from one of my > > assignments. > > please check below i added your comments. > > > > NNELLURI-M-L13P:google_class nnelluri$ cat overlap.py > > #!/usr/bin/env python3 > > > > > > def tuple_overlap(old_list,overlap = None): > > > > old_list.sort() > > if overlap is None: > > overlap = [] > > for fir,sec in zip(old_list,old_list[1:]): > > > > if fir[1] >= sec[0] and fir[1] <=sec[1]:=======>Here i am validating > > 2nd element in first variablae is in-between seconds variable if it is > > there is a overlap > > overlap.append(fir) > > > > if sec[0] >= fir[0] and sec[1] <= fir[1]:=====> Here i am checking if > > first element in second variable is in-between second variable , there is > > a oberlap > > overlap.append(sec) > > > > overlap = sorted(overlap) > > return overlap > > > > > > overlaps = tuple_overlap( [(1,10),(15,20),(101,110)] ) > > print( "Overlap 1 =", overlaps ) > > assert overlaps == [] > > > > overlaps = tuple_overlap( [(1,20),(15,20),(101,110)]) > > print( "Overlap 2 =", overlaps ) > > assert overlaps ==[(1, 20), (15, 20)] > > > > overlaps = tuple_overlap( [(1,10),(15,20),(1,10),(1,10),(101,110)]) > > print( "Overlap 3 =", overlaps ) > > assert overlaps == [(1, 10), (1, 10),(1,10)] > > > > > > NNELLURI-M-L13P:google_class nnelluri$ > > > > > > Please let know if you know correct solution. > > I would have to work it out myself ;) but I think I can give you a few > hints: > > If you add some debugging information > > >>> tuple_overlap( [(1,10, "a"),(15,20, "b"),(1,10, "c"),(1,10, "d"), > (101,110, "e")]) > [(1, 10, 'a'), (1, 10, 'c'), (1, 10, 'c'), (1, 10, 'd')] > > you can see that (1, 10, "c") occurs twice. Your code checks adjacent > tuples, and if three tuples overlap the tuple in the middle may be added > twice. In this situation you need to ensure that every tuple is added only > once. > > But not only that: > > >>> tuple_overlap([(0, 10), (1, 3)]) > [(1, 3)] > > Hm, one overlapping tuple? with what? with itself? > > If tuple A overlaps with tuple B, tuple B overlaps with A, i. e. instead > of > two independent checks you can use a single one, and add both tuples if > there is an overlap. > > That single check may be easier to implement if you ask yourself the > opposite question: when do the tuples not overlap? > > Also consider [(0, 10), (1, 3), (5, 20)]. > > Clearly you need to decide what to do if t1 overlaps with t2 and t3, but > t2 > not with t2. > > Finally, does your task say something about independent overlaps, e. g. > what > should be returned by > > tuple_overlap([(0, 2), (1, 3), (5, 7), (6, 8)]) > > ? > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From narasimha928 at gmail.com Fri Feb 28 15:34:15 2020 From: narasimha928 at gmail.com (Narasimharao Nelluri) Date: Fri, 28 Feb 2020 12:34:15 -0800 Subject: [Tutor] overlapping tuples In-Reply-To: References: <0a6ff5ca-46e7-4562-cce3-d49adc66236d@DancesWithMice.info> Message-ID: Looks like misunderstood your question, even if independent over lap we should return all the over lap tuples in this case we should return all the tuples in a list. because here two sets of over laps, (0,2) overlaps with (1,3) and there is a overlap between (5,7) and (6,8). tuple_overlap([(0, 2), (1, 3), (5, 7), (6, 8)]) Thanks Narasimha. On Fri, Feb 28, 2020 at 12:06 PM Narasimharao Nelluri < narasimha928 at gmail.com> wrote: > Hi peter, > > Thanks a lot for your inputs, i will work your inputs and let you know. > For the last question if there is independent over lap for ex: in below > case python should return [(5,7),(6,8)] > > tuple_overlap([(0, 2), (1, 3), (5, 7), (6, 8)]) > > I tried different combinations as of i didn't get perfect solution. > > Thanks > Narasimha. > > > On Fri, Feb 28, 2020 at 11:40 AM Peter Otten <__peter__ at web.de> wrote: > >> Narasimharao Nelluri wrote: >> >> > Hi David , >> > Thanks for your feedback. Yes here i am solving Home work from one of my >> > assignments. >> > please check below i added your comments. >> > >> > NNELLURI-M-L13P:google_class nnelluri$ cat overlap.py >> > #!/usr/bin/env python3 >> > >> > >> > def tuple_overlap(old_list,overlap = None): >> > >> > old_list.sort() >> > if overlap is None: >> > overlap = [] >> > for fir,sec in zip(old_list,old_list[1:]): >> > >> > if fir[1] >= sec[0] and fir[1] <=sec[1]:=======>Here i am validating >> > 2nd element in first variablae is in-between seconds variable if it is >> > there is a overlap >> > overlap.append(fir) >> > >> > if sec[0] >= fir[0] and sec[1] <= fir[1]:=====> Here i am checking >> if >> > first element in second variable is in-between second variable , there >> is >> > a oberlap >> > overlap.append(sec) >> > >> > overlap = sorted(overlap) >> > return overlap >> > >> > >> > overlaps = tuple_overlap( [(1,10),(15,20),(101,110)] ) >> > print( "Overlap 1 =", overlaps ) >> > assert overlaps == [] >> > >> > overlaps = tuple_overlap( [(1,20),(15,20),(101,110)]) >> > print( "Overlap 2 =", overlaps ) >> > assert overlaps ==[(1, 20), (15, 20)] >> > >> > overlaps = tuple_overlap( [(1,10),(15,20),(1,10),(1,10),(101,110)]) >> > print( "Overlap 3 =", overlaps ) >> > assert overlaps == [(1, 10), (1, 10),(1,10)] >> > >> > >> > NNELLURI-M-L13P:google_class nnelluri$ >> > >> > >> > Please let know if you know correct solution. >> >> I would have to work it out myself ;) but I think I can give you a few >> hints: >> >> If you add some debugging information >> >> >>> tuple_overlap( [(1,10, "a"),(15,20, "b"),(1,10, "c"),(1,10, "d"), >> (101,110, "e")]) >> [(1, 10, 'a'), (1, 10, 'c'), (1, 10, 'c'), (1, 10, 'd')] >> >> you can see that (1, 10, "c") occurs twice. Your code checks adjacent >> tuples, and if three tuples overlap the tuple in the middle may be added >> twice. In this situation you need to ensure that every tuple is added >> only >> once. >> >> But not only that: >> >> >>> tuple_overlap([(0, 10), (1, 3)]) >> [(1, 3)] >> >> Hm, one overlapping tuple? with what? with itself? >> >> If tuple A overlaps with tuple B, tuple B overlaps with A, i. e. instead >> of >> two independent checks you can use a single one, and add both tuples if >> there is an overlap. >> >> That single check may be easier to implement if you ask yourself the >> opposite question: when do the tuples not overlap? >> >> Also consider [(0, 10), (1, 3), (5, 20)]. >> >> Clearly you need to decide what to do if t1 overlaps with t2 and t3, but >> t2 >> not with t2. >> >> Finally, does your task say something about independent overlaps, e. g. >> what >> should be returned by >> >> tuple_overlap([(0, 2), (1, 3), (5, 7), (6, 8)]) >> >> ? >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> https://mail.python.org/mailman/listinfo/tutor >> > From PyTutor at DancesWithMice.info Fri Feb 28 19:37:13 2020 From: PyTutor at DancesWithMice.info (David L Neil) Date: Sat, 29 Feb 2020 13:37:13 +1300 Subject: [Tutor] overlapping tuples In-Reply-To: References: <0a6ff5ca-46e7-4562-cce3-d49adc66236d@DancesWithMice.info> Message-ID: On 29/02/20 9:34 AM, Narasimharao Nelluri wrote: > Looks like misunderstood your question, even if independent over lap we > should return all the over lap tuples in this case we should return all the > tuples in a list. > because here two sets of over laps, (0,2) overlaps with (1,3) and there is > a overlap between (5,7) and (6,8). > > tuple_overlap([(0, 2), (1, 3), (5, 7), (6, 8)]) +1 Well done! Will be interesting to read how you develop things from here... -- Regards =dn From mats at wichmann.us Fri Feb 28 20:03:13 2020 From: mats at wichmann.us (Mats Wichmann) Date: Fri, 28 Feb 2020 18:03:13 -0700 Subject: [Tutor] overlapping tuples In-Reply-To: References: <0a6ff5ca-46e7-4562-cce3-d49adc66236d@DancesWithMice.info> Message-ID: <235a9339-d408-dd63-83a3-422157635de8@wichmann.us> On 2/28/20 1:06 PM, Narasimharao Nelluri wrote: > Hi peter, > > Thanks a lot for your inputs, i will work your inputs and let you know. For > the last question if there is independent over lap for ex: in below case > python should return [(5,7),(6,8)] > > tuple_overlap([(0, 2), (1, 3), (5, 7), (6, 8)]) > > I tried different combinations as of i didn't get perfect solution. One of the problems here is we don't have the problem definition - unless I've missed something, I don't know what "overlap" means, this question not being precise enough. A tuple of (0, 2) has two values in it. A tuple of (1, 3) has two values in it. None of the values in those two tuples match, so one reading of "overlap" suggests that there's no overlap between those two. Python supports sets, and in set theory, an "overlap" is the intersection - the elements that appear in both. So you could do this: >>> a = set((0, 1, 2)) >>> b = set((1, 2, 3)) >>> print(b.intersection(a)) set([1, 2]) >>> print a & b set([1, 2]) But this doesn't _appear_ to be what you're talking about at all. Instead, it _seems_ your tuples represent ranges of integers, with both endpoints included. Is that right? That's a different problem. But you can still use sets to work on this. Range gives you all the values, but range(x, y) is the values from x up to but not including y: >>> print(tuple(range(1, 4))) (1, 2, 3) so you want to add one to the end of the range. >>> a = set(range(0, 2+1)) >>> b = set(range(1, 3+1)) >>> c = set(range(5, 7+1)) >>> print(a, b, c) (set([0, 1, 2]), set([1, 2, 3]), set([5, 6, 7])) >>> print(a & b) set([1, 2]) >>> print(a & c) set([]) >>> if a & b: ... print("a and b overlap") ... a and b overlap >>> if a & c: ... print("a and c overlap") ... >>> Does that help you think about the problem? From narasimha928 at gmail.com Fri Feb 28 20:36:12 2020 From: narasimha928 at gmail.com (Narasimharao Nelluri) Date: Fri, 28 Feb 2020 17:36:12 -0800 Subject: [Tutor] overlapping tuples In-Reply-To: <235a9339-d408-dd63-83a3-422157635de8@wichmann.us> References: <0a6ff5ca-46e7-4562-cce3-d49adc66236d@DancesWithMice.info> <235a9339-d408-dd63-83a3-422157635de8@wichmann.us> Message-ID: Thanks Mats for your inputs. overlap means if two numbers share across two tuples For Ex if we take two tuples (1,20) and (15,20) there is a over lap because 15,6,17,18,19,20 numbers are sharing between these two sets. Sets may not solve the issue because sets will eliminate duplicate elements in a given data structure. Here we are finding all the over lapping tuples in a given list of tuples. Thanks Narasimha. On Fri, Feb 28, 2020 at 5:03 PM Mats Wichmann wrote: > On 2/28/20 1:06 PM, Narasimharao Nelluri wrote: > > Hi peter, > > > > Thanks a lot for your inputs, i will work your inputs and let you know. > For > > the last question if there is independent over lap for ex: in below case > > python should return [(5,7),(6,8)] > > > > tuple_overlap([(0, 2), (1, 3), (5, 7), (6, 8)]) > > > > I tried different combinations as of i didn't get perfect solution. > > One of the problems here is we don't have the problem definition - > unless I've missed something, I don't know what "overlap" means, this > question not being precise enough. > > A tuple of (0, 2) has two values in it. > A tuple of (1, 3) has two values in it. > None of the values in those two tuples match, so one reading of > "overlap" suggests that there's no overlap between those two. Python > supports sets, and in set theory, an "overlap" is the intersection - the > elements that appear in both. > > So you could do this: > > >>> a = set((0, 1, 2)) > >>> b = set((1, 2, 3)) > >>> print(b.intersection(a)) > set([1, 2]) > >>> print a & b > set([1, 2]) > > > But this doesn't _appear_ to be what you're talking about at all. > Instead, it _seems_ your tuples represent ranges of integers, with both > endpoints included. Is that right? That's a different problem. But you > can still use sets to work on this. > > Range gives you all the values, but range(x, y) is the values from x up > to but not including y: > > >>> print(tuple(range(1, 4))) > (1, 2, 3) > > so you want to add one to the end of the range. > > >>> a = set(range(0, 2+1)) > >>> b = set(range(1, 3+1)) > >>> c = set(range(5, 7+1)) > >>> print(a, b, c) > (set([0, 1, 2]), set([1, 2, 3]), set([5, 6, 7])) > >>> print(a & b) > set([1, 2]) > >>> print(a & c) > set([]) > >>> if a & b: > ... print("a and b overlap") > ... > a and b overlap > >>> if a & c: > ... print("a and c overlap") > ... > >>> > > Does that help you think about the problem? > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From Richard at Damon-Family.org Sat Feb 29 06:39:05 2020 From: Richard at Damon-Family.org (Richard Damon) Date: Sat, 29 Feb 2020 06:39:05 -0500 Subject: [Tutor] overlapping tuples In-Reply-To: References: <0a6ff5ca-46e7-4562-cce3-d49adc66236d@DancesWithMice.info> <235a9339-d408-dd63-83a3-422157635de8@wichmann.us> Message-ID: <5d950133-50a1-cc72-98b3-a1832121b9d6@Damon-Family.org> On 2/28/20 8:36 PM, Narasimharao Nelluri wrote: > Thanks Mats for your inputs. overlap means if two numbers share across two > tuples For Ex if we take two tuples (1,20) and (15,20) there is a > over lap because 15,16,17,18,19,20 numbers are sharing between these > two sets. Sets may not solve the issue because sets will > eliminate duplicate elements in a given data structure. Here we are > finding all the over lapping tuples in a given list of tuples. > > Thanks > Narasimha. I think wat Mats was pointing out was that there is a lot of apparently assumed knowledge about the problem, i.e. a lack of a formal statement of what you are trying to do. My understanding is as follows: We are given a List of 2 element Tuples (a, b) each representing a closed interval (inclusive of the end points) where the tuple (a, b) represents all the values x such that a <= x <= b. We want to find all the tuples in the list that represent ranges that overlap with another tuple in the list. I think what we still need is a better definition of what the output is you want. Do you want a list of the tuples that participate in the overlap (which is my first understanding of the problem) or do you want the just a list of the ranges of overlap? Details in the problem statement have great impact on the details of the solution. Is it allowed for a given range to be repeated? (Your example says Yes) If a given range overlaps multiple other ranges, should it be listed once or once per overlap? Your example of: list3 = [(1,10),(15,20),(1,10),(1,10),(101,110)] tuple_overlap(list3) in the first case it should return [(1, 10), (1, 10), (1, 10)] as (1, 10) occurs 3 times in the second case it should probably return [{1, 10), (1, 10), (1, 10), (1, 10), (1, 10), (1, 10)] as you have 3 tuples (1, 10) and each one overlaps with 2 other tuples. Both of these options need a bit more work in your program to handle, and it makes a big difference as to which one you want. This goes back to one of the basic principles of programming, makes sure you have (and give to others) a good problem definition, and be prepared as you move along to come up with more questions that need answers. Short English statements may SEEM to be enough, but often are missing details and have assumptions in definitions that need to be answered, -- Richard Damon From alan.gauld at yahoo.co.uk Sat Feb 29 08:05:26 2020 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 29 Feb 2020 13:05:26 +0000 Subject: [Tutor] overlapping tuples In-Reply-To: References: <0a6ff5ca-46e7-4562-cce3-d49adc66236d@DancesWithMice.info> <235a9339-d408-dd63-83a3-422157635de8@wichmann.us> Message-ID: On 29/02/2020 01:36, Narasimharao Nelluri wrote: > Thanks Mats for your inputs. overlap means if two numbers share across two > tuples For Ex if we take two tuples (1,20) and (15,20) there is a > over lap because 15,6,17,18,19,20 numbers are sharing between these > two sets. When programming you need to be precise. tuples, sets and ranges are 3 very different things. You are treating tuples as ranges. In your problem the tuple (1,5) represents the range 1,2,3,4,5. But to us(and Python) a tuple is a collection of items. The tuple (1,5) means just the two numbers 1 and 5. Its OK to use tuples to store the range limits like that but we can't guess, you need to explain that to us. And likewise sets has a specific meaning - a collection of unique items (for some arbitrary definition of uniqueness). ranges are also a thing in Python, you can define a range from n to m-1 as range(n,m-1). That returns a range object. You might find it easier to solve your problem if you use range objects instead of tuples? -- 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 From alan.gauld at yahoo.co.uk Sat Feb 29 08:16:10 2020 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 29 Feb 2020 13:16:10 +0000 Subject: [Tutor] overlapping tuples In-Reply-To: References: <0a6ff5ca-46e7-4562-cce3-d49adc66236d@DancesWithMice.info> <235a9339-d408-dd63-83a3-422157635de8@wichmann.us> Message-ID: On 29/02/2020 13:05, Alan Gauld via Tutor wrote: > ranges are also a thing in Python, you can define a range from > n to m-1 as range(n,m-1). Oops! That should, of course, be range(n,m) -- 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 From sjeik_appie at hotmail.com Sat Feb 29 15:10:36 2020 From: sjeik_appie at hotmail.com (sjeik_appie at hotmail.com) Date: Sat, 29 Feb 2020 21:10:36 +0100 Subject: [Tutor] Control MS Access with Python In-Reply-To: Message-ID: On 25 Feb 2020 23:44, Alan Gauld via Tutor wrote: On 25/02/2020 17:01, Will Liu wrote: > I already have pyodbc installed and have MS Access connected with Python IDLE. > Now I am able to read data through Python IDLE. Am I on the right way? ---- Hi, besides pyodbc there's also ceodbc, which 'executemany' method is about ten times faster. Might even be a drop in replacement, so you could try both modules