From DerekSmith at racksquared.com Fri Sep 1 09:42:04 2017 From: DerekSmith at racksquared.com (Derek Smith) Date: Fri, 1 Sep 2017 13:42:04 +0000 Subject: [Tutor] intro book for python Message-ID: Hello! I am new to python coming from a Perl and Unix Admin background. I reviewed the site https://wiki.python.org/moin/IntroductoryBooks and would like to know which book(s) you all recommended for a newbie? Thank you! From mats at wichmann.us Fri Sep 1 13:43:56 2017 From: mats at wichmann.us (Mats Wichmann) Date: Fri, 1 Sep 2017 11:43:56 -0600 Subject: [Tutor] intro book for python In-Reply-To: References: Message-ID: <30392d7a-bf5e-e197-3648-b7ac2de26617@wichmann.us> On 09/01/2017 07:42 AM, Derek Smith wrote: > > Hello! > > I am new to python coming from a Perl and Unix Admin background. I reviewed the site https://wiki.python.org/moin/IntroductoryBooks and would like to know which book(s) you all recommended for a newbie? Hi Derek, welcome to Python. We don't recommend anything on the wiki, it wouldn't really be fair, there are so many books written by dedicated Python enthusiasts, how could we seem to prefer one over another? Some publishers are more diligent than others at adding everything they publish, but that's not really representative of quality, either. That said, there's obviously more to choose from than one could possibly get through. There ARE forums where people will be more than happy to offer an opinion, some of those were listed in the autoreply. The python tutor mailing list (of which some of the webmasters are also participants) will be happy to help you along as you run into questions. Stack Overflow is also a good source for answers to questions, although there it's best to search first, or you'll get shut down with the somewhat unhelpful "this question has already been asked and answered" ding. From mats at wichmann.us Fri Sep 1 13:46:09 2017 From: mats at wichmann.us (Mats Wichmann) Date: Fri, 1 Sep 2017 11:46:09 -0600 Subject: [Tutor] intro book for python In-Reply-To: <30392d7a-bf5e-e197-3648-b7ac2de26617@wichmann.us> References: <30392d7a-bf5e-e197-3648-b7ac2de26617@wichmann.us> Message-ID: <614b4fef-e078-3012-d5e3-be5a524b0255@wichmann.us> Okay, this reply makes it seem like I was really confused. My mail client did a weird thing, made it look to me like this question was on the Python webmaster list, where I also hang out - now it's resorted and I see it was already sent to tutor. Sorry about confusing anyone else :) And welcome! On 09/01/2017 11:43 AM, Mats Wichmann wrote: > On 09/01/2017 07:42 AM, Derek Smith wrote: >> >> Hello! >> >> I am new to python coming from a Perl and Unix Admin background. I reviewed the site https://wiki.python.org/moin/IntroductoryBooks and would like to know which book(s) you all recommended for a newbie? > > Hi Derek, welcome to Python. > > We don't recommend anything on the wiki, it wouldn't really be fair, > there are so many books written by dedicated Python enthusiasts, how > could we seem to prefer one over another? Some publishers are more > diligent than others at adding everything they publish, but that's not > really representative of quality, either. > > That said, there's obviously more to choose from than one could possibly > get through. There ARE forums where people will be more than happy to > offer an opinion, some of those were listed in the autoreply. > > The python tutor mailing list (of which some of the webmasters are also > participants) will be happy to help you along as you run into questions. > Stack Overflow is also a good source for answers to questions, although > there it's best to search first, or you'll get shut down with the > somewhat unhelpful "this question has already been asked and answered" ding. > > > From raghunadhprasad at gmail.com Fri Sep 1 13:51:56 2017 From: raghunadhprasad at gmail.com (Raghunadh) Date: Fri, 01 Sep 2017 12:51:56 -0500 Subject: [Tutor] intro book for python Message-ID: Hello Derek, I would start with this book https://learnpythonthehardway.org Raghunadh On September 1, 2017, at 12:35 PM, Derek Smith wrote: Hello! I am new to python coming from a Perl and Unix Admin background. I reviewed the site https://wiki.python.org/moin/IntroductoryBooks and would like to know which book(s) you all recommended for a newbie? Thank you! _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From kwpolska at gmail.com Fri Sep 1 15:08:51 2017 From: kwpolska at gmail.com (Chris Warrick) Date: Fri, 1 Sep 2017 21:08:51 +0200 Subject: [Tutor] intro book for python In-Reply-To: References: Message-ID: I would recommend reading the official Python tutorial [0] This tutorial will explain the important parts of Python. It doesn?t spend too much time explaining programming basics though. My alternate recommendations include Think Python [1] or Automate the Boring Stuff with Python [2]. On 1 September 2017 at 19:51, Raghunadh wrote: > Hello Derek, > > I would start with this book > > hxxps://learnpythonthehardway.org > > Raghunadh LPTHW is a terrible book: slow and boring, tells readers to memorize truth tables instead of understanding them (sic!), 19% of it is thoughtlessly teaching print() ? overall, a failure at teaching people to program. Moreover, the author wrote a nonsensical essay bashing Python 3 [3] (debunked in [4]), and released a Python 3.6 version of his book shortly afterwards. [0] https://docs.python.org/3/tutorial/index.html [1] http://greenteapress.com/wp/think-python-2e/ [2] https://automatetheboringstuff.com/ [3] https://learnpythonthehardway.org/book/nopython3.html [4] https://eev.ee/blog/2016/11/23/a-rebuttal-for-python-3/ -- Chris Warrick PGP: 5EAAEA16 From mats at wichmann.us Fri Sep 1 16:06:36 2017 From: mats at wichmann.us (Mats Wichmann) Date: Fri, 1 Sep 2017 14:06:36 -0600 Subject: [Tutor] intro book for python In-Reply-To: References: Message-ID: <8898834f-5ba0-4ed9-6b74-02ae5f40d0bf@wichmann.us> On 09/01/2017 01:08 PM, Chris Warrick wrote: > I would recommend reading the official Python tutorial [0] This > tutorial will explain the important parts of Python. It doesn?t spend > too much time explaining programming basics though. > > My alternate recommendations include Think Python [1] or Automate the > Boring Stuff with Python [2]. > > > On 1 September 2017 at 19:51, Raghunadh wrote: >> Hello Derek, >> >> I would start with this book >> >> hxxps://learnpythonthehardway.org >> >> Raghunadh > > LPTHW is a terrible book: slow and boring, tells readers to memorize > truth tables instead of understanding them (sic!), 19% of it is > thoughtlessly teaching print() ? overall, a failure at teaching people > to program. Moreover, the author wrote a nonsensical essay bashing > Python 3 [3] (debunked in [4]), and released a Python 3.6 version of > his book shortly afterwards. So I pointed out people would have strong opinions :) Turns out - I had forgotten this though I saw it once long ago - that some people tried to put together a page for people coming from Perl. https://wiki.python.org/moin/PerlPhrasebook that might possibly help setting some early concepts, and "idioms", into place; it certainly won't replace a good Python book, but still... From gvmcmt at gmail.com Fri Sep 1 14:14:55 2017 From: gvmcmt at gmail.com (Sri Kavi) Date: Fri, 1 Sep 2017 23:44:55 +0530 Subject: [Tutor] intro book for python In-Reply-To: References: Message-ID: I would choose to learn from The Python Tutorial: https://docs.python.org/3/tutorial/ On Sep 1, 2017 23:06, "Derek Smith" wrote: Hello! I am new to python coming from a Perl and Unix Admin background. I reviewed the site https://wiki.python.org/moin/IntroductoryBooks and would like to know which book(s) you all recommended for a newbie? Thank you! _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From mohamed.sidik at yahoo.co.uk Fri Sep 1 11:37:20 2017 From: mohamed.sidik at yahoo.co.uk (Mohamed Sidik) Date: Fri, 01 Sep 2017 19:37:20 +0400 Subject: [Tutor] Oauth 2 Message-ID: Hello all, can someone advice on any easy to follow resources of python and oauth 2. Thanks, Mo Sent from my Sony Xperia? smartphone From rampappula at gmail.com Fri Sep 1 16:48:06 2017 From: rampappula at gmail.com (ramakrishna reddy) Date: Fri, 1 Sep 2017 13:48:06 -0700 Subject: [Tutor] Variable name containing '-' Message-ID: Hi there, I am implementing a soap webservice where a method which has keyword parameter(named:abc-xyz) needs to be used to call third party app. eg: obj.method(abc-xyz=10) As python does not support '-' in variable names, I am a bit confused. I know in case of methods we can use getattr(self.object, 'method-name')() can be used. But in case of variable how we can use ? Thanks, Ram. From steve at pearwood.info Fri Sep 1 20:04:06 2017 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 2 Sep 2017 10:04:06 +1000 Subject: [Tutor] Variable name containing '-' In-Reply-To: References: Message-ID: <20170902000406.GE13110@ando.pearwood.info> On Fri, Sep 01, 2017 at 01:48:06PM -0700, ramakrishna reddy wrote: > Hi there, > > I am implementing a soap webservice where a method which has keyword > parameter(named:abc-xyz) needs to be used to call third party app. Just because your webservice offers a parameter called "foo" doesn't mean your Python code has to use the same name "foo". Pick a different name. Or change your webservice to use abc_xyz instead. > eg: obj.method(abc-xyz=10) > > As python does not support '-' in variable names, I am a bit confused. > I know in case of methods we can use getattr(self.object, 'method-name')() > can be used. But in case of variable how we can use ? It is an accident that getattr(self.object, 'method-name') works. You cannot rely on it, it is not supported by the language, and although it works today it may not work in the future. -- Steve From __peter__ at web.de Sat Sep 2 03:45:55 2017 From: __peter__ at web.de (Peter Otten) Date: Sat, 02 Sep 2017 09:45:55 +0200 Subject: [Tutor] Variable name containing '-' References: Message-ID: ramakrishna reddy wrote: > Hi there, > > I am implementing a soap webservice where a method which has keyword > parameter(named:abc-xyz) needs to be used to call third party app. > > eg: obj.method(abc-xyz=10) > > As python does not support '-' in variable names, I am a bit confused. > I know in case of methods we can use getattr(self.object, 'method-name')() > can be used. But in case of variable how we can use ? There might be an official way to deal with this problem. What SOAP library are you using? Does its documentation say something about this case? If there is no good answer you may have to resort to the hackish obj.method(**{"abc-xyz": 10}) From breamoreboy at yahoo.co.uk Sun Sep 3 00:10:24 2017 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sun, 3 Sep 2017 05:10:24 +0100 Subject: [Tutor] intro book for python In-Reply-To: References: Message-ID: On 01/09/17 18:51, Raghunadh wrote: > Hello Derek, > > I would start with this book > > https://learnpythonthehardway.org > > Raghunadh > I cannot recommend anything from the author of LPTHW after he had the audacity to write this https://learnpythonthehardway.org/book/nopython3.html about Python 3, in addition to which there are several vastly superior books and/or tutorials anyway. Kindest regards. Mark Lawrence. From leamhall at gmail.com Sun Sep 3 06:02:08 2017 From: leamhall at gmail.com (Leam Hall) Date: Sun, 3 Sep 2017 06:02:08 -0400 Subject: [Tutor] Python 3 for Beginners was: (Re: intro book for python) In-Reply-To: References: Message-ID: <707b18e9-f078-b944-9983-e4b7b6f531c2@gmail.com> On 09/03/2017 12:10 AM, Mark Lawrence via Tutor wrote: > On 01/09/17 18:51, Raghunadh wrote: >> Hello Derek, >> >> I would start with this book >> >> https://learnpythonthehardway.org >> >> Raghunadh >> > > I cannot recommend anything from the author of LPTHW after he had the > audacity to write this > https://learnpythonthehardway.org/book/nopython3.html about Python 3, in > addition to which there are several vastly superior books and/or > tutorials anyway. > > Kindest regards. > > Mark Lawrence. There are lots of other books on Python, that's true. "Practical Programming" (Gries, Campbell, Montojo) is one I use. Are you going to toss "Learning Python" since Mark points out some of python's drift from it's core values? I appreciate that link. Zed's right. Python 3 isn't used by the OS tools on Red Hat, and that's that major Linux vendor in the US. Has SuSE converted all OS tools to require Python 3? Ubuntu 18 is supposed to be Python 3 only, whenever it gets out. Anyone that uses python on Linux has to use Python 2. That means Python 3 is just one more language that requires work to install and maintain. I'm not seeing the benefits. How long has Python 3 been out? How many others are seeing the benefits of total change? When will people who say "you should upgrade" realize it's weeks or months of work with no real reason to do so? Yesterday I was coding and had to work around Python 3 dict.keys() returning a "dict_keys" type vs a list. Why did we need another type for this? I'm a coding beginner. I can talk a decent game in a few languages like python but I'm not experienced enough or smart enough to deal with these sorts of problems easily. Returning a new type, without significant benefit, makes it harder for people to progress in the language. Some years ago I wanted to play with an IRC bot sort of thing. Someone on Freenode #python mentioned Twisted so I got that and started playing. Half an hour, maybe forty five minutes later and my little project did what I was trying to do. This was before I really knew any python; the language was that clean and easy to learn. I'd prefer Zed be wrong about Python dying. I'm not willing to put any money on it though. Leam From alan.gauld at yahoo.co.uk Sun Sep 3 09:39:43 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 3 Sep 2017 14:39:43 +0100 Subject: [Tutor] Python 3 for Beginners was: (Re: intro book for python) In-Reply-To: <707b18e9-f078-b944-9983-e4b7b6f531c2@gmail.com> References: <707b18e9-f078-b944-9983-e4b7b6f531c2@gmail.com> Message-ID: On 03/09/17 11:02, Leam Hall wrote: > Anyone that uses python on Linux has to use Python 2. Errr, nope. I use python 3 on Linux all the time. My last significant Python 2 program was about 4 years ago. It may be true that the distro maintainers are still using python 2 for historical reasons but that's got no relevance to whether a Linux user would want to use Python 3 for their own programs - even ones accessing the OS. > I'm not seeing the benefits. How long has Python 3 been out? How many > others are seeing the benefits of total change? It's not a huge advantage, many of the changes in Python 3 are theoretically based, cleaning up of how Python does things to make them fit with what Computer Science teaches. That often means that it becomes harder to use, but the long term payoff is a better foundation for building new things in the future. We may only see the real benefit of Python 3 when Python 4 comes out - which based on history should be in 2020! > "you should upgrade" realize it's weeks or months of work with no real > reason to do so? I would not advocate converting all the existing code to v3 just for the sake of it, but there is a good case for converting programs as you upgrade them. Doing a single application isn't a big hit if you are already doing work on it. > Yesterday I was coding and had to work around Python 3 dict.keys() > returning a "dict_keys" type vs a list. Why did we need another type for > this? Because it makes the whole ecosystem more consistent and ultimately more extendible. But I agree in purely pragmatic terms, and especially for new programmers without a CS background, Python 3 is much, much, harder to come to terms with. And things like dict views and iterators instead of simple lists are a big part of that. If I was writing my original "Learn to Program" book now, I'm not sure I'd still pick Python. On the other hand, I can't think of any other language that is a lot better for teaching raw beginners, while still being useful in the real world. > these sorts of problems easily. Returning a new type, without > significant benefit, makes it harder for people to progress in the > language. The difference is that python has entered the world of computing professionals. Its now being used for industrial scale projects by programming professionals who have a CS background. They expect the language to behave in certain ways. They expect it to be able to handle large data volumes without crashing (huge lists getting loaded into memory for example) Many of the changes accommodate that audience rather than the total newbie. > language was that clean and easy to learn. It was easy to learn but it had lots of not so nice things inside. Python 3 has tried to tidy itself up, and industrialize itself but does so at the expense of adding complexity for beginners. -- 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 Sep 3 09:55:56 2017 From: mats at wichmann.us (Mats Wichmann) Date: Sun, 3 Sep 2017 07:55:56 -0600 Subject: [Tutor] Python 3 for Beginners was: (Re: intro book for python) In-Reply-To: <707b18e9-f078-b944-9983-e4b7b6f531c2@gmail.com> References: <707b18e9-f078-b944-9983-e4b7b6f531c2@gmail.com> Message-ID: On 09/03/2017 04:02 AM, Leam Hall wrote: > Anyone that uses python on Linux has to use Python 2. Every current distro I know of has a python 3 package, plus lots and lots of add-ons in python 3 mode. It's quite easy to use python 3 as a result... and if that doesn't work you can install your own to your workspace (rather than as a "system package") just as easily. From david at graniteweb.com Sun Sep 3 10:06:40 2017 From: david at graniteweb.com (David Rock) Date: Sun, 3 Sep 2017 09:06:40 -0500 Subject: [Tutor] Python 3 for Beginners was: (Re: intro book for python) In-Reply-To: References: <707b18e9-f078-b944-9983-e4b7b6f531c2@gmail.com> Message-ID: > On Sep 3, 2017, at 08:55, Mats Wichmann wrote: > > On 09/03/2017 04:02 AM, Leam Hall wrote: > >> Anyone that uses python on Linux has to use Python 2. > > Every current distro I know of has a python 3 package, plus lots and > lots of add-ons in python 3 mode. It's quite easy to use python 3 as a > result... and if that doesn't work you can install your own to your > workspace (rather than as a "system package") just as easily. While a bit anecdotal, the complaint is valid. Red Hat does not support Python 3 in their base installation on RHEL and the only way to get it ?officially? (i.e., directly from Red hat) is through Software Collections, which is a bit cumbersome to use and not available in all cases. There are a lot of places (including where I work) that frown heavily on using packages that aren?t from the base repositories from the OS maintainers. Installing into ?your own workspace? isn?t so easy when you are talking about 2000 servers, either. In order for me to write code that I know will work across all the servers, I have to use python 2 because I know it will be there. I can?t say the same thing about python 3, and that?s not something I?m likely to be able to fix within the bounds of company procedures. So while the original statement is obviously false, the heart of why it was said is functionally true. ? David Rock david at graniteweb.com From kwpolska at gmail.com Sun Sep 3 11:04:58 2017 From: kwpolska at gmail.com (Chris Warrick) Date: Sun, 3 Sep 2017 17:04:58 +0200 Subject: [Tutor] Python 3 for Beginners was: (Re: intro book for python) In-Reply-To: <707b18e9-f078-b944-9983-e4b7b6f531c2@gmail.com> References: <707b18e9-f078-b944-9983-e4b7b6f531c2@gmail.com> Message-ID: On 3 September 2017 at 12:02, Leam Hall wrote: mmend anything from the author of LPTHW after he had the >> audacity to write this https://learnpythonthehardway.org/book/nopython3.html >> about Python 3, in addition to which there are several vastly superior books >> and/or tutorials anyway. >> >> Kindest regards. >> >> Mark Lawrence. > > > There are lots of other books on Python, that's true. "Practical > Programming" (Gries, Campbell, Montojo) is one I use. > > Are you going to toss "Learning Python" since Mark points out some of > python's drift from it's core values? > > I appreciate that link. Zed's right. Many of Zed?s argument are false and plain BS: https://eev.ee/blog/2016/11/23/a-rebuttal-for-python-3/ > Python 3 isn't used by the OS tools on Red Hat, and that's that major Linux vendor in the US. This will change in RHEL 8, whenever that comes out: yum was replaced by dnf a few versions of Fedora ago, and that?s written in Python 3. > Anyone that uses python on Linux has to use Python 2. That means Python 3 is > just one more language that requires work to install and maintain. I'm not > seeing the benefits. How long has Python 3 been out? How many others are > seeing the benefits of total change? When will people who say "you should > upgrade" realize it's weeks or months of work with no real reason to do so? You?re getting: sane Unicode support, f"strings", type hinting, pathlib, asyncio, and a few more improvements. > Yesterday I was coding and had to work around Python 3 dict.keys() returning > a "dict_keys" type vs a list. Why did we need another type for this? I'm a > coding beginner. Performance and resource usage. If you use a list, Python needs to do some extra work to convert internal data structures of a dict into a list, and also store that new list in memory. > I can talk a decent game in a few languages like python but > I'm not experienced enough or smart enough to deal with these sorts of > problems easily. Returning a new type, without significant benefit, makes it > harder for people to progress in the language. There are a lot of things that return iterators or other fancy types instead of lists in Python 3 (eg. zip, range, all of itertools). You can always iterate over those like you would, and if you need a list for some reason, you can just call list(the_thing). > Some years ago I wanted to play with an IRC bot sort of thing. Someone on > Freenode #python mentioned Twisted so I got that and started playing. Half > an hour, maybe forty five minutes later and my little project did what I was > trying to do. This was before I really knew any python; the language was > that clean and easy to learn. You can still do that with Python 3. (Although you?ll be better off using asyncio and some IRC lib for that.) -- Chris Warrick PGP: 5EAAEA16 From leamhall at gmail.com Sun Sep 3 11:43:38 2017 From: leamhall at gmail.com (Leam Hall) Date: Sun, 3 Sep 2017 11:43:38 -0400 Subject: [Tutor] Python 3 for Beginners was: (Re: intro book for python) In-Reply-To: References: <707b18e9-f078-b944-9983-e4b7b6f531c2@gmail.com> Message-ID: On 09/03/2017 10:06 AM, David Rock wrote: > >> On Sep 3, 2017, at 08:55, Mats Wichmann wrote: >> >> On 09/03/2017 04:02 AM, Leam Hall wrote: >> >>> Anyone that uses python on Linux has to use Python 2. >> >> Every current distro I know of has a python 3 package, plus lots and >> lots of add-ons in python 3 mode. It's quite easy to use python 3 as a >> result... and if that doesn't work you can install your own to your >> workspace (rather than as a "system package") just as easily. > > While a bit anecdotal, the complaint is valid. Red Hat does not support Python 3 in their base installation on RHEL and the only way to get it ?officially? (i.e., directly from Red hat) is through Software Collections, which is a bit cumbersome to use and not available in all cases. There are a lot of places (including where I work) that frown heavily on using packages that aren?t from the base repositories from the OS maintainers. > > Installing into ?your own workspace? isn?t so easy when you are talking about 2000 servers, either. In order for me to write code that I know will work across all the servers, I have to use python 2 because I know it will be there. I can?t say the same thing about python 3, and that?s not something I?m likely to be able to fix within the bounds of company procedures. > > So while the original statement is obviously false, the heart of why it was said is functionally true. Sorry; there's a significant gap between what I wrote and what I meant. My fault. If you use the OS system's Python then you're using Python 2. That will continue for the next five or more years. I'm in the same boat as David; installing Python 3 is a non-starter. That's not the case for a lot of folks, but if I'm going to write something there's a high probability that Python 3 won't be on the target machine. While I prefer 2 over 3, if I went in to work and everything was magically Python 3 based I wouldn't whine. Much. Sorry for the miscommunication earlier. Leam From gvmcmt at gmail.com Sun Sep 3 13:34:17 2017 From: gvmcmt at gmail.com (Sri Kavi) Date: Sun, 3 Sep 2017 23:04:17 +0530 Subject: [Tutor] Python 3 for Beginners was: (Re: intro book for python) In-Reply-To: References: <707b18e9-f078-b944-9983-e4b7b6f531c2@gmail.com> Message-ID: Hi guys, this mailing list is for people trying to learn Python and this list is here to help us all. It?s one of the hundreds of programming languages in use today. Use it as a tool and see if it can help you accomplish your goal. If it fails to serve you, you?ve so many choices. Every language has its advantages and issues. On Sun, Sep 3, 2017 at 9:13 PM, Leam Hall wrote: > On 09/03/2017 10:06 AM, David Rock wrote: > >> >> On Sep 3, 2017, at 08:55, Mats Wichmann wrote: >>> >>> On 09/03/2017 04:02 AM, Leam Hall wrote: >>> >>> Anyone that uses python on Linux has to use Python 2. >>>> >>> >>> Every current distro I know of has a python 3 package, plus lots and >>> lots of add-ons in python 3 mode. It's quite easy to use python 3 as a >>> result... and if that doesn't work you can install your own to your >>> workspace (rather than as a "system package") just as easily. >>> >> >> While a bit anecdotal, the complaint is valid. Red Hat does not support >> Python 3 in their base installation on RHEL and the only way to get it >> ?officially? (i.e., directly from Red hat) is through Software Collections, >> which is a bit cumbersome to use and not available in all cases. There are >> a lot of places (including where I work) that frown heavily on using >> packages that aren?t from the base repositories from the OS maintainers. >> >> Installing into ?your own workspace? isn?t so easy when you are talking >> about 2000 servers, either. In order for me to write code that I know will >> work across all the servers, I have to use python 2 because I know it will >> be there. I can?t say the same thing about python 3, and that?s not >> something I?m likely to be able to fix within the bounds of company >> procedures. >> >> So while the original statement is obviously false, the heart of why it >> was said is functionally true. >> > > Sorry; there's a significant gap between what I wrote and what I meant. My > fault. > > If you use the OS system's Python then you're using Python 2. That will > continue for the next five or more years. I'm in the same boat as David; > installing Python 3 is a non-starter. That's not the case for a lot of > folks, but if I'm going to write something there's a high probability that > Python 3 won't be on the target machine. > > While I prefer 2 over 3, if I went in to work and everything was magically > Python 3 based I wouldn't whine. Much. > > Sorry for the miscommunication earlier. > > Leam > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From kckaiyer at gmail.com Tue Sep 5 03:56:52 2017 From: kckaiyer at gmail.com (Nathan Kckaiyer) Date: Tue, 5 Sep 2017 15:56:52 +0800 Subject: [Tutor] Executing "if :" function on terminal Message-ID: Dear Tutor, i have just started learning python. I have repeatedly hit a road block on my mac os terminal . Everytime i use the "if :" command i get syntax errors. i do not see any errors on text editor i am following instructions from a book Original input on editor >>> a=2 >>> if a==2 : print("correct") Last login: Tue Sep 5 15:43:03 on ttys000 Solariss-MacBook:~ Solaris$ /var/folders/Db/DbSciGOcFmyQ3L+HyzOb5E+++TI/-Tmp-/Cleanup\ At\ Startup/test-526290211.381.py.command ; exit; File "/Users/Solaris/Desktop/test.py", line 1 Python 3.0.1 (r301:69597, Feb 14 2009, 19:03:52) ^ SyntaxError: invalid syntax logout [Process completed] From alan.gauld at yahoo.co.uk Tue Sep 5 04:46:34 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 5 Sep 2017 09:46:34 +0100 Subject: [Tutor] Executing "if :" function on terminal In-Reply-To: References: Message-ID: On 05/09/17 08:56, Nathan Kckaiyer wrote: > i have just started learning python. I have repeatedly hit a road > block on my mac os terminal . Everytime i use the "if :" command i > get syntax errors. The problem is that you are somehow executing the Python interpreter prompt in the interpreter. WE need to know exactly how you are running the code. Its possible you are trying to execute the shell window of IDLE (or some other IDE) or that you are copy/pasting too much code from one window to another. But its impossible to guess. You need to tell us *exactly* how you run your code. What tools are you using? What buttons/menus you use etc. >>>> a=2 >>>> if a==2 : > print("correct") So far so good. That looks like you are typing into the interactive mode of the Python interpreter. Now if you just hit Return you should see "correct" being printed on the screen. > Solariss-MacBook:~ Solaris$ > /var/folders/Db/DbSciGOcFmyQ3L+HyzOb5E+++TI/-Tmp-/Cleanup\ At\ > Startup/test-526290211.381.py.command ; exit; > File "/Users/Solaris/Desktop/test.py", line 1 > Python 3.0.1 (r301:69597, Feb 14 2009, 19:03:52) > ^ > SyntaxError: invalid syntax But this looks like an OS terminal of some kind trying to execute a file called test.py But that file does not contain the Python code above, it includes the header printed by the interpreter in interactive mode. That is not executable code and so Python complains. test.py should only contain: a=2 if a==2 : print("correct") and nothing else. HTH -- 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 edmundopierre at yahoo.com Tue Sep 5 09:29:33 2017 From: edmundopierre at yahoo.com (edmundo pierre) Date: Tue, 5 Sep 2017 13:29:33 +0000 (UTC) Subject: [Tutor] I want to display Integral symbols References: <592785203.3050170.1504618173463.ref@mail.yahoo.com> Message-ID: <592785203.3050170.1504618173463@mail.yahoo.com> I have been trying to display mathematics symbols on my Tkinter (using Python) project, but was not successful .I am trying ?to compute integrals on my Tkinter project and will like to display this image on my tkinter:?2xdx......... Jupiter can display that symbol using sympy, but I can not display it in my Tkinter. Any help? From ruth.e.hardy at btinternet.com Tue Sep 5 14:25:15 2017 From: ruth.e.hardy at btinternet.com (Ruth Hardy) Date: Tue, 5 Sep 2017 19:25:15 +0100 Subject: [Tutor] Help With Python Tasks Message-ID: <30A31B59-E9E4-4AC8-9601-A64A06974D26@btinternet.com> Dear Sir or Madam I was wondering if you can help me please with these computer science tasks as I don?t know where to start: First Task Write code do the following things involving strings. Wherever possible, you should try to incorporate functions/procedures, although they are not appropriate for all tasks. Print a list of 20 random numbers one after the other vertically on the screen. Half way through, use a user input request to pause printing until input is received. Ask a user to enter two numbers, one after the other. Your code should validate that you receive numbers and prompt the user appropriately. For the: first number, use try-except to ensure you have a floating point number; place a comment in you code about how this works second number, use .isnumeric() to check you have an integer; place a comment in your code to explain why this only works for integers Use a forced type conversion to turn both numbers into floats, divided one by the other and print the answer to 2 d.p. You code should keep on asking for numbers until two have been received. Ask the user to input a true/false value and convert the input to a Boolean (hint: I don?t think you can do a forced type conversion for this?) Second Task Write a menu system based on functions where the main program loop is only 2 lines long Write a simple calculator program based around a menu loop. You should be able to enter ?number 1? and ?number 2? which will be displayed on the screen every time the menu is redisplayed?; there should also be a clear screen function used to avoid the screen getting too cluttered. The calculator menu should allow the user to: enter values for ?number 1? and ?number 2? add ?number 1? and ?number 2? subtract ?number 2? from ?number 1? multiply ?number 1? and ?number 2? divide ?number 1? by ?number 2? exit cleanly You program must display an answer that the user can read after each calculation. I would appreciate your help so much as I need to have a finished copy of this code by tomorrow late morning ! Kind Regards Miss Hardy From alan.gauld at yahoo.co.uk Tue Sep 5 19:07:26 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 6 Sep 2017 00:07:26 +0100 Subject: [Tutor] I want to display Integral symbols In-Reply-To: <592785203.3050170.1504618173463@mail.yahoo.com> References: <592785203.3050170.1504618173463.ref@mail.yahoo.com> <592785203.3050170.1504618173463@mail.yahoo.com> Message-ID: On 05/09/17 14:29, edmundo pierre via Tutor wrote: > I have been trying to display mathematics symbols on my Tkinter (using Python) project, > but was not successful So show us what you did. Its hard to figure out what's gone wrong when we can't see your code. And that includes telling us what kind of widget you are trying to display this on. A Label? a Text? a Canvas? Any of those are candidates but all have different ways of doing it. > .I am trying to compute integrals on my Tkinter project and will like to > display this image on my tkinter: > ?2xdx......... Are you trying to display an image? Or are you trying to display that as text? If text you need to pick the right font, one that has the integral sign included. You will also likely need to do a fair bit of tweaking with font sizes and line spacing. > Jupiter can display that symbol using sympy,> I can not display it in my Tkinter. We need more information. As a minimum: Which OS are you using? Which Python version? (and distribution if it includes Jupiter) What UI widget are you using? Are you displaying an image or text? If text which font are you using? Finally what does your code look like? What error messages(in full) do you get(if any) in the console? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alan.gauld at yahoo.co.uk Tue Sep 5 20:05:05 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 6 Sep 2017 01:05:05 +0100 Subject: [Tutor] Help With Python Tasks In-Reply-To: <30A31B59-E9E4-4AC8-9601-A64A06974D26@btinternet.com> References: <30A31B59-E9E4-4AC8-9601-A64A06974D26@btinternet.com> Message-ID: On 05/09/17 19:25, Ruth Hardy wrote: > I was wondering if you can help me please with these computer science tasks We can help, but we won;t do them for you. > First Task > > Write code do the following things involving strings. Wherever possible, > you should try to incorporate functions/procedures, > > Print a list of 20 random numbers one after the other vertically on the screen. There are (at least) two ways to do this. One very easy but laborious, the other much shorter and quicker. Do you know what they are? Can you print 20 numbers (forget random for now) on the screen? > Half way through, use a user input request to pause printing until > input is received. Do you know how to read user input? > Ask a user to enter two numbers, one after the other. This is slightly ambiguous, but I'd assume they mean you should ask for input twice. Do you know how to store user input into a variable? Do you know how to convert user input to a number? > Your code should validate that you receive numbers and > prompt the user appropriately. Do you know how to validate/check that the received input is a number? (there are several ways to do this, I'm not sure how they expect you to tackle it, it depends on what you've been taught so far) For the: > first number, use try-except to ensure you have a floating point number; OK, That answers my question. You can use try/except to check the number is valid... > place a comment in you code about how this works > second number, use .isnumeric() to check you have an integer; OK, They tell you what to do here. Personally I don;t like the isnumeric() technique but its what they want you to do so do it... > Use a forced type conversion to turn both numbers into floats, >divided one by the other and print the answer to 2 d.p. Do you know how to print a number so it shows two dp? > You code should keep on asking for numbers until two have been received. OK, Lets just get it working for a single pair of numbers first. Done that? Good, so now wrap it in a loop. What kind of loop will work here? > Ask the user to input a true/false value and convert th> input to a Boolean (hint: I don?t think you can do a forced > type conversion for this?) The hint is correct. How would you do it without a type conversion? > Second Task Let's leave this task to another email. See if you can answer the questions above. If you can you should be able to join the bits together to get the task completed. If not come back to us with specific issues sand we'll explain a bit more. Show us your code plus any error messages you get. HTH -- 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 wpmartin at gmail.com Wed Sep 6 01:34:25 2017 From: wpmartin at gmail.com (Pat Martin) Date: Tue, 5 Sep 2017 22:34:25 -0700 Subject: [Tutor] Select a string Message-ID: Hello all, I am trying to write a program for a programming class that finds the number of a specific string (bob) in a string of characters. I am using one of the sample strings they give me and it should find 2 instances of bob but my script returns 0. Since they want it to find 2 from the bobob in the string using "bob in s" doesn't work (it only returns 1). My eyes are crossing looking at the code, can someone give me a hint on what I am missing that causes this to not give me the correct answer of 2. #!/usr/bin/env python3 s = 'azcbobobegghakl' count = 0 theo = False firstb = False for i in s: if i == 'b': firstb == True if (i == 'b') and (theo == True): count += 1 if (i =='o') and (firstb == True): theo = True if (i != 'b') and (i != 'o'): theo = False firstb = False print(count) Thanks, Pat From ben+python at benfinney.id.au Wed Sep 6 01:54:31 2017 From: ben+python at benfinney.id.au (Ben Finney) Date: Wed, 06 Sep 2017 15:54:31 +1000 Subject: [Tutor] Select a string References: Message-ID: <851snkbcdk.fsf@benfinney.id.au> Pat Martin writes: > My eyes are crossing looking at the code, can someone give me a hint > on what I am missing that causes this to not give me the correct > answer of 2. It's good to admit that looking at the code has ceased to help :-) So, try a different approach. Without using the code, can you describe your algorithm? Make a pseudo-code description of how you expect to transform the input to the output. For example, I might describe the algorithm for reversing a text string:: * Given a text string named ?input_text? * Set the output, named ?output_text?, to the value "". * Set the index (named ?index?) to the character length of ?input_text?. * While ?index? is greater than 0: * Subtract 1 from ?index?. * Get the character at index ?index? of ?input_text?, assign the name ?input_char? to that character. * Append ?input_char? to ?output_text?. * Return the value of ?output_text?. Can you describe your algorithm that way, so we can see what is your intention for the code? -- \ ?Faith is the determination to remain ignorant in the face of | `\ all evidence that you are ignorant.? ?Shaun Mason | _o__) | Ben Finney From cs at cskk.id.au Wed Sep 6 01:49:52 2017 From: cs at cskk.id.au (Cameron Simpson) Date: Wed, 6 Sep 2017 15:49:52 +1000 Subject: [Tutor] Select a string In-Reply-To: References: Message-ID: <20170906054952.GA22006@cskk.homeip.net> On 05Sep2017 22:34, Pat Martin wrote: >I am trying to write a program for a programming class that finds the >number of a specific string (bob) in a string of characters. I am using one >of the sample strings they give me and it should find 2 instances of bob >but my script returns 0. Since they want it to find 2 from the bobob in the >string using "bob in s" doesn't work (it only returns 1). My eyes are >crossing looking at the code, can someone give me a hint on what I am >missing that causes this to not give me the correct answer of 2. > >#!/usr/bin/env python3 > >s = 'azcbobobegghakl' > >count = 0 >theo = False >firstb = False > >for i in s: > if i == 'b': > firstb == True This line is a boolean expression using "==", not an assignment using "=". As a consequence firstb is never set to True and the rest of the logic never fires. It is legal in Python to just put an expression on a line. It is usually worth putting in print() calls to debug things like this. I put: print(i, firstb, theo, count) at the start of the loop and learned that firstb never becomes true. I also put the same print statement at the bottom. The advantage of hte above print statement is that it shows you the character from the string along with each set of values. That way you can scan down the output to the "bob" part and look for correct behaviour. I that hadn't helped I've have stuck print after each "if" until the problem became glaringly obvious. BTW, it isn't good to write tests like: if theo == True: "theo" a Boolean anyway. Just say: if theo: It reads more naturally as well. Cheers, Cameron Simpson (formerly cs at zip.com.au) From benjsouthwell at gmail.com Wed Sep 6 03:34:26 2017 From: benjsouthwell at gmail.com (Ben Southwell) Date: Wed, 6 Sep 2017 17:34:26 +1000 Subject: [Tutor] Select a string In-Reply-To: <20170906054952.GA22006@cskk.homeip.net> References: <20170906054952.GA22006@cskk.homeip.net> Message-ID: May I suggest you use regular expressions? You can check it out here (take note of the difference between search, findall etc): https://docs.python.org/3/library/re.html Essentially it is a standard library built for matching patterns in strings. It is a powerful tool that is kind of it's own language in itself (but other languages have re libraries too so it's definitely worth learning). The magic of re is learning how to construct your patterns. re.findall() will return all non-overlapping matches of a pattern in a given string. However, the pattern I've used below will match overlapping bobs, this is because of the lookahead assertion (the "?=" part of the pattern). I won't go into regex anymore, it's something I believe you learn as you do otherwise its way too syntax to learn at once. DISCLAIMER: I have only just learnt regex myself lately so if anyone wants to charm in with a better method please to do so! Here is a snippet which solves your problem #!/usr/bin/env python3 import re pat = r'(?=(bob))' s = 'azcbobobegghakl' result = re.findall(pat, s) print(result) print(len(result)) On 6 September 2017 at 15:49, Cameron Simpson wrote: > On 05Sep2017 22:34, Pat Martin wrote: > >> I am trying to write a program for a programming class that finds the >> number of a specific string (bob) in a string of characters. I am using >> one >> of the sample strings they give me and it should find 2 instances of bob >> but my script returns 0. Since they want it to find 2 from the bobob in >> the >> string using "bob in s" doesn't work (it only returns 1). My eyes are >> crossing looking at the code, can someone give me a hint on what I am >> missing that causes this to not give me the correct answer of 2. >> >> #!/usr/bin/env python3 >> >> s = 'azcbobobegghakl' >> >> count = 0 >> theo = False >> firstb = False >> >> for i in s: >> if i == 'b': >> firstb == True >> > > This line is a boolean expression using "==", not an assignment using "=". > As a consequence firstb is never set to True and the rest of the logic > never fires. > > It is legal in Python to just put an expression on a line. > > It is usually worth putting in print() calls to debug things like this. I > put: > > print(i, firstb, theo, count) > > at the start of the loop and learned that firstb never becomes true. I > also put the same print statement at the bottom. > > The advantage of hte above print statement is that it shows you the > character from the string along with each set of values. That way you can > scan down the output to the "bob" part and look for correct behaviour. > > I that hadn't helped I've have stuck print after each "if" until the > problem became glaringly obvious. > > BTW, it isn't good to write tests like: > > if theo == True: > > "theo" a Boolean anyway. Just say: > > if theo: > > It reads more naturally as well. > > Cheers, > Cameron Simpson (formerly cs at zip.com.au) > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Best Regards, Ben Southwell From edmundopierre at yahoo.com Wed Sep 6 03:09:14 2017 From: edmundopierre at yahoo.com (edmundo pierre) Date: Wed, 6 Sep 2017 07:09:14 +0000 (UTC) Subject: [Tutor] Fw: I want to display Integral symbols In-Reply-To: References: <592785203.3050170.1504618173463.ref@mail.yahoo.com> <592785203.3050170.1504618173463@mail.yahoo.com> Message-ID: <1580748199.3726222.1504681754810@mail.yahoo.com> So show us what you did. Its hard to figure out what's gone wrong when we can't see your code:That is what I did: from sympy import*from tkinter import*from sympy import init_printing root = Tk()root.geometry("450x350+500+500")root.title("Factorisations/Simplifications")root.configure(background = "gray")#Difine variablevar1= StringVar()var2 =StringVar()#Create the function answer def Answer():? ? x = symbols("x")? ? A = var1.get()? ? B = factor(A)? ? Right_insert_top .insert(END,str(B)) ? ? #A1 = var2.get()? ? #B1 = factor(A1)? ? #Right_insert_top .insert(END,str(B1))#Create the function simplification def Simplification():? ? init_printing()? ? x= symbols("x")? ? D = var2.get()? ? E = expand(D)? ? Right_insert_top .insert(END,str((E))?? ??#Create the Factorisation frameTops = Frame(root,relief= "sunken")Tops.pack(side = TOP) #Create a Label of the TopTop_Label = Label(Tops,font=("arial",30,"bold"),text = "Factorisations/Simplifications",bd = 10)Top_Label.pack(side = "top",fill =BOTH ) #Create the left frameLeft = Frame(root,relief= "sunken")Left.pack(side = "left",fill =BOTH ) #Create the top left frameTop_left = Frame(Left,relief= "sunken")Top_left.pack(side ="top") #Create the Labels top and bottomLeft_top = Label(Top_left ,text ="Factorise: ",bd = 5)Left_top.pack(side= "left",fill =BOTH) top_entry = Entry(Top_left,textvariable = var1,bd = 5)top_entry.pack(side= "right",fill =X) #Create the bottom leftLeft_bottom = Frame(Left,relief= "sunken")Left_bottom.pack(side = "bottom",fill =BOTH ) Left_bottom_label = Label(Left_bottom,text ="Simplifie: ",bd = 5)Left_bottom_label.pack(side= "left") bottom_entry = Entry(Left_bottom ,textvariable = var2,bd = 5)bottom_entry.pack(side= "right",fill =X) #Create a Right side Frame Right = Frame (root,relief= "sunken")Right.pack(side = "right",fill = BOTH) #Create a Frame on the Top rightRight_top = Frame (Right,relief= "sunken")Right_top.pack(side = "top") #Create the bottom frame on the rightRight_bottom = Frame (Right,relief= "sunken")Right_bottom.pack(side = "bottom")#Create a insert text on the screen on the rightRight_insert_top = Text( Right_top,font=("arial",10,"bold"),bd= 5)Right_insert_top.pack() #Create the exit bottom#Right_exit_buton = Button(Right_bottom1 , text = "Exit", font=("arial",20,"bold"),bd = 5)#Right_exit_buton.pack(side ="right") #Create the Reset bottomRight_reset_buton = Button(Right_bottom , text = "Answer\nSimplifie ",command = Simplification, ?font=("arial",20,"bold"),bd = 5)Right_reset_buton.pack(side ="right")#Create the Answer buttonRight_answer_buton = Button(Right_bottom , text = "Answer\nFactorisasions",command= Answer, font=("arial",20,"bold"),bd = 5)Right_answer_buton.pack(side ="left") root.mainloop() I am using Window 7. I have two ?functions: Factorization and Simplification. I am using Tkinter (GUI). When I entered the polynomial :(x+3)**2 at the simplify box and when I clicked ANSWER SIMPLIFY, I got: x**2+6*x+9, which is Correct. But I do not like the syntax. I will like my syntax to be clear like this in my Tkinter:? (x?3)2? ? ? x-32x-32? and the answer to be like this:?x2+6x+9I am trying to display the answer : x2?6x+9on the Tkinter screen I created. I am using Python 3.6.1. I used the Integral as an example of how my answer should look like. My code run perfectly, you can try. Thank you! From edmundopierre at yahoo.com Wed Sep 6 02:39:53 2017 From: edmundopierre at yahoo.com (edmundo pierre) Date: Wed, 6 Sep 2017 06:39:53 +0000 (UTC) Subject: [Tutor] Help References: <550650043.3714240.1504679993689.ref@mail.yahoo.com> Message-ID: <550650043.3714240.1504679993689@mail.yahoo.com> Hi Python, I am trying to make a french to English dictionary. First of all, I made a dictionary with keys and values. Then I asked the user to enter a letter. If the letter correspond to the Keys, my print function should display the Key and value. But my code below, could not . Thank you! List ={"le":"Determinant: The. Pronoun: Him, Her, It, Them .. Example: vive la politique, vive l'amour:long live politics, long live love","de": "Preposition: Of, From, Some, Any.. Example: Il ne r?ve que d'argent et de plaisirs: he only dreams of money and pleasure","un":" Adjective,Noum, Pronoun: One..Article: A,An... Example: Je me suis cass? un ongle: I broke one of my fingernails","?": "Preposition: To,At,In. Example: Ils restent ? l'?cole le plus longtemps possible:they remain at school as long as possible","?tre": "Verb: To be..Noum: Being..Example:Tout le monde veut ?tre beau:everybody wants to be beautiful","et": "Conjunction: And.. Example: Et les larmes se remirent ? couler:and the tears started flowing again","en": "Adverb:Thereof. Preposition: In, Into, Of, At..Example: Je suis retourn?e en Espagne en septembre:I returned to Spain in September","avoir": "Verb: To have. Noun: Asset, Credit..Example: On ?tait six donc tu peux pas avoir une conversation:there were six of us so you can't have a conversation","que": "Conjunction: That,Than. Pronoun:Which,Who,Whom..Example: C'est un soldat,mais que fait? Il est ici?: it's a soldier. But what's he doing here?","pour": "Preposition: For, In order to....Example: Elle jouait pour gagner:she played to win","dans": "Preposition: In, Into, From..Example: Je reviendrai dans dix minutes:I will return in 10 minutes","ce": "Proposition,Adjective: This, that..Example: Je ne d?teste pas cet homme: I do not detest this man","il": "Pronoun:He, It....Example: Allez voir s'il est bless?: go see if he is injured","qui": "Pronoun: Who, Whom....Example: Je ne sais pas ? qui m'adresser: I don't know who to talk to","ne": "Adverb: Not...Example: Nous ne faisons pas du tr?s bon travail:we are not doing very good work","sur":"Preposition:On,Upon, Over,...Example: T'avais une chance sur un million:you had one chance in a million",? } keys = List.keys()print(keys) a = str(input("")) if 'a' in keys:? ? print(a + ":"+ List['a'])else:? ? print("impossible") From alan.gauld at yahoo.co.uk Wed Sep 6 04:54:17 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 6 Sep 2017 09:54:17 +0100 Subject: [Tutor] Select a string In-Reply-To: References: Message-ID: On 06/09/17 06:34, Pat Martin wrote: > but my script returns 0. Since they want it to find 2 from the bobob in the > string using "bob in s" doesn't work (it only returns 1). Your code has bugs and Cameron has addressed those along with general advice on how to debug your code in future. However there are other string methods that could save you some work. At the >>> prompt use dir(str) to see a list of all the methods available. Then use help(str.methodName) to find out what each one does. (Or just browse the string documentation on the web site!) In this case you might find some of the following methods useful: str.count str.find str.index str.startswith and of course you can use string slicing to access a substring. Combining slicing with the parameters of find or index should make your task easier. Finally, if you want to carry on with your loop approach you might find that enumerate is useful here since it gives you the index as well as the character. That will allow you to look-ahead to see if the next two characters are 'ob'... That should simplify things. -- 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 kwpolska at gmail.com Wed Sep 6 05:31:42 2017 From: kwpolska at gmail.com (Chris Warrick) Date: Wed, 6 Sep 2017 11:31:42 +0200 Subject: [Tutor] Help In-Reply-To: <550650043.3714240.1504679993689@mail.yahoo.com> References: <550650043.3714240.1504679993689.ref@mail.yahoo.com> <550650043.3714240.1504679993689@mail.yahoo.com> Message-ID: On 6 September 2017 at 08:39, edmundo pierre via Tutor wrote: > Hi Python, > I am trying to make a french to English dictionary. First of all, I made a dictionary with keys and values. Then I asked the user to enter a letter. If the letter correspond to the Keys, my print function should display the Key and value. But my code below, could not . Thank you! > > List ={"le":"?"} (it?s called dict, or dictionary, and ?List?/?dict? are not good variable names) > keys = List.keys()print(keys) > a = str(input("")) > if 'a' in keys: print(a + ":"+ List['a'])else: print("impossible") You?re trying to find the letter 'a' and print it, you aren?t using the variable a. Don?t put it in quotes. So, do it like this: data = {"le": "?"} print(data.keys()) search_key = str(input("> ")) if search_key in data: print(search_key, ":", data[search_key]) else: print("Not in dictionary.") (I also cleaned it up a little) -- Chris Warrick PGP: 5EAAEA16 From alan.gauld at yahoo.co.uk Wed Sep 6 05:33:01 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 6 Sep 2017 10:33:01 +0100 Subject: [Tutor] Fw: I want to display Integral symbols In-Reply-To: <1580748199.3726222.1504681754810@mail.yahoo.com> References: <592785203.3050170.1504618173463.ref@mail.yahoo.com> <592785203.3050170.1504618173463@mail.yahoo.com> <1580748199.3726222.1504681754810@mail.yahoo.com> Message-ID: On 06/09/17 08:09, edmundo pierre via Tutor wrote: > So show us what you did. Its hard to figure out what's > gone wrong when we can't see your code:That is what I did: > from sympy import*from tkinter import*from sympy import init_printing > root = Tk()root.geometry("450x350+500+500")root.title("Factorisations/Simplifications")root.configure(background = "gray")#Difine variablevar1= StringVar()var2 =StringVar()#Create the function answer > def Answer(): x = symbols("x") A = var1.get() B = factor(A) Right_insert_top .insert(END,str(B)) As you can see your code is a mess. you need to send in plain text format not HTML, otherwise the mail system mangles everything into an unreadable mess. I'll try to unscramble it as best I can but it would help a lot if in future you can switch to plain text for mails with code. (This is true of most programming mailing lists not just Python) from sympy import* from tkinter import* from sympy import init_printing root =Tk() root.geometry("450x350+500+500") root.title("Factorisations/Simplifications") root.configure(background = "gray") #Difine variable var1= StringVar() var2 =StringVar() #Create the function answer def Answer(): x = symbols("x") A = var1.get() B = factor(A) Right_insert_top .insert(END,str(B)) #Create the function simplification def Simplification(): init_printing() x= symbols("x") D = var2.get() E = expand(D) Right_insert_top .insert(END,str((E)) #Create the Factorisation frame Tops = Frame(root,relief= "sunken")Tops.pack(side = TOP) #Create a Label of the Top Top_Label = Label(Tops,font=("arial",30,"bold"),text = "Factorisations/Simplifications",bd = 10) Top_Label.pack(side = "top",fill =BOTH ) > #Create the left frame #Create a Right side Frame Right = Frame (root,relief= "sunken") Right.pack(side = "right",fill = BOTH) #Create a Frame on the Top right Right_top = Frame (Right,relief= "sunken") Right_top.pack(side = "top") ... #Create a insert text on the screen on the right Right_insert_top = Text( Right_top,font=("arial",10,"bold"),bd= 5) Right_insert_top.pack() Ok, I got bored after this, and we have the basic info we need... We can now see that you are using a Text widget with the Arial font. > I am using Window 7. > I got: x**2+6*x+9, which is Correct. But I do not like the syntax. OK, So all you need to do is write a string formatting function that takes your current output and returns the output you want. You can write and test that in a command line tool which will avoid all the complexity of a GUI. Then plug that into your GUI and insert the output into your Text widget. I don't know sympy but it may have some string formatting functions that you can use. If not you will need to interpret the input string yourself. There are some math expression parsers around on the internet if you search for the,m. You could adapt one of them I'm sure. > I used the Integral as an example of how my answer should look All the mathematical symbols have their own characters. Here is a link to the characters that Arial font supports: http://www.fileformat.info/info/unicode/font/arial_unicode_ms/list.htm You will see the integral variations at U222B-U2233 Use your browser string find tool to find the other operators/symbols. Your formatting function will need to insert the appropriate unicode characters into your output string. > My code run perfectly, you can try. Your code does not run perfectly since it doesn't do what you want! I assume you mean your code doesn't generate any errors. That's a different thing :-) -- 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 wpmartin at gmail.com Wed Sep 6 11:01:38 2017 From: wpmartin at gmail.com (Pat Martin) Date: Wed, 6 Sep 2017 08:01:38 -0700 Subject: [Tutor] Select a string In-Reply-To: References: Message-ID: I knew it was an assignment I just couldn't see it no matter how hard I looked at the code. Thanks for the tips on printing the variables, I was printing them right after the if statements not at the beginning and ending of the loop. I will remember that about True/False and comparisons thanks for that as well. The reason I haven't used regex/methods/etc is it hasn't been covered yet, we have only covered basic types, for, while and if statements. On Wed, Sep 6, 2017 at 1:54 AM, Alan Gauld via Tutor wrote: > On 06/09/17 06:34, Pat Martin wrote: > > > but my script returns 0. Since they want it to find 2 from the bobob in > the > > string using "bob in s" doesn't work (it only returns 1). > > Your code has bugs and Cameron has addressed those along > with general advice on how to debug your code in future. > However there are other string methods that could save > you some work. > > At the >>> prompt use dir(str) to see a list of all > the methods available. Then use help(str.methodName) > to find out what each one does. > (Or just browse the string documentation on the web site!) > > In this case you might find some of the following methods useful: > > str.count > str.find > str.index > str.startswith > > and of course you can use string slicing to access a > substring. Combining slicing with the parameters of > find or index should make your task easier. > > Finally, if you want to carry on with your loop approach > you might find that enumerate is useful here since it > gives you the index as well as the character. That > will allow you to look-ahead to see if the next two > characters are 'ob'... That should simplify things. > > -- > 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 wpmartin at gmail.com Wed Sep 6 12:12:10 2017 From: wpmartin at gmail.com (Pat Martin) Date: Wed, 6 Sep 2017 09:12:10 -0700 Subject: [Tutor] Select a string In-Reply-To: References: Message-ID: I got it working for all the tests, but the code is ugly. I think regex or using string methods would have been easier and neater but like I said we hadn't covered it in the class yet so didn't think I should use it. I am embarrassed by how bad the code looks to me. I am self taught and have written a couple useful scripts but still very much a beginner and there are definite holes in my knowledge. Hoping this class fills in some of the holes. Thanks again all for the suggestions and the tips. On Wed, Sep 6, 2017 at 8:01 AM, Pat Martin wrote: > I knew it was an assignment I just couldn't see it no matter how hard I > looked at the code. Thanks for the tips on printing the variables, I was > printing them right after the if statements not at the beginning and ending > of the loop. I will remember that about True/False and comparisons thanks > for that as well. > > The reason I haven't used regex/methods/etc is it hasn't been covered yet, > we have only covered basic types, for, while and if statements. > > On Wed, Sep 6, 2017 at 1:54 AM, Alan Gauld via Tutor > wrote: > >> On 06/09/17 06:34, Pat Martin wrote: >> >> > but my script returns 0. Since they want it to find 2 from the bobob in >> the >> > string using "bob in s" doesn't work (it only returns 1). >> >> Your code has bugs and Cameron has addressed those along >> with general advice on how to debug your code in future. >> However there are other string methods that could save >> you some work. >> >> At the >>> prompt use dir(str) to see a list of all >> the methods available. Then use help(str.methodName) >> to find out what each one does. >> (Or just browse the string documentation on the web site!) >> >> In this case you might find some of the following methods useful: >> >> str.count >> str.find >> str.index >> str.startswith >> >> and of course you can use string slicing to access a >> substring. Combining slicing with the parameters of >> find or index should make your task easier. >> >> Finally, if you want to carry on with your loop approach >> you might find that enumerate is useful here since it >> gives you the index as well as the character. That >> will allow you to look-ahead to see if the next two >> characters are 'ob'... That should simplify things. >> >> -- >> 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 cs at cskk.id.au Wed Sep 6 17:16:14 2017 From: cs at cskk.id.au (Cameron Simpson) Date: Thu, 7 Sep 2017 07:16:14 +1000 Subject: [Tutor] Select a string In-Reply-To: References: Message-ID: <20170906211614.GA23997@cskk.homeip.net> On 06Sep2017 09:12, Pat Martin wrote: >I got it working for all the tests, but the code is ugly. I think regex or >using string methods would have been easier and neater but like I said we Always go for string methods first. Regexps are cryptic and easy to get wrong, and relatively hard to debug when they are wrong. And they're more expensive to create and to run. The flip side is that there are things which can be said easily and concisely with regexps. But you should generally consider string methods first. >hadn't covered it in the class yet so didn't think I should use it. I am >embarrassed by how bad the code looks to me. That comes with practice: getting intuition about what reads well, and what tends to be error prone or fragile. For the exercise, have a relook at the code you have. What would make it easier to read? Easier to debug? Better variable names? Even trivial changes like changing "firstb" to "first_b" and "theo" to "the_o" can help. Would things be clearer with a leading comment explaining the purposes of the state variables: # Scan a string for instances of "bob", including overlaps like "bobob". # State variables: # first_b: set on encountering the first "b" of a potential "bob" # the_o: set on encountering the :o" of a potential "bob"; only set if the # preceeding character was the "b" (first_b). Partiularly for little state machines like yours I often find such descriptions useful. They help get the logic clear in my own mind _before_ writing the loop, and help you and others understand the code later when you come back to debug. It can also help to comment the individual if-statements. Eg: # recognise the "o" of "bob" if the preceeding character was "b" Cheers, Cameron Simpson (formerly cs at zip.com.au) From eishf2000 at gmail.com Thu Sep 7 02:49:08 2017 From: eishf2000 at gmail.com (devN) Date: Thu, 07 Sep 2017 06:49:08 +0000 Subject: [Tutor] Remote module loading Message-ID: Hi, I am newbie in python. I wrote a module which is meant to be run across couple of unix OS variants (redhat, debian, bsd and solaris). The module is updated frequently and new features added. import platform import os import subprocess def main (): RESPONSE = dict (); PYVER = float (platform.python_version_tuple ()[0] + "." + platform.python_version_tuple ()[1]); if PYVER < 2.4: return; RESPONSE["HOSTNAME"] = platform.node (); RESPONSE["MEMORY"] = str (float (os.sysconf ('SC_PAGE_SIZE')) * os.sysconf ('SC_PHYS_PAGES') /(1024 * 1024 * 1024)) +"GB"; RESPONSE["OSTYPE"] = platform.system (); RESPONSE["OSKERNEL"] = platform.uname ()[3]; RESPONSE["PROCESSOR"] = platform.processor (); if 'SunOS' in RESPONSE.values (): RESPONSE["CORECOUNT"] = re.sub ('\s+', '',subprocess.Popen ("/usr/sbin/psrinfo | wc -l | cat", shell =True, stdout = subprocess.PIPE, stderr =subprocess.STDOUT).stdout.readline ()); RESPONSE["OSVERSION"] = platform.release (); if 'Linux' in RESPONSE.values (): RESPONSE["OSKERNEL"] = platform.release (); RESPONSE["OSVERSION"] = platform.linux_distribution ()[0] + " " + platform.linux_distribution ()[1]; RESPONSE["CORECOUNT"] = CPUCOUNT; print (RESPONSE); if __name__== '__main__': main () The above code was an alpha and there could be further optimizations which I request you to overlook. My requirement is, I have an agent copied in all the UNIX variants and I have done that only once and dont want to do it again (too many machines and no possibility of ansible or chef implementation). The agent currently loads the module locally but I need to let all clients know that the module is updated with new features, but really can't. So I thought to expose the module as plain text over http from a remote apache container. Is there any way to load the module from remote just as simple as import statement (without using urllib or wget piped to python)? This could also mean does PYTHONPATH support http/ftp? Thanks From singh.vikram.0714 at gmail.com Thu Sep 7 09:14:34 2017 From: singh.vikram.0714 at gmail.com (Vikram Singh) Date: Thu, 7 Sep 2017 18:44:34 +0530 Subject: [Tutor] New to Python Message-ID: I've been learning Python from Google For Education . A little help will be appreciated regarding the right way and right tutorials to learn Python from. Thanks in advance. From edmundopierre at yahoo.com Thu Sep 7 12:20:36 2017 From: edmundopierre at yahoo.com (edmundo pierre) Date: Thu, 7 Sep 2017 16:20:36 +0000 (UTC) Subject: [Tutor] Help References: <1083763863.4763253.1504801236327.ref@mail.yahoo.com> Message-ID: <1083763863.4763253.1504801236327@mail.yahoo.com> I am trying to write a code that asks an User to Enter a decimal number, the my program should separate the number in two parts like this: 1 cae:Enter a number: 12.9Decimal part: 12whole part : 9 2 case:Enter: 10Decimal par:Whole part: 10 That is my code:? A = input("Enter a number") C = [ ]? ?? for ?i ?in ?str(A) : ? ? ? C.append(i)? ? ? if i ==".# period ":? ? ? ? ?break? ? ? aa= "".join(c)? ?print(aa, end =" ") ? ?if i == int(A):? ? ? ? ?print(" It is the whole part") I can not display the whole part when I enter float number. Plus when I enter an integer, it takes that enteger as a decimal number. Any help? From __peter__ at web.de Thu Sep 7 14:12:54 2017 From: __peter__ at web.de (Peter Otten) Date: Thu, 07 Sep 2017 20:12:54 +0200 Subject: [Tutor] Remote module loading References: Message-ID: devN wrote: > Is there any way to load the module from remote just as > simple as import statement (without using urllib or wget piped to python)? > This could also mean does PYTHONPATH support http/ftp? While there are hooks to do everything you can imagine https://docs.python.org/dev/library/importlib.html that's not very accessible to newbies. Why don't you make the module available through the file system? Think NFS or SMB or WebDAV... From __peter__ at web.de Thu Sep 7 14:35:49 2017 From: __peter__ at web.de (Peter Otten) Date: Thu, 07 Sep 2017 20:35:49 +0200 Subject: [Tutor] Help References: <1083763863.4763253.1504801236327.ref@mail.yahoo.com> <1083763863.4763253.1504801236327@mail.yahoo.com> Message-ID: edmundo pierre via Tutor wrote: > > I am trying to write a code that asks an User to Enter a decimal number, > the my program should separate the number in two parts like this: 1 > cae:Enter a number: 12.9Decimal part: 12whole part : 9 2 case:Enter: > 10Decimal par:Whole part: 10 That is my code: > A = input("Enter a number") > C = [ ] for i in str(A) : > C.append(i) if i ==".# period ": break aa= "".join(c) > print(aa, end =" ") if i == int(A): print(" It is the whole part") > I can not display the whole part when I enter float number. Plus when I > enter an integer, it takes that enteger as a decimal number. Any help? Hello Edmundo! We are all volunteers. Please help us help you. If you have a look at https://mail.python.org/pipermail/tutor/2017-September/111984.html you can see what we see of your code. It's completely garbled, and second- guessing what it might originally have looked like is no fun. You have to find out how to get your email program to send unmodified plain text before we can engage in a meaningful conversation. Thank you. From robertvstepp at gmail.com Thu Sep 7 14:39:24 2017 From: robertvstepp at gmail.com (boB Stepp) Date: Thu, 7 Sep 2017 13:39:24 -0500 Subject: [Tutor] New to Python In-Reply-To: References: Message-ID: Welcome to Tutor! On Thu, Sep 7, 2017 at 8:14 AM, Vikram Singh wrote: > I've been learning Python from Google For Education > . A little help will be > appreciated regarding the right way and right tutorials to learn Python > from. Thanks in advance. This is a very common question on this list as you might imagine. If you go to the searchable Tutor list archives at https://www.mail-archive.com/tutor at python.org/ and search for "learn python", you will get 8048 hits! You might want to do that, look over the results and perhaps perform other searches for topics that more closely match your interest. One thing I notice about the Google course you link to is that it is Python 2-based. The latest version of Python is now in version 3.6, and generally speaking, if you are just starting out with Python, you probably should be focusing your efforts on Python 3. As a fellow learner, I can recommend some things *not* to do: 1) Keep buying bunches of interesting books on Python and not studying any of them thoroughly! Instead, find a resource -- printed on paper or online -- that speaks to you and work through it thoroughly. 2) Starting new project after new project and finishing very few of them! Instead, pick something interesting and start working on it, planning on augmenting it as your knowledge grows in parallel with your more formal studying. 3) Working earnestly for a while, then taking long breaks off! If you are like me, you will tend to forget many things you have studied previously, even forgetting you have asked about these things previously on Tutor!! Instead, try to work on at least a little bit of Python studying each and every day, longer when life allows. 4) I have more I could share, but I think you take my points! If you have never done any programming, you may need to find a very gentle resource to start out with that explains not only Python, but general programming/computer science concepts in a lot of detail. OTOH, if you already have experience with programming, then you probably can greatly accelerate your learning progress, perhaps even getting by with studying the official Python tutorial at https://docs.python.org/3/tutorial/index.html Bear in mind that Python has its own culture and ways of doing things that can be a bit different from other languages you may have studied/worked in. You might enjoy the "Zen of Python" by Tim Peters. You can access it in the Python interpreter by typing >>> import this I have been rambling a bit. I just noticed that the searchable Tutor archive is back up and searched for all of my previous posts. Five years and I have not come very far. Do as I say and not as I do!!! ~(:>)) And always ask questions here when you get stuck. Try not to top post. Give your OS and Python version and all other relevant information to allow the experts to diagnose your problem(s). Always COPY AND PASTE both your relevant code and FULL ERROR TRACEBACK into a plain text email to Tutor. Try to limit your code to just the part that is causing you to pull your hair out (If you still have any! ~(:>)) ). And come back with more specific goals for your self-study along with any relevant background. Perhaps someone might be able to offer advice that is more tailored to your needs and goals. And again, welcome! This is a very friendly and helpful place to learn!! Cheers! -- boB From mats at wichmann.us Thu Sep 7 14:50:42 2017 From: mats at wichmann.us (Mats Wichmann) Date: Thu, 7 Sep 2017 12:50:42 -0600 Subject: [Tutor] Help In-Reply-To: <1083763863.4763253.1504801236327@mail.yahoo.com> References: <1083763863.4763253.1504801236327.ref@mail.yahoo.com> <1083763863.4763253.1504801236327@mail.yahoo.com> Message-ID: <6015243b-4802-8611-a978-cbf9c169c84c@wichmann.us> On 09/07/2017 10:20 AM, edmundo pierre via Tutor wrote: > > I am trying to write a code that asks an User to Enter a decimal number, the my program should separate the number in two parts like this: > 1 cae:Enter a number: 12.9Decimal part: 12whole part : 9 > 2 case:Enter: 10Decimal par:Whole part: 10 > That is my code:? > A = input("Enter a number") > C = [ ]? ?? for ?i ?in ?str(A) : > ? ? ? C.append(i)? ? ? if i ==".# period ":? ? ? ? ?break? ? ? aa= "".join(c)? ?print(aa, end =" ") > ? ?if i == int(A):? ? ? ? ?print(" It is the whole part") > I can not display the whole part when I enter float number. Plus when I enter an integer, it takes that enteger as a decimal number. Any help? Depending on what you have already learned, these hints may be helpful. 1. type() will let you experiment with what Python thinks you got. So as to the second question, A = input("Enter a number") print(type(A)) if it is not what you expected, how can you get it to something you can work with. Hint: you're already doing that once in your code. 2. strings are easy to split into pieces. See the split() method. This simple tutorial might give you an idea: http://www.pythonforbeginners.com/dictionary/python-split From robertvstepp at gmail.com Thu Sep 7 14:57:55 2017 From: robertvstepp at gmail.com (boB Stepp) Date: Thu, 7 Sep 2017 13:57:55 -0500 Subject: [Tutor] Help In-Reply-To: References: <1083763863.4763253.1504801236327.ref@mail.yahoo.com> <1083763863.4763253.1504801236327@mail.yahoo.com> Message-ID: Greetings Edmundo! On Thu, Sep 7, 2017 at 1:35 PM, Peter Otten <__peter__ at web.de> wrote: > edmundo pierre via Tutor wrote: > >> >> I am trying to write a code that asks an User to Enter a decimal number, >> the my program should separate the number in two parts like this: 1 >> cae:Enter a number: 12.9Decimal part: 12whole part : 9 2 case:Enter: >> 10Decimal par:Whole part: 10 That is my code: >> A = input("Enter a number") >> C = [ ] for i in str(A) : >> C.append(i) if i ==".# period ": break aa= "".join(c) >> print(aa, end =" ") if i == int(A): print(" It is the whole part") >> I can not display the whole part when I enter float number. Plus when I >> enter an integer, it takes that enteger as a decimal number. Any help? > > Hello Edmundo! > > We are all volunteers. Please help us help you. If you have a look at > > https://mail.python.org/pipermail/tutor/2017-September/111984.html > > you can see what we see of your code. It's completely garbled, and second- > guessing what it might originally have looked like is no fun. > > You have to find out how to get your email program to send unmodified plain > text before we can engage in a meaningful conversation. I'd like to augment Peter's request: Please use a more informative subject line than "Help". I notice you have posted two "Help" emails recently on two substantially different topics. As to the most current topic, your code is garbled to me as well, but it looks like you realize that input() returns a string, not a float, and that you are trying to use string methods to get what you want. Another approach you might want to look into is using float(input()) to convert the string resembling a float into a floating point number and then use the operator "//" (to perform integer division) and then use that result to extract the decimal portion. You will still have to work on dealing with the decimal point removal and just getting the decimal fraction as just integer-like numbers, but that is not too hard ... -- boB From eishf2000 at gmail.com Thu Sep 7 18:28:55 2017 From: eishf2000 at gmail.com (devN) Date: Thu, 07 Sep 2017 22:28:55 +0000 Subject: [Tutor] Remote module loading In-Reply-To: References: Message-ID: Well, it's another hassle to keep NFS shared and mounted in every other systems, not to include the firewall rules. Let me check out with the importlib On Fri, 8 Sep 2017, 00:14 Peter Otten <__peter__ at web.de> wrote: > devN wrote: > > Is there any way to load the module from remote just as > > simple as import statement (without using urllib or wget piped to > python)? > > This could also mean does PYTHONPATH support http/ftp? > > While there are hooks to do everything you can imagine > > https://docs.python.org/dev/library/importlib.html > > that's not very accessible to newbies. > > Why don't you make the module available through the file system? Think NFS > or SMB or WebDAV... > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From mats at wichmann.us Thu Sep 7 20:25:25 2017 From: mats at wichmann.us (Mats Wichmann) Date: Thu, 7 Sep 2017 18:25:25 -0600 Subject: [Tutor] Remote module loading In-Reply-To: References: Message-ID: On 09/07/2017 12:49 AM, devN wrote: > Hi, > I am newbie in python. I wrote a module which is meant to be run across > couple of unix OS variants (redhat, debian, bsd and solaris). The module is > updated frequently and new features added. > ... > The above code was an alpha and there could be further optimizations which > I request you to overlook. > My requirement is, I have an agent copied in all the UNIX variants and I > have done that only once and dont want to do it again (too many machines > and no possibility of ansible or chef implementation). The agent currently > loads the module locally but I need to let all clients know that the module > is updated with new features, but really can't. > So I thought to expose the module as plain text over http from a remote > apache container. Is there any way to load the module from remote just as > simple as import statement (without using urllib or wget piped to python)? > This could also mean does PYTHONPATH support http/ftp? > > Thanks We had a bit of a discussion not too long ago about the general topic. You can search in the list archive for "How to deploy seamless script updates to your clients" You've left a situation that isn't really very flexible since you say you don't want to copy the agent again. Nonetheless, I'll point you to a little bit of fiddling I did on this topic that you could just chew on and see if it makes any sense. It's not really "newbie" code. The basic idea is that you have a known location which contains a file with information about available releases; the module has a function a participating script can call to find out if it has a new version available. Link to my pointer to this: https://www.mail-archive.com/tutor at python.org/msg76681.html The rest of the discussion has some other good ideas in it too (a version control server may be able to provide a kind of solution). From eishf2000 at gmail.com Thu Sep 7 21:55:14 2017 From: eishf2000 at gmail.com (devN) Date: Fri, 08 Sep 2017 01:55:14 +0000 Subject: [Tutor] Remote module loading In-Reply-To: References: Message-ID: In fact, that's very close to what I needed. The repo or source would have version, target audience, and few other parameters. Thanks a zillion. I will take it up from here. I'll post you guys the complete version sometime in the future when it's ready. Cheers! Farhan On Fri, 8 Sep 2017, 06:26 Mats Wichmann wrote: > On 09/07/2017 12:49 AM, devN wrote: > > Hi, > > I am newbie in python. I wrote a module which is meant to be run across > > couple of unix OS variants (redhat, debian, bsd and solaris). The module > is > > updated frequently and new features added. > > > ... > > The above code was an alpha and there could be further optimizations > which > > I request you to overlook. > > My requirement is, I have an agent copied in all the UNIX variants and I > > have done that only once and dont want to do it again (too many machines > > and no possibility of ansible or chef implementation). The agent > currently > > loads the module locally but I need to let all clients know that the > module > > is updated with new features, but really can't. > > So I thought to expose the module as plain text over http from a remote > > apache container. Is there any way to load the module from remote just as > > simple as import statement (without using urllib or wget piped to > python)? > > This could also mean does PYTHONPATH support http/ftp? > > > > Thanks > > We had a bit of a discussion not too long ago about the general topic. > You can search in the list archive for "How to deploy seamless script > updates to your clients" > > You've left a situation that isn't really very flexible since you say > you don't want to copy the agent again. Nonetheless, I'll point you to > a little bit of fiddling I did on this topic that you could just chew on > and see if it makes any sense. It's not really "newbie" code. The > basic idea is that you have a known location which contains a file with > information about available releases; the module has a function a > participating script can call to find out if it has a new version > available. > > Link to my pointer to this: > https://www.mail-archive.com/tutor at python.org/msg76681.html > > > The rest of the discussion has some other good ideas in it too (a > version control server may be able to provide a kind of solution). > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From george at fischhof.hu Fri Sep 8 03:35:04 2017 From: george at fischhof.hu (George Fischhof) Date: Fri, 8 Sep 2017 09:35:04 +0200 Subject: [Tutor] New to Python In-Reply-To: References: Message-ID: 2017-09-07 20:39 GMT+02:00 boB Stepp : > Welcome to Tutor! > > On Thu, Sep 7, 2017 at 8:14 AM, Vikram Singh > wrote: > > I've been learning Python from Google For Education > > . A little help will be > > appreciated regarding the right way and right tutorials to learn Python > > from. Thanks in advance. > > This is a very common question on this list as you might imagine. If > you go to the searchable Tutor list archives at > > https://www.mail-archive.com/tutor at python.org/ > > and search for "learn python", you will get 8048 hits! You might want > to do that, look over the results and perhaps perform other searches > for topics that more closely match your interest. > > One thing I notice about the Google course you link to is that it is > Python 2-based. The latest version of Python is now in version 3.6, > and generally speaking, if you are just starting out with Python, you > probably should be focusing your efforts on Python 3. > > As a fellow learner, I can recommend some things *not* to do: > > 1) Keep buying bunches of interesting books on Python and not > studying any of them thoroughly! Instead, find a resource -- printed > on paper or online -- that speaks to you and work through it > thoroughly. > > 2) Starting new project after new project and finishing very few of > them! Instead, pick something interesting and start working on it, > planning on augmenting it as your knowledge grows in parallel with > your more formal studying. > > 3) Working earnestly for a while, then taking long breaks off! If > you are like me, you will tend to forget many things you have studied > previously, even forgetting you have asked about these things > previously on Tutor!! Instead, try to work on at least a little bit > of Python studying each and every day, longer when life allows. > > 4) I have more I could share, but I think you take my points! > > If you have never done any programming, you may need to find a very > gentle resource to start out with that explains not only Python, but > general programming/computer science concepts in a lot of detail. > OTOH, if you already have experience with programming, then you > probably can greatly accelerate your learning progress, perhaps even > getting by with studying the official Python tutorial at > > https://docs.python.org/3/tutorial/index.html > > Bear in mind that Python has its own culture and ways of doing things > that can be a bit different from other languages you may have > studied/worked in. You might enjoy the "Zen of Python" by Tim Peters. > You can access it in the Python interpreter by typing > > >>> import this > > I have been rambling a bit. I just noticed that the searchable Tutor > archive is back up and searched for all of my previous posts. Five > years and I have not come very far. Do as I say and not as I do!!! > ~(:>)) > > And always ask questions here when you get stuck. Try not to top > post. Give your OS and Python version and all other relevant > information to allow the experts to diagnose your problem(s). Always > COPY AND PASTE both your relevant code and FULL ERROR TRACEBACK into a > plain text email to Tutor. Try to limit your code to just the part > that is causing you to pull your hair out (If you still have any! > ~(:>)) ). > > And come back with more specific goals for your self-study along with > any relevant background. Perhaps someone might be able to offer > advice that is more tailored to your needs and goals. > > And again, welcome! This is a very friendly and helpful place to learn!! > > Cheers! > > > -- > boB > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > Hi Tutors, As this is a really frequent question, maybe it would be good to put a short list (the answers cumulated no more than about 10 websites) into the welcome message of the Tutor mailing list. BR, George From alan.gauld at yahoo.co.uk Fri Sep 8 07:17:16 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 8 Sep 2017 12:17:16 +0100 Subject: [Tutor] New to Python In-Reply-To: References: Message-ID: On 08/09/17 08:35, George Fischhof wrote: > As this is a really frequent question, maybe it would be good to put a > short list (the answers cumulated no more than about 10 websites) into the > welcome message of the Tutor mailing list. The welcome message should be a very short (ideally one "screen" ) message about the mailing list itself. It could have a URL to an FAQ if we had such a thing... Somebody tried to put a tutor FAQ together a long time ago but I think it died. And the main FAQ is probably too heavyweight for the typical tutor newbie. (Although it might be better than nothing!) Currently we have a link to the beginners page on the wiki. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From robertvstepp at gmail.com Sat Sep 9 20:29:49 2017 From: robertvstepp at gmail.com (boB Stepp) Date: Sat, 9 Sep 2017 19:29:49 -0500 Subject: [Tutor] How is database creation normally handled? Message-ID: While reading about SQL, SQLite and the Python module sqlite3, it appears that I could (1) have a program check for the existence of the program's database, and if not found, create it, make the tables, etc.; or, (2) create the database separately and include it with the program. What are the pros and cons of each approach? (1) to me looks only helpful if I wish to write a program that might want to allow the user to have multiple databases. But this sounds like a lot of extra coding and checking to make it work well. But if I only wish to have a single database, then (2) sounds like the approach to use. I would create the database, populate it with the needed empty tables with the desired fields, making it ready to use by the program's user. Not having any experience in the database arena, I'm not even sure I know how to properly think about this. -- boB From leamhall at gmail.com Sat Sep 9 20:48:34 2017 From: leamhall at gmail.com (leam hall) Date: Sat, 9 Sep 2017 20:48:34 -0400 Subject: [Tutor] How is database creation normally handled? In-Reply-To: References: Message-ID: On Sat, Sep 9, 2017 at 8:29 PM, boB Stepp wrote: > While reading about SQL, SQLite and the Python module sqlite3, it > appears that I could (1) have a program check for the existence of the > program's database, and if not found, create it, make the tables, > etc.; or, (2) create the database separately and include it with the > program. What are the pros and cons of each approach? (1) to me > looks only helpful if I wish to write a program that might want to > allow the user to have multiple databases. But this sounds like a lot > of extra coding and checking to make it work well. But if I only wish > to have a single database, then (2) sounds like the approach to use. > I would create the database, populate it with the needed empty tables > with the desired fields, making it ready to use by the program's user. > > Not having any experience in the database arena, I'm not even sure I > know how to properly think about this. > I'd write a configure script separate to set things up. It only gets run when needed and you can change the process if you want to move from SQLite3 to MongoDB or whatever. It also lets the user set up test databases. From robertvstepp at gmail.com Sat Sep 9 20:56:36 2017 From: robertvstepp at gmail.com (boB Stepp) Date: Sat, 9 Sep 2017 19:56:36 -0500 Subject: [Tutor] Standard module sqlite3 or APSW? Message-ID: First, if I have not misinterpreted anything, the subject header is perhaps better written as "pysqlite or APSW?" As far as I can tell from reading the documentation at https://docs.python.org/3/library/sqlite3.html , the module sqlite3 is in actuality using the pysqlite project. So if all of this is correct, does the documentation at https://rogerbinns.github.io/apsw/pysqlite.html#pysqlitediffs give an objective assessment of the differences between pysqlite and APSW? The bottom line results I am getting from that page is: (1) If my database needs are simple, go with the standard library module, sqlite3. (2) If I wish to use all of the latest, greatest features of SQLite3, and I don't care about some of its non-standard SQL implementations, then use APSW if SQLite3 meets my database needs and I don't anticipate having to upgrade to one of the big database projects. (3) If I am prototyping a database solution, anticipating transitioning to one of the big database projects, then use the standard library sqlite3 as it will make it easier to make the transition, due to better SQL standardization. Am I getting the correct understanding of the pros and cons? A further question. Is APSW being maintained solely by a single person? That is, is it likely to be around for years to come? TIA! -- boB From leamhall at gmail.com Sat Sep 9 21:08:02 2017 From: leamhall at gmail.com (leam hall) Date: Sat, 9 Sep 2017 21:08:02 -0400 Subject: [Tutor] Standard module sqlite3 or APSW? In-Reply-To: References: Message-ID: On Sat, Sep 9, 2017 at 8:56 PM, boB Stepp wrote: > First, if I have not misinterpreted anything, the subject header is > perhaps better written as "pysqlite or APSW?" As far as I can tell > from reading the documentation at > https://docs.python.org/3/library/sqlite3.html , the module sqlite3 is > in actuality using the pysqlite project. > > So if all of this is correct, does the documentation at > https://rogerbinns.github.io/apsw/pysqlite.html#pysqlitediffs give an > objective assessment of the differences between pysqlite and APSW? > > The bottom line results I am getting from that page is: > > (1) If my database needs are simple, go with the standard library > module, sqlite3. > > (2) If I wish to use all of the latest, greatest features of SQLite3, > and I don't care about some of its non-standard SQL implementations, > then use APSW if SQLite3 meets my database needs and I don't > anticipate having to upgrade to one of the big database projects. > > (3) If I am prototyping a database solution, anticipating > transitioning to one of the big database projects, then use the > standard library sqlite3 as it will make it easier to make the > transition, due to better SQL standardization. > > Am I getting the correct understanding of the pros and cons? > > A further question. Is APSW being maintained solely by a single > person? That is, is it likely to be around for years to come? > > TIA! > >From a newbie, my choices are usually guided by "what's in the stdlib?" Starting there tends to take me pretty far and if I get to the bumpy parts I know exactly why I might want to change. Staying with the stdlib also means others can use my code with minimal fuss. From alan.gauld at yahoo.co.uk Sun Sep 10 04:08:03 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 10 Sep 2017 09:08:03 +0100 Subject: [Tutor] How is database creation normally handled? In-Reply-To: References: Message-ID: On 10/09/17 01:29, boB Stepp wrote: > While reading about SQL, SQLite and the Python module sqlite3, it > appears that I could (1) have a program check for the existence of the > program's database, and if not found, create it, make the tables, > etc.; or, (2) create the database separately and include it with the > program. Almost always number 2. The slight variant on that is to put the SQL to create the tables etc into a sql file and then if a database file does not exist create the database by executing the sql script (which you can do via the executescript() function) One reason for doing it this way is that while SQL is standardised the standardisation is mainly in the query part of the language. Each database is quite different in its data definition language (different data types, features like triggers and stored procedures etc). So by putting the DDL into a sql file that your program just executes the non portable bit gets removed from your Python code. You can even have multiple different versions of the DDL file, one per database, and your Python code can remain pretty much oblivious to the changes (not quite true, but its much easier). Populating the data is another matter. I tend to do that via a Spreadsheet/csv file and write a loader in Python to loop over the data loading it into the various tables then setting the table properties/constraints as needed. (if you set all constraints before loading the data you can get into a kind of data deadlock where you can't get any initial data loaded!). Other people will use a dedicated database GUI to do initial data load. SQLiteManager is one such option for Sqlite. The last option is to just start with an empty database and allow the code to populate it, but as mentioned, if you have a lot of constraints in your database design you can get to the point of not being able to insert any data. So you usually need to populate some standing data first. But in this case it can be part of the creation script. If you haven't come across database constraints yet, its where you define criteria that must be true. At a simple level its things like NOT NULL or UNIQUE. But they can get quite complex like being a foreign key, in which case the foreign object must exist before you can create the dependant one. And when you get combinations of constraints coupled to triggers(which automatically execute SQL when a database event occurs) it all gets very messy. Done properly constraints are a powerful tool to prevent data corruption. They are seductive in their power and can save a lot of defensive programming in the host application. But... One common database beginners gotcha is to go mad with constraints to the point that any database action triggers a snowball of other actions and performance slows to a crawl or even locks up. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alan.gauld at yahoo.co.uk Sun Sep 10 04:18:41 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 10 Sep 2017 09:18:41 +0100 Subject: [Tutor] Standard module sqlite3 or APSW? In-Reply-To: References: Message-ID: On 10/09/17 01:56, boB Stepp wrote: > So if all of this is correct, does the documentation at > https://rogerbinns.github.io/apsw/pysqlite.html#pysqlitediffs give an > objective assessment of the differences between pysqlite and APSW? I believe so. I've never used APSW, only read a single web article on it so I'm no expert. But for me it would only be of interest if I was totally sure I'd never want to move my data to anything else. And that's pretty rare since for small data I might change to, say, a JSON file. And for large volumes (or shared access) I'd move to a server database like MySQL. In either case the investment in learning a non-standard data layer is not worth it. And of course it adds another external dependency to my project, and I hate external dependencies! > (1) If my database needs are simple, go with the standard library > module, sqlite3. Usually this is true if I'm using SQLite. If my needs were not simple I'd probably be using MySQL or FireBird or similar. > (2) If I wish to use all of the latest, greatest features of SQLite3, > and I don't care about some of its non-standard SQL implementations, > then use APSW if SQLite3 meets my database needs and I don't > anticipate having to upgrade to one of the big database projects. This is very rare for me and pretty much why I don;t use APSW > (3) If I am prototyping a database solution, anticipating > transitioning to one of the big database projects, then use the > standard library sqlite3 as it will make it easier to make the > transition, due to better SQL standardization. Again, this is usually my scenario. > A further question. Is APSW being maintained solely by a single > person? That is, is it likely to be around for years to come? I'm not sure on that one. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From __peter__ at web.de Sun Sep 10 05:28:37 2017 From: __peter__ at web.de (Peter Otten) Date: Sun, 10 Sep 2017 11:28:37 +0200 Subject: [Tutor] How is database creation normally handled? References: Message-ID: boB Stepp wrote: > While reading about SQL, SQLite and the Python module sqlite3, it > appears that I could (1) have a program check for the existence of the > program's database, and if not found, create it, make the tables, > etc.; or, I'm only a casual user of sqlite, and therefore I prefer to have the table definitions in the script. It's as easy writing create table if not exists foo ... instead of create table foo ... This also allows you to easily add tables later on. > (2) create the database separately and include it with the > program. What are the pros and cons of each approach? (1) to me > looks only helpful if I wish to write a program that might want to > allow the user to have multiple databases. Even if the user needs only a single db you should make that configurable to allow for testing. > But this sounds like a lot > of extra coding and checking to make it work well. I don't think so. You need the database schema, and once you have that it doesn't matter if you build one or 1000 databases from it. > But if I only wish > to have a single database, then (2) sounds like the approach to use. > I would create the database, populate it with the needed empty tables > with the desired fields, making it ready to use by the program's user. > > Not having any experience in the database arena, I'm not even sure I > know how to properly think about this. Here's a little demo script to get you going. There's only a single table, but the principle works with any number of tables and indices. $ ls sqlitedemo.py $ cat sqlitedemo.py #!/usr/bin/env python3 import sqlite3 from itertools import chain from contextlib import closing def ensure_db(filename): db = sqlite3.connect(filename) with closing(db.cursor()) as cursor: cursor.execute( "create table if not exists addresses (name, email);" ) return db def insert_action(args, parser): with ensure_db(args.database) as db: with closing(db.cursor()) as cursor: cursor.execute( "insert into addresses values (?, ?);", (args.name, args.email) ) def fix_none(row): return [ "NULL"if value is None else value for value in row ] def show_action(args, parser): with ensure_db(args.database) as db: with closing(db.cursor()) as cursor: cursor.execute("select * from addresses order by name") fieldnames = [ column_desc[0] for column_desc in cursor.description ] template = " | ".join(("{:20}",)*len(fieldnames)) separator = ["-" * 20] * len(fieldnames) for row in chain([fieldnames, separator], cursor): print(template.format(*fix_none(row))) def main(): import argparse parser = argparse.ArgumentParser() parser.add_argument("-d", "--database", default="default.sqlite") sub = parser.add_subparsers() insert = sub.add_parser("insert") insert.add_argument("name") insert.add_argument("email") insert.set_defaults(func=insert_action) show = sub.add_parser("show") show.set_defaults(func=show_action) args = parser.parse_args() args.func(args, parser) if __name__ == "__main__": main() $ ./sqlitedemo.py show name | email -------------------- | -------------------- $ ls default.sqlite sqlitedemo.py $ ./sqlitedemo.py insert jim jim at example.com $ ./sqlitedemo.py insert sue sue at elsewhere.org $ ./sqlitedemo.py show name | email -------------------- | -------------------- jim | jim at example.com sue | sue at elsewhere.org $ ./sqlitedemo.py -d otherdb insert peter peter at uknowwhere $ ls default.sqlite otherdb sqlitedemo.py $ ./sqlitedemo.py -d otherdb show name | email -------------------- | -------------------- peter | peter at uknowwhere PS: I don't know if it makes sense to close the cursors, I just thought it looked clean when I added it to ensure_db() and ended up adding it everywhere. From geokerbi at gmx.at Sun Sep 10 07:32:17 2017 From: geokerbi at gmx.at (Elmar Klein) Date: Sun, 10 Sep 2017 13:32:17 +0200 Subject: [Tutor] beginning to code Message-ID: <9c1d431e-3276-79d1-436f-343ded9511f1@gmx.at> Hi there, im starting to learn how to code (bougt me the "automate the boring stuff with phyton" book). And im not going anywhere with a code sample using the "continue" statement. The code i should try is as following: while True: ????? print ('who are you') ????? name = input () ????? if name != 'bert': ????????????? continue ??????? print ('hi, joe. pwd?') ??????? pwd = input () ??????? if pwd == 'sword': ??????????????? break ??????? print ('access granted') But everytime this produces an error (unindent does not match any outer indention level) in the "print ('hi, joe. Pwd?')" sentence. What am i doing wrong? Greetings kerbi From senthil at uthcode.com Sun Sep 10 08:36:00 2017 From: senthil at uthcode.com (Senthil Kumaran) Date: Sun, 10 Sep 2017 05:36:00 -0700 Subject: [Tutor] beginning to code In-Reply-To: <9c1d431e-3276-79d1-436f-343ded9511f1@gmx.at> References: <9c1d431e-3276-79d1-436f-343ded9511f1@gmx.at> Message-ID: > unindent does not match any outer indention level Means that your Intendentaion is not proper. You should align your print statement and rest to the same level as if statement. Use a proper editor that supports Python Syntax. On Sun, Sep 10, 2017 at 4:32 AM, Elmar Klein wrote: > Hi there, > > im starting to learn how to code (bougt me the "automate the boring stuff > with phyton" book). > > And im not going anywhere with a code sample using the "continue" > statement. > > The code i should try is as following: > > while True: > print ('who are you') > name = input () > if name != 'bert': > continue > print ('hi, joe. pwd?') > pwd = input () > if pwd == 'sword': > break > print ('access granted') > > But everytime this produces an error (unindent does not match any outer > indention level) in the "print ('hi, joe. Pwd?')" sentence. > > What am i doing wrong? > > Greetings > > kerbi > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From bgailer at gmail.com Sun Sep 10 09:25:57 2017 From: bgailer at gmail.com (Bob Gailer) Date: Sun, 10 Sep 2017 09:25:57 -0400 Subject: [Tutor] beginning to code In-Reply-To: References: <9c1d431e-3276-79d1-436f-343ded9511f1@gmx.at> Message-ID: On Sep 10, 2017 8:40 AM, "Senthil Kumaran" wrote: > > > unindent does not match any outer indention level > > Means that your Intendentaion is not proper. You should align your print > statement and rest to the same level as if statement. > > Use a proper editor that supports Python Syntax. > Also the last line will print access granted if the password does not match. Probably not what you wanted. Either indent it and put it before break or unindent it so it lines up with while. For what it's worth if the user never gets the password he is stuck forever in the loop. In the future please include the entire traceback in your email. That makes it a lot easier for us to relate it to your code. > On Sun, Sep 10, 2017 at 4:32 AM, Elmar Klein wrote: > > > Hi there, > > > > im starting to learn how to code (bougt me the "automate the boring stuff > > with phyton" book). > > > > And im not going anywhere with a code sample using the "continue" > > statement. > > > > The code i should try is as following: > > > > while True: > > print ('who are you') > > name = input () > > if name != 'bert': > > continue > > print ('hi, joe. pwd?') > > pwd = input () > > if pwd == 'sword': > > break > > print ('access granted') > > > > But everytime this produces an error (unindent does not match any outer > > indention level) in the "print ('hi, joe. Pwd?')" sentence. > > > > What am i doing wrong? > > > > Greetings > > > > kerbi > > > > > > > > _______________________________________________ > > Tutor maillist - Tutor at python.org > > To unsubscribe or change subscription options: > > https://mail.python.org/mailman/listinfo/tutor > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From robertvstepp at gmail.com Sun Sep 10 13:19:25 2017 From: robertvstepp at gmail.com (boB Stepp) Date: Sun, 10 Sep 2017 12:19:25 -0500 Subject: [Tutor] beginning to code In-Reply-To: <9c1d431e-3276-79d1-436f-343ded9511f1@gmx.at> References: <9c1d431e-3276-79d1-436f-343ded9511f1@gmx.at> Message-ID: Hello kerbi! On Sun, Sep 10, 2017 at 6:32 AM, Elmar Klein wrote: > Hi there, > > im starting to learn how to code (bougt me the "automate the boring stuff > with phyton" book). > > And im not going anywhere with a code sample using the "continue" statement. > > The code i should try is as following: > > while True: > print ('who are you') > name = input () > if name != 'bert': > continue > print ('hi, joe. pwd?') > pwd = input () > if pwd == 'sword': > break > print ('access granted') > > But everytime this produces an error (unindent does not match any outer > indention level) in the "print ('hi, joe. Pwd?')" sentence. As the others have said your indentation is inconsistent. "print ('who are you')" is indented 6 spaces under "while True:". But "continue" is indented 8 spaces under "if name != 'bert':". And "print ('hi, joe. pwd?')" is indented 2 spaces under "if name != 'bert':" when it should not be indented at all! You might want to review pages 37-38 where the author talks about "Blocks of Code" and make sure you are *getting it*. If not, come back with specific questions. Senthil mentioned using a proper programmer's editor that supports Python syntax. This can make things much easier. Generally speaking, you wish a single level of indentation to always be the same number of spaces. The commonly used number is one indentation level equals 4 spaces. Most editors will allow you to use the key to indent, converting it to 4 spaces (Or a different number if you configure your differently in the editor.). A side note: Don't mix tabs with spaces! Ensure that your editor converts tabs to spaces. Otherwise you can have a real mess! So look carefully on page 51 of your book where I found the code you are playing with. It looks like you are playing around with modifying it, which is a good thing to do. But you still must maintain the proper block structure of the author's code that you are modifying if you want to accomplish the kinds of things the author is doing. Pay particular to the vertical alignment of each line's contents. For instance, the "w" in "while True:" lines up with the "p" in "print('Access granted.')", which is the last line of the text's code example. Everything else between the first and last lines is indented one or more levels and belongs to the while loop's execution responsibilities. When you look at the while loop's block contents you notice additional levels of indentation belonging to the two if statements. Again notice how these additional levels of indentation define the blocks belonging to each if statement's responsibilities. Another thing is that if I have found the correct page of the code you are trying out, you are being rather free with how you match capitalization with the author's code. This won't matter a whole lot for strings you are printing to screen, but if you start being sloppy with capitalization with Python keywords or identifiers/variable names, then you will soon come to a lot of grief! Also, you add extra spaces to the print() functions. Fortunately this does not matter here, but it shows that you are being a bit cavalier with how you re-type code. This lack of attention to seemingly teeny-tiny details can cause you much grief, such as in your indentation problems. Whereas a human being can usually easily interpret your intentions, computers, being extremely literal in their interpretation of what is typed, might easily not get your intentions. Be careful what you type! It might be educational for you to take your current code with its inconsistent indentation and correct it line by line, but re-running the code after each such correction to see what exactly happens and what new error tracebacks you get. Hope this helps! -- boB From mats at wichmann.us Sun Sep 10 13:41:32 2017 From: mats at wichmann.us (Mats Wichmann) Date: Sun, 10 Sep 2017 11:41:32 -0600 Subject: [Tutor] How is database creation normally handled? In-Reply-To: References: Message-ID: <1ed22231-64cf-b23b-101c-4a6a6d7b2838@wichmann.us> On 09/10/2017 02:08 AM, Alan Gauld via Tutor wrote: > On 10/09/17 01:29, boB Stepp wrote: >> While reading about SQL, SQLite and the Python module sqlite3, it >> appears that I could (1) have a program check for the existence of the >> program's database, and if not found, create it, make the tables, >> etc.; or, (2) create the database separately and include it with the >> program. > > Almost always number 2. > > The slight variant on that is to put the SQL to create the > tables etc into a sql file and then if a database file does > not exist create the database by executing the sql script > (which you can do via the executescript() function) > > One reason for doing it this way is that while SQL is > standardised the standardisation is mainly in the query > part of the language. Each database is quite different > in its data definition language (different data types, > features like triggers and stored procedures etc). > > So by putting the DDL into a sql file that your program > just executes the non portable bit gets removed from your > Python code. You can even have multiple different > versions of the DDL file, one per database, and your > Python code can remain pretty much oblivious to the > changes (not quite true, but its much easier). > > Populating the data is another matter. I tend to do > that via a Spreadsheet/csv file and write a loader > in Python to loop over the data loading it into the > various tables then setting the table properties/constraints > as needed. (if you set all constraints before loading the > data you can get into a kind of data deadlock where > you can't get any initial data loaded!). All of this depends on how you intend to use the database. If it's a small thing, say to enable experiments, you can certainly build it on the fly (esp. if you'll never go beyond the sqlite level). I'm generally agreeing that the "build it outside your program" and "keep the DB code in SQL" are good ideas. There are so many different types of databases for different uses. I've spent a decade and a half on a project (sadly now it's just a trickle of time to maintain anything that breaks) where we do have a central "official" database, but want people to be able to do local experiments, because the "official" database helps describe a standard, making it update-rarely. So on any approved change, the database is immediately dumped, and the version-control copy of that dump has the changes committed. With some supporting code (written in Perl, not Python - hey, I did't start those scripts :)), anybody who wants to experiment with proposed updates, either to fix a reported bug, or to prototype changes for the next version, can just pull the repository and go "make restore" and a local copy of the (mysql) db will be built. So that's what I just agreed with: the setup process happens outside the code app which will access the DB; the DB creation _and_ the data population are both kept entirely in SQL (as a result of using the dumps). That's proven to be very workable for that somewhat unusual usage model, but maybe it's not that unusual - any time you need to start with a reproducible known state of the DB, that would work pretty well; it would be a disaster for a DB where there were tons of commits, stuff changed mostly interactively, etc. From kwpolska at gmail.com Sun Sep 10 14:31:51 2017 From: kwpolska at gmail.com (Chris Warrick) Date: Sun, 10 Sep 2017 20:31:51 +0200 Subject: [Tutor] How is database creation normally handled? In-Reply-To: References: Message-ID: On 10 September 2017 at 02:29, boB Stepp wrote: > While reading about SQL, SQLite and the Python module sqlite3, it > appears that I could (1) have a program check for the existence of the > program's database, and if not found, create it, make the tables, > etc.; or, (2) create the database separately and include it with the > program. What are the pros and cons of each approach? (1) to me > looks only helpful if I wish to write a program that might want to > allow the user to have multiple databases. But this sounds like a lot > of extra coding and checking to make it work well. But if I only wish > to have a single database, then (2) sounds like the approach to use. > I would create the database, populate it with the needed empty tables > with the desired fields, making it ready to use by the program's user. > > Not having any experience in the database arena, I'm not even sure I > know how to properly think about this. A separate database creation script will be better. It?s good to separate the logic of your app and configuration/setup ? just like you shouldn?t be ?helpfully? installing dependencies when someone runs your script. For a good approach, look at Django: every app (sub-package of a site) has its own set of migrations. Migrations are responsible for creating tables, and more importantly ? for updating them. Because you will need to make changes to your original DB structure throughout the lifetime of your software. I built a small Django app over the weekend, and I?ve created 10 migrations throughout the process?, and that will probably be true of your project. Now, if those migrations would have to go to the main codebase instead of a side directory, it would be a burden to maintain. If there were no migration frameworks, I?d have to include something like ?run this SQL to upgrade your DB when upgrading from version 0.1.0 to 0.1.1?, which is even less fun. So, when I make some change to my models.py (which has the database schema), I can just run: ./manage.py makemigrations ./manage.py migrate and my database will be magically updated, hassle-free and I don?t even have to look at the auto-generated code. (If you are writing a web app: do look at Django! If you aren?t: Alembic does the same thing for SQLAlchemy. Something for plain sqlite3 may or may not exist.) ? Progressive enhancement: adding more features that need extra columns I didn?t think of first. Or removing features that weren?t cool. Or restoring them the next day. -- Chris Warrick PGP: 5EAAEA16 From alan.gauld at yahoo.co.uk Sun Sep 10 14:54:50 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 10 Sep 2017 19:54:50 +0100 Subject: [Tutor] beginning to code In-Reply-To: References: <9c1d431e-3276-79d1-436f-343ded9511f1@gmx.at> Message-ID: For some reaspon I'm missing the original post so apologies to replying to Senthil instead of the OP directly... >> The code i should try is as following: >> >> while True: >> print ('who are you') >> name = input () >> if name != 'bert': >> continue >> print ('hi, joe. pwd?') >> pwd = input () >> >> But everytime this produces an error (unindent does not match any outer >> indention level) in the "print ('hi, joe. Pwd?')" sentence. Its best when posting to the group to include the full error text rather than summarizing it. In this case we don't need it but in other cases the extra detail may be important. The error is telling you that the indentation of that line is wrong. Indentation, or spacing, is very important in Python - much more so than in other languages. Python uses the spacing to tell which lines are grouped together. In your code Python can't tell whether the print line is supposed to be inside the if block along with the continue statement or outside the if block and following the if statement. You need to align the print (and all subsequent lines) with the if. One question though. As written, your code only goes past the if statement if the input name is *bert*. Why then does the welcome say Hi *joe*???? 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 anubhav.yadav at gmx.com Mon Sep 11 05:38:45 2017 From: anubhav.yadav at gmx.com (GMX) Date: Mon, 11 Sep 2017 15:08:45 +0530 Subject: [Tutor] How to create an object in database only if the object is not already there? Message-ID: Hi,? I am using `pony` orm to write a simple class as my model. Here is the class.? ``` from pony.orm import Database from pony.orm import Required, Optional from pony.orm import db_session from pony.orm import select, commit DB_PATH = ?db.sqlite? db = Database() class Course(db.Entity): ? ? """ ? ? A class to represent a course ? ? """ ? ? title = Required(str, unique=True) ? ? url = Optional(str, unique=True) ? ? thumbnail = Optional(str) ? ? processed = Optional(bool, default=False) db.bind(provider='sqlite', filename=DB_PATH, create_db=True) db.generate_mapping(create_tables=True) ``` Now when I create a Course object like this: ? ? >>> Course(title=?A new course?) An object is create in the database. I don?t want to have this behaviour, but what I want to be doing is create a Course object and then only commit in the database if the course is not already available in the database. If the course is already in the database,? the orm should just return the same object. I have implemented the requirement as follows: ``` class Course(db.Entity): ? ? """ ? ? A class to represent a course ? ? """ ? ? title = Required(str, unique=True) ? ? url = Optional(str, unique=True) ? ? thumbnail = Optional(str) ? ? processed = Optional(bool, default=False) ? ? @staticmethod ? ? @db_session ? ? def new(title, url=None, thumbnail=None, processed=False): ? ? ? ? """Return a Course either new or from database""" ? ? ? ? course = select(c for c in Course if c.title == title)[:] ? ? ? ? if course: ? ? ? ? ? ? return course[0] ? ? ? ? return Course(title=title, url=url, thumbnail=thumbnail, processed=processed) db.bind(provider='sqlite', filename=DB_PATH, create_db=True) db.generate_mapping(create_tables=True) ``` Can there be a better method of doing that? Please let me know.? Thanks.? ? You are not born knowing everything. You go on learning? - Anubhav Yadav From e2012 at live.ru Mon Sep 11 06:58:51 2017 From: e2012 at live.ru (=?koi8-r?B?4crO1dIg+tXM2MvB0s7Bxdc=?=) Date: Mon, 11 Sep 2017 10:58:51 +0000 Subject: [Tutor] question about calendar module in standard libriary Message-ID: Hello all! There is a class Calendar in calendar.py in standard libriary. class Calendar(object): """ Base calendar class. This class doesn't do any formatting. It simply provides data to subclasses. """ def __init__(self, firstweekday=0): self.firstweekday = firstweekday # 0 = Monday, 6 = Sunday def getfirstweekday(self): return self._firstweekday % 7 def setfirstweekday(self, firstweekday): self._firstweekday = firstweekday firstweekday = property(getfirstweekday, setfirstweekday) As far as I understand, even if user enters inappropriate firstweekday parameter (bigger than 6) during instansiation of the Calendar, the Calendar swallows it (and latter returns correct firstweekday value due to %7 in getfirstweekday method). So, the question is why not explicitly raise ValueError if user enters the firstweekday parameter bigger that 6 (with accordance with the Zen). Am I missing something? Thank you in advance Best regards, Aynur ? ?????????, ?.?. ??????????? From alan.gauld at yahoo.co.uk Mon Sep 11 10:26:57 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 11 Sep 2017 15:26:57 +0100 Subject: [Tutor] How to create an object in database only if the object is not already there? In-Reply-To: References: Message-ID: On 11/09/17 10:38, GMX wrote: > I am using `pony` orm to write a simple class as my model. pony is not a standard module (indeed I only heard about it from your email) so you are probably best asking on the pony support forum if such exists or via the author. > from pony.orm import Database > from pony.orm import Required, Optional > from pony.orm import db_session > from pony.orm import select, commit > > DB_PATH = ?db.sqlite? > > db = Database() > > class Course(db.Entity): > """ > A class to represent a course > """ > title = Required(str, unique=True) > url = Optional(str, unique=True) > thumbnail = Optional(str) > processed = Optional(bool, default=False) > > db.bind(provider='sqlite', filename=DB_PATH, create_db=True) > db.generate_mapping(create_tables=True) > ``` > > Now when I create a Course object like this: > > >>> Course(title=?A new course?) > > An object is create in the database. I don?t want to have this behaviour, But that's kind of what an ORM does. The whole point of using an ORM is that it translates your objects into database transactions. > but what I want to be doing is create a Course object > and then only commit in the database if the course is > not already available in the database. Thats very specific to your ORM. There probably are options to do that, but you will need to ask the pony users. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alan.gauld at yahoo.co.uk Mon Sep 11 10:28:55 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 11 Sep 2017 15:28:55 +0100 Subject: [Tutor] question about calendar module in standard libriary In-Reply-To: References: Message-ID: On 11/09/17 11:58, ????? ??????????? wrote: > So, the question is why not explicitly raise ValueError if > user enters the firstweekday parameter bigger that 6 Its a valid question but you probably need to find the original PEP document to find the answer and that module has been around for a *very* long time! -- 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 steve at pearwood.info Mon Sep 11 10:42:36 2017 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 12 Sep 2017 00:42:36 +1000 Subject: [Tutor] question about calendar module in standard libriary In-Reply-To: References: Message-ID: <20170911144234.GS13110@ando.pearwood.info> On Mon, Sep 11, 2017 at 10:58:51AM +0000, ????? ??????????? wrote: > class Calendar(object): > def __init__(self, firstweekday=0): > self.firstweekday = firstweekday # 0 = Monday, 6 = Sunday > > def getfirstweekday(self): > return self._firstweekday % 7 > > def setfirstweekday(self, firstweekday): > self._firstweekday = firstweekday > > firstweekday = property(getfirstweekday, setfirstweekday) > > As far as I understand, even if user enters inappropriate firstweekday > parameter (bigger than 6) during instansiation of the Calendar, the > Calendar swallows it (and latter returns correct firstweekday value > due to %7 in getfirstweekday method). That looks right to me. > So, the question is why not explicitly raise ValueError if user enters > the firstweekday parameter bigger that 6 (with accordance with the > Zen). Am I missing something? I don't think there is any specific reason. Probably just the personal choice of the person who wrote the code. The decision doesn't appear to be documented anywhere, so I don't think its official behaviour. -- Steve From __peter__ at web.de Mon Sep 11 12:17:26 2017 From: __peter__ at web.de (Peter Otten) Date: Mon, 11 Sep 2017 18:17:26 +0200 Subject: [Tutor] question about calendar module in standard libriary References: Message-ID: ????? ??????????? wrote: > Hello all! > > > There is a class Calendar in calendar.py in standard libriary. > > > class Calendar(object): > """ > Base calendar class. This class doesn't do any formatting. It > simply provides data to subclasses. > """ > > def __init__(self, firstweekday=0): > self.firstweekday = firstweekday # 0 = Monday, 6 = Sunday > > def getfirstweekday(self): > return self._firstweekday % 7 > > def setfirstweekday(self, firstweekday): > self._firstweekday = firstweekday > > firstweekday = property(getfirstweekday, setfirstweekday) > > > As far as I understand, even if user enters inappropriate firstweekday > parameter (bigger than 6) during instansiation of the Calendar, the > Calendar swallows it (and latter returns correct firstweekday value due to > %7 in getfirstweekday method). > > > So, the question is why not explicitly raise ValueError if user enters the > firstweekday parameter bigger that 6 (with accordance with the Zen). Am I > missing something? It does no harm to those who use the class properly while it allows those unfamiliar with the idea of a 0th day to specify 7 instead 0. The behaviour thus may be interpreted as an example of """Be strict when sending and tolerant when receiving.""" See also https://tools.ietf.org/html/rfc1958 https://en.wikipedia.org/wiki/Robustness_principle From sjeik_appie at hotmail.com Mon Sep 11 10:12:41 2017 From: sjeik_appie at hotmail.com (Albert-Jan Roskam) Date: Mon, 11 Sep 2017 14:12:41 +0000 Subject: [Tutor] How to create an object in database only if the object is not already there? In-Reply-To: References: Message-ID: From: Tutor on behalf of GMX Sent: Monday, September 11, 2017 9:38 AM To: tutor at python.org Subject: [Tutor] How to create an object in database only if the object is not already there? Now when I create a Course object like this: ? ? >>> Course(title=?A new course?) An object is create in the database. I don?t want to have this behaviour, but what I want to be doing is create a Course object and then only commit in the database if the course is not already available in the database. ===> I have no experience with Pony, but with Sqlalchemy one could do something like: if not session.query(Course.title).filter_by(title=?A new course?).first(): course = Course(title=?A new course?) Or perhaps you could set a primary key constraint on course title. From robertvstepp at gmail.com Tue Sep 12 15:05:39 2017 From: robertvstepp at gmail.com (boB Stepp) Date: Tue, 12 Sep 2017 14:05:39 -0500 Subject: [Tutor] How to write database-agnostic python code? (Is this even possible?) Message-ID: As I continue to read about SQL, one thing jumps out: There are many differences between how SQL statements are implemented among the different database products. Even for relatively simple, straightforward things like field concatenation. One DB might use "||" as the operator. Another uses "+". Yet another only uses a "CONCAT" function. This is crazy! It almost seems like I would need to write a DB-translator class that takes a SQL statement (In some DB's version) and the target DB as inputs, and then translates the statement into that DB's particular usage of SQL. So how does one write one's python program to be DB-agnostic? And if this is impossible, then what is the best way to structure the overall program to isolate this SQL-specific stuff by itself, insofar as possible, so any migrations to a new DB type is as easy as possible? -- boB From mats at wichmann.us Tue Sep 12 15:17:55 2017 From: mats at wichmann.us (Mats Wichmann) Date: Tue, 12 Sep 2017 13:17:55 -0600 Subject: [Tutor] How to write database-agnostic python code? (Is this even possible?) In-Reply-To: References: Message-ID: On 09/12/2017 01:05 PM, boB Stepp wrote: > As I continue to read about SQL, one thing jumps out: There are many > differences between how SQL statements are implemented among the > different database products. Even for relatively simple, > straightforward things like field concatenation. One DB might use > "||" as the operator. Another uses "+". Yet another only uses a > "CONCAT" function. This is crazy! > > It almost seems like I would need to write a DB-translator class that > takes a SQL statement (In some DB's version) and the target DB as > inputs, and then translates the statement into that DB's particular > usage of SQL. So how does one write one's python program to be > DB-agnostic? And if this is impossible, then what is the best way to > structure the overall program to isolate this SQL-specific stuff by > itself, insofar as possible, so any migrations to a new DB type is as > easy as possible? > Well, sort of. dbapi is designed to make access to databases agnostic to the underlying db: https://www.python.org/dev/peps/pep-0249/ (and bunches of tutorials on the internet) But dealing with "local extension to SQL" is a different problem. SQL is actually a standard and should work the same everywhere, but then people decide they need to improve it. Being agnostic means avoiding mysql-specific syntax, postgres-specific syntax, etc. Which means you need to know what is specific in the first place... From sjeik_appie at hotmail.com Tue Sep 12 16:50:26 2017 From: sjeik_appie at hotmail.com (Albert-Jan Roskam) Date: Tue, 12 Sep 2017 20:50:26 +0000 Subject: [Tutor] How to write database-agnostic python code? (Is this even possible?) In-Reply-To: References: Message-ID: (sorry for top posting) There are various SQL abstraction layers for this. I have only used Sqlalchemy. This package has two APIs: query and ORM. You might want to look at the query API. This is closer to SQL than ORM. You can use straight(obj) to inspect the SQL that's emitted ________________________________ From: Tutor on behalf of boB Stepp Sent: Tuesday, September 12, 2017 7:05:39 PM To: tutor Subject: [Tutor] How to write database-agnostic python code? (Is this even possible?) As I continue to read about SQL, one thing jumps out: There are many differences between how SQL statements are implemented among the different database products. Even for relatively simple, straightforward things like field concatenation. One DB might use "||" as the operator. Another uses "+". Yet another only uses a "CONCAT" function. This is crazy! It almost seems like I would need to write a DB-translator class that takes a SQL statement (In some DB's version) and the target DB as inputs, and then translates the statement into that DB's particular usage of SQL. So how does one write one's python program to be DB-agnostic? And if this is impossible, then what is the best way to structure the overall program to isolate this SQL-specific stuff by itself, insofar as possible, so any migrations to a new DB type is as easy as possible? -- boB _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From robertvstepp at gmail.com Tue Sep 12 21:58:10 2017 From: robertvstepp at gmail.com (boB Stepp) Date: Tue, 12 Sep 2017 20:58:10 -0500 Subject: [Tutor] How to write database-agnostic python code? (Is this even possible?) In-Reply-To: References: Message-ID: On Tue, Sep 12, 2017 at 2:17 PM, Mats Wichmann wrote: > On 09/12/2017 01:05 PM, boB Stepp wrote: >> As I continue to read about SQL, one thing jumps out: There are many >> differences between how SQL statements are implemented among the >> different database products. Even for relatively simple, >> straightforward things like field concatenation. One DB might use >> "||" as the operator. Another uses "+". Yet another only uses a >> "CONCAT" function. This is crazy! [...] > But dealing with "local extension to SQL" is a different problem. SQL > is actually a standard and should work the same everywhere, but then > people decide they need to improve it. Being agnostic means avoiding > mysql-specific syntax, postgres-specific syntax, etc. Which means you > need to know what is specific in the first place... In the example I was alluding to, concatenating fields, surely this is a "standard SQL" query? So, for instance, if I had a Sales table with FirstName and LastName fields and wanted to use SQL to get the full name from these: 1) In Microsoft SQL Server: SELECT FirstName + ' ' + LastName FROM Sales 2) MySQL: SELECT CONCAT(FirstName, ' ', LastName) FROM Sales; 3)SQLite: SELECT FirstName || ' ' || LastName FROM Sales; And these would be the SQL commands/statements I would have cursor.execute use from the sqlite3 module. They would be different depending on which database product I was using. Am I horribly misunderstanding something??? And if not, I have several other apparently fundamental SQL examples where similar situations exist based on the book on SQL I am only 43 pages into! -- boB From robertvstepp at gmail.com Tue Sep 12 22:15:10 2017 From: robertvstepp at gmail.com (boB Stepp) Date: Tue, 12 Sep 2017 21:15:10 -0500 Subject: [Tutor] How to write database-agnostic python code? (Is this even possible?) In-Reply-To: References: Message-ID: On Tue, Sep 12, 2017 at 8:58 PM, boB Stepp wrote: > On Tue, Sep 12, 2017 at 2:17 PM, Mats Wichmann wrote: >> On 09/12/2017 01:05 PM, boB Stepp wrote: >>> As I continue to read about SQL, one thing jumps out: There are many >>> differences between how SQL statements are implemented among the >>> different database products. Even for relatively simple, >>> straightforward things like field concatenation. One DB might use >>> "||" as the operator. Another uses "+". Yet another only uses a >>> "CONCAT" function. This is crazy! > > [...] > >> But dealing with "local extension to SQL" is a different problem. SQL >> is actually a standard and should work the same everywhere, but then >> people decide they need to improve it. Being agnostic means avoiding >> mysql-specific syntax, postgres-specific syntax, etc. Which means you >> need to know what is specific in the first place... > > In the example I was alluding to, concatenating fields, surely this is > a "standard SQL" query? So, for instance, if I had a Sales table with > FirstName and LastName fields and wanted to use SQL to get the full > name from these: > > 1) In Microsoft SQL Server: > SELECT > FirstName + ' ' + LastName > FROM Sales > > 2) MySQL: > SELECT > CONCAT(FirstName, ' ', LastName) > FROM Sales; > > 3)SQLite: > SELECT > FirstName || ' ' || LastName > FROM Sales; > > And these would be the SQL commands/statements I would have > cursor.execute use from the sqlite3 module... I misspoke somewhat here. I suppose there are different database wrappers to access different databases. But the standard python database api should still use a cursor.execute to run SQL commands no matter the wrapper? > ... They would be different > depending on which database product I was using. Am I horribly > misunderstanding something??? And if not, I have several other > apparently fundamental SQL examples where similar situations exist > based on the book on SQL I am only 43 pages into! > > -- > boB -- boB From alan.gauld at yahoo.co.uk Wed Sep 13 03:41:27 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 13 Sep 2017 08:41:27 +0100 Subject: [Tutor] How to write database-agnostic python code? (Is this even possible?) In-Reply-To: References: Message-ID: On 12/09/17 20:05, boB Stepp wrote: > As I continue to read about SQL, one thing jumps out: There are many > differences between how SQL statements are implemented > usage of SQL. So how does one write one's python program to be > DB-agnostic? And if this is impossible, then what is the best way to > structure the overall program to isolate this SQL-specific stuff by > itself, The classic approach is to have 3 tiers in the architecture. 1) UI layer - usually a GUI toolkit - the VC part of MVC 2) Business logic - its the M bit of MVC... and where most OOP happens 3) data access - the ugly underbelly where theory hits physical storage By sticking to those roles you can build all the data access (SQL) stuff in a layer of its own that can be swapped out if needed. You simply(?!) create an API from the logic layer to fetch whatever data is needed. Now looking at that architecture it should be apparent that it implies certain things about the kind of SQL you should be executing in the data layer. It should not be UI/presentation focused - thats the GUI layer's job. So no formatting etc. Also it should not be doing any fancy calculations - that's the logic layer's job. It should just be fetching raw data. If you limit your SQL to queries fetching raw fields you should find you are working with the portable subset of standard SQL most of the time. Basically you are just mapping your logic layer object models to underlying data tables. (It is entirely possible to build database apps with a thin GUI atop the DB and thats what Oracle et al would like, but you then forget about database portability, you will be using lots of proprietary database vendor technology. Effectively you put both the logic and data access into the database server) Most of the non-standard SQL things are there to enable you to write batch reports that can be sent direct to a printer/text file without any other coding. If you are using a language like Python you can do much nicer formatting there and perform much more sophisticated processing, so you don't need those features of SQL. Basically SELECT some, fields (never *) FROM some tables WHERE some conditions ORDER BY a key Should be much SQL as you need in the data access layer. The one thing that bugs me with the dbapi is that it does not hide the data insertion character so in some databases you use ? and in others its %. So you may want to write your data layer functions with the insertion character parameterized so you only need to change it in one place. The other area that differs is login, but thees not much can be done with that and you should only need to do it once. HTH, Alan G From __peter__ at web.de Wed Sep 13 03:55:47 2017 From: __peter__ at web.de (Peter Otten) Date: Wed, 13 Sep 2017 09:55:47 +0200 Subject: [Tutor] How to write database-agnostic python code? (Is this even possible?) References: Message-ID: boB Stepp wrote: > On Tue, Sep 12, 2017 at 2:17 PM, Mats Wichmann wrote: >> On 09/12/2017 01:05 PM, boB Stepp wrote: >>> As I continue to read about SQL, one thing jumps out: There are many >>> differences between how SQL statements are implemented among the >>> different database products. Even for relatively simple, >>> straightforward things like field concatenation. One DB might use >>> "||" as the operator. Another uses "+". Yet another only uses a >>> "CONCAT" function. This is crazy! > > [...] > >> But dealing with "local extension to SQL" is a different problem. SQL >> is actually a standard and should work the same everywhere, but then >> people decide they need to improve it. Being agnostic means avoiding >> mysql-specific syntax, postgres-specific syntax, etc. Which means you >> need to know what is specific in the first place... > > In the example I was alluding to, concatenating fields, surely this is > a "standard SQL" query? So, for instance, if I had a Sales table with > FirstName and LastName fields and wanted to use SQL to get the full > name from these: > > 1) In Microsoft SQL Server: > SELECT > FirstName + ' ' + LastName > FROM Sales > > 2) MySQL: > SELECT > CONCAT(FirstName, ' ', LastName) > FROM Sales; > > 3)SQLite: > SELECT > FirstName || ' ' || LastName > FROM Sales; > > And these would be the SQL commands/statements I would have > cursor.execute use from the sqlite3 module. They would be different > depending on which database product I was using. Am I horribly > misunderstanding something??? And if not, I have several other > apparently fundamental SQL examples where similar situations exist > based on the book on SQL I am only 43 pages into! Life is hard ;) While the specific problem can be worked around by performing the concatenation in Python cs.execute("select firstname, lastname from sales;") for firstname, lastname in iter(cursor.fetchone, None): print("{} {}".format(firstname, lastname)) at some point you will need to write database specific SQL or delegate that task to a library -- sqlalchemy was already mentioned. I suspect that you will then end up learning both the SQL dialects and the ORM API... Personally I would start with a single database, try to achieve a clean structure and good unit test coverage -- and worry about generalisation later. From pcpanchal123 at gmail.com Tue Sep 12 20:28:41 2017 From: pcpanchal123 at gmail.com (Pareshkumar Panchal) Date: Tue, 12 Sep 2017 20:28:41 -0400 Subject: [Tutor] Create CDF using Python Message-ID: I am a python beginner, I have set of data in excel file. i would like to create the CDF plots & find 40th & 70th Percentile using python with functionality of refresh whenever the data sets update. Thanks in Advance for your help ! From edmundopierre at yahoo.com Wed Sep 13 11:03:41 2017 From: edmundopierre at yahoo.com (edmundo pierre) Date: Wed, 13 Sep 2017 15:03:41 +0000 (UTC) Subject: [Tutor] I can not display the answer of the Limit when I click the Button Answer. References: <1284113657.1190681.1505315021953.ref@mail.yahoo.com> Message-ID: <1284113657.1190681.1505315021953@mail.yahoo.com> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?That is my code below. I used tkinter to calculate the limit of a function. I used Grid geometry, but when I click the button Answer, but nothing comes out so I do not know where I am doing wrong. I hope my code will be visible, I have tried to fixer my email. Thank you! ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? from tkinter import* ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? from sympy import*? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?from sympy import init_printing? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?from math import*? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? init_printing(use_unicode = True) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?root = Tk() ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #Create the Limits Function ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?def Limits():? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?x = symbols("x")? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?A = var1.get()? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? B = var2.get()? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? C= limit(A,x,B)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?text.insert(0.0,"str(C)") ? ? p ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?#Create variable ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? var1 = StringVar()? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? var2 =IntVar()? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?var3 =IntVar()? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? var4 =IntVar()? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? var5 =StringVar() ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Tops = Label(root, font = ("arial",10,"bold"),fg = "red",bg = "blue",text ="Limits").grid(row=0, column =0 ,sticky = W) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?#Create the Limts frame ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Limits =Label(root,font = ("arial",10,"italic"),text = "Limite de:").grid(row = 2,column = 0, sticky = W) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Limits = Entry(root,text = var1).grid( row = 2,column = 1,sticky = W) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Limits = Label( root,font = ("arial",10,"italic"),text = "de x ? ").grid(row = 2,column = 2) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Limits= Entry(root,text = var2).grid(row = 2, column = 3) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Limits =Label(root,font = ("arial",10,"italic"),text = "? gauche(-) et droite(+)").grid(row = 2,column = 4,rowspan = 1,columnspan = ? 1 ,sticky = W) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Limits = Entry(root,text = var5).grid(row = 2,column = 5, sticky = W) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?#Create a text on the right frame ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? text = Text(root).grid(row = 6, column = 2) ? ? ? ? ? ? ? ? ? ? ? #Create the Button ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Button1 = Button(Limits,font = ("arial",20,"bold"),fg = "red",bg = "blue", text = "Answer",command = Limits).grid(row = 7, colu= 2) From mats at wichmann.us Wed Sep 13 13:10:09 2017 From: mats at wichmann.us (Mats Wichmann) Date: Wed, 13 Sep 2017 11:10:09 -0600 Subject: [Tutor] I can not display the answer of the Limit when I click the Button Answer. In-Reply-To: <1284113657.1190681.1505315021953@mail.yahoo.com> References: <1284113657.1190681.1505315021953.ref@mail.yahoo.com> <1284113657.1190681.1505315021953@mail.yahoo.com> Message-ID: <20152002-ad9f-adf2-7810-51307094b70b@wichmann.us> On 09/13/2017 09:03 AM, edmundo pierre via Tutor wrote: > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?That is my code below. I used tkinter to calculate the limit of a function. I used Grid geometry, but when I click the button Answer, but nothing comes out so I do not know where I am doing wrong. I hope my code will be visible, I have tried to fixer my email. I am afraid it didn't... https://mail.python.org/pipermail/tutor/2017-September/112024.html From alan.gauld at yahoo.co.uk Wed Sep 13 13:47:56 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 13 Sep 2017 18:47:56 +0100 Subject: [Tutor] How is database creation normally handled? In-Reply-To: <1ed22231-64cf-b23b-101c-4a6a6d7b2838@wichmann.us> References: <1ed22231-64cf-b23b-101c-4a6a6d7b2838@wichmann.us> Message-ID: On 10/09/17 18:41, Mats Wichmann wrote: > restore" and a local copy of the (mysql) db will be built. So that's > what I just agreed with: the setup process happens outside the code app ... > maybe it's not that unusual - any time you need to start with a > reproducible known state of the DB, that would work pretty well; it > would be a disaster for a DB where there were tons of commits, stuff > changed mostly interactively, etc. Absolutely, I'm talking about getting it up and running, once you start loading live data and making regular changes you are into proper data backups and logging and all the rest of the stuff any good DBA should do in their sleep. Possibly even a hot standby using snapshots/synchronisation technology. But in the early dev/test phase of a project you regularly have to pull down and setup the database and in those cases separate SQL scripts are the best option in my experience. -- 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 Sep 13 13:53:39 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 13 Sep 2017 18:53:39 +0100 Subject: [Tutor] Create CDF using Python In-Reply-To: References: Message-ID: On 13/09/17 01:28, Pareshkumar Panchal wrote: > I have set of data in excel file. i would like to create the CDF plots & > find 40th & 70th Percentile using python with functionality of refresh > whenever the data sets update. There may be a good reason for this but frankly that's what spreadsheets are good at so it is probably a lot easier doing it in Excel. The next easiest way is to export it from Excel into a CSV file and then use Python's csv module to read that and process the data. If you really must access the data directly from Excel then there are a couiple of third party modules you can install that will let you do that. The plotting and calculation stuff should be available via SciPy but I'm no expert on that, ask the SciPy community for guidance. But if its already in Excel I'd just use Excel... -- 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 maxischmeii at gmail.com Wed Sep 13 14:17:06 2017 From: maxischmeii at gmail.com (Maxime S) Date: Wed, 13 Sep 2017 20:17:06 +0200 Subject: [Tutor] How to write database-agnostic python code? (Is this even possible?) In-Reply-To: References: Message-ID: 2017-09-13 3:58 GMT+02:00 boB Stepp : > On Tue, Sep 12, 2017 at 2:17 PM, Mats Wichmann wrote: > > On 09/12/2017 01:05 PM, boB Stepp wrote: > >> As I continue to read about SQL, one thing jumps out: There are many > >> differences between how SQL statements are implemented among the > >> different database products. Even for relatively simple, > >> straightforward things like field concatenation. One DB might use > >> "||" as the operator. Another uses "+". Yet another only uses a > >> "CONCAT" function. This is crazy! > > [...] > > > But dealing with "local extension to SQL" is a different problem. SQL > > is actually a standard and should work the same everywhere, but then > > people decide they need to improve it. Being agnostic means avoiding > > mysql-specific syntax, postgres-specific syntax, etc. Which means you > > need to know what is specific in the first place... > > In the example I was alluding to, concatenating fields, surely this is > a "standard SQL" query? So, for instance, if I had a Sales table with > FirstName and LastName fields and wanted to use SQL to get the full > name from these: > Concatenation is indeed part of the ISO standard (using the || operator). Unfortunately no DB respect the standard 100%. There is a nice comparaison of the different behaviours here: http://troels.arvin.dk/db/rdbms/ It is very hard to create an application that is truly DB-independent. Most applications just pick one and then have to stick with it ever after. Garanti sans virus. www.avast.com <#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2> From robertvstepp at gmail.com Wed Sep 13 23:11:08 2017 From: robertvstepp at gmail.com (boB Stepp) Date: Wed, 13 Sep 2017 22:11:08 -0500 Subject: [Tutor] How to write database-agnostic python code? (Is this even possible?) In-Reply-To: References: Message-ID: On Wed, Sep 13, 2017 at 2:41 AM, Alan Gauld via Tutor wrote: > The classic approach is to have 3 tiers in the architecture. > 1) UI layer - usually a GUI toolkit - the VC part of MVC > 2) Business logic - its the M bit of MVC... and where most OOP happens > 3) data access - the ugly underbelly where theory hits physical storage > > By sticking to those roles you can build all the data > access (SQL) stuff in a layer of its own that can be swapped > out if needed. You simply(?!) create an API from the logic > layer to fetch whatever data is needed. [...] > If you limit your SQL to queries fetching raw fields you > should find you are working with the portable subset of > standard SQL most of the time. Basically you are just > mapping your logic layer object models to underlying > data tables. ... This sounds a lot like the approach I've evolved into taking with the proprietary scripting language I use at work: I only do what I absolutely have to do in Pinnacle HotScripting to get the needed data to my Python program, let Python do all necessary processing, and finally have Python generate the needed Pinnacle HotScripting commands that need to be run to affect the planning environment. [...] > Basically > > SELECT some, fields (never *) Why no "*"? Does this open up a security vulnerability? > FROM some tables > WHERE some conditions > > ORDER BY a key > > Should be much SQL as you need in the data access layer. This simplifies things greatly! Does this mean I can stop reading up on SQL? ~(:>)) I suppose there will be a similar parallel for writing data back into the database? > The one thing that bugs me with the dbapi is that it does > not hide the data insertion character so in some databases > you use ? and in others its %. I will try to keep this in the back of my mind ... Thanks, Alan. This helps a lot! -- boB From robertvstepp at gmail.com Wed Sep 13 23:36:15 2017 From: robertvstepp at gmail.com (boB Stepp) Date: Wed, 13 Sep 2017 22:36:15 -0500 Subject: [Tutor] How to write database-agnostic python code? (Is this even possible?) In-Reply-To: References: Message-ID: On Wed, Sep 13, 2017 at 2:55 AM, Peter Otten <__peter__ at web.de> wrote: > boB Stepp wrote: >> >> And these would be the SQL commands/statements I would have >> cursor.execute use from the sqlite3 module. They would be different >> depending on which database product I was using. Am I horribly >> misunderstanding something??? And if not, I have several other >> apparently fundamental SQL examples where similar situations exist >> based on the book on SQL I am only 43 pages into! > > Life is hard ;) And I was so badly hoping to achieve SQL nirvana! ~(:>)) > While the specific problem can be worked around by performing the > concatenation in Python > > cs.execute("select firstname, lastname from sales;") > for firstname, lastname in iter(cursor.fetchone, None): > print("{} {}".format(firstname, lastname)) Yeah, this just reinforces Alan's points. > at some point you will need to write database specific SQL or delegate that > task to a library -- sqlalchemy was already mentioned. I suspect that you > will then end up learning both the SQL dialects and the ORM API... I maybe did not realize the full import of Albert-Jan's suggestion then. Will sqlalchemy (Or a similar product.) totally abstract away these annoying SQL variations amongst db vendors? > Personally I would start with a single database, try to achieve a clean > structure and good unit test coverage -- and worry about generalisation > later. I've decided I'm going even simpler than the chess ratings project. I've been lately taking lots of blood pressure readings lately (Stressed out from all my efforts at Python, y'know!). I think the data will fit nicely in A SINGLE TABLE -- date, time of day, BP reading, and a comments field. I will apply TDD, OOP, sqlite3, etc. to what I hope will be a simple but meaningful project. The "business logic", as Alan puts it, will be for a given date range report the mean BP reading. If I get really enthusiastic, I might add some trend analysis features, ... -- see, already feature creep is sneaking in! My real hope from doing this simplified project is that the amount of code might be small enough that I can post it here in its entirety for critiquing. Hopefully this will get most of my GCEs (Gross Conceptual Errors) out of the way and I can return to the chess ratings project, my wife's projects, etc., and have a better hope of success. -- boB From robertvstepp at gmail.com Wed Sep 13 23:40:57 2017 From: robertvstepp at gmail.com (boB Stepp) Date: Wed, 13 Sep 2017 22:40:57 -0500 Subject: [Tutor] How to write database-agnostic python code? (Is this even possible?) In-Reply-To: References: Message-ID: I forgot one other thing you mentioned that I had a question about. On Wed, Sep 13, 2017 at 2:41 AM, Alan Gauld via Tutor wrote: > If you limit your SQL to queries fetching raw fields you > should find you are working with the portable subset of > standard SQL most of the time. Basically you are just > mapping your logic layer object models to underlying > data tables. My initial thoughts here are that typically a particular class would map to a particular table and that each object instance would correspond to a row in said table. Is this typically how this is done? -- boB From cs at cskk.id.au Wed Sep 13 19:51:37 2017 From: cs at cskk.id.au (Cameron Simpson) Date: Thu, 14 Sep 2017 09:51:37 +1000 Subject: [Tutor] How to write database-agnostic python code? (Is this even possible?) In-Reply-To: References: Message-ID: <20170913235137.GA84670@cskk.homeip.net> On 12Sep2017 14:05, boB Stepp wrote: >As I continue to read about SQL, one thing jumps out: There are many >differences between how SQL statements are implemented among the >different database products. Even for relatively simple, >straightforward things like field concatenation. One DB might use >"||" as the operator. Another uses "+". Yet another only uses a >"CONCAT" function. This is crazy! > >It almost seems like I would need to write a DB-translator class that >takes a SQL statement (In some DB's version) and the target DB as >inputs, and then translates the statement into that DB's particular >usage of SQL. So how does one write one's python program to be >DB-agnostic? And if this is impossible, then what is the best way to >structure the overall program to isolate this SQL-specific stuff by >itself, insofar as possible, so any migrations to a new DB type is as >easy as possible? I have two approaches myself. First, all DB drivers present the DBI interface from PEP 249, so once connected you have a standard suite of methods. Most databases speak a superset of SQL92, so provided your SQL is not esoteric you can just get on with things. Um, except for parameter syntax ("$9", "?", ":foo_9")? You can work around that. Secondly, there's SQLAlchemy. It knows the dialects and asks you to write "native" looking python syntax for selects etc. So stuff like: db_conn.select(t.col1 == 9 and t.col2 == 10) where "t" is a "table" object it has handed you. I believe these are just special objects with attributes for columns and the right __eq__ etc dunder methods to compute the correct SQL syntax. No escaping or param substitution in your own code. It also has an ORM, which I've not used. Cheers, Cameron Simpson (formerly cs at zip.com.au) From __peter__ at web.de Thu Sep 14 03:06:59 2017 From: __peter__ at web.de (Peter Otten) Date: Thu, 14 Sep 2017 09:06:59 +0200 Subject: [Tutor] How to write database-agnostic python code? (Is this even possible?) References: Message-ID: boB Stepp wrote: >> at some point you will need to write database specific SQL or delegate >> that task to a library -- sqlalchemy was already mentioned. I suspect >> that you will then end up learning both the SQL dialects and the ORM >> API... > > I maybe did not realize the full import of Albert-Jan's suggestion > then. Will sqlalchemy (Or a similar product.) totally abstract away > these annoying SQL variations amongst db vendors? While I have absolutley no experience with sqlalchemy I would be surprised if it didn't abstract away the common variations like concatenation. I would be even more surprised if there weren't any operations, vendor- specific or standardised, that can be performed with SQL, but not highlevel sqlalchemy. That's the "Law of leaky abstractions", https://www.joelonsoftware.com/2002/11/11/the-law-of-leaky-abstractions/ From breamoreboy at yahoo.co.uk Thu Sep 14 00:33:06 2017 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Thu, 14 Sep 2017 05:33:06 +0100 Subject: [Tutor] How to write database-agnostic python code? (Is this even possible?) In-Reply-To: <20170913235137.GA84670@cskk.homeip.net> References: <20170913235137.GA84670@cskk.homeip.net> Message-ID: On 14/09/2017 00:51, Cameron Simpson wrote: > > Secondly, there's SQLAlchemy. It knows the dialects and asks you to > write "native" looking python syntax for selects etc. So stuff like: > > ?db_conn.select(t.col1 == 9 and t.col2 == 10) > > where "t" is a "table" object it has handed you. I believe these are > just special objects with attributes for columns and the right __eq__ > etc dunder methods to compute the correct SQL syntax. No escaping or > param substitution in your own code. It also has an ORM, which I've not > used. > > Cheers, > Cameron Simpson (formerly cs at zip.com.au) SQLAlchemy isn't the only Python ORM of course. There is a useful little article about ORMs and their availability here https://www.fullstackpython.com/object-relational-mappers-orms.html. A more detailed comparison is given here http://pythoncentral.io/sqlalchemy-vs-orms/ which refers to storm, which is only mentioned in passing in the first link. I've successfully used peewee, I can't really comment on the others. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email has been checked for viruses by AVG. http://www.avg.com From alan.gauld at yahoo.co.uk Thu Sep 14 04:15:30 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 14 Sep 2017 09:15:30 +0100 Subject: [Tutor] How to write database-agnostic python code? (Is this even possible?) In-Reply-To: References: Message-ID: On 14/09/17 04:11, boB Stepp wrote: >> SELECT some, fields (never *) > > Why no "*"? Does this open up a security vulnerability? Not so much security as resilience to change. If you use * and the data schema changes to include extra fields then your * query returns the extra fields and all the code using that query now has to handle those extra fields. Typically you have an API call that looks like: getOpenOrders(custID) -> tuple(orderID, Order date, productID, Value) But suddenly your code has to handle getOpenOrders(custID) -> tuple(orderID, Order date, SalesRep, productID, Priority, Value) The fields you want are now at different indexes in the tuple, all the code that extracts those fields has to change. Whereas if your qurery specifies the fields it wants then the data schema can change and it doesn't affect your API return values. > I suppose there will be a similar parallel for writing data back into > the database? Yes, the INSERT command has the general shape INSERT INTO table (col_name, col_name,...) VALUES (val, val, ...) WHERE filter_expression and UPDATE has UPDATE table SET col_name = val1 col_name = val2 ... WHERE filter_expression and delete is just DELETE FROM table WHERE filter_expression The critical thing to remember is that these all work on ALL ROWS that match the filter. Its easy to modify more than you intend with a lazily written expression! So in practice, for a typical OO data layer I usually have a WHERE clause like ... WHERE primary_key = object.ID -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alan.gauld at yahoo.co.uk Thu Sep 14 04:30:33 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 14 Sep 2017 09:30:33 +0100 Subject: [Tutor] How to write database-agnostic python code? (Is this even possible?) In-Reply-To: References: Message-ID: On 14/09/17 04:40, boB Stepp wrote: >> mapping your logic layer object models to underlying >> data tables. > > My initial thoughts here are that typically a particular class would > map to a particular table and that each object instance would > correspond to a row in said table. Is this typically how this is > done? Simplistically yes, but it gets more tricky with inheritance. Does the superclass data live in the same table as the subclass data or is there a separate table for the superclass? The first solution implies a lot of duplication of fields across tables and can also lead to problems if you are saving changes to a list of superclass references using class methods (but hopefully you would be using polymorphic methods which will solve the problem for you!) More seriously there is a problem when you want a query that operates at the superclass level - all red cars for example. You now have to query all tables representing subclasses of car - and modify the query code every time you create a new subclass... The second solution requires links between child and parent tables and the queries will need to navigate this link for any fields used within the query that exist in the superclass. This can be a significant overhead, especially on deep inheritance trees. There is no simple solution and it depends on the nature of your application which evil is worst. It's a fundamental disjoint between OOP and the relational model. Many RDBMS now have mechanisms to assist in this by, for example creating views that join all superclass tables together into a virtual single table. I don't think SQLite has anything automatic yet, you need to decide your solution yourself - which for a small dataset might even involve instantiating all objects and doing the query on the in-memory objects!. (itertools is your friend!) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From cs at cskk.id.au Thu Sep 14 18:52:56 2017 From: cs at cskk.id.au (Cameron Simpson) Date: Fri, 15 Sep 2017 08:52:56 +1000 Subject: [Tutor] How to write database-agnostic python code? (Is this even possible?) In-Reply-To: References: Message-ID: <20170914225256.GA83629@cskk.homeip.net> On 14Sep2017 09:15, Alan Gauld wrote: >On 14/09/17 04:11, boB Stepp wrote: >>> SELECT some, fields (never *) >> >> Why no "*"? Does this open up a security vulnerability? > >Not so much security as resilience to change. >If you use * and the data schema changes to include extra >fields then your * query returns the extra fields and all >the code using that query now has to handle those extra >fields. Alan's point about resilience is the core point here. But also: efficiency. Fetch only the columns you need. Some columns may be very bulky (eg "BLOB"s in MySQL - arbitrary chunks of data), and some may require secondary accesses (i.e. the data may be variable sized and stored apart from the core fixed size column data). And data warehouses often empty "columnar storage", where each column is stored separated from theothers, supporting very fast scans of single columns. So asking for more has performance implications, sometimes large. Also, asking for exactly what you need promotes cleaner and more correct code (or code easier to prove correct and keep correct); you don't accidentally introduce dependence on some column you didn't really need; that can have flow on effects as surrounding code might start to take advantage of the extra information more than required. Such code is harder to modify later because more semantics need to be preserved. Cheers, Cameron Simpson (formerly cs at zip.com.au) From arj.python at gmail.com Thu Sep 14 22:19:14 2017 From: arj.python at gmail.com (Abdur-Rahmaan Janhangeer) Date: Fri, 15 Sep 2017 06:19:14 +0400 Subject: [Tutor] New to Python In-Reply-To: References: Message-ID: 1) valuable resources : sololearn tutorialspoint python3 top tip : concentrate on concepts good books for beginners there are many like .. dive into python 3 available free online .. automate the boring stuff available free online a good reference-like book: .. Learning Python .. Programming Python both by Mark Lutz Concerning the FAQ since i see it is prohibited to say RTFM, i propose the faq link is given along with some kind words. Abdur-Rahmaan Janhangeer, Mauritius abdurrahmaanjanhangeer.wordpress.com On 7 Sep 2017 20:51, "Vikram Singh" wrote: > I've been learning Python from Google For Education > . A little help will be > appreciated regarding the right way and right tutorials to learn Python > from. Thanks in advance. > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From arj.python at gmail.com Thu Sep 14 22:28:14 2017 From: arj.python at gmail.com (Abdur-Rahmaan Janhangeer) Date: Fri, 15 Sep 2017 06:28:14 +0400 Subject: [Tutor] Help In-Reply-To: <1083763863.4763253.1504801236327@mail.yahoo.com> References: <1083763863.4763253.1504801236327.ref@mail.yahoo.com> <1083763863.4763253.1504801236327@mail.yahoo.com> Message-ID: besides already written notes, i suggest use .split(".") floatNum = input("> ") parts = floatNum.split(".") whole = parts[0] deci = parts[1] Abdur-Rahmaan Janhangeer, Mauritius abdurrahmaanjanhangeer.wordpress.com On 7 Sep 2017 21:51, "edmundo pierre via Tutor" wrote: > > I am trying to write a code that asks an User to Enter a decimal number, > the my program should separate the number in two parts like this: > 1 cae:Enter a number: 12.9Decimal part: 12whole part : 9 > 2 case:Enter: 10Decimal par:Whole part: 10 > That is my code: > A = input("Enter a number") > C = [ ] for i in str(A) : > C.append(i) if i ==".# period ": break aa= > "".join(c) print(aa, end =" ") > if i == int(A): print(" It is the whole part") > I can not display the whole part when I enter float number. Plus when I > enter an integer, it takes that enteger as a decimal number. Any help? > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From humanityexists at outlook.com Thu Sep 14 23:08:21 2017 From: humanityexists at outlook.com (Pratyusha Thundena) Date: Fri, 15 Sep 2017 03:08:21 +0000 Subject: [Tutor] Python Help Message-ID: <0ACF4699-A9DD-4F6F-A557-D26D6CB4BEA1@outlook.com> How do check to see if string ?garden? contains a vowel(a, e , i , o, u , or y) using a for loop? I?m a beginner and I only just got into loops. Please help. Thanks! Sincerely, Pratricia Dewan From alan.gauld at yahoo.co.uk Fri Sep 15 12:25:19 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 15 Sep 2017 17:25:19 +0100 Subject: [Tutor] Python Help In-Reply-To: <0ACF4699-A9DD-4F6F-A557-D26D6CB4BEA1@outlook.com> References: <0ACF4699-A9DD-4F6F-A557-D26D6CB4BEA1@outlook.com> Message-ID: On 15/09/17 04:08, Pratyusha Thundena wrote: > How do check to see if string ?garden? contains a > vowel(a, e , i , o, u , or y) using a for loop? Hi, this sounds like homework and we won't do your homework for you, but we do give hints. How would you do this without a computer? There are (at least) two ways: 1) you can check each letter in your word and see if it is a vowel. 2) we can check each vowel and see if it is in your word A for loop gives you each item in a sequence one by one so would work in either solution. Let's consider option 1 in pseudo-code: for each letter in word: if letter is a vowel: do something... what? So that leads to two questions: 1) how do you tell if a letter is a vowel? Hint: look at what the string 'in' operator does 2) What do you want to do if you find a vowel? print something? exit? or what...? See if that helps, if you still need help come back with a specific question (the more specific the question, the more specific will be the answer) and show us your code plus any error messages you received. -- 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 DerekSmith at racksquared.com Fri Sep 15 16:50:52 2017 From: DerekSmith at racksquared.com (Derek Smith) Date: Fri, 15 Sep 2017 20:50:52 +0000 Subject: [Tutor] array input from keyboard Message-ID: I need to accept input from the user then store in an array/list. This is my 1st python script. #!/usr/bin/env python # Derek Smith # 09/2017 # accept volumes for TSM for tape mgmt. import os import sys nput1 = "" nput2 = "" nput1 = input("\nIs your input 'file' based or 'cli' based? ") if nput1 == "file" : nput2 = input("Please provide your input file? ") nput2 = nput2.lower() print (nput2) fh = open(nput2,"r") for ln in fh: ln = ln.rstrip() os.system("/usr/bin/dsmadmc -id=dereksmith -password=x -dataonly=yes move drm %s" %ln, "tost=onsiter") os.system("/usr/bin/dsmadmc -id=dereksmith -password=x -dataonly=yes move drm %s" %ln, "wherest=vaultr tost=onsiter") elif nput1 == "cli" : vols = [] vols = input("Please enter your volume ids, comma delimited? ") vols = vols.upper() for vols in vols : vols = vols.rstrip() print("/usr/bin/dsmadmc -id=dereksmith -password=dereksmith -dataonly=yes move drm %s" %vols, "tost=onsiter") elif print ("Incorrect input, exiting.") sys.exit(99) __OUTPUT__ # python tsm_moveVR_tonsiter.py Is your input 'file' based or 'cli' based? cli Please enter your volume ids, comma delimited? r20344l5,r20355l5 /usr/bin/dsmadmc -id=dereksmith -password=x -dataonly=yes move drm R tost=onsiter /usr/bin/dsmadmc -id=dereksmith -password=x -dataonly=yes move drm 2 tost=onsiter /usr/bin/dsmadmc -id=dereksmith -password=x -dataonly=yes move drm 0 tost=onsiter /usr/bin/dsmadmc -id=dereksmith -password=x -dataonly=yes move drm 3 tost=onsiter /usr/bin/dsmadmc -id=dereksmith -password=x -dataonly=yes move drm 4 tost=onsite ... ... ... Its printing each element per line. I have tried various changes, read online help but gave up looking. I need it to print as below: /usr/bin/dsmadmc -id=dereksmith -password=x -dataonly=yes move drm R20344L5 tost=onsiter /usr/bin/dsmadmc -id=dereksmith -password=x -dataonly=yes move drm R20355L5 tost=onsiter Thank you!! Derek Smith | Unix/TSM Administrator | Racksquared Data Centers :: dereksmith at racksquared.com *: www.racksquared.com | www.racksquared.jobs [cid:image003.png at 01D2E9AA.1B9CF8F0] From fernando.gmp at gmail.com Fri Sep 15 16:16:22 2017 From: fernando.gmp at gmail.com (Fernando Gualberto) Date: Fri, 15 Sep 2017 17:16:22 -0300 Subject: [Tutor] Python + Irc Message-ID: How i can integrate Python program with IRC to send Messages from program to the IRC? -- *Fernando Gualberto Martins* From alan.gauld at yahoo.co.uk Fri Sep 15 18:39:54 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 15 Sep 2017 23:39:54 +0100 Subject: [Tutor] array input from keyboard In-Reply-To: References: Message-ID: On 15/09/17 21:50, Derek Smith wrote: > if nput1 == "file" : > nput2 = input("Please provide your input file? ") > nput2 = nput2.lower() Are you sure the filenames will always be lower case? > print (nput2) > fh = open(nput2,"r") You should wrap that in a try/except in case there is an issue opening the file, like it doesn't exist or you don't have permission. > > for ln in fh: > ln = ln.rstrip() > os.system("/usr/bin/dsmadmc -id=dereksmith -password=x -dataonly=yes move drm %s" %ln, "tost=onsiter") > os.system("/usr/bin/dsmadmc -id=dereksmith -password=x -dataonly=yes move drm %s" %ln, "wherest=vaultr tost=onsiter") > Strictly speaking you should close() the file here since you opened it. > elif nput1 == "cli" : > vols = [] > vols = input("Please enter your volume ids, comma delimited? ") > vols = vols.upper() > for vols in vols : Here you assign an empty list to vols Then you assign a string to vols, losing the array then you convert the string to upper case - are volume IDs always upper case? Then you go into a really confusing for loop where you assign the characters of vols to vols which seems to work OK except that at the end of the loop vols will equal the last character in vols... It would be much easier to read and understand if you used a different name for the loop variable. > vols = vols.rstrip() > print("/usr/bin/dsmadmc -id=dereksmith -password=dereksmith -dataonly=yes move drm %s" %vols, "tost=onsiter") > Is your input 'file' based or 'cli' based? cli > Please enter your volume ids, comma delimited? r20344l5,r20355l5 > /usr/bin/dsmadmc -id=dereksmith -password=x -dataonly=yes move drm R tost=onsiter > /usr/bin/dsmadmc -id=dereksmith -password=x -dataonly=yes move drm 2 tost=onsiter > /usr/bin/dsmadmc -id=dereksmith -password=x -dataonly=yes move drm 0 tost=onsiter > /usr/bin/dsmadmc -id=dereksmith -password=x -dataonly=yes move drm 3 tost=onsiter > /usr/bin/dsmadmc -id=dereksmith -password=x -dataonly=yes move drm 4 tost=onsite > Its printing each element per line. Because you never split the line into its separate elements, Python just sees a long string. You need to add a vols = vols.split(',') to get a list of substrings. You probably want that just after the call to upper() Incidentally if you are using string formatting its more common to just have a single string and the insereted value at the ehnd, so your line would look like: print("/usr/bin/dsmadmc -id=dereksmith -password=dereksmith -dataonly=yes move drm %s tost=onsiter" % vols) And of course the name/passwd should really be parameterized: print("/usr/bin/dsmadmc -id=%s -password=%s -dataonly=yes move drm %s tost=onsiter" % (nm, pwd, vols) ) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alan.gauld at yahoo.co.uk Fri Sep 15 18:44:17 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 15 Sep 2017 23:44:17 +0100 Subject: [Tutor] Python + Irc In-Reply-To: References: Message-ID: On 15/09/17 21:16, Fernando Gualberto wrote: > How i can integrate Python program with IRC to send Messages from program > to the IRC? If you google "python irc module" you should get several hits. Try looking at the documentation and pick the one that seems easiest for your needs. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From robertvstepp at gmail.com Fri Sep 15 21:06:39 2017 From: robertvstepp at gmail.com (boB Stepp) Date: Fri, 15 Sep 2017 20:06:39 -0500 Subject: [Tutor] How to write database-agnostic python code? (Is this even possible?) In-Reply-To: References: Message-ID: On Thu, Sep 14, 2017 at 2:06 AM, Peter Otten <__peter__ at web.de> wrote: > That's the "Law of leaky abstractions", > https://www.joelonsoftware.com/2002/11/11/the-law-of-leaky-abstractions/ Thanks for the link to this article. I really enjoyed reading it. In fact, so far every article I have read by Joel Spolsky has been both an enjoyable and educational read! Near the very end of the article he says, "... And when you need to hire a programmer to do mostly VB programming, it?s not good enough to hire a VB programmer, because they will get completely stuck in tar every time the VB abstraction leaks..." That has me wondering: Where does the Python abstraction leak??? -- boB From robertvstepp at gmail.com Fri Sep 15 21:51:30 2017 From: robertvstepp at gmail.com (boB Stepp) Date: Fri, 15 Sep 2017 20:51:30 -0500 Subject: [Tutor] How to write database-agnostic python code? (Is this even possible?) In-Reply-To: References: <20170913235137.GA84670@cskk.homeip.net> Message-ID: On Wed, Sep 13, 2017 at 11:33 PM, Mark Lawrence via Tutor wrote: > SQLAlchemy isn't the only Python ORM of course. There is a useful little > article about ORMs and their availability here > https://www.fullstackpython.com/object-relational-mappers-orms.html. A more > detailed comparison is given here > http://pythoncentral.io/sqlalchemy-vs-orms/ which refers to storm, which is > only mentioned in passing in the first link. I've successfully used peewee, > I can't really comment on the others. Thanks for these links, Mark. I had not come across Full Stack Python previously. It appears to be a goldmine of useful information! I think I will put off doing ORM studies until I get into something more substantial than I am currently working with. Besides, after reading the Joel Spolsky article on leaky data abstractions, it appears to be a wise move to get proficient in SQL first before going on to ORMs. -- boB From oliver.schmitz at godaddy.com Fri Sep 15 17:52:11 2017 From: oliver.schmitz at godaddy.com (Oliver Schmitz) Date: Fri, 15 Sep 2017 23:52:11 +0200 Subject: [Tutor] Python + Irc In-Reply-To: References: Message-ID: <22adf006-fd42-1969-a25d-38351a6cc435@godaddy.com> Hi Fernando, i guess the most popular and feature-rich IRC library for python is (guess how it is named ...) irc by jaraco. pypi: https://pypi.python.org/pypi/irc github: https://github.com/jaraco/irc If you want to build something more advanced (quite difficult for beginners) on your own, you could have a look at twisted's IRC protocol implementation: http://twistedmatrix.com/documents/current/api/twisted.words.im.ircsupport.html Twisted is quite impressive and performant, but as it is event-based, it is more difficult to implement and for most usecases, this would be overkill. Nevertheless it's great ;). Am 15.09.17 um 22:16 schrieb Fernando Gualberto: > How i can integrate Python program with IRC to send Messages from program > to the IRC? > Regards, Oliver Schmitz From oliver.schmitz at godaddy.com Fri Sep 15 18:07:31 2017 From: oliver.schmitz at godaddy.com (Oliver Schmitz) Date: Sat, 16 Sep 2017 00:07:31 +0200 Subject: [Tutor] array input from keyboard In-Reply-To: References: Message-ID: Hi Derek, you are iterating over the input string, this will result in single chars in each iteration of your for loop. You could use `split` on your string to split the string into a list, splitted by comma. Instead of: > vols = [] > vols = input("Please enter your volume ids, comma delimited? ") > for vols in vols : > vols = vols.rstrip() Do the following: vols = input("Please enter your volume ids, comma delimited? ") for vol in vols.replace(" ", "").split(","): # dome something with your vol The creation of an empty list isn't needed. You shouldn't name the element of the iteration the same as the object you are iterating on. So instead of `for vols in vols` use for `vol in vols` as inside the loop you are working on a single volume instead of multiples. The `replace` will remove all your whitespace (as it seems your strings don't need/dont' want them). Next the `split` will split your input elements comma seperated into a list for the iteration by the `for` loop. Am 15.09.17 um 22:50 schrieb Derek Smith: > I need to accept input from the user then store in an array/list. This is my 1st python script. > > #!/usr/bin/env python > # Derek Smith > # 09/2017 > # accept volumes for TSM for tape mgmt. > > import os > import sys > > nput1 = "" > nput2 = "" > nput1 = input("\nIs your input 'file' based or 'cli' based? ") > > if nput1 == "file" : > nput2 = input("Please provide your input file? ") > nput2 = nput2.lower() > print (nput2) > fh = open(nput2,"r") > > for ln in fh: > ln = ln.rstrip() > os.system("/usr/bin/dsmadmc -id=dereksmith -password=x -dataonly=yes move drm %s" %ln, "tost=onsiter") > os.system("/usr/bin/dsmadmc -id=dereksmith -password=x -dataonly=yes move drm %s" %ln, "wherest=vaultr tost=onsiter") > > elif nput1 == "cli" : > vols = [] > vols = input("Please enter your volume ids, comma delimited? ") > vols = vols.upper() > for vols in vols : > vols = vols.rstrip() > print("/usr/bin/dsmadmc -id=dereksmith -password=dereksmith -dataonly=yes move drm %s" %vols, "tost=onsiter") > elif > print ("Incorrect input, exiting.") > sys.exit(99) > > > __OUTPUT__ > > # python tsm_moveVR_tonsiter.py > > Is your input 'file' based or 'cli' based? cli > Please enter your volume ids, comma delimited? r20344l5,r20355l5 > /usr/bin/dsmadmc -id=dereksmith -password=x -dataonly=yes move drm R tost=onsiter > /usr/bin/dsmadmc -id=dereksmith -password=x -dataonly=yes move drm 2 tost=onsiter > /usr/bin/dsmadmc -id=dereksmith -password=x -dataonly=yes move drm 0 tost=onsiter > /usr/bin/dsmadmc -id=dereksmith -password=x -dataonly=yes move drm 3 tost=onsiter > /usr/bin/dsmadmc -id=dereksmith -password=x -dataonly=yes move drm 4 tost=onsite > ... > ... > ... > > Its printing each element per line. I have tried various changes, read online help but gave up looking. > I need it to print as below: > > > /usr/bin/dsmadmc -id=dereksmith -password=x -dataonly=yes move drm R20344L5 tost=onsiter > /usr/bin/dsmadmc -id=dereksmith -password=x -dataonly=yes move drm R20355L5 tost=onsiter > > Thank you!! > Derek Smith | Unix/TSM Administrator | Racksquared Data Centers > :: dereksmith at racksquared.com *: www.racksquared.com | www.racksquared.jobs > > [cid:image003.png at 01D2E9AA.1B9CF8F0] > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > Regards, Oliver Schmitz From miknau1 at gmail.com Fri Sep 15 20:32:52 2017 From: miknau1 at gmail.com (miknau1 at gmail.com) Date: Fri, 15 Sep 2017 20:32:52 -0400 Subject: [Tutor] Python HW question Message-ID: <59bc7134.d4a3370a.e301a.922e@mx.google.com> Good evening, I really need some Python help. I?m a beginner and have some hw for class: Write a program that calculates longest substring of increasing letters in st, say st=?Supercalifragilisticexpialidocious?. Do it in a few steps: 1.Find longest increasing substring starting from 0th position, use comparison <= to check if one letter greater than other 2. Use function len of object str(ing) to find length of string 3.Write one line of code that choses longest string from 2 4. Loop through all possible starting position start of the str, and find longest increasing substring of st. I have no idea where to start this problem. Any help you can provide will be spectacular. Thanks, Mikhail Sent from Mail for Windows 10 From alan.gauld at yahoo.co.uk Sat Sep 16 03:55:50 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 16 Sep 2017 08:55:50 +0100 Subject: [Tutor] Python HW question In-Reply-To: <59bc7134.d4a3370a.e301a.922e@mx.google.com> References: <59bc7134.d4a3370a.e301a.922e@mx.google.com> Message-ID: On 16/09/17 01:32, miknau1 at gmail.com wrote: > I really need some Python help. I?m a beginner and have some hw for class: OK, Usual caveat: we don;t do your homework, just give hints. > Write a program that calculates longest substring of increasing letters in st, say st=?Supercalifragilisticexpialidocious?. Do it in a few steps: I'm assuming that means a string where each letter is higher than the previous one, such as 'best'? > 1.Find longest increasing substring starting from 0th position, > use comparison <= to check if one letter greater than other You don't tell us about how much Python or programming experience you have so I'll assume its near zero and apologize if I'm pitching too low. Do you know how to write some code to check each letter in a string, one by one? If so could you write a short program to test whether a string is increasing? ie start at the beginning and check whether each letter is greater than the previous one. If you can do that then the rest of the homework becomes much easier. Do you know how to define your own function? If so that will also make things slightly simpler. > 2. Use function len of object str(ing) to find length of string > 3.Write one line of code that choses longest string from 2 If you can do 2 then 3 is trivial. For example if we have two strings: s1 = 'Python' s2 = 'Perl' Can you write code to display the lengths of s1 and s2? Can you then write a single line of code that chooses which string is longesst? > 4. Loop through all possible starting position start> of the str, and find longest increasing substring of st. This is combining step 1 with steps 2 and 3. > I have no idea where to start this problem. Start with 1 above. Write some code to determine if a string is an increasing string. If you know how put it in a function, but that's a nice to have... If you get stuck come back here and show us the code and any error messages you get. We'll try to help you fix it. -- 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 ben+python at benfinney.id.au Sat Sep 16 03:56:09 2017 From: ben+python at benfinney.id.au (Ben Finney) Date: Sat, 16 Sep 2017 17:56:09 +1000 Subject: [Tutor] Python HW question References: <59bc7134.d4a3370a.e301a.922e@mx.google.com> Message-ID: <85y3pf5b6u.fsf@benfinney.id.au> writes: > I really need some Python help. I?m a beginner and have some hw for > class: Welcome, and congratulations on beginning to learn Python! > Write a program that calculates longest substring of increasing > letters in st, say st=?Supercalifragilisticexpialidocious?. That is *very* brief. Do you at least understand what the problem is ? that is, can you re-state the requirement and explain it to someone else (me)? > I have no idea where to start this problem. Any help you can provide > will be spectacular. * Do you know how to ?write a program? with Python, and run it successfully? I will assume you do know that. * Can you show a Python statement that ?uses the comparison ?<=? to check if one letter is greater than another?? * Can you show a ?loop through all possible starting positions of a sub-string? in a larger string? -- \ ?If you always want the latest and greatest, then you have to | `\ buy a new iPod at least once a year.? ?Steve Jobs, MSNBC | _o__) interview 2006-05-25 | Ben Finney From mats at wichmann.us Sat Sep 16 12:38:20 2017 From: mats at wichmann.us (Mats Wichmann) Date: Sat, 16 Sep 2017 10:38:20 -0600 Subject: [Tutor] Python Help In-Reply-To: References: <0ACF4699-A9DD-4F6F-A557-D26D6CB4BEA1@outlook.com> Message-ID: <848b74f2-0e77-fed5-a5ca-eb39f9b2daf8@wichmann.us> On 09/15/2017 10:25 AM, Alan Gauld via Tutor wrote: > On 15/09/17 04:08, Pratyusha Thundena wrote: >> How do check to see if string ?garden? contains a >> vowel(a, e , i , o, u , or y) using a for loop? > > Hi, this sounds like homework and we won't do > your homework for you, but we do give hints. > > How would you do this without a computer? > There are (at least) two ways: > 1) you can check each letter in your word and > see if it is a vowel. > 2) we can check each vowel and see if it is > in your word > > A for loop gives you each item in a sequence one > by one so would work in either solution. > > Let's consider option 1 in pseudo-code: > > for each letter in word: > if letter is a vowel: > do something... what? > > So that leads to two questions: > 1) how do you tell if a letter is a vowel? > Hint: look at what the string 'in' operator does > 2) What do you want to do if you find a vowel? > print something? exit? or what...? > > See if that helps, if you still need help come back with > a specific question (the more specific the question, the > more specific will be the answer) and show us your code > plus any error messages you received. It also leads to a chance to lecture (just what you wanted, right): Computer programming is hard. One of the reasons it's hard is because you don't always know exactly what the problem you're trying to solve consists of. Here in a learning situation it's going to be safe, but in the real world you will often find what seemed obvious to the person who framed the requirement may be full of ambiguities or subtle effects. Like: Does a string contain a vowel? - you list the 5 1/2 vowels from English. y is the 1/2: grammatically, it is considered a vowel in words like 'baby' or 'sky', but it is a consonant in words like 'yes' or 'yogurt'. Should your solution take this into account? It would make the code vastly more complicated, because you would have to teach it to divide the word correctly into syllables, and apply the rule for when it is treated as a consonant. Which means you probably need to start by validating that the input is a correct word. Of course you are not going to worry about that solving a classroom problem (unless the instructor or book is being very sneaky). and then there is the complication of language. should your solution work for English only? Or for other languages? I grew up speaking a language where ??? are vowels. If you are trying to process information entered by a user in a website, for example, there's a pretty reasonable chance that it should understand other languages, and then you need to look into character-set specifications, unicode, etc. Enjoy learning Python! From arj.python at gmail.com Sun Sep 17 09:50:46 2017 From: arj.python at gmail.com (Abdur-Rahmaan Janhangeer) Date: Sun, 17 Sep 2017 17:50:46 +0400 Subject: [Tutor] Python + Irc In-Reply-To: References: Message-ID: i made an irc bot (was my first python project meaning i was googling like py print since i already knew other langs but had to know py syntax and so ... much unpythonic stuffs out there) https://github.com/Abdur-rahmaanJ/honeybot if you want to contribute, welcome ! this is from scratch ! Abdur-Rahmaan Janhangeer, Mauritius abdurrahmaanjanhangeer.wordpress.com On 16 Sep 2017 01:20, "Fernando Gualberto" wrote: How i can integrate Python program with IRC to send Messages from program to the IRC? -- *Fernando Gualberto Martins* _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From DerekSmith at racksquared.com Mon Sep 18 13:59:39 2017 From: DerekSmith at racksquared.com (Derek Smith) Date: Mon, 18 Sep 2017 17:59:39 +0000 Subject: [Tutor] pip install of subprocess module Message-ID: Hi All, Thank for the recent replies. I want to use the subprocess.run module b/c per the docs os.system is antiquated and cannot use more than 1 arg in 3.6. Why am I doing wrong below? Also couldn't I just copy and paste the subprocess code from github and create a file, but then how do I tell python this is a new module called os.system since this py file requires os.system and os.spawn* I am on AIX 7.1, python 3.6.2, pip 9.0.1 Thx agn! # pip install git+https://github.com/python/cpython/blob/3.6/Lib/subprocess.py -vvv Collecting git+https://github.com/python/cpython/blob/3.6/Lib/subprocess.py Cloning https://github.com/python/cpython/blob/3.6/Lib/subprocess.py to /tmp/pip-bw8rryo9-build Running command git clone -q https://github.com/python/cpython/blob/3.6/Lib/subprocess.py /tmp/pip-bw8rryo9-build remote: Not Found fatal: repository 'https://github.com/python/cpython/blob/3.6/Lib/subprocess.py/' not found Cleaning up... Command "git clone -q https://github.com/python/cpython/blob/3.6/Lib/subprocess.py /tmp/pip-bw8rryo9-build" failed with error code 128 in None Exception information: Traceback (most recent call last): File "/opt/freeware/lib/python3.6/site-packages/pip/basecommand.py", line 215, in main status = self.run(options, args) File "/opt/freeware/lib/python3.6/site-packages/pip/commands/install.py", line 335, in run wb.build(autobuilding=True) File "/opt/freeware/lib/python3.6/site-packages/pip/wheel.py", line 749, in build self.requirement_set.prepare_files(self.finder) File "/opt/freeware/lib/python3.6/site-packages/pip/req/req_set.py", line 380, in prepare_files ignore_dependencies=self.ignore_dependencies)) File "/opt/freeware/lib/python3.6/site-packages/pip/req/req_set.py", line 620, in _prepare_file session=self.session, hashes=hashes) File "/opt/freeware/lib/python3.6/site-packages/pip/download.py", line 805, in unpack_url unpack_vcs_link(link, location) File "/opt/freeware/lib/python3.6/site-packages/pip/download.py", line 479, in unpack_vcs_link vcs_backend.unpack(location) File "/opt/freeware/lib/python3.6/site-packages/pip/vcs/__init__.py", line 286, in unpack self.obtain(location) File "/opt/freeware/lib/python3.6/site-packages/pip/vcs/git.py", line 142, in obtain self.run_command(['clone', '-q', url, dest]) File "/opt/freeware/lib/python3.6/site-packages/pip/vcs/__init__.py", line 325, in run_command spinner) File "/opt/freeware/lib/python3.6/site-packages/pip/utils/__init__.py", line 707, in call_subprocess % (command_desc, proc.returncode, cwd)) pip.exceptions.InstallationError: Command "git clone -q https://github.com/python/cpython/blob/3.6/Lib/subprocess.py /tmp/pip-bw8rryo9-build" failed with error code 128 in None Derek Smith | Unix/TSM Administrator | Racksquared Data Centers :: dereksmith at racksquared.com *: www.racksquared.com | www.racksquared.jobs [cid:image003.png at 01D2E9AA.1B9CF8F0] From alan.gauld at yahoo.co.uk Mon Sep 18 18:24:31 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 18 Sep 2017 23:24:31 +0100 Subject: [Tutor] pip install of subprocess module In-Reply-To: References: Message-ID: On 18/09/17 18:59, Derek Smith wrote: > I want to use the subprocess.run module b/c per the> docs os.system is antiquated Its not so much antiquated but it is deprecated because it is severely limited, all you get back is an error code. > Why am I doing wrong below? You are trying to install it, but subprocess is part of the standard library, it should already be installoed. Just use the >>> prompt and try >>> import subprocess If you don't get an error then its already there. > python this is a new module called os.system subprocess is its own moduile it is not part of os. import os import subprocess > since this py file requires os.system and os.spawn* Both of those can be replaced by calls within subprocess but they will not be called by those names. Read the subprocess module documents carefully they give example code for replacing these functions. -- 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 clarabuyss at gmail.com Tue Sep 19 03:48:40 2017 From: clarabuyss at gmail.com (Clara Chua) Date: Tue, 19 Sep 2017 15:48:40 +0800 Subject: [Tutor] Require help for a script Message-ID: Hi Python tutor, I require help for a script that asks user for number of rows, r and number of columns c, and generates a r x c matrix with the following values: - The value of each element in the first row is the number of the column - The value of each element in the first column is the number of the row - The rest of the elements each has a value equal to the sum of the element above it and the element to the left. Example of such matrix if a user inputs r=4 and c=5 1 2 3 4 5 2 4 7 11 16 3 7 14 25 41 4 11 25 50 91 We were told to use the for loops as well as numpy for this. Thank you in advance! From alan.gauld at yahoo.co.uk Tue Sep 19 04:54:18 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 19 Sep 2017 09:54:18 +0100 Subject: [Tutor] Require help for a script In-Reply-To: References: Message-ID: On 19/09/17 08:48, Clara Chua wrote: > Hi Python tutor, I require help for a script that asks user for number of > rows, r and number of columns c, and generates a r x c matrix with the > following values: What help do you need? Which part of the above do you not know how to do? Can you ask the user for two numbers and store them in two variables? It says generate a matrix, I'm guessing that's where the numpy stuff comes in although a standard Python list would be just as effective in this case. In either case can you define a new two dimensional data structure using the sizes read previously? > - The value of each element in the first row is the number of the column Do you know how to populate the first row? > - The value of each element in the first column is the number of the row Do you know how to populate the first column (or first element of each row)? > - The rest of the elements each has a value equal to the sum of the element > above it and the element to the left. Do you know how to iterate over each cell in your matrix, starting with the second row? For a given cell do you know how to read the value above? For a given cell do you know how to read the value left? Do you know how to sum two values and store the result in the current cell? > Example of such matrix if a user inputs r=4 and c=5 > > 1 2 3 4 5 > 2 4 7 11 16 ... Tell us which part of the problem you are stuck with. Show us the code you have tried and any error messages (in full). Then we can give you more focused help. -- 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 dvnsarma at gmail.com Tue Sep 19 10:06:56 2017 From: dvnsarma at gmail.com (=?UTF-8?B?RC5WLk4uU2FybWEg4LCh4LC/LuCwteCwvy7gsI7gsKjgsY0u4LC24LCw4LGN4LCu?=) Date: Tue, 19 Sep 2017 19:36:56 +0530 Subject: [Tutor] Require help for a script In-Reply-To: References: Message-ID: First fill the first row and first column of the matrix with seperate loops. Then the rest of the elements of the matrix can be filled with nested loops. regards, Sarma. On Tue, Sep 19, 2017 at 1:18 PM, Clara Chua wrote: > Hi Python tutor, I require help for a script that asks user for number of > rows, r and number of columns c, and generates a r x c matrix with the > following values: > > - The value of each element in the first row is the number of the column > - The value of each element in the first column is the number of the row > - The rest of the elements each has a value equal to the sum of the element > above it and the element to the left. > > Example of such matrix if a user inputs r=4 and c=5 > > 1 2 3 4 5 > 2 4 7 11 16 > 3 7 14 25 41 > 4 11 25 50 91 > > We were told to use the for loops as well as numpy for this. > Thank you in advance! > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From clarabuyss at gmail.com Tue Sep 19 10:11:12 2017 From: clarabuyss at gmail.com (C) Date: Tue, 19 Sep 2017 14:11:12 +0000 Subject: [Tutor] (no subject) Message-ID: Hi Python tutor, I require help for a script that asks user for number of rows, r and number of columns c, and generates a r x c matrix with the following values: - The value of each element in the first row is the number of the column - The value of each element in the first column is the number of the row - The rest of the elements each has a value equal to the sum of the element above it and the element to the left. Example of such matrix if a user inputs r=4 and c=5 1 2 3 4 5 2 4 7 11 16 3 7 14 25 41 4 11 25 50 91 For now I only filled up the first row and first columns. We were told to use the for loops as well as numpy for this. However, I cannot quite figure out how I can use loops to do this. Please help! Thank you in advance! From saahndongransom at gmail.com Tue Sep 19 10:45:17 2017 From: saahndongransom at gmail.com (Saahndong Ransom) Date: Tue, 19 Sep 2017 07:45:17 -0700 Subject: [Tutor] Tutor Digest, Vol 163, Issue 28 In-Reply-To: References: Message-ID: What are the uses of python? What are easy way to learn python? On Sep 19, 2017 15:07, wrote: Send Tutor mailing list submissions to tutor at python.org To subscribe or unsubscribe via the World Wide Web, visit https://mail.python.org/mailman/listinfo/tutor or, via email, send a message with subject or body 'help' to tutor-request at python.org You can reach the person managing the list at tutor-owner at python.org When replying, please edit your Subject line so it is more specific than "Re: Contents of Tutor digest..." Today's Topics: 1. pip install of subprocess module (Derek Smith) 2. Re: pip install of subprocess module (Alan Gauld) 3. Require help for a script (Clara Chua) 4. Re: Require help for a script (Alan Gauld) 5. Re: Require help for a script (D.V.N.Sarma ??.??.???.????) ---------- Forwarded message ---------- From: Derek Smith To: "tutor at python.org" Cc: Bcc: Date: Mon, 18 Sep 2017 17:59:39 +0000 Subject: [Tutor] pip install of subprocess module Hi All, Thank for the recent replies. I want to use the subprocess.run module b/c per the docs os.system is antiquated and cannot use more than 1 arg in 3.6. Why am I doing wrong below? Also couldn't I just copy and paste the subprocess code from github and create a file, but then how do I tell python this is a new module called os.system since this py file requires os.system and os.spawn* I am on AIX 7.1, python 3.6.2, pip 9.0.1 Thx agn! # pip install git+https://github.com/python/cpython/blob/3.6/Lib/ subprocess.py -vvv Collecting git+https://github.com/python/cpython/blob/3.6/Lib/subprocess.py Cloning https://github.com/python/cpython/blob/3.6/Lib/subprocess.py to /tmp/pip-bw8rryo9-build Running command git clone -q https://github.com/python/ cpython/blob/3.6/Lib/subprocess.py /tmp/pip-bw8rryo9-build remote: Not Found fatal: repository 'https://github.com/python/cpython/blob/3.6/Lib/ subprocess.py/' not found Cleaning up... Command "git clone -q https://github.com/python/cpython/blob/3.6/Lib/ subprocess.py /tmp/pip-bw8rryo9-build" failed with error code 128 in None Exception information: Traceback (most recent call last): File "/opt/freeware/lib/python3.6/site-packages/pip/basecommand.py", line 215, in main status = self.run(options, args) File "/opt/freeware/lib/python3.6/site-packages/pip/commands/install.py", line 335, in run wb.build(autobuilding=True) File "/opt/freeware/lib/python3.6/site-packages/pip/wheel.py", line 749, in build self.requirement_set.prepare_files(self.finder) File "/opt/freeware/lib/python3.6/site-packages/pip/req/req_set.py", line 380, in prepare_files ignore_dependencies=self.ignore_dependencies)) File "/opt/freeware/lib/python3.6/site-packages/pip/req/req_set.py", line 620, in _prepare_file session=self.session, hashes=hashes) File "/opt/freeware/lib/python3.6/site-packages/pip/download.py", line 805, in unpack_url unpack_vcs_link(link, location) File "/opt/freeware/lib/python3.6/site-packages/pip/download.py", line 479, in unpack_vcs_link vcs_backend.unpack(location) File "/opt/freeware/lib/python3.6/site-packages/pip/vcs/__init__.py", line 286, in unpack self.obtain(location) File "/opt/freeware/lib/python3.6/site-packages/pip/vcs/git.py", line 142, in obtain self.run_command(['clone', '-q', url, dest]) File "/opt/freeware/lib/python3.6/site-packages/pip/vcs/__init__.py", line 325, in run_command spinner) File "/opt/freeware/lib/python3.6/site-packages/pip/utils/__init__.py", line 707, in call_subprocess % (command_desc, proc.returncode, cwd)) pip.exceptions.InstallationError: Command "git clone -q https://github.com/python/cpython/blob/3.6/Lib/subprocess.py /tmp/pip-bw8rryo9-build" failed with error code 128 in None Derek Smith | Unix/TSM Administrator | Racksquared Data Centers :: dereksmith at racksquared.com *: www.racksquared.com | www.racksquared.jobs [cid:image003.png at 01D2E9AA.1B9CF8F0] ---------- Forwarded message ---------- From: Alan Gauld To: tutor at python.org Cc: Bcc: Date: Mon, 18 Sep 2017 23:24:31 +0100 Subject: Re: [Tutor] pip install of subprocess module On 18/09/17 18:59, Derek Smith wrote: > I want to use the subprocess.run module b/c per the> docs os.system is antiquated Its not so much antiquated but it is deprecated because it is severely limited, all you get back is an error code. > Why am I doing wrong below? You are trying to install it, but subprocess is part of the standard library, it should already be installoed. Just use the >>> prompt and try >>> import subprocess If you don't get an error then its already there. > python this is a new module called os.system subprocess is its own moduile it is not part of os. import os import subprocess > since this py file requires os.system and os.spawn* Both of those can be replaced by calls within subprocess but they will not be called by those names. Read the subprocess module documents carefully they give example code for replacing these functions. -- 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 ---------- Forwarded message ---------- From: Clara Chua To: tutor at python.org Cc: Bcc: Date: Tue, 19 Sep 2017 15:48:40 +0800 Subject: [Tutor] Require help for a script Hi Python tutor, I require help for a script that asks user for number of rows, r and number of columns c, and generates a r x c matrix with the following values: - The value of each element in the first row is the number of the column - The value of each element in the first column is the number of the row - The rest of the elements each has a value equal to the sum of the element above it and the element to the left. Example of such matrix if a user inputs r=4 and c=5 1 2 3 4 5 2 4 7 11 16 3 7 14 25 41 4 11 25 50 91 We were told to use the for loops as well as numpy for this. Thank you in advance! ---------- Forwarded message ---------- From: Alan Gauld To: tutor at python.org Cc: Bcc: Date: Tue, 19 Sep 2017 09:54:18 +0100 Subject: Re: [Tutor] Require help for a script On 19/09/17 08:48, Clara Chua wrote: > Hi Python tutor, I require help for a script that asks user for number of > rows, r and number of columns c, and generates a r x c matrix with the > following values: What help do you need? Which part of the above do you not know how to do? Can you ask the user for two numbers and store them in two variables? It says generate a matrix, I'm guessing that's where the numpy stuff comes in although a standard Python list would be just as effective in this case. In either case can you define a new two dimensional data structure using the sizes read previously? > - The value of each element in the first row is the number of the column Do you know how to populate the first row? > - The value of each element in the first column is the number of the row Do you know how to populate the first column (or first element of each row)? > - The rest of the elements each has a value equal to the sum of the element > above it and the element to the left. Do you know how to iterate over each cell in your matrix, starting with the second row? For a given cell do you know how to read the value above? For a given cell do you know how to read the value left? Do you know how to sum two values and store the result in the current cell? > Example of such matrix if a user inputs r=4 and c=5 > > 1 2 3 4 5 > 2 4 7 11 16 ... Tell us which part of the problem you are stuck with. Show us the code you have tried and any error messages (in full). Then we can give you more focused help. -- 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 ---------- Forwarded message ---------- From: "D.V.N.Sarma ??.??.???.????" To: Clara Chua Cc: "tutor at python.org" Bcc: Date: Tue, 19 Sep 2017 19:36:56 +0530 Subject: Re: [Tutor] Require help for a script First fill the first row and first column of the matrix with seperate loops. Then the rest of the elements of the matrix can be filled with nested loops. regards, Sarma. On Tue, Sep 19, 2017 at 1:18 PM, Clara Chua wrote: > Hi Python tutor, I require help for a script that asks user for number of > rows, r and number of columns c, and generates a r x c matrix with the > following values: > > - The value of each element in the first row is the number of the column > - The value of each element in the first column is the number of the row > - The rest of the elements each has a value equal to the sum of the element > above it and the element to the left. > > Example of such matrix if a user inputs r=4 and c=5 > > 1 2 3 4 5 > 2 4 7 11 16 > 3 7 14 25 41 > 4 11 25 50 91 > > We were told to use the for loops as well as numpy for this. > Thank you in advance! > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor _______________________________________________ Tutor maillist - Tutor at python.org https://mail.python.org/mailman/listinfo/tutor From alan.gauld at yahoo.co.uk Tue Sep 19 12:48:34 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 19 Sep 2017 17:48:34 +0100 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: On 19/09/17 15:11, C wrote: > Hi Python tutor, I require help for a script that asks user for number of > rows, r and number of columns c, and generates a r x c matrix with the > following values: You already posted this in another thread, please do not multi-post it just clutters up the archive and splits the discussion. you already have replies on the original post. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alan.gauld at yahoo.co.uk Tue Sep 19 12:52:00 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 19 Sep 2017 17:52:00 +0100 Subject: [Tutor] Tutor Digest, Vol 163, Issue 28 In-Reply-To: References: Message-ID: On 19/09/17 15:45, Saahndong Ransom wrote: > What are the uses of python? > What are easy way to learn python? Python is a general purpose programming language that can be used to build many kinds of software applications from computer administration to web servers to desktop GUI applications. The Python wiki is the bet place to start, it provides many different tutorials depending on your level of existing knowledge about programming. https://wiki.python.org/moin/BeginnersGuide If you are a complete beginner you might like my tutorial, linked below. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From larrystaley07 at comcast.net Tue Sep 19 16:13:58 2017 From: larrystaley07 at comcast.net (Larry Staley) Date: Tue, 19 Sep 2017 13:13:58 -0700 Subject: [Tutor] Need Help with install of Python! Message-ID: <007001d33183$d44b7c70$7ce27550$@net> Hello I am very new to Python just having installed Python Version 2.7 onto my windows 8.1 laptop. I thought the install was successful and was entering information for my first assignment when I received an unexpected error. I executed a Sheets command using an earlier generated getSheetNames function that successfully was entered by me. However, when I ran the Sheets command I received the following: Import Error: No modules name Pandas If Pandas were not included in the original install, would it not have been caught when I input the getSheetNames function: def getSheetNames(excelfile): from pandas import ExcelFile return (ExcelFile(excelfile)).sheet_names Here again was my command: >>> excelfile=r:"C:\Users\Larry >>> sheets=getSheetNames (excelfile);sheets Traceback (most recent call last): File "", line 1, in File "", line 2, in getSheetNames ImportError: No module named pandas >>> I have in addition included the actual Python I executed and received the message. It is attached in the file. I would appreciate any help in getting this issue resolved. If I need to install Pandas, please indicate clearly how I do using my current ver 2.7 python install. Thanks again for your help. Larry Staley 650.274.6794 (c) larrystaley07 at comcast.net -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: not_finding.txt URL: From alan.gauld at yahoo.co.uk Tue Sep 19 20:18:31 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 20 Sep 2017 01:18:31 +0100 Subject: [Tutor] Need Help with install of Python! In-Reply-To: <007001d33183$d44b7c70$7ce27550$@net> References: <007001d33183$d44b7c70$7ce27550$@net> Message-ID: On 19/09/17 21:13, Larry Staley wrote: > Hello I am very new to Python just having installed Python Version 2.7 onto > my windows 8.1 laptop. I thought the install was successful and was > entering information for my first assignment when I received an unexpected > error. Where did you get your vesion of Python? If it was the standard distribution from python.org or activestate.com then it does not include any of the SciPy packages(*) and you need to install them separately. If you expect to be using mamny of these types of package you are best fetching a distribution that includes them all, for example, Anaconda or Enthought (*)Pandas is part of the SciPy suite of third party add-ons. > I executed a Sheets command using an earlier generated getSheetNames > function that successfully was entered by me. When you define a function the code inside is not executed merely compiled into a function object ready for execution. > However, when I ran the Sheets command I received the following: Its only when you call the function that the code inside gets executed. > def getSheetNames(excelfile): > from pandas import ExcelFile Its normal in Python to put all imports at the top of the file rather than inside any functions. In this case you try to import pandas everytime you call the function and while its not a big overhead it mounts up if you were calling this inside a repeating loop. And if the import was outside the function you would pick up the import error earlier. >>>> excelfile=r:"C:\Users\Larry >>>> sheets=getSheetNames > (excelfile);sheets I'm not sure what you are doing with that final ;sheets. I assuyme trying to evaluate the result of the function? It would be normal to just print it: >>> print getSheetNames(excelFile) or, if you need to store the result: >>> sheets=getSheetNames(excelfile) >>> sheets Combining commands on a single line doesn't save much typing and makes debugging harder. > I have in addition included the actual Python I executed and received the > message. It is attached in the file. attachments often get rejected by the mailer, if its not a huge file(>100 lines) just paste it into the email. Always include the full error trace too. > If I need to install Pandas, please indicate clearly how I do using my > current ver 2.7 python install. The easiest way is just to grab one of the all-inclusive Python distros mentioned above. -- 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 DerekSmith at racksquared.com Wed Sep 20 17:09:39 2017 From: DerekSmith at racksquared.com (Derek Smith) Date: Wed, 20 Sep 2017 21:09:39 +0000 Subject: [Tutor] subprocess check_output Message-ID: Why does python output this b and newline characters and how can I may it return just what I need, the number 8 w/no special characters? See below. I tried str(scratch), but no go, still prints that garbage. #!/bin/env python3 cmdstr = "/usr/bin/dsmadmc" id = "-id=dereksmith" pw = "-password=xxxx" do = "-dataonly=yes" from subprocess import check_output scratch = check_output( [ cmdstr, id, pw, do, "select", "count(*)", "from", "libvolumes", "where", "status='Scratch'", "and", "library_name='TS3200'" ] ) print (scratch) __OUTPUT__ # ./ts3200_scratchcount_check.py email sent successfully b' 8\n' Thx!! Derek Smith | Unix/TSM Administrator | Racksquared Data Centers :: dereksmith at racksquared.com *: www.racksquared.com | www.racksquared.jobs [cid:image003.png at 01D2E9AA.1B9CF8F0] From alan.gauld at yahoo.co.uk Wed Sep 20 19:13:56 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 21 Sep 2017 00:13:56 +0100 Subject: [Tutor] subprocess check_output In-Reply-To: References: Message-ID: On 20/09/17 22:09, Derek Smith wrote: > Why does python output this b and newline characters Because that's what the command is returning. The b indicates its an array of bytes (as opposed to a unicode string) Note the module dpocumentation for check_output() says: "By default, this function will return the data as encoded bytes. The actual encoding of the output data may depend on the command being invoked, so the decoding to text will often need to be handled at the application level." > how can I may it return just what I need, > the number 8 w/no special characters? You can't but you can convert the bytes into a number if that's what you want. Just use the int() function. > I tried str(scratch), but no go, still prints that garbage. That just converts the sequence of bytes into a sequence of characters. > from subprocess import check_output > scratch = check_output( [ cmdstr, id, pw, do, "select", "count(*)", "from", "libvolumes", "where", "status='Scratch'", "and", "library_name='TS3200'" ] ) You probably don't need to split the query into separate words, a single string would work just as well. Or if you want it formatted nicely a few strings: scratch = check_output( [ cmdstr, id, pw, do, "select count(*) from libvolumes", "where status='Scratch' and library_name='TS3200'" ] ) -- 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 orpha.pen_j0 at outlook.com Wed Sep 20 17:06:08 2017 From: orpha.pen_j0 at outlook.com (orpha.pen_j0 at outlook.com) Date: Wed, 20 Sep 2017 21:06:08 +0000 Subject: [Tutor] running or calling a module from the desktop Message-ID: Mmkay. I know that you know how to do this and I would appreciate it if you would tell me. Please. I would like to import, run or set path to desktop. So that I can use things there were I usually work on new items, I know how the rest of you are different from that. (I'm going to stop joking around right here, hello, I am a new user.) I'm Jason. I don't know if you could use the cloud as a means to import items? Sounds like more trouble than its worth at first... But seriously, If I have a module on my desktop, HOW would I explain to python-chevron that I would like to use this (resource, file, module, extension from it's current location?) I began typing importlib and then realized I have no-idea how to tell python what a path is.. other than the first page of the on-line tutorial. I'm 3.6Python, on Win10Pro64. Umm, I do not yet know, but Iron python capable, whatever that means. Honestly I think it has to do with VB which, they don't call that that, and forms, or something. Just another quick question, the first time I heard SQL as an acronym. I thought they said Server-Query-Language. Does anyone else remember it having been called that? I'm pretty sure a teacher said that. Was it a slip of the tongue? Possibly a thorough length of time ago in human years. Please do not forget to address my main question. (I can't help it, I haven't had company in a while.) I'm trying to call a user created Llist.py into python IDLE while using py-prompt, not interested in URL's but interested only on system files most notably those on the desktop. From alan.gauld at yahoo.co.uk Thu Sep 21 05:01:57 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 21 Sep 2017 10:01:57 +0100 Subject: [Tutor] running or calling a module from the desktop In-Reply-To: References: Message-ID: On 20/09/17 22:06, orpha.pen_j0 at outlook.com wrote: > I would like to import, run or set path to desktop. OK, On Windows go to Windows Exploder and type in the address %HOMEPATH%\Desktop Go there and you should be able to see/figure out the full path to your personal desktop folder. Then add that path to your PYTHONPATH environment variable. Python will then look in your desktop folder for modules you try to import. > So that I can use things there were I usually work > on new items, I know how the rest of you are different from that. Yes because your desktop will get awfully messy very quickly once you start working on bigger projects with lots of files. Its much better to create a folder for your code. But its your PC, you do what you want! :-) > I don't know if you could use the cloud as a > means to import items? I don;t think so, not yet anyway. > HOW would I explain to python-chevron I assume you mean the Python >>> prompt? If not you'll need to explain what Pyhon chevron is. If you do mean >>> then the above addition to PYTHONPATH should do the trick. > Iron python capable, whatever that means. Its the version of Python that runs on .NET. I've never used it but I think it pincludes a compiler that can generate .NET code that can be used with C#, VB.Net etc. > the first time I heard SQL as an acronym. I thought > they said Server-Query-Language. If they did, they were wrong. SQL has always stood for Structured Query Language. It has nothing to do with servers. -- 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 evuraan at gmail.com Thu Sep 21 18:46:29 2017 From: evuraan at gmail.com (Evuraan) Date: Thu, 21 Sep 2017 15:46:29 -0700 Subject: [Tutor] subprocess.getstatusoutput : UnicodeDecodeError Message-ID: Greetings! My search-fu failed me, so thought of finally asking this question here. How can I work around this issue where subprocess.getstatusoutput gives up, on Python 3.5.2: >>> subprocess.getstatusoutput("tail -3 /tmp/pmaster.db",) Traceback (most recent call last): File "", line 1, in File "/usr/lib/python3.5/subprocess.py", line 805, in getstatusoutput data = check_output(cmd, shell=True, universal_newlines=True, stderr=STDOUT) File "/usr/lib/python3.5/subprocess.py", line 626, in check_output **kwargs).stdout File "/usr/lib/python3.5/subprocess.py", line 695, in run stdout, stderr = process.communicate(input, timeout=timeout) File "/usr/lib/python3.5/subprocess.py", line 1059, in communicate stdout = self.stdout.read() File "/usr/lib/python3.5/encodings/ascii.py", line 26, in decode return codecs.ascii_decode(input, self.errors)[0] UnicodeDecodeError: 'ascii' codec can't decode byte 0xe0 in position 189: ordinal not in range(128) >>> That file's content is kryptonite for python apparently. Other shell operations work. >>> subprocess.getstatusoutput("file /tmp/pmaster.db",) (0, '/tmp/pmaster.db: Non-ISO extended-ASCII text, with very long lines, with LF, NEL line terminators') >>> subprocess.getstatusoutput("wc -l /tmp/pmaster.db",) (0, '155065 /tmp/pmaster.db') >>> subprocess.getstatusoutput("tail -3 /tmp/pmaster.db",) Thanks in advance for your time and advice! From mats at wichmann.us Thu Sep 21 19:36:24 2017 From: mats at wichmann.us (Mats Wichmann) Date: Thu, 21 Sep 2017 17:36:24 -0600 Subject: [Tutor] subprocess.getstatusoutput : UnicodeDecodeError In-Reply-To: References: Message-ID: <23ed39f2-347b-affb-3600-ccf24e3c87be@wichmann.us> On 09/21/2017 04:46 PM, Evuraan wrote: > Greetings! > > My search-fu failed me, so thought of finally asking this question here. > > > How can I work around this issue where subprocess.getstatusoutput gives > up, on Python 3.5.2: You picked a fun one! First off, subprocess.getstatusoutput is a dubious API, because if you call a command on a UNIX/Linux flavored system, it has the possibility of writing to both the standard output and standard error streams, but this API cannot separate them. I remember fighting this in something years and years ago. Second, this thing has changed even in the Python 3.x series, which is a bad sign (see docs): first it didn't exist, then it was put back, then the first element of the return tuple changed. Third, according to the docs, "none of the guarantees described above regarding security and exception handling consistency are valid for these functions" (referring to "legacy" getstatusoutput and getoutput) Probably - don't use it. You have your main hint in the error messages: "Non-ISO extended-ASCII text" along with failures claiming a problem decoding something that is not in the range of standard ASCII: 'ascii' codec can't decode byte 0xe0 in position 189: ordinal not in range(128)' Maybe try using subprocess.check_output, although it's not a direct replacement. But: do you really want to "tail" what's probably not really a plaintext file? Just guessing, but the .db as well as the error msgs are a hint. You'll need to fill us in more on what you want to accomplish. From evuraan at gmail.com Thu Sep 21 20:04:24 2017 From: evuraan at gmail.com (Evuraan) Date: Thu, 21 Sep 2017 17:04:24 -0700 Subject: [Tutor] subprocess.getstatusoutput : UnicodeDecodeError In-Reply-To: <23ed39f2-347b-affb-3600-ccf24e3c87be@wichmann.us> References: <23ed39f2-347b-affb-3600-ccf24e3c87be@wichmann.us> Message-ID: > > But: do you really want to "tail" what's probably not really a plaintext > file? Just guessing, but the .db as well as the error msgs are a hint. although the filename ends with a ".db", it is just a text file. not tailing a SQLite or a binary file, just happened to name it so. I work around the same sort elsewhere thusly: with open("/tmp/pmaster.db",encoding="utf-8",errors="ignore") as fobj: or even with codecs with codecs.open("/tmp/pmaster.db",encoding="utf-8",errors="ignore") as fobj: I think I've to follow suit here, instead of tail, apparently there's no "errors=ignore" for subprocess. I was hoping subprocess would be impervious to decoding errors, as the text is coming from tail. From steve at pearwood.info Thu Sep 21 20:39:57 2017 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 22 Sep 2017 10:39:57 +1000 Subject: [Tutor] subprocess.getstatusoutput : UnicodeDecodeError In-Reply-To: References: Message-ID: <20170922003957.GQ13110@ando.pearwood.info> On Thu, Sep 21, 2017 at 03:46:29PM -0700, Evuraan wrote: > How can I work around this issue where subprocess.getstatusoutput gives > up, on Python 3.5.2: getstatusoutput is a "legacy" function. It still exists for code that has already been using it, but it is not recommended for new code. https://docs.python.org/3.5/library/subprocess.html#using-the-subprocess-module Since you're using Python 3.5, let's try using the brand new `run` function and see if it does better: import subprocess result = subprocess.run(["tail", "-3", "/tmp/pmaster.db"], stdout=subprocess.PIPE) print("return code is", result.returncode) print("output is", result.stdout) It should do better than getstatusoutput, since it returns plain bytes without assuming they are ASCII. You can then decode them yourself: # try this and see if it is sensible print("output is", result.stdout.decode('latin1')) # otherwise this print("output is", result.stdout.decode('utf-8', errors='replace')) > >>> subprocess.getstatusoutput("tail -3 /tmp/pmaster.db",) > Traceback (most recent call last): [...] > File "/usr/lib/python3.5/encodings/ascii.py", line 26, in decode > return codecs.ascii_decode(input, self.errors)[0] > UnicodeDecodeError: 'ascii' codec can't decode byte 0xe0 in position 189: > ordinal not in range(128) Let's look at the error message. getstatusoutput apparently expects only pure ASCII output, because it is choking on a non-ASCII byte, namely 0xe0. Obviously 0xe0 (or in decimal, 224) is not an ASCII value, since ASCII goes from 0 to 127 only. If there's one non-ASCII byte in the file, there are probably more. So what is that mystery 0xe0 byte? It is hard to be sure, because it depends on the source. If pmaster.db is a binary file, it could mean anything or nothing. If it is a text file, it depends on the encoding that the file uses. If it comes from a Mac, it might be: py> b'\xe0'.decode('macroman') '?' If it comes from Windows in Western Europe, it might be: py> b'\xe0'.decode('latin1') '?' If it comes from Windows in Greece, it might be: py> b'\xe0'.decode('iso 8859-7') '?' and so forth. There's no absolutely reliable way to tell. This is the sort of nightmare that Unicode was invented to fix, but unfortunately there still exist millions of files, data formats and applications which insist on using rubbish "extended ASCII" encodings instead. > That file's content is kryptonite for python apparently. Other shell > operations work. > > >>> subprocess.getstatusoutput("file /tmp/pmaster.db",) > (0, '/tmp/pmaster.db: Non-ISO extended-ASCII text, with very long lines, > with LF, NEL line terminators') The `file` command agrees with me: it is not ASCII. -- Steve From evuraan at gmail.com Thu Sep 21 21:57:22 2017 From: evuraan at gmail.com (Evuraan) Date: Thu, 21 Sep 2017 18:57:22 -0700 Subject: [Tutor] subprocess.getstatusoutput : UnicodeDecodeError In-Reply-To: <20170922003957.GQ13110@ando.pearwood.info> References: <20170922003957.GQ13110@ando.pearwood.info> Message-ID: > > getstatusoutput is a "legacy" function. It still exists for code that > has already been using it, but it is not recommended for new code. > > https://docs.python.org/3.5/library/subprocess.html#using-the-subprocess-module > > Since you're using Python 3.5, let's try using the brand new `run` > function and see if it does better: > > import subprocess > result = subprocess.run(["tail", "-3", "/tmp/pmaster.db"], > stdout=subprocess.PIPE) > print("return code is", result.returncode) > print("output is", result.stdout) > > > It should do better than getstatusoutput, since it returns plain bytes > without assuming they are ASCII. You can then decode them yourself: > > # try this and see if it is sensible > print("output is", result.stdout.decode('latin1')) > > # otherwise this > print("output is", result.stdout.decode('utf-8', errors='replace')) > > > >> >>> subprocess.getstatusoutput("tail -3 /tmp/pmaster.db",) >> Traceback (most recent call last): > [...] >> File "/usr/lib/python3.5/encodings/ascii.py", line 26, in decode >> return codecs.ascii_decode(input, self.errors)[0] >> UnicodeDecodeError: 'ascii' codec can't decode byte 0xe0 in position 189: >> ordinal not in range(128) > > Let's look at the error message. getstatusoutput apparently expects only > pure ASCII output, because it is choking on a non-ASCII byte, namely > 0xe0. Obviously 0xe0 (or in decimal, 224) is not an ASCII value, since > ASCII goes from 0 to 127 only. > > If there's one non-ASCII byte in the file, there are probably more. > > So what is that mystery 0xe0 byte? It is hard to be sure, because it > depends on the source. If pmaster.db is a binary file, it could mean > anything or nothing. If it is a text file, it depends on the encoding > that the file uses. If it comes from a Mac, it might be: > > py> b'\xe0'.decode('macroman') > '?' > > If it comes from Windows in Western Europe, it might be: > > py> b'\xe0'.decode('latin1') > '?' > > If it comes from Windows in Greece, it might be: > > py> b'\xe0'.decode('iso 8859-7') > '?' > > and so forth. There's no absolutely reliable way to tell. This is the > sort of nightmare that Unicode was invented to fix, but unfortunately > there still exist millions of files, data formats and applications which > insist on using rubbish "extended ASCII" encodings instead. > > > >> That file's content is kryptonite for python apparently. Other shell >> operations work. >> >> >>> subprocess.getstatusoutput("file /tmp/pmaster.db",) >> (0, '/tmp/pmaster.db: Non-ISO extended-ASCII text, with very long lines, >> with LF, NEL line terminators') > > The `file` command agrees with me: it is not ASCII. Thank you Steve! subprocess.run handles it better. >>> subprocess.getstatusoutput("tail -400 /tmp/pmaster.txt",) Traceback (most recent call last): File "", line 1, in File "/usr/lib/python3.5/subprocess.py", line 805, in getstatusoutput data = check_output(cmd, shell=True, universal_newlines=True, stderr=STDOUT) File "/usr/lib/python3.5/subprocess.py", line 626, in check_output **kwargs).stdout File "/usr/lib/python3.5/subprocess.py", line 695, in run stdout, stderr = process.communicate(input, timeout=timeout) File "/usr/lib/python3.5/subprocess.py", line 1059, in communicate stdout = self.stdout.read() File "/usr/lib/python3.5/codecs.py", line 321, in decode (result, consumed) = self._buffer_decode(data, self.errors, final) UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe0 in position 60942: invalid continuation byte as opposed to: >>> result = subprocess.run(["tail", "-400", "/tmp/pmaster.txt"], stdout=subprocess.PIPE) >>> result.returncode 0 >>> subprocess.getstatusoutput("file /tmp/pmaster.txt",) (0, '/tmp/pmaster.txt: Non-ISO extended-ASCII text, with very long lines, with LF, NEL line terminators') >>> That was awesome! :) From kwpolska at gmail.com Fri Sep 22 03:51:22 2017 From: kwpolska at gmail.com (Chris Warrick) Date: Fri, 22 Sep 2017 09:51:22 +0200 Subject: [Tutor] subprocess.getstatusoutput : UnicodeDecodeError In-Reply-To: References: <20170922003957.GQ13110@ando.pearwood.info> Message-ID: On 22 September 2017 at 03:57, Evuraan wrote: >>>> result = subprocess.run(["tail", "-400", "/tmp/pmaster.txt"], stdout=subprocess.PIPE) >>>> result.returncode > 0 >>>> subprocess.getstatusoutput("file /tmp/pmaster.txt",) > (0, '/tmp/pmaster.txt: Non-ISO extended-ASCII text, with very long > lines, with LF, NEL line terminators') >>>> You?re still using the deprecated function. >>> subprocess.run(['file', '/tmp/pmaster.txt'], stdout=subprocess.PIPE) CompletedProcess(args=['file', '/tmp/pmaster.txt'], returncode=0, stdout=b'/tmp/pmaster.txt: Non-ISO?\n') >>> result = _ # underscore means result of previous line in interactive mode >>> result.stdout b'/tmp/pmaster.txt: Non-ISO?line terminators\n' >>> result.returncode 0 And if you want to get a Unicode string (if output of command is your system encoding, hopefully UTF-8): >>> subprocess.run(['file', '/tmp/pmaster.txt'], stdout=subprocess.PIPE, universal_newlines=True) CompletedProcess(args=['file', '/tmp/pmaster.txt'], returncode=0, stdout='/tmp/pmaster.txt: Non-ISO?\n') >>> (_.stdout is an unicode string) Also, going back to your original example: you should not be using `tail` from within Python. You should not depend on tail being available (it?s not on Windows), and there may also be version differences. Instead of tail, you should use Python?s standard file operations (open()) to accomplish your task. [advertisement] Extra reading on security (shell=False) and the necessity of calling subprocesses: https://chriswarrick.com/blog/2017/09/02/spawning-subprocesses-smartly-and-securely/ [/advertisement] -- Chris Warrick PGP: 5EAAEA16 From george at fischhof.hu Fri Sep 22 04:22:50 2017 From: george at fischhof.hu (George Fischhof) Date: Fri, 22 Sep 2017 10:22:50 +0200 Subject: [Tutor] Need Help with install of Python! In-Reply-To: References: <007001d33183$d44b7c70$7ce27550$@net> Message-ID: 2017-09-20 2:18 GMT+02:00 Alan Gauld via Tutor : > On 19/09/17 21:13, Larry Staley wrote: > > Hello I am very new to Python just having installed Python Version 2.7 > onto > > my windows 8.1 laptop. I thought the install was successful and was > > entering information for my first assignment when I received an > unexpected > > error. > > Where did you get your vesion of Python? > If it was the standard distribution from python.org or > activestate.com then it does not include any of the SciPy > packages(*) and you need to install them separately. > > If you expect to be using mamny of these types of package > you are best fetching a distribution that includes them > all, for example, Anaconda or Enthought > > (*)Pandas is part of the SciPy suite of third party add-ons. > > > I executed a Sheets command using an earlier generated getSheetNames > > function that successfully was entered by me. > > When you define a function the code inside is not > executed merely compiled into a function object > ready for execution. > > > However, when I ran the Sheets command I received the following: > > Its only when you call the function that the code inside > gets executed. > > > def getSheetNames(excelfile): > > from pandas import ExcelFile > > Its normal in Python to put all imports at the top of > the file rather than inside any functions. In this > case you try to import pandas everytime you call > the function and while its not a big overhead it > mounts up if you were calling this inside a > repeating loop. > > And if the import was outside the function you would > pick up the import error earlier. > > >>>> excelfile=r:"C:\Users\Larry > >>>> sheets=getSheetNames > > (excelfile);sheets > > I'm not sure what you are doing with that > final ;sheets. I assuyme trying to evaluate the result of the function? > > It would be normal to just print it: > > >>> print getSheetNames(excelFile) > > or, if you need to store the result: > > >>> sheets=getSheetNames(excelfile) > >>> sheets > > Combining commands on a single line doesn't save > much typing and makes debugging harder. > > > I have in addition included the actual Python I executed and received the > > message. It is attached in the file. > > attachments often get rejected by the mailer, > if its not a huge file(>100 lines) just paste > it into the email. > > Always include the full error trace too. > > > If I need to install Pandas, please indicate clearly how I do using my > > current ver 2.7 python install. > > The easiest way is just to grab one of the > all-inclusive Python distros mentioned above. > > -- > 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 > Hi Larry, If You just installed Python 2.7, maybe it would be a good idea to replace it with Python 3.6. It will not solve Your problem, but support of Pyhon 2 will be finished in 2020, and that time You will have to learn Python 3 anyway. So it is better to start with it if You have no any special requirement to use Python 2. (Python 3 is more modern, and is the future) ;-) @tutors Hi Tutors, I think we should encourage people new to Python to use Python 3 instaed of Python 2 as this is the future. ;-) BR, George From callofdutyslayer261 at gmail.com Fri Sep 22 17:41:12 2017 From: callofdutyslayer261 at gmail.com (Mizav) Date: Fri, 22 Sep 2017 17:41:12 -0400 Subject: [Tutor] I have a problem Message-ID: You are using pip version 7.1.2, however version 9.0.1 is available. You should consider upgrading via the 'python -m pip install --upgrade pip' command. From nulla.epistola at web.de Sat Sep 23 05:46:12 2017 From: nulla.epistola at web.de (Sibylle Koczian) Date: Sat, 23 Sep 2017 11:46:12 +0200 Subject: [Tutor] I have a problem In-Reply-To: References: Message-ID: Am 22.09.2017 um 23:41 schrieb Mizav: > You are using pip version 7.1.2, however version 9.0.1 is available. > You should consider upgrading via the 'python -m pip install --upgrade pip' > command. > And what is the problem? Did you understand the message? Did you try to do the upgrade and what happened when you did? We can probably help you, but we'd need to know some things: - the Python version you used when you got this message - the operating system of the computer - the full error message (cut and paste, please, don't retype!). From mats at wichmann.us Sat Sep 23 11:04:32 2017 From: mats at wichmann.us (Mats Wichmann) Date: Sat, 23 Sep 2017 09:04:32 -0600 Subject: [Tutor] I have a problem In-Reply-To: References: Message-ID: On 09/23/2017 03:46 AM, Sibylle Koczian wrote: > Am 22.09.2017 um 23:41 schrieb Mizav: >> You are using pip version 7.1.2, however version 9.0.1 is available. >> You should consider upgrading via the 'python -m pip install --upgrade >> pip' >> command. >> > > And what is the problem? Did you understand the message? Did you try to > do the upgrade and what happened when you did? > > We can probably help you, but we'd need to know some things: > - the Python version you used when you got this message > - the operating system of the computer > - the full error message (cut and paste, please, don't retype!). Were you setting up a virtualenv? (I often see out-of-date messages when doing so) From prateek.iq at gmail.com Sat Sep 23 09:15:46 2017 From: prateek.iq at gmail.com (Prateek) Date: Sat, 23 Sep 2017 18:45:46 +0530 Subject: [Tutor] A Question Regarding the Documentation Format Message-ID: Hi Whenever I use help(input) inside Python shell i get the following output: >>> >>> help(input) Help on built-in function input in module builtins: input(...) input([prompt]) -> string I want to know what the significance of "-> string". I have tried consulting several books but none of them gave me a clear-cut explanation for this. Thanks From kwpolska at gmail.com Sun Sep 24 06:03:43 2017 From: kwpolska at gmail.com (Chris Warrick) Date: Sun, 24 Sep 2017 12:03:43 +0200 Subject: [Tutor] A Question Regarding the Documentation Format In-Reply-To: References: Message-ID: On 23 September 2017 at 15:15, Prateek wrote: > input(...) > input([prompt]) -> string > > I want to know what the significance of "-> string". I have tried > consulting several books but none of them gave me a clear-cut explanation > for this. This indicates the return type: input() returns a string. -- Chris Warrick PGP: 5EAAEA16 From phil_lor at bigpond.com Sun Sep 24 06:24:40 2017 From: phil_lor at bigpond.com (Phil) Date: Sun, 24 Sep 2017 20:24:40 +1000 Subject: [Tutor] Converting a string to a byte array Message-ID: Thank you for reading this. The following code sends "Fred" to my serial connected device without any problems. import serial ser = serial.Serial('/dev/ttyACM0',9600) ser.write(b'Fred\n') I'm trying to do the same under pyqt but I'm having trouble converting a string to a byte array. This works correctly from the pyqt IDE but not from the console. python3 mycode.py generates "typeError: string argument without an encoding" It's very likely that my method of converting a string to a byte array is incorrect. This is my attempt: mytext = "Fred" mybytes = bytes(mytext) byte = byte + '\n' ser.write(mybytes) I don't understand why this works from the pyqt IDE but not when run from the console. I suppose the IDE is adding the correct encoding. I suspect utf8 is involved somewhere. -- Regards, Phil From __peter__ at web.de Sun Sep 24 08:26:07 2017 From: __peter__ at web.de (Peter Otten) Date: Sun, 24 Sep 2017 14:26:07 +0200 Subject: [Tutor] Converting a string to a byte array References: Message-ID: Phil wrote: > Thank you for reading this. > > The following code sends "Fred" to my serial connected device without > any problems. > > import serial > > ser = serial.Serial('/dev/ttyACM0',9600) > ser.write(b'Fred\n') > > > I'm trying to do the same under pyqt but I'm having trouble converting a > string to a byte array. This works correctly from the pyqt IDE but not > from the console. python3 mycode.py generates "typeError: string > argument without an encoding" > > It's very likely that my method of converting a string to a byte array > is incorrect. This is my attempt: > > mytext = "Fred" > mybytes = bytes(mytext) > byte = byte + '\n' > ser.write(mybytes) This is a bit of a mess. Always cut and paste actual code that you want to show. If there is an exception include the traceback. > I don't understand why this works from the pyqt IDE but not when run > from the console. I suppose the IDE is adding the correct encoding. I > suspect utf8 is involved somewhere. When you want to convert a string to a byte sequence it is your turn to decide about the encoding. With "Fred" even ASCII would work. When Python doesn't pick a default you have to specify it yourself, e. g. if you want to use UTF-8: >>> text = "Fred" >>> text.encode("utf-8") b'Fred' In Python 3 you cannot mix str and bytes, so to add a newline your options are >>> (text + "\n").encode("utf-8") # concatenate text b'Fred\n' >>> text.encode("utf-8") + b"\n" # concatenate bytes b'Fred\n' If you want to write only strings it may also be worth trying to wrap the Serial instance into a TextIOWrapper: # untested! import serial import io ser = serial.Serial('/dev/ttyACM0', 9600) serial_device = io.TextIOWrapper(ser) print("Fred", file=serial_device) From __peter__ at web.de Sun Sep 24 10:12:37 2017 From: __peter__ at web.de (Peter Otten) Date: Sun, 24 Sep 2017 16:12:37 +0200 Subject: [Tutor] A Question Regarding the Documentation Format References: Message-ID: Prateek wrote: > Hi > > Whenever I use help(input) inside Python shell i get the following output: > >>>> >>>> help(input) > Help on built-in function input in module builtins: > > input(...) > input([prompt]) -> string > > I want to know what the significance of "-> string". I have tried > consulting several books but none of them gave me a clear-cut explanation > for this. Note that the "->" is also valid Python: >>> def f(x: int) -> str: ... return "<{}>".format(x) ... >>> f(42) '<42>' It was originally "invented" here https://www.python.org/dev/peps/pep-3107/#return-values and later morphed into https://docs.python.org/dev/library/typing.html According to that f() should take an integer and return a str. This information can be used by type checkers like http://mypy-lang.org/ From cs at cskk.id.au Sun Sep 24 17:26:55 2017 From: cs at cskk.id.au (Cameron Simpson) Date: Mon, 25 Sep 2017 07:26:55 +1000 Subject: [Tutor] Converting a string to a byte array In-Reply-To: References: Message-ID: <20170924212655.GA12913@cskk.homeip.net> On 24Sep2017 20:24, Phil wrote: >I'm trying to do the same under pyqt but I'm having trouble converting a >string to a byte array. This works correctly from the pyqt IDE but not from >the console. python3 mycode.py generates "typeError: string argument without >an encoding" Is it possible your pyqt IDE is using Python 2 instead of Python 3. In Python 2 strings are effectively byte arrays. >It's very likely that my method of converting a string to a byte array >is incorrect. This is my attempt: > >mytext = "Fred" >mybytes = bytes(mytext) >byte = byte + '\n' >ser.write(mybytes) This can't be your code. I suspect the third line should be talking about "mybytes". >I don't understand why this works from the pyqt IDE but not when run from the >console. I suppose the IDE is adding the correct encoding. I'm guessing the IDE is python 2 and not doing any encoding at all. In python 2 str _is_ effectively bytes and if you stay in ASCII you just get away with it. >I suspect utf8 is involved somewhere. In Python 3, strs contain Unicode code points, and when converting to bytes you need to specify how those code points are to be encoded, because a byte is 8 bits wide, not enough to store most code points. So you need to "encode" the str into bytes. So you need to know what your serial device expects. ASCII only? Some 8 bit coding like ISO8859-1 (covers a lotof Western Europe) or something else. For most modern environments the encoding will be UTF-8, but in serial environments that may not be the case depending on what is reading your data. Anyway, if you're only using ASCII your ok, because the ISO8859 codings and UTF-8 use the _same_ encoding for pure ASCII as ASCII does (1 to 1, each byte holding a value from 0-127 becing the ASCII code). So you could try UTF-8 if you don't know what your serial device expects. So: mytext = "Fred" mytext = mytext + "\n" mybytes = mytext.encode('utf-8') ser.write(mybytes) Notice that I've appended the newline _before_ converting to bytes, while we're still talking about "text" (the stuff that goes in a str). This will "just work". The risk is if your device can't cope with UTF-8. If it turns out that your device expects something more constrained (eg ASCII) you can put 'ascii' where I have 'utf-8'. That has the advantage that your code will raise an exception if you feed in something outside ASCII, which your device wouldn't have coped with anyway. Look up the documentation for what your device expects. If the documentation is silent on this, maybe use 'ascii' instead just on principle - better to notice in your program and decide what to do there than to feed garbage to your device and have is misbehave. Cheers, Cameron Simpson (formerly cs at zip.com.au) From phil_lor at bigpond.com Sun Sep 24 19:29:05 2017 From: phil_lor at bigpond.com (Phil) Date: Mon, 25 Sep 2017 09:29:05 +1000 Subject: [Tutor] Converting a string to a byte array In-Reply-To: <20170924212655.GA12913@cskk.homeip.net> References: <20170924212655.GA12913@cskk.homeip.net> Message-ID: On 25/09/17 07:26, Cameron Simpson wrote: Thank you Cameron and Peter for your replies. >> I don't understand why this works from the pyqt IDE but not when run >> from the console. I suppose the IDE is adding the correct encoding. > > I'm guessing the IDE is python 2 and not doing any encoding at all. In > python 2 str _is_ effectively bytes and if you stay in ASCII you just > get away with it. > No, the IDE is Eric and as far as I know it's python3 only. Just for interest I amended my code to use what you provided and tried it under IDLE. There aren't any errors but but my Arduino is not responding. However, if I enter python3 mycode.py then it works perfectly. I'm sure there's an explanation for this. I have thoney, another python IDE, on a raspberrypi I'll try that later and see what the result is. Anyway, it works from Eric and from the command prompt. > So you need to know what your serial device expects. ASCII only? As it turns out, it doesn't matter if the data is ASCII or UTF-8. > ?mytext = "Fred" > ?mytext = mytext + "\n" > ?mybytes = mytext.encode('utf-8') > ?ser.write(mybytes) > > Notice that I've appended the newline _before_ converting to bytes, Thank you for the code and the explanation, it's greatly appreciated. It's all a bit of an anticlimax really. Now that it works I don't know what to do with it. Like so many of my projects. -- Regards, Phil From cs at cskk.id.au Sun Sep 24 20:34:18 2017 From: cs at cskk.id.au (Cameron Simpson) Date: Mon, 25 Sep 2017 10:34:18 +1000 Subject: [Tutor] Converting a string to a byte array In-Reply-To: References: Message-ID: <20170925003418.GA90354@cskk.homeip.net> On 25Sep2017 09:29, Phil wrote: >On 25/09/17 07:26, Cameron Simpson wrote: >>>I don't understand why this works from the pyqt IDE but not when run from >>>the console. I suppose the IDE is adding the correct encoding. >> >>I'm guessing the IDE is python 2 and not doing any encoding at all. >>In python 2 str _is_ effectively bytes and if you stay in ASCII you >>just get away with it. > >No, the IDE is Eric and as far as I know it's python3 only. Weird. What if you put your code in try/except in the IDE: try: your code here except BaseException as e: print("exception: %s" % (e,)) raise just in case some esoteric exception is being swallowed silently by the IDE. Not that the error you cite should act that way... >Just for interest I amended my code to use what you provided and tried it >under IDLE. There aren't any errors but but my Arduino is not responding. Weird indeed. Try the try/except above and see if it reveals anything at all. >However, if I enter python3 mycode.py then it works perfectly. I'm sure >there's an explanation for this. I have thoney, another python IDE, on a >raspberrypi I'll try that later and see what the result is. Anyway, it works >from Eric and from the command prompt. > >>So you need to know what your serial device expects. ASCII only? > >As it turns out, it doesn't matter if the data is ASCII or UTF-8. If you're only using ASCII valid characters then the byte sequences are the same, so you don't really know yet. >> ?mytext = "Fred" >> ?mytext = mytext + "\n" >> ?mybytes = mytext.encode('utf-8') >> ?ser.write(mybytes) >> >>Notice that I've appended the newline _before_ converting to bytes, > >Thank you for the code and the explanation, it's greatly appreciated. > >It's all a bit of an anticlimax really. Now that it works I don't know what to >do with it. Like so many of my projects. Control something? As a remote to control a TV or PVR, using an IR adapter? The weather? Courier delivery times? So many possibilities... Cheers, Cameron Simpson (formerly cs at zip.com.au) From sjeik_appie at hotmail.com Mon Sep 25 09:20:53 2017 From: sjeik_appie at hotmail.com (Albert-Jan Roskam) Date: Mon, 25 Sep 2017 13:20:53 +0000 Subject: [Tutor] logging to cmd.exe Message-ID: Hi, With Python 3.5 under Windows I am using the logging module to log messages to stdout (and to a file), but this occasionally causes logging errors because some characters cannot be represented in the codepage used by cmd.exe (cp850, aka OEM codepage, I think). What is the best way to prevent this from happening? The program runs fine, but the error is distracting. I know I can use s.encode(sys.stdout.encoding, 'replace') and log that, but this is ugly and tedious to do when there are many log messages. I also don't understand why %r (instead of %s) still causes an error. I thought that the character representation uses only ascii characters?! import logging import sys assert sys.version_info.major > 2 logging.basicConfig(filename="d:/log.txt", level=logging.DEBUG,format='%(asctime)s %(message)s') handler = logging.StreamHandler(stream=sys.stdout) logger = logging.getLogger(__name__) logger.addHandler(handler) s = '\u20ac' logger.info("euro sign: %r", s) --- Logging error --- Traceback (most recent call last): File "c:\python3.5\lib\logging\__init__.py", line 982, in emit stream.write(msg) File "c:\python3.5\lib\encodings\cp850.py", line 19, in encode return codecs.charmap_encode(input,self.errors,encoding_map)[0] UnicodeEncodeError: 'charmap' codec can't encode character '\u20ac' in position 12: character maps to Call stack: File "q:\temp\logcheck.py", line 10, in logger.info("euro sign: %r", s) Message: 'euro sign: %r' Arguments: ('\u20ac',) Thanks in advance for your replies! Albert-Jan From mysecretrobotfactory at gmail.com Sun Sep 24 18:53:53 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Sun, 24 Sep 2017 15:53:53 -0700 Subject: [Tutor] windows specific: read process memory Message-ID: Hi all, I have a few questions about memory scanning! The following code attempts to print out all addresses whose value is int(-143)! 1. I am using a for loop to go through all the addresses! Is this the right thing to do? 2. I feed the read process memory function with hex(i), correct? 3. I am a little bummed by what to put down for my buffer and buffer size, is what I did proper? 4. This is not in the code, but if I actually know the value I want in the memory is a Double, how do i modify my code to look at only doubles? thanks all! > code starts User32 = ctypes.WinDLL('User32', use_last_error=True) Kernel32 = ctypes.WinDLL('kernel32', use_last_error=True) PROCESS_VM_READ = 0x0010 PID = 'given' Process = Kernel32.OpenProcess(PROCESS_VM_READ, 0, PID) ReadProcessMemory = Kernel32.ReadProcessMemory buffer_size = 1000 buffer = ctypes.create_string_buffer(buffer_size) # looking for the addresses where the values are -143, for example # I used 10000000 as an abitrary number, I need to scan the entire application # memory space, but I am not sure how to acquire that value. for i in range(1,10000000): if ReadProcessMemory(Process, hex(i), buffer, buffer_size, None): if float(buffer) == int(-143) : print(float(buffer)) print('Done.') From edmundopierre at yahoo.com Sun Sep 24 23:34:05 2017 From: edmundopierre at yahoo.com (edmundo pierre) Date: Mon, 25 Sep 2017 03:34:05 +0000 (UTC) Subject: [Tutor] How to sort float number from big numbers to small numbers? References: <1276087951.1345891.1506310445624.ref@mail.yahoo.com> Message-ID: <1276087951.1345891.1506310445624@mail.yahoo.com> I am trying to sort float numbers input by an user from the bigger to smaller number. I do not know how to compare float numbers. Any ideas? Thank you! Edwin From alan.gauld at yahoo.co.uk Mon Sep 25 09:43:20 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 25 Sep 2017 14:43:20 +0100 Subject: [Tutor] How to sort float number from big numbers to small numbers? In-Reply-To: <1276087951.1345891.1506310445624@mail.yahoo.com> References: <1276087951.1345891.1506310445624.ref@mail.yahoo.com> <1276087951.1345891.1506310445624@mail.yahoo.com> Message-ID: On 25/09/17 04:34, edmundo pierre via Tutor wrote: > I am trying to sort float numbers input by an user from the bigger to smaller number. I do not know how to compare float numbers. Any ideas? Thank you! The same way you sort anything else. Using the comparison operations ==, <, >, <=, >= There is a slight snag in that comparing float to absolute values is not straightforward but for sorting thats irrelevant. Of course, normally you just use the sort() method to do it, or call the sorted() function. Alan G From david at graniteweb.com Mon Sep 25 09:36:55 2017 From: david at graniteweb.com (David Rock) Date: Mon, 25 Sep 2017 08:36:55 -0500 Subject: [Tutor] How to sort float number from big numbers to small numbers? In-Reply-To: <1276087951.1345891.1506310445624@mail.yahoo.com> References: <1276087951.1345891.1506310445624.ref@mail.yahoo.com> <1276087951.1345891.1506310445624@mail.yahoo.com> Message-ID: <02DCD932-5898-4349-B6C5-EF7B15FEA630@graniteweb.com> > On Sep 24, 2017, at 22:34, edmundo pierre via Tutor wrote: > > I am trying to sort float numbers input by an user from the bigger to smaller number. I do not know how to compare float numbers. Any ideas? Thank you! > Edwin Some basic ideas to think about: 1. inputs are usually strings, so make sure you are storing them as floats 2. put them in a list. You can then use list built-ins to sort What have you tried so far? Are you trying to start at all, or are you trying things that aren?t working? ? David Rock david at graniteweb.com From __peter__ at web.de Mon Sep 25 10:59:58 2017 From: __peter__ at web.de (Peter Otten) Date: Mon, 25 Sep 2017 16:59:58 +0200 Subject: [Tutor] logging to cmd.exe References: Message-ID: Albert-Jan Roskam wrote: > Hi, > > > With Python 3.5 under Windows I am using the logging module to log > messages to stdout (and to a file), but this occasionally causes logging > errors because some characters cannot be represented in the codepage used > by cmd.exe (cp850, aka OEM codepage, I think). What is the best way to > prevent this from happening? The program runs fine, but the error is > distracting. I know I can use s.encode(sys.stdout.encoding, 'replace') and > log that, but this is ugly and tedious to do when there are many log > messages. I also don't understand why %r (instead of %s) still causes an > error. I thought that the character representation uses only ascii > characters?! Not in Python 3. You can enforce ascii with "%a": >>> euro = '\u20ac' >>> print("%r" % euro) '?' >>> print("%a" % euro) '\u20ac' Or you can set an error handler with PYTHONIOENCODING (I have to use something that is not utf-8-encodable for the demo): $ python3 -c 'print("\udc85")' Traceback (most recent call last): File "", line 1, in UnicodeEncodeError: 'utf-8' codec can't encode character '\udc85' in position 0: surrogates not allowed $ PYTHONIOENCODING=:backslashreplace python3 -c 'print("\udc85")' \udc85 Or you follow the convention and log to stderr: $ python3 -c 'import sys; print("\udc85", file=sys.stderr)' \udc85 $ $ python3 -c 'import logging; logging.basicConfig(); logging.getLogger().warn("\udc85")' > to_prove_it_s_not_stdout WARNING:root:\udc85 > import logging > import sys > > assert sys.version_info.major > 2 > logging.basicConfig(filename="d:/log.txt", > level=logging.DEBUG,format='%(asctime)s %(message)s') handler = > logging.StreamHandler(stream=sys.stdout) logger = > logging.getLogger(__name__) logger.addHandler(handler) > > s = '\u20ac' > logger.info("euro sign: %r", s) > > > > --- Logging error --- > Traceback (most recent call last): > File "c:\python3.5\lib\logging\__init__.py", line 982, in emit > stream.write(msg) > File "c:\python3.5\lib\encodings\cp850.py", line 19, in encode > return codecs.charmap_encode(input,self.errors,encoding_map)[0] > UnicodeEncodeError: 'charmap' codec can't encode character '\u20ac' in > position 12: character maps to Call stack: > File "q:\temp\logcheck.py", line 10, in > logger.info("euro sign: %r", s) > Message: 'euro sign: %r' > Arguments: ('\u20ac',) > > > Thanks in advance for your replies! > > > Albert-Jan > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From steve at pearwood.info Mon Sep 25 12:52:58 2017 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 26 Sep 2017 02:52:58 +1000 Subject: [Tutor] How to sort float number from big numbers to small numbers? In-Reply-To: <1276087951.1345891.1506310445624@mail.yahoo.com> References: <1276087951.1345891.1506310445624.ref@mail.yahoo.com> <1276087951.1345891.1506310445624@mail.yahoo.com> Message-ID: <20170925165258.GT13110@ando.pearwood.info> On Mon, Sep 25, 2017 at 03:34:05AM +0000, edmundo pierre via Tutor wrote: > I am trying to sort float numbers input by an user from the bigger to > smaller number. I do not know how to compare float numbers. Any ideas? > Thank you! If the numbers are provided by the user, they will be in the form of strings, so you have to convert to float first: # pretend these came from the user a = '1.2' b = '100.9' c = '2.4' alist = [a, b, c] alist.sort() print(alist) will print the result: ['1.2', '100.9', '2.4'] which is not what you want. Instead, do this: # pretend these came from the user a = '1.2' b = '100.9' c = '2.4' alist = [float(a), float(b), float(c)] alist.sort() print(alist) which will print [1.2, 2.4, 100.9] instead. Also, you can make a copy of the list using sorted() instead of sort(): alist = [float(a), float(b), float(c)] print(sorted(alist)) Be warned: some numbers which can be written exactly in decimal, like 0.1, may sometimes appear ever-so-slightly "off" when you convert to a float. This is not a bug, but an unfortunate side-effect of the way computers do arithmetic in base 2 (binary). Feel free to ask if you would like more information. -- Steve From breamoreboy at yahoo.co.uk Mon Sep 25 12:19:04 2017 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Mon, 25 Sep 2017 17:19:04 +0100 Subject: [Tutor] logging to cmd.exe In-Reply-To: References: Message-ID: On 25/09/2017 14:20, Albert-Jan Roskam wrote: > Hi, > > > With Python 3.5 under Windows I am using the logging module to log messages to stdout (and to a file), but this occasionally causes logging errors because some characters cannot be represented in the codepage used by cmd.exe (cp850, aka OEM codepage, I think). What is the best way to prevent this from happening? The program runs fine, but the error is distracting. I know I can use s.encode(sys.stdout.encoding, 'replace') and log that, but this is ugly and tedious to do when there are many log messages. I also don't understand why %r (instead of %s) still causes an error. I thought that the character representation uses only ascii characters?! > > > import logging > import sys > > assert sys.version_info.major > 2 > logging.basicConfig(filename="d:/log.txt", level=logging.DEBUG,format='%(asctime)s %(message)s') > handler = logging.StreamHandler(stream=sys.stdout) > logger = logging.getLogger(__name__) > logger.addHandler(handler) > > s = '\u20ac' > logger.info("euro sign: %r", s) > > > > --- Logging error --- > Traceback (most recent call last): > File "c:\python3.5\lib\logging\__init__.py", line 982, in emit > stream.write(msg) > File "c:\python3.5\lib\encodings\cp850.py", line 19, in encode > return codecs.charmap_encode(input,self.errors,encoding_map)[0] > UnicodeEncodeError: 'charmap' codec can't encode character '\u20ac' in position 12: character maps to > Call stack: > File "q:\temp\logcheck.py", line 10, in > logger.info("euro sign: %r", s) > Message: 'euro sign: %r' > Arguments: ('\u20ac',) > > > Thanks in advance for your replies! > > > Albert-Jan > Rather than change your code can you change the codepage with the chcp command? C:\Users\Mark\Documents\MyPython>chcp Active code page: 65001 C:\Users\Mark\Documents\MyPython>type mytest.py import logging import sys assert sys.version_info.major > 2 logging.basicConfig(filename="d:/log.txt", level=logging.DEBUG,format='%(asctime)s %(message)s') handler = logging.StreamHandler(stream=sys.stdout) logger = logging.getLogger(__name__) logger.addHandler(handler) s = '\u20ac' logger.info("euro sign: %r", s) C:\Users\Mark\Documents\MyPython>mytest.py euro sign: '?' -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email has been checked for viruses by AVG. http://www.avg.com From cs at cskk.id.au Mon Sep 25 17:02:14 2017 From: cs at cskk.id.au (Cameron Simpson) Date: Tue, 26 Sep 2017 07:02:14 +1000 Subject: [Tutor] Converting a string to a byte array In-Reply-To: <20170925003418.GA90354@cskk.homeip.net> References: <20170925003418.GA90354@cskk.homeip.net> Message-ID: <20170925210214.GA85911@cskk.homeip.net> On 25Sep2017 09:29, Phil wrote: [...] >Just for interest I amended my code to use what you provided and tried it >under IDLE. There aren't any errors but but my Arduino is not responding. >However, if I enter python3 mycode.py then it works perfectly. I'm sure >there's an explanation for this. Just to this. If your serial file handle is a normal buffered one, you may also need to call .flush(). When you run from the command line as "python3 mycode.py" is done automatically at programme exit. In the IDE, the programme has not exited, so your bytes may be lurking in the buffer, unsent. Cheers, Cameron Simpson (formerly cs at zip.com.au) From phil_lor at bigpond.com Mon Sep 25 19:21:31 2017 From: phil_lor at bigpond.com (Phil) Date: Tue, 26 Sep 2017 09:21:31 +1000 Subject: [Tutor] Converting a string to a byte array In-Reply-To: <20170925210214.GA85911@cskk.homeip.net> References: <20170925003418.GA90354@cskk.homeip.net> <20170925210214.GA85911@cskk.homeip.net> Message-ID: On 26/09/17 07:02, Cameron Simpson wrote: > > Just to this. If your serial file handle is a normal buffered one, you > may also need to call .flush(). When you run from the command line as > "python3 mycode.py" is done automatically at programme exit. In the IDE, > the programme has not exited, so your bytes may be lurking in the > buffer, unsent. Thank you Cameron, that sounds like a logical explanation. I'll try it. -- Regards, Phil From sjeik_appie at hotmail.com Tue Sep 26 07:22:37 2017 From: sjeik_appie at hotmail.com (Albert-Jan Roskam) Date: Tue, 26 Sep 2017 11:22:37 +0000 Subject: [Tutor] logging to cmd.exe In-Reply-To: References: , Message-ID: From: Tutor on behalf of Mark Lawrence via Tutor Sent: Monday, September 25, 2017 4:19 PM To: tutor at python.org Subject: Re: [Tutor] logging to cmd.exe ? On 25/09/2017 14:20, Albert-Jan Roskam wrote: > Hi, > > > With Python 3.5 under Windows I am using the logging module to log messages to stdout (and to a file), but this occasionally causes logging errors because some characters cannot be represented in the codepage used by cmd.exe (cp850, aka OEM codepage, I think). What is the best way to prevent this from happening? The program runs fine, but the error is distracting. I know I can use s.encode(sys.stdout.encoding, 'replace') and log that, but this is ugly and tedious to do when there are many log messages. I also don't understand why %r (instead of %s) still causes an error. I thought that the character representation uses only ascii characters?! > > > import logging > import sys > > assert sys.version_info.major > 2 > logging.basicConfig(filename="d:/log.txt", level=logging.DEBUG,format='%(asctime)s %(message)s') > handler = logging.StreamHandler(stream=sys.stdout) > logger = logging.getLogger(__name__) > logger.addHandler(handler) > > s = '\u20ac' > logger.info("euro sign: %r", s) > > > > --- Logging error --- > Traceback (most recent call last): >??? File "c:\python3.5\lib\logging\__init__.py", line 982, in emit >????? stream.write(msg) >??? File "c:\python3.5\lib\encodings\cp850.py", line 19, in encode >????? return codecs.charmap_encode(input,self.errors,encoding_map)[0] > UnicodeEncodeError: 'charmap' codec can't encode character '\u20ac' in position 12: character maps to > Call stack: >??? File "q:\temp\logcheck.py", line 10, in >????? logger.info("euro sign: %r", s) > Message: 'euro sign: %r' > Arguments: ('\u20ac',) > > > Thanks in advance for your replies! > > > Albert-Jan > Rather than change your code can you change the codepage with the chcp command? ========> Good to keep in mind, but my objection would be similar to that with specifying the PYTHONIOENCODING variable: one needs to change the environment first before the script runs without errors. C:\Users\Mark\Documents\MyPython>chcp Active code page: 65001 ========> Wow! cmd.exe can use cp65001 aka utf8??? I always thought 65001 was not a 'real' codepage, even though some locales (e.g. Georgia) use it [1]. Why does cmd.exe still use cp850? [1] https://docs.moodle.org/dev/Table_of_locales PS: sorry about the missing quote (>>) markers. Hotmail can't do this. Is Gmail better? From mats at wichmann.us Tue Sep 26 08:35:05 2017 From: mats at wichmann.us (Mats Wichmann) Date: Tue, 26 Sep 2017 06:35:05 -0600 Subject: [Tutor] logging to cmd.exe In-Reply-To: References: Message-ID: On 09/26/2017 05:22 AM, Albert-Jan Roskam wrote: > Rather than change your code can you change the codepage with the chcp > command? the way chcp takes effect is problematic for this: "Programs that you start after you assign a new code page use the new code page, however, programs (except Cmd.exe) that you started before assigning the new code page use the original code page. " so making the change from inside the code does not seem like it will work. > ========> Good to keep in mind, but my objection would be similar to that with specifying the PYTHONIOENCODING variable: one needs to change the environment first before the script runs without errors. quicktip: try Python 3.6. It's had a change in this area and no longer uses the code page. I think there's also a module you can use for pre-3.6, sorry too lazy to do a search. From eryksun at gmail.com Tue Sep 26 10:38:33 2017 From: eryksun at gmail.com (eryk sun) Date: Tue, 26 Sep 2017 09:38:33 -0500 Subject: [Tutor] logging to cmd.exe In-Reply-To: References: Message-ID: > cmd.exe can use cp65001 aka utf8??? CMD is a Unicode application that for the most part uses WinAPI wide-character functions, including the console API functions (as does Python 3.6+). There are a few exceptions. CMD uses the console codepage when decoding batch files (line by line, so you can change the codepage in the middle of a batch script), when writing output from its internal commands (e.g. dir) to pipes and files (the /u option overrides this), and when reading output from programs in a `FOR /F` loop. > Why does cmd.exe still use cp850? In the above cases CMD uses the active console input or output codepage, which defaults to the system locale's OEM codepage. If it's not attached to a console (i.e. when run as a DETACHED_PROCESS), CMD uses the ANSI codepage in these cases. Anyway, you appear to be talking about the Windows console, which people often confuse with CMD. Programs that use command-line interfaces (CLIs) and text user interfaces (TUIs), such as classic system shells, are clients of a given console or terminal interface. A TUI application typically is tightly integrated with the console or terminal interface (e.g. a curses application), while a CLI application typically just uses standard I/O (stdin, stdout, stderr). Both cmd.exe and python.exe are Windows console clients. There's nothing special about cmd.exe in this regard. Now, there are a couple of significant problems with using codepage 65001 in the Windows console. Prior to Windows 8, WriteFile and WriteConsoleA return the number of decoded wide characters written to the console, which is a bug because they're supposed to return the number of bytes written. It's not a problem so long as there's a one-to-mapping between bytes and characters in the console's output codepage. But UTF-8 can have up to 4 bytes per character. This misleads buffered writers such as C FILE streams and Python 3's io module, which in turn causes gibberish to be printed after every write of a string that includes non-ASCII characters. Prior to Windows 10, with codepage 65001, reading input from the console via ReadConsole or ReadConsoleA fails if the input has non-ASCII characters. It gets reported as a successful read of zero bytes. This causes Python to think it's at EOF, so the REPL quits (as if Ctrl+Z had been entered) and input() raises EOFError. Even in Windows 10, while the entire read doesn't fail, it's not much better. It replaces non-ASCII characters with NUL bytes. For example, in Windows 10.0.15063: >>> os.read(0, 100) abc???def b'abc\x00\x00\x00def\r\n' Microsoft is gradually working on fixing UTF-8 support in the console (well, two developers are working on it). They appear to have fixed it at least for the private console APIs used by the new Linux subsystem in Windows 10: Python 3.5.2 (default, Nov 17 2016, 17:05:23) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import os >>> s = os.read(0, 100) abc???def >>> s b'abc\xce\xb1\xce\xb2\xce\xb3def\n' >>> s.decode() 'abc???def\n' Maybe it's fixed in the Windows API in an upcoming update. But still, there are a lot of Windows 7 and 8 systems out there, for which codepage 65001 in the console will remain broken. > I always thought 65001 was not a 'real' codepage, even though some locales (e.g. Georgia) use it [1]. Codepage 65001 isn't used by any system locale as the legacy ANSI or OEM codepage. The console allows it probably because no one thought to prevent using it in the late 1990s. It has been buggy for two decades. Moodle seems to have special support for using UTF-8 with Georgian. But as far as Windows is concerned, there is no legacy codepage for Georgian. For example: import ctypes kernel32 = ctypes.WinDLL('kernel32', use_last_error=True) LD_ACP = LOCALE_IDEFAULTANSICODEPAGE = 0x00001004 acp = (ctypes.c_wchar * 6)() >>> kernel32.GetLocaleInfoEx('ka-GE', LD_ACP, acp, 6) 2 >>> acp.value '0' A value of zero here means no ANSI codepage is defined [1]: If no ANSI code page is available, only Unicode can be used for the locale. In this case, the value is CP_ACP (0). Such a locale cannot be set as the system locale. Applications that do not support Unicode do not work correctly with locales marked as "Unicode only". Georgian (ka-GE) is a Unicode-only locale [2] that cannot be set as the system locale. [1]: https://msdn.microsoft.com/en-us/library/dd373761 [2]: https://msdn.microsoft.com/en-us/library/ms930130.aspx From eryksun at gmail.com Tue Sep 26 10:48:44 2017 From: eryksun at gmail.com (eryk sun) Date: Tue, 26 Sep 2017 09:48:44 -0500 Subject: [Tutor] logging to cmd.exe In-Reply-To: References: Message-ID: On Tue, Sep 26, 2017 at 7:35 AM, Mats Wichmann wrote: > On 09/26/2017 05:22 AM, Albert-Jan Roskam wrote: > >> Rather than change your code can you change the codepage with the chcp >> command? > > the way chcp takes effect is problematic for this: > > "Programs that you start after you assign a new code page use the new > code page, however, programs (except Cmd.exe) that you started before > assigning the new code page use the original code page. " Some console applications only check the codepage at startup. If you change it while the program is running, they'll encode/decode text for the original codepage, but the console will decode/encode it for its current codepage. That's called mojibake. Prior to 3.6, at startup Python uses the input codepage for sys.stdin, and the output codepage for sys.stdout and sys.stderr. You can of course rebind sys.std* if you change the codepage via chcp.com or SetConsoleCP() and SetConsoleOutputCP(). If you do change the codepage, it's considerate to remember the previous value and restore it in an atexit function. > I think there's also a module you can use for pre-3.6, sorry too lazy to > do a search. It's win_unicode_console [1]. [1]: https://pypi.python.org/pypi/win_unicode_console From sjeik_appie at hotmail.com Tue Sep 26 07:26:40 2017 From: sjeik_appie at hotmail.com (Albert-Jan Roskam) Date: Tue, 26 Sep 2017 11:26:40 +0000 Subject: [Tutor] logging to cmd.exe In-Reply-To: References: , Message-ID: From: Tutor on behalf of Peter Otten <__peter__ at web.de> Sent: Monday, September 25, 2017 2:59 PM To: tutor at python.org Subject: Re: [Tutor] logging to cmd.exe Albert-Jan Roskam wrote: > Hi, > > > With Python 3.5 under Windows I am using the logging module to log > messages to stdout (and to a file), but this occasionally causes logging > errors because some characters cannot be represented in the codepage used > by cmd.exe (cp850, aka OEM codepage, I think). What is the best way to > prevent this from happening? The program runs fine, but the error is > distracting. I know I can use s.encode(sys.stdout.encoding, 'replace') and > log that, but this is ugly and tedious to do when there are many log > messages. I also don't understand why %r (instead of %s) still causes an > error. I thought that the character representation uses only ascii > characters?! Not in Python 3. You can enforce ascii with "%a": >>> euro = '\u20ac' >>> print("%r" % euro) '?' >>> print("%a" % euro) '\u20ac' ========> aaahh, I did not know about %a. Thank you! Or you can set an error handler with PYTHONIOENCODING (I have to use something that is not utf-8-encodable for the demo): $ python3 -c 'print("\udc85")' Traceback (most recent call last): File "", line 1, in UnicodeEncodeError: 'utf-8' codec can't encode character '\udc85' in position 0: surrogates not allowed $ PYTHONIOENCODING=:backslashreplace python3 -c 'print("\udc85")' \udc85 ========> Nice to know about this variable, though I prefer not to change the environment because other will need to do the same. For others who would like to read more: https://docs.python.org/3/using/cmdline.html Or you follow the convention and log to stderr: $ python3 -c 'import sys; print("\udc85", file=sys.stderr)' \udc85 $ $ python3 -c 'import logging; logging.basicConfig(); logging.getLogger().warn("\udc85")' > to_prove_it_s_not_stdout WARNING:root:\udc85 ========> That's perhaps the best choice. But will messages with logging level warning and lower also be logged to stderr? From __peter__ at web.de Tue Sep 26 11:56:22 2017 From: __peter__ at web.de (Peter Otten) Date: Tue, 26 Sep 2017 17:56:22 +0200 Subject: [Tutor] logging to cmd.exe References: Message-ID: Albert-Jan Roskam wrote: [me] > Or you follow the convention and log to stderr: > > $ python3 -c 'import sys; print("\udc85", file=sys.stderr)' > \udc85 > $ $ python3 -c 'import logging; logging.basicConfig(); > logging.getLogger().warn("\udc85")' > to_prove_it_s_not_stdout > WARNING:root:\udc85 [Albert-Jan] > That's perhaps the best choice. But will messages with logging > level warning and lower also be logged to stderr? That are two distinct aspects. You may specify both what is logged and where it is logged. The easiest way to set up the filter is again basicConfig(): $ python3 -c 'from logging import *; basicConfig(); warn("important"); info("nice to know")' WARNING:root:important While the default level is WARNING you may specify something else: $ python3 -c 'from logging import *; basicConfig(level=INFO); warn("important"); info("nice to know")' WARNING:root:important INFO:root:nice to know From edmundopierre at yahoo.com Wed Sep 27 09:10:45 2017 From: edmundopierre at yahoo.com (edmundo pierre) Date: Wed, 27 Sep 2017 13:10:45 +0000 (UTC) Subject: [Tutor] Help with putting numbers from highest to lowest. References: <456155522.495962.1506517845293.ref@mail.yahoo.com> Message-ID: <456155522.495962.1506517845293@mail.yahoo.com> Hello, When I used sort() to do that, but my problem is that sort() just arrange numbers from small to big, not from big to small. That is the issue I am having now. For instance: # The user is entering those numbers:a = 2.7b = 4.7c= 5.8d = 7.9# I will like the answer to be like this:?7.9? 5.8? 4.7 2.7 #but if I use sort(), I will have that answer, but I do not want that:2.7 4.7 5.8 7.9 Thank you! From roel at roelschroeven.net Wed Sep 27 16:33:32 2017 From: roel at roelschroeven.net (Roel Schroeven) Date: Wed, 27 Sep 2017 22:33:32 +0200 Subject: [Tutor] Help with putting numbers from highest to lowest. In-Reply-To: <456155522.495962.1506517845293@mail.yahoo.com> References: <456155522.495962.1506517845293.ref@mail.yahoo.com> <456155522.495962.1506517845293@mail.yahoo.com> Message-ID: edmundo pierre via Tutor schreef op 27/09/2017 15:10: > Hello, > When I used sort() to do that, but my problem is that sort() just arrange numbers from small to big, not from big to small. That is the issue I am having now. For instance: > # The user is entering those numbers:a = 2.7b = 4.7c= 5.8d = 7.9# I will like the answer to be like this: 7.9 5.8 4.7 2.7 > #but if I use sort(), I will have that answer, but I do not want that:2.7 4.7 5.8 7.9 You can reverse the sort order with reverse=True, like so: numbers = [7.9, 4.7, 2.7, 5.8] numbers.sort(reverse=True) print(numbers) --> [7.9, 5.8, 4.7, 2.7] See https://docs.python.org/3/library/stdtypes.html?highlight=sort#list.sort -- The saddest aspect of life right now is that science gathers knowledge faster than society gathers wisdom. -- Isaac Asimov Roel Schroeven From jderry at austin.utexas.edu Wed Sep 27 15:36:21 2017 From: jderry at austin.utexas.edu (Derry, James R) Date: Wed, 27 Sep 2017 19:36:21 +0000 Subject: [Tutor] Help with putting numbers from highest to lowest. In-Reply-To: <456155522.495962.1506517845293@mail.yahoo.com> References: <456155522.495962.1506517845293.ref@mail.yahoo.com>, <456155522.495962.1506517845293@mail.yahoo.com> Message-ID: In [2]: ?sorted Signature: sorted(iterable, /, *, key=None, reverse=False) Docstring: Return a new list containing all items from the iterable in ascending order. A custom key function can be supplied to customize the sort order, and the reverse flag can be set to request the result in descending order. Type: builtin_function_or_method In [3]: sorted([3,1,5], reverse=True) Out[3]: [5, 3, 1] ________________________________________ From: Tutor [tutor-bounces+jderry=mail.utexas.edu at python.org] on behalf of edmundo pierre via Tutor [tutor at python.org] Sent: Wednesday, September 27, 2017 8:10 AM To: Tutor Python Subject: [Tutor] Help with putting numbers from highest to lowest. Hello, When I used sort() to do that, but my problem is that sort() just arrange numbers from small to big, not from big to small. That is the issue I am having now. For instance: # The user is entering those numbers:a = 2.7b = 4.7c= 5.8d = 7.9# I will like the answer to be like this: 7.9 5.8 4.7 2.7 #but if I use sort(), I will have that answer, but I do not want that:2.7 4.7 5.8 7.9 Thank you! _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From breamoreboy at yahoo.co.uk Wed Sep 27 16:04:50 2017 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 27 Sep 2017 21:04:50 +0100 Subject: [Tutor] Help with putting numbers from highest to lowest. In-Reply-To: <456155522.495962.1506517845293@mail.yahoo.com> References: <456155522.495962.1506517845293.ref@mail.yahoo.com> <456155522.495962.1506517845293@mail.yahoo.com> Message-ID: On 27/09/2017 14:10, edmundo pierre via Tutor wrote: > Hello, > When I used sort() to do that, but my problem is that sort() just arrange numbers from small to big, not from big to small. That is the issue I am having now. For instance: > # The user is entering those numbers:a = 2.7b = 4.7c= 5.8d = 7.9# I will like the answer to be like this:?7.9? 5.8? 4.7 2.7 > #but if I use sort(), I will have that answer, but I do not want that:2.7 4.7 5.8 7.9 > Thank you! You need reverse=True on the call to sort. You can find this out by:- 1. Using the interactive interpreter help, e.g. >>> l = [2.7, 4.7, 5.8, 7.9] >>> help(l.sort) Help on built-in function sort: sort(*, key=None, reverse=False) method of builtins.list instance Stable sort *IN PLACE*. None 2. clicking `index` to the top right of https://docs.python.org/3/, then `s` then `(list method)` under `sort`. 3. using your favourite search engine. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email has been checked for viruses by AVG. http://www.avg.com From breamoreboy at yahoo.co.uk Wed Sep 27 18:12:37 2017 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 27 Sep 2017 23:12:37 +0100 Subject: [Tutor] Help with putting numbers from highest to lowest. In-Reply-To: References: <456155522.495962.1506517845293.ref@mail.yahoo.com> <456155522.495962.1506517845293@mail.yahoo.com> Message-ID: On 27/09/2017 20:36, Derry, James R wrote: > [Top posting fixed] > ________________________________________ > From: Tutor [tutor-bounces+jderry=mail.utexas.edu at python.org] on behalf of edmundo pierre via Tutor [tutor at python.org] > Sent: Wednesday, September 27, 2017 8:10 AM > To: Tutor Python > Subject: [Tutor] Help with putting numbers from highest to lowest. > > Hello, > When I used sort() to do that, but my problem is that sort() just arrange numbers from small to big, not from big to small. That is the issue I am having now. For instance: > # The user is entering those numbers:a = 2.7b = 4.7c= 5.8d = 7.9# I will like the answer to be like this: 7.9 5.8 4.7 2.7 > #but if I use sort(), I will have that answer, but I do not want that:2.7 4.7 5.8 7.9 > Thank you! > In [2]: ?sorted Noting that this is iPython specific... > Signature: sorted(iterable, /, *, key=None, reverse=False) > Docstring: > Return a new list containing all items from the iterable in ascending order. > > A custom key function can be supplied to customize the sort order, and the > reverse flag can be set to request the result in descending order. > Type: builtin_function_or_method > > In [3]: sorted([3,1,5], reverse=True) > Out[3]: [5, 3, 1] ...as are all the In[x] and Out[y] bits and pieces. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email has been checked for viruses by AVG. http://www.avg.com From LARRYSTALEY07 at comcast.net Wed Sep 27 22:51:10 2017 From: LARRYSTALEY07 at comcast.net (LARRYSTALEY07 at comcast.net) Date: Thu, 28 Sep 2017 02:51:10 +0000 (UTC) Subject: [Tutor] New Item In-Reply-To: <1562227876.4249932.1506564830121.JavaMail.zimbra@comcast.net> Message-ID: <1509230025.4273771.1506567070915.JavaMail.zimbra@comcast.net> I am very new to Python and appreciate the input as I was able to fully install Python with all needed libraries (i.e., numpy, pandas, etc.). However, I now have an application question in needed to construct a 2D Histogram. Basically, I have an Excel file that includes three columns: Column 1 - Gender (Male or Female) Column 2 - Height (in inches) Column 3 - Hand Span (in inches) There are 168 entries in the Excel file. I need to construct two separate 2D histograms for the two classes, one for the height and the other for the hand span. These formulas would appear as: r = ROUND ( (B-1) ((h(i) - h(min) / (h(max) - h(min)) ***this is for the height and requires each the min and max height from the 168 entries. B is for number of bins. I will use 7. i is for each height entry, from 1 through 168. c = ROUND ( (B-1) ((s(i) - s(min) / (s(max) - s(min)) ***this is for the hand span and requires each the min and max hand span from the 168 entries. B is for number of bins. Again, I am using 7. i is for each hand span entry, from 1 through 168. Finally, if gender = Female, I update H(f) as H(f)[r,c] + 1. Else is Male or update H(m) as H(m)[r,c] + H(m)[r,c] + 1. As it appears I need arrays first totalled by gender, height and hand span, appears that would look like the following: data=readExcel(excelfile) X=np.array(data[:,1],dtype=float); S=np.array(data[:,2],dtype=float); T=np.array(data[:,0],dtype=str); Finally, is my intended coding for the actual 2D histogram. I will get min and max from the height and hand span arrays. Note I am still learning Python and looping is new to me: # Define histogram classifier to build histogram using two variables def Build1DHistogramClassifier(X, S, smin, smax,T,B,xmin,xmax): HF=np.zeros(B).astype('int32'); HM=np.zeros(B).astype('int32'); binindices1=(np.round(((B-1)*(X-xmin)/(xmax-xmin)))).astype('int32'); binindices2=(np.round(((B-1)*(S-smin)/(smax-smin)))).astype('int32'); for i,b in enumerate(binindices1): for i,c in enumerate(bindindices2): if T[i]=='Female': HF[b,c]+=1; else: HM[b,c]+=1; return [HF, HM] I would appreciate any input on this approach and at least if I am going in the right direction. Thanks again. Larry Staley larrystaley07 at comcast.net 650.274.6794 From __peter__ at web.de Thu Sep 28 06:46:03 2017 From: __peter__ at web.de (Peter Otten) Date: Thu, 28 Sep 2017 12:46:03 +0200 Subject: [Tutor] New Item References: <1562227876.4249932.1506564830121.JavaMail.zimbra@comcast.net> <1509230025.4273771.1506567070915.JavaMail.zimbra@comcast.net> Message-ID: LARRYSTALEY07 at comcast.net wrote: > I am very new to Python and appreciate the input as I was able to fully > install Python with all needed libraries (i.e., numpy, pandas, etc.). > However, I now have an application question in needed to construct a 2D > Histogram. > > Basically, I have an Excel file that includes three columns: > Column 1 - Gender (Male or Female) > Column 2 - Height (in inches) > Column 3 - Hand Span (in inches) I have yet to grok your code samples, but my feeling is that your approach is too low-level. Do you mean something like http://matplotlib.org/examples/pylab_examples/hist2d_demo.html by "2d histograms"? That would require very little code written by yourself: import pandas as pd from matplotlib import pyplot filename = "size.xls" sheetname = "first" data = pd.read_excel(filename, sheetname) for index, sex in enumerate(["female", "male"], 1): pyplot.figure(index) subset = data[data["Gender"] == sex] pyplot.hist2d(subset["Height"].values, subset["Hand Span"].values) pyplot.show() > > There are 168 entries in the Excel file. > > I need to construct two separate 2D histograms for the two classes, one > for the height and the other for the hand span. These formulas would > appear as: > > r = ROUND ( (B-1) ((h(i) - h(min) / (h(max) - h(min)) ***this is for the > height and requires each the min and max height from the 168 entries. B is > for number of bins. I will use 7. i is for each height entry, from 1 > through 168. c = ROUND ( (B-1) ((s(i) - s(min) / (s(max) - s(min)) ***this > is for the hand span and requires each the min and max hand span from the > 168 entries. B is for number of bins. Again, I am using 7. i is for each > hand span entry, from 1 through 168. > > Finally, if gender = Female, I update H(f) as H(f)[r,c] + 1. Else is Male > or update H(m) as H(m)[r,c] + H(m)[r,c] + 1. > > As it appears I need arrays first totalled by gender, height and hand > span, appears that would look like the following: > > > > data=readExcel(excelfile) > X=np.array(data[:,1],dtype=float); > S=np.array(data[:,2],dtype=float); > T=np.array(data[:,0],dtype=str); > > > > > Finally, is my intended coding for the actual 2D histogram. I will get min > and max from the height and hand span arrays. Note I am still learning > Python and looping is new to me: > > > > > # Define histogram classifier to build histogram using two variables > def Build1DHistogramClassifier(X, S, smin, smax,T,B,xmin,xmax): > HF=np.zeros(B).astype('int32'); > HM=np.zeros(B).astype('int32'); > binindices1=(np.round(((B-1)*(X-xmin)/(xmax-xmin)))).astype('int32'); > binindices2=(np.round(((B-1)*(S-smin)/(smax-smin)))).astype('int32'); > for i,b in enumerate(binindices1): > for i,c in enumerate(bindindices2): > if T[i]=='Female': > HF[b,c]+=1; > else: > HM[b,c]+=1; > return [HF, HM] > > > > > I would appreciate any input on this approach and at least if I am going > in the right direction. > > Thanks again. > > > > > Larry Staley > > larrystaley07 at comcast.net > > 650.274.6794 > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From sjeik_appie at hotmail.com Fri Sep 29 10:35:46 2017 From: sjeik_appie at hotmail.com (Albert-Jan Roskam) Date: Fri, 29 Sep 2017 14:35:46 +0000 Subject: [Tutor] logging to cmd.exe In-Reply-To: References: , Message-ID: Dear Mats, Peter and Eryk, THANK YOU for your replies. What a wealth of information! Have a great weekend! Albert-Jan From chris2014 at postbox.xyz Fri Sep 29 13:02:07 2017 From: chris2014 at postbox.xyz (Chris) Date: Fri, 29 Sep 2017 19:02:07 +0200 Subject: [Tutor] Directory Structure Message-ID: <20170929190207.4ecdb229@cd> Hello, I'd like to store a directory tree in a python script. Background: Maildirs with mails older than five years should be archived. The folder structure should be kept in the target. I was very surprised, that there seems no readily usable module available. (In Perl neither). What's the best way to save them? - Chris From peter.collidge at gmail.com Fri Sep 29 03:51:23 2017 From: peter.collidge at gmail.com (Peter Collidge) Date: Fri, 29 Sep 2017 17:51:23 +1000 Subject: [Tutor] Python programming for the absolute beginner Message-ID: I have borrowed the above book from my local library but I believe it was written in 2010 and as a result I am having difficulty in deciding which version of Python to download. Can anyone help? Thanks Peter Collidge From peterrisley47 at gmail.com Thu Sep 28 18:35:32 2017 From: peterrisley47 at gmail.com (peter) Date: Thu, 28 Sep 2017 12:35:32 -1000 Subject: [Tutor] problem with python3.5 headfirst python 2nd ed chpt 10 test drive example decorator issues Message-ID: I am on chapter 10 of headfirst python second edition. got most of the prior codes to work but am stuck on this one. I will add the simple_webapp.py which is a decorator enabled? and checker.py which is the decorator. when I go into 127.0.0.1:5000 and enter I get the correct response. 127.0.0.1:5000/page1 gets me 'you are not logged in' also correct. '127.0.0.1:5000/login' returns 'you are now logged in'? which is correct but '127.0.0.1:5000/page1' after that should return 'this is page 1' but instead returns 'you are not logged in'. When I login to page 1 the 'session['logged_in'] = True' should still be true but apparently it has not been passed to 'check_logged_in'. I am stumped and do not know how to proceed to figure this out. Here are the codes Also they are attached to this email. I am hoping someone can show me the errors of my ways. hopefully it is something simple but I have gone over it a lot and think the code is correct. Thank you for your attention and help. Peter Risley checker.py from flask import session from functools import wraps def check_logged_in(func): ??? @wraps(func) ??? def wrapper(*args, **kwargs): ??????? if 'logged _in' in session: ??????????? return func(*args, **kwargs) ??????? return 'You are not logged in.' ??? return wrapper simple_webapp.py from flask import Flask, session from checker import check_logged_in """this 'simple_webapp.py' , which pulls all of chp 10 code together. When you need to restrict access to specific URLs, base your strategy on this webapp's mechanism. This uses checker.py check_logged_in and which is a decorator function to do the work.""" app = Flask(__name__) @app.route('/') def hello() -> str: ??? return 'Hello from the simple webapp.' @app.route('/page1') @check_logged_in def page1(): ??? return 'this is page 1.' @app.route('/page2') @check_logged_in def page2(): ??? return 'this is page 2.' @app.route('/page3') @check_logged_in def page3(): ??? return 'this is page 3.' @app.route('/login') def do_login() -> str: ??? session['logged_in'] = True ??? return 'you are now logged in.' @app.route('/logout') def do_logout() -> str: ??? session.pop('logged_in') ??? return 'you are now logged out.' app.secret_key = 'yes' if __name__ == '__main__': ??? app.run(debug=True) From __peter__ at web.de Fri Sep 29 14:19:24 2017 From: __peter__ at web.de (Peter Otten) Date: Fri, 29 Sep 2017 20:19:24 +0200 Subject: [Tutor] problem with python3.5 headfirst python 2nd ed chpt 10 test drive example decorator issues References: Message-ID: peter wrote: > I am on chapter 10 of headfirst python second edition. got most of the > prior codes to work but am stuck on this one. I will add the > simple_webapp.py which is a decorator enabled and checker.py which is > the decorator. when I go into 127.0.0.1:5000 and enter I get the correct > response. 127.0.0.1:5000/page1 gets me 'you are not logged in' also > correct. > > '127.0.0.1:5000/login' returns 'you are now logged in' which is correct > but '127.0.0.1:5000/page1' after that should return 'this is page 1' but > instead returns 'you are not logged in'. > > When I login to page 1 the 'session['logged_in'] = True' should still be > true but apparently it has not been passed to 'check_logged_in'. > > I am stumped and do not know how to proceed to figure this out. > > Here are the codes > > Also they are attached to this email. I am hoping someone can show me > the errors of my ways. hopefully it is something simple but I have gone > over it a lot and think the code is correct. The best approach is usually to do something completely different, and than look at the code with fresh eyes. Or to ask someone else, of course... > Thank you for your attention and help. > session['logged_in'] = True > if 'logged _in' in session: It looks like an extra space slipped in here. From alan.gauld at yahoo.co.uk Fri Sep 29 14:20:20 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 29 Sep 2017 19:20:20 +0100 Subject: [Tutor] Python programming for the absolute beginner In-Reply-To: References: Message-ID: On 29/09/17 08:51, Peter Collidge wrote: > I have borrowed the above book from my local library but I believe it was > written in 2010 and as a result I am having difficulty in deciding which > version of Python to download. > Can anyone help? If you want to follow the book use the version the book uses - probably 2.6 or something close? When you finish the book move to Python 3.6 and go through the official tutorial that accompanbies it, that should bring you up to date with the latest version. There won't be a huge difference but there are significant changes in moving from 2 to 3, so use the version you are studying first then upgrade. All the older versions are available on the download page of python.org. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From mats at wichmann.us Fri Sep 29 14:16:11 2017 From: mats at wichmann.us (Mats Wichmann) Date: Fri, 29 Sep 2017 12:16:11 -0600 Subject: [Tutor] Directory Structure In-Reply-To: <20170929190207.4ecdb229@cd> References: <20170929190207.4ecdb229@cd> Message-ID: <0f8016a4-29ad-7770-1cbb-761c2edbda59@wichmann.us> On 09/29/2017 11:02 AM, Chris wrote: > Hello, > > I'd like to store a directory tree in a python script. > > Background: Maildirs with mails older than five years should be > archived. The folder structure should be kept in the target. > > I was very surprised, that there seems no readily usable module > available. (In Perl neither). > > What's the best way to save them? It's not clear what you're really looking for... File/directory usage is really an OS-specific thing, and most of the functionality you want seems like it would be in the os module. Have you looked at functions like os.renames() and os.makedirs()? Do you need something much more complicated than that? From john.b.larocca at intel.com Fri Sep 29 14:30:36 2017 From: john.b.larocca at intel.com (Larocca, John B) Date: Fri, 29 Sep 2017 18:30:36 +0000 Subject: [Tutor] Python programming for the absolute beginner In-Reply-To: References: Message-ID: Hi Peter My company primarily uses 2.7, but as I understand it 2.x flavors are compatible with each other. 2.x scripts are generally not compatible with 3.x versions and vice versa So, keep that in mind. Regards, -John -----Original Message----- From: Tutor [mailto:tutor-bounces+john.b.larocca=intel.com at python.org] On Behalf Of Peter Collidge Sent: Friday, September 29, 2017 12:51 AM To: tutor at python.org Subject: [Tutor] Python programming for the absolute beginner I have borrowed the above book from my local library but I believe it was written in 2010 and as a result I am having difficulty in deciding which version of Python to download. Can anyone help? Thanks Peter Collidge _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From alan.gauld at yahoo.co.uk Fri Sep 29 14:41:27 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 29 Sep 2017 19:41:27 +0100 Subject: [Tutor] Directory Structure In-Reply-To: <20170929190207.4ecdb229@cd> References: <20170929190207.4ecdb229@cd> Message-ID: On 29/09/17 18:02, Chris wrote: > I'd like to store a directory tree in a python script. That doesn't make much sense. A directory tree is stored on the hard disk. A python script is the source code to a program you execute What do you mean by "store the directory tree in the script"? Do you mean you want the script to copy an existing directory tree? Or to create a new one? Or to store some kind of representation of the tree in memory when your code runs? Or create a static model of a directory tree in your source code? Or something else? - if so, what? > Background: Maildirs with mails older than five years should be > archived. The folder structure should be kept in the target. Again that's not very clear. Which mail system? Which OS? Some use a single file? Others use a directory tree (so I'm guess you mean one of those) and others use a database. (the representation as folders within a mail client bears little resemblance to how the physical data may be stored on disk) And what are you trying to do? remove old folders from the existing mail system - that could screw up its internal integrity checking (checksums for example)~ - or create an archive elsewhere? > I was very surprised, that there seems no readily usable module > available. (In Perl neither). Once we understand what you actually want to do we might find something suitable. But for now I have no clue what exactly you need. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alan.gauld at yahoo.co.uk Fri Sep 29 14:59:53 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 29 Sep 2017 19:59:53 +0100 Subject: [Tutor] problem with python3.5 headfirst python 2nd ed chpt 10 test drive example decorator issues In-Reply-To: References: Message-ID: On 28/09/17 23:35, peter wrote: > I am on chapter 10 of headfirst python second edition. got most of the > prior codes to work but am stuck on this one. I don;t know the book and only vaguely know Flask, but I'd start by adding some debug print statements to the functions. Something like def .... s = str(session[logged_in]) return s + ... To see what the actual value is in each call. > def check_logged_in(func): > ??? @wraps(func) > ??? def wrapper(*args, **kwargs): > ??????? if 'logged _in' in session: > ??????????? return func(*args, **kwargs) Maybe try returning session.keys() in this func? > ??????? return 'You are not logged in.' > ??? return wrapper > > @app.route('/') > def hello() -> str: > ??? return 'Hello from the simple webapp.' > > > @app.route('/page1') > @check_logged_in > def page1(): > ??? return 'this is page 1.' > > @app.route('/page2') > @check_logged_in > def page2(): > ??? return 'this is page 2.' > > @app.route('/page3') > @check_logged_in > def page3(): > ??? return 'this is page 3.' > > > @app.route('/login') > def do_login() -> str: > ??? session['logged_in'] = True > ??? return 'you are now logged in.' > > > @app.route('/logout') > def do_logout() -> str: > ??? session.pop('logged_in') > ??? return 'you are now logged out.' > > app.secret_key = 'yes' > > if __name__ == '__main__': > ??? app.run(debug=True) -- 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 Fri Sep 29 15:23:13 2017 From: chigga101 at gmail.com (Matthew Ngaha) Date: Fri, 29 Sep 2017 20:23:13 +0100 Subject: [Tutor] Python programming for the absolute beginner In-Reply-To: References: Message-ID: On Fri, Sep 29, 2017 at 7:20 PM, Alan Gauld via Tutor wrote: > If you want to follow the book use the version the book > uses - probably 2.6 or something close? > I think the book uses either Python 3.0 or 3.1. It's been a while since I read it but it doesn't use Python 2. The 2nd edition written in 2010 used Python 3 but the 1st edition used Python 2. From chris2014 at postbox.xyz Fri Sep 29 15:34:08 2017 From: chris2014 at postbox.xyz (Chris) Date: Fri, 29 Sep 2017 21:34:08 +0200 Subject: [Tutor] Directory Structure In-Reply-To: References: <20170929190207.4ecdb229@cd> Message-ID: <20170929213408.16838f23@cd> On Fri, 29 Sep 2017 19:41:27 +0100 Alan Gauld via Tutor wrote: > On 29/09/17 18:02, Chris wrote: > > > I'd like to store a directory tree in a python script. > > That doesn't make much sense. > A directory tree is stored on the hard disk. > A python script is the source code to a program you execute > > What do you mean by "store the directory tree in the script"? > Do you mean you want the script to copy an existing directory > tree? Or to create a new one? Or to store some kind of > representation of the tree in memory when your code runs? > Or create a static model of a directory tree in > your source code? > Or something else? - if so, what? I want to store some kind of representation of the tree in memory when the code runs. Then I could not only move the mails but also create lists, e.g. a table with mail headers. > Again that's not very clear. > Which mail system? Which OS? Some use a single file? > Others use a directory tree (so I'm guess you mean one of those) > and others use a database. Directory tree, it's called Maildir. Server is dovecot. > And what are you trying to do? remove old folders from the > existing mail system - that could screw up its internal > integrity checking (checksums for example)~ - or create > an archive elsewhere? Create an archive elsewhere. Copything the mails isn't the problem. Finding them is easy. I just don't know howto represent the tree in the script. > Once we understand what you actually want to do we might > find something suitable. But for now I have no clue what > exactly you need. Sorry, my question wasn't detailed enough. - Chris From chris2014 at postbox.xyz Fri Sep 29 15:39:24 2017 From: chris2014 at postbox.xyz (Chris) Date: Fri, 29 Sep 2017 21:39:24 +0200 Subject: [Tutor] Directory Structure In-Reply-To: <0f8016a4-29ad-7770-1cbb-761c2edbda59@wichmann.us> References: <20170929190207.4ecdb229@cd> <0f8016a4-29ad-7770-1cbb-761c2edbda59@wichmann.us> Message-ID: <20170929213924.4ac2ecb3@cd> On Fri, 29 Sep 2017 12:16:11 -0600 Mats Wichmann wrote: > It's not clear what you're really looking for... Sorry. Tried to ask more precisely in my reply to Alan. > File/directory usage is really an OS-specific thing, and most of the > functionality you want seems like it would be in the os module. > Have you looked at functions like os.renames() and os.makedirs()? Sure, but I'd like to know how to represent the directory structure in my script. Finding (os.walk) and copying the mails is not the problem. > Do you need something much more complicated than that? Well, I just like to know how to do that. Of course, I can find the mails with os.walk and move them. Create the necessary directory structure with something like mkdir -p. That's no problem. But when I want to print a list for example, I have to keep the structure, because folders have different meanings depending where they're in the tree. - Chris From mysecretrobotfactory at gmail.com Fri Sep 29 14:47:31 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Fri, 29 Sep 2017 11:47:31 -0700 Subject: [Tutor] I want to learn how memory works! Message-ID: Hi all, after 1 week of on and off hacking, I realized I simply don't know enough about how memory works fundamentally! Could you point me to a source of information about all the things someone should know before he starts writing a memory scanner? Attached is my current code, which doesn't work and can't figure out why. That's where I am at. > code starts. import ctypes User32 = ctypes.WinDLL('User32', use_last_error=True) Kernel32 = ctypes.WinDLL('kernel32', use_last_error=True) PID = 5924 PROCESS_QUERY_INFORMATION = 0x0400 PROCESS_VM_READ = 0x0010 Process = Kernel32.OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_READ, False, PID) ReadProcessMemory = Kernel32.ReadProcessMemory buffer = ctypes.create_string_buffer(4) bufferSize = (ctypes.sizeof(buffer)) # I think instead of using 1000000000, I should use the size of the total # memory used. but I don't know how to find this value. for n in range(1000000000): if ReadProcessMemory(Process, n, buffer, bufferSize, None): print('buffer: ',buffer) else: print('something is wrong!') print('Done.') From rajbalel at gmail.com Fri Sep 29 16:02:56 2017 From: rajbalel at gmail.com (Rajesh Balel) Date: Fri, 29 Sep 2017 13:02:56 -0700 Subject: [Tutor] problem with python3.5 headfirst python 2nd ed chpt 10 test drive example decorator issues In-Reply-To: References: Message-ID: Hi I don't see any session initializer , please try with that session = web.session.Session(app,store,initializer={'login': 0,'privilege': 0,'username':'Guest','logged_in':False}) Regards Rajesh On Thu, Sep 28, 2017 at 3:35 PM, peter wrote: > I am on chapter 10 of headfirst python second edition. got most of the > prior codes to work but am stuck on this one. I will add the > simple_webapp.py which is a decorator enabled and checker.py which is the > decorator. when I go into 127.0.0.1:5000 and enter I get the correct > response. 127.0.0.1:5000/page1 gets me 'you are not logged in' also > correct. > > '127.0.0.1:5000/login' returns 'you are now logged in' which is correct > but '127.0.0.1:5000/page1' after that should return 'this is page 1' but > instead returns 'you are not logged in'. > > When I login to page 1 the 'session['logged_in'] = True' should still be > true but apparently it has not been passed to 'check_logged_in'. > > I am stumped and do not know how to proceed to figure this out. > > Here are the codes > > Also they are attached to this email. I am hoping someone can show me the > errors of my ways. hopefully it is something simple but I have gone over it > a lot and think the code is correct. > > Thank you for your attention and help. > > Peter Risley > > checker.py > > from flask import session > from functools import wraps > > def check_logged_in(func): > @wraps(func) > def wrapper(*args, **kwargs): > if 'logged _in' in session: > return func(*args, **kwargs) > return 'You are not logged in.' > return wrapper > > simple_webapp.py > > from flask import Flask, session > from checker import check_logged_in > > """this 'simple_webapp.py' , which pulls all of chp 10 code together. When > you need to restrict access to specific URLs, base your strategy on this > webapp's mechanism. > This uses checker.py check_logged_in and which is a decorator function to > do the work.""" > > > > app = Flask(__name__) > > @app.route('/') > def hello() -> str: > return 'Hello from the simple webapp.' > > > @app.route('/page1') > @check_logged_in > def page1(): > return 'this is page 1.' > > @app.route('/page2') > @check_logged_in > def page2(): > return 'this is page 2.' > > @app.route('/page3') > @check_logged_in > def page3(): > return 'this is page 3.' > > > @app.route('/login') > def do_login() -> str: > session['logged_in'] = True > return 'you are now logged in.' > > > @app.route('/logout') > def do_logout() -> str: > session.pop('logged_in') > return 'you are now logged out.' > > app.secret_key = 'yes' > > if __name__ == '__main__': > app.run(debug=True) > > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From alan.gauld at yahoo.co.uk Fri Sep 29 19:23:45 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 30 Sep 2017 00:23:45 +0100 Subject: [Tutor] Directory Structure In-Reply-To: <20170929213408.16838f23@cd> References: <20170929190207.4ecdb229@cd> <20170929213408.16838f23@cd> Message-ID: On 29/09/17 20:34, Chris wrote: > I want to store some kind of representation of the tree in memory when > the code runs. Then I could not only move the mails but also create > lists, e.g. a table with mail headers. Thanks for the extra detail but... Probably the reason you can't find any modules out there is this is not something anyone would normally want to do. It would consume quite a lot of memory and provide little advantage (a little bit of speed) compared to building the lists (or copying the files) dynamically as you traverse the real directories. > Directory tree, it's called Maildir. Server is dovecot. > > Create an archive elsewhere. Copything the mails isn't the problem. > Finding them is easy. I just don't know howto represent the tree in the > script. Are you sure you really need to? The oly reason I can see for trying to do that would be if you were trying to build some kind of dynamic query engine that processed a lot of different queries in a single session. If you know before you run the code what you need to extract you are usually better just scanning the folders and processing the data as you find it. That way you only traverse the tree once rather than once to build the tree then search the tree (in memory) and go back again to process the files. If you really need it in memory there are some generic modules for building tree structures in memory - you will need to define the mail objects of course the modules are data neutral, they just allow you to create and navigate the tree, populating it with whatever objects you want. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From sjeik_appie at hotmail.com Fri Sep 29 10:55:42 2017 From: sjeik_appie at hotmail.com (Albert-Jan Roskam) Date: Fri, 29 Sep 2017 14:55:42 +0000 Subject: [Tutor] OT: how to best follow the posting style on this list Message-ID: Hi, I often read messages to this list from my Android from the mobile Hotmail page. On other occasions I would read from my browser, from?a Windows or a Linux desktop. Howver, Hotmail pretty much sucks when it comes to obeying the posting style [1]. It's even worse than Yahoo mail, which I've also tried (until they got hacked). Is there a recommended way to do this? Maybe gmail? I prefer to use more or less the same interface across platforms and devices, but this is not crucial. I've also been looking for pan-like apps (not necessarily free) for my Phone, but I couldn't find one that worked. Perhaps because I used a free news server (my ISP doesn't have one). Thanks in advance! Best wishes, Albert-Jan [1] https://en.wikipedia.org/wiki/Posting_style From steve at pearwood.info Fri Sep 29 21:32:57 2017 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 30 Sep 2017 11:32:57 +1000 Subject: [Tutor] Directory Structure In-Reply-To: <20170929190207.4ecdb229@cd> References: <20170929190207.4ecdb229@cd> Message-ID: <20170930013257.GY13110@ando.pearwood.info> On Fri, Sep 29, 2017 at 07:02:07PM +0200, Chris wrote: > Background: Maildirs with mails older than five years should be > archived. The folder structure should be kept in the target. Archived to what? > I was very surprised, that there seems no readily usable module > available. (In Perl neither). Reusable module to do what *precisely*? If you cannot explain what you need, how do you expect somebody to have anticipated your requirements and written a module to do it? > What's the best way to save them? Depends on what you are doing. But coding the paths in your source code is almost certainly not what you want to do. Surely you want to read the paths from the maildir itself, as it *actually* exists, rather than try to hard-code what you expect it to be in your source code? Have you looked at the contents of a maildir? Its actually an almost flat structure. Nested mail folders are not nested on the disk: a user's mail folder structure that looks like: inbox sent trash personal ??? family ??? friends work ??? critical ??? important ??? low is stored on disk as: Maildir/ ??? .sent/ ??? .trash/ ??? .personal/ ??? .personal.family/ ??? .person.friends/ ??? .work ??? .work.critical ??? .work.important ??? .work.low (Not shown: the mails themselves, some dovecot specific data files in the top level, and the special cur/ new/ tmp/ subdirectories found in each mail directory.) So all you really need is to record the path to the top level maildir directories (the original, and the place where you are archiving them). The subdirectories, you read from the disk as you go. Actually, *you* don't read them at all. Have you looked at the mailbox module in the standard library? It supports Maildir. I expect that what you would do is something like: source = Maildir('path/to/source') archive = Maildir('path/to/archive') for each directory in source: for each mail in directory: if mail older than five years: copy mail to archive delete mail from source and let the mailbox module do the hard work. (Especially the part about copying the mail, since the module will ensure that the flags and dates are copied correctly.) -- Steve From chris2014 at postbox.xyz Sat Sep 30 01:35:51 2017 From: chris2014 at postbox.xyz (Chris) Date: Sat, 30 Sep 2017 07:35:51 +0200 Subject: [Tutor] Directory Structure In-Reply-To: References: <20170929190207.4ecdb229@cd> <20170929213408.16838f23@cd> Message-ID: <20170930073551.55a64850@cd> On Sat, 30 Sep 2017 00:23:45 +0100 Alan Gauld via Tutor wrote: > Probably the reason you can't find any modules out there is this > is not something anyone would normally want to do. It would > consume quite a lot of memory and provide little advantage > (a little bit of speed) compared to building the lists (or > copying the files) dynamically as you traverse the real > directories. Ok, then that's definitely the better solution. > > Directory tree, it's called Maildir. Server is dovecot. > > > > Create an archive elsewhere. Copything the mails isn't the problem. > > Finding them is easy. I just don't know howto represent the tree in > > the script. > > Are you sure you really need to? It's more curiosity. > If you know before you run the code what you need to extract > you are usually better just scanning the folders and processing > the data as you find it. That way you only traverse the tree > once rather than once to build the tree then search the tree > (in memory) and go back again to process the files. OK. > If you really need it in memory there are some generic modules > for building tree structures in memory - you will need to > define the mail objects of course the modules are data neutral, > they just allow you to create and navigate the tree, populating > it with whatever objects you want. Ok, can you recommend some specific? - Chris From chris2014 at postbox.xyz Sat Sep 30 01:56:15 2017 From: chris2014 at postbox.xyz (Chris) Date: Sat, 30 Sep 2017 07:56:15 +0200 Subject: [Tutor] Directory Structure In-Reply-To: <20170930013257.GY13110@ando.pearwood.info> References: <20170929190207.4ecdb229@cd> <20170930013257.GY13110@ando.pearwood.info> Message-ID: <20170930075615.21f0688e@cd> Thank you for your detailed reply! I've tried to explain in my mails to Alan and Mats what I'm trying to achieve. On Sat, 30 Sep 2017 11:32:57 +1000 Steven D'Aprano wrote: > On Fri, Sep 29, 2017 at 07:02:07PM +0200, Chris wrote: > > > Background: Maildirs with mails older than five years should be > > archived. The folder structure should be kept in the target. > > Archived to what? A separate Maildir on tape. > > I was very surprised, that there seems no readily usable module > > available. (In Perl neither). > > Reusable module to do what *precisely*? If you cannot explain what > you need, how do you expect somebody to have anticipated your > requirements and written a module to do it? Represent the structure in memory. > > What's the best way to save them? > > Depends on what you are doing. But coding the paths in your source > code is almost certainly not what you want to do. Surely you want to > read the paths from the maildir itself, as it *actually* exists, > rather than try to hard-code what you expect it to be in your source > code? Well, if I had the structure in memory, I could save additional information and could print different lists, e.g. sorted by attachment size, sorted by project. A project can appear in different places in the tree. > Have you looked at the contents of a maildir? Its actually an almost > flat structure. Nested mail folders are not nested on the disk: a > user's mail folder structure that looks like: > > inbox > sent > trash > personal > ??? family > ??? friends > work > ??? critical > ??? important > ??? low > > > is stored on disk as: > > Maildir/ > ??? .sent/ > ??? .trash/ > ??? .personal/ > ??? .personal.family/ > ??? .person.friends/ > ??? .work > ??? .work.critical > ??? .work.important > ??? .work.low Good objection. You can make dovecot use the first layout on disk. Probably not a gain in disguise. > So all you really need is to record the path to the top level maildir > directories (the original, and the place where you are archiving > them). The subdirectories, you read from the disk as you go. Ok, I could even do this with the first structure. > Actually, *you* don't read them at all. Have you looked at the > mailbox module in the standard library? It supports Maildir. I expect > that what you would do is something like: > > source = Maildir('path/to/source') > archive = Maildir('path/to/archive') > for each directory in source: > for each mail in directory: > if mail older than five years: > copy mail to archive > delete mail from source I've used os.walk. I'll have a look at the Maildir library. - Chris From alan.gauld at yahoo.co.uk Sat Sep 30 02:44:27 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 30 Sep 2017 07:44:27 +0100 Subject: [Tutor] I want to learn how memory works! In-Reply-To: References: Message-ID: On 29/09/17 19:47, Michael C wrote: > Could you point me to a source of information about all the things someone > should know before he starts writing a memory scanner? Not a single source but wikipedia is a good start for anything technical. In particular you need to understand the difference between virtual(VM) and physical memory(PM). Virtual memory is the memory that your program(process in OS speak) sees and physical memory is the RAM installed in your computer. The OS maps virtual memory to physical memory and depending on the OS that mapping can be done in many ways. In some OS (especially mainframes) you can specify in a config file how much VM each process is given at startup, in others you specify how much VM it needs so the OS won't allow it to start up unless there is that much available (this is often used on small machines and embedded systems). In others the VM is always a theoretical space determined by the address size (or built into the kernel). The PM is a combination of the theoretical address space, the installed RAM and the virtual memory page file(s). The OS swaps memory between RAM and page file as necessary. It is quite a complex topic and heavily OS dependent. It looks like you are using Windows and it's too long since I looked at that level of detail (around NT4!) to be confident of a reply, but ~ I'd start with wikipedia for the basic concepts then move to MSDN for the detail for your OS. The key point is that from inside a process you are seeing a virtualized version of memory, rarely, if ever, the actual physical RAM addresses. Only the OS sees that. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From robertvstepp at gmail.com Sat Sep 30 10:59:23 2017 From: robertvstepp at gmail.com (boB Stepp) Date: Sat, 30 Sep 2017 09:59:23 -0500 Subject: [Tutor] Python programming for the absolute beginner In-Reply-To: References: Message-ID: On Fri, Sep 29, 2017 at 1:20 PM, Alan Gauld via Tutor wrote: > On 29/09/17 08:51, Peter Collidge wrote: >> I have borrowed the above book from my local library but I believe it was >> written in 2010 and as a result I am having difficulty in deciding which >> version of Python to download. >> Can anyone help? > > If you want to follow the book use the version the book > uses - probably 2.6 or something close? I no longer have a copy of this book, but I am fairly certain it uses an early version of Python 3. The main problem, though, is it uses a customized version of Pygame for its later gaming code called Livewires, so that would have to be Python-version-compatible. IIRC, the author had a download site where you could get the whole package together: correct Python version used in book, correct Livewires version, etc., and that this was addressed in the book (Maybe an appendix? Or the first get things setup chapter? Can't remember for sure.) If the OP goes on to use the sequel to this book, named "More Python Programming for the Absolute Beginner" (By a different author.), *that* book uses straight-up Pygame, again with an early version of Python 3. Again, I don't think there will be any problems with the exact version of Python 3 used as long as the version of Pygame is compatible with the chosen Python. -- boB From robertvstepp at gmail.com Sat Sep 30 10:37:15 2017 From: robertvstepp at gmail.com (boB Stepp) Date: Sat, 30 Sep 2017 09:37:15 -0500 Subject: [Tutor] OT: how to best follow the posting style on this list In-Reply-To: References: Message-ID: On Fri, Sep 29, 2017 at 9:55 AM, Albert-Jan Roskam wrote: > > Hi, > > I often read messages to this list from my Android from the mobile Hotmail page. On other occasions I would read from my browser, from a Windows or a Linux desktop. Howver, Hotmail pretty much sucks when it comes to obeying the posting style [1]. It's even worse than Yahoo mail, which I've also tried (until they got hacked). Is there a recommended way to do this? Maybe gmail? I prefer to use more or less the same interface across platforms and devices, but this is not crucial. I've also been looking for pan-like apps (not necessarily free) for my Phone, but I couldn't find one that worked. Perhaps because I used a free news server (my ISP doesn't have one). This is something I have struggled with as well. For reading purposes it is usually not much of a problem. It is when replying to posts that the trouble begins. I have settled into Gmail over time, mostly because it seems to work with my Android phone, iPad, desktop (Linux or Windows), etc., where my primary goal is to have all of my contact information well and easily synced. But I have been unable to to coerce it to do plain text responses on my iPad for instance. I used to be able to do this on my phone, but I have been through at least two major Android updates to my phone and now the way is not obvious, if it exists, anymore (Not that I wish to reply to Tutor posts much from my phone!). However, I have seen other members of this list occasionally responding from their phones who are Gmail users, so perhaps I am being dense. And even with my desktop usage of Gmail (Using the Chrome browser in Windows and Firefox for Linux), occasionally updates have broken my settings and I have had to fiddle around to regain a fixed width font and default plain text email settings. So if there is a better way that would make things uniformly *easy* across all types of common devices, I would be interested, too! I wonder what the people at Google use for lists like Tutor? I believe there are some such people on the main list. -- boB From mats at wichmann.us Sat Sep 30 11:19:06 2017 From: mats at wichmann.us (Mats Wichmann) Date: Sat, 30 Sep 2017 09:19:06 -0600 Subject: [Tutor] OT: how to best follow the posting style on this list In-Reply-To: References: Message-ID: <088aa732-fd79-254b-2cc8-678b5d432590@wichmann.us> On 09/30/2017 08:37 AM, boB Stepp wrote: > On Fri, Sep 29, 2017 at 9:55 AM, Albert-Jan Roskam > wrote: >> >> Hi, >> >> I often read messages to this list from my Android from the mobile Hotmail page. On other occasions I would read from my browser, from a Windows or a Linux desktop. Howver, Hotmail pretty much sucks when it comes to obeying the posting style [1]. It's even worse than Yahoo mail, which I've also tried (until they got hacked). Is there a recommended way to do this? Maybe gmail? I prefer to use more or less the same interface across platforms and devices, but this is not crucial. I've also been looking for pan-like apps (not necessarily free) for my Phone, but I couldn't find one that worked. Perhaps because I used a free news server (my ISP doesn't have one). > > So if there is a better way that would make things uniformly *easy* > across all types of common devices, I would be interested, too! I > wonder what the people at Google use for lists like Tutor? I believe > there are some such people on the main list. > Thanks for asking, by the way! Honestly, those of us who care about the stuff in the reference are more and more being considered dinosaurs... email has pretty much moved on and almost finished leaving behind the plain-text option. When I worked for Samsung for a while, most of the company had to use an in-house developed mail system as it was the only "approved" one, and it simply had no option for not emitting styled emails (my organization as specifically charged with working in the open source world got an exemption, but that was highly rare). No I don't know how other companies deal with the "participate in external mailing list" scenario, there's certainly enough of them. I don't have good answers. I use K-9 on my android phone and it does fine, although occasionally throws up on _received_ emails with excess crud. You can set it up as a client of your gmail account if you wish, you don't have to use gmail to read gmail! Actually have more trouble on the desktop: I'm on some mailing list where almost all of the participants work in Outlook-only shops, and we'll get things like "my responses interleaved in blue" (guess what: Thunderbird on my Linux desktop set up the way I have it doesn't show those). Sometimes get some funky apparently Outlook-only list style markup come through as plain text rendering things completely unreadable. Big sigh... absolutely nothing I can do about the mess, just have to grit my teeth and muddle through. My surviving advice is "do the best you can", wish it was better. You can look at a message or two you've sent in an archive and see how bad it comes out, unfortunately reading your own email in the client that sent it doesn't usually accurately reflect what it will look like to others. From srigalibe at gmail.com Sat Sep 30 13:12:46 2017 From: srigalibe at gmail.com (Sri G.) Date: Sat, 30 Sep 2017 22:42:46 +0530 Subject: [Tutor] Most common words in a text file Message-ID: I'm learning programming with Python. I?ve written the code below for finding the most common words in a text file that has about 1.1 million words. It's working fine, but I believe there is always room for improvement. When run, the function in the script gets a text file from the command-line argument sys.argv[1], opens the file in read mode, converts the text to lowercase, makes a list of words from the text after removing any whitespaces or empty strings, and stores the list elements as dictionary keys and values in a collections.Counter object. Finally, it returns a dictionary of the most common words and their counts. The words.most_common() method gets its argument from the optional top parameter. import sysimport collections def find_most_common_words(textfile, top=10): ''' Returns the most common words in the textfile.''' textfile = open(textfile) text = textfile.read().lower() textfile.close() words = collections.Counter(text.split()) # how often each word appears return dict(words.most_common(top)) filename = sys.argv[1] top_five_words = find_most_common_words(filename, 5) I need your comments please. Sri From cs at cskk.id.au Sat Sep 30 17:54:32 2017 From: cs at cskk.id.au (Cameron Simpson) Date: Sun, 1 Oct 2017 08:54:32 +1100 Subject: [Tutor] Most common words in a text file In-Reply-To: References: Message-ID: <20170930215432.GA14835@cskk.homeip.net> On 30Sep2017 22:42, Sri G. wrote: >I?ve written the code below for finding the most common words in a text >file that has about 1.1 million words. It's working fine, but I believe >there is always room for improvement. > >When run, the function in the script gets a text file from the command-line >argument sys.argv[1], opens the file in read mode, converts the text to >lowercase, makes a list of words from the text after removing any >whitespaces or empty strings, and stores the list elements as dictionary >keys and values in a collections.Counter object. Finally, it returns a >dictionary of the most common words and their counts. The >words.most_common() method gets its argument from the optional top > parameter. Thank you, this is a nice clear explaination. >import collections It is common to import specific names from modules; whether you just import the module or specific names depends on your needs. However, in this case, instead of importing "collections" and creating a counter with: collections.Counter() I would import the name "Counter" and use it like this: from collections import Counter ... ... Counter() ... >def find_most_common_words(textfile, top=10): It is often good form to externalise the "10" as a constant (Python doesn't have constants per se, but it has a convention for value which would be constants in other languages): DEFAULT_TOP_SIZE = 10 def find_most_common_words(textfile, top=DEFAULT_TOP_SIZE): In a library of functions you can then put all the "constants" up the top where they can be seen or modified, and reference then lower down where the functions are defined. > ''' Returns the most common words in the textfile.''' > textfile = open(textfile) You shouldn't reuse "textfile" as a file. Keep the filename value distinct. I tend to use names like "textpath" for the filename and "textfile" for the open file, eg: textfile = open(textpath) Of course you change the parameter name to match. > textfile = open(textfile) > text = textfile.read().lower() > textfile.close() The preferred way of opening files looks like this: with open(textpath) as textfile: ... work on the data from textfile ... ... ... This automatically closes textfile on exiting the "with" suite, even if an exception occurs (which is very handy in situations where the exception might be caught in outer code - you never miss closing the file). It is also shorter and more readable. > text = textfile.read().lower() This code reads the entire text into memory. It is more frugal to read the file progressively, which is good because it scales to files of any size. This concern is prominent in my mind because I started programming on systems with a 16 bit address space, so 64K max including the program itself _and_ the OS. But it applies even today when many data files are very large. So one might process file file as lines of text: C = Counter() for line in textfile: words = line.lower().split() C.update(words) This avoids the need for more than a single line to be stored in memory at any given time. Cheers, Cameron Simpson (formerly cs at zip.com.au) From cs at cskk.id.au Sat Sep 30 18:24:21 2017 From: cs at cskk.id.au (Cameron Simpson) Date: Sun, 1 Oct 2017 09:24:21 +1100 Subject: [Tutor] Directory Structure In-Reply-To: <20170930073551.55a64850@cd> References: <20170930073551.55a64850@cd> Message-ID: <20170930222421.GA77725@cskk.homeip.net> On 30Sep2017 07:35, Chris wrote: >On Sat, 30 Sep 2017 00:23:45 +0100 >Alan Gauld via Tutor wrote: >> If you really need it in memory there are some generic modules >> for building tree structures in memory - you will need to >> define the mail objects of course the modules are data neutral, >> they just allow you to create and navigate the tree, populating >> it with whatever objects you want. > >Ok, can you recommend some specific? Maybe not, but for your needs wouldn't nested dicts do the trick? Or nested objects? class MaildirInfo(object): def __init__(self, path): self.path = path self.subdirs = [] # array of more Maildirinfo instances self.other stuff here as needed ... top = MaildirInfo("top-maildir-path") then do the os.walk thing and fill out the data structure. .subdirs could also be a dict mapping named to MaildirInfo instance, depending on your needs. Rather than Alan's take that nobody would want to do this, as someone who has spent much time mucking with my mail tree using many scripts I'd say: everyone wants to do something different. The task itself is fairly simple, so roll your own. That makes is easy to implement your own policy instead of trying to do some kind of feature survey of stuff out there. Cheers, Cameron Simpson (formerly cs at zip.com.au) From robertvstepp at gmail.com Sat Sep 30 23:38:03 2017 From: robertvstepp at gmail.com (boB Stepp) Date: Sat, 30 Sep 2017 22:38:03 -0500 Subject: [Tutor] logging to cmd.exe In-Reply-To: References: Message-ID: On Tue, Sep 26, 2017 at 6:22 AM, Albert-Jan Roskam wrote: > PS: sorry about the missing quote (>>) markers. Hotmail can't do this. Is Gmail better? Yeah, in Gmail it will handle the quote markers when doing plain text. -- boB From breamoreboy at yahoo.co.uk Sat Sep 30 17:02:31 2017 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sat, 30 Sep 2017 22:02:31 +0100 Subject: [Tutor] Most common words in a text file In-Reply-To: References: Message-ID: On 30/09/2017 18:12, Sri G. wrote: > I'm learning programming with Python. > > I?ve written the code below for finding the most common words in a text > file that has about 1.1 million words. It's working fine, but I believe > there is always room for improvement. > > When run, the function in the script gets a text file from the command-line > argument sys.argv[1], opens the file in read mode, converts the text to > lowercase, makes a list of words from the text after removing any > whitespaces or empty strings, and stores the list elements as dictionary > keys and values in a collections.Counter object. Finally, it returns a > dictionary of the most common words and their counts. The > words.most_common() method gets its argument from the optional top > parameter. > > import sysimport collections > def find_most_common_words(textfile, top=10): > ''' Returns the most common words in the textfile.''' > > textfile = open(textfile) > text = textfile.read().lower() > textfile.close() The modern Pythonic way is:- with open(textfile) as textfile: text = textfile.read().lower() The file close is handled automatically for you. For those who don't know this construct using the "with" keyword is called a context manager, here's an article about them https://jeffknupp.com/blog/2016/03/07/python-with-context-managers/ > words = collections.Counter(text.split()) # how often each word appears > > return dict(words.most_common(top)) > > filename = sys.argv[1] How about some error handling if the user forgets the filename? The Pythonic way is to use a try/except looking for an IndexError, but there's nothing wrong with checking the length of sys.argv. > top_five_words = find_most_common_words(filename, 5) > > I need your comments please. > > Sri Pretty good all in all :) -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email has been checked for viruses by AVG. http://www.avg.com