From rmwenya2002 at yahoo.com Thu Nov 1 10:01:30 2018 From: rmwenya2002 at yahoo.com (richard mwenya) Date: Thu, 1 Nov 2018 14:01:30 +0000 (UTC) Subject: [Tutor] TESTING References: <1948011328.20500200.1541080890462.ref@mail.yahoo.com> Message-ID: <1948011328.20500200.1541080890462@mail.yahoo.com> Hello. Everyone is quiet or is it my email thats not working? From ianhclark510 at gmail.com Thu Nov 1 10:21:32 2018 From: ianhclark510 at gmail.com (Ian Clark) Date: Thu, 1 Nov 2018 07:21:32 -0700 Subject: [Tutor] TESTING In-Reply-To: <1948011328.20500200.1541080890462@mail.yahoo.com> References: <1948011328.20500200.1541080890462.ref@mail.yahoo.com> <1948011328.20500200.1541080890462@mail.yahoo.com> Message-ID: Hello, i'm seeing your message On Thu, Nov 1, 2018 at 7:03 AM richard mwenya via Tutor wrote: > Hello. > Everyone is quiet or is it my email thats not working? > _______________________________________________ > 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 Thu Nov 1 13:15:08 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 1 Nov 2018 17:15:08 +0000 Subject: [Tutor] TESTING In-Reply-To: <1948011328.20500200.1541080890462@mail.yahoo.com> References: <1948011328.20500200.1541080890462.ref@mail.yahoo.com> <1948011328.20500200.1541080890462@mail.yahoo.com> Message-ID: On 01/11/2018 14:01, richard mwenya via Tutor wrote: > Hello. > Everyone is quiet or is it my email thats not working? Just quiet, I've seen nothing in the moderators queue for 5 days. But that happens sometimes. I guess it just means nobody is having any Python issues right now. -- Alan G List moderator From reignofcolours at gmail.com Thu Nov 1 13:24:44 2018 From: reignofcolours at gmail.com (Mario Radomirovic) Date: Thu, 1 Nov 2018 18:24:44 +0100 Subject: [Tutor] TESTING In-Reply-To: References: <1948011328.20500200.1541080890462.ref@mail.yahoo.com> <1948011328.20500200.1541080890462@mail.yahoo.com> Message-ID: All good On Thu, 1 Nov 2018 6:17 pm Alan Gauld via Tutor wrote: > On 01/11/2018 14:01, richard mwenya via Tutor wrote: > > Hello. > > Everyone is quiet or is it my email thats not working? > > Just quiet, I've seen nothing in the moderators queue > for 5 days. But that happens sometimes. > > I guess it just means nobody is having any Python issues > right now. > > > -- > Alan G > List moderator > > _______________________________________________ > 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 Nov 1 13:54:12 2018 From: mats at wichmann.us (Mats Wichmann) Date: Thu, 1 Nov 2018 11:54:12 -0600 Subject: [Tutor] TESTING In-Reply-To: References: <1948011328.20500200.1541080890462.ref@mail.yahoo.com> <1948011328.20500200.1541080890462@mail.yahoo.com> Message-ID: On 11/1/18 11:24 AM, Mario Radomirovic wrote: > All good > > On Thu, 1 Nov 2018 6:17 pm Alan Gauld via Tutor wrote: > >> On 01/11/2018 14:01, richard mwenya via Tutor wrote: >>> Hello. >>> Everyone is quiet or is it my email thats not working? >> >> Just quiet, I've seen nothing in the moderators queue >> for 5 days. But that happens sometimes. >> >> I guess it just means nobody is having any Python issues >> right now. or the ones who do gave up on us because people say they won't solve homework problems :) From avigross at verizon.net Thu Nov 1 23:45:05 2018 From: avigross at verizon.net (Avi Gross) Date: Thu, 1 Nov 2018 23:45:05 -0400 Subject: [Tutor] TESTING In-Reply-To: References: <1948011328.20500200.1541080890462.ref@mail.yahoo.com> <1948011328.20500200.1541080890462@mail.yahoo.com> Message-ID: <004401d4725e$71798a70$546c9f50$@verizon.net> I am not totally up on the exact purposes of this group but have noted how many of the questions asked have been frustrating. I wonder if it would make any sense to provide a template people might fill out to suit their needs. An example might be people who want you to pretty much do their homework for them from beginning to end. The purpose of a tutor is to guide someone in learning how to do something for themselves. If they ask for an idea on an algorithm in English or pseudocode on how to solve the problem, hey might be given hints but not actual python code. If they wrote a snippet of code on their own and wonder how to test it, again, there can be advice. If they ask for the names of packages that might be useful, ditto. If they encounter an error and can isolate it to a PART of the code, others might be willing to point out that $ is not valid in Python user names (other than in regular expressions or strings) and they should consider writing in a language like PERL or a shell command language that uses it, albeit for another purpose. But to do it ALL for someone generally is not the role of a tutor in the real world. On the other hand, when someone discusses how to optimize their code, and shows the ways they already tried, it may be valid to present them with a complete program that illustrates it assuming this is not a homework problem but a more advanced person that may be interested. But, to make this message more meaningful, I have a question. In my reading I see Python has been bifurcated into often incompatible releases we call 2X and 3X. Many times if you want something to be portable, you have to forgo using some of the niftier new features. But for a language to evolve, you sometimes need to bite the bullet. I was one of the earliest uses of C++ when I was at Bell Labs and the decision was then made to not just call it C (or D or add-one-to-C or even ++C) but make a clean break with C and give it a new name. The aforementioned PERL also split into incompatible languages. So, does anyone see any further breaks in Python such as when 4.0 comes along or will we have some backward compatibility for a while? As I see it, one of the earlier guiding principles of Python was to have mainly one obvious way to do something and in my reading, I often see MANY ways to do it. Much of this is caused by adding new features while retaining the old. Yes, I also see places where a new feature is accompanied by deprecating an older one and eventually removing it. But that too causes incompatibilities especially when you use a package written by someone and then abandoned. I recently ran into a problem with an R script I wrote a year or so ago that I needed to modify in a minor way (re-scale the limits of the Y axis on a graph) which I might have done in a few minutes. But the package I used to do part of the work had been modified and the function just printed out that I should use a replacement function which did not work. Reloading old versions of the package still did not work because it had many dependencies and getting them all correct was more work than rewriting my code after reading what their code used to run. I wonder if others have written tools (such as in frozen programs) that gather up an exact snapshot of the environment needed when you submit a paper so it can be replicated! Avi -----Original Message----- From: Tutor On Behalf Of Mats Wichmann Sent: Thursday, November 1, 2018 1:54 PM To: tutor at python.org Subject: Re: [Tutor] TESTING On 11/1/18 11:24 AM, Mario Radomirovic wrote: > All good > > On Thu, 1 Nov 2018 6:17 pm Alan Gauld via Tutor wrote: > >> On 01/11/2018 14:01, richard mwenya via Tutor wrote: >>> Hello. >>> Everyone is quiet or is it my email thats not working? >> >> Just quiet, I've seen nothing in the moderators queue for 5 days. But >> that happens sometimes. >> >> I guess it just means nobody is having any Python issues right now. or the ones who do gave up on us because people say they won't solve homework problems :) From alan.gauld at yahoo.co.uk Fri Nov 2 06:02:36 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 2 Nov 2018 10:02:36 +0000 Subject: [Tutor] TESTING In-Reply-To: <004401d4725e$71798a70$546c9f50$@verizon.net> References: <1948011328.20500200.1541080890462.ref@mail.yahoo.com> <1948011328.20500200.1541080890462@mail.yahoo.com> <004401d4725e$71798a70$546c9f50$@verizon.net> Message-ID: On 02/11/2018 03:45, Avi Gross wrote: > I am not totally up on the exact purposes of this group but have noted how > many of the questions asked have been frustrating. Unfortunately that goes with the territory. We are a list catering to beginners to Python and, frequently, to programming. As a result the participants often have little or no experience of using technical mailing lists or fora. > I wonder if it would make any sense to provide a template people might fill > out to suit their needs. The problem is how. Where would the template reside? We do provide some hints on the welcome mails that go out but its clear that most newcomers don't read it, so a template would likely suffer the same fate. But the biggest problem for a mailing list, unlike a web site, is that there is nowhere to post information in a persistent manner. You could go to the archive and search, but that takes effort which newbies are unlikely to expend. Also, we want to encourage people to use the list and a long list of "rules" and formats is likely to drive them elsewhere. > An example might be people who want you to pretty much do their homework for > them from beginning to end. This has been an issue as long as I've been on the internet (ie since before 1986)and there is IMHO no cure other than to consistently offer help but not solutions. Those who want to learn will come back. Those who just want a quick fix will just ask on Quora... > But to do it ALL for someone generally is not the role of a tutor in the > real world. Absolutely. > On the other hand, when someone discusses how to optimize their code, and > shows the ways they already tried, it may be valid to present them with a > complete program that illustrates it assuming this is not a homework problem > but a more advanced person that may be interested. And that's how we try to deal with it. If its a genuine problem we will give more complete solutions, if its (suspected) homework we give hints and tips. > But, to make this message more meaningful, I have a question. In my reading > I see Python has been bifurcated into often incompatible releases we call 2X > and 3X. Yes, although 2.X is effectively deprecated. But industry usage requires that it still gets some support. > So, does anyone see any further breaks in Python such as when 4.0 comes > along or will we have some backward compatibility for a while? Others can answer this better than I but so far each generation of Python has been close to a decade apart (v1 ~ 1991, v2 ~ c2000, v3 ~ c2010) So v4 would logically appear around 2020, but I've seen no specific discussion of that whereas both v2 and v3 had long debates about what would be in/out and how much backward compatibility would be included. > one of the earlier guiding principles of Python was to have mainly one > obvious way to do something and in my reading, I often see MANY ways Yes, I've never bought the 1 obvious way line. Any decent programming language offers multiple paths. Language idioms may evolve to suggest a preferred option but in practice you need alternatives for expressiveness. > it. Much of this is caused by adding new features while retaining the old. And is exacerbated by the Open Source model. Anyone can come up with a "good idea" to "fix" an issue and it gets adopted. Just look at the long history of how to start an external process from Python. system(), popen() - and its several variants - exec(), command(), subprocess,,, > especially when you use a package written by someone and then abandoned. That's always a problem with 3rd party libraries. If its in the core language and standard library it should be maintained, or at least go through a controlled and published obsolescence process. > I wonder if others have written tools (such as in frozen programs) that > gather up an exact snapshot of the environment needed when you submit a > paper so it can be replicated! That's really a configuration management issue and certainly such tools exist, but they require you to store all the historical versions within the CM system. The best I ever used was ClearCase which had a concept of views, controlled by a text file. By changing the view then restarting the tool it would magically recreate the full disk image of the project as it had been frozen, including code, compilers, debuggers etc. The only problem was when the OS itself was incompatible and that required a virtual machine. As a maintenance programmer it was absolutely brilliant. You could take a bug report and within minutes have the relevant version of the system loaded and ready to work, then next bug you had a completely different version ready in the blink of an eye (well maybe 2 or 3 minutes! :) But ClearCase cost megabucks. -- 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 rls4jc at gmail.com Fri Nov 2 17:13:46 2018 From: rls4jc at gmail.com (Roger Lea Scherer) Date: Fri, 2 Nov 2018 14:13:46 -0700 Subject: [Tutor] Installing python Message-ID: Hey, I have installed python 3.7 on my computer Windows10 (C:\Users\Roger\AppData\Local\Programs\Python\Python37), but when I go to the git repository that holds all my projects and type "python --version" in WindowsPowershell, I get Python 3.6.5. So I guess my question is am I supposed to install python 3.7 in the git repository? and a related question I think, am I supposed to install python 3.7 in every virtual environment I create, or is there a way to link python to each virtual environment or each repo or each project? I guess I'm so used to installing programs in Windows or Mac and having them accessible globally (I guess is the word) that I expected python 3.7 to do the same thing, although I understand about programs written earlier crashing because of upgrades, like from 2.7 to 3.4 for instance. I'm so confused I'm not sure I even know what questions to ask, but the above questions seem to be a missing link in my knowledge at this point. Thank you as always. -- Roger Lea Scherer 623.255.7719 *Strengths:* Input, Strategic, Responsibility, Learner, Ideation Virus-free. www.avast.com <#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2> From alan.gauld at yahoo.co.uk Fri Nov 2 18:49:37 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 2 Nov 2018 22:49:37 +0000 Subject: [Tutor] Installing python In-Reply-To: References: Message-ID: On 02/11/2018 21:13, Roger Lea Scherer wrote: > I have installed python 3.7 on my computer Windows10 > (C:\Users\Roger\AppData\Local\Programs\Python\Python37), Caveat: I'm no expert on modern Windoze. But... That looks like the wrong path to me. My Windows install is usually either in C:\Python... or C:\Program files\Python... Depending on distro... How exactly did you install it? Did you use the Pyhon.org binary installer? Did you use the default settings? I don't know if they have 3.7 available yet, but I usually recommend activestate.com for Windows users, its install seems less problematic in my (limited) experience. And of course comes loaded with lots of extra Windows specific goodies. > the git repository that holds all my projects and type "python --version" > in WindowsPowershell, I get Python 3.6.5. So you must have two Pythons installed? What is your PATH set to? That's what controls which Python you pick up (along with some registry settings) > So I guess my question is am I supposed to install python 3.7 in the git Definitely not! > a related question I think, am I supposed to install python > 3.7 in every virtual environment I create, I don't think so but never use virtual environments. Never found a need. > I guess I'm so used to installing programs in Windows or Mac and having > them accessible globally That's what happens with Python for me... But you have two versions installed so it depends on what Windows thinks is the default install. What happens if you have two versions of MS Office installed and you type winword? > to do the same thing, although I understand about programs written earlier > crashing because of upgrades, like from 2.7 to 3.4 for instance. That's got nothing to do with install issues, that's incompatibity in the code between v2 and v3. I think the key is how exactly you installed both versions. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From steve at pearwood.info Fri Nov 2 19:00:19 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 3 Nov 2018 10:00:19 +1100 Subject: [Tutor] Installing python In-Reply-To: References: Message-ID: <20181102230018.GN3817@ando.pearwood.info> On Fri, Nov 02, 2018 at 02:13:46PM -0700, Roger Lea Scherer wrote: > Hey, > > I have installed python 3.7 on my computer Windows10 > (C:\Users\Roger\AppData\Local\Programs\Python\Python37), but when I go to > the git repository that holds all my projects and type "python --version" > in WindowsPowershell, I get Python 3.6.5. What happens if you type "python37 --version" instead? What happens if you call the Python launcher instead? Try this and see what it does: py -V https://docs.python.org/3/using/windows.html#launcher > So I guess my question is am I supposed to install python 3.7 in the git > repository? Heavens no. > I guess I'm so used to installing programs in Windows or Mac and having > them accessible globally (I guess is the word) that I expected python 3.7 > to do the same thing, I expect it is accessible globally, but you need to know what to call it. I'm not a Windows expert, but I expect that it will be more or less equivalent to other OSes in the sense that when you call a program by name (say, "notepad", or "python") it searches a list of known program locations (probably recorded in the Registry) for a matching name, and runs the first one found. I expect that you have entries for python36 python37 and probably others, plus an alias for "python" which points to python36. You may be able to change the alias, but I don't know how you do that on Windows. Or just be explicit about the one you want. Or use the "py" Windows launcher, which is supposed to manage all that for you. (I don't know how well it works.) > although I understand about programs written earlier > crashing because of upgrades, like from 2.7 to 3.4 for instance. They shouldn't *crash* the interpreter. If they do, that's a bug in the interpreter. They might *raise an exception*. The difference is that a crash (a segfault or core dump) is the interpreter melting down and exploding, while an exception is a nicely controlled shut down. Hope this helps shed some light on what's going on. -- Steve From mats at wichmann.us Fri Nov 2 19:44:21 2018 From: mats at wichmann.us (Mats Wichmann) Date: Fri, 2 Nov 2018 17:44:21 -0600 Subject: [Tutor] Installing python In-Reply-To: References: Message-ID: <2d3937d6-d558-0020-7e34-4f4b64c73dc0@wichmann.us> On 11/2/18 4:49 PM, Alan Gauld via Tutor wrote: > On 02/11/2018 21:13, Roger Lea Scherer wrote: > >> I have installed python 3.7 on my computer Windows10 >> (C:\Users\Roger\AppData\Local\Programs\Python\Python37), > > Caveat: I'm no expert on modern Windoze. > But... That looks like the wrong path to me. > > My Windows install is usually either in > > C:\Python... > or > C:\Program files\Python... > > Depending on distro... that's actually exactly the right path for a Python 3.5+ if you chose a "user install", which you usually should. Nothing wrong there. However, you should check if it is in the path - if you wanted it in the path. More below. > > How exactly did you install it? Did you use the Pyhon.org > binary installer? Did you use the default settings? > > I don't know if they have 3.7 available yet, > but I usually recommend activestate.com for > Windows users, its install seems less > problematic in my (limited) experience. > And of course comes loaded with lots of > extra Windows specific goodies. > >> the git repository that holds all my projects and type "python --version" >> in WindowsPowershell, I get Python 3.6.5. > > So you must have two Pythons installed? > What is your PATH set to? That's what controls > which Python you pick up (along with some > registry settings) > >> So I guess my question is am I supposed to install python 3.7 in the git > > Definitely not! > >> a related question I think, am I supposed to install python >> 3.7 in every virtual environment I create, > > I don't think so but never use virtual environments. > Never found a need. > >> I guess I'm so used to installing programs in Windows or Mac and having >> them accessible globally actually, windows makes that harder than Mac or Linux, where programs go to a global shared location. on windows apps go... well, all kinds of places, but it's nearly always a _unique_ directory per app. if you look in Program Files and Program Files (x86) that's what you see - a unique directory either per program, or per vendor (like Microsoft, Adobe, etc.) - and then program-specific subdirectories somewhere underneath. All that mess is papered over by adding each one of those directories to your path. which many programs do by default, and some ask you. Python asks you - but not in a very obvious way, I think you have to select the advanced options before you even see the question. the "modern" answer for Windows is supposed to be the Python Launcher, which should be in your path, and is invoked by typing "py". py lets you speficy the python you want if you don't want the default. Try this for grins: py -0 --list it shows you what it thinks are the available Pythons on your system. if you indeed get two, and the default is 3.6, then you can run Python 3.7 by typing: py -3.7 pretty simple, hopefully. hope this helps. From breamoreboy at gmail.com Fri Nov 2 20:07:53 2018 From: breamoreboy at gmail.com (Mark Lawrence) Date: Sat, 3 Nov 2018 00:07:53 +0000 Subject: [Tutor] Installing python In-Reply-To: References: Message-ID: On 02/11/2018 22:49, Alan Gauld via Tutor wrote: > On 02/11/2018 21:13, Roger Lea Scherer wrote: > >> I have installed python 3.7 on my computer Windows10 >> (C:\Users\Roger\AppData\Local\Programs\Python\Python37), > > Caveat: I'm no expert on modern Windoze. > But... That looks like the wrong path to me. > > My Windows install is usually either in > > C:\Python... > or > C:\Program files\Python... > > Depending on distro... > The C:\Users... path has been the standard for some time for a single user install. C:\Python was binned some years back for security reasons whilst C:\Program Files\Python... was added for security reasons. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From alan.gauld at yahoo.co.uk Fri Nov 2 21:16:05 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 3 Nov 2018 01:16:05 +0000 Subject: [Tutor] Installing python In-Reply-To: <2d3937d6-d558-0020-7e34-4f4b64c73dc0@wichmann.us> References: <2d3937d6-d558-0020-7e34-4f4b64c73dc0@wichmann.us> Message-ID: On 02/11/2018 23:44, Mats Wichmann wrote: > that's actually exactly the right path for a Python 3.5+ if you chose a > "user install", which you usually should. Ah, I always install for all users. That's why I've not seen that path presumably? But why *should* you install for a single user? I only ever do that for programs that I'm still writing and don't want other users to accidentally start. But I want everyone to be able to run my Python programs so I always install for everyone. > the "modern" answer for Windows is supposed to be the Python Launcher, I keep forgetting that thing exists. I really must spend some time getting to grips with 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 mats at wichmann.us Fri Nov 2 23:54:21 2018 From: mats at wichmann.us (Mats Wichmann) Date: Fri, 2 Nov 2018 21:54:21 -0600 Subject: [Tutor] Installing python In-Reply-To: References: <2d3937d6-d558-0020-7e34-4f4b64c73dc0@wichmann.us> Message-ID: ays install for everyone. > >> the "modern" answer for Windows is supposed to be the Python Launcher, > > I keep forgetting that thing exists. I really must spend > some time getting to grips with it... I used it for a bit because I was trying to disentangle test suite failures unique to windows for a project... and had four concurrent versions of python while doing that. but then ended up having to install a bunch of "extra" stuff so set up virtualenvs to precisely control the environment I was getting, and not using py any more. I think it's a case of you use what works and that may not require python launcher, if what you already have works fine :) (esp. for those of us who are not necessarily normally windows users) From adameyring at gmail.com Fri Nov 2 21:32:40 2018 From: adameyring at gmail.com (Adam Eyring) Date: Fri, 2 Nov 2018 21:32:40 -0400 Subject: [Tutor] Installing python In-Reply-To: References: Message-ID: On Fri, Nov 2, 2018 at 8:10 PM Mark Lawrence wrote: > On 02/11/2018 22:49, Alan Gauld via Tutor wrote: > > On 02/11/2018 21:13, Roger Lea Scherer wrote: > > > >> I have installed python 3.7 on my computer Windows10 > >> (C:\Users\Roger\AppData\Local\Programs\Python\Python37), > > > > Caveat: I'm no expert on modern Windoze. > > But... That looks like the wrong path to me. > > > > My Windows install is usually either in > > > > C:\Python... > > or > > C:\Program files\Python... > > > > Depending on distro... > > > > The C:\Users... path has been the standard for some time for a single > user install. C:\Python was binned some years back for security reasons > whilst C:\Program Files\Python... was added for security reasons. > > The c:\users... path is frustrating to find because the AppData folder by default is hidden. I haven't bothered to make it visible, so I just create a shortcut to the Pythonxx folder to access my code files. From avigross at verizon.net Fri Nov 2 23:27:39 2018 From: avigross at verizon.net (Avi Gross) Date: Fri, 2 Nov 2018 23:27:39 -0400 Subject: [Tutor] Installing python In-Reply-To: References: <2d3937d6-d558-0020-7e34-4f4b64c73dc0@wichmann.us> Message-ID: <008901d47325$2de42e60$89ac8b20$@verizon.net> Alan, Many people have machines with a single login that is shared with nobody. I install lots of software on multiple machines that only I use and have often wondered what the purpose for me was to install for everyone. On some machines you then need to log in as whatever variant of superuser you have to be if you want to be able to write in restricted directories or make changes to the operating system or global configuration files. There can be tradeoffs in how long it takes to find a program on your PATH or PYTHONPATH or other such things. But, of course, if you have many users who might otherwise install their own personal copies of software on the same machine, especially one where many people may log in at once, then it may make more sense to share. But I wonder about the many modern languages that can be extended by also installing one of thousands of packages above and beyond the standard ones routinely in place. Do you want people to replace earlier versions for everyone or be able to install them in a global area at all? Among other considerations, if they have write access they might be able to modify something others use, like numpy, which is a serious security risk when you can sneak in a call to pop open a shell that reformats the hard disk or whatever. Avi -----Original Message----- From: Tutor On Behalf Of Alan Gauld via Tutor Sent: Friday, November 2, 2018 9:16 PM To: tutor at python.org Subject: Re: [Tutor] Installing python On 02/11/2018 23:44, Mats Wichmann wrote: > that's actually exactly the right path for a Python 3.5+ if you chose > a "user install", which you usually should. Ah, I always install for all users. That's why I've not seen that path presumably? But why *should* you install for a single user? I only ever do that for programs that I'm still writing and don't want other users to accidentally start. But I want everyone to be able to run my Python programs so I always install for everyone. > the "modern" answer for Windows is supposed to be the Python Launcher, I keep forgetting that thing exists. I really must spend some time getting to grips with 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 _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From rba2124 at gmail.com Sat Nov 3 00:01:55 2018 From: rba2124 at gmail.com (Roger B. Atkins) Date: Fri, 2 Nov 2018 21:01:55 -0700 Subject: [Tutor] Installing python In-Reply-To: References: <2d3937d6-d558-0020-7e34-4f4b64c73dc0@wichmann.us> Message-ID: Okay, but what about the virtual environments? Once the Windows 10 path environment variable has a version of Python added to it, Windows will find it (I am using Win 10), but will it be found from within any given virtual environment (venv), regardless of which version created the environment? And if 3rd party packages have been installed within a venv, will they be found a) by Windows, and b) by the Python version within the venv, even if it was not used to install the packages? These questions confounded me until I finally deleted Anaconda, and Python with it, because when running programs in Windows vice the Spyder IDE, Windows could find Python, but not the 3rd party packages such as pyperclip, bs4, and requests. I suspect it had something to do with the Anaconda installation process establishing a venv, and the package locations not existing (or not being visible) in Windows. That's just a WAG based on not being able to find the specified directories in Windows Explorer. So, I'm just wondering if my quagmire was part of or related to Mr. Scherer's, and whether he might have similar problems if he goes back and forth between different virtual environments. On Fri, Nov 2, 2018 at 6:18 PM Alan Gauld via Tutor wrote: > > On 02/11/2018 23:44, Mats Wichmann wrote: > > > that's actually exactly the right path for a Python 3.5+ if you chose a > > "user install", which you usually should. > > Ah, I always install for all users. That's why I've not > seen that path presumably? > > But why *should* you install for a single user? I only > ever do that for programs that I'm still writing and > don't want other users to accidentally start. But I > want everyone to be able to run my Python programs > so I always install for everyone. > > > the "modern" answer for Windows is supposed to be the Python Launcher, > > I keep forgetting that thing exists. I really must spend > some time getting to grips with 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 > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From mats at wichmann.us Sat Nov 3 10:39:21 2018 From: mats at wichmann.us (Mats Wichmann) Date: Sat, 3 Nov 2018 08:39:21 -0600 Subject: [Tutor] Installing python In-Reply-To: References: <2d3937d6-d558-0020-7e34-4f4b64c73dc0@wichmann.us> Message-ID: <1bb7bede-5051-5a4a-ced9-3e3bd6b0bdc1@wichmann.us> On 11/2/18 10:01 PM, Roger B. Atkins wrote: > Okay, but what about the virtual environments? Once the Windows 10 > path environment variable has a version of Python added to it, Windows > will find it (I am using Win 10), but will it be found from within any > given virtual environment (venv), regardless of which version created > the environment? And if 3rd party packages have been installed within > a venv, will they be found a) by Windows, and b) by the Python version > within the venv, even if it was not used to install the packages? (a) when you activate a virtualenv, it will always find that python (the one the env was created from), that's what the activation does - fiddle paths so you see that envirnoment and not any other that may be sitting around. (b) when you pip install with a virtualenv active the installation will be private to that virtualenv. which leads to... Anaconda uses virtualenvs (afiak, _always_, rather than "when you ask for one" like many IDEs do), so, packages installed though Anaconda are in that sense "private". They're not secret - you can see them all in the physical location where the virtualenv is set up, if you figure out where that is. > These questions confounded me until I finally deleted Anaconda, and > Python with it, because when running programs in Windows vice the > Spyder IDE, Windows could find Python, but not the 3rd party packages > such as pyperclip, bs4, and requests. I suspect it had something to do > with the Anaconda installation process establishing a venv, and the > package locations not existing (or not being visible) in Windows. I don't think developers of projects like Anaconda expect that their target users will try to run Python lots of different ways, so getting things all under their control is helpful and presents a consistent front, which is really the value the project brings - you want to do some (for example) scientific work in Python, and some notes tell you you need to install these 17 packages, and if you're on Windows, a couple of them maybe don't work because you don't have a C compiler installed - Anaconda puts that all in once place and makes it easier. Doesn't mean it isn't still occasionally confusing. If you need to run more versions, you can always install pkgs a second time, "outside" the Anaconda world. It should even be quick to do, pip caches packages it has downloaded so it probably won't even need to re-download. p.s. Python can tell you which Python it is running: $ python Python 2.7.15 (default, Oct 15 2018, 15:24:06) [GCC 8.1.1 20180712 (Red Hat 8.1.1-5)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> sys.executable '/usr/bin/python' >>> $ source ~/virtualenv/py2/bin/activate $ python Python 2.7.15 (default, Oct 15 2018, 15:24:06) [GCC 8.1.1 20180712 (Red Hat 8.1.1-5)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> sys.executable '/home/mats/virtualenv/py2/bin/python' >>> From rba2124 at gmail.com Sun Nov 4 01:20:35 2018 From: rba2124 at gmail.com (Roger B. Atkins) Date: Sat, 3 Nov 2018 22:20:35 -0700 Subject: [Tutor] Installing python In-Reply-To: <1bb7bede-5051-5a4a-ced9-3e3bd6b0bdc1@wichmann.us> References: <2d3937d6-d558-0020-7e34-4f4b64c73dc0@wichmann.us> <1bb7bede-5051-5a4a-ced9-3e3bd6b0bdc1@wichmann.us> Message-ID: Thanks. That confirms some of my surmises. I may still have the Anaconda version on one of my notebook computers. I may use that to write and debug source code, since I like the Spyder IDE, and then copy working programs to my main desktop computer to run them in Windows with no virtual environment. On Sat, Nov 3, 2018 at 7:39 AM Mats Wichmann wrote: > > On 11/2/18 10:01 PM, Roger B. Atkins wrote: > > Okay, but what about the virtual environments? Once the Windows 10 > > path environment variable has a version of Python added to it, Windows > > will find it (I am using Win 10), but will it be found from within any > > given virtual environment (venv), regardless of which version created > > the environment? And if 3rd party packages have been installed within > > a venv, will they be found a) by Windows, and b) by the Python version > > within the venv, even if it was not used to install the packages? > > (a) when you activate a virtualenv, it will always find that python (the > one the env was created from), that's what the activation does - fiddle > paths so you see that envirnoment and not any other that may be sitting > around. > > (b) when you pip install with a virtualenv active the installation will > be private to that virtualenv. > > which leads to... Anaconda uses virtualenvs (afiak, _always_, rather > than "when you ask for one" like many IDEs do), so, packages installed > though Anaconda are in that sense "private". They're not secret - you > can see them all in the physical location where the virtualenv is set > up, if you figure out where that is. > > > These questions confounded me until I finally deleted Anaconda, and > > Python with it, because when running programs in Windows vice the > > Spyder IDE, Windows could find Python, but not the 3rd party packages > > such as pyperclip, bs4, and requests. I suspect it had something to do > > with the Anaconda installation process establishing a venv, and the > > package locations not existing (or not being visible) in Windows. > I don't think developers of projects like Anaconda expect that their > target users will try to run Python lots of different ways, so getting > things all under their control is helpful and presents a consistent > front, which is really the value the project brings - you want to do > some (for example) scientific work in Python, and some notes tell you > you need to install these 17 packages, and if you're on Windows, a > couple of them maybe don't work because you don't have a C compiler > installed - Anaconda puts that all in once place and makes it easier. > Doesn't mean it isn't still occasionally confusing. If you need to run > more versions, you can always install pkgs a second time, "outside" the > Anaconda world. It should even be quick to do, pip caches packages it > has downloaded so it probably won't even need to re-download. > > > p.s. Python can tell you which Python it is running: > > $ python > Python 2.7.15 (default, Oct 15 2018, 15:24:06) > [GCC 8.1.1 20180712 (Red Hat 8.1.1-5)] on linux2 > Type "help", "copyright", "credits" or "license" for more information. > >>> import sys > >>> sys.executable > '/usr/bin/python' > >>> > $ source ~/virtualenv/py2/bin/activate > $ python > Python 2.7.15 (default, Oct 15 2018, 15:24:06) > [GCC 8.1.1 20180712 (Red Hat 8.1.1-5)] on linux2 > Type "help", "copyright", "credits" or "license" for more information. > >>> import sys > >>> sys.executable > '/home/mats/virtualenv/py2/bin/python' > >>> > From asad.hasan2004 at gmail.com Tue Nov 6 08:13:12 2018 From: asad.hasan2004 at gmail.com (Asad) Date: Tue, 6 Nov 2018 18:43:12 +0530 Subject: [Tutor] Regex for Filesystem path Message-ID: Hi all , Can you provide some advice and code for the following problem : I have a logfile to check for errors : /a/b/c/d/test/test_2814__2018_10_05_12_12_45/logA.log f3 = open ( r"/a/b/c/d/test/test_2814__2018_10_05_12_12_45/logA.log", 'r' ) st1 = f3.readlines () from the above log I extract the directory location to determine the location of another log using the following regular expresssion: for j in range ( len ( st1 ) ): patchnumber = re.compile(r'(\d+)\/(\d+)') mo = patchnumber.search (st1[j-1]) a = mo.group() ## 123456/789 =================================================================== How to do I traverse to the required directory which is /a/b/c/d/test/123456/789 ? 1) First I need to extract /a/b/c/d/test/ from /a/b/c/d/test/test_2814__2018_10_05_12_12_45/logA.log ? 2) Then add 123456/789 and create directory location as /a/b/c/d/test/123456/789 3) cd /a/b/c/d/test/123456/789 4) look for the latest file in the directory /a/b/c/d/test/123456/789 5) print its content Please advice , -- Asad Hasan +91 9582111698 From alan.gauld at yahoo.co.uk Tue Nov 6 13:10:59 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 6 Nov 2018 18:10:59 +0000 Subject: [Tutor] Regex for Filesystem path In-Reply-To: References: Message-ID: On 06/11/2018 13:13, Asad wrote: > Can you provide some advice and code for the following problem : The first thing is to go read the documentation for the os.path module. It is designed for reliable path manipulation. > /a/b/c/d/test/test_2814__2018_10_05_12_12_45/logA.log > > f3 = open ( r"/a/b/c/d/test/test_2814__2018_10_05_12_12_45/logA.log", 'r' ) > st1 = f3.readlines () You hardly ever need readlines() any more, just iterate over the file, its much easier. > for j in range ( len ( st1 ) ): for line in f3: > patchnumber = re.compile(r'(\d+)\/(\d+)') > > mo = patchnumber.search (st1[j-1]) Are you sure that's right? For the first index (0) stl[j-1] will be stl[-1] which is the last line in the file. > a = mo.group() ## 123456/789 > =================================================================== > How to do I traverse to the required directory which is > /a/b/c/d/test/123456/789 ? You can use relative paths in os.chdir. So a payth of '..' will be one level up from the current directory. Of course you need to chdir to that directory first but os.path will tell you the dir you need. Or if its a hard coded path just store the /a/b/c/d/test/ in a variable. But I'm guessing that's too obvious so the path may vary? > 1) First I need to extract /a/b/c/d/test/ from > /a/b/c/d/test/test_2814__2018_10_05_12_12_45/logA.log ? get the dir then chdir to .. from there. > 2) Then add 123456/789 and create directory location as > /a/b/c/d/test/123456/789 Simple string manipulation or use the os.path functions. > 3) cd /a/b/c/d/test/123456/789 os.chdir() > 4) look for the latest file in the directory /a/b/c/d/test/123456/789 Slightly more complex, you need the creation timestamp. You can find that with os.path.getctime() (or several other options, eg os.stat) > 5) print its content standard file processing 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 srinivasan.rns at gmail.com Tue Nov 6 13:07:40 2018 From: srinivasan.rns at gmail.com (srinivasan) Date: Tue, 6 Nov 2018 23:37:40 +0530 Subject: [Tutor] SyntaxError: can't assign to literal while using ""blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % (fs)" using subprocess module in Python Message-ID: Dear Python Experts Team, As am newbie to python development, I am trying to use the below function to get verify the filesystem type of the SD card parition using bash command in python using subprocess module, I ma seeing the below Error "SyntaxError: can't assign to literal" *CODE:* *====* import helper from os import path import subprocess import os import otg_ni class emmc(object): """ emmc getters and setters info: https://www.kernel.org/doc/Documentation/cpu-freq/user-guide.txt """ def __init__(self): self._helper = helper.helper() self._otg_ni = otg_ni.otg_ni() *def get_fstype_of_mounted_partition(self, fs):* """ Get the filesystem type of the mounted partition. :partition_name : Partition path as string (e.g. /dev/mmcblk0p1) :return: filesystem type as string or None if not found """ * cmd = "blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % (fs)* *return self._helper.execute_cmd_output_string(cmd)* *def execute_cmd_output_string(self, cmd, enable_shell=False):* """ Execute a command and return its output as a string. :param cmd: abs path of the command with arguments :param enable_shell : force the cmd to be run as shell script :return: a string. """ try: result = subprocess.check_output(split(cmd), stderr=subprocess.STDOUT, shell=enable_shell) except subprocess.CalledProcessError as e: s = """While executing '{}' something went wrong. Return code == '{}' Return output:\n'{}' """.format(cmd, e.returncode, e.output, shell=enable_shell) raise AssertionError(s) return result.strip().decode("utf-8") *if __name__ == "__main__":* m = emmc() * m.get_fstype_of_mounted_partition("/dev/mmcblk0p1")* *Error:* *======* root:~/qa/test_library# python3 sd.py File "sd.py", line 99 * cmd = "blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % (fs)* * ^* *SyntaxError: can't assign to literal* root:~/qa/test_library# Kindly do the needful as early as possible, as am stuck with this issue from past 2 days no clues yet, please redirect me to the correct forum if this is not the right place for pasting python related queries Many Thanks in advance, Srini From david at graniteweb.com Tue Nov 6 13:33:59 2018 From: david at graniteweb.com (David Rock) Date: Tue, 6 Nov 2018 12:33:59 -0600 Subject: [Tutor] SyntaxError: can't assign to literal while using ""blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % (fs)" using subprocess module in Python In-Reply-To: References: Message-ID: > > *def get_fstype_of_mounted_partition(self, fs):* > """ > Get the filesystem type of the mounted partition. > > :partition_name : Partition path as string (e.g. /dev/mmcblk0p1) > :return: filesystem type as string or None if not found > """ > > * cmd = "blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % (fs)* > *return self._helper.execute_cmd_output_string(cmd)* > > > > root:~/qa/test_library# python3 sd.py > File "sd.py", line 99 > * cmd = "blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % (fs)* > * ^* > *SyntaxError: can't assign to literal* > root:~/qa/test_library# > looking at cmd = "blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % (fs)* It?s probably because you have ? characters that are inside ? characters and it can?t tell where the string ends. It looks like you are trying to do cmd = "blkid -o export %s | grep 'TYPE' | cut -d? = " -f3" % (fs)* which doesn?t make sense. Try using triple quotes instead so it?s clear what string you are trying to use. cmd = ?""blkid -o export %s | grep 'TYPE' | cut -d"=" -f3?"" % (fs) ? David Rock david at graniteweb.com From mats at wichmann.us Tue Nov 6 14:07:56 2018 From: mats at wichmann.us (Mats Wichmann) Date: Tue, 6 Nov 2018 12:07:56 -0700 Subject: [Tutor] Regex for Filesystem path In-Reply-To: References: Message-ID: <709f028a-f725-434c-23c5-c046bc8eef32@wichmann.us> >> 4) look for the latest file in the directory /a/b/c/d/test/123456/789 > > Slightly more complex, you need the creation timestamp. > You can find that with os.path.getctime() (or several > other options, eg os.stat) here's a trick you might be able to make use of: somelist = generate-list-of-files-in-directory newest = max(somelist, key=os.path.getctime) From cs at cskk.id.au Tue Nov 6 14:47:33 2018 From: cs at cskk.id.au (Cameron Simpson) Date: Wed, 7 Nov 2018 06:47:33 +1100 Subject: [Tutor] Regex for Filesystem path In-Reply-To: References: Message-ID: <20181106194733.GA44754@cskk.homeip.net> On 06Nov2018 18:10, Alan Gauld wrote: >On 06/11/2018 13:13, Asad wrote: > >> Can you provide some advice and code for the following problem : > >The first thing is to go read the documentation for the os.path module. >It is designed for reliable path manipulation. > >> /a/b/c/d/test/test_2814__2018_10_05_12_12_45/logA.log >> >> f3 = open ( r"/a/b/c/d/test/test_2814__2018_10_05_12_12_45/logA.log", 'r' ) >> st1 = f3.readlines () > >You hardly ever need readlines() any more, just iterate >over the file, its much easier. > >> for j in range ( len ( st1 ) ): > >for line in f3: Not to mention cheaper in memory usage. [...snip...] >> a = mo.group() ## 123456/789 >> =================================================================== >> How to do I traverse to the required directory which is >> /a/b/c/d/test/123456/789 ? > >You can use relative paths in os.chdir. >So a payth of '..' will be one level up from the current >directory. Of course you need to chdir to that directory first >but os.path will tell you the dir you need. It is better to just construct the required path. Chdir there requires a chdir back, and chdir affects all the relative paths your programme may be using. I'd use os.path.dirname to get '/a/b/c/d/test' and then just append to it with os.path.join to contruct each directory path. [...] >But I'm guessing that's too obvious so the path may vary? >> 1) First I need to extract /a/b/c/d/test/ from >> /a/b/c/d/test/test_2814__2018_10_05_12_12_45/logA.log ? Use os.path.dirname: # up the top from os.path import dirname, join # later testdir = dirname(logfile_path) >get the dir then chdir to .. from there. > >> 2) Then add 123456/789 and create directory location as >> /a/b/c/d/test/123456/789 > >Simple string manipulation or use the os.path functions. Eg dirpath = join(testdir, '123456/789') >> 3) cd /a/b/c/d/test/123456/789 > >os.chdir() I still recommend avoiding this. Just construct the full path to what you need. >> 4) look for the latest file in the directory /a/b/c/d/test/123456/789 > >Slightly more complex, you need the creation timestamp. >You can find that with os.path.getctime() (or several >other options, eg os.stat) Do not use ctime, it is _not_ "creation" time. It is "last change to inode" time. It _starts_ as creation time, but a chmod or even a link/unlink can change it: anything that changes the metadata. Generally people want mtime (last nmodified time), which is the last time the file data got changed. It is more meaningful. Cheers, Cameron Simpson From joseph.gulizia at gmail.com Tue Nov 6 16:50:42 2018 From: joseph.gulizia at gmail.com (Joseph Gulizia) Date: Tue, 6 Nov 2018 15:50:42 -0600 Subject: [Tutor] Request for help with code Message-ID: I'm using the bookazine "The Python Book" First Edition on pages 13-14 it gives the code (listed further below). It asks for user to state a given number of integers (for example 4)...then user enters integers. It doesn't stop seeking input after the number requested thereby creating an infinite loop. ----------------------------- CODE ----------------------------- # Python Book Page_13.py # Joe G. # several comment lines explain the code below it. # Re-typing is good practice # We're going to write a program that will ask the user to input an arbitrary # number of intergers, store them in a collection, and then demonstrate how the # collection would be used in various control structures. # Used for the sys.exit function import sys # Requests number of intergers target_int=raw_input("How many intergers?") # By now, the variable target_int contains a string representtion of # whatever the user typed. We need to try and convert that to an interger but # be ready to # deal with the error if it's not. Otherwise the program will # crash # Begin the error check try: target_int=int(target_int) except ValueError: sys.exit("You must enter an interger") # creates a collection (list) called ints ints=list() # keeps track of number of intergers count=0 # Keep asking for an interger until we have the required number while count References: Message-ID: On Nov 6, 2018 4:51 PM, "Joseph Gulizia" wrote: > > I'm using the bookazine "The Python Book" First Edition on pages 13-14 it > gives the code (listed further below). > > It asks for user to state a given number of integers (for example 4)...then > user enters integers. It doesn't stop seeking input after the number > requested thereby creating an infinite loop. > > ----------------------------- > CODE > ----------------------------- > > # Python Book Page_13.py > # Joe G. > > # several comment lines explain the code below it. > # Re-typing is good practice > > # We're going to write a program that will ask the user to input an > arbitrary > # number of intergers, store them in a collection, and then demonstrate how > the > # collection would be used in various control structures. > > # Used for the sys.exit function > import sys > # Requests number of intergers > target_int=raw_input("How many intergers?") > # By now, the variable target_int contains a string representtion of > # whatever the user typed. We need to try and convert that to an interger > but > # be ready to # deal with the error if it's not. Otherwise the program will > # crash > # Begin the error check > try: > target_int=int(target_int) > except ValueError: > sys.exit("You must enter an interger") > # creates a collection (list) called ints > ints=list() > # keeps track of number of intergers > count=0 > # Keep asking for an interger until we have the required number > while count new_int=raw_input("Please enter interger{0}:".format(count+1)) > isint=False > try: > new_int=int(new_int) > except: > print("You must enter an interger") > # Only carry on if we have an interger. If not, we'll loop again > # Notice below I use == which is different from =. The single equals sign > is an > # assignment operator whereas the double equals sign is a comparison > operator. I would > # call it a married eguals sign....but whenever single is mentioned I have > to mention marriage. > > if isint==True: > # Add the interger to the collection > ints.append(new_int) > # Increment the count by 1 > count+=1 > # print statement ("using a for loop") > print("Using a for loop") > for value in ints: > print(str(value)) > # Or with a while loop: > print("Using a while loop") > # We already have the total above, but knowing the len function is very > # useful. > total = len(ints) > count = 0 > while count < total: > print(str(ints[count])) > count +=1 > > count = 0 > while count < total: > print(str(ints[count])) > count += 1 > > ------------------------------- > END OF CODE > ------------------------------- > Sample output: > > How many integers?3 > Please enter integer1:1 > Please enter integer1:2 > Please enter integer1:3 > Please enter integer1:a > You must enter an integer > Please enter integer1:4 > Please enter integer1:5 > Please enter integer1:6 > Please enter integer1:b > You must enter an integer > Please enter integer1: > (Keeps Looping) > > Thanks in advance Your code came through with all of the indentation removed. Please be sure to send plain text. We could guess at the indentation but we might get it wrong. With the indentation corrected the code you supplied does not agree with the execution. Note the difference between integer and interger. You also did not make any request. Please in the future tell us what you want from us. The fact that the program keeps asking for integer 1 suggest that count is not being incremented. This would also explain why the loop never ends. From steve at pearwood.info Tue Nov 6 17:48:05 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 7 Nov 2018 09:48:05 +1100 Subject: [Tutor] Request for help with code In-Reply-To: References: Message-ID: <20181106224804.GB4071@ando.pearwood.info> On Tue, Nov 06, 2018 at 03:50:42PM -0600, Joseph Gulizia wrote: > I'm using the bookazine "The Python Book" First Edition on pages 13-14 it > gives the code (listed further below). > > It asks for user to state a given number of integers (for example 4)...then > user enters integers. It doesn't stop seeking input after the number > requested thereby creating an infinite loop. The first and most important rule for asking for help with code is to always post plain, unformatted text, not "Rich Text" (formatted text, styled text). Don't add colours, highlighting, bold, italic, fancy fonts, dancing paperclips or anything else, because when you do, it destroys the necessary indentation of the code and makes it impossible to run or debug. For example, your code shows up like this: # Begin the error check try: target_int=int(target_int) except ValueError: sys.exit("You must enter an interger") In that case, we could(?) guess the correct indentation, but (1) sometimes we can't, and (2) we shouldn't have to. The *second* most important rule for posting code is to *keep it simple*. We're volunteers, not slaves, and you should always ask first before posting masses of code for us to investigate. Anything more than, oh, say, thirty lines (including comments) counts as masses. Please try reading this: http://sscce.org/ it is written for Java programmers but the advice applies as well here. Before posting your entire code, take the time to simplify it to a *mimimal example* which demonstrates the same problem. Half the time this will allow you to solve the problem yourself, and the other half of the time, it makes it easier on us slaves! I mean volunteers. At a *guess*, and this is purely a guess because the lack of indentation makes it impossible to understand the structure of your code, I think the infinite loop is probably intentional. If you guess wrongly, it asks you to guess again, *forever*, until you guess correctly. But as I said, I can't really tell. -- Steve From joseph.gulizia at gmail.com Tue Nov 6 18:15:40 2018 From: joseph.gulizia at gmail.com (Joseph Gulizia) Date: Tue, 6 Nov 2018 17:15:40 -0600 Subject: [Tutor] Request for help with code In-Reply-To: References: Message-ID: Apologies for earlier errors when asking for help -- I am hopeful that this shortened post displays properly: The code is Python 2... trying to solve why loop doesn't stop at given number of integers....if I input request for 3 integers....it keeps asking for integer1...and keeps asking for input,,,so count isn't working....trying to figure out why. Pointers please. import sys target_int=raw_input("How many integers?") try: target_int=int(target_int) except ValueError: sys.exit("You must enter an integer") # creates a collection (list) called ints ints=list() # keeps track of number of integers count=0 # Keep asking for an integer until we have the required number while count wrote: > I'm using the bookazine "The Python Book" First Edition on pages 13-14 it > gives the code (listed further below). > > It asks for user to state a given number of integers (for example > 4)...then user enters integers. It doesn't stop seeking input after the > number requested thereby creating an infinite loop. > > ----------------------------- > CODE > ----------------------------- > > # Python Book Page_13.py > # Joe G. > > # several comment lines explain the code below it. > # Re-typing is good practice > > # We're going to write a program that will ask the user to input an > arbitrary > # number of intergers, store them in a collection, and then demonstrate > how the > # collection would be used in various control structures. > > # Used for the sys.exit function > import sys > # Requests number of intergers > target_int=raw_input("How many intergers?") > # By now, the variable target_int contains a string representtion of > # whatever the user typed. We need to try and convert that to an interger > but > # be ready to # deal with the error if it's not. Otherwise the program > will > # crash > # Begin the error check > try: > target_int=int(target_int) > except ValueError: > sys.exit("You must enter an interger") > # creates a collection (list) called ints > ints=list() > # keeps track of number of intergers > count=0 > # Keep asking for an interger until we have the required number > while count new_int=raw_input("Please enter interger{0}:".format(count+1)) > isint=False > try: > new_int=int(new_int) > except: > print("You must enter an interger") > # Only carry on if we have an interger. If not, we'll loop again > # Notice below I use == which is different from =. The single equals sign > is an > # assignment operator whereas the double equals sign is a comparison > operator. I would > # call it a married eguals sign....but whenever single is mentioned I have > to mention marriage. > > if isint==True: > # Add the interger to the collection > ints.append(new_int) > # Increment the count by 1 > count+=1 > # print statement ("using a for loop") > print("Using a for loop") > for value in ints: > print(str(value)) > # Or with a while loop: > print("Using a while loop") > # We already have the total above, but knowing the len function is very > # useful. > total = len(ints) > count = 0 > while count < total: > print(str(ints[count])) > count +=1 > > count = 0 > while count < total: > print(str(ints[count])) > count += 1 > > ------------------------------- > END OF CODE > ------------------------------- > Sample output: > > How many integers?3 > Please enter integer1:1 > Please enter integer1:2 > Please enter integer1:3 > Please enter integer1:a > You must enter an integer > Please enter integer1:4 > Please enter integer1:5 > Please enter integer1:6 > Please enter integer1:b > You must enter an integer > Please enter integer1: > (Keeps Looping) > > Thanks in advance > Joe > From joel.goldstick at gmail.com Tue Nov 6 18:32:25 2018 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Tue, 6 Nov 2018 18:32:25 -0500 Subject: [Tutor] Request for help with code In-Reply-To: References: Message-ID: On Tue, Nov 6, 2018 at 6:17 PM Joseph Gulizia wrote: > > Apologies for earlier errors when asking for help -- I am hopeful that this > shortened post displays properly: > The code is Python 2... trying to solve why loop doesn't stop at given > number of integers....if I input request for 3 integers....it keeps asking > for integer1...and keeps asking for input,,,so count isn't > working....trying to figure out why. Pointers please. > > import sys > > target_int=raw_input("How many integers?") > > try: > target_int=int(target_int) > except ValueError: > sys.exit("You must enter an integer") > > # creates a collection (list) called ints > > ints=list() > > # keeps track of number of integers > > count=0 > > # Keep asking for an integer until we have the required number > > while count new_int=raw_input("Please enter integer{0}:".format(count+1)) > isint=False > try: > new_int=int(new_int) > except: > print("You must enter an integer") > ----------------- > > > On Tue, Nov 6, 2018 at 3:50 PM Joseph Gulizia > wrote: > > > I'm using the bookazine "The Python Book" First Edition on pages 13-14 it > > gives the code (listed further below). > > > > It asks for user to state a given number of integers (for example > > 4)...then user enters integers. It doesn't stop seeking input after the > > number requested thereby creating an infinite loop. > > > > ----------------------------- > > CODE > > ----------------------------- > > > > # Python Book Page_13.py > > # Joe G. > > > > # several comment lines explain the code below it. > > # Re-typing is good practice > > > > # We're going to write a program that will ask the user to input an > > arbitrary > > # number of intergers, store them in a collection, and then demonstrate > > how the > > # collection would be used in various control structures. > > > > # Used for the sys.exit function > > import sys > > # Requests number of intergers > > target_int=raw_input("How many intergers?") > > # By now, the variable target_int contains a string representtion of > > # whatever the user typed. We need to try and convert that to an interger > > but > > # be ready to # deal with the error if it's not. Otherwise the program > > will > > # crash > > # Begin the error check > > try: > > target_int=int(target_int) > > except ValueError: > > sys.exit("You must enter an interger") > > # creates a collection (list) called ints > > ints=list() > > # keeps track of number of intergers > > count=0 > > # Keep asking for an interger until we have the required number > > while count > new_int=raw_input("Please enter interger{0}:".format(count+1)) > > isint=False > > try: > > new_int=int(new_int) > > except: > > print("You must enter an interger") > > # Only carry on if we have an interger. If not, we'll loop again > > # Notice below I use == which is different from =. The single equals sign > > is an > > # assignment operator whereas the double equals sign is a comparison > > operator. I would > > # call it a married eguals sign....but whenever single is mentioned I have > > to mention marriage. > > > > if isint==True: > > # Add the interger to the collection > > ints.append(new_int) > > # Increment the count by 1 > > count+=1 > > # print statement ("using a for loop") > > print("Using a for loop") > > for value in ints: > > print(str(value)) > > # Or with a while loop: > > print("Using a while loop") > > # We already have the total above, but knowing the len function is very > > # useful. > > total = len(ints) > > count = 0 > > while count < total: > > print(str(ints[count])) > > count +=1 > > > > count = 0 > > while count < total: > > print(str(ints[count])) > > count += 1 > > > > ------------------------------- > > END OF CODE > > ------------------------------- > > Sample output: > > > > How many integers?3 > > Please enter integer1:1 > > Please enter integer1:2 > > Please enter integer1:3 > > Please enter integer1:a > > You must enter an integer > > Please enter integer1:4 > > Please enter integer1:5 > > Please enter integer1:6 > > Please enter integer1:b > > You must enter an integer > > Please enter integer1: > > (Keeps Looping) > > > > Thanks in advance > > Joe > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor still not indented.. sorry.. figure that out -- Joel Goldstick http://joelgoldstick.com/blog http://cc-baseballstats.info/stats/birthdays From joseph.gulizia at gmail.com Tue Nov 6 18:36:32 2018 From: joseph.gulizia at gmail.com (Joseph Gulizia) Date: Tue, 6 Nov 2018 17:36:32 -0600 Subject: [Tutor] Request for help with code In-Reply-To: References: Message-ID: Funny.... using a text editor....and showed indented in my browser. Won't bother the list again. On Tue, Nov 6, 2018, 17:32 Joel Goldstick On Tue, Nov 6, 2018 at 6:17 PM Joseph Gulizia > wrote: > > > > Apologies for earlier errors when asking for help -- I am hopeful that > this > > shortened post displays properly: > > The code is Python 2... trying to solve why loop doesn't stop at given > > number of integers....if I input request for 3 integers....it keeps > asking > > for integer1...and keeps asking for input,,,so count isn't > > working....trying to figure out why. Pointers please. > > > > import sys > > > > target_int=raw_input("How many integers?") > > > > try: > > target_int=int(target_int) > > except ValueError: > > sys.exit("You must enter an integer") > > > > # creates a collection (list) called ints > > > > ints=list() > > > > # keeps track of number of integers > > > > count=0 > > > > # Keep asking for an integer until we have the required number > > > > while count > new_int=raw_input("Please enter integer{0}:".format(count+1)) > > isint=False > > try: > > new_int=int(new_int) > > except: > > print("You must enter an integer") > > ----------------- > > > > > > On Tue, Nov 6, 2018 at 3:50 PM Joseph Gulizia > > wrote: > > > > > I'm using the bookazine "The Python Book" First Edition on pages > 13-14 it > > > gives the code (listed further below). > > > > > > It asks for user to state a given number of integers (for example > > > 4)...then user enters integers. It doesn't stop seeking input after > the > > > number requested thereby creating an infinite loop. > > > > > > ----------------------------- > > > CODE > > > ----------------------------- > > > > > > # Python Book Page_13.py > > > # Joe G. > > > > > > # several comment lines explain the code below it. > > > # Re-typing is good practice > > > > > > # We're going to write a program that will ask the user to input an > > > arbitrary > > > # number of intergers, store them in a collection, and then demonstrate > > > how the > > > # collection would be used in various control structures. > > > > > > # Used for the sys.exit function > > > import sys > > > # Requests number of intergers > > > target_int=raw_input("How many intergers?") > > > # By now, the variable target_int contains a string representtion of > > > # whatever the user typed. We need to try and convert that to an > interger > > > but > > > # be ready to # deal with the error if it's not. Otherwise the program > > > will > > > # crash > > > # Begin the error check > > > try: > > > target_int=int(target_int) > > > except ValueError: > > > sys.exit("You must enter an interger") > > > # creates a collection (list) called ints > > > ints=list() > > > # keeps track of number of intergers > > > count=0 > > > # Keep asking for an interger until we have the required number > > > while count > > new_int=raw_input("Please enter interger{0}:".format(count+1)) > > > isint=False > > > try: > > > new_int=int(new_int) > > > except: > > > print("You must enter an interger") > > > # Only carry on if we have an interger. If not, we'll loop again > > > # Notice below I use == which is different from =. The single equals > sign > > > is an > > > # assignment operator whereas the double equals sign is a comparison > > > operator. I would > > > # call it a married eguals sign....but whenever single is mentioned I > have > > > to mention marriage. > > > > > > if isint==True: > > > # Add the interger to the collection > > > ints.append(new_int) > > > # Increment the count by 1 > > > count+=1 > > > # print statement ("using a for loop") > > > print("Using a for loop") > > > for value in ints: > > > print(str(value)) > > > # Or with a while loop: > > > print("Using a while loop") > > > # We already have the total above, but knowing the len function is very > > > # useful. > > > total = len(ints) > > > count = 0 > > > while count < total: > > > print(str(ints[count])) > > > count +=1 > > > > > > count = 0 > > > while count < total: > > > print(str(ints[count])) > > > count += 1 > > > > > > ------------------------------- > > > END OF CODE > > > ------------------------------- > > > Sample output: > > > > > > How many integers?3 > > > Please enter integer1:1 > > > Please enter integer1:2 > > > Please enter integer1:3 > > > Please enter integer1:a > > > You must enter an integer > > > Please enter integer1:4 > > > Please enter integer1:5 > > > Please enter integer1:6 > > > Please enter integer1:b > > > You must enter an integer > > > Please enter integer1: > > > (Keeps Looping) > > > > > > Thanks in advance > > > Joe > > > > > _______________________________________________ > > Tutor maillist - Tutor at python.org > > To unsubscribe or change subscription options: > > https://mail.python.org/mailman/listinfo/tutor > > still not indented.. sorry.. figure that out > > -- > Joel Goldstick > http://joelgoldstick.com/blog > http://cc-baseballstats.info/stats/birthdays > From cs at cskk.id.au Tue Nov 6 18:50:57 2018 From: cs at cskk.id.au (Cameron Simpson) Date: Wed, 7 Nov 2018 10:50:57 +1100 Subject: [Tutor] Request for help with code In-Reply-To: References: Message-ID: <20181106235057.GA86245@cskk.homeip.net> On 06Nov2018 15:50, Joseph Gulizia I'm using the bookazine "The Python Book" First Edition on pages 13-14 it >gives the code (listed further below). > >It asks for user to state a given number of integers (for example 4)...then >user enters integers. It doesn't stop seeking input after the number >requested thereby creating an infinite loop. It is vital to preserve the indenting when pasting in code. Indent level is critical to Python's control flow. I'm going to look anyway, but without the indenting I may misinterpret the logic. [...snip...] # creates a collection (list) called ints ints=list() # keeps track of number of intergers count=0 # Keep asking for an interger until we have the required number while count", count) You should see that the expected code is actually reached and run, and if it isn't, the corresponding print()s do not happen. Cheers, Cameron Simpson From mats at wichmann.us Tue Nov 6 18:54:27 2018 From: mats at wichmann.us (Mats Wichmann) Date: Tue, 6 Nov 2018 16:54:27 -0700 Subject: [Tutor] Request for help with code In-Reply-To: References: Message-ID: On 11/6/18 4:36 PM, Joseph Gulizia wrote: > Funny.... using a text editor....and showed indented in my browser. Won't > bother the list again. We don't want you to "not bother" us, just hoping to get things in a state where we can actually help... here's what we end up seeing: https://www.mail-archive.com/tutor at python.org/msg79222.html https://www.mail-archive.com/tutor at python.org/msg79225.html since unlike most other languages, indentation is a crucial part of the syntax, we tend to grumble. there must be some solution... guys, do you know of any alternate way to send a message to the list if a mail client isn't cooperating? or do we have instructions for beating gmail into submission? Obviously gmail is a key part of the modern infrastructure despite its (ahem) misfeatures. From david at graniteweb.com Tue Nov 6 19:10:43 2018 From: david at graniteweb.com (David Rock) Date: Tue, 6 Nov 2018 18:10:43 -0600 Subject: [Tutor] Request for help with code In-Reply-To: References: Message-ID: <20181107001043.GF26482@apple.graniteweb.com> * Mats Wichmann [2018-11-06 16:54]: > > there must be some solution... guys, do you know of any alternate way to > send a message to the list if a mail client isn't cooperating? or do we > have instructions for beating gmail into submission? Obviously gmail is > a key part of the modern infrastructure despite its (ahem) misfeatures. Looking at the mail headers, I'm seeing Content-Type: text/plain; charset="us-ascii" But that is probably just what the final format that gets sent to the list ends up with. We might have some suggestions if we know for sure what email client is being used. A gmail.com address is not a guarantee that gmail within a browser is what's being used (I use mutt to send mail through my gmail.com account all the time). What's the MUA (mail client)? If it's in a browser, what browser is being used? Off the top, I would suggest looking for a "plain text" selection on a dropdown. -- David Rock david at graniteweb.com From alan.gauld at yahoo.co.uk Tue Nov 6 19:23:27 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 7 Nov 2018 00:23:27 +0000 Subject: [Tutor] Regex for Filesystem path In-Reply-To: <20181106194733.GA44754@cskk.homeip.net> References: <20181106194733.GA44754@cskk.homeip.net> Message-ID: On 06/11/2018 19:47, Cameron Simpson wrote: > It is better to just construct the required path. Chdir there requires a > chdir back, and chdir affects all the relative paths your programme may > be using. > > I'd use os.path.dirname to get '/a/b/c/d/test' and then just append to > it with os.path.join to contruct each directory path. That was my original thought but the OP had his long path one below test so would need to strip one level off the dirname result. That's why I opted for chdir as the simplest solution(albeit not very efficient) > Do not use ctime, it is _not_ "creation" time. It is "last change to > inode" time. It _starts_ as creation time, but a chmod or even a > link/unlink can change it: anything that changes the metadata. Good catch, I should have known that. My bad. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alan.gauld at yahoo.co.uk Tue Nov 6 19:37:25 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 7 Nov 2018 00:37:25 +0000 Subject: [Tutor] SyntaxError: can't assign to literal while using ""blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % (fs)" using subprocess module in Python In-Reply-To: References: Message-ID: On 06/11/2018 18:07, srinivasan wrote: > bash command in python using subprocess module, I ma seeing the below > *? ? ? ? cmd = "blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % (fs)* In general you should try to do as little as possible using bash and subprocess. Especially try to avoid long pipelines since you are starting a new OS process for every element in the pipeline. That means, in your case, you are running 4 processes to get your result - Python, blkid, grep and cut Python is designed to do much of what the shell command can do almost as easily and much more efficiently (no new processes being started). In this case just execute the blkid bit in bash because its too difficult to replicate simply in Python. Then use Python to search for the TYPE lines and slice them to size. That will, in turn, simplify your command string and remove the issue of multiple quotes. -- 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 cameron at cskk.homeip.net Tue Nov 6 17:06:41 2018 From: cameron at cskk.homeip.net (Cameron Simpson) Date: Wed, 7 Nov 2018 09:06:41 +1100 Subject: [Tutor] Request for help with code Message-ID: <20181106220641.GA81934@cskk.homeip.net> On 06Nov2018 15:50, Joseph Gulizia ", count) You should see that the expected code is actually reached and run, and if it isn't, the corresponding print()s do not happen. Cheers, Cameron Simpson From sarfraaz at gmail.com Tue Nov 6 13:13:23 2018 From: sarfraaz at gmail.com (Sarfraaz Ahmed) Date: Tue, 6 Nov 2018 23:43:23 +0530 Subject: [Tutor] Regex for Filesystem path In-Reply-To: References: Message-ID: Hello, There are specific operating system, path related modules in Python for handling these scenarios. You could try looking at os.path module. On Tue, Nov 6, 2018 at 11:16 PM Asad wrote: > Hi all , > > Can you provide some advice and code for the following problem : > > I have a logfile to check for errors : > > /a/b/c/d/test/test_2814__2018_10_05_12_12_45/logA.log > > f3 = open ( r"/a/b/c/d/test/test_2814__2018_10_05_12_12_45/logA.log", 'r' ) > st1 = f3.readlines () > > from the above log I extract the directory location to determine the > location of another log using the following regular expresssion: > Specifically, try exploring "dirname" function of os.path module. That might come in handy for your situation. -- Thanks -- Sarfraaz Ahmed From steve at pearwood.info Wed Nov 7 02:52:48 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 7 Nov 2018 18:52:48 +1100 Subject: [Tutor] Request for help with code In-Reply-To: References: Message-ID: <20181107075247.GD4071@ando.pearwood.info> On Tue, Nov 06, 2018 at 06:32:25PM -0500, Joel Goldstick wrote: [snip nearly seven screens of quoted text] > still not indented.. sorry.. figure that out Hey Joel, is the Backspace key on your keyboard broken? :-) This sort of mass (and unnecessary) quoting is why Bottom-Posting gets a bad reputation. The reader shouldn't have to scroll through 6+ screenfuls of text to see your one, solitary comment. Please snip the quoted text to the minimum needed to establish context. If your mail client doesn't allow deleting the quoted text (and you can't get a better mail client!) it is better to Top-Post (heresy!), provided your signature shows up above the quoting so we know to stop reading. Thanks in advance, -- Steve From adameyring at gmail.com Tue Nov 6 20:18:57 2018 From: adameyring at gmail.com (Adam Eyring) Date: Tue, 6 Nov 2018 20:18:57 -0500 Subject: [Tutor] Request for help with code In-Reply-To: References: Message-ID: When I post code with questions, I just copy and paste from Python IDLE 3.6. Colors are removed, but indentation is preserved. On Tue, Nov 6, 2018 at 6:59 PM Mats Wichmann wrote: > On 11/6/18 4:36 PM, Joseph Gulizia wrote: > > Funny.... using a text editor....and showed indented in my browser. > Won't > > bother the list again. > > We don't want you to "not bother" us, just hoping to get things in a > state where we can actually help... > > here's what we end up seeing: > > https://www.mail-archive.com/tutor at python.org/msg79222.html > https://www.mail-archive.com/tutor at python.org/msg79225.html > > since unlike most other languages, indentation is a crucial part of the > syntax, we tend to grumble. > > there must be some solution... guys, do you know of any alternate way to > send a message to the list if a mail client isn't cooperating? or do we > have instructions for beating gmail into submission? Obviously gmail is > a key part of the modern infrastructure despite its (ahem) misfeatures. > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From avigross at verizon.net Tue Nov 6 22:20:52 2018 From: avigross at verizon.net (Avi Gross) Date: Tue, 6 Nov 2018 22:20:52 -0500 Subject: [Tutor] SyntaxError: can't assign to literal while using ""blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % (fs)" using subprocess module in Python In-Reply-To: References: Message-ID: <007301d47648$e3f2f3a0$abd8dae0$@verizon.net> I may be missing something but it looks like the embedded double quotes may be a problem in this: cmd = "blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % ... if you use single quotes as in: cut -d"=" becomes cut -d'=' or escape the double quotes with \" and so on ... The above seems to be seen as: cmd=ALPHA=BETA % ... where ALPHA="blkid -o export %s | grep 'TYPE' | cut -d" and BETA=" -f3" Other issues may also be there. -----Original Message----- From: Tutor On Behalf Of Alan Gauld via Tutor Sent: Tuesday, November 6, 2018 7:37 PM To: tutor at python.org Cc: python-dev at python.org Subject: Re: [Tutor] SyntaxError: can't assign to literal while using ""blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % (fs)" using subprocess module in Python On 06/11/2018 18:07, srinivasan wrote: > bash command in python using subprocess module, I ma seeing the below > * cmd = "blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % > (fs)* In general you should try to do as little as possible using bash and subprocess. Especially try to avoid long pipelines since you are starting a new OS process for every element in the pipeline. That means, in your case, you are running 4 processes to get your result - Python, blkid, grep and cut Python is designed to do much of what the shell command can do almost as easily and much more efficiently (no new processes being started). In this case just execute the blkid bit in bash because its too difficult to replicate simply in Python. Then use Python to search for the TYPE lines and slice them to size. That will, in turn, simplify your command string and remove the issue of multiple quotes. -- 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 sjeik_appie at hotmail.com Wed Nov 7 09:48:40 2018 From: sjeik_appie at hotmail.com (Albert-Jan Roskam) Date: Wed, 7 Nov 2018 14:48:40 +0000 Subject: [Tutor] best way to dynamically set class variables? Message-ID: Hi, Background: In my code I use sqlalchemy to read SQL server data. I want to write that data to HDF5 using pytables (see 'declaring a column descriptor': https://www.pytables.org/usersguide/tutorials.html). My question is not about pytables or sqlalchemy per se, but I thought it would be informative to mention this. What is the best way to dynamically set class variables? I am looking for a generalization of something like this: class Parent: pass class Child(Parent): col1 = 'str' col2 = 'int' Several (im)possible solutions: # ------- class Parent: pass class_vars = dict(col1='str', col2='int') # approach 1 Child = type('Child', (Parent,), class_vars) # approach 2 class Child(Parent): pass Child.__dict__.update( class_vars ) # AttributeError: 'mappingproxy' object has no attribute 'update' # approach 3 class Child(Parent): pass for k, v in class_vars.items(): setattr(Child, k, v) I initially chose approach #1, but I find this way of defining a class quite cryptic (but then, it's part of the language definition!). What's the best way to do this? I am using Python 3.5 (Windows). Thanks in advance! Best wishes, Albert-Jan From alan.gauld at yahoo.co.uk Wed Nov 7 13:33:20 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 7 Nov 2018 18:33:20 +0000 Subject: [Tutor] best way to dynamically set class variables? In-Reply-To: References: Message-ID: On 07/11/2018 14:48, Albert-Jan Roskam wrote: > What is the best way to dynamically set class variables? Remember the golden rule of OOP is that objects(and classes) should do it to themselves. Ideally the class variables should be there to support some kind of class behaviour and that behaviour should set the variables. (Reading is arguably different, it's usually OK for external objects to "grab a value" from a class. But if you are modifying it from outside then who is doing what and should that "what" not be done by the class (maybe in a class method)? Having said that, if you can find a valid scenario where objects/functions outside the class need to modify the internals of the class directly then directly is how they should do it. class C: classvar = 'foo' x = C.classvar # read directly C.classvar = 'bar' # assign directly I am looking for a generalization of something like this: > > class Parent: pass > class Child(Parent): > col1 = 'str' > col2 = 'int' What's not general about that? I think I'm maybe missing the point of your question? > # ------- > class Parent: pass > class_vars = dict(col1='str', col2='int') > > # approach 1 > Child = type('Child', (Parent,), class_vars) > > # approach 2 > class Child(Parent): pass > Child.__dict__.update( class_vars ) # AttributeError: 'mappingproxy' object has no attribute 'update' > > # approach 3 > class Child(Parent): pass > for k, v in class_vars.items(): > setattr(Child, k, v) That all seems incredibly complicated and I'm not sure what it would buy you over direct assignment? -- 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 asad.hasan2004 at gmail.com Wed Nov 7 10:56:52 2018 From: asad.hasan2004 at gmail.com (Asad) Date: Wed, 7 Nov 2018 21:26:52 +0530 Subject: [Tutor] Regex for Filesystem path (Asad) In-Reply-To: References: Message-ID: Hi All, I tired seems its not working as required : from os.path import dirname, join testdir = dirname("/a/b/c/d/test/test_2814__2018_10_05_12_12_45/logA.log") dirpath = join(testdir, '123456/789') print dirpath /a/b/c/d/test/test_2814__2018_10_05_12_12_45\123456/789 Instead i need the script to go to the location : /a/b/c/d/test/123456/789 Please advice . Thanks, > ---------- Forwarded message ---------- > From: Cameron Simpson > To: tutor at python.org > Cc: > Bcc: > Date: Wed, 7 Nov 2018 06:47:33 +1100 > Subject: Re: [Tutor] Regex for Filesystem path > On 06Nov2018 18:10, Alan Gauld wrote: > >On 06/11/2018 13:13, Asad wrote: > > > >> Can you provide some advice and code for the following problem : > > > >The first thing is to go read the documentation for the os.path module. > >It is designed for reliable path manipulation. > > > >> /a/b/c/d/test/test_2814__2018_10_05_12_12_45/logA.log > >> > >> f3 = open ( r"/a/b/c/d/test/test_2814__2018_10_05_12_12_45/logA.log", > 'r' ) > >> st1 = f3.readlines () > > > >You hardly ever need readlines() any more, just iterate > >over the file, its much easier. > > > >> for j in range ( len ( st1 ) ): > > > >for line in f3: > > Not to mention cheaper in memory usage. > > [...snip...] > >> a = mo.group() ## 123456/789 > >> =================================================================== > >> How to do I traverse to the required directory which is > >> /a/b/c/d/test/123456/789 ? > > > >You can use relative paths in os.chdir. > >So a payth of '..' will be one level up from the current > >directory. Of course you need to chdir to that directory first > >but os.path will tell you the dir you need. > > It is better to just construct the required path. Chdir there requires a > chdir back, and chdir affects all the relative paths your programme may > be using. > > I'd use os.path.dirname to get '/a/b/c/d/test' and then just append to > it with os.path.join to contruct each directory path. > > [...] > >But I'm guessing that's too obvious so the path may vary? > >> 1) First I need to extract /a/b/c/d/test/ from > >> /a/b/c/d/test/test_2814__2018_10_05_12_12_45/logA.log ? > > Use os.path.dirname: > > # up the top > from os.path import dirname, join > > # later > testdir = dirname(logfile_path) > > >get the dir then chdir to .. from there. > > > >> 2) Then add 123456/789 and create directory location as > >> /a/b/c/d/test/123456/789 > > > >Simple string manipulation or use the os.path functions. > > Eg dirpath = join(testdir, '123456/789') > > >> 3) cd /a/b/c/d/test/123456/789 > > > >os.chdir() > > I still recommend avoiding this. Just construct the full path to what > you need. > > >> 4) look for the latest file in the directory > /a/b/c/d/test/123456/789 > > > >Slightly more complex, you need the creation timestamp. > >You can find that with os.path.getctime() (or several > >other options, eg os.stat) > > Do not use ctime, it is _not_ "creation" time. It is "last change to > inode" time. It _starts_ as creation time, but a chmod or even a > link/unlink can change it: anything that changes the metadata. > > Generally people want mtime (last nmodified time), which is the last > time the file data got changed. It is more meaningful. > > Cheers, > Cameron Simpson > > > > > -- Asad Hasan +91 9582111698 From brian.j.oney at googlemail.com Wed Nov 7 05:10:01 2018 From: brian.j.oney at googlemail.com (Brian J. Oney) Date: Wed, 07 Nov 2018 11:10:01 +0100 Subject: [Tutor] SyntaxError: can't assign to literal while using ""blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % (fs)" using subprocess module in Python In-Reply-To: References: <007301d47648$e3f2f3a0$abd8dae0$@verizon.net> Message-ID: <1541585401.1731.1.camel@gmail.com> On Wed, 2018-11-07 at 10:22 +0100, srinivasan wrote: > blkid -o export %s | grep \'TYPE\' | cut -d\"=\" -f3 You don't need to escape the single quotes. Try either: "blkid -o export %s | grep 'TYPE' | cut -d'=' -f3" or: 'blkid -o export %s | grep "TYPE" | cut -d"=" -f3' or: "blkid -o export %s | grep \"TYPE\" | cut -d\"=\" -f3" HTH From dave at the-hills.org.uk Wed Nov 7 12:22:44 2018 From: dave at the-hills.org.uk (Dave Hill) Date: Wed, 7 Nov 2018 17:22:44 +0000 Subject: [Tutor] Is there a better way Message-ID: <8805d57f-50f0-178a-35aa-3932f098a5ae@the-hills.org.uk> I have an sqlite database as a source {Asset and Test data from a Megger PAT Tester}, which contains a number of tables, and I end up with an OpenOffice spreadsheet, with numerous sheets The data is proceesed in three parts, each using a separate Class. I extract the useful tables, and store them as '.csv' versions. {Class#1} I then need to correlate data from various tables, and sort into locations, again storing them as '.csv' versions. {Class#2} Finally, I write all of the locations as separate sheets in an OpenOffice spreadsheet. {Class#3} At this final stage I am converting table index numbers to the relevant string. For example, from the '.csv' file the : TestGroupIndex [1] => Test Group ['SC2'] '.csv' file for a location AssetIndex AssetId Description TestGroupIndex RetestFrequency 106 43 Router 1 24 164 25 500 Drill 8 24 167 26 110v Ext Lead 11 24 173 37 Router DW625E 1 24 180 47 Vacuum 1 24 181 48 110v Ext 11 24 sheet from '.ods' file Asset Asset Test Test Next Index Id Description Group Freq Test 106 43 Router ['SC2'] 24 24/11/19 164 25 500 Drill ['TO2'] 24 17/12/19 167 26 110v Ext Lead ['EL3'] 24 24/11/19 173 37 Router DW625E ['SC2'] 24 12/10/20 180 47 Vacuum ['SC2'] 24 27/12/19 181 48 110v Ext ['EL3'] 24 17/12/19 Test Group '.csv' TestGroupIndex Class Description TestGroupId VoltageOutput 1 2 Class 2 Double insulated SC2 230 2 1 Standard class 1 earthed SC1 230 The test group table has 30 entries but the index range is 1:31, i.e. one test group has been deleted at some point in the past, and the numbering is out of my control. To access the test group I read the testgroup.csv file to a numpy array, inside {Class#3}, with two blanks, one for [0], and the other deleted group. class OdsProcesses: ??? def __init__(self, path, filename, noGroups): ??? ??? . ??? ??? . ??????? self.group = np.zeros(((self.noGroups + 1), 1), dtype = np.dtype('U10'))? # 10-character string ??? def extract_TestGroup(self): ??????????? # Open Test Group file for reading ??????? groupFilename = self.path ??????? groupFilename += self.filename ??????? groupFilename += self.TG_suffix ??????? testGroup = csv.DictReader(open(groupFilename, 'r')) ????? # Loop through all Data ??????? for row in testGroup: ??????????? index = int(row["TestGroupIndex"]) ??????????? self.group[index] = row["TestGroupId"] ??? def get_TestGroup(self, index): ??????? groupStr = self.group[index] ??? def writeODSData(self, sheet, roomNum): ??? ??? . ??? ??? . ??????????? groupID = self.get_TestGroup(group) This works but feels 'clumpy', and I have a feeling that there is a better way, but I have run out of ideas. I am running Python 3.7.0, on a Windows 10 PC Thank you for any assistance Dave From rba2124 at gmail.com Wed Nov 7 08:14:44 2018 From: rba2124 at gmail.com (Roger B. Atkins) Date: Wed, 7 Nov 2018 06:14:44 -0700 Subject: [Tutor] Request for help with code In-Reply-To: References: Message-ID: Gmail: Click Compose (Upper Left corner) Window opens. In lower right corner within the compose window, click on the 3 stacked dots. Select text only. On Wed, Nov 7, 2018 at 1:39 AM Adam Eyring wrote: > > When I post code with questions, I just copy and paste from Python IDLE > 3.6. Colors are removed, but indentation is preserved. > > On Tue, Nov 6, 2018 at 6:59 PM Mats Wichmann wrote: > > > On 11/6/18 4:36 PM, Joseph Gulizia wrote: > > > Funny.... using a text editor....and showed indented in my browser. > > Won't > > > bother the list again. > > > > We don't want you to "not bother" us, just hoping to get things in a > > state where we can actually help... > > > > here's what we end up seeing: > > > > https://www.mail-archive.com/tutor at python.org/msg79222.html > > https://www.mail-archive.com/tutor at python.org/msg79225.html > > > > since unlike most other languages, indentation is a crucial part of the > > syntax, we tend to grumble. > > > > there must be some solution... guys, do you know of any alternate way to > > send a message to the list if a mail client isn't cooperating? or do we > > have instructions for beating gmail into submission? Obviously gmail is > > a key part of the modern infrastructure despite its (ahem) misfeatures. > > > > > > _______________________________________________ > > 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 srinivasan.rns at gmail.com Wed Nov 7 04:22:00 2018 From: srinivasan.rns at gmail.com (srinivasan) Date: Wed, 7 Nov 2018 10:22:00 +0100 Subject: [Tutor] SyntaxError: can't assign to literal while using ""blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % (fs)" using subprocess module in Python In-Reply-To: <007301d47648$e3f2f3a0$abd8dae0$@verizon.net> References: <007301d47648$e3f2f3a0$abd8dae0$@verizon.net> Message-ID: After changing the line to *"cmd = "blkid -o export %s | grep \'TYPE\' | cut -d\"=\" -f3" % fs"*, Now I dont see the error "SyntaxError: can't assign to literal" This is not returning exactly "*vfat*" instead of this, it is returning as "* /dev/mmcblk1p1: LABEL="efi" UUID="1084-AA42" TYPE="vfat"* " Could you please help me as it seems to be like grep and cut commands are not working in the above line ie., on *cmd = "blkid -o export %s | grep \'TYPE\' | cut -d\"=\" -f3" % fs*? On Wed, Nov 7, 2018 at 9:41 AM Avi Gross wrote: > I may be missing something but it looks like the embedded double quotes > may be a problem in this: > > cmd = "blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % ... > > if you use single quotes as in: > > cut -d"=" > > becomes > > cut -d'=' > > or escape the double quotes with \" and so on ... > > The above seems to be seen as: > > cmd=ALPHA=BETA % ... > > where ALPHA="blkid -o export %s | grep 'TYPE' | cut -d" > and BETA=" -f3" > > Other issues may also be there. > -----Original Message----- > From: Tutor On Behalf Of > Alan Gauld via Tutor > Sent: Tuesday, November 6, 2018 7:37 PM > To: tutor at python.org > Cc: python-dev at python.org > Subject: Re: [Tutor] SyntaxError: can't assign to literal while using > ""blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % (fs)" using > subprocess module in Python > > On 06/11/2018 18:07, srinivasan wrote: > > > bash command in python using subprocess module, I ma seeing the below > > * cmd = "blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % > > (fs)* > > In general you should try to do as little as possible using bash and > subprocess. Especially try to avoid long pipelines since you are starting a > new OS process for every element in the pipeline. That means, in your case, > you are running 4 processes to get your result - Python, blkid, grep and cut > > Python is designed to do much of what the shell command can do almost as > easily and much more efficiently (no new processes being started). > > In this case just execute the blkid bit in bash because its too difficult > to replicate simply in Python. Then use Python to search for the TYPE lines > and slice them to size. > > That will, in turn, simplify your command string and remove the issue of > multiple quotes. > > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.amazon.com/author/alan_gauld > Follow my photo-blog on Flickr at: > http://www.flickr.com/photos/alangauldphotos > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From srinivasan.rns at gmail.com Wed Nov 7 06:31:40 2018 From: srinivasan.rns at gmail.com (srinivasan) Date: Wed, 7 Nov 2018 12:31:40 +0100 Subject: [Tutor] SyntaxError: can't assign to literal while using ""blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % (fs)" using subprocess module in Python In-Reply-To: <1541585401.1731.1.camel@gmail.com> References: <007301d47648$e3f2f3a0$abd8dae0$@verizon.net> <1541585401.1731.1.camel@gmail.com> Message-ID: Even after changing as per the below "blkid -o export %s | grep 'TYPE' | cut -d'=' -f3" or: 'blkid -o export %s | grep "TYPE" | cut -d"=" -f3' or: "blkid -o export %s | grep \"TYPE\" | cut -d\"=\" -f3" Still my output is: */dev/mmcblk1p1: LABEL="efi" UUID="1084-AA42" TYPE="vfat"* My expected output should be only: *vfat* Could you guys please do the needful? On Wed, Nov 7, 2018 at 11:10 AM Brian J. Oney wrote: > On Wed, 2018-11-07 at 10:22 +0100, srinivasan wrote: > > blkid -o export %s | grep \'TYPE\' | cut -d\"=\" -f3 > > You don't need to escape the single quotes. > Try either: > > "blkid -o export %s | grep 'TYPE' | cut -d'=' -f3" > or: > 'blkid -o export %s | grep "TYPE" | cut -d"=" -f3' > or: > "blkid -o export %s | grep \"TYPE\" | cut -d\"=\" -f3" > > HTH > From wachobc at gmail.com Wed Nov 7 11:22:22 2018 From: wachobc at gmail.com (Chip Wachob) Date: Wed, 7 Nov 2018 11:22:22 -0500 Subject: [Tutor] Displaying Status on the Command Line Message-ID: Hello, I'm sure that this is simple and my searches have just not used the correct words. What I would like to do is display, on a single line, in the terminal / command line a progress percentage, or, simply a sequence of - / - \, etc.. or even, accumulating period characters. What would the escape codes be, or is there a better way to handle this? Note that I need this to be platform agnostic. Thank you in advance for your insight. From alan.gauld at yahoo.co.uk Wed Nov 7 14:15:09 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 7 Nov 2018 19:15:09 +0000 Subject: [Tutor] Displaying Status on the Command Line In-Reply-To: References: Message-ID: On 07/11/2018 16:22, Chip Wachob wrote: > What I would like to do is display, on a single line, in the terminal > / command line a progress percentage, or, simply a sequence of - / - > \, etc.. or even, accumulating period characters. > > What would the escape codes be, or is there a better way to handle this? It depends on your Python version. In Python v2 you simply put a comma after your output character to turn off the auto newline while someProcess(): # should probably be in a separate thread... time.sleep(1) # 1 second pause print '.', # comma suppresses newline If you want to suppress the spaces tyoo things get a tad more complex and you are probably best writing direct to sys.stdout In Python 3 there are parameters to print() while someProcess(): time.sleep(1) print('.', end='', sep='') # no newline and no spaces You shouldn't need any special escape codes. -- 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 Nov 7 14:21:58 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 7 Nov 2018 19:21:58 +0000 Subject: [Tutor] Regex for Filesystem path (Asad) In-Reply-To: References: Message-ID: On 07/11/2018 15:56, Asad wrote: > Hi All, > > I tired seems its not working as required : > > from os.path import dirname, join > > testdir = dirname("/a/b/c/d/test/test_2814__2018_10_05_12_12_45/logA.log") Note that this will set testdir to /a/b/c/d/test/test_2814__2018_10_05_12_12_45 But you want the dir above that. The easiest way (if this is always the case) is to just use dirname() again: testdir = dirname(testdir) Which should result in: /a/b/c/d/test You could of course do it in one line as myPath = "/a/b/c/d/test/test_2814__2018_10_05_12_12_45/logA.log" testdir = dirname( dirname(myPath) ) Which is nicer than my original suggestion of using chdir and relative paths :-) -- 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 zachary.ware+pytut at gmail.com Wed Nov 7 14:23:15 2018 From: zachary.ware+pytut at gmail.com (Zachary Ware) Date: Wed, 7 Nov 2018 13:23:15 -0600 Subject: [Tutor] Displaying Status on the Command Line In-Reply-To: References: Message-ID: On Wed, Nov 7, 2018 at 1:17 PM Alan Gauld via Tutor wrote: > In Python 3 there are parameters to print() > > while someProcess(): > time.sleep(1) > print('.', end='', sep='') # no newline and no spaces You'll also want `flush=True` here to avoid having your dots buffered until end-of-line. -- Zach From alan.gauld at yahoo.co.uk Wed Nov 7 14:29:35 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 7 Nov 2018 19:29:35 +0000 Subject: [Tutor] SyntaxError: can't assign to literal while using ""blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % (fs)" using subprocess module in Python In-Reply-To: References: <007301d47648$e3f2f3a0$abd8dae0$@verizon.net> <1541585401.1731.1.camel@gmail.com> Message-ID: On 07/11/2018 11:31, srinivasan wrote: > Even after changing as per the below > "blkid -o export %s | grep 'TYPE' | cut -d'=' -f3" > or: > 'blkid -o export %s | grep "TYPE" | cut -d"=" -f3' > or: > "blkid -o export %s | grep \"TYPE\" | cut -d\"=\" -f3" > > Still my output is: > */dev/mmcblk1p1: LABEL="efi" UUID="1084-AA42" TYPE="vfat"* > > My expected output should be only: > *vfat* > > Could you guys please do the needful? I would strongly suggest that you stop trying to use grep and cut from within the subprocess. Instead just process the lines with Python. Its clearer and more efficient. In this case it seems to be almost trivial: output = subprocess.... # run blkid shell command and read output for line in output: if line.endswith("vfat"): # do whatever you want to do with line. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From mats at wichmann.us Wed Nov 7 14:36:40 2018 From: mats at wichmann.us (Mats Wichmann) Date: Wed, 07 Nov 2018 12:36:40 -0700 Subject: [Tutor] best way to dynamically set class variables? In-Reply-To: References: Message-ID: <620D217E-2F98-4936-B607-622ECED1A8FF@wichmann.us> Not sure what you're after, but what's wrong with just setting them? Parent.avar = "a class var" On November 7, 2018 7:48:40 AM MST, Albert-Jan Roskam wrote: >Hi, > >Background: In my code I use sqlalchemy to read SQL server data. I want >to write that data to HDF5 using pytables (see 'declaring a column >descriptor': https://www.pytables.org/usersguide/tutorials.html). My >question is not about pytables or sqlalchemy per se, but I thought it >would be informative to mention this. > >What is the best way to dynamically set class variables? I am looking >for a generalization of something like this: > >class Parent: pass >class Child(Parent): > col1 = 'str' > col2 = 'int' > >Several (im)possible solutions: > ># ------- >class Parent: pass >class_vars = dict(col1='str', col2='int') > ># approach 1 >Child = type('Child', (Parent,), class_vars) > ># approach 2 >class Child(Parent): pass >Child.__dict__.update( class_vars ) # AttributeError: 'mappingproxy' >object has no attribute 'update' > ># approach 3 >class Child(Parent): pass >for k, v in class_vars.items(): > setattr(Child, k, v) > >I initially chose approach #1, but I find this way of defining a class >quite cryptic (but then, it's part of the language definition!). What's >the best way to do this? I am using Python 3.5 (Windows). Thanks in >advance! > >Best wishes, >Albert-Jan >_______________________________________________ >Tutor maillist - Tutor at python.org >To unsubscribe or change subscription options: >https://mail.python.org/mailman/listinfo/tutor -- Sent from my Android device with K-9 Mail. Please excuse my brevity. From sjeik_appie at hotmail.com Wed Nov 7 15:07:07 2018 From: sjeik_appie at hotmail.com (Albert-Jan Roskam) Date: Wed, 7 Nov 2018 20:07:07 +0000 Subject: [Tutor] best way to dynamically set class variables? In-Reply-To: <620D217E-2F98-4936-B607-622ECED1A8FF@wichmann.us> Message-ID: On 7 Nov 2018 20:36, Mats Wichmann wrote: Not sure what you're after, but what's wrong with just setting them? Parent.avar = "a class var" Hi Alan, Mats, I should have mentioned that the code is part of a function sql_to_hdf5. So it should be able to convert an *arbitrary* Sql server table into hdf5 format*). But even if it was just one specific table: it's quite cumbersome to write the column names (class variable names) and the target hdf5 data types (class variable values) for, say, a hundred columns. This is an example from the pytables website: >>> from tables import * >>> class Particle(IsDescription): ... name = StringCol(16) # 16-character String ... idnumber = Int64Col() # Signed 64-bit integer ... ADCcount = UInt16Col() # Unsigned short integer ... TDCcount = UInt8Col() # unsigned byte ... grid_i = Int32Col() # 32-bit integer ... grid_j = Int32Col() # 32-bit integer ... pressure = Float32Col() # float (single-precision) ... energy = Float64Col() # double (double-precision) Imagine having to write this for 100 columns, brrr. So the code grabs the sql column names and the datatypes (easy with sqlalchemy) and translates that into the hdf5 equivalent. And pytables uses a class, with class variables, to define this. A classmethod might be nice. I've never used this before, but I'll try this (I did try __new__ this afternoon, but it looked even more complicated). And it just occurred to me that exec() might even be a way (namedtuple does it, too). Or should I now grab my coat for even mentioning exec? :-))) Best wishes, Albert-Jan *) I tried using pandas to_hdf for this, but this does not work wel with mixed dtypes, NULL values, and chunkwise reading. From cs at cskk.id.au Wed Nov 7 17:20:48 2018 From: cs at cskk.id.au (Cameron Simpson) Date: Thu, 8 Nov 2018 09:20:48 +1100 Subject: [Tutor] Displaying Status on the Command Line In-Reply-To: References: Message-ID: <20181107222048.GA82419@cskk.homeip.net> On 07Nov2018 11:22, Chip Wachob wrote: >I'm sure that this is simple and my searches have just not used the >correct words. > >What I would like to do is display, on a single line, in the terminal >/ command line a progress percentage, or, simply a sequence of - / - >\, etc.. or even, accumulating period characters. > >What would the escape codes be, or is there a better way to handle this? > >Note that I need this to be platform agnostic. I'e got a module 'cs.upd' on PyPI which does this. "pip install cs.upd" to obtain it. Typical usage: import sys import time from cs.upd import Upd upd = Upd(sys.stdout) for percentage in range(1,100): upd.out("Progress: %d%%", percentage) if percentage % 10 == 0: upd.nl("completed %d%%", percentage) time.sleep(0.1) upd.out("Complete!") time.sleep(1.0) That example is obviously contrived, and the sleeps are so you can see it all happen. But you can slot this into simple terminal based programmes to present dynamic progress. Cheers, Cameron Simpson From steve at pearwood.info Wed Nov 7 17:28:09 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 8 Nov 2018 09:28:09 +1100 Subject: [Tutor] best way to dynamically set class variables? In-Reply-To: References: Message-ID: <20181107222808.GE4071@ando.pearwood.info> On Wed, Nov 07, 2018 at 02:48:40PM +0000, Albert-Jan Roskam wrote: > What is the best way to dynamically set class variables? I am looking > for a generalization of something like this: > > class Parent: pass > class Child(Parent): > col1 = 'str' > col2 = 'int' The obvious solution is to do exactly that: just set the class attribute in the subclass. If the value is dynamically generated, so be it: class Child(Parent): col1 = calculate_some_value() col2 = col1 + calculate_something_else() If the names of the class attributes themselves have to be generated, that's what locals() is for: class Child(Parent): for i in range(10): name = "column" + str(i) locals()[name] = i del i will give you class attributes column0 = 0, column1 = 1, etc. Of course you can generate the names any way you like, e.g. read them from a text file. A cleaner solution might be to move the code into a function and pass the class namespace to it: def make_attrs(ns): for i in range(10): name = "column" + str(i) ns[name] = i class Child(Parent): make_attrs(locals()) assert column0 == 0 assert Child.column1 == 1 Does this help? -- Steve From oscar.j.benjamin at gmail.com Wed Nov 7 17:32:33 2018 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Wed, 7 Nov 2018 22:32:33 +0000 Subject: [Tutor] best way to dynamically set class variables? In-Reply-To: References: Message-ID: On Wed, 7 Nov 2018 at 18:35, Alan Gauld via Tutor wrote: > > On 07/11/2018 14:48, Albert-Jan Roskam wrote: > > > What is the best way to dynamically set class variables? > > I think I'm maybe missing the point of your question? I think you are as well :) IIUC then the question is: how can I programatically/dynamically create a class that has some attributes derived from data that is known at runtime? Am I understanding this correctly Albert? > > # ------- > > class Parent: pass > > class_vars = dict(col1='str', col2='int') > > > > # approach 1 > > Child = type('Child', (Parent,), class_vars) This seems fine to me. It may seem cryptic but that's only because it's unusual to do this. You are creating a "type" and that is the constructor for type objects. -- Oscar From steve at pearwood.info Wed Nov 7 18:00:51 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 8 Nov 2018 10:00:51 +1100 Subject: [Tutor] Displaying Status on the Command Line In-Reply-To: References: Message-ID: <20181107230051.GF4071@ando.pearwood.info> On Wed, Nov 07, 2018 at 11:22:22AM -0500, Chip Wachob wrote: > Hello, > > I'm sure that this is simple and my searches have just not used the > correct words. > > What I would like to do is display, on a single line, in the terminal > / command line a progress percentage, or, simply a sequence of - / - > \, etc.. or even, accumulating period characters. > > What would the escape codes be, or is there a better way to handle this? Here's a quick and easy(?) way to do this which works on typical Linux systems. I've written it for Python 3, let us know if you need help getting it to work on Python 2. import time def spinner_demo(): for i in range(1, 101): c = r'-\|/-\|/'[i % 8] print("Spinner: %c %d%%\r" % (c, i), flush=True, end='') time.sleep(0.15) print('\n') To display a throbber instead, change the first line inside the loop to c = '.oOo'[i % 4] Here's a progress bar: def progress_demo(): N = 250 template = "Working hard... %- 40s %3.0f%% complete\r" for i in range(N): perc = i*100/N bar = '#'*(i*40//N + 1) print(template % (bar, perc), flush=True, end='') time.sleep(0.1) print('\n') > Note that I need this to be platform agnostic. That's hard, even on a single platform like Linux. Most xterminal windows use either the xterm or vt1000 set of commands, which are broadly similar, but that's not guaranteed. If somebody happens to be running a different terminal type, they'll see something weird. And I have no idea what happens on Windows. -- Steve From alan.gauld at yahoo.co.uk Wed Nov 7 18:06:31 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 7 Nov 2018 23:06:31 +0000 Subject: [Tutor] best way to dynamically set class variables? In-Reply-To: References: <620D217E-2F98-4936-B607-622ECED1A8FF@wichmann.us> Message-ID: On 07/11/2018 20:07, Albert-Jan Roskam wrote: > I should have mentioned that the code ... should be able to convert > an *arbitrary* Sql server table into hdf5 format*). Umm, yes that would have helped! >>>> from tables import * >>>> class Particle(IsDescription): > ... name = StringCol(16) # 16-character String > ... idnumber = Int64Col() # Signed 64-bit integer > ... ADCcount = UInt16Col() # Unsigned short integer > ... TDCcount = UInt8Col() # unsigned byte > ... grid_i = Int32Col() # 32-bit integer > ... grid_j = Int32Col() # 32-bit integer > ... pressure = Float32Col() # float (single-precision) > ... energy = Float64Col() # double (double-precision) > > > Imagine having to write this for 100 columns, brrr. No problem, I've done that dozens of time for production C++ code, it's business as usual in commercial programming. Of course I'd get the data from a meta SQL query and feed it into an emacs macro to generate the code but I'd still have to churn out the class definitions. (Or I might even write a Python program to generate the C++ code for me) But it's eminently doable and keeps the rest of the code simple. Of course Python allows for dynamic creation of classes so you can be more subtle than that. :-) > So the code grabs the sql column names and the datatypes > and translates that into the hdf5 equivalent. Should be relatively easy to write a function to do that. I'm not clear if the class definition already exists or if you are trying to create the class (Based on the table name maybe?)as well as its attributes? Also column names are usually instance attributes not class attributes. Each instance of the class being a row in the table... > And pytables uses a class, with class variables, to define this. No idea what pytables is or what it does so can't comment. > A classmethod might be nice. I've never used this before, If there is some kind of abstract superclass (TableModel or somesuch?) then a class method there could spin off the new subclasses. > exec() might even be a way It's a way but it's a bad way! :-) Another option would be to explore metaclasses and modify the class creation mechanism -- 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 Wed Nov 7 18:21:14 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 8 Nov 2018 10:21:14 +1100 Subject: [Tutor] SyntaxError: can't assign to literal while using ""blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % (fs)" using subprocess module in Python In-Reply-To: References: <007301d47648$e3f2f3a0$abd8dae0$@verizon.net> Message-ID: <20181107232113.GG4071@ando.pearwood.info> On Wed, Nov 07, 2018 at 10:22:00AM +0100, srinivasan wrote: > Could you please help me as it seems to be like grep and cut commands are > not working in the above line ie., on *cmd = "blkid -o export %s | grep > \'TYPE\' | cut -d\"=\" -f3" % fs*? Then get them working first. What happens when you execute that line directly on the command line? But I have to wonder, are you being paid by the hour? Because it seems like a waste of time and effort to be using a pipeline of commands for this. You seem to be deliberately turning a small, easy job into a huge, difficult job. The *easiest* way to do this is to do the string processing in Python, not using the hard-to-use shell commands. Python has its own grep module: import grep but since all you are doing is looking for a plain string with no wildcards, you don't even need that. Collect the output of blkid into a string, then do your processing in Python: # Untested for line in output.splitlines(): # each line looks something like this # "/dev/md0: UUID="3cb91a07-14c9-4ae4-81e1-6eb662eabeee" TYPE="ext3" p = line.find('TYPE="') if p == -1: continue # skip the line result = line[p:] if line.endswith('"'): result = result[:-1] print("Found file type", result) Much easier to read and write and debug than a pipeline of Unix command line tools, and likely to be faster too. -- Steve From mats at wichmann.us Wed Nov 7 18:30:29 2018 From: mats at wichmann.us (Mats Wichmann) Date: Wed, 7 Nov 2018 16:30:29 -0700 Subject: [Tutor] SyntaxError: can't assign to literal while using ""blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % (fs)" using subprocess module in Python In-Reply-To: References: <007301d47648$e3f2f3a0$abd8dae0$@verizon.net> <1541585401.1731.1.camel@gmail.com> Message-ID: On 11/7/18 4:31 AM, srinivasan wrote: > Even after changing as per the below > "blkid -o export %s | grep 'TYPE' | cut -d'=' -f3" > or: > 'blkid -o export %s | grep "TYPE" | cut -d"=" -f3' > or: > "blkid -o export %s | grep \"TYPE\" | cut -d\"=\" -f3" > > Still my output is: > */dev/mmcblk1p1: LABEL="efi" UUID="1084-AA42" TYPE="vfat"* > > My expected output should be only: > *vfat* then you don't want -f3... vfat is the 4th field if you split (cut) that string on equal signs. usually if a shell pipeline doesn't work from Python, it means you're not invoking the shell. I notice in your original code you default the "shell=" value to false. From cs at cskk.id.au Wed Nov 7 18:44:02 2018 From: cs at cskk.id.au (Cameron Simpson) Date: Thu, 8 Nov 2018 10:44:02 +1100 Subject: [Tutor] Displaying Status on the Command Line In-Reply-To: <20181107230051.GF4071@ando.pearwood.info> References: <20181107230051.GF4071@ando.pearwood.info> Message-ID: <20181107234402.GA91897@cskk.homeip.net> On 08Nov2018 10:00, Steven D'Aprano wrote: >> Note that I need this to be platform agnostic. > >That's hard, even on a single platform like Linux. Most, nearly all, terminal honour carriage return and backspace. That is technically enough. Even terminals with a destructive backspace (rare - it is normally just a cursor motion) can get by (backspace, overwrite the new text). >Most xterminal windows use either the xterm or vt1000 set of commands, >which are broadly similar, but that's not guaranteed. If somebody >happens to be running a different terminal type, they'll see something >weird. > >And I have no idea what happens on Windows. I'd sort of expect Windows terminals, even cmd.exe, to accept the ANSI sequences, which is what vt100 and xterms use. But that is expectation, not knowledge. Cheers, Cameron Simpson From alan.gauld at yahoo.co.uk Wed Nov 7 20:15:40 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 8 Nov 2018 01:15:40 +0000 Subject: [Tutor] best way to dynamically set class variables? In-Reply-To: References: <620D217E-2F98-4936-B607-622ECED1A8FF@wichmann.us> Message-ID: On 07/11/2018 23:06, Alan Gauld via Tutor wrote: > Another option would be to explore metaclasses > and modify the class creation mechanism Which, of course, is what you were doing with the type(...) call in your 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 Wed Nov 7 20:21:51 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 8 Nov 2018 01:21:51 +0000 Subject: [Tutor] best way to dynamically set class variables? In-Reply-To: References: <620D217E-2F98-4936-B607-622ECED1A8FF@wichmann.us> Message-ID: On 07/11/2018 23:06, Alan Gauld via Tutor wrote: >> Imagine having to write this for 100 columns, brrr. > > No problem, I've done that dozens of time for production C++ code, > it's business as usual in commercial programming. > > Of course I'd get the data from a meta SQL query and feed it into an > emacs macro to generate the code but I'd still have to churn out the > class definitions. (Or I might even write a Python program to generate > the C++ code for me) But it's eminently doable and keeps the rest of the > code simple. It just occurred to me that this bit of C++ nostalgia might not be as irrelevant as I first thought. A simple approach you could use would be to get Python to generate a new python file(module) containing the required class definition (simple string processing) and then dynamically import the new file. Very similar to my emacs macro approach except with dynamic loading. You'd probably need a batch/cron job to version control all these new files too... And I've no idea how efficient that would be if there were a lot of tables to be processed - but in that scenario I suspect the whole class per table approach is flawed! Just a thought... or 3... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From __peter__ at web.de Thu Nov 8 02:46:22 2018 From: __peter__ at web.de (Peter Otten) Date: Thu, 08 Nov 2018 08:46:22 +0100 Subject: [Tutor] best way to dynamically set class variables? References: <620D217E-2F98-4936-B607-622ECED1A8FF@wichmann.us> Message-ID: Alan Gauld via Tutor wrote: >>exec() might even be a way > > It's a way but it's a bad way! :-) And then > A simple approach you could use would be to get Python to > generate a new python file(module) containing the required class > definition (simple string processing) and then dynamically > import the new file. That's basically exec(), with better tracebacks and a higher chance to run outdated code ;) By the way I don't think exec() is bad as long as you control its input and as long as this input is fairly simple. From asad.hasan2004 at gmail.com Wed Nov 7 21:55:16 2018 From: asad.hasan2004 at gmail.com (Asad) Date: Thu, 8 Nov 2018 08:25:16 +0530 Subject: [Tutor] Regex for Filesystem path (Asad) In-Reply-To: References: Message-ID: Hi All , Thanks it works for me . However the other issue is : testdir = dirname(dirname("/a/b/c/d/test/test_2814__2018_10_05_12_12_45/logA.log")) /a/b/c/d/test dirpath = join(testdir, '28163133/22326541') print dirpath /a/b/c/d/test\28163133/22326541 * Why is it putting \ this breaks the unix path it should be: /a/b/c/d/test/28163133/22326541 ===> for unix platform logs \a\b\c\d\test\28163133\22326541 ===> for windows platform logs Please advice , Thanks, On Thu, N > ---------- Forwarded message ---------- > From: Alan Gauld > To: tutor at python.org > Cc: > Bcc: > Date: Wed, 7 Nov 2018 19:21:58 +0000 > Subject: Re: [Tutor] Regex for Filesystem path (Asad) > On 07/11/2018 15:56, Asad wrote: > > Hi All, > > > > I tired seems its not working as required : > > > > from os.path import dirname, join > > > > testdir = > dirname("/a/b/c/d/test/test_2814__2018_10_05_12_12_45/logA.log") > > Note that this will set testdir to > > /a/b/c/d/test/test_2814__2018_10_05_12_12_45 > > But you want the dir above that. > The easiest way (if this is always the case) is to just use > dirname() again: > > testdir = dirname(testdir) > > Which should result in: > > /a/b/c/d/test > > You could of course do it in one line as > > myPath = "/a/b/c/d/test/test_2814__2018_10_05_12_12_45/logA.log" > testdir = dirname( dirname(myPath) ) > > Which is nicer than my original suggestion of using > chdir and relative paths :-) > > -- > 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: Zachary Ware > To: > Cc: tutor > Bcc: > Date: Wed, 7 Nov 2018 13:23:15 -0600 > Subject: Re: [Tutor] Displaying Status on the Command Line > On Wed, Nov 7, 2018 at 1:17 PM Alan Gauld via Tutor > wrote: > > In Python 3 there are parameters to print() > > > > while someProcess(): > > time.sleep(1) > > print('.', end='', sep='') # no newline and no spaces > > You'll also want `flush=True` here to avoid having your dots buffered > until end-of-line. > > -- > Zach > > _______________________________________________ > Tutor maillist - Tutor at python.org > https://mail.python.org/mailman/listinfo/tutor > -- Asad Hasan +91 9582111698 From avigross at verizon.net Wed Nov 7 21:34:43 2018 From: avigross at verizon.net (Avi Gross) Date: Wed, 7 Nov 2018 21:34:43 -0500 Subject: [Tutor] SyntaxError: can't assign to literal while using ""blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % (fs)" using subprocess module in Python In-Reply-To: References: <007301d47648$e3f2f3a0$abd8dae0$@verizon.net> Message-ID: <009a01d4770b$9b6685c0$d2339140$@verizon.net> It is easier to understand code when it is shown unaltered. The character "*" (as in asterisk) has plenty of meaning in many languages and especially in python. WHY is it being shown in your snippet at beginning and end? *"cmd = "blkid -o export %s | grep \'TYPE\' | cut -d\"=\" -f3" % fs"*, Is it there as a form of emphasis or markup? It dos NOT belong when showing others and asking for help when every symbol (not to mention indentation) can have other meanings or make it nonsensical. The simplest way for you to debug is to do a step at a time and check. Before asking for help, it can be a goof learning experience to try each step out using print statements or other techniques. In your case, have you tried the overall command directly from the shell? I won't do much more but start what you could have done. Removing the two asterisks and the strange added comma I see: "cmd = "blkid -o export %s | grep \'TYPE\' | cut -d\"=\" -f3" % fs" But why is it all in quotes? Removing the quotes: cmd = "blkid -o export %s | grep \'TYPE\' | cut -d\"=\" -f3" % fs OK, to test this I need a value for fs to be interpolated into the string. Here is my output: >>> fs = "TESTING" >>> cmd = "blkid -o export %s | grep \'TYPE\' | cut -d\"=\" -f3" % fs >>> print(cmd) blkid -o export TESTING | grep 'TYPE' | cut -d"=" -f3 Is that the command line you wanted? If so, you can continue by yourself. If not, you need to fix it before continuing. But, as others have noted, there really is not much reason to do this using a shell script with pipes if you know enough Python including some added modules. You remind me of a supervisor years ago who did not have a computer science background but decided he wanted to run some scripts that executed remote commands to gather billing files and statistics from many machines including (would you believe it) some in places like Japan over 300 baud connections. He wrote some humungous scripts for ksh (Korn Shell) that did something like what you are doing and worse. Lots of loops that iterated over each machine then each billing file with pipelines as long as 10 pipe symbols in a command line. Lots of use of grep to match patterns like the first character of a line, then programs like cut to grab a subset of columns (separated by pipe symbols, of course) and so on. The program ran for hours in middle of the night for the US but made me get calls from Japan and Israel complaining about how sluggish their machines were doing their busy hour. So, I rewrote the entire script he used as something that gathered the raw files to a US machine and called a single AWK script that sliced and diced very efficiently. Python is even more powerful and what you want, perhaps not trivial, is straightforward. I can't say it has built-in methods to display available information about block devices. But you can spawn a simpler command to run just blkid with output redirected to a pipe in your process. (Alternately you can direct it to a file then open that.) Not sure what you want but is your output a bit like this? ID_FS_UUID=7fa9c421-0054-4555-b0ca-b470a97a3d84 ID_FS_UUID_ENC=7fa9c421-0054-4555-b0ca-b470a97a3d84 ID_FS_VERSION=1.0 6- ID_FS_USAGE=filesystem You can then have a loop iterating over the lines with variable "line" each time being the holder of one line worth of info. You want only lines that contain "TYPE" and for a fixed string like that you don't need a grep variant. You want to segment it based on "=" symbols? Try: line.find("TYPE") If that returns a find, continue. (-1 if not found.) You want to now split that line into multiple parts based on the "=" symbol. pieces = line.split("=") and the results now are a list of strings. But recall the first piece in Python has a 0 index. So your -f3 would be: pieces[2] I won't continue with the rest of your code but if the line you really want is like the one above: ID_FS_TYPE=ext4 You can pretty much do all the above very compactly using a regular expression that (in English) matches a line containing Anything followed by exactly "TYPE=" followed by open paren, anything to end of line, and close paren. The result of any match will be available to you as the first and only part of the pattern you asked for. You can work out details. Probably less efficient but not very much code. And the string method can be compacted in many ways. I mean code like line.split[2] or in your case if there is a match for line.find() for "TYPE=" then you can index the return value by adding the 5 characters that matched: >>> line = "ID_FS_TYPE=ext4" >>> line.find("TYPE=") 6 >>> line[6+5:] 'ext4' -----Original Message----- From: Tutor On Behalf Of srinivasan Sent: Wednesday, November 7, 2018 4:22 AM To: tutor at python.org; python-list at python.org Subject: Re: [Tutor] SyntaxError: can't assign to literal while using ""blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % (fs)" using subprocess module in Python After changing the line to *"cmd = "blkid -o export %s | grep \'TYPE\' | cut -d\"=\" -f3" % fs"*, Now I dont see the error "SyntaxError: can't assign to literal" This is not returning exactly "*vfat*" instead of this, it is returning as "* /dev/mmcblk1p1: LABEL="efi" UUID="1084-AA42" TYPE="vfat"* " Could you please help me as it seems to be like grep and cut commands are not working in the above line ie., on *cmd = "blkid -o export %s | grep \'TYPE\' | cut -d\"=\" -f3" % fs*? On Wed, Nov 7, 2018 at 9:41 AM Avi Gross wrote: > I may be missing something but it looks like the embedded double > quotes may be a problem in this: > > cmd = "blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % ... > > if you use single quotes as in: > > cut -d"=" > > becomes > > cut -d'=' > > or escape the double quotes with \" and so on ... > > The above seems to be seen as: > > cmd=ALPHA=BETA % ... > > where ALPHA="blkid -o export %s | grep 'TYPE' | cut -d" > and BETA=" -f3" > > Other issues may also be there. > -----Original Message----- > From: Tutor On Behalf > Of Alan Gauld via Tutor > Sent: Tuesday, November 6, 2018 7:37 PM > To: tutor at python.org > Cc: python-dev at python.org > Subject: Re: [Tutor] SyntaxError: can't assign to literal while using > ""blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % (fs)" using > subprocess module in Python > > On 06/11/2018 18:07, srinivasan wrote: > > > bash command in python using subprocess module, I ma seeing the below > > * cmd = "blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % > > (fs)* > > In general you should try to do as little as possible using bash and > subprocess. Especially try to avoid long pipelines since you are > starting a new OS process for every element in the pipeline. That > means, in your case, you are running 4 processes to get your result - > Python, blkid, grep and cut > > Python is designed to do much of what the shell command can do almost > as easily and much more efficiently (no new processes being started). > > In this case just execute the blkid bit in bash because its too > difficult to replicate simply in Python. Then use Python to search for > the TYPE lines and slice them to size. > > That will, in turn, simplify your command string and remove the issue > of multiple quotes. > > > -- > Alan G > Author of the Learn to Program web site http://www.alan-g.me.uk/ > http://www.amazon.com/author/alan_gauld > Follow my photo-blog on Flickr at: > http://www.flickr.com/photos/alangauldphotos > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From avigross at verizon.net Wed Nov 7 22:50:09 2018 From: avigross at verizon.net (Avi Gross) Date: Wed, 7 Nov 2018 22:50:09 -0500 Subject: [Tutor] Displaying Status on the Command Line In-Reply-To: References: Message-ID: <00b701d47716$253df3f0$6fb9dbd0$@verizon.net> What Alan wrote makes sense if you just want to put out one mark per second till you stop. But if you want a percentage of progress, you need some way to estimate what percent of the way you are to being done. You need to determine how many marks represent 100% such as 50 periods. You need to have your code pause periodically and assess if the current percentage requires more characters than have been put out so far. If you were at 10% last time and had put out 5 dots and are now at 16% and need 3 more dots, you might say something like this with n=3, and ch="." print(ch*n, end="", flush=True), sep="") Note you want to make sure the buffers are flushed to make it real time and when done may want to print a newline so any further output is not left dangling. The above print statement is for Python 3.x, and as Alan explained, in version 2.X you may need to use alternate syntax. Not sure how you flush buffers but you can skip the print statement and write in other ways to sys.stdout. -----Original Message----- From: Tutor On Behalf Of Alan Gauld via Tutor Sent: Wednesday, November 7, 2018 2:15 PM To: tutor at python.org Subject: Re: [Tutor] Displaying Status on the Command Line On 07/11/2018 16:22, Chip Wachob wrote: > What I would like to do is display, on a single line, in the terminal > / command line a progress percentage, or, simply a sequence of - / - > \, etc.. or even, accumulating period characters. > > What would the escape codes be, or is there a better way to handle this? It depends on your Python version. In Python v2 you simply put a comma after your output character to turn off the auto newline while someProcess(): # should probably be in a separate thread... time.sleep(1) # 1 second pause print '.', # comma suppresses newline If you want to suppress the spaces tyoo things get a tad more complex and you are probably best writing direct to sys.stdout In Python 3 there are parameters to print() while someProcess(): time.sleep(1) print('.', end='', sep='') # no newline and no spaces You shouldn't need any special escape codes. -- 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 avigross at verizon.net Wed Nov 7 23:35:19 2018 From: avigross at verizon.net (Avi Gross) Date: Wed, 7 Nov 2018 23:35:19 -0500 Subject: [Tutor] best way to dynamically set class variables? In-Reply-To: References: Message-ID: <00b801d4771c$748c7a20$5da56e60$@verizon.net> I have been reading the replies and wonder sometimes if we understand the real question as intended. Classes in Python can be changed in all kinds of ways even after they have been defined and the changes take effect on any new instances created afterward. So can instances in multiple ways. If you want to store the names of a hundred columns in a variable or even a hundred variables, you have ways to assign them. You can even change methods on the fly. If what you want is even more flexibility to design the class later after receiving more data such as the names and types of the columns in a data table, you can either write the description as text into a temporary file and import it, if that makes sense, or make a string to be evaluated in memory. Both can be dangerous if you do not trust the parts added as the code is going to be run at runtime and can do malicious things. Python often has so many ways to do things that various ones may work better for you. In your case, one example would be to intercept the ability to set and get (unknown) components of a class or instance by using the right dunder function such as __getattr__ and have it KNOW about your dynamic variable names and control access to them. There are many ways to do this, CAREFULLY, and some work only or differently in new style classes. Heck, you can put all the important code in an external function called by the above that can dynamically be made in Python at a later time. One architecture might be to store your new info in one or more dictionaries and have that functionality check if a valid request is made and return it. Obviously it matters where you want the data held as in per instance or per class or superclass and so on. Of course, I may misunderstand your issue. But from what it sounds like, your main request is a way to associate multiple items to be stored after a class is created but before it is used. There are an amazing number of ways even before you loom at more advanced methods like decorators. -----Original Message----- From: Tutor On Behalf Of Oscar Benjamin Sent: Wednesday, November 7, 2018 5:33 PM To: tutor at python.org Subject: Re: [Tutor] best way to dynamically set class variables? On Wed, 7 Nov 2018 at 18:35, Alan Gauld via Tutor wrote: > > On 07/11/2018 14:48, Albert-Jan Roskam wrote: > > > What is the best way to dynamically set class variables? > > I think I'm maybe missing the point of your question? I think you are as well :) IIUC then the question is: how can I programatically/dynamically create a class that has some attributes derived from data that is known at runtime? Am I understanding this correctly Albert? > > # ------- > > class Parent: pass > > class_vars = dict(col1='str', col2='int') > > > > # approach 1 > > Child = type('Child', (Parent,), class_vars) This seems fine to me. It may seem cryptic but that's only because it's unusual to do this. You are creating a "type" and that is the constructor for type objects. -- Oscar _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From wachobc at gmail.com Wed Nov 7 23:06:28 2018 From: wachobc at gmail.com (Chip Wachob) Date: Wed, 7 Nov 2018 23:06:28 -0500 Subject: [Tutor] Displaying Status on the Command Line In-Reply-To: <20181107234402.GA91897@cskk.homeip.net> References: <20181107230051.GF4071@ando.pearwood.info> <20181107234402.GA91897@cskk.homeip.net> Message-ID: Wow! Thank you! Lots of things for me to try. I should have mentioned that I'm working with Python 2, but I think I can parse my way through these examples. Best, On 11/7/18, Cameron Simpson wrote: > On 08Nov2018 10:00, Steven D'Aprano wrote: >>> Note that I need this to be platform agnostic. >> >>That's hard, even on a single platform like Linux. > > Most, nearly all, terminal honour carriage return and backspace. That is > technically enough. Even terminals with a destructive backspace (rare - > it is normally just a cursor motion) can get by (backspace, overwrite > the new text). > >>Most xterminal windows use either the xterm or vt1000 set of commands, >>which are broadly similar, but that's not guaranteed. If somebody >>happens to be running a different terminal type, they'll see something >>weird. >> >>And I have no idea what happens on Windows. > > I'd sort of expect Windows terminals, even cmd.exe, to accept the ANSI > sequences, which is what vt100 and xterms use. But that is expectation, > not knowledge. > > Cheers, > Cameron Simpson > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From steve at pearwood.info Thu Nov 8 05:20:13 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 8 Nov 2018 21:20:13 +1100 Subject: [Tutor] SyntaxError: can't assign to literal while using ""blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % (fs)" using subprocess module in Python In-Reply-To: <009a01d4770b$9b6685c0$d2339140$@verizon.net> References: <007301d47648$e3f2f3a0$abd8dae0$@verizon.net> <009a01d4770b$9b6685c0$d2339140$@verizon.net> Message-ID: <20181108102013.GH4071@ando.pearwood.info> On Wed, Nov 07, 2018 at 09:34:43PM -0500, Avi Gross wrote: > It is easier to understand code when it is shown unaltered. The character > "*" (as in asterisk) has plenty of meaning in many languages and especially > in python. > > WHY is it being shown in your snippet at beginning and end? As far as I can tell, it is Gmail being "helpful" (i.e. obnoxious) by adding markup to the plain-text version of the email, whenever you use so-called "Rich Text" (HTML formatting). -- Steve From alan.gauld at yahoo.co.uk Thu Nov 8 05:42:44 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 8 Nov 2018 10:42:44 +0000 Subject: [Tutor] Displaying Status on the Command Line In-Reply-To: References: <20181107230051.GF4071@ando.pearwood.info> <20181107234402.GA91897@cskk.homeip.net> Message-ID: On 08/11/2018 04:06, Chip Wachob wrote: > I should have mentioned that I'm working with Python 2, but I think I > can parse my way through these examples. OK, In that case you may want to investigate the sys.stdout approach. Just remember it's a pre opened file and use the write() method. But it won't do any of the print type things, you need to convert all values to strings and add in new lines etc yourself. IN fact if you go that way you might want to use sys.stderr instead since that will mean your progress indicator does not appear in the true output should you pipe it into a file. And finally, don't forget the >>> prompt is your friend for this kind of thing. Experiment there to get it right before committing it to a larger script. -- 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 Nov 8 05:49:31 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 8 Nov 2018 10:49:31 +0000 Subject: [Tutor] Regex for Filesystem path (Asad) In-Reply-To: References: Message-ID: On 08/11/2018 02:55, Asad wrote: > Why is it putting \ this breaks the unix path it should be: > > /a/b/c/d/test/28163133/22326541 ===> for unix platform logs > > \a\b\c\d\test\28163133\22326541 ===> for windows platform logs os.path.join uses the separator that is correct for your OS. Since you are running on Windows it uses \ The preferred way of constructing a path is to do it all in os.path, that way, regardless of OS it will always produce the right thing. But it can get a bit clunky (especially with Windows drive letters included) so most folks cheat and use hard coded strings. But as you discovered that can lead to mixed separators. Try: os.path.join('a','b','c','d','test') -- 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 Nov 8 05:51:44 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 8 Nov 2018 10:51:44 +0000 Subject: [Tutor] best way to dynamically set class variables? In-Reply-To: References: <620D217E-2F98-4936-B607-622ECED1A8FF@wichmann.us> Message-ID: On 08/11/2018 07:46, Peter Otten wrote: > By the way I don't think exec() is bad as long as you control its input and > as long as this input is fairly simple. Yes, but reading arbitrary column names from a database is not exactly controlled input... -- 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 srinivasan.rns at gmail.com Thu Nov 8 05:20:27 2018 From: srinivasan.rns at gmail.com (srinivasan) Date: Thu, 8 Nov 2018 11:20:27 +0100 Subject: [Tutor] best way to dynamically set class variables? In-Reply-To: <00b801d4771c$748c7a20$5da56e60$@verizon.net> References: <00b801d4771c$748c7a20$5da56e60$@verizon.net> Message-ID: Once again thanks a lot guys for all your help and I really appreciate it that I can really come back to you, if am stuck with any python issues, as i was really a embedded linux platform developer >From now I have learnt not to use much bash commands with pipe and to just use the bash command and parse it using python code For now I have simplified and it working as below, might be useful for others sometime def get_fstype_of_mounted_partition(self, partition_path): """ Get the filesystem type of the mounted partition. :partition_name : Partition path as string (e.g. /dev/mmcblk0p1) :return: filesystem type as string """ cmd = "lsblk %s -n -o FSTYPE" % partition_path return self._helper.execute_cmd_output_string(cmd) On Thu, Nov 8, 2018 at 11:11 AM Avi Gross wrote: > I have been reading the replies and wonder sometimes if we understand the > real question as intended. > > Classes in Python can be changed in all kinds of ways even after they have > been defined and the changes take effect on any new instances created > afterward. So can instances in multiple ways. If you want to store the > names > of a hundred columns in a variable or even a hundred variables, you have > ways to assign them. You can even change methods on the fly. > > If what you want is even more flexibility to design the class later after > receiving more data such as the names and types of the columns in a data > table, you can either write the description as text into a temporary file > and import it, if that makes sense, or make a string to be evaluated in > memory. Both can be dangerous if you do not trust the parts added as the > code is going to be run at runtime and can do malicious things. > > Python often has so many ways to do things that various ones may work > better > for you. In your case, one example would be to intercept the ability to set > and get (unknown) components of a class or instance by using the right > dunder function such as __getattr__ and have it KNOW about your dynamic > variable names and control access to them. There are many ways to do this, > CAREFULLY, and some work only or differently in new style classes. Heck, > you > can put all the important code in an external function called by the above > that can dynamically be made in Python at a later time. One architecture > might be to store your new info in one or more dictionaries and have that > functionality check if a valid request is made and return it. Obviously it > matters where you want the data held as in per instance or per class or > superclass and so on. > > Of course, I may misunderstand your issue. But from what it sounds like, > your main request is a way to associate multiple items to be stored after a > class is created but before it is used. There are an amazing number of ways > even before you loom at more advanced methods like decorators. > > -----Original Message----- > From: Tutor On Behalf Of > Oscar Benjamin > Sent: Wednesday, November 7, 2018 5:33 PM > To: tutor at python.org > Subject: Re: [Tutor] best way to dynamically set class variables? > > On Wed, 7 Nov 2018 at 18:35, Alan Gauld via Tutor > wrote: > > > > On 07/11/2018 14:48, Albert-Jan Roskam wrote: > > > > > What is the best way to dynamically set class variables? > > > > I think I'm maybe missing the point of your question? > > I think you are as well :) > > IIUC then the question is: how can I programatically/dynamically create a > class that has some attributes derived from data that is known at runtime? > > Am I understanding this correctly Albert? > > > > # ------- > > > class Parent: pass > > > class_vars = dict(col1='str', col2='int') > > > > > > # approach 1 > > > Child = type('Child', (Parent,), class_vars) > > This seems fine to me. It may seem cryptic but that's only because it's > unusual to do this. You are creating a "type" and that is the constructor > for type objects. > > -- > Oscar > _______________________________________________ > 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 zachary.ware+pytut at gmail.com Thu Nov 8 10:36:53 2018 From: zachary.ware+pytut at gmail.com (Zachary Ware) Date: Thu, 8 Nov 2018 09:36:53 -0600 Subject: [Tutor] Displaying Status on the Command Line In-Reply-To: References: <20181107230051.GF4071@ando.pearwood.info> <20181107234402.GA91897@cskk.homeip.net> Message-ID: On Thu, Nov 8, 2018 at 4:12 AM Chip Wachob wrote: > I should have mentioned that I'm working with Python 2, but I think I > can parse my way through these examples. You can use any of the `print` function tricks above in Python 2 with the following boilerplate: from __future__ import print_function import sys _orig_print = print def print(*args, **kwargs): flush = kwargs.pop('flush', False) _orig_print(*args, **kwargs) if flush: file = kwargs.get('file', sys.stdout) file.flush() When you get to upgrade to Python 3, just throw the above code away and things will work just the same :) -- Zach From sjeik_appie at hotmail.com Thu Nov 8 10:51:39 2018 From: sjeik_appie at hotmail.com (Albert-Jan Roskam) Date: Thu, 8 Nov 2018 15:51:39 +0000 Subject: [Tutor] Regex for Filesystem path (Asad) In-Reply-To: References: , Message-ID: From: Tutor on behalf of Alan Gauld via Tutor Sent: Thursday, November 8, 2018 10:49 AM To: tutor at python.org Subject: Re: [Tutor] Regex for Filesystem path (Asad) On 08/11/2018 02:55, Asad wrote: > Why is it putting \ this breaks the unix path it should be: > > /a/b/c/d/test/28163133/22326541 ===> for unix platform logs > > \a\b\c\d\test\28163133\22326541 ===> for windows platform logs Hi, Maybe pathlib will be of use here: >>> from pathlib import Path >>> Path('c:\\beeh/foo\\bar/baz').as_posix() 'c:/beeh/foo/bar/baz' I was thinking it would also be possible to do (in Windows): import os.path os.path.sep = '/' os.path.normpath('c:\\beeh/foo\\bar/baz') But alas, this still creates normalized windows-style paths. From steve at pearwood.info Thu Nov 8 11:08:06 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 9 Nov 2018 03:08:06 +1100 Subject: [Tutor] Displaying Status on the Command Line In-Reply-To: References: <20181107230051.GF4071@ando.pearwood.info> <20181107234402.GA91897@cskk.homeip.net> Message-ID: <20181108160806.GI4071@ando.pearwood.info> On Thu, Nov 08, 2018 at 09:36:53AM -0600, Zachary Ware wrote: > You can use any of the `print` function tricks above in Python 2 with > the following boilerplate: > > from __future__ import print_function > > import sys > > _orig_print = print > > def print(*args, **kwargs): > flush = kwargs.pop('flush', False) > _orig_print(*args, **kwargs) > if flush: > file = kwargs.get('file', sys.stdout) > file.flush() Sorry, I don't understand that. Maybe its too early in the morning for my brain, but given that you've imported the Python 3 print function from the __future__ why do you need the customer wrapper? from __future__ import print_function alone should give you exactly the Python 3 print function, with all its bells and whistles. What have I missed? -- Steve From zachary.ware+pytut at gmail.com Thu Nov 8 11:16:35 2018 From: zachary.ware+pytut at gmail.com (Zachary Ware) Date: Thu, 8 Nov 2018 10:16:35 -0600 Subject: [Tutor] Displaying Status on the Command Line In-Reply-To: <20181108160806.GI4071@ando.pearwood.info> References: <20181107230051.GF4071@ando.pearwood.info> <20181107234402.GA91897@cskk.homeip.net> <20181108160806.GI4071@ando.pearwood.info> Message-ID: On Thu, Nov 8, 2018 at 10:10 AM Steven D'Aprano wrote: > Sorry, I don't understand that. Maybe its too early in the morning for > my brain, but given that you've imported the Python 3 print function > from the __future__ why do you need the customer wrapper? > > from __future__ import print_function > > alone should give you exactly the Python 3 print function, with all its > bells and whistles. > > What have I missed? The `flush` parameter was added in 3.3. -- Zach From __peter__ at web.de Thu Nov 8 11:53:00 2018 From: __peter__ at web.de (Peter Otten) Date: Thu, 08 Nov 2018 17:53 +0100 Subject: [Tutor] Regex for Filesystem path (Asad) References: Message-ID: Albert-Jan Roskam wrote: > I was thinking it would also be possible to do (in Windows): > import os.path > os.path.sep = '/' > os.path.normpath('c:\\beeh/foo\\bar/baz') > > But alas, this still creates normalized windows-style paths. If your input data has only forward slashes you can keep it that way with posixpath, i. e. >>> import posixpath >>> posixpath.join("foo", "bar") 'foo/bar' should produce the same result on windows. posixpath.normpath() cannot replace backward with forward slashes because the backslash is an ordinary character on Linux et. al. Fun fact: scripts written with Windows in mind sometimes work on Linux, with the output going to unexpected places: $ python3 Python 3.4.3 (default, Nov 28 2017, 16:41:13) [GCC 4.8.4] on linux Type "help", "copyright", "credits" or "license" for more information. >>> with open("c:\\foo\\bar.txt", "w") as f: print("hi", file=f) ... >>> $ ls c:\foo\bar.txt From avigross at verizon.net Thu Nov 8 11:34:35 2018 From: avigross at verizon.net (Avi Gross) Date: Thu, 8 Nov 2018 11:34:35 -0500 Subject: [Tutor] best way to dynamically set class variables? In-Reply-To: References: <620D217E-2F98-4936-B607-622ECED1A8FF@wichmann.us> Message-ID: <005c01d47780$ef6c5300$ce44f900$@verizon.net> An interesting discussion that is outside the scope of a group like this is HOW malicious things can be done and perhaps how to avoid them. Obviously some contexts are totally uncontrolled. If you write a "calculator" that asks the user to type in an arbitrary string like "2*(3+5)" or "sin(30)" and execute that string and show the result, then they can slip in anything like a shell command to reformat the hard disk. What can you do to minimize risks in such situations? Obviously you might want to scan the string before executing and look for things like carriage returns and semi-colons that might be used to add continuation commands beyond what is asked for. You might limit the length of the string. You might scan for keywords like def and lambda. But note that a cursory scan like that has false positives as well as false negatives. You might recognize something within a character string context that is harmless or you might reject a valid SQL query because it used a word you disallow that is actually a harmless name of a data column. And, realistically, Python has so many ways to get around things that it gets silly. Given some room and ingenuity, you can create code that assembles individual characters and then executes them into a program so a scan may not reveal anything. Heck, if you can simply create a module on the disk somewhere, all you need to do is insert enough code to IMPORT the file and you can do pretty much anything. If asked to enter a calculator entry, for example, and you simply say: 5+3;import mymodule You then have an exec("5+3;import mymodule") Some such things may generate an error but only after the side effect is done. Python code is often wide open, by design, so subtle messing with internals is easy. As an example, you can change the search path for modules with an assignment statement and then any subsequent call for importing a named module gets the one you substituted. So, yes, executing random code can be dangerous. But life is dangerous -----Original Message----- From: Tutor On Behalf Of Alan Gauld via Tutor Sent: Thursday, November 8, 2018 5:52 AM To: tutor at python.org Subject: Re: [Tutor] best way to dynamically set class variables? On 08/11/2018 07:46, Peter Otten wrote: > By the way I don't think exec() is bad as long as you control its > input and as long as this input is fairly simple. Yes, but reading arbitrary column names from a database is not exactly controlled input... -- 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 sjeik_appie at hotmail.com Fri Nov 9 08:26:48 2018 From: sjeik_appie at hotmail.com (Albert-Jan Roskam) Date: Fri, 9 Nov 2018 13:26:48 +0000 Subject: [Tutor] best way to dynamically set class variables? In-Reply-To: <005c01d47780$ef6c5300$ce44f900$@verizon.net> Message-ID: On 8 Nov 2018 17:34, Avi Gross wrote: > What can you do to minimize risks in such > situations? ast.literal_eval, with a smallish maximum string length? https://docs.python.org/3/library/ast.html. (That's exactly the only ast function that know! :-) From tcynotebook at yahoo.com Fri Nov 9 08:12:53 2018 From: tcynotebook at yahoo.com (TCY) Date: Fri, 9 Nov 2018 13:12:53 +0000 (UTC) Subject: [Tutor] Require Python assistance In-Reply-To: <1312987070.1668633.1541769130895@mail.yahoo.com> References: <559547924.217977.1522157928257.ref@mail.yahoo.com> <559547924.217977.1522157928257@mail.yahoo.com> <535501093.682412.1525703505213@mail.yahoo.com> <1115632323.2127431.1525974328618@mail.yahoo.com> <356291793.2112245.1525974451505@mail.yahoo.com> <1178927576.2273072.1526545319227@mail.yahoo.com> <1274118091.2404057.1526572537730@mail.yahoo.com> <1900546590.2411865.1526572608440@mail.yahoo.com> <1520020048.6454366.1531917970317@mail.yahoo.com> <1991446395.6462588.1531918078283@mail.yahoo.com> <939831097.6473703.1531918176088@mail.yahoo.com> <329499635.939600.1531926156239@mail.yahoo.com> <887888139.7131023.1532005647139@mail.yahoo.com> <858260973.7119608.1532005735270@mail.yahoo.com> <1596215811.642466.1533145220950@mail.yahoo.com> <1136530566.916113.1533174815196@mail.yahoo.com> <1846147981.1374090.1533235452342@mail.yahoo.com> <1763216397.1381624.1533235523518@mail.yahoo.com> <265131015.1359394.1533235638102@mail.yahoo.com> <839631826.1383533.1533235772907@mail.yahoo.com> <1704775713.1349564.1533236940628@mail.yahoo.com> <841864469.1959726.1535044929232@mail.yahoo.com> <1725011459.3862453.1535386061662@mail.yahoo.com> <1328057060.3862336.1535386224961@mail.yahoo.com> <1349824934.4459547.1535456208361@mail.yahoo.com> <1169986303.4577673.1535470961020@mail.yahoo.com> <1011716056.4604560.1535471061509@mail.yahoo.com> <1019989840.315200.1536207834789@mail.yahoo.com> <363674840.316542.1536207927948@mail.yahoo.com> <1726655563.527481.1536243720931@mail.yahoo.com> <919572861.584145.1536244484841@mail.yahoo.com> <409597830.1343593.1536339293192@mail.yahoo.com> <1948110863.1330888.1536339313230@mail.yahoo.com> <2097347877.6010551.1538909820437@mail.yahoo.com> <1409403312.5993538.1538909845900@mail.yahoo.com> <867296533.5978921.1538909910075@mail.yahoo.com> <938838385.6029585.1538925371342@mail.yahoo.com> <1375833295.6054730.1538925418350@mail.yahoo.com> <655695312.3866452.1538925556609@mail.yahoo.com> <1519409035.6168187.1538944886747@mail.yahoo.com> <1531250281.6174255.1538944924593@mail.yahoo.com> <647621466.6143434.1538944986507@mail.yahoo.com> <1531311844.6149729.1538945411517@mail.yahoo.com> <564000330.6151491.1538945545811@mail.yahoo.com> <44273975.6157485.1538945591229@mail.yahoo.com> <535603501.2281223.1538945653354@mail.yahoo.com> <1146954955.6142706.1538945687177@mail.yahoo.com> <2063742995.6600837.1539013942312@mail.yahoo.com> <1321735552.6578529.1539014123698@mail.yahoo.com> <409182852.6588113.1539014163066@mail.yahoo.com> <2108740270.6574240.1539014206987@mail.yahoo.com> <1426138829.6579370.1539014232521@mail.yahoo.com> <229104365.6610868.1539014990159@mail.yahoo.com> <215598985.6594401.1539015110698@mail.yahoo.com> <931280594.6587172.1539015157471@mail.yahoo.com> <1562533037.6614878.1539015601323@mail.yahoo.com> <712177048.6595145.1539015666365@mail.yahoo.com> <95250898.6604794.1539016082624@mail.yahoo.com> <447337164.6629547.1539016620756@mail.yahoo.com> <320666540.6636269.1539016696756@mail.yahoo.com> <1315286861.7971689.1539181873842@mail.yahoo.com> <106829269.7987226.1539181965943@mail.yahoo.com> <626592298.7992539.1539182023374@mail.yahoo.com> <1238605342.7990886.1539183791524@mail.yahoo.com> <887027562.7994885.1539183808538@mail.yahoo.com> <583059277.8012976.1539183850160@mail.yahoo.com> <1678400883.8007410.1539184096012@mail.yahoo.com> <1290128819.10123425.1539517409185@mail.yahoo.com> <2065864438.10184690.1539531623534@mail.yahoo.com> <801829196.10641023.1539612930063@mail.yahoo.com> <1133805726.10651938.1539612979554@mail.yahoo.com> <2108831891.10646172.1539613458220@mail.yahoo.com> <1938639925.10646944.1539613519655@mail.yahoo.com> <1465913256.10667979.1539613543152@mail.yahoo.com> <2123077998.10655925.1539613616283@mail.yahoo.com> <1255077750.10651527.1539613720993@mail.yahoo.com> <1780259773.10675096.1539613746691@mail.yahoo.com> <402991640.10650128.1539613960651@mail.yahoo.com> <905795136.10681212.1539614090612@mail.yahoo.com> <1311505867.10658316.1539614123621@mail.yahoo.com> <394503757.10710774.1539616268734@mail.yahoo.com> <512692706.10701437.1539616484993@mail.yahoo.com> <212975096.11375328.1539698523843@mail.yahoo.com> <1623561268.11398655.1539698595468@mail.yahoo.com> <2076478963.5056472.1539698628334@mail.yahoo.com> <1041777507.11388158.1539698793539@mail.yahoo.com> <1352817248.11390222.1539698825940@mail.yahoo.com> <642441089.11389115.1539699159860@mail.yahoo.com> <140812712.11395013.1539699657906@mail.yahoo.com> <1990958742.11394498.1539699821052@mail.yahoo.com> <212291344.11395108.1539699977927@mail.yahoo.com> <2074604586.11403218.1539700468293@mail.yahoo.com> <1788338328.11392194.1539700732580@mail.yahoo.com> <2026801483.11411607.1539700813221@mail.yahoo.com> <1301548033.11440233.1539701265259@mail.yahoo.com> <72933360.11413788.1539701538638@mail.yahoo.com> <654474514.11414537.1539701621227@mail.yahoo.com> <1443091419.11803377.1539732343620@mail.yahoo.com> <1415683108.11785621.1539732427769@mail.yahoo.com> <869426798.12118701.1539782725602@mail.yahoo.com> <1344161280.12090270.1539782752328@mail.yahoo.com> <1906685632.12108403.1539782776211@mail.yahoo.com> <279784886.5748017.1539782842372@mail.yahoo.com> <2135834961.12096754.1539783163607@mail.yahoo.com> <1689626577.12096082.1539783297388@mail.yahoo.com> <970728499.12097767.1539783471743@mail.yahoo.com> <1388704479.12174759.1539788660504@mail.yahoo.com> <102484135.12170295.1539789052132@mail.yahoo.com> <1022812982.12850876.1539873434344@mail.yahoo.com> <1791990538.13814370.1539995266992@mail.yahoo.com> <947351404.13802318.1539995298320@mail.yahoo.com> <550769037.13848528.1539995316707@mail.yahoo.com> <1150437904.13846235.1539995384698@mail.yahoo.com> <1286563149.13831415.1539995572515@mail.yahoo.com> <1136181982.13828426.1539996303213@mail.yahoo.com> <456764249.13818488.1539996439369@mail.yahoo.com> <1676786498.13822050.1539996733674@mail.yahoo.com> <1657941522.20433.1539996791615@mail.yahoo.com> <881524561.13845276.1539997055694@mail.yahoo.com> <1209351765.966885.1541426837677@mail.yahoo.com> <62610152.949858.1541426922647@mail.yahoo.com> <741826051.958810.1541426994466@mail.yahoo.com> <907969922.959487.1541427049746@mail.yahoo.com> <1928989798.947795.1541427069402@mail.yahoo.com> <805706031.954684.1541427111213@mail.yahoo.com> <1478214179.964639.1541427301967@mail.yahoo.com> <339215607.487255.1541612866974@mail.yahoo.com> <945634632.486948.1541612931325@mail.yahoo.com> <692802749.493505.1541612961189@mail.yahoo.com> <1714983038.461533.1541612976011@mail.yahoo.com> <282176662.473715.1541613141271@mail.yahoo.com> <299338521.458967.1541613196035@mail.yahoo.com> <407872480.482917.1541613292445@mail.yahoo.com> <1789176467.483182.1541613590484@mail.yahoo.com> <486734590.470387.1541613726860@mail.yahoo.com> <1982581909.484799.1541613994501@mail.yahoo.com> <421299386.491204.1541614206826@mail.yahoo.com> <13799012.486674.1541614287613@mail.yahoo.com> <473020141.498945.1541614360816@mail.yahoo.com> <1449794414.514001.1541614659420@mail.yahoo.com> <1606537225.492285.1541615108761@mail.yahoo.com> <1260672116.507517.1541615420039@mail.yahoo.com> <1523313631.497036.1541615555698@mail.yahoo.com> <1982678908.518309.1541615812877@mail.yahoo.com> <84294836.496697.1541616245334@mail.yahoo.com> <1645376306.1091578.1541690420962@mail.yahoo.com> <774292797.1084321.1541690451970@mail.yahoo.com> <1246147888.1078101.1541690717243@mail.yahoo.com> <1960960185.1665775.1541768029030@mail.yahoo.com> <1093048974.1642162.1541768168731@mail.yahoo.com> <59871221.1665795.1541768293075@mail.yahoo.com> <1794659185.1663446.1541768342602@mail.yahoo.com> <103230789.1647883.1541768390276@mail.yahoo.com> <1828574620.1678085.1541768611863@mail.yahoo.com> <1169828473.1681738.1541768654595@mail.yahoo.com> <954001335.1655386.1541768697111@mail.yahoo.com> <376411895.1423855.1541768990250@mail.yahoo.com> <1312987070.1668633.1541769130895@mail.yahoo.com> Message-ID: <2073802151.1643370.1541769173825@mail.yahoo.com> Dear? ? ??May I know how to solve the cryptography?with?Python?programming language as below - (1) Implement Elgamal Method(2) Implement Elliptic Curve Cryptography method(3) Implement Rabin Method(4) Implement RSA Method Find?(a) Prime test?(b) Inverse function Please help me by provide your advice and suggestion so that?I can improve my computing skills (please see the attached file)? Prayerfully?? Tron Orino Yeong?tcynotebook at yahoo.com?0916643858 From bgailer at gmail.com Fri Nov 9 13:59:31 2018 From: bgailer at gmail.com (Bob Gailer) Date: Fri, 9 Nov 2018 13:59:31 -0500 Subject: [Tutor] Require Python assistance In-Reply-To: <2073802151.1643370.1541769173825@mail.yahoo.com> References: <559547924.217977.1522157928257.ref@mail.yahoo.com> <1315286861.7971689.1539181873842@mail.yahoo.com> <106829269.7987226.1539181965943@mail.yahoo.com> <626592298.7992539.1539182023374@mail.yahoo.com> <1238605342.7990886.1539183791524@mail.yahoo.com> <887027562.7994885.1539183808538@mail.yahoo.com> <583059277.8012976.1539183850160@mail.yahoo.com> <1678400883.8007410.1539184096012@mail.yahoo.com> <1290128819.10123425.1539517409185@mail.yahoo.com> <2065864438.10184690.1539531623534@mail.yahoo.com> <801829196.10641023.1539612930063@mail.yahoo.com> <1133805726.10651938.1539612979554@mail.yahoo.com> <2108831891.10646172.1539613458220@mail.yahoo.com> <1938639925.10646944.1539613519655@mail.yahoo.com> <1465913256.10667979.1539613543152@mail.yahoo.com> <2123077998.10655925.1539613616283@mail.yahoo.com> <1255077750.10651527.1539613720993@mail.yahoo.com> <1780259773.10675096.1539613746691@mail.yahoo.com> <402991640.10650128.1539613960651@mail.yahoo.com> <905795136.10681212.1539614090612@mail.yahoo.com> <1311505867.10658316.1539614123621@mail.yahoo.com> <394503757.10710774.1539616268734@mail.yahoo.com> <512692706.10701437.1539616484993@mail.yahoo.com> <212975096.11375328.1539698523843@mail.yahoo.com> <1623561268.11398655.1539698595468@mail.yahoo.com> <2076478963.5056472.1539698628334@mail.yahoo.com> <1041777507.11388158.1539698793539@mail.yahoo.com> <1352817248.11390222.1539698825940@mail.yahoo.com> <642441089.11389115.1539699159860@mail.yahoo.com> <140812712.11395013.1539699657906@mail.yahoo.com> <1990958742.11394498.1539699821052@mail.yahoo.com> <212291344.11395108.1539699977927@mail.yahoo.com> <2074604586.11403218.1539700468293@mail.yahoo.com> <1788338328.11392194.1539700732580@mail.yahoo.com> <2026801483.11411607.1539700813221@mail.yahoo.com> <1301548033.11440233.1539701265259@mail.yahoo.com> <72933360.11413788.1539701538638@mail.yahoo.com> <654474514.11414537.1539701621227@mail.yahoo.com> <1443091419.11803377.1539732343620@mail.yahoo.com> <1415683108.11785621.1539732427769@mail.yahoo.com> <869426798.12118701.1539782725602@mail.yahoo.com> <1344161280.12090270.1539782752328@mail.yahoo.com> <1906685632.12108403.1539782776211@mail.yahoo.com> <279784886.5748017.1539782842372@mail.yahoo.com> <2135834961.12096754.1539783163607@mail.yahoo.com> <1689626577.12096082.1539783297388@mail.yahoo.com> <970728499.12097767.1539783471743@mail.yahoo.com> <1388704479.12174759.1539788660504@mail.yahoo.com> <102484135.12170295.1539789052132@mail.yahoo.com> <1022812982.12850876.1539873434344@mail.yahoo.com> <1791990538.13814370.1539995266992@mail.yahoo.com> <947351404.13802318.1539995298320@mail.yahoo.com> <550769037.13848528.1539995316707@mail.yahoo.com> <1150437904.13846235.1539995384698@mail.yahoo.com> <1286563149.13831415.1539995572515@mail.yahoo.com> <1136181982.13828426.1539996303213@mail.yahoo.com> <456764249.13818488.1539996439369@mail.yahoo.com> <1676786498.13822050.1539996733674@mail.yahoo.com> <1657941522.20433.1539996791615@mail.yahoo.com> <881524561.13845276.1539997055694@mail.yahoo.com> <1209351765.966885.1541426837677@mail.yahoo.com> <62610152.949858.1541426922647@mail.yahoo.com> <741826051.958810.1541426994466@mail.yahoo.com> <907969922.959487.1541427049746@mail.yahoo.com> <1928989798.947795.1541427069402@mail.yahoo.com> <805706031.954684.1541427111213@mail.yahoo.com> <1478214179.964639.1541427301967@mail.yahoo.com> <339215607.487255.1541612866974@mail.yahoo.com> <945634632.486948.1541612931325@mail.yahoo.com> <692802749.493505.1541612961189@mail.yahoo.com> <1714983038.461533.1541612976011@mail.yahoo.com> <282176662.473715.1541613141271@mail.yahoo.com> <299338521.458967.1541613196035@mail.yahoo.com> <407872480.482917.1541613292445@mail.yahoo.com> <1789176467.483182.1541613590484@mail.yahoo.com> <486734590.470387.1541613726860@mail.yahoo.com> <1982581909.484799.1541613994501@mail.yahoo.com> <421299386.491204.1541614206826@mail.yahoo.com> <13799012.486674.1541614287613@mail.yahoo.com> <473020141.498945.1541614360816@mail.yahoo.com> <1449794414.514001.1541614659420@mail.yahoo.com> <1606537225.492285.1541615108761@mail.yahoo.com> <1260672116.507517.1541615420039@mail.yahoo.com> <1523313631.497036.1541615555698@mail.yahoo.com> <1982678908.518309.1541615812877@mail.yahoo.com> <84294836.496697.1541616245334@mail.yahoo.com> <1645376306.1091578.1541690420962@mail.yahoo.com> <774292797.1084321.1541690451970@mail.yahoo.com> <1246147888.1078101.1541690717243@mail.yahoo.com> <1960960185.1665775.1541768029030@mail.yahoo.com> <1093048974.1642162.1541768168731@mail.yahoo.com> <59871221.1665795.1541768293075@mail.yahoo.com> <1794659185.1663446.1541768342602@mail.yahoo.com> <103230789.1647883.1541768390276@mail.yahoo.com> <1828574620.1678085.1541768611863@mail.yahoo.com> <1169828473.1681738.1541768654595@mail.yahoo.com> <954001335.1655386.1541768697111@mail.yahoo.com> <376411895.1423855.1541768990250@mail.yahoo.com> <1312987070.1668633.1541769130895@mail.yahoo.com> <2073802151.1643370.1541769173825@mail.yahoo.com> Message-ID: On Nov 9, 2018 12:34 PM, "TCY via Tutor" wrote: > > > > > Dear > May I know how to solve the cryptography with Python programming language as below - > (1) Implement Elgamal Method(2) Implement Elliptic Curve Cryptography method(3) Implement Rabin Method(4) Implement RSA Method > Find (a) Prime test (b) Inverse function This list does not send attachments. If the file is not large just send it as part of the email body. As you can see the text above came thru as 1 long line. I presume when you sent it it looked like 4 lines. If that is the case you need to send the email as plain text rather than some fancy formatted way. > Please help me by provide your advice Learn how to ask questions that are more precise and specific. "Solve the cryptography" doesn't tell me anything. The more clearly you state your objectives the easier it becomes for us to help you. Google is your friend here. For example try searching Google for "Elliptic Curve Cryptography python" and suggestion so that I can improve my computing skills (please see the attached file) Bob Gailer From mats at wichmann.us Fri Nov 9 14:10:15 2018 From: mats at wichmann.us (Mats Wichmann) Date: Fri, 9 Nov 2018 12:10:15 -0700 Subject: [Tutor] Require Python assistance In-Reply-To: References: <559547924.217977.1522157928257.ref@mail.yahoo.com> <1523313631.497036.1541615555698@mail.yahoo.com> <1982678908.518309.1541615812877@mail.yahoo.com> <84294836.496697.1541616245334@mail.yahoo.com> <1645376306.1091578.1541690420962@mail.yahoo.com> <774292797.1084321.1541690451970@mail.yahoo.com> <1246147888.1078101.1541690717243@mail.yahoo.com> <1960960185.1665775.1541768029030@mail.yahoo.com> <1093048974.1642162.1541768168731@mail.yahoo.com> <59871221.1665795.1541768293075@mail.yahoo.com> <1794659185.1663446.1541768342602@mail.yahoo.com> <103230789.1647883.1541768390276@mail.yahoo.com> <1828574620.1678085.1541768611863@mail.yahoo.com> <1169828473.1681738.1541768654595@mail.yahoo.com> <954001335.1655386.1541768697111@mail.yahoo.com> <376411895.1423855.1541768990250@mail.yahoo.com> <1312987070.1668633.1541769130895@mail.yahoo.com> <2073802151.1643370.1541769173825@mail.yahoo.com> Message-ID: <5c026d2e-c245-0154-7838-d3b987514b06@wichmann.us> On 11/9/18 11:59 AM, Bob Gailer wrote: > On Nov 9, 2018 12:34 PM, "TCY via Tutor" wrote: >> >> >> >> >> Dear >> May I know how to solve the cryptography with Python programming > language as below - >> (1) Implement Elgamal Method(2) Implement Elliptic Curve Cryptography > method(3) Implement Rabin Method(4) Implement RSA Method >> Find (a) Prime test (b) Inverse function > > This list does not send attachments. If the file is not large just send it > as part of the email body. > > As you can see the text above came thru as 1 long line. I presume when you > sent it it looked like 4 lines. If that is the case you need to send the > email as plain text rather than some fancy formatted way. > >> Please help me by provide your advice > > Learn how to ask questions that are more precise and specific. "Solve the > cryptography" doesn't tell me anything. The more clearly you state your > objectives the easier it becomes for us to help you. > > Google is your friend here. For example try searching Google for "Elliptic > Curve Cryptography python" > and suggestion so that I can improve my computing skills (please see the > attached file) There has been a *lot* of crypto work in Python. If your objective is to learn the techniques you might look at what others have done, if your objective is to use these techniques, there's a decent chance it has been already implemented. You could look, as a starting point (but by no means as the only resource), at the pycrypto package https://www.dlitz.net/software/pycrypto/ From steve at pearwood.info Fri Nov 9 19:03:20 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 10 Nov 2018 11:03:20 +1100 Subject: [Tutor] best way to dynamically set class variables? In-Reply-To: <005c01d47780$ef6c5300$ce44f900$@verizon.net> References: <620D217E-2F98-4936-B607-622ECED1A8FF@wichmann.us> <005c01d47780$ef6c5300$ce44f900$@verizon.net> Message-ID: <20181110000319.GN4071@ando.pearwood.info> On Thu, Nov 08, 2018 at 11:34:35AM -0500, Avi Gross wrote: > An interesting discussion that is outside the scope of a group like this is > HOW malicious things can be done and perhaps how to avoid them. > > Obviously some contexts are totally uncontrolled. If you write a > "calculator" that asks the user to type in an arbitrary string like > "2*(3+5)" or "sin(30)" and execute that string and show the result, then > they can slip in anything like a shell command to reformat the hard disk. If they want to reformat their own hard disk, there are much easier ways than slipping a shell command into a Python calculator. The risk, tiny as it is, is that something which starts life as a desktop application running on the user's own computer gets extracted out into a library used in a web application running on somebody else's server. So in that sense, it is better to avoid eval/exec even on locally-run desktop applications. But it is *critical* to avoid eval or exec on untrusted input running on a server. > What can you do to minimize risks in such situations? (1) Don't use eval or exec. (2) If you must use eval or exec, consider allowing only a whitelist of allowed commands. You can do that by specifying the global and local namespace arguments: ns = {'round': round, 'sin': math.sin, '__builtins__': None} eval(command, ns, ns) You must set __builtins__ to something, if it is missing, the interpreter will set it to the real builtins module and open the doors wide open. (3) Don't use eval or exec. (4) But even with a whitelist, it is remarkably easy to break out of the sandbox. Most(?) tricks for doing so involve using dunder (Double UNDERscore) attributes, so a quick palliative for this is to disallow any command that includes an underscore: if '_' in command: raise InvalidCommand('syntax error: no underscores allowed') else: eval(command, ns, ns) (5) Its okay to use eval and exec for your own use, at the interactive interpreter, or in quick scripts you use in a trusted environment. (6) But even if you successfully block all the escape tricks, the user can trivially DOS (Denial Of Service) your calculator web app: command = '(2**1000)**(2**1000)**(2**1000)' so you need to run it in an environment where evaluation will timeout after a certain amount of time, without using up all the memory on your server. (7) For experts, its okay to use eval or exec. Maybe. For a good use of exec, see the source code to namedtuple in the collections module. Or this: http://code.activestate.com/recipes/578918-yet-another-namedtuple/ -- Steve From avigross at verizon.net Fri Nov 9 15:17:18 2018 From: avigross at verizon.net (Avi Gross) Date: Fri, 9 Nov 2018 15:17:18 -0500 Subject: [Tutor] Require Python assistance In-Reply-To: <2073802151.1643370.1541769173825@mail.yahoo.com> References: <1312987070.1668633.1541769130895@mail.yahoo.com> <2073802151.1643370.1541769173825@mail.yahoo.com> Message-ID: <007301d47869$36c95390$a45bfab0$@verizon.net> As I see others have replied, the question is not easy to understand and seems so broad as to be overwhelming to actually do without further info. It would be easier if someone asked if we could direct him to resources that either explain the algorithms needed or to full-blown packages/modules they could easily incorporate. From the text shown, does it not seem like some kind of HW assignment perhaps to compare and contrast various methods? As usual, attachments cannot be seen. If someone is interested, you can ask, in situations like these, to be sent such things directly to your mail. Alternately, there are services out there that let you share files in the cloud for retrieval, sometimes even quite large files. Sending a link like that with your message might work, in moderation. The message indicates they want to improve their computing skills. That may suggest wanting to implement algorithms in basic Python from scratch, as compared to wanting packages that do it all for you. If I were asking for help, I have some things I would include that include spelling out what I need but also a synopsis of what I have done, such as go a search for: " python encryption package " or whatever. If it was an assignment in on page that might be worth mentioning. As I see it, not speaking for Alan, I see several kinds of useful purposes in tutoring. One is to help someone get started when they have no idea what resources to even look for. You may also want to ask questions to get them to spell out their need more clearly and perhaps figure out what to do. You then expect them to go away and do the work. If they come back, it should be with a more focused need and again, they should get advice and hints and maybe a brief example but not have the work done for them. If they have a bug, and have narrowed it down using print statements or debuggers or local help, and can reproduce it in a small example, maybe they can ask if others can detect some syntax error or logic error. I recently heard that Python 3.8 or so may add a new assignment operator (:=) so if they tried it and failed under existing versions, we might tell them to either not use it now or wait. This group is open and welcoming and should remain so. But it is reasonable to expect that answers and suggestions will not be given unless enough information is presented. I understand that attachments are not allowed for an assortment of reasons. The forum is hosted on python.org and space is limited. I could see creating a companion group such as pythontutorattachments at groups.yahoo.com where you could send messages with attachments for those who wish to subscribe with the main interaction remaining here or one-on-one. Back to the case in point. Can I assume all the encryption methods have been implemented in anything from algorithm pseudocode to in some programming language (perhaps other than Python) in a form you can read the source code? If so, assuming multilingual abilities, the problem becomes one of translating it into Python. But, of course, if the need is to make it work in all reasonable versions of Python, good luck! But before we are asked, SEARCH for things. A few well placed keywords can locate so much. Part of learning to be a programmer is to do as much as you can by yourself. -----Original Message----- From: Tutor On Behalf Of TCY via Tutor Sent: Friday, November 9, 2018 8:13 AM To: tutor at python.org Subject: [Tutor] Require Python assistance Dear May I know how to solve the cryptography with Python programming language as below - (1) Implement Elgamal Method(2) Implement Elliptic Curve Cryptography method(3) Implement Rabin Method(4) Implement RSA Method Find (a) Prime test (b) Inverse function Please help me by provide your advice and suggestion so that I can improve my computing skills (please see the attached file) Prayerfully Tron Orino Yeong tcynotebook at yahoo.com 0916643858 _______________________________________________ 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 Sat Nov 10 02:49:57 2018 From: sjeik_appie at hotmail.com (Albert-Jan Roskam) Date: Sat, 10 Nov 2018 07:49:57 +0000 Subject: [Tutor] best way to dynamically set class variables? In-Reply-To: <20181110000319.GN4071@ando.pearwood.info> Message-ID: On 10 Nov 2018 01:03, Steven D'Aprano wrote: On Thu, Nov 08, 2018 at 11:34:35AM -0500, Avi Gross wrote: > An interesting discussion that is outside the scope of a group like this is > HOW malicious things can be done and perhaps how to avoid them. > Isn't the rule, simply: this_is_stupid = eval(input("please enter malicious code: ")) ... and other uses range from 'code smell' to 'elegant' (where namedtuple is an example of the latter) From steve at pearwood.info Sat Nov 10 04:28:55 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 10 Nov 2018 20:28:55 +1100 Subject: [Tutor] Require Python assistance In-Reply-To: <007301d47869$36c95390$a45bfab0$@verizon.net> References: <1312987070.1668633.1541769130895@mail.yahoo.com> <2073802151.1643370.1541769173825@mail.yahoo.com> <007301d47869$36c95390$a45bfab0$@verizon.net> Message-ID: <20181110092855.GQ4071@ando.pearwood.info> On Fri, Nov 09, 2018 at 03:17:18PM -0500, Avi Gross wrote: > As I see others have replied, the question is not easy to understand > and seems so broad as to be overwhelming to actually do without > further info. My guess is that the OP is doing a comp sci course and has been given an assignment to write some crypto code. Notice the use of the word "implement" -- my guess is that the assignment is to implement these classic crypto algorithms as an exercise, rather than use an existing, polished crypto library. That is, it's homework. -- Steve From tcynotebook at yahoo.com Sat Nov 10 10:42:56 2018 From: tcynotebook at yahoo.com (TCY) Date: Sat, 10 Nov 2018 15:42:56 +0000 (UTC) Subject: [Tutor] Require Python assistance In-Reply-To: <2023936503.205211.1541864553101@mail.yahoo.com> References: <559547924.217977.1522157928257.ref@mail.yahoo.com> <559547924.217977.1522157928257@mail.yahoo.com> <535501093.682412.1525703505213@mail.yahoo.com> <1115632323.2127431.1525974328618@mail.yahoo.com> <356291793.2112245.1525974451505@mail.yahoo.com> <1178927576.2273072.1526545319227@mail.yahoo.com> <1274118091.2404057.1526572537730@mail.yahoo.com> <1900546590.2411865.1526572608440@mail.yahoo.com> <1520020048.6454366.1531917970317@mail.yahoo.com> <1991446395.6462588.1531918078283@mail.yahoo.com> <939831097.6473703.1531918176088@mail.yahoo.com> <329499635.939600.1531926156239@mail.yahoo.com> <887888139.7131023.1532005647139@mail.yahoo.com> <858260973.7119608.1532005735270@mail.yahoo.com> <1596215811.642466.1533145220950@mail.yahoo.com> <1136530566.916113.1533174815196@mail.yahoo.com> <1846147981.1374090.1533235452342@mail.yahoo.com> <1763216397.1381624.1533235523518@mail.yahoo.com> <265131015.1359394.1533235638102@mail.yahoo.com> <839631826.1383533.1533235772907@mail.yahoo.com> <1704775713.1349564.1533236940628@mail.yahoo.com> <841864469.1959726.1535044929232@mail.yahoo.com> <1725011459.3862453.1535386061662@mail.yahoo.com> <1328057060.3862336.1535386224961@mail.yahoo.com> <1349824934.4459547.1535456208361@mail.yahoo.com> <1169986303.4577673.1535470961020@mail.yahoo.com> <1011716056.4604560.1535471061509@mail.yahoo.com> <1019989840.315200.1536207834789@mail.yahoo.com> <363674840.316542.1536207927948@mail.yahoo.com> <1726655563.527481.1536243720931@mail.yahoo.com> <919572861.584145.1536244484841@mail.yahoo.com> <409597830.1343593.1536339293192@mail.yahoo.com> <1948110863.1330888.1536339313230@mail.yahoo.com> <2097347877.6010551.1538909820437@mail.yahoo.com> <1409403312.5993538.1538909845900@mail.yahoo.com> <867296533.5978921.1538909910075@mail.yahoo.com> <938838385.6029585.1538925371342@mail.yahoo.com> <1375833295.6054730.1538925418350@mail.yahoo.com> <655695312.3866452.1538925556609@mail.yahoo.com> <1519409035.6168187.1538944886747@mail.yahoo.com> <1531250281.6174255.1538944924593@mail.yahoo.com> <647621466.6143434.1538944986507@mail.yahoo.com> <1531311844.6149729.1538945411517@mail.yahoo.com> <564000330.6151491.1538945545811@mail.yahoo.com> <44273975.6157485.1538945591229@mail.yahoo.com> <535603501.2281223.1538945653354@mail.yahoo.com> <1146954955.6142706.1538945687177@mail.yahoo.com> <2063742995.6600837.1539013942312@mail.yahoo.com> <1321735552.6578529.1539014123698@mail.yahoo.com> <409182852.6588113.1539014163066@mail.yahoo.com> <2108740270.6574240.1539014206987@mail.yahoo.com> <1426138829.6579370.1539014232521@mail.yahoo.com> <229104365.6610868.1539014990159@mail.yahoo.com> <215598985.6594401.1539015110698@mail.yahoo.com> <931280594.6587172.1539015157471@mail.yahoo.com> <1562533037.6614878.1539015601323@mail.yahoo.com> <712177048.6595145.1539015666365@mail.yahoo.com> <95250898.6604794.1539016082624@mail.yahoo.com> <447337164.6629547.1539016620756@mail.yahoo.com> <320666540.6636269.1539016696756@mail.yahoo.com> <1315286861.7971689.1539181873842@mail.yahoo.com> <106829269.7987226.1539181965943@mail.yahoo.com> <626592298.7992539.1539182023374@mail.yahoo.com> <1238605342.7990886.1539183791524@mail.yahoo.com> <887027562.7994885.1539183808538@mail.yahoo.com> <583059277.8012976.1539183850160@mail.yahoo.com> <1678400883.8007410.1539184096012@mail.yahoo.com> <1290128819.10123425.1539517409185@mail.yahoo.com> <2065864438.10184690.1539531623534@mail.yahoo.com> <801829196.10641023.1539612930063@mail.yahoo.com> <1133805726.10651938.1539612979554@mail.yahoo.com> <2108831891.10646172.1539613458220@mail.yahoo.com> <1938639925.10646944.1539613519655@mail.yahoo.com> <1465913256.10667979.1539613543152@mail.yahoo.com> <2123077998.10655925.1539613616283@mail.yahoo.com> <1255077750.10651527.1539613720993@mail.yahoo.com> <1780259773.10675096.1539613746691@mail.yahoo.com> <402991640.10650128.1539613960651@mail.yahoo.com> <905795136.10681212.1539614090612@mail.yahoo.com> <1311505867.10658316.1539614123621@mail.yahoo.com> <394503757.10710774.1539616268734@mail.yahoo.com> <512692706.10701437.1539616484993@mail.yahoo.com> <212975096.11375328.1539698523843@mail.yahoo.com> <1623561268.11398655.1539698595468@mail.yahoo.com> <2076478963.5056472.1539698628334@mail.yahoo.com> <1041777507.11388158.1539698793539@mail.yahoo.com> <1352817248.11390222.1539698825940@mail.yahoo.com> <642441089.11389115.1539699159860@mail.yahoo.com> <140812712.11395013.1539699657906@mail.yahoo.com> <1990958742.11394498.1539699821052@mail.yahoo.com> <212291344.11395108.1539699977927@mail.yahoo.com> <2074604586.11403218.1539700468293@mail.yahoo.com> <1788338328.11392194.1539700732580@mail.yahoo.com> <2026801483.11411607.1539700813221@mail.yahoo.com> <1301548033.11440233.1539701265259@mail.yahoo.com> <72933360.11413788.1539701538638@mail.yahoo.com> <654474514.11414537.1539701621227@mail.yahoo.com> <1443091419.11803377.1539732343620@mail.yahoo.com> <1415683108.11785621.1539732427769@mail.yahoo.com> <869426798.12118701.1539782725602@mail.yahoo.com> <1344161280.12090270.1539782752328@mail.yahoo.com> <1906685632.12108403.1539782776211@mail.yahoo.com> <279784886.5748017.1539782842372@mail.yahoo.com> <2135834961.12096754.1539783163607@mail.yahoo.com> <1689626577.12096082.1539783297388@mail.yahoo.com> <970728499.12097767.1539783471743@mail.yahoo.com> <1388704479.12174759.1539788660504@mail.yahoo.com> <102484135.12170295.1539789052132@mail.yahoo.com> <1022812982.12850876.1539873434344@mail.yahoo.com> <1791990538.13814370.1539995266992@mail.yahoo.com> <947351404.13802318.1539995298320@mail.yahoo.com> <550769037.13848528.1539995316707@mail.yahoo.com> <1150437904.13846235.1539995384698@mail.yahoo.com> <1286563149.13831415.1539995572515@mail.yahoo.com> <1136181982.13828426.1539996303213@mail.yahoo.com> <456764249.13818488.1539996439369@mail.yahoo.com> <1676786498.13822050.1539996733674@mail.yahoo.com> <1657941522.20433.1539996791615@mail.yahoo.com> <881524561.13845276.1539997055694@mail.yahoo.com> <1209351765.966885.1541426837677@mail.yahoo.com> <62610152.949858.1541426922647@mail.yahoo.com> <741826051.958810.1541426994466@mail.yahoo.com> <907969922.959487.1541427049746@mail.yahoo.com> <1928989798.947795.1541427069402@mail.yahoo.com> <805706031.954684.1541427111213@mail.yahoo.com> <1478214179.964639.1541427301967@mail.yahoo.com> <339215607.487255.1541612866974@mail.yahoo.com> <945634632.486948.1541612931325@mail.yahoo.com> <692802749.493505.1541612961189@mail.yahoo.com> <1714983038.461533.1541612976011@mail.yahoo.com> <282176662.473715.1541613141271@mail.yahoo.com> <299338521.458967.1541613196035@mail.yahoo.com> <407872480.482917.1541613292445@mail.yahoo.com> <1789176467.483182.1541613590484@mail.yahoo.com> <486734590.470387.1541613726860@mail.yahoo.com> <1982581909.484799.1541613994501@mail.yahoo.com> <421299386.491204.1541614206826@mail.yahoo.com> <13799012.486674.1541614287613@mail.yahoo.com> <473020141.498945.1541614360816@mail.yahoo.com> <1449794414.514001.1541614659420@mail.yahoo.com> <1606537225.492285.1541615108761@mail.yahoo.com> <1260672116.507517.1541615420039@mail.yahoo.com> <1523313631.497036.1541615555698@mail.yahoo.com> <1982678908.518309.1541615812877@mail.yahoo.com> <84294836.496697.1541616245334@mail.yahoo.com> <1645376306.1091578.1541690420962@mail.yahoo.com> <774292797.1084321.1541690451970@mail.yahoo.com> <1246147888.1078101.1541690717243@mail.yahoo.com> <1960960185.1665775.1541768029030@mail.yahoo.com> <1093048974.1642162.1541768168731@mail.yahoo.com> <59871221.1665795.1541768293075@mail.yahoo.com> <1794659185.1663446.1541768342602@mail.yahoo.com> <103230789.1647883.1541768390276@mail.yahoo.com> <1828574620.1678085.1541768611863@mail.yahoo.com> <1169828473.1681738.1541768654595@mail.yahoo.com> <954001335.1655386.1541768697111@mail.yahoo.com> <376411895.1423855.1541768990250@mail.yahoo.com> <1312987070.1668633.1541769130895@mail.yahoo.com> <2073802151.1643370.1541769173825@mail.yahoo.com> <1891643093.1656174.1541769428253@mail.yahoo.com> <383698911.1689265.1541769759148@mail.yahoo.com> <1289381218.1661302.1541770091412@mail.yahoo.com> <1207224595.1671150.1541770356402@mail.yahoo.com> <8854105.1692419.1541770450521@mail.yahoo.com> <2101456153.1684741.1541770711560@mail.yahoo.com> <1872411493.1690388.1541770848505@mail.yahoo.com> <2044104113.1670546.1541771357985@mail.yahoo.com> <1818741729.1673474.1541771500836@mail.yahoo.com> <2023936503.205211.1541864553101@mail.yahoo.com> Message-ID: <1495221197.198339.1541864576514@mail.yahoo.com> Dear? ? ??May I know how to solve the cryptography?with?Python?programming language as below - (1) Implement Elgamal Method(2) Implement Elliptic Curve Cryptography method(3) Implement Rabin Method(4) Implement RSA Method Find?(a) Prime test?(b) Inverse function Please help me by provide your advice and suggestion so that?I can improve my computing skills (please see the attached file)? Prayerfully?? Tron Orino Yeong?tcynotebook at yahoo.com?0916643858 From avigross at verizon.net Sat Nov 10 13:10:06 2018 From: avigross at verizon.net (Avi Gross) Date: Sat, 10 Nov 2018 13:10:06 -0500 Subject: [Tutor] Require Python assistance In-Reply-To: <1495221197.198339.1541864576514@mail.yahoo.com> References: <2023936503.205211.1541864553101@mail.yahoo.com> <1495221197.198339.1541864576514@mail.yahoo.com> Message-ID: <008501d47920$9c878af0$d596a0d0$@verizon.net> WARNING to any that care: As the following letter is a repeat request without any hint they read the earlier comments here, I did a little searching and see very much the same request on another forum asking how to do this in MATLAB: https://forum.allaboutcircuits.com/threads/matlab-programming-language-for-cryptography.153704/ """ Dear May I know how to solve the cryptography with Matlab programming language as below - (1) Implement Elgamal Method (2) Implement Elliptic Curve Cryptography method (3) Implement RSA Method (4) Implement Rabin Method Find (a) Prime test (b) Inverse function Please help me by provide your advice and suggestion so that I can improve my computing skills (please see the attached file) Prayerfully """ I note someone there also wonders if this is HW. And, it is formatted better on multiple lines as compared to our version. If I had to guess, it might be another sort of HW as in a Cryptography course or Analysis of Algorithms and the person is wanting a way to implement it, not practice any particular language. Many people do not speak English as a native language and we can make allowances while trying to understand what they want. But the above looks like a form-letter starting with DEAR and leaving room for the name(s) of the intended recipients and ends with the slightly different ending of Prayerfully as compared to Sincerely or Thanks in Advance and so on and no signature. I am not sure what to make of this and am simply going to ignore the request as it seems to be a shotgun approach ... Many problems can be solved in multiple languages. But if the request had simply been to point them to information on the cryptographic methods, it would not fit well in a list about problems with doing things in Python. -----Original Message----- From: Tutor On Behalf Of TCY via Tutor Sent: Saturday, November 10, 2018 10:43 AM To: tutor at python.org Subject: [Tutor] Require Python assistance Dear May I know how to solve the cryptography with Python programming language as below - (1) Implement Elgamal Method(2) Implement Elliptic Curve Cryptography method(3) Implement Rabin Method(4) Implement RSA Method Find (a) Prime test (b) Inverse function Please help me by provide your advice and suggestion so that I can improve my computing skills (please see the attached file) Prayerfully Tron Orino Yeong tcynotebook at yahoo.com 0916643858 _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From asad.hasan2004 at gmail.com Sun Nov 11 01:49:36 2018 From: asad.hasan2004 at gmail.com (Asad) Date: Sun, 11 Nov 2018 12:19:36 +0530 Subject: [Tutor] Example for read and readlines() Message-ID: Hi All , If I am loading a logfile what should I use from the option 1,2,3 f3 = open ( r"/a/b/c/d/test/test_2814__2018_10_05_12_12_45/logA.log", 'r' ) 1) should only iterate over f3 2) st = f3.read() Should iterate over st 3) st1 = f3.readlines() Should iterate over st1 How are the above options different it they are not can there be some examples to describe in which situations should we each method . Thanks, -- Asad Hasan +91 9582111698 From steve at pearwood.info Sun Nov 11 04:40:49 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 11 Nov 2018 20:40:49 +1100 Subject: [Tutor] Example for read and readlines() In-Reply-To: References: Message-ID: <20181111094048.GX4071@ando.pearwood.info> On Sun, Nov 11, 2018 at 12:19:36PM +0530, Asad wrote: > Hi All , > > If I am loading a logfile what should I use from the option 1,2,3 Depends what you want to do. I assume that the log file is formatted into lines of text, so you probably want to iterate over each line. with open(filename, 'r') as f: for line in f: process(line) is the best idiom to use for line-by-line iteration. It only reads each line as needed, not all at once, so it can handle huge files even if the file is bigger than the memory you have. > f3 = open ( r"/a/b/c/d/test/test_2814__2018_10_05_12_12_45/logA.log", 'r' ) Don't use raw strings r"..." for pathnames. > 1) should only iterate over f3 > > 2) st = f3.read() Use this if you want to iterate over the file character by character, after reading the entire file into memory at once. > 3) st1 = f3.readlines() Use this if you want to read all the lines into memory at once. -- Steve From alan.gauld at yahoo.co.uk Sun Nov 11 04:53:23 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 11 Nov 2018 09:53:23 +0000 Subject: [Tutor] Require Python assistance In-Reply-To: <008501d47920$9c878af0$d596a0d0$@verizon.net> References: <2023936503.205211.1541864553101@mail.yahoo.com> <1495221197.198339.1541864576514@mail.yahoo.com> <008501d47920$9c878af0$d596a0d0$@verizon.net> Message-ID: On 10/11/2018 18:10, Avi Gross wrote: > WARNING to any that care: > > As the following letter is a repeat request without any hint they read the earlier comments here, I did a little searching and see very much the same request on another forum asking how to do this in MATLAB: The OP has also repeated posted the same message to this list (which I rejected as moderator). -- 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 Nov 11 05:00:33 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 11 Nov 2018 10:00:33 +0000 Subject: [Tutor] Example for read and readlines() In-Reply-To: References: Message-ID: On 11/11/2018 06:49, Asad wrote: > Hi All , > > If I am loading a logfile what should I use from the option 1,2,3 > > f3 = open ( r"/a/b/c/d/test/test_2814__2018_10_05_12_12_45/logA.log", 'r' ) > > 1) should only iterate over f3 This is best for processing line by line which is the most common way to handle files. It saves memory and allows you to exit early, without reading the entire file if you are only looking for say a single entry. for line in file: if terminal_Condition: break # process line here > 2) st = f3.read() The best solution if you want to process individual characters or small character groups. Also best if you want to process the entire file at once, for example using a regular expression which might span lines. > 3) st1 = f3.readlines() Mainly historical and superseded by iterating over the file. But sometimes useful if you need to do multiple passes over the lines since it only reads the file once. Very heavy memory footprint for big files. -- 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 Nov 11 05:02:40 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 11 Nov 2018 10:02:40 +0000 Subject: [Tutor] Example for read and readlines() In-Reply-To: <20181111094048.GX4071@ando.pearwood.info> References: <20181111094048.GX4071@ando.pearwood.info> Message-ID: On 11/11/2018 09:40, Steven D'Aprano wrote: >> f3 = open ( r"/a/b/c/d/test/test_2814__2018_10_05_12_12_45/logA.log", 'r' ) > > Don't use raw strings r"..." for pathnames. Umm, Why not? -- 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 asad.hasan2004 at gmail.com Sun Nov 11 05:04:35 2018 From: asad.hasan2004 at gmail.com (Asad) Date: Sun, 11 Nov 2018 15:34:35 +0530 Subject: [Tutor] Example for read and readlines() In-Reply-To: References: Message-ID: Hi All, thanks for the reply so to put into context say I have a file logfile formatted in text lines 1) and I want to extract the start time , error number and end time from this logfile so in this case what should I use I guess option 1 : with open(filename, 'r') as f: for line in f: process(line) should be fine or any other option ? 2) Another case is a text formatted logfile and I want to print (n-4) lines n is the line where error condition was encountered . 3) Do we need to ensure that each line in the logfile ends with \n . \n is not visible so can we verify in someway to proof EOL \n is placed in the file . Thanks, > > > > > > ---------- Forwarded message ---------- > From: Asad > To: tutor at python.org > Cc: > Bcc: > Date: Sun, 11 Nov 2018 12:19:36 +0530 > Subject: [Tutor] Example for read and readlines() > Hi All , > > If I am loading a logfile what should I use from the option 1,2,3 > > f3 = open ( r"/a/b/c/d/test/test_2814__2018_10_05_12_12_45/logA.log", 'r' ) > > 1) should only iterate over f3 > > 2) st = f3.read() > Should iterate over st > > > 3) st1 = f3.readlines() > > Should iterate over st1 > > How are the above options different it they are not can there be some > examples to describe in which situations should we each method . > > > Thanks, > > -- > Asad Hasan > +91 9582111698 > > > > > ---------- Forwarded message ---------- > From: "Steven D'Aprano" > To: tutor at python.org > Cc: > Bcc: > Date: Sun, 11 Nov 2018 20:40:49 +1100 > Subject: Re: [Tutor] Example for read and readlines() > On Sun, Nov 11, 2018 at 12:19:36PM +0530, Asad wrote: > > Hi All , > > > > If I am loading a logfile what should I use from the option > 1,2,3 > > Depends what you want to do. I assume that the log file is formatted > into lines of text, so you probably want to iterate over each line. > > with open(filename, 'r') as f: > for line in f: > process(line) > > is the best idiom to use for line-by-line iteration. It only reads each > line as needed, not all at once, so it can handle huge files even if the > file is bigger than the memory you have. > > > > f3 = open ( r"/a/b/c/d/test/test_2814__2018_10_05_12_12_45/logA.log", > 'r' ) > > Don't use raw strings r"..." for pathnames. > > > > 1) should only iterate over f3 > > > > 2) st = f3.read() > > Use this if you want to iterate over the file character by character, > after reading the entire file into memory at once. > > > > 3) st1 = f3.readlines() > > Use this if you want to read all the lines into memory at once. > > > > -- > Steve > > _______________________________________________ > Tutor maillist - Tutor at python.org > https://mail.python.org/mailman/listinfo/tutor > -- Asad Hasan +91 9582111698 From alan.gauld at yahoo.co.uk Sun Nov 11 09:46:18 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 11 Nov 2018 14:46:18 +0000 Subject: [Tutor] Example for read and readlines() In-Reply-To: References: Message-ID: On 11/11/2018 10:04, Asad wrote: > 1) and I want to extract the start time , error number and end > time from this logfile so in this case what should I use I guess option 1 : > > with open(filename, 'r') as f: > for line in f: > process(line) Yes, that woyuld be the best choice in that scenario. > 2) Another case is a text formatted logfile and I want to print (n-4) > lines n is the line where error condition was encountered . In that case you could use readlines() if it is a small file. Or you could save the last 5 lines and print those each time you find an error line. You should probably write a function to save the line since it needs to move the previous lines up one. buffer = ['','','','',''] def saveLine(line, buff): buff[0] = buff[1] buff[1] = buff[2] buff[2] = buff[3] buff[3] = buff[4] buff[4] = line for line in file: saveLine(line,buffer) if error_condition: printBuffer() readlines is simpler but stores the entire file in memory. The buffer saves memory but requires some extra processing to save/print. There are some modules for handling cyclic stores etc but in a simple case like this they are probably overkill. > 3) Do we need to ensure that each line in the logfile ends with \n . > > \n is not visible so can we verify in someway to proof EOL \n is placed > in the file . textfile lines are defined by the existence of the \n so both readlines() and a loop over the file will both read multiple lines if a \n is missing. You could use read() and a regex to check for some text marker and insert the newlines. This would be best if the whole file had them missing. If it just an occasional line then you can iterate over the file as usual and check each line for a missing \n and insert (or split) as needed. You might want to write the modified lines back to a new file. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From avigross at verizon.net Sun Nov 11 13:11:55 2018 From: avigross at verizon.net (Avi Gross) Date: Sun, 11 Nov 2018 13:11:55 -0500 Subject: [Tutor] saveLine Message-ID: <006e01d479ea$0951f020$1bf5d060$@verizon.net> Alan and others have answered the questions posed and what I am asking now is to look at the function he proposed to keep track of the last five lines. There is nothing wrong with it but I wonder what alternatives people would prefer. His code is made for exactly 5 lines to be buffered and is quite efficient. But what if you wanted N lines buffered, perhaps showing a smaller number of lines on some warnings or errors and the full N in other cases? Here is Alan's code for comparison: buffer = ['','','','',''] def saveLine(line, buff): buff[0] = buff[1] buff[1] = buff[2] buff[2] = buff[3] buff[3] = buff[4] buff[4] = line Again, that works fine. If N was not 5, I would suggest initializing the buffer might look like this: buffed = 5 buffer = [''] * buffed Then instead of changing the list in place, I might change the function to return the new string created by taking the substring containing all but the first that is then concatenated with the new entry: def saveLineN(line, buff): buff = buff[1:] + [line] return buff Clearly less efficient but more general. And, yes, the return statement could be the entire function as in: def saveLineN(line, buff): return buff[1:] + [line] Here is a transcript of it running: >>> buffer ['', '', '', '', ''] >>> saveLineN('a', buffer) ['', '', '', '', 'a'] >>> buffer = saveLineN('a', buffer) >>> buffer ['', '', '', '', 'a'] >>> buffer = saveLineN('b', buffer) >>> buffer = saveLineN('c', buffer) >>> buffer = saveLineN('d', buffer) >>> buffer = saveLineN('e', buffer) >>> buffer ['a', 'b', 'c', 'd', 'e'] >>> buffer = saveLineN('6th', buffer) >>> buffer ['b', 'c', 'd', 'e', '6th'] So perhaps using in-line changes might make sense. Buff.pop(0) would remove the zeroeth item with the side effect of returning the first item to be ignored. >>> buffer = ['a', 'b', 'c', 'd', 'e'] >>> buffer.pop(0) 'a' >>> buffer ['b', 'c', 'd', 'e'] And it can be extended in-line: >>> buffer.append('6th') >>> buffer ['b', 'c', 'd', 'e', '6th'] Sorry, I mean appended, not extended! LOL! So to make this compact and less wasteful, I consider using del buffer[0]: >>> del buffer[0] >>> buffer ['b', 'c', 'd', 'e'] So here is this version that might be more efficient. It deletes the first item/line of the buffer then adds a new nth in-line: def saveLineN2(line, buff): del buff[0] buff.append(line) Here is a transcript of it in use, using N=3 to be different: >>> buffed = 3 >>> buffer = [''] * buffed >>> buffer ['', '', ''] >>> saveLineN2('First Line\n', buffer) >>> buffer ['', '', 'First Line\n'] >>> saveLineN2('Second Line\n', buffer) >>> saveLineN2('Third Line\n', buffer) >>> buffer ['First Line\n', 'Second Line\n', 'Third Line\n'] >>> saveLineN2('nth Line\n', buffer) >>> buffer ['Second Line\n', 'Third Line\n', 'nth Line\n'] I can think of many other ways to do this, arguably some are more weird than others. There is the obvious one which does all the changes in one line as in: buff[0],buff[1],buff[2] = buff[1],buff[2],line Of course, for 5 you change that a bit. Might even be a tad more efficient. There is also the odd concept of not scrolling along but dealing with things at print time. I mean you can have a list with 1:N entries and a variable that holds an index from 1 to N. You store a new line at buffer[index] each time then you increment index modulo N. This resets it to 0 periodically. At print time, you print buffer[index:] + buffer[:index] and you have the same result. Does anyone have comments on what methods may be better for some purposes or additional ways to do this? I mean besides reading all lines into memory and holding on to them and indexing backwards. For that matter, you can revisit the question of using a list of lines and consider a dictionary variant which might work better for larger values of N. No need to slide a buffer window along, just maintain a modular index and overwrite the key value as in buffer[index] = line One more comment, if I may. Alan mentions but does not define a printBuffer() function. Using any of the methods above, you can end up with an error happening early on so the kind of fixed-length buffer mentioned above contains blank entries (not even a '\n') so it probably should suppress printing items of zero length or that are empty. And, in some of the methods shown above, it may be worth starting with an empty buffer and adding lines up to some N and only then removing the first entry each time. That would complicate the code a bit but make printing trivial. From __peter__ at web.de Sun Nov 11 14:43:11 2018 From: __peter__ at web.de (Peter Otten) Date: Sun, 11 Nov 2018 20:43:11 +0100 Subject: [Tutor] saveLine References: <006e01d479ea$0951f020$1bf5d060$@verizon.net> Message-ID: Avi Gross wrote: > Alan and others have answered the questions posed and what I am asking now > is to look at the function he proposed to keep track of the last five > lines. > > There is nothing wrong with it but I wonder what alternatives people would > prefer. His code is made for exactly 5 lines to be buffered and is quite > efficient. But what if you wanted N lines buffered, perhaps showing a > smaller number of lines on some warnings or errors and the full N in other > cases? The standard library features collections.deque. With that: buffer = collections.deque(maxlen=N) save_line = buffer.append This will start with an empty buffer. To preload the buffer: buffer = collections.deque(itertools.repeat("", N), maxlen=N) To print the buffer: print_buffer = sys.stdout.writelines or, more general: def print_buffer(items, end=""): for item in items: print(item, end=end) Also, for smallish N: def print_buffer(items, end=""): print(*items, sep=end) From 5familyobrien at gmail.com Sun Nov 11 18:00:00 2018 From: 5familyobrien at gmail.com (Stealth Fleet) Date: Sun, 11 Nov 2018 15:00:00 -0800 Subject: [Tutor] (no subject) Message-ID: tokenAmount = input( "How many tokens would you like to buy or cash in?: ") print (tokenAmount) def buy (): if tokenAmount <= 400: buy = tokenAmount * .2099 print( " You would like to spend $"+(buy) + "on" + (tokenAmount) + "tokens.") elif tokenAmount > "400" <= "549": buy = tokenAmount * .3999 print( " You would like to spend $"+(buy) + "on" + (tokenAmount) + "tokens.") elif tokenAmount >= "550" <= "749": buy = tokenAmount * .4999 print( " You would like to spend $"+(buy) + "on" + (tokenAmount) + "tokens.") elif tokenAmount >= "750" <= "999": buy = tokenAmount * .6299 print( " You would like to spend $"+(buy) + "on" + (tokenAmount) + "tokens.") else: buy = tokenAmount * .7999 print( " You would like to spend $"+(buy) + "on" + (tokenAmount) + "tokens.") def cashIn (): cashIn = tokenAmount * .05 print( "The amount of money you will receive is $"+ (cashIn)) tokenAmount works but the buy and cashIn are not being ran why? When I put "print(buy, cashIn)" it gives me a long message that ends in an error any and all help is greatly appreciated. Sent from Mail for Windows 10 From avigross at verizon.net Sun Nov 11 17:22:50 2018 From: avigross at verizon.net (Avi Gross) Date: Sun, 11 Nov 2018 17:22:50 -0500 Subject: [Tutor] Bufferin Message-ID: <001b01d47a0d$156374b0$402a5e10$@verizon.net> After my earlier message (snipped out for space savings) focused on choosing among various methods to retain a buffer of recent lines from a file, I realized that for many, the best method is simply to import a solution others have created, often in the form of an object. Many of the methods I discussed (and undoubtedly many other ways) are likely to be incorporated using object-oriented aspects in a class. I mean if you created a class called Bufferin and set methods like: __init__ takes an argument "n" with say a default of 5 and initializes whatever internal data structure will hold the info. __add__ might overlay the + sign operator to take the string (or any object) passed to it and add it to the internal representation for your ordered collection of n objects and perhaps delete something to make room. __iadd__ and __radd__ might be set to allow += to work as an overlay or to accept adding from the other side. __str__ or perhaps __repr__ could be overlaid to print the results in whatever way you want. And, you could just make normal named methods to allow you to say Bufferin.insert() and Bufferin.display() or whatever interface you want to use. Using an instance of a class this way would let you have multiple active buffers active containing the last N lines (or any objects) from multiple sources. Subclassing such an object might even let you be more selective. For example, a log may contain entries you do not want buffered such as just warnings or entries that say what time something started. You want the last N lines for context to be mostly things that are useful in debugging. Any of the above can include specific logic that intercepts the call to add an item and only calls the class method if it is deemed to be worth keeping. In any case, buffering is an extremely common phenomenon, and a search for something like "python circular buffer class" can provide code you might just include and use for free and that may be well tested or even more efficient. Some are probably parts of packages normally available in built-in modules. So, again, the solution Alan suggested as a small part of the program he was discussing, is fine. I like to get more abstract and see if there is a reasonable solution with more power for other needs. My earlier message focused on ways to either do it simpler from a human perspective or maybe even faster and then on making the size of the buffer variable. Using a class object may help not only by having a way to maintain the data without passing it repeatedly in functional calls but by allowing multiple buffers running independently. Heck, I can even imagine a log where multiple processes or threads write to the same log file in an interspersed way. Your goal in the program that began this discussion might then be to scan for a problem in ANY process or session and as soon as found, display the last N lines for just that entity. So you might read each line, find something like a process ID, and throw the line into an appropriate buffer. One way would be to have a master dictionary whose keys are that ID and the values are instances of your buffer object. When a failure is encountered, get the object for that key and print the contents as discussed above. You might then continue processing the log after optionally removing that object or setting it back to empty and process any other such errors till the log is completed. Just some thoughts but also a comment I must add. To those that don't know me (meaning almost everyone) I have been known to play games that range from humor to sarcasm to word games. I chose the name Bufferin because of the similarity in wordplay but also because there is an analgesic product called Bufferin that is named that for a somewhat different reason having more to do with Chemistry. It is basically Aspirin but with added ingredients that buffer it (Magnesium and Calcium carbonates and oxides) that are antacids so that some people experience less gastric discomfort when taking plain aspirin. Alan used "buff" as a variable, which has other meanings like someone in good physical shape or phrases like being in the buff meaning without clothes. Similar ideas in the word "buffed." I generally do not stop and explain my puns or wordplay but if something seems phrased oddly, I may be bending things a bit. I did exert some self-control by not using the name of my favorite Vampire Slayer ? Last comment. If the purpose of the limited Buffer is to save space by not reading the entire file at once and retaining just enough context when needed, you may want to remember to release this space too when done using it. Python garbage collection may kick in for say closing the file opened in a "for line in open(...):" or a context manager context like "with open(...) as line:" but you may want to delete things like the buffers manually when done unless they too are set up to be temporary. For a buffer size of 5, no big deal. But if you feel the need to retain the last 100K lines, perhaps in multiple buffers, ... Avi From avigross at verizon.net Sun Nov 11 18:06:08 2018 From: avigross at verizon.net (Avi Gross) Date: Sun, 11 Nov 2018 18:06:08 -0500 Subject: [Tutor] saveLine decked Message-ID: <002201d47a13$21f775e0$65e661a0$@verizon.net> Peter, Appreciated. I wrote something like this in another message before reading yours. Indeed one of the things I found was the deque class in the collections module. But I was not immediately clear on whether that would be directly applicable. Their maximum sounded like if you exceeded it, it might either reject the addition or throw an error. The behavior I wanted was sort of a sliding window protocol where the oldest entry scrolled off the screen or was simply removed. Sort of like what you might do with a moving average that takes the average of just the last 20 days of a stock price. But I searched some more and stand corrected. "New in version 2.4. If maxlen is not specified or is None, deques may grow to an arbitrary length. Otherwise, the deque is bounded to the specified maximum length. Once a bounded length deque is full, when new items are added, a corresponding number of items are discarded from the opposite end. Bounded length deques provide functionality similar to the tail filter in Unix. They are also useful for tracking transactions and other pools of data where only the most recent activity is of interest." That sounds exactly like what is needed. As long as you keep adding at the end (using the append method) it should eventually remove from the beginning automatically. No need to use count and selectively remove or pop manually. And despite all the additional functionality, I suspect it is tuned and perhaps has parts written in C++ for added speed. I do note that any larger log file used in the application discussed may throw things on the deque many times but only ask it to display rarely so the former should be optimized. But one question before I go, Columbo style. The manual page (https://docs.python.org/2/library/collections.html ) suggest you call deque with an iterator. That would not necessarily meet our need as giving it the entire file as an iterator would just grind away without any logic and keep just the last N lines. We could arrange the logic in our own iterator, such as a function that reads a line at a time using its own open iterator and yields the line over but that too is problematic as to how and when you stop and print the results. But on second look, the iterator is optional and I tried creating a deque using just a maxlen=3 argument for illustration. >>> from collections import deque >>> a=deque(maxlen=3) >>> a deque([], maxlen=3) >>> a.append('line 1\n') >>> a deque(['line 1\n'], maxlen=3) >>> a.append('line 2\n') >>> a.append('line 3\n') >>> a deque(['line 1\n', 'line 2\n', 'line 3\n'], maxlen=3) >>> a.append('line N\n') >>> a deque(['line 2\n', 'line 3\n', 'line N\n'], maxlen=3) OK, that looks right so all you need to figure out is how to print it in a format you want. As it happens, deque has an str and a repr that seem the same when I try to print: >>> a.__str__() "deque(['line 2\\n', 'line 3\\n', 'line N\\n'], maxlen=3)" >>> a.__repr__() "deque(['line 2\\n', 'line 3\\n', 'line N\\n'], maxlen=3)" So you either need to subclass deque to get your own printable version (or use an amazing number of other Python tricks since you can, or do something manually. >>> for line in a: print(line) line 2 line 3 line N OK, that works but my \n characters at the end of some items might suggest using end='' in the 3.X version of print for a smaller display. Summary: the method Peter mentions is a decent solution with no programming or debugging overhead. It is even flexible enough, if you choose, to store or display the lines backwards as in showing the last line that showed the error, followed by successively earlier lines. Why use a limited solution when you can play with a full deck? -----Original Message----- From: Tutor On Behalf Of Peter Otten Sent: Sunday, November 11, 2018 2:43 PM To: tutor at python.org Subject: Re: [Tutor] saveLine Avi Gross wrote: > Alan and others have answered the questions posed and what I am asking > now is to look at the function he proposed to keep track of the last > five lines. > > There is nothing wrong with it but I wonder what alternatives people > would prefer. His code is made for exactly 5 lines to be buffered and > is quite efficient. But what if you wanted N lines buffered, perhaps > showing a smaller number of lines on some warnings or errors and the > full N in other cases? The standard library features collections.deque. With that: buffer = collections.deque(maxlen=N) save_line = buffer.append This will start with an empty buffer. To preload the buffer: buffer = collections.deque(itertools.repeat("", N), maxlen=N) To print the buffer: print_buffer = sys.stdout.writelines or, more general: def print_buffer(items, end=""): for item in items: print(item, end=end) Also, for smallish N: def print_buffer(items, end=""): print(*items, sep=end) _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From steve at pearwood.info Sun Nov 11 18:47:37 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 12 Nov 2018 10:47:37 +1100 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: <20181111234737.GA4071@ando.pearwood.info> On Sun, Nov 11, 2018 at 03:00:00PM -0800, Stealth Fleet wrote: [...] > tokenAmount works but the buy and cashIn are not being ran why? You define the function, but never call it. Functions aren't called automatically. py> def example(): ... print("calling example") ... py> py> # nothing is happening yet... ... py> example() # call the function calling example > When I put > "print(buy, cashIn)" it gives me a long message that ends in an error Would you like to tell us that message, or would you prefer that we make a wild guess? Please COPY AND PASTE (don't summarise or retype from memory) the ENTIRE error message, not just the last line, and especially DON'T take a screen shot or photo and send that. -- Steve From chigga101 at gmail.com Mon Nov 12 06:45:53 2018 From: chigga101 at gmail.com (Matthew Ngaha) Date: Mon, 12 Nov 2018 11:45:53 +0000 Subject: [Tutor] I think I've broken my Python Message-ID: I haven't coded in about 3 years and I used to use Python3.4 on openSUSE13.1. I had a virtual environment with this Python for a development Django website. Before I stopped coding, I upgraded to Python3.5, but everything still worked if I remember correctly. But now I'm back to coding I tried to load up my Django website but I get the error: Traceback (most recent call last): File "manage.py", line 8, in from django.core.management import execute_from_command_line ImportError: No module named django.core.management What happens is Django's manage.py can't be found so the line: "from django.core.management import execute_from_command_line" throws the error. In the virtual environment when I type python in the command line, it opens Python2.7 and when I type Python3, it opens Python3.5. So it seems my Python3.4 is lost. I thought the virtual environment only loaded up the Python version it was installed for, in my case Python3.4 when I type python or python3 in the command line, had I known this wasn't the case I would have never upgraded Python. Is there anything I can do? From mats at wichmann.us Mon Nov 12 10:29:40 2018 From: mats at wichmann.us (Mats Wichmann) Date: Mon, 12 Nov 2018 08:29:40 -0700 Subject: [Tutor] I think I've broken my Python In-Reply-To: References: Message-ID: On 11/12/18 4:45 AM, Matthew Ngaha wrote: > I haven't coded in about 3 years and I used to use Python3.4 on > openSUSE13.1. I had a virtual environment with this Python for a > development Django website. Before I stopped coding, I upgraded to > Python3.5, but everything still worked if I remember correctly. But > now I'm back to coding I tried to load up my Django website but I get > the error: > Traceback (most recent call last): > File "manage.py", line 8, in > from django.core.management import execute_from_command_line > ImportError: No module named django.core.management > > What happens is Django's manage.py can't be found so the line: > "from django.core.management import execute_from_command_line" > throws the error. In the virtual environment when I type python in the > command line, it opens Python2.7 and when I type Python3, it opens > Python3.5. So it seems my Python3.4 is lost. I thought the virtual > environment only loaded up the Python version it was installed for, in > my case Python3.4 when I type python or python3 in the command line, > had I known this wasn't the case I would have never upgraded Python. > Is there anything I can do? Reconstruct the virtualenv you want maybe? Their whole concept is that they're cheap and easy and can just be thrown away. Of course, you then have to remember what you did to create it in order to make a new one... Virtualenv *can* make a full copy of the Python environment you are building it from, but does not by default - it uses links to save space if the platform is okay with that. That does mean that if the original goes away due to version upgrade, you're likely to be left with an unusable virtualenv (see the --always-copy argument to virtualenv). Since you're on a Linux platform, use "which" to see how the names you are typing are resolving which python which python3 Usually if the virtualenv has been broken by external factors, it won't even activate. Are you sure you remembered to try to activate it? From chigga101 at gmail.com Mon Nov 12 12:07:50 2018 From: chigga101 at gmail.com (Matthew Ngaha) Date: Mon, 12 Nov 2018 17:07:50 +0000 Subject: [Tutor] I think I've broken my Python In-Reply-To: References: Message-ID: On Mon, Nov 12, 2018 at 3:31 PM Mats Wichmann wrote: > > Reconstruct the virtualenv you want maybe? Their whole concept is that > they're cheap and easy and can just be thrown away. Of course, you then > have to remember what you did to create it in order to make a new one... It's been 3 years, I'm clueless as to how I created it. Also I'm a noob when it comes to Linux which makes this even more confusing for me. The 3 years I've been absent from Programming I went back to Windows. > Since you're on a Linux platform, use "which" to see how the names you > are typing are resolving > > which python > which python3 The "command" gives this output: (website) matthew at linux-oq68:~/Documents/mywork/web/website/blogsite> which python /usr/bin/python (website) matthew at linux-oq68:~/Documents/mywork/web/website/blogsite> which python3 /usr/bin/python3 > Usually if the virtualenv has been broken by external factors, it won't > even activate. Are you sure you remembered to try to activate it? > Yep, I double checked to make sure I remembered to try to activate it. From mats at wichmann.us Mon Nov 12 12:50:33 2018 From: mats at wichmann.us (Mats Wichmann) Date: Mon, 12 Nov 2018 10:50:33 -0700 Subject: [Tutor] I think I've broken my Python In-Reply-To: References: Message-ID: <46a2955a-28a4-b335-9404-3d897dcaa635@wichmann.us> On 11/12/18 10:07 AM, Matthew Ngaha wrote: > On Mon, Nov 12, 2018 at 3:31 PM Mats Wichmann wrote: >> >> Reconstruct the virtualenv you want maybe? Their whole concept is that >> they're cheap and easy and can just be thrown away. Of course, you then >> have to remember what you did to create it in order to make a new one... > > It's been 3 years, I'm clueless as to how I created it. Also I'm a > noob when it comes to Linux which makes this even more confusing for > me. The 3 years I've been absent from Programming I went back to > Windows. > >> Since you're on a Linux platform, use "which" to see how the names you >> are typing are resolving >> >> which python >> which python3 > > The "command" gives this output: > > (website) matthew at linux-oq68:~/Documents/mywork/web/website/blogsite> > which python > /usr/bin/python > (website) matthew at linux-oq68:~/Documents/mywork/web/website/blogsite> > which python3 > /usr/bin/python3 > >> Usually if the virtualenv has been broken by external factors, it won't >> even activate. Are you sure you remembered to try to activate it? >> > Yep, I double checked to make sure I remembered to try to activate it. > it's possible there was never a virtualenv involved... on a Linux system you'd normally install the distribution package for django, instead of installing bits in a virtualenv. The distro package contains a template manage.py. Which, on a current Fedora, contains wording suggesting maybe a virtualenv wasn't started - but the real message is that it couldn't find necessary Django pieces in the python environment (this is consistent with your original message, suggesting it found and is running manage, but other things are missing). Here's the interesting part of the content of that template for those who might have more ideas: if __name__ == "__main__": os.environ.setdefault("DJANGO_SETTINGS_MODULE", "{{ project_name }}.settings") try: from django.core.management import execute_from_command_line except ImportError as exc: raise ImportError( "Couldn't import Django. Are you sure it's installed and " "available on your PYTHONPATH environment variable? Did you " "forget to activate a virtual environment?" ) from exc execute_from_command_line(sys.argv) From chigga101 at gmail.com Mon Nov 12 13:32:22 2018 From: chigga101 at gmail.com (Matthew Ngaha) Date: Mon, 12 Nov 2018 18:32:22 +0000 Subject: [Tutor] I think I've broken my Python In-Reply-To: References: Message-ID: On Mon, Nov 12, 2018 at 5:35 PM Roger B. Atkins wrote: > > What about installing or reinstalling Django using the python version > that produced the error message? Yeah if all else fails I'll try this. Thanks. From chigga101 at gmail.com Mon Nov 12 13:41:03 2018 From: chigga101 at gmail.com (Matthew Ngaha) Date: Mon, 12 Nov 2018 18:41:03 +0000 Subject: [Tutor] I think I've broken my Python In-Reply-To: <46a2955a-28a4-b335-9404-3d897dcaa635@wichmann.us> References: <46a2955a-28a4-b335-9404-3d897dcaa635@wichmann.us> Message-ID: On Mon, Nov 12, 2018 at 5:52 PM Mats Wichmann wrote: > but the real message is that it couldn't > find necessary Django pieces in the python environment (this is > consistent with your original message, suggesting it found and is > running manage, but other things are missing). > You're right, I was wrong when I said "manage.py can't be found." It's the import statement from manage.py that causes the problem. From asad.hasan2004 at gmail.com Sun Nov 11 20:54:00 2018 From: asad.hasan2004 at gmail.com (Asad) Date: Mon, 12 Nov 2018 07:24:00 +0530 Subject: [Tutor] Example for read and readlines() (Asad) In-Reply-To: References: Message-ID: Hi All , Thanks for the reply . I am building a framework for the two error conditions, therefore I need to read and readlines because in one only regex is required and in other regex+ n-1 line is required to process : #Here we are opening the file and substituting space " " for each \n encountered f3 = open (r"D:\QI\log.log", 'r') string = f3.read() string1 = f3.readlines() regex = re.compile ( "\n" ) st = regex.sub ( " ", string ) if re.search('ERR1',st): y=re.findall("[A-Z][a-z][a-z] [ 123][0-9] [012][0-9]:[0-5][0-9]:[0-5][0-9] [0-9][0-9][0-9][0-9]",st) print y patchnumber = re.compile(r'(\d+)\/(\d+)') ======> doesnot work it only works if I use #string = f3.read() for j in range(len(string1)): if re.search ( r'ERR2', string1[j] ): print "Error line \n", string1[j - 1] mo = patchnumber.search (string1[j-1]) a = mo.group() print a print os.getcwd() break Please advice how to proceed. Thanks, On Sun, Nov 11, 2018 at 10:30 PM 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. Re: Require Python assistance (Alan Gauld) > 2. Re: Example for read and readlines() (Alan Gauld) > 3. Re: Example for read and readlines() (Alan Gauld) > 4. Re: Example for read and readlines() (Asad) > 5. Re: Example for read and readlines() (Alan Gauld) > > > > ---------- Forwarded message ---------- > From: Alan Gauld > To: tutor at python.org > Cc: > Bcc: > Date: Sun, 11 Nov 2018 09:53:23 +0000 > Subject: Re: [Tutor] Require Python assistance > On 10/11/2018 18:10, Avi Gross wrote: > > WARNING to any that care: > > > > As the following letter is a repeat request without any hint they read > the earlier comments here, I did a little searching and see very much the > same request on another forum asking how to do this in MATLAB: > > The OP has also repeated posted the same message to this list > (which I rejected as moderator). > > > -- > 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: Alan Gauld > To: tutor at python.org > Cc: > Bcc: > Date: Sun, 11 Nov 2018 10:00:33 +0000 > Subject: Re: [Tutor] Example for read and readlines() > On 11/11/2018 06:49, Asad wrote: > > Hi All , > > > > If I am loading a logfile what should I use from the option > 1,2,3 > > > > f3 = open ( r"/a/b/c/d/test/test_2814__2018_10_05_12_12_45/logA.log", > 'r' ) > > > > 1) should only iterate over f3 > > This is best for processing line by line which is the most > common way to handle files. It saves memory and allows you > to exit early, without reading the entire file if you are > only looking for say a single entry. > > for line in file: > if terminal_Condition: break > # process line here > > > 2) st = f3.read() > > The best solution if you want to process individual characters > or small character groups. Also best if you want to process > the entire file at once, for example using a regular expression > which might span lines. > > > 3) st1 = f3.readlines() > > Mainly historical and superseded by iterating over the file. > But sometimes useful if you need to do multiple passes over > the lines since it only reads the file once. Very heavy > memory footprint for big files. > > > -- > 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: Alan Gauld > To: tutor at python.org > Cc: > Bcc: > Date: Sun, 11 Nov 2018 10:02:40 +0000 > Subject: Re: [Tutor] Example for read and readlines() > On 11/11/2018 09:40, Steven D'Aprano wrote: > > >> f3 = open ( r"/a/b/c/d/test/test_2814__2018_10_05_12_12_45/logA.log", > 'r' ) > > > > Don't use raw strings r"..." for pathnames. > > Umm, Why not? > > -- > 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: Asad > To: tutor at python.org > Cc: > Bcc: > Date: Sun, 11 Nov 2018 15:34:35 +0530 > Subject: Re: [Tutor] Example for read and readlines() > Hi All, > > thanks for the reply so to put into context say I have a file > logfile formatted in text lines > > 1) and I want to extract the start time , error number and end > time from this logfile so in this case what should I use I guess option 1 > : > > with open(filename, 'r') as f: > for line in f: > process(line) > > should be fine or any other option ? > > 2) Another case is a text formatted logfile and I want to print (n-4) > lines n is the line where error condition was encountered . > > 3) Do we need to ensure that each line in the logfile ends with \n . > > \n is not visible so can we verify in someway to proof EOL \n is placed > in the file . > > > Thanks, > > > > > > > > > > > > > > > > ---------- Forwarded message ---------- > > From: Asad > > To: tutor at python.org > > Cc: > > Bcc: > > Date: Sun, 11 Nov 2018 12:19:36 +0530 > > Subject: [Tutor] Example for read and readlines() > > Hi All , > > > > If I am loading a logfile what should I use from the option > 1,2,3 > > > > f3 = open ( r"/a/b/c/d/test/test_2814__2018_10_05_12_12_45/logA.log", > 'r' ) > > > > 1) should only iterate over f3 > > > > 2) st = f3.read() > > Should iterate over st > > > > > > 3) st1 = f3.readlines() > > > > Should iterate over st1 > > > > How are the above options different it they are not can there be some > > examples to describe in which situations should we each method . > > > > > > Thanks, > > > > -- > > Asad Hasan > > +91 9582111698 > > > > > > > > > > ---------- Forwarded message ---------- > > From: "Steven D'Aprano" > > To: tutor at python.org > > Cc: > > Bcc: > > Date: Sun, 11 Nov 2018 20:40:49 +1100 > > Subject: Re: [Tutor] Example for read and readlines() > > On Sun, Nov 11, 2018 at 12:19:36PM +0530, Asad wrote: > > > Hi All , > > > > > > If I am loading a logfile what should I use from the option > > 1,2,3 > > > > Depends what you want to do. I assume that the log file is formatted > > into lines of text, so you probably want to iterate over each line. > > > > with open(filename, 'r') as f: > > for line in f: > > process(line) > > > > is the best idiom to use for line-by-line iteration. It only reads each > > line as needed, not all at once, so it can handle huge files even if the > > file is bigger than the memory you have. > > > > > > > f3 = open ( r"/a/b/c/d/test/test_2814__2018_10_05_12_12_45/logA.log", > > 'r' ) > > > > Don't use raw strings r"..." for pathnames. > > > > > > > 1) should only iterate over f3 > > > > > > 2) st = f3.read() > > > > Use this if you want to iterate over the file character by character, > > after reading the entire file into memory at once. > > > > > > > 3) st1 = f3.readlines() > > > > Use this if you want to read all the lines into memory at once. > > > > > > > > -- > > Steve > > > > _______________________________________________ > > Tutor maillist - Tutor at python.org > > https://mail.python.org/mailman/listinfo/tutor > > > > > -- > Asad Hasan > +91 9582111698 > > > > > ---------- Forwarded message ---------- > From: Alan Gauld > To: tutor at python.org > Cc: > Bcc: > Date: Sun, 11 Nov 2018 14:46:18 +0000 > Subject: Re: [Tutor] Example for read and readlines() > On 11/11/2018 10:04, Asad wrote: > > > 1) and I want to extract the start time , error number and end > > time from this logfile so in this case what should I use I guess option > 1 : > > > > with open(filename, 'r') as f: > > for line in f: > > process(line) > > Yes, that woyuld be the best choice in that scenario. > > > 2) Another case is a text formatted logfile and I want to print (n-4) > > lines n is the line where error condition was encountered . > > In that case you could use readlines() if it is a small file. > Or you could save the last 5 lines and print those each time > you find an error line. You should probably write a function > to save the line since it needs to move the previous lines > up one. > > buffer = ['','','','',''] > > def saveLine(line, buff): > buff[0] = buff[1] > buff[1] = buff[2] > buff[2] = buff[3] > buff[3] = buff[4] > buff[4] = line > > for line in file: > saveLine(line,buffer) > if error_condition: > printBuffer() > > readlines is simpler but stores the entire file in memory. > The buffer saves memory but requires some extra processing > to save/print. There are some modules for handling cyclic > stores etc but in a simple case like this they are probably > overkill. > > > 3) Do we need to ensure that each line in the logfile ends with \n . > > > > \n is not visible so can we verify in someway to proof EOL \n is > placed > > in the file . > > textfile lines are defined by the existence of the \n > so both readlines() and a loop over the file will both > read multiple lines if a \n is missing. > > You could use read() and a regex to check for some > text marker and insert the newlines. This would be best > if the whole file had them missing. > > If it just an occasional line then you can iterate over > the file as usual and check each line for a missing \n and > insert (or split) as needed. > > You might want to write the modified lines back to > a new file. > > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.amazon.com/author/alan_gauld > Follow my photo-blog on Flickr at: > http://www.flickr.com/photos/alangauldphotos > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > https://mail.python.org/mailman/listinfo/tutor > -- Asad Hasan +91 9582111698 From asad.hasan2004 at gmail.com Mon Nov 12 06:14:17 2018 From: asad.hasan2004 at gmail.com (Asad) Date: Mon, 12 Nov 2018 16:44:17 +0530 Subject: [Tutor] How to create a html hyperlink for a document Message-ID: Hi All , I am creating a python script to analyze the log and provide a solution , in the solution part I want to include a document and create a hyperlink to the document so that its clickable . I have python 2.6.6 cannot use :hyperlink module because this is production server and the module cannot be installed on it . Need some trick to create it . Please advice , Thanks, -- Asad Hasan +91 9582111698 From srinivasan.rns at gmail.com Mon Nov 12 03:28:19 2018 From: srinivasan.rns at gmail.com (srinivasan) Date: Mon, 12 Nov 2018 09:28:19 +0100 Subject: [Tutor] Issue in parsing the strings in python code Message-ID: Dear Python Experts team, This question might be very simple for you, As am newbie to python, could you please how to parse the below strings 1. Could you please do the needful in guiding me, that how can I extract the strings under the UUID column in python code in the below output (nmcli c show), I need to extract the UUID of "Funkloch" ie., "1da7d068-4548-4446-bf88-a440e49db1b1" for "TYPE" wifi and device "wlp1s0" and return this string ("1da7d068-4548-4446-bf88-a440e49db1b1") to the robotframework? root:~/qa/robot_tests# nmcli c show NAME UUID TYPE DEVICE Funkloch 1552 c8e1e8c0-0f25-4299-a9ae-2910cfef2ebd wifi wlp1s0 Wired connection 1 2a14fbe6-58a0-3b7f-b986-5d1b36a94ec0 ethernet enp0s21f0u4 Funkloch 1da7d068-4548-4446-bf88-a440e49db1b1 wifi -- Funkloch 10 f4d9ce13-aab0-4485-9929-6070ad52a196 wifi -- Funkloch 100 8b48a220-1754-4988-84ad-d0f83a9b4ede wifi -- 2. Similarly, As I need to verify whether the DEVICE "wlp1s0" is connected to "Funkloch" or not? could you please help me, how can I extract the "connected" status under "STATE column for DEVICE "wlp1s0" and CONNECTION "Funkloch 1552" root:~/qa/robot_tests# nmcli dev DEVICE TYPE STATE CONNECTION enp0s21f0u4 ethernet connected Wired connection 1 wlp1s0 wifi connected Funkloch 1552 enp2s0 ethernet unavailable -- sit0 iptunnel unmanaged -- lo loopback unmanaged -- Kindly do the needful as am trying this from past two 2 days, still no clues Many thanks in advance From shakti.shrivastava13 at gmail.com Mon Nov 12 03:44:21 2018 From: shakti.shrivastava13 at gmail.com (Shakti Kumar) Date: Mon, 12 Nov 2018 14:14:21 +0530 Subject: [Tutor] Issue in parsing the strings in python code In-Reply-To: References: Message-ID: On Mon, 12 Nov 2018 at 14:02, srinivasan wrote: > Dear Python Experts team, > > This question might be very simple for you, As am newbie to python, could > you please how to parse the below strings > > 1. Could you please do the needful in guiding me, that how can I extract > the strings under the UUID column in python code in the below output (nmcli > c show), I need to extract the UUID of "Funkloch" ie., > "1da7d068-4548-4446-bf88-a440e49db1b1" for "TYPE" wifi and device "wlp1s0" > and return this string ("1da7d068-4548-4446-bf88-a440e49db1b1") to the > robotframework? > > root:~/qa/robot_tests# nmcli c show > NAME UUID TYPE DEVICE > > Funkloch 1552 c8e1e8c0-0f25-4299-a9ae-2910cfef2ebd wifi wlp1s0 > > Wired connection 1 2a14fbe6-58a0-3b7f-b986-5d1b36a94ec0 ethernet > enp0s21f0u4 > Funkloch 1da7d068-4548-4446-bf88-a440e49db1b1 wifi -- > > Funkloch 10 f4d9ce13-aab0-4485-9929-6070ad52a196 wifi -- > > Funkloch 100 8b48a220-1754-4988-84ad-d0f83a9b4ede wifi -- > > > > 2. Similarly, As I need to verify whether the DEVICE "wlp1s0" is connected > to "Funkloch" or not? could you please help me, how can I extract the > "connected" status under "STATE column for DEVICE "wlp1s0" and CONNECTION > "Funkloch 1552" > > root:~/qa/robot_tests# nmcli dev > DEVICE TYPE STATE CONNECTION > enp0s21f0u4 ethernet connected Wired connection 1 > wlp1s0 wifi connected Funkloch 1552 > enp2s0 ethernet unavailable -- > sit0 iptunnel unmanaged -- > lo loopback unmanaged -- > > Kindly do the needful as am trying this from past two 2 days, still no > clues > > Many thanks in advance > -- > https://mail.python.org/mailman/listinfo/python-list > You can look into textfsm parser for the same, it was released specifically for this purpose of parsing CLI outputs. It should be the ideal solution for your scenario since I see some rows in your cli output which have spaces in the same column, so a hardcoded index after splitting each line on spaces will not work out. From srinivasan.rns at gmail.com Mon Nov 12 14:37:06 2018 From: srinivasan.rns at gmail.com (srinivasan) Date: Tue, 13 Nov 2018 01:07:06 +0530 Subject: [Tutor] Unable to get the gateway IP of wlan interface using python code Message-ID: Dear Python Experts, *First method:* I need to get the IP address basically the gateway IP in my setup I get it as "192.168.178.1" when I run the below standalone python code. * def get_gateway_ip(self):* * """* * Get the IP address to the WIFI module from the AP* * """* * cmd = 'ip route show 0.0.0.0/0 dev wlp1s0 | cut -d\ -f3'* * f = os.popen(cmd)* * return str(f.read().strip())* I am trying to log the IP in the robot framework script and ensure that my router is able to ping but "*${SSID_GATEWAY_IP}*" doesn't seem to get collected from the above python code and pass this value to my custom method "*Wait Until Device Is Pingable"* * ${RET} = Wait Until Device Is Pingable ${SSID_GATEWAY_IP}* * Should Be True ${RET}* But whenever I hardcode the "*${SSID_GATEWAY_IP} 192.168.178.1*" in the robot framework, it seems to be working with "*Wait Until Device Is Pingable ${SSID_GATEWAY_IP}*" But the below robot framework script doesn't seems to work with the return value received from the above python script, could you please do the needful? *Get Gateway IP of SSID* * ${SSID_GATEWAY_IP} = Get Gateway Ip* * Log ${SSID_GATEWAY_IP}* * ${RET} = Wait Until Device Is Pingable ${SSID_GATEWAY_IP}* * Should Be True ${RET}* *SECOND METHOD:* When I use the subprocess I see the below issue: def get_gateway_ip(self): """ Get the IP address to the WIFI module from the AP """ #cmd = 'ip route show 0.0.0.0/0 dev wlp1s0 | cut -d\ -f3' # f = os.popen(cmd) # return str(f.read().strip()) p = subprocess.Popen('ip route show 0.0.0.0/0 dev wlp1s0 | cut -d\ -f3', stdout=subprocess.PIPE) result = p.communicate()[0] print(result) #list = os.popen('ip route show 0.0.0.0/0 dev wlp1s0 | cut -d\ -f3').read() # p = Popen(cmd, shell=True, stdout=PIPE) # out, err = p.communicate() # #return (p.returncode, out, err) # return out # #print('returncode: %s' % result[0]) # #print('output: %s' % result[1]) # #print('error: %s' % result[2]) #return self._helper.execute_cmd_output_string(cmd) Error: root:~/qa/test_library# python3 wifi.py Enabling wifi Verify wifi connectivity True Get gateway wifi ip Traceback (most recent call last): File "wifi.py", line 134, in print(m.get_gateway_ip()) File "wifi.py", line 76, in get_gateway_ip p = subprocess.Popen('ip route show 0.0.0.0/0 dev wlp1s0 | cut -d\ -f3', stdout=subprocess.PIPE) File "/usr/lib/python3.5/subprocess.py", line 676, in __init__ restore_signals, start_new_session) File "/usr/lib/python3.5/subprocess.py", line 1289, in _execute_child raise child_exception_type(errno_num, err_msg) FileNotFoundError: [Errno 2] No such file or directory: 'ip route show 0.0.0.0/0 dev wlp1s0 | cut -d\\ -f3' root:~/qa/test_library# As I am stuck with this issue from past 2 days, wondering for any clues Kindly do the needful as early as possible Many Thanks in adavnce From avigross at verizon.net Sun Nov 11 18:35:08 2018 From: avigross at verizon.net (Avi Gross) Date: Sun, 11 Nov 2018 18:35:08 -0500 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: <002801d47a17$2f0c51c0$8d24f540$@verizon.net> In replying to what "Stealth Fleet" asked, I have too many comments, starting with a suggestion to put a SUBECT on the Subject: line. Your first error was not converting the text input to an integer or floating point number. tokenAmount = input( "How many tokens would you like to buy or cash in?: ") Fix that and then see if other things fail. BTW, you created functions which take no arguments and use a global variable but then create local variables that would not persist in a real application. If your assignment was just to print what would have been calculated, that may suffice but that may be another area you think more about. -----Original Message----- From: Tutor On Behalf Of Stealth Fleet Sent: Sunday, November 11, 2018 6:00 PM To: tutor at python.org Subject: [Tutor] (no subject) tokenAmount = input( "How many tokens would you like to buy or cash in?: ") print (tokenAmount) def buy (): if tokenAmount <= 400: buy = tokenAmount * .2099 print( " You would like to spend $"+(buy) + "on" + (tokenAmount) + "tokens.") elif tokenAmount > "400" <= "549": buy = tokenAmount * .3999 print( " You would like to spend $"+(buy) + "on" + (tokenAmount) + "tokens.") elif tokenAmount >= "550" <= "749": buy = tokenAmount * .4999 print( " You would like to spend $"+(buy) + "on" + (tokenAmount) + "tokens.") elif tokenAmount >= "750" <= "999": buy = tokenAmount * .6299 print( " You would like to spend $"+(buy) + "on" + (tokenAmount) + "tokens.") else: buy = tokenAmount * .7999 print( " You would like to spend $"+(buy) + "on" + (tokenAmount) + "tokens.") def cashIn (): cashIn = tokenAmount * .05 print( "The amount of money you will receive is $"+ (cashIn)) tokenAmount works but the buy and cashIn are not being ran why? When I put "print(buy, cashIn)" it gives me a long message that ends in an error any and all help is greatly appreciated. Sent from Mail for Windows 10 _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From rba2124 at gmail.com Mon Nov 12 12:34:55 2018 From: rba2124 at gmail.com (Roger B. Atkins) Date: Mon, 12 Nov 2018 10:34:55 -0700 Subject: [Tutor] I think I've broken my Python In-Reply-To: References: Message-ID: What about installing or reinstalling Django using the python version that produced the error message? On Mon, Nov 12, 2018 at 10:10 AM Matthew Ngaha wrote: > > On Mon, Nov 12, 2018 at 3:31 PM Mats Wichmann wrote: > > > > Reconstruct the virtualenv you want maybe? Their whole concept is that > > they're cheap and easy and can just be thrown away. Of course, you then > > have to remember what you did to create it in order to make a new one... > > It's been 3 years, I'm clueless as to how I created it. Also I'm a > noob when it comes to Linux which makes this even more confusing for > me. The 3 years I've been absent from Programming I went back to > Windows. > > > Since you're on a Linux platform, use "which" to see how the names you > > are typing are resolving > > > > which python > > which python3 > > The "command" gives this output: > > (website) matthew at linux-oq68:~/Documents/mywork/web/website/blogsite> > which python > /usr/bin/python > (website) matthew at linux-oq68:~/Documents/mywork/web/website/blogsite> > which python3 > /usr/bin/python3 > > > Usually if the virtualenv has been broken by external factors, it won't > > even activate. Are you sure you remembered to try to activate it? > > > Yep, I double checked to make sure I remembered to try to activate it. > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From bill at celestial.net Mon Nov 12 18:54:19 2018 From: bill at celestial.net (Bill Campbell) Date: Mon, 12 Nov 2018 15:54:19 -0800 Subject: [Tutor] Unable to get the gateway IP of wlan interface using python code In-Reply-To: References: Message-ID: <20181112235419.GA2250@ayn.mi.celestial.com> On Tue, Nov 13, 2018, srinivasan wrote: >Dear Python Experts, > >*First method:* > >I need to get the IP address basically the gateway IP in my setup I get it >as "192.168.178.1" when I run the below standalone python code. > > >* def get_gateway_ip(self):* >* """* >* Get the IP address to the WIFI module from the AP* >* """* > >* cmd = 'ip route show 0.0.0.0/0 dev wlp1s0 | cut >-d\ -f3'* >* f = os.popen(cmd)* >* return str(f.read().strip())* This command should get the gateway IP. Linux: cmd = "ip route list | awk '/^default/{print $3}'" or perhaps Linux: cmd = "netstat -rn | awk '/^0.0.0.0/{print $2}'" OSX: cmd = "netstat -rn | awk '/^default/{print $2}'" I don't have a freebsd system available to test this, but I think this pattern should work: re.compile(r'^(default|0\.0\.0\.0)\s+(\S+)') Bill -- INTERNET: bill at celestial.com Bill Campbell; Celestial Software LLC URL: http://www2.celestial.com/ PO Box 820; 6641 E. Mercer Way Mobile: (206) 947-5591 Mercer Island, WA 98040-0820 Fax: (206) 232-9186 Skype: jwccsllc On the free market, everyone earns according to his productive value in satisfying consumer desires. Under statist distribution, everyone earns in proportion to the amount he can plunder from the producers. -- Murray N. Rothbard From david at graniteweb.com Mon Nov 12 19:55:15 2018 From: david at graniteweb.com (David Rock) Date: Mon, 12 Nov 2018 18:55:15 -0600 Subject: [Tutor] Unable to get the gateway IP of wlan interface using python code In-Reply-To: References: Message-ID: <9AF7F9C8-D4E3-4BD0-9B68-85FC66C404A6@graniteweb.com> > On Nov 12, 2018, at 13:37, srinivasan wrote: > > Dear Python Experts, > > *First method:* > > I need to get the IP address basically the gateway IP in my setup I get it > as "192.168.178.1" when I run the below standalone python code. Is there a requirement to use only what comes in the standard libraries, or can you use things from pypi? Getting interface details is exactly why netifaces was created https://pypi.org/project/netifaces/ damocles:src drock$ python3 Python 3.7.0 (default, Oct 28 2018, 22:17:08) [Clang 8.0.0 (clang-800.0.42.1)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import netifaces >>> gws = netifaces.gateways() >>> gws {'default': {2: ('192.168.69.1', 'en0')}, 2: [('192.168.69.1', 'en0', True)]} >>> gws['default'] {2: ('192.168.69.1', 'en0?)} ? David Rock david at graniteweb.com From steve at pearwood.info Tue Nov 13 05:38:17 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 13 Nov 2018 21:38:17 +1100 Subject: [Tutor] How to create a html hyperlink for a document In-Reply-To: References: Message-ID: <20181113103815.GE4071@ando.pearwood.info> On Mon, Nov 12, 2018 at 04:44:17PM +0530, Asad wrote: > Hi All , > > I am creating a python script to analyze the log and provide a > solution , in the solution part I want to include a document and create a > hyperlink to the document so that its clickable . > I have python 2.6.6 cannot use :hyperlink module because this is production > server and the module cannot be installed on it . Need some trick to create > it . This is not something you can control. Whether or not the link is clickable depends on the application you use to view the document. For some applications, the answer is to just include a URL as text: http://www.python.org/ will do. For other applications, you will need to format the URL as a HTML reference: Python or as markdown: [Python](http://www.python.org/) or possibly even ReST: `Python `_. or some other format. Or possibly you can't do it at all. Notepad, for example, has no concept of clickable links. -- Steve From steve at pearwood.info Tue Nov 13 05:44:37 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 13 Nov 2018 21:44:37 +1100 Subject: [Tutor] How to create a html hyperlink for a document In-Reply-To: References: Message-ID: <20181113104436.GF4071@ando.pearwood.info> On Mon, Nov 12, 2018 at 04:44:17PM +0530, Asad wrote: > Hi All , > > I am creating a python script to analyze the log and provide a > solution , in the solution part I want to include a document and create a > hyperlink to the document so that its clickable . > I have python 2.6.6 cannot use :hyperlink module because this is production > server and the module cannot be installed on it . Need some trick to create > it . If it wasn't obvious from my previous response, html hyperlinks are just text. There's no "trick" to create it: you just type the text which makes the HTML code. -- Steve From cs at cskk.id.au Tue Nov 13 05:17:17 2018 From: cs at cskk.id.au (Cameron Simpson) Date: Tue, 13 Nov 2018 21:17:17 +1100 Subject: [Tutor] Example for read and readlines() (Asad) In-Reply-To: References: Message-ID: <20181113101717.GA87505@cskk.homeip.net> On 12Nov2018 07:24, Asad wrote: > Thanks for the reply . I am building a framework for the two > error >conditions, therefore I need to read and readlines because in one only >regex is required and in other regex+ n-1 line is required to process : > >#Here we are opening the file and substituting space " " for each \n >encountered >f3 = open (r"D:\QI\log.log", 'r') >string = f3.read() >string1 = f3.readlines() My first remark is that both these lines read _and_ _consume_ the file content. So "string" gets the entire file content, and "string1" gets an empty array of lines, because the file is already at the end, where there is no more data. It is also better to use this idiom to read and then close a file: with open(r"D:\QI\log.log", 'r') as f3: string = f3.read() This reliably closes f3 once the "with" suite completes, even if there's some kind of exception. You need 2 copies of the file data. You can do this 2 ways. The first way is to read the file twice: with open(r"D:\QI\log.log", 'r') as f3: string = f3.read() with open(r"D:\QI\log.log", 'r') as f3: string1 = f3.readlines() The efficient way is to read the file once, then make string from string1, or string1 from string. For example: with open(r"D:\QI\log.log", 'r') as f3: string1 = f3.readlines() string = ''.join(string1) >regex = re.compile ( "\n" ) >st = regex.sub ( " ", string ) Using a regular expression to replace a fixed string such as "\n" is overkill. Consider: st = string.replace("\n", " ") Python strings have a bunch of handy methods for common simple things. Have a read of the docs for further detail. >if re.search('ERR1',st): > y=re.findall("[A-Z][a-z][a-z] [ 123][0-9] >[012][0-9]:[0-5][0-9]:[0-5][0-9] [0-9][0-9][0-9][0-9]",st) > print y On the other hand, a regexp is a good tool for something like the above. >patchnumber = re.compile(r'(\d+)\/(\d+)') ======> doesnot >work it only works if I use #string = f3.read() This may be because "string" is a single string (the whole file text as one string). "string1" is a _list_ of individual strings, one for each line. Personally, i would call this "strings" or "lines" or some other plural word; your code will be easier to read, and easier to debug. Conversely, a misleading name makes debugging harder because you expect the variable to contain what its name suggests, and if it doesn't this will impede you in finding problems, because you will be thinking the whrong thing about what your program is doing. >for j in range(len(string1)): > if re.search ( r'ERR2', string1[j] ): > print "Error line \n", string1[j - 1] > mo = patchnumber.search (string1[j-1]) > a = mo.group() > print a > print os.getcwd() > break > >Please advice how to proceed. mo.group() returns the whole match. The above seems to look for the string 'ERR2' in a line, and look for a patch number in the previous line. Is that what is it supposed to do? If the above isn't working, it would help to see the failing output and a description of what good output is meant to look like. Finally, please consider turning off "digest mode" in your list subscription. It will make things easier for everyone. Cheers, Cameron Simpson From avigross at verizon.net Mon Nov 12 17:57:29 2018 From: avigross at verizon.net (Avi Gross) Date: Mon, 12 Nov 2018 17:57:29 -0500 Subject: [Tutor] Example for read and readlines() (Asad) In-Reply-To: References: Message-ID: <009301d47adb$16d03f80$4470be80$@verizon.net> Asad, Like many projects, there may be many ways to do things BUT some rules do apply. You can only read an open file ONCE unless you seek back to the beginning or reopen it. string = f3.read() string1 = f3.readlines() The first line reads the entire file into a single buffer. The second program line won't work as intended. The first consumed the entire file. Much of the rest is not organized well enough for me to understand what you want to do. I find it important for people to try some simple things like examining the values step by step. Had you typed print (string) print (string1) on a small sample file, you might have fixed that before continuing. Then each step along the way you could examine and verify it made sense up to that point. Try writing the outline of the logic of your program first in English or your native language as an algorithm. Then see what tools are needed. Look at a sample of the log you are evaluating and see what it takes to locate the lines you want and then to break out the parts you want to keep for further use. What I see looks like this: If you find one instance of the string "ERR1" Then You want to find ALL (nonoverlapping) regions consisting of an upper-case letter followed by two lower-case letters and a space and either a space or digits 1 to 3 and digits 0-9 and a space and ... Fairly complex pattern. But you are searching the contents of the ENTIRE file for this and since you seem to have wanted to replace all newlines by spaces and your pattern includes spaces, this would match something that wrapped around from line to line. Is this what you wanted? You then switch gears to using the readlines version and I decided to get back to my regularly scheduled life. As noted, that probably is an empty string or worse. Good luck. -----Original Message----- From: Tutor On Behalf Of Asad Sent: Sunday, November 11, 2018 8:54 PM To: tutor at python.org Subject: Re: [Tutor] Example for read and readlines() (Asad) Hi All , Thanks for the reply . I am building a framework for the two error conditions, therefore I need to read and readlines because in one only regex is required and in other regex+ n-1 line is required to process : #Here we are opening the file and substituting space " " for each \n encountered f3 = open (r"D:\QI\log.log", 'r') string = f3.read() string1 = f3.readlines() regex = re.compile ( "\n" ) st = regex.sub ( " ", string ) if re.search('ERR1',st): y=re.findall("[A-Z][a-z][a-z] [ 123][0-9] [012][0-9]:[0-5][0-9]:[0-5][0-9] [0-9][0-9][0-9][0-9]",st) print y patchnumber = re.compile(r'(\d+)\/(\d+)') ======> doesnot work it only works if I use #string = f3.read() for j in range(len(string1)): if re.search ( r'ERR2', string1[j] ): print "Error line \n", string1[j - 1] mo = patchnumber.search (string1[j-1]) a = mo.group() print a print os.getcwd() break Please advice how to proceed. Thanks, From avigross at verizon.net Tue Nov 13 23:59:24 2018 From: avigross at verizon.net (Avi Gross) Date: Tue, 13 Nov 2018 23:59:24 -0500 Subject: [Tutor] look back comprehensively Message-ID: <000001d47bd6$d04752e0$70d5f8a0$@verizon.net> I have been thinking about the thread we have had where the job seemed to be to read in a log file and if some string was found, process the line before it and generate some report. Is that generally correct? The questioner suggested they needed both the entire file as one string but also as a list of strings. Suggestions were made, IF SO, to read the entire file twice, once as a whole and once as lines. Another suggestion was to read either version ONCE and use cheaper Python methods to make the second copy. We also looked at a similar issue about using a buffer to keep the last N lines. I thought of another tack that may be in between but allow serious functionality. OUTLINE: Use just the readlines version to get a list of strings representing each line. Assuming the searched text is static, no need for regular expressions. You can ask if 'something' in line But if you find it, you may not have an index so a use of enumerate (or zip) to make a tuple might be of use. You can do a list comprehension on an enumerate object to get both the indexes where the requested 'something' was found and also optionally the line contents (ignored) and be able to use the indices to look at the index (or more) before. Here is an example using a fake program where I create the four lines and generate only the tuples needed for further processing, or just the indices for the line ABOVE where it was found. It can be done with simple list comprehensions or made into a generator expression. -CODE- """ Sample code showing how to read a (simulated) file and search for a fixed string and return the item number in a list of strings for further processing including of earlier lines. """ # Make test data without use of file fromfile = str1 = """alpha line one beta line two gamma line three alphabet line four""" lines= fromfile.split('\n') print("RAW data: ", lines) # just for illustration errors = [(index,line) for (index, line) in enumerate(lines) if 'bet' in line] just_indices = [index - 1 for (index, line) in enumerate(lines) if 'bet' in line] from pprint import pprint print("ERROR tuples:") # just for illustration pprint(errors) print("Just error indices:") pprint(just_indices) -END-CODE- -OUTPUT- RAW data: ['alpha line one', 'beta line two', 'gamma line three', 'alphabet line four'] ERROR tuples: [(1, 'beta line two'), (3, 'alphabet line four')] Just error indices: [0, 2] -END-OUTPUT- Again, this did two ways, and only one is needed. But the next step would be to iterate over the results and process the earlier line to find whatever it is you need to report. Many ways to do that such as: for (index, ignore) in errors or for index in just_indices You can use a regular expression on a line at a time. And so on. Again, other methods mentioned work fine, and using a deque to store earlier lines in a limited buffer while not reading the entire file into memory would also be a good way. Warning: the above assumes the text found will never be in the zeroeth line. Otherwise, you need to check as accessing line -1 may actually return the last line! As stated many times, there seem to be an amazing number of ways to do anything. As an example, I mentioned using zip above. One obvious method is to zip it with a range statement making it look just like enumerate. A more subtle one would be to make a copy of the set of lines the same length but each line content shifted by one. Zip that to the original and you get a tuple with (line 0, null) then (line 1, line 0) up to (line n, line n-1) Yes, that doubles memory use but you can solve so much more in one somewhat more complicated list comprehension. Anyone want to guess how? If you recall, some regular expression matches something on the previous line. Let us make believe you wrote a function called do_the_match(line, re) that applies the regular expression to the line of text and returns the matching text or perhaps an empty string if not found. So if you define that function then the following code will work. First, make my funny zip: I make lines_after as copy of lines shifted over one. Or more exactly circularly permuted by one. The goal is to search in line_after and if found, do the regular expression match in line before. >>> lines_after = lines[1:] >>> lines_after.append(lines[0]) >>> lines_after ['beta line two', 'gamma line three', 'alphabet line four', 'alpha line one'] >>> lines ['alpha line one', 'beta line two', 'gamma line three', 'alphabet line four'] >>> list(zip(lines_after, lines)) [('beta line two', 'alpha line one'), ('gamma line three', 'beta line two'), ('alphabet line four', 'gamma line three'), ('alpha line one', 'alphabet line four')] So the list comprehension looks something like this: matches = [ do_theMatch(line, re) for (line_after, line) in zip(line_after, line) if 'something' in line_after ] Need I mention the above code was used in Python 3.7.0 ??? I think it is important to try to learn the idioms and odd customs of a language even if sometimes it results in more memory or CPU usage but maybe better not to overly complicate the code till nobody understands it. The latter may be in a mild way 'elegant' but even if I made the variable names more descriptive it might be harder to understand than a simple enumerated version or several passes in more normal iteration statements. Feel free to comment. I have a thick skin and love to learn from others. Avi From devpratapsingh890 at gmail.com Wed Nov 14 08:20:40 2018 From: devpratapsingh890 at gmail.com (Dev Pratap Singh) Date: Wed, 14 Nov 2018 18:50:40 +0530 Subject: [Tutor] Request to join subscriber list Message-ID: Sir I am really excited to learn about programming languages and as python becoming very famous these days i just wanted a list of all commands of it. Please if you are able to send it From plucena24 at gmail.com Tue Nov 13 20:58:40 2018 From: plucena24 at gmail.com (Pablo Lucena) Date: Tue, 13 Nov 2018 17:58:40 -0800 Subject: [Tutor] best way to dynamically set class variables? In-Reply-To: References: <20181110000319.GN4071@ando.pearwood.info> Message-ID: I really like the idea of using Python to generate another .py file with the dynamic definitions taken into account. That way you separate the "hackery" into a single function or module, document how it works, and have it also annotate the dynamically generated python source containing the class definitions as you need them with top of file doc-string stating "this file was dynamically generated by xxx - don't modify by hand". That way your code is not confusing, you isolate the trick and the resulting source, and you have full source files with all class attributes that developers can look at and source control, rather than looking at a weird looking class definition. It's just a simple python file after all, no tricks. On Fri, Nov 9, 2018 at 11:50 PM Albert-Jan Roskam wrote: > > > On 10 Nov 2018 01:03, Steven D'Aprano wrote: > > On Thu, Nov 08, 2018 at 11:34:35AM -0500, Avi Gross wrote: > > An interesting discussion that is outside the scope of a group like this > is > > HOW malicious things can be done and perhaps how to avoid them. > > > > Isn't the rule, simply: > > this_is_stupid = eval(input("please enter malicious code: ")) > > ... and other uses range from 'code smell' to 'elegant' (where namedtuple > is an example of the latter) > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- *Pablo Lucena* From srinivasan.rns at gmail.com Wed Nov 14 03:47:08 2018 From: srinivasan.rns at gmail.com (srinivasan) Date: Wed, 14 Nov 2018 09:47:08 +0100 Subject: [Tutor] All of a sudden code started throwing errors Message-ID: Dear Python Experts Could you please let me know why am I seeing the following errors as before this it was working consistently? root:~/qa/robot_tests# python3 -m robot --variable SIGNAL_LEVEL_THRESHOLD:-60 wifi_testing.robot ============================================================================== Wifi Testing :: This is the Maschine Native OS Build Wi-Fi Test. ============================================================================== Initialize Wi-Fi Module | PASS | ------------------------------------------------------------------------------ Enable Wi-Fi Module | PASS | ------------------------------------------------------------------------------ Connect Wi-Fi Module to SSID | PASS | ------------------------------------------------------------------------------ Check for Wi-Fi Connectivity | PASS | ------------------------------------------------------------------------------ Log Wi-Fi Module IP | PASS | ------------------------------------------------------------------------------ Get Gateway IP and Check Whether Wi-Fi is Pingable | PASS | ------------------------------------------------------------------------------ Check for Wi-Fi Signal Strength with Threshold | FAIL | '-68 >= -60' should be true. ------------------------------------------------------------------------------ Wifi Testing :: This is the Maschine Native OS Build Wi-Fi Test. | FAIL | 7 critical tests, 6 passed, 1 failed 7 tests total, 6 passed, 1 failed ============================================================================== Output: /home/root/qa/robot_tests/output.xml Log: /home/root/qa/robot_tests/log.html Report: /home/root/qa/robot_tests/report.html root:~/qa/robot_tests# cat /proc/net/wireless Inter-| sta-| Quality | Discarded packets | Missed | WE face | tus | link level noise | nwid crypt frag retry misc | beacon | 22 wlp1s0: 0000 44. -66. -256 0 0 0 0 9 0 Errors: ===== root:~/qa/robot_tests# python3 -m robot --variable SIGNAL_LEVEL_THRESHOLD:-70 wifi_testing.robot Traceback (most recent call last): File "/usr/lib/python3.5/runpy.py", line 183, in _run_module_as_main mod_name, mod_spec, code = _get_module_details(mod_name, _Error) File "/usr/lib/python3.5/runpy.py", line 142, in _get_module_details return _get_module_details(pkg_main_name, error) File "/usr/lib/python3.5/runpy.py", line 109, in _get_module_details __import__(pkg_name) File "/usr/lib/python3.5/site-packages/robot/__init__.py", line 41, in from robot.rebot import rebot, rebot_cli File "/usr/lib/python3.5/site-packages/robot/rebot.py", line 40, in from robot.conf import RebotSettings File "/usr/lib/python3.5/site-packages/robot/conf/__init__.py", line 27, in from .settings import RobotSettings, RebotSettings File "/usr/lib/python3.5/site-packages/robot/conf/settings.py", line 33, in class _BaseSettings(object): File "/usr/lib/python3.5/site-packages/robot/conf/settings.py", line 46, in _BaseSettings 'OutputDir' : ('outputdir', abspath('.')), File "/usr/lib/python3.5/site-packages/robot/utils/robotpath.py", line 84, in abspath return normpath(_abspath(path), case_normalize) File "/usr/lib/python3.5/posixpath.py", line 362, in abspath cwd = os.getcwd() FileNotFoundError: [Errno 2] No such file or directory root:~/qa/robot_tests# cat /proc/net/wireless Inter-| sta-| Quality | Discarded packets | Missed | WE face | tus | link level noise | nwid crypt frag retry misc | beacon | 22 wlp1s0: 0000 45. -65. -256 0 0 0 0 12 0 root:~/qa/robot_tests# python3 -m robot wifi_testing.robot Traceback (most recent call last): File "/usr/lib/python3.5/runpy.py", line 183, in _run_module_as_main mod_name, mod_spec, code = _get_module_details(mod_name, _Error) File "/usr/lib/python3.5/runpy.py", line 142, in _get_module_details return _get_module_details(pkg_main_name, error) File "/usr/lib/python3.5/runpy.py", line 109, in _get_module_details __import__(pkg_name) File "/usr/lib/python3.5/site-packages/robot/__init__.py", line 41, in from robot.rebot import rebot, rebot_cli File "/usr/lib/python3.5/site-packages/robot/rebot.py", line 40, in from robot.conf import RebotSettings File "/usr/lib/python3.5/site-packages/robot/conf/__init__.py", line 27, in from .settings import RobotSettings, RebotSettings File "/usr/lib/python3.5/site-packages/robot/conf/settings.py", line 33, in class _BaseSettings(object): File "/usr/lib/python3.5/site-packages/robot/conf/settings.py", line 46, in _BaseSettings 'OutputDir' : ('outputdir', abspath('.')), File "/usr/lib/python3.5/site-packages/robot/utils/robotpath.py", line 84, in abspath return normpath(_abspath(path), case_normalize) File "/usr/lib/python3.5/posixpath.py", line 362, in abspath cwd = os.getcwd() FileNotFoundError: [Errno 2] No such file or directory root:~/qa/robot_tests# From srinivasan.rns at gmail.com Wed Nov 14 10:15:57 2018 From: srinivasan.rns at gmail.com (srinivasan) Date: Wed, 14 Nov 2018 16:15:57 +0100 Subject: [Tutor] Cannot find reference 'bluetoothctl' in 'sh.py' less... (Ctrl+F1) Message-ID: Dear Python Experts, As am newbie to python, I am planning to automate BT functionality test using Bluez "bluetoothctl" utility by writing python wrapper and robot framework integrated with Jenkins I came across the below link: https://www.reddit.com/r/raspberry_pi/comments/4bxu2o/bluetoothctl_in_python_program/ In the above link, I saw the below code snippet *code:* *from sh import bluetoothctl* *mac = "AA:BB:CC:DD:EE"* *bluetoothctl("connect",mac)* And firstly I wanted to verify BT functionality with my PC and the bluetooth device (basically turned on BT option in my phone and trying to discover my phone as a BT device) And I have installed the below packages in my ubuntu 18.04 desktop PC $* pip3 install sh* Collecting sh Downloading https://files.pythonhosted.org/packages/4a/22/17b22ef5b049f12080f5815c41bf94de3c229217609e469001a8f80c1b3d/sh-1.12.14-py2.py3-none-any.whl Installing collected packages: sh Successfully installed sh-1.12.14 *$ pip3 install bluetoothctl* *Collecting bluetoothctl* * Could not find a version that satisfies the requirement bluetoothctl (from versions: )* *No matching distribution found for bluetoothctl* $ *pip3 install pexpect* Collecting pexpect Downloading https://files.pythonhosted.org/packages/89/e6/b5a1de8b0cc4e07ca1b305a4fcc3f9806025c1b651ea302646341222f88b/pexpect-4.6.0-py2.py3-none-any.whl (57kB) 100% |????????????????????????????????| 61kB 1.5MB/s Collecting ptyprocess>=0.5 (from pexpect) Downloading https://files.pythonhosted.org/packages/d1/29/605c2cc68a9992d18dada28206eeada56ea4bd07a239669da41674648b6f/ptyprocess-0.6.0-py2.py3-none-any.whl Installing collected packages: ptyprocess, pexpect Successfully installed pexpect-4.6.0 ptyprocess-0.6.0 $ When I try to paste the below code on pycharm and try to point on the word "bluetoothctl" in the beginning of the line "*from sh import bluetoothctl*" *from sh import bluetoothctl* *mac = "your bluetooth mac"* *bluetoothctl("connect", mac)* In the pycharm, I see the below error message : *Cannot find reference 'bluetoothctl' in 'sh.py' less... (Ctrl+F1) * *Inspection info: This inspection detects names that should resolve but don't. Due to dynamic dispatch and duck typing, this is possible in a limited but useful number of cases. Top-level and class-level items are supported better than instance items.* Could you please help me to resolve the above issue, like why am I seeing the above issue it seems to be some importing "bluetoothhctl" module issue (sorry if my understanding is wrong) Kindly do the needful Many Thanks in advance From whitney.eichelberger at yahoo.com Wed Nov 14 13:51:44 2018 From: whitney.eichelberger at yahoo.com (Whitney Eichelberger) Date: Wed, 14 Nov 2018 13:51:44 -0500 Subject: [Tutor] (no subject) Message-ID: <7B26913C-34D0-4A4B-B896-4EB19B4B81F9@yahoo.com> I need help creating a weighted GPA calculator. I attempted to create one but I was unable to incorporate different leveled classes (College Prep, Honors, and AP). How can I incorporate those leveled classes into the calculator in order to get a final GPA From mats at wichmann.us Wed Nov 14 15:11:02 2018 From: mats at wichmann.us (Mats Wichmann) Date: Wed, 14 Nov 2018 13:11:02 -0700 Subject: [Tutor] Request to join subscriber list In-Reply-To: References: Message-ID: <595521ea-2b15-7cf2-3ab2-108198819af7@wichmann.us> On 11/14/18 6:20 AM, Dev Pratap Singh wrote: > Sir > I am really excited to learn about programming languages and as python > becoming very famous these days i just wanted a list of all commands of it. > Please if you are able to send it these are the "official" documentation links: https://docs.python.org/3/ From m at funkyhat.org Wed Nov 14 15:10:42 2018 From: m at funkyhat.org (Matt Wheeler) Date: Wed, 14 Nov 2018 20:10:42 +0000 Subject: [Tutor] All of a sudden code started throwing errors In-Reply-To: References: Message-ID: <49C5C430-ECEE-4EA2-BE53-D6FB2615AFD1@funkyhat.org> > On 14 Nov 2018, at 13:51, Peter Otten <__peter__ at web.de> wrote: > > Diagnosis: you managed to remove your current working directory, probably > because you used os.chdir() to switch to a temporary directory and then > forgot to switch back. > > Solution: don't do that ;) > > I recommend that you avoid chdir() in your code and instead always include > the directory into the filename. A pattern I find myself using frequently, when I do need to ?cd?, is to write a tiny context manager: ``` import os from contextlib import context manager @contextmanager def pushd(path): old_dir = os.getcwd() os.chdir(path) try: yield finally: os.chdir(old_dir) ``` (I tend to just copy this into projects where I need it (or write it again), as a whole dependency for something so tiny seems like it would be overkill :) -- Matt Wheeler http://funkyh.at From cs at cskk.id.au Wed Nov 14 15:50:19 2018 From: cs at cskk.id.au (Cameron Simpson) Date: Thu, 15 Nov 2018 07:50:19 +1100 Subject: [Tutor] GPA calculator In-Reply-To: <7B26913C-34D0-4A4B-B896-4EB19B4B81F9@yahoo.com> References: <7B26913C-34D0-4A4B-B896-4EB19B4B81F9@yahoo.com> Message-ID: <20181114205019.GA83530@cskk.homeip.net> On 14Nov2018 13:51, Whitney Eichelberger wrote: >I need help creating a weighted GPA calculator. I attempted to create >one but I was unable to incorporate different leveled classes (College >Prep, Honors, and AP). How can I incorporate those leveled classes into >the calculator in order to get a final GPA Please always include your code, and some sample data and the output, and explain what isn't as you want it. Past that into your message - this list does not do attachments. Also, please explain terms like "GPA". I imagine it may mean "Grade Point Average", but really it might mean almost anything. On the tutor list we're happy to help with code by expalining problems and suggesting solutions, but we don't write things outright - you need to do that yourself in order to learn. Finally, I put a better description in the Subject: line above. It is useful so that people know what various discussions are about. Cheers, Cameron Simpson From david at graniteweb.com Wed Nov 14 16:00:25 2018 From: david at graniteweb.com (David Rock) Date: Wed, 14 Nov 2018 15:00:25 -0600 Subject: [Tutor] GPA calculator In-Reply-To: <20181114205019.GA83530@cskk.homeip.net> References: <7B26913C-34D0-4A4B-B896-4EB19B4B81F9@yahoo.com> <20181114205019.GA83530@cskk.homeip.net> Message-ID: <2291362C-9A34-4C69-A852-6465A2C21C7C@graniteweb.com> > On Nov 14, 2018, at 14:50, Cameron Simpson wrote: > > On 14Nov2018 13:51, Whitney Eichelberger wrote: >> I need help creating a weighted GPA calculator. I attempted to create one but I was unable to incorporate different leveled classes (College Prep, Honors, and AP). How can I incorporate those leveled classes into the calculator in order to get a final GPA > > Please always include your code, and some sample data and the output, and explain what isn't as you want it. > > Past that into your message - this list does not do attachments. > > Also, please explain terms like "GPA". I imagine it may mean "Grade Point Average", but really it might mean almost anything. One assumes you are talking about school, but regardless, you need to be able to explain your goal before you can code it. For example, do you know how to manually calculate a weighted GPA? Do you know how your school does the weighting (.5 point, full point, etc)? This link has a good overview of the process/steps you probably need to work through. https://www.wikihow.com/Calculate-Your-Weighted-GPA let us know what you have tried so far and where you get stuck, and we will be better able to help. ? David Rock david at graniteweb.com From steve at pearwood.info Wed Nov 14 16:41:12 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 15 Nov 2018 08:41:12 +1100 Subject: [Tutor] Cannot find reference 'bluetoothctl' in 'sh.py' less... (Ctrl+F1) In-Reply-To: References: Message-ID: <20181114214112.GH4071@ando.pearwood.info> On Wed, Nov 14, 2018 at 04:15:57PM +0100, srinivasan wrote: [...] > $* pip3 install sh* You don't need to show every command you ran, if they worked successfully. We're volunteers, giving our own efforts for free, not being paid by the hour. The more irrelevant detail you drop in our laps, the fewer of us will bother reading it all. Cut out anything not relevant to your actual problem. Please read this: http://sscce.org/ which will help you debug your own problems, and ask better questions when you can't debug them yourself. However, error messages are important: I missed this the first two times I read your post, and only spotted it by accident: > *$ pip3 install bluetoothctl* > *Collecting bluetoothctl* > * Could not find a version that satisfies the requirement bluetoothctl > (from versions: )* > *No matching distribution found for bluetoothctl* You tried to install a package which doesn't exist, or pip cannot find it. Since it is not installed, naturally later on you will be unable to import that package. > When I try to paste the below code on pycharm and try to point on the word > "bluetoothctl" in the beginning of the line "*from sh import bluetoothctl*" > > *from sh import bluetoothctl* [...] > In the pycharm, I see the below error message : > > *Cannot find reference 'bluetoothctl' in 'sh.py' less... (Ctrl+F1) * > *Inspection info: This inspection detects names that should resolve but > don't. Due to dynamic dispatch and duck typing, this is possible in a > limited but useful number of cases. Top-level and class-level items are > supported better than instance items.* That means that PyCharm's linter cannot find any name called "bluetoothctl" in the sh.py package. Because PyCharm doesn't know what sort of name bluetoothctl is (a subpackage, a module, a class, a function, a variable...) it cannot be more specific than saying it can't resolve the reference. In some (rare!) cases, that's not actually a missing name. It just means that PyCharm isn't clever enough to determine where the name is, but if you actually *run the code* the Python interpreter will find it and the code will run correctly. But this is not the case this time, since you don't actually have bluetoothctl installed, so it cannot be imported. https://duckduckgo.com/?q=bluetoothctl+python -- Steve From steve at pearwood.info Wed Nov 14 17:01:31 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 15 Nov 2018 09:01:31 +1100 Subject: [Tutor] look back comprehensively In-Reply-To: <000001d47bd6$d04752e0$70d5f8a0$@verizon.net> References: <000001d47bd6$d04752e0$70d5f8a0$@verizon.net> Message-ID: <20181114220131.GI4071@ando.pearwood.info> On Tue, Nov 13, 2018 at 11:59:24PM -0500, Avi Gross wrote: > I have been thinking about the thread we have had where the job seemed to be > to read in a log file and if some string was found, process the line before > it and generate some report. Is that generally correct? If that description is correct, then the solution is trivial: iterate over the file, line by line, keeping the previous line: previous_line = None for current_line in file: process(current_line, previous_line) previous_line = current_line No need for complex solutions, or memory-hungry solutions that require reading the entire file into memory at once (okay for, say, a million lines, but not if your logfile is 2GB in size). If you need the line number: previous_line = None for line_num, current_line in enumerate(file, 1): process(current_line, previous_line) previous_line = current_line > Use just the readlines version to get a list of strings representing each > line. That requires reading the entire file into memory at once. That may be acceptable if your file is guaranteed to be small, but since log files can grow big enough to fill hard drives, that might not be a good assumption for serious production-quality scripts. -- Steve From avigross at verizon.net Wed Nov 14 19:33:37 2018 From: avigross at verizon.net (Avi Gross) Date: Wed, 14 Nov 2018 19:33:37 -0500 Subject: [Tutor] python commandos Message-ID: <004601d47c7a$d9f76330$8de62990$@verizon.net> Dev, There are many ways to learn a programming language but a list of all commands is not necessarily a good way. I suspect your real question is how do you learn Python as either a beginner at this language or with little knowledge of computers in general. There are tons of books and classes both online and in the real world that are a better way to start. Python has a very short list of commands as in reserved words. Many are the same or similar to other languages such as if/elif/else/while/for and so on. What matters more is what they do and how they are used and the CONCEPTS manipulated and even the philosophies involved in what commands are there and even what commands are NOT there. There are also lists of operators like + += / * >= and so on, some of which are also reserved words in the first list like and/in/not. Again, what matters is not the exact names but what they do and don't do. There are tons of names for built-in functions but you can trivially replace them or import literally tens of thousands or more other functions. There are all kinds of objects, including the built in, with various names and many have all kinds of internal parts with names you can access and manipulate. The answer to your question is that there are more names than your combined vocabulary in all the human languages you speak but you don't need to know them till you decide to use them. If you want some simpler books or tutorials, many libraries (or book sellers) let you search for key words like "Python". There are many free sources including what you can find here: https://www.python.org/ Specifically, you may want to look here: https://www.python.org/doc/ If you got to this mailing list, you may already have been there. Make sure you get educated especially in whatever version of Python you are using as there are changes with each version with a serious disconnect between version 3 and anything before. -----Original Message----- From: Tutor On Behalf Of Dev Pratap Singh Sent: Wednesday, November 14, 2018 8:21 AM To: tutor at python.org Subject: [Tutor] Request to join subscriber list Sir I am really excited to learn about programming languages and as python becoming very famous these days i just wanted a list of all commands of it. Please if you are able to send it _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From johnblower2016 at outlook.com Thu Nov 15 08:18:16 2018 From: johnblower2016 at outlook.com (John Blower) Date: Thu, 15 Nov 2018 13:18:16 +0000 Subject: [Tutor] Problem using termcolor and colorama Message-ID: I try to create colour text by using these two modules , only returns the colour code, no colour text. How to solve this problem. >>> import termcolor >>> import colorama >>> colorama.init() >>> termcolor.cprint('Hello World','red') [31mHello World[0m Sent from Mail for Windows 10 From david at graniteweb.com Thu Nov 15 12:08:32 2018 From: david at graniteweb.com (David Rock) Date: Thu, 15 Nov 2018 11:08:32 -0600 Subject: [Tutor] Problem using termcolor and colorama In-Reply-To: References: Message-ID: <2DC54BBB-3FDF-4561-8E56-006740930C68@graniteweb.com> > On Nov 15, 2018, at 07:18, John Blower wrote: > > I try to create colour text by using these two modules , only returns the colour code, no colour text. > How to solve this problem. > >>>> import termcolor >>>> import colorama >>>> colorama.init() >>>> termcolor.cprint('Hello World','red') > [31mHello World[0m I don?t know what those modules are, but based on the output, where you use them will matter. Those are the correct ESC codes to print something in red, but your terminal needs to support displaying the colors. If you are trying this in the python interpreter, try making a basic test script and run it instead to see if it works outside the interpreter environment. I assume you are on Windows, too. What OS and what version of Python are you using? ? David Rock david at graniteweb.com From steve at pearwood.info Thu Nov 15 12:10:00 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 16 Nov 2018 04:10:00 +1100 Subject: [Tutor] Problem using termcolor and colorama In-Reply-To: References: Message-ID: <20181115170959.GJ4071@ando.pearwood.info> On Thu, Nov 15, 2018 at 01:18:16PM +0000, John Blower wrote: > I try to create colour text by using these two modules , only returns the colour code, no colour text. > How to solve this problem. Use a terminal that supports colour codes. > >>> import termcolor > >>> import colorama > >>> colorama.init() > >>> termcolor.cprint('Hello World','red') > [31mHello World[0m What platform are you using? What terminal are you using? What terminals does termcolor support? What does the documentation for termcolor say? -- Steve From __peter__ at web.de Thu Nov 15 15:58:40 2018 From: __peter__ at web.de (Peter Otten) Date: Thu, 15 Nov 2018 21:58:40 +0100 Subject: [Tutor] Problem using termcolor and colorama References: Message-ID: John Blower wrote: > I try to create colour text by using these two modules , only returns the > colour code, no colour text. How to solve this problem. > >>>> import termcolor >>>> import colorama >>>> colorama.init() >>>> termcolor.cprint('Hello World','red') > [31mHello World[0m Start with one module. Does >>> import colorama >>> print(colorama.Fore.RED + "Hello" + colorama.Style.RESET_ALL) Hello print Hello in red? If yes, how do >>> colorama.Fore.RED + "Hello" + colorama.Style.RESET_ALL '\x1b[31mHello\x1b[0m' and >>> termcolor.colored("Hello", "red") '\x1b[31mHello\x1b[0m' differ on your system? If no, does >>> from ctypes import windll Traceback (most recent call last): File "", line 1, in ImportError: cannot import name 'windll' raise an exception on your system? This is expected on my (Linux) system, but on Windows it probably means that something went wrong. The pypi page says """ I?ve personally only tested it on Windows XP (CMD, Console2), Ubuntu (gnome- terminal, xterm), and OS X. """ From breakfast.music.xo at gmail.com Thu Nov 15 18:38:28 2018 From: breakfast.music.xo at gmail.com (Break fast) Date: Thu, 15 Nov 2018 15:38:28 -0800 Subject: [Tutor] Regarding "IDLE Subprocess Didn't Make Connection" Error Message-ID: Hello Python Tutors, First off, thank you for existing! I hope I am emailing the right queue in this situation. I have installed Python from the python.org website, but have found myself unable to use IDLE due to an error that is as follows: "IDLE subprocess didn't make connection. Either IDLE can't start a subprocess, or personal Firewall software is blocking the connection" I have been researching this issue nonstop the last two days, and haven't had any luck in resolving the issue. I am running Windows 7 (x64), and have installed Python 3.7.1 and 3.6.7 numerous times now. >I have shut off Avast (the only firewall on this pc). >Scoured the Python root folder for any .py files, and there are none. >Restarted PC after each install. >Installed Python to a different harddrive. >Ran IDLE as Admin And I am still encountering the same issue. If anybody has any insight on how to proceed from here, I would be extremely grateful for any advice you could offer. Thank you again, and hope you all have a good day! --Logan McDonald Virus-free. www.avast.com <#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2> From avigross at verizon.net Fri Nov 16 10:56:32 2018 From: avigross at verizon.net (Avi Gross) Date: Fri, 16 Nov 2018 15:56:32 +0000 (UTC) Subject: [Tutor] Regarding "IDLE Subprocess Didn't Make Connection" Error References: <76112270.1707612.1542383792886.ref@mail.yahoo.com> Message-ID: <76112270.1707612.1542383792886@mail.yahoo.com> BREAK fast , You certainly haven't been idle! Until you get IDLE working ideally, you can still use one of many text editors and programming environments and perhaps run python on the files manually. Have you verified that the rest of your installation worked? Sent from AOL Mobile Mail On Friday, November 16, 2018 Break fast wrote: Hello Python Tutors, First off, thank you for existing! I hope I am emailing the right queue in this situation. I have installed Python from the python.org website, but have found myself unable to use IDLE due to an error that is as follows: "IDLE subprocess didn't make connection. Either IDLE can't start a subprocess, or personal Firewall software is blocking the connection" I have been researching this issue nonstop the last two days, and haven't had any luck in resolving the issue. I am running Windows 7 (x64), and have installed Python 3.7.1 and 3.6.7 numerous times now. >I have shut off Avast (the only firewall on this pc). >Scoured the Python root folder for any .py files, and there are none. >Restarted PC after each install. >Installed Python to a different harddrive. >Ran IDLE as Admin And I am still encountering the same issue. If anybody has any insight on how to proceed from here, I would be extremely grateful for any advice you could offer. Thank you again, and hope you all have a good day! --Logan McDonald Virus-free. www.avast.com <#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2> _______________________________________________ Tutor maillist? -? Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From anilduggirala at fastmail.fm Fri Nov 16 14:31:42 2018 From: anilduggirala at fastmail.fm (Anil Duggirala) Date: Fri, 16 Nov 2018 14:31:42 -0500 Subject: [Tutor] Info on APIs and handling JSON Message-ID: <20181116193142.bv33lzm3q5k3hpnn@adell35> hello, I have been looking for documentation about how to connect to APIs and using the provided info within python. I have found a few websites talking about very particular cases. Probably the most enlightening was the 'requests' library documentation. Can someone please point me to serious and comprehensive info on how to deal with APIs and then handling JSON and other provided info in python? Maybe a book? thanks a lot. From mats at wichmann.us Fri Nov 16 18:06:01 2018 From: mats at wichmann.us (Mats Wichmann) Date: Fri, 16 Nov 2018 16:06:01 -0700 Subject: [Tutor] Info on APIs and handling JSON In-Reply-To: <20181116193142.bv33lzm3q5k3hpnn@adell35> References: <20181116193142.bv33lzm3q5k3hpnn@adell35> Message-ID: On 11/16/18 12:31 PM, Anil Duggirala wrote: > hello, > > I have been looking for documentation about how to connect to APIs and > using the provided info within python. I have found a few websites > talking about very particular cases. Probably the most enlightening was > the 'requests' library documentation. Can someone please point me to > serious and comprehensive info on how to deal with APIs and then > handling JSON and other provided info in python? Maybe a book? json handling is simple: the json module is part of the Python standard library. https://docs.python.org/3/library/json.html APIs have no specific answer. An API is an Application Programming Interface, each is different and specific to the project in question but refers to how you communicate with the functionality of that project from your program. Some popular ones have Python adaptations available, otherwise you read the documentation and figure out how to code your own. Sometimes the API is a style that lets you use an existing Python project, even if it doesn't know anything about the thing you want to talk to. requests is a good example - is the project you're interested in provides a RESTful API, using HTTP protocols, then requests can certainly be used to talk to it, but you still need to discover the specifics and code them up yourself. Other APIs provide other interface styles. From steve at pearwood.info Sat Nov 17 06:17:48 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 17 Nov 2018 22:17:48 +1100 Subject: [Tutor] Regarding "IDLE Subprocess Didn't Make Connection" Error In-Reply-To: References: Message-ID: <20181117111745.GP4071@ando.pearwood.info> On Thu, Nov 15, 2018 at 03:38:28PM -0800, Break fast wrote: > "IDLE subprocess didn't make connection. Either IDLE can't start a > subprocess, or personal Firewall software is blocking the connection" > > I have been researching this issue nonstop the last two days, Without pausing to eat or sleep? You're more dedicated than I would be... > and haven't had any luck in resolving the issue. This is a common issue, there's probably a bazillion places that talk about it: https://duckduckgo.com/?q=IDLE+subprocess+didn%27t+make+connection Unfortunately there are many different things which could cause it (the error message is, frankly, unhelpful). This bug report looks promising: https://bugs.python.org/issue14576 Try making sure that the HOME environment variable exists, and is writable. > I am running Windows 7 (x64), and have installed Python 3.7.1 and 3.6.7 > numerous times now. Why? Reinstalling should be the *last* resort, not first, and especially not reinstalling over and over and over again. -- Steve From steve at pearwood.info Sat Nov 17 06:32:17 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 17 Nov 2018 22:32:17 +1100 Subject: [Tutor] Regarding "IDLE Subprocess Didn't Make Connection" Error In-Reply-To: References: Message-ID: <20181117113217.GQ4071@ando.pearwood.info> Another thought comes to mind... On Thu, Nov 15, 2018 at 03:38:28PM -0800, Break fast wrote: > "IDLE subprocess didn't make connection. Either IDLE can't start a > subprocess, or personal Firewall software is blocking the connection" > > I have been researching this issue nonstop the last two days, and haven't > had any luck in resolving the issue. > > > I am running Windows 7 (x64), and have installed Python 3.7.1 and 3.6.7 > numerous times now. Try opening a Windows console (command prompt) and launching IDLE from that, just in case some more informative error messages are printed to the console. You can try these commands: python36 -m idlelib python37 -m idlelib and if they print any error messages, please copy the ENTIRE message and send them to this list. -- Steve From avigross at verizon.net Sat Nov 17 12:38:07 2018 From: avigross at verizon.net (Avi Gross) Date: Sat, 17 Nov 2018 12:38:07 -0500 Subject: [Tutor] Regarding "IDLE Subprocess Didn't Make Connection" Error In-Reply-To: <20181117111745.GP4071@ando.pearwood.info> References: <20181117111745.GP4071@ando.pearwood.info> Message-ID: <006701d47e9c$4d822600$e8867200$@verizon.net> I was wondering if I was the only one who felt the urge to apply a tad of humor and suppressed most of the thoughts about idol/IDLE worship and other puns so I am glad to see Steve do just a little of the same. It seems that despite how portable Python is touted to be, quite a few people report problems in getting the basics working when installing. And if you can't get started, that is obviously a barrier. My suggestion earlier was based on the fact that IDLE is an add-on and they should first check if Python itself works. Any text editor can be used that just produces plain text and scripts can be run in other ways. But I have a thought. An old an often effective method to solve a problem is to search in the source code. Yes, you did not write IDLE. I am still not clear on how IDLE aborts on startup but I recall that IDLE may be written in Python with source code available. I went into python to see where to look by seeing the path it would search. >>> import sys >>> print(sys.path) I then followed a trail and what I found was idlelib with many components and when I import that library in the IDLE main unit I see some of the results: >>> import idlelib >>> dir(idlelib) ['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'autocomplete', 'autocomplete_w', 'calltip_w', 'calltips', 'config', 'debugger', 'debugger_r', 'debugobj', 'debugobj_r', 'hyperparser', 'iomenu', 'macosx', 'multicall', 'pyparse', 'rpc', 'run', 'scrolledlist', 'stackviewer', 'testing', 'tree', 'windows', 'zoomheight'] I could potentially follow along, and for a while I did. I think that after a few steps, on my machine, it hits pyshell in C:\Users\avid2016\AppData\Local\Programs\Python\Python37-32\Lib\idlelib and then runs code starting with: #! /usr/bin/env python3 import sys try: from tkinter import * except ImportError: print("** IDLE can't import Tkinter.\n" "Your Python may not be configured for Tk. **", file=sys.__stderr__) raise SystemExit(1) And it goes on and on. It imports all kinds of things and creates many classes and if called directly, runs a big function called main. I searched for "subprocess" and "IDLE" to see if the file had whatever error message you saw and I think I found it in the definition of a function: def display_no_subprocess_error(self): tkMessageBox.showerror( "Subprocess Startup Error", "IDLE's subprocess didn't make connection. Either IDLE can't " "start a subprocess or personal firewall software is blocking " "the connection.", parent=self.tkconsole.text) Correction, the first argument of "self' was a hint and that is actually a method in: class ModifiedInterpreter(InteractiveInterpreter): Now if that is the error message, you work backwards to see when it might have been called/triggered. Searching for a call to that method shows two places: Here is one snippet: self.spawn_subprocess() #time.sleep(20) # test to simulate GUI not accepting connection # Accept the connection from the Python execution server self.rpcclt.listening_sock.settimeout(10) try: self.rpcclt.accept() except socket.timeout: self.display_no_subprocess_error() return None Here is the other: self.spawn_subprocess() try: self.rpcclt.accept() except socket.timeout: self.display_no_subprocess_error() return None I am going to stop now and suggest it happens when IDLE creates a subprocess to run in parallel and plays games with sockets and fails. FWIW, the method called before is: def spawn_subprocess(self): if self.subprocess_arglist is None: self.subprocess_arglist = self.build_subprocess_arglist() self.rpcsubproc = subprocess.Popen(self.subprocess_arglist) Presumably something goes wrong in there but I suspect the Popen failed. Why would it fail? Well, it could be many things including whatever actual info is in the arglist ... That might take plenty of additional digging including suggestions about something not being set right in the path or other registry components or some rogue library component being imported rather than the intended one or so much more. But, in theory, you can even take the source code and run it more carefully and debug this. Not something for a novice. Not sure if that helps but if I actually had to read all that code and do serious debugging, it might take me days and I have a funeral and a party (unrelated) to go to today alone ? -----Original Message----- From: Tutor On Behalf Of Steven D'Aprano Sent: Saturday, November 17, 2018 6:18 AM To: tutor at python.org Subject: Re: [Tutor] Regarding "IDLE Subprocess Didn't Make Connection" Error On Thu, Nov 15, 2018 at 03:38:28PM -0800, Break fast wrote: > "IDLE subprocess didn't make connection. Either IDLE can't start a > subprocess, or personal Firewall software is blocking the connection" > > I have been researching this issue nonstop the last two days, Without pausing to eat or sleep? You're more dedicated than I would be... > and haven't had any luck in resolving the issue. This is a common issue, there's probably a bazillion places that talk about it: https://duckduckgo.com/?q=IDLE+subprocess+didn%27t+make+connection Unfortunately there are many different things which could cause it (the error message is, frankly, unhelpful). This bug report looks promising: https://bugs.python.org/issue14576 Try making sure that the HOME environment variable exists, and is writable. > I am running Windows 7 (x64), and have installed Python 3.7.1 and 3.6.7 > numerous times now. Why? Reinstalling should be the *last* resort, not first, and especially not reinstalling over and over and over again. -- Steve _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From harry_oneill_52 at hotmail.co.uk Sat Nov 17 20:55:20 2018 From: harry_oneill_52 at hotmail.co.uk (Harry Oneill) Date: Sun, 18 Nov 2018 01:55:20 +0000 Subject: [Tutor] Finding the largest gap in tuple between two lists. Message-ID: Hello there everyone i hope you are all doing fantastic. Im currently working on a little program that is designed to do a basic function to help me to trade efficiently within the game Star Citizen: #1 take two inputs ( location and destination ) #2 Find what product is best to buy at location to sell at destination #3 Print the results currently i have figured out how to get python to take the two inputs and provide me with the lowest buy price and highest sell price. However the information i want to produce is what has the largest profit margin possible between the two locations so i need to compare what items are available in both locations and then see what one has the largest price difference then display that items name. Below is the code i have currently made for this. Current_Location = input("Where are you currently located? :").lower() Current_Destination = input("Where are you planning to travel? :").lower() def Find_Lowest_Buy_Olisar(): print("Finding lowest buy price for OLISAR...") print(*Sort_Buying_Olisar[:1], sep='\n') def Find_Highest_Sell_Olisar(): print("Finding highest sell price for OLISAR...") print(*Sort_Selling_Olisar[:1], sep='\n') def Find_Lowest_Buy_Levski(): print("Finding lowest buy price for LEVSKI...") print(*Sort_Buying_Levski[:1], sep='\n') def Find_Highest_Sell_Levski(): print("Finding highest sell price for LEVSKI...") print(*Sort_Selling_Levski[:1], sep='\n') Buying_Olisar = [('Medical Supply', 17.01), ('Waste', 0.005),] Sort_Buying_Olisar = sorted_by_second = sorted(Buying_Olisar, key=lambda tup: tup[1]) Selling_Olisar = [('Agricium', 25.60), ('Aluminum', 1.25), ('Beryl', 4.26), ('Chlorine', 1.57), ('Corundum', 2.53), ('Diamond', 6.90), ('Distilled Spirits', 4.95), ('Fluorine', 2.80), ('Gold', 6.07), ('Hydrogen', 1.02), ('Iodine', 0.41), ('Laranite', 28.91), ('Processed Food', 1.39), ('Quartz', 1.44), ('Scrap', 1.67), ('Stims', 3.40), ('Titanium', 8.27), ('Tungsten', 3.90),] Sort_Selling_Olisar = sorted(Selling_Olisar, key=lambda tup:(-tup[1], tup[0])) Buying_Levski = [('Agricultural Supply', 1.11), ('Aluminum', 1.20), ('Hydrogen', 0.98), ('Iodine', 0.38), ('Quartz', 1.37), ('Waste', 0.005),] Sort_Buying_Levski = sorted_by_second = sorted(Buying_Levski, key=lambda tup: tup[1]) Selling_Levski = [('Agricium', 25.60), ('Altruciatoxine', 11.63), ('Beryl', 4.25), ('Chlorine', 1.56), ('Corundum', 2.53), ('Diamond', 6.07), ('Distilled Spirits', 4.95), ('Fluorine', 2.80), ('Gold', 6.07), ('Laranite', 28.25), ('Medical Supply', 18.00), ('Processed Food', 1.38), ('Scrap', 1.68), ('Stims', 3.40), ('Titanium', 8.27), ('Tungsten', 3.90), ('Widdow', 24.00),] Sort_Selling_Levski = sorted(Selling_Levski, key=lambda tup:(-tup[1], tup[0])) if Current_Location == "olisar": Find_Lowest_Buy_Olisar() elif Current_Location == "levski": Find_Lowest_Buy_Levski() else: print("Unknown location please try again.") if Current_Destination == "olisar": Find_Highest_Sell_Olisar() elif Current_Destination == "levski": Find_Highest_Sell_Levski() else: print("Unknown location please try again.") Any input would be hugely appreciated. Kind regards, Harry O'Neill From mats at wichmann.us Sun Nov 18 09:45:16 2018 From: mats at wichmann.us (Mats Wichmann) Date: Sun, 18 Nov 2018 07:45:16 -0700 Subject: [Tutor] Finding the largest gap in tuple between two lists. In-Reply-To: References: Message-ID: <4922c43c-9903-175b-c089-056970874f64@wichmann.us> On 11/17/18 6:55 PM, Harry Oneill wrote: > Hello there everyone i hope you are all doing fantastic. > > Im currently working on a little program that is designed to do a basic function to help me to trade efficiently within the game Star Citizen: > > #1 take two inputs ( location and destination ) > #2 Find what product is best to buy at location to sell at destination > #3 Print the results > > currently i have figured out how to get python to take the two inputs and provide me with the lowest buy price and highest sell price. > > However the information i want to produce is what has the largest profit margin possible between the two locations so i need to compare what items are available in both locations and then see what one has the largest price difference then display that items name. > > Below is the code i have currently made for this. > > > Any input would be hugely appreciated. > > Kind regards, > > Harry O'Neill Harry, sounds like a fun little project. Thanks for asking it in a way that makes it easy to read! I'll give you a couple of ideas to think about, others may have completely different thoughts. == First off, think about how to represent the data you have. 1. while it is true that the item and the price can be considered a tuple, and then you can put those in a list, any time in Python (and in a number of other programming languages) you have what look like mapping a key to a data value, you should think dictionary - then you access the price by indexing by the goods string. An example of just quickly manipulating what you already have into dictionaries: Buying_Levski = {'Agricultural Supply': 1.11, 'Aluminum': 1.20, 'Hydrogen': 0.98, 'Iodine': 0.38, 'Quartz': 1.37, 'Waste': 0.005,} the price of buying Hydrogen on Levski is thus Buying_Levksi['Hydrogen'], which is a lot more readable than a numeric index into a tuple which is itself obtained by a numeric index into a list. Lists *do* have the advantage of being easy to sort, but bear with me. 2. storing the data pertinent to an endpoint by the name of the endpoint means each investigation has to hardcode the name of those endpoint variables. You probably want to think of a data structure that will let you fish out the pricing dictionary by endpoint name and transaction type (buy or sell). === Second, let's rethink how you can extract what you want from this data. 1. From an endpoint pair - say Buy/Levski and Sell/Olisar, you need the common items. No point in looking further at items that can't both be bought at the source and sold at the destination. Fortunately, Python sets are really good at this, but first you have to extract only the item names from your dictionary, since that comparison is based only on good names. Something like this: def common(curr, other): return set(curr.keys()).intersection(set(other.keys())) aka build a set using the keys from the first argument, and call the intersection method on that set giving it the set built from the keys of the second argument. Which you can call this way to see that it's working: possibles = common(Buying_Levski, Selling_Olisar) print("common Levski -> Olisar", possibles) (you can certainly solve this in other ways than using sets) 2. From the common items, figure out which provides the greatest difference between the buy price and sell price. We'll presume, because you didn't mention it, that there is no volume component to this and that these prices are all for one unit and units consume the same amount of carrying capacity in your vessel. So one way to tackle this might be to build a new dictionary containing the items and their price difference. Given our previous idea of having a set which contains the common items: prices = {k: Selling_Olisar[k] - Buying_Levski[k] for k in possibles} That's pretty concise... it's called a "dictionary comprehension" if you haven't seen those before, and means loop through "possibles" saving the possibles item as the key and the difference between sell price and buy price as the value. Note1: see how irritating it is to have to keep referring to the data structures by name this way? Note2: you'll probably get a surprise when you do this - floating point numbers don't always represent "cleanly", so the subtractions might give you intesting-looking values if you print them out. There are answers for that, too. Given that dictionary, what is the key corresponding to the largest value - the good you want to buy? That's a little tricky with a dictionary, but with some manipulation, or constructing the proper sort function as an argument to the max() function, it can be done. From mats at wichmann.us Sun Nov 18 11:18:46 2018 From: mats at wichmann.us (Mats Wichmann) Date: Sun, 18 Nov 2018 09:18:46 -0700 Subject: [Tutor] Regarding "IDLE Subprocess Didn't Make Connection" Error In-Reply-To: <006701d47e9c$4d822600$e8867200$@verizon.net> References: <20181117111745.GP4071@ando.pearwood.info> <006701d47e9c$4d822600$e8867200$@verizon.net> Message-ID: On 11/17/18 10:38 AM, Avi Gross wrote: > I was wondering if I was the only one who felt the urge to apply a tad of humor and suppressed most of the thoughts about idol/IDLE worship and other puns so I am glad to see Steve do just a little of the same. > > It seems that despite how portable Python is touted to be, quite a few people report problems in getting the basics working when installing. And if you can't get started, that is obviously a barrier. My suggestion earlier was based on the fact that IDLE is an add-on and they should first check if Python itself works. Any text editor can be used that just produces plain text and scripts can be run in other ways. > > But I have a thought. An old an often effective method to solve a problem is to search in the source code. Yes, you did not write IDLE. > > I am still not clear on how IDLE aborts on startup but I recall that IDLE may be written in Python with source code available. actually, one of the "surprising" ways IDLE can fail for people is specifically because it is written in Python itself: if they have a Python file in the directory they are starting it from that duplicates the name of an important file in IDLE - if the import statement is encountered in IDLE and that import can be satisfied by the local file it picks that one first, and then IDLE breaks... anyway, one of the common things people say to get around this is to just dump IDLE. this sounds harsh: Python strives to supply a usable beginner environment and then it doesn't work, and we say "so don't use it"? well, it doesn't bother me to say that... there are tons of IDE-type environments for Python, ones that call themselves editors but have "run script inside the editor" behavior and ones that are full IDE. By all means, try the information on the internet on getting IDLE working, but it's not worth spending a *ton* of time on it, IDLE is not Python, it's just a helpful tool, and if a tool doesn't work but another one will, throw away the broken one. Here are some thoughts: https://wiki.python.org/moin/PythonEditors https://wiki.python.org/moin/IntegratedDevelopmentEnvironments those are user-edited pages, if anyone has more favorites that are not listed there, please feel free to volunteer to add more. Between several systems I have the following things that let me edit and run Python code: IDLE (yup, works for me) Eric Atom (with additional Python configuration for nicer env) Eclipse (with Python environment pydev) PyCharm (with additional Python configuration for nicer env) SublimeText (with additional Python configuration for nicer env) Visual Studio Code (with additional Python configuration for nicer env) vim some often favored Windows tools don't appear on this list, like Code::Blocks, because I'm not principally a Windows user. I don't use all of those: some not at all, some frequently, some occasionally. They all work fine. All are free except SublimeText; PyCharm comes in both free community and non-free other editions. Some are big and heavy (I don't recommend Eclipse for the faint of heart), some are not. From asad.hasan2004 at gmail.com Sun Nov 18 10:18:55 2018 From: asad.hasan2004 at gmail.com (Asad) Date: Sun, 18 Nov 2018 20:48:55 +0530 Subject: [Tutor] how to print lines which contain matching words or strings Message-ID: Hi All , I have a set of words and strings : like : p = [A ,"B is good" ,123456 , "C "] I have a file in which I need to print only the lines which matches the pattern in p thanks, -- Asad Hasan +91 9582111698 From avigross at verizon.net Sun Nov 18 08:11:36 2018 From: avigross at verizon.net (Avi Gross) Date: Sun, 18 Nov 2018 08:11:36 -0500 Subject: [Tutor] Finding the largest gap in tuple between two lists. In-Reply-To: References: Message-ID: <003c01d47f40$3c70d8d0$b5528a70$@verizon.net> Harry, Your code may not have come through as intended as the formatting often combined lines. It may help to think about the question before giving deeper answers. You have two places and two sets of goods with prices. You want the intersection of those two to focus on what goods are in both. There are various ways to do that ranging from using sets to creating an empty object (such as a list or dictionary) then iterating over all items in group A and adding only what is also IN group B. You then want to maximize the difference in price for a particular item in group A versus B. If you have the right data structures, that is a snap. Suppose you made two dictionaries holding the keys in A and the keys in B alongside values for each. You could iterate over the keys in either dictionary and calculate A[key] = B[key] and if it is higher than the previous high, replace the previous high while also saving the value of key. Is that roughly what is needed? You buy one item with the maximum "profit"? I can imagine having a fixed dollar amount to spend and a similar problem where you can buy as many as you can afford of that item as a variant or even a knapsack version where you might buy cheaper ones with any money left, ... If you post again, see if you can find a way to have the text formatted properly as we have seen examples where Python programs get different results, or fail completely, with changes in indentation. -----Original Message----- From: Tutor On Behalf Of Harry Oneill Sent: Saturday, November 17, 2018 8:55 PM To: tutor at python.org Subject: [Tutor] Finding the largest gap in tuple between two lists. Hello there everyone i hope you are all doing fantastic. Im currently working on a little program that is designed to do a basic function to help me to trade efficiently within the game Star Citizen: #1 take two inputs ( location and destination ) #2 Find what product is best to buy at location to sell at destination #3 Print the results currently i have figured out how to get python to take the two inputs and provide me with the lowest buy price and highest sell price. However the information i want to produce is what has the largest profit margin possible between the two locations so i need to compare what items are available in both locations and then see what one has the largest price difference then display that items name. Below is the code i have currently made for this. Current_Location = input("Where are you currently located? :").lower() Current_Destination = input("Where are you planning to travel? :").lower() def Find_Lowest_Buy_Olisar(): print("Finding lowest buy price for OLISAR...") print(*Sort_Buying_Olisar[:1], sep='\n') def Find_Highest_Sell_Olisar(): print("Finding highest sell price for OLISAR...") print(*Sort_Selling_Olisar[:1], sep='\n') def Find_Lowest_Buy_Levski(): print("Finding lowest buy price for LEVSKI...") print(*Sort_Buying_Levski[:1], sep='\n') def Find_Highest_Sell_Levski(): print("Finding highest sell price for LEVSKI...") print(*Sort_Selling_Levski[:1], sep='\n') Buying_Olisar = [('Medical Supply', 17.01), ('Waste', 0.005),] Sort_Buying_Olisar = sorted_by_second = sorted(Buying_Olisar, key=lambda tup: tup[1]) Selling_Olisar = [('Agricium', 25.60), ('Aluminum', 1.25), ('Beryl', 4.26), ('Chlorine', 1.57), ('Corundum', 2.53), ('Diamond', 6.90), ('Distilled Spirits', 4.95), ('Fluorine', 2.80), ('Gold', 6.07), ('Hydrogen', 1.02), ('Iodine', 0.41), ('Laranite', 28.91), ('Processed Food', 1.39), ('Quartz', 1.44), ('Scrap', 1.67), ('Stims', 3.40), ('Titanium', 8.27), ('Tungsten', 3.90),] Sort_Selling_Olisar = sorted(Selling_Olisar, key=lambda tup:(-tup[1], tup[0])) Buying_Levski = [('Agricultural Supply', 1.11), ('Aluminum', 1.20), ('Hydrogen', 0.98), ('Iodine', 0.38), ('Quartz', 1.37), ('Waste', 0.005),] Sort_Buying_Levski = sorted_by_second = sorted(Buying_Levski, key=lambda tup: tup[1]) Selling_Levski = [('Agricium', 25.60), ('Altruciatoxine', 11.63), ('Beryl', 4.25), ('Chlorine', 1.56), ('Corundum', 2.53), ('Diamond', 6.07), ('Distilled Spirits', 4.95), ('Fluorine', 2.80), ('Gold', 6.07), ('Laranite', 28.25), ('Medical Supply', 18.00), ('Processed Food', 1.38), ('Scrap', 1.68), ('Stims', 3.40), ('Titanium', 8.27), ('Tungsten', 3.90), ('Widdow', 24.00),] Sort_Selling_Levski = sorted(Selling_Levski, key=lambda tup:(-tup[1], tup[0])) if Current_Location == "olisar": Find_Lowest_Buy_Olisar() elif Current_Location == "levski": Find_Lowest_Buy_Levski() else: print("Unknown location please try again.") if Current_Destination == "olisar": Find_Highest_Sell_Olisar() elif Current_Destination == "levski": Find_Highest_Sell_Levski() else: print("Unknown location please try again.") Any input would be hugely appreciated. Kind regards, Harry O'Neill _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From srinivasan.rns at gmail.com Sun Nov 18 09:59:11 2018 From: srinivasan.rns at gmail.com (srinivasan) Date: Sun, 18 Nov 2018 20:29:11 +0530 Subject: [Tutor] Issue in parsing the string output from the command using "subprocess" Message-ID: Dear Python Experts Team, As am newbie to python and learning python, working on embedded linux platform, my intention is to delete all the SSID's before connecting my Wi-Fi module to specific SSID., I am trying to parse command output using the "subprocess" with wrapper "execute_cmd_output_string" written on it described as below, using the nmcli commands "*nmcli -t -f TYPE,UUID con" and "**"nmcli connection delete uuid ".* Could you please help me, what could be the bug in the below method "*def wifi_disconnect_reconnect(self, ssid, pw):" *using the method "execute_cmd_output_string" (in-turn uses "subprocess") which is failing to give correct output of., UUID's for *"nmcli connection delete uuid "* ? But which works fine with "commands.getstatusoutput" (res = commands.getstatusoutput("nmcli -t -f TYPE,UUID con")), but I dont want to include "commands.getstatusoutput" which costs me for including one more python module in my rootfs Typical output for "nmcli -t -f TYPE,UUID con" ~$ nmcli -t -f TYPE,UUID con 802-11-wireless:3f5011d4-5681-4fed-8dea-95dee790e9e2 802-11-wireless:bfb93be0-740d-426e-b215-0fdc2f652877 802-11-wireless:69b60cf4-65aa-442b-a54c-fb82905adb0d 802-11-wireless:dc2a15ec-d3da-491e-9c8f-cb054f375837 802-11-wireless:5164e7f2-4489-462e-b093-76bc51bf1303 802-11-wireless:d462e8c4-fac7-42f2-8f9a-6846f52d4e8c 802-11-wireless:e5020744-5c9c-453c-92ec-7a854fc893e6 802-11-wireless:eed3358e-8635-471d-b7e9-5c2973a05128 802-11-wireless:ceea6707-a929-4941-9047-a75e061914b6 802-11-wireless:dd6b9c83-db7b-42b9-99c0-14a04f6f35f5 802-11-wireless:9f764fff-6288-49c4-9412-902e89230136 802-11-wireless:72c627cd-77a3-4d16-bb2c-058040d8e4fc ~$ *Python Code Snipeet:* *------------------------------* * def wifi_disconnect_reconnect(self, ssid, pw):* * """* * Connect to Access point using SSID and PW.* * :param ssid: SSID of the ACCESS POINT.* * :param pw: password for connecting to the access point.* * :return: command output as True or False.* * """* * cmd = "nmcli -t -f TYPE,UUID con"* * res = self._helper.execute_cmd_output_string(cmd)* * print(res)* * lines = res[1].split('\n') ------------------------> I suspect the issue might be here* * print(lines)* * for line in lines:* * parts = line.split(":")* * print(parts)* * print(parts[1])* * if (parts[0] == "802-11-wireless"):* * print("nmcli connection delete uuid "+ parts[1])* * os.system("nmcli connection delete uuid "+ parts[1])* * cmd = "nmcli device wifi connect '%s' password %s" % (ssid, pw)* * exit_code = self._helper.execute_cmd_return_code(cmd)* * return True if exit_code == 0 else False* def execute_cmd_output_string(self, cmd, enable_shell=False): """ Execute a command and return its output as a string. :param cmd: abs path of the command with arguments :param enable_shell : force the cmd to be run as shell script :return: a string. """ try: result = subprocess.check_output(split(cmd), stderr=subprocess.STDOUT, shell=enable_shell) except subprocess.CalledProcessError as e: s = """While executing '{}' something went wrong. Return code == '{}' Return output:\n'{}' """.format(cmd, e.returncode, e.output, shell=enable_shell) raise AssertionError(s) return result.strip().decode("utf-8") if __name__ == "__main__": m = wifi() print("disconnect and reconnect") print(m.wifi_disconnect_reconnect("NaWiFi", "abcds")) *Errors:* ----------- Traceback (most recent call last): 802-11-wireless:3f5011d4-5681-4fed-8dea-95dee790e9e2 802-11-wireless:dc2a15ec-d3da-491e-9c8f-cb054f375837 802-11-wireless:5164e7f2-4489-462e-b093-76bc51bf1303 File "/home/srinivasan/Downloads/qa_wifi_nov15_after_incorporating_thilo_comments_zip/qa/test_library/wifi.py", line 153, in 802-11-wireless:d462e8c4-fac7-42f2-8f9a-6846f52d4e8c print(m.wifi_connect("NI WiFi", "T.f.o.s.1996!")) 802-11-wireless:e5020744-5c9c-453c-92ec-7a854fc893e6 802-11-wireless:eed3358e-8635-471d-b7e9-5c2973a05128 File "/home/srinivasan/Downloads/qa_wifi_nov15_after_incorporating_thilo_comments_zip/qa/test_library/wifi.py", line 77, in wifi_connect 802-11-wireless:ceea6707-a929-4941-9047-a75e061914b6 print(parts[1]) 802-11-wireless:dd6b9c83-db7b-42b9-99c0-14a04f6f35f5 *IndexError: list index out of range* 802-11-wireless:9f764fff-6288-49c4-9412-902e89230136 802-11-wireless:72c627cd-77a3-4d16-bb2c-058040d8e4fc [u'0'] [u'0'] Process finished with exit code 1 But when I execute the below the command using the python module "commands". I am able to successfully parse the multi line string output and extarct the UUID's from the command "nmcli -t -f TYPE,UUID con" and pass it to *"nmcli connection delete uuid " for* deleting all the SSID's before connecting Wi-Fi to specific SSID. * def wifi_disconnect_reconnect(self, ssid, pw):* * """* * Connect to Access point using SSID and PW.* * :param ssid: SSID of the ACCESS POINT.* * :param pw: password for connecting to the access point.* * :return: command output as True or False.* * """* * res = commands.getstatusoutput("nmcli -t -f TYPE,UUID con")* * lines = res[1].split('\n')* * for line in lines:* * parts = line.split(":")* * if (parts[0] == "802-11-wireless"):* * os.system("nmcli connection delete uuid "+ parts[1])* Kindly do the needful, as I am wandering for any clues what's going wrong with error and the highlighted code snippet... Many Thanks in advance From __peter__ at web.de Sun Nov 18 13:10:36 2018 From: __peter__ at web.de (Peter Otten) Date: Sun, 18 Nov 2018 19:10:36 +0100 Subject: [Tutor] Finding the largest gap in tuple between two lists. References: <4922c43c-9903-175b-c089-056970874f64@wichmann.us> Message-ID: Mats Wichmann wrote: > 1. From an endpoint pair - say Buy/Levski and Sell/Olisar, you need the > common items. No point in looking further at items that can't both be > bought at the source and sold at the destination. Fortunately, Python > sets are really good at this, but first you have to extract only the > item names from your dictionary, since that comparison is based only on > good names. Something like this: > > def common(curr, other): > return set(curr.keys()).intersection(set(other.keys())) As dict.keys() are very similar to a set you can also write curr.keys() & other.keys() or curr.keys() & other # if you don't care about symmetry ;) > So one way to tackle this might be to build a new dictionary containing > the items and their price difference. Given our previous idea of having > a set which contains the common items: > > prices = {k: Selling_Olisar[k] - Buying_Levski[k] for k in possibles} While a dict is almost always a good idea there is no need to build one if you're only interested in one entry. Instead feed the pairs directly to max(): selling = dict(Selling_Olisar) buying = dict(Buying_Levski) possibles = selling.keys() & buying if possibles: profit, good = max( (selling[k] - buying[k], k) for k in possibles ) if profit > 0: print("Best good", good, "wins", profit, "per unit") else: print("Found nothing profitable.") else: print("Found nothing that can be sold at your destination") From bgailer at gmail.com Sun Nov 18 13:19:26 2018 From: bgailer at gmail.com (Bob Gailer) Date: Sun, 18 Nov 2018 13:19:26 -0500 Subject: [Tutor] how to print lines which contain matching words or strings In-Reply-To: References: Message-ID: On Nov 18, 2018 12:14 PM, "Asad" wrote: > > Hi All , > > I have a set of words and strings : > > like : > > p = [A ,"B is good" ,123456 , "C "] > > I have a file in which I need to print only the lines which matches the > pattern in p > > thanks, you are welcome, but I'm not sure what you're thanking us for. I don't see any kind of request in your email. There are various resources on how to ask effective questions. You might try Googling that topic. I have more that I will say later. Bob Gailer From bgailer at gmail.com Sun Nov 18 13:41:29 2018 From: bgailer at gmail.com (Bob Gailer) Date: Sun, 18 Nov 2018 13:41:29 -0500 Subject: [Tutor] how to print lines which contain matching words or strings In-Reply-To: References: Message-ID: On Nov 18, 2018 1:19 PM, "Bob Gailer" wrote: > > On Nov 18, 2018 12:14 PM, "Asad" wrote: > > > > Hi All , > > > > I have a set of words and strings : > > > > like : > > > > p = [A ,"B is good" ,123456 , "C "] > > > > I have a file in which I need to print only the lines which matches the > > pattern in p > > > > thanks, Terminology is very important. You say set then you show us a list. You say words and strings, but your list contains a python identifier and a number in addition to Strings. You say pattern but don't Define what you mean by pattern. I could not begin to guess what your definition of pattern is. We also do not know what the term word means to you. You may say that is obvious but it is not. So tell us your meaning of word. To make the question meaningful I would suggest you show us sample of the file indicating lines that meet the pattern and lines that don't. Please realize that we are not here to write code for you, rather we like to help you when we see you put in some effort. So I suggest you write a Python program that attempts to do what you want, then tell us where the program isn't doing what you want or where you are stuck will try to help. Please respond in some way so that we know you have heard us. From leslie.ingramdrake at gmail.com Sun Nov 18 15:57:55 2018 From: leslie.ingramdrake at gmail.com (Les Drake) Date: Sun, 18 Nov 2018 12:57:55 -0800 Subject: [Tutor] please add gillidrake@gmail.com Message-ID: From bgailer at gmail.com Sun Nov 18 18:50:48 2018 From: bgailer at gmail.com (bob gailer) Date: Sun, 18 Nov 2018 18:50:48 -0500 Subject: [Tutor] seeking beginners tutorial for async Message-ID: I have yet to find a tutorial that helps me understand and apply async! The ones I have found are either incomplete, or they wrap some other service, or they are immediately so complex that I have no hope of understanding them. I did find a useful javascript tutorial at https://javascript.info/promise-basics, but trying to map it to python is very frustrating. The python docs also do not help. Can you point me to any resources that are actually useful to a beginner? Bob Gailer guru of many languages including Python ( up till now!) From mats at wichmann.us Mon Nov 19 12:05:35 2018 From: mats at wichmann.us (Mats Wichmann) Date: Mon, 19 Nov 2018 10:05:35 -0700 Subject: [Tutor] seeking beginners tutorial for async In-Reply-To: References: Message-ID: On 11/18/18 4:50 PM, bob gailer wrote: > I have yet to find a tutorial that helps me understand and apply async! > > The ones I have found are either incomplete, or they wrap some other > service, or they are immediately so complex that I have no hope of > understanding them. > > I did find a useful javascript tutorial at > https://javascript.info/promise-basics, but trying to map it to python > is very frustrating. > > The python docs also do not help. Can you be more specific what you're looking for? There are a lot of aspects to async programming in Python, and there's been a lot of language-support changes in recent versions. This just means there will be a lot of differing efforts to explain things based on when the tutorial was written. Plus async, while the way we work as humans, is different to the way we tend to think of programming (yes, I'm speaking for myself here) unless you're a GUI programmer, where event loops are old hat. "Wraps some other service" - several tutorials I've glanced at do a simple webserver, because that's an example where we understand why synchronous doesn't work, and the basic concepts are pretty simple (good webservers are _just_ a little harder, of course). From bgailer at gmail.com Mon Nov 19 16:13:58 2018 From: bgailer at gmail.com (Bob Gailer) Date: Mon, 19 Nov 2018 16:13:58 -0500 Subject: [Tutor] seeking beginners tutorial for async In-Reply-To: References: Message-ID: > Can you be more specific what you're looking for? For starters a minimal executable program that uses the async keyword. On the JavaScript side this is trivial and easily understood. I did find in the python documentation a hello world program that uses async IO. It helped me understand how to build an event Loop , start soon, start later stop Loop, run forever and run until complete. That was very helpful. But it did not introduce async. I'd like to see the trivial program built up step-by-step adding one new feature at a time so that I can understand exactly what that feature does. I am talking about python 3.6 and 3.7. Thank you for asking for the clarification, I hope this helps. From avigross at verizon.net Sun Nov 18 20:13:41 2018 From: avigross at verizon.net (Avi Gross) Date: Sun, 18 Nov 2018 20:13:41 -0500 Subject: [Tutor] how to print lines which contain matching words or strings In-Reply-To: References: Message-ID: <005201d47fa5$1bf96e90$53ec4bb0$@verizon.net> Asad, As others have already pointed out, your request is far from clear. Ignoring the strange use of words, and trying to get the gist of the request, would this be close to what you wanted to say? You have a file you want to open and process a line at a time. You want to select just lines that meet your criteria and print them while ignoring the rest. So what are the criteria? It sounds like you have a list of criteria that might be called patterns. Your example shows a heterogenous collection: [A ,"B is good" ,123456 , "C "] A is either an error or the name of a variable that contains something. We might want a hint as searching for any old object makes no sense. The second and fourth are exact strings. No special regular expression pattern. Searching for them is trivial using normal string functionality. Assuming they can be anywhere in a line: >>> line1 = "Vitamin B is good for you and so is vitamin C" >>> line2 = "Currently nonsensical." >>> line3 = "" >>> "B is good" in line1 True >>> "B is good" in line2 False >>> "B is good" in line3 False >>> "C" in line1 True >>> "C" in line2 True >>> "C" in line2 True To test everything in a list, you need code like for each line: for whatever in [A ,"B is good" ,123456 , "C "] If whatever in line: print(line) Actually, the above could print multiple copies so you should break out after any one matches. 123456 is a challenge to match. You could search for str(whatever) perhaps. Enough. First explain what you really want. If you want to do a more general search using regular expressions, then the list of things to search for would be all the string in RE format. You could search multiple times or use the OR operator carefully inside one regular expression. You have not stated any need to tell what was matched or where it is the line so that would be yet another story. -----Original Message----- From: Tutor On Behalf Of Asad Sent: Sunday, November 18, 2018 10:19 AM To: tutor at python.org Subject: [Tutor] how to print lines which contain matching words or strings Hi All , I have a set of words and strings : like : p = [A ,"B is good" ,123456 , "C "] I have a file in which I need to print only the lines which matches the pattern in p thanks, -- Asad Hasan +91 9582111698 _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From dimitarxivanov at gmail.com Mon Nov 19 18:52:49 2018 From: dimitarxivanov at gmail.com (Dimitar Ivanov) Date: Mon, 19 Nov 2018 23:52:49 +0000 Subject: [Tutor] [Python 3] Threads status, join() and Semaphore queue Message-ID: Hello everyone, I'm having a hard time getting my head around threads so I was hoping someone who has better understanding of their underlying functionality could lend me a helping hand, in particular how threads work with each other when using thread.join() and Semaphore set with maximum value. I'll try to keep it as clear and concise as possible, but please don't hesitate to ask if anything about my approach is unclear or, frankly, awful. I'm writing a script that performs a couple of I/O operations and CLI commands for each element in a list of IDs. The whole process takes a while and may vary based on the ID, hence the threading approach sounded like the best fit since next ID can start once space has freed up. I'm parsing an extract of my code below and will explain what I can't properly understand underneath. Note: Please ignore any syntax typos, I'm rewriting the code to make it suitable for here. file1.py --------- ids = [] threadsPool = [] for id in ids: thread = threading.Thread(target=file2.runStuff, name=str(id), args=(id, )) threadsPool.append(thread) for thread in threadsPool: thread.start() for thread in threadsPool: print(thread.enumerate()) print("Queuing thread" + str(thread)) thread.join() file2.py ---------- queue = threading.Semaphore(2) def runStuff(id): queue.acquire() print("Lock acquired for " + str(id)) file3.doMoreStuff() file4.evenMoreStuff() queue.release() Onto my confusion - as long as I don't try to print information about the thread that's being queued or the total amount of threads using .enumerate(), the script is working absolutely flawlessly, each thread that doesn't have a lock is waiting until it acquires it and then moves on. I decided it'd be nice to be able to provide more information about which thread starts next and how many threads are active right now (each can take a different amount of time), however, when I tried to do that, my log was showing me some pretty funky output which at first made me believe I've messed up all my threads, example: << 2018-11-19 15:01:38,094 file2 [ID09] INFO - Lock acquired for ID09 <---- this is from file2.py ------ some time later and other logs in here --------- [<_MainThread(MainThread, started 140431033562880)>, ] <---- output from thread.enumerate(), file1.py << 2018-11-19 15:01:38,103 file1 [MainThread] DEBUG - Queuing thread - <---- output from print() right after thread.enumerate() After some head scratching, I believe I've finally tracked down the reason for my confusion: The .start() loop starts the threads and the first 2 acquire a lock immediately and start running, later on the .join() queue puts the rest in waiting for lock, that's fine, what I didn't realize, of course, is that the .join() loop goes through threads that have already been instantly kicked off by the .start() loop (the first 2 threads since Semaphore allows 2 locks) and then my print in that loop is telling me that those threads are being queued, except they aren't since they are already running, it's just my text is telling me that, since I wasn't smart enough to realize what's about to happen, as seen below: << 2018-11-19 15:01:33,094 file1.py [MainThread] DEBUG - Queuing thread - <--- makes it clear the thread has already even finished Which finally gets me to my cry for help - I know I can't modify the threadsPool list to remove the threads already created on the fly, so I can have only the ones pending to be queued in the 2nd loop, but for the life of me I can't think of a proper way to try and extract some information about what threads are still going (or rather, have finished since thread.enumerate() shows both running and queued). I have the feeling I'm using a very wrong approach in trying to extract that information in the .join() loop, since it only goes back to it once a thread has finished, but at the same time it feels like the perfect timing. I feel like (and I might be very wrong) if I could only have the threads that are actually being queued in there (getting rid of the ones started initially), my print(thread) will be the absolute sufficient amount of information I want to display. And just in case you are wondering why I have my threads starting in file1.py and my Semaphore queue in file2.py, it's because I wanted to split the runStuff(id) function in a separate module due to its length. I don't know if it's a good way to do it, but thankfully the Python interpreter is smart enough to see through my ignorance. I'm also really sorry for the wall of text, I just hope the information provided is enough to clear up the situation I'm in and what I'm struggling with. Thank you in advance and with kindest regards, Dimitar From cs at cskk.id.au Tue Nov 20 02:22:23 2018 From: cs at cskk.id.au (Cameron Simpson) Date: Tue, 20 Nov 2018 18:22:23 +1100 Subject: [Tutor] [Python 3] Threads status, join() and Semaphore queue In-Reply-To: References: Message-ID: <20181120072223.GA94457@cskk.homeip.net> On 19Nov2018 23:52, Dimitar Ivanov wrote: >I'm having a hard time getting my head around threads so I was hoping >someone who has better understanding of their underlying functionality >could lend me a helping hand, in particular how threads work with each >other when using thread.join() and Semaphore set with maximum value. I'll >try to keep it as clear and concise as possible, but please don't hesitate >to ask if anything about my approach is unclear or, frankly, awful. > >I'm writing a script that performs a couple of I/O operations and CLI >commands for each element in a list of IDs. The whole process takes a while >and may vary based on the ID, hence the threading approach sounded like the >best fit since next ID can start once space has freed up. I'm parsing an >extract of my code below and will explain what I can't properly understand >underneath. I'm just going to scatter a few random remarks about your code in here with your listing before addressing your other queries lower down... >file1.py >--------- >ids = [] >threadsPool = [] >for id in ids: The name "id" is unfortunate as it conflicts with the id() builtin function. Maybe "element_id"? Wordier, but also more clear. > thread = threading.Thread(target=file2.runStuff, name=str(id), args=(id, >)) > threadsPool.append(thread) >for thread in threadsPool: > thread.start() You could start each thread right after creating it if you wished. >for thread in threadsPool: > print(thread.enumerate()) "enumerate" is a function from the threading module, not a method of a Thread. So try: print(threading.enumerate()) Frankly I'm surprised that "thread.enumerate" works at all. > print("Queuing thread" + str(thread)) > thread.join() > >file2.py >---------- >queue = threading.Semaphore(2) I'd be disinclined to call this a "queue", which usually implies a FIFO list of some variety: put things onto it, and pull things off it, usually first in first off. Maybe just "sem" or "thread_capacity" or something? >def runStuff(id): > queue.acquire() > print("Lock acquired for " + str(id)) > file3.doMoreStuff() > file4.evenMoreStuff() > queue.release() > >Onto my confusion - as long as I don't try to print information about the >thread that's being queued or the total amount of threads using >.enumerate(), the script is working absolutely flawlessly, each thread that >doesn't have a lock is waiting until it acquires it and then moves on. I >decided it'd be nice to be able to provide more information about which >thread starts next and how many threads are active right now (each can take >a different amount of time), however, when I tried to do that, my log was >showing me some pretty funky output which at first made me believe I've >messed up all my threads, example: > > ><< 2018-11-19 15:01:38,094 file2 [ID09] INFO - Lock acquired for >ID09 <---- this is from file2.py >------ some time later and other logs in here --------- >[<_MainThread(MainThread, started 140431033562880)>, 140430614177536)>] <---- output from thread.enumerate(), file1.py ><< 2018-11-19 15:01:38,103 file1 [MainThread] DEBUG - Queuing thread - > <---- output from print() right >after thread.enumerate() > >After some head scratching, I believe I've finally tracked down the reason >for my confusion: > >The .start() loop starts the threads and the first 2 acquire a lock >immediately and start running, later on the .join() queue puts the rest in >waiting for lock, that's fine, what I didn't realize, of course, is that >the .join() loop goes through threads that have already been instantly >kicked off by the .start() loop (the first 2 threads since Semaphore allows >2 locks) and then my print in that loop is telling me that those threads >are being queued, except they aren't since they are already running, it's >just my text is telling me that, since I wasn't smart enough to realize >what's about to happen, as seen below: > ><< 2018-11-19 15:01:33,094 file1.py [MainThread] DEBUG - Queuing thread - > <--- makes it clear the thread has >already even finished Yes. The .join() has NO EFFECT on the Thread itself: it doesn't start it or stop it. It just waits for the Thread to complete. So yes, your log message is misleading. >Which finally gets me to my cry for help - I know I can't modify the >threadsPool list to remove the threads already created on the fly, so I can >have only the ones pending to be queued in the 2nd loop, but for the life >of me I can't think of a proper way to try and extract some information >about what threads are still going (or rather, have finished since >thread.enumerate() shows both running and queued). Well, what you'd probably _like_ is a way to be told about each Thread as it completes, and report them then. Which you can do using a Queue, getting each Thread to report its completion to the Queue as it happens. Untested example: from queue import Queue q = Queue() threadsPool = [] for id in ids: thread = threading.Thread(target=file2.runStuff, name=str(id), args=(id, q)) threadsPool.append(thread) and modify runStuff thus: def runStuff(id, q): sem.aquire() ... sem.release() q.put(id) After the threads are started, collect completed ids: for count in range(len(ids)): id = q.get() print("completed work on id %r" % (id,)) You'll notice no .join() there. Getting the id off the queue "q" implies that the Thread has completed. >I have the feeling I'm using a very wrong approach in trying to extract >that information in the .join() loop, since it only goes back to it once a >thread has finished, but at the same time it feels like the perfect timing. You're collecting specific threads. If other threads complete earlier than that specific thread, they don't get reported immediately. You're reporting threads in the order you made them, not in the order they complete. Using a Queue and not worrying about the threads themselves lets you gets ids as they're done, in whatever order. >And just in case you are wondering why I have my threads starting in >file1.py and my Semaphore queue in file2.py, it's because I wanted to split >the runStuff(id) function in a separate module due to its length. I don't >know if it's a good way to do it, but thankfully the Python interpreter is >smart enough to see through my ignorance. I usually split modules based on function, not size. Put related things in the same module. Often in classes, but let us not go there yet. If you want to get a little funky, separate the runStuff code which works on the id from the control code (the semaphore use). You could then run the queue collection in its own thread. Have the main control code also manage the semaphore: q = Queue() threadsPool = [] for id in ids: thread = threading.Thread(target=file2.runStuff, name=str(id), args=(id, q)) threadsPool.append(thread) collector = Thread(target=collect, args=(q, ids)) collector.start() for thread in threadPool: sem.acquire() thread.start() # wait for collection to complete collector.join() def collect(q, ids): for count in range(len(ids)): id = q.get() sem.release() so that you acquire the semaphore before starting each thread, and release the semaphore as threads complete and report their id values. Because these things need to happen in parallel (acquire, start versus q.get, release) you run the collector in its own thread. Does this clear anything up? Cheers, Cameron Simpson From asad.hasan2004 at gmail.com Mon Nov 19 22:15:08 2018 From: asad.hasan2004 at gmail.com (Asad) Date: Tue, 20 Nov 2018 08:45:08 +0530 Subject: [Tutor] how to print lines which contain matching words or strings In-Reply-To: References: Message-ID: Hi Avi Gross /All, Thanks for the reply. Yes you are correct , I would like to to open a file and process a line at a time from the file and want to select just lines that meet my criteria and print them while ignoring the rest. i have created the following code : import re import os f3 = open(r'file1.txt',r) f = f3.readlines() d = [] for linenum in range(len(f)): if re.search("ERR-1" ,f[linenum]) print f[linenum] break if re.search("\d\d\d\d\d\d",f[linenum]) --- > seach for a patch number length of six digits for example 123456 print f[line] break if re.search("Good Morning",f[linenum]) print f[line] break if re.search("Breakfast",f[linenum]) print f[line] break ... further 5 more hetrogeneus if conditions I have ======================================================================= This is beginners approach to print the lines which match the if conditions . How should I make it better may be create a dictionary of search items or a list and then iterate over the lines in a file to print the lines matching the condition. Please advice , Thanks, Previous email : ====================================================================== Asad, As others have already pointed out, your request is far from clear. Ignoring the strange use of words, and trying to get the gist of the request, would this be close to what you wanted to say? You have a file you want to open and process a line at a time. You want to select just lines that meet your criteria and print them while ignoring the rest. So what are the criteria? It sounds like you have a list of criteria that might be called patterns. Your example shows a heterogenous collection: [A ,"B is good" ,123456 , "C "] A is either an error or the name of a variable that contains something. We might want a hint as searching for any old object makes no sense. The second and fourth are exact strings. No special regular expression pattern. Searching for them is trivial using normal string functionality. Assuming they can be anywhere in a line: >>> line1 = "Vitamin B is good for you and so is vitamin C" >>> line2 = "Currently nonsensical." >>> line3 = "" >>> "B is good" in line1 True >>> "B is good" in line2 False >>> "B is good" in line3 False >>> "C" in line1 True >>> "C" in line2 True >>> "C" in line2 True To test everything in a list, you need code like for each line: for whatever in [A ,"B is good" ,123456 , "C "] If whatever in line: print(line) Actually, the above could print multiple copies so you should break out after any one matches. 123456 is a challenge to match. You could search for str(whatever) perhaps. Enough. First explain what you really want. If you want to do a more general search using regular expressions, then the list of things to search for would be all the string in RE format. You could search multiple times or use the OR operator carefully inside one regular expression. You have not stated any need to tell what was matched or where it is the line so that would be yet another story. -----Original Message----- From: Tutor On Behalf Of Asad Sent: Sunday, November 18, 2018 10:19 AM To: tutor at python.org Subject: [Tutor] how to print lines which contain matching words or strings Hi All , I have a set of words and strings : like : p = [A ,"B is good" ,123456 , "C "] I have a file in which I need to print only the lines which matches the pattern in p thanks, On Tue, Nov 20, 2018 at 6:12 AM 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. Re: seeking beginners tutorial for async (Mats Wichmann) > 2. Re: seeking beginners tutorial for async (Bob Gailer) > 3. Re: how to print lines which contain matching words or > strings (Avi Gross) > 4. [Python 3] Threads status, join() and Semaphore queue > (Dimitar Ivanov) > > > > ---------- Forwarded message ---------- > From: Mats Wichmann > To: tutor at python.org > Cc: > Bcc: > Date: Mon, 19 Nov 2018 10:05:35 -0700 > Subject: Re: [Tutor] seeking beginners tutorial for async > On 11/18/18 4:50 PM, bob gailer wrote: > > I have yet to find a tutorial that helps me understand and apply async! > > > > The ones I have found are either incomplete, or they wrap some other > > service, or they are immediately so complex that I have no hope of > > understanding them. > > > > I did find a useful javascript tutorial at > > https://javascript.info/promise-basics, but trying to map it to python > > is very frustrating. > > > > The python docs also do not help. > > Can you be more specific what you're looking for? > > There are a lot of aspects to async programming in Python, and there's > been a lot of language-support changes in recent versions. This just > means there will be a lot of differing efforts to explain things based > on when the tutorial was written. Plus async, while the way we work as > humans, is different to the way we tend to think of programming (yes, > I'm speaking for myself here) unless you're a GUI programmer, where > event loops are old hat. "Wraps some other service" - several tutorials > I've glanced at do a simple webserver, because that's an example where > we understand why synchronous doesn't work, and the basic concepts are > pretty simple (good webservers are _just_ a little harder, of course). > > > > > > ---------- Forwarded message ---------- > From: Bob Gailer > To: Mats Wichmann > Cc: tutor at python.org > Bcc: > Date: Mon, 19 Nov 2018 16:13:58 -0500 > Subject: Re: [Tutor] seeking beginners tutorial for async > > Can you be more specific what you're looking for? > > For starters a minimal executable program that uses the async keyword. > > On the JavaScript side this is trivial and easily understood. > > I did find in the python documentation a hello world program that uses > async IO. It helped me understand how to build an event Loop , start soon, > start later stop Loop, run forever and run until complete. That was very > helpful. But it did not introduce async. > > I'd like to see the trivial program built up step-by-step adding one new > feature at a time so that I can understand exactly what that feature does. > > I am talking about python 3.6 and 3.7. > > Thank you for asking for the clarification, I hope this helps. > > > > > ---------- Forwarded message ---------- > From: Avi Gross > To: > Cc: > Bcc: > Date: Sun, 18 Nov 2018 20:13:41 -0500 > Subject: Re: [Tutor] how to print lines which contain matching words or > strings > Asad, > > As others have already pointed out, your request is far from clear. > > Ignoring the strange use of words, and trying to get the gist of the > request, would this be close to what you wanted to say? > > You have a file you want to open and process a line at a time. You want to > select just lines that meet your criteria and print them while ignoring the > rest. > > So what are the criteria? It sounds like you have a list of criteria that > might be called patterns. Your example shows a heterogenous collection: > > [A ,"B is good" ,123456 , "C "] > > A is either an error or the name of a variable that contains something. We > might want a hint as searching for any old object makes no sense. > > The second and fourth are exact strings. No special regular expression > pattern. Searching for them is trivial using normal string functionality. > Assuming they can be anywhere in a line: > > >>> line1 = "Vitamin B is good for you and so is vitamin C" > >>> line2 = "Currently nonsensical." > >>> line3 = "" > >>> "B is good" in line1 > True > >>> "B is good" in line2 > False > >>> "B is good" in line3 > False > >>> "C" in line1 > True > >>> "C" in line2 > True > >>> "C" in line2 > True > > To test everything in a list, you need code like for each line: > > for whatever in [A ,"B is good" ,123456 , "C "] > If whatever in line: print(line) > > Actually, the above could print multiple copies so you should break out > after any one matches. > > 123456 is a challenge to match. You could search for str(whatever) perhaps. > > Enough. First explain what you really want. > > If you want to do a more general search using regular expressions, then the > list of things to search for would be all the string in RE format. You > could > search multiple times or use the OR operator carefully inside one regular > expression. You have not stated any need to tell what was matched or where > it is the line so that would be yet another story. > > -----Original Message----- > From: Tutor On Behalf Of > Asad > Sent: Sunday, November 18, 2018 10:19 AM > To: tutor at python.org > Subject: [Tutor] how to print lines which contain matching words or strings > > Hi All , > > I have a set of words and strings : > > like : > > p = [A ,"B is good" ,123456 , "C "] > > I have a file in which I need to print only the lines which matches the > pattern in p > > thanks, > > -- > Asad Hasan > +91 9582111698 > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > > > > > > ---------- Forwarded message ---------- > From: Dimitar Ivanov > To: tutor at python.org > Cc: > Bcc: > Date: Mon, 19 Nov 2018 23:52:49 +0000 > Subject: [Tutor] [Python 3] Threads status, join() and Semaphore queue > Hello everyone, > > I'm having a hard time getting my head around threads so I was hoping > someone who has better understanding of their underlying functionality > could lend me a helping hand, in particular how threads work with each > other when using thread.join() and Semaphore set with maximum value. I'll > try to keep it as clear and concise as possible, but please don't hesitate > to ask if anything about my approach is unclear or, frankly, awful. > > I'm writing a script that performs a couple of I/O operations and CLI > commands for each element in a list of IDs. The whole process takes a while > and may vary based on the ID, hence the threading approach sounded like the > best fit since next ID can start once space has freed up. I'm parsing an > extract of my code below and will explain what I can't properly understand > underneath. > > Note: Please ignore any syntax typos, I'm rewriting the code to make it > suitable for here. > > > file1.py > --------- > ids = [] > threadsPool = [] > for id in ids: > thread = threading.Thread(target=file2.runStuff, name=str(id), args=(id, > )) > threadsPool.append(thread) > for thread in threadsPool: > thread.start() > for thread in threadsPool: > print(thread.enumerate()) > print("Queuing thread" + str(thread)) > thread.join() > > file2.py > ---------- > queue = threading.Semaphore(2) > def runStuff(id): > queue.acquire() > print("Lock acquired for " + str(id)) > file3.doMoreStuff() > file4.evenMoreStuff() > queue.release() > > > Onto my confusion - as long as I don't try to print information about the > thread that's being queued or the total amount of threads using > .enumerate(), the script is working absolutely flawlessly, each thread that > doesn't have a lock is waiting until it acquires it and then moves on. I > decided it'd be nice to be able to provide more information about which > thread starts next and how many threads are active right now (each can take > a different amount of time), however, when I tried to do that, my log was > showing me some pretty funky output which at first made me believe I've > messed up all my threads, example: > > > << 2018-11-19 15:01:38,094 file2 [ID09] INFO - Lock acquired for > ID09 <---- this is from file2.py > ------ some time later and other logs in here --------- > [<_MainThread(MainThread, started 140431033562880)>, 140430614177536)>] <---- output from thread.enumerate(), file1.py > << 2018-11-19 15:01:38,103 file1 [MainThread] DEBUG - Queuing thread - > <---- output from print() right > after thread.enumerate() > > > After some head scratching, I believe I've finally tracked down the reason > for my confusion: > > The .start() loop starts the threads and the first 2 acquire a lock > immediately and start running, later on the .join() queue puts the rest in > waiting for lock, that's fine, what I didn't realize, of course, is that > the .join() loop goes through threads that have already been instantly > kicked off by the .start() loop (the first 2 threads since Semaphore allows > 2 locks) and then my print in that loop is telling me that those threads > are being queued, except they aren't since they are already running, it's > just my text is telling me that, since I wasn't smart enough to realize > what's about to happen, as seen below: > > << 2018-11-19 15:01:33,094 file1.py [MainThread] DEBUG - Queuing thread - > <--- makes it clear the thread has > already even finished > > Which finally gets me to my cry for help - I know I can't modify the > threadsPool list to remove the threads already created on the fly, so I can > have only the ones pending to be queued in the 2nd loop, but for the life > of me I can't think of a proper way to try and extract some information > about what threads are still going (or rather, have finished since > thread.enumerate() shows both running and queued). > > I have the feeling I'm using a very wrong approach in trying to extract > that information in the .join() loop, since it only goes back to it once a > thread has finished, but at the same time it feels like the perfect timing. > I feel like (and I might be very wrong) if I could only have the threads > that are actually being queued in there (getting rid of the ones started > initially), my print(thread) will be the absolute sufficient amount of > information I want to display. > > And just in case you are wondering why I have my threads starting in > file1.py and my Semaphore queue in file2.py, it's because I wanted to split > the runStuff(id) function in a separate module due to its length. I don't > know if it's a good way to do it, but thankfully the Python interpreter is > smart enough to see through my ignorance. > > I'm also really sorry for the wall of text, I just hope the information > provided is enough to clear up the situation I'm in and what I'm struggling > with. > > Thank you in advance and with kindest regards, > Dimitar > > _______________________________________________ > Tutor maillist - Tutor at python.org > https://mail.python.org/mailman/listinfo/tutor > -- Asad Hasan +91 9582111698 From mats at wichmann.us Tue Nov 20 09:18:25 2018 From: mats at wichmann.us (Mats Wichmann) Date: Tue, 20 Nov 2018 07:18:25 -0700 Subject: [Tutor] how to print lines which contain matching words or strings In-Reply-To: References: Message-ID: <501617b3-e278-7a5d-46cc-2205a53279bb@wichmann.us> On 11/19/18 8:15 PM, Asad wrote: > Hi Avi Gross /All, > > Thanks for the reply. Yes you are correct , I would like to to > open a file and process a line at a time from the file and want to select > just lines that meet my criteria and print them while ignoring the rest. i > have created the following code : > > > import re > import os > > f3 = open(r'file1.txt',r) > f = f3.readlines() > d = [] > for linenum in range(len(f)): > if re.search("ERR-1" ,f[linenum]) > print f[linenum] > break > if re.search("\d\d\d\d\d\d",f[linenum]) --- > seach for a patch > number length of six digits for example 123456 > print f[line] > break > if re.search("Good Morning",f[linenum]) > print f[line] > break > if re.search("Breakfast",f[linenum]) > print f[line] > break > ... > further 5 more hetrogeneus if conditions I have > > ======================================================================= > This is beginners approach to print the lines which match the if conditions > . > > How should I make it better may be create a dictionary of search items or a > list and then iterate over the lines in a file to print the lines matching > the condition. We usually suggest using a context manager for file handling, so that cleanup happens automatically when the context is complete: with open('file1.txt', 'r') as f3: # do stuff # when you get here, f3 is closed There's no need to do a counting loop, using the count as an index into an array. That's an idiom from other programing languages; in Python you may as well just loop directly over the list (array)... lists are iterable. for line in f: # search in line Indeed, there's no real need to read all the lines in with readlines, you can just loop directly over the file object - the f3 opened above: for line in f3: # search in line There's no need to use a regular expression search if your pattern is a simple string, you can use the "in" keyword: if "Breakfast" in line: print line Keep your REs for more complex matches. Do those help? From avigross at verizon.net Tue Nov 20 11:25:14 2018 From: avigross at verizon.net (Avi Gross) Date: Tue, 20 Nov 2018 11:25:14 -0500 Subject: [Tutor] how to print lines which contain matching words or strings In-Reply-To: References: Message-ID: <001101d480ed$9e9c39b0$dbd4ad10$@verizon.net> Asad, Thank you for the clarification. I am glad that you stated (albeit at the end) that you wanted a better idea of how to do it than the code you display. I stripped out the earlier parts of the discussion for storage considerations but they can be found in the archives if needed. There are several ways to look at your code. One is to discuss it the general way it is. The other is to discuss how it could be, and there are often many people that champion one style or another. I will work with your style but point out the more compact form many favor first. As has been pointed out, people coming from languages like C, may try to write in a similar style even in a language that supports other ays. So if your goal is what you say, then all you need is doable in very few lines of code. The basic idea is iteration. You can use it several times. You have a file. In Python (at least recent versions) the opened file is an iterator. So the outline of your program can look like: for line in open(...): process_line(line, re_list) I snuck in a function called process_line that you need to define or replace by code. I also snuck in a list of regular expressions you would create, perhaps above the loop. I will not give you a tutorial on regular expressions. Suffice it to say they tend to be strings. You do not search for 123 but rather for "123" or str(123) or anything that becomes a single string. Here is one of many ways to learn how to make proper expressions and use them: https://docs.python.org/2/howto/regex.html Since you want to repeatedly use the same expressions for each line, you may want to compile each one and have a list of the compiled versions. If you have a list like this: re_str = [ "ABC", "123", "(and)|(AND)", "[_A-Za-z][_A-Za-z0-9]*" ] you can use a loop such as list comprehension like this: re_comp = [ re.compile(pattern) for pattern in re_str ] So in the function above, or in-line, you can loop over the expressions for each line sort of like this: for pat in re_comp: <> print line and break out. The latter line is not actual Python code but a place you use whatever matching function you want. The variable "pat" holds each compiled pattern one at a time so pat.search(line) or pat.match(line) and so on can be used depending on your need. Since you actually do not care what matches you have lots of leeway. There are many other ways but this one is quite simple and broad and adjust to any number or type of pattern if properly used. Back to your code. No need to use a raw string on a normal filename but harmless. f3 = open(r'file1.txt',r) Why file1 is read into variable f3 remains a harmless mystery. But then I see you using another style by reading the entire file into memory f = f3.readlines() d = [] Nothing wrong with that, although the example above shows how to process one line at a time. So far, you seem to want to make a list of lines that match and not print till later. for linenum in range(len(f)): OK, that is valid Python but far from optimal. Yes, you can loop over indices of the list f using the length. But since such a list of strings is an iterable, you could have done something similar to the method I showed above: for line in f: But going with what you have, you decided to create a series of individual if statements. if re.search("ERR-1" ,f[linenum]) print f[linenum] break if re.search("\d\d\d\d\d\d",f[linenum]) --- > seach for a patch number length of six digits for example 123456 print f[line] break and so on. Ignoring the comment in the code that makes it fail, this is presumably valid but not Pythonic. One consideration is that the if statement can look like this: If (condition1 and (condition2 or condition3)) ... So you could do a list of "or" statements in one if. In pseudocode: If (matches(line, re1) or matches(line, re2) ... or ...) The above, if properly written with N parts will return true as soon as the first condition matches. You can then print or copy for later printing. No break needed. But note each of the pseudo-code matches() must return as pythonic True or be False. The extended form of "if" is another way: If condition1 : Something elif condition2: Something else elif condition3: Have fun else: whatever I note you made an empty list with d = [] But you never used it. My initial guess was that you wanted to add lines to the list. Since you printed instead, is it needed. You asked about using dictionaries. Yes, you can store just about anything in dictionaries and iterate over them in the random order. But a list of strings or compiled regular expressions would work fine for this application. Having said that, you can make a dictionary but what would be the key? The key has to be something immutable and is there any obvious advantage? If you care about efficiency, some final notes. The order of the searches might matter. The most commonly found ones should be tested first and the rarest ones last. Since the algorithm does not want to print a line multiple times, it will stop evaluating when it finds what it wants. Regular expressions are powerful but also not cheap. If many or all the things you are searching for are simple text, consider using normal string functions as was discussed earlier. One approach would be to have two modules and something like this: If string_search(...): Print it elif re_search(..): Print it Else Skip it And be warned you may make spurious matches. If you search for "and" you will match sand and ampersand. You need to make the regular expression understand you want a word boundary before and after or whatever your need is. You can do very powerful but expensive things like only matching if you find the same word at least three times on that line, no matter what the word is. You may need to tell it whether a match should be greedy and many other considerations such as ignoring case. Have fun. Avi -----Original Message----- From: Tutor On Behalf Of Asad Sent: Monday, November 19, 2018 10:15 PM To: tutor at python.org Subject: Re: [Tutor] how to print lines which contain matching words or strings Hi Avi Gross /All, Thanks for the reply. Yes you are correct , I would like to to open a file and process a line at a time from the file and want to select just lines that meet my criteria and print them while ignoring the rest. i have created the following code : import re import os f3 = open(r'file1.txt',r) f = f3.readlines() d = [] for linenum in range(len(f)): if re.search("ERR-1" ,f[linenum]) print f[linenum] break if re.search("\d\d\d\d\d\d",f[linenum]) --- > seach for a patch number length of six digits for example 123456 print f[line] break if re.search("Good Morning",f[linenum]) print f[line] break if re.search("Breakfast",f[linenum]) print f[line] break ... further 5 more hetrogeneus if conditions I have ======================================================================= This is beginners approach to print the lines which match the if conditions . How should I make it better may be create a dictionary of search items or a list and then iterate over the lines in a file to print the lines matching the condition. Please advice , Thanks, From avigross at verizon.net Tue Nov 20 13:08:35 2018 From: avigross at verizon.net (Avi Gross) Date: Tue, 20 Nov 2018 13:08:35 -0500 Subject: [Tutor] Pythonic way Message-ID: <005201d480fc$0e615240$2b23f6c0$@verizon.net> This is not a question or reply. Nor is it short. If not interested, feel free to delete. It is an observation based on recent experiences. We have had quite a few messages that pointed out how some people approach solving a problem using subconscious paradigms inherited from their past. This includes people who never programmed and are thinking of how they might do it manually as well as people who are proficient in one or more other computer languages and their first attempt to visualize the solution may lead along paths that are possibly doable in Python but not optimal or even suggested. I recently had to throw together a routine that would extract info from multiple SAS data files and join them together on one key into a large DataFrame (or data.frame or other such names for a tabular object.). Then I needed to write them out to disk either as a CSV or XLSX file for future use. Since I have studied and used (not to mention abused) many programming languages, my first thought was to do this in R. It has lots of the tools needed to do such things including packages (sort of like modules you can import but not exactly) and I have done many data/graphics programs in it. I then redid it in Python after some thought. The pseudocode outline is: * Read in all the files into a set of data.frame objects. * Trim back the variables/columns of some of them as many are not needed. * Join them together on a common index using a full or outer join. * Save the result on disk as a Comma Separated Values file. * Save the result on disk as a named tab in a new style EXCEL file. I determined some of what I might us such as the needed built-in commands, packages and functions I could use for the early parts but ran into an annoyance as some of the files contained duplicate entries. Luckily, the R function reduce (not the same as map/reduce) is like many things in R and takes a list of items and makes it work. Also, by default, it renames duplicates so if you have ALPHA in multiple places, it names them ALPHA.x and ALPHA.x.x and other variations. df_joined <- reduce(df_list, full_join, by = "COMMON") Mind you, when I stripped away many of the columns not needed in some of the files, there were fewer duplicates and a much smaller output file. But I ran into a wall later. Saving into a CSV is trivial. There are multiple packages meant to be used for saving into a XLSX file but they all failed for me. One wanted something in JAVA and another may want PERL and some may want packages I do not have installed. So, rather than bash my head against the wall, Itwisted and used the best XSLX maker there is. I opened the CSV file in EXCEL manually and did a SAVE AS ? Then I went to plan C (no, not the language C or its many extensions like C++) as I am still learning Python and have not used it much. As an exercise, I decided to learn how to do this in Python using tools/modules like numpy and pandas that I have not had much need for as well as additional tools for reading and writing files in other formats. My first attempts gradually worked, after lots of mistakes and looking at manual pages. It followed an eclectic set of paradigms but worked. Not immediately, as I ran into a problem in that the pandas version of a join did not tolerate duplicate column names when used on a list. I could get it to rename the left or right list (adding a suffix a suffix) when used on exactly two DataFrames. So, I needed to take the first df and do a df.join(second, ?) then take that and join the third and so on. I also needed to keep telling it to set the index to the common value for each and every df including the newly joined series. And, due to size, I chose to keep deleting df no longer in use but that would not be garbage collected. I then looked again at how to tighten it up in a more pythonic way. In English (my sixth language since we are talking about languages ? ) I did some things linearly then shifted it to a list method. I used lists of file names and lists of the df made from each file after removing unwanted columns. (NOTE: I use ?column? but depending on language and context I mean variable or field or axis or many other ways to say a group of related information in a tabular structure that crosses rows or instances.) So I was able to do my multi-step join more like this: join_with_list = dflist[1:] current = df1 suffix = 1 for df in join_with_list: current = current.join(df, how='outer', rsuffix='_'+str(suffix)) suffix += 1 current.set_index('ID') In this formulation, the intermediate DataFrame objects held in current will silently be garbage collected as nothing points to them, for example. Did I mention these were huge files? The old code was much longer and error prone as I had a df1, df2, ? df8 as well as other intermediates and was easy to copy and paste then not edit the changes properly. On to the main point. Saving as a CSV was trivial. Saving as an XLSX took some work BECAUSE I had pandas but apparently was missing some components it needed. I had to figure out what was needed and get it and finally got it working nicely. In this case, I am sure I might have been able to figure out how to make my R environment work in my Windows 10 machine or installed the other software needed or move my development into Cygwin or even used Linux. Now for the main point. As hinted above, sometimes the functionality in a particular programming language like R or Python already requires the use of external resources that can include parts made in other languages. Python makes extensive use of internal parts being made in C for speed and the interpreter itself is in C or C++. Similarly R uses C and C++. So the next step can be to integrate the languages. They have strengths and weaknesses and I know both are used heavily in Machine Learning which is part of my current focus. Some things are best done when your language matches the method/algorithm you use. R has half a dozen object-oriented variations and generally they are not as useful as the elegant and consistent OO model in Python. But it has strengths that let me make weird graphics in at least three different major philosophies (base, lattice and ggplot) and lots of nice things and ways based on the underlying philosophy being that everything is a vector and most operations are vectorized or can be. Handy. It can lead to modes of thinking about a problem quite different than the pythonic way. So, I can now load an R package that lets me shift back and forth in the same session to a now embedded python Interpreter. Many ?objects? can be created and processed in one language, then passed through the veil to functionality in the other and back and forth. You can load modules within the python component and load packages in the R component. Within reason, you use the best tools for the job. If part is best done with sets or dictionaries or generators, do it on the python side. Want to use ggplot2 for graphics, do it on the R side. In reality, there are many more programming languages (especially dormant ones) than are really needed. But they evolve and some are designed more for some tasks than others. So, if you want to use Python, why not see if you can use it the way it is loosely intended. If all you are doing is rewriting it to fit the mold of another language, why not use that, if still available. Do I have a favorite language? Not really. I note that in attempts to improve Python (and other languages too) over the years, they keep adding and often in ways that sort of change enough so there is no longer as clear a philosophy. I can see heavy borrowing from many horse breeds and making a camel. So there isn?t really ONE pythonic way for many things. We have two completely separate ways to format strings that end up with fairly similar functionality. Actually, there is an implicit third way ? So I think there are multiple overlapping sets of what it means to be pythonic. If you come from a procedural background, you can do much without creating objects or using functional programming skills. If you come from an OO background, you can have fun making endless classes and subclasses to do just about everything including procedural things the hard way by creating one master object that controls lots of lave objects. If you lie functional programming with factories that churn out functions that may retain their variables, again, have fun. There are other such paradigms supported including lots of miniature sub-languages you can create with regular expressions being an example, as well as the print formatting methods. To be fluent in python, though, you need to be able to use other people?s existing code and perhaps be able to modify or emulate it. That effectively means being open to multiple ways so in a sense, being pythonic includes being flexible, to a point. Good place to stop and resume my previously scheduled life. From alan.gauld at yahoo.co.uk Tue Nov 20 15:22:01 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 20 Nov 2018 20:22:01 +0000 Subject: [Tutor] Pythonic way In-Reply-To: <005201d480fc$0e615240$2b23f6c0$@verizon.net> References: <005201d480fc$0e615240$2b23f6c0$@verizon.net> Message-ID: On 20/11/2018 18:08, Avi Gross wrote: > ... So there isn?t really ONE pythonic way for many things. That's true and, I think, inevitable for anything developed in the open source world. If you compare it to a language entirely controlled by a single mind - like Oberon or Eiffel say - then there is much less consistency. But how many people actually use Oberon or Eiffel in the real world these days? > We have two completely separate ways to format strings And many options for concurrency and for running external programs. Much of it is history and the need for backward compatibility. And let's not even think about web and GUI frameworks! > ..you can do much without creating objects or using functional programming > ...If you come from an OO background, you can have fun making endless classes >...If you lie functional programming with factories that churn out functions > ...There are other such paradigms supported including lots of miniature sub-languages > ...effectively means being open to multiple ways I think that's a very deliberate feature of Python going back to its original purpose of being a teaching language that can be used beyond the classroom. It was always intended to support multi paradigms. After all, every programmer should be aware of multiple paradigms and when to best use each. BUt, Python is currently suffering the same fate as C++ in that, as it becomes more mainstream in real-world industry, the feature demands upon it inevitably move it away from some of those original teaching based ideas. It is certainly a much harder language to learn today than it was when I started in 1998. From a pure academic CS view many changes are good (eg. iterators and meta programming) but from a non-academic beginner(or even high school student) they are just plain confusing. It's all part of being a success in the real world. The funding for development comes from the industrial user community not the high schools or colleges, so their needs come first. PS. Just back from vacation so still catching up on the last week's discussions! -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From breamoreboy at gmail.com Tue Nov 20 15:45:11 2018 From: breamoreboy at gmail.com (Mark Lawrence) Date: Tue, 20 Nov 2018 20:45:11 +0000 Subject: [Tutor] Pythonic way In-Reply-To: <005201d480fc$0e615240$2b23f6c0$@verizon.net> References: <005201d480fc$0e615240$2b23f6c0$@verizon.net> Message-ID: On 20/11/2018 18:08, Avi Gross wrote: > > We have two completely separate ways to format strings that end up with fairly similar functionality. Actually, there is an implicit third way ? > You could argue five ways :-) 1. C printf style formatting https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting 2. New style string formatting https://docs.python.org/3/library/string.html#string-formatting 3. f-strings https://docs.python.org/3/reference/lexical_analysis.html#f-strings 4. String templates https://docs.python.org/3/library/string.html#template-strings 5. String methods https://docs.python.org/3/library/stdtypes.html#string-methods Any advance on five anybody? -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From steve at pearwood.info Tue Nov 20 17:35:48 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 21 Nov 2018 09:35:48 +1100 Subject: [Tutor] Pythonic way In-Reply-To: References: <005201d480fc$0e615240$2b23f6c0$@verizon.net> Message-ID: <20181120223547.GC4319@ando.pearwood.info> On Tue, Nov 20, 2018 at 08:22:01PM +0000, Alan Gauld via Tutor wrote: > I think that's a very deliberate feature of Python going back > to its original purpose of being a teaching language that > can be used beyond the classroom. I don't think that is correct -- everything I've read is that Guido designed Python as a scripting language for use in the "Amoeba" operating system. You might be thinking of Python's major influence, ABC, which was designed as a teaching language -- but not by Guido himself. Guido was heavily influenced by ABC, both in what to do, and what not to do. https://www.artima.com/intv/pythonP.html http://python-history.blogspot.com/2009/02/early-language-design-and-development.html -- Steve From avigross at verizon.net Tue Nov 20 22:05:05 2018 From: avigross at verizon.net (Avi Gross) Date: Tue, 20 Nov 2018 22:05:05 -0500 Subject: [Tutor] evolutionary drift Message-ID: <005801d48147$00eead10$02cc0730$@verizon.net> Steve, You may be right. It often happens that someone has a (small) idea, perhaps very focused, and others chime in and try to apply it more widely, perhaps by making it more general, and it grows. Over the years, the earlier adopters may be seen almost as co-creators or even become the lead in the story. Tell me who seems to be associated with Apple and who did much of the technical work? I am not saying that Jobs did not have vision and marketing talent and an eye for style and so on. I am not even sure who came up with ideas back then. Another such person, Bill Gates, did do some programming of BASIC early on and so forth. So, whatever the history of early Python (and predecessors) was, it may have begun as some enhancements and improvements of what came before and perhaps new paradigms. Others who saw it may have seen something that looked easier to teach. An obvious example, if it was there way back then, was removing lots of brackets used in other languages (such as {([])} ) and using indentation. Feels more natural to not-so-mathematical types. But over the years the brackets have returned and worse. Like many languages, Python used what symbols it could find on the keyboard and then overloaded them horribly. Parentheses are used for grouping but also for tuples and to force invocation of a function and to hold the argument tuple (albeit no trailing comma is needed for a single argument as in other tuples) and presumably in other contexts such as within regular expressions. Periods can mean quite a few things as can asterisk and so on. There are few matched sets of characters on the keyboard. () is now used for tuples, among other things. [] is used for lists except when it is used for dictionary access as in cards[key] versus text[5] and {} is used for dictionaries except when you use [] but also for sets ... Heck, they ran out of symbols. {} is an empty dictionary and you say set() for an empty set. Ditto for tuple. <> is not currently used as a matched set as it has many other uses like in comparisons. Some languages even use <> as the same as != or ~= to mean not equals. "" and '' and even `` are sort of used as if they are a matched set I some languages (`` is in R, not Python) but are typographically identical as compared to text processing that creates two versions. EBCDIC had a few other symbols but many languages now use only ASCII symbols and have to combine them to make complex and even non-intuitive combinations as symbols. Try teaching a child in say C++ that: X++==++Y is valid and even meaningful if read as: X++ == ++Y because it asks you to get the current value of X to compare to the NEW value of Y incremented and return a Boolean result and immediately thereafter, increment X. Heck, you can write (X++++Y) and so on. I have seen languages with an = and == and even === alongside := and ::= and -> and --> and <- and <-- and more all to mean variations on a theme. If we had started with many more symbols, in some ways it would be harder but in other ways easier. Mathematicians borrow symbols from lower case, upper case and script letters from languages like Greek and Latin but also from Hebrew as in, well not easy to include in a file contain normal text but aleph-null (and aleph-one and infinitely more levels of infinity.) A simple teaching language that uses English words children know might either be verbose in places or long as in making you spell out DELETE instead of del. But quite a few languages are simple if you leave out most of the functionality. Yes, you need to explain why some things must end in a semicolon or be indented a certain way or require parentheses or ... What you don't want to teach is a complex language like English. I was teaching my Dad as he prepared for Citizenship and he balked when told there were at least seven distinct ways you pronounce OUGH in common English words. So, perhaps python can be used to teach basic programming for several reasons including no need to declare variables and their "types" in advance and having relatively intelligent basic types that easily get converted in the background as in various kinds of numbers. But when you use more and more features, it expands into areas where, frankly, there is no one right answer and choices are often made by fiat or by a majority. Mathematically, if you view a topic like multiple inheritance in classes and the kludges made to get around logical issues, you see what a mess it really is and very hard to teach. Look at the double underscore notation(prefix-only) for variables that renames them to be unique within a certain scope to avoid collisions in the mythical searched name space. I am not against such drift but at times wonder if a one-size-has-to-fit-all mentality is wise. I had a thought on an earlier topic by Asad. He wanted to write in Python what is effectively a member of the UNIX grep family. Specifically, fgrep (or maybe now grep -F) takes a list of things to search for, albeit fixed strings. His need is literally to Get a line, apply Regular Expession and Print. GREP. In my earlier days at Bell Labs, it was often easier, especially in prototyping, to string together multiple tools, often single-minded little ones, sort of like what someone here wanted to do with calling a pipeline of programs from within Python and getting the results back into the program. It was not unusual to do a grep piped to a cut to select parts of the line then pipe it to something that excludes some of those in a reverse grep on a second pattern then pipe it to something like the stream editor sed to make changes and so on. Yes, AWK and PERL and other more full-blown languages evolved but were often initially used to do small parts of the pipelines like these. We used hammers as thumbtacks and collections of thumbtacks as hammers. Python can often let you trivially build such little tools that were harder to do in C, partially because some of that is built-in or easily attachable as a module, perhaps with parts from a C library. So it may be easier now to write the pipeline as straight python code and often more efficient. I was involved with the history of things at times that now have legends about them. I remember conversations I participated in back in the Usenet days where Tim Berners-Lee was discussing what should and should not be part of hypertext which evolved into HTML. For example, should there be a FLASHING/BLINKING text as compared to bold/italic/emphasis/color. Many people participated, including others whose names are now well-known for other achievements but for many it is now remembered as the accomplishment of one or a few. Many people added to or influenced Python. Python can still be a great teaching language if kept to a subset and by avoiding some tricks, even if intriguing or delightful. One idea SOME have is to keep things as simple as you can and no simpler. Few would like to see the two-line programs I can come up with. Imagine initializing dozens of variables on one line like this: a,b,c,d,...y,z = 3,1,4,1,5,9,2,6,5,3,5, ... Or just: a=3;b=1;c=4;...z=3.14159 One-line is not the same as smart or even comprehensible. The second line might be an entire program as one big nested list comprehension. Recall the example of applying a sequence of patterns to a sequence of lines from a file? [ print(line) for line in open(filename,'r') for pattern in patterns if pattern in line] Or something like that. I expect it will produce duplicates, though. So, a longer version still on one line might convert the actual list to a set fed to a sort fed to a print as in: Print( sorted( set( [ print(line) for line in open(filename,'r') for pattern in patterns if pattern in line] ) ) ) I know, not the right order! What do you expect from a one-liner ? I haven't checked the exact use live (sort of like how I was rushed and did not check spelling and grammar earlier today before sending) but the point is a line like that can loop on both lines and patterns nested and apply an if condition and return the (not meaningful) results of printing the line to be ignored as you don't actually want to make a list, just do a print as a side effect. Sometimes a very elegant solution is also hard to understand. The second version moves the print outside, so no side-effect. What is easy to teach to children? I remember an experience I had teaching Geometry to tenth graders. There is a well known method to prove isosceles triangles (with two equal sides) must have two equal angles opposite those sides. But it requires you to create a line by dropping a perpendicular to make two smaller triangles so you can then do side-angle-side comparisons to show congruence. There is a more elegant proof I knew from taking higher level courses (Math is one of my degrees) that involves merely relabeling triangle ABC as CAB and showing how the reflected triangle is congruent to itself and thus congruent parts are equal. I turned from the blackboard to see a very puzzled class that had gotten lost on the very first step and never caught on to the beautiful and elegant and very abstract idea. I did not then try to even mention that there are many other geometries such as non-Euclidean Riemannian or Lobachevskian varieties. Wrong level for them. But, our Universe does not seem to be Euclidean, nonetheless. Frankly, much of what is added to a simple language is rarely used and some perhaps never. I was once an expert on all the word processors of the time with the intent of making programs to interconvert between them so you would send an attachment to an email which would arrive to each recipient in the form of their choice with the minimal number of conversion steps. But so many features turned out to be rarely used or even known about. Look at the modern Microsoft Word and features like making labels or setting up regions where a different language is used and spelling should be checked differently or embedding live objects like spreadsheets and on and on. I doubt many people use a tenth of the available features and often have no idea they are even there. Some more academic settings, and that includes AT&T before the breakup and for quite a while after, try to do as much as they can imagine. For example if there is a command using the letter A and one for B, we obviously need one for Q and every other letter even if they are not mnemonic or never used. They often do not ask if anyone really wants the extra stuff or whether including it means coming to market years later and with lots of bugs as the requirements are too ambitious or have conflicts within them. I wrote some of those requirements, while protesting, and often the projects never made it to light and are well forgotten now. I conclude by saying that on this forum, if a beginner or novice asks questions, we often do not tell them the right way is to make a bunch of objects with data hidden and preserved and so on, if there is a fairly simple way. But if asked how we would do it, the answer might often be that we would just grab some pre-made modules and connect it with a little code. Someone else has already done all that work and the pythonic way is to make a subclass of something that already works ... Avious -----Original Message----- From: Tutor On Behalf Of Steven D'Aprano Sent: Tuesday, November 20, 2018 5:36 PM To: tutor at python.org Subject: Re: [Tutor] Pythonic way On Tue, Nov 20, 2018 at 08:22:01PM +0000, Alan Gauld via Tutor wrote: > I think that's a very deliberate feature of Python going back to its > original purpose of being a teaching language that can be used beyond > the classroom. I don't think that is correct -- everything I've read is that Guido designed Python as a scripting language for use in the "Amoeba" operating system. You might be thinking of Python's major influence, ABC, which was designed as a teaching language -- but not by Guido himself. Guido was heavily influenced by ABC, both in what to do, and what not to do. https://www.artima.com/intv/pythonP.html http://python-history.blogspot.com/2009/02/early-language-design-and-development.html -- Steve _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From avigross at verizon.net Wed Nov 21 00:38:57 2018 From: avigross at verizon.net (Avi Gross) Date: Wed, 21 Nov 2018 00:38:57 -0500 Subject: [Tutor] the fivefold path Message-ID: <006d01d4815c$7fb771d0$7f265570$@verizon.net> Mark, Thanks for the expansion. Yes, there are ever more ways to format text. There can be an indefinite expansion beyond this is many ways. I was thinking of ways to make bits and pieces from objects and stringing them together, perhaps inventing your own methods. Anyone can come up with another meta-language, perhaps embed them in a module with classes, and use overloading methods to make it easier to use. But worst of all is to simply find an external application and open up a connection to it such as with Popen() or a call to a URL that accepts some formatting string and returns something formatted. Consider something like a UNIX Shell language like sh/csh/ksh or more modern incarnations like BASH that use a dollar sign in imaginative ways such as $PATH or `command args` to make substitutions. You can imagine invoking something like that while passing along parts needed and taking back the result. Now, arguably, those are not part of the language. But if you want to build a weird enough scenario, you could have a Rube Goldberg device that bounced around multiple servers on the internet running many kinds of OS and percolated some complex result back into a now formatted string. Diversity is nice but also often a distraction. Worse is when trying to read what others have written. On that topic, I note many people like to include short segments in their ENGLISH writing from other languages. Latin and French are common inserts as they are easy to read albeit not always to comprehend. Lawyers seem unable to communicate without lots of Latin nonsense and some musicians must toss in periodic tidbits of Italian and so on. Languages that use characters totally outside what is in the character sets In English or near relatives that have added characters with diacritical marks, are less often seen. How often do you see Greek or Hebrew? OK, I admit I see them often, but my reading tastes are eclectic. I bet many would at least like a translation and/or transliteration next to such quotes or just the translation. Back to programming. Nothing wrong with multiple ways if they offer more functionality that is useful But why constantly reinvent the wheel as an ellipse? Look at the issue of strings. There actually is a reason for the many variation in python. Single and double quotes are pretty much identical with the exception that they make it easier to include the other symbol without backslashes. Triple quotes (three doubles) offer other improvements and so do several raw or Unicode variants. But strictly speaking, they are often more syntactic sugar for one or a few underlying storage mechanisms accompanied by access methods. Objects, of a sort. The f version mentioned as a formatting method is in a sense another such object with additional methods. You can well imagine more such things you can design into a language, albeit some may not be able to use existing symbols you ran out of unless you want to use "'"'" stuff "'"'" (where you may not be able to read the nested " and ' and " and ' and " to have some exotic meaning like format the sting repeatedly in a loop till it does not change. For example, if $PATH evaluated to something${DATE}something`echo "hello \$world!"`something and $DATE expanded to $MONTH/$DAY/$YEAR then repeated formatting till no more expansions happen could be another type of string. No, not asking for that especially since I can see easy ways to make infinite loops ? Mark, perhaps jokingly, asks what can you do for your language. Nice sentiment but I find it less useful to be wedded or loyal to things as it is always more interesting to see if something new developed is at least as interesting. Many ideas when carried through to the point of absurdity stop being as useful. I am thinking of some LISP variants with very nice mathematical/philosophical ideas including how an amazing number of things can be done recursively. So if you asked them to write a function in lisp that compares A and B, they don't bother putting the numbers in two registers like A and B and issuing a compare directive at machine language level. Instead, they write the LISP equivalent of this pseudocode: Greater(A,B) If A == 0, return FALSE If B == 0, return TRUE Otherwise, return Greater(A-1, B-1) Try comparing A,B = 999,999,999,999, 1,000,000,000,000 How much memory does your computer have? The numbers are ONE apart with A being smaller. But the algorithm slavishly puts recursive function calls on the stack about a trillion times then unwinds. You say no problem, tail recursion allows replacing the function on the stack. Maybe. So all you need is a trillion times the CPU time or so for something done in a few cycles in most hardware. (((((Parenthetically, I am not picking on LISP), as most modern) (computer languages)) allow (((equivalent)) code. ))) Half the fun when I thought LISP was having people get lost counting parentheses at a time that editors did not easily tell you what matched what. But keeping track of multiple threads in a GUI can be a modern version of nested complexity. I used to observe the Obfuscated C contests years ago. Anyone for obfuscated python. Well, not here in the tutor group. https://www.ioccc.org/ http://wiki.c2.com/?ObfuscatedPython I have played in the past with tricks to write code mostly in a language like Hungarian using a pre-processor that flipped the key words back into the usual English style and of course used variable names in yet other languages like transliterated Aramaic. Hard to find people who could even figure out what the program did. Throw that into a language like Python where you can create classes and overload operators and it can get to look like an Abbot and Costello routine. But if you want clear, self-documenting, code, I prefer that. Time to get back to normal questions on this forum? -----Original Message----- From: Tutor On Behalf Of Mark Lawrence Sent: Tuesday, November 20, 2018 3:45 PM To: tutor at python.org Subject: Re: [Tutor] Pythonic way On 20/11/2018 18:08, Avi Gross wrote: > > We have two completely separate ways to format strings that end up > with fairly similar functionality. Actually, there is an implicit > third way ? > You could argue five ways :-) 1. C printf style formatting https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting 2. New style string formatting https://docs.python.org/3/library/string.html#string-formatting 3. f-strings https://docs.python.org/3/reference/lexical_analysis.html#f-strings 4. String templates https://docs.python.org/3/library/string.html#template-strings 5. String methods https://docs.python.org/3/library/stdtypes.html#string-methods Any advance on five anybody? -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From alan.gauld at yahoo.co.uk Wed Nov 21 05:31:11 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 21 Nov 2018 10:31:11 +0000 Subject: [Tutor] Pythonic way In-Reply-To: <20181120223547.GC4319@ando.pearwood.info> References: <005201d480fc$0e615240$2b23f6c0$@verizon.net> <20181120223547.GC4319@ando.pearwood.info> Message-ID: On 20/11/2018 22:35, Steven D'Aprano wrote: > On Tue, Nov 20, 2018 at 08:22:01PM +0000, Alan Gauld via Tutor wrote: > >> I think that's a very deliberate feature of Python going back >> to its original purpose of being a teaching language that >> can be used beyond the classroom. > > I don't think that is correct -- everything I've read is that Guido > designed Python as a scripting language for use in the "Amoeba" > operating system. I think both are true. Guido was working on Amoeba at the time he built Python so it was an obvious choice of platform, although he first built it on a Mac. But he didn't set out specifically to build a scripting language for Amoeba but rather to build "a descendant of ABC" which was also usable in the real world, specifically The C/Unix world. To do that he believed it had to address the major barriers to ABC which were 1)being capable of being extended and 2) improved I/O. My source for that conclusion is Guido's forward to "Programming Python" 1st edition. https://www.python.org/doc/essays/foreword/ Also his comments when he set up the "Computer Programming for Everybody" initiative, which he led for several years. (Which I can't locate...) So I believe that ease of teaching was definitely part of the original game plan and was definitely a factor in some of the later developments around v2.0 or so. -- 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 Nov 21 06:09:12 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 21 Nov 2018 11:09:12 +0000 Subject: [Tutor] evolutionary drift In-Reply-To: <005801d48147$00eead10$02cc0730$@verizon.net> References: <005801d48147$00eead10$02cc0730$@verizon.net> Message-ID: On 21/11/2018 03:05, Avi Gross wrote: > <> is not currently used as a matched set as it has many other uses like in comparisons.> Some languages even use <> as the same as != or ~= to mean not equals. Indeed, Python used to do the same but it was removed in, I think, v2. > A simple teaching language that uses English words children know Probably the best example of that I have seen is Logo. Although it does use [] and () But otherwise its quite child friendly. But ultimately that makes it less likeable in the "grown up world"... > Python can still be a great teaching language if kept to a subset That is true and it is still one of the very few languages that I'd recommend for teaching. But sadly its underbelly shows through very early for beginners. For example in v1 Python range() returned a list. That was easy to understand. Now range() returns a "range object" - what the heck is that? and why do we need it? Similarly with iterators. Things that could easily be iterated over without thought now require "iterators" and/or evaluate to iterators. And the error messages tell you so - but iterators are a concept wholly alien to most beginners. And are quite hard to explain. Those are just the two things that beginners most frequently mail e about from my tutorial. There are lots of other areas where Python implementation now shines through in ways that trip beginners up. > What is easy to teach to children? I'm not sure we should even focus on children. It's more about teaching anyone(regardless of age) who has no prior experience, and especially little or no formal math background. Someone with good high school math can be taught programming fairly easily. But with no math foundation even basic concepts like expressions and assignment become very difficult. It may even be impossible. I used to think that anyone could learn to program but over the last 30 years of trying I've come to the conclusion that its not true. You need a brain that's wired a certain way otherwise it just doesn't make any kind of sense. Its like math. Some folks just can't understand mathematical concepts, they are illogical and non-sensical to them. They might learn some basic principles by rote but they never really see how or why it is so. Same with programming. Some people just don't get it. Thankfully those are a very small minority! -- 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 avigross at verizon.net Wed Nov 21 11:31:59 2018 From: avigross at verizon.net (Avi Gross) Date: Wed, 21 Nov 2018 11:31:59 -0500 Subject: [Tutor] origins bootstrapped. Message-ID: <009101d481b7$b9c300f0$2d4902d0$@verizon.net> Alan has been involved with Python for a long time so he has more to offer historically. I don't see some things as either/or. You can start with one major motivation and it morphs from a one-celled creature like an Amoeba to a complex vertebrate like a Python which needs modules added so it can walk around better. OK, horrible analogy but interesting naming. So some say Guido started with learning his ABC and then became educated enough to understand Monty Python and reach for the holy grail. OK, even worse. Time to get serious. I have seen this on many projects, not just programming languages and environments. Something fairly simple is imagined then perhaps prototyped. Someone may notice that what was created may be used in another way if perhaps expanded a bit. Someone then realizes they now have functionality that can be used to create something else, in a form of bootstrapping. After a while they have a collection of tools that can be combined to make something more complex. The biological analogy above can be an example. No, I am not saying that a distant ancestor of a snake like a python was an amoeba. But they do share common ancestors they have both diverged from with the amoeba remaining a single celled organism and the python descending from something that became multi-cellular then differentiated into having different kinds of cells in tissues and organs and became a somewhat integrated whole that is possibly more than the sum of its parts. The ABC analogy is also obvious. Once an alphabet is chosen and provisional meanings given to each letter, it can grow and even adjust to making words and sentences and even seemingly endless streams of consciousness like some of my messages. Python was built on top of other achievements that some people were learning from. There were many steps along the way from building machines programmed one byte at a time in binary (I hated a class that made me do that as one error means start over) to various levels where a compiler and then an interpreter would parse things. We have been discussing using regular expressions. Much of a language like python is having bits and pieces of code written in ASCII or Unicode be parsed using hopefully unambiguous rules into tokens that can be made into decision trees or whatever data structure. That deepens on being able to look for and find some sort of pattern in strings. I am not sure what python and others use, but it may be tools similar to string search or regular expressions that allows them to bootstrap. Back when my group was programming in C, I was sent to Denver for a class in Lex/Yacc to learn how to use C libraries that now look primitive. One was a lexical analyzer and the other sort of a parser somewhat rudely named as Yet Another Compiler-Compiler. But today, what do most people use? Our tools improve, often by being a wrapper to older tools and so on for multiple levels. New functionality is added too. Can I ask a question that I really want an opinion on? As a preface, I see some think python as a formal language is being pushed by industry in directions that may not meld as well for its use in other contexts like for teaching students. How much of that is due to it being a relative open and free product? There are plenty of other applications that you pay for and thus have to be responsive to the buyers to remain in business. Python has many implementations including some freer than others. Yet is has gone through a bit of a bifurcation and many would like to see 2.X retained and others wish everyone should migrate. Is there room for a smaller core language that remains good for teaching purposes and that is small enough to fit in a Rasberry pi, while other versions are of industrial strength? Do we already sort of have some of that? I was thinking of how many languages and environments have been looking at working using parallelism. Most people simply have no need for the complication. When you add the ability to do multiprocessing within an application using something like threads, you spend lots of time making sure you literally lock down shared resources so they are used serially. You need to make sure race conditions do not lock up all your threads at once. Lots of added overhead is only worth it if you gain in the process. Add multiple cores in your CPU, and you may need to handle more complications as they are actually running in parallel, perhaps still sharing a common memory. Allow it to use multiple processors around the world, and you need even more elaborate control structures to synchronize all that. It definitely is worth doing but does everyone need it especially for teaching an intro class? I was thinking about the little project I mentioned the other day. Should some of it be done in parallel using methods available? One part of the problem was to read in N files into N pandas DataFrame objects. I knew that I/O tends to be fairly slow and most programs take a nap while waiting. In this case, a nontrivial amount of CPU is involved after reading in dozens of lines to try to recognize the likely type of each intended column and then process the data read in and continue reading more and processing it perhaps while also looking to see if any additional data indicates an error. So the CPU usage may be nontrivial and perhaps even lag the I/O timeframe at times. An obvious speedup might be had by starting up N threads with each opening one file and doing what I said above into one shared process with N variables now available. But will it be faster? Should they all start at once, or maybe follow each other every tenth of a second? If N is huge, should we have a queue that starts up M copies and replenishes them as threads complete? Should the actual files be moved onto multiple hard disks because the total I/O from one disk may be maxed out or even degraded by too many requests in random areas of the disk? Lots of questions like that but I have to head out so I will pause and simply say that I opted not to bother as the darn program finished in 5 or 10 seconds. For heavy industrial uses, like some of the applications in the cloud dealing with huge problems, it may well be worth it. -----Original Message----- From: Tutor On Behalf Of Alan Gauld via Tutor Sent: Wednesday, November 21, 2018 5:31 AM To: tutor at python.org Subject: Re: [Tutor] Pythonic way On 20/11/2018 22:35, Steven D'Aprano wrote: > On Tue, Nov 20, 2018 at 08:22:01PM +0000, Alan Gauld via Tutor wrote: > >> I think that's a very deliberate feature of Python going back to its >> original purpose of being a teaching language that can be used beyond >> the classroom. > > I don't think that is correct -- everything I've read is that Guido > designed Python as a scripting language for use in the "Amoeba" > operating system. I think both are true. Guido was working on Amoeba at the time he built Python so it was an obvious choice of platform, although he first built it on a Mac. But he didn't set out specifically to build a scripting language for Amoeba but rather to build "a descendant of ABC" which was also usable in the real world, specifically The C/Unix world. To do that he believed it had to address the major barriers to ABC which were 1)being capable of being extended and 2) improved I/O. My source for that conclusion is Guido's forward to "Programming Python" 1st edition. https://www.python.org/doc/essays/foreword/ Also his comments when he set up the "Computer Programming for Everybody" initiative, which he led for several years. (Which I can't locate...) So I believe that ease of teaching was definitely part of the original game plan and was definitely a factor in some of the later developments around v2.0 or so. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From alan.gauld at yahoo.co.uk Wed Nov 21 19:54:29 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 22 Nov 2018 00:54:29 +0000 Subject: [Tutor] origins bootstrapped. In-Reply-To: <009101d481b7$b9c300f0$2d4902d0$@verizon.net> References: <009101d481b7$b9c300f0$2d4902d0$@verizon.net> Message-ID: On 21/11/2018 16:31, Avi Gross wrote: > Alan has been involved with Python for a long time so he has more to offer > historically. I'm not so sure about that, several folks on this list have been around longer than me. And I don't follow the main comp.lang.python list that closely. I'm simply giving my perspective for whatever that may be worth. > OK, horrible analogy but interesting naming. So some say Guido started with > learning his ABC and then became educated enough to understand Monty Python > and reach for the holy grail. Made me laugh out loud! > Back when my group was programming in C, I was sent to Denver for a class in > Lex/Yacc to learn how to use C libraries that now look primitive. One was a > lexical analyzer and the other sort of a parser somewhat rudely named as Yet > Another Compiler-Compiler. Still powerful tools and in active use in several projects. They were great for quickly bootstrapping a small bespoke language. > some think python as a formal language is being pushed by industry in > directions that may not meld as well for its use in other contexts like for > teaching students. How much of that is due to it being a relative open and > free product? I think that's true but not necessarily bad. It just takes the language in as different direction. And as you said, that happens in many projects. They start as one ting and end up someplace entirely different. I remember one project that started out as a network management system for a fairly obscure protocol and wound up as both a customer service system for our Global Corporate clients and as part of the monitoring system for the English Channel Tunnel!. Very different applications of the same root code base. > ...Is there room for a smaller core > language that remains good for teaching purposes and that is small enough to > fit in a Rasberry pi, while other versions are of industrial strength? Do we > already sort of have some of that? We sort of have that. Python v3 certainly works well on the pi. We could certainly have a smaller language for teaching but then we had that in ABC and nobody used it. Students don't like learning stuff that they can't use in the real world. And if you want purity for beginners we already have Logo, Scheme, Squeak/Scratch and a few others. But none of those really work well in the wider world. Which is why I still recommend python, warts and all. > I was thinking of how many languages and environments have been looking at > working using parallelism. Most people simply have no need Absolutely and for beginners a single thread is more than enough to cope with. > I was thinking about the little project I mentioned the other day. Should > some of it be done in parallel using methods available? It sounded a lot like a job for the map-reduce paradigm. Which is parallel where it can be and sequential where it should be... > An obvious speedup might be had by starting up N threads with each opening > one file and doing what I said above into one shared process with N > variables now available. But will it be faster? Trying to calculate (or guess) this kind of thing in advance is near impossible. The best solution is to prototype and measure, making sure to do so on typical data volumes. That having been said if you know (or discover) that you definitely need parallelism then its definitely worth revisiting the design to ensure the data structures and overall workflow are optimised for a parallel approach. > ...I will pause and simply say that I opted not to bother > as the darn program finished in 5 or 10 seconds. Exactly so. AS the famous quote says "Premature optimisation is..." > For heavy industrial uses, like some of the applications in the cloud > dealing with huge problems, it may well be worth it. In many cases it's the only practical solution. Almost all of my industrial programming has involved multi processing and threading. Almost none (I think one )of my personal programming projects has needed 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 mats at wichmann.us Wed Nov 21 20:57:55 2018 From: mats at wichmann.us (Mats Wichmann) Date: Wed, 21 Nov 2018 18:57:55 -0700 Subject: [Tutor] origins bootstrapped. In-Reply-To: References: <009101d481b7$b9c300f0$2d4902d0$@verizon.net> Message-ID: <60150808-8cf4-1555-3f3f-143ecb2f153e@wichmann.us> On 11/21/18 5:54 PM, Alan Gauld via Tutor wrote: > On 21/11/2018 16:31, Avi Gross wrote: >> An obvious speedup might be had by starting up N threads with each opening >> one file and doing what I said above into one shared process with N >> variables now available. But will it be faster? > > Trying to calculate (or guess) this kind of thing in > advance is near impossible. The best solution is to > prototype and measure, making sure to do so on typical > data volumes. > > That having been said if you know (or discover) that > you definitely need parallelism then its definitely worth > revisiting the design to ensure the data structures > and overall workflow are optimised for a parallel approach. > >> ...I will pause and simply say that I opted not to bother >> as the darn program finished in 5 or 10 seconds. > > Exactly so. > AS the famous quote says "Premature optimisation is..." > >> For heavy industrial uses, like some of the applications in the cloud >> dealing with huge problems, it may well be worth it. > > In many cases it's the only practical solution. > Almost all of my industrial programming has involved multi > processing and threading. Almost none (I think one )of my > personal programming projects has needed it. > People play all kinds of parallelism tricks with Python because Python has a certain Impedimet Which Shall Remain Nameless (except I'm certain someone will mention it). Anyway, it's one thing to try to decompose a massive problem, that's interesting on a certain level (see some of the talks companies like Google have done on scaling their services) but is really hard to replicate at home. But another use for non-linear programming, if you want to call it that, is task that just needs a different programming model. That's where a lot of the async stuff with coroutines and event loops that has been beefed up recently is quite interesting. Even very simple programs can run into cases where it may make sense, usually if there are things you have to wait for and want to be able to do other work while doing so. I actually got around to watching David Beazley's talk from a couple years ago, and it was pretty impressive - something I'd flagged as "watch later" I don't know how long ago, more than a year at least. Wish I'd watched it earlier now! I hate posting those obscure YouTube links where people don't know what they are clicking on, so search for this title if interested: David Beazley - Python Concurrency From the Ground Up (it's from the 2015 PyCon) From david at graniteweb.com Wed Nov 21 21:00:40 2018 From: david at graniteweb.com (David Rock) Date: Wed, 21 Nov 2018 20:00:40 -0600 Subject: [Tutor] origins bootstrapped. In-Reply-To: <009101d481b7$b9c300f0$2d4902d0$@verizon.net> References: <009101d481b7$b9c300f0$2d4902d0$@verizon.net> Message-ID: > On Nov 21, 2018, at 10:31, Avi Gross wrote: > > Is there room for a smaller core > language that remains good for teaching purposes and that is small enough to > fit in a Rasberry pi, while other versions are of industrial strength? Do we > already sort of have some of that? What comes stock on a Pi is more than sufficient (there?s plenty of room for ?standard? python 2 and python 3). Micropython (https://micropython.org/) fits that category nicely for micro controllers and Adafruit?s version of it, CircuitPython has a strong following https://www.adafruit.com/circuitpython These have been great to allow people learn not only python, but how to physically interact with the world outside the computer. ? David Rock david at graniteweb.com From steve at pearwood.info Thu Nov 22 01:05:50 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 22 Nov 2018 17:05:50 +1100 Subject: [Tutor] origins bootstrapped. In-Reply-To: <009101d481b7$b9c300f0$2d4902d0$@verizon.net> References: <009101d481b7$b9c300f0$2d4902d0$@verizon.net> Message-ID: <20181122060549.GD4319@ando.pearwood.info> On Wed, Nov 21, 2018 at 11:31:59AM -0500, Avi Gross wrote: > Alan has been involved with Python for a long time so he has more to offer > historically. I've been involved with Python for a long time too. What exactly are you trying to say? > Can I ask a question that I really want an opinion on? As a preface, I see > some think python as a formal language is being pushed by industry in > directions that may not meld as well for its use in other contexts like for > teaching students. I think there is always going to be tension between the needs of different users. Beginners need simplicity; expert, experienced programmers need power; both have very different ideas of what "readable code" means. I don't think Python is being pushed in any direction by "industry". It is evolving according to the needs of the programmers who use it, some of whom may work for some industry or another. > How much of that is due to it being a relative open and > free product? There are plenty of other applications that you pay for and > thus have to be responsive to the buyers to remain in business. Python has > many implementations including some freer than others. I don't know of any non-free (free as in beer, or free as in speech) implementations of Python. Can you elaborate? > Yet is has gone > through a bit of a bifurcation and many would like to see 2.X retained and > others wish everyone should migrate. Is there room for a smaller core > language that remains good for teaching purposes and that is small enough to > fit in a Rasberry pi, while other versions are of industrial strength? Do we > already sort of have some of that? Standard CPython is light enough to run on fairly low-powered devices, including Raspberry Pi. For an even smaller footprint, you can use Micropython, which will run on embedded devices, although ?Py does make some comprompises that means that it's not a fully compliant Python implementation. There are, or were, other small implementations: - Pippy, Python for Palm (probably unmaintained by now...) - Python for S60, for the Nokia S60 platform (likewise...) - Pythonce, for Windows CE (who still uses WinCE?) - PyMite for embedded devices - Python-iPod - Py4A and QPython (Android) - TinyPy - PyPad for the iPad - Pycorn, Python running on bare hardware with no OS > I was thinking of how many languages and environments have been looking at > working using parallelism. [...] > It definitely is worth doing but does everyone need it especially for > teaching an intro class? Who teaches threading and parallelization in introductory classes? -- Steve From anilduggirala at fastmail.fm Thu Nov 22 11:56:25 2018 From: anilduggirala at fastmail.fm (Anil Duggirala) Date: Thu, 22 Nov 2018 11:56:25 -0500 Subject: [Tutor] Info on APIs and handling JSON In-Reply-To: References: <20181116193142.bv33lzm3q5k3hpnn@adell35> Message-ID: <1542905785.3833934.1585825304.25524159@webmail.messagingengine.com> thanks a lot Mats, that is what I was thinking. The info on JSON will however be useful to me anyways, thanks a lot, On Fri, Nov 16, 2018, at 6:06 PM, Mats Wichmann wrote: > On 11/16/18 12:31 PM, Anil Duggirala wrote: > > hello, > > > > I have been looking for documentation about how to connect to APIs and > > using the provided info within python. I have found a few websites > > talking about very particular cases. Probably the most enlightening was > > the 'requests' library documentation. Can someone please point me to > > serious and comprehensive info on how to deal with APIs and then > > handling JSON and other provided info in python? Maybe a book? > > json handling is simple: the json module is part of the Python standard > library. > > https://docs.python.org/3/library/json.html > > APIs have no specific answer. An API is an Application Programming > Interface, each is different and specific to the project in question but > refers to how you communicate with the functionality of that project > from your program. Some popular ones have Python adaptations available, > otherwise you read the documentation and figure out how to code your > own. Sometimes the API is a style that lets you use an existing Python > project, even if it doesn't know anything about the thing you want to > talk to. requests is a good example - is the project you're interested > in provides a RESTful API, using HTTP protocols, then requests can > certainly be used to talk to it, but you still need to discover the > specifics and code them up yourself. Other APIs provide other interface > styles. > > > > From avigross at verizon.net Thu Nov 22 10:37:20 2018 From: avigross at verizon.net (Avi Gross) Date: Thu, 22 Nov 2018 10:37:20 -0500 Subject: [Tutor] seniority Message-ID: <005e01d48279$41b3e7b0$c51bb710$@verizon.net> Steven, Good question. I do not know how long and in what capacity many people have been involved with anything, including Python in some form. I do know a bit about Alan and plenty about myself. I was saying that I would take seriously what was said by people LIKE Alan because they had more experience and perhaps more internal knowledge than someone like e who took an introductory course in Python maybe five years ago then went on to many other languages and only came back recently and is doing some serious studying. The main difference between me and others here with questions is that I am in the general category Alan mentioned of someone with a mindset that has been very hospitable to mathematics and an algorithmic way of thinking. But that does not give me much knowledge in an experiential sense other than what I have read. And, some of my resources are out of date. Last night I watched several talks by David Beazley that Mats pointed out and plan on doing some more. He kept talking about revolutionary changes in release 3.6 and I note I am now using a 3.7 variant. He suggested you use these such as the f-string or depending on dictionaries to be in the order things were added and so on. My thought was that this would disrupt anyone running not just 2.X but any older version of 3.X but that may have been his point. You may think you know the language, but it is like layers of the onion and some of your favorite features were definitely not in place way back when the people with seniority got into it. Watching him rapidly improvise code live, I also saw how some people can make ever deeper abstractions come to life in ways that even sort of add features to the language if used right. He did an entire talk on using classes and metaclasses and decorators to change the useless annotations added to the language that don't actually check if arguments to functions (or in classes) are of the type or condition expected, such as a positive integer, to one where the invisible wrappers created will check implicitly and return errors. It is very possible to write programs that others may not easily understand just using existing features, let alone as it keeps evolving. Rushing out, as usual. Today is Thanksgiving in my part of the world so will reply to the rest of the message another time. Avi -----Original Message----- From: Tutor On Behalf Of Steven D'Aprano Sent: Thursday, November 22, 2018 1:06 AM To: tutor at python.org Subject: Re: [Tutor] origins bootstrapped. On Wed, Nov 21, 2018 at 11:31:59AM -0500, Avi Gross wrote: > Alan has been involved with Python for a long time so he has more to > offer historically. I've been involved with Python for a long time too. What exactly are you trying to say? <<>>> https://mail.python.org/mailman/listinfo/tutor From avigross at verizon.net Thu Nov 22 16:45:17 2018 From: avigross at verizon.net (Avi Gross) Date: Thu, 22 Nov 2018 16:45:17 -0500 Subject: [Tutor] learning languages Message-ID: <003101d482ac$ad659b10$0830d130$@verizon.net> Steve, I would go a step further. The word "industry" is used by some to mean THEM. I would suggest it is more like US as the younger people of yesteryear age into becoming the employees and even management of the industries involved. When a language like Python has been around long enough so lot of people are exposed to it in places like College, then when they find work, they often select a place that has what they are used to or try to bring it along at some point. I have had to largely start over several times as the computers and environment at the University of Wisconsin were very different than I had used in N.Y. I used different hardware (as it evolved) and other programming languages and operating systems and so on. When I moved on to Bell Labs, nobody cared about what I could do with PASCAL or with DEC Operating systems. I rapidly adjusted to UNIX and C and all that comes with it. Hence my regular search for novelty and learning new ways. Heck, when I used to teach, I did not want to teach the same course twice. Been there. Done that. But for many people, the idea of leveraging what they are used to has been a factor in bringing many academic versions of software and ideas into industry. But once in a more real world, people may find that the tools may not fit them as well and the choice is to make new ones or try to change the existing ones. I speculate this is one way to look at the pressure. I am puzzled about your question of what I meant by free. Yes, if you want a higher quality product you may have to pay for it or if you want to make commercial use. But for routine purposes, there are many things now for free. Look at many of things in the GNU category. You can take a PC (not free) add free enough operating systems like LINUX, add suite after suite of software including Python and R (but not S) and free mailers and spreadsheets and games. If you want a commercial variant on R, you can buy S. I am not up on it, but Python has many implementations and some may come at a price, especially if you buy it for commercial use. You mention a bunch of variants in your message with some only having a PieceOfThePy and there are many more. But if you just want to learn it, tons of info is free including plenty of documentation and lots of free add-ons like modules or code that does useful things. As pointed out, IDLE is a free editor/environment written in Python and just sitting there to be used or edited or adjusted to other needs. You ask who taught parallel processing in an intro class. True if you mean an intro PROGRAMMING class. But I have seen many classes on topics like Analysis of Algorithms or Machine Learning where a part of the class is to explain partially in the context of one or more computer languages or have you do assignments. Someone recently wanted to know how to program various cryptographic methods in Python and elsewhere asked how to do it in another language. But my point was perhaps not what you assume. My point is that when teaching beginners how to do things within a language, there may be no need for many things. The very basic functionality has lots of black boxes underneath it that can be safely ignored or given as handwaving. Someone wondered if adding generators complicated the language. Yes, but also no. Creating a generator is one thing. Using it is trivial. Isn't it nice to be able to use almost any type or function as a generator and not care how the black box was made? True, it means when you really want it all at once, you might have to wrap a list() command around it or use it in some context that exercises the generator to completion. Now teaching one of umpteen ways to make a generator or convert an existing thing into a generator, is a tad more advanced. Using them, maybe not so much. As a reality, as long as machines get ever faster and with more memory, some bloat is tolerable. If a programming language has features rarely seen or used, you can still use it to teach. But people tend to try to show off and even make a virtue of doing more complex things. How many have been told that python allows something like this: a,b = b,a or 5 < x <= 10 The former can easily be done using a temporary variable and probably is done that way underneath. The latter might also be done a simpler way except you need to make sure x is only evaluated once. As it happens, the first example is of major importance because it pervades the language with things like a function returning a tuple. Last comment. Is it better to teach in a bay language, say showing how you can use turtles moving around to draw objects and then switch to even more complex and complete languages for final use or should you start with something you can grow into? Think starter houses where people move up when kids arrive (and when they can afford it) and maybe downsize after they leave. -----Original Message----- From: Tutor On Behalf Of Steven D'Aprano Sent: Thursday, November 22, 2018 1:06 AM To: tutor at python.org Subject: Re: [Tutor] origins bootstrapped. On Wed, Nov 21, 2018 at 11:31:59AM -0500, Avi Gross wrote: ... > Can I ask a question that I really want an opinion on? As a preface, I see > some think python as a formal language is being pushed by industry in > directions that may not meld as well for its use in other contexts like for > teaching students. I think there is always going to be tension between the needs of different users. Beginners need simplicity; expert, experienced programmers need power; both have very different ideas of what "readable code" means. I don't think Python is being pushed in any direction by "industry". It is evolving according to the needs of the programmers who use it, some of whom may work for some industry or another. > How much of that is due to it being a relative open and > free product? There are plenty of other applications that you pay for and > thus have to be responsive to the buyers to remain in business. Python has > many implementations including some freer than others. I don't know of any non-free (free as in beer, or free as in speech) implementations of Python. Can you elaborate? > Yet is has gone > through a bit of a bifurcation and many would like to see 2.X retained and > others wish everyone should migrate. Is there room for a smaller core > language that remains good for teaching purposes and that is small enough to > fit in a Rasberry pi, while other versions are of industrial strength? Do we > already sort of have some of that? Standard CPython is light enough to run on fairly low-powered devices, including Raspberry Pi. For an even smaller footprint, you can use Micropython, which will run on embedded devices, although ?Py does make some comprompises that means that it's not a fully compliant Python implementation. There are, or were, other small implementations: - Pippy, Python for Palm (probably unmaintained by now...) - Python for S60, for the Nokia S60 platform (likewise...) - Pythonce, for Windows CE (who still uses WinCE?) - PyMite for embedded devices - Python-iPod - Py4A and QPython (Android) - TinyPy - PyPad for the iPad - Pycorn, Python running on bare hardware with no OS > I was thinking of how many languages and environments have been looking at > working using parallelism. [...] > It definitely is worth doing but does everyone need it especially for > teaching an intro class? Who teaches threading and parallelization in introductory classes? From alan.gauld at yahoo.co.uk Thu Nov 22 19:54:14 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 23 Nov 2018 00:54:14 +0000 Subject: [Tutor] origins bootstrapped. In-Reply-To: <20181122060549.GD4319@ando.pearwood.info> References: <009101d481b7$b9c300f0$2d4902d0$@verizon.net> <20181122060549.GD4319@ando.pearwood.info> Message-ID: On 22/11/2018 06:05, Steven D'Aprano wrote: > I don't know of any non-free (free as in beer, or free as in speech) > implementations of Python. Can you elaborate? There are several commercial distributions (as opposed to implementations) of Python, that may be what Avi has in mind. Some of these are commercial IDEs that include python as part of an integrated bundle - I think Blackadder is one such - and others are just uber distros like Enthought Entropy(?) which is a "supported" distro for scientific work - rather like Anaconda. Others are in the Movie industry where it is either tied to a particular tool or again to a support arrangement. The implementations are the standard open source code but the distribution is paid for, with the value add either in the toolset, the packaging or the support. But maybe Avi means something different... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From breamoreboy at gmail.com Fri Nov 23 02:37:38 2018 From: breamoreboy at gmail.com (Mark Lawrence) Date: Fri, 23 Nov 2018 07:37:38 +0000 Subject: [Tutor] learning languages In-Reply-To: <003101d482ac$ad659b10$0830d130$@verizon.net> References: <003101d482ac$ad659b10$0830d130$@verizon.net> Message-ID: On 22/11/2018 21:45, Avi Gross wrote: [big snip] Please take this up on some other forum as I see nothing here that relates to the purpose of this list, i.e. teaching Python to beginners. TIA. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From avigross at verizon.net Thu Nov 22 20:33:02 2018 From: avigross at verizon.net (Avi Gross) Date: Thu, 22 Nov 2018 20:33:02 -0500 Subject: [Tutor] Born to be free Message-ID: <006501d482cc$79f44d60$6ddce820$@verizon.net> Alan, Yes, I meant that many things are effectively free these days. Some things are required to be distributed free by CopyLeft. But you can pay a nominal fee for say a CD of the software. You can pay for a bundle like you describe, perhaps including some consulting or warranties to keep you updated or who knows what? I have been using a slew of programs lately in work for someone else who gave me access to software they paid for under some license. I mean statistical tools that are sold or licensed and have been around for ages. But I find I can cobble together my own programs and often find packages for free to do some parts. But as noted, I often have to process data that came from sources like SAS, MPLUS, STATA, SPSS and others. I often have to return the results of the calculations back in those formats. I have not felt the urge to buy the software but the day may come. My goal is to use a variety of tools that include R and Python unless the easiest path is ... I am not in the market for buying or selling such things so I simply admit my lack of encyclopedic memory and experience. I suggested that based on my knowledge, to date, I gather Python is mostly available for free and this might impact companies that wish to bend it to their needs rather than paying someone to give them an expensive tool. Many languages and other shared goods have ISO committees or Standards Bodies that document it or regulate changes. In the past, I often found companies like AT&T and HP attending all kinds of such bodies to influence the direction they take or be informed where they are headed. Some people have a photographic memory while some have a pornographic memory. Anyone with both is beyond dangerous ? -----Original Message----- From: Tutor On Behalf Of Alan Gauld via Tutor Sent: Thursday, November 22, 2018 7:54 PM To: tutor at python.org Subject: Re: [Tutor] origins bootstrapped. On 22/11/2018 06:05, Steven D'Aprano wrote: > I don't know of any non-free (free as in beer, or free as in speech) > implementations of Python. Can you elaborate? There are several commercial distributions (as opposed to implementations) of Python, that may be what Avi has in mind. Some of these are commercial IDEs that include python as part of an integrated bundle - I think Blackadder is one such - and others are just uber distros like Enthought Entropy(?) which is a "supported" distro for scientific work - rather like Anaconda. Others are in the Movie industry where it is either tied to a particular tool or again to a support arrangement. The implementations are the standard open source code but the distribution is paid for, with the value add either in the toolset, the packaging or the support. But maybe Avi means something different... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From avigross at verizon.net Fri Nov 23 00:34:59 2018 From: avigross at verizon.net (Avi Gross) Date: Fri, 23 Nov 2018 00:34:59 -0500 Subject: [Tutor] A required question Message-ID: <000901d482ee$46dbe330$d493a990$@verizon.net> Just to be different, and perhaps return to the purpose of this group, I have a question. Is there some functionality available in Python that you could put in the beginning of a program so it aborts with a message if the version of R being run is not acceptable? Let me elaborate. There seem to be new features added as you move up. I mean 2.2, 2.3, ? 3.0 and so on. But there also seem to be features deprecated then removed. To make it crazier, some features are back-ported into later releases of 2.X while some features are activated only in you import the right things from __FUTURE__ . Add to that the fact that various modules may exist or not exist on your machine in your search PATH and they may import yet others, and it gets crazy. Even weirder is that you can probably download a missing file as the program runs and then import it and probably many other scenarios where you own program writes a module and then imports it! What I was thinking was the ability to do something like this: import ReChoir as require require.version(condition, before=True, after=False) require.modules(module list, recursive=True) require.os([?Eunuchs?, ?Windblows?]) require.functionality(?print3?) I hope you get the idea. Any one of the statements above might be made anywhere in your program but is best near the top of the main program perhaps after any imports from the dunder FUTURE. I am not designing it. I am giving a very silly example and also suggesting a possible scenario like this: if ! require( <> ): print(?Sorry, your system is too primitive to use this snazzy software.?) print(?In a moment, you will be re-directed to a dumber version written?) print(?stone age tools. Please upgrade sooooooooon.? print(?running program paleo.py for you with same arguments.? sleep(5) exit( os.system(<<>>) ) So in some cases, you quit with a message saying why you quit. In the example at the end, you may call alternate functionality, perhaps a shell-script rather than python, that does something for them. In an extreme case, the script would download a later version of Python as a sort of bootstrap. ? Yes, tons of holes in here and implementation details. For example, some features are only needed if the command line or data lead you down some path. No need to abort most of the time and not easy or even possible to see if you might need it. So is there anything interesting out there already, designed to not so much make python portable, but to prevent things from running and then dying in middle of something important. Yes, I know there is an assert command you can use and there are system variables you can query to see what version of Python you are running on what OS and so on. No doubt there are tools you can use to see if everything imported exists (maybe not the right file) somewhere in the path. The latter alone is a huge issue as there have been changes in how you import including some really odd ones where they broke old functionality. There may come a day when 2.X is as extinct as polio (stubbornly hanging in there as some refuse to vaccinate) but I do not foresee the march of changes slowing in 3.x, let alone 4.x^2 and beyond. Such a tool might make it easier for people to use new features with less fear. One more thought. Such a function set might simply try new features inside a ?try? and catch the error so they can gracefully annoy the user with a snide remark and even tell them what is missing. For example a line with a := in it should fail everywhere now but might work later. An f?string? is not that old. A line using open() in an iterator context would fail somewhere back in 2.x. In theory, you can make a list of testable new features and even give them names you can ask for such as the ?print3? above. Why do I ask? I had a great day today except it followed a horrible day yesterday. I did a bunch of work in R but the main point could just as easily happen in Python. I wanted various packages to do various things. Some had broken for me in the last year or so and been replaced with something that refused to work. In English, it was supposed to take a linear model generated from data and calculate some predicted info from it and make a nice graph with lines showing the impact of another variable at the mean and +/- a standard deviation. The problem is that I had the latest version of R installed but some of the many packages required were not yet available or their dependencies were either not available or of the wrong date. This happens with Python modules too. With lots of research, I loaded some things from alternate places and backed up to older versions and then forward and eventually had a brainstorm of figuring out what to feed the needed function so it worked. But a second problem remained with a different package I had tried, and a variant of the same problem in the current package for other functionality. The complaint was that the regression was expecting a vector at one point within a data.frame but say a one-dimensional matrix. Weird as I did not create it that way. Traced the problem to the rescaling function an fixed that by resetting it to a numeric vector afterward and suddenly all kinds of things worked. Then I had the usual other problem of saving data in yet another format and finding it hard to use package after package because it was not available for my version. Persistence worked, though. And, yes, my next plan was to move into finding some similar functionality in Python. Why break when you can brake and after a break can bend and find a plan B? This led to some of the above thoughts. Wouldn?t it be nice to state what must be there in front of a program and be sure it was before doing the rest? From alan.gauld at yahoo.co.uk Fri Nov 23 10:35:28 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 23 Nov 2018 15:35:28 +0000 Subject: [Tutor] A required question In-Reply-To: <000901d482ee$46dbe330$d493a990$@verizon.net> References: <000901d482ee$46dbe330$d493a990$@verizon.net> Message-ID: On 23/11/2018 05:34, Avi Gross wrote: > Just to be different, and perhaps return to the purpose of this group, Actually I think the higher level debate of Python's direction as a teaching language is entirely appropriate for this group. It kind of defines the group and and its current and future role. Whereas... > What I was thinking was the ability to do something like this: > > import ReChoir as require > > require.version(condition, before=True, after=False) > require.modules(module list, recursive=True) > require.os([?Eunuchs?, ?Windblows?]) > require.functionality(?print3?) I can see the logic but suspect discussion of new features is probably the preserve of the main Python list. If you can get traction there somebody might actually go ahead and write one! On the tutor list such matters are usually adequately covered by virtual environments etc. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From david at graniteweb.com Fri Nov 23 21:27:35 2018 From: david at graniteweb.com (David Rock) Date: Fri, 23 Nov 2018 20:27:35 -0600 Subject: [Tutor] A required question In-Reply-To: References: <000901d482ee$46dbe330$d493a990$@verizon.net> Message-ID: <449AD65C-DD2B-45BA-953D-020A2B92D323@graniteweb.com> > On Nov 23, 2018, at 09:35, Alan Gauld via Tutor wrote: > > On 23/11/2018 05:34, Avi Gross wrote: >> What I was thinking was the ability to do something like this: >> >> import ReChoir as require >> >> require.version(condition, before=True, after=False) >> require.modules(module list, recursive=True) >> require.os([?Eunuchs?, ?Windblows?]) >> require.functionality(?print3?) > > I can see the logic but suspect discussion of new features is > probably the preserve of the main Python list. If you can get > traction there somebody might actually go ahead and write one! discussion of a ?require? library probably isn?t necessary. It?s pretty straightforward to include the logic using existing methods. For the version of python, test against sys.version_info For the modules, put your import calls in a try block and handle exceptions For the OS version, test against os.name or sys.platform The last one, ?functionality,? is a bit vague. Probably another candidate for a try block. ? David Rock david at graniteweb.com From avigross at verizon.net Fri Nov 23 21:29:55 2018 From: avigross at verizon.net (Avi Gross) Date: Fri, 23 Nov 2018 21:29:55 -0500 Subject: [Tutor] A required question In-Reply-To: References: <000901d482ee$46dbe330$d493a990$@verizon.net> Message-ID: <008c01d4839d$9675be70$c3613b50$@verizon.net> Alan, I appreciate the feedback. I am guilty of starting with what was meant to be an example that wandered off into something that looks like I am developing an actual set of methods. But on the main topic, I wondered if the language or user community had already built some FUNCTIONALITY along these lines.I was thinking, as usual, across languages and environments so let me explain. The import statement in R is something like: library("ggplot2") When it fails, it generates a serious error. A sister routine is available with a similar call that instead returns True/False. It happens to be called: require("ggplot2") Since it return a TRUE/FALSE value (R uses full uppercase for the pythonic True/False) you can write a function that tests to see if the package was loaded and if not, tries to install it and load it. if (! require(GARBARGE)) { install.packages("GARBARGE") library(GARBARGE) } Of course since there is no such package, I still get an error. But if it was something that the user on that machine had not loaded but needed, it might pause and lock and load and be able to continue. BTW, R sometimes requires strings to be quoted but sometimes because of very lazy evaluation, you can skip the quotes and the function can take the naked value off the argument stack before it is evaluated, turn it into a quoted string, and off you go. The install.packages command does not do this but the other two do. Back to Python. I know how to check if a module is already loaded into a program. But I am not aware of how you can check if it can be loaded except by trying to load it. That may be enough. <> print("-=" * 20) print("Trying to import non-existent module garbarge") try: import garbarge except: print("failed") else: print("I am drowning in garbarge!!!") print("=-" * 20) print("Getting ready to process numbers with module numpy.") try: import numpy except: print("drat! failed again") else: print("I can calculate on numpy!") print("-=" * 20) <> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= Trying to import non-existent module garbarge failed =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- Getting ready to process numbers with module numpy. I can calculate on numpy! -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= <> In that code, I suppose, you could insert an appropriate call to load the failed module and try again. I could design and write a function like that BUT I am going to behave ? Hope that helps explain where I am coming from. Since I can see how to write such tools, I have to suspect it has been done, not just once, but a seemingly infinite number of times. And note actually importing a module to test if it can be can have side effects. So, to add value, I am going to partially answer my own question. There are modules that help. import imp >>> imp.find_module('eggs') Traceback (most recent call last): File "", line 1, in imp.find_module('eggs') File "C:\Users\avid2016\AppData\Local\Programs\Python\Python37-32\lib\imp.py", line 297, in find_module raise ImportError(_ERR_MSG.format(name), name=name) ImportError: No module named 'eggs' >>> imp.find_module('pandas') (None, 'C:\\Users\\avid2016\\AppData\\Roaming\\Python\\Python37\\site-packages\\pandas', ('', '', 5)) So I took a quick try to see if I could slightly replicate an attempt to load. No guarantees this exact code works anywhere else: I hardc-oded in "pandas" as the first try: modulosity = "pandas" print(" importing " + modulosity + "By hook or by crook.") import imp var = imp.find_module(modulosity) if var: print ("FOUND: " + modulosity + " at " + str(var)) else: print("Module needs loading:" + modulosity) import os os.system("python -m pip install " + modulosity) exec(f"import {modulosity}") Output: importing pandasBy hook or by crook. Warning (from warnings module): File "C:/Users/avid2016/AppData/Local/Programs/Python/Python37-32/temp.py", line 28 import imp DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses FOUND: pandas at (None, 'C:\\Users\\avid2016\\AppData\\Roaming\\Python\\Python37\\site-packages\\pandas', ('', '', 5)) Now replacing pandas by non-existent koala: importing koalaBy hook or by crook. Warning (from warnings module): File "C:/Users/avid2016/AppData/Local/Programs/Python/Python37-32/temp.py", line 28 import imp DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses Traceback (most recent call last): File "C:/Users/avid2016/AppData/Local/Programs/Python/Python37-32/temp.py", line 29, in var = imp.find_module(modulosity) File "C:\Users\avid2016\AppData\Local\Programs\Python\Python37-32\lib\imp.py", line 297, in find_module raise ImportError(_ERR_MSG.format(name), name=name) ImportError: No module named 'koala' Amazingly it failed! Calling it a night as I find errors even after I rewrite it like this; modulosity = "SimPy" print(" importing " + modulosity + "By hook or by crook.") import imp try: var = imp.find_module(modulosity) print ("FOUND: " + modulosity + " at " + str(var)) except: print("NOT FOUND") print("Module needs loading: " + modulosity) import os os.system("python -m pip install " + modulosity) exec(f"import {modulosity}") I am sure something is doable and can be made into a proper function. Yes, I cheated by using exec. But it is now after Spanksgiving. -----Original Message----- From: Tutor On Behalf Of Alan Gauld via Tutor Sent: Friday, November 23, 2018 10:35 AM To: tutor at python.org Subject: Re: [Tutor] A required question On 23/11/2018 05:34, Avi Gross wrote: > Just to be different, and perhaps return to the purpose of this group, Actually I think the higher level debate of Python's direction as a teaching language is entirely appropriate for this group. It kind of defines the group and and its current and future role. Whereas... > What I was thinking was the ability to do something like this: > > import ReChoir as require > > require.version(condition, before=True, after=False) > require.modules(module list, recursive=True) require.os([?Eunuchs?, > ?Windblows?]) > require.functionality(?print3?) I can see the logic but suspect discussion of new features is probably the preserve of the main Python list. If you can get traction there somebody might actually go ahead and write one! On the tutor list such matters are usually adequately covered by virtual environments etc. -- Alan G From avigross at verizon.net Fri Nov 23 22:48:06 2018 From: avigross at verizon.net (Avi Gross) Date: Fri, 23 Nov 2018 22:48:06 -0500 Subject: [Tutor] A required question In-Reply-To: <449AD65C-DD2B-45BA-953D-020A2B92D323@graniteweb.com> References: <000901d482ee$46dbe330$d493a990$@verizon.net> <449AD65C-DD2B-45BA-953D-020A2B92D323@graniteweb.com> Message-ID: <009b01d483a8$82db9500$8892bf00$@verizon.net> David, As I suspected. Yes, I am aware how to do those things. Just wondered if anyone automated the process so a fairly simple interface worked. I am dropping the request. Avi -----Original Message----- From: Tutor On Behalf Of David Rock Sent: Friday, November 23, 2018 9:28 PM To: Tutor Python Subject: Re: [Tutor] A required question > On Nov 23, 2018, at 09:35, Alan Gauld via Tutor wrote: > > On 23/11/2018 05:34, Avi Gross wrote: >> What I was thinking was the ability to do something like this: >> >> import ReChoir as require >> >> require.version(condition, before=True, after=False) >> require.modules(module list, recursive=True) require.os([?Eunuchs?, >> ?Windblows?]) >> require.functionality(?print3?) > > I can see the logic but suspect discussion of new features is probably > the preserve of the main Python list. If you can get traction there > somebody might actually go ahead and write one! discussion of a ?require? library probably isn?t necessary. It?s pretty straightforward to include the logic using existing methods. For the version of python, test against sys.version_info For the modules, put your import calls in a try block and handle exceptions For the OS version, test against os.name or sys.platform The last one, ?functionality,? is a bit vague. Probably another candidate for a try block. ? David Rock david at graniteweb.com _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From brian0203ngari at gmail.com Sat Nov 24 15:37:45 2018 From: brian0203ngari at gmail.com (Brian Ngari) Date: Sat, 24 Nov 2018 15:37:45 -0500 Subject: [Tutor] Help.Please! Message-ID: Hi there, I'm struggling mightily on this problem. Write a program that determines if you need an oil change. This program should determine if your car needs an oil change. Assume you change your oil every 7,500 miles and you want to change the oil if you?re within 500 miles of that number. Please prompt the user for the current mileage recorded on the car and also for the mileage recorded on the car at the time of the last oil change. Then, please let the user know if it?s time to change the oil. Be sure to check for invalid entries (e.g., if the mileage at the time of the last oil change is greater than the current mileage) and, if there are invalid entries, please continue to prompt the user. Here is my code: input('Enter the number of miles') x=int(input('Enter the last milage you went for an oil change.')) if x>5000: print('You need an oil change.') else: x<5000 print('No oil change.') I'm getting two different answers as outputs. I don't know why. thanks for your help. From dimitarxivanov at gmail.com Sat Nov 24 11:08:32 2018 From: dimitarxivanov at gmail.com (Dimitar Ivanov) Date: Sat, 24 Nov 2018 16:08:32 +0000 Subject: [Tutor] [Python 3] Threads status, join() and Semaphore queue In-Reply-To: <20181120072223.GA94457@cskk.homeip.net> References: <20181120072223.GA94457@cskk.homeip.net> Message-ID: Hi Cameron, Massive apologies for the delayed answer! Your explanation definitely clears up quite a bit of my misunderstanding, thank you for that! There was a reason why I shy away from using Queue, but for the life of me I can't remember right now what that reason was. I will have to modify my code using your example and give it another try, I will make sure to let you know if I run into any issues or additional questions. :) Regards, Dimitar On Tue, 20 Nov 2018 at 08:39, Cameron Simpson wrote: > On 19Nov2018 23:52, Dimitar Ivanov wrote: > >I'm having a hard time getting my head around threads so I was hoping > >someone who has better understanding of their underlying functionality > >could lend me a helping hand, in particular how threads work with each > >other when using thread.join() and Semaphore set with maximum value. I'll > >try to keep it as clear and concise as possible, but please don't hesitate > >to ask if anything about my approach is unclear or, frankly, awful. > > > >I'm writing a script that performs a couple of I/O operations and CLI > >commands for each element in a list of IDs. The whole process takes a > while > >and may vary based on the ID, hence the threading approach sounded like > the > >best fit since next ID can start once space has freed up. I'm parsing an > >extract of my code below and will explain what I can't properly understand > >underneath. > > I'm just going to scatter a few random remarks about your code in here > with your listing before addressing your other queries lower down... > > >file1.py > >--------- > >ids = [] > >threadsPool = [] > >for id in ids: > > The name "id" is unfortunate as it conflicts with the id() builtin > function. Maybe "element_id"? Wordier, but also more clear. > > > thread = threading.Thread(target=file2.runStuff, name=str(id), args=(id, > >)) > > threadsPool.append(thread) > >for thread in threadsPool: > > thread.start() > > You could start each thread right after creating it if you wished. > > >for thread in threadsPool: > > print(thread.enumerate()) > > "enumerate" is a function from the threading module, not a method of a > Thread. So try: > > print(threading.enumerate()) > > Frankly I'm surprised that "thread.enumerate" works at all. > > > print("Queuing thread" + str(thread)) > > thread.join() > > > >file2.py > >---------- > >queue = threading.Semaphore(2) > > I'd be disinclined to call this a "queue", which usually implies a FIFO > list of some variety: put things onto it, and pull things off it, > usually first in first off. Maybe just "sem" or "thread_capacity" or > something? > > >def runStuff(id): > > queue.acquire() > > print("Lock acquired for " + str(id)) > > file3.doMoreStuff() > > file4.evenMoreStuff() > > queue.release() > > > >Onto my confusion - as long as I don't try to print information about the > >thread that's being queued or the total amount of threads using > >.enumerate(), the script is working absolutely flawlessly, each thread > that > >doesn't have a lock is waiting until it acquires it and then moves on. I > >decided it'd be nice to be able to provide more information about which > >thread starts next and how many threads are active right now (each can > take > >a different amount of time), however, when I tried to do that, my log was > >showing me some pretty funky output which at first made me believe I've > >messed up all my threads, example: > > > > > ><< 2018-11-19 15:01:38,094 file2 [ID09] INFO - Lock acquired for > >ID09 <---- this is from file2.py > >------ some time later and other logs in here --------- > >[<_MainThread(MainThread, started 140431033562880)>, >140430614177536)>] <---- output from thread.enumerate(), file1.py > ><< 2018-11-19 15:01:38,103 file1 [MainThread] DEBUG - Queuing thread - > > <---- output from print() right > >after thread.enumerate() > > > >After some head scratching, I believe I've finally tracked down the reason > >for my confusion: > > > >The .start() loop starts the threads and the first 2 acquire a lock > >immediately and start running, later on the .join() queue puts the rest in > >waiting for lock, that's fine, what I didn't realize, of course, is that > >the .join() loop goes through threads that have already been instantly > >kicked off by the .start() loop (the first 2 threads since Semaphore > allows > >2 locks) and then my print in that loop is telling me that those threads > >are being queued, except they aren't since they are already running, it's > >just my text is telling me that, since I wasn't smart enough to realize > >what's about to happen, as seen below: > > > ><< 2018-11-19 15:01:33,094 file1.py [MainThread] DEBUG - Queuing thread - > > <--- makes it clear the thread has > >already even finished > > Yes. The .join() has NO EFFECT on the Thread itself: it doesn't start it > or stop it. It just waits for the Thread to complete. So yes, your log > message is misleading. > > >Which finally gets me to my cry for help - I know I can't modify the > >threadsPool list to remove the threads already created on the fly, so I > can > >have only the ones pending to be queued in the 2nd loop, but for the life > >of me I can't think of a proper way to try and extract some information > >about what threads are still going (or rather, have finished since > >thread.enumerate() shows both running and queued). > > Well, what you'd probably _like_ is a way to be told about each Thread > as it completes, and report them then. Which you can do using a Queue, > getting each Thread to report its completion to the Queue as it happens. > > Untested example: > > from queue import Queue > q = Queue() > threadsPool = [] > for id in ids: > thread = threading.Thread(target=file2.runStuff, name=str(id), > args=(id, q)) > threadsPool.append(thread) > > and modify runStuff thus: > > def runStuff(id, q): > sem.aquire() > ... > sem.release() > q.put(id) > > After the threads are started, collect completed ids: > > for count in range(len(ids)): > id = q.get() > print("completed work on id %r" % (id,)) > > You'll notice no .join() there. Getting the id off the queue "q" implies > that the Thread has completed. > > >I have the feeling I'm using a very wrong approach in trying to extract > >that information in the .join() loop, since it only goes back to it once a > >thread has finished, but at the same time it feels like the perfect > timing. > > You're collecting specific threads. If other threads complete earlier > than that specific thread, they don't get reported immediately. You're > reporting threads in the order you made them, not in the order they > complete. Using a Queue and not worrying about the threads themselves > lets you gets ids as they're done, in whatever order. > > >And just in case you are wondering why I have my threads starting in > >file1.py and my Semaphore queue in file2.py, it's because I wanted to > split > >the runStuff(id) function in a separate module due to its length. I don't > >know if it's a good way to do it, but thankfully the Python interpreter is > >smart enough to see through my ignorance. > > I usually split modules based on function, not size. Put related things > in the same module. Often in classes, but let us not go there yet. > > If you want to get a little funky, separate the runStuff code which > works on the id from the control code (the semaphore use). You could > then run the queue collection in its own thread. Have the main control > code also manage the semaphore: > > q = Queue() > threadsPool = [] > for id in ids: > thread = threading.Thread(target=file2.runStuff, name=str(id), > args=(id, q)) > threadsPool.append(thread) > > collector = Thread(target=collect, args=(q, ids)) > collector.start() > > for thread in threadPool: > sem.acquire() > thread.start() > > # wait for collection to complete > collector.join() > > def collect(q, ids): > for count in range(len(ids)): > id = q.get() > sem.release() > > so that you acquire the semaphore before starting each thread, and > release the semaphore as threads complete and report their id values. > > Because these things need to happen in parallel (acquire, start versus > q.get, release) you run the collector in its own thread. > > Does this clear anything up? > > Cheers, > Cameron Simpson > From stephen.m.smith at comcast.net Sat Nov 24 16:41:16 2018 From: stephen.m.smith at comcast.net (stephen.m.smith at comcast.net) Date: Sat, 24 Nov 2018 16:41:16 -0500 Subject: [Tutor] FW: Python Selenium Message-ID: <00c301d4843e$6e267580$4a736080$@comcast.net> I am working with a site that allows you to make reservations for different times of the day. The HTML (controlled by someone else) does not include specific names for each time ? the HTML is very similar. With help from this group, I have figured out how to locate an element using an xpath with the time spelled out (this way the module can substitute a time based on user input). Here is the line of code (for 7:35): br.find_element_by_xpath("//div[contains(.,'7:35')]") Here is where I need assistance: I need to access an element associated with that element to see if it is blank or occupied. I have included the HTML for two different time slots. The first (7:30) is not occupied as shown by the 4 class statements at the bottom with the ><. The second, for 7:35 is partially occupied ? the names are shown as is the slot count information. I am trying to figure out a clever way to look down in the HTML once I have found the element to see if it is partially occupied or not. Any help with that would be appreciated. From alan.gauld at yahoo.co.uk Sat Nov 24 19:09:21 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 25 Nov 2018 00:09:21 +0000 Subject: [Tutor] Help.Please! In-Reply-To: References: Message-ID: On 24/11/2018 20:37, Brian Ngari wrote: > miles of that number. Please prompt the user for the current mileage > recorded on the car and also for the mileage recorded on the car at the > time of the last oil change. Then, please let the user know if it?s time to > change the oil. You do some of this but you don't check if they need an oil change. > Be sure to check for invalid entries (e.g., if the mileage... > and, if there are invalid entries, please continue to prompt the user. And you don't do this. You need a loop somewhere... I suggest before worrying about the oil change code that you get the input bit working, just print the two values initially. Once you can correctly read two valid values move on to using them to check the oil. > input('Enter the number of miles') You don't store this value anywhere > x=int(input('Enter the last milage you went for an oil change.')) > > if x>5000: > print('You need an oil change.') This claims you need an oil change if the mileage at the last change was >5000. Even if I haven't driven any miles at all since then. Something wrong. You need to use both the values that you read from the user. > else: > x<5000 This doesn't make much sense. You compare x to 5000 but don't do anything with the result. I'm not even sure what you are trying to do here. > print('No oil change.') Remember indentation is all important in Python. Here you always print the message regar5dless of what happened further up. > I'm getting two different answers as outputs. I don't know why. Probably that last indentation issue to blame for that. More seriously you are not doing what the homework assignment asks you to do. Read it again and see my comments above. Have another go and if still stuck come back to us. -- 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 Sat Nov 24 20:19:35 2018 From: cs at cskk.id.au (Cameron Simpson) Date: Sun, 25 Nov 2018 12:19:35 +1100 Subject: [Tutor] [Python 3] Threads status, join() and Semaphore queue In-Reply-To: References: Message-ID: <20181125011935.GA96108@cskk.homeip.net> On 24Nov2018 16:08, Dimitar Ivanov wrote: >Your explanation definitely clears up quite a bit of my >misunderstanding, >thank you for that! > >There was a reason why I shy away from using Queue, but for the life of me >I can't remember right now what that reason was. I will have to modify my >code using your example and give it another try, I will make sure to let >you know if I run into any issues or additional questions. :) Questions are welcome. Cheers, Cameron Simpson From srinivasan.rns at gmail.com Sun Nov 25 12:13:10 2018 From: srinivasan.rns at gmail.com (srinivasan) Date: Sun, 25 Nov 2018 22:43:10 +0530 Subject: [Tutor] Issue in using "subprocess.Popen" for parsing the command output Message-ID: Dear Python Experts Team, As am newbie still learning the python syntax from past 2 weeks, Excuse me, If this might be silly question, As I am trying to execute shell command (ie, nmcli) using "subprocess.Popen". 1. Am trying to improve the below code with "try" and "exception", could you please help me how "try" and "exception" can be used on the below code snippet. I hope in my code with try and exception, seems to be a bug. 2. As I am trying to execute shell commands using "subprocess.Popen", I am trying to parse the strings output by "cmd = "nmcli device wifi connect '%s' password '%s'" % (ssid, pw)" command as below, but it is throwing the below error as shown in "Output error logs:" Could you please let me to fix the bug in the below code snippet, where I need the collect the strings of the command output and later how to be parsed after execution of the command for example, I need to parse the string "Connection activation failed: " and compare it with the command output, could you please help me how this can be achieved? *Command:* :~$ nmcli device wifi connect 'Apartment 18' password '40672958689850014685abcdf' Error: Connection activation failed: (7) Secrets were required, but not provided. :~$ *Code:* *import sys* *import subprocess* *interface = "wlan0"* *def main(ssid, pw):* * # cmd = "nmcli device wifi connect '%s' password '%s'" % (ssid, pw)* * #* * # proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, universal_newlines=True)* * # stdout, stderr = proc.communicate()* * # retcode = proc.returncode* * #* * # print("printing stdout!!!!!!!!!!", stdout)* * # print("printing retcode!!!!!!!!!!", retcode)* * try:* * cmd = "nmcli device wifi connect '%s' password '%s'" % (ssid, pw)* * proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, universal_newlines=True)* * stdout, stderr = proc.communicate()* * retcode = proc.returncode* * print("printing stdout!!!!!!!!!!", stdout)* * print("printing retcode!!!!!!!!!!", retcode)* * except subprocess.CalledProcessError as e:* * s = """While executing '{}' something went wrong.* * Return code == '{}'* * Return output:\n'{}'* * """.format(cmd, e.returncode, e.output, shell=enable_shell)* * raise AssertionError(s)* * return proc.strip().decode("utf-8")* *main("Apartment 18", "40672958689850014685")* *Output error logs:* /home/srinivasan/Downloads/wifidisconnectissuenov23/qa/venv/bin/python /home/srinivasan/Downloads/wifidisconnectissuenov23/qa/test_library/test4.py Traceback (most recent call last): File "/home/srinivasan/Downloads/wifidisconnectissuenov23/qa/test_library/test4.py", line 38, in printing stdout!!!!!!!!!! printing retcode!!!!!!!!!! 0 main("Apartment 18", "40672958689850014685") File "/home/srinivasan/Downloads/wifidisconnectissuenov23/qa/test_library/test4.py", line 36, in main return proc.strip().decode("utf-8") AttributeError: 'Popen' object has no attribute 'strip' Process finished with exit code 1 Kindly do the needful as am stuck with this issue from 2 days Many Thanks in advance, From srinivasan.rns at gmail.com Sun Nov 25 12:58:43 2018 From: srinivasan.rns at gmail.com (srinivasan) Date: Sun, 25 Nov 2018 23:28:43 +0530 Subject: [Tutor] Issue in using "subprocess.Popen" for parsing the command output In-Reply-To: References: <1ba59bac-4791-760a-a9f9-3ee0d28d5ea6@mrabarnett.plus.com> Message-ID: Even only with "*proc.decode("utf-8")"* in the above code still it seems to throw the error #return proc.strip().decode("utf-8") #return proc.decode("utf-8").strip() * return proc.decode("utf-8")* Error: /home/srinivasan/Downloads/wifidisconnectissuenov23_homework/venv/bin/python /home/srinivasan/Downloads/wifidisconnectissuenov23_homework/qa/test_library/test4.py printing stdout!!!!!!!!!! printing retcode!!!!!!!!!! 0 Traceback (most recent call last): File "/home/srinivasan/Downloads/wifidisconnectissuenov23_homework/qa/test_library/test4.py", line 31, in main("Apartment 18", "40672958689850014685") * File "/home/srinivasan/Downloads/wifidisconnectissuenov23_homework/qa/test_library/test4.py", line 29, in main* * return proc.decode("utf-8")* *AttributeError: 'Popen' object has no attribute 'decode'* Process finished with exit code 1 On Sun, Nov 25, 2018 at 11:24 PM srinivasan wrote: > Hope now I have changed on the string output as below, could you please > correct me if am still wrong? > > import sys > import subprocess > > interface = "wlan0" > > > def main(ssid, pw): > > try: > cmd = "nmcli device wifi connect '%s' password '%s'" % (ssid, pw) > > proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, > stderr=subprocess.PIPE, shell=True, universal_newlines=True) > stdout, stderr = proc.communicate() > retcode = proc.returncode > > print("printing stdout!!!!!!!!!!", stdout) > print("printing retcode!!!!!!!!!!", retcode) > > except subprocess.CalledProcessError as e: > s = """While executing '{}' something went wrong. > Return code == '{}' > Return output:\n'{}' > """.format(cmd, e.returncode, e.output, shell=True) > raise AssertionError(s) > > #return proc.strip().decode("utf-8") > * return proc.decode("utf-8").strip()* > > main("Apartment 18", "40672958689850014685ad") > > Error: > > /home/srinivasan/Downloads/wifidisconnectissuenov23_homework/venv/bin/python > /home/srinivasan/Downloads/wifidisconnectissuenov23_homework/qa/test_library/test4.py > Traceback (most recent call last): > File > "/home/srinivasan/Downloads/wifidisconnectissuenov23_homework/qa/test_library/test4.py", > line 30, in > main("Apartment 18", "40672958689850014685") > * File > "/home/srinivasan/Downloads/wifidisconnectissuenov23_homework/qa/test_library/test4.py", > line 28, in main* > * return proc.decode("utf-8").strip()* > *AttributeError: 'Popen' object has no attribute 'decode'* > printing stdout!!!!!!!!!! > printing retcode!!!!!!!!!! 0 > > Process finished with exit code 1 > > > > On Sun, Nov 25, 2018 at 11:19 PM MRAB wrote: > >> On 2018-11-25 17:13, srinivasan wrote: >> > Dear Python Experts Team, >> > >> > As am newbie still learning the python syntax from past 2 weeks, Excuse >> me, >> > If this might be silly question, As I am trying to execute shell command >> > (ie, nmcli) using "subprocess.Popen". >> > >> > 1. Am trying to improve the below code with "try" and "exception", could >> > you please help me how "try" and "exception" can be used on the below >> code >> > snippet. I hope in my code with try and exception, seems to be a bug. >> > >> > 2. As I am trying to execute shell commands using "subprocess.Popen", I >> am >> > trying to parse the strings output by "cmd = "nmcli device wifi connect >> > '%s' password '%s'" % (ssid, pw)" command as below, but it is throwing >> the >> > below error as shown in "Output error logs:" >> > >> > Could you please let me to fix the bug in the below code snippet, >> where I >> > need the collect the strings of the command output and later how to be >> > parsed after execution of the command for example, I need to parse the >> > string "Connection activation failed: " and compare it with the command >> > output, could you please help me how this can be achieved? >> > >> > *Command:* >> > :~$ nmcli device wifi connect 'Apartment 18' password >> > '40672958689850014685abcdf' >> > Error: Connection activation failed: (7) Secrets were required, but not >> > provided. >> > :~$ >> > >> > *Code:* >> > *import sys* >> > *import subprocess* >> > >> > *interface = "wlan0"* >> > >> > >> > *def main(ssid, pw):* >> > >> > * # cmd = "nmcli device wifi connect '%s' password '%s'" % (ssid, >> pw)* >> > * #* >> > * # proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, >> > stderr=subprocess.PIPE, shell=True, universal_newlines=True)* >> > * # stdout, stderr = proc.communicate()* >> > * # retcode = proc.returncode* >> > * #* >> > * # print("printing stdout!!!!!!!!!!", stdout)* >> > * # print("printing retcode!!!!!!!!!!", retcode)* >> > >> > * try:* >> > * cmd = "nmcli device wifi connect '%s' password '%s'" % (ssid, >> pw)* >> > >> > * proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, >> > stderr=subprocess.PIPE, shell=True, universal_newlines=True)* >> > * stdout, stderr = proc.communicate()* >> > * retcode = proc.returncode* >> > >> > * print("printing stdout!!!!!!!!!!", stdout)* >> > * print("printing retcode!!!!!!!!!!", retcode)* >> > >> > * except subprocess.CalledProcessError as e:* >> > * s = """While executing '{}' something went wrong.* >> > * Return code == '{}'* >> > * Return output:\n'{}'* >> > * """.format(cmd, e.returncode, e.output, >> > shell=enable_shell)* >> > * raise AssertionError(s)* >> > >> > * return proc.strip().decode("utf-8")* >> > >> > *main("Apartment 18", "40672958689850014685")* >> > >> > *Output error logs:* >> > >> > /home/srinivasan/Downloads/wifidisconnectissuenov23/qa/venv/bin/python >> > >> /home/srinivasan/Downloads/wifidisconnectissuenov23/qa/test_library/test4.py >> > Traceback (most recent call last): >> > File >> > >> "/home/srinivasan/Downloads/wifidisconnectissuenov23/qa/test_library/test4.py", >> > line 38, in >> > printing stdout!!!!!!!!!! >> > printing retcode!!!!!!!!!! 0 >> > main("Apartment 18", "40672958689850014685") >> > File >> > >> "/home/srinivasan/Downloads/wifidisconnectissuenov23/qa/test_library/test4.py", >> > line 36, in main >> > return proc.strip().decode("utf-8") >> > AttributeError: 'Popen' object has no attribute 'strip' >> > >> > Process finished with exit code 1 >> > >> > Kindly do the needful as am stuck with this issue from 2 days >> > >> > Many Thanks in advance, >> > >> Look carefully at the traceback. It's actually telling you what the >> problem is. >> >> You're trying to do .strip() on proc, but proc is the process itself. >> >> Instead, what you want is to do .strip() on the string that it output. >> -- >> https://mail.python.org/mailman/listinfo/python-list >> > From mats at wichmann.us Sun Nov 25 14:09:48 2018 From: mats at wichmann.us (Mats Wichmann) Date: Sun, 25 Nov 2018 12:09:48 -0700 Subject: [Tutor] Issue in using "subprocess.Popen" for parsing the command output In-Reply-To: References: <1ba59bac-4791-760a-a9f9-3ee0d28d5ea6@mrabarnett.plus.com> Message-ID: <355a38b5-b761-8376-3a31-eaf23fa7f3b9@wichmann.us> On 11/25/18 10:58 AM, srinivasan wrote: > Even only with "*proc.decode("utf-8")"* in the above code still it seems to > throw the error > > #return proc.strip().decode("utf-8") > #return proc.decode("utf-8").strip() > * return proc.decode("utf-8")* > > Error: > /home/srinivasan/Downloads/wifidisconnectissuenov23_homework/venv/bin/python > /home/srinivasan/Downloads/wifidisconnectissuenov23_homework/qa/test_library/test4.py > printing stdout!!!!!!!!!! > printing retcode!!!!!!!!!! 0 > Traceback (most recent call last): > File > "/home/srinivasan/Downloads/wifidisconnectissuenov23_homework/qa/test_library/test4.py", > line 31, in > main("Apartment 18", "40672958689850014685") > * File > "/home/srinivasan/Downloads/wifidisconnectissuenov23_homework/qa/test_library/test4.py", > line 29, in main* > * return proc.decode("utf-8")* > *AttributeError: 'Popen' object has no attribute 'decode'* the error tells you what is wrong. proc is a Popen object, and does not have a decode method. simple enough. strings have a decode method. You're decoding the wrong thing, you need to decode what you get back from communicating with the Popen object, not the Popen object itself. stdin is the string (or more likely, in Python 3, a bytes object) you got back from calling communicate(). decode that. From steve at pearwood.info Sun Nov 25 17:03:04 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 26 Nov 2018 09:03:04 +1100 Subject: [Tutor] Issue in using "subprocess.Popen" for parsing the command output In-Reply-To: References: Message-ID: <20181125220303.GI4319@ando.pearwood.info> I think you are sending email using Gmail. If so, there is a command in Gmail to send only PLAIN TEXT with no added formatting. Please use it. Your code at the moment has extra asterisks * added at the beginning and end of each line. More comments below. On Sun, Nov 25, 2018 at 10:43:10PM +0530, srinivasan wrote: > 1. Am trying to improve the below code with "try" and "exception", could > you please help me how "try" and "exception" can be used on the below code > snippet. I hope in my code with try and exception, seems to be a bug. As a beginner, you should normally not use try...except to report errors. You should learn how to diagnose errors by reading the traceback. Covering up the traceback with try...except makes debugging harder. Your use here: > * try:* > * cmd = "nmcli device wifi connect '%s' password '%s'" % (ssid, pw)* > * proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, universal_newlines=True)* > * stdout, stderr = proc.communicate()* > * retcode = proc.returncode* > * print("printing stdout!!!!!!!!!!", stdout)* > * print("printing retcode!!!!!!!!!!", retcode)* > * except subprocess.CalledProcessError as e:* > * s = """While executing '{}' something went wrong.* > * Return code == '{}'* > * Return output:\n'{}'* > * """.format(cmd, e.returncode, e.output, shell=enable_shell)* > * raise AssertionError(s)* doesn't seem right to me. The string.format() method doesn't take a shell=enable_shell agument, so I expect that line s = """...""".format(cmd, ..., shell=enable_shell) to fail. But even if it doesn't fail, the next line: raise AssertionError(s) is an abuse of exceptions. The failure here is *not* an assertion, and you shouldn't use AssertionError. You wouldn't use TypeError or UnicodeEncodeError or AttributeError. "AssertionError" should not be used for "some arbitrary error". There are almost no reasons to manually raise AssertionError, except perhaps in test frameworks like unittest. Normally you should only get an AssertionError from the "assert" command: https://import-that.dreamwidth.org/676.html My opinion is, you should remove that try...except altogether. I don't think that it helps your code, even if it worked. Calls to Popen can fail in many, many ways, and it seems pointless to single out just one of them and to replace the useful traceback and error message with a less accurate one. > *Command:* > :~$ nmcli device wifi connect 'Apartment 18' password > '40672958689850014685abcdf' > Error: Connection activation failed: (7) Secrets were required, but not > provided. If you cannot get nmcli working directly from the command line, you have *no hope* of getting it working with Python getting in the way. *First* you must be able to run the command directly from the shell, with no errors. Then you can move the *working* command to Python and Popen. > return proc.strip().decode("utf-8") > AttributeError: 'Popen' object has no attribute 'strip' The error should explain exactly what the problem is. You are tying to call the STRING METHOD string.decode on a Popen object. Did you read the error message? I don't know how to fix it because I don't know what you are trying to do. -- Steve From cs at cskk.id.au Sun Nov 25 19:30:02 2018 From: cs at cskk.id.au (Cameron Simpson) Date: Mon, 26 Nov 2018 11:30:02 +1100 Subject: [Tutor] Issue in using "subprocess.Popen" for parsing the command output In-Reply-To: <20181125220303.GI4319@ando.pearwood.info> References: <20181125220303.GI4319@ando.pearwood.info> Message-ID: <20181126003002.GA72465@cskk.homeip.net> On 26Nov2018 09:03, Steven D'Aprano wrote: >On Sun, Nov 25, 2018 at 10:43:10PM +0530, srinivasan wrote: >> 1. Am trying to improve the below code with "try" and "exception", >> could >> you please help me how "try" and "exception" can be used on the below code >> snippet. I hope in my code with try and exception, seems to be a bug. > >As a beginner, you should normally not use try...except to report >errors. You should learn how to diagnose errors by reading the >traceback. Covering up the traceback with try...except makes debugging >harder. Very true, but... >Your use here: > > >> * try:* >> * cmd = "nmcli device wifi connect '%s' password '%s'" % (ssid, pw)* >> * proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, universal_newlines=True)* >> * stdout, stderr = proc.communicate()* >> * retcode = proc.returncode* >> * print("printing stdout!!!!!!!!!!", stdout)* >> * print("printing retcode!!!!!!!!!!", retcode)* >> * except subprocess.CalledProcessError as e:* >> * s = """While executing '{}' something went wrong.* >> * Return code == '{}'* >> * Return output:\n'{}'* >> * """.format(cmd, e.returncode, e.output, shell=enable_shell)* >> * raise AssertionError(s)* [...] >But even if it doesn't fail, the next line: > > raise AssertionError(s) > >is an abuse of exceptions. The failure here is *not* an assertion, and >you shouldn't use AssertionError. You wouldn't use TypeError or >UnicodeEncodeError or AttributeError. "AssertionError" should not be >used for "some arbitrary error". [...] >My opinion is, you should remove that try...except altogether. I don't >think that it helps your code, even if it worked. Calls to Popen can >fail in many, many ways, and it seems pointless to single out just one >of them and to replace the useful traceback and error message with a >less accurate one. I'd add one qualificaion here: it may be that he wants to report this exception in particular, while still not "handling it". In which case I'd advocate something like: try: ... Popen stuff ... except subprocess.CalledProcessError as e: s = .... print(s, file=sys.stderr) raise i.e. report some special message, then _reraise_ the original exception. In this way he gets to keep the original exception and traceback for debugging, which still making whatever special message he wanted to make. Cheers, Cameron Simpson From pasokan at gmail.com Sun Nov 25 22:54:45 2018 From: pasokan at gmail.com (Asokan Pichai) Date: Mon, 26 Nov 2018 09:24:45 +0530 Subject: [Tutor] A required question In-Reply-To: <009b01d483a8$82db9500$8892bf00$@verizon.net> References: <000901d482ee$46dbe330$d493a990$@verizon.net> <449AD65C-DD2B-45BA-953D-020A2B92D323@graniteweb.com> <009b01d483a8$82db9500$8892bf00$@verizon.net> Message-ID: On Sat, Nov 24, 2018, 14:33 Avi Gross David, > > As I suspected. Yes, I am aware how to do those things. Just wondered if > anyone automated the process so a fairly simple interface worked. > Does the requirements.txt file (associated with pip IIRC) does most of what you want? > > I am dropping the request. > > Avi > > -----Original Message----- > From: Tutor On Behalf Of > David Rock > Sent: Friday, November 23, 2018 9:28 PM > To: Tutor Python > Subject: Re: [Tutor] A required question > > > > On Nov 23, 2018, at 09:35, Alan Gauld via Tutor > wrote: > > > > On 23/11/2018 05:34, Avi Gross wrote: > >> What I was thinking was the ability to do something like this: > >> > >> import ReChoir as require > >> > >> require.version(condition, before=True, after=False) > >> require.modules(module list, recursive=True) require.os([?Eunuchs?, > >> ?Windblows?]) > >> require.functionality(?print3?) > > > > I can see the logic but suspect discussion of new features is probably > > the preserve of the main Python list. If you can get traction there > > somebody might actually go ahead and write one! > > discussion of a ?require? library probably isn?t necessary. It?s pretty > straightforward to include the logic using existing methods. > > For the version of python, test against sys.version_info For the modules, > put your import calls in a try block and handle exceptions For the OS > version, test against os.name or sys.platform The last one, > ?functionality,? is a bit vague. Probably another candidate for a try > block. > > > ? > David Rock > david at graniteweb.com > > > > > _______________________________________________ > 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 stephen.m.smith at comcast.net Mon Nov 26 05:21:40 2018 From: stephen.m.smith at comcast.net (stephen.m.smith at comcast.net) Date: Mon, 26 Nov 2018 05:21:40 -0500 Subject: [Tutor] click() performs unreliably Message-ID: <00fc01d48571$d289c570$779d5050$@comcast.net> I am trying to click on one of two buttons on a page. Here is an image of the relevant portion of the page, the HTML and the two xpaths. /html/body/div[3]/div/div[3]/div[6]/div/div[2]/div/div[3]/a[1] /html/body/div[3]/div/div[3]/div[6]/div/div[2]/div/div[3]/a[2] I have made numerous attempts to click on the first button with very limited success - it works maybe 25% of the time. Looking through the code snippet below, you will be able to see some of the various techniques I have tried - I commented out some of the attempts when they failed so that I could remember what I have tried. I have looked extensively online at previous posts, but can't find anything that works reliably. The code executes fine and I can see the button depressed and get routed to the previous page, but the other actions associated with the click (backing out the previous step) are not performed. Yet if I pause the code and intervene by clicking on the button by hand, the routing takes place and the associated steps are executed. When I click on the second button (with a virtually identical xpath address) the processing works as it should. I have tried to find the element by xpath and class name, I have tried commands with a click() at the end of the find, I have tried .send_keys("\n") at the end of the find and other approaches as well. A simple find_element_by_xpath with the address for the second button works 100% of the time. I really don't understand what I am missing and can't seem to find another method to try. Thanks for any and all thoughts. ## br.get('chrome://settings/') ## br.execute_script('chrome.settingsPrivate.setDefaultZoom(1.5);') br.find_element_by_xpath("/html/body/div[3]/div/div[3]/div[6]/div/div[2]/div /div[3]/a[1]").click() #br.find_element_by_class_name("go_back_button").send_keys("\n") # Back out time (test mode) #print(thread, "Test mode - backing out time(s)") #result = xpath_search(self, xpath_for_go_back_button, br, 10, do_click) ## ## button = br.find_element_by_xpath("//*[@id='main']/div[6]/div/div[2]/div/div[3]/a[1]" ) ## button.click() ## #sleep(20) ## result = xpath_search(self,"/html/body/div[3]/div/div[3]/div[6]/div/div[2]/div/div[3] /a[1]", br, 5, do_click) ## print("Back out time result =", result) ## if result == failure: ## self.queue.put("(" + thread + ") Unable to back out time") ## if thread == "1": ## endApplication(self, br, thread) #### return ## try: ## WebDriverWait(br, 5).until(EC.element_to_be_clickable((By.XPATH, xpath_for_go_back_button))) ## back_out = br.find_element_by_xpath(xpath_for_go_back_button) ## back_out.click() ## print("Cancel tee time worked") ## except NoSuchElementException: ## print("Cancel tee time did not work") ## return From alan.gauld at yahoo.co.uk Mon Nov 26 09:02:29 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 26 Nov 2018 14:02:29 +0000 Subject: [Tutor] click() performs unreliably In-Reply-To: <00fc01d48571$d289c570$779d5050$@comcast.net> References: <00fc01d48571$d289c570$779d5050$@comcast.net> Message-ID: On 26/11/2018 10:21, stephen.m.smith at comcast.net wrote: > I am trying to click on one of two buttons on a page. Here is an image of > the relevant portion of the page, the HTML and the two xpaths. I assume this is related to your earlier post about using Selenium? If so, you will probably get a better response asking on the Selenium help fora since this list is really focused on core Python and the standard library. Relatively few members will be using Selenium. The Selenium support pages has several suggestions: https://www.seleniumhq.org/support/ -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From mats at wichmann.us Mon Nov 26 10:23:23 2018 From: mats at wichmann.us (Mats Wichmann) Date: Mon, 26 Nov 2018 08:23:23 -0700 Subject: [Tutor] A required question In-Reply-To: References: <000901d482ee$46dbe330$d493a990$@verizon.net> <449AD65C-DD2B-45BA-953D-020A2B92D323@graniteweb.com> <009b01d483a8$82db9500$8892bf00$@verizon.net> Message-ID: <1b50ffb0-c14f-6ea7-5904-4fc0b0067a6a@wichmann.us> On 11/25/18 8:54 PM, Asokan Pichai wrote: > On Sat, Nov 24, 2018, 14:33 Avi Gross >> David, >> >> As I suspected. Yes, I am aware how to do those things. Just wondered if >> anyone automated the process so a fairly simple interface worked. >> > Does the requirements.txt file (associated with pip IIRC) does most of what > you want? If so, then also worth looking at the pipfile and pipenv which are aiming to improve on the experience. From avigross at verizon.net Mon Nov 26 18:09:30 2018 From: avigross at verizon.net (Avi Gross) Date: Mon, 26 Nov 2018 18:09:30 -0500 Subject: [Tutor] A retired question Message-ID: <008601d485dd$163b2af0$42b180d0$@verizon.net> After saying I dropped my request, short replies keep coming. Let me reply in this context: Would there be BUILT-IN tools that can be expected to already be everywhere which can be used at the top of a program you write and distribute to quickly check if the rest of the program has the resources it needs to run probably. I am not currently planning on designing or implementing anything. It was an academic question. I was thinking along the lines of slightly advanced ways functions can start with a group of assertions testing what is expected and quit if not set right. They use assert(). Some of what is being suggested may have useful pieces you can use in doing something like this. But I note that the ability to package things is not something most users need. You need a kernel of guaranteed software you can count on in order to test if other required elements and conditions can be met. Realistically, there are so many variations on python and how you can get copies distributed that I cannot assume that if I write a routine that uses numpy or pandas, as I have been doing, will be installed before a user makes use of the software. And, you can even build your own versions of python while leaving out or including things. It is nice to be flexible. But then you may need to stock to some version from prehistoric times using relatively few features AND being careful to not use features that were later changed or removed. Why bother? OK, here is the solution. At the beginning of the script put out a big warning telling the user that if they do not have all the required software they are proceeding at their own risk and take full responsibility if something weird happens before the software dies. Hitting ENTER will be considered acceptance of the terms! Again. Dropped. Moving on. If the goal here is to tutor, no need to share complex solutions but rather help students learn how to look at their problems and see what kinds of data and logic to apply in what order and then how to debug the inevitable mistakes like a missing comma. -----Original Message----- From: Tutor On Behalf Of Mats Wichmann Sent: Monday, November 26, 2018 10:23 AM To: pasokan at gmail.com Cc: tutor at python.org Subject: Re: [Tutor] A required question On 11/25/18 8:54 PM, Asokan Pichai wrote: > On Sat, Nov 24, 2018, 14:33 Avi Gross >> David, >> >> As I suspected. Yes, I am aware how to do those things. Just wondered >> if anyone automated the process so a fairly simple interface worked. >> > Does the requirements.txt file (associated with pip IIRC) does most of > what you want? If so, then also worth looking at the pipfile and pipenv which are aiming to improve on the experience. _______________________________________________ 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 Nov 26 19:55:41 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 27 Nov 2018 11:55:41 +1100 Subject: [Tutor] A retired question In-Reply-To: <008601d485dd$163b2af0$42b180d0$@verizon.net> References: <008601d485dd$163b2af0$42b180d0$@verizon.net> Message-ID: <20181127005539.GN4319@ando.pearwood.info> On Mon, Nov 26, 2018 at 06:09:30PM -0500, Avi Gross wrote: > Would there be BUILT-IN tools that can be expected to already be everywhere > which can be used at the top of a program you write and distribute to > quickly check if the rest of the program has the resources it > needs to run probably. No. -- Steve From mats at wichmann.us Mon Nov 26 20:38:49 2018 From: mats at wichmann.us (Mats Wichmann) Date: Mon, 26 Nov 2018 18:38:49 -0700 Subject: [Tutor] A retired question In-Reply-To: <20181127005539.GN4319@ando.pearwood.info> References: <008601d485dd$163b2af0$42b180d0$@verizon.net> <20181127005539.GN4319@ando.pearwood.info> Message-ID: <4cf1afc5-c3b8-b03b-af20-10a044aa4ae5@wichmann.us> On 11/26/18 5:55 PM, Steven D'Aprano wrote: > On Mon, Nov 26, 2018 at 06:09:30PM -0500, Avi Gross wrote: > >> Would there be BUILT-IN tools that can be expected to already be everywhere >> which can be used at the top of a program you write and distribute to >> quickly check if the rest of the program has the resources it >> needs to run probably. > > No. Indeed. But the whole idea of the Python "batteries included" concept is that Python + standard library gets you an impressively long way without resorting to external modules, and that set is a promise. From avigross at verizon.net Mon Nov 26 19:18:46 2018 From: avigross at verizon.net (Avi Gross) Date: Mon, 26 Nov 2018 19:18:46 -0500 Subject: [Tutor] Writing the right code rite Message-ID: <009501d485e6$c335c400$49a14c00$@verizon.net> I am here to learn but also to help out when I already know something that is asked. What has been of some concern to me is how to reply when the asked has not shared with us lots of info. If I see them using "print" without parentheses, I can assume they are not using 3.x and perhaps emulate their usage. Showing them samples of code that won't work in their environment is a bit of a waste of time, theirs and mine. But in watching, I conclude that a subset of the requests come from people who are some form of early students and only aware of selected aspects of the language. For some of them uses of words and phrases is a tad loose so when they say "list" they might mean something that can range from what others might call tuples or lists or dictionaries or sets or reaching a bit beyond to other words like vectors, arrays, matrices, bags, collections, name spaces and so on. What many want is for someone to help them write some kind of simple loop. Ideally, they would state the problem clearly enough and try to write code and ask help when it does not seem to work. If they answer a few questions to clarify, they can often get a simple answer they can use or understand. But it might help to steer them otherwise. We have had recent requests for example for what sounded like a need to read text into words and then keep adding them to a 'list" if not already there. What kind of answers have people provided? What many of the answers would have in common is to break the lines up into tokens we consider words. But even that can be a complex task that can be done many ways depending on what the file might contain and whether words can include an apostrophe or hypen. Some solutions might tokenize a line at a time and some do the entire file at once using some regular expression that matches anything after one or more non-word characters till the next series of one or more non-word characters. Others might just split a line on a single space which likely won't work on normal English text containing punctuation and so on. Similarly, some may suggest tossing all the tokens into a set to get unique results. Others may suggest using a dictionary and couniting how many times each word appears (even if that was not required.) Others may use lists and introduce use of whys to check if something is already in the list or extend the list. Some may suggest using a data structure like a dequeue for reasons I am not able to explain. Others may want to use a list and carefully explain how to search in an item is already in a list and how to extend the list. Some may even want them to keep the list in alphabetical order. Some may want to dump all words in list 1 then iterate on it by adding only words not already in list2 to list2. Some may want to use a binary tree that finds thing in O(log(N) or something. And yes, some would like you to make a long list then sort it and apply a function such as an iterator that returns the next item after finding all consecutive items that are the same. There are an amazing number of fairly reasonable ways to solve problems, with the usual tradeoffs. But if someone is new to this, what answer meets their immediate needs? For most school projects, unless they are told to use some module, the expectation is for something fairly simple without worrying that the small dataset used will run very long or use much memory. And, the solution probably should not be deeply nested in ways hard to read. I recently wrote a function for someone (not on this group) with about a dozen lines of code that did things step by step using variables with somewhat meaningful names. In English, the goal was to take in a DataFrame and only keep those rows where some subset of the columns all had valid data. The long version was easy to understand. It did things like make a narrow copy of the data using just that subset of the columns. Then it applied a function that returned a vector of True/False if the narrow rows were all OK or had anything missing. Then that vector was used to index the original table to select only rows where it was true. But all that could be collapsed into a single line with a single but somewhat nested statement as each step sort of feeds into being an index of another step. But I would not want that kind of code around without paragraphs of comments and in this case, what difference does it make if temporary variables had a name before being garbage collected when the function returns? Since I find I am more interested in examining different ways to solve a problem with some thought on what is better or just easier or more elegant and so on, I am not always a best fit for instructing new students unless they are sitting in a class I am teaching and mostly at the same level. This is an open forum where we often have no clue what the student already knows or has tried and we do not encourage them to post more than a minimal example. But we can probably assume most just want to get something done, not create functions and objects they could reuse . I know it slows things down, especially with a moderator, but often the best answer is to ask some questions before trying to supply an answer. Perhaps a rapid email exchange directly with a student, perhaps moving on to instant messaging or phone or video forms of communication would work better for those interested. When done, you might post a summary if appropriate for others interested but note showing a full solution can be unfair if other students working on the same problem just latch on to that. From alan.gauld at yahoo.co.uk Tue Nov 27 05:22:24 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 27 Nov 2018 10:22:24 +0000 Subject: [Tutor] Writing the right code rite In-Reply-To: <009501d485e6$c335c400$49a14c00$@verizon.net> References: <009501d485e6$c335c400$49a14c00$@verizon.net> Message-ID: On 27/11/2018 00:18, Avi Gross wrote: > But in watching, I conclude that a subset of the requests come from people > who are some form of early students and only aware of selected aspects of > the language. One of the challenges of this list is identifying the level of the requester. We get several general categories: 1) Professional programmers just starting with Python. Often with a specific project and need a rapid run up the learning curve. They know all about code and tools they just need to know how Python does it. 2a) Academic beginners - those learning Python on a course. These often are not very interested in learning about the theoretical side of things, or whether there is another way to tackle the problem, they simply want to get their homework done. Knowledge level ranges from junior school to under-grad CS courses. 2b) Advanced students (eg. PhD etc) who may or may not have previous programming experience but typically need help using Python for a specific aim. These are often like a cross between 1 and 2a. They know all about their domain but possibly have little CS knowledge/experience. They often assume that NumPy/SciPy are part of the standard library and that most Python users will know them. 3) Hobbyist programmers - those who learn for fun, maybe just out of curiosity or to extend some other skill set - such as working on the Raspberry Pi or with a package like Blender. These can range from youngsters(pre teen) to seniors(70+). They usually have zero CS knowledge and are varied in their enthusiasm for learning the background principles. We need to identify the skill and knowledge level and learning goals of each individual and take that into account in our responses. Figuring that out is often harder than answering the immediate question! We just need to remember that they all have the same right to be here and there are no stupid questions. (Although they may need some help to ask the right question!) -- 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 Tue Nov 27 05:39:38 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 27 Nov 2018 21:39:38 +1100 Subject: [Tutor] Writing the right code rite In-Reply-To: <009501d485e6$c335c400$49a14c00$@verizon.net> References: <009501d485e6$c335c400$49a14c00$@verizon.net> Message-ID: <20181127103937.GP4319@ando.pearwood.info> On Mon, Nov 26, 2018 at 07:18:46PM -0500, Avi Gross wrote: > What kind of answers have people provided? The archives of this mailing list go back to 1994. If you spend some time browsing randomly at the answers people provide, you should get a feel for what sort of answers we tend to give. https://mail.python.org/pipermail/tutor/ > I know it slows things down, especially with a moderator, but often the best > answer is to ask some questions before trying to supply an answer. Indeed. Learning how to ask good questions is part of the learning process, and when people ask poor questions, asking questions in return will either: - teach them by example what sort of information they need to solve their problem; or - discourage the lazy help-vampires from draining the energy out of this group. http://www.skidmore.edu/~pdwyer/e/eoc/help_vampire.htm > Perhaps a > rapid email exchange directly with a student, perhaps moving on to instant > messaging or phone or video forms of communication would work better for > those interested. Of course you are an adult in a free country and you have the right to do whatever you like, but I don't recommend this. In fact I would consider it anti-social and hostile to the rest of the community. I think I speak for most of us when I say we're looking to help and educate the entire community. We're not the personal servants of individual posters, or private teachers. We post so that *everyone* can learn from the answers, not just the person asking the question, and the *process* of reaching the answer is just as important as the final code. Taking that process off-list is, in my opinion, not helping the community. (Here, I'm not speaking for others.) Its also likely to get annoying fast, for both parties: you, when the poster starts bombarding you with question after question, and the poster themselves, when you are too slow to respond. At least if they email the list, there is the opportunity for others to reply in your stead. Of course there are circumstances where it is appropriate to take discussion off-list: - you're being paid to help; - the discussion wanders off-topic; - or it becomes irrelevant and of no interest to the rest of the community (perhaps because it is too specialised to interest anyone but yourself and the original poster); - or moves into confidential/private areas of discussions that shouldn't be posted to the list (were you asked to sign an NDA?); etc. Some subjective judgement may be required. > When done, you might post a summary if appropriate for > others interested but note showing a full solution can be unfair if other > students working on the same problem just latch on to that. The community standard here is that we don't do homework for others. We'll answer concrete questions about Python the language itself, of course: "how do I use the zip function?". We'll help them debug their code, if they've written some code. If they haven't written even a single line of code, we generally don't do much more than say "Show us what you've tried." We'll help guide people towards solutions, but not hand them the solution on a platter. That's for students in school, of course. We're a bit more flexible when it comes to self-learners or even professional programmers asking for help. And we usually take it on trust if they say "this isn't homework". But even then, we're not the personal slave of the poster, and we have no obligation to solve their problem for them. We're not obliged to hand over a complete solution, and in fact doing so goes against our stated aim to *teach* people. (How will they learn effectively if we do their work for them?) Again, some subjective judgement is required. I wouldn't hesitate to answer minor or trivial questions in full, e.g. "how do I count the number of digits in a string?". If it were homework, I might answer a slightly different question instead: number_of_vowels = sum(thestring.count(vowel) for vowel in "aeiouAEIOU") and let them generalise to digits. But more substantial questions, I probably wouldn't unless the problem really tickled my fancy and I had plenty of time to work on a solution. (Time. I remember when I had time.) -- Steve From srinivasan.rns at gmail.com Tue Nov 27 04:46:12 2018 From: srinivasan.rns at gmail.com (srinivasan) Date: Tue, 27 Nov 2018 10:46:12 +0100 Subject: [Tutor] Issue in using "subprocess.Popen" for parsing the command output In-Reply-To: <20181125220303.GI4319@ando.pearwood.info> References: <20181125220303.GI4319@ando.pearwood.info> Message-ID: Dear All, I have fixed the issue with below code snippet for parsing the command output without try and exception, pls let me know if any improvements are needed, might be useful for others def to_bytes(self, str): # Encode to UTF-8 to get binary data. if isinstance(str, bytes): return str return str.encode('utf-8') def to_string(self, bytes): if isinstance(bytes, str): return bytes return self.to_bytes(bytes) def convert_string(self, bytes): try: return self.to_string(bytes.decode('utf-8')) except AttributeError: # 'str' object has no attribute 'decode'. return str(bytes) except UnicodeError: return str(bytes) def sample(self, ssid, pw): cmd = "nmcli device wifi connect '%s' password '%s'" % (ssid, pw) p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) out, err = p.communicate() out = self.convert_string(out) err = self.convert_string(err) print("The value of data", out.split(" ")) print("printing stdout!!!!!!!!!!", out) print("printing err!!!!!!!!!!", err) print("printing retcode!!!!!!!!!!", p.returncode) if p.returncode != 0: raise subprocess.CalledProcessError(cmd=cmd, returncode=p.returncode, output="{}\n{}".format(out, err)) return out if __name__ == "__main__": m = bt() print(m.sample("NIassddWiFi", "T.f.o.s.1996!abcdfg")) On Sun, Nov 25, 2018 at 11:05 PM Steven D'Aprano wrote: > > I think you are sending email using Gmail. If so, there is a command in > Gmail to send only PLAIN TEXT with no added formatting. Please use it. > Your code at the moment has extra asterisks * added at the beginning and > end of each line. > > More comments below. > > > On Sun, Nov 25, 2018 at 10:43:10PM +0530, srinivasan wrote: > > > 1. Am trying to improve the below code with "try" and "exception", could > > you please help me how "try" and "exception" can be used on the below code > > snippet. I hope in my code with try and exception, seems to be a bug. > > As a beginner, you should normally not use try...except to report > errors. You should learn how to diagnose errors by reading the > traceback. Covering up the traceback with try...except makes debugging > harder. > > Your use here: > > > > * try:* > > * cmd = "nmcli device wifi connect '%s' password '%s'" % (ssid, pw)* > > * proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, universal_newlines=True)* > > * stdout, stderr = proc.communicate()* > > * retcode = proc.returncode* > > * print("printing stdout!!!!!!!!!!", stdout)* > > * print("printing retcode!!!!!!!!!!", retcode)* > > * except subprocess.CalledProcessError as e:* > > * s = """While executing '{}' something went wrong.* > > * Return code == '{}'* > > * Return output:\n'{}'* > > * """.format(cmd, e.returncode, e.output, shell=enable_shell)* > > * raise AssertionError(s)* > > doesn't seem right to me. The string.format() method doesn't take a > shell=enable_shell agument, so I expect that line > > s = """...""".format(cmd, ..., shell=enable_shell) > > to fail. But even if it doesn't fail, the next line: > > raise AssertionError(s) > > is an abuse of exceptions. The failure here is *not* an assertion, and > you shouldn't use AssertionError. You wouldn't use TypeError or > UnicodeEncodeError or AttributeError. "AssertionError" should not be > used for "some arbitrary error". > > There are almost no reasons to manually raise AssertionError, except > perhaps in test frameworks like unittest. Normally you should only get > an AssertionError from the "assert" command: > > https://import-that.dreamwidth.org/676.html > > My opinion is, you should remove that try...except altogether. I don't > think that it helps your code, even if it worked. Calls to Popen can > fail in many, many ways, and it seems pointless to single out just one > of them and to replace the useful traceback and error message with a > less accurate one. > > > > *Command:* > > :~$ nmcli device wifi connect 'Apartment 18' password > > '40672958689850014685abcdf' > > Error: Connection activation failed: (7) Secrets were required, but not > > provided. > > If you cannot get nmcli working directly from the command line, you have > *no hope* of getting it working with Python getting in the way. > > *First* you must be able to run the command directly from the shell, > with no errors. Then you can move the *working* command to Python and > Popen. > > > return proc.strip().decode("utf-8") > > AttributeError: 'Popen' object has no attribute 'strip' > > The error should explain exactly what the problem is. You are tying to > call the STRING METHOD string.decode on a Popen object. Did you read the > error message? > > I don't know how to fix it because I don't know what you are trying to > do. > > > -- > Steve > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From steve at pearwood.info Tue Nov 27 06:13:37 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 27 Nov 2018 22:13:37 +1100 Subject: [Tutor] Writing the right code rite In-Reply-To: References: <009501d485e6$c335c400$49a14c00$@verizon.net> Message-ID: <20181127111336.GQ4319@ando.pearwood.info> On Tue, Nov 27, 2018 at 10:22:24AM +0000, Alan Gauld via Tutor wrote: > there are no stupid questions. "Was it you or your brother who died in the war?" More seriously: https://www.themuse.com/advice/how-to-ask-stupid-questions-without-sounding-stupid although I disagree with the last bit of advice. I think it is fine to acknowledge that you recognise when you are asking a "stupid" (basic) question. -- Steve From avigross at verizon.net Tue Nov 27 10:51:22 2018 From: avigross at verizon.net (Avi Gross) Date: Tue, 27 Nov 2018 10:51:22 -0500 Subject: [Tutor] Writing the right code rite In-Reply-To: <20181127103937.GP4319@ando.pearwood.info> References: <009501d485e6$c335c400$49a14c00$@verizon.net> <20181127103937.GP4319@ando.pearwood.info> Message-ID: <006801d48669$0bd280e0$237782a0$@verizon.net> Steve, I appreciated your letter and it helped point me in the direction this group may be designed for. For some it is sort of like forms of therapy where the goal is to help them reach an insight rather than toss a diagnosis at them and maybe write a prescription. The ultimate goal is to learn how to use resources by yourself. I am so used to doing my own work and getting a real understanding, that it is not easy to adjust to how some others are like the Help Vampires in the article you quoted. Having read it, I do recognize how some people I know may fit into that category and I have often after a while automatically set up not enabling them in some of the ways you describe. Amusingly enough, I have lived in Transylvania (border area where Romania and Hungary meet) and never met any (nonexistent) Vampires. But they seem to be everywhere in books and on TV so why not HELP Vampires. Avi -----Original Message----- From: Tutor On Behalf Of Steven D'Aprano Sent: Tuesday, November 27, 2018 5:40 AM To: tutor at python.org Subject: Re: [Tutor] Writing the right code rite On Mon, Nov 26, 2018 at 07:18:46PM -0500, Avi Gross wrote: > What kind of answers have people provided? The archives of this mailing list go back to 1994. If you spend some time browsing randomly at the answers people provide, you should get a feel for what sort of answers we tend to give. https://mail.python.org/pipermail/tutor/ > I know it slows things down, especially with a moderator, but often > the best answer is to ask some questions before trying to supply an answer. Indeed. Learning how to ask good questions is part of the learning process, and when people ask poor questions, asking questions in return will either: - teach them by example what sort of information they need to solve their problem; or - discourage the lazy help-vampires from draining the energy out of this group. http://www.skidmore.edu/~pdwyer/e/eoc/help_vampire.htm > Perhaps a > rapid email exchange directly with a student, perhaps moving on to > instant messaging or phone or video forms of communication would work > better for those interested. Of course you are an adult in a free country and you have the right to do whatever you like, but I don't recommend this. In fact I would consider it anti-social and hostile to the rest of the community. I think I speak for most of us when I say we're looking to help and educate the entire community. We're not the personal servants of individual posters, or private teachers. We post so that *everyone* can learn from the answers, not just the person asking the question, and the *process* of reaching the answer is just as important as the final code. Taking that process off-list is, in my opinion, not helping the community. (Here, I'm not speaking for others.) Its also likely to get annoying fast, for both parties: you, when the poster starts bombarding you with question after question, and the poster themselves, when you are too slow to respond. At least if they email the list, there is the opportunity for others to reply in your stead. Of course there are circumstances where it is appropriate to take discussion off-list: - you're being paid to help; - the discussion wanders off-topic; - or it becomes irrelevant and of no interest to the rest of the community (perhaps because it is too specialised to interest anyone but yourself and the original poster); - or moves into confidential/private areas of discussions that shouldn't be posted to the list (were you asked to sign an NDA?); etc. Some subjective judgement may be required. > When done, you might post a summary if appropriate for others > interested but note showing a full solution can be unfair if other > students working on the same problem just latch on to that. The community standard here is that we don't do homework for others. We'll answer concrete questions about Python the language itself, of course: "how do I use the zip function?". We'll help them debug their code, if they've written some code. If they haven't written even a single line of code, we generally don't do much more than say "Show us what you've tried." We'll help guide people towards solutions, but not hand them the solution on a platter. That's for students in school, of course. We're a bit more flexible when it comes to self-learners or even professional programmers asking for help. And we usually take it on trust if they say "this isn't homework". But even then, we're not the personal slave of the poster, and we have no obligation to solve their problem for them. We're not obliged to hand over a complete solution, and in fact doing so goes against our stated aim to *teach* people. (How will they learn effectively if we do their work for them?) Again, some subjective judgement is required. I wouldn't hesitate to answer minor or trivial questions in full, e.g. "how do I count the number of digits in a string?". If it were homework, I might answer a slightly different question instead: number_of_vowels = sum(thestring.count(vowel) for vowel in "aeiouAEIOU") and let them generalise to digits. But more substantial questions, I probably wouldn't unless the problem really tickled my fancy and I had plenty of time to work on a solution. (Time. I remember when I had time.) -- Steve _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From srinivasan.rns at gmail.com Tue Nov 27 07:50:04 2018 From: srinivasan.rns at gmail.com (srinivasan) Date: Tue, 27 Nov 2018 13:50:04 +0100 Subject: [Tutor] Error Python version 3.6 does not support this syntax. Message-ID: Dear Python Experts, As still I am newbie and learning python, I am trying to reuse the Bluetoothctl wrapper in Python from the link ( https://gist.github.com/egorf/66d88056a9d703928f93) I am using python3.6 version, In pycharm editor on the bold highlighted code snippets I see the error message "Python version 3.6 does not support this syntax.", Could you please how help me how the below highlighted lines of code can be can be ported to python3.6 version? * except BluetoothctlError, e:* * print(e)* * return None* Full Code snippet: ============== import time import pexpect import subprocess import sys class BluetoothctlError(Exception): """This exception is raised, when bluetoothctl fails to start.""" pass class Bluetoothctl: """A wrapper for bluetoothctl utility.""" def __init__(self): out = subprocess.check_output("rfkill unblock bluetooth", shell = True) self.child = pexpect.spawn("bluetoothctl", echo = False) def get_output(self, command, pause = 0): """Run a command in bluetoothctl prompt, return output as a list of lines.""" self.child.send(command + "\n") time.sleep(pause) start_failed = self.child.expect(["bluetooth", pexpect.EOF]) if start_failed: raise BluetoothctlError("Bluetoothctl failed after running " + command) return self.child.before.split("\r\n") def start_scan(self): """Start bluetooth scanning process.""" try: out = self.get_output("scan on") * except BluetoothctlError, e: print(e) return None* def make_discoverable(self): """Make device discoverable.""" try: out = self.get_output("discoverable on") * except BluetoothctlError, e: print(e) return None* def parse_device_info(self, info_string): """Parse a string corresponding to a device.""" device = {} block_list = ["[\x1b[0;", "removed"] string_valid = not any(keyword in info_string for keyword in block_list) if string_valid: try: device_position = info_string.index("Device") except ValueError: pass else: if device_position > -1: attribute_list = info_string[device_position:].split(" ", 2) device = { "mac_address": attribute_list[1], "name": attribute_list[2] } return device def get_available_devices(self): """Return a list of tuples of paired and discoverable devices.""" try: out = self.get_output("devices") * except BluetoothctlError, e: print(e) return None* else: available_devices = [] for line in out: device = self.parse_device_info(line) if device: available_devices.append(device) return available_devices def get_paired_devices(self): """Return a list of tuples of paired devices.""" try: out = self.get_output("paired-devices") * except BluetoothctlError, e: print(e) return None* else: paired_devices = [] for line in out: device = self.parse_device_info(line) if device: paired_devices.append(device) return paired_devices def get_discoverable_devices(self): """Filter paired devices out of available.""" available = self.get_available_devices() paired = self.get_paired_devices() return [d for d in available if d not in paired] def get_device_info(self, mac_address): """Get device info by mac address.""" try: out = self.get_output("info " + mac_address) * except BluetoothctlError, e: print(e) return None* else: return out def pair(self, mac_address): """Try to pair with a device by mac address.""" try: out = self.get_output("pair " + mac_address, 4) * except BluetoothctlError, e: print(e) return None* else: res = self.child.expect(["Failed to pair", "Pairing successful", pexpect.EOF]) success = True if res == 1 else False return success def remove(self, mac_address): """Remove paired device by mac address, return success of the operation.""" try: out = self.get_output("remove " + mac_address, 3) * except BluetoothctlError, e: print(e) return None* else: res = self.child.expect(["not available", "Device has been removed", pexpect.EOF]) success = True if res == 1 else False return success def connect(self, mac_address): """Try to connect to a device by mac address.""" try: out = self.get_output("connect " + mac_address, 2) * except BluetoothctlError, e: print(e) return None* else: res = self.child.expect(["Failed to connect", "Connection successful", pexpect.EOF]) success = True if res == 1 else False return success def disconnect(self, mac_address): """Try to disconnect to a device by mac address.""" try: out = self.get_output("disconnect " + mac_address, 2) * except BluetoothctlError, e: print(e) return None* else: res = self.child.expect(["Failed to disconnect", "Successful disconnected", pexpect.EOF]) success = True if res == 1 else False return success kindly do the needful, Many Thanks in advance, From mats at wichmann.us Tue Nov 27 13:47:46 2018 From: mats at wichmann.us (Mats Wichmann) Date: Tue, 27 Nov 2018 11:47:46 -0700 Subject: [Tutor] Error Python version 3.6 does not support this syntax. In-Reply-To: References: Message-ID: On 11/27/18 5:50 AM, srinivasan wrote: > Dear Python Experts, > > As still I am newbie and learning python, I am trying to reuse the > Bluetoothctl wrapper in Python from the link ( > https://gist.github.com/egorf/66d88056a9d703928f93) I am using python3.6 > version, In pycharm editor on the bold highlighted code snippets I see the > error message "Python version 3.6 does not support this syntax.", once again you've posted in a way that inserts lots of extra crud, you avoided that last time. The syntax change is simple (and works on most older Pythons too): except ErrorType, e: becomes except ErrorType as e: > > Could you please how help me how the below highlighted lines of code can be > can be ported to python3.6 version? > > * except BluetoothctlError, e:* > > * print(e)* > * return None* > From kamtarin7722 at gmail.com Tue Nov 27 16:04:05 2018 From: kamtarin7722 at gmail.com (Kamina Kamtarin) Date: Tue, 27 Nov 2018 16:04:05 -0500 Subject: [Tutor] Need help,please! Message-ID: A De/Coder. Think back to 3rd grade when you passed notes to friends in class. We can't let the teacher see what we're writing so we used a code. A=1, B=2, C=3, etc. Your job is to create a program which does the following: 1. Presents the user with a menu choice: encode or decode 2. Depending on what they chose you will either encode letters into numbers (seperated by dashes) or decode a series of numbers (separated by dashes) into letters. 3. For example: - "How are you?" would encode as "8-15-23 1-18-5 25-15-21?" - "8-15-23 1-18-5 25-15-21" would decode as "How are you?" From alan.gauld at yahoo.co.uk Tue Nov 27 17:17:43 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 27 Nov 2018 22:17:43 +0000 Subject: [Tutor] Need help,please! In-Reply-To: References: Message-ID: On 27/11/2018 21:04, Kamina Kamtarin wrote: > A De/Coder. Think back to 3rd grade when you passed notes to friends in > class. We can't let the teacher see what we're writing so we used a code. > A=1, B=2, C=3, etc. Your job is to create a program which does the > following: > > 1. Presents the user with a menu choice: encode or decode > 2. Depending on what they chose you will either encode letters into > numbers (seperated by dashes) or decode a series of numbers (separated by > dashes) into letters. > 3. For example: > - "How are you?" would encode as "8-15-23 1-18-5 25-15-21?" > - "8-15-23 1-18-5 25-15-21" would decode as "How are you?" Look at the builtin ord() and chr() functions. For example ord('A') -> 65 and ord('a') -> 97 Similarly chr(97) -> 'a' and chr(65) -> 'A' Now a little bit of arithmetic should get you to/from 1. How you handle upper/lower case issues is up to you, but if they aren't important then the string upper() and lower() methods may help too. I don't know what you do with punctuation, you didn't say but you should be able to get the core done using the 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 david at graniteweb.com Tue Nov 27 17:33:28 2018 From: david at graniteweb.com (David Rock) Date: Tue, 27 Nov 2018 16:33:28 -0600 Subject: [Tutor] encoder decoder problem In-Reply-To: References: Message-ID: <2F2D7D8D-C6AB-495C-972A-9C0FAE5FF1B8@graniteweb.com> > On Nov 27, 2018, at 16:17, Alan Gauld via Tutor wrote: > > On 27/11/2018 21:04, Kamina Kamtarin wrote: >> A De/Coder. Think back to 3rd grade when you passed notes to friends in >> class. We can't let the teacher see what we're writing so we used a code. >> A=1, B=2, C=3, etc. Your job is to create a program which does the >> following: >> >> 1. Presents the user with a menu choice: encode or decode >> 2. Depending on what they chose you will either encode letters into >> numbers (seperated by dashes) or decode a series of numbers (separated by >> dashes) into letters. >> 3. For example: >> - "How are you?" would encode as "8-15-23 1-18-5 25-15-21?" >> - "8-15-23 1-18-5 25-15-21" would decode as "How are you?" > > Look at the builtin ord() and chr() functions. > > For example ord('A') -> 65 and ord('a') -> 97 > > Similarly chr(97) -> 'a' and chr(65) -> ?A' I would also try to break up your requirements into smaller pieces. For example, some things to solve: How would you get input? How would you break up a string into individual characters? How would you break up an encoded string into individual numbers? How would you put the characters/numbers back together for output? Have you started writing any code? What have you tried so far? ? David Rock david at graniteweb.com From 2hendrixtp at stu.bps-ok.org Tue Nov 27 19:30:19 2018 From: 2hendrixtp at stu.bps-ok.org (Treyton Hendrix) Date: Tue, 27 Nov 2018 18:30:19 -0600 Subject: [Tutor] I need help with my project Message-ID: If the user selected a sandwich, french fries, and a beverage, reduce the total cost of the order by $1.00. This is what I have to do and I don't know where to start. From avigross at verizon.net Tue Nov 27 20:26:04 2018 From: avigross at verizon.net (Avi Gross) Date: Tue, 27 Nov 2018 20:26:04 -0500 Subject: [Tutor] PEP 572 -- Assignment Expressions In-Reply-To: References: Message-ID: <005a01d486b9$54801960$fd804c20$@verizon.net> Ivo, One thing you could have done is explore with simpler code to see if you can deduce what is happening. If you opened your Python interpreter and tried to see what happens with a simplified variant like this, what do you get? if (match = 5) is not None: pass That might answer your question on why a := might introduce new functionality. Something similar would work fine in a language like C/C++. Heck, it is used often as a concise side effect. Both languages have an == to be used in expressions but with a meaning of COMPARE, not set equal to. I guess someone thought that situations like matching a regular expression would benefit for the side effect. There are workarounds, of course. As an example, in SOME cases, the value of the last expression can be found momentarily in the special variable consisting of a single underscore. But the expression above probably would evaluate to True, ... Ovi -----Original Message----- From: Python-list On Behalf Of Ivo Shipkaliev Sent: Tuesday, November 27, 2018 5:48 AM To: python-list at python.org Subject: PEP 572 -- Assignment Expressions Hello. Maybe it's too late for a discussion, but I just couldn't resist. I just found out about this new ":=" operator. I need to ask: What is the need for this additional ":" to the "="? Why: if (match := pattern.search(data)) is not None: # Do something with match What is wrong with: if match = pattern.search(data) is not None: # Do something with match Assignment expression or assignment statement, it's an assignment, right? It is very clear to everyone that it's an assignment! Can't it all just be a "="? Thank you very much! Kind Regards Ivo Shipkaliev -- https://mail.python.org/mailman/listinfo/python-list From steve at pearwood.info Wed Nov 28 04:13:17 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 28 Nov 2018 20:13:17 +1100 Subject: [Tutor] PEP 572 -- Assignment Expressions In-Reply-To: <005a01d486b9$54801960$fd804c20$@verizon.net> References: <005a01d486b9$54801960$fd804c20$@verizon.net> Message-ID: <20181128091316.GR4319@ando.pearwood.info> On Tue, Nov 27, 2018 at 08:26:04PM -0500, Avi Gross wrote: > Ivo, You've replied to the wrong mailing list. Ivo wrote to Python-List, not Tutor. But now that you've raised the issue... [Ivo asked] > Why: > if (match := pattern.search(data)) is not None: > # Do something with match > > What is wrong with: > if match = pattern.search(data) is not None: > # Do something with match > > Assignment expression or assignment statement, it's an assignment, right? > It is very clear to everyone that it's an assignment! Can't it all just be a > "="? It seems that Ivo didn't read the PEP because that is discussed: https://www.python.org/dev/peps/pep-0572/#id32 Despite Ivo's statement that it is "clear to everyone", the problem is that whenever you see an assignment x=y in an assignment, there's no obvious way of telling whether they meant assignment or it was a mistyped equality test x==y. So it is not clear at all. In C, this confusion between = and == is a frequent source of serious bugs, including one attempt at inserting a back-door into the Linux kernel: https://freedom-to-tinker.com/2013/10/09/the-linux-backdoor-attempt-of-2003/ https://lwn.net/Articles/57135/ Requiring the colon x:=y makes it less likely to mess up the assignment or comparison. -- Steve From nulla.epistola at web.de Wed Nov 28 06:34:09 2018 From: nulla.epistola at web.de (Sibylle Koczian) Date: Wed, 28 Nov 2018 12:34:09 +0100 Subject: [Tutor] I need help with my project In-Reply-To: References: Message-ID: Am 28.11.2018 um 01:30 schrieb Treyton Hendrix: > If the user selected a sandwich, french fries, and a beverage, reduce the > total cost of the order by $1.00. > > This is what I have to do and I don't know where to start. Well, you are lucky. I just had my first mind-reading lesson today, you are my first victim and I'll tell you what you didn't say about your problem: You know prices for sandwiches (one sort or more, with different prices?), french fries and beverages. If the user selects one or two of them, he pays the sum of the prices. If he selects one of each, he pays the sum minus $1.00. How would you do this by hand? If my mind-reading isn't yet perfect, please fill the gaps. From bgailer at gmail.com Wed Nov 28 06:40:22 2018 From: bgailer at gmail.com (Bob Gailer) Date: Wed, 28 Nov 2018 06:40:22 -0500 Subject: [Tutor] I need help with my project In-Reply-To: References: Message-ID: On Nov 28, 2018 3:43 AM, "Treyton Hendrix" <2hendrixtp at stu.bps-ok.org> wrote: > > If the user selected a sandwich, french fries, and a beverage, reduce the > total cost of the order by $1.00. > > This is what I have to do and I don't know where to start. You start by learning how to ask effective questions. Example: I am taking python 101 at Fubar University. I have been given the following assignment: Here you tell us the entire assignment not just one sentence from it. Then you show us whatever attempt you have made to solve the problem. Have you written any Python program? Show us the program. Tell us where you are stuck. We really like to help but we do not have any crystal balls to look into. Help us understand your situation fully. Bob Gailer From alan.gauld at yahoo.co.uk Wed Nov 28 08:20:31 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 28 Nov 2018 13:20:31 +0000 Subject: [Tutor] I need help with my project In-Reply-To: References: Message-ID: On 28/11/2018 00:30, Treyton Hendrix wrote: > If the user selected a sandwich, french fries, and a beverage, reduce the > total cost of the order by $1.00. > > This is what I have to do and I don't know where to start. Neither do we because we don't know what you are talking about. There is no context. How does a user select these items? Is it a web page? a GUI? A CLI? An in-store sensor that detects items being removed from the fridge? (Maybe under the control of a Raspbery Pi or Arduino?) And how do you know the original prices? Are they hard coded? read from a data file or database? Input by the user? Or maybe the fridge sensor reads bar codes attached to the actual items? How do you calculate the total? Is it stored in a variable? Is it stored as a floating point value (you should never do that for money!) or as an integer number of pennies/cents? If the latter total -= 100 Is the obvious answer to your question. Is it even in code or do you just write it down on paper? Or maybe you are using an electronic calculator? I could go on but hopefully you see how little information you have given us to work from.? If it is in code show us what you hae so far and we can help you add this feature. Without any clue what you are doing we can't really help very much. -- 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 avigross at verizon.net Wed Nov 28 11:44:28 2018 From: avigross at verizon.net (Avi Gross) Date: Wed, 28 Nov 2018 11:44:28 -0500 Subject: [Tutor] I need help with my project In-Reply-To: References: Message-ID: <006b01d48739$a13d6cf0$e3b846d0$@verizon.net> I suggest starting at the beginning when asking a question to people who have no way of knowing what you have not told them. Your sentence is completely in middle or even near the end of something that has to be larger: " If the user selected a sandwich, french fries, and a beverage, reduce the total cost of the order by $1.00." It sounds like you are being asked to emulate a place you can buy food. So I suspect you need to display a menu, ask the user to enter info, keep a running total and so on. So how do you think you would store the order as well as whatever you need to know about past sales or the non-discounted cost of the current order? How do you see if the current order has all the parts needed for the discount. Do you need to apply the discount just once or multiple times if they ordered food for lots of people? Sure, we could write some code for you, but perhaps you need to think it through first and maybe if you get stuck, ask if someone can tell you what is wrong with the code. Given what you sent, the answer is easy. RECOGNIZE they placed an order that meets the criterion: SUBTRACT 1.00 from their order. But that is not written in Python for obvious reasons. -----Original Message----- From: Tutor On Behalf Of Treyton Hendrix Sent: Tuesday, November 27, 2018 7:30 PM To: tutor at python.org Subject: [Tutor] I need help with my project If the user selected a sandwich, french fries, and a beverage, reduce the total cost of the order by $1.00. This is what I have to do and I don't know where to start. _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From avigross at verizon.net Wed Nov 28 12:56:32 2018 From: avigross at verizon.net (Avi Gross) Date: Wed, 28 Nov 2018 12:56:32 -0500 Subject: [Tutor] user overloaded Message-ID: <007801d48743$b29b0750$17d115f0$@verizon.net> OOPS, Sorry Steve. Yes, I am on multiple mailing lists and the tutor list by default replies to the sender and you need to edit it to reply to the group. I will be careful. Your other point is broader. There are lots of operations (not just ion python) that are easy to misuse. Sometimes the same symbol is used in multiple ways, such as how parentheses can mean a tuple even when you wanted to use them for ordering. There was a recent discussion about how the try statement changed after 2.x so instead of the part that used to say: try ... except ValueError, e: ... You now need to do this format: try ... except ValueError as e: ... Why the change? I have not looked to see, but note the earlier version is sort of a tuple without parentheses which Python allows in many contexts. As the keyword "as" is now used in other places like import statements so why not make things more consistent. But to others who just see a CHANGE and wonder why, many things seem mysterious. Yes, read the manual but that is not something new users seem to want to do, let alone know how to find what to read and slog through lots of pages. Back to your topic. "=" as a symbol has been used over the years in many ways. In normal mathematics, it is mostly seen as a comparison operator as in tan(x) = sin(x)/cos(x) Many programming languages used it instead to sort of mean MAKE EQUALS. x = x + 5 is an example where it makes no mathematical sense. How can a number be equal to itself when 5 is added? The meaning clearly is something like x gets assigned a value by evaluating the current value of x and adding 5 to that. When done, the old value of x is gone and it has a new value. For this reason, some languages like PASCAL used := so the above became x := x + 5 Yes, same symbol we are discussing and NOT what Python intends. Languages like R also decided to not use a naked "=" sign and encouraged arrow notation like: x <- x + 5 and even x + x -> x And yes, for global variables there are --> and <-- and worse. But they still accept a naked "=" as well and use that alone in function definitions and calls for setting values by name. Yes, it can confuse people when an assignment is "<-" and a comparison is "<=" . And there are forms of equality that some languages need to distinguish. What does it mean if two (unordered) sets are equal? Must they be two reference to the same set or is it shorthand for them being strict subsets of each other in both directions? You can come up with additional abstractions like whether a list is a shallow or deep copy of another and how it effects your ideas about them being equal. You may want a way to say things are approximately equal. In math, something like ~= is used. Python has an operator called "is" that can be a variant on equality. But it can be subtle when python reuses an immutable object and they seem to be the same even when you try to make them different: >>> a = "supercalifragilisticexpialidocious" * 20 >>> b = "supercalifragilisticexpialidocious" * 10 + "supercalifragilisticexpialidocious" * 10 >>> a == b True >>> a is b True Weird. Try that with mutables: >>> a = [ 'hello' ] >>> b = [ 'hello' ] >>> a == b True >>> a is b False For people new to computer science or a particular programming language, there can be too much and users get overloaded by all the overloading. Adding yet another variant to have a side-effect within an expression using ":=" will extend some of the power of python but at a cost for some, and especially new users. Consider the concept of OR. Some languages spell it out when used in a Boolean context. a or b But python does something new with this. In some contexts the above effectively becomes an IF. >>> a = 5 >>> b = 10 >>> x = a or b >>> x 5 >>> a = 0 >>> x = a or b >>> x 10 So because python has a creative definition of what is True and What is False, the above is sort of this code: if ( a EVALUATES TO TRUE) x = a else x = WHATEVER b EVALUATES to Since I chose integers, a is true whenever it is not precisely zero. For lists and many other objects, they are True if not empty. That is not the same meaning of "or" that many languages use. Add other meanings like a "bitwise or", "exclusive or" or the union of two sets and you can see people get confused especially when the same or similar symbols are used. Many languages have a symbol like "|" and another like "||" for example. Arguably python allows users to invent their own overloading in classes so that using some version of "or" between two objects means whatever you want it to mean. I recently (for fun) created a sort of auto-increment (as in the C/C++ symbol ++) so that if you had a counter called x, then adding ANYTHING to x using the += notation just incremented it by 1. X = odd_object(6) # X initialized to 6 X += 1 # X is now 7 X += 12 # X is now 8 as the __iadd__ method ignores the 12 and adds just 1 The above abomination does not replace ++ as an autoincrement but can give you some functionality but many users will have no idea what just happened. I note it can be worse in languages that allow you to create your own operators. I often use %>% in R for example and can make my own by placing almost any nonsense between percent signs and binding it to a function. As I understand it, back to Python, if adding a := to be used in expressions does not break any existing code, it may not be so bad. I mean nobody is supposed to be using that symbol now, so what does it hurt? Ah, but wait. If I had the previously discussed notion of checking whether the string I wanted to dynamically evaluate contained possibly dangerous code, would it be looking for a ":=" ? Heck, if I am parsing something and looking for a colon that indicates the beginning of a BODY, might it get confused by finding this colon in a wrong context? I conclude by saying life is complicated and then you die. I mean there are many places where similar but not identical things happen in languages as well as life. -----Original Message----- From: Tutor On Behalf Of Steven D'Aprano Sent: Wednesday, November 28, 2018 4:13 AM To: tutor at python.org Subject: Re: [Tutor] PEP 572 -- Assignment Expressions On Tue, Nov 27, 2018 at 08:26:04PM -0500, Avi Gross wrote: > Ivo, You've replied to the wrong mailing list. Ivo wrote to Python-List, not Tutor. But now that you've raised the issue... [Ivo asked] > Why: > if (match := pattern.search(data)) is not None: > # Do something with match > > What is wrong with: > if match = pattern.search(data) is not None: > # Do something with match > > Assignment expression or assignment statement, it's an assignment, right? > It is very clear to everyone that it's an assignment! Can't it all > just be a "="? It seems that Ivo didn't read the PEP because that is discussed: https://www.python.org/dev/peps/pep-0572/#id32 Despite Ivo's statement that it is "clear to everyone", the problem is that whenever you see an assignment x=y in an assignment, there's no obvious way of telling whether they meant assignment or it was a mistyped equality test x==y. So it is not clear at all. In C, this confusion between = and == is a frequent source of serious bugs, including one attempt at inserting a back-door into the Linux kernel: https://freedom-to-tinker.com/2013/10/09/the-linux-backdoor-attempt-of-2003/ https://lwn.net/Articles/57135/ Requiring the colon x:=y makes it less likely to mess up the assignment or comparison. -- Steve _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From steve at pearwood.info Wed Nov 28 18:15:18 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 29 Nov 2018 10:15:18 +1100 Subject: [Tutor] user overloaded In-Reply-To: <007801d48743$b29b0750$17d115f0$@verizon.net> References: <007801d48743$b29b0750$17d115f0$@verizon.net> Message-ID: <20181128231516.GZ4319@ando.pearwood.info> On Wed, Nov 28, 2018 at 12:56:32PM -0500, Avi Gross wrote: > try ... > except ValueError, e: > ... > > You now need to do this format: > > try ... > except ValueError as e: > ... > > Why the change? Because the first form was a trap and a bug magnet: try: block except TypeError, ValueError, KeyError: print "surprise! this will not catch key errors!" [...] > Back to your topic. "=" as a symbol has been used over the years in many > ways. In normal mathematics, it is mostly seen as a comparison operator as > in tan(x) = sin(x)/cos(x) That's not a comparison, that's an identity. The tangent function is (or at least can be) defined as sine divided by cosine. > Many programming languages used it instead to sort of mean MAKE EQUALS. That's usually called "assignment" or "binding". > x = x + 5 > > is an example where it makes no mathematical sense. How can a number be > equal to itself when 5 is added? There is no solution there in the normal real number system, but there are forms of arithmetic where it does, e.g. "clock arithmetic". If your clock only has values 0 through 4, then adding 5 to any x will give you x back again. Although normally we write "congruent" rather than "equals" for clock arithmetic: x ? x + 5 (mod 5) In any case, just because there is no solution doesn't mean that the question makes no sense: x = x*5 makes perfect sense and has solution 0. Likewise: x = x**2 + k makes perfect sense too, and can have zero, one or two solutions depending on the value of k. > The meaning clearly is something like x > gets assigned a value by evaluating the current value of x and adding 5 to > that. When done, the old value of x is gone and it has a new value. > > For this reason, some languages like PASCAL used := so the above became > > x := x + 5 > > Yes, same symbol we are discussing and NOT what Python intends. Actually, it is precisely what Python intends, except for two minor differences: 1. in Pascal, := assignment is a statement, not an expression, while in Python it will be an expresion; 2. Python will discourage people from using the := expression form as a de facto statement, where = will do. [...] > And there are forms of equality that some languages need to distinguish. > What does it mean if two (unordered) sets are equal? Must they be two > reference to the same set or is it shorthand for them being strict subsets > of each other in both directions? You can come up with additional > abstractions like whether a list is a shallow or deep copy of another and > how it effects your ideas about them being equal. You may want a way to say > things are approximately equal. In math, something like ~= is used. Python > has an operator called "is" that can be a variant on equality. The "is" operator is in no way, shape or form an equality test. It is an *identity* test, which returns True if and only if the two operands are the same object. Equality does not imply identity: py> x = [] py> (x == []) and (x is not []) True nor does identity imply equality: py> NAN = float(NAN) py> (NAN is NAN) and (NAN != NAN) True Anyone using "is" when then test for equality is programming in a state of sin. > Consider the concept of OR. Some languages spell it out when used in a > Boolean context. > > a or b > > But python does something new with this. In some contexts the above > effectively becomes an IF. This is called a "short-circuiting boolean operator", and it was old, old old when Python started using it 25+ years ago. I believe Lisp was the first to introduce them, in the 1950s. https://en.wikipedia.org/wiki/Short-circuit_evaluation -- Steve From mats at wichmann.us Wed Nov 28 20:49:40 2018 From: mats at wichmann.us (Mats Wichmann) Date: Wed, 28 Nov 2018 18:49:40 -0700 Subject: [Tutor] online interactive Py resource? Message-ID: I'm going to end up tutoring someone (family member) learning Python in the new year. Look forward to it. It will be a remote arrangement. Haven't trained anyone in the whole picture (as opposed to bits and bobs of possibly helpful advice on demand, like here) since I wrote and delivered a training course for a commercial training company, the last instance of which was pre-historic in tech terms (2002 in calendar terms). That those were very much hands-on, in-person. It occurs to me it would be cool to work interactively in a distributed Internet editing environment that could run Python code. Are there such? There are distributed editing environments, like Etherpad, Gobby, (cough) Google Docs. There are online interactive Python environments, such as the ones from the nice folks at PythonAnywhere. is there something that combines the two? I'm not completely sure PythonAnywhere doesn't have that capability... but I'm looking for some wisdom and experiences (and maybe best practices) from the other tutors around here. cheers, -- mats From alan.gauld at yahoo.co.uk Thu Nov 29 03:17:03 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 29 Nov 2018 08:17:03 +0000 Subject: [Tutor] online interactive Py resource? In-Reply-To: References: Message-ID: On 29/11/2018 01:49, Mats Wichmann wrote: > It occurs to me it would be cool to work interactively in a distributed > Internet editing environment that could run Python code. Why not just share your desktop? I assume you trust the person you are mentoring?! I don't recall exactly how you do that but we used it a lot when I was working in a Windows world.... Netmeeting maybe? -- 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 avigross at verizon.net Wed Nov 28 21:04:36 2018 From: avigross at verizon.net (Avi Gross) Date: Wed, 28 Nov 2018 21:04:36 -0500 Subject: [Tutor] writer overloaded Message-ID: <005f01d48787$e14a3aa0$a3deafe0$@verizon.net> Steve, I read your reply in which you address selected parts of my earlier message. I conclude I am either not writing as clearly as I hoped or your interpretation is sometimes not what I intend. And, I can also be wrong or incomplete! I won't reply in-line or comment much on what I accept or agree with. Your comment about a good reason for the change in error handling makes perfect sense. But it relates to what I said in a way. The sequence of values allowed is a tuple of any length but it there are two or more, it gets ambiguous whether the last one is a special one used as a variable name you assign the particular error to or yet another kind of error. Adding "as" changes the syntax to "tuple [as name]" which is unambiguous and also allows "name" to be a reuse of a name that might otherwise happen to be an Error that wouldn't happen in this context but does exist. My focus is on what I said about Mathematics. I have very specific approaches to what I mean by mathematics and it is a very broad field with many seemingly different kinds which may overlap on careful study but which can each have their own use of concepts and symbols. Yes, mathematics as in routine algebra and geometry has a fairly serious focus on equality, often tautology, especially in higher and more abstract forms as when making proofs step by step. There are related concepts like congruence where you might not use an equals sign or inequalities where you use variants like <=. When a mathematician wants to SET a variable to a value, they often say things like: Given: Y = 5 Or Let Y = 5 when X = 10 be the sides of a rectangle then the Area = X*Y Or other phrases like "assuming" where the meaning remains that the combined expression on either side of the equals sign evaluates to the same thing. (X+1)*(X-1) = X^2 - 1 literally tells you there are at least two ways to say something that are the same no matter what X is. tan(x) =- sin(x)/cos(x) Is the same idea but maybe not exactly for the reason you mention that you can calculate it that way. The definition of the sin(x) is based on a Euclidean right triangle where you have two other angles that are not 90 degrees (or pi/2 radians) and you pick one of those angles and call it x. For each such angle, there are two sides of the triangle that delineate the angle with one being called the "hypotenuse" (the longest one) and one being called "adjacent". I know this is very basic geometry trending into trigonometry but trying to be very clear here. The third side sort of "opposite" the angle is called that. Now the definition of sin(x) is the ratio of opposite/hypotenuse. When the angle is 0, there is no triangle, As it opens up wider toward 90 degrees, the ratio climbs from 0 to as high as 1, asymptotically. At the same time as the opposite side lengthens, the adjacent side gets smaller and the cos(x) is defined as adjacent/hypotenuse and moves from 1 down towards 0. If you define the tangent as opposite/adjacent, it can go towards plus and minus infinity but someone can show that the ratio of: sin(x)/cos(x) = (opposite/hypotenuse)/(adjacent/hypotenuse) the latter part can be converted from a division to a multiplication by using the reciprocal and other allowed mathematical conversions in the context of this algebra. = opposite/hypotenuse * hypotenuse/ adjacent = (opposite*hypotenuse)/(adjacent*hypotenuse) = (opposite/adjacent)*(hypotenuse/hypotenuse) = (opposite/adjacent) + 1 = opposite/adjacent = tan(x) There are many details in the proof above, or similar ones like when you are allowed to use associative or commutative laws because the real numbers over the operators of addition and multiplication form a ring which has been proven to allow the steps above but you get the idea. So, yes, one of many ways to calculate a tangent is to use the sine or cosine for a particular known angle but there are deeper ways such as evaluating enough terms of the right infinite series. I repeat. In most of mathematics "=" means they are equal or assumed to be equal with any assignment taking place sort of off stage. Feel free to disagree on what may be a mostly philosophical point but that is why many places in mathematics do not feel a need for a second operator. I am trying to keep this as "text" since the mailing list does not tolerate rich formats so it is harder to illustrate. But there are mathematical operators that make provisional assignments possible. The upside-down A can be used to mean "for all x" and the backwards E can mean "there exists an x" and there are ways to specify conditions and intended results and so on. So yes, Mathematics as a whole can say the kind of things you want. But generally the way you might say to do something to x and store the results is to introduce a different variable such as x' and then x''. Programming languages definitely need a way to rebind but you could write perfectly usable code like this: x = 5 temp = x x = temp^2 +2*temp +1 The above would still rebind x but would not show it having two different values at the same time, seemingly on the same line. But, as long as you understand precedence rule or use lots of parantheses, it is clear that everything to the left of the equals sort of happens first. Mind you, the right side can be complex and take a while to evaluate to an object such as: a.b()["key1"]["key2"][5] = a a might have a method called b which returns a dictionary whose entry for key1 is yet another dictionary whose result is a list whose sixth entry you want to set to point back to what a is or is it was? And as noted, in languages that support pre as well as post autoincrement operators, you might need multiple temp variables to do the equivalent of: while (*p++ == *++q) ; bad example as python hides pointers so how about something else python does not support ? x++ = --y Your point about peano arithmetic is indeed valid. There are many places where you can indeed have something that looks nonsensical still have valid solutions and even an infinite number of such solutions. x = (x + 5) mod N is sort of what you meant. And I don't see what that disproves much as it involves a new operator. Both standard mathematics and python would see that as valid. In your example, both x are the same. I mean the same each time you try a new solution, not always the same. Similarly, asking what angle has a particular sine value has an infinite number of answers as adding or subtracting any integral multiple of 360 degrees (or 2*pi radians) gives you the same result. That is partially because this is in a sense peano arithmetic mod 360. ? The mathematical definition of "=" in such cases is not really different. Any of the valid answers is equal to the result, although for some purposes, you want the lowest positive answer meaning between 0 and 360. And the result is in a context. Complex numbers are hard to visualize but they are often the solutions to quadratic or higher-order equations. Without that context, you say there are no solutions, or at least two fewer if they are complex conjugates. If the two roots of an equation are: x=5 OR x=6 There is no contradiction. Two cases are being examined. X is at times one and at times another, but never both at the same time. You are quibbling about what I mean about the python "is" statement. Yes, it is an identity test but I repeat. I stand by my statement. It is about pointers or memory locations being compared for absolute equality. Does the first variable or expression refer to or point to the same location in memory as the second. I gave examples of how python when trying to use less memory interferes by making two different objects share the same area of memory in some cases when they are deemed immutable. It may not matter then but if your code depends on them not being seen as equal, you may have to do weird things like making them into objects that may internally point to the same string or number but then ask if object A is object B and then they are not. When discussing whether the absolute values of two things are the same, you are saying something different than whether the two things are the same. Similar the length/magnitude of two vectors (in the mathematical sense of the square root of the sum of the squares denoting a distance from a multi-dimensional origin in N-space) can be equal even if the two vectors share no components. The address of an object is another such function and "is" generally seems to be testing the current address, sort of. There may well be exceptions. I stand by my overall point. There are MANY ways people compare things for equality and some have subtle nuances. If this were something like C where pointers are commonly used then p == q and *p == *q Have related but different meanings. If p points to the first 'l' (el) in "hello world" and so does q then both statements are true. If you move q to point to the second or third letter 'l' while leaving p in place, then only *p == *q is true. If q points to any non-'l' character in the same string or anywhere else where the byte pointed to (pre-unicode) does not have the same eight bits in the same order, then both of the above are false. In python they hide pointers but they are probably there in many of the underlying implementations, albeit some use other techniques with name spaces and dictionaries. And who knows what else. So in many cases, "p is q" means identity in a pointer way or memory location way. But clearly there can be exceptions such as evaluating p searches in one namespace and evaluating q in another, so they may not be deemed to be identical and actually aren't. You may have also missed my last point. Short circuit evaluation is old. That is not what I consider unusual in python. What I found to be different (and it is possible other languages had or now have it) is that it does not bother jumping into a mathematical Boolean mode. That is deferred indefinitely as not important. The result of quite a few operations is neither True nor False but just about ANYTHING. To explicitly make it force a Boolean result you need to call bool(A or B and not C) which otherwise simply return A or B or C, whatever they are and the truthiness of it is determined by either it having a length of zero, or being numerically 0 or whatever the __nonzero__ (pre 3.X) or __bool__ methods for that object return, perhaps recursively if they return another object. It turns out, mathematically, to be valid and work in an amazing number of contexts and even where it might not, you can probably use the __bool__ method to make something that works. Sorry to be this long. I could have summed up my entire previous message in a few sentences but then you might have said I was too broad and showed no reasons or examples. As I said, life is short. -----Original Message----- From: Tutor On Behalf Of Steven D'Aprano Sent: Wednesday, November 28, 2018 6:15 PM To: tutor at python.org Subject: Re: [Tutor] user overloaded On Wed, Nov 28, 2018 at 12:56:32PM -0500, Avi Gross wrote: > try ... > except ValueError, e: > ... > > You now need to do this format: > > try ... > except ValueError as e: > ... > > Why the change? Because the first form was a trap and a bug magnet: try: block except TypeError, ValueError, KeyError: print "surprise! this will not catch key errors!" [...] > Back to your topic. "=" as a symbol has been used over the years in > many ways. In normal mathematics, it is mostly seen as a comparison > operator as in tan(x) = sin(x)/cos(x) That's not a comparison, that's an identity. The tangent function is (or at least can be) defined as sine divided by cosine. > Many programming languages used it instead to sort of mean MAKE EQUALS. That's usually called "assignment" or "binding". > x = x + 5 > > is an example where it makes no mathematical sense. How can a number be > equal to itself when 5 is added? There is no solution there in the normal real number system, but there are forms of arithmetic where it does, e.g. "clock arithmetic". If your clock only has values 0 through 4, then adding 5 to any x will give you x back again. Although normally we write "congruent" rather than "equals" for clock arithmetic: x ? x + 5 (mod 5) In any case, just because there is no solution doesn't mean that the question makes no sense: x = x*5 makes perfect sense and has solution 0. Likewise: x = x**2 + k makes perfect sense too, and can have zero, one or two solutions depending on the value of k. > The meaning clearly is something like x > gets assigned a value by evaluating the current value of x and adding 5 to > that. When done, the old value of x is gone and it has a new value. > > For this reason, some languages like PASCAL used := so the above became > > x := x + 5 > > Yes, same symbol we are discussing and NOT what Python intends. Actually, it is precisely what Python intends, except for two minor differences: 1. in Pascal, := assignment is a statement, not an expression, while in Python it will be an expresion; 2. Python will discourage people from using the := expression form as a de facto statement, where = will do. [...] > And there are forms of equality that some languages need to distinguish. > What does it mean if two (unordered) sets are equal? Must they be two > reference to the same set or is it shorthand for them being strict subsets > of each other in both directions? You can come up with additional > abstractions like whether a list is a shallow or deep copy of another and > how it effects your ideas about them being equal. You may want a way to say > things are approximately equal. In math, something like ~= is used. Python > has an operator called "is" that can be a variant on equality. The "is" operator is in no way, shape or form an equality test. It is an *identity* test, which returns True if and only if the two operands are the same object. Equality does not imply identity: py> x = [] py> (x == []) and (x is not []) True nor does identity imply equality: py> NAN = float(NAN) py> (NAN is NAN) and (NAN != NAN) True Anyone using "is" when then test for equality is programming in a state of sin. > Consider the concept of OR. Some languages spell it out when used in a > Boolean context. > > a or b > > But python does something new with this. In some contexts the above > effectively becomes an IF. This is called a "short-circuiting boolean operator", and it was old, old old when Python started using it 25+ years ago. I believe Lisp was the first to introduce them, in the 1950s. https://en.wikipedia.org/wiki/Short-circuit_evaluation -- Steve _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From avigross at verizon.net Wed Nov 28 22:36:34 2018 From: avigross at verizon.net (Avi Gross) Date: Wed, 28 Nov 2018 22:36:34 -0500 Subject: [Tutor] online interactive Py resource? In-Reply-To: References: Message-ID: <007401d48794$ba1561a0$2e4024e0$@verizon.net> Mats, This may not be a direct answer to your question but I have used more generic tools in a way that might make some sense. You can use a videoconference tool such as SKYPE to connect and have one of you share your screen with the otgher so you can see whatever is open such as an editor they are editing code in and another running python interactively as well as any pop-up windows and so on. You can talk them through things like change the next line ... You can also have part of the screen on which you can do text chat such as sending them some text to paste into their program or even push a download of files or receive them. Not quite the same shared editing experience but there are ways to use something like Google Docs to collaborate and perhaps even set up to run python directly on the remote file or just copy/paste it. Of course, the shared editor would ideally not be a full-blown text processor or even understand python code. There are also ways to take over their computer (with permission) and have it accept your commands as you demonstrate things. I recall a web site I once used in a course that allowed me to crerate and save python code that it ran remotely: http://www.codeskulptor.org/ Each time you save a file, you get a URL like this: http://www.codeskulptor.org/#user45_M9Oefx1vom_0.py If you click on that URL it should show the minor code I made and let you edit it and then I can click and see the change. This being an open forum, multiple people may do so. On that screen, the leftmost small menu item near the top runs the code with output on the right panel. The second saves the current code under the same URKL that you maybe would bookmark. Other controls let you save a local copy, etc. Note there is a version 3 version to use at: https://py3.codeskulptor.org/ Some of these techniques may be helpful for anyone wanting some interaction but I do not claim they handle well having two people edit files at the same time. You can always GIT. Avi -----Original Message----- From: Tutor On Behalf Of Mats Wichmann Sent: Wednesday, November 28, 2018 8:50 PM To: tutor Subject: [Tutor] online interactive Py resource? I'm going to end up tutoring someone (family member) learning Python in the new year. Look forward to it. It will be a remote arrangement. Haven't trained anyone in the whole picture (as opposed to bits and bobs of possibly helpful advice on demand, like here) since I wrote and delivered a training course for a commercial training company, the last instance of which was pre-historic in tech terms (2002 in calendar terms). That those were very much hands-on, in-person. It occurs to me it would be cool to work interactively in a distributed Internet editing environment that could run Python code. Are there such? There are distributed editing environments, like Etherpad, Gobby, (cough) Google Docs. There are online interactive Python environments, such as the ones from the nice folks at PythonAnywhere. is there something that combines the two? I'm not completely sure PythonAnywhere doesn't have that capability... but I'm looking for some wisdom and experiences (and maybe best practices) from the other tutors around here. cheers, -- mats _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From breamoreboy at gmail.com Thu Nov 29 08:31:44 2018 From: breamoreboy at gmail.com (Mark Lawrence) Date: Thu, 29 Nov 2018 13:31:44 +0000 Subject: [Tutor] writer overloaded In-Reply-To: <005f01d48787$e14a3aa0$a3deafe0$@verizon.net> References: <005f01d48787$e14a3aa0$a3deafe0$@verizon.net> Message-ID: On 29/11/2018 02:04, Avi Gross wrote: [another massive snip] Please take this some place else as I see no place on this *TUTOR* list for your ramblings. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From asad.hasan2004 at gmail.com Thu Nov 29 07:53:42 2018 From: asad.hasan2004 at gmail.com (Asad) Date: Thu, 29 Nov 2018 18:23:42 +0530 Subject: [Tutor] Search error code in a logfile and print all lines until the error code Message-ID: Hi All , I need some help to print error lines from a log file : for example the following logfile : SQL> SQL> SET PAGESIZE 0 SQL> SELECT 'Starting apply for patch 26925263/21857460 on ' || 2 SYSTIMESTAMP FROM dual; Starting apply for patch 26925263/21857460 on 28-JUN-18 07.46.34.893957 PM -05:00 1 row selected. SQL> SET PAGESIZE 10 SQL> SQL> BEGIN 2 dbms_sqlpatch.patch_initialize(p_patch_id => 26925263, 3 p_patch_uid => 21857460, 4 p_flags => '&flags', 5 p_description => '&description', 6 p_action => 'APPLY', 7 p_logfile => '&full_logfile', 8 p_bundle_series => '&bundle_series'); 9 END; 10 / BEGIN * ERROR at line 1: ORA-20001: set_patch_metadata not called ORA-06512: at "SYS.DBMS_SQLPATCH", line 647 ORA-06512: at line 2 SQL> SQL> COLUMN install_file NEW_VALUE sql_script SQL> SQL> SQL> ALTER SESSION SET CURRENT_SCHEMA = SYS; Session altered. So here I want to open the log file and find the error and print all the lines from starting till the error : ORA-20001: set_patch_metadata not called f4 = open (r"file1.log", 'r') string2=f4.readlines() for i in range(len(string2)): position=i lastposition =position+1 while True: if re.search('ORA-20001: set_patch_metadata not called',string2[lastposition]): break elif lastposition==len(string2)-1: break else: lastposition += 1 errorcheck=string2[position:lastposition] print errorcheck It gives me error : IOPub data rate exceeded. The notebook server will temporarily stop sending output to the client in order to avoid crashing it. To change this limit, set the config variable `--NotebookApp.iopub_data_rate_limit`. Current values: NotebookApp.iopub_data_rate_limit=1000000.0 (bytes/sec) NotebookApp.rate_limit_window=3.0 (secs) the first for loop will search for the error and provide me the position of the error . I think the logic I am using is correct but need some expert comment to make it work also some guidance on how to print all the lines until the error is received . Please advice, -- Asad Hasan +91 9582111698 From muhammedesenn at gmail.com Thu Nov 29 04:24:24 2018 From: muhammedesenn at gmail.com (Muhammed Esen) Date: Thu, 29 Nov 2018 12:24:24 +0300 Subject: [Tutor] Trouble about creating database Message-ID: I can't create a database even tough I imported pymysql. I have Xammp on my Mac and I already started MySQL Database. What could be the problem? Thanks have a nice day. From breamoreboy at gmail.com Thu Nov 29 13:10:26 2018 From: breamoreboy at gmail.com (Mark Lawrence) Date: Thu, 29 Nov 2018 18:10:26 +0000 Subject: [Tutor] Trouble about creating database In-Reply-To: References: Message-ID: On 29/11/2018 09:24, Muhammed Esen wrote: > I can't create a database even tough I imported pymysql. I have Xammp on my > Mac and I already started MySQL Database. What could be the problem? > Thanks have a nice day. > Please help us to help you by providing a small code snippet that reproduces the problem and either the complete traceback or the error message. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From alan.gauld at yahoo.co.uk Thu Nov 29 13:46:00 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 29 Nov 2018 18:46:00 +0000 Subject: [Tutor] Trouble about creating database In-Reply-To: References: Message-ID: On 29/11/2018 09:24, Muhammed Esen wrote: > I can't create a database even tough I imported pymysql. I have Xammp on my > Mac and I already started MySQL Database. What could be the problem? Just about anything since you haven't shown us your code. It could be a syntax error, a problem with arguments to one of the functions, an access control error with passwords etc A network or server problem, etc... We can't begin to guess unless you show us the code you used and the full error message.. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alan.gauld at yahoo.co.uk Thu Nov 29 13:57:40 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 29 Nov 2018 18:57:40 +0000 Subject: [Tutor] Search error code in a logfile and print all lines until the error code In-Reply-To: References: Message-ID: On 29/11/2018 12:53, Asad wrote: > Hi All , > > I need some help to print error lines from a log file : I don;t have time right now for a detailed analysis but there are some obvious starting points: > f4 = open (r"file1.log", 'r') > string2=f4.readlines() > for i in range(len(string2)): Its usually easier to just use for line in f4: process line here... > position=i > lastposition =position+1 > while True: > if re.search('ORA-20001: set_patch_metadata not > called',string2[lastposition]): You don;t need to use regular expressions for fixed strings. A regular string search will be more efficient. if searchString in line > break > elif lastposition==len(string2)-1: If you uyse the for line in file idiom you don't need this check. The loop will never run off the end. > break That break is not indented so will always exit the loop on the first go round. I'll assume for now its just a typo or email glitch... > else: > lastposition += 1 > errorcheck=string2[position:lastposition] > print errorcheck > I think the logic I am using is correct but need some expert comment to > make it work also some guidance on how to print all the lines until the > error is received . The easiest way to do this is usually to set a flag True when you hit the start line and then false when you hit the end. print while the flag is True for line in file: if isStart(line): flag = True if isEnd(line): flag = False if flag: print line Now write the isStart(line) and isEnd(line) functions and you should be good to go. Sorry if I'm missing something key, as I said I just skimmed this. gotta run, -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From mats at wichmann.us Thu Nov 29 14:48:41 2018 From: mats at wichmann.us (Mats Wichmann) Date: Thu, 29 Nov 2018 12:48:41 -0700 Subject: [Tutor] Error Python version 3.6 does not support this syntax. In-Reply-To: References: Message-ID: <0e1b086f-f2e5-a668-417f-78934a9c47cb@wichmann.us> On 11/29/18 12:20 PM, srinivasan wrote: > Dear Python Experts, > > With the below code snippet, I am seeing the below error, I am using > python 3.6, could you please what could be the issue? > self.child = pexpect.spawn("bluetoothctl", echo = False) ... > self.child.send(command + "\n") > time.sleep(pause) > start_failed = self.child.expect(["bluetooth", pexpect.EOF]) ... > return self.child.before.split("\r\n") > -------------------------------------------------------------------------------> > the issue seems to be here > line 27, in get_output > return self.child.before.split("\r\n") > TypeError: a bytes-like object is required, not 'str' your types don't match. it's Python 3 so what you get back from talking to an external process is a bytes object. you're calling the split method on that object, but passing it a string to split on - that's what the error is saying. It shouldn't be any more complicated to fix that than to give it a byte object: return self.child.before.split(b"\r\n") or... decode the object to a str before splitting on a string. but since you're specifically splitting on lines, you may as well use the splitlines method instead of the split method, since that's what it is designed for. From cs at cskk.id.au Thu Nov 29 17:18:47 2018 From: cs at cskk.id.au (Cameron Simpson) Date: Fri, 30 Nov 2018 09:18:47 +1100 Subject: [Tutor] Error Python version 3.6 does not support this syntax. In-Reply-To: References: Message-ID: <20181129221847.GA84153@cskk.homeip.net> On 30Nov2018 02:19, srinivasan wrote: >Thanks a lot for your quick responses, again the below line seems to >be throwing the same error, is that should I again decode the line >where am facing the issue to str? or could you please let me if there >is any alternative solution for the same or workaround in python 3.6? > >Code Snippet: > > def parse_device_info(self, info_string): > """Parse a string corresponding to a device.""" > device = {} > block_list = ["[\x1b[0;", "removed"] > string_valid = not any(keyword in info_string for keyword in >block_list) ---------------------------> Again this line seems to be >the same issue [...] > def get_paired_devices(self): > """Return a list of tuples of paired devices.""" > try: > out = self.get_output("paired-devices") > except BluetoothctlError as e: > print(e) > return None > else: > paired_devices = [] > for line in out: > device = self.parse_device_info(line) [...] Your problem is basicly that reading from command output gets you bytes data, not text (str) data. This is because pipes transfer bytes; that the command may issue text simply means that those bytes are an encoding of the text. Your entire process treats the output as text, because the commands issue textual output. Therefore, the most sensible thing to do at this point is to _decode_ the bytes into text as soon as you get them from the command, and then the rest of your programme can work in text (str) from then on. So I would be inclined to change: for line in out: device = self.parse_device_info(line) into (untested): for line_b in out: line = line_b.decode(errors='replace') device = self.parse_device_info(line) That will turn line_b (a bytes object holding the line) into text before you try to do any parsing. From that point onward, everything is text (str) and you do not need to put any bytes->str stuff elsewhere in your programme. Some remarks: That decode line above uses the default bytes->str decoding, which is 'utf-8'. That is probably how your system works, but if it is not you need to adjust accordingly. If the command ussues pure ASCII you'll be fine regardless. The decode uses "errors='replace'", which means that if the bytes data are _not_ correct UTF-8 encoded text, the decoder will put some replacement characters in the result instead of raising an exception. This simplifies your code, and since you're parsing the output anyway for infomation the replacements should show up to your eye. The default decode mode is 'strict', which would raise an exception on an invalid encoding. Purists might decode the output stream ("out") before the for-loop, but in most encodings including UTF-8, you can split on newlines (byte code 10, ASCII NL) safely anyway, so we just split first and decode each "bytes" line individually. In the loop I deliberately iterate over "line_b", and have the decoded text in "line", instead of doing something like: for line in out: line = line.decode(....) That way I am always sure that I intend to talk about bytes in one place (line_b) and text (line) in another. Having variable that might contain bytes _or_ text leads to confusion and difficult debugging. The core takeaway here is that you want to keep in mind whether you're working in bytes or text (str). Keep the division clean, that way all you other code can be written appropriately. So: the command pipe output is bytes. COnvert it to text before passing to your text parsing code. That way all the parsing code can work in text (str) and have no weird conversion logic. Cheers, Cameron Simpson From srinivasan.rns at gmail.com Thu Nov 29 14:20:49 2018 From: srinivasan.rns at gmail.com (srinivasan) Date: Fri, 30 Nov 2018 00:50:49 +0530 Subject: [Tutor] Error Python version 3.6 does not support this syntax. In-Reply-To: References: Message-ID: Dear Python Experts, With the below code snippet, I am seeing the below error, I am using python 3.6, could you please what could be the issue? Code Snippet: ------------------- import time import pexpect import subprocess import sys class BluetoothctlError(Exception): """This exception is raised, when bluetoothctl fails to start.""" pass class Bluetoothctl: """A wrapper for bluetoothctl utility.""" def __init__(self): out = subprocess.check_output("rfkill unblock bluetooth", shell = True) self.child = pexpect.spawn("bluetoothctl", echo = False) def get_output(self, command, pause = 0): """Run a command in bluetoothctl prompt, return output as a list of lines.""" self.child.send(command + "\n") time.sleep(pause) start_failed = self.child.expect(["bluetooth", pexpect.EOF]) if start_failed: raise BluetoothctlError("Bluetoothctl failed after running " + command) return self.child.before.split("\r\n") -------------------------------------------------------------------------------> the issue seems to be here def start_scan(self): """Start bluetooth scanning process.""" try: out = self.get_output("scan on") except BluetoothctlError as e: print(e) return None if __name__ == "__main__": print("Init bluetooth...") bl = Bluetoothctl() print("Ready!") bl.start_scan() print("Scanning for 10 seconds...") for i in range(0, 10): print(i) time.sleep(1) Error Logs: ------------- /home/srinivasan/Downloads/bt_tests/qa/venv/bin/python /home/srinivasan/Downloads/bt_tests/qa/test_library/bt_tests.py Init bluetooth... Ready! Traceback (most recent call last): File "/home/srinivasan/Downloads/bt_tests/qa/test_library/bt_tests.py", line 169, in bl.start_scan() File "/home/srinivasan/Downloads/bt_tests/qa/test_library/bt_tests.py", line 32, in start_scan out = self.get_output("scan on") File "/home/srinivasan/Downloads/bt_tests/qa/test_library/bt_tests.py", line 27, in get_output return self.child.before.split("\r\n") TypeError: a bytes-like object is required, not 'str' Process finished with exit code 1 On Wed, Nov 28, 2018 at 12:17 AM Mats Wichmann wrote: > > On 11/27/18 5:50 AM, srinivasan wrote: > > Dear Python Experts, > > > > As still I am newbie and learning python, I am trying to reuse the > > Bluetoothctl wrapper in Python from the link ( > > https://gist.github.com/egorf/66d88056a9d703928f93) I am using python3.6 > > version, In pycharm editor on the bold highlighted code snippets I see the > > error message "Python version 3.6 does not support this syntax.", > > once again you've posted in a way that inserts lots of extra crud, you > avoided that last time. > > The syntax change is simple (and works on most older Pythons too): > > except ErrorType, e: > > becomes > > except ErrorType as e: > > > > > > Could you please how help me how the below highlighted lines of code can be > > can be ported to python3.6 version? > > > > * except BluetoothctlError, e:* > > > > * print(e)* > > * return None* > > From srinivasan.rns at gmail.com Thu Nov 29 15:49:25 2018 From: srinivasan.rns at gmail.com (srinivasan) Date: Fri, 30 Nov 2018 02:19:25 +0530 Subject: [Tutor] Error Python version 3.6 does not support this syntax. In-Reply-To: <0e1b086f-f2e5-a668-417f-78934a9c47cb@wichmann.us> References: <0e1b086f-f2e5-a668-417f-78934a9c47cb@wichmann.us> Message-ID: Dear Mats, Thanks a lot for your quick responses, again the below line seems to be throwing the same error, is that should I again decode the line where am facing the issue to str? or could you please let me if there is any alternative solution for the same or workaround in python 3.6? Code Snippet: def parse_device_info(self, info_string): """Parse a string corresponding to a device.""" device = {} block_list = ["[\x1b[0;", "removed"] string_valid = not any(keyword in info_string for keyword in block_list) ---------------------------> Again this line seems to be the same issue if string_valid: try: device_position = info_string.index("Device") except ValueError: pass else: if device_position > -1: attribute_list = info_string[device_position:].split(" ", 2) device = { "mac_address": attribute_list[1], "name": attribute_list[2] } return device def get_paired_devices(self): """Return a list of tuples of paired devices.""" try: out = self.get_output("paired-devices") except BluetoothctlError as e: print(e) return None else: paired_devices = [] for line in out: device = self.parse_device_info(line) if device: paired_devices.append(device) return paired_devices if __name__ == "__main__": print("Init bluetooth...") bl = Bluetoothctl() print("Ready!") bl.start_scan() print("Scanning for 10 seconds...") for i in range(0, 10): print(i) time.sleep(1) bl.pair("64:A2:F9:06:63:79") print("Pairing for 10 seconds...") for i in range(0, 10): print(i) time.sleep(1) # Seems to be an issue ---------- bl.get_paired_devices() print("Getting Paired devices for 10 seconds...") for i in range(0, 10): print(i) time.sleep(1) Error Logs: Traceback (most recent call last): , in parse_device_info string_valid = not any(keyword in info_string for keyword in block_list) File "/home/srinivasan/Downloads/bt_tests/qa/test_library/bt_tests.py", line 52, in string_valid = not any(keyword in info_string for keyword in block_list) TypeError: a bytes-like object is required, not 'str' On Fri, Nov 30, 2018 at 1:18 AM Mats Wichmann wrote: > > On 11/29/18 12:20 PM, srinivasan wrote: > > Dear Python Experts, > > > > With the below code snippet, I am seeing the below error, I am using > > python 3.6, could you please what could be the issue? > > > self.child = pexpect.spawn("bluetoothctl", echo = False) > ... > > self.child.send(command + "\n") > > time.sleep(pause) > > start_failed = self.child.expect(["bluetooth", pexpect.EOF]) > ... > > return self.child.before.split("\r\n") > > -------------------------------------------------------------------------------> > > the issue seems to be here > > line 27, in get_output > > return self.child.before.split("\r\n") > > TypeError: a bytes-like object is required, not 'str' > > your types don't match. it's Python 3 so what you get back from talking > to an external process is a bytes object. you're calling the split > method on that object, but passing it a string to split on - that's what > the error is saying. It shouldn't be any more complicated to fix that > than to give it a byte object: > > return self.child.before.split(b"\r\n") > > or... decode the object to a str before splitting on a string. > > but since you're specifically splitting on lines, you may as well use > the splitlines method instead of the split method, since that's what it > is designed for. > > > > > From steve at pearwood.info Thu Nov 29 22:48:29 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 30 Nov 2018 14:48:29 +1100 Subject: [Tutor] Error Python version 3.6 does not support this syntax. In-Reply-To: References: <0e1b086f-f2e5-a668-417f-78934a9c47cb@wichmann.us> Message-ID: <20181130034829.GH4319@ando.pearwood.info> On Fri, Nov 30, 2018 at 02:19:25AM +0530, srinivasan wrote: > Dear Mats, > > Thanks a lot for your quick responses, again the below line seems to > be throwing the same error, is that should I again decode the line > where am facing the issue to str? or could you please let me if there > is any alternative solution for the same or workaround in python 3.6? You don't need a "workaround", you need to fix the bug in your code: > Traceback (most recent call last): > , in parse_device_info > string_valid = not any(keyword in info_string for keyword in block_list) > File "/home/srinivasan/Downloads/bt_tests/qa/test_library/bt_tests.py", > line 52, in > string_valid = not any(keyword in info_string for keyword in block_list) > TypeError: a bytes-like object is required, not 'str' You get that error when you wrongly try to test for a string inside a bytes object: py> 'string' in b'bytes' Traceback (most recent call last): File "", line 1, in TypeError: a bytes-like object is required, not 'str' You fix that by making sure both objects are strings, or both are bytes, which ever is better for your program: py> 'a' in 'abc' # string in string is okay True py> b'a' in b'abc' # bytes in bytes is okay True py> 'a' in b'abc' # string in bytes is NOT OKAY Traceback (most recent call last): File "", line 1, in TypeError: a bytes-like object is required, not 'str' py> b'a' in 'abc' # bytes in string is NOT OKAY Traceback (most recent call last): File "", line 1, in TypeError: 'in ' requires string as left operand, not bytes Don't mix strings and bytes. It won't work. -- Steve From cs at cskk.id.au Fri Nov 30 18:14:02 2018 From: cs at cskk.id.au (Cameron Simpson) Date: Sat, 1 Dec 2018 10:14:02 +1100 Subject: [Tutor] I need help with my project In-Reply-To: <006b01d48739$a13d6cf0$e3b846d0$@verizon.net> References: <006b01d48739$a13d6cf0$e3b846d0$@verizon.net> Message-ID: <20181130231402.GA10155@cskk.homeip.net> Avi and Alan and Sibylle, you're making this a bit hard on the OP (Treyton). Yes he's supplied no context, but it is easy to make some suggestions. Each of yours suggests he design a much wider system (menu entry, web interface, some kind of GUI). All of which is (a) beyond him and (b) irrelevant. Why not pretend he _has_ the existing order, from wherever. Suggest ways to store that order (in a list, or a dict mapping ordable items to counts, or something). Then ask him to write a little Python, or even detailed English prose. Treyton: you seem to have recitied a homework question: >If the user selected a sandwich, french fries, and a beverage, reduce >the >total cost of the order by $1.00. >This is what I have to do and I don't know where to start. Ok, this is clear: Treyton can't get off the ground, very common for beginning programmers. The core challenge is to break your problem into a sequence of tasks. How would _you_, a person, do this if you had a food order given to you? Think about a food order. It is usually a list of standard food items, a count of how many of each. And each item will have a cost. The total cost is the sum of (each item's cost * its price * its count), for each item in the order. Or for all possible items, by presuming that unordered items just have a count of 0. So you need: A label for each item, so you can talk about it. You can just use a string for this, eg "sandwich" or "fries". Make the strings simple to start with to avoid spelling mistakes. You can always associate better names with the short strings later. You need a table of items and their costs. It is normal to make a mapping for this, such a Python's dict type. You can write dicts literally: costs = { "sandwich": 200, "fries": 100, } In the example above, I'm imagining you have dollars and cents, and making prices in cents. You also need a representation of the order, being the item type and the count. You could use a Python list for this. Example: [ "fries", 2 ] The whole order might be a list of those, example: [ ["fries", 2 ], [ "sandwich", 3 ] ] So, a list of lists. For purposes of your program you can just set all this stuff up at the beginning, not worrying about GUIs or input forma or any complication. whole_order = [ ["fries", 2 ], [ "sandwich", 3 ] ] Now comes the part you need to do: - write some Python code to compute the total cost of the order (item cost * item count), summed for all the items. Print this raw total so that you can see it is correct. - write some totally separate code to look at the order and decide if the client met your special condition (sandwich, fries, beverage) and get a true/false result. Print this, too. - write a Python statement to subtract $1.00 (or 100 cents) from the total if that condition is true. Print that. Then fiddle the order and run your programme several times to check that it is behaving the way it should. If you find difficulties you cannot surmount, come back here (by replying directly to one of the messages in your discussion) with: - your complete code - your expected output, and the output from your programme - a complete transcript of any error message, for example if your programme raised an exception Make sure these are inline in your message, _not_ attachments. We drop attachments in this list. Cheers, Cameron Simpson From henriquecsj at outlook.com Fri Nov 30 08:47:11 2018 From: henriquecsj at outlook.com (Henrique Castro) Date: Fri, 30 Nov 2018 13:47:11 +0000 Subject: [Tutor] Moving a conda environment to an off-line computer Message-ID: Dear colleagues, Soon I'll start to use one of the powerful computers on my university as a tool in my Ph.D. The computer does not have an internet connection and I need to find a way to install a conda environment on it. At first I tried to install and set the conda environment that I need in a computer with internet connection and taking care to copy everything in a path that is similar in the off-line computer (i.e I installed everything on /home/henrique/bin/anaconda3 at home and tried to copy everything to /home/henrique/bin/anaconda3 in the off-line computer - with the same .bashrc) but when I run conda I get an error(it works on my home computer): (deepchem) [henrique at europio qm7] $ python qm7_ANI.py /home/henrique/bin/anaconda3/envs/deepchem/lib/python3.6/site-packages/sklearn/ensemble/weight_boosting.py:29: DeprecationWarning: numpy.core.umath_tests is an internal NumPy module and should not be imported. It will be removed in a future NumPy release. from numpy.core.umath_tests import inner1d Traceback (most recent call last): File "/home/henrique/bin/anaconda3/envs/deepchem/lib/python3.6/urllib/request.py", line 1318, in do_open encode_chunked=req.has_header('Transfer-encoding')) File "/home/henrique/bin/anaconda3/envs/deepchem/lib/python3.6/http/client.py", line 1239, in request self._send_request(method, url, body, headers, encode_chunked) File "/home/henrique/bin/anaconda3/envs/deepchem/lib/python3.6/http/client.py", line 1285, in _send_request self.endheaders(body, encode_chunked=encode_chunked) File "/home/henrique/bin/anaconda3/envs/deepchem/lib/python3.6/http/client.py", line 1234, in endheaders self._send_output(message_body, encode_chunked=encode_chunked) File "/home/henrique/bin/anaconda3/envs/deepchem/lib/python3.6/http/client.py", line 1026, in _send_output self.send(msg) File "/home/henrique/bin/anaconda3/envs/deepchem/lib/python3.6/http/client.py", line 964, in send self.connect() File "/home/henrique/bin/anaconda3/envs/deepchem/lib/python3.6/http/client.py", line 936, in connect (self.host,self.port), self.timeout, self.source_address) File "/home/henrique/bin/anaconda3/envs/deepchem/lib/python3.6/socket.py", line 704, in create_connection for res in getaddrinfo(host, port, 0, SOCK_STREAM): File "/home/henrique/bin/anaconda3/envs/deepchem/lib/python3.6/socket.py", line 745, in getaddrinfo for res in _socket.getaddrinfo(host, port, family, type, proto, flags): socket.gaierror: [Errno -2] Name or service not known During handling of the above exception, another exception occurred: Traceback (most recent call last): File "qm7_ANI.py", line 15, in featurizer='BPSymmetryFunction', split='stratified', move_mean=False) File "/home/henrique/bin/anaconda3/envs/deepchem/lib/python3.6/site-packages/deepchem/molnet/load_function/qm7_datasets.py", line 50, in load_qm7_from_mat 'http://deepchem.io.s3-website-us-west-1.amazonaws.com/datasets/qm7.mat' File "/home/henrique/bin/anaconda3/envs/deepchem/lib/python3.6/site-packages/deepchem/utils/__init__.py", line 85, in download_url urlretrieve(url, os.path.join(dest_dir, name)) File "/home/henrique/bin/anaconda3/envs/deepchem/lib/python3.6/urllib/request.py", line 248, in urlretrieve with contextlib.closing(urlopen(url, data)) as fp: File "/home/henrique/bin/anaconda3/envs/deepchem/lib/python3.6/urllib/request.py", line 223, in urlopen return opener.open(url, data, timeout) File "/home/henrique/bin/anaconda3/envs/deepchem/lib/python3.6/urllib/request.py", line 526, in open response = self._open(req, data) File "/home/henrique/bin/anaconda3/envs/deepchem/lib/python3.6/urllib/request.py", line 544, in _open '_open', req) File "/home/henrique/bin/anaconda3/envs/deepchem/lib/python3.6/urllib/request.py", line 504, in _call_chain result = func(*args) File "/home/henrique/bin/anaconda3/envs/deepchem/lib/python3.6/urllib/request.py", line 1346, in http_open return self.do_open(http.client.HTTPConnection, req) File "/home/henrique/bin/anaconda3/envs/deepchem/lib/python3.6/urllib/request.py", line 1320, in do_open raise URLError(err) urllib.error.URLError: Any help is much appreciated -- Henrique C. S. Junior From alan.gauld at yahoo.co.uk Fri Nov 30 19:33:44 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 1 Dec 2018 00:33:44 +0000 Subject: [Tutor] Moving a conda environment to an off-line computer In-Reply-To: References: Message-ID: On 30/11/2018 13:47, Henrique Castro wrote: > ... use one of the powerful computers on my university It would help if we knew what kind of powerful computer this is. What OS does it run? It sounds like some variety of Unix, but which? Does it have a C compiler on board? > The computer does not have an internet connection > and I need to find a way to install a conda environment on it. This may be a question that the Anaconda community can better answer since building your own Anaconda suite is non trivial. So unless you find a pre-built environment for your new OS which you can download and copy to removable media you will likely need specialist help. That's more likely to be found on the Anaconda fora than on a general Python list like this one. > File "/home/henrique/bin/anaconda3/envs/deepchem/lib/python3.6/http/client.py", line 936, in connect > (self.host,self.port), self.timeout, self.source_address) > File "/home/henrique/bin/anaconda3/envs/deepchem/lib/python3.6/socket.py", line 704, in create_connection > for res in getaddrinfo(host, port, 0, SOCK_STREAM): > File "/home/henrique/bin/anaconda3/envs/deepchem/lib/python3.6/socket.py", line 745, in getaddrinfo > for res in _socket.getaddrinfo(host, port, family, type, proto, flags): > socket.gaierror: [Errno -2] Name or service not known Apparently the test file tries to access the internet so will obviously fail on a non internet connected device. (BTW is it on any kind of network or is it entirely standalone?) -- 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 Fri Nov 30 19:35:47 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 1 Dec 2018 11:35:47 +1100 Subject: [Tutor] Moving a conda environment to an off-line computer In-Reply-To: References: Message-ID: <20181201003545.GK4319@ando.pearwood.info> On Fri, Nov 30, 2018 at 01:47:11PM +0000, Henrique Castro wrote: > Dear colleagues, > Soon I'll start to use one of the powerful computers on my university > as a tool in my Ph.D. The computer does not have an internet > connection and I need to find a way to install a conda environment on > it. [...] Generally speaking, it is *really hard* to copy an installed suite of applications from one machine to another. It's usually best to get an off-line (no internet access) installer, copy it to the machine, then run the installer. This isn't true in all cases, but many application installers make changes to (for example) hidden files, registry settings, environment variables etc and if you miss copying or duplicating even one, the application will not work correctly. For an extremely specialised question like this, you should talk to Conda specialists, or Conda themselves. We know the Python language, we're not experts on the internal details of what it takes for Conda's suite of tools to work. Possibly Conda provides a CD or DVD off-line installer? https://duckduckgo.com/?q=Conda+off-line+installer Another possibility is to build a desktop machine with Conda on-line, install everything you need, then copy that to a virtual machine and copy the VM to the university computer. Talk to the university's system administrator about the possibility of running a VM on the computer. Another possibility is that your installation of Conda is fine, but whatever function you tried using assumes that there is some sort of network connection available, and if it is not available, the function simply fails. If that's the case, you might be stuck: unless Conda fix the bug in the function, it won't work without a network connection. Or perhaps you can talk to the sys admin about turning on the network software on the computer, even if it isn't connected to anything. That might be enough to allow the function to work. (Perhaps it tries to connect to localhost, and if that fails, the whole thing dies.) -- Steve From mats at wichmann.us Fri Nov 30 19:50:47 2018 From: mats at wichmann.us (Mats Wichmann) Date: Fri, 30 Nov 2018 17:50:47 -0700 Subject: [Tutor] Moving a conda environment to an off-line computer In-Reply-To: <20181201003545.GK4319@ando.pearwood.info> References: <20181201003545.GK4319@ando.pearwood.info> Message-ID: On 11/30/18 5:35 PM, Steven D'Aprano wrote: > On Fri, Nov 30, 2018 at 01:47:11PM +0000, Henrique Castro wrote: > >> Dear colleagues, > >> Soon I'll start to use one of the powerful computers on my university >> as a tool in my Ph.D. The computer does not have an internet >> connection and I need to find a way to install a conda environment on >> it. > [...] > > Generally speaking, it is *really hard* to copy an installed suite of > applications from one machine to another. It's usually best to get an > off-line (no internet access) installer, copy it to the machine, then > run the installer. You really should poke around the Anaconda community for this. From some earlier research I did when someone asked me a question (not here on tutor), I recall conda used to have some unfortunate dependencies on having an internet connection, and they've done quite a lot of work on making what you want possible. I think there are minimum versions you need, and some flags you need (some kind of --offline thing) but those folks know about this scenario. I have no idea how you do the "install", so I'll just stop writing now... From steve at pearwood.info Fri Nov 30 20:14:31 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 1 Dec 2018 12:14:31 +1100 Subject: [Tutor] Search error code in a logfile and print all lines until the error code In-Reply-To: References: Message-ID: <20181201011431.GM4319@ando.pearwood.info> On Thu, Nov 29, 2018 at 06:23:42PM +0530, Asad wrote: [...] > It gives me error : > > IOPub data rate exceeded. > The notebook server will temporarily stop sending output > to the client in order to avoid crashing it. > To change this limit, set the config variable > `--NotebookApp.iopub_data_rate_limit`. > > Current values: > NotebookApp.iopub_data_rate_limit=1000000.0 (bytes/sec) > NotebookApp.rate_limit_window=3.0 (secs) That doesn't look like a Python error. What is "it" that gives you that error? What precisely are you doing that gives the error? -- Steve From henriquecsj at outlook.com Fri Nov 30 19:43:50 2018 From: henriquecsj at outlook.com (Henrique Castro) Date: Sat, 1 Dec 2018 00:43:50 +0000 Subject: [Tutor] Moving a conda environment to an off-line computer In-Reply-To: References: , Message-ID: Hi, The computer is a cluster with 4 nodes each running Linux (our Fedora-based distro), 12 cores/64 GB RAM per node using CUDA. At first, I'll be using only one node. -- Henrique C. S. Junior ________________________________ From: Tutor on behalf of Alan Gauld via Tutor Sent: Friday, November 30, 2018 22:33 To: tutor at python.org Subject: Re: [Tutor] Moving a conda environment to an off-line computer On 30/11/2018 13:47, Henrique Castro wrote: > ... use one of the powerful computers on my university It would help if we knew what kind of powerful computer this is. What OS does it run? It sounds like some variety of Unix, but which? Does it have a C compiler on board? > The computer does not have an internet connection > and I need to find a way to install a conda environment on it. This may be a question that the Anaconda community can better answer since building your own Anaconda suite is non trivial. So unless you find a pre-built environment for your new OS which you can download and copy to removable media you will likely need specialist help. That's more likely to be found on the Anaconda fora than on a general Python list like this one. > File "/home/henrique/bin/anaconda3/envs/deepchem/lib/python3.6/http/client.py", line 936, in connect > (self.host,self.port), self.timeout, self.source_address) > File "/home/henrique/bin/anaconda3/envs/deepchem/lib/python3.6/socket.py", line 704, in create_connection > for res in getaddrinfo(host, port, 0, SOCK_STREAM): > File "/home/henrique/bin/anaconda3/envs/deepchem/lib/python3.6/socket.py", line 745, in getaddrinfo > for res in _socket.getaddrinfo(host, port, family, type, proto, flags): > socket.gaierror: [Errno -2] Name or service not known Apparently the test file tries to access the internet so will obviously fail on a non internet connected device. (BTW is it on any kind of network or is it entirely standalone?) -- Alan G Author of the Learn to Program web site https://eur04.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.alan-g.me.uk%2F&data=02%7C01%7C%7C2a739252872d48babd6208d65725104e%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636792214023931869&sdata=8KuHyIuihRIwDB1bKIF7ZS7PJRwdtNLMVk7AYNOW%2B1s%3D&reserved=0 https://eur04.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.amazon.com%2Fauthor%2Falan_gauld&data=02%7C01%7C%7C2a739252872d48babd6208d65725104e%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636792214023931869&sdata=mgXxj1AChik%2B%2F2lCfL%2BNdJdfhUFDc2XLmEwgIMs8n50%3D&reserved=0 Follow my photo-blog on Flickr at: https://eur04.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.flickr.com%2Fphotos%2Falangauldphotos&data=02%7C01%7C%7C2a739252872d48babd6208d65725104e%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636792214023931869&sdata=HtkISkcgLNnKMFTIZcqT3VEZaBbPVfdRV11M16YjpQI%3D&reserved=0 _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://eur04.safelinks.protection.outlook.com/?url=https%3A%2F%2Fmail.python.org%2Fmailman%2Flistinfo%2Ftutor&data=02%7C01%7C%7C2a739252872d48babd6208d65725104e%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636792214023931869&sdata=vrmETDxK13BxuOWQ2cSMG13xmb%2FX3ly85NvVwqEUjXg%3D&reserved=0