From phillor9 at gmail.com Wed Feb 3 19:25:12 2021 From: phillor9 at gmail.com (Phil) Date: Thu, 4 Feb 2021 11:25:12 +1100 Subject: [Tutor] Is the use of -> usual in Python? Message-ID: <3dce66b9-ed9a-f935-d9db-3f716c36d7d1@gmail.com> |The following is a snippet from a class that I came across yesterday. I'm not familiar with the use of "->" in Python code. Is it usual practice? Nothing showed up during an Internet search. | ||def receive(self) -> str: line = self.serial.read_until(self.TERMINATOR) return line.decode('UTF8').strip() def send(self, text: str) -> bool: line = '%s\r\f' % text self.serial.write(line.encode('|UTF8'))|# the line should be echoed. # If it isn't, something is wrong. return text == self.receive() | |-- Regards, Phil From mats at wichmann.us Wed Feb 3 19:44:55 2021 From: mats at wichmann.us (Mats Wichmann) Date: Wed, 3 Feb 2021 17:44:55 -0700 Subject: [Tutor] Is the use of -> usual in Python? In-Reply-To: <3dce66b9-ed9a-f935-d9db-3f716c36d7d1@gmail.com> References: <3dce66b9-ed9a-f935-d9db-3f716c36d7d1@gmail.com> Message-ID: <281d3b99-8f95-6b6f-7735-660549098e33@wichmann.us> On 2/3/21 5:25 PM, Phil wrote: > |The following is a snippet from a class that I came across yesterday. > I'm not familiar with the use of "->" in Python code. Is it usual > practice? Nothing showed up during an Internet search. | > ||def receive(self) -> str: line = > self.serial.read_until(self.TERMINATOR) return > line.decode('UTF8').strip() def send(self, text: str) -> bool: line = > '%s\r\f' % text self.serial.write(line.encode('|UTF8'))|# the line > should be echoed. # If it isn't, something is wrong. return text == > self.receive() | |-- Regards, > Phil It's the function annotation syntax (actunally not just for functions). It's a runtime no-op i.e. the Python interpreter ignores it, but type-aware static checkers understand it. Search for something like "python type hints" to get more info. From mhysnm1964 at gmail.com Sat Feb 6 05:33:26 2021 From: mhysnm1964 at gmail.com (mhysnm1964 at gmail.com) Date: Sat, 6 Feb 2021 21:33:26 +1100 Subject: [Tutor] OS lib creating an directory using an absolute path. Message-ID: <000c01d6fc73$82dabc20$88903460$@gmail.com> All, I am using the os library. If I have a directory structure with any missing directory (folders), then the os.mkdir function fails as it cannot find the filenamefilename. For example: Current directory structure is: C:\books with nothing inside. The path I am wanting to create is: E:\books\a\anne rice Yes, I could break the path and check each level of the directory. But I was wondering if there was any other method to do this. As if you do this at Windows 10 command line: Mkdir E:\books\"a\anne rice" The 'a' directory plus 'anne rice' is also created. I hope this makes sense. Python 3.8 under windows 10. Sean From mats at wichmann.us Sat Feb 6 05:49:37 2021 From: mats at wichmann.us (Mats Wichmann) Date: Sat, 6 Feb 2021 03:49:37 -0700 Subject: [Tutor] OS lib creating an directory using an absolute path. In-Reply-To: <000c01d6fc73$82dabc20$88903460$@gmail.com> References: <000c01d6fc73$82dabc20$88903460$@gmail.com> Message-ID: <27965210-dd76-0ef7-8aff-2fab33783ec6@wichmann.us> On 2/6/21 3:33 AM, mhysnm1964 at gmail.com wrote: > All, > > > > I am using the os library. If I have a directory structure with any missing > directory (folders), then the os.mkdir function fails as it cannot find the > filenamefilename. ... > Yes, I could break the path and check each level of the directory. But I was > wondering if there was any other method to do this. As if you do this at > Windows 10 command line: > > Mkdir E:\books\"a\anne rice" > > The 'a' directory plus 'anne rice' is also created. use os.makedirs instead, or use the pathlib mkdir function with appropriate argument and would really suggest you not use directory names with spaces in them, it will eventually bring you grief. From nathan-tech at hotmail.com Sat Feb 6 05:51:58 2021 From: nathan-tech at hotmail.com (nathan tech) Date: Sat, 6 Feb 2021 10:51:58 +0000 Subject: [Tutor] OS lib creating an directory using an absolute path. In-Reply-To: <000c01d6fc73$82dabc20$88903460$@gmail.com> References: <000c01d6fc73$82dabc20$88903460$@gmail.com> Message-ID: Heya, myself I tend to break it down and use os.mkdir on each. A quick google however revealed this question from stackoverflow which I didn't know, and may be of particular use. https://stackoverflow.com/questions/600268/mkdir-p-functionality-in-python HTH! Nathan On 06/02/2021 10:33, mhysnm1964 at gmail.com wrote: > All, > > > > I am using the os library. If I have a directory structure with any missing > directory (folders), then the os.mkdir function fails as it cannot find the > filenamefilename. For example: > > > > Current directory structure is: > > > > C:\books with nothing inside. > > > > The path I am wanting to create is: > > > > E:\books\a\anne rice > > > > Yes, I could break the path and check each level of the directory. But I was > wondering if there was any other method to do this. As if you do this at > Windows 10 command line: > > > > Mkdir E:\books\"a\anne rice" > > > > The 'a' directory plus 'anne rice' is also created. > > > > I hope this makes sense. Python 3.8 under windows 10. > > > > Sean > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://emea01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fmail.python.org%2Fmailman%2Flistinfo%2Ftutor&data=04%7C01%7C%7C889add95823f43b4533008d8ca8ad53f%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637482044923551659%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=rPTuEiR6BEHkSsajTb25n0Yu8PLP0JA05UJNkrFzj7w%3D&reserved=0 From ming at pgp.cool Sat Feb 6 05:50:33 2021 From: ming at pgp.cool (Ming) Date: Sat, 6 Feb 2021 18:50:33 +0800 Subject: [Tutor] OS lib creating an directory using an absolute path. In-Reply-To: <000c01d6fc73$82dabc20$88903460$@gmail.com> References: <000c01d6fc73$82dabc20$88903460$@gmail.com> Message-ID: <20210206105029.GA15@pgp.cool> On Sat, Feb 06, 2021 at 09:33:26PM +1100, mhysnm1964 at gmail.com wrote: > All, > > I am using the os library. If I have a directory structure with any missing > directory (folders), then the os.mkdir function fails as it cannot find the > filenamefilename. For example: > > Current directory structure is: > > C:\books with nothing inside. > > The path I am wanting to create is: > > E:\books\a\anne rice > > Yes, I could break the path and check each level of the directory. But I was > wondering if there was any other method to do this. As if you do this at > Windows 10 command line: > > Mkdir E:\books\"a\anne rice" > > The 'a' directory plus 'anne rice' is also created. > > I hope this makes sense. Python 3.8 under windows 10. > > Sean > Hello, I think you may need to use os.makedirs instead of os.mkdir. It will recursively create directories, and if the path parameter has only one level, it is the same as the mkdir() function. -- OpenPGP fingerprint: 3C47 5977 4819 267E DD64 C7E4 6332 5675 A739 C74E From 1611kjb at gmail.com Sat Feb 6 09:06:13 2021 From: 1611kjb at gmail.com (Michael Deslippe) Date: Sat, 6 Feb 2021 14:06:13 +0000 Subject: [Tutor] OS lib creating an directory using an absolute path. In-Reply-To: 3.1026632 References: 3.1026632 Message-ID: I would direct you to this article: http://www.compciv.org/practicum/shakefiles/a-creating-a-directory-idempotently/ ---Mike ________________________________ From: Tutor on behalf of mhysnm1964 at gmail.com Sent: Saturday, February 6, 2021 5:33:26 AM To: tutor at python.org Subject: [Tutor] OS lib creating an directory using an absolute path. All, I am using the os library. If I have a directory structure with any missing directory (folders), then the os.mkdir function fails as it cannot find the filenamefilename. For example: Current directory structure is: C:\books with nothing inside. The path I am wanting to create is: E:\books\a\anne rice Yes, I could break the path and check each level of the directory. But I was wondering if there was any other method to do this. As if you do this at Windows 10 command line: Mkdir E:\books\"a\anne rice" The 'a' directory plus 'anne rice' is also created. I hope this makes sense. Python 3.8 under windows 10. Sean _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From mhysnm1964 at gmail.com Sat Feb 6 13:29:30 2021 From: mhysnm1964 at gmail.com (mhysnm1964 at gmail.com) Date: Sun, 7 Feb 2021 05:29:30 +1100 Subject: [Tutor] OS lib creating an directory using an absolute path. In-Reply-To: References: 3.1026632 Message-ID: <00ac01d6fcb6$045dec80$0d19c580$@gmail.com> Thanks all for the quick response. Michael, that is a good article. From: Michael Deslippe <1611kjb at gmail.com> Sent: Sunday, 7 February 2021 1:06 AM To: mhysnm1964 at gmail.com; tutor at python.org Subject: Re: [Tutor] OS lib creating an directory using an absolute path. I would direct you to this article: http://www.compciv.org/practicum/shakefiles/a-creating-a-directory-idempoten tly/ ---Mike _____ From: Tutor > on behalf of mhysnm1964 at gmail.com > Sent: Saturday, February 6, 2021 5:33:26 AM To: tutor at python.org > Subject: [Tutor] OS lib creating an directory using an absolute path. All, I am using the os library. If I have a directory structure with any missing directory (folders), then the os.mkdir function fails as it cannot find the filenamefilename. For example: Current directory structure is: C:\books with nothing inside. The path I am wanting to create is: E:\books\a\anne rice Yes, I could break the path and check each level of the directory. But I was wondering if there was any other method to do this. As if you do this at Windows 10 command line: Mkdir E:\books\"a\anne rice" The 'a' directory plus 'anne rice' is also created. I hope this makes sense. Python 3.8 under windows 10. Sean _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From robertvstepp at gmail.com Sat Feb 6 18:38:07 2021 From: robertvstepp at gmail.com (boB Stepp) Date: Sat, 6 Feb 2021 17:38:07 -0600 Subject: [Tutor] OS lib creating an directory using an absolute path. In-Reply-To: <27965210-dd76-0ef7-8aff-2fab33783ec6@wichmann.us> References: <000c01d6fc73$82dabc20$88903460$@gmail.com> <27965210-dd76-0ef7-8aff-2fab33783ec6@wichmann.us> Message-ID: On Sat, Feb 06, 2021 at 03:49:37AM -0700, Mats Wichmann wrote: >and would really suggest you not use directory names with spaces in >them, it will eventually bring you grief. Out of habit from looong ago when spaces were not allowed at all, I tend not to use spaces in directory/folder names. But don't all modern operating systems now allow spaces in their directory names? What would be the potential sources of grief in modern times? -- Wishing you only the best, boB Stepp From alan.gauld at yahoo.co.uk Sat Feb 6 19:03:25 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 7 Feb 2021 00:03:25 +0000 Subject: [Tutor] OS lib creating an directory using an absolute path. In-Reply-To: References: <000c01d6fc73$82dabc20$88903460$@gmail.com> <27965210-dd76-0ef7-8aff-2fab33783ec6@wichmann.us> Message-ID: On 06/02/2021 23:38, boB Stepp wrote: > On Sat, Feb 06, 2021 at 03:49:37AM -0700, Mats Wichmann wrote: > >> and would really suggest you not use directory names with spaces in >> them, it will eventually bring you grief. > > Out of habit from looong ago when spaces were not allowed at all, I tend > not to use spaces in directory/folder names. But don't all modern > operating systems now allow spaces in their directory names? What would be > the potential sources of grief in modern times? Speaking personally I find filenames/folders with spaces continually trip me up, especially in shell scripts and programs. You need to remember to always add quotes and escape sequences and be careful about splitting paths etc etc. If you only manage files via a GUI then it may work OK but any time you have to type filenames or paths spaces introduce extra complexity. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From mats at wichmann.us Sat Feb 6 19:13:48 2021 From: mats at wichmann.us (Mats Wichmann) Date: Sat, 6 Feb 2021 17:13:48 -0700 Subject: [Tutor] OS lib creating an directory using an absolute path. In-Reply-To: References: <000c01d6fc73$82dabc20$88903460$@gmail.com> <27965210-dd76-0ef7-8aff-2fab33783ec6@wichmann.us> Message-ID: <358990f8-f3f5-6623-d7fd-c342867c0350@wichmann.us> On 2/6/21 5:03 PM, Alan Gauld via Tutor wrote: > On 06/02/2021 23:38, boB Stepp wrote: >> On Sat, Feb 06, 2021 at 03:49:37AM -0700, Mats Wichmann wrote: >> >>> and would really suggest you not use directory names with spaces in >>> them, it will eventually bring you grief. >> >> Out of habit from looong ago when spaces were not allowed at all, I tend >> not to use spaces in directory/folder names. But don't all modern >> operating systems now allow spaces in their directory names? What would be >> the potential sources of grief in modern times? > > Speaking personally I find filenames/folders with spaces continually > trip me up, especially in shell scripts and programs. You need to > remember to always add quotes and escape sequences and be careful > about splitting paths etc etc. > > If you only manage files via a GUI then it may work OK but any time > you have to type filenames or paths spaces introduce extra complexity. Right. And why buy extra complexity? There are lots of cases where you build command lines, or process pathnames, and those - even on Windows shells - are universally split by Whitespace. We're talking about working with Python, not a gui, so... _of course_ it's possible to make things work with filenames that have spaces in them, but it's just one extra thing to keep track of when there are so many other things to keep track of. From robertvstepp at gmail.com Sat Feb 6 19:18:58 2021 From: robertvstepp at gmail.com (boB Stepp) Date: Sat, 6 Feb 2021 18:18:58 -0600 Subject: [Tutor] OS lib creating an directory using an absolute path. In-Reply-To: References: <000c01d6fc73$82dabc20$88903460$@gmail.com> <27965210-dd76-0ef7-8aff-2fab33783ec6@wichmann.us> Message-ID: On Sun, Feb 07, 2021 at 12:03:25AM +0000, Alan Gauld via Tutor wrote: >On 06/02/2021 23:38, boB Stepp wrote: >> On Sat, Feb 06, 2021 at 03:49:37AM -0700, Mats Wichmann wrote: >> >>> and would really suggest you not use directory names with spaces in >>> them, it will eventually bring you grief. >> >> Out of habit from looong ago when spaces were not allowed at all, I tend >> not to use spaces in directory/folder names. But don't all modern >> operating systems now allow spaces in their directory names? What would be >> the potential sources of grief in modern times? > >Speaking personally I find filenames/folders with spaces continually >trip me up, especially in shell scripts and programs. You need to >remember to always add quotes and escape sequences and be careful >about splitting paths etc etc. I take your points about care in quoting and splitting paths, but doesn't one, nowadays, have to be prepared to deal with potential spaces as well as other irksome characters? In the OP's original post it seemed to me the potential existed for having to deal with directory structures as they were found and not necessarily as one would want to create them oneself. But I might not have been paying close attention. But I am with you. If I am the one creating the directories then I will almost certainly not have any spaces in them, at least if I am programming and not working in Word, Open Office, etc. and naming a document. And even then I have a strong tendency not to use spaces or potentially troublesome characters. -- Wishing you only the best, boB Stepp From manpritsinghece at gmail.com Sun Feb 7 00:25:20 2021 From: manpritsinghece at gmail.com (Manprit Singh) Date: Sun, 7 Feb 2021 10:55:20 +0530 Subject: [Tutor] Question about code readability and good coding practise Message-ID: Dear Sir , Just need your suggestions : Consider a problem where I have to make a program that allows the user to give integer inputs 10 times from the keyboard and then put all these 10 integers into a python list. Writing like this would be considered readable and good code ? var = [int(input("Enter number")) for i in range(10)] regards Manprit Singh From PyTutor at DancesWithMice.info Sun Feb 7 01:48:27 2021 From: PyTutor at DancesWithMice.info (dn) Date: Sun, 7 Feb 2021 19:48:27 +1300 Subject: [Tutor] Question about code readability and good coding practise In-Reply-To: References: Message-ID: <103c7341-7b11-1001-374e-445c265e88b4@DancesWithMice.info> On 07/02/2021 18.25, Manprit Singh wrote: > Dear Sir , > > Just need your suggestions : > > Consider a problem where I have to make a program that allows the user to > give integer inputs 10 times from the keyboard and then put all these 10 > integers into a python list. > Writing like this would be considered readable and good code ? > > var = [int(input("Enter number")) for i in range(10)] List comprehensions are concise and powerful. Very useful to those in-the-know, but can be quite mystifying (and therefore error-prone) to 'beginners'. The code does not include a gap between the input-prompt and the user-entered data. Is it a coding matter or perhaps more for user experience (UX): there is no extra indication to the user that (s)he is expected to respond - better practice would include adding a question-mark or colon. At the specification level, the idea of asking for ten numbers without distinguishing between them, seems odd - and asking for user-confusion or user-introduced errors, eg do I put "width" before "height" or vice-versa? At the execution level, how do you handle data-entry errors, eg pressing (or on the previous line, double-pressing) Enter? What about someone typing "t" or "y" instead of "5"? Combining the last two topics, how will the code be able to advise the user which piece of data is at-fault? If there is a fault, which is caught, how will the code AND the user know where to re-start the amorphous list of 10 inputs? PS they are not 'just' "inputs" (per question wording), the code requires that they be (by Python definition and the user's machine's implementation) integers! -- Regards, =dn From mhysnm1964 at gmail.com Sun Feb 7 03:07:02 2021 From: mhysnm1964 at gmail.com (mhysnm1964 at gmail.com) Date: Sun, 7 Feb 2021 19:07:02 +1100 Subject: [Tutor] Unknown encoded file types. Message-ID: <009101d6fd28$399e4080$acdac180$@gmail.com> Hi all, Windows 10, python 3.8 is what I am using. I have 100's of small plain text files that are under 5k each. I am concatenating them into one big text file. The issue I am having is getting encoding errors. I have tried to open them with the encode parameter on the "with open" command. Some of the files are throwing encoding UTF errors. Looking like they are not in that format. The only reliable way I have managed to open the files is in binary mode. With open (filename, 'rb') as fp: Content = fp.read() I don't need to process the content thus why I am not using fp.readline() Is there any way to identify the encoded format before opening to change the encoded format? I have seen some info on the net and don't understand it. Sean From PyTutor at DancesWithMice.info Sun Feb 7 03:32:56 2021 From: PyTutor at DancesWithMice.info (dn) Date: Sun, 7 Feb 2021 21:32:56 +1300 Subject: [Tutor] Unknown encoded file types. In-Reply-To: <009101d6fd28$399e4080$acdac180$@gmail.com> References: <009101d6fd28$399e4080$acdac180$@gmail.com> Message-ID: <468daf99-edb9-4295-bb1b-b9bd40f85e3a@DancesWithMice.info> On 07/02/2021 21.07, mhysnm1964 at gmail.com wrote: > Hi all, > > Windows 10, python 3.8 is what I am using. > > I have 100's of small plain text files that are under 5k each. I am > concatenating them into one big text file. The issue I am having is getting > encoding errors. I have tried to open them with the encode parameter on the > "with open" command. Some of the files are throwing encoding UTF errors. > Looking like they are not in that format. The only reliable way I have > managed to open the files is in binary mode. > > With open (filename, 'rb') as fp: > Content = fp.read() > > I don't need to process the content thus why I am not using fp.readline() > > Is there any way to identify the encoded format before opening to change the > encoded format? I have seen some info on the net and don't understand it. Which OpSys is in-use (the source of the data-files)? Which locale is set? Which encode parameter(s) have been used? Is it likely that the filenames and/or contents include European or other characters outside the US-ASCII set? -- Regards, =dn From alan.gauld at yahoo.co.uk Sun Feb 7 04:19:06 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 7 Feb 2021 09:19:06 +0000 Subject: [Tutor] Unknown encoded file types. In-Reply-To: <009101d6fd28$399e4080$acdac180$@gmail.com> References: <009101d6fd28$399e4080$acdac180$@gmail.com> Message-ID: On 07/02/2021 08:07, mhysnm1964 at gmail.com wrote: > I have 100's of small plain text files that are under 5k each. I am > concatenating them into one big text file. The issue I am having is getting > encoding errors. I have tried to open them with the encode parameter on the > "with open" command. Some of the files are throwing encoding UTF errors. > Looking like they are not in that format. The only reliable way I have > managed to open the files is in binary mode. Yes, that's right, the only reliable way of opening a file, if you don't know what is in it, is using binary mode and treating it as a stream of bytes. You can interrogate the bytes and see if you recognise any of them, or a sequence and from that infer an encoding. You say they are text files, but how do you know? Even if they have a .txt extension that's no guarantee that they are really text. And if they are how old are they? If more than 20 years you are likely to be facing all manner of weird encodings. > Is there any way to identify the encoded format before opening to change the > encoded format? I have seen some info on the net and don't understand it. Not with certainty. There are tools that can look at the first few bytes and make an intelligent guess but none are reliable. Opening a file without knowing what is in it is always fraught with issues. -- 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 Sun Feb 7 04:54:48 2021 From: cs at cskk.id.au (Cameron Simpson) Date: Sun, 7 Feb 2021 20:54:48 +1100 Subject: [Tutor] Unknown encoded file types. In-Reply-To: <009101d6fd28$399e4080$acdac180$@gmail.com> References: <009101d6fd28$399e4080$acdac180$@gmail.com> Message-ID: On 07Feb2021 19:07, Sean Murphy wrote: >Windows 10, python 3.8 is what I am using. > >I have 100's of small plain text files that are under 5k each. I am >concatenating them into one big text file. The issue I am having is getting >encoding errors. I have tried to open them with the encode parameter on the >"with open" command. Some of the files are throwing encoding UTF errors. >Looking like they are not in that format. The only reliable way I have >managed to open the files is in binary mode. > >With open (filename, 'rb') as fp: > Content = fp.read() That's a fast way to read the whole file, provided that you know it is small (you say above that you do, so good). But in other circumstances this is an inviation to consume an arbitrary mount of memory. >I don't need to process the content thus why I am not using >fp.readline() Ah, but you do if there's scope for multiple encodings. If you know they're all the -same_ encoding you can just concatenate the bytes. Maybe forcing some newlines between them (but, hahaha, that requires knowing the encoding). But if they're mixed, putting them all in one file implies converting them all the the _same_ encoding. Do you know if they're all the same encoding? Or might they be a mix? >Is there any way to identify the encoded format before opening to >change the encoded format? I have seen some info on the net and don't understand it. Reliably? No. There was recently quite a lot of discussion around this problem on python-ideas. There _are_ libraries which try to identify the encoding of some presumed to be text file. Regardless, the approach would be: - open the file to learn its encoding (using some library) - then open the file in the correct encoding Cheers, Cameron Simpson From mhysnm1964 at gmail.com Sun Feb 7 04:55:49 2021 From: mhysnm1964 at gmail.com (mhysnm1964 at gmail.com) Date: Sun, 7 Feb 2021 20:55:49 +1100 Subject: [Tutor] Unknown encoded file types. In-Reply-To: <000501d6fd37$40f47700$c2dd6500$@gmail.com> References: <009101d6fd28$399e4080$acdac180$@gmail.com> <000501d6fd37$40f47700$c2dd6500$@gmail.com> Message-ID: <000601d6fd37$6bc353c0$4349fb40$@gmail.com> Alan, Looping in the mailer with my response. This is what I was suspecting. Thanks for confirming. I have even tried to decode the binary variable into UTF and it failed. I am thinking of trying to work out how to clean the file to remove any text that don't fall within the western language. Far as I am aware, only European / English should be present. More English than anything else. I will do some searching on that topic to see if I understand it or how difficult it is Another question? When using binary mode to load a text file. Does all the encoding bytes stay present in the file after the content of the file has been loaded? Thus when you join the content from two files together. You are getting the encoding information half way through the join text? Sean -----Original Message----- From: Tutor On Behalf Of Alan Gauld via Tutor Sent: Sunday, 7 February 2021 8:19 PM To: tutor at python.org Subject: Re: [Tutor] Unknown encoded file types. On 07/02/2021 08:07, mhysnm1964 at gmail.com wrote: > I have 100's of small plain text files that are under 5k each. I am > concatenating them into one big text file. The issue I am having is > getting encoding errors. I have tried to open them with the encode > parameter on the "with open" command. Some of the files are throwing encoding UTF errors. > Looking like they are not in that format. The only reliable way I have > managed to open the files is in binary mode. Yes, that's right, the only reliable way of opening a file, if you don't know what is in it, is using binary mode and treating it as a stream of bytes. You can interrogate the bytes and see if you recognise any of them, or a sequence and from that infer an encoding. You say they are text files, but how do you know? Even if they have a .txt extension that's no guarantee that they are really text. And if they are how old are they? If more than 20 years you are likely to be facing all manner of weird encodings. > Is there any way to identify the encoded format before opening to > change the encoded format? I have seen some info on the net and don't understand it. Not with certainty. There are tools that can look at the first few bytes and make an intelligent guess but none are reliable. Opening a file without knowing what is in it is always fraught with issues. -- 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 mhysnm1964 at gmail.com Sun Feb 7 06:02:13 2021 From: mhysnm1964 at gmail.com (mhysnm1964 at gmail.com) Date: Sun, 7 Feb 2021 22:02:13 +1100 Subject: [Tutor] Unknown encoded file types. In-Reply-To: References: <009101d6fd28$399e4080$acdac180$@gmail.com> Message-ID: <000801d6fd40$b2cad5a0$186080e0$@gmail.com> Cameron, I know they are not all the same encoding. The files which throw the encoding errors open fine in a text editor as plain English. I wouldn't be surprised some are in plain ASCII or using other type of UTF. Your comment on using readline instead of read is interesting. As the first 2 or 4 bytes of a file from my understanding is where the UTF (encoding) information is stored. Is this correct? My understanding of the difference between readline and read is how the information is stored. Readline stores it in a list while read stores as a string. Can you read a single line from each file? I haven't looked into this. If I look at each first file and test it against plain ascii and a few UTF common file formats. This might give me more info. Note: for got to mention before. When I had about 10 files in a byte variable. I could not convert into ASCII or UTF using the decode method. It complained about a character not being able to be decoded. Gave some offset in the error message. I will post the error tomorrow. To late here now. Sean -----Original Message----- From: Tutor On Behalf Of Cameron Simpson Sent: Sunday, 7 February 2021 8:55 PM To: tutor at python.org Subject: Re: [Tutor] Unknown encoded file types. On 07Feb2021 19:07, Sean Murphy wrote: >Windows 10, python 3.8 is what I am using. > >I have 100's of small plain text files that are under 5k each. I am >concatenating them into one big text file. The issue I am having is >getting encoding errors. I have tried to open them with the encode >parameter on the "with open" command. Some of the files are throwing encoding UTF errors. >Looking like they are not in that format. The only reliable way I have >managed to open the files is in binary mode. > >With open (filename, 'rb') as fp: > Content = fp.read() That's a fast way to read the whole file, provided that you know it is small (you say above that you do, so good). But in other circumstances this is an inviation to consume an arbitrary mount of memory. >I don't need to process the content thus why I am not using >fp.readline() Ah, but you do if there's scope for multiple encodings. If you know they're all the -same_ encoding you can just concatenate the bytes. Maybe forcing some newlines between them (but, hahaha, that requires knowing the encoding). But if they're mixed, putting them all in one file implies converting them all the the _same_ encoding. Do you know if they're all the same encoding? Or might they be a mix? >Is there any way to identify the encoded format before opening to >change the encoded format? I have seen some info on the net and don't understand it. Reliably? No. There was recently quite a lot of discussion around this problem on python-ideas. There _are_ libraries which try to identify the encoding of some presumed to be text file. Regardless, the approach would be: - open the file to learn its encoding (using some library) - then open the file in the correct encoding Cheers, Cameron Simpson _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From mhysnm1964 at gmail.com Sun Feb 7 06:06:39 2021 From: mhysnm1964 at gmail.com (mhysnm1964 at gmail.com) Date: Sun, 7 Feb 2021 22:06:39 +1100 Subject: [Tutor] joining strings using arrays and string variables. Message-ID: <001101d6fd41$50e93290$f2bb97b0$@gmail.com> All, A really basic question. If I want to join a list that has a maximum of 5 elements plus two strings variable. The below doesn't seem to work: >>> a = ['hello','fred','and','tom'] >>> b = '/'.join(['welcome', 'the', 'following', a]) Traceback (most recent call last): File "", line 1, in TypeError: sequence item 3: expected str instance, list found >>> b = '/'.join('welcome', 'the', 'following', a) Traceback (most recent call last): File "", line 1, in TypeError: join() takes exactly one argument (4 given) How can you do the above? Sean From cs at cskk.id.au Sun Feb 7 06:40:10 2021 From: cs at cskk.id.au (Cameron Simpson) Date: Sun, 7 Feb 2021 22:40:10 +1100 Subject: [Tutor] Unknown encoded file types. In-Reply-To: <000601d6fd37$6bc353c0$4349fb40$@gmail.com> References: <000601d6fd37$6bc353c0$4349fb40$@gmail.com> Message-ID: On 07Feb2021 20:55, Sean Murphy wrote: >This is what I was suspecting. Thanks for confirming. I have even tried >to decode the binary variable into UTF and it failed. That just says it isn't UTF-8. If you were trying UTF-8. >I am thinking of trying >to work out how to clean the file to remove any text that don't fall within >the western language. Is that necessary? >Far as I am aware, only European / English should be >present. More English than anything else. That leaves plenty of scope for nonASCII bytes. What kind of criterion do you think would help you? >When using binary mode to load a text file. Does all the encoding bytes >stay present in the file after the content of the file has been loaded? Thus when >you join the content from two files together. You are getting the encoding >information half way through the join text? The often aren't any "encoding bytes" to save/preserve. The text will simply have been transcribed in whatever encoding was in use. There aren't standard "markers" for this stuff, which is why an unknown file is guesswork. If the text commences with a BOM (FFFE or FEFF) it is probably UTF-16BE or UTF-16LE respectively. But otherwise you're on your own, falling back to libraries which guess from the elading data and the byte value distributions. Cheers, Cameron Simpson From sarfraaz at gmail.com Sun Feb 7 06:55:36 2021 From: sarfraaz at gmail.com (Sarfraaz Ahmed) Date: Sun, 7 Feb 2021 17:25:36 +0530 Subject: [Tutor] Unknown encoded file types. In-Reply-To: References: <000601d6fd37$6bc353c0$4349fb40$@gmail.com> Message-ID: On Sun, Feb 7, 2021 at 5:11 PM Cameron Simpson wrote: > On 07Feb2021 20:55, Sean Murphy wrote: > >This is what I was suspecting. Thanks for confirming. I have even tried > >to decode the binary variable into UTF and it failed. > I had a similar encoding issue on my Mac OS machine yesterday and this stackoverflow answer helped me. I updated my experience as a comment there to help others who might face a similar issue. https://stackoverflow.com/questions/18171739/unicodedecodeerror-when-reading-csv-file-in-pandas-with-python#comment116826345_18172249 You basically need to find out the encoding for each file and provide that encoding format at the time of opening the file. This major work is in identifying the encoding format of all your files. Hopefully, if they are same, then you would have less work. Hope that helps. > > That just says it isn't UTF-8. If you were trying UTF-8. > > >I am thinking of trying > >to work out how to clean the file to remove any text that don't fall > within > >the western language. > > Is that necessary? > > >Far as I am aware, only European / English should be > >present. More English than anything else. > > That leaves plenty of scope for nonASCII bytes. What kind of criterion > do you think would help you? > > >When using binary mode to load a text file. Does all the encoding bytes > >stay present in the file after the content of the file has been loaded? > Thus when > >you join the content from two files together. You are getting the encoding > >information half way through the join text? > > The often aren't any "encoding bytes" to save/preserve. The text will > simply have been transcribed in whatever encoding was in use. There > aren't standard "markers" for this stuff, which is why an unknown file > is guesswork. > > If the text commences with a BOM (FFFE or FEFF) it is probably UTF-16BE > or UTF-16LE respectively. But otherwise you're on your own, falling back > to libraries which guess from the elading data and the byte value > distributions. > > Cheers, > Cameron Simpson > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Jazaak Allahu Khair -- Sarfraaz Ahmed From cs at cskk.id.au Sun Feb 7 07:04:56 2021 From: cs at cskk.id.au (Cameron Simpson) Date: Sun, 7 Feb 2021 23:04:56 +1100 Subject: [Tutor] Unknown encoded file types. In-Reply-To: <000801d6fd40$b2cad5a0$186080e0$@gmail.com> References: <000801d6fd40$b2cad5a0$186080e0$@gmail.com> Message-ID: On 07Feb2021 22:02, Sean Murphy wrote: >I know they are not all the same encoding. The files which throw the >encoding errors open fine in a text editor as plain English. I wouldn't be >surprised some are in plain ASCII or using other type of UTF. Plain English fits in ASCII. It will look exactly like ASCII in UTF8, and like 2-byte pairs of ASCII+NUL or NUL+ASCII in UTF16. The summary of my verbaige below is that for your situation you may be able to infer (not deduce) a character set and encoding from sniffing the first 2 bytes. >Your comment >on using readline instead of read is interesting. As the first 2 or 4 bytes >of a file from my understanding is where the UTF (encoding) information is >stored. Is this correct? _If_ it is UTF16. Possibly. Plain ASCII, for example, has nothing. I understand that some common Windows programmes (Notepad?) write UTF16, likely UTF16LE (little endian). I also understand that often these text files start with a BOM (byte order marker), FFFE for UTF16BE and FEFF for UTF16LE. If you scan the first 2 bytes and they are FFFE or FEFF and you're expecting text, then I think it very likely that they're utf16. And open accepts, for example, encoding='utf-6le' as an option. See the codecs module in the standard library for information on available encodings. I noticed there that there are several byte order mark sequences. Be aware the ASCII, UTF-8 and the various ISO8859 8-bit charsets often don't have these markers at the start (the 8 bit ones certainly won't). >My understanding of the difference between readline and read is how the >information is stored. Readline stores it in a list while read stores as a >string. No, they both return a string in text mode. In binary mode read returns a bytes object, and readline returns a bytes object ending in code 10 (newline, on the assumption that the bytes might be ASCII or ASCIIlike). If readline is even available in binary mode; I know bytes objects gained a few "str-like" methods. >Can you read a single line from each file? Reading a line requires recognising line endings, which depends on the encoding. But many character sets use ASCII as a base and newlines at the end of lines. (Or carriage returns, eg MacOS9 and earlier). >I haven't looked into >this. If I look at each first file and test it against plain ascii and a few >UTF common file formats. This might give me more info. That will be guesswork, but within a given domain (eg yours) it may be reliable. >Note: for got to mention before. When I had about 10 files in a byte >variable. I could not convert into ASCII or UTF using the decode method. There are various UTFs. Unicode is a mapping of "code points" (ordinals) to characters (and some other things). UTF8, UTF16 et al are different _encodings_ of those ordinals for storage in bytes in a file. So UTF8 has a variable number of bytes per ordinal which among its features are (a) it is compact for Western alphabets and (b) identical to ASCII For the the characters which are n the ASCII range. UTF16 uses 2 bytes per ordinal, less compact but fixed width. There are ordinals in Unicode beyond the 16 bit range, BTW. >It >complained about a character not being able to be decoded. Gave some offset >in the error message. I will post the error tomorrow. To late here now. A successful decode requires decoding all the bytes, so if your bytes end part way through a character the decode will fail even if you're using the right encoding. But any UTF16 encoding will be an even number of bytes. If you're dealing with "common" Windows files I'd expect to be able to sniff the first 2 bytes for a 16 bit BOM - if present, presume UTF16 in whichever flavour. Otherwise try UTF8 and see how it goes. The various BOMs in the codecs modules suggest there might be other BOM sequences worth trying (not all 2 bytes long). And let is not get into other character sets and encodings (not ASCII, not UTF, eg EBCEDIC or Shift JIS). Cheers, Cameron Simpson From cs at cskk.id.au Sun Feb 7 07:09:37 2021 From: cs at cskk.id.au (Cameron Simpson) Date: Sun, 7 Feb 2021 23:09:37 +1100 Subject: [Tutor] joining strings using arrays and string variables. In-Reply-To: <001101d6fd41$50e93290$f2bb97b0$@gmail.com> References: <001101d6fd41$50e93290$f2bb97b0$@gmail.com> Message-ID: On 07Feb2021 22:06, Sean Murphy wrote: >If I want to join a list that has a maximum of 5 elements plus two >strings variable. The below doesn't seem to work: > >>>> a = ['hello','fred','and','tom'] This is a flat list of str. >>>> b = '/'.join(['welcome', 'the', 'following', a]) This tries to join a list of most-str, except for a, which is itself a list: ['welcome', 'the', 'following', ['hello','fred','and','tom']] >Traceback (most recent call last): > File "", line 1, in >TypeError: sequence item 3: expected str instance, list found That'll be a, which isn't a str, it is a list. >>>> b = '/'.join('welcome', 'the', 'following', a) Join expects a single iterable of strs. Not 4 arguments. You probably want this: b = '/'.join(['welcome', 'the', 'following'] + a) Nornmally I'd prepare all that before calling join: a = ['hello','fred','and','tom'] a2 = ['welcome', 'the', 'following'] + a b = '/'.join(a2) just for clarity rather than burying it inside the join() arguments. Cheers, Cameron Simpson From alan.gauld at yahoo.co.uk Sun Feb 7 07:15:27 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 7 Feb 2021 12:15:27 +0000 Subject: [Tutor] Unknown encoded file types. In-Reply-To: <000601d6fd37$6bc353c0$4349fb40$@gmail.com> References: <009101d6fd28$399e4080$acdac180$@gmail.com> <000501d6fd37$40f47700$c2dd6500$@gmail.com> <000601d6fd37$6bc353c0$4349fb40$@gmail.com> Message-ID: On 07/02/2021 09:55, mhysnm1964 at gmail.com wrote: > When using binary mode to load a text file. Does all the encoding bytes stay > present in the file after the content of the file has been loaded? Thus when > you join the content from two files together. You are getting the encoding > information half way through the join text? This is is the problem, there are no encoding bytes. the encoding is implicit. You have to know (or figure out) the encoding based on the content! Modern file formats such as HTML/XML etc include an encoding string at the start of the file for exactly that reason. But older encodings (pre-unicode) had no clue what the encoding was. About all you can do is visually inspect them - maybe even using a hex editor or assembly debugger! A good editor such as vim or emacs or will try to present anything it recognizes and that can help figure out what's missing and give a clue. Then you can try multiple different encodings to see if any of them work. But ultimately it is guess work and trial and error! If you know the country/language used that can often narrow things down significantly. And if its unicode then you really only have 3 options to try so its not too bad. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alan.gauld at yahoo.co.uk Sun Feb 7 07:25:14 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 7 Feb 2021 12:25:14 +0000 Subject: [Tutor] joining strings using arrays and string variables. In-Reply-To: <001101d6fd41$50e93290$f2bb97b0$@gmail.com> References: <001101d6fd41$50e93290$f2bb97b0$@gmail.com> Message-ID: On 07/02/2021 11:06, mhysnm1964 at gmail.com wrote: >>>> a = ['hello','fred','and','tom'] > >>>> b = '/'.join(['welcome', 'the', 'following', a]) > > > Traceback (most recent call last): > TypeError: sequence item 3: expected str instance, list found The problem is that a is a list. You need to flatten the two lists into a single list. + will do that >>> [1,2,3]+[4,5,6] [1,2,3,4,5,6] So in your case: b = '/'.join(['welcome', 'the', 'following'] + a) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alan.gauld at yahoo.co.uk Sun Feb 7 07:28:43 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 7 Feb 2021 12:28:43 +0000 Subject: [Tutor] Unknown encoded file types. In-Reply-To: References: <000801d6fd40$b2cad5a0$186080e0$@gmail.com> Message-ID: > On 07Feb2021 22:02, Sean Murphy wrote: >> My understanding of the difference between readline and read is how the >> information is stored. Readline stores it in a list while read stores as a >> string. You are thinking of readlines() - note the 's'! readline() returns a single line as a string. readlines() returns all the lines as a list of strings > So UTF8 has a variable number of bytes per ordinal which among its > features are (a) it is compact for Western alphabets and (b) identical > to ASCII For the the characters which are n the ASCII range. UTF16 uses > 2 bytes per ordinal, less compact but fixed width. Being picky, utf16 can extend to 4 bytes for a few rare cases. > There are ordinals in Unicode beyond the 16 bit range, BTW. Just so. > But any UTF16 encoding will be an even number of bytes. This is true, unlike utf8 which can be 1,2,3 or 4 bytes long. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alan.gauld at yahoo.co.uk Sun Feb 7 08:07:23 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 7 Feb 2021 13:07:23 +0000 Subject: [Tutor] Unknown encoded file types. In-Reply-To: <000601d6fd37$6bc353c0$4349fb40$@gmail.com> References: <009101d6fd28$399e4080$acdac180$@gmail.com> <000501d6fd37$40f47700$c2dd6500$@gmail.com> <000601d6fd37$6bc353c0$4349fb40$@gmail.com> Message-ID: On 07/02/2021 09:55, mhysnm1964 at gmail.com wrote: > to work out how to clean the file to remove any text that don't fall within > the western language. Far as I am aware, only European / English should be > present. More English than anything else. Most of the non standard characters in Latin encodings for European languages are to cater for all the ornamentations that Europeans seem to like. umaut, circumflex, cedilla, grave etc. Stripping out those characters will leave words that are hard to translate. The good news is that there is a finite number of such encodings and its not impossible, given you have a relatively small set of files(in computer terms) you could just try each decoding in turn and display say the first 10 lines for all that don't give an error. That would let you select what looks like the best for each file. encodings = ['ascii','utf8','utf16', 'latin8539', 'latin..... etc] for file in files: for encoding in encodings try: decode file display filename + encoding + 10 lines except: continue else: print " no encoding worked for ' + file Just a thought. -- 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 Sun Feb 7 09:27:31 2021 From: mats at wichmann.us (Mats Wichmann) Date: Sun, 7 Feb 2021 07:27:31 -0700 Subject: [Tutor] Question about code readability and good coding practise In-Reply-To: References: Message-ID: <5402ce9c-93ba-89ee-2a79-341e74b07e0d@wichmann.us> On 2/6/21 10:25 PM, Manprit Singh wrote: > Dear Sir , > > Just need your suggestions : > > Consider a problem where I have to make a program that allows the user to > give integer inputs 10 times from the keyboard and then put all these 10 > integers into a python list. > Writing like this would be considered readable and good code ? > > var = [int(input("Enter number")) for i in range(10)] I'll agree with @dn here. When I first started seeing comprehensions they seemed really obscure, over time (not much time) they became very readable for me, more so than rolling things out into loops. So really no definitive answer on readability of using comprehensions in general - it may depend on whether you expect your audience to be experienced or inexperienced Python programmers. To the specific case, when you use "input", you are taking data from outside the program. Such data should always be carefully validated - that's just basic security practice. The line above just does too much hiding. I'd assert that the "readable" form for such a scenario would have a loop the body of which contains something like prompt for input input valid? add to saved data else explain problem and go again Once again, probably in "real life" coding, you'll very rarely use input... From breamoreboy at gmail.com Sun Feb 7 09:09:23 2021 From: breamoreboy at gmail.com (Mark Lawrence) Date: Sun, 7 Feb 2021 14:09:23 +0000 Subject: [Tutor] Unknown encoded file types. In-Reply-To: <009101d6fd28$399e4080$acdac180$@gmail.com> References: <009101d6fd28$399e4080$acdac180$@gmail.com> Message-ID: On 07/02/2021 08:07, mhysnm1964 at gmail.com wrote: [snip] > > Is there any way to identify the encoded format before opening to change the > encoded format? I have seen some info on the net and don't understand it. > The most popular tool is https://chardet.readthedocs.io/en/latest/ but as all ready been noted nothing is 100% reliable. Alternatives https://python.libhunt.com/chardet-alternatives -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From connectsachit at gmail.com Sun Feb 7 08:22:42 2021 From: connectsachit at gmail.com (Sachit Murarka) Date: Sun, 7 Feb 2021 18:52:42 +0530 Subject: [Tutor] Creating instance of child classes dynamically Message-ID: Hey Users, I have different classes in different python files. There are child classes and I do have a base class in a different class. My input to the python will be a string , according to the string I have to create an object of child class and call the functions. Here I have to use the concept of reflection + dynamic polymorphism. Could anyone please help me here? Kind Regards, Sachit Murarka From jf9481 at mci4me.at Sun Feb 7 06:56:47 2021 From: jf9481 at mci4me.at (jf9481 at mci4me.at) Date: Sun, 07 Feb 2021 12:56:47 +0100 Subject: [Tutor] Python on Mac with macOS Catalina with 10.15.7 won't open Message-ID: <6587a4aaab17f1625f4243825a7d47a3@mci4me.at> Dear Sir or Madam, For my master thesis I will probably have to use Python. I tried to download the latest version, which worked just fine and I got the certificates necessary, as instructed by the installation guide. But after double clicking on the launcher nothing would happen, except for the start window to pop up. In a tutorial I saw that a Python window is supposed to open up, which doesn't happen. Maybe you can help me with this problem, since I am new to this and don't really know what I should be looking for. Best regards, Falk Jungmeier. From john at johnweller.co.uk Sun Feb 7 06:52:40 2021 From: john at johnweller.co.uk (John Weller) Date: Sun, 7 Feb 2021 11:52:40 -0000 Subject: [Tutor] os.path Query Message-ID: <016a01d6fd47$bdd226e0$397674a0$@johnweller.co.uk> Hi I need to check that a file is present in the current directory before I attempt to open it. I have used both os.path.isfile("some_file.txt") and os.path.exists("some_file.txt") with apparent success which begs the question - what is the difference between them and which should I use? John John Weller 07976 393631 From alan.gauld at yahoo.co.uk Sun Feb 7 09:58:32 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 7 Feb 2021 14:58:32 +0000 Subject: [Tutor] Creating instance of child classes dynamically In-Reply-To: References: Message-ID: On 07/02/2021 13:22, Sachit Murarka wrote: > I have different classes in different python files. There are child classes > and I do have a base class in a different class. I'm not sure what that last bit means? "A base class in a different class" Do you mean you have created the base class inside some other class? That's extremely unusual, is there a reason? Or do you just mean you have a seperate base class? > My input to the python will be a string , according to the string I have to > create an object of child class and call the functions. Presumably you mean call the methods, not functions? But otherwise that's fairly straightforward, you can test your string and create whichever type of object is required. Presumably there is something in the string that lets you decide which class you need? > Here I have to use the concept of reflection + dynamic polymorphism. In python all polymorphism is dynamic. And I'm not sure why you would need reflection. I don't see anything in your problem that looks like a candidate. > Could anyone please help me here? You probably need to be more specific. May be give is two input samples and tell us which class you need for each, and how you decided that. Once see see a few examples we can help find a programmable 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 alan.gauld at yahoo.co.uk Sun Feb 7 10:03:25 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 7 Feb 2021 15:03:25 +0000 Subject: [Tutor] Python on Mac with macOS Catalina with 10.15.7 won't open In-Reply-To: <6587a4aaab17f1625f4243825a7d47a3@mci4me.at> References: <6587a4aaab17f1625f4243825a7d47a3@mci4me.at> Message-ID: On 07/02/2021 11:56, jf9481--- via Tutor wrote: > download the latest version, which worked just fine and I got the > certificates necessary, as instructed by the installation guide. I'm not sure what certificates you mean? Could you tell us exactly which vesion of Python you downloaded? Was it the official python.org distro opr perhaps one of the scientific ones like Annaconda or Enthought? > But after double clicking on the launcher nothing would happen, except > for the start window to pop up. Again, I'm not sure what you mean by the start window. But I'm not a Mac user so it may be Mac specific. Python is an interpreter and usually used from a command line - the terminal app in MacOS I believe? Or from inside an IDE such as Eclipse, netbeans or Visual Code. Basic Python comes with a basic IDE called IDLE, but I'm not sure how you find/launch that on a Mac. > In a tutorial I saw that a Python window is supposed to open up, which > doesn't happen. Does this "Python Window" containa prompt that looks like >>> If so it may be IDLE.(or some other IDE) Can you give a link to the tutorial so we can see what you see? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alan.gauld at yahoo.co.uk Sun Feb 7 10:05:06 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 7 Feb 2021 15:05:06 +0000 Subject: [Tutor] os.path Query In-Reply-To: <016a01d6fd47$bdd226e0$397674a0$@johnweller.co.uk> References: <016a01d6fd47$bdd226e0$397674a0$@johnweller.co.uk> Message-ID: On 07/02/2021 11:52, John Weller wrote: > attempt to open it. I have used both os.path.isfile("some_file.txt") and > os.path.exists("some_file.txt") with apparent success which begs the > question - what is the difference between them and which should I use? exists(something) tells you if something exists in your filesystem. It gives no clue what something is - a drive, a folder, a file or a shortcut. isfile(something) tells you if something both exists and is a file. And only a file, anything else will be false. -- 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 eryksun at gmail.com Sun Feb 7 10:47:49 2021 From: eryksun at gmail.com (Eryk Sun) Date: Sun, 7 Feb 2021 09:47:49 -0600 Subject: [Tutor] os.path Query In-Reply-To: References: <016a01d6fd47$bdd226e0$397674a0$@johnweller.co.uk> Message-ID: On 2/7/21, Alan Gauld via Tutor wrote: > > exists(something) > > tells you if something exists in your filesystem. > It gives no clue what something is - a drive, a folder, a file or a > shortcut. > > isfile(something) > > tells you if something both exists and is a file. > And only a file, anything else will be false. Note that both of these tests are both based on os.stat() calls that traverse symlinks in POSIX, and any type of reparse point in Windows (e.g. symlinks, mountpoints). Thus they don't test whether "something" is a symlink. From connectsachit at gmail.com Sun Feb 7 10:18:22 2021 From: connectsachit at gmail.com (Sachit Murarka) Date: Sun, 7 Feb 2021 20:48:22 +0530 Subject: [Tutor] Creating instance of child classes dynamically In-Reply-To: References: Message-ID: I meant base class in a different python file. It was a typo. Example : parent.py class Parent(): @abstractmethod def process(self): child1.py class Child1(): def process(self): #Doing something child2.py class Child2(): def process(self): #Doing something else My use case is , Parent will define the template. On the run type user will pass a string , eg String will be child1 or child2 etc. Accordingly we want to call process function of that class. Please note each child class is in a different file . In actual solution there will be many children and we can not put all children in a single py file. As using reflections we can create objects at run time by giving fully qualified class names(Atleast in java). I have worked in Java not in Python much, so trying to achieve something similar. In java we used to do this: String input="Child1"; Parent p = Class.forName(input); p.process() // this will call the method of base class which was stored in the input variable. Kind Regards, Sachit Murarka On Sun, Feb 7, 2021 at 8:29 PM Alan Gauld via Tutor wrote: > On 07/02/2021 13:22, Sachit Murarka wrote: > > > I have different classes in different python files. There are child > classes > > and I do have a base class in a different class. > > I'm not sure what that last bit means? "A base class in a different > class" Do you mean you have created the base class inside some other > class? That's extremely unusual, is there a reason? Or do you just mean > you have a seperate base class? > > > My input to the python will be a string , according to the string I have > to > > create an object of child class and call the functions. > > Presumably you mean call the methods, not functions? > But otherwise that's fairly straightforward, you can > test your string and create whichever type of object > is required. Presumably there is something in the > string that lets you decide which class you need? > > > Here I have to use the concept of reflection + dynamic polymorphism. > > In python all polymorphism is dynamic. > And I'm not sure why you would need reflection. > I don't see anything in your problem that looks > like a candidate. > > > Could anyone please help me here? > > You probably need to be more specific. > May be give is two input samples and tell us which class > you need for each, and how you decided that. Once > see see a few examples we can help find a > programmable 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 > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From alan.gauld at yahoo.co.uk Sun Feb 7 12:52:40 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 7 Feb 2021 17:52:40 +0000 Subject: [Tutor] Creating instance of child classes dynamically In-Reply-To: References: Message-ID: On 07/02/2021 15:18, Sachit Murarka wrote: > I meant base class in a different python file. It was a typo. > > Example : > > parent.py > class Parent(): > @abstractmethod > def process(self): > > child1.py > class Child1(): > def process(self): > #Doing something > > child2.py > class Child2(): > def process(self): > #Doing something else > > > My use case is , Parent will define the template. On the run type user will > pass a string , eg String will be child1 or child2 etc. Accordingly we want > to call process function of that class. OK, that's all pretty standard although a dictionary of functions keyed by string is probably a more effective way to do it. > Please note each child class is in a different file . In actual solution > there will be many children and we can not put all children in a single py > file. Best practice in Python is to keep related classes in a single module to minimize dependencies. But you don;t want a huge number of classes in a single module, I agree. > As using reflections we can create objects at run time by giving fully > qualified class names(Atleast in java). I have worked in Java not in Python > much, so trying to achieve something similar. In java we used to do this: > > String input="Child1"; > Parent p = Class.forName(input); > p.process() // this will call the method of base class which was stored in In Pyton classes are objects so you can just assign the class to a variable or put it in a dictionary so: import child1, child2 # Store classes classes = { "Child1": child1.Child1(), "Child2": child2.Child2,...} classname = "Child1" classes["Child1"]().process() # calls process on an instance of Child1 But as I said functions might be more effective here: def func1():... def fuc2():... etc... funcs = {"Child1":func1,"Child2":func2 funcs[input]() There's no point in creating classes just for the sake of it. OTOH if your classes have several other methods than just process() and they share the common data of the instance then they might make sense. -- 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 Feb 7 13:39:05 2021 From: PyTutor at DancesWithMice.info (dn) Date: Mon, 8 Feb 2021 07:39:05 +1300 Subject: [Tutor] Creating instance of child classes dynamically In-Reply-To: References: Message-ID: <723783e9-21e4-feb7-5653-9046a2d570bb@DancesWithMice.info> On 08/02/2021 06.52, Alan Gauld via Tutor wrote: > On 07/02/2021 15:18, Sachit Murarka wrote: >> I meant base class in a different python file. It was a typo. >> >> Example : >> >> parent.py >> class Parent(): >> @abstractmethod >> def process(self): >> >> child1.py >> class Child1(): >> def process(self): >> #Doing something >> >> child2.py >> class Child2(): >> def process(self): >> #Doing something else >> >> >> My use case is , Parent will define the template. On the run type user will >> pass a string , eg String will be child1 or child2 etc. Accordingly we want >> to call process function of that class. > > OK, that's all pretty standard although a dictionary of functions > keyed by string is probably a more effective way to do it. > >> Please note each child class is in a different file . In actual solution >> there will be many children and we can not put all children in a single py >> file. > > Best practice in Python is to keep related classes in a single > module to minimize dependencies. But you don;t want a huge number of > classes in a single module, I agree. > >> As using reflections we can create objects at run time by giving fully >> qualified class names(Atleast in java). I have worked in Java not in Python >> much, so trying to achieve something similar. In java we used to do this: >> >> String input="Child1"; >> Parent p = Class.forName(input); >> p.process() // this will call the method of base class which was stored in > > In Pyton classes are objects so you can just assign the class to a > variable or put it in a dictionary so: > > import child1, child2 > # Store classes > classes = { "Child1": child1.Child1(), "Child2": child2.Child2,...} > > classname = "Child1" > classes["Child1"]().process() # calls process on an instance of Child1 > > But as I said functions might be more effective here: > > def func1():... > def fuc2():... > etc... > > funcs = {"Child1":func1,"Child2":func2 > > funcs[input]() > > There's no point in creating classes just for the sake of it. > OTOH if your classes have several other methods than just > process() and they share the common data of the instance > then they might make sense. Left to my own devices, I would almost-certainly follow @Alan's approach. As an *ex-*Java programmer, this reference may be of-interest: https://pythonspot.com/factory-method/ -- Regards, =dn From PyTutor at DancesWithMice.info Sun Feb 7 13:54:34 2021 From: PyTutor at DancesWithMice.info (dn) Date: Mon, 8 Feb 2021 07:54:34 +1300 Subject: [Tutor] Unknown encoded file types. In-Reply-To: References: <009101d6fd28$399e4080$acdac180$@gmail.com> <000501d6fd37$40f47700$c2dd6500$@gmail.com> <000601d6fd37$6bc353c0$4349fb40$@gmail.com> Message-ID: <5b3a88dc-4895-9305-20b0-dec67da7f430@DancesWithMice.info> On 08/02/2021 02.07, Alan Gauld via Tutor wrote: > On 07/02/2021 09:55, mhysnm1964 at gmail.com wrote: > >> to work out how to clean the file to remove any text that don't fall within >> the western language. Far as I am aware, only European / English should be >> present. More English than anything else. > > Most of the non standard characters in Latin encodings for European > languages are to cater for all the ornamentations that Europeans seem to > like. umaut, circumflex, cedilla, grave etc. Stripping out those > characters will leave words that are hard to translate. > > The good news is that there is a finite number of such encodings and its > not impossible, given you have a relatively small set of files(in > computer terms) you could just try each decoding in turn and display say > the first 10 lines for all that don't give an error. That would let > you select what looks like the best for each file. > > encodings = ['ascii','utf8','utf16', 'latin8539', 'latin..... etc] > > for file in files: > for encoding in encodings > try: > decode file > display filename + encoding + 10 lines > except: continue > else: print " no encoding worked for ' + file > > Just a thought. Whilst @Mark follows the above post with a relatively-automated suggestion (will still require manual inspection), I was going to suggest something similar to this idea of a test-bed routine ranging across a set of 'likely' encodings until you found happiness. This because you must surely have some hint of an idea of the source of the files, eg if they have come from a German, Danish, ... user. Accordingly, running through a range of the ISO 8859 variants which were employed by MS-Win OpSys may yield one choice which works without error. Failing that, start with the source (?Luke), and see if a visual inspection of the file-contents, using NotePad/editor/word-processor shows you any of those umlauts or other diacritical marks. Did you (offer to) publish the (pertinent) contents of a sample file together with the full error-listing generated by Python? -- Regards, =dn From mats at wichmann.us Sun Feb 7 14:47:30 2021 From: mats at wichmann.us (Mats Wichmann) Date: Sun, 7 Feb 2021 12:47:30 -0700 Subject: [Tutor] Python on Mac with macOS Catalina with 10.15.7 won't open In-Reply-To: <6587a4aaab17f1625f4243825a7d47a3@mci4me.at> References: <6587a4aaab17f1625f4243825a7d47a3@mci4me.at> Message-ID: <94f09366-ddcb-0ea7-9510-8301ffb8f7f7@wichmann.us> On 2/7/21 4:56 AM, jf9481--- via Tutor wrote: > Dear Sir or Madam, > > For my master thesis I will probably have to use Python. I tried to > download the latest version, which worked just fine and I got the > certificates necessary, as instructed by the installation guide. > But after double clicking on the launcher nothing would happen, except > for the start window to pop up. > > In a tutorial I saw that a Python window is supposed to open up, which > doesn't happen. > > Maybe you can help me with this problem, since I am new to this and > don't really know what I should be looking for. You should make sure you've read this: https://docs.python.org/3/using/mac.html From cs at cskk.id.au Sun Feb 7 17:24:51 2021 From: cs at cskk.id.au (Cameron Simpson) Date: Mon, 8 Feb 2021 09:24:51 +1100 Subject: [Tutor] Unknown encoded file types. In-Reply-To: <5b3a88dc-4895-9305-20b0-dec67da7f430@DancesWithMice.info> References: <5b3a88dc-4895-9305-20b0-dec67da7f430@DancesWithMice.info> Message-ID: On 08Feb2021 07:54, DL Neil wrote: >On 08/02/2021 02.07, Alan Gauld via Tutor wrote: >> The good news is that there is a finite number of such encodings and >> its >> not impossible, given you have a relatively small set of files(in >> computer terms) you could just try each decoding in turn and display say >> the first 10 lines for all that don't give an error. That would let >> you select what looks like the best for each file. >> >> encodings = ['ascii','utf8','utf16', 'latin8539', 'latin..... etc] >> >> for file in files: >> for encoding in encodings >> try: >> decode file >> display filename + encoding + 10 lines >> except: continue >> else: print " no encoding worked for ' + file >> >> Just a thought. > > >Whilst @Mark follows the above post with a relatively-automated >suggestion (will still require manual inspection), I was going to >suggest something similar to this idea of a test-bed routine ranging >across a set of 'likely' encodings until you found happiness. > >This because you must surely have some hint of an idea of the source of >the files, eg if they have come from a German, Danish, ... user. >Accordingly, running through a range of the ISO 8859 variants which were >employed by MS-Win OpSys may yield one choice which works without >error. Aye. We've got an importer for some CSV data in a current project with exactly that problem, and exactly that heuristic: for encoding in 'utf-8', 'windows-1252', 'cp932': try: return line.decode(encoding) except UnicodeDecodeError: pass warning( '%r, line %d: cannot decode line %r, falling back to iso8859-1', self.filename, self.lineno, line ) return line.decode('iso8859-1') The choice of encodings above is entirely parochial to our source data, and still hits the fallback. Sean, note that in the above code: Successful decoding DOES NOT mean the correct encoding was used, as various byte sequences can decode in multiple encodings, yeilding different outcomes. See: https://en.wikipedia.org/wiki/Mojibake The fallback above ('iso8859-1', ISO Latin 1) is an 8-bit encoding where all bytes are 1-to-1 with the target ordinal. Like _any_ of the ISO8859 character sets, it will _always_ decode successfully because every byte is accepted. That doesn't mean it is correct. Cheers, Cameron Simpson From cs at cskk.id.au Sun Feb 7 17:39:39 2021 From: cs at cskk.id.au (Cameron Simpson) Date: Mon, 8 Feb 2021 09:39:39 +1100 Subject: [Tutor] Creating instance of child classes dynamically In-Reply-To: References: Message-ID: On 07Feb2021 20:48, Sachit Murarka wrote: >I meant base class in a different python file. It was a typo. That's ok. You just need to import that base class into the files defining the subclasses: from parent import Parent class Child1(Parent): def process(self): ... >My use case is , Parent will define the template. On the run type user >will >pass a string , eg String will be child1 or child2 etc. Accordingly we want >to call process function of that class. > >Please note each child class is in a different file . In actual solution >there will be many children and we can not put all children in a single py >file. That's also ok. But your code which makes instances of each child class needs to import them: parser.py: from child1 import Child1 from child2 import Child2 ... ... pick the right subclass for your input here ... >As using reflections we can create objects at run time by giving fully >qualified class names(Atleast in java). That's not going to play so well in Python. But a little similar to Alan's approach (which maps names to process() implementations), you can map names to classes: from child1 import Child1 from child2 import Child2 class_map = { 'child1name': Child1, 'child2name': Child2, } def Child(name, *a, **kw): ''' Create an instance of a subclass based on `name`. ''' cls = class_map[name] return cls(*a, **kw) ... parsing code ... name = ... get the right name from the input data ... child = Child(name, initialiser-args-here...) ... child.process() and that calls your factory function "Child" with the name, and the factory picks the desired class and makes a new instance. You can do more elaborate things, like keep a registry of classes in the Parent class, have each child subclass register itself, and consult that registry from the Child() factory above. But start simple. Cheers, Cameron Simpson From alan.gauld at yahoo.co.uk Mon Feb 8 04:03:21 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 8 Feb 2021 09:03:21 +0000 Subject: [Tutor] OS lib creating an directory using an absolute path. In-Reply-To: References: <000c01d6fc73$82dabc20$88903460$@gmail.com> <27965210-dd76-0ef7-8aff-2fab33783ec6@wichmann.us> Message-ID: On 07/02/2021 00:18, boB Stepp wrote: >> Speaking personally I find filenames/folders with spaces continually >> trip me up, especially in shell scripts and programs. > I take your points about care in quoting and splitting paths, but doesn't > one, nowadays, have to be prepared to deal with potential spaces as well as other > irksome characters? In production code of course. But many of my scripts are for personal use and I don't *expect* to find spaces in names. But of course occasionally I do :-( -- 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 Tue Feb 9 05:57:30 2021 From: cs at cskk.id.au (Cameron Simpson) Date: Tue, 9 Feb 2021 21:57:30 +1100 Subject: [Tutor] OS lib creating an directory using an absolute path. In-Reply-To: References: Message-ID: On 08Feb2021 09:03, Alan Gauld wrote: >On 07/02/2021 00:18, boB Stepp wrote: >>> Speaking personally I find filenames/folders with spaces continually >>> trip me up, especially in shell scripts and programs. > >> I take your points about care in quoting and splitting paths, but doesn't >> one, nowadays, have to be prepared to deal with potential spaces as well as other >> irksome characters? > >In production code of course. But many of my scripts are for personal >use and I don't *expect* to find spaces in names. But of course >occasionally I do :-( Alas, I must apply my personal scripts to files from others. I like them to be robust. Cheers, Cameron Simpson From alan.gauld at yahoo.co.uk Tue Feb 9 08:47:38 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 9 Feb 2021 13:47:38 +0000 Subject: [Tutor] OS lib creating an directory using an absolute path. In-Reply-To: References: Message-ID: On 09/02/2021 10:57, Cameron Simpson wrote: > Alas, I must apply my personal scripts to files from others. I like them > to be robust. Yes, its increasingly the case that even "official" files from installed apps have files with spaces in the names so I'm having to remember to use quotes etc. But, particularly in the shell it's not always obvious just when and what kind of quotation/escape sequence I need. The fewer of the things I find the better, so I still avoid adding to the chaos with my personal stuff. -- 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 magician002 at live.com Tue Feb 9 10:30:24 2021 From: magician002 at live.com (ken c) Date: Tue, 9 Feb 2021 15:30:24 +0000 Subject: [Tutor] (no subject) Message-ID: Hello Ladies & Gentlemen, I am wanting to build a MP3 player using raspberry pi as a base, using a portable hard drive to hold the music, etc. I want to be able to access music, short stories, audio books, old time radio and possibly movies. To make matters a little more complicated, I have to do a lot of traveling and would like to be able to use this in a vehicle instead of listening to the inane comments of the DJs and the commercials. However, I am not in the best of health and know nothing about how to build the software in order to make this happen. Thanks for reading this rant, Kenny Sent from Mail for Windows 10 From PyTutor at DancesWithMice.info Tue Feb 9 14:51:21 2021 From: PyTutor at DancesWithMice.info (dn) Date: Wed, 10 Feb 2021 08:51:21 +1300 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: <90020f1a-a4bb-5444-9cc5-146630663d72@DancesWithMice.info> On 10/02/2021 04.30, ken c wrote: > Hello Ladies & Gentlemen, > > I am wanting to build a MP3 player using raspberry pi as a base, using a portable hard drive to hold the music, etc. > I want to be able to access music, short stories, audio books, old time radio and possibly movies. To make matters a little more complicated, I have to do a lot of traveling and would like to be able to use this in a vehicle instead of listening to the inane comments of the DJs and the commercials. However, I am not in the best of health and know nothing about how to build the software in order to make this happen. Hello Kenny, and welcome to the list. We're happy to help people along their Python journey. Perhaps you could re-phrase the above as a specific question: - are you offering to pay someone to build such a device? - do you want to learn how to code in Python? - do you want to learn how to access an MP3 player from Python? - do you have the various battery/power-supply issues sorted-out? - etc/other? -- Regards, =dn From cs at cskk.id.au Tue Feb 9 17:29:07 2021 From: cs at cskk.id.au (Cameron Simpson) Date: Wed, 10 Feb 2021 09:29:07 +1100 Subject: [Tutor] OS lib creating an directory using an absolute path. In-Reply-To: References: Message-ID: On 09Feb2021 13:47, Alan Gauld wrote: >On 09/02/2021 10:57, Cameron Simpson wrote: >> Alas, I must apply my personal scripts to files from others. I like >> them >> to be robust. > >Yes, its increasingly the case that even "official" files from >installed apps have files with spaces in the names so I'm having >to remember to use quotes etc. But, particularly in the shell >it's not always obvious just when and what kind of >quotation/escape sequence I need. If you've got something tricky, poll me privately. I write a _lot_ of shell. >The fewer of the things I find the better, so I still avoid >adding to the chaos with my personal stuff. Aye. Our home media server has entirely lowercase dashed filenames. Though a few have apostrophes in them :-( Apps like Plex _require_ a media tree with filenames-with-spaces, because they deduce titles etc from them. Ugh. I have a script to make a parallel Plex tree from the real media tree. Cheers, Cameron Simpson From nathan-tech at hotmail.com Tue Feb 9 17:36:36 2021 From: nathan-tech at hotmail.com (nathan tech) Date: Tue, 9 Feb 2021 22:36:36 +0000 Subject: [Tutor] small graphics question Message-ID: Hi Folks, I was originally going to ask this over on the wx python mailing list, but I found myself hesitating. I am pondering a simple question: How do I write x to the power of y, in python/ Not 5^2 for 5 squared, but 5 superscript 2? or 5 superscript x where x is any number? Similarly, how do I produce fractions?? I asked here in the end because I figure for wx python it is just tracking down that character sequence and then writing it on screen (which I can do!) Thanks in advance, Nathan From robertvstepp at gmail.com Tue Feb 9 18:12:49 2021 From: robertvstepp at gmail.com (boB Stepp) Date: Tue, 9 Feb 2021 17:12:49 -0600 Subject: [Tutor] small graphics question In-Reply-To: References: Message-ID: On 21/02/09 10:36PM, nathan tech wrote: >How do I write x to the power of y, in python/ "**" is the operator you seek as in your case x**y >Similarly, how do I produce fractions?? Are you just wanting to divide? A normal float division uses "/" as its operator. If you are wanting to deal quite strictly with fractions, the standard library has a module devoted to that. The standard library also has a module devoted to handling decimals, where you can monitor the level of precision you need. See the official docs on the main Python website. -- Wishing you only the best, boB Stepp From robertvstepp at gmail.com Tue Feb 9 18:20:31 2021 From: robertvstepp at gmail.com (boB Stepp) Date: Tue, 9 Feb 2021 17:20:31 -0600 Subject: [Tutor] small graphics question In-Reply-To: References: Message-ID: On 21/02/09 10:36PM, nathan tech wrote: >I was originally going to ask this over on the wx python mailing list, >but I found myself hesitating. > >I am pondering a simple question: > >How do I write x to the power of y, in python/ > >Not 5^2 for 5 squared, but 5 superscript 2? or 5 superscript x where x >is any number? > >Similarly, how do I produce fractions?? > >I asked here in the end because I figure for wx python it is just >tracking down that character sequence and then writing it on screen >(which I can do!) I just realized I may not be answering the correct questions! Are you really asking how to display superscripts, fractions, etc., say in a print function? If you know the Unicode code point for the symbol you wish to generate you can use the chr() function. But this sounds like you are going to quickly get into font size issues and placements in the case of fractions. Surely wxPython has ways of dealing with these? Sorry about the earlier response. Didn't mean to underestimate you! -- Wishing you only the best, boB Stepp From PyTutor at DancesWithMice.info Tue Feb 9 18:27:47 2021 From: PyTutor at DancesWithMice.info (dn) Date: Wed, 10 Feb 2021 12:27:47 +1300 Subject: [Tutor] small graphics question In-Reply-To: References: Message-ID: <5d60a33a-da8e-14d8-c9ea-737045b6af7e@DancesWithMice.info> On 10/02/2021 11.36, nathan tech wrote: ... > How do I write x to the power of y, in python/ > > Not 5^2 for 5 squared, but 5 superscript 2? or 5 superscript x where x > is any number? > > Similarly, how do I produce fractions?? > > I asked here in the end because I figure for wx python it is just > tracking down that character sequence and then writing it on screen > (which I can do!) Interestingly, I started-out puzzling your question because Thunderbird (email client) displayed the "Not 5..." with the power as a superscript. It was only when I hit Reply that I saw the text as "Not 5 circumflex 2". Can only guess what you will see (above). Please clarify "write" and "produce". Do you want to know: - how to compute numbers raised to some power, and - how to compute values expressed as numerator and denominator (fractions) or - how to display formulae (including powers and/or fractions) as text in a convenient manner -- Regards, =dn From breamoreboy at gmail.com Tue Feb 9 18:05:35 2021 From: breamoreboy at gmail.com (Mark Lawrence) Date: Tue, 9 Feb 2021 23:05:35 +0000 Subject: [Tutor] small graphics question In-Reply-To: References: Message-ID: <853f5719-39ca-f1e3-be54-0efdb1675011@gmail.com> On 09/02/2021 22:36, nathan tech wrote: > Hi Folks, > > > I was originally going to ask this over on the wx python mailing list, > but I found myself hesitating. > > I am pondering a simple question: > > How do I write x to the power of y, in python/ > > Not 5^2 for 5 squared, but 5 superscript 2? or 5 superscript x where x > is any number? > > Similarly, how do I produce fractions?? > > I asked here in the end because I figure for wx python it is just > tracking down that character sequence and then writing it on screen > (which I can do!) > > Thanks in advance, > > Nathan > Please see this list https://docs.python.org/3/library/stdtypes.html#index-13 which I think answers both of your questions. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From nathan-tech at hotmail.com Tue Feb 9 20:01:03 2021 From: nathan-tech at hotmail.com (nathan tech) Date: Wed, 10 Feb 2021 01:01:03 +0000 Subject: [Tutor] small graphics question In-Reply-To: <5d60a33a-da8e-14d8-c9ea-737045b6af7e@DancesWithMice.info> References: <5d60a33a-da8e-14d8-c9ea-737045b6af7e@DancesWithMice.info> Message-ID: Hi everyone! Gosh! so sorry, I didn't realise my mail client had automatically switched over to the power to actually look superscript! So, to clarify and answer questions: 1. I do, in fact, know how to work out powers and fractions in python, (though thanks for the tips!) :) 2. Yes, what I am actually asking is, how do I go about displaying formulae styled strings in something such as the print function. that is to say, for instance, print("2/3") should come out as 2, with a line and the 3 under it. Similarly 2 to the power of 3, should come out as 2 superscript 3. Relatedly I need to make it variable, so the 2 superscript 3 could be 5 superscript 293, which is why I'm not just finding the characters on google and substituting in. I hope this helps! Side note: I, myself, and blind and that may also contribute to why I didn't notice the mail client turning the characters properly and why my descriptions of fractions is a bit wonkey, sorry in advance :) Nathan On 09/02/2021 23:27, dn via Tutor wrote: > On 10/02/2021 11.36, nathan tech wrote: > ... > >> How do I write x to the power of y, in python/ >> >> Not 5^2 for 5 squared, but 5 superscript 2? or 5 superscript x where x >> is any number? >> >> Similarly, how do I produce fractions?? >> >> I asked here in the end because I figure for wx python it is just >> tracking down that character sequence and then writing it on screen >> (which I can do!) > > Interestingly, I started-out puzzling your question because Thunderbird > (email client) displayed the "Not 5..." with the power as a superscript. > It was only when I hit Reply that I saw the text as "Not 5 circumflex > 2". Can only guess what you will see (above). > > Please clarify "write" and "produce". Do you want to know: > - how to compute numbers raised to some power, and > - how to compute values expressed as numerator and denominator (fractions) > or > - how to display formulae (including powers and/or fractions) as text in > a convenient manner > From alan.gauld at yahoo.co.uk Tue Feb 9 20:09:37 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 10 Feb 2021 01:09:37 +0000 Subject: [Tutor] small graphics question In-Reply-To: References: Message-ID: On 09/02/2021 22:36, nathan tech wrote: > I am pondering a simple question: > > How do I write x to the power of y, in python/ Its not simple, its extremely difficult! The reason is it depends on many factors including choice of font and OS. Being wxPython the OS issues are hopefully dealt with there. But the font issues remain. > Not 5^2 for 5 squared, but 5 superscript 2? or 5 superscript x where x > is any number? > > Similarly, how do I produce fractions?? This is all about producing smaller characters and then raising or lowering them above the baseline. The easiest way may be to create an HTML text window and use HTML and CSS to create the effects you want. HTML can certainly do super and sub scripts > I asked here in the end because I figure for wx python it is just > tracking down that character sequence and then writing it on screen Not that simple and the wxPython crowd probably have more people who do this kind of thing regularly. -- 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 Tue Feb 9 20:41:39 2021 From: nathan-tech at hotmail.com (nathan tech) Date: Wed, 10 Feb 2021 01:41:39 +0000 Subject: [Tutor] small graphics question In-Reply-To: References: Message-ID: Hi Alan, thanks for your clarifications here I genuinely was not sure. I didn't want to go over there only to have them be like "laud, this guy. It's so obvious!" I'll hop over and see what they have to say. V best Nathan On 10/02/2021 01:09, Alan Gauld via Tutor wrote: > On 09/02/2021 22:36, nathan tech wrote: > >> I am pondering a simple question: >> >> How do I write x to the power of y, in python/ > Its not simple, its extremely difficult! > > The reason is it depends on many factors including > choice of font and OS. Being wxPython the OS issues > are hopefully dealt with there. But the font > issues remain. > >> Not 5^2 for 5 squared, but 5 superscript 2? or 5 superscript x where x >> is any number? >> >> Similarly, how do I produce fractions?? > This is all about producing smaller characters and > then raising or lowering them above the baseline. > > The easiest way may be to create an HTML text window > and use HTML and CSS to create the effects you want. > HTML can certainly do super and sub scripts > >> I asked here in the end because I figure for wx python it is just >> tracking down that character sequence and then writing it on screen > Not that simple and the wxPython crowd probably have > more people who do this kind of thing regularly. > > From alan.gauld at yahoo.co.uk Tue Feb 9 20:41:54 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 10 Feb 2021 01:41:54 +0000 Subject: [Tutor] small graphics question In-Reply-To: References: <5d60a33a-da8e-14d8-c9ea-737045b6af7e@DancesWithMice.info> Message-ID: On 10/02/2021 01:01, nathan tech wrote: > 2. Yes, what I am actually asking is, how do I go about displaying > formulae styled strings in something such as the print function. > > that is to say, for instance, print("2/3") should come out as 2, with a > line and the 3 under it. For fractions there are some fonts with characters that represent the common fractions. But they are very limited - half,third,quarters etc. > Similarly 2 to the power of 3, should come out as 2 superscript 3. Again some fonts offer squared and possibly cubed but you won;t get a simple general solution that way, you really need to look at switching font settings on a character by character basis. I still think the HTMLtext widget may be he easiest way to go. -- 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 Feb 10 04:13:57 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 10 Feb 2021 09:13:57 +0000 Subject: [Tutor] small graphics question In-Reply-To: References: <5d60a33a-da8e-14d8-c9ea-737045b6af7e@DancesWithMice.info> Message-ID: On 10/02/2021 01:41, Alan Gauld via Tutor wrote: > For fractions there are some fonts with characters that represent the > common fractions. But they are very limited - half,third,quarters etc. It just struck me that the darker corners of unicode might be worth investigating. There are code points for some pretty obscure things and a full range of super and subscript characters might well be in there. If that's the case then you could indeed just create a string with the unicode chars and the Text widget should display them. Just a thought. -- 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 mhysnm1964 at gmail.com Wed Feb 10 05:47:23 2021 From: mhysnm1964 at gmail.com (mhysnm1964 at gmail.com) Date: Wed, 10 Feb 2021 21:47:23 +1100 Subject: [Tutor] Unknown encoded file types. In-Reply-To: References: <5b3a88dc-4895-9305-20b0-dec67da7f430@DancesWithMice.info> Message-ID: <018e01d6ff9a$1f5774c0$5e065e40$@gmail.com> All, Thank you for your assistance. After doing more investigation. There is some unusual characters in the files which look like French or similar languages. So I will play with your kind code samples and libraries to see what is being used. Sean -----Original Message----- From: Tutor On Behalf Of Cameron Simpson Sent: Monday, 8 February 2021 9:25 AM To: tutor at python.org Subject: Re: [Tutor] Unknown encoded file types. On 08Feb2021 07:54, DL Neil wrote: >On 08/02/2021 02.07, Alan Gauld via Tutor wrote: >> The good news is that there is a finite number of such encodings and >> its not impossible, given you have a relatively small set of files(in >> computer terms) you could just try each decoding in turn and display >> say the first 10 lines for all that don't give an error. That would >> let you select what looks like the best for each file. >> >> encodings = ['ascii','utf8','utf16', 'latin8539', 'latin..... etc] >> >> for file in files: >> for encoding in encodings >> try: >> decode file >> display filename + encoding + 10 lines >> except: continue >> else: print " no encoding worked for ' + file >> >> Just a thought. > > >Whilst @Mark follows the above post with a relatively-automated >suggestion (will still require manual inspection), I was going to >suggest something similar to this idea of a test-bed routine ranging >across a set of 'likely' encodings until you found happiness. > >This because you must surely have some hint of an idea of the source of >the files, eg if they have come from a German, Danish, ... user. >Accordingly, running through a range of the ISO 8859 variants which >were employed by MS-Win OpSys may yield one choice which works without >error. Aye. We've got an importer for some CSV data in a current project with exactly that problem, and exactly that heuristic: for encoding in 'utf-8', 'windows-1252', 'cp932': try: return line.decode(encoding) except UnicodeDecodeError: pass warning( '%r, line %d: cannot decode line %r, falling back to iso8859-1', self.filename, self.lineno, line ) return line.decode('iso8859-1') The choice of encodings above is entirely parochial to our source data, and still hits the fallback. Sean, note that in the above code: Successful decoding DOES NOT mean the correct encoding was used, as various byte sequences can decode in multiple encodings, yeilding different outcomes. See: https://en.wikipedia.org/wiki/Mojibake The fallback above ('iso8859-1', ISO Latin 1) is an 8-bit encoding where all bytes are 1-to-1 with the target ordinal. Like _any_ of the ISO8859 character sets, it will _always_ decode successfully because every byte is accepted. That doesn't mean it is correct. Cheers, Cameron Simpson _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From __peter__ at web.de Wed Feb 10 06:01:28 2021 From: __peter__ at web.de (Peter Otten) Date: Wed, 10 Feb 2021 12:01:28 +0100 Subject: [Tutor] small graphics question In-Reply-To: References: Message-ID: <7baf139f-079b-41cc-1b2d-de8e36c55d84@web.de> On 09/02/2021 23:36, nathan tech wrote: > Hi Folks, > > > I was originally going to ask this over on the wx python mailing list, > but I found myself hesitating. In such cases my preferred tool is a search engine... > I am pondering a simple question: > > How do I write x to the power of y, in python/ > > Not 5^2 for 5 squared, but 5 superscript 2? or 5 superscript x where x > is any number? ...which found me an example for the rich text here: https://stackoverflow.com/questions/53156000/superscript-in-wxpython-richtextctrl I also verified that unicode superscript codepoints are displayed correctly by changing the line st = wx.StaticText(pnl, label="Hello World!") in helloworld2.py on https://www.wxpython.org/pages/overview/#hello-world to st = wx.StaticText( pnl, label= "Hello World!\N{SUPERSCRIPT ONE}" "\N{SUPERSCRIPT TWO}\N{SUPERSCRIPT THREE}" ) > Similarly, how do I produce fractions?? > > I asked here in the end because I figure for wx python it is just > tracking down that character sequence and then writing it on screen > (which I can do!) > > Thanks in advance, > > Nathan > > _______________________________________________ > Tutor maillist? -? Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From __peter__ at web.de Wed Feb 10 06:01:28 2021 From: __peter__ at web.de (Peter Otten) Date: Wed, 10 Feb 2021 12:01:28 +0100 Subject: [Tutor] small graphics question In-Reply-To: References: Message-ID: <7baf139f-079b-41cc-1b2d-de8e36c55d84@web.de> On 09/02/2021 23:36, nathan tech wrote: > Hi Folks, > > > I was originally going to ask this over on the wx python mailing list, > but I found myself hesitating. In such cases my preferred tool is a search engine... > I am pondering a simple question: > > How do I write x to the power of y, in python/ > > Not 5^2 for 5 squared, but 5 superscript 2? or 5 superscript x where x > is any number? ...which found me an example for the rich text here: https://stackoverflow.com/questions/53156000/superscript-in-wxpython-richtextctrl I also verified that unicode superscript codepoints are displayed correctly by changing the line st = wx.StaticText(pnl, label="Hello World!") in helloworld2.py on https://www.wxpython.org/pages/overview/#hello-world to st = wx.StaticText( pnl, label= "Hello World!\N{SUPERSCRIPT ONE}" "\N{SUPERSCRIPT TWO}\N{SUPERSCRIPT THREE}" ) > Similarly, how do I produce fractions?? > > I asked here in the end because I figure for wx python it is just > tracking down that character sequence and then writing it on screen > (which I can do!) > > Thanks in advance, > > Nathan > > _______________________________________________ > Tutor maillist? -? Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From rudy at matela.com.br Wed Feb 10 09:10:59 2021 From: rudy at matela.com.br (Rudy Matela) Date: Wed, 10 Feb 2021 11:10:59 -0300 Subject: [Tutor] small graphics question In-Reply-To: References: Message-ID: Hi Nathan, Maybe this is not exactly what you are looking for, but maybe you can use Unicode characters for that: * superscripts : x?, x?, x?, x?, x?, x?, x?, x?, x?, x?, for example: 2?? * vulgar fractions : ?, ?, ?, ?, ?, etc... Since Python supports Unicode (and possibly wx python), you can use these in regular Python strings: $ python >>> print("2?? + ?") 2?? + ? Best Regards, Rudy On Tue, Feb 9, 2021 at 7:37 PM nathan tech wrote: > Hi Folks, > > > I was originally going to ask this over on the wx python mailing list, > but I found myself hesitating. > > I am pondering a simple question: > > How do I write x to the power of y, in python/ > > Not 5^2 for 5 squared, but 5 superscript 2? or 5 superscript x where x > is any number? > > Similarly, how do I produce fractions?? > > I asked here in the end because I figure for wx python it is just > tracking down that character sequence and then writing it on screen > (which I can do!) > > Thanks in advance, > > Nathan > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From PyTutor at DancesWithMice.info Wed Feb 10 16:23:56 2021 From: PyTutor at DancesWithMice.info (dn) Date: Thu, 11 Feb 2021 10:23:56 +1300 Subject: [Tutor] small graphics question In-Reply-To: References: <5d60a33a-da8e-14d8-c9ea-737045b6af7e@DancesWithMice.info> Message-ID: <653499d7-a6fe-8daa-8bce-d96416cd9e10@DancesWithMice.info> On 10/02/2021 14.01, nathan tech wrote: > Gosh! so sorry, I didn't realise my mail client had automatically > switched over to the power to actually look superscript! Unlikely that it was you - some email packages/web-page presentations have a mind of their own! > So, to clarify and answer questions: > > 1. I do, in fact, know how to work out powers and fractions in python, > (though thanks for the tips!) :) > > 2. Yes, what I am actually asking is, how do I go about displaying > formulae styled strings in something such as the print function. > > that is to say, for instance, print("2/3") should come out as 2, with a > line and the 3 under it. > > Similarly 2 to the power of 3, should come out as 2 superscript 3. > > Relatedly I need to make it variable, so the 2 superscript 3 could be 5 > superscript 293, which is why I'm not just finding the characters on > google and substituting in. If the result can be computed as a string (or graphic) for printing, it can be a Python object (variable). > I hope this helps! > > Side note: I, myself, and blind and that may also contribute to why I > didn't notice the mail client turning the characters properly and why my > descriptions of fractions is a bit wonkey, sorry in advance :) Which brings me to ask (apologies for nosiness): using sub-/super-scripts and fractions, which must fit within the current line-height, must surely be more difficult to read - so why do it (that way)? Extending further, a suggestion to look at Tex/Latex (which was apparently only sent to me, cf entire list) looked likely. One reason that package was invented (by a ComSc 'luminary' no-less!) was to enable the printing of math formulae/formulas in traditional text-book and math notation instead of the numerator/denominator format forced upon us by typewriters. However, is it possible to combine wxPython with Tex? NB to continue to read beyond this point illustrates why top-posting is 'frowned upon' by the list, ie the logic of a question-then-answer sequence in this type of forum > On 09/02/2021 23:27, dn via Tutor wrote: >> On 10/02/2021 11.36, nathan tech wrote: >> ... >> >>> How do I write x to the power of y, in python/ >>> >>> Not 5^2 for 5 squared, but 5 superscript 2? or 5 superscript x where x >>> is any number? >>> >>> Similarly, how do I produce fractions?? >>> >>> I asked here in the end because I figure for wx python it is just >>> tracking down that character sequence and then writing it on screen >>> (which I can do!) >> >> Interestingly, I started-out puzzling your question because Thunderbird >> (email client) displayed the "Not 5..." with the power as a superscript. >> It was only when I hit Reply that I saw the text as "Not 5 circumflex >> 2". Can only guess what you will see (above). >> >> Please clarify "write" and "produce". Do you want to know: >> - how to compute numbers raised to some power, and >> - how to compute values expressed as numerator and denominator >> (fractions) >> or >> - how to display formulae (including powers and/or fractions) as text in >> a convenient manner >> > _______________________________________________ > Tutor maillist? -? Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor -- Regards, =dn From PyTutor at DancesWithMice.info Wed Feb 10 17:05:16 2021 From: PyTutor at DancesWithMice.info (dn) Date: Thu, 11 Feb 2021 11:05:16 +1300 Subject: [Tutor] Unknown encoded file types. In-Reply-To: <018e01d6ff9a$1f5774c0$5e065e40$@gmail.com> References: <5b3a88dc-4895-9305-20b0-dec67da7f430@DancesWithMice.info> <018e01d6ff9a$1f5774c0$5e065e40$@gmail.com> Message-ID: <3136a0c7-8d83-cb04-f9c1-28a02a4dc0fc@DancesWithMice.info> On 10/02/2021 23.47, mhysnm1964 at gmail.com wrote: > All, > > Thank you for your assistance. After doing more investigation. There is some > unusual characters in the files which look like French or similar languages. > So I will play with your kind code samples and libraries to see what is > being used. Which leads this non-MS-Win user to ask: if UTF-8/standard Python has 'trouble' understanding files originally created in ISO 8859-n locale, eg MS-Win's Latin-1 etc; given MSFT's assumptions of eco-system/world-domination, does today's version of Win10 enjoy suitable backwards-compatibility and thus have no difficulties, eg a FileManager listing file-names or a NotePad displaying text-content? (is a/the solution to 'upgrade' to UTF-8 'there', and thereafter Python's I/O will perform without incident) -- Regards, =dn From adameyring at gmail.com Wed Feb 10 17:13:06 2021 From: adameyring at gmail.com (Adam Eyring) Date: Wed, 10 Feb 2021 17:13:06 -0500 Subject: [Tutor] (no subject) In-Reply-To: <90020f1a-a4bb-5444-9cc5-146630663d72@DancesWithMice.info> References: <90020f1a-a4bb-5444-9cc5-146630663d72@DancesWithMice.info> Message-ID: You might want to check this group of tutorials on projects with the Raspberry pi. https://projects.raspberrypi.org/en AME On Tue, Feb 9, 2021 at 2:51 PM dn via Tutor wrote: > On 10/02/2021 04.30, ken c wrote: > > Hello Ladies & Gentlemen, > > > > I am wanting to build a MP3 player using raspberry pi as a base, using a > portable hard drive to hold the music, etc. > > I want to be able to access music, short stories, audio books, old time > radio and possibly movies. To make matters a little more complicated, I > have to do a lot of traveling and would like to be able to use this in a > vehicle instead of listening to the inane comments of the DJs and the > commercials. However, I am not in the best of health and know nothing about > how to build the software in order to make this happen. > > > Hello Kenny, and welcome to the list. > > We're happy to help people along their Python journey. > > Perhaps you could re-phrase the above as a specific question: > - are you offering to pay someone to build such a device? > - do you want to learn how to code in Python? > - do you want to learn how to access an MP3 player from Python? > - do you have the various battery/power-supply issues sorted-out? > - etc/other? > -- > Regards, > =dn > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From alan.gauld at yahoo.co.uk Wed Feb 10 20:02:46 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 11 Feb 2021 01:02:46 +0000 Subject: [Tutor] small graphics question In-Reply-To: <653499d7-a6fe-8daa-8bce-d96416cd9e10@DancesWithMice.info> References: <5d60a33a-da8e-14d8-c9ea-737045b6af7e@DancesWithMice.info> <653499d7-a6fe-8daa-8bce-d96416cd9e10@DancesWithMice.info> Message-ID: On 10/02/2021 21:23, dn via Tutor wrote: > Extending further, a suggestion to look at Tex/Latex (which was > apparently only sent to me, I don't know of any LateX views in wxPython. But thats similar to my suggestion to use HTML which does support super and subscript characters via markup and wxPython does have an HTMLtext view component. But if unicode has code-points for all of the numbers as superscript/subscript then it would be possible to just output the unicode characters when needed. Whether unicode code points or HTML markup is most readable is a moot point! -- 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 Feb 10 20:10:14 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 11 Feb 2021 01:10:14 +0000 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: On 09/02/2021 15:30, ken c wrote: > I am wanting to build a MP3 player using raspberry pi as a base, > using a portable hard drive to hold the music, etc. That is certainly doable but ambitious, especially if its the fist such project you have tackled! > I want to be able to access music, short stories, audio books, So far they are all just audio files so no issue. > ...old time radio and possibly movies. I don;t know what you mea by old time radio - is it recordings or do you mean listen to live AM/FM broadcasts? If the latter its a whole new ballgame! Still possible but significantly different to the other options. Movies would presumably just be the soundtrack or are you planning on adding a screen too? If just the soundtrack things are still doable. > know nothing about how to build the software in order to make this happen. We can help with the Python parts but most of the hard work will be best done using one or other of the command-line audio players available on Linux (the Pi OS). Your Python code will comprise a UI and some calls to control the player software. There are some excellent raspberryPi forums that will be happy to help with the hardware (and software for that matter!) If you find their advice too complex come back here and we can try to decipher it for you. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From Richard at Damon-Family.org Wed Feb 10 22:26:38 2021 From: Richard at Damon-Family.org (Richard Damon) Date: Wed, 10 Feb 2021 22:26:38 -0500 Subject: [Tutor] Unknown encoded file types. In-Reply-To: <3136a0c7-8d83-cb04-f9c1-28a02a4dc0fc@DancesWithMice.info> References: <5b3a88dc-4895-9305-20b0-dec67da7f430@DancesWithMice.info> <018e01d6ff9a$1f5774c0$5e065e40$@gmail.com> <3136a0c7-8d83-cb04-f9c1-28a02a4dc0fc@DancesWithMice.info> Message-ID: <65e4e817-d77a-844a-e938-0b43d0127da7@Damon-Family.org> On 2/10/21 5:05 PM, dn via Tutor wrote: > On 10/02/2021 23.47, mhysnm1964 at gmail.com wrote: >> All, >> >> Thank you for your assistance. After doing more investigation. There is some >> unusual characters in the files which look like French or similar languages. >> So I will play with your kind code samples and libraries to see what is >> being used. > > Which leads this non-MS-Win user to ask: > > if UTF-8/standard Python has 'trouble' understanding files originally > created in ISO 8859-n locale, eg MS-Win's Latin-1 etc; given MSFT's > assumptions of eco-system/world-domination, does today's version of > Win10 enjoy suitable backwards-compatibility and thus have no > difficulties, eg a FileManager listing file-names or a NotePad > displaying text-content? > > (is a/the solution to 'upgrade' to UTF-8 'there', and thereafter > Python's I/O will perform without incident) One note about the Windows file system names. File names come in two 'flavors', short file names, with a fixed 8.3 format, which are always stored in the system default 8 bit code page. Then there are the 'Long File Names' which can be basically arbitrarily long and are always stored in UTF-16 (originally UCS-2). If a file has both a long name and a short name the short name will be hidden from the user. Note that because the short file names use the system encoding, moving a removable media from one machine to another using a very different code page, can give some strange short file names. The place you have the issue is looking at the CONTENTS of the file, where unless the file format somehow encodes the file encoding format, you have the issue that you need to guess to figure out which 8 bit code page a file was made with (and if it was). This issue is more of a problem on Windows, because Windows roots go back significantly farther (especially in international markets) and thus has more legacy issues. The *nix world had the advantage of going international later, and at that point UTF-8 was a good option and got around the code page encoding issues. -- Richard Damon From breamoreboy at gmail.com Wed Feb 10 23:22:07 2021 From: breamoreboy at gmail.com (Mark Lawrence) Date: Thu, 11 Feb 2021 04:22:07 +0000 Subject: [Tutor] Unknown encoded file types. In-Reply-To: <3136a0c7-8d83-cb04-f9c1-28a02a4dc0fc@DancesWithMice.info> References: <5b3a88dc-4895-9305-20b0-dec67da7f430@DancesWithMice.info> <018e01d6ff9a$1f5774c0$5e065e40$@gmail.com> <3136a0c7-8d83-cb04-f9c1-28a02a4dc0fc@DancesWithMice.info> Message-ID: <3666bf07-dc1b-1068-cde4-60a77d80b8f0@gmail.com> On 10/02/2021 22:05, dn via Tutor wrote: > On 10/02/2021 23.47, mhysnm1964 at gmail.com wrote: >> All, >> >> Thank you for your assistance. After doing more investigation. There is some >> unusual characters in the files which look like French or similar languages. >> So I will play with your kind code samples and libraries to see what is >> being used. > > > Which leads this non-MS-Win user to ask: > > if UTF-8/standard Python has 'trouble' understanding files originally > created in ISO 8859-n locale, eg MS-Win's Latin-1 etc; given MSFT's > assumptions of eco-system/world-domination, does today's version of > Win10 enjoy suitable backwards-compatibility and thus have no > difficulties, eg a FileManager listing file-names or a NotePad > displaying text-content? > > (is a/the solution to 'upgrade' to UTF-8 'there', and thereafter > Python's I/O will perform without incident) > There's some interesting debate about https://www.python.org/dev/peps/pep-0597/ -- Add optional EncodingWarning here https://mail.python.org/archives/list/python-dev at python.org/thread/SFYUP2TWD5JZ5KDLVSTZ44GWKVY4YNCV/ if anybody is interested. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From tcm2118 at columbia.edu Wed Feb 10 20:29:23 2021 From: tcm2118 at columbia.edu (Tristin Cara Moone) Date: Wed, 10 Feb 2021 18:29:23 -0700 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: i see On Tue, Feb 9, 2021 at 12:07 PM ken c wrote: > Hello Ladies & Gentlemen, > > I am wanting to build a MP3 player using raspberry pi as a base, using a > portable hard drive to hold the music, etc. > I want to be able to access music, short stories, audio books, old time > radio and possibly movies. To make matters a little more complicated, I > have to do a lot of traveling and would like to be able to use this in a > vehicle instead of listening to the inane comments of the DJs and the > commercials. However, I am not in the best of health and know nothing about > how to build the software in order to make this happen. > > Thanks for reading this rant, > Kenny > > Sent from Mail for > Windows 10 > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- 1 [505] 701-9323 From alan.gauld at yahoo.co.uk Thu Feb 11 04:46:17 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 11 Feb 2021 09:46:17 +0000 Subject: [Tutor] Unknown encoded file types. In-Reply-To: <65e4e817-d77a-844a-e938-0b43d0127da7@Damon-Family.org> References: <5b3a88dc-4895-9305-20b0-dec67da7f430@DancesWithMice.info> <018e01d6ff9a$1f5774c0$5e065e40$@gmail.com> <3136a0c7-8d83-cb04-f9c1-28a02a4dc0fc@DancesWithMice.info> <65e4e817-d77a-844a-e938-0b43d0127da7@Damon-Family.org> Message-ID: On 11/02/2021 03:26, Richard Damon wrote: > This issue is more of a problem on Windows, because Windows roots go > back significantly farther (especially in international markets) and > thus has more legacy issues. The *nix world had the advantage of going > international later, and at that point UTF-8 was a good option and got > around the code page encoding issues. That's an interesting perspective but I'm struggling to see the logic? I'd agree that Windows tends to have a bigger legacy problem, but not because of its age but because it did nothing to mitigate the issue early on(in the MS DOS days). Rather it catered to a large number of local standards and even used those internally. But Unix was used all around the world in the late 70's and long before DOS even became an idea, let alone an established product. They just took a different approach to separating the concerns of representing international alphabets within the system as opposed to in user content - an approach based on decades of experience in that field as a telco (eg. In the international telex network). Both systems have identical issues in managing mixed encodings in their file content. Windows has a slightly bigger problem in managing mixed encodings in its filenames and other internal features. How much bigger is a moot point. And both utilise i18n for internationalization of software. -- 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 jamesmithen at googlemail.com Thu Feb 11 08:50:06 2021 From: jamesmithen at googlemail.com (James Mithen) Date: Thu, 11 Feb 2021 13:50:06 +0000 Subject: [Tutor] (no subject) Message-ID: Hi, Could I have some Python tutoring please. The last time I did it was quite a while ago and I've forgotten everything. Yours Sincerely James Mithen From rudy at matela.com.br Thu Feb 11 11:48:04 2021 From: rudy at matela.com.br (Rudy Matela) Date: Thu, 11 Feb 2021 13:48:04 -0300 Subject: [Tutor] Python exercises for beginners In-Reply-To: References: Message-ID: Hello members of the Python Tutor mailing list, I have been working on a website aimed at teaching programming and computer science through quick examples in the form of exercises. It is called "Computer Science by Example ": https://cscx.org/ The exercises start very simple (e.g. read two numbers and print their sum ) then increase in difficulty and complexity gradually (e.g. solving the change-making problem ). The website has an "online judge" functionality where students can submit their solutions which are tested and graded automatically without human intervention. It currently supports solutions in *Python* and additionally: C, C++, C#, Haskell, Java, JavaScript, Lua and Ruby. The exercises are useful not only to first time programmers, but also to experienced programmers migrating to using Python or other languages. Check it out at cscx.org. The exercises are freely available for anyone to try. Best Regards, Rudy From PyTutor at DancesWithMice.info Thu Feb 11 13:16:34 2021 From: PyTutor at DancesWithMice.info (dn) Date: Fri, 12 Feb 2021 07:16:34 +1300 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: <1ee34c81-7ec4-81fe-65a0-a9e5a2605db6@DancesWithMice.info> On 12/02/2021 02.50, James Mithen via Tutor wrote: > Hi, > > Could I have some Python tutoring please. > > The last time I did it was quite a while ago and I've forgotten everything. Hi and welcome to the list. List members are volunteers who stand ready to *help you* learn. If you experience difficulty please copy-paste relevant code and all error-messages, and several suggestions will likely revert... If you are looking for teaching, then aside from ($paid) private tutoring, I recommend looking at MOOCs available from the likes of edX, Coursera, et al. Our own ListAdmin also offers one from his web site. Many books provide a structured approach to learning the language. Others will suggest videos, eg YouTube. (without reference to the word "structured", as above) As you can see from this list, there are different ways that different people may prefer to learn... Disclaimer: I use the edX platform - but not for Python training. -- Regards, =dn From awatila at ieee.org Thu Feb 11 15:16:16 2021 From: awatila at ieee.org (awatila at ieee.org) Date: Thu, 11 Feb 2021 23:16:16 +0300 Subject: [Tutor] Python exercises for beginners In-Reply-To: References: Message-ID: <016901d700b2$c1c52360$454f6a20$@ieee.org> Thank you for the good resource Alex Watila, SMACM, MIEEE Publicity Co-Chair, IEEE PowerAfrica Conference (2021 & 2020) 2021 IEEE Collabratec Ambassador Chair, Professional Activities, Conferences and Events, IEEE Kenya Section -----Original Message----- From: Tutor On Behalf Of Rudy Matela Sent: Thursday, February 11, 2021 7:48 PM To: Python Tutor Subject: [Tutor] Python exercises for beginners Hello members of the Python Tutor mailing list, I have been working on a website aimed at teaching programming and computer science through quick examples in the form of exercises. It is called "Computer Science by Example ": https://cscx.org/ The exercises start very simple (e.g. read two numbers and print their sum ) then increase in difficulty and complexity gradually (e.g. solving the change-making problem ). The website has an "online judge" functionality where students can submit their solutions which are tested and graded automatically without human intervention. It currently supports solutions in *Python* and additionally: C, C++, C#, Haskell, Java, JavaScript, Lua and Ruby. The exercises are useful not only to first time programmers, but also to experienced programmers migrating to using Python or other languages. Check it out at cscx.org. The exercises are freely available for anyone to try. Best Regards, Rudy _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From mysecretrobotfactory at gmail.com Thu Feb 11 16:51:12 2021 From: mysecretrobotfactory at gmail.com (Chris C) Date: Thu, 11 Feb 2021 13:51:12 -0800 Subject: [Tutor] HackerRank practices? Message-ID: Hi, I am wondering if there is a website or something that works like Hackerrank. I want to do the challenges but I only know Python. Is there something like that, but in Python? Thanks From mats at wichmann.us Thu Feb 11 18:33:27 2021 From: mats at wichmann.us (Mats Wichmann) Date: Thu, 11 Feb 2021 16:33:27 -0700 Subject: [Tutor] HackerRank practices? In-Reply-To: References: Message-ID: <357f2406-81ad-c3c5-0eac-215a07a14ccf@wichmann.us> On 2/11/21 2:51 PM, Chris C wrote: > Hi, I am wondering if there is a website or something that works like > Hackerrank. > I want to do the challenges but I only know Python. > > Is there something like that, but in Python? Never used Hackerrank, but thought it let you code solutions in Python? The times I've looked at coding evaluation sites, I think all of them I've looked at have allowed solving in Python. PyBytes is a Python-focused site to solve problems, don't know if that fits your needs. From mysecretrobotfactory at gmail.com Thu Feb 11 19:10:27 2021 From: mysecretrobotfactory at gmail.com (Chris C) Date: Thu, 11 Feb 2021 16:10:27 -0800 Subject: [Tutor] HackerRank practices? In-Reply-To: <357f2406-81ad-c3c5-0eac-215a07a14ccf@wichmann.us> References: <357f2406-81ad-c3c5-0eac-215a07a14ccf@wichmann.us> Message-ID: my bad. I should have learned that Hacker rank has python. Sorry All. On Thu, Feb 11, 2021 at 3:34 PM Mats Wichmann wrote: > On 2/11/21 2:51 PM, Chris C wrote: > > Hi, I am wondering if there is a website or something that works like > > Hackerrank. > > I want to do the challenges but I only know Python. > > > > Is there something like that, but in Python? > > Never used Hackerrank, but thought it let you code solutions in Python? > > The times I've looked at coding evaluation sites, I think all of them > I've looked at have allowed solving in Python. > > PyBytes is a Python-focused site to solve problems, don't know if that > fits your needs. > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From thomas.anderson at little-beak.com Mon Feb 15 15:39:50 2021 From: thomas.anderson at little-beak.com (Thomas A. Anderson) Date: Mon, 15 Feb 2021 21:39:50 +0100 Subject: [Tutor] regex help for a noob Message-ID: Hello, I have a long file that has single characters I would like to be extracted, and added to a list in the end. The file has many lines, not all have the text I am looking for. The single characters I am looking for are nestled within a ("_"), i.e. parenthesis and double quote. I have tried the following code: import re def getlist(): """ creates a list from file """ list = [] dataload = open("/Users/drexl/Lyntin/sample.txt", "r") regExp = '\".*?\"' for line in dataload.readlines(): x = re.findall(regExp, line) if x: list.append(x) print list getlist() I get the desired result, more or less, slightly more on the less side =( I am getting this as a list output: [['"n"'], ['"n"'], ['"e"'], ['"w"'], ['"n"']] where I would like a more basic list: list = ['n', 'n', 'e', 'w', 'n'] I have tried various other regex expressions, but they only give me worse or the same results. So, I don't think it is regex related? But somewhere else, I am missing something? Thanks for the help in advance. From alan.gauld at yahoo.co.uk Mon Feb 15 17:47:55 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 15 Feb 2021 22:47:55 +0000 Subject: [Tutor] regex help for a noob In-Reply-To: References: Message-ID: There are several things to comment on here... On 15/02/2021 20:39, Thomas A. Anderson via Tutor wrote: > The single characters I am looking for are nestled within a ("_"), i.e. > parenthesis and double quote. > > I have tried the following code: > > > import re > > def getlist(): > """ creates a list from file """ list = [] > dataload = open("/Users/drexl/Lyntin/sample.txt", "r") Best Python practice says use a with statement for this: with open("/Users/drexl/Lyntin/sample.txt", "r") as dataload: That will ensue it gets closed again, even if you hit an exception. > regExp = '\".*?\"' This regex does not correspond to your specification. Where are the ()? I'd expect something like: regExp = "\(\"(.)\"|) # match any single char between (" and ")... You want to extract the bit inside the quotes so that's what the group (ie the (.) bit) will do. > for line in dataload.readlines(): You don't need the readlines() its better to use the file object as an iterator: for line in dataload: However I'm not sure you eben need to scan line by line, you could just read() the whole file and do it as a single search with findall()... But there may be data complications that preclude that... > x = re.findall(regExp, line) > if x: > list.append(x) findall() returns a list of found items. You are appending the whole list to your list. You probably want to add the lists together: list += x Also its very bad practice to use a type name for a variable. You have hidden the list() function so you can't now convert strings, say, to lists: Ls = list("abc") -> error because list is now an actual list. > I have tried various other regex expressions, but they only give me worse or the same results. > So, I don't think it is regex related? But somewhere else, I am missing something? You are mostly missing the fact that appending a list to a list puts the whole list into the containing list a = [1] b = [2] c = [] c.append(a) -> [[1]] c.append(b) -> [[1],[2]] But there's quite a few other things to tidy up 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 david at graniteweb.com Mon Feb 15 17:48:07 2021 From: david at graniteweb.com (David Rock) Date: Mon, 15 Feb 2021 16:48:07 -0600 Subject: [Tutor] regex help for a noob In-Reply-To: References: Message-ID: <20210215224807.GG17314@graniteweb.com> * Thomas A. Anderson via Tutor [2021-02-15 21:39]: > > import re > > def getlist(): > """ creates a list from file """ list = [] > dataload = open("/Users/drexl/Lyntin/sample.txt", "r") > regExp = '\".*?\"' for line in dataload.readlines(): > x = re.findall(regExp, line) > if x: > list.append(x) > > print list > > > getlist() > > I get the desired result, more or less, slightly more on the less side =( > > I am getting this as a list output: > [['"n"'], ['"n"'], ['"e"'], ['"w"'], ['"n"']] > > where I would like a more basic list: > list = ['n', 'n', 'e', 'w', 'n'] It sounds like you need to use a group in you regex: instead of: '\".*?\"' use: '\"(.*?)\"' Basically, if you put () around the part you want, it gets "grouped" and can be referenced later by index. re.findall will use groups if they are set: re.findall(pattern, string, flags=0) Return all non-overlapping matches of pattern in string, as a list of strings. The string is scanned left-to-right, and matches are returned in the order found. If one or more groups are present in the pattern, return a list of groups; this will be a list of tuples if the pattern has more than one group. Empty matches are included in the result. You may still end up with a list of lists, but I think that will get you closer to what you want. -- David Rock david at graniteweb.com From alexkleider at gmail.com Mon Feb 15 19:07:38 2021 From: alexkleider at gmail.com (Alex Kleider) Date: Mon, 15 Feb 2021 16:07:38 -0800 Subject: [Tutor] regex help for a noob In-Reply-To: References: Message-ID: Others have already provided solutions but no one has mentioned what for me is the best "goto" for python regex: https://docs.python.org/3/howto/regex.html It not only explains groups but also how to name them (a Python provided extra feature, I believe) which might be useful for you. On Mon, Feb 15, 2021 at 2:09 PM Thomas A. Anderson via Tutor < tutor at python.org> wrote: > Hello, > > I have a long file that has single characters I would like to be > extracted, and added to a list in the end. > > The file has many lines, not all have the text I am looking for. > > The single characters I am looking for are nestled within a ("_"), i.e. > parenthesis and double quote. > > I have tried the following code: > > > import re > > def getlist(): > """ creates a list from file """ list = [] > dataload = open("/Users/drexl/Lyntin/sample.txt", "r") > regExp = '\".*?\"' for line in dataload.readlines(): > x = re.findall(regExp, line) > if x: > list.append(x) > > print list > > > getlist() > > I get the desired result, more or less, slightly more on the less side =( > > I am getting this as a list output: > [['"n"'], ['"n"'], ['"e"'], ['"w"'], ['"n"']] > > where I would like a more basic list: > list = ['n', 'n', 'e', 'w', 'n'] > > I have tried various other regex expressions, but they only give me worse > or the same results. > So, I don't think it is regex related? But somewhere else, I am missing > something? > > Thanks for the help in advance. > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From thomas.anderson at little-beak.com Mon Feb 15 18:17:33 2021 From: thomas.anderson at little-beak.com (Thomas A. Anderson) Date: Tue, 16 Feb 2021 00:17:33 +0100 Subject: [Tutor] regex help for a noob In-Reply-To: References: Message-ID: Thanks for the reply. I love this! I have a lot to learn. After reading some, I figured I was making a list of a list, since findall creates a list. I was able to create a list comprehension to flatten the list, but would rather have a more pythonic way of clean code, so will examine everything again. On 15.02.21 23:47, Alan Gauld via Tutor wrote: > There are several things to comment on here... > > On 15/02/2021 20:39, Thomas A. Anderson via Tutor wrote: > >> The single characters I am looking for are nestled within a ("_"), i.e. >> parenthesis and double quote. >> >> I have tried the following code: >> >> >> import re >> >> def getlist(): >> """ creates a list from file """ > list = [] >> dataload = open("/Users/drexl/Lyntin/sample.txt", "r") > Best Python practice says use a with statement for this: > > with open("/Users/drexl/Lyntin/sample.txt", "r") as dataload: > > That will ensue it gets closed again, even if you hit an exception. > >> regExp = '\".*?\"' > This regex does not correspond to your specification. Where are the ()? > I'd expect something like: > > regExp = "\(\"(.)\"|) # match any single char between (" and ")... > > You want to extract the bit inside the quotes so that's > what the group (ie the (.) bit) will do. > >> for line in dataload.readlines(): > You don't need the readlines() its better to use the file > object as an iterator: > > for line in dataload: > > However I'm not sure you eben need to scan line by line, you > could just read() the whole file and do it as a single search > with findall()... But there may be data complications that > preclude that... > >> x = re.findall(regExp, line) >> if x: >> list.append(x) > findall() returns a list of found items. You are appending the whole > list to your list. You probably want to add the lists together: > > list += x > > Also its very bad practice to use a type name for a variable. You > have hidden the list() function so you can't now convert strings, > say, to lists: > > Ls = list("abc") -> error because list is now an actual list. > >> I have tried various other regex expressions, but they only give me worse or the same results. >> So, I don't think it is regex related? But somewhere else, I am missing something? > You are mostly missing the fact that appending a list to a > list puts the whole list into the containing list > > a = [1] > b = [2] > c = [] > c.append(a) -> [[1]] > c.append(b) -> [[1],[2]] > > But there's quite a few other things to tidy up too. > From thomas.anderson at little-beak.com Mon Feb 15 18:26:55 2021 From: thomas.anderson at little-beak.com (Thomas A. Anderson) Date: Tue, 16 Feb 2021 00:26:55 +0100 Subject: [Tutor] regex help for a noob In-Reply-To: <20210215224807.GG17314@graniteweb.com> References: <20210215224807.GG17314@graniteweb.com> Message-ID: <2d060d9c-f941-ed10-6d78-ef78c1d53ab3@little-beak.com> Thanks David and Alan! Now all working as desired. On 15.02.21 23:48, David Rock wrote: > * Thomas A. Anderson via Tutor [2021-02-15 21:39]: >> import re >> >> def getlist(): >> """ creates a list from file """ list = [] >> dataload = open("/Users/drexl/Lyntin/sample.txt", "r") >> regExp = '\".*?\"' for line in dataload.readlines(): >> x = re.findall(regExp, line) >> if x: >> list.append(x) >> >> print list >> >> >> getlist() >> >> I get the desired result, more or less, slightly more on the less side =( >> >> I am getting this as a list output: >> [['"n"'], ['"n"'], ['"e"'], ['"w"'], ['"n"']] >> >> where I would like a more basic list: >> list = ['n', 'n', 'e', 'w', 'n'] > It sounds like you need to use a group in you regex: > instead of: '\".*?\"' > use: '\"(.*?)\"' > > Basically, if you put () around the part you want, it gets "grouped" and can be referenced later by index. > re.findall will use groups if they are set: > > > re.findall(pattern, string, flags=0) > Return all non-overlapping matches of > pattern in string, as a list of strings. The string is scanned left-to-right, > and matches are returned in the order found. If one or more groups are present > in the pattern, return a list of groups; this will be a list of tuples if the > pattern has more than one group. Empty matches are included in the result. > > You may still end up with a list of lists, but I think that will get you closer to what you want. > From singh000taran at gmail.com Tue Feb 16 05:43:09 2021 From: singh000taran at gmail.com (=?UTF-8?B?4Kmn?=) Date: Tue, 16 Feb 2021 16:13:09 +0530 Subject: [Tutor] (no subject) Message-ID: I have started learning Python recently. Please help me with this. Create a dictionary where all the keys are strings, and all the values are integers. For example: {'Monday':19,'Tuesday':20} Write the dictionary on a single line, don't assign a variable name to the dictionary. From alan.gauld at yahoo.co.uk Tue Feb 16 11:39:20 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 16 Feb 2021 16:39:20 +0000 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: On 16/02/2021 10:43, ? wrote: > I have started learning Python recently. > Please help me with this. > > Create a dictionary where all the keys are strings, and all the values are > integers. > For example: {'Monday':19,'Tuesday':20} > Write the dictionary on a single line, don't assign a variable name to the > dictionary. What exactly do you need help with? You seem to have successfully solved the problem in your request. Having said that, writing it on a single line without assignment is only useful (and even then, not very) within the interactive interpreter. It's considered a bug in any real program. -- 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 david at graniteweb.com Tue Feb 16 11:57:38 2021 From: david at graniteweb.com (David Rock) Date: Tue, 16 Feb 2021 10:57:38 -0600 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: <20210216165738.GB28879@graniteweb.com> * Alan Gauld via Tutor [2021-02-16 16:39]: > On 16/02/2021 10:43, ? wrote: > > I have started learning Python recently. > > Please help me with this. > > > > Create a dictionary where all the keys are strings, and all the values are > > integers. > > For example: {'Monday':19,'Tuesday':20} > > Write the dictionary on a single line, don't assign a variable name to the > > dictionary. > > What exactly do you need help with? > You seem to have successfully solved the problem in your request. To elaborate: Are you expected to take a set of values and build the dictionary? Are you expected to just write a dictionary (which you've already done, as Alan pointed out)? With the statement having an already-correct example, the shortest path is to replicate it with different values. If we look at the example: {'Monday':19,'Tuesday':20} this is made up of {'string1': number1, 'string2': number2} Strings in this context are characters inside quotes; 'string1' ,'string2', 'Monday', 'Tuesday', etc integers are just real, whole numbers with no quotes: 19, 20, 34, -1, etc So it sounds like you just need to write any dictionary that follows the above format. -- David Rock david at graniteweb.com From learn2program at gmail.com Tue Feb 16 12:52:22 2021 From: learn2program at gmail.com (Alan Gauld) Date: Tue, 16 Feb 2021 17:52:22 +0000 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: <162e72b0-4859-8960-0cb8-c9cbfd90764f@yahoo.co.uk> Please always use ReplyAll (or reply List if you have that option) on the tutor list because otherwise it only goes to the person to whom you are replying. On 16/02/2021 17:01, ? wrote: > I have tried > > string = "Monday - 19, Tuesday - 20" > Dict = dict((x.strip(), int(y.strip())) ? > ? ? ? ? ? ? ?for x, y in (element.split('-') ? > ? ? ? ? ? ? ?for element in string.split(', '))) > print(Dict) > ? > and > > string = "{'Monday': 19, 'Tuesday':20}" > Dict = eval(string) > print(Dict) > > Both give error You are doing far more than you were asked and making life extremely complex. All you had to do was write one single line defining a dictionary. See davbids reply to your original mail. Also, please don't use eval() its very insecure and hardly ever needed even by experts. -- 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 dimitarxivanov at gmail.com Wed Feb 17 06:43:11 2021 From: dimitarxivanov at gmail.com (Dimitar Ivanov) Date: Wed, 17 Feb 2021 11:43:11 +0000 Subject: [Tutor] Trying to understand how object re-instantiation gets propagated Message-ID: Hi all, Apologies if this isn't the correct place to ask, I'm running into a rather curious situation that I'm trying to fully grasp and so far my logic is failing me so I hope maybe you could help clear up my confusion. :) I have a small Python program that works with JIRA issues, I've created my own class that works on top of the Python jira package as means to have easier access to the fields I require. While this setup isn't directly related to my question and I suspect you could easily recreate my situation with any OOP setup, I feel like it's necessary to mention what I'm doing. My JIRA issue class (shown below) retrieves the values from a JIRA server and saves them as instance variables. To avoid outdated information when I update a value on the JIRA server, I made a small function that retrieves the JIRA issue from the server again and re-instantiates a new object of my class: def main(): jira_issue = jira_connection.issue("ISSUE-1") # Retrieving the issue from the JIRA server my_issue = JiraIssue(jira_issue) print(my_issue.assignee) # Output is "Unassigned" # Go in JIRA and change the assignee to 'Foobar' now my_issue.reload() print(my_issue.assignee) # Output now is "Foobar" class JiraIssue: def __init__(self, jira_issue): self.key = issue.key self.assignee = issue.fields.assignee def reload(self): jira_issue = jira_connection.issue(self.key) # Retrieving the issue from the JIRA server issue = JiraIssue.__init__(self, jira_issue) return issue At this point, my logic tells me that once I have invoked 'my_issue.reload()', I should have assigned its return value to a variable in order to gain access to the object that was re-instantiated but instead Python just "knows" that my_issue is now referring to the newly instantiated object by the reload() method. So, my question is - how does this happen? How does the interpreter know of the new object and automatically assigns it to my_issue? Thanks a lot in advance! :) Regards, Dimitar From alan.gauld at yahoo.co.uk Wed Feb 17 07:22:36 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 17 Feb 2021 12:22:36 +0000 Subject: [Tutor] Trying to understand how object re-instantiation gets propagated In-Reply-To: References: Message-ID: On 17/02/2021 11:43, Dimitar Ivanov wrote: > My JIRA issue class (shown below) retrieves the values from a JIRA server > and saves them as instance variables. To avoid outdated information when I > update a value on the JIRA server, I made a small function that retrieves > the JIRA issue from the server again and re-instantiates a new object of my > class: No it doesn't. It reinitializes the same object. initialization and instantiation are two different concepts and Python separates them into two different methods. __new__() constructs new instances __init__() initialises existing instances. When you create a new object with myObj = MyClass() Python first calls myClass.__new__() to create an empty instance Then calls MyClass.__init__() to initialize that instance. > class JiraIssue: > def __init__(self, jira_issue): > self.key = issue.key > self.assignee = issue.fields.assignee > > def reload(self): > jira_issue = jira_connection.issue(self.key) # Retrieving the issue > from the JIRA server > issue = JiraIssue.__init__(self, jira_issue) So this line just reinitialises the existing object it does not create a new one. > Python just "knows" that my_issue is now referring to the newly > instantiated object by the reload() method. There is no new object, its still the same one but with updated values. Incidentally, you don't really need objects here at all, you could just create a dictionary with the issue attributes in it. myIssue = {'key':jira_issue.key, 'assignee': jira_issue.fiels.assignee} Then write a reload() function to refresh the values. To quote the YouTube video "Stop writing classes" - "Any time you have a class with just an init() plus one method all you really need is a function" Of course you may be planning on adding a bunch of other methods, in which case just carry on with the class. :-) -- 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 chigga101 at gmail.com Wed Feb 17 14:05:20 2021 From: chigga101 at gmail.com (Matthew Ngaha) Date: Wed, 17 Feb 2021 19:05:20 +0000 Subject: [Tutor] Is this possible in Python? Message-ID: Hi guys, my networking friend wants to get into programming to create an app. Can someone please tell me if this type of application is possible in python? and what libraries/frameworks are needed. I consulted the python irc channel and the response from a user is that they don't think it's a Python issue but a 2nd opinion is welcome. Here are the details: 1) client makes DNS request to resolve apps url to an IP address. 2) client's DNS-proxy forwards the request to one of the 3 global load balancers fronting the application. 3) Responding load balancer uses geolocation info to direct user to an external server in the regional cloud closest to them. 4) Subsequent DNS requests from the same client's DNS-proxy is directed to the same server in the cloud. 5) client/user creates a TCP connection to the login server in the cloud. 6) The SSL session is terminated and re-encrypted and forwarded to another server. 7) If an existing user, a login prompt will be provided for the user to to input a username and password. 8) If a new user, they would have to enter registration details. There is a lot more to this, but you get this gist. I won't go on and bore you with more detail. From chigga101 at gmail.com Wed Feb 17 15:42:16 2021 From: chigga101 at gmail.com (Matthew Ngaha) Date: Wed, 17 Feb 2021 20:42:16 +0000 Subject: [Tutor] Is this possible in Python? In-Reply-To: References: Message-ID: On Wed, Feb 17, 2021 at 8:14 PM Rudy Matela wrote: > > What you describe seems possible. > > It seems like something that may involve the requests library. If you're new to Python, you can also take a look at the Python tutorial. Isn't request just for the client side? But what about the server side? what about point 3, 4, 5, and 6 from the server side, is that possible? 3) Responding load balancer uses geolocation info to direct user to an external server in the regional cloud closest to them. 4) Subsequent DNS requests from the same client's DNS-proxy is directed to the same server in the cloud. 5) client/user creates a TCP connection to the login server in the cloud. 6) The SSL session is terminated and re-encrypted and forwarded to another server. imo this is too low level for python frameworks, using TCP to listen to connection and so on. but I could be wrong From PyTutor at DancesWithMice.info Wed Feb 17 15:50:19 2021 From: PyTutor at DancesWithMice.info (dn) Date: Thu, 18 Feb 2021 09:50:19 +1300 Subject: [Tutor] Trying to understand how object re-instantiation gets propagated In-Reply-To: References: Message-ID: On 18/02/2021 00.43, Dimitar Ivanov wrote: ... > My JIRA issue class (shown below) retrieves the values from a JIRA server > and saves them as instance variables. To avoid outdated information when I > update a value on the JIRA server, I made a small function that retrieves > the JIRA issue from the server again and re-instantiates a new object of my > class: ... > So, my question is - how does this happen? How does the interpreter know of > the new object and automatically assigns it to my_issue? Why "a new object"? Why think in terms of two objects? 1 Have faith: my_issue is created with data from Jira. The assignee is changed by the code. The Jira db is updated. The local data and the (remote) db are now identical - assuming there was no exception during the update. 2 Be sure: There appears to be no need to hold the pre-update JiraIssue object as well as its assignee-updated object. After the update is sent to Jira, consider ignoring/deleting that object (from the code), and instantiating an entirely new/different JiraIssue object (rather than reload-ing). > def main(): > jira_issue = jira_connection.issue("ISSUE-1") # Retrieving the issue > from the JIRA server > my_issue = JiraIssue(jira_issue) ... > my_issue.reload() ... > def reload(self): > jira_issue = jira_connection.issue(self.key) # Retrieving the issue Why is the jira_connection call outside of the JiraIssue object, and not part of its __init__() - given that the same step is part of reload()? (or vice-versa: why is reload() a method and not external...) > class JiraIssue: > def __init__(self, jira_issue): > self.key = issue.key > self.assignee = issue.fields.assignee Is this working-code? Parameter is "jira_issue" but method's assignments refer to an "issue" object. -- Regards, =dn From rudy at matela.com.br Wed Feb 17 15:14:15 2021 From: rudy at matela.com.br (Rudy Matela) Date: Wed, 17 Feb 2021 17:14:15 -0300 Subject: [Tutor] Is this possible in Python? In-Reply-To: References: Message-ID: What you describe seems possible. It seems like something that may involve the requests library. If you're new to Python, you can also take a look at the Python tutorial . On Wed, Feb 17, 2021 at 4:10 PM Matthew Ngaha wrote: > Hi guys, my networking friend wants to get into programming to create > an app. Can someone please tell me if this type of application is > possible in python? and what libraries/frameworks are needed. > > I consulted the python irc channel and the response from a user is > that they don't think it's a Python issue but a 2nd opinion is > welcome. > > Here are the details: > > 1) client makes DNS request to resolve apps url to an IP address. > 2) client's DNS-proxy forwards the request to one of the 3 global load > balancers fronting the application. > 3) Responding load balancer uses geolocation info to direct user to an > external server in the regional cloud closest to them. > 4) Subsequent DNS requests from the same client's DNS-proxy is > directed to the same server in the cloud. > 5) client/user creates a TCP connection to the login server in the cloud. > 6) The SSL session is terminated and re-encrypted and forwarded to > another server. > 7) If an existing user, a login prompt will be provided for the user > to to input a username and password. > 8) If a new user, they would have to enter registration details. > > > There is a lot more to this, but you get this gist. I won't go on and > bore you with more detail. > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From alan.gauld at yahoo.co.uk Wed Feb 17 18:45:24 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 17 Feb 2021 23:45:24 +0000 Subject: [Tutor] Is this possible in Python? In-Reply-To: References: Message-ID: On 17/02/2021 19:05, Matthew Ngaha wrote: > Hi guys, my networking friend wants to get into programming to create > an app. Can someone please tell me if this type of application is > possible in python? and what libraries/frameworks are needed. The standard library starts with the socket library which lets you do pretty much anything you want at IP level. (You could go lower still with the serial io libraries etc but sockets is usually where networking starts) On top of that are built lots of support libraries for most of the common network tasks including ssh, email, web, ftp, xrpc and more There are third party networking frameworks like twisted that sit above all that and can create servers as well. > 1) client makes DNS request to resolve apps url to an IP address. > 2) client's DNS-proxy forwards the request to one of the 3 global load > balancers fronting the application. > 3) Responding load balancer uses geolocation info to direct user to an > external server in the regional cloud closest to them. > 4) Subsequent DNS requests from the same client's DNS-proxy is > directed to the same server in the cloud. > 5) client/user creates a TCP connection to the login server in the cloud. > 6) The SSL session is terminated and re-encrypted and forwarded to > another server. > 7) If an existing user, a login prompt will be provided for the user > to to input a username and password. > 8) If a new user, they would have to enter registration details. Its not clear where you expect Python to fit into that scenario. Python could be programmed to do any of it or all of it. (A lot of work but possible) -- 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 chigga101 at gmail.com Thu Feb 18 09:31:02 2021 From: chigga101 at gmail.com (Matthew Ngaha) Date: Thu, 18 Feb 2021 14:31:02 +0000 Subject: [Tutor] Is this possible in Python? In-Reply-To: References: Message-ID: > Its not clear where you expect Python to fit into that scenario. > Python could be programmed to do any of it or all of it. (A lot > of work but possible) Thanks for your response. To be honest I'm not sure where Python fits in either. This is a project my networking friend, who knows nothing about programming, is looking to start and he came to me to see if it was possible. I really don't know what to tell him, but surely there must be an easier way of achieving this. All that networking talk in the steps listed seem a bit too low level for an application, even though as you said it is possible. Thanks again. From alan.gauld at yahoo.co.uk Thu Feb 18 18:26:07 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 18 Feb 2021 23:26:07 +0000 Subject: [Tutor] Is this possible in Python? In-Reply-To: References: Message-ID: On 18/02/2021 14:31, Matthew Ngaha wrote: >> Its not clear where you expect Python to fit into that scenario. > in either. This is a project my networking friend, who knows nothing > about programming, is looking to start and he came to me to see if it > was possible. It is surely possible but a big project. But it is a system not an application. The description is of a client-server type transaction with distributed components. It needs significant technical design as well as software design. but if he is a network guy he probably knows how to do the technical bits. However, there are standard components for most of it. He needs to work out where his requirements differ from the norm. It may well be that he simply needs to do some scripting in Javascript rather than Python programming. Or it may be he needs to write a bespoke component in which case Python might be better. The devil is in the detail. > must be an easier way of achieving this. All that networking talk in > the steps listed seem a bit too low level for an application, even > though as you said it is possible. It's not low level really, its just a networking set of applications rather than an end user tool. But well within the scope of Python. However he will need to do some serious study of generic programming (regardless of the language he chooses) because network programming is never simple. You have to follow very specific standards and they usually have lots of options that you must handle. A big job. -- 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 sahuprashant200101 at gmail.com Thu Feb 18 21:50:52 2021 From: sahuprashant200101 at gmail.com (prashant sahu) Date: Fri, 19 Feb 2021 08:20:52 +0530 Subject: [Tutor] error generating Message-ID: print("learning dictionaries") print("Geek Translator") geek = {"404": "clueless. From the web error message 404, meaning page not found.", "Googling": "searching the Internet for background information on a person.", "Keyboard Plaque" : "the collection of debris found in computer keyboards.", "Link Rot" : "the process by which web page links become obsolete.", "Percussive Maintenance" : "the act of striking an electronic device to make it work.", "Uninstalled" : "being fired. Especially popular during the dot-bomb era."} choice= 2000 while choice>4: print( """ 0- Quit 1- Look up a Geek term 2- add a geek term 3- Redifine a geek term 4- Delete a geek term """ ) choice= int(input("Choice: ")) if choice==4: term= input("\nWhat term do you want me to delete___:") if term in geek: del geek[term] print(term,"\nis no more in ur dictionary") else: print("chill buddy,,, this ",term," is already out of my dictionary") input() when i am executing this code this is showing error massage like this: "syntex error Unindent does not match any outer indentation level" please help me here From alan.gauld at yahoo.co.uk Fri Feb 19 07:05:08 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 19 Feb 2021 12:05:08 +0000 Subject: [Tutor] error generating In-Reply-To: References: Message-ID: On 19/02/2021 02:50, prashant sahu wrote: > while choice>4: > print( > """ ... > """ > ) > choice= int(input("Choice: ")) > if choice==4: > term= input("\nWhat term do you want me to delete___:") > if term in geek: > del geek[term] > print(term,"\nis no more in ur dictionary") > else: > print("chill buddy,,, this ",term," is already out of my > dictionary") > input() > > when i am executing this code this is showing error massage like this: > "syntex error > Unindent does not match any outer indentation level" > please help me here That's because your indentation does not match up. The if under choice... is indented but it shouldn't be and the term= line is not indented but it should be. Python is usually pretty accurate in its error messages. The line is occasionally one or two out but if you look a few lines above the reported line you will usually see the error. Speaking of which, when posting questions it's always helpful if you post the entire error message not just the last line, they contain a lot of useful information. -- 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 ming at pgp.cool Fri Feb 19 06:59:09 2021 From: ming at pgp.cool (Ming) Date: Fri, 19 Feb 2021 19:59:09 +0800 Subject: [Tutor] error generating In-Reply-To: References: Message-ID: <20210219115906.GA15@pgp.cool> On Fri, Feb 19, 2021 at 08:20:52AM +0530, prashant sahu wrote: > [...] > choice= int(input("Choice: ")) > if choice==4: > term= input("\nWhat term do you want me to delete___:") > if term in geek: > del geek[term] > print(term,"\nis no more in ur dictionary") > else: > print("chill buddy,,, this ",term," is already out of my > dictionary") > input() Hi, there is an obvious indentation error in your code: the code block following 'if' needs to be indented. Modify as follows: ... choice= int(input("Choice: ")) if choice==4: term= input("\nWhat term do you want me to delete___:") if term in geek: ... -- OpenPGP fingerprint: 3C47 5977 4819 267E DD64 C7E4 6332 5675 A739 C74E From va_kvaratskhelia at cu.edu.ge Sat Feb 20 06:27:01 2021 From: va_kvaratskhelia at cu.edu.ge (Vakhtang Kvaratskhelia) Date: Sat, 20 Feb 2021 15:27:01 +0400 Subject: [Tutor] Python problem Message-ID: Hello, i have written the following code that uses the bisection search to find the optimal percentage to save. However i am unable to prevent an infinite loop in case the annual salary is 10,000 also i do not get how i can use only integer division while using the bisection search? when i substitute // division instead of / division the loop runs infinitely. I want to use this hint : "Because we are searching for a value that is in principle a float, we are going to limit ourselves to two decimals of accuracy (i.e., we may want to save at 7.04% or 0.0704 in decimal ? but we are not going to worry about the difference between 7.041% and 7.039%). This means we can search for an integer between 0 and 10000 (using integer division), and then convert it to a decimal percentage (using float division) to use when we are calculating the current_savings after 36 months. By using this range, there are only a finite number of numbers that we are searching over, as opposed to the infinite number of decimals between 0 and 1. This range will help prevent infinite loops. The reason we use 0 to 10000 is to account for two additional decimal places in the range 0% to 100%. Your code should print out a decimal (e.g. 0.0704 for 7.04%)." but i am not able to. this is the code i have written and it gives the correct amount as a solution except it is not rounded to 4 decimals as it would have been if i was using integer division: annual_salary=150000 total_cost=1000000 Months=36 semi_annual_raise=0.07 portion_down_payment=0.25 r=0.04 portion_saved=0 epsilon=100 possible_savings=0 Steps=0 for i in range(0,Months): possible_savings+= annual_salary/12 + possible_savings*0.04/12 if (i+1)%6 == 0: annual_salary=annual_salary*(1+semi_annual_raise) low=0 high=10000 while abs(total_cost*portion_down_payment - portion_saved * possible_savings) >= epsilon: if portion_saved * possible_savings < total_cost*portion_down_payment : low=portion_saved else: high=portion_saved portion_saved=(high+low)/2 Steps+=1 print("Optimal portion saved is = ",portion_saved) print("Steps in bisection search: ",Steps) Can you help with this? Thank you in advance. From mats at wichmann.us Sat Feb 20 12:06:29 2021 From: mats at wichmann.us (Mats Wichmann) Date: Sat, 20 Feb 2021 10:06:29 -0700 Subject: [Tutor] Python problem In-Reply-To: References: Message-ID: <3bcabc37-dced-5c9c-331c-7d169fbe5791@wichmann.us> On 2/20/21 4:27 AM, Vakhtang Kvaratskhelia wrote: > Hello, > > 0% to 100%. Your code should print out a decimal (e.g. 0.0704 for 7.04%)." > but i am not able to. > this is the code i have written and it gives the correct amount as a > solution except it is not rounded to 4 decimals A floating point number is just a representation, it's not intrinsically rounded. You do know that you can control the formatting of what you print? Or is that something that hasn't been taught yet (this is from a course, right)? > print("Optimal portion saved is = ",portion_saved) For example, you can do this (one of several different ways to control the conversion of the float to the string that will be printed): print(f"Optimal portion saved is {portion_saved:.4f}") (Python's "f-strings" are comparatively new, and don't work on Python versions prior to 3.6) Alternatively, Python has a decimal floating point library that is intended for this kind of usage: https://docs.python.org/3/library/decimal.html From learn2program at gmail.com Mon Feb 22 10:44:56 2021 From: learn2program at gmail.com (Alan Gauld) Date: Mon, 22 Feb 2021 15:44:56 +0000 Subject: [Tutor] How this list works Message-ID: <8b15095b-7e4b-e02d-37e9-f2d981073d54@yahoo.co.uk> Over the last few months I've had several emails asking me to delete certain posts (including one from mid 2018!). The usual reason for this is that the sender has included personal details that they do not wish published. I thought I'd better make it clear how the list works and the limits of our (the moderators) abilities. When you post a mail to the list it comes to the server and possibly into the moderation queue. (Only a small proportion go to the queue, most go straight through. If it comes to the moderators there is a very narrow window in which you can catch or attention and prevent it being posted. If approved, (either automatically by the server or by a moderator) it goes to the mail relay server which then sends new email copies out to every subscriber (or adds it to the digest for those who use that feature) - that's currently over 800 people in total .? Once those emails go out they *cannot be retrieved*. This is not Microsoft Exchange! Some of the addresses on the list are mail archive sites (there are at least 3 that I know of). When they receive the email they automatically publish it on their archive site, subject only to their local security policy. Those sites are independent of the mailing list (even the python.org archive) and we do not have any control or access to them. We do not even know how many there are for sure. These sites can be accessed by potentially tens of thousands (even millions!) of viewers - anyone on the internet in fact. So, in summary, when posting to the tutor list be mindful that it is a public message and do not include any personal information that you are not happy to share with the world. Once posted it cannot be retrieved. It is effectively a permanent record on public display. -- 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 va_kvaratskhelia at cu.edu.ge Mon Feb 22 11:47:06 2021 From: va_kvaratskhelia at cu.edu.ge (Vakhtang Kvaratskhelia) Date: Mon, 22 Feb 2021 20:47:06 +0400 Subject: [Tutor] Python problem In-Reply-To: <8rg23ghdvl8nc7i4bd2ib20rkindv6le11@4ax.com> References: <8rg23ghdvl8nc7i4bd2ib20rkindv6le11@4ax.com> Message-ID: Thank you very much for the reply. I understand those comments, however what i don't understand is how can you use integer division "//" with the bisection search code? The "/" division works just fine but the "//" doesn't work with the bisection search code. Is it possible that the hint is wrong? Or maybe integer division doesn't mean "//" On Sat, Feb 20, 2021 at 9:31 PM Dennis Lee Bieber wrote: > On Sat, 20 Feb 2021 15:27:01 +0400, Vakhtang Kvaratskhelia > declaimed the following: > > > > >I want to use this hint : "Because we are searching for a value that is in > >principle a float, we are going to limit ourselves to two decimals of > >accuracy (i.e., we may want to save at 7.04% or 0.0704 in decimal ? but we > >are not going to worry about the difference between 7.041% and 7.039%). > >This means we can search for an integer between 0 and 10000 (using integer > >division), and then convert it to a decimal percentage (using float > >division) to use when we are calculating the current_savings after 36 > >months. By using this range, there are only a finite number of numbers > that > >we are searching over, as opposed to the infinite number of decimals > >between 0 and 1. This range will help prevent infinite loops. The reason > we > >use 0 to 10000 is to account for two additional decimal places in the > range > >0% to 100%. Your code should print out a decimal (e.g. 0.0704 for 7.04%)." > >but i am not able to. > > Bottom up. Python floats are double precision -- approximately 15 > significant digits -- ALWAYS. > > If you need specific formatting for output, you have to provide the > correct formatting string when outputting the result. Your code is just > outputting a sequence of a string and a numeric with no formatting codes -- > so naturally Python is going to display as many significant decimals as it > can... > > >>> import math > >>> math.pi > 3.141592653589793 > >>> "%7.4f" % math.pi > ' 3.1416' > >>> > > I'm not so certain about that justification for using integers and > then > converting to float for output... While real world may have an infinite > range between 0.0 and 1.0, computers do not. Furthermore, if you are using > an epsilon to define when two values are "close enough" you will never be > computing in increments of less than that epsilon. > > abs(a-b)<0.00005 > > Of course, going further from the track... If you are discussing > financial values, using the Decimal module configured for the desired > resolution is probably recommended. > > > -- > Wulfraed Dennis Lee Bieber AF6VN > wlfraed at ix.netcom.com > http://wlfraed.microdiversity.freeddns.org/ > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From PyTutor at DancesWithMice.info Mon Feb 22 18:03:48 2021 From: PyTutor at DancesWithMice.info (dn) Date: Tue, 23 Feb 2021 12:03:48 +1300 Subject: [Tutor] Python problem In-Reply-To: References: <8rg23ghdvl8nc7i4bd2ib20rkindv6le11@4ax.com> Message-ID: On 23/02/2021 05.47, Vakhtang Kvaratskhelia wrote: > Thank you very much for the reply. > > I understand those comments, however what i don't understand is how can you > use integer division "//" with the bisection search code? The "/" division > works just fine but the "//" doesn't work with the bisection search code. > > Is it possible that the hint is wrong? Or maybe integer division doesn't > mean "//" [am assuming you to be a ComSc student studying math/algorithms, and thus to be held to a different standard to (say) a hobbyist] The "Is it possible" is easily checked using the documentation and/or experimentation ("reflection"): https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex <<< x // y floored quotient of x and y... Also referred to as integer division. The resultant value is a whole integer, though the result?s type is not necessarily int. The result is always rounded towards minus infinity: 1//2 is 0, (-1)//2 is -1, 1//(-2) is -1, and (-1)//(-2) is 0. >>> Thus: >>> 5/2 2.5 >>> 5//2 2 >>> type( 5//2 ) >>> I want to use this hint : "Because we are searching for a value that is in >>> principle a float, we are going to limit ourselves to two decimals of >>> accuracy (i.e., we may want to save at 7.04% or 0.0704 in decimal ? but we >>> are not going to worry about the difference between 7.041% and 7.039%). >>> This means we can search for an integer between 0 and 10000 (using integer >>> division), and then convert it to a decimal percentage (using float >>> division) to use when we are calculating the current_savings after 36 >>> months. By using this range, there are only a finite number of numbers >> that >>> we are searching over, as opposed to the infinite number of decimals >>> between 0 and 1. This range will help prevent infinite loops. The reason >> we >>> use 0 to 10000 is to account for two additional decimal places in the >> range >>> 0% to 100%. Your code should print out a decimal (e.g. 0.0704 for 7.04%)." >>> but i am not able to. >> I'm not so certain about that justification for using integers and >> then >> converting to float for output... While real world may have an infinite ... Separate in your mind the two 'values'! The first is the $value or is it the interest-rate, which is expressed as a decimal/float. The second is the list *index* (or "pointer") which *must* be an integer. [will assume "list" for convenience = any collection] Back to the docs: https://docs.python.org/3/reference/expressions.html#grammar-token-subscription <<< 6.3.2. Subscriptions Subscription of a sequence (string, tuple or list) or mapping (dictionary) object usually selects an item from the collection: subscription ::= primary "[" expression_list "]" The primary must evaluate to an object that supports subscription (lists or dictionaries for example). User-defined objects can support subscription by defining a __getitem__() method. For built-in objects, there are two types of objects that support subscription: If the primary is a mapping, the expression list must evaluate to an object whose value is one of the keys of the mapping, and the subscription selects the value in the mapping that corresponds to that key. (The expression list is a tuple except if it has exactly one item.) If the primary is a sequence, the expression list must evaluate to an integer or a slice (as discussed in the following section). >>> continued and confirmed by: https://docs.python.org/3/reference/datamodel.html#object.__getitem__ <<< object.__getitem__(self, key) Called to implement evaluation of self[key]. For sequence types, the accepted keys should be integers and slice objects. Note that the special interpretation of negative indexes (if the class wishes to emulate a sequence type) is up to the __getitem__() method. If key is of an inappropriate type, TypeError may be raised; >>> The "bi" part of "bisect" refers to "two" (as I'm sure you understand, but just sayin'). There is also a concept of "equal" (cf one cake for you and all the rest for me!). Consider though, if the list is an even number of elements long it can be halved, ie use the 'half' index to (after search considerations) divide the whole list (conceptually) into two segments of equal length. If however, the list is an odd number of elements in length, halving the length yields a float value and rounding/flooring that will yield two segments of slightly-uneven, not-quite-half-length, segments! The same applies when one bisects a segment. NB I have to draw myself little block-diagrams in order to learn/to keep track of ideas, such as the following (again, just sayin'!):- [although this description follows the usual approach found in text-books, the Python idiom may involve direct manipulation/trimming to consider only the relevant segment of the original list, cf using 'pointers' to do the same, conceptually] A KPI when implementing this algorithm is to keep track of the indices/indexes representing the entire list, *and* those delimiting the current segment under-search - most importantly: *not* losing an index during some rounding-process! The former is done for you by Python, ie a list's range is 0:len(). The code has to manage the latter - most illustrations use names such as "top" and "bottom" (if you think/'see' of the arrangement vertically), or "left" and "right", "begin" and "end", or similar. These refer to, and define, the segment currently being searched. Thus, to discuss <<>>. What may actually happen is that Python auto-magically converts a calculated-to-be-float index into an integer, and subsequently/consequently *that* test 'works'. Great stuff! However!!! If that is the case, then some list-element at a segment-boundary may not be being included within the subsequent sub-segment, when it should. Accordingly, a search where a 'missing index' also holds the answer, will fail - or worse: conclude erroneously! To (dis)prove this, may I suggest that you take (a copy of) the existing code (could remove anything not related to the indices/indexes). Then add debug-prints to show what is/not being included at each bisection step. Try this with different lengths of list, and if you like, with 'the answer' located in different positions within the list. Ensure that each element is considered, and/or none is ("silently") ignored! Concluding with the problem of significant-digits and floats. It is unlikely that any particular interest-rate will generate an exact target-amount at the (exact) end of some time-period. Accordingly, if interest is paid monthly, one waits until the end of the month, but then the withdrawal will exceed 'the target' by some small amount. Thus, being mathematically-precise, the required sum will be generated after some number of months, plus a portion of the 'last' month. Alternately, to round-out to an integral number of months, the interest-rate can be 'tweaked' to give an exact answer as an integral number of months - but should be limited in precision, per advice given. Reinforcing @Wlfraed's advice, and something which has served me well (and continues to do so!): when coding asymptotic solutions (and any recursive routine) the first priority is to code 'how do I make it stop?'. - or in my parlance: "where's the exit?", "the nearest exit may be behind you", or "stop the world - I want to get off"! Has this helped? -- Regards, =dn From writer5643 at gmail.com Mon Feb 22 13:37:30 2021 From: writer5643 at gmail.com (Shaun Keyes-McClements) Date: Mon, 22 Feb 2021 11:37:30 -0700 Subject: [Tutor] Questions about Creating Executables and Backward Compatibility Message-ID: Hi Team, I am a hobbyist programmer with the following questions: Since Python is a scripting language and does not need to be compiled, how would I make executables of programs written in either Python 3.1 or 3.9? PIP seems to support specific versions of Python, such as 3.4 and 3.7, respectively. What I would like, is to share text adventures I've written with those who don't have Python installed on their systems. Is there a way to do this, I mean, do I need to create a library and import it into another language such as C++, to be compiled? Next question, with the exception of updates made, are Python 3x versions typically backward compatible? For example, have the Input and Print Functions remained the same in terms of syntax from let's say 3.1 onward? Most of my experience with Python is with 3.1; I recently installed 3.9 and don't want to reinvent the wheel by rewriting all of my programs from scratch, though I am not opposed to revising for efficiency and improved User Experience. Thank you for your time and attention. Shaun From marc.tompkins at gmail.com Mon Feb 22 18:16:28 2021 From: marc.tompkins at gmail.com (Marc Tompkins) Date: Mon, 22 Feb 2021 15:16:28 -0800 Subject: [Tutor] Questions about Creating Executables and Backward Compatibility In-Reply-To: References: Message-ID: I think py2exe might be what you're looking for: https://www.py2exe.org/ It bundles the Python interpreter + supporting files, along with your script and any dependencies, into a single executable suitable for distribution. It's a compromise, of course - on one hand the executable is relatively large because it has to include Python itself, and on the other hand your users don't get a usable Python development environment out of it - but it gets the job done fairly easily. On Mon, Feb 22, 2021 at 3:05 PM Shaun Keyes-McClements wrote: > Hi Team, > > I am a hobbyist programmer with the following questions: > > Since Python is a scripting language and does not need to be compiled, how > would I make executables of programs written in either Python 3.1 or 3.9? > PIP seems to support specific versions of Python, such as 3.4 and 3.7, > respectively. What I would like, is to share text adventures I've > written with those who don't have Python installed on their systems. Is > there a way to do this, I mean, do I need to create a library and import it > into another language such as C++, to be compiled? Next question, with the > exception of updates made, are Python 3x versions typically backward > compatible? For example, have the Input and Print Functions remained the > same in terms of syntax from let's say 3.1 onward? Most of my experience > with Python is with 3.1; I recently installed 3.9 and don't want to > reinvent the wheel by rewriting all of my programs from scratch, though I > am not opposed to revising for efficiency and improved User Experience. > Thank you for your time and attention. > > Shaun > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From alan.gauld at yahoo.co.uk Mon Feb 22 18:49:23 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 22 Feb 2021 23:49:23 +0000 Subject: [Tutor] Questions about Creating Executables and Backward Compatibility In-Reply-To: References: Message-ID: On 22/02/2021 18:37, Shaun Keyes-McClements wrote: > Since Python is a scripting language and does not need to be compiled, how > would I make executables of programs written in either Python 3.1 or 3.9? Technically you don't. Python is always interpreted. Just like Basic and JavaScript (and even Java although it is also compiled!) etc. And since Python is installed by default on almost all computers except Windows these days you often don't need to. (And even a few Windows computers have it nowadays. HP being one such make.) But as Marc has pointed out there are several tools that will build a pseudo exe that embeds and runs the interpreter plus your Python code plus any libraries you import. To your users it looks and feels like a single file executable but really it's still the interpreter interpreting your code. > exception of updates made, are Python 3x versions typically backward > compatible? Yes, with a few very rare exceptions. (For example Python dictionaries used to be unordered. But now they preserve insertion order. So if you wrote code that relied on the dictionary order not being fixed then it will likely break. But that would be highly unlikely to be a realistic scenario.) Major versions of Python are not compatible though, so v3 code won't usually work on a v2 interpreter or vice versa. But v3.0 code should (nearly?) all work on v3.9 3.9 code may not run on v3.0 if you use any of the new features. But a surprisingly large subset will be just fine. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From PyTutor at DancesWithMice.info Mon Feb 22 19:06:04 2021 From: PyTutor at DancesWithMice.info (dn) Date: Tue, 23 Feb 2021 13:06:04 +1300 Subject: [Tutor] Python problem In-Reply-To: References: <8rg23ghdvl8nc7i4bd2ib20rkindv6le11@4ax.com> Message-ID: <45b94ede-3844-39c2-4463-8e49bff9bda7@DancesWithMice.info> On 23/02/2021 12.56, Dennis Lee Bieber wrote: > On Tue, 23 Feb 2021 12:03:48 +1300, dn via Tutor > declaimed the following: > > >> The second is the list *index* (or "pointer") which *must* be an >> integer. [will assume "list" for convenience = any collection] >> > > My apologies, but I think you went off the rails at this point... As I > recall the original post, there is no list/index involved. It is all just > an iterative numeric computation -- compute a candidate split point (in the > scaled integer range 0..10000 => 0.00%..100.00%), determine if the optimal > value is above or below that split, determine a new split point within > 0..Split or Split..10000 (depending upon which side one determined is > optimal), taking into account some epsilon used to determine if a solution > has been found. > > It is not a binary search of a collection object... > > OTOH: the code that was in the original post contains floating point > division operators, so one has not seen the supposedly failing code. You're correct. The algorithm can be used against previously-computed data, ie sitting in a list or a table; and at other times computing ever more-precise results 'dynamically' and by 'splitting the difference'. Sigh! Not seeing the entire assignment-question, nor the actual code... This is a Python list. What's all this talk about "Rails", Mizz Ruby? -- Regards, =dn From singh000taran at gmail.com Sat Feb 27 02:09:55 2021 From: singh000taran at gmail.com (=?UTF-8?B?4Kmn?=) Date: Sat, 27 Feb 2021 12:39:55 +0530 Subject: [Tutor] (no subject) Message-ID: Sir, For the question - Define a function called myfunc that takes in a string, and returns a matching string where every even letter is uppercase, and every odd letter is lowercase. Assume that the incoming string only contains letters, and don't worry about numbers, spaces or punctuation. The output string can start with either an uppercase or lowercase letter, so long as letters alternate throughout the string. I tried - def mufunc(**kwargs): finaloutput = '' " i = 0 while i < len(kwargs): if i%2 == 0: finaloutput = finaloutput + kwargs[i].upper() else: finaloutput = finaloutput + kwargs[i].lower() i = i + 1 return finaloutput myfunc('Anthropomorphism') It's not working. Please guide me. Thanking you Yours sincerely Tarandeep Singh From va_kvaratskhelia at cu.edu.ge Sat Feb 27 02:23:19 2021 From: va_kvaratskhelia at cu.edu.ge (Vakhtang Kvaratskhelia) Date: Sat, 27 Feb 2021 11:23:19 +0400 Subject: [Tutor] Python problem In-Reply-To: References: Message-ID: Thank you very much for the reply. i worked out other small problems but the main question was: can i use bisection search with the integer division? Every time i run a bisection search code through python tutor visualization the outcome is the same: the integer division turns into an infinite loop. What i mean is that if i write the code (for simplicity i have precalculated variables mentioned in the previous emails and narrowed the code down to the following ) : low=0 high=10000 epsilon=100 portion_saved=0 while abs(250,000 - portion_saved * 566,774) >= epsilon: if portion_saved * 566774 < 250,000 : low=portion_saved else: high=portion_saved portion_saved=(high+low) / 2 print(portion_saved) The result is the same as the answer in the book that had the problem. but if i run the code: low=0 high=10000 epsilon=100 portion_saved=0 while abs(250,000 - portion_saved * 566,774) >= epsilon: if portion_saved * 566,774 < 250,000 : low=portion_saved else: high=portion_saved portion_saved=(high+low) // 2 print(portion_saved) Where i use "//" instead of "/" i get infinite loop because the "//" gets stuck on one particular guess and doesn't continue the search as the "/" division would. So then how do i use the hint which was given to me in the book? the hint was the following: "Because we are searching for a value that is in principle a float, we are going to limit ourselves to two decimals of accuracy (i.e., we may want to save at 7.04% or 0.0704 in decimal ? but we are not going to worry about the difference between 7.041% and 7.039%). This means we can search for an integer between 0 and 10000 (using integer division), and then convert it to a decimal percentage (using float division) to use when we are calculating the current_savings after 36 months. By using this range, there are only a finite number of numbers that we are searching over, as opposed to the infinite number of decimals between 0 and 1. This range will help prevent infinite loops. The reason we use 0 to 10000 is to account for two additional decimal places in the range 0% to 100%. Your code should print out a decimal (e.g. 0.0704 for 7.04%)." Is it possible that the hint doesn't mean "//" by the "integer division"? Thank you in advance. On Tue, Feb 23, 2021 at 10:42 PM Dennis Lee Bieber wrote: > On Sat, 20 Feb 2021 15:27:01 +0400, Vakhtang Kvaratskhelia > declaimed the following: > > I'm going all the way back to your first post just to comment on a > number of inefficiencies seen in the provide code. > > Note: as you have NOT provided the code using // I can not comment > on > possible problems there... > > > > >However i am unable to prevent an infinite loop in case the annual salary > >is 10,000 > > > > Given the nature of the values provided in the code, I don't know > if > you posted scaled integers (for use in the // case?) or "real" values. I > would suspect that "annual salary" that low is a value that will not > converge to a solution -- that is, the income is too low to make payments > which will close out the cost in the time span specified. > > > >I want to use this hint : "Because we are searching for a value that is in > >principle a float, we are going to limit ourselves to two decimals of > >accuracy (i.e., we may want to save at 7.04% or 0.0704 in decimal ? but we > >are not going to worry about the difference between 7.041% and 7.039%). > > This clause would appear to define the convergence/termination > criteria. It is unclear, however, if that criteria is to be abs(upper - > lower) < 0.01, or < 0.001, or something else like < 0.005. > > Now, comments on your posted code... > > > > >annual_salary=150000 > >total_cost=1000000 > >Months=36 > >semi_annual_raise=0.07 > >portion_down_payment=0.25 > >r=0.04 > > This is not used in your code -- I'm presuming it was meant to be > used > where your hard-coded 0.04 below. > > >portion_saved=0 > >epsilon=100 > >possible_savings=0 > >Steps=0 > >for i in range(0,Months): > > range(0, months) is the same as range(months) > > However, since you keep doing (i+1)%6 in subsequent code, it would > be > better to use range(1, months+1) and i%6 which uses > only one > addition. > > > possible_savings+= annual_salary/12 + possible_savings*0.04/12 > > Similarly, you keep dividing annual salary by 12, and 0.04 by 12. > It > would be more efficient to precompute monthly salary and monthly interest > rate outside the loop (and recompute monthly salary when a raise is > applied). > > > if (i+1)%6 == 0: > > As mentioned, changing the range() call allows removing the +1 from > this. > > > annual_salary=annual_salary*(1+semi_annual_raise) > > And here, again, you have an addition that could have been done > outside > the loop. My solution keeps an addition, but changes where it takes place. > > >low=0 > >high=10000 > >while abs(total_cost*portion_down_payment - portion_saved * > >possible_savings) >= epsilon: > > Again, total cost * portion down payment never changes, precompute > it > outside the loop. > > I also note that "high" appears to be a scaled integer percentage, > yet > this code is not using the // operator. Low/high should be decimal fraction > percentage 0.0/1.0. As noted above, your convergence criteria is supposed > to be when the difference between low/high is < some limit. I don't > understand what your "while" statement is doing with some epsilon > (especially as I don't know if your "100" epsilon is supposed to represent > actual 100.0, or is a scaled integer again and represents 1.0) > > > if portion_saved * possible_savings < total_cost*portion_down_payment > : > > low=portion_saved > > else: > > high=portion_saved > > portion_saved=(high+low)/2 > > You never test for convergence -- should low/high end up with the > same > value, portion saved will not change. > > You also never showed us the result of running your code, so > knowing > what is a "right" result is difficult. My attempt at rewriting your logic > into a reusable format (your code requires editing to change any parameter) > resulted in... (watch out for line wraps) > > -=-=- > > def getInt(prompt): > while True: > sInt = input("Enter %s as an integer: " % prompt) > try: > iInt = int(sInt) > break > except: #yes, a bare except clause, this is not production > print("Invalid entry for integer: '%s'\n" % sInt) > return iInt > > def getFloat(prompt): > while True: > sFloat = input("Enter %s as a float: " % prompt) > try: > fFloat = float(sFloat) > break > except: #yes, a bare except clause, this is not production > print("Invalid entry for float: '%s'\n" % sFloat) > return fFloat > > def getPercent(prompt): > while True: > sFloat = input("Enter %s as a float percentage [nn.n, not 0.nnn]: " > % prompt) > try: > fFloat = float(sFloat) > if not (0.0 <= fFloat <= 100.0): > print("Invalid range for percent 0.0 - 100.0: %s\n" % > fFloat) > else: > break > except: #yes, a bare except clause, this is not production > print("Invalid entry for float: '%s'\n" % fFloat) > return fFloat / 100.0 #convert to decimal fraction 0.0-1.0 > > > annualSalary = getFloat("annual salary") > monthlySalary = annualSalary / 12.0 #algorithm works in months > > totalCost = getFloat("total cost") > > months = getInt("months") > > raisePercentRate = getPercent("rate of periodic raises") > raisePeriod = getInt("months between raises") > > downPaymentRate = getPercent("percent of cost used for down payment") > > annualInterestRate = getPercent("annual interest rate") > monthlyInterestRate = annualInterestRate / 12.0 > > convergenceCriteria = getPercent("convergence criteria") > > #compute maximum possible savings > possibleSavings = 0 > for month in range(1, months + 1): > possibleSavings += monthlySalary + possibleSavings * > monthlyInterestRate > if month % raisePeriod == 0: > annualSalary += annualSalary * raisePercentRate > monthlySalary = annualSalary / 12.0 > > #iterate over percentage (as decimal fraction) range > steps = 0 > low = 0.0 > high = 1.0 > downPayment = totalCost * downPaymentRate > while abs(high - low) > convergenceCriteria: > portionSaved = (high + low) / 2.0 > steps += 1 > if portionSaved * possibleSavings < downPayment: > low = portionSaved > else: > high = portionSaved > > print("Optimal portion saved is %7.2f %%" % (portionSaved * 100.0)) > print("Search required %s steps\n\n" % steps) > > -=-=- > > ... which produced the following when entering what I think is your set of > parameters. I have no idea if the results are correct. > > -=-=- > C:\Users\Wulfraed\Documents\_Hg-Repositories\Python > Progs>bisectionSearch.py > Enter annual salary as a float: 150000.0 > Enter total cost as a float: 1000000.0 > Enter months as an integer: 36 > Enter rate of periodic raises as a float percentage [nn.n, not 0.nnn]: 7.0 > Enter months between raises as an integer: 6 > Enter percent of cost used for down payment as a float percentage [nn.n, > not 0.nnn]: 25.0 > Enter annual interest rate as a float percentage [nn.n, not 0.nnn]: 4.0 > Enter convergence criteria as a float percentage [nn.n, not 0.nnn]: 1.0 > Optimal portion saved is 44.53 % > Search required 7 steps > > > > C:\Users\Wulfraed\Documents\_Hg-Repositories\Python Progs> > -=-=- > > Out of curiosity, just what does "portion saved" really represent? > In > my tests it appears to be the percentage of salary required to make > payments, not the amount one saves. > > Also, there is no test for feasibility -- all three of these runs > came > with the same result, but that result likely means the salary is too small > to make the payments. > > -=-=- > C:\Users\Wulfraed\Documents\_Hg-Repositories\Python > Progs>bisectionSearch.py > Enter annual salary as a float: 15000.0 > Enter total cost as a float: 1000000.0 > Enter months as an integer: 36 > Enter rate of periodic raises as a float percentage [nn.n, not 0.nnn]: 7.0 > Enter months between raises as an integer: 6 > Enter percent of cost used for down payment as a float percentage [nn.n, > not 0.nnn]: 25.0 > Enter annual interest rate as a float percentage [nn.n, not 0.nnn]: 4.0 > Enter convergence criteria as a float percentage [nn.n, not 0.nnn]: 1.0 > Optimal portion saved is 99.22 % > Search required 7 steps > > > > C:\Users\Wulfraed\Documents\_Hg-Repositories\Python > Progs>bisectionSearch.py > Enter annual salary as a float: 10000.0 > Enter total cost as a float: 1000000.0 > Enter months as an integer: 36 > Enter rate of periodic raises as a float percentage [nn.n, not 0.nnn]: 7. > Enter months between raises as an integer: 6 > Enter percent of cost used for down payment as a float percentage [nn.n, > not 0.nnn]: 25 > Enter annual interest rate as a float percentage [nn.n, not 0.nnn]: 4 > Enter convergence criteria as a float percentage [nn.n, not 0.nnn]: 1 > Optimal portion saved is 99.22 % > Search required 7 steps > > > > C:\Users\Wulfraed\Documents\_Hg-Repositories\Python > Progs>bisectionSearch.py > Enter annual salary as a float: 9000 > Enter total cost as a float: 1000000 > Enter months as an integer: 36 > Enter rate of periodic raises as a float percentage [nn.n, not 0.nnn]: 7 > Enter months between raises as an integer: 6 > Enter percent of cost used for down payment as a float percentage [nn.n, > not 0.nnn]: 25 > Enter annual interest rate as a float percentage [nn.n, not 0.nnn]: 4 > Enter convergence criteria as a float percentage [nn.n, not 0.nnn]: 1 > Optimal portion saved is 99.22 % > Search required 7 steps > -=-=- > > In fact, annual salary needs to be around 70000.00 before one gets > something less than "99.22" > > -=-=- > C:\Users\Wulfraed\Documents\_Hg-Repositories\Python > Progs>bisectionSearch.py > Enter annual salary as a float: 70000 > Enter total cost as a float: 1000000 > Enter months as an integer: 36 > Enter rate of periodic raises as a float percentage [nn.n, not 0.nnn]: 7 > Enter months between raises as an integer: 6 > Enter percent of cost used for down payment as a float percentage [nn.n, > not 0.nnn]: 25 > Enter annual interest rate as a float percentage [nn.n, not 0.nnn]: 4 > Enter convergence criteria as a float percentage [nn.n, not 0.nnn]: 1 > Optimal portion saved is 94.53 % > Search required 7 steps > -=-=- > > A similar condition applies when the annual salary is larger than > total > cost. > > > -- > Wulfraed Dennis Lee Bieber AF6VN > wlfraed at ix.netcom.com > http://wlfraed.microdiversity.freeddns.org/ > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From alan.gauld at yahoo.co.uk Sat Feb 27 06:19:54 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 27 Feb 2021 11:19:54 +0000 Subject: [Tutor] Python problem In-Reply-To: References: Message-ID: On 27/02/2021 07:23, Vakhtang Kvaratskhelia wrote: > What i mean is that if i write the code (for simplicity i have > precalculated variables mentioned in the previous emails and narrowed the > code down to the following ) : I'm confused. I'd expect both versions to give an infinite loop since you never change portion_saved and it is equal to zero. So the while loop will always evaluate to the same result. Also you have commas in your numbers which changes the meaning in Python. For example the while test parens looks like this to python: (250, (000 - portion_saved * 566), 774) Which works out at (250, 0, 774) But that should result in an error when you apply abs() Now the other possibility is that your locale settings allow commas in a number as a decimal point. In which case I'm not sure how Python distinguishes numbers in a tuple? But either way the fact that you multiply by zero and never change portion_saved should result in an infinite loop. The other thing I see different is that in the integer division version you have a comma in the if test but not in the float division version. > > low=0 > high=10000 > epsilon=100 > portion_saved=0 > while abs(250,000 - portion_saved * 566,774) >= epsilon: > if portion_saved * 566774 < 250,000 : > low=portion_saved > else: > high=portion_saved > portion_saved=(high+low) / 2 > print(portion_saved) > > The result is the same as the answer in the book that had the problem. > > but if i run the code: > > low=0 > high=10000 > epsilon=100 > portion_saved=0 > while abs(250,000 - portion_saved * 566,774) >= epsilon: > if portion_saved * 566,774 < 250,000 : > low=portion_saved > else: > high=portion_saved > portion_saved=(high+low) // 2 > print(portion_saved) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alan.gauld at yahoo.co.uk Sat Feb 27 06:31:36 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 27 Feb 2021 11:31:36 +0000 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: On 27/02/2021 07:09, ? wrote: > I tried - > > > def mufunc(**kwargs): > finaloutput = '' " > i = 0 > while i < len(kwargs): > if i%2 == 0: > finaloutput = finaloutput + kwargs[i].upper() > else: > finaloutput = finaloutput + kwargs[i].lower() > i = i + 1 > > return finaloutput > > myfunc('Anthropomorphism') > > > It's not working. Please guide me. "Its not working" is not very helpful, please tell us what doesn't work. In particular send us any error messages(in full) that you get as they contain lots of useful information. In this case the problem lies in the line: finaloutput = '' " Your quotes do not match so python doesn't know what it means. You should get a syntax error message highlighting that line. You have another problem in that your function is called mufunc() rather than myfunc() These details are trivial for humans but critically important for computers. More importantly you say you want **kwargs as a parameter. I suspect you copied that from somewhere and don't understand what it means. Just remove the ** from kwargs with for now. Finally, you need to print the result of your function call to see the result. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alan.gauld at yahoo.co.uk Sat Feb 27 06:33:49 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 27 Feb 2021 11:33:49 +0000 Subject: [Tutor] Python problem In-Reply-To: References: Message-ID: On 27/02/2021 11:19, Alan Gauld via Tutor wrote: > On 27/02/2021 07:23, Vakhtang Kvaratskhelia wrote: > >> What i mean is that if i write the code (for simplicity i have >> precalculated variables mentioned in the previous emails and narrowed the >> code down to the following ) : > > I'm confused. I'd expect both versions to give an infinite loop since > you never change portion_saved and it is equal to zero. So the while > loop will always evaluate to the same result. OOPS! I just spotted that you do in fact modify the portion_saved. Ignore that. But i'm still confused about how the commas are working. If someone with a comma sensitive locale could explain I'd be interested. >> low=0 >> high=10000 >> epsilon=100 >> portion_saved=0 >> while abs(250,000 - portion_saved * 566,774) >= epsilon: >> if portion_saved * 566,774 < 250,000 : >> low=portion_saved >> else: >> high=portion_saved >> portion_saved=(high+low) // 2 Sorry, I missed that line somehow! >> print(portion_saved) -- 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 breamoreboy at gmail.com Sat Feb 27 09:15:36 2021 From: breamoreboy at gmail.com (Mark Lawrence) Date: Sat, 27 Feb 2021 14:15:36 +0000 Subject: [Tutor] Python problem In-Reply-To: References: Message-ID: <4cc9231f-729f-f4fd-f124-daa50730934b@gmail.com> On 27/02/2021 13:32, Dennis Lee Bieber wrote: > On Sat, 27 Feb 2021 11:23:19 +0400, Vakhtang Kvaratskhelia > declaimed the following: > > >> low=0 >> high=10000 >> epsilon=100 >> portion_saved=0 >> while abs(250,000 - portion_saved * 566,774) >= epsilon: >> if portion_saved * 566774 < 250,000 : >> low=portion_saved >> else: >> high=portion_saved >> portion_saved=(high+low) / 2 >> print(portion_saved) >> >> The result is the same as the answer in the book that had the problem. >> >> but if i run the code: >> >> low=0 >> high=10000 >> epsilon=100 >> portion_saved=0 >> while abs(250,000 - portion_saved * 566,774) >= epsilon: >> if portion_saved * 566,774 < 250,000 : >> low=portion_saved >> else: >> high=portion_saved >> portion_saved=(high+low) // 2 >> print(portion_saved) >> >> Where i use "//" instead of "/" i get infinite loop because the "//" gets >> stuck on one particular guess and doesn't continue the search as the "/" >> division would. >> > > In both versions, you NEVER TEST for high/low converging on a value. > Your WHILE loop is testing on the monetary (?) computation, and I don't > know what units epsilon is supposed to be -- scaled integer for "1.00", or > an actual difference of 100(currency units). > > Reread your "hint"... > >> "Because we are searching for a value that is in principle a float, we are >> going to limit ourselves to two decimals of accuracy (i.e., we may want to >> save at 7.04% or 0.0704 in decimal ? but we are not going to worry about >> the difference between 7.041% and 7.039%). This means we can search for an >> integer between 0 and 10000 (using integer division), and then convert it >> to a decimal percentage (using float division) to use when we are > > The convergence is on the scaled %age, NOT on the monetary value you > compute. The binary search is for %, not money result. > > You have to determine how close high and low can be to each other to > count as "same value". Note that 7.039..7.041 spans a range of 0.002. So: > abs(high - low) < 0.002 would indicate you are done with the loop. Now > scale that to your integer version (which is going to be a problem -- using > the suggested scale means the above would be 703.9..704.1, but you don't > have the .9/.1 -- your smallest epsilon is 1 [704 - 703] corresponding to > 0.01%, not the 0.002% of the example) > > Note: the number of steps taken will be consistent, and based solely > upon how many significant digits you are carrying: 0..100% with epsilon 1% > will always take 7 steps. 0..100% with epsilon 0.1% will take 10 steps. > Binary search, 2^7 spans 0..127; 2^10 spans 0..1024. This is why I state > that using scaled integers and integer division is futile -- the search > ends when the difference between high and low is less than your selected > epsilon value, so "infinite number of floating values between 0.0 .. 100.0" > just doesn't matter -- if epsilon is 1.0% (100 as scaled integer) the loop > will take 7 steps; if 0.1% (10 as scaled integer) it will take 10 steps. > > The only justification I can see for using scaled integers is that one > is running on a microcontroller that does not have floating point > operations at all. > I do not see the purpose of all this analysis when the code given does not even run:- cat mytest.py low = 0 high = 10000 epsilon = 100 portion_saved = 0 while abs(250, 000 - portion_saved * 566, 774) >= epsilon: if portion_saved * 566774 < 250, 000: low = portion_saved else: high = portion_saved portion_saved = (high+low) / 2 print(portion_saved) mark at mark-HP-15-Notebook-PC:~/mypython$ python3 ./mytest.py File "./mytest.py", line 6 if portion_saved * 566774 < 250, 000: ^ SyntaxError: invalid syntax The call to abs gets away with it as there are three parameters listed, which is wrong anyway as abs only takes one argument. In fact check the difference between 566, 774 in the call to abs and 566774 in the line below. Correct all the problems and the output I get is 0.4410743713378906. Is that correct? I've no idea and I'm not bothered enough to check. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From roel at roelschroeven.net Sat Feb 27 11:19:11 2021 From: roel at roelschroeven.net (Roel Schroeven) Date: Sat, 27 Feb 2021 17:19:11 +0100 Subject: [Tutor] Python problem In-Reply-To: References: Message-ID: Alan Gauld via Tutor schreef op 27/02/2021 om 12:33: > On 27/02/2021 11:19, Alan Gauld via Tutor wrote: >> On 27/02/2021 07:23, Vakhtang Kvaratskhelia wrote: >> >>> What i mean is that if i write the code (for simplicity i have >>> precalculated variables mentioned in the previous emails and narrowed the >>> code down to the following ) : >> >> I'm confused. I'd expect both versions to give an infinite loop since >> you never change portion_saved and it is equal to zero. So the while >> loop will always evaluate to the same result. > > OOPS! I just spotted that you do in fact modify the portion_saved. > Ignore that. > > But i'm still confused about how the commas are working. If someone with > a comma sensitive locale could explain I'd be interested. The meaning of Python source code (or any programming language, I would hope) does not change depending on locale. My locale uses comma as decimal point; to demonstrate that Python is smart enough to ignore that and use the point as decimal point: >>> 3.14 + 2.7 5.84 Consequence is that Vakthangs code doesn't work, in exactly the way you predict: >>> 250,000 - portion_saved * 566,774 (250, 0, 774) >>> abs(250,000 - portion_saved * 566,774) Traceback (most recent call last): File "", line 1, in abs(250,000 - portion_saved * 566,774) TypeError: abs() takes exactly one argument (3 given) -- "Honest criticism is hard to take, particularly from a relative, a friend, an acquaintance, or a stranger." -- Franklin P. Jones Roel Schroeven From roel at roelschroeven.net Sat Feb 27 11:24:47 2021 From: roel at roelschroeven.net (Roel Schroeven) Date: Sat, 27 Feb 2021 17:24:47 +0100 Subject: [Tutor] How this list works In-Reply-To: <8b15095b-7e4b-e02d-37e9-f2d981073d54@yahoo.co.uk> References: <8b15095b-7e4b-e02d-37e9-f2d981073d54@yahoo.co.uk> Message-ID: Alan Gauld schreef op 22/02/2021 om 16:44: > Over the last few months I've had several emails asking me to delete > certain > posts (including one from mid 2018!). > > The usual reason for this is that the sender has included personal > details that > they do not wish published. I thought I'd better make it clear how the list > works and the limits of our (the moderators) abilities. > > [...] There is an automated response sent to people new to the Tutor list, or who have not posted in a while. Maybe it's an idea to add a section about this issue to that automated response? By the time people get that automated response the harm is already done, but at least they should then be aware for future posts. -- "Honest criticism is hard to take, particularly from a relative, a friend, an acquaintance, or a stranger." -- Franklin P. Jones Roel Schroeven From alan.gauld at yahoo.co.uk Sat Feb 27 18:27:18 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 27 Feb 2021 23:27:18 +0000 Subject: [Tutor] How this list works In-Reply-To: References: <8b15095b-7e4b-e02d-37e9-f2d981073d54@yahoo.co.uk> Message-ID: On 27/02/2021 16:24, Roel Schroeven wrote: > There is an automated response sent to people new to the Tutor list, or > who have not posted in a while. Maybe it's an idea to add a section > about this issue to that automated response? I did consider that, but its already quite long and I suspect most newbies don't bother reading it anyway! Plus its not a unique issue to this list... > By the time people get that > automated response the harm is already done, but at least they should > then be aware for future posts. There is that. I will see if I can put together a single sentence that succinctly makes it clear. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alan.gauld at yahoo.co.uk Sun Feb 28 10:20:12 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 28 Feb 2021 15:20:12 +0000 Subject: [Tutor] How this list works In-Reply-To: References: <8b15095b-7e4b-e02d-37e9-f2d981073d54@yahoo.co.uk> Message-ID: On 27/02/2021 23:27, Alan Gauld via Tutor wrote: > There is that. I will see if I can put together a single sentence > that succinctly makes it clear. A single paragraph has been added: ----- Also, remember that this is a public email list and anything you send to it will be sent out to hundreds of internet users some of whom republish the messages on archive web sites. Once it leaves our server we have no control over it and cannot retrieve or delete it. Do not include any confidential or private details that you do not want to be seen on the public internet. ------ -- 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 hongerlapjes at gmail.com Sun Feb 28 16:04:25 2021 From: hongerlapjes at gmail.com (hongerlapjes) Date: Sun, 28 Feb 2021 22:04:25 +0100 Subject: [Tutor] elif statement doesn't work Message-ID: For school I have to make a simple roulette game. If the user inputs "STOP", the game starts. But I cant seem to make the input STOP. ValueError: invalid literal for int() with base 10: 'STOP' What am I doing wrong? """ ROULETTESPEL """ import random invoer = ["STOP"] for x in range(37): invoer.append(x) getallen = [] def roulette (): print("Rien ne va plus.") winnendgetal=(random.randint(0,36)) print("De uitkomst is", winnendgetal) for winnendgetal in getallen: fiches +=35 print("Je hebt gewonnen en je hebt nu" , fiches, "fiches.") else: if fiches == 0: print("GAME OVER") def start (): fiches = 10 print("Je hebt", fiches, "fiches!") while fiches > 0: inzet=input("Op welk getal wil je inzetten?"'\n') print(inzet) if int(inzet) >= 0 and int(inzet) <= 36 : fiches -= 1 getallen.append(inzet) print(getallen) print("Je hebt nog", fiches, "fiches over.") elif str(inzet) == "STOP" : roulette() else: start() start () roulette () From alan.gauld at yahoo.co.uk Sun Feb 28 18:41:35 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 28 Feb 2021 23:41:35 +0000 Subject: [Tutor] elif statement doesn't work In-Reply-To: References: Message-ID: On 28/02/2021 21:04, hongerlapjes wrote: > For school I have to make a simple roulette game. > If the user inputs "STOP", the game starts. But I cant seem to make the > input STOP. ValueError: invalid literal for int() with base 10: 'STOP' > What am I doing wrong? The error message says you are passing the string "STOP" to the int() conversion function. int() does not know how to convert "STOP" to an integer, so you get a ValueError. This is a common problem when you mix input types. You have to either test the type before converting or catch the error and deal with it there. The other option is not to convert the digits but compare them with string numbers. But that really only works for equality tests not for <>. > def start (): > fiches = 10 > print("Je hebt", fiches, "fiches!") > > while fiches > 0: > inzet=input("Op welk getal wil je inzetten?"'\n') > print(inzet) > > if int(inzet) >= 0 and int(inzet) <= 36 : > fiches -= 1 > getallen.append(inzet) > print(getallen) > print("Je hebt nog", fiches, "fiches over.") > > elif str(inzet) == "STOP" : > roulette() Since "STOP" seems to be the only string you accept the simple solution in this case is to move the test for "STOP" before the conversion to int(). If inzet is not "STOP" then you should be safe to convert it to an integer. -- 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