From yawantwiadjei at gmail.com Wed Jul 1 08:42:10 2020 From: yawantwiadjei at gmail.com (YAW ANTWI-ADJEI) Date: Wed, 1 Jul 2020 12:42:10 +0000 Subject: [Tutor] How to use a Variable in an input statement Message-ID: Hello, I want to know how I can use a variable such as 'StudentName' in an input statement such that the prompt will read something like "Yaw, how old are you?" assuming that the variable StudentName = 'Yaw' Thus, I want something like: *StudentName* = 'Yaw' Age = input(*StudentName*, 'how old are you?') Thank you for your help in advanced Yaw Antwi-Adjei From alan.gauld at yahoo.co.uk Wed Jul 1 09:37:00 2020 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 1 Jul 2020 14:37:00 +0100 Subject: [Tutor] How to use a Variable in an input statement In-Reply-To: References: Message-ID: On 01/07/2020 13:42, YAW ANTWI-ADJEI wrote: > Thus, I want something like: > *StudentName* = 'Yaw' > Age = input(*StudentName*, 'how old are you?') There are several ways to do this, but string formatting is probably best: age = input("{} how old are you? ".format(Studentname)) OR age = input("%s how old are you? " % StudentName) Or even, in recent pythons(post 3.6): age = input(f"{StudentName} how old are you? ") The last one is preferred if you don't have to support older versions. -- 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 Wed Jul 1 10:13:08 2020 From: mats at wichmann.us (Mats Wichmann) Date: Wed, 1 Jul 2020 08:13:08 -0600 Subject: [Tutor] How to use a Variable in an input statement In-Reply-To: References: Message-ID: <501ed9a0-a630-c9e9-01bc-1bf5ccd95590@wichmann.us> On 7/1/20 7:37 AM, Alan Gauld via Tutor wrote: > On 01/07/2020 13:42, YAW ANTWI-ADJEI wrote: > >> Thus, I want something like: >> *StudentName* = 'Yaw' >> Age = input(*StudentName*, 'how old are you?') > > There are several ways to do this, but string formatting is > probably best: > > age = input("{} how old are you? ".format(Studentname)) > > OR > > age = input("%s how old are you? " % StudentName) > > Or even, in recent pythons(post 3.6): > > age = input(f"{StudentName} how old are you? ") > > The last one is preferred if you don't have to support older versions. To expand: input takes a single string argument, so build the string as you wish. You can do it inline, as above, or separately, like: age_prompt = f"{StudentName}: how old are you? " age = input(age_prompt) Also reminder: input returns a string. Don't treat the return as anything else (e.g. an integer) until you've converted it - and normally, you also want to "validate" the contents of any external data source before using it or you risk surprises (at best) or serious Bad Things (at worst). From john at johnweller.co.uk Wed Jul 1 13:52:35 2020 From: john at johnweller.co.uk (John Weller) Date: Wed, 1 Jul 2020 18:52:35 +0100 Subject: [Tutor] FTP Errors Message-ID: <012a01d64fd0$682efcb0$388cf610$@johnweller.co.uk> I have a program that runs (should run) 24/7 in a loop. In the loop the program captures some data which it stores in a string. The string is appended to a list and the earliest string popped to keep it at 10 lines. The list is then uploaded to a web server using FTP. It then goes to sleep for 5 minutes before repeating. I had some problems with the program crashing when the internet dropped for a short time so put a try, except section in to trap the errors which appeared to work however I am now getting the program crashing again when it tries to upload instead of the except catching the problem. It will work perfectly uploading every 5 minutes for hours on end then will crash. The upload function is below: def upload(file1, file2): try: ftp = FTP(data.ftp_host) # connect to host, default port ftp.login(user=data.ftp_user, passwd=data.ftp_passwd) except ftplib.all_errors as err: #Log the error logging.error(err) else: ftp.cwd(data.ftp_dir) ftp.storlines("STOR " + file1, open(file1, 'rb')) ftp.storlines("STOR " + file2, open(file2, 'rb')) ftp.quit() The error messages are below: Traceback (most recent call last): File "garage_11.py", line 245, in upload ftp.login(user=data.ftp_user, passwd=data.ftp_passwd) File "/usr/lib/python3.7/ftplib.py", line 420, in login resp = self.sendcmd('PASS ' + passwd) File "/usr/lib/python3.7/ftplib.py", line 273, in sendcmd return self.getresp() File "/usr/lib/python3.7/ftplib.py", line 246, in getresp raise error_perm(resp) ftplib.error_perm: 530 Login incorrect. During handling of the above exception, another exception occurred: Traceback (most recent call last): File "garage_11.py", line 450, in loop(data, values_list) File "garage_11.py", line 372, in loop upload('garage.html', data.filename) File "garage_11.py", line 246, in upload except ftplib.all_errors as err: NameError: name 'ftplib' is not defined It looks as though the problem lies in the exception handling and I would be grateful for any pointers ? TIA John John Weller 01380 723235 07976 393631 From __peter__ at web.de Wed Jul 1 18:16:06 2020 From: __peter__ at web.de (Peter Otten) Date: Thu, 02 Jul 2020 00:16:06 +0200 Subject: [Tutor] FTP Errors References: <012a01d64fd0$682efcb0$388cf610$@johnweller.co.uk> Message-ID: John Weller wrote: > I have a program that runs (should run) 24/7 in a loop. In the loop the > program captures some data which it stores in a string. The string is > appended to a list and the earliest string popped to keep it at 10 lines. > The list is then uploaded to a web server using FTP. It then goes to > sleep for 5 minutes before repeating. > > > > I had some problems with the program crashing when the internet dropped > for a short time so put a try, except section in to trap the errors which > appeared to work however I am now getting the program crashing again when > it tries to upload instead of the except catching the problem. It will > work perfectly uploading every 5 minutes for hours on end then will crash. > > > > The upload function is below: > > > > def upload(file1, file2): > > try: > > ftp = FTP(data.ftp_host) # connect to host, default port > > ftp.login(user=data.ftp_user, passwd=data.ftp_passwd) > > except ftplib.all_errors as err: Study the traceback carfully: the line above is the problem. You probably wrote from ftplib import FTP or from ftplib import * In both cases the name ftplib is not available to your module. To fix this you can add the line import ftplib I recommend that for consistency you also change > ftp = FTP(data.ftp_host) # connect to host, default port to ftp = ftplib.FTP(data.ftp_host) and remove the from ... import. > #Log the error > > logging.error(err) > > else: > > ftp.cwd(data.ftp_dir) > > ftp.storlines("STOR " + file1, open(file1, 'rb')) > > ftp.storlines("STOR " + file2, open(file2, 'rb')) > > ftp.quit() > > > > The error messages are below: > > > > Traceback (most recent call last): > > File "garage_11.py", line 245, in upload > > ftp.login(user=data.ftp_user, passwd=data.ftp_passwd) > > File "/usr/lib/python3.7/ftplib.py", line 420, in login > > resp = self.sendcmd('PASS ' + passwd) > > File "/usr/lib/python3.7/ftplib.py", line 273, in sendcmd > > return self.getresp() > > File "/usr/lib/python3.7/ftplib.py", line 246, in getresp > > raise error_perm(resp) > > ftplib.error_perm: 530 Login incorrect. > > > > During handling of the above exception, another exception occurred: > > > > Traceback (most recent call last): > > File "garage_11.py", line 450, in > > loop(data, values_list) > > File "garage_11.py", line 372, in loop > > upload('garage.html', data.filename) > > File "garage_11.py", line 246, in upload > > except ftplib.all_errors as err: > > NameError: name 'ftplib' is not defined > > > > It looks as though the problem lies in the exception handling and I would > be grateful for any pointers ? > > > > TIA > > > > John > > > > John Weller > > 01380 723235 > > 07976 393631 > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From john at johnweller.co.uk Thu Jul 2 11:47:57 2020 From: john at johnweller.co.uk (John Weller) Date: Thu, 2 Jul 2020 16:47:57 +0100 Subject: [Tutor] FTP Errors In-Reply-To: References: <012a01d64fd0$682efcb0$388cf610$@johnweller.co.uk> Message-ID: <00eb01d65088$2d3264e0$87972ea0$@johnweller.co.uk> > > > except ftplib.all_errors as err: > > Study the traceback carfully: the line above is the problem. > You probably wrote > > from ftplib import FTP > You were quite right!! > In both cases the name ftplib is not available to your module. To fix this you can > add the line > > import ftplib > > I recommend that for consistency you also change > > > ftp = FTP(data.ftp_host) # connect to host, default port > > to > > ftp = ftplib.FTP(data.ftp_host) Changed. > and remove the from ... import. > Not quite sure what this means? Many thanks Peter. John John Weller 01380 723235 07976 393631 From __peter__ at web.de Thu Jul 2 12:24:16 2020 From: __peter__ at web.de (Peter Otten) Date: Thu, 02 Jul 2020 18:24:16 +0200 Subject: [Tutor] FTP Errors References: <012a01d64fd0$682efcb0$388cf610$@johnweller.co.uk> <00eb01d65088$2d3264e0$87972ea0$@johnweller.co.uk> Message-ID: John Weller wrote: >> >> > except ftplib.all_errors as err: >> >> Study the traceback carfully: the line above is the problem. >> You probably wrote >> >> from ftplib import FTP >> > > You were quite right!! > >> In both cases the name ftplib is not available to your module. To fix >> this you can add the line >> >> import ftplib >> >> I recommend that for consistency you also change >> >> > ftp = FTP(data.ftp_host) # connect to host, default port >> >> to >> >> ftp = ftplib.FTP(data.ftp_host) > > > Changed. > >> and remove the from ... import. >> > Not quite sure what this means? I suppose you have a line from ftplib import * in your code? If so, remove that line. Instead add the line import ftplib As a consequence of that change you have to replace all occurences of FTP with ftplib.FTP From nathan-tech at hotmail.com Thu Jul 2 19:20:05 2020 From: nathan-tech at hotmail.com (nathan tech) Date: Fri, 3 Jul 2020 00:20:05 +0100 Subject: [Tutor] could someone please explain multiprocessing Message-ID: Hi tutor List, I hope everyone is keeping safe and doing well. I was wondering if someone was able to explain the multiprocessing module to me. I'm trying to get it to where my program would be able to do something like: Hi parent, are you running? Yes I am. Great, here is a file for you. Thanks child, you can close now and not launch a second instance. okay, goodnight! So far the way I've done this is through the socket module by having the first program listen on localhost, but this on some systems raises a windows security error. This obviously makes it rather suspicious! I'm wondering how windows media player goes about it? Thanks for any help you can offer. Nathan From alan.gauld at yahoo.co.uk Thu Jul 2 20:07:56 2020 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 3 Jul 2020 01:07:56 +0100 Subject: [Tutor] could someone please explain multiprocessing In-Reply-To: References: Message-ID: On 03/07/2020 00:20, nathan tech wrote: > I was wondering if someone was able to explain the multiprocessing > module to me. You might try the concurrency topics in my tutorial. http://www.alan-g.me.uk/l2p2/tutthread.htm If you have used threading it should all make sense. Its very basic, just an introduction to the concepts, but it should get you started. > Hi parent, are you running? > Yes I am. > Great, here is a file for you. > Thanks child, you can close now and not launch a second instance. > okay, goodnight! > > > So far the way I've done this is through the socket module by having the > first program listen on localhost, but this on some systems raises a > windows security error. I'm not sure I understand this bit. What is the "first program" (ie the main thread?) listening for on the socket? Surely you are not communicating between threads via sockets? That makes no sense? > I'm wondering how windows media player goes about it? I have no idea. In fact I don't even know what aspect of WMP you are referring to? What feature does it have that requires concurrency? (I'm a Linux user who rarely uses Windows and certainly never WMP!) It might help us give a more comprehensive answer if you could describe the application level requirements rather than just the proposed solution. -- 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 bharath_achar at outlook.com Fri Jul 3 07:50:29 2020 From: bharath_achar at outlook.com (Bharath Achar) Date: Fri, 3 Jul 2020 11:50:29 +0000 Subject: [Tutor] Help need with finding method to filter with regex and modify different column with correct match foung Message-ID: Hello, Is there a direct python pandas method to match value of series and update different series with some string ? I couldn?t find any direct method of doing it. Here the match is to find a value in a series that is made up of given set of keywords and one mandatory keyword. Example : Here?s the keywords table : KW.head(5) Case kws mand_kw field_friendly_name 1 Full|name|nm|txt|dsc|[0-9] full Full Name 2 first|name|nm|txt|dsc|[0-9] first First Name 3 last|name|nm|txt|dsc|[0-9] last Last Name 4 legal|name|nm|txt|dsc|[0-9] legal Legal Name 5 nick|name|nm|txt|dsc|[0-9] nick Nick Name Dataframe : DF.head(14) _id field_name field_friendly_name purpose_of_use is_included_in_report 1 fullname 2 Xyz 3 custname 4 fullnm 5 firstname 6 Abc 7 firstnm 8 fullname74 9 fullnm 10 legalname 11 legalnm 12 nickname 13 Pqr 14 nicknm For each of kws from KW find match in field_name in DF that it is made up of only keywords specified in kws and not any other words and a mandatorily should have a mand_kw keyword. For example : Case1: Find all field_name that is only made up of keyword ?Full|name|nm|txt|dsc|[0-9]? if so, assign field_friendly_name of KW - ?Full Name? to field_friendly_name in DF and update is_included_in_report as true. If the value is not made of these keywords, then skip. _id field_name field_friendly_name purpose_of_use is_included_in_report 1 fullname Full Name TRUE 4 fullnm Full Name TRUE 8 fulldsc74 Full Name TRUE 9 fulltxt Full Name TRUE Regards, Bharath Achar -------------------------------------- Thanks, Bharath Achar Sr. Storage and Backup Consultant Intel India pvt ltd, Bangalore. Cell :: + 91 7259670196 Bharath_achar at outlook.com -------------------------------------- From bharath_achar at outlook.com Fri Jul 3 08:57:36 2020 From: bharath_achar at outlook.com (Bharath Achar) Date: Fri, 3 Jul 2020 12:57:36 +0000 Subject: [Tutor] Help need with finding method to filter with regex and modify different column with correct match foung In-Reply-To: References: Message-ID: here's the code what I've tried: import pandas import re for i in range (0,KW.kws.size): for j in range (0,DF.field_name): if (DF.field_name.match(r"KW.kws[i]") == True && (DF.field_name.match(r"mand_kw[i]" == True)): DF.field_friendly_name[j] = KW.field_friendly_name[i] DF.is_included_in_report[j] = "True" -------------------------------------- Thanks, Bharath Achar Sr. Storage and Backup Consultant Intel India pvt ltd, Bangalore. Cell :: + 91 7259670196 Bharath_achar at outlook.com -------------------------------------- ________________________________ From: Bharath Achar Sent: Friday, July 3, 2020 17:20 To: tutor at python.org Subject: Help need with finding method to filter with regex and modify different column with correct match foung Hello, Is there a direct python pandas method to match value of series and update different series with some string ? I couldn?t find any direct method of doing it. Here the match is to find a value in a series that is made up of given set of keywords and one mandatory keyword. Example : Here?s the keywords table : KW.head(5) Case kws mand_kw field_friendly_name 1 Full|name|nm|txt|dsc|[0-9] full Full Name 2 first|name|nm|txt|dsc|[0-9] first First Name 3 last|name|nm|txt|dsc|[0-9] last Last Name 4 legal|name|nm|txt|dsc|[0-9] legal Legal Name 5 nick|name|nm|txt|dsc|[0-9] nick Nick Name Dataframe : DF.head(14) _id field_name field_friendly_name purpose_of_use is_included_in_report 1 fullname 2 Xyz 3 custname 4 fullnm 5 firstname 6 Abc 7 firstnm 8 fullname74 9 fullnm 10 legalname 11 legalnm 12 nickname 13 Pqr 14 nicknm For each of kws from KW find match in field_name in DF that it is made up of only keywords specified in kws and not any other words and a mandatorily should have a mand_kw keyword. For example : Case1: Find all field_name that is only made up of keyword ?Full|name|nm|txt|dsc|[0-9]? if so, assign field_friendly_name of KW - ?Full Name? to field_friendly_name in DF and update is_included_in_report as true. If the value is not made of these keywords, then skip. _id field_name field_friendly_name purpose_of_use is_included_in_report 1 fullname Full Name TRUE 4 fullnm Full Name TRUE 8 fulldsc74 Full Name TRUE 9 fulltxt Full Name TRUE Regards, Bharath Achar -------------------------------------- Thanks, Bharath Achar Sr. Storage and Backup Consultant Intel India pvt ltd, Bangalore. Cell :: + 91 7259670196 Bharath_achar at outlook.com -------------------------------------- From mats at wichmann.us Fri Jul 3 15:47:19 2020 From: mats at wichmann.us (Mats Wichmann) Date: Fri, 3 Jul 2020 13:47:19 -0600 Subject: [Tutor] could someone please explain multiprocessing In-Reply-To: References: Message-ID: On 7/2/20 5:20 PM, nathan tech wrote: > Hi tutor List, > > > I hope everyone is keeping safe and doing well. > > I was wondering if someone was able to explain the multiprocessing > module to me. > > I'm trying to get it to where my program would be able to do something > like: > > Hi parent, are you running? > > Yes I am. > > Great, here is a file for you. > > Thanks child, you can close now and not launch a second instance. > > okay, goodnight! > > > So far the way I've done this is through the socket module by having the > first program listen on localhost, but this on some systems raises a > windows security error. This obviously makes it rather suspicious! Multiprocessing is easy, until it isn't. Which usually happens fairly fast. "easy": kick off a new process to do a bit of self-contained work. "harder": send short messages between processes "also harder": coordinate between processes (locks, etc.) "still harder": send data between processes the documentation for the Python multiprocess module talks about all of these and more. From nathan-tech at hotmail.com Fri Jul 3 23:01:56 2020 From: nathan-tech at hotmail.com (nathan tech) Date: Sat, 4 Jul 2020 04:01:56 +0100 Subject: [Tutor] could someone please explain multiprocessing In-Reply-To: References: Message-ID: Hi Alan and Mats, First up I wanted to address the questions raised by Alan regarding my mention of sockets. What I meant by this was: 1. There is one centralised program file, lets call this program.py. 2. Upon launch, program.py attempts to connect to localhost:6868 (just an example). If this works, it sends a message then exits. 3. If it fails, the program then listens on the said port, and waits (or allows the user to do other things such as play other music). When it receives the message it then processes it. My basic aim was to try and find a way around the age old problem of: If program is already running, don't run it again instead just tell it some info. In terms of WMP, what I was referring to there was basically what I explained above, it has the ability to launch the media player, play a song and if you play another file (by clicking on it), it does not open two instances of WMP instead it just adds that to the one being played and plays it after. To be honest I actually do have the entire concept working with a sockets backend, kind of a client/server situation but my main problem is that windows throws up a security alert (most likely the firewall). I mean I guess I could force something in the installer, but really, if a simple media player requires you modify security settings, am I the only one who scratches his head and thinks "no thanks." That's why I then turned to multiprocessing it seems a safer alternative. I'll definitely check out your tutorial Alan, so thanks very much for that. Finally i wanted to address Mats response (very appreciated). I have studied a couple of examples, documented and otherwise and the main question I kept coming across was: "But, what if I want it to track down a different process to pass the information too?" That is why I came to the list. Hope this helps and I'm sorry for being so vague. (It's honestly not intentional!) Nathan On 03/07/2020 20:47, Mats Wichmann wrote: > On 7/2/20 5:20 PM, nathan tech wrote: >> Hi tutor List, >> >> >> I hope everyone is keeping safe and doing well. >> >> I was wondering if someone was able to explain the multiprocessing >> module to me. >> >> I'm trying to get it to where my program would be able to do something >> like: >> >> Hi parent, are you running? >> >> Yes I am. >> >> Great, here is a file for you. >> >> Thanks child, you can close now and not launch a second instance. >> >> okay, goodnight! >> >> >> So far the way I've done this is through the socket module by having the >> first program listen on localhost, but this on some systems raises a >> windows security error. This obviously makes it rather suspicious! > > Multiprocessing is easy, until it isn't. Which usually happens fairly fast. > > "easy": kick off a new process to do a bit of self-contained work. > "harder": send short messages between processes > "also harder": coordinate between processes (locks, etc.) > "still harder": send data between processes > > the documentation for the Python multiprocess module talks about all of > these and more. > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://eur04.safelinks.protection.outlook.com/?url=https%3A%2F%2Fmail.python.org%2Fmailman%2Flistinfo%2Ftutor&data=02%7C01%7C%7Ccc71ca19c49e44c0468a08d81f8a1275%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637294025154600280&sdata=Yrhztil5rd7r3DM%2BPIR%2FWTi55nqfsrpiO6GfKPsuvls%3D&reserved=0 From alan.gauld at yahoo.co.uk Sat Jul 4 07:30:20 2020 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 4 Jul 2020 12:30:20 +0100 Subject: [Tutor] could someone please explain multiprocessing In-Reply-To: References: Message-ID: On 04/07/2020 04:01, nathan tech wrote: > 1. There is one centralised program file, lets call this program.py. > > 2. Upon launch, program.py attempts to connect to localhost:6868 (just > an example). If this works, it sends a message then exits. So if it connects it send a message and exits. > 3. If it fails, the program then listens on the said port, and waits But how can it listen if it failed to connect? And if it did connect it has already exited? > allows the user to do other things such as play other music). When it > receives the message it then processes it. You started by saying program.py sent the message, but now you have it waiting for a message? What is sending the message? How does it know you are waiting? > My basic aim was to try and find a way around the age old problem of: > > If program is already running, don't run it again instead just tell it > some info. OK, That's usually done with a launcher that just starts, checks for an existing instance and if none found launches the real program. If there is one, it adds the new job to a queue or sends a message (in Unix a signal) to interrupt the existing process and restart with a new file. (Or whatever the exact behaviour that is required.) That means the launcher knows how to a) locate an instance of the main program b) lanch the mai program and c) send a signal or message to the main program. The main program knows how to a) perform its primary function b) receive messages/signals and monitor for them c) How to extend/modify its work queue (or else pulls work from a known shared file) There is no real need for threading or concurrency here. -- 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 Jul 5 10:36:47 2020 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 5 Jul 2020 15:36:47 +0100 Subject: [Tutor] Fwd: Re: Beginner Noob References: <6a3e7a14-27e1-59f3-5082-055a06105f7a.ref@yahoo.co.uk> Message-ID: <6a3e7a14-27e1-59f3-5082-055a06105f7a@yahoo.co.uk> On 06/06/2020 09:38, DL Neil via Tutor wrote: > there is a version of C which is native to the Arduino. Which version is that? The regular Arduino develoment tool comes with C++ as standard, where would you find a native Arduino C? (And why would you want to? given C++ is almost a complete superset of C) -- 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-tech at hotmail.com Sun Jul 5 06:33:09 2020 From: nathan-tech at hotmail.com (nathan tech) Date: Sun, 5 Jul 2020 11:33:09 +0100 Subject: [Tutor] could someone please explain multiprocessing In-Reply-To: References: Message-ID: Hi Alan, Thanks for your response, as always you somehow manage to decode my ramblings into order, then state the solution in an obvious way that makes me smack my forever and go, "duh!" I admit part of me had been half avoiding shared files because in my head I'm thinking, it's going to have to keep checking that file on a regular basis which is a lot of open, close, open, close. With the signal solution though it would only do it when signaled to do so. Genius! Just to make sure I have this right, would you mind glancing over this Seudo code? ???? Launcher: import psutil ?currently_running=[] ?for process in running_processes: ? if(process["name"]=="my_programs_launcher" or process["name"]=="my_programs_name"): ?? running.append(process["id"]) ?if(len(running)==1): ? # we are the only one! Launch the main program and job done. ?else: ? # we are already running. Open the shared file, append the arguments, and then send a signal ? Main program: ?# do all the things ?# check to see if any signals have been received ?if(signal_received): ? # open the file and process it, deleting and or clearing the file after so that it does not repeat itself. Could a method here be something like: ?if(file_last_modified(path)>last_time_this_program_accessed_it): ?? # process contents and update variables Thanks for your help as always. Nathan On 04/07/2020 12:30, Alan Gauld via Tutor wrote: > On 04/07/2020 04:01, nathan tech wrote: > >> 1. There is one centralised program file, lets call this program.py. >> >> 2. Upon launch, program.py attempts to connect to localhost:6868 (just >> an example). If this works, it sends a message then exits. > So if it connects it send a message and exits. > >> 3. If it fails, the program then listens on the said port, and waits > But how can it listen if it failed to connect? And if it did > connect it has already exited? > >> allows the user to do other things such as play other music). When it >> receives the message it then processes it. > You started by saying program.py sent the message, but now you have > it waiting for a message? What is sending the message? How does it > know you are waiting? > >> My basic aim was to try and find a way around the age old problem of: >> >> If program is already running, don't run it again instead just tell it >> some info. > OK, That's usually done with a launcher that just starts, checks > for an existing instance and if none found launches the real program. > If there is one, it adds the new job to a queue or sends a message > (in Unix a signal) to interrupt the existing process and restart > with a new file. (Or whatever the exact behaviour that is required.) > > That means the launcher knows how to > a) locate an instance of the main program > b) lanch the mai program and > c) send a signal or message to the main program. > > The main program knows how to > a) perform its primary function > b) receive messages/signals and monitor for them > c) How to extend/modify its work queue (or else pulls > work from a known shared file) > > There is no real need for threading or concurrency here. > From alan.gauld at yahoo.co.uk Sun Jul 5 13:34:36 2020 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 5 Jul 2020 18:34:36 +0100 Subject: [Tutor] could someone please explain multiprocessing In-Reply-To: References: Message-ID: On 05/07/2020 11:33, nathan tech wrote: > import psutil > ?currently_running=[] > ?for process in running_processes: > ? if(process["name"]=="my_programs_launcher" or > process["name"]=="my_programs_name"): > > ? ? running.append(process["id"]) > ?if(len(running)==1): ? # we are the only one! Launch the main program and job done. I think you are doing more work than necessaary. You don;t need a list of running instances since there is only zero or one. - Its a boolean condition. So just use the if test above. > ?else: > ? # we are already running. Open the shared file, append the arguments, > and then send a signal Yep. > ? Main program: > ?# do all the things > ?# check to see if any signals have been received > ?if(signal_received): > ? # open the file and process it, deleting and or clearing the file > after so that it does not repeat itself. Yep. But... check out the signal module. The actual code will be more event driven than the above. You just set up the handler and the OS will work with the Python virtual machine to call the handler when the signal arrives. > ?if(file_last_modified(path)>last_time_this_program_accessed_it): > ?? # process contents and update variables Yep again. -- 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 Sun Jul 5 16:12:43 2020 From: PyTutor at DancesWithMice.info (dn) Date: Mon, 6 Jul 2020 08:12:43 +1200 Subject: [Tutor] Fwd: Re: Beginner Noob In-Reply-To: <6a3e7a14-27e1-59f3-5082-055a06105f7a@yahoo.co.uk> References: <6a3e7a14-27e1-59f3-5082-055a06105f7a.ref@yahoo.co.uk> <6a3e7a14-27e1-59f3-5082-055a06105f7a@yahoo.co.uk> Message-ID: <59428a52-3c0c-db1d-5a2a-f6ffe2ccc7ff@DancesWithMice.info> On 6/07/20 2:36 AM, Alan Gauld via Tutor wrote: > On 06/06/2020 09:38, DL Neil via Tutor wrote: > >> there is a version of C which is native to the Arduino. > > Which version is that? The regular Arduino develoment tool > comes with C++ as standard, where would you find a native > Arduino C? (And why would you want to? given C++ is almost > a complete superset of C) Apologies @Alan. I fear that these words were chosen too quickly, or are being taken more precisely than was intended, in their r?le as a v.short comparison. IIRC I was summarising my own reason for choosing the Raspberry Pi introduction to SBCs (over the Arduino), which (conveniently) qualified me for the most gold-stars - because it (just happens to) recommend Python! That said, if I was confused or confusing you, longer and more focussed introductions are also befuddled by the heritage of various Arduino development facilities: <<< Arduino programs are written in the Arduino Integrated Development Environment (IDE). Arduino IDE is a special software running on your system that allows you to write sketches (synonym for program in Arduino language) for different Arduino boards. The Arduino programming language is based on a very simple hardware programming language called processing, which is similar to the C language. After the sketch is written in the Arduino IDE, it should be uploaded on the Arduino board for execution. >>> https://www.hackerearth.com/blog/developers/arduino-programming-for-beginners/ <<< Programming on Arduino is definitely not the same thing as programming on a standard computer. So, what exactly is the Arduino language? It seems like you?re programming in C/C++, but soon you realize that it?s not exactly C/C++. You don?t really know what you can do, and what you can?t do. In this post I?ll show you all the secrets behind the Arduino language, so you?ll know exactly what to expect from it, and how you can use it more efficiently! >>> https://roboticsbackend.com/the-arduino-language-in-10-points/ <<< Language Reference See the extended reference for more advanced features of the Arduino languages and the libraries page for interfacing with particular types of hardware. Arduino programs can be divided in three main parts: structure, values (variables and constants), and functions. The Arduino language is based on C/C++. >>> https://www.ele.uri.edu/courses/ele205/ELE205Lab/ELE205_Lab_files/Arduino%20-%20Reference.pdf This StackExchange seems to have arguments in favor of C *and* in favor of C++, so the question would appear perplexing if not confusing: https://arduino.stackexchange.com/questions/816/c-vs-the-arduino-language Recognising the confusion, and presenting his own case, as well as some history-and-geography of the alternatives (and the escape-clause of the deep entanglement of C and C++): THE ARDUINO PROGRAMMING LANGUAGE: WHICH ONE IS IT? https://www.idogendel.com/en/archives/19 Apologies if you were hoping for a pointer to a C or C++ compiler that runs natively on the Arduino! (such may/not exist, I have not looked) Many facilities are (now) available to develop in a variety of languages, either directly on an Arduino or elsewhere for download. However, the basic facilities that come with a beginner's kit (apropos the OP), whatever the provenance of its language, are *not* Python. -- Regards =dn From alan.gauld at yahoo.co.uk Sun Jul 5 19:36:35 2020 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 6 Jul 2020 00:36:35 +0100 Subject: [Tutor] Fwd: Re: Beginner Noob In-Reply-To: <59428a52-3c0c-db1d-5a2a-f6ffe2ccc7ff@DancesWithMice.info> References: <6a3e7a14-27e1-59f3-5082-055a06105f7a.ref@yahoo.co.uk> <6a3e7a14-27e1-59f3-5082-055a06105f7a@yahoo.co.uk> <59428a52-3c0c-db1d-5a2a-f6ffe2ccc7ff@DancesWithMice.info> Message-ID: On 05/07/2020 21:12, dn via Tutor wrote: >> Which version is that? The regular Arduino develoment tool >> comes with C++ as standard, > <<< > It seems like you?re programming in C/C++, but soon you realize that > it?s not exactly C/C++. You don?t really know what you can do, and what > you can?t do. Hmm, Thanks for the quotation/citation. I must confess I've been sporadically progranming Arduino boards for about 7 years now and not once realized it was not C++. Everything I've done has assumed a C++98 base and it has just worked...(In fact the Arduino is about the only time I use C/C++ nowadays) > Apologies if you were hoping for a pointer to a C or C++ compiler that > runs natively on the Arduino! (such may/not exist, I have not looked) I was intrigued if such existed since the only thing I was aware of was the vanilla Arduino IDE tool set. And so far as I was aware (based on a fairly cheap beginners guidebook) that was a substantial subset of C++.( A few libraries don't exist and a couple of non-standard Arduino specific ones are provided but the actual language syntax is pretty complete C++98 - and maybe even C++11) > ...whatever the provenance of its language, are *not* Python. That's for sure! :-) -- 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 Sun Jul 5 19:46:39 2020 From: PyTutor at DancesWithMice.info (dn) Date: Mon, 6 Jul 2020 11:46:39 +1200 Subject: [Tutor] Fwd: Re: Beginner Noob In-Reply-To: References: <6a3e7a14-27e1-59f3-5082-055a06105f7a.ref@yahoo.co.uk> <6a3e7a14-27e1-59f3-5082-055a06105f7a@yahoo.co.uk> <59428a52-3c0c-db1d-5a2a-f6ffe2ccc7ff@DancesWithMice.info> Message-ID: <03cb4720-515c-d4f4-1437-285db3f19544@DancesWithMice.info> On 6/07/20 11:36 AM, Alan Gauld via Tutor wrote: > On 05/07/2020 21:12, dn via Tutor wrote: > >>> Which version is that? The regular Arduino develoment tool >>> comes with C++ as standard, > >> <<< >> It seems like you?re programming in C/C++, but soon you realize that >> it?s not exactly C/C++. You don?t really know what you can do, and what >> you can?t do. > > Hmm, Thanks for the quotation/citation. > > I must confess I've been sporadically progranming Arduino > boards for about 7 years now and not once realized it was > not C++. Everything I've done has assumed a C++98 base and > it has just worked...(In fact the Arduino is about the only > time I use C/C++ nowadays) > >> Apologies if you were hoping for a pointer to a C or C++ compiler that >> runs natively on the Arduino! (such may/not exist, I have not looked) > > I was intrigued if such existed since the only thing I was aware of > was the vanilla Arduino IDE tool set. And so far as I was aware > (based on a fairly cheap beginners guidebook) that was a > substantial subset of C++.( A few libraries don't exist and > a couple of non-standard Arduino specific ones are provided but the > actual language syntax is pretty complete C++98 - and maybe even > C++11) > >> ...whatever the provenance of its language, are *not* Python. > > That's for sure! :-) Which is the reason why I was heading to Raspberry Pi - less to cope-with in the change from a PC to an SBC. However, first my supplier let me down, then there were questions about 'power', ... (blessing in disguise? today there are larger/more powerful versions) So, I ended-up shifting 'sideways' and picking-up a GPU/co-processor, with the intent of embracing CUDA. However, fate intervened (or something), and the hardware sits, lonely as a cloud (hah!), in my workshop; whilst I work on 'other things', and then along came COVID-19 and the rush to do-stuff-differently... "Free time". What's that? (heavy) Sigh! -- Regards =dn From niveshgupta0015678 at gmail.com Mon Jul 6 09:15:30 2020 From: niveshgupta0015678 at gmail.com (Nivesh Gupta) Date: Mon, 6 Jul 2020 18:45:30 +0530 Subject: [Tutor] Empty script file. Message-ID: I downloaded python on July 5 2020. The Setup was successful but the script folder was empty.. There was no pip related file die too which I am not able to use pip install.. Please help.. From deepakdixit0001 at gmail.com Mon Jul 6 14:04:01 2020 From: deepakdixit0001 at gmail.com (Deepak Dixit) Date: Mon, 6 Jul 2020 23:34:01 +0530 Subject: [Tutor] Empty script file. In-Reply-To: References: Message-ID: Nivesh, We are here to help you on python but how can we get an idea of which script folder you are talking about and what pip module you want to install? You should include some information about the OS, Python version and the location you are looking for. On Mon, Jul 6, 2020 at 11:24 PM Nivesh Gupta wrote: > I downloaded python on July 5 2020. > The Setup was successful but the script folder was empty.. > There was no pip related file die too which I am not able to use pip > install.. > Please help.. > _______________________________________________ > 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 alan.gauld at yahoo.co.uk Mon Jul 6 14:05:39 2020 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 6 Jul 2020 19:05:39 +0100 Subject: [Tutor] Empty script file. In-Reply-To: References: Message-ID: On 06/07/2020 14:15, Nivesh Gupta wrote: > I downloaded python on July 5 2020. > The Setup was successful but the script folder was empty.. > There was no pip related file die too which I am not able to use pip > install.. Which version of Python? >From which repository(ActiveState, Anaconda, python.org etc?) On which OS/version? Where exactly are you looking for the script folder? On my Linux system I don't appear to have a scripts folder... As for pip, how are you running it? What error message do you get? If you type (at the OS prompt): python3 -m pip do you get the usage 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 mats at wichmann.us Mon Jul 6 14:09:58 2020 From: mats at wichmann.us (Mats Wichmann) Date: Mon, 6 Jul 2020 12:09:58 -0600 Subject: [Tutor] Empty script file. In-Reply-To: References: Message-ID: <7d6c9efb-3073-64c9-3920-dc2039f9eaef@wichmann.us> On 7/6/20 7:15 AM, Nivesh Gupta wrote: > I downloaded python on July 5 2020. > The Setup was successful but the script folder was empty.. > There was no pip related file die too which I am not able to use pip > install.. don't use the pip "executable" anyway, it will only lead you to eventual confusion. Install, instead, as python -m pip install somepackage Variants: * if on Windows, using the python.org installer, you can install the Python Launcher as well (recommended), if so substitute its name above py -m pip install somepackage * you may not want to install to a "system location", particularly on a non-Windows system. You can instead install to a user-private location: python -m pip install --user somepackage From manpritsinghece at gmail.com Tue Jul 7 02:45:01 2020 From: manpritsinghece at gmail.com (Manprit Singh) Date: Tue, 7 Jul 2020 12:15:01 +0530 Subject: [Tutor] Question about about PEP8 Message-ID: Dear all, As i have read in PEP 8 , it suggests not to compare values to true or false using "==". Secondarily it is also written in python documentation that empty collections and sequences (list, tuple set and dict) are considered false . So I just need to know if the code written by me in two cases is correct or not: *Case 1) I have to update the dict if it is empty using an if statement . * e = {} # empty dict if not e: e.update(A=3) # will update the dict if it is empty otherwise not print(e) *case 2 ) I have to remove and print each item of dict in LIFO order :* d = {"A":9, "B":6, "C":3} while d: x = d.popitem() print(x) Regards Manprit Singh From alan.gauld at yahoo.co.uk Tue Jul 7 04:12:06 2020 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 7 Jul 2020 09:12:06 +0100 Subject: [Tutor] Question about about PEP8 In-Reply-To: References: Message-ID: On 07/07/2020 07:45, Manprit Singh wrote: > As i have read in PEP 8 , it suggests not to compare values to true or > false using "==". Secondarily it is also written in python documentation > that empty collections and sequences (list, tuple set and dict) are > considered false . That is correct > So I just need to know if the code written by me in two cases is correct or > not: But note hat PEP8 is purely a style guide it says nothing about correctness. Unless you plan to submit your code for inclusion in the Python distribution you can completely ignore PEP8 if you wish, your code will still work. > *Case 1) I have to update the dict if it is empty using an if statement . > e = {} # empty dict > if not e: > e.update(A=3) # will update the dict if it is empty otherwise Yes that's what PEP8 recommends. > *case 2 ) I have to remove and print each item of dict in LIFO order :* > > d = {"A":9, "B":6, "C":3} > while d: > x = d.popitem() > print(x) And that will work too, but unless you really need to empty the dict (or even if you do) a more pythonic approach would be: for x in d.items(): print(x) d = {} # if you really need an empty dic It's not any shorter, in fact slightly more characters, but it avoids using a while loop for iterating a sequence. -- 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 __peter__ at web.de Tue Jul 7 04:33:46 2020 From: __peter__ at web.de (Peter Otten) Date: Tue, 07 Jul 2020 10:33:46 +0200 Subject: [Tutor] Question about about PEP8 References: Message-ID: Alan Gauld via Tutor wrote: > On 07/07/2020 07:45, Manprit Singh wrote: >> *case 2 ) I have to remove and print each item of dict in LIFO order :* >> >> d = {"A":9, "B":6, "C":3} >> while d: >> x = d.popitem() >> print(x) > > And that will work too, but unless you really need to empty the dict > (or even if you do) a more pythonic approach would be: > > for x in d.items(): To get LIFO order: for x in reversed(d.items()): > print(x) > > d = {} # if you really need an empty dic > > It's not any shorter, in fact slightly more characters, > but it avoids using a while loop for iterating a sequence. From mdrotts at yahoo.com Thu Jul 9 08:37:59 2020 From: mdrotts at yahoo.com (mdrotts) Date: Thu, 9 Jul 2020 12:37:59 +0000 (UTC) Subject: [Tutor] New to Linux, trying to install python 3 - please help :) References: <2011674014.4837775.1594298279672.ref@mail.yahoo.com> Message-ID: <2011674014.4837775.1594298279672@mail.yahoo.com> Hi, I'm a total noobie to Linux and want to install Python 3 (3.8.3) on a Linux Mint (previously used on Windows 10 machine).? Pulling up Terminal and following the instructions from Readme Build Instructions ------------------ On Unix, Linux, BSD, macOS, and Cygwin:: ? ? ./configure ? ? make ? ? make test ? ? sudo make install This will install Python as ``python3``. when entering the first line, I get "bash: no such file or directory" How can I install the downloaded version (directly downloaded 3.8.3 from python.org > downloads)?? Sorry, first post, so please be gentle :D Thanks!D.? From mats at wichmann.us Thu Jul 9 10:17:05 2020 From: mats at wichmann.us (Mats Wichmann) Date: Thu, 9 Jul 2020 08:17:05 -0600 Subject: [Tutor] New to Linux, trying to install python 3 - please help :) In-Reply-To: <2011674014.4837775.1594298279672@mail.yahoo.com> References: <2011674014.4837775.1594298279672.ref@mail.yahoo.com> <2011674014.4837775.1594298279672@mail.yahoo.com> Message-ID: <698528f2-b6dc-f633-fc3c-a8d135042cf3@wichmann.us> On 7/9/20 6:37 AM, mdrotts via Tutor wrote: > Hi, > I'm a total noobie to Linux and want to install Python 3 (3.8.3) on a Linux Mint (previously used on Windows 10 machine).? > Pulling up Terminal and following the instructions from Readme > > > Build Instructions Don't. sudo apt-get install python3 or use the software manager, which is possibly a bit more user friendly invoke as python3 unless... if you're on Ulyana, which I presume is the derivative of Ubuntu 20.04LTS (haven't used yet), you can also probably install python-is-python3 to make the "default" python be Python 3. From mats at wichmann.us Thu Jul 9 10:26:45 2020 From: mats at wichmann.us (Mats Wichmann) Date: Thu, 9 Jul 2020 08:26:45 -0600 Subject: [Tutor] New to Linux, trying to install python 3 - please help :) In-Reply-To: <698528f2-b6dc-f633-fc3c-a8d135042cf3@wichmann.us> References: <2011674014.4837775.1594298279672.ref@mail.yahoo.com> <2011674014.4837775.1594298279672@mail.yahoo.com> <698528f2-b6dc-f633-fc3c-a8d135042cf3@wichmann.us> Message-ID: On 7/9/20 8:17 AM, Mats Wichmann wrote: > On 7/9/20 6:37 AM, mdrotts via Tutor wrote: >> Hi, >> I'm a total noobie to Linux and want to install Python 3 (3.8.3) on a Linux Mint (previously used on Windows 10 machine).? >> Pulling up Terminal and following the instructions from Readme >> >> >> Build Instructions > > Don't. to be clear: there's absolutely nothing wrong with building your own Pythons from source, but if you're new to Linux, you have lots of other things to think about, and you should just use the versions that your distribution has packaged for you - they have years of experience at getting that right. Trying to build can come later, if you find a need. From alan.gauld at yahoo.co.uk Thu Jul 9 12:21:54 2020 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 9 Jul 2020 17:21:54 +0100 Subject: [Tutor] New to Linux, trying to install python 3 - please help :) In-Reply-To: References: <2011674014.4837775.1594298279672.ref@mail.yahoo.com> <2011674014.4837775.1594298279672@mail.yahoo.com> <698528f2-b6dc-f633-fc3c-a8d135042cf3@wichmann.us> Message-ID: On 09/07/2020 15:26, Mats Wichmann wrote: >>> Build Instructions >> >> Don't. > > to be clear: there's absolutely nothing wrong with building your own > Pythons from source, but if you're new to Linux, you have lots of other > things to think about, and you should just use the versions that your > distribution has packaged for you One thing you should note is that Python on Linux comes in lots of parts, unlike Windows where one download gets everything. So as well as installing python3 you possibly want to install python3-tk and idle-python3.8 a few others. This is where the software manager app beats apt-get, you can search for Python3 and get a list of all the possible bits 'n pieces you might need. Similarly most popular packages you may have installed with pip are also available in the software manager - flask, lxml, etc... -- 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 alexkleider at protonmail.com Thu Jul 9 13:36:37 2020 From: alexkleider at protonmail.com (alexkleider) Date: Thu, 09 Jul 2020 17:36:37 +0000 Subject: [Tutor] New to Linux, trying to install python 3 - please help :) In-Reply-To: References: <2011674014.4837775.1594298279672.ref@mail.yahoo.com> <2011674014.4837775.1594298279672@mail.yahoo.com> <698528f2-b6dc-f633-fc3c-a8d135042cf3@wichmann.us> Message-ID: On my Debian 10 system 'apt install python3' would bring in python3.7. (Unnecessary since it's already there by default.) I believe the goal is to bring in python3.8 and if so is there any option other than to build locally? Sent with ProtonMail Secure Email. ??????? Original Message ??????? On Thursday, July 9, 2020 9:21 AM, Alan Gauld via Tutor wrote: > On 09/07/2020 15:26, Mats Wichmann wrote: > > > > > Build Instructions > > > > > > Don't. > > > > to be clear: there's absolutely nothing wrong with building your own > > Pythons from source, but if you're new to Linux, you have lots of other > > things to think about, and you should just use the versions that your > > distribution has packaged for you > > One thing you should note is that Python on Linux comes in lots of > parts, unlike Windows where one download gets everything. So as well as > installing python3 you possibly want to install > > python3-tk and > idle-python3.8 > > a few others. This is where the software manager app beats apt-get, you > can search for Python3 and get a list of all the possible bits 'n pieces > you might need. > > Similarly most popular packages you may have installed with pip are > also available in the software manager - flask, lxml, etc... > > -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- > > 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 __peter__ at web.de Thu Jul 9 13:59:36 2020 From: __peter__ at web.de (Peter Otten) Date: Thu, 09 Jul 2020 19:59:36 +0200 Subject: [Tutor] New to Linux, trying to install python 3 - please help :) References: <2011674014.4837775.1594298279672.ref@mail.yahoo.com> <2011674014.4837775.1594298279672@mail.yahoo.com> <698528f2-b6dc-f633-fc3c-a8d135042cf3@wichmann.us> Message-ID: alexkleider via Tutor wrote: > On my Debian 10 system 'apt install python3' would bring in python3.7. > (Unnecessary since it's already there by default.) I believe the goal is > to bring in python3.8 and if so is there any option other than to build > locally? I don't know. But if you end up installing from source use $ sudo make altinstall # usually safe; you can have multiple versions of # Python in parallel not $ sudo make install # danger; may break your system From PyTutor at DancesWithMice.info Thu Jul 9 17:22:11 2020 From: PyTutor at DancesWithMice.info (dn) Date: Fri, 10 Jul 2020 09:22:11 +1200 Subject: [Tutor] New to Linux, trying to install python 3 - please help :) In-Reply-To: References: <2011674014.4837775.1594298279672.ref@mail.yahoo.com> <2011674014.4837775.1594298279672@mail.yahoo.com> <698528f2-b6dc-f633-fc3c-a8d135042cf3@wichmann.us> Message-ID: On 10/07/20 4:21 AM, Alan Gauld via Tutor wrote: > On 09/07/2020 15:26, Mats Wichmann wrote: > >>>> Build Instructions >>> >>> Don't. >> >> to be clear: there's absolutely nothing wrong with building your own >> Pythons from source, but if you're new to Linux, you have lots of other >> things to think about, and you should just use the versions that your >> distribution has packaged for you +1 ... > a few others. This is where the software manager app beats apt-get, you > can search for Python3 and get a list of all the possible bits 'n pieces > you might need. > > Similarly most popular packages you may have installed with pip are > also available in the software manager - flask, lxml, etc... What is "software manager"? (in case the generic term's first search-result is not what you had in mind) - something other than apt-get - a pre-packaged distribution, eg Anaconda - an MS-Win package (also available in Linux Mint?) - something else? -- Regards =dn From mats at wichmann.us Thu Jul 9 17:58:40 2020 From: mats at wichmann.us (Mats Wichmann) Date: Thu, 9 Jul 2020 15:58:40 -0600 Subject: [Tutor] New to Linux, trying to install python 3 - please help :) In-Reply-To: References: <2011674014.4837775.1594298279672.ref@mail.yahoo.com> <2011674014.4837775.1594298279672@mail.yahoo.com> <698528f2-b6dc-f633-fc3c-a8d135042cf3@wichmann.us> Message-ID: <20293974-a3f5-af34-0b99-c6ba48799f15@wichmann.us> On 7/9/20 3:22 PM, dn via Tutor wrote: >> Similarly most popular packages you may have installed with pip are >> also available in the software manager - flask, lxml, etc... > > What is "software manager"? there's a specific (gui) tool available in Mint called "Software Manager". It's similar to the (gui) tool Ubuntu calls "Ubuntu Software". From mdrotts at yahoo.com Thu Jul 9 16:19:16 2020 From: mdrotts at yahoo.com (mdrotts) Date: Thu, 9 Jul 2020 20:19:16 +0000 (UTC) Subject: [Tutor] New to Linux, trying to install python 3 - please help :) In-Reply-To: References: <2011674014.4837775.1594298279672.ref@mail.yahoo.com> <2011674014.4837775.1594298279672@mail.yahoo.com> <698528f2-b6dc-f633-fc3c-a8d135042cf3@wichmann.us> Message-ID: <193618068.5108536.1594325956519@mail.yahoo.com> Thanks everyone! Assistance is greatly appreciated!D.? On Thursday, July 9, 2020, 01:43:38 PM EDT, alexkleider via Tutor wrote: On my Debian 10 system 'apt install python3' would bring in python3.7. (Unnecessary since it's already there by default.) I believe the goal is to bring in python3.8 and if so is there any option other than to build locally? Sent with ProtonMail Secure Email. ??????? Original Message ??????? On Thursday, July 9, 2020 9:21 AM, Alan Gauld via Tutor wrote: > On 09/07/2020 15:26, Mats Wichmann wrote: > > > > > Build Instructions > > > > > > Don't. > > > > to be clear: there's absolutely nothing wrong with building your own > > Pythons from source, but if you're new to Linux, you have lots of other > > things to think about, and you should just use the versions that your > > distribution has packaged for you > > One thing you should note is that Python on Linux comes in lots of > parts, unlike Windows where one download gets everything. So as well as > installing python3 you possibly want to install > > python3-tk and > idle-python3.8 > > a few others. This is where the software manager app beats apt-get, you > can search for Python3 and get a list of all the possible bits 'n pieces > you might need. > > Similarly most popular packages you may have installed with pip are > also available in the software manager - flask, lxml, etc... > > -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- > > 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 _______________________________________________ Tutor maillist? -? Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From PyTutor at DancesWithMice.info Thu Jul 9 18:35:05 2020 From: PyTutor at DancesWithMice.info (dn) Date: Fri, 10 Jul 2020 10:35:05 +1200 Subject: [Tutor] New to Linux, trying to install python 3 - please help :) In-Reply-To: <20293974-a3f5-af34-0b99-c6ba48799f15@wichmann.us> References: <2011674014.4837775.1594298279672.ref@mail.yahoo.com> <2011674014.4837775.1594298279672@mail.yahoo.com> <698528f2-b6dc-f633-fc3c-a8d135042cf3@wichmann.us> <20293974-a3f5-af34-0b99-c6ba48799f15@wichmann.us> Message-ID: <49978d2b-2593-f9d0-0b51-2682038d1e5f@DancesWithMice.info> On 10/07/20 9:58 AM, Mats Wichmann wrote: > On 7/9/20 3:22 PM, dn via Tutor wrote: > >>> Similarly most popular packages you may have installed with pip are >>> also available in the software manager - flask, lxml, etc... >> >> What is "software manager"? > > there's a specific (gui) tool available in Mint called "Software > Manager". It's similar to the (gui) tool Ubuntu calls "Ubuntu Software". Thanks! Have checked, and it is not mentioned in Fedora's dnf (yum/rpm) list of packages. These days, there seems to be such an abundance of package managers/downloaders, one wonders if they are not contributing to the very problem they seek to solve? To the OP: My Fedora31 (so, not the latest/32 release) is offering Python 3.7.n as its 'latest'. (Further) Recommend following Linux Mint's 'latest', unless there is something compelling/required from v3.8. Check here: https://docs.python.org/3/whatsnew/3.8.html -- Regards =dn From alan.gauld at yahoo.co.uk Thu Jul 9 18:55:20 2020 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 9 Jul 2020 23:55:20 +0100 Subject: [Tutor] New to Linux, trying to install python 3 - please help :) In-Reply-To: <193618068.5108536.1594325956519@mail.yahoo.com> References: <2011674014.4837775.1594298279672.ref@mail.yahoo.com> <2011674014.4837775.1594298279672@mail.yahoo.com> <698528f2-b6dc-f633-fc3c-a8d135042cf3@wichmann.us> <193618068.5108536.1594325956519@mail.yahoo.com> Message-ID: On 09/07/2020 21:19, mdrotts via Tutor wrote: > On my Debian 10 system 'apt install python3' would bring in python3.7. 3.8 is available on Linux Mint so there must be a deb package out there. You just need to find the ppa link and add it to your software manager. Google (or duck duck go or....) is your friend. -- 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 Jul 9 18:57:44 2020 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 9 Jul 2020 23:57:44 +0100 Subject: [Tutor] New to Linux, trying to install python 3 - please help :) In-Reply-To: References: <2011674014.4837775.1594298279672.ref@mail.yahoo.com> <2011674014.4837775.1594298279672@mail.yahoo.com> <698528f2-b6dc-f633-fc3c-a8d135042cf3@wichmann.us> Message-ID: On 09/07/2020 22:22, dn via Tutor wrote: > What is "software manager"? > - something other than apt-get It's the Linux Mint package manager. Although I was using it in the generic sense for whatever package manager your package uses, or a general one like Synaptic (which is what I personally prefer). -- 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 john at johnweller.co.uk Fri Jul 10 03:13:31 2020 From: john at johnweller.co.uk (John Weller) Date: Fri, 10 Jul 2020 08:13:31 +0100 Subject: [Tutor] New to Linux, trying to install python 3 - please help :) In-Reply-To: References: Message-ID: > 3.8 is available on Linux Mint so there must be a deb package out there. > You just need to find the ppa link and add it to your software manager. > Google (or duck duck go or....) is your friend. I have 3.8 on my Ubuntu machine and Raspberry Pi. John Weller From cs at cskk.id.au Sat Jul 11 05:35:57 2020 From: cs at cskk.id.au (Cameron Simpson) Date: Sat, 11 Jul 2020 19:35:57 +1000 Subject: [Tutor] New to Linux, trying to install python 3 - please help :) In-Reply-To: <193618068.5108536.1594325956519@mail.yahoo.com> References: <193618068.5108536.1594325956519@mail.yahoo.com> Message-ID: <20200711093557.GA84442@cskk.homeip.net> On 09Jul2020 20:19, mdrotts wrote: > Thanks everyone! Assistance is greatly appreciated!D.? > On Thursday, July 9, 2020, 01:43:38 PM EDT, alexkleider via Tutor wrote: > > On my Debian 10 system 'apt install python3' would bring in python3.7. (Unnecessary since it's already there by default.) >I believe the goal is to bring in python3.8 and if so is there any >option other than to build locally? My Ubuntu system has a "python3.8" package. What does: apt-cache search python | grep 3.8 get you? You may find that the default "python3" will remain 3.7 until you update to the next release, but that does not prevent you installing 3.8 as well. Cheers, Cameron Simpson From alexkleider at protonmail.com Sat Jul 11 12:21:15 2020 From: alexkleider at protonmail.com (alexkleider) Date: Sat, 11 Jul 2020 16:21:15 +0000 Subject: [Tutor] New to Linux, trying to install python 3 - please help :) In-Reply-To: <20200711093557.GA84442@cskk.homeip.net> References: <193618068.5108536.1594325956519@mail.yahoo.com> <20200711093557.GA84442@cskk.homeip.net> Message-ID: <0a8yWf31LYNQo2YT0wOiLhTmwuFEO1JkUL70fA7miYgskDS5gsT-8bJprXJvRUQSVx2jVFIO4mGNMFXAP7wXz4Jnty0LwVaGd90vNNmeDU0=@protonmail.com> ??????? Original Message ??????? On Saturday, July 11, 2020 2:35 AM, Cameron Simpson wrote: > On 09Jul2020 20:19, mdrotts mdrotts at yahoo.com wrote: > > > Thanks everyone! Assistance is greatly appreciated!D.? > > On Thursday, July 9, 2020, 01:43:38 PM EDT, alexkleider via Tutor tutor at python.org wrote: > > On my Debian 10 system 'apt install python3' would bring in python3.7. (Unnecessary since it's already there by default.) > > I believe the goal is to bring in python3.8 and if so is there any > > option other than to build locally? > > My Ubuntu system has a "python3.8" package. What does: > > apt-cache search python | grep 3.8 > > get you? You may find that the default "python3" will remain 3.7 until > you update to the next release, but that does not prevent you installing > 3.8 as well. > It seems Ubuntu provides it but not from the Debian upstream repo- on my Debian10 system: alex at X1:~$ apt-cache search python | grep '3.8' python-m3u8 - Python m3u8 parser - Python 2.x python3-m3u8 - Python m3u8 parser - Python 3.x python-rfc3986 - validating URI references per RFC 3986 - Python 2.x python3-rfc3986 - validating URI references per RFC 3986 - Python 3.x python-uritools - RFC 3986 compliant replacement for urlparse (Python 2) python3-uritools - RFC 3986 compliant replacement for urlparse (Python 3) I built (3.8) from source (using instructions found on the web) and it went smoothly enough. From iamsatyabrata428 at gmail.com Sun Jul 12 17:00:34 2020 From: iamsatyabrata428 at gmail.com (SATYABRATA DATTA) Date: Mon, 13 Jul 2020 02:30:34 +0530 Subject: [Tutor] Drawing confidence level ellipses Message-ID: A model predicts value of some quantity A and B which need to be fitted to the experimental 1-sigma,2-sigma etc.confidence intervals. I have a given experimental data file containing the experimental output of A,B and delta_chi_square Similar to this A. B. Chi_sq 0.0001 0.170 5.883053e+02 0.0002 0.175 5.818523e+02 0.0003 0.180 5.756402e+02 0.0004 0.185 5.696054e+02 0.0005 0.190 5.638476e+02 0.0006 0.195 5.584292e+02 0.0007 0.200 5.532851e+02 0.0008 0.205 5.484670e+02 0.0009 0.210 5.439196e+02 0.0010 0.215 5.396868e+02 0.0011 0.220 5.342531e+02 0.0012 0.225 5.259659e+02 How to plot the confidence regions on the same modelled data plot by matplotlib. From PyTutor at DancesWithMice.info Sun Jul 12 22:27:51 2020 From: PyTutor at DancesWithMice.info (dn) Date: Mon, 13 Jul 2020 14:27:51 +1200 Subject: [Tutor] Drawing confidence level ellipses In-Reply-To: References: Message-ID: <7fd4c46b-baeb-1257-c400-eaca621614f7@DancesWithMice.info> On 13/07/20 9:00 AM, SATYABRATA DATTA wrote: > A model predicts value of some quantity A and B which need to be fitted to > the experimental 1-sigma,2-sigma etc.confidence intervals. I have a given > experimental data file containing the experimental output of A,B and > delta_chi_square Similar to this > A. B. Chi_sq > 0.0001 0.170 5.883053e+02 > 0.0002 0.175 5.818523e+02 > 0.0003 0.180 5.756402e+02 > 0.0004 0.185 5.696054e+02 > 0.0005 0.190 5.638476e+02 > 0.0006 0.195 5.584292e+02 > 0.0007 0.200 5.532851e+02 > 0.0008 0.205 5.484670e+02 > 0.0009 0.210 5.439196e+02 > 0.0010 0.215 5.396868e+02 > 0.0011 0.220 5.342531e+02 > 0.0012 0.225 5.259659e+02 > How to plot the confidence regions on the same modelled data plot by > matplotlib. What are you asking? - the statistics - the Python What have you tried thus far? Are you wanting someone to write the code for you? DuckDuckGo responded to a search with multiple examples - whether their constraints suit your's is another matter though... -- Regards =dn From 1611kjb at gmail.com Mon Jul 13 15:36:06 2020 From: 1611kjb at gmail.com (1611kjb at gmail.com) Date: Mon, 13 Jul 2020 15:36:06 -0400 Subject: [Tutor] Formatting Prompts Message-ID: <0ca901d6594c$db05b820$91112860$@gmail.com> The input() function allows for a prompt to be inserted to provide information to the user. Can this string be formatted or does it just accept ascii text? I'm shooting for something like: . Counter += 1 Input("This is attempt# ", counter) . I've tried several variations and nothing seems to work. ---Mike From alan.gauld at yahoo.co.uk Mon Jul 13 18:56:34 2020 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 13 Jul 2020 23:56:34 +0100 Subject: [Tutor] Formatting Prompts In-Reply-To: <0ca901d6594c$db05b820$91112860$@gmail.com> References: <0ca901d6594c$db05b820$91112860$@gmail.com> Message-ID: On 13/07/2020 20:36, 1611kjb at gmail.com wrote: > The input() function allows for a prompt to be inserted to provide > information to the user. Can this string be formatted or does it just accept > ascii text? Strings in Python are unicode. They are also objects so have methods. One such method is the format() method so you can do things like: > Counter += 1 > input("This is attempt {} ".format(counter)) Or if you have a recent version of python use a format string: input(f"This is attempt {counter}") But you can generate the string in any way, including stored variables or the return value from a function: input(do_fancy_formatting() ) So far as input is concerned its just a regular string. How you build that string is up to 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 john at johnweller.co.uk Tue Jul 14 05:07:20 2020 From: john at johnweller.co.uk (John Weller) Date: Tue, 14 Jul 2020 10:07:20 +0100 Subject: [Tutor] Formatting Prompts In-Reply-To: <0ca901d6594c$db05b820$91112860$@gmail.com> References: <0ca901d6594c$db05b820$91112860$@gmail.com> Message-ID: <003d01d659be$2f0567b0$8d103710$@johnweller.co.uk> Hi Mike > The input() function allows for a prompt to be inserted to provide information to the > user. Can this string be formatted or does it just accept ascii text? I'm shooting for > something like: > > Counter += 1 > > Input("This is attempt# ", counter) > Your code looks as though you are trying to output a string "This is attempt #47" or whatever whereas your input command is asking the user for an attempt number - at least that is what it looks like to me, no doubt Alan will correct me if I am wrong ? If it is the case then your command should be print("This is attempt #" + str(counter)) I think. John > > > I've tried several variations and nothing seems to work. > From john at johnweller.co.uk Tue Jul 14 14:15:16 2020 From: john at johnweller.co.uk (John Weller) Date: Tue, 14 Jul 2020 19:15:16 +0100 Subject: [Tutor] FTP Upload Message-ID: <00b101d65a0a$ba444f20$2ecced60$@johnweller.co.uk> Hi I have some graphs created with matplotlib which I need to upload to a web server. I am using the command: filename = 'mygraph.png' ftp.storbinary("STOR " + filename, open(filename, "rb")) When I try to look at the graphs on the website they don't display and if I then download them to my PC using FileZilla and try to open them they are corrupt and show as Invalid Image. If however I upload them manually using FileZilla they display correctly on the website. I am obviously doing something wrong ? Advice please. TIA John John Weller 01380 723235 07976 393631 From alan.gauld at yahoo.co.uk Tue Jul 14 14:57:35 2020 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 14 Jul 2020 19:57:35 +0100 Subject: [Tutor] FTP Upload In-Reply-To: <00b101d65a0a$ba444f20$2ecced60$@johnweller.co.uk> References: <00b101d65a0a$ba444f20$2ecced60$@johnweller.co.uk> Message-ID: On 14/07/2020 19:15, John Weller wrote: > Hi > > I have some graphs created with matplotlib which I need to upload to a web server. I am using the command: > > filename = 'mygraph.png' > ftp.storbinary("STOR " + filename, open(filename, "rb")) > > When I try to look at the graphs on the website they don't display and if I then download them to my PC using FileZilla and try to open them they are corrupt and show as Invalid Image. If however I upload them manually using FileZilla they display correctly on the website. I am obviously doing something wrong ? Advice please. I don't see anything obviously wrong. Suggestions: 1) implement the callback and use it to display progress, just to check that the number of blocks is what you'd expect for the filesize 2) pass the file object explicitly: def callback(block): .... print(....) cmd = "STOR %s" % filename fd = open(...) if fd: ftp.storbinary((cmd, fd, callback) fd.close() That shouldn't change anything, but my long standing approach to anything involving files, especially binary ones, is to take full control of opening and closing! Othewise I don't know what's up. -- 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 Jul 14 16:11:31 2020 From: PyTutor at DancesWithMice.info (dn) Date: Wed, 15 Jul 2020 08:11:31 +1200 Subject: [Tutor] Formatting Prompts In-Reply-To: <003d01d659be$2f0567b0$8d103710$@johnweller.co.uk> References: <0ca901d6594c$db05b820$91112860$@gmail.com> <003d01d659be$2f0567b0$8d103710$@johnweller.co.uk> Message-ID: On 14/07/20 9:07 PM, John Weller wrote: > Hi Mike > >> The input() function allows for a prompt to be inserted to provide information to the >> user. Can this string be formatted or does it just accept ascii text? I'm shooting for >> something like: >> >> Counter += 1 >> >> Input("This is attempt# ", counter) >> > > Your code looks as though you are trying to output a string "This is attempt #47" or whatever whereas your input command is asking the user for an attempt number - at least that is what it looks like to me, no doubt Alan will correct me if I am wrong ? > > If it is the case then your command should be print("This is attempt #" + str(counter)) I think. Remember that if you open python3 at the command line, you can experiment with the REPL: typing-in commands and watching them execute immediately (or complain if there's an error). It's a very quick tool to learn something new or check one's understanding! There's no reason why a complicated "prompt" couldn't be embedded into a preceding print(). Indeed print() offers some automatic formatting 'for free'. However, remember that the default is that it will add an EOL after the printed-string! NB EOL/End-of-line varies between different OpSys. input() (note the lack of upper-case!) performs two tasks. It takes a single argument (a string) and outputs it to sysout (usually the screen - for reasons that will become obvious), it then waits for input from sysin (usually the keyboard), which it then returns to the code (as a string). So, the usual presentation is: result = input( "Any data? " ) Note the trailing space - the rationale for which can be discovered by quick experimentation; also the question-mark which is a (minimal) UX (user experience) feature. https://docs.python.org/dev/library/functions.html?highlight=input#input https://docs.python.org/dev/tutorial/controlflow.html?highlight=input Much of @Alan's earlier advice appears in: https://docs.python.org/dev/tutorial/inputoutput.html?highlight=input and the more authoritative https://docs.python.org/dev/tutorial/stdlib2.html?highlight=input -- Regards =dn From alan.gauld at yahoo.co.uk Tue Jul 14 18:10:00 2020 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 14 Jul 2020 23:10:00 +0100 Subject: [Tutor] Formatting Prompts In-Reply-To: References: <0ca901d6594c$db05b820$91112860$@gmail.com> <003d01d659be$2f0567b0$8d103710$@johnweller.co.uk> Message-ID: On 14/07/2020 21:11, dn via Tutor wrote: > preceding print(). Indeed print() offers some automatic formatting 'for > free'. However, remember that the default is that it will add an EOL > after the printed-string! But the default can be changed... -- 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 harold.doran at cambiumassessment.com Tue Jul 14 14:23:28 2020 From: harold.doran at cambiumassessment.com (Harold Doran) Date: Tue, 14 Jul 2020 18:23:28 +0000 Subject: [Tutor] FTP Upload In-Reply-To: <00b101d65a0a$ba444f20$2ecced60$@johnweller.co.uk> References: <00b101d65a0a$ba444f20$2ecced60$@johnweller.co.uk> Message-ID: <43dac009fe7f4542b691645cdec09d10@cambiumassessment.com> Consider serializing and sending as json. See examples here: https://matplotlib.org/3.1.1/devel/MEP/MEP25.html#examples -----Original Message----- From: Tutor On Behalf Of John Weller Sent: Tuesday, July 14, 2020 2:15 PM To: tutor at python.org Subject: [Tutor] FTP Upload External email alert: Be wary of links & attachments. Hi I have some graphs created with matplotlib which I need to upload to a web server. I am using the command: filename = 'mygraph.png' ftp.storbinary("STOR " + filename, open(filename, "rb")) When I try to look at the graphs on the website they don't display and if I then download them to my PC using FileZilla and try to open them they are corrupt and show as Invalid Image. If however I upload them manually using FileZilla they display correctly on the website. I am obviously doing something wrong ? Advice please. TIA John John Weller 01380 723235 07976 393631 _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From PyTutor at DancesWithMice.info Tue Jul 14 22:02:39 2020 From: PyTutor at DancesWithMice.info (dn) Date: Wed, 15 Jul 2020 14:02:39 +1200 Subject: [Tutor] Formatting Prompts In-Reply-To: References: <0ca901d6594c$db05b820$91112860$@gmail.com> <003d01d659be$2f0567b0$8d103710$@johnweller.co.uk> Message-ID: <963ba3e8-c373-13ac-0ffa-681b31be74f6@DancesWithMice.info> On 15/07/20 10:10 AM, Alan Gauld via Tutor wrote: > On 14/07/2020 21:11, dn via Tutor wrote: > >> preceding print(). Indeed print() offers some automatic formatting 'for >> free'. However, remember that the default is that it will add an EOL >> after the printed-string! > > But the default can be changed... True! Apologies, I didn't provide a web.ref for that: https://docs.python.org/dev/library/functions.html?highlight=print#print Personal preference: if the prompt-string is somewhat involved (cf "Type here: ") per the OP's request, is to assign the calculated value to a string variable (object) and then use that in the input(). Such improves readability. That said, that practice can/could/should also be applied in other situations, because will it eases testing/proving/debugging that block of code! -- Regards =dn From PyTutor at DancesWithMice.info Tue Jul 14 22:53:25 2020 From: PyTutor at DancesWithMice.info (dn) Date: Wed, 15 Jul 2020 14:53:25 +1200 Subject: [Tutor] FTP Upload In-Reply-To: <00b101d65a0a$ba444f20$2ecced60$@johnweller.co.uk> References: <00b101d65a0a$ba444f20$2ecced60$@johnweller.co.uk> Message-ID: On 15/07/20 6:15 AM, John Weller wrote: > I have some graphs created with matplotlib which I need to upload to a web server. I am using the command: > > filename = 'mygraph.png' > ftp.storbinary("STOR " + filename, open(filename, "rb")) > > When I try to look at the graphs on the website they don't display and if I then download them to my PC using FileZilla and try to open them they are corrupt and show as Invalid Image. If however I upload them manually using FileZilla they display correctly on the website. I am obviously doing something wrong ? Advice please. I've not tried the ftp library. Remember that ftp can work in binary or text modes. Does the former need to be set, to avoid binary-file corruption? -- Regards =dn From alan.gauld at yahoo.co.uk Wed Jul 15 05:05:56 2020 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 15 Jul 2020 10:05:56 +0100 Subject: [Tutor] FTP Upload In-Reply-To: <000a01d65a85$5c4ed9f0$14ec8dd0$@johnweller.co.uk> References: <00b101d65a0a$ba444f20$2ecced60$@johnweller.co.uk> <000a01d65a85$5c4ed9f0$14ec8dd0$@johnweller.co.uk> Message-ID: <475b605d-265d-ef60-9881-cfdda2902ed4@yahoo.co.uk> Forwarding to list... On 15/07/2020 09:53, John Weller wrote: >> I don't see anything obviously wrong. >> >> Suggestions: >> >> 1) implement the callback and use it to display progress, just to check that the >> number of blocks is what you'd expect for the filesize >> >> 2) pass the file object explicitly: >> >> def callback(block): >> .... >> print(....) >> >> cmd = "STOR %s" % filename >> fd = open(...) >> if fd: >> ftp.storbinary((cmd, fd, callback) >> fd.close() >> >> That shouldn't change anything, but my long standing approach to anything involving >> files, especially binary ones, is to take full control of opening and closing! >> >> Otherwise I don't know what's up. > Thanks to all who responded. I've found the problem - but unfortunately not the cause or the cure! The corrupt files have lost a handful of bytes, not as a block, and the file is being terminated with x0A which it shouldn't be. At least I know the syntax is correct, I'll try Alan's approach next. It must be something in my environment so I'll look at that as well. John -- 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 Wed Jul 15 13:55:16 2020 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 15 Jul 2020 18:55:16 +0100 Subject: [Tutor] FTP Upload In-Reply-To: <00d601d65ac2$57420cb0$05c62610$@johnweller.co.uk> References: <00b101d65a0a$ba444f20$2ecced60$@johnweller.co.uk> <00d601d65ac2$57420cb0$05c62610$@johnweller.co.uk> Message-ID: Always reply-ALL orReply-List to include the tutor list. On 15/07/2020 17:09, John Weller wrote: >> 1) implement the callback and use it to display progress, just to check that the >> number of blocks is what you'd expect for the filesize >> > I'm struggling to understand callback. As I know the problem, ie bytes being dropped,> would callback be of any benefit? If so, how would I implement it. >> def callback(block): >> .... >> print(....) At the simplest level just store how many times callback gets called. (It should be once every block) so you can see if the expected number of blocks are being sent. In particular whether the final (partial) block is being sent... You could do fancier stuff like check the size of block each time and print that: count = 0 def callback(black): global count count += 1 print("Block %d sent with size %d" % (count, len(block)) cmd = "STOR %s" % "my_graph.png" fd = open("my_graph.png", "rb") if fd: # I assume open() returns False if it can't open the file ftp.storbinary(cmd, fd, callback)fd.close() print ("A total of %d blocks sent" % count) Assuming your files are not massive. If they are then simply limit the prints to every 10th or if len(block) != blocksize Note, none of that is tested, just based on reading the docs on callback!... :-) -- 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 kumarmayank7138 at gmail.com Thu Jul 16 04:18:31 2020 From: kumarmayank7138 at gmail.com (MAYANK KUMAR) Date: Thu, 16 Jul 2020 13:48:31 +0530 Subject: [Tutor] I am not able to( " import Youtube from pytube ")I have to submit my project in 2 days. Please help sir I would be very grateful to you Message-ID: I have installed by pytube and pytube 3 by pip install pytube .And I am making a youtube video downloader so I have to import youtube from pytube which shows error .Please help me sir . I would be very grateful to you .Please reply fast.Thankyou Sir. From mats at wichmann.us Thu Jul 16 08:16:59 2020 From: mats at wichmann.us (Mats Wichmann) Date: Thu, 16 Jul 2020 06:16:59 -0600 Subject: [Tutor] I am not able to( " import Youtube from pytube ")I have to submit my project in 2 days. Please help sir I would be very grateful to you In-Reply-To: References: Message-ID: <9d46b73e-833b-c2e4-3668-9b7f565466b7@wichmann.us> On 7/16/20 2:18 AM, MAYANK KUMAR wrote: > I have installed by pytube and pytube 3 by pip install pytube .And I am > making a youtube video downloader so I have to import youtube from pytube > which shows error .Please help me sir . I would be very grateful to you > .Please reply fast.Thankyou Sir. You'll need to post what you have tried, and what the errors were (paste, don't try to summarize: "which shows error" is useless to people who want to help). Probably you'll get the best information by contacting the project directly. Use pytube3 for Python 3. From alan.gauld at btinternet.com Thu Jul 16 09:30:24 2020 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 16 Jul 2020 14:30:24 +0100 Subject: [Tutor] FTP Upload In-Reply-To: <00a901d65b73$5aff5d60$10fe1820$@johnweller.co.uk> References: <00b101d65a0a$ba444f20$2ecced60$@johnweller.co.uk> <00d601d65ac2$57420cb0$05c62610$@johnweller.co.uk> <00a901d65b73$5aff5d60$10fe1820$@johnweller.co.uk> Message-ID: On 16/07/2020 14:16, John Weller wrote: >> def callback(black): >> global count >> count += 1 >> print("Block %d sent with size %d" % (count, len(block)) > I included the callback procedure in a test program > I was running and it caused syntax Count the parens in the print line. I was one short. oops... -- 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 john at johnweller.co.uk Thu Jul 16 09:16:42 2020 From: john at johnweller.co.uk (John Weller) Date: Thu, 16 Jul 2020 14:16:42 +0100 Subject: [Tutor] FTP Upload In-Reply-To: References: <00b101d65a0a$ba444f20$2ecced60$@johnweller.co.uk> <00d601d65ac2$57420cb0$05c62610$@johnweller.co.uk> Message-ID: <00a901d65b73$5aff5d60$10fe1820$@johnweller.co.uk> > > On 15/07/2020 17:09, John Weller wrote: > >> 1) implement the callback and use it to display progress, just to > >> check that the number of blocks is what you'd expect for the filesize > >> > > I'm struggling to understand callback. As I know the problem, ie bytes being > dropped,> would callback be of any benefit? If so, how would I implement it. > > >> def callback(block): > >> .... > >> print(....) > > At the simplest level just store how many times callback gets called. > (It should be once every block) so you can see if the expected number of blocks are > being sent. In particular whether the final (partial) block is being sent... > > You could do fancier stuff like check the size of block each time and print that: > > count = 0 > > def callback(black): > global count > count += 1 > print("Block %d sent with size %d" % (count, len(block)) > > > cmd = "STOR %s" % "my_graph.png" > fd = open("my_graph.png", "rb") > if fd: # I assume open() returns False if it can't open the file > ftp.storbinary(cmd, fd, callback)fd.close() print ("A total of %d blocks sent" > % count) > > Assuming your files are not massive. If they are then simply limit the prints to every > 10th or if len(block) != blocksize > > Note, none of that is tested, just based on reading the docs on callback!... :-) Hi Alan, Thanks as always for your help. The problem seems to have gone away so was obviously caused by a noisy internet connection; I immediately jumped to the conclusion that it was something I had done wrong ? I included the callback procedure in a test program I was running and it caused syntax errors in the following lines that I couldn't trace so in the end, as it is now working, I left it out. Regards John John Weller 01380 723235 07976 393631 From lorinkintrea at gmail.com Thu Jul 16 19:45:48 2020 From: lorinkintrea at gmail.com (tech nician) Date: Thu, 16 Jul 2020 16:45:48 -0700 Subject: [Tutor] I have been having difficulty getting cv2 to be recognized...its missing Message-ID: I have the code setup but cv2 is not in the module directory. also understand that opencv-python can be a substitute. I am running 3.7 and pycharm. What am I doing wrong? Here is some screen captures? Used pip to install opencv-python .. Lorin 253-533-4710 From alan.gauld at yahoo.co.uk Fri Jul 17 04:05:31 2020 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 17 Jul 2020 09:05:31 +0100 Subject: [Tutor] I have been having difficulty getting cv2 to be recognized...its missing In-Reply-To: References: Message-ID: On 17/07/2020 00:45, tech nician wrote: > I have the code setup but cv2 is not in the module directory. also > understand that opencv-python can be a substitute. I am running 3.7 and > pycharm. What am I doing wrong? Since this is not a standard library module you are probably better asking on the module support forum. A quick Google search suggests it is a very common issue since most hits on opencv are for the same problem! > Here is some screen captures? The mail server strips out attachments for security reasons. Other information that might be useful: 1) the OS you are using 2) The Python version and distro if non-standard(eg anaconda) 3) The exact command line used to install opencv -- 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 nickylodeon2000 at gmail.com Fri Jul 17 17:18:55 2020 From: nickylodeon2000 at gmail.com (Nick Lukashevich) Date: Sat, 18 Jul 2020 00:18:55 +0300 Subject: [Tutor] Woody, your personal assistant. Draft 0.1 Message-ID: <30bcbe93-16ff-53f6-27d8-e1d1b4453d7a@gmail.com> Hello world! My first email here! :) I am Nick D. Lukashevich or NDL, or nickelodeon I am currently on Modules and Functions topic of Learning to Program and am very close to starting Handling Files. So I have come up with something really really fun. :) It's my first useful piece of software. :) What it does is it listens to user's commands and does what he or she says. Well, currently it can only open programs and the list of programs is quite short. But I am really excited to learn more :) I called it Woody just because it sounds sweet and kind. I hope you will find it fun too and I will be happy to hear any comments. The program uses speech recognition module I installed it using pip3 like so "pip3 install SpeechRecognition" The module requires PyAudio which I got from my system's repositories If you run Woody in terminal you'll get a few debug messages about ALSA and stuff Well, there is a helpful link(https://stackoverflow.com/questions/7088672/pyaudio-working-but-spits-out-error-messages-each-time/17673011#17673011) But for now I just don't really care, I will learn how to deal with these things next :) When run in IDLE, it looks much prettier I found out that if your run it this way: "nohup ./woody.py", the program will run in background even after you close the shell which is what you would expect from an assistant :) Thank you! Here is the code: #!/usr/bin/python3 ############################# # Program?? :?? Woody.py # Author??? :?? N.D.Lukashevich # Version?? :?? Draft 0.1 ''' Woody, personal assistant. ''' ############################# import speech_recognition as spr import os # This is a list of programs which Woody can open for you. # First elements of each sublist are names of programs # and actual commands to be run by os.system(). # All the rest strings in sublists are other possible ways a user can name a program. # I live in Russia, so I added some names in Russian. # Case doesn't matter. I think prefixes and postfixes doesn't matter either, # but I need to test it better :) programs = [ ??????????? ['google-chrome', '???? ????', '???????'], ??????????? ['tomatoes', '???????', '?????', '?????', '???'], ??????????? ['blender', '???????'], ??????????? ['supertux2', '???????'], ??????????? ['xonotic-sdl', '?????'], ??????????? ['supertuxkart'] ??????????? ] # If a user's command sounds like 'open ', # this function will deal with it # Commands is a list of words said by a user def openProgram(commands): ??? for command in commands: ??????? for program in programs: ??????????? for name in range(0, len(program)): ??????????????? # the second case is useful because some possible names ??????????????? # are longer than those in programs list ??????????????? # this check let us not care about different endings ??????????????? if command in program[name] or program[name] in command: ??????????????????? os.system(program[0]) ??????????????????? return store = spr.Recognizer() with spr.Microphone() as mic: ??? print('$ ', end=' ') ??? while True: ??????? audio_input = store.record(mic, duration=5) ??????? try: ??????????? # I will implement a way to support Russian too ??????????? # by using language="ru-RU" parameter of recognize_google() ??????????? # It doesn't work sometime, though. Because of my programming :) ??????????? # I think maybe using an argument is a way to go like "./woody.py ru" ??????????? speech = store.recognize_google(audio_input) ??????????? # speech is a string, here we set it to lowercase for convenience ??????????? speech = speech.lower() ??????????? print(speech) ??????????? # convert string to a list, so it will be easier to work with commands ??????????? commands = speech.split() ??????????? # '??????' is the Russian for 'open', it's the same about 'play' :) ??????????? if 'open' in commands or '??????' in commands: ??????????????? openProgram(commands) ??????????? # This is yet to be implemented ??????????? if 'play' in commands or '??????' in commands: ??????????????? playMusic() ??????????? # if a user said something, print a new line ??????????? if len(commands) > 0: ??????????????? print('$ ', end=' ') ??????????? # else stay on the same one ??????????? else: ??????????????? print('',end='') ??????? except: ??????????? # I am yet to learn about error handling :) ??????????? # Although I do know that this is not the way to do it :) ??????????? print('',end='') From alan.gauld at yahoo.co.uk Fri Jul 17 19:00:07 2020 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 18 Jul 2020 00:00:07 +0100 Subject: [Tutor] Woody, your personal assistant. Draft 0.1 In-Reply-To: <30bcbe93-16ff-53f6-27d8-e1d1b4453d7a@gmail.com> References: <30bcbe93-16ff-53f6-27d8-e1d1b4453d7a@gmail.com> Message-ID: On 17/07/2020 22:18, Nick Lukashevich wrote: > Hello world! My first email here! :) I am Nick D. Lukashevich or NDL, or > nickelodeon > > I am currently on Modules and Functions topic of Learning to Program and > am very close to starting Handling Files. > Sounds like its my tutorial you are following and you look like you are getting to grips with things pretty well. You've certainly gone well ahead of the material in the tutor. > The program uses speech recognition module That's pretty ambitious and something I've never tried myself. I've done a little bit of speech synthesis (ie text to speech) but never the other way round. Well done. -- 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 nickylodeon2000 at gmail.com Fri Jul 17 20:36:09 2020 From: nickylodeon2000 at gmail.com (Nick Lukashevich) Date: Sat, 18 Jul 2020 03:36:09 +0300 Subject: [Tutor] Woody, your personal assistant. Draft 0.1 In-Reply-To: References: <30bcbe93-16ff-53f6-27d8-e1d1b4453d7a@gmail.com> Message-ID: Thank you! Yeah, I must say I really love your tutorial. And it feels awesome to communicate with you. This is something I've loved since I was a little kid - computers - and I feel this something in me that makes me want to change things, express myself, have fun, take control in other words Just create thousands of cool things. I am actually watching Steve Jobs The Man In The Machine here (when taking breaks from working with the tutorial :)), and although I do consider him the one of those who made this world a worse place, I can't help myself loving the other side of coin. And he said that some people, they could've become poets or painters but in our time, they chose to express themselves using machines. I really liked that line, because I am just learning but I've already felt it. This kind of something that comes from your childhood, something in your nature that makes you love computers and especially the art of programming them. On 7/18/20 2:00 AM, Alan Gauld via Tutor wrote: > On 17/07/2020 22:18, Nick Lukashevich wrote: >> Hello world! My first email here! :) I am Nick D. Lukashevich or NDL, or >> nickelodeon >> >> I am currently on Modules and Functions topic of Learning to Program and >> am very close to starting Handling Files. >> > Sounds like its my tutorial you are following and you look like > you are getting to grips with things pretty well. You've certainly > gone well ahead of the material in the tutor. > >> The program uses speech recognition module > That's pretty ambitious and something I've never tried myself. > I've done a little bit of speech synthesis (ie text to speech) > but never the other way round. > > Well done. > From nathan-tech at hotmail.com Sat Jul 18 06:57:10 2020 From: nathan-tech at hotmail.com (nathan tech) Date: Sat, 18 Jul 2020 11:57:10 +0100 Subject: [Tutor] a little help with the re odule Message-ID: Hi there I hope everyone is well. I'm trying to do a little bit of work with the re module but have rather gotten stuck and am hoping someone will be able to fill in the gaps in my understanding. First, what I'm trying to do: I want to match one string to another, but the match pattern has to be flexible. In this regard, what I'd like to do is: User supplies: The cat sat on the mat. The match pattern is: The * sat on the * Now with this example a bit of work with the \w match would work fine. What I can not figure out is lets say I have the match pattern: The * sat on the * What I want to do is for that to match all of the following: the cat sat on the mat The dog sat on the shoe The dog and the cat sat on the hoverboard. The big angry mouse sat on the mat and ate it. Is this even possible? Thanks for any help. Nathan From alan.gauld at yahoo.co.uk Sat Jul 18 07:43:19 2020 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 18 Jul 2020 12:43:19 +0100 Subject: [Tutor] a little help with the re odule In-Reply-To: References: Message-ID: On 18/07/2020 11:57, nathan tech wrote: > I'm trying to do a little bit of work with the re module but have rather > gotten stuck and am hoping someone will be able to fill in the gaps in > my understanding. Sure, but first the usual caveat. regex are immensely powerful and quite addictive. But very often the regular string methods are faster and simpler. Don't let the cleverness of regex seduce you into over use. That having been said... > The match pattern is: The * sat on the * > > Now with this example a bit of work with the \w match would work fine. For single words yes. But you can combine regex with multipliers and optional controls etc. regex is like a programming language with many of the same control structures. Also there will always be several ways to do it. It is worth bookmarking one of the online regex testers to try out your regex to ensure it does what you expect. > What I can not figure out is lets say I have the match pattern: The * > sat on the * > > What I want to do is for that to match all of the following: > > the cat sat on the mat > The dog sat on the shoe While \w will work for a single word it gets more complex with multiple words. > The dog and the cat sat on the hoverboard. > The big angry mouse sat on the mat and ate it. This gets more complex because you want to match multiple words between The and sat. The simplest way here is probably the .* combination (zero or more repetitions of any character). The .* sat on the .* Or + if you want at least 1 character. The .+ sat on the .+ But there are a myriad other ways to grab that content depending on what exactly you plan on doing with the matches once you grab them. You might find my tutorial topic on regex useful. -- 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 Jul 18 11:32:24 2020 From: mats at wichmann.us (Mats Wichmann) Date: Sat, 18 Jul 2020 09:32:24 -0600 Subject: [Tutor] a little help with the re odule In-Reply-To: References: Message-ID: <0e44ee98-dc0b-27e9-b73d-7cced8719700@wichmann.us> On 7/18/20 5:43 AM, Alan Gauld via Tutor wrote: > On 18/07/2020 11:57, nathan tech wrote: > >> I'm trying to do a little bit of work with the re module but have rather >> gotten stuck and am hoping someone will be able to fill in the gaps in >> my understanding. > > Sure, but first the usual caveat. regex are immensely powerful and quite > addictive. But very often the regular string methods are faster and > simpler. Don't let the cleverness of regex seduce you into over use. That (entirely true) being said, regular expressions were actually designed for this kind of processing of natural language where the contents of the input can vary quite a bit so also don't be scared of it! :) From nathan-tech at hotmail.com Sat Jul 18 08:51:22 2020 From: nathan-tech at hotmail.com (nathan tech) Date: Sat, 18 Jul 2020 13:51:22 +0100 Subject: [Tutor] a little help with the re odule In-Reply-To: References: Message-ID: As always, the exact answer I was looking for, thanks Alan! I'll definitely check out the tutorial you mentioned as well. On 18/07/2020 12:43, Alan Gauld via Tutor wrote: > On 18/07/2020 11:57, nathan tech wrote: > >> I'm trying to do a little bit of work with the re module but have rather >> gotten stuck and am hoping someone will be able to fill in the gaps in >> my understanding. > Sure, but first the usual caveat. regex are immensely powerful and quite > addictive. But very often the regular string methods are faster and > simpler. Don't let the cleverness of regex seduce you into over use. > > That having been said... > >> The match pattern is: The * sat on the * >> >> Now with this example a bit of work with the \w match would work fine. > For single words yes. But you can combine regex with multipliers and > optional controls etc. regex is like a programming language with many > of the same control structures. > > Also there will always be several ways to do it. It is worth bookmarking > one of the online regex testers to try out your regex to ensure it does > what you expect. > >> What I can not figure out is lets say I have the match pattern: The * >> sat on the * >> >> What I want to do is for that to match all of the following: >> >> the cat sat on the mat >> The dog sat on the shoe > While \w will work for a single word it gets more complex > with multiple words. > >> The dog and the cat sat on the hoverboard. >> The big angry mouse sat on the mat and ate it. > This gets more complex because you want to match multiple words between > The and sat. The simplest way here is probably the .* combination > (zero or more repetitions of any character). > > The .* sat on the .* > > Or + if you want at least 1 character. > > The .+ sat on the .+ > > But there are a myriad other ways to grab that content depending > on what exactly you plan on doing with the matches once you grab them. > > You might find my tutorial topic on regex useful. > From cs at cskk.id.au Sun Jul 19 01:55:58 2020 From: cs at cskk.id.au (Cameron Simpson) Date: Sun, 19 Jul 2020 15:55:58 +1000 Subject: [Tutor] a little help with the re odule In-Reply-To: References: Message-ID: <20200719055558.GA78641@cskk.homeip.net> On 18Jul2020 13:51, nathan tech wrote: >As always, the exact answer I was looking for, thanks Alan! >>>What I want to do is for that to match all of the following: >>> >>>the cat sat on the mat >>>The dog sat on the shoe >>While \w will work for a single word it gets more complex >>with multiple words. >> >>>The dog and the cat sat on the hoverboard. >>>The big angry mouse sat on the mat and ate it. >>This gets more complex because you want to match multiple words between >>The and sat. The simplest way here is probably the .* combination >>(zero or more repetitions of any character). >> >>The .* sat on the .* >> >>Or + if you want at least 1 character. >> >>The .+ sat on the .+ Just some things I find handy in this kind of situation: The (\w.*\w) sat on the (\w.*\w). matches strings which start and end with word characters. Good for phrases. Whitespace might not always be spaces. \s+ is a general thing for whitespace. However, it makes your patterns hard to read (and therefore, hard to debug): The\s+(\w.*\w)\s+sat\s+on\s+the\s+(\w.*\w). You can simplify various things by normalising the input. For whitespace this is pretty easy: text = re.sub(r'\s+', ' ', text) and now all the whitespace is a single space, letting you use your simpler regexps. Including assuming that every word gap is only one space, not several. And finally, when you're pulling bits out of longer regexps, do not forget the "named groups": The (?P\w.*\w) sat on the (?P\w.*\w). thus: ptn = re.compile(r'The (?P\w.*\w) sat on the (?P\w.*\w).') m = ptn.match(text) the_cat = m.group('subject') the_mat = m.group('object') For you example points at group 1 and 2 is easy, but regexps tend to get out of handy and naming the brackets bits of interest is extremely useful. Cheers, Cameron Simpson From cs at cskk.id.au Sun Jul 19 04:02:16 2020 From: cs at cskk.id.au (Cameron Simpson) Date: Sun, 19 Jul 2020 18:02:16 +1000 Subject: [Tutor] a little help with the re odule In-Reply-To: <20200719055558.GA78641@cskk.homeip.net> References: <20200719055558.GA78641@cskk.homeip.net> Message-ID: <20200719080216.GA7697@cskk.homeip.net> On 19Jul2020 15:55, Cameron Simpson wrote: >For you example points at group 1 and 2 is easy, but regexps tend to >get >out of handy and naming the brackets bits of interest is extremely >useful. Argh. Word garbage. I meant to type: For your example pointing at groups 1 and 2 is easy, but regexps tend to get out of hand and naming the bracketed bits of interest is extremely useful. Apologies, Cameron Simpson From manpritsinghece at gmail.com Sun Jul 19 06:55:48 2020 From: manpritsinghece at gmail.com (Manprit Singh) Date: Sun, 19 Jul 2020 16:25:48 +0530 Subject: [Tutor] looping over a dictionary with tuple of 2 elements as value Message-ID: Dear all, Consider a dictionary as given below : a = {"A" : (3, 1), "B" : (4, 9)} Here in this dictionary for key "A" the value is tuple (3, 1), and for key "B" the value is tuple (4, 9) . Now for looping over this dictionary to get an output like this : A 3 1 B 4 9 for i, (j, k) in a.items( ): print(i, j, k) Is this for loop syntactically correct ? Regards Manprit Singh From alan.gauld at yahoo.co.uk Sun Jul 19 08:40:30 2020 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 19 Jul 2020 13:40:30 +0100 Subject: [Tutor] looping over a dictionary with tuple of 2 elements as value In-Reply-To: References: Message-ID: On 19/07/2020 11:55, Manprit Singh wrote: > Dear all, > Consider a dictionary as given below : > a = {"A" : (3, 1), "B" : (4, 9)} > Here in this dictionary for key "A" the value is tuple (3, 1), and for key > "B" the value is tuple (4, 9) . Now for looping over this dictionary to get > an output like this : > A 3 1 > B 4 9 > > for i, (j, k) in a.items( ): > print(i, j, k) > > Is this for loop syntactically correct ? When it comes to syntax the best place to ask is the interpreter: >>> a = {"A" : (3, 1), "B" : (4, 9)} >>> for i, (j, k) in a.items( ): print(i,j,k) A 3 1 B 4 9 >>> So yes, it is syntactically correct. -- 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 jf_byrnes at comcast.net Sun Jul 19 16:25:55 2020 From: jf_byrnes at comcast.net (Jim) Date: Sun, 19 Jul 2020 15:25:55 -0500 Subject: [Tutor] Debug help Message-ID: Mint 18.3 - Python 3.6 I wrote a script that uses the yfinance, oosheet and pandas modules to get closing stock prices from Yahoo Finance to put in a Libreoffice spreadsheet to use in a stock trading simulation. I usually run the script on a Sunday to get each days closing prices and calculate the stocks value in the porfilio for the previous week. The script works fine on Mon to Thurs but gives the following error when I try to get Fridays prices. If I run the script on the following Monday it works without error. (env36) jfb at jims-mint18 ~ $ /home/jfb/EVs/env36/bin/python3 /home/jfb/Dev/Python/LO_update_stk_history_yf_dev.py Traceback (most recent call last): File "/home/jfb/Dev/Python/LO_update_stk_history_yf_dev.py", line 128, in update_from_web() File "/home/jfb/Dev/Python/LO_update_stk_history_yf_dev.py", line 102, in update_from_web S().value = stk File "/home/jfb/EVs/env36/lib/python3.6/site-packages/oosheet/__init__.py", line 438, in value cell.setValue(value) uno.RuntimeException: Couldn't convert Date 2020-07-17 134858.5 <> 2020-07-17 134858.5 << is the value of the 1st stk in the list>> Name: Close, dtype: float64 to a UNO type; caught exception: : 'Series' object has no attribute 'getTypes', traceback follows /home/jfb/EVs/env36/lib/python3.6/site-packages/pandas/core/generic.py:5274 in function __getattr__() [return object.__getattribute__(self, name)] I guess something internal at Yahoo Finance is causing the problem. I found an issue that looks similar to mine on the yFinance github page and added to it including a small test program to demonstrate the problem but never heard back. Here is the part of the code that generates the error, the remainder of the script just manipulates the spreadsheet. for stock in stocks: # ~ count = 0 # ~ while count < total_num_of_stocks: # ~ while count < 33: while True: # ~ count = count + 1 try: # ~ print('Day = ' + day) ticker = yf.Ticker(stock) # ~ print('Ticker = ' + ticker) hist = ticker.history(start=day) # ~ print('History = ' + hist) close_price = hist.loc[day]['Close'] # ~ print('Close = ' + close_price) closing_prices.append(close_price) except: print ('yfinance JSONDecodeError DAILY, retyring') # ~ print ('ticker: ' + hist) continue break # Get stock values by multiplying the list elements by number of shares # using zip() value = [num_shares * stk for num_shares, stk in zip(shares, closing_prices)] # Put date in cell then go down one row S(cell_loc).string = day S().dispatch('GoDown') # Fill column for MY100000 with stock values using the number of stocks in MY100000 for stk in value[:total_num_MY100000]: S().value = stk # Line 102 S().dispatch('GoDown') If I run the script with the print lines outside the except commented out it will print the date in the spreadsheet, go down 1 cell and throw the error and end. If I uncomment any of the print lines it seems to go into a endless loop printing out the exception error msg. I wonder if anyone has an idea of how to figure out what Yahoo Finance is doing to cause a problem and I am curious as to why uncommenting a print seems to cause an endless loop. Thanks, Jim From cs at cskk.id.au Sun Jul 19 19:57:46 2020 From: cs at cskk.id.au (Cameron Simpson) Date: Mon, 20 Jul 2020 09:57:46 +1000 Subject: [Tutor] Debug help In-Reply-To: References: Message-ID: <20200719235746.GA27668@cskk.homeip.net> On 19Jul2020 15:25, jim wrote: [...] >Here is the part of the code that generates the error, the remainder of >the script just manipulates the spreadsheet. > > for stock in stocks: > # ~ count = 0 > # ~ while count < total_num_of_stocks: > # ~ while count < 33: > while True: > # ~ count = count + 1 > try: > # ~ print('Day = ' + day) > ticker = yf.Ticker(stock) > # ~ print('Ticker = ' + ticker) > hist = ticker.history(start=day) > # ~ print('History = ' + hist) > close_price = hist.loc[day]['Close'] > # ~ print('Close = ' + close_price) > closing_prices.append(close_price) > > except: > print ('yfinance JSONDecodeError DAILY, retyring') > # ~ print ('ticker: ' + hist) > continue > break > > # Get stock values by multiplying the list elements by number of shares > # using zip() > value = [num_shares * stk for num_shares, stk in zip(shares, >closing_prices)] > > # Put date in cell then go down one row > S(cell_loc).string = day > S().dispatch('GoDown') > > # Fill column for MY100000 with stock values using the number of >stocks in MY100000 > for stk in value[:total_num_MY100000]: > S().value = stk # Line 102 > S().dispatch('GoDown') > >If I run the script with the print lines outside the except commented >out it will print the date in the spreadsheet, go down 1 cell and >throw the error and end. If I uncomment any of the print lines it >seems to go into a endless loop printing out the exception error msg. > >I wonder if anyone has an idea of how to figure out what Yahoo Finance >is doing to cause a problem and I am curious as to why uncommenting a >print seems to cause an endless loop. I'm unfamiliar with Yahoo Finance, but I want to comment on your debugging issues. Your code's control structure is like this: while True: try: [...] # ~ print('Close = ' + close_price) [...] except: print ('yfinance JSONDecodeError DAILY, retyring') # ~ print ('ticker: ' + hist) continue First up, consider what happens when anything goes wrong inside the try/except: you print something and continue, restarting the loop. If that thing happens on every pass, you get an infinite loop. See the commented out print(). Supposing close_price is not a string. The addition will raise a TypeError exception. And into the infinite loop you go. Better, for debugging statements, to perform as few operations as possible. because if something is wrong, any operation might itself fail because things as not as you expect. So consider: print('Close = ', close_price) Because print() converts all is arguments to strings, the form above does no addition and still produces a result. However, the biggest problem here is what we call a "bare except": an except which catches _any_ exception. That's handy for reporting on _everything_ which explodes, but otherwise a bad practice. The rule of thumb for catching excpetions is to _only_ catch what you expect and know how to handle _correctly_. Your code catches everything (again, great for reporting) but does not know what to do with it. Because you do not print the exception or where it came from, you learn nothing about the underlying issue. Consider this: while True: try: [...] # ~ print('Close = ' + close_price) [...] except Exception as e: print("unexpection exception:", e) print ('yfinance JSONDecodeError DAILY, retyring') # ~ print ('ticker: ' + hist) continue At least this will show you what is wrong. But better still is not to catch it at all. It is unexpected, so your print-and-continue action is probably inappropriate (because you do not know the context - _anything_ might be wrong). As a scarey example, consider: while True: try: x = closed_price [...] # ~ print('Close = ' + close_price) [...] except Exception as e: print("unexpection exception:", e) print ('yfinance JSONDecodeError DAILY, retyring') # ~ print ('ticker: ' + hist) continue Here, "closed_price" is a misspelled variable name. Python will raise a NameError because it is an unknown name, and you won't have even printed out the exception, so you will be blind to a simple typo in the code. The usual pattern with try/except is to catch exactly what you can handle, and let other exceptions out. That does abort your programme, but you get the raw exception _and_ a nice stacktrace showing where it came from: while True: try: x = closed_price [...] # ~ print('Close = ' + close_price) [...] except HTTPError as e: print("HTTP networking error", e) time.sleep(1) continue Here we catch an expected failure (the network maybe went away, and the HTTP conencion did not work). We print that, and we sleep for 1 second to avoid a busy loop (spinning until the network works again). But this is an _expected_ exception with a well defined plan for accomodating it. So we catch it, report it, and continue. Now, you may only know how to handle a specific type of an exception. Then the pattern is only slightly more elaborate: while True: try: x = closed_price [...] # ~ print('Close = ' + close_price) [...] except HTTPError as e: # I'm just making up some kind of test on the exception here if e.errno == errno.EIO: print("HTTP networking error", e) time.sleep(1) continue raise Here, we know how to handle a specific type of HTTPError. Any others are just reraised, producing an error and a stack trace for you to examine. Cheers, Cameron Simpson From anilduggirala at fastmail.fm Mon Jul 20 13:37:16 2020 From: anilduggirala at fastmail.fm (Anil Felipe Duggirala) Date: Mon, 20 Jul 2020 12:37:16 -0500 Subject: [Tutor] moving a virtual environment to a new machine Message-ID: hello, When I am moving to a new machine, and I have python projects that are working with particular virtual environments. What is the best practice to recreate these virtual environments in the new machine. Assume, for example, that I will be working on the same operating system on the new machine. I have tried just copying the venv folders to a usb pendrive, but there appear to always be symlinks or some kind of links that are not easily copied (they may be in a system folder). Will simply creating a new environment (in the new machine) an pip installing the required packages create a "practically identical" virtual environment? does git serve any purpose in achieving this? thank you, From mats at wichmann.us Mon Jul 20 14:03:19 2020 From: mats at wichmann.us (Mats Wichmann) Date: Mon, 20 Jul 2020 12:03:19 -0600 Subject: [Tutor] moving a virtual environment to a new machine In-Reply-To: References: Message-ID: <3d4c19c4-86f2-9376-a3f0-10fd0df063d3@wichmann.us> On 7/20/20 11:37 AM, Anil Felipe Duggirala wrote: > hello, > > When I am moving to a new machine, and I have python projects that are > working with particular virtual environments. What is the best practice > to recreate these virtual environments in the new machine. Assume, for > example, that I will be working on the same operating system on the new > machine. > > I have tried just copying the venv folders to a usb pendrive, but there > appear to always be symlinks or some kind of links that are not easily > copied (they may be in a system folder). > > Will simply creating a new environment (in the new machine) an pip > installing the required packages create a "practically identical" > virtual environment? does git serve any purpose in achieving this? virtualenvs can be created with or without symlinks, ones with aren't going to be very useful unless the target system is set up the same as the originating one. (this varies by command, "virtualenv" takes a --always-copy argument, venv takes --copies, etc.). but recreating them can be useful, and easy enough. To repopulate a virtualenv use a "requirements.txt" file (the name isn't magical, that's just the conventional name), at which point you can: python -m pip install -r requirements.txt Helpfully, you can generate that file very easily, inside the original virtualenv do: python -m pip freeze > requirements.txt I guess in some cases you might have to pay attention to versions of things... From anilduggirala at fastmail.fm Mon Jul 20 15:14:33 2020 From: anilduggirala at fastmail.fm (Anil Felipe Duggirala) Date: Mon, 20 Jul 2020 14:14:33 -0500 Subject: [Tutor] moving a virtual environment to a new machine In-Reply-To: <3d4c19c4-86f2-9376-a3f0-10fd0df063d3@wichmann.us> References: <3d4c19c4-86f2-9376-a3f0-10fd0df063d3@wichmann.us> Message-ID: <5106dd1d-ee50-4d82-beed-cc589ccabc34@fastmail.fm> > virtualenvs can be created with or without symlinks, ones with aren't > going to be very useful unless the target system is set up the same as > the originating one. (this varies by command, "virtualenv" takes a > --always-copy argument, venv takes --copies, etc.). So this should work to simply copy the virtual environment on the new machine right? I would like to ask what most people do. For example, I am using the Pycharm IDE. By default, when you create a new project, it will create a venv in the main project folder. My question is, what is the best way to just be able to copy the whole project to a new machine in that case? thank you From manpritsinghece at gmail.com Tue Jul 21 12:37:51 2020 From: manpritsinghece at gmail.com (Manprit Singh) Date: Tue, 21 Jul 2020 22:07:51 +0530 Subject: [Tutor] If - else Vs OR Message-ID: Dear all, Consider a problem in which there is a list a, which can contain even as well as odd numbers. For Example a = [2, 3, 4, 6, 8] Now i have to filter out all even numbers from this list, and make a new list a1 that contains all odd numbers . In case, if the list a does not contains any odd number, then the resultant will be an empty list , this resultant list can be made by : a1 = [ i for i in a if i%2 != 0] So, a1 will be empty if list a does not contain any odd number , and if list a has one or more odd numbers then a1 will contain all those odd numbers . i have to write a program which will print "Empty List" if a1 is empty and print list a1 if a1 is not empty : i am writing 2 solutions to this problem . My question is which one method should be preferred sol 1) : a = [2, 3, 4, 6, 8] a1 = [i for i in a if i%2 != 0] if a1: print(a1) else: print("Empty list") sol 2) : a = [2, 3, 4, 6, 8] a1 = [i for i in a if i%2 != 0] print(a1 or "Empty list") From __peter__ at web.de Tue Jul 21 15:03:33 2020 From: __peter__ at web.de (Peter Otten) Date: Tue, 21 Jul 2020 21:03:33 +0200 Subject: [Tutor] If - else Vs OR References: Message-ID: Manprit Singh wrote: > Dear all, > Consider a problem in which there is a list a, which can contain even as > well as odd numbers. For Example a = [2, 3, 4, 6, 8] > Now i have to filter out all even numbers from this list, and make a new > list a1 that contains all odd numbers . In case, if the list a does not > contains any odd number, then the resultant will be an empty list , this > resultant list can be made by : > a1 = [ i for i in a if i%2 != 0] > So, a1 will be empty if list a does not contain any odd number , and if > list a has one or more odd numbers then a1 will contain all those odd > numbers . > i have to write a program which will print "Empty List" if a1 is empty and > print list a1 if a1 is not empty : > i am writing 2 solutions to this problem . My question is which one > method should be preferred > sol 1) : > > a = [2, 3, 4, 6, 8] > a1 = [i for i in a if i%2 != 0] > if a1: > print(a1) > else: > print("Empty list") > > sol 2) : > > a = [2, 3, 4, 6, 8] > a1 = [i for i in a if i%2 != 0] > print(a1 or "Empty list") I think they are both fine, as is (3) print(a1 if a1 else "Empty list") Another idea: you might try and simplify the if part of the list comprehension. From PyTutor at DancesWithMice.info Tue Jul 21 16:26:45 2020 From: PyTutor at DancesWithMice.info (dn) Date: Wed, 22 Jul 2020 08:26:45 +1200 Subject: [Tutor] If - else Vs OR In-Reply-To: References: Message-ID: <718f96ea-f1f8-2442-d5ad-ab4135f81847@DancesWithMice.info> On 7/22/20 7:03 AM, Peter Otten wrote: > Manprit Singh wrote: > >> Dear all, >> Consider a problem in which there is a list a, which can contain even as >> well as odd numbers. For Example a = [2, 3, 4, 6, 8] >> Now i have to filter out all even numbers from this list, and make a new >> list a1 that contains all odd numbers . In case, if the list a does not >> contains any odd number, then the resultant will be an empty list , this >> resultant list can be made by : >> a1 = [ i for i in a if i%2 != 0] >> So, a1 will be empty if list a does not contain any odd number , and if >> list a has one or more odd numbers then a1 will contain all those odd >> numbers . >> i have to write a program which will print "Empty List" if a1 is empty and >> print list a1 if a1 is not empty : >> i am writing 2 solutions to this problem . My question is which one >> method should be preferred >> sol 1) : >> >> a = [2, 3, 4, 6, 8] >> a1 = [i for i in a if i%2 != 0] >> if a1: >> print(a1) >> else: >> print("Empty list") >> >> sol 2) : >> >> a = [2, 3, 4, 6, 8] >> a1 = [i for i in a if i%2 != 0] >> print(a1 or "Empty list") > > I think they are both fine, as is > > (3) > > print(a1 if a1 else "Empty list") > > Another idea: you might try and simplify the if part of the list > comprehension. Agreed, I was amused at the taking-advantage of 'truthiness' in the print() but not in the list-comprehension! Further, some suggest that a well-chosen name, for the string-constant in this case, will not only assist in reading-comprehension but also ease maintenance (eg should the users decide to use a less technical explanation than "Empty list" -> "No data available"). Of the original options, doesn't the explicit if...print() seem more readable? Is @Peter's contribution more readable than either/both? If the reader is likely to be more 'Python apprentice' than 'master', then recommend avoiding either/both 'short-cuts'. OTOH, if decide for concise-and-powerful, then may as well go for both/broke! -- Regards =dn From alan.gauld at yahoo.co.uk Tue Jul 21 19:49:19 2020 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 22 Jul 2020 00:49:19 +0100 Subject: [Tutor] If - else Vs OR In-Reply-To: References: Message-ID: On 21/07/2020 17:37, Manprit Singh wrote: > i am writing 2 solutions to this problem . My question is which one method > should be preferred > sol 1) : > > a = [2, 3, 4, 6, 8] > a1 = [i for i in a if i%2 != 0] > if a1: > print(a1) > else: > print("Empty list") > > sol 2) : > > a = [2, 3, 4, 6, 8] > a1 = [i for i in a if i%2 != 0] > print(a1 or "Empty list") For ease of maintenance you should always aim to maximize the clarity of your intent. In the first case its easy to see that its an if/else situation. In the second case you are relying on short circuit evaluation of a boolean expression. Somebody has to decode it. Reading it as written it sounds like you don't care which value gets printed, (ie print either of them it doesn't matter) but in fact you you do, you just rely on the short circuit evaluation of booleans to choose. So my vote is option 1 every time. It's much clearer what the code is trying to do. If for some reason (and it would be a very unusual reason) you need to do it in a single line you could use the conditional expression: print(a1 if a1 else "Empty list") That retains the expression of intent even though it is still more cumbersome for the reader to parse. But its definitely better than the "clever code" boolean 'or' based solution. -- 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 jf_byrnes at comcast.net Wed Jul 22 17:39:43 2020 From: jf_byrnes at comcast.net (Jim) Date: Wed, 22 Jul 2020 16:39:43 -0500 Subject: [Tutor] Debug help In-Reply-To: <20200719235746.GA27668@cskk.homeip.net> References: <20200719235746.GA27668@cskk.homeip.net> Message-ID: On 7/19/20 6:57 PM, Cameron Simpson wrote: > On 19Jul2020 15:25, jim wrote: > [...] >> Here is the part of the code that generates the error, the remainder of >> the script just manipulates the spreadsheet. >> >> for stock in stocks: >> # ~ count = 0 >> # ~ while count < total_num_of_stocks: >> # ~ while count < 33: >> while True: >> # ~ count = count + 1 >> try: >> # ~ print('Day = ' + day) >> ticker = yf.Ticker(stock) >> # ~ print('Ticker = ' + ticker) >> hist = ticker.history(start=day) >> # ~ print('History = ' + hist) >> close_price = hist.loc[day]['Close'] >> # ~ print('Close = ' + close_price) >> closing_prices.append(close_price) >> >> except: >> print ('yfinance JSONDecodeError DAILY, retyring') >> # ~ print ('ticker: ' + hist) >> continue >> break >> >> # Get stock values by multiplying the list elements by number of shares >> # using zip() >> value = [num_shares * stk for num_shares, stk in zip(shares, >> closing_prices)] >> >> # Put date in cell then go down one row >> S(cell_loc).string = day >> S().dispatch('GoDown') >> >> # Fill column for MY100000 with stock values using the number of >> stocks in MY100000 >> for stk in value[:total_num_MY100000]: >> S().value = stk # Line 102 >> S().dispatch('GoDown') >> >> If I run the script with the print lines outside the except commented >> out it will print the date in the spreadsheet, go down 1 cell and >> throw the error and end. If I uncomment any of the print lines it >> seems to go into a endless loop printing out the exception error msg. >> >> I wonder if anyone has an idea of how to figure out what Yahoo Finance >> is doing to cause a problem and I am curious as to why uncommenting a >> print seems to cause an endless loop. > > I'm unfamiliar with Yahoo Finance, but I want to comment on your > debugging issues. > > Your code's control structure is like this: > > while True: > try: > [...] > # ~ print('Close = ' + close_price) > [...] > except: > print ('yfinance JSONDecodeError DAILY, retyring') > # ~ print ('ticker: ' + hist) > continue > > First up, consider what happens when anything goes wrong inside the > try/except: you print something and continue, restarting the loop. If > that thing happens on every pass, you get an infinite loop. > > See the commented out print(). Supposing close_price is not a string. > The addition will raise a TypeError exception. And into the infinite > loop you go. > > Better, for debugging statements, to perform as few operations as > possible. because if something is wrong, any operation might itself fail > because things as not as you expect. So consider: > > print('Close = ', close_price) > > Because print() converts all is arguments to strings, the form above > does no addition and still produces a result. > > However, the biggest problem here is what we call a "bare except": an > except which catches _any_ exception. That's handy for reporting on > _everything_ which explodes, but otherwise a bad practice. > > The rule of thumb for catching excpetions is to _only_ catch what you > expect and know how to handle _correctly_. Your code catches everything > (again, great for reporting) but does not know what to do with it. > > Because you do not print the exception or where it came from, you learn > nothing about the underlying issue. Consider this: > > while True: > try: > [...] > # ~ print('Close = ' + close_price) > [...] > except Exception as e: > print("unexpection exception:", e) > print ('yfinance JSONDecodeError DAILY, retyring') > # ~ print ('ticker: ' + hist) > continue > > At least this will show you what is wrong. But better still is not to > catch it at all. It is unexpected, so your print-and-continue action is > probably inappropriate (because you do not know the context - _anything_ > might be wrong). As a scarey example, consider: > > while True: > try: > x = closed_price > [...] > # ~ print('Close = ' + close_price) > [...] > except Exception as e: > print("unexpection exception:", e) > print ('yfinance JSONDecodeError DAILY, retyring') > # ~ print ('ticker: ' + hist) > continue > > Here, "closed_price" is a misspelled variable name. Python will raise a > NameError because it is an unknown name, and you won't have even printed > out the exception, so you will be blind to a simple typo in the code. > > The usual pattern with try/except is to catch exactly what you can > handle, and let other exceptions out. That does abort your programme, > but you get the raw exception _and_ a nice stacktrace showing where it > came from: > > while True: > try: > x = closed_price > [...] > # ~ print('Close = ' + close_price) > [...] > except HTTPError as e: > print("HTTP networking error", e) > time.sleep(1) > continue > > Here we catch an expected failure (the network maybe went away, and the > HTTP conencion did not work). We print that, and we sleep for 1 second > to avoid a busy loop (spinning until the network works again). But this > is an _expected_ exception with a well defined plan for accomodating it. > So we catch it, report it, and continue. > > Now, you may only know how to handle a specific type of an exception. > Then the pattern is only slightly more elaborate: > > while True: > try: > x = closed_price > [...] > # ~ print('Close = ' + close_price) > [...] > except HTTPError as e: > # I'm just making up some kind of test on the exception here > if e.errno == errno.EIO: > print("HTTP networking error", e) > time.sleep(1) > continue > raise > > Here, we know how to handle a specific type of HTTPError. Any others are > just reraised, producing an error and a stack trace for you to examine. > Cameron, I am finally able to get back to this discussion. Thanks for the great explanation of try/except. I was always confused when people talked about only catch what you expect and can handle. I googled python exceptions and found a list of them. It makes more sense now. The error that promoted my question was: uno.RuntimeException: Couldn't convert Date 2020-07-17 134858.5 2020-07-17 134858.5 Name: Close, dtype: float64 to a UNO type; caught exception: : 'Series' object has no attribute 'getTypes', traceback follows This comes from the module ooSheet, because it was given data in a form it did not expect. Can try/except be used to catch exemptions from an imported module? In this case I would want the program to stop, but I imagine there could be a case where I would want it to continue. Thanks, Jim From alan.gauld at yahoo.co.uk Wed Jul 22 18:42:44 2020 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 22 Jul 2020 23:42:44 +0100 Subject: [Tutor] If - else Vs OR In-Reply-To: References: Message-ID: Please use reply-ALL or Reply0List when responding to the group. CCing my reply. On 22/07/2020 19:00, Nick Kartha wrote: > Since when could we do if else /inside/?the print() > This is new to me, and have not seen this in another language. print takes a string argument(as well as some others) That argument can be a literal string or any python expression that evaluates to a string. The expression if else can be used anywhere that a value is required, including inside a print. C/C++ has a similar construct: ? : And a couple of others have borrowed that. Python took it and made it more readable. > I assume the condition is checked before the print Yes it is an expression that is evaluated and the result printed. Think about these examples of similar things: print(5+3) # -> print(8) -> print"8") myObject.name = "Alan" print(myObject.name) # -> print("Alan") x = 42 print("The answer to ... everything is {}".format(x)) -> print("The answer to ... everything is 42") These are all expressions that get evaluated to a string before printing. The conditional expression is no different. -- 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 Wed Jul 22 18:53:57 2020 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 22 Jul 2020 23:53:57 +0100 Subject: [Tutor] If - else Vs OR In-Reply-To: References: Message-ID: On 22/07/2020 23:42, Alan Gauld via Tutor wrote: > C/C++ has a similar construct: > > ? : Oops, I got that wrong. Its ? : But the end result is the same. It's an expression that returns a single value depending on the result of a test. Sorry for any confusion caused. Also it's worth pointing out that in fact the "values" returned can themselves be expressions (in both the C or Python versions) But that's usually a bad idea since it can result in pretty convoluted code! -- 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 cs at cskk.id.au Wed Jul 22 19:49:40 2020 From: cs at cskk.id.au (Cameron Simpson) Date: Thu, 23 Jul 2020 09:49:40 +1000 Subject: [Tutor] Debug help In-Reply-To: References: Message-ID: <20200722234940.GA33475@cskk.homeip.net> On 22Jul2020 16:39, jim wrote: >On 7/19/20 6:57 PM, Cameron Simpson wrote: >>On 19Jul2020 15:25, jim wrote: >>[...] >>>Here is the part of the code that generates the error, the remainder of >>>the script just manipulates the spreadsheet. >>> >>> for stock in stocks: >>> # ~ count = 0 >>> # ~ while count < total_num_of_stocks: >>> # ~ while count < 33: >>> while True: >>> # ~ count = count + 1 >>> try: >>> # ~ print('Day = ' + day) >>> ticker = yf.Ticker(stock) >>> # ~ print('Ticker = ' + ticker) [...] > >I am finally able to get back to this discussion. Thanks for the great >explanation of try/except. I was always confused when people talked >about only catch what you expect and can handle. I googled python >exceptions and found a list of them. It makes more sense now. A random google found reference might not be ideal (not to mention that programmes can invent their own). Consider these links: The Builtin Exceptions https://docs.python.org/3/library/exceptions.html Exceptions in the Python execution model https://docs.python.org/3/reference/executionmodel.html#exceptions Errors and Exceptions from the Python Tutorial https://docs.python.org/3/tutorial/errors.html >The error that promoted my question was: > >uno.RuntimeException: Couldn't convert Date >2020-07-17 134858.5 >2020-07-17 134858.5 >Name: Close, dtype: float64 to a UNO type; caught exception: 'AttributeError'>: 'Series' object has no attribute 'getTypes', >traceback follows > >This comes from the module ooSheet, because it was given data in a >form it did not expect. Can try/except be used to catch exemptions >from an imported module? In this case I would want the program to >stop, but I imagine there could be a case where I would want it to >continue. Not easily. Sometimes not at all. This is because the place where something could most usefully be done is buried deep in the code. By the time the exception reaches you too much follow on logic has been skipped, and you won't have whatever data should have been returned. If you're getting a series of data from a generator, the generator will have exiting with the exception and all the following data will be missing, and so forth. You can of course catch the exception and continue your own local control structure eg the next loop iteration. Your choices depend on how the exception occurred and the internal structure of the module you're using. Have a look at the stack trace - it will identify where the exception was raised. This includes the soruce code references! Options include: Call something lower level. Suppose you code is calling a function which returns spreadsheet like data, some table of values from the stock data. Likely that function itself calls lower level things to process the data and construct the array. You could do that work yourself, and call some per-row function, and catch the exception on a row-by-row basis, skipping (and reporting in detail) the bad row. Pass in a special parser. Some libraries have a degree of extensibility in mind, and have a hook for a custom parser, i.e. a functioin to convert the raw data into whatever data type you intend to get. If there's such a hook, use it. Initially you can find out the default parser function, and write a small function which: - calls the default with a try/except - catches exceptions and reports the exception and the dfata which raised it, and return maybe None For example: # you'd need to find this, if it exists default_parser = module.something def my_parser(row): try: return default_parser(row) except Exception as e: print("default_parser fails on row %r with exception %s" % (row, e)) return None data = ticker.fetch(parser=my_parser) Obviously this requires the module to offer this facility. Monkey patch! Modify the modul in place. Monkey patching is where you alter a module after it is loaded. In your case, look at the module source code and find the low level function which raised the exception. This is available in the stack trace! Let's suppose the low level thing is module.parse_datum(), to invent a name. You can do the custom parser with a monkey patch: import yf # you'd need to find this, if it exists original_parser = yf.parse_datum def my_parse_datum(datum): try: return original_parse_datum(datum) except Exception as e: print("yf.parse_datum fails on %r with exception %s" % (datum, e)) # you could parse datum specially here, if you know what to # handle return None # monkey patch here: yf.parse_datum = my_parse_datum ... yf.Ticker(...) ... Now your special function is in the module where the original parser was, and _uses_ the original parse unless it fails. If nothing else, this provides the data causing the trouble. And you can then write your own code to handle just that data when it shows up! Cheers, Cameron Simpson From hongerlapjes at gmail.com Wed Jul 22 19:33:42 2020 From: hongerlapjes at gmail.com (hongerlapjes) Date: Thu, 23 Jul 2020 01:33:42 +0200 Subject: [Tutor] TypeError: 'float' object is not iterable Message-ID: I'm learning python and need help. How can I sum up "bedrag"? I've replaced the "." with a "," and changed the string into a float, but I can't iter. I'm lost. What do I need to do? I've tried numpy For content csv see: My python code: ??? import csv ? ? import numpy as np ??? with open('oefen.csv') as csv_file: ??? csv_reader = csv.DictReader(csv_file, delimiter=',') ??? print(csv_reader.fieldnames) ??? for row in csv_reader: ??????? #change string into float and replace the comma with a dot ??????? bedrag = float(row['Bedrag (EUR)'].replace(",", ".")) ??? ??? ??? #try 1 gives: TypeError: 'float' object is not iterable ?????? ? ?? som = sum(bedrag) ??????????? print(som) ??????????? #try 2 gives: TypeError: iteration over a 0-d array ??? ??? ??? arr = np.array(float(row['Bedrag (EUR)'].replace(",", "."))) ??? ??? ??? som = sum(arr) ??? ??? ??? print(som) ??????????? #try 3 gives: TypeError: 'float' object is not iterable ??? ??? ??? arr = np.array(list(float(row['Bedrag (EUR)'].replace(",", ".")))) ??????????? som = sum(arr) ??????????? print(som) From alan.gauld at yahoo.co.uk Thu Jul 23 03:11:01 2020 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 23 Jul 2020 08:11:01 +0100 Subject: [Tutor] TypeError: 'float' object is not iterable In-Reply-To: References: Message-ID: On 23/07/2020 00:33, hongerlapjes wrote: > I'm learning python and need help. How can I sum up "bedrag"? > I've replaced the "." with a "," and changed the string into a float, > but I can't iter. I'm lost. What do I need to do? Your question does not make much sense. You have made bedrag a float. That is a single number. How do you sum a single number? Lets look at what you are doing... > ??????? #change string into float and replace the comma with a dot > ??????? bedrag = float(row['Bedrag (EUR)'].replace(",", ".")) Is like saying bedrag = 1.234 > ??? ??? ??? #try 1 gives: TypeError: 'float' object is not iterable > ?????? ? ?? som = sum(bedrag) som = sum(1.234) Does that make sense? > ??????????? #try 2 gives: TypeError: iteration over a 0-d array > ??? ??? ??? arr = np.array(float(row['Bedrag (EUR)'].replace(",", "."))) arr = np.array(1.234) How would that work? What kind of array do you expect? > ??????????? #try 3 gives: TypeError: 'float' object is not iterable > ??? ??? ??? arr = np.array(list(float(row['Bedrag (EUR)'].replace(",", > ".")))) arr = np.array(list(1.234)) What kind of list does list(1.234) create? sum() needs a sequence but you have a single value. -- 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 __peter__ at web.de Thu Jul 23 03:57:28 2020 From: __peter__ at web.de (Peter Otten) Date: Thu, 23 Jul 2020 09:57:28 +0200 Subject: [Tutor] TypeError: 'float' object is not iterable References: Message-ID: hongerlapjes wrote: > I'm learning python and need help. How can I sum up "bedrag"? > I've replaced the "." with a "," and changed the string into a float, > but I can't iter. I'm lost. What do I need to do? The iteration is done by your for loop. You need another variable that you initialize before it: totaalbedrag = 0.0 for row in csv_reader: bedrag = float(row['Bedrag (EUR)'].replace(",", ".")) totaalbedrag = totaalbedrag + bedrag print(totaalbedrag) A more concise way to spell that is totallbedrag = sum( float(row['Bedrag (EUR)'].replace(",", ".")) for row in csv_reader ) print(totaalbedrag) Here the loop is moved into the "generator expression" that is used as the argument for the sum function. If you are doing that or simalar operations a lot you should look at pandas (built on top of numpy) which gives you highlevel operations to do the same: >>> import pandas as pd >>> df = pd.read_csv("oefen.csv") >>> df Bedrag 0 1.23 1 2.34 [2 rows x 1 columns] >>> df["Bedrag"].sum() 3.5699999999999998 From gamezgodfred at gmail.com Thu Jul 23 10:43:36 2020 From: gamezgodfred at gmail.com (gameli godfred) Date: Thu, 23 Jul 2020 14:43:36 +0000 Subject: [Tutor] Error from importing numpy and pandas Message-ID: *Hello, I get this error when ever I try to import numpy and pandas. Please can I get assistance ob how to resolve this? I have tried different methods but to no avail. * *Thank you.* OSError Traceback (most recent call last) in ----> 1 import numpy as np 2 import pandas as pd ~\AppData\Roaming\Python\Python37\site-packages\numpy\__init__.py in 138 139 # Allow distributors to run custom init code--> 140 from . import _distributor_init 141 142 from . import core ~\AppData\Roaming\Python\Python37\site-packages\numpy\_distributor_init.py in 24 # NOTE: would it change behavior to load ALL 25 # DLLs at this path vs. the name restriction?---> 26 WinDLL(os.path.abspath(filename)) 27 DLL_filenames.append(filename) 28 if len(DLL_filenames) > 1: ~\anaconda3\lib\ctypes\__init__.py in __init__(self, name, mode, handle, use_errno, use_last_error) 362 363 if handle is None:--> 364 self._handle = _dlopen(self._name, mode) 365 else: 366 self._handle = handle OSError: [WinError 193] %1 is not a valid Win32 application Virus-free. www.avast.com <#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2> From alan.gauld at yahoo.co.uk Fri Jul 24 05:19:17 2020 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 24 Jul 2020 10:19:17 +0100 Subject: [Tutor] Error from importing numpy and pandas In-Reply-To: References: Message-ID: On 23/07/2020 15:43, gameli godfred wrote: > *Hello, I get this error when ever I try to import numpy and pandas. > Please can I get assistance ob how to resolve this? I have tried > different methods but to no avail. * > It would help to try and isolate the problem. What happens if you go to the Python prompt and import numpy alone? Do you get the same error? If that works try importing pandas alone. Do you get the same error? Or is it only in combination? Given that this is unlikely to be your code at fault it is probably an installation issue. Are you sure your numpy/pandas matches your python version? How did you install it? Are you using a combined distribution like Anaconda? Or did you install numpy/pandas on top of a vanilla Python? -- 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 laura.marchesini at gmail.com Sat Jul 25 13:51:00 2020 From: laura.marchesini at gmail.com (laura marchesini) Date: Sat, 25 Jul 2020 19:51:00 +0200 Subject: [Tutor] Request of Help about pymatch Message-ID: Good Morning to everyone, I am Laura, a student at University in Economics and Finance. I am writing my Master thesis using Python and I am facing some problems with pymatch.. I would be very grateful if You could give me a hint to solve this particular issue. I am performing Propensity Score Matching with pymatch, in particular matching companies of two different groups according to three variables. One of these (Sales) is wrongly recognized by Python as many dummies as many values of the variable itself. The other 2 variables are correctly recognized as continuous variables. When I call m.X I see thousands of sales variables, instead of one single column with values for each company. I used the same code before for another group of data, and it worked well, so I am not understanding what is not wrong here.... Could You please help me?? That would mean much for me, Thank You for the attention, Laura From alan.gauld at yahoo.co.uk Sat Jul 25 19:07:07 2020 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 26 Jul 2020 00:07:07 +0100 Subject: [Tutor] Request of Help about pymatch In-Reply-To: References: Message-ID: On 25/07/2020 18:51, laura marchesini wrote: > my Master thesis using Python and I am facing some problems with pymatch.. > I would be very grateful if You could give me a hint to solve this > particular issue. We can try but I must start with the caveat hat this list is for folks learning Python language and its standard library. For questions about 3rd party libraries - like pymatch - you are usually better off asking on their support fora or by contacting the author. I note that pymatch has a web site and the author is on Facebook, so you might be able to contact him in that way. That having been said, we can try to help. > One of these (Sales) is wrongly recognized by Python as many dummies as > many values of the variable itself. The other 2 variables are correctly > recognized as continuous variables. When I call m.X I see thousands of > sales variables, instead of one single column with values for each company. Some code might help? And any error messages would definitely help, although it sounds like that's not really the issue here. Alternatively some input/output data samples would be helpful - the minimal amount needed to show the issue. Bear in mind that we are not experts in your domain. Our expertise is in programming with python. so you need to explain your data and what should/should not be matched and why that is so. But without code/data we can only make wild stabs at what might be wrong. > I used the same code before for another group of data, and it worked well, > so I am not understanding what is not wrong here.... Maybe sharing that would help too? -- 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 npigossi at gmail.com Sun Jul 26 10:22:48 2020 From: npigossi at gmail.com (Nelson Pigossi Jr) Date: Sun, 26 Jul 2020 11:22:48 -0300 Subject: [Tutor] Request of Help about pymatch In-Reply-To: References: Message-ID: <9775685C-8340-4948-ACDC-4784CDD6F39D@gmail.com> Laura, can you send a sample of your code and some test data? _________________ Nelson Pigossi Jr npigossi at gmail.com npigossi at mac.com > On 25 Jul 2020, at 14:51, laura marchesini wrote: > > Good Morning to everyone, > > I am Laura, a student at University in Economics and Finance. I am writing > my Master thesis using Python and I am facing some problems with pymatch.. > I would be very grateful if You could give me a hint to solve this > particular issue. > > I am performing Propensity Score Matching with pymatch, in particular > matching companies of two different groups according to three variables. > One of these (Sales) is wrongly recognized by Python as many dummies as > many values of the variable itself. The other 2 variables are correctly > recognized as continuous variables. When I call m.X I see thousands of > sales variables, instead of one single column with values for each company. > > I used the same code before for another group of data, and it worked well, > so I am not understanding what is not wrong here.... > > Could You please help me?? That would mean much for me, > Thank You for the attention, > Laura > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From jf_byrnes at comcast.net Sun Jul 26 17:02:19 2020 From: jf_byrnes at comcast.net (Jim) Date: Sun, 26 Jul 2020 16:02:19 -0500 Subject: [Tutor] Debug help In-Reply-To: <20200722234940.GA33475@cskk.homeip.net> References: <20200722234940.GA33475@cskk.homeip.net> Message-ID: On 7/22/20 6:49 PM, Cameron Simpson wrote: >> >> I am finally able to get back to this discussion. Thanks for the great >> explanation of try/except. I was always confused when people talked >> about only catch what you expect and can handle. I googled python >> exceptions and found a list of them. It makes more sense now. > > A random google found reference might not be ideal (not to mention that > programmes can invent their own). Consider these links: > > The Builtin Exceptions > https://docs.python.org/3/library/exceptions.html > > Exceptions in the Python execution model > https://docs.python.org/3/reference/executionmodel.html#exceptions > > Errors and Exceptions from the Python Tutorial > https://docs.python.org/3/tutorial/errors.html Thanks for the links. I ended up reading through the explanation from the docs for Python 3.8 >> The error that promoted my question was: >> >> uno.RuntimeException: Couldn't convert Date >> 2020-07-17 134858.5 >> 2020-07-17 134858.5 >> Name: Close, dtype: float64 to a UNO type; caught exception: > 'AttributeError'>: 'Series' object has no attribute 'getTypes', >> traceback follows >> >> This comes from the module ooSheet, because it was given data in a >> form it did not expect. Can try/except be used to catch exemptions >>from an imported module? In this case I would want the program to >> stop, but I imagine there could be a case where I would want it to >> continue. > > Not easily. Sometimes not at all. This is because the place where > something could most usefully be done is buried deep in the code. By the > time the exception reaches you too much follow on logic has been > skipped, and you won't have whatever data should have been returned. If > you're getting a series of data from a generator, the generator will > have exiting with the exception and all the following data will be > missing, and so forth. > > You can of course catch the exception and continue your own local > control structure eg the next loop iteration. > > Your choices depend on how the exception occurred and the internal > structure of the module you're using. > > Have a look at the stack trace - it will identify where the exception > was raised. This includes the soruce code references! > > Options include: > > Call something lower level. Suppose you code is calling a function which > returns spreadsheet like data, some table of values from the stock data. > Likely that function itself calls lower level things to process the data > and construct the array. You could do that work yourself, and call some > per-row function, and catch the exception on a row-by-row basis, > skipping (and reporting in detail) the bad row. > > Pass in a special parser. Some libraries have a degree of extensibility > in mind, and have a hook for a custom parser, i.e. a functioin to > convert the raw data into whatever data type you intend to get. If > there's such a hook, use it. Initially you can find out the default > parser function, and write a small function which: > - calls the default with a try/except > - catches exceptions and reports the exception and the dfata which > raised it, and return maybe None > > For example: > > # you'd need to find this, if it exists > default_parser = module.something > > def my_parser(row): > try: > return default_parser(row) > except Exception as e: > print("default_parser fails on row %r with exception %s" % (row, e)) > return None > > data = ticker.fetch(parser=my_parser) > > Obviously this requires the module to offer this facility. > > Monkey patch! Modify the modul in place. > > Monkey patching is where you alter a module after it is loaded. In your > case, look at the module source code and find the low level function > which raised the exception. This is available in the stack trace! > > Let's suppose the low level thing is module.parse_datum(), to invent a > name. You can do the custom parser with a monkey patch: > > import yf > > # you'd need to find this, if it exists > original_parser = yf.parse_datum > > def my_parse_datum(datum): > try: > return original_parse_datum(datum) > except Exception as e: > print("yf.parse_datum fails on %r with exception %s" % (datum, e)) > # you could parse datum specially here, if you know what to > # handle > return None > > # monkey patch here: > yf.parse_datum = my_parse_datum > > ... > yf.Ticker(...) > ... > > Now your special function is in the module where the original parser > was, and _uses_ the original parse unless it fails. > > If nothing else, this provides the data causing the trouble. And you can > then write your own code to handle just that data when it shows up! > Thanks for the explanation. Regards, Jim From sophialore07 at gmail.com Sun Jul 26 18:15:21 2020 From: sophialore07 at gmail.com (sophia lore) Date: Sun, 26 Jul 2020 15:15:21 -0700 Subject: [Tutor] (no subject) Message-ID: Create a program that iterates using a *for loop* over all values of list_mod. The program must check each value and append the string "True" to the list result if the value is even or append the string "False" to the list result otherwise. In [ ]: ### GRADED list_mod = list(range(1,15)) result = [] I have been working on this question for class for two days. I have tried numerous combinations of if/else statements in the for loop to achieve the outcome requested. All of the code I have tried returns errors. I can't seem to grasp how to set this up. Any help would be appreciated. Thanks From alan.gauld at yahoo.co.uk Sun Jul 26 20:10:12 2020 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 27 Jul 2020 01:10:12 +0100 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: On 26/07/2020 23:15, sophia lore wrote: > Create a program that iterates using a *for loop* over all values of > list_mod. The program must check each value and append the string "True" to > the list result if the value is even or append the string "False" to the > list result otherwise. > In [ ]: > > ### GRADED > > list_mod = list(range(1,15)) > result = [] There are several ways to do this depending on your level of knowledge. But given you are struggling I'll assume we have to stick to the basics. All you need is a for loop and a single if/else. > I have been working on this question for class for two days. I have tried > numerous combinations of if/else statements in the for loop to achieve the > outcome requested. All of the code I have tried returns errors. I can't > seem to grasp how to set this up. Any help would be appreciated. It will help us enormously if you post your code and the full error message since the errors contain a lot of useful information (once you learn how to decipher them!) But without code we would just be guessing. And we won't do your homework for you, we can only give hints and direction. -- 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 Sun Jul 26 20:14:44 2020 From: PyTutor at danceswithmice.info (dn) Date: Mon, 27 Jul 2020 12:14:44 +1200 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: <41c76df2-355f-7819-d447-2c68fa3abd84@DancesWithMice.info> On 27/07/2020 10:15, sophia lore wrote: > Create a program that iterates using a *for loop* over all values of > list_mod. The program must check each value and append the string "True" to > the list result if the value is even or append the string "False" to the > list result otherwise. > > ### GRADED > > list_mod = list(range(1,15)) > result = [] > > I have been working on this question for class for two days. I have tried > numerous combinations of if/else statements in the for loop to achieve the > outcome requested. All of the code I have tried returns errors. I can't > seem to grasp how to set this up. Any help would be appreciated. Sorry to hear that the problem is frustrating you. What numerous combinations have you tried? (please copy-paste the code, and any error-messages into your reply email msg) We are prepared to help you learn - but not to do your homework/win the grade for you! What should be the for-loop that will iterate over list_mod? How can one decide if a value is odd or even? (what is the definition of "even") Computing "4 / 2" or "4 * 0.5" is easy, isn't it? What about "5 / 2" (or...)? What if those two results are expressed as floating-point numbers, and then again but as integers? If you can answer those questions, the solution will become more clear... -- Regards =dn From __peter__ at web.de Mon Jul 27 04:05:35 2020 From: __peter__ at web.de (Peter Otten) Date: Mon, 27 Jul 2020 10:05:35 +0200 Subject: [Tutor] (no subject) References: Message-ID: sophia lore wrote: > Create a program that iterates using a *for loop* over all values of > list_mod. The program must check each value and append the string "True" > to the list result if the value is even or append the string "False" to > the list result otherwise. > In [ ]: > > ### GRADED > > list_mod = list(range(1,15)) > > result = [] > > > > > I have been working on this question for class for two days. I have tried > numerous combinations of if/else statements in the for loop to achieve the > outcome requested. All of the code I have tried returns errors. I can't > seem to grasp how to set this up. Any help would be appreciated. If you have a "big" problem that you cannot solve directly it often helps to break it into "small" problems that you find easier and solve these "small" problems. Then combine the solutions for the "small" problems to solve the "big" one. Think of the question as two problems -- can you write a script that iterates over list_mod and appends the string "True" to result for every item in list_mod? Write that down. Then write a script that takes an int value = int(input("Enter an int value: ")) if ...: # replace the dots with an expression that checks if value is odd print("Value is odd") else: print("Value is even") Once you have both scripts working and understand what they do you can try and combine them into a solution for your original problem. Hints: - You can drop the line with input() - Where does the if...else check go (before, into, or after the loop)? - What has to be done for odd values instead of just printing? For even values? If you get an error message or a result you cannot explain come back here to ask. Provide the full traceback and the script that is causing it. From ptan1 at capellauniversity.edu Thu Jul 30 23:35:43 2020 From: ptan1 at capellauniversity.edu (Phaik Huen Tan) Date: Thu, 30 Jul 2020 22:35:43 -0500 Subject: [Tutor] Draw table and compare random input Message-ID: Hi there, I am supposed to draw a table for my random input (12 months of expenses) and compared it against the monthly target. I am able to get input from the user but I am not able to compare each input and display each input by month individually. Can you please provide guidance on where I did wrong on my code? Thank you! Phaik-Huen Below is my code: # This program calculates monthly expenses. # Create a variable to control the loop. keep_going = 'Y' #Calculate monthly target for each of the 12 months using annual IT expenses of $42500 monthly_target = 42500/12 print('Monthly target expenditure is $', format(monthly_target,'.2f')) while keep_going == 'Y' or keep_going == 'y': # Initialize an accumulator for site expenditure total = 0 # Get the site expenditure month_exp = int(input('Please enter month expenditure: ')) if month_exp < 0: print('Value must be greater than 0') month_exp = int(input('Enter the correct site expenditure: ')) # See if the user wants to do enter another one. keep_going = input('Do you want to enter another ' + 'month expenditure (Enter Y or y for yes): ') #Print the table headings print() print('Month\t|Expense\t|Over,Under or Exact') print('-----------------------------------------------') if month_exp >= monthly_target: print("Over") elif month_exp <= monthly_target: print("Under") else: print("Exact") From o1bigtenor at gmail.com Fri Jul 31 12:56:29 2020 From: o1bigtenor at gmail.com (o1bigtenor) Date: Fri, 31 Jul 2020 11:56:29 -0500 Subject: [Tutor] Assistance request when using python 'calendar' Message-ID: Greetings I regularly work in planning through multiple years at once. This means that I like to have a lot of stuff available in a calendar function. Python seems to be locked when I need to display more than 1 year at a time. I don't see a way to display something like 3 years worth of calendar starting at a point 23 months from now. (I see how to display 1 year at a time but not multiple years.) Is there a way to do this? TIA