From tobywobycarlton at icloud.com Mon Mar 1 17:05:23 2021 From: tobywobycarlton at icloud.com (Toby Carlton) Date: Mon, 1 Mar 2021 15:05:23 -0700 Subject: [Tutor] Python Quit Unexpectedly Message-ID: <09873E64-ED3D-4415-99D6-75697EAD0872@icloud.com> Every time I try to run pygame.display.set_mode() it gives me the error message - python quit unexpectedly. Please help! From alan.gauld at yahoo.co.uk Tue Mar 2 17:06:26 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 2 Mar 2021 22:06:26 +0000 Subject: [Tutor] Python Quit Unexpectedly In-Reply-To: <09873E64-ED3D-4415-99D6-75697EAD0872@icloud.com> References: <09873E64-ED3D-4415-99D6-75697EAD0872@icloud.com> Message-ID: <s1mct3$1kc$1@ciao.gmane.io> On 01/03/2021 22:05, Toby Carlton via Tutor wrote: > Every time I try to run pygame.display.set_mode() it gives me the error message - python quit unexpectedly. Please help! Can you provide the basic context please: Which OS are you using? Which Python version? How are you erunning the code? And can you include at least the function that calls pygame.display.set_mode()? Better yet the full file if its not hundreds of lines long. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From torbjorn.svensson.diaz at gmail.com Fri Mar 5 08:50:22 2021 From: torbjorn.svensson.diaz at gmail.com (=?UTF-8?Q?Torbj=c3=b6rn_Svensson_Diaz?=) Date: Fri, 5 Mar 2021 14:50:22 +0100 Subject: [Tutor] Minimum Skill Set Message-ID: <ee8b2bb6-67f5-42bd-a5c1-a86e58a9b247@gmail.com> Hello, tutors! I have a question about computer programming. Some programmers have PhD degrees, a full third of US programmers have no degrees and three quarters of US programming jobs don't require degrees. So what is the minimum skill set needed to perform the easiest programming tasks for a programmer or software tester? At https://teachyourselfcs.com/ material that all programmers should master are listed. OTOH, there seems to be a lot of programmer that make a living out of programming or software testing mastering less material. What is the absolute minimum? Best regards, -- Torbj?rn Svensson Diaz From alan.gauld at yahoo.co.uk Fri Mar 5 14:27:27 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 5 Mar 2021 19:27:27 +0000 Subject: [Tutor] Minimum Skill Set In-Reply-To: <ee8b2bb6-67f5-42bd-a5c1-a86e58a9b247@gmail.com> References: <ee8b2bb6-67f5-42bd-a5c1-a86e58a9b247@gmail.com> Message-ID: <s1u0n1$v58$1@ciao.gmane.io> On 05/03/2021 13:50, Torbj?rn Svensson Diaz wrote: > I have a question about computer programming. Some programmers have PhD > degrees, a full third of US programmers have no degrees and three > quarters of US programming jobs don't require degrees. So what is the > minimum skill set needed to perform the easiest programming tasks for a > programmer or software tester? The answer to this varies quite a bit and depends on what you mean by "programmer". The British Computer Society has a grading system for IT jobs (the Industry Standard Model or ISM) and it defines programmer as a relatively low skilled (and low paid) job similar to say an office clerk or newly qualified tradesman or almost qualified apprentice. By that they mean someone who writes code that somebody else has designed down to procedure or function level. They may have a high school qualification in computing or have done an IT apprenticeship. Below that there are those who are not officially programmers but who spend a lot of time writing code. This includes system administrators etc (When I say 'below' I do not mean admins are lower rated than programmers, quite the opposite! Rather I mean in the sense of formal programming skills and training.) Next up the ratings comes the systems programmer/analyst. These usually have a formal qualification in programming and can create program designs (ie for a single process). They can take in business requirements and translate them to software designs. These folks earn around the same as a fully qualified and experienced tradesman such as a plumber or electrician. Software testers sit in this area too. Then comes software engineers. These are folks who can design large scale systems with multiple processes and complex data models. Often involving networking etc. This is what the general public often think of when they think "Programmer" They will usually have a degree of some kind(not necessarily CS) And they earn similar money to other professionals such as civil engineers, etc. Software engineers tend to spend a minority of their time actually writing code(say 20-40%). Getting near the top we have architects who define high level system structures, determine corporate IT strategy and design the very largest systems. They often have post graduate qualifications and many years experience. They earn mid-upper management salaries (notice the significant shift in comparison from engineer to manager!) Architects typically spend very little time (<10%) writing code, but they may spend a lot of time reading other peoples(~20%)! And that's the UK view, and I think similar to much of Europe. But the approaches in other areas of the world are quite different! Also, even in the UK it is a somewhat outdated view and may even have been updated recently (I ended my involvement with BCS back about 2005) The model I decribed was based largely on the old Mainframe computing, corporate IT department mentality. In practice, the layers described above still apply but the lower ones now include folks working on web sites with very little formal training, having started on HTML/CSS and copying snippets of Javascript. They have built their skills but probably have little if any CS training. The absolute minimum is: however little you can persuade someone to employ you! I know of one guy with zero formal training who is called a programmer in his job title but still only writes HTML/CSS and copies snippets of Javascript or asks questions on stack-exchange and copies the answers into his code, without knowing how they work. But if you don't want to spend your life (re)writing web front ends for basic level web sites and have aspirations to develop full stack applications or work on large systems, or use state of the art practices you will likely want a qualification behind you. It doesn't need to be a CS degree. Any form of STEM course will usually suffice to get you into a job. After that its more about what you do and the experience you acquire. And remember those pay ranges. Being a "programmer" is no guarantee of high pay. You could be earning the same as a new qualified hairdresser (without tips) or on a 6 figure salary. There is a world of difference out there. Finally, there is another category of programmer who is a specialist in some field or other. They may spend their whole life working on a single piece of code. Or they write highly complex code related to a specialist topic like astrophysics or weather simulations. These folks can be earning big money but working like the tier two guy. And often qualified at PhD level - but in their specialism not in "programming". And, as I say, things work very differently in other places. The only areas I have any experience with are the USA and India. But I'll let others with more direct experience comment. -- 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.Flynn at capita.com Fri Mar 5 14:06:58 2021 From: Steve.Flynn at capita.com (Flynn, Stephen (Life & Pensions)) Date: Fri, 5 Mar 2021 19:06:58 +0000 Subject: [Tutor] Minimum Skill Set In-Reply-To: <ee8b2bb6-67f5-42bd-a5c1-a86e58a9b247@gmail.com> References: <ee8b2bb6-67f5-42bd-a5c1-a86e58a9b247@gmail.com> Message-ID: <LO0P265MB311544E9EC660416F26405C897969@LO0P265MB3115.GBRP265.PROD.OUTLOOK.COM> Absolute minimum - the ability to read and think. S. -----Original Message----- From: Tutor <tutor-bounces+steve.flynn=capita.co.uk at python.org> On Behalf Of Torbj?rn Svensson Diaz Sent: Friday, March 5, 2021 1:50 PM To: tutor at python.org Subject: [Tutor] Minimum Skill Set **EXTERNAL** Hello, tutors! I have a question about computer programming. Some programmers have PhD degrees, a full third of US programmers have no degrees and three quarters of US programming jobs don't require degrees. So what is the minimum skill set needed to perform the easiest programming tasks for a programmer or software tester? At https://teachyourselfcs.com/ material that all programmers should master are listed. OTOH, there seems to be a lot of programmer that make a living out of programming or software testing mastering less material. What is the absolute minimum? Best regards, -- Torbj?rn Svensson Diaz _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor This message has been scanned by Capita systems, but if you believe it to be spam, please send it to spam at forcepoint.com. Messages sent to spam at forcepoint.com are queued for email analysis by Forcepoint Threat Lab. This email originates from outside of Capita. Keep this in mind before responding, opening attachments or clicking any links. Unless you recognise the sender and know the content is safe. If in any doubt, the grammar and spelling are poor, or the name doesn't match the email address then please contact the sender via an alternate known method. This email is security checked and subject to the disclaimer on web-page: https://www.capita.com/email-disclaimer.aspx From sjeik_appie at hotmail.com Thu Mar 11 09:37:33 2021 From: sjeik_appie at hotmail.com (Albert-Jan Roskam) Date: Thu, 11 Mar 2021 15:37:33 +0100 Subject: [Tutor] About (dev-)requirements.txt Message-ID: <DB6PR01MB3895B66F26A0F3887746EDFD83909@DB6PR01MB3895.eurprd01.prod.exchangelabs.com> Hi, How do I generate separate (i.e., disjoint) lists of requirements for the app itself and the dev-requirements? I know I can use pip freeze, but how do I separate dev and non-dev? Let's say I use nose, pdoc3 and tox for dev-requirements.txt. How do I separate those packages AND their dependencies from the non-dev requirements.txt? And what do I do with libraries like setuptools, wheel and pip? Should they be in both lists? Or should they be installed first, before anything else? I recently experienced many problems related to old pip/wheel versions after I tried to recreate a virtualenv with existing requirement.txt's but a higher Python version so I hope next time this process will be less "trial and error". Thanks! Albert-Jan From PyTutor at DancesWithMice.info Thu Mar 11 15:01:38 2021 From: PyTutor at DancesWithMice.info (dn) Date: Fri, 12 Mar 2021 09:01:38 +1300 Subject: [Tutor] About (dev-)requirements.txt In-Reply-To: <DB6PR01MB3895B66F26A0F3887746EDFD83909@DB6PR01MB3895.eurprd01.prod.exchangelabs.com> References: <DB6PR01MB3895B66F26A0F3887746EDFD83909@DB6PR01MB3895.eurprd01.prod.exchangelabs.com> Message-ID: <90e3ea5c-cd0d-5c92-8a97-9471e833bc2f@DancesWithMice.info> On 12/03/2021 03.37, Albert-Jan Roskam wrote: > Hi, > How do I generate separate (i.e., disjoint) lists of requirements for the > app itself and the dev-requirements? I know I can use pip freeze, but how > do I separate dev and non-dev? Let's say I use nose, pdoc3 and tox for > dev-requirements.txt. How do I separate those packages AND their > dependencies from the non-dev requirements.txt? > And what do I do with libraries like setuptools, wheel and pip? Should > they be in both lists? Or should they be installed first, before anything > else? I recently experienced many problems related to old pip/wheel > versions after I tried to recreate a virtualenv with existing > requirement.txt's but a higher Python version so I hope next time this > process will be less "trial and error". This is an interesting question: there are a number of methodologies which aim to ensure that whatever was needed in the dev.env, 'everything' is carried-forward into "prod". However, there are indeed some things we don't (necessarily) want! Perhaps an opposite view is to ask: why bother? What does it matter if some test or debug tool is replicated in "prod". It should never be used/called. Is the storage-space it occupies, significant? Thus, is it worth the brain-time? Rather than listing the components of the environment, which tool are you using to parcel-up the code and deliver it/replicate it? -- Regards, =dn From mats at wichmann.us Thu Mar 11 15:32:39 2021 From: mats at wichmann.us (Mats Wichmann) Date: Thu, 11 Mar 2021 13:32:39 -0700 Subject: [Tutor] About (dev-)requirements.txt In-Reply-To: <DB6PR01MB3895B66F26A0F3887746EDFD83909@DB6PR01MB3895.eurprd01.prod.exchangelabs.com> References: <DB6PR01MB3895B66F26A0F3887746EDFD83909@DB6PR01MB3895.eurprd01.prod.exchangelabs.com> Message-ID: <08abe77c-51a0-56e8-d482-622429807d11@wichmann.us> On 3/11/21 7:37 AM, Albert-Jan Roskam wrote: > Hi, > How do I generate separate (i.e., disjoint) lists of requirements for the > app itself and the dev-requirements? I know I can use pip freeze, but how > do I separate dev and non-dev? Let's say I use nose, pdoc3 and tox for > dev-requirements.txt. How do I separate those packages AND their > dependencies from the non-dev requirements.txt? > And what do I do with libraries like setuptools, wheel and pip? Should > they be in both lists? Or should they be installed first, before anything > else? I recently experienced many problems related to old pip/wheel > versions after I tried to recreate a virtualenv with existing > requirement.txt's but a higher Python version so I hope next time this > process will be less "trial and error". > Thanks! > Albert-Jan This seems to be a space where conventions are evolving. I used to hear the advice to put install requirements in setup.py, and build requirements in requirements.txt. More recently I've heard people using separate requirements files, like you mention. requirements-dev.txt seems to be the convention - PyPi even has a dummy package of this name to prevent issues if you try to install using this file and leave out the "-r". https://pypi.org/project/requirements-dev.txt/ If you're using pipenv (I don't, at the moment, so this is just from reading docs, not personal experience): pipenv lock -r and pipenv lock -r --dev will generate you separate lists for runtime and for dev. BUT... that means you already had to record what you wanted in the pipfile, so it feels kind of like "to solve the problem, you first have to solve the problem" :) Let us know what you find out... From alan.gauld at yahoo.co.uk Thu Mar 11 16:30:54 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 11 Mar 2021 21:30:54 +0000 Subject: [Tutor] About (dev-)requirements.txt In-Reply-To: <DB6PR01MB3895B66F26A0F3887746EDFD83909@DB6PR01MB3895.eurprd01.prod.exchangelabs.com> References: <DB6PR01MB3895B66F26A0F3887746EDFD83909@DB6PR01MB3895.eurprd01.prod.exchangelabs.com> Message-ID: <s2e26e$ndb$1@ciao.gmane.io> On 11/03/2021 14:37, Albert-Jan Roskam wrote: > How do I generate separate (i.e., disjoint) lists of requirements for the > app itself and the dev-requirements? Ok, This one puzzles me. I come from a background of large projects, with typically between 50-500 developers. Requirements are something catalogued in a book (possibly several volumes - the biggest I saw was 7 volumes of 50-100+ ages each) or in a CASE tool (eg Borland Together) or a project management tool such as Jira. I would expect a total of several thousand requirements, with each release addressing perhaps 100-200 at a time. But these requirements would be business requirements: Like: Req010301: The system should calculate customer propensity to spend using the McClelland-Smythe algorithm (version 2) or R-Sales-003: The sales agent shall be presented with a list of up-selling and cross-selling opportunities based on both current promotions and the customers previous sales history. Or, most commonly, a use case scenario which would encompass several linked requirements. Are these the kinds of application requirements you have in mind? And I'm not sure what you mean by dev-requirements? Are these perhaps the low level developer user stories we had to create as part of system design? Things like: I want the up-selling and cross-selling product lists to appear in a single panel on the Sales screen so that I do not lose sight of the current customer data. Or is it something else? Can you give examples of both type of requirements? -- 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 Mar 11 19:28:22 2021 From: mats at wichmann.us (Mats Wichmann) Date: Thu, 11 Mar 2021 17:28:22 -0700 Subject: [Tutor] About (dev-)requirements.txt In-Reply-To: <s2e26e$ndb$1@ciao.gmane.io> References: <DB6PR01MB3895B66F26A0F3887746EDFD83909@DB6PR01MB3895.eurprd01.prod.exchangelabs.com> <s2e26e$ndb$1@ciao.gmane.io> Message-ID: <dac9e926-b154-8ae2-9c9e-4d8bf1e03a91@wichmann.us> On 3/11/21 2:30 PM, Alan Gauld via Tutor wrote: > On 11/03/2021 14:37, Albert-Jan Roskam wrote: >> How do I generate separate (i.e., disjoint) lists of requirements for the >> app itself and the dev-requirements? > > Ok, This one puzzles me. > I come from a background of large projects, with typically > between 50-500 developers. Requirements are something catalogued > in a book Requirements is the wrong word, but that seems to be the term the Python packaging ecosystem settled on. It's "dependencies", as in other Python packages needed... It's a problem that's very actual for me, as I'm working on a project that can be run with nothing but the Python standard library, but for a project developer, there are dependencies - xml tools, sphinx documentation generator, etc. - these are development dependencies, but not runtime dependencies, and we've also wrestled with keeping them straight. From sjeik_appie at hotmail.com Fri Mar 12 03:31:14 2021 From: sjeik_appie at hotmail.com (Albert-Jan Roskam) Date: Fri, 12 Mar 2021 09:31:14 +0100 Subject: [Tutor] About (dev-)requirements.txt In-Reply-To: <dac9e926-b154-8ae2-9c9e-4d8bf1e03a91@wichmann.us> Message-ID: <DB6PR01MB3895A286610C1E999A921757836F9@DB6PR01MB3895.eurprd01.prod.exchangelabs.com> On 12 Mar 2021 01:28, Mats Wichmann <mats at wichmann.us> wrote: On 3/11/21 2:30 PM, Alan Gauld via Tutor wrote: > On 11/03/2021 14:37, Albert-Jan Roskam wrote: >> How do I generate separate (i.e., disjoint) lists of requirements for the >> app itself and the dev-requirements? > > Ok, This one puzzles me. > I come from a background of large projects, with typically > between 50-500 developers. Requirements are something catalogued > in a book =====>> Hi all, @Alan: this is a nice page about it https://blog.usejournal.com/why-and-how-to-make-a-requirements-txt-f329c685181e I've read a bit more about this and I *may* have a solution (I haven't tried it yet). It assumes a virtualenv is used. Let's say I use pandas in my project and pandas itself has two dependencies, numpy and openpyxl. pip freeze > constraints.txt now move the *direct* dependencies (i.e. pandas) of the project in a file requirements.txt so i looks like: -c constraints.txt pandas==1.0.0 Constraints.txt looks like: numpy==1.0.0 openpyxl==1.0.0 Installation is simply pip install -r requirements.txt *If* any of the direct requirements need something in constraints.txt, they are constrained to the given version. Separating the "real" (=direct) requirements into dev and non-dev is now relatively easy. Although I also like DN's answer: why worry about it? Best wishes, Albert-Jan From sjeik_appie at hotmail.com Fri Mar 12 11:32:04 2021 From: sjeik_appie at hotmail.com (Albert-Jan Roskam) Date: Fri, 12 Mar 2021 17:32:04 +0100 Subject: [Tutor] About (dev-)requirements.txt In-Reply-To: <DB6PR01MB3895A286610C1E999A921757836F9@DB6PR01MB3895.eurprd01.prod.exchangelabs.com> Message-ID: <DB6PR01MB3895CEA4EA268FEBB7DED0D3836F9@DB6PR01MB3895.eurprd01.prod.exchangelabs.com> =====>> Hi all, Ps: I should have added the source: https://luminousmen.medium.com/pip-constraints-files-b684992705b6 From alan.gauld at yahoo.co.uk Sun Mar 14 08:19:32 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 14 Mar 2021 12:19:32 +0000 Subject: [Tutor] About (dev-)requirements.txt In-Reply-To: <dac9e926-b154-8ae2-9c9e-4d8bf1e03a91@wichmann.us> References: <DB6PR01MB3895B66F26A0F3887746EDFD83909@DB6PR01MB3895.eurprd01.prod.exchangelabs.com> <s2e26e$ndb$1@ciao.gmane.io> <dac9e926-b154-8ae2-9c9e-4d8bf1e03a91@wichmann.us> Message-ID: <s2kv0k$p4a$1@ciao.gmane.io> On 12/03/2021 00:28, Mats Wichmann wrote: > On 3/11/21 2:30 PM, Alan Gauld via Tutor wrote: >> On 11/03/2021 14:37, Albert-Jan Roskam wrote: >>> How do I generate separate (i.e., disjoint) lists of requirements for the >>> app itself and the dev-requirements? >> >> Ok, This one puzzles me. >> I come from a background of large projects, with typically >> between 50-500 developers. Requirements are something catalogued >> in a book > > Requirements is the wrong word, but that seems to be the term the Python > packaging ecosystem settled on. It's "dependencies", as in other Python > packages needed... Thanks for the clarification. Unfortunately that means I have nothing to contribute, I steer clear of that whole packaging thing as much as possible! :-) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From singh000taran at gmail.com Sun Mar 14 07:53:32 2021 From: singh000taran at gmail.com (=?UTF-8?B?4Kmn?=) Date: Sun, 14 Mar 2021 17:23:32 +0530 Subject: [Tutor] (no subject) Message-ID: <CAHnm4MGWeZzA-L3RtAKVDFH9V9j3jWU6L-Oa=F5VOQgH9s_ZxQ@mail.gmail.com> Respected sir, please guide me with the question given below. I don't fully understand the question. Write a python program to read from input a string. The input contains COMMA separated string values. The output of the program is the count of string values where (a) the length is >= 2; AND (b) the first and last character of the string value are the same. Input: abc,xyz,aba,1221,1 Output: 2 Input: abc, xyz , alpha Output: 1 [Note that the second string ( xyz ) has SPACE at the start and at the end; while the last string ( alpha) has SPACE only at the start. From singh000taran at gmail.com Sun Mar 14 08:39:58 2021 From: singh000taran at gmail.com (=?UTF-8?B?4Kmn?=) Date: Sun, 14 Mar 2021 18:09:58 +0530 Subject: [Tutor] (no subject) Message-ID: <CAHnm4MG+WkFob_M7OkyLGt=Xwy+UE==JcyHMMsGmSBvG=s2Ezw@mail.gmail.com> Dear sir, I have not studies graph-related things in python. While answering please try to be more descriptive; I will be grateful if you do so. Question - In this problem, you need to create a program for managing Graphs. The input to the program will be as described next: The first line will contain a positive integer NN. NN denotes the number of nodes in the graph. Nodes themselves are numbered from 0 to N?1N?1. The second line will contain a non-negative integer MM (M?0M?0). This will be the number of edges in the graph (However, some of these could be invalid/duplicate). The next MM lines each will contain a pair of integers i,ji,j representing a directed edge i?ji?j. Self edges (i?ii?i) are allowed. However, multi-edges (two or more instances of i?ji?j) have to be stored only once. Last line will contain an integer kk. The output of the program is: If kk represents a valid node, print all the nodes ss such that s?ks?k is a (directed) edge in the graph. The nodes have to be in sorted order, separated by COMMA (no whitespace). If kk does not represent a valid node, print "ERROR: Invalid Node." EXAMPLES ___________________________________ INPUT: 3 4 0,1 1,2 2,0 0,2 2 OUTPUT: 0,1 _____________________________ INPUT: 3 4 0,1 0,2 2,0 0,2 2 OUTPUT: 0 _____________________________ INPUT: 1 0 0 OUTPUT: (Note empty output) _____________________________ NOTE: Remember that Self edges (i?ii?i) are allowed. Multi-edges (two or more instances of i?ji?j) have to be counted only once. From singh000taran at gmail.com Sun Mar 14 08:52:36 2021 From: singh000taran at gmail.com (=?UTF-8?B?4Kmn?=) Date: Sun, 14 Mar 2021 18:22:36 +0530 Subject: [Tutor] (no subject) In-Reply-To: <CAHnm4MGWeZzA-L3RtAKVDFH9V9j3jWU6L-Oa=F5VOQgH9s_ZxQ@mail.gmail.com> References: <CAHnm4MGWeZzA-L3RtAKVDFH9V9j3jWU6L-Oa=F5VOQgH9s_ZxQ@mail.gmail.com> Message-ID: <CAHnm4MHhYhQfhEza5X_QHk0oR4koRMMtDP_4jDThHFyyF-fmLg@mail.gmail.com> I am writing the question again in a more readable form. Write a python program to read from input a string. The input contains COMMA separated string values. The output of the program is the count of string values where (a) the length is >= 2; AND (b) the first and last character of the string value are the same. Input: abc,xyz,aba,1221,1 Output: 2 Input: abc, xyz , alpha Output: 1 [Note that the second string ( xyz ) has SPACE at the start and at the end; while the last string ( alpha) has SPACE only at the start. On Sun, Mar 14, 2021 at 5:52 PM ? <singh000taran at gmail.com> wrote: > Respected sir, please guide me with the question given below. I don't fully > understand the question. > Write a python program to read from input a string. The input contains > COMMA separated string values. The output of the program is the count of > string values where (a) the length is >= 2; AND (b) the first and last > character of the string value are the same. Input: abc,xyz,aba,1221,1 > Output: 2 Input: abc, xyz , alpha Output: 1 [Note that the second string ( > xyz ) has SPACE at the start and at the end; while the last string ( alpha) > has SPACE only at the start. > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- TaranSingh From alan.gauld at yahoo.co.uk Sun Mar 14 10:46:40 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 14 Mar 2021 14:46:40 +0000 Subject: [Tutor] (no subject) In-Reply-To: <CAHnm4MGWeZzA-L3RtAKVDFH9V9j3jWU6L-Oa=F5VOQgH9s_ZxQ@mail.gmail.com> References: <CAHnm4MGWeZzA-L3RtAKVDFH9V9j3jWU6L-Oa=F5VOQgH9s_ZxQ@mail.gmail.com> Message-ID: <s2l7kg$5de$1@ciao.gmane.io> On 14/03/2021 11:53, ? wrote: > Respected sir, please guide me with the question given below. I don't fully > understand the question. What parts do you not understand? What parts do you understand? > Write a python program to read from input a string. Do you know how to do that? Consider the input() function. > The input contains COMMA separated string values. Do you know how to separate a string into separate fields? Consider the string.split() method. > The output of the program is the count of > string values where (a) the length is >= 2; Do you know how to find the length of a string Consider the len() function Do you know how to increment a counter for a condition? Consider the if construct and addition operator AND (b) the first and last > character of the string value are the same. Do you know how to extract the first and last characters of a string? Consider indexing Do you know how to logically combine tests (with AND in this case?) Consider the boolean operators. > Input: abc,xyz,aba,1221,1 > Output: 2 Do you understand why the output is 2? Can you write code that produces that result? Input: abc, xyz , alpha Output: 1 [Note that the second string ( > xyz ) has SPACE at the start and at the end; while the last string ( alpha) > has SPACE only at the start. Including spaces as part of the fields is sneaky but does not, in practice, make things any more easy or difficult. Note that we do not do homework for you, but we will review your code and offer suggestions(as above0 to help you do your homework. -- 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 learn2program at gmail.com Mon Mar 15 19:56:15 2021 From: learn2program at gmail.com (Alan Gauld) Date: Mon, 15 Mar 2021 23:56:15 +0000 Subject: [Tutor] Fwd: Graph question was: Re: (no subject) In-Reply-To: <CAHnm4MFAUbXkqDCFkP-mDk0aAurkAMJ30nCcHtfFTCbBoueWAA@mail.gmail.com> References: <CAHnm4MFAUbXkqDCFkP-mDk0aAurkAMJ30nCcHtfFTCbBoueWAA@mail.gmail.com> Message-ID: <97959d16-764a-f64e-051c-fceeb44539a3@gmail.com> Forwarding to the group.... -------- Forwarded Message -------- I have written it in a more readable format. In this problem, you need to create a program for managing Graphs. The input to the program will be as described next: The first line will contain a positive integer NN. NNdenotes the number of nodes in the graph. Nodes themselves are numbered from 0 to N?1N?1. ?The second line will contain a non-negative integer MM(M?0M?0). This will be the number of edges in the graph (However, some of these could be invalid/duplicate). The next MMlines each will contain a pair of integers i,ji,jrepresenting a directed edge i?ji?j. Self edges (i?ii?i) are allowed. However, multi-edges (two or more instances of i?ji?j) have to be stored only once. Last line will contain an integer kk. The output of the program is: If kkrepresents a valid node, print all the nodes sssuch that s?ks?kis a (directed) edge in the graph. The nodes have to be in sorted order, separated by COMMA (no whitespace). If kkdoes not represent a valid node, print "ERROR: Invalid Node." EXAMPLES ___________________________________ INPUT: 3 4 0,1 1,2 2,0 0,2 2 OUTPUT: 0,1 _____________________________ INPUT: 3 4 0,1 0,2 2,0 0,2 2 OUTPUT: 0 _____________________________ INPUT: 1 0 0 OUTPUT: (Note empty output) _____________________________ NOTE: Remember that Self edges (i?ii?i) are allowed. Multi-edges (two or more instances of i?ji?j) have to be counted only once. On Sun, Mar 14, 2021 at 8:28 PM Alan Gauld <learn2program at gmail.com <mailto:learn2program at gmail.com>> wrote: Please supply a relevant subject line, it makes tracking the discussion much easier. On 14/03/2021 12:39, ? wrote: > Dear sir, I have not studies graph-related things in python. Have you studied graphs in math? Python is pretty much irrelevant since it does not directly deal in graphs. You need to do that yourself. ie. can you solve the problem below without Python? If not then that is where you must start. First understand the problem. > The first line > will contain a positive integer NN. NN denotes the number of nodes in the > graph. Nodes themselves are numbered from 0 to N?1N?1. OK, This has lost me already. The number of nodes should surely be N? Not NN? How are the nodes in a graph numbered Can you provide an example? The input samples below do not appear to match the description. Can you repost the question with clearer formatting and better examples? The more you can help us the more we can help you. > The second line will > contain a non-negative integer MM (M?0M?0). This will be the number of > edges in the graph (However, some of these could be invalid/duplicate). The > next MM lines each will contain a pair of integers i,ji,j representing a > directed edge i?ji?j. Self edges (i?ii?i) are allowed. However, multi-edges > (two or more instances of i?ji?j) have to be stored only once. Last line > will contain an integer kk. The output of the program is: If kk represents > a valid node, print all the nodes ss such that s?ks?k is a (directed) edge > in the graph. The nodes have to be in sorted order, separated by COMMA (no > whitespace). If kk does not represent a valid node, print "ERROR: Invalid > Node." EXAMPLES ___________________________________ INPUT: 3 4 0,1 1,2 2,0 > 0,2 2 OUTPUT: 0,1 _____________________________ INPUT: 3 4 0,1 0,2 2,0 0,2 > 2 OUTPUT: 0 _____________________________ INPUT: 1 0 0 OUTPUT: (Note empty > output) _____________________________ NOTE: Remember that Self edges (i?ii?i) > are allowed. Multi-edges (two or more instances of i?ji?j) have to be > counted only once. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ <http://www.alan-g.me.uk/> http://www.amazon.com/author/alan_gauld <http://www.amazon.com/author/alan_gauld> Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos <http://www.flickr.com/photos/alangauldphotos> -- TaranSingh From alan.gauld at yahoo.co.uk Mon Mar 15 20:34:34 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 16 Mar 2021 00:34:34 +0000 Subject: [Tutor] Fwd: Graph question was: Re: (no subject) In-Reply-To: <97959d16-764a-f64e-051c-fceeb44539a3@gmail.com> References: <CAHnm4MFAUbXkqDCFkP-mDk0aAurkAMJ30nCcHtfFTCbBoueWAA@mail.gmail.com> <97959d16-764a-f64e-051c-fceeb44539a3@gmail.com> Message-ID: <s2oueq$238$1@ciao.gmane.io> On 15/03/2021 23:56, Alan Gauld wrote: > I have written it in a more readable format. > In this problem, you need to create a program for managing Graphs. The > input to the program will be as described next: The first line will > contain a positive integer NN. NNdenotes the number of nodes in the > graph. Nodes themselves are numbered from 0 to N?1N?1. I'm probably just being dense but I still don't understand this. It says the number is NN which denotes the number of nodes. I'd read that as a two digit number. Then it says it ranges from 0 to N-1 That suggests to me that the number of nodes should really read the index of the highest node? Maybe? Except... It says "numbered from 0 to N-1N-1" which suggests that if N is, say, 5 the numbering could be up to 44? Which makes no sense to me at all! > ?The second line will contain a non-negative integer MM(M?0M?0). This > will be the number of edges in the graph (However, some of these could > be invalid/duplicate). This has a similar issue in that how is a single number of edges represented using a double digit? And when we look at the sample data below the first two "lines" are both single digits... > The next MMlines each will contain a pair of > integers i,ji,jrepresenting a directed edge i?ji?j. Self edges (i?ii?i) > are allowed. However, multi-edges (two or more instances of i?ji?j) have > to be stored only once. Last line will contain an integer kk. And what is kk supposed to be? There is no explanation. > If kkrepresents a valid node, print all the nodes > sssuch that s?ks?kis a (directed) edge in the graph. OK, Now we have a sort of explanation for kk - it might be a node. but we introduce a new term s. What is s? > ___________________________________ INPUT: 3 4 0,1 1,2 2,0 0,2 2 OUTPUT: > 0,1 I assume this should be written: 3 4 0,1 1,2 2,0 0,2 2 ie 3 nodes, 4 edges and the mystery node is 2. The solution is apparently 0,1 but I'm not sure why? As I say, it's probably me missing something (and my graph theory days are long behind me so that's very likely!) But unless we understand what the data represents and how the output should be calculated we can't begin to discuss what the Python code might look like. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From PyTutor at DancesWithMice.info Mon Mar 15 20:52:57 2021 From: PyTutor at DancesWithMice.info (dn) Date: Tue, 16 Mar 2021 13:52:57 +1300 Subject: [Tutor] Fwd: Graph question was: Re: (no subject) In-Reply-To: <s2oueq$238$1@ciao.gmane.io> References: <CAHnm4MFAUbXkqDCFkP-mDk0aAurkAMJ30nCcHtfFTCbBoueWAA@mail.gmail.com> <97959d16-764a-f64e-051c-fceeb44539a3@gmail.com> <s2oueq$238$1@ciao.gmane.io> Message-ID: <4ed3cb44-7f51-4faf-c614-29175042b791@DancesWithMice.info> On 16/03/2021 13.34, Alan Gauld via Tutor wrote: > On 15/03/2021 23:56, Alan Gauld wrote: > >> I have written it in a more readable format. >> In this problem, you need to create a program for managing Graphs. The >> input to the program will be as described next: The first line will >> contain a positive integer NN. NNdenotes the number of nodes in the >> graph. Nodes themselves are numbered from 0 to N?1N?1. > > I'm probably just being dense but I still don't understand this. > > It says the number is NN which denotes the number of nodes. > I'd read that as a two digit number. > > Then it says it ranges from 0 to N-1 > That suggests to me that the number of nodes should really > read the index of the highest node? Maybe? Except... > > It says "numbered from 0 to N-1N-1" which suggests that if > N is, say, 5 the numbering could be up to 44? > Which makes no sense to me at all! > >> ?The second line will contain a non-negative integer MM(M?0M?0). This >> will be the number of edges in the graph (However, some of these could >> be invalid/duplicate). > > This has a similar issue in that how is a single number of edges > represented using a double digit? > > And when we look at the sample data below the first > two "lines" are both single digits... > >> The next MMlines each will contain a pair of >> integers i,ji,jrepresenting a directed edge i?ji?j. Self edges (i?ii?i) >> are allowed. However, multi-edges (two or more instances of i?ji?j) have >> to be stored only once. Last line will contain an integer kk. > > And what is kk supposed to be? There is no explanation. > >> If kkrepresents a valid node, print all the nodes >> sssuch that s?ks?kis a (directed) edge in the graph. > > OK, Now we have a sort of explanation for kk - it might be a node. > but we introduce a new term s. What is s? > >> ___________________________________ INPUT: 3 4 0,1 1,2 2,0 0,2 2 OUTPUT: >> 0,1 > > I assume this should be written: > > 3 > 4 > 0,1 > 1,2 > 2,0 > 0,2 > 2 > > ie 3 nodes, 4 edges and the mystery node is 2. > The solution is apparently 0,1 but I'm not sure why? > > As I say, it's probably me missing something (and my > graph theory days are long behind me so that's very > likely!) > > But unless we understand what the data represents > and how the output should be calculated we can't > begin to discuss what the Python code might look > like. +1 The first confusion, eg "NN", is likely caused by a failed transition from the original format of the assignment into email. It makes sense when one ignores the duplication, instead reading "N" or "N?1"... Your re-formatting of the graph-data makes tremendous sense - because it is so much easier to read! It is a case where I'd be recommending the use of JSON/YAML (even XML) to avoid errors through self-documentation, eg nodes_count: 3 edges_count: 4 edge: 0,1 ... NB don't quote me - I can't remember the exact purposes of each of those numbers. Also, I fail to see why the second is necessary if there are that number of pairs of edge information. As you say, what are we missing? Sadly, there are still tutors who seem to feel that assignments should hinge on understanding arcane aspects, instead of testing the trainee's understanding of Python and/or Graph Theory. My sympathy to the students... As it happens, I've just dusted-off my Graph Theory as part of evaluating AWS' Neptune (graph database). So, IMHO the assignment does make sense. Although the trivial nature and paucity of sample-data is not unduly helpful (perhaps less critical if the course is in Graph Theory cf Python Coding!). (I think) I can see what is required. Hence the earlier response asking the OP for his progress and ideas towards solution... -- Regards, =dn From sb.monzur at gmail.com Wed Mar 17 01:15:22 2021 From: sb.monzur at gmail.com (S Monzur) Date: Wed, 17 Mar 2021 11:15:22 +0600 Subject: [Tutor] How to write a loop in python to find HTML tags in a text file Message-ID: <CAKXx11y2wW9k83zotgroThrROhJeqnkbTfjv2NQ0KPY=bMQ_HA@mail.gmail.com> Dear all, The attached text file <https://drive.google.com/file/d/1gVwRH-TlRks-ZJtb6P8vd3WkqcrIIAa0/view?usp=sharing> contains data from 3 news articles scraped from a news website. I would like to write a loop that separates the metadata from the article body for each of these three articles. The linked code <https://pastebin.com/FU2Axiuc>works for a single news article only (i.e., if I keep only one article in the text file). People have previously suggested using beautiful soup and regular expressions, but please note that I just want to modify the existing code to add a loop, and not use any other methods/functions. Note about the text file: 1. H1 class tag denotes the start of each article. The tag div class=<div class="story-element story-element-text\?> denotes the body text of the article. The text is in a Non-English script, but the tags are all in English. Looking forward to your help! Monzur The output should probably look like this for 3 articles: > <h1 class=???</div>> > *******> > <div class= ??</div>> > *******> > <h1 class=???</div>> > *******> > <div class= ??</div>> > *******> > <h1 class=???</div>> > *******> > <div class= ??</div> From alan.gauld at yahoo.co.uk Wed Mar 17 06:35:02 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 17 Mar 2021 10:35:02 +0000 Subject: [Tutor] How to write a loop in python to find HTML tags in a text file In-Reply-To: <CAKXx11y2wW9k83zotgroThrROhJeqnkbTfjv2NQ0KPY=bMQ_HA@mail.gmail.com> References: <CAKXx11y2wW9k83zotgroThrROhJeqnkbTfjv2NQ0KPY=bMQ_HA@mail.gmail.com> Message-ID: <s2sm0m$11v5$1@ciao.gmane.io> On 17/03/2021 05:15, S Monzur wrote: > data from 3 news articles scraped from a news website. I would like to > write a loop that separates the metadata from the article body for each of > these three articles. The linked code <https://pastebin.com/FU2Axiuc>works > for a single news article only (i.e., if I keep only one article in the > text file). People have previously suggested using beautiful soup and > regular expressions, but please note that I just want to modify the > existing code to add a loop, and not use any other methods/functions. The correct answer is indeed to use an html parser like Beautiful Soup. Not regular expressions, they are unreliable with HTML data. Your code is short enough to post, no need for pastebin. > with open('threenewsarticles.txt', 'r', encoding='utf8') as my_file: > rawData = my_file.read() > print(rawData) > > #Separating body text from metadata. This code only works if the > textfile has one article. > > articleStart = rawData.find("<div class=\"story-element story-element- > text\">") > articlemetaData = rawData[:articleStart] > articleBody = rawData[articleStart:] This works for a single article (provided the div never crosses a line boundary which it is perfectly entitled to do). But you cannot find the closing <div> without a huge amount of effort since there could be other divs within the body. But, to find subsequent articles you need to restart your search after your article. So you need to find the end of the article. And that is going to involve going through counting div and /div pairs until they come to zero and you hit an unmatched /div, which will be your closing /div. You can then reset the start of file at that point and go back to the start. ie the beginning of your "loop". [ You could of course ignore the end of file data and just search for the start of the next file tag, I'd suggest <html>should be the starting point rather than <H1> since there's nothing to prevent the author using H1 anywhere in the body. That will be easier than identifying the closing </div>. But it will make the body html unbalanced and therefore harder to process in later steps (if there are any).] That's a lot of work to avoid using a parser which already does all that for you! And will extract all the articles (however many there are) in one pass. A parser will also cope with most tweaks or changes to the html that the publisher may make whereas not using a parser will require you to constantly tweak your code to match. It's a job for life and if you have the time to spare that's OK.. It's your time. A slightly easier approach if you have the option is to keep the articles in separate files. Looping over multiple files using code that works for one file is much easier. -- 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 sb.monzur at gmail.com Wed Mar 17 07:27:53 2021 From: sb.monzur at gmail.com (S Monzur) Date: Wed, 17 Mar 2021 17:27:53 +0600 Subject: [Tutor] How to write a loop in python to find HTML tags in a text file In-Reply-To: <s2sm0m$11v5$1@ciao.gmane.io> References: <CAKXx11y2wW9k83zotgroThrROhJeqnkbTfjv2NQ0KPY=bMQ_HA@mail.gmail.com> <s2sm0m$11v5$1@ciao.gmane.io> Message-ID: <CAKXx11w+UBpyrpXyWeLJDpE9C44tQM5LVt0nSvSm1PCXSWQjuQ@mail.gmail.com> Thank you for explaining the process. Might you advise me on how to use beautiful soup on this text file to a) separate the metadata from the bodytext and b) remove all the html tags from the body text, and print the clean body text of three articles separately? On Wed, Mar 17, 2021 at 4:35 PM Alan Gauld via Tutor <tutor at python.org> wrote: > On 17/03/2021 05:15, S Monzur wrote: > > > data from 3 news articles scraped from a news website. I would like to > > write a loop that separates the metadata from the article body for each > of > > these three articles. The linked code <https://pastebin.com/FU2Axiuc > >works > > for a single news article only (i.e., if I keep only one article in the > > text file). People have previously suggested using beautiful soup and > > regular expressions, but please note that I just want to modify the > > existing code to add a loop, and not use any other methods/functions. > > The correct answer is indeed to use an html parser like Beautiful Soup. > Not regular expressions, they are unreliable with HTML data. > > Your code is short enough to post, no need for pastebin. > > > with open('threenewsarticles.txt', 'r', encoding='utf8') as my_file: > > rawData = my_file.read() > > print(rawData) > > > > #Separating body text from metadata. This code only works if the > > textfile has one article. > > > > articleStart = rawData.find("<div class=\"story-element story-element- > > text\">") > > articlemetaData = rawData[:articleStart] > > articleBody = rawData[articleStart:] > > This works for a single article (provided the div never crosses > a line boundary which it is perfectly entitled to do). > But you cannot find the closing <div> without a huge amount > of effort since there could be other divs within the body. > > But, to find subsequent articles you need to restart your search > after your article. So you need to find the end of the article. And that > is going to involve going through counting div and /div pairs until they > come to zero and you hit an unmatched /div, which will be your closing > /div. You can then reset the start of file at that point and go back to > the start. ie the beginning of your "loop". > > [ You could of course ignore the end of file data and just search > for the start of the next file tag, I'd suggest <html>should be the > starting point rather than <H1> since there's nothing to prevent > the author using H1 anywhere in the body. That will be easier than > identifying the closing </div>. But it will make the body html > unbalanced and therefore harder to process in later steps > (if there are any).] > > That's a lot of work to avoid using a parser which already does > all that for you! And will extract all the articles (however > many there are) in one pass. A parser will also cope with most > tweaks or changes to the html that the publisher may make > whereas not using a parser will require you to constantly > tweak your code to match. It's a job for life and if you > have the time to spare that's OK.. It's your time. > > A slightly easier approach if you have the option is to > keep the articles in separate files. Looping over > multiple files using code that works for one file > is much easier. > > -- > 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 Mar 17 14:19:43 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 17 Mar 2021 18:19:43 +0000 Subject: [Tutor] How to write a loop in python to find HTML tags in a text file In-Reply-To: <CAKXx11w+UBpyrpXyWeLJDpE9C44tQM5LVt0nSvSm1PCXSWQjuQ@mail.gmail.com> References: <CAKXx11y2wW9k83zotgroThrROhJeqnkbTfjv2NQ0KPY=bMQ_HA@mail.gmail.com> <s2sm0m$11v5$1@ciao.gmane.io> <CAKXx11w+UBpyrpXyWeLJDpE9C44tQM5LVt0nSvSm1PCXSWQjuQ@mail.gmail.com> Message-ID: <s2th7v$p4t$1@ciao.gmane.io> On 17/03/2021 11:27, S Monzur wrote: > Thank you for explaining the process. Might you advise me on how to use > beautiful soup on this text file to a) separate the metadata from the > bodytext and b) remove all the html tags I don't have BS installed at present. Maybe someone who does can contribute a solution? I did try with the standard python HTML parser but it seems your file only has part of the HTML rather than the comlete html text. That confuses the parser so it won't work (BS may work since it is much more forgiving than the standard one). If you have access to the original html and can save each message as a separate file that will make everyone's life much easier. If you don;t then you may have to stick to your original strategy and live with the pain. FWIW Here is the standard library version for stripping the messages. Hopefully the basic technique is obvious.: import html.parser class MessageParser(html.parser.HTMLParser): def __init__(self): super().__init__() self.inMessage = False def handle_starttag(self, name, atts): if name == "div": for key,val in atts: if key == 'class' and val == "story-element story-element-text": self.inMessage = True def handle_endtag(self, name): self.inMessage = False def handle_data(self,data): if self.inMessage: print('\n----------------\n', data) if __name__ == "__main__": with open('articles.txt') as htm: parser = MessageParser() parser.feed(htm) For stripping tags you can use an external program for that - html2txt It's available for Linux and MacOS, I don't know about Windows. But that would be the simplest option if you have it available. Otherwise you can write code like the above that detects all "text like" tags (Hn, P, etc.) and only prints the body. You will need to decide what to do with lists and tables 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 adameyring at gmail.com Thu Mar 18 11:11:36 2021 From: adameyring at gmail.com (Adam Eyring) Date: Thu, 18 Mar 2021 11:11:36 -0400 Subject: [Tutor] How to write a loop in python to find HTML tags in a text file In-Reply-To: <s2th7v$p4t$1@ciao.gmane.io> References: <CAKXx11y2wW9k83zotgroThrROhJeqnkbTfjv2NQ0KPY=bMQ_HA@mail.gmail.com> <s2sm0m$11v5$1@ciao.gmane.io> <CAKXx11w+UBpyrpXyWeLJDpE9C44tQM5LVt0nSvSm1PCXSWQjuQ@mail.gmail.com> <s2th7v$p4t$1@ciao.gmane.io> Message-ID: <CAPStRW-2x5rZ1guFiv+izW6MPBpBAB0yot0aEKu7O+DA5Nh6iA@mail.gmail.com> On Wed, Mar 17, 2021 at 2:20 PM Alan Gauld via Tutor <tutor at python.org> wrote: > On 17/03/2021 11:27, S Monzur wrote: > > Thank you for explaining the process. Might you advise me on how to use > > beautiful soup on this text file to a) separate the metadata from the > > bodytext and b) remove all the html tags > > I don't have BS installed at present. Maybe someone who > does can contribute a solution? > > I don't work with Beautiful Soup, but this chapter in the free PDF book Automate the Boring Stuff with Python may help: https://automatetheboringstuff.com/2e/chapter12/ bs4 is the Beautiful Soup module. AME From manpritsinghece at gmail.com Fri Mar 19 11:55:35 2021 From: manpritsinghece at gmail.com (Manprit Singh) Date: Fri, 19 Mar 2021 21:25:35 +0530 Subject: [Tutor] Question related to for loops Message-ID: <CAO1OCwb96ma-Kcf6JP8VuyD43up2NXKzGKQm2iMDR0ajHE_RYQ@mail.gmail.com> Dear sir , consider a problem of finding sum of even and odd numbers inside a list.. One can write a function like this : def odd_evensum(x): odd, even = 0, 0 for i in x: if i%2 != 0: odd = odd + i else: even = even + i return odd, even In an other way if i write the same function like this : def odd_evensum(x): odd = sum(i for i in x if i%2 != 0) even = sum(i for i in x if i%2 == 0) return odd, even what should be preferred? From __peter__ at web.de Fri Mar 19 13:04:51 2021 From: __peter__ at web.de (Peter Otten) Date: Fri, 19 Mar 2021 18:04:51 +0100 Subject: [Tutor] Question related to for loops In-Reply-To: <CAO1OCwb96ma-Kcf6JP8VuyD43up2NXKzGKQm2iMDR0ajHE_RYQ@mail.gmail.com> References: <CAO1OCwb96ma-Kcf6JP8VuyD43up2NXKzGKQm2iMDR0ajHE_RYQ@mail.gmail.com> Message-ID: <f976628e-4756-76fa-f341-69d7301c4dc8@web.de> On 19/03/2021 16:55, Manprit Singh wrote: > Dear sir , > > consider a problem of finding sum of even and odd numbers inside a list.. > One can write a function like this : > > def odd_evensum(x): > odd, even = 0, 0 > for i in x: > if i%2 != 0: > odd = odd + i > else: > even = even + i > return odd, even > > In an other way if i write the same function like this : > def odd_evensum(x): > odd = sum(i for i in x if i%2 != 0) > even = sum(i for i in x if i%2 == 0) > return odd, even > > what should be preferred? The first version because it is more likely to produce the correct output. I would clean up it a bit though: def odd_evensum3(x): odd = 0 even = 0 for i in x: if i % 2: odd += i else: even += i return odd, even For bonus points: can you come up with an argument that would return (4, 2) for the first version of odd_evensum() and (4, 0) for the second? From alan.gauld at yahoo.co.uk Fri Mar 19 15:20:27 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 19 Mar 2021 19:20:27 +0000 Subject: [Tutor] Question related to for loops In-Reply-To: <CAO1OCwb96ma-Kcf6JP8VuyD43up2NXKzGKQm2iMDR0ajHE_RYQ@mail.gmail.com> References: <CAO1OCwb96ma-Kcf6JP8VuyD43up2NXKzGKQm2iMDR0ajHE_RYQ@mail.gmail.com> Message-ID: <s32thr$5mt$1@ciao.gmane.io> On 19/03/2021 15:55, Manprit Singh wrote: > In an other way if i write the same function like this : > def odd_evensum(x): > odd = sum(i for i in x if i%2 != 0) > even = sum(i for i in x if i%2 == 0) > return odd, even > > what should be preferred? In this case you loop over the input list twice - once for odds and once for evens. Then you loop over each of the resultant lists again to do the summations. I haven't measured it but would expect the second version to be significantly slower for large input lists. -- 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 Mar 19 15:33:31 2021 From: breamoreboy at gmail.com (Mark Lawrence) Date: Fri, 19 Mar 2021 19:33:31 +0000 Subject: [Tutor] Question related to for loops In-Reply-To: <s32thr$5mt$1@ciao.gmane.io> References: <CAO1OCwb96ma-Kcf6JP8VuyD43up2NXKzGKQm2iMDR0ajHE_RYQ@mail.gmail.com> <s32thr$5mt$1@ciao.gmane.io> Message-ID: <ad22859e-6b24-5551-0125-c8b2e258fa54@gmail.com> On 19/03/2021 19:20, Alan Gauld via Tutor wrote: > On 19/03/2021 15:55, Manprit Singh wrote: > >> In an other way if i write the same function like this : >> def odd_evensum(x): >> odd = sum(i for i in x if i%2 != 0) >> even = sum(i for i in x if i%2 == 0) >> return odd, even >> >> what should be preferred? > > In this case you loop over the input list twice > - once for odds and once for evens. > Then you loop over each of the resultant lists > again to do the summations. I don't see any resultant lists to loop over, only a generator expression being passed to sum. > > I haven't measured it but would expect the second > version to be significantly slower for large input > lists. > -- 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 Mar 19 17:04:32 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 19 Mar 2021 21:04:32 +0000 Subject: [Tutor] Question related to for loops In-Reply-To: <ad22859e-6b24-5551-0125-c8b2e258fa54@gmail.com> References: <CAO1OCwb96ma-Kcf6JP8VuyD43up2NXKzGKQm2iMDR0ajHE_RYQ@mail.gmail.com> <s32thr$5mt$1@ciao.gmane.io> <ad22859e-6b24-5551-0125-c8b2e258fa54@gmail.com> Message-ID: <s333l0$pk6$1@ciao.gmane.io> On 19/03/2021 19:33, Mark Lawrence wrote: >>> odd = sum(i for i in x if i%2 != 0) >>> even = sum(i for i in x if i%2 == 0) > I don't see any resultant lists to loop over, only a generator > expression being passed to sum. Good point, I was thinking the generator built a list then summed it but with a generator it adds as it goes. But it is still looping over the original list twice so I'd still expect it to be slower. (Although 2 generators in C might beat a single python loop) As always with performance questions you'd need to measure it on real world data. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alan.gauld at yahoo.co.uk Fri Mar 19 17:16:29 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 19 Mar 2021 21:16:29 +0000 Subject: [Tutor] How to write a loop in python to find HTML tags in a text file In-Reply-To: <CAKXx11w+UBpyrpXyWeLJDpE9C44tQM5LVt0nSvSm1PCXSWQjuQ@mail.gmail.com> References: <CAKXx11y2wW9k83zotgroThrROhJeqnkbTfjv2NQ0KPY=bMQ_HA@mail.gmail.com> <s2sm0m$11v5$1@ciao.gmane.io> <CAKXx11w+UBpyrpXyWeLJDpE9C44tQM5LVt0nSvSm1PCXSWQjuQ@mail.gmail.com> Message-ID: <s334bd$cb5$1@ciao.gmane.io> On 17/03/2021 11:27, S Monzur wrote: > Thank you for explaining the process. Might you advise me on how to use > beautiful soup on this text file to a) separate the metadata from the > bodytext nobody has come back with a BS solution so I'll try to address the original issue... >> This works for a single article (provided the div never crosses >> a line boundary which it is perfectly entitled to do). >> But you cannot find the closing <div> without a huge amount >> of effort since there could be other divs within the body. You haven't answered this impliec question. But assuming you a) cannot use the full original html message (the easiest solution) and b) you don't care about the content or format of the message body(as implied by your partial solution) Then you can add a loop by simply doing the following (in pseudo code) with (open input file) as inf instring == inf.read() #read the whole file messages = [] while instring: msgStart = instring.find(STARTTAG)) msgEnd = instring.find(ENDTAG, msgStart) messages.append(insytring[msgStart:msgEnd] instring = instring[msgEnd:] You will need to convert that to Python... >> A slightly easier approach if you have the option is to >> keep the articles in separate files. This allows you to put your exoistoing code in a function and call it in a loop: messages = [getMessageBody(msgFile) for msgFile in os.listdir(MSGDIR)] 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 alan.gauld at yahoo.co.uk Fri Mar 19 19:27:05 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 19 Mar 2021 23:27:05 +0000 Subject: [Tutor] Question related to for loops In-Reply-To: <857a5gtsrdv5custkokksfujfu4q590ool@4ax.com> References: <CAO1OCwb96ma-Kcf6JP8VuyD43up2NXKzGKQm2iMDR0ajHE_RYQ@mail.gmail.com> <857a5gtsrdv5custkokksfujfu4q590ool@4ax.com> Message-ID: <s33c09$l17$1@ciao.gmane.io> On 19/03/2021 21:54, Dennis Lee Bieber wrote: > Besides the fact that it processes the input list TWICE, once each for > odd and even values, I think you meant for > > i for i in x if > > to be > > 1 for i in x if > > You want a count of how many even/odd entries there are, not a sum According to his original post he does actually want the sum. It's an odd request but its what both of his examples generate. And he specifically said: >>> consider a problem of finding sum of even and odd numbers >>> inside a list.. -- 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 manpritsinghece at gmail.com Sun Mar 21 03:15:18 2021 From: manpritsinghece at gmail.com (Manprit Singh) Date: Sun, 21 Mar 2021 12:45:18 +0530 Subject: [Tutor] consider a problem of finding if two lists have any item in common Message-ID: <CAO1OCwbWvCstXP1Rcot8sq5wfLB+yVVVDw7bXMxh6beDF4icwA@mail.gmail.com> Dear sir , take two lists lst1 = [2, 4, 6, 9] lst2 = [2, 5, 9, 0] this can be done using isdisjoint(other), it will return True if the lists have no items in common .The official documentation says nothing about other , just need to know if other can be an iterable or it should strictly be a set or frozenset? what should be the correct way ? This set(lst1).isdisjoint(set(lst2)) Or This set(lst1).isdisjoint(lst2) Regards Manprit Singh From joel.goldstick at gmail.com Sun Mar 21 04:07:12 2021 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Sun, 21 Mar 2021 04:07:12 -0400 Subject: [Tutor] consider a problem of finding if two lists have any item in common In-Reply-To: <CAO1OCwbWvCstXP1Rcot8sq5wfLB+yVVVDw7bXMxh6beDF4icwA@mail.gmail.com> References: <CAO1OCwbWvCstXP1Rcot8sq5wfLB+yVVVDw7bXMxh6beDF4icwA@mail.gmail.com> Message-ID: <CAPM-O+yboy51wee9g6Nd5jzb5PBJJJm4t0WAkDdK6ZTmEU9QCA@mail.gmail.com> On Sun, Mar 21, 2021 at 3:16 AM Manprit Singh <manpritsinghece at gmail.com> wrote: > > Dear sir , > take two lists > > lst1 = [2, 4, 6, 9] > lst2 = [2, 5, 9, 0] > this can be done using isdisjoint(other), it will return True if the lists > have no items in common .The official documentation says nothing about > other , just need to know if other can be an iterable or it should strictly > be a set or frozenset? > what should be the correct way ? > This > set(lst1).isdisjoint(set(lst2)) > Or This > set(lst1).isdisjoint(lst2) > > Regards > Manprit Singh > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor It looks like the l2 can be a list >>> l1 = [1,2,3] >>> l2 = [2,4,7] >>> set(l1).isdisjoint(l2) False >>> -- Joel Goldstick http://joelgoldstick.com/blog http://cc-baseballstats.info/stats/birthdays From alan.gauld at yahoo.co.uk Sun Mar 21 04:52:53 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 21 Mar 2021 08:52:53 +0000 Subject: [Tutor] consider a problem of finding if two lists have any item in common In-Reply-To: <CAO1OCwbWvCstXP1Rcot8sq5wfLB+yVVVDw7bXMxh6beDF4icwA@mail.gmail.com> References: <CAO1OCwbWvCstXP1Rcot8sq5wfLB+yVVVDw7bXMxh6beDF4icwA@mail.gmail.com> Message-ID: <s371h5$p8t$1@ciao.gmane.io> On 21/03/2021 07:15, Manprit Singh wrote: > lst1 = [2, 4, 6, 9] > lst2 = [2, 5, 9, 0] > this can be done using isdisjoint(other), it will return True if the lists > have no items in common .The official documentation says nothing about > other , just need to know if other can be an iterable or it should strictly > be a set or frozenset? > what should be the correct way ? Don't ask us, ask the interpreter, it knows best. And it gives faster answers too. If its answers aren't what you'd expect *then* ask us why? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From __peter__ at web.de Sun Mar 21 10:53:02 2021 From: __peter__ at web.de (Peter Otten) Date: Sun, 21 Mar 2021 15:53:02 +0100 Subject: [Tutor] consider a problem of finding if two lists have any item in common In-Reply-To: <s371h5$p8t$1@ciao.gmane.io> References: <CAO1OCwbWvCstXP1Rcot8sq5wfLB+yVVVDw7bXMxh6beDF4icwA@mail.gmail.com> <s371h5$p8t$1@ciao.gmane.io> Message-ID: <76195d4b-9a83-cc0a-f2b4-34c3deebfe81@web.de> On 21/03/2021 09:52, Alan Gauld via Tutor wrote: > On 21/03/2021 07:15, Manprit Singh wrote: > >> lst1 = [2, 4, 6, 9] >> lst2 = [2, 5, 9, 0] >> this can be done using isdisjoint(other), it will return True if the lists >> have no items in common .The official documentation says nothing about >> other , just need to know if other can be an iterable or it should strictly >> be a set or frozenset? >> what should be the correct way ? > > Don't ask us, ask the interpreter, it knows best. > And it gives faster answers too. > > If its answers aren't what you'd expect *then* ask us why? While I tend to regard the interpreter's "answer" as authoritative there is always a small chance that you rely on an implementation accident or even a bug. After a quick look into the documentation my impression is that for most (all other?) set methods an iterable instead of a set is explicitly allowed. Therefore I'd expect that a documentation bug report would be welcome. From __peter__ at web.de Sun Mar 21 10:53:02 2021 From: __peter__ at web.de (Peter Otten) Date: Sun, 21 Mar 2021 15:53:02 +0100 Subject: [Tutor] consider a problem of finding if two lists have any item in common In-Reply-To: <s371h5$p8t$1@ciao.gmane.io> References: <CAO1OCwbWvCstXP1Rcot8sq5wfLB+yVVVDw7bXMxh6beDF4icwA@mail.gmail.com> <s371h5$p8t$1@ciao.gmane.io> Message-ID: <76195d4b-9a83-cc0a-f2b4-34c3deebfe81@web.de> On 21/03/2021 09:52, Alan Gauld via Tutor wrote: > On 21/03/2021 07:15, Manprit Singh wrote: > >> lst1 = [2, 4, 6, 9] >> lst2 = [2, 5, 9, 0] >> this can be done using isdisjoint(other), it will return True if the lists >> have no items in common .The official documentation says nothing about >> other , just need to know if other can be an iterable or it should strictly >> be a set or frozenset? >> what should be the correct way ? > > Don't ask us, ask the interpreter, it knows best. > And it gives faster answers too. > > If its answers aren't what you'd expect *then* ask us why? While I tend to regard the interpreter's "answer" as authoritative there is always a small chance that you rely on an implementation accident or even a bug. After a quick look into the documentation my impression is that for most (all other?) set methods an iterable instead of a set is explicitly allowed. Therefore I'd expect that a documentation bug report would be welcome. From mats at wichmann.us Sun Mar 21 11:01:18 2021 From: mats at wichmann.us (Mats Wichmann) Date: Sun, 21 Mar 2021 09:01:18 -0600 Subject: [Tutor] consider a problem of finding if two lists have any item in common In-Reply-To: <76195d4b-9a83-cc0a-f2b4-34c3deebfe81@web.de> References: <CAO1OCwbWvCstXP1Rcot8sq5wfLB+yVVVDw7bXMxh6beDF4icwA@mail.gmail.com> <s371h5$p8t$1@ciao.gmane.io> <76195d4b-9a83-cc0a-f2b4-34c3deebfe81@web.de> Message-ID: <37ebf01f-8580-4ea6-39ed-ab459042bcfd@wichmann.us> On 3/21/21 8:53 AM, Peter Otten wrote: > On 21/03/2021 09:52, Alan Gauld via Tutor wrote: >> On 21/03/2021 07:15, Manprit Singh wrote: >> >>> lst1 = [2, 4, 6, 9] >>> lst2 = [2, 5, 9, 0] >>> this can be done using isdisjoint(other), it will return True if the >>> lists >>> have no items in common .The official documentation says nothing about >>> other , just need to know if other can be an iterable or it should >>> strictly >>> be a set or frozenset? >>> what should be the correct way ? >> >> Don't ask us, ask the interpreter, it knows best. >> And it gives faster answers too. >> >> If its answers aren't what you'd expect *then* ask us why? I'd perhaps also note that while the answer to the question asked may indeed be interesting, a lot of the time you actually want to know _what_ they have in common, and that would be a different operation. Which you can always truth-test to find out if they indeed had any common members. From alan.gauld at yahoo.co.uk Sun Mar 21 12:40:56 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 21 Mar 2021 16:40:56 +0000 Subject: [Tutor] consider a problem of finding if two lists have any item in common In-Reply-To: <76195d4b-9a83-cc0a-f2b4-34c3deebfe81@web.de> References: <76195d4b-9a83-cc0a-f2b4-34c3deebfe81@web.de> Message-ID: <441304C8-72E7-4862-82A1-094D11A7E3C2@yahoo.co.uk> Sent from my iPad > On 21 Mar 2021, at 14:54, Peter Otten <__peter__ at web.de> wrote: > > While I tend to regard the interpreter's "answer" as authoritative there > is always a small chance that you rely on an implementation accident or > even a bug. > Yes, but that?s a very small chance. Especially for a well established language feature. And I?d argue there?s at least as much chance of being given misinformation, or incomplete information, asking on a group like tutor! But it?s always much quicker to ask the interpreter in the first instance. Then if doubt remains you can present evidence why you think things are other than they should be. Alan g. From __peter__ at web.de Sun Mar 21 13:39:55 2021 From: __peter__ at web.de (Peter Otten) Date: Sun, 21 Mar 2021 18:39:55 +0100 Subject: [Tutor] consider a problem of finding if two lists have any item in common In-Reply-To: <37ebf01f-8580-4ea6-39ed-ab459042bcfd@wichmann.us> References: <CAO1OCwbWvCstXP1Rcot8sq5wfLB+yVVVDw7bXMxh6beDF4icwA@mail.gmail.com> <s371h5$p8t$1@ciao.gmane.io> <76195d4b-9a83-cc0a-f2b4-34c3deebfe81@web.de> <37ebf01f-8580-4ea6-39ed-ab459042bcfd@wichmann.us> Message-ID: <9719b25f-9ea0-b233-2f15-ecfce5ab63da@web.de> On 21/03/2021 16:01, Mats Wichmann wrote: > On 3/21/21 8:53 AM, Peter Otten wrote: >> On 21/03/2021 09:52, Alan Gauld via Tutor wrote: >>> On 21/03/2021 07:15, Manprit Singh wrote: >>> >>>> lst1 = [2, 4, 6, 9] >>>> lst2 = [2, 5, 9, 0] >>>> this can be done using isdisjoint(other), it will return True if the >>>> lists >>>> have no items in common .The official documentation says nothing about >>>> other , just need to know if other can be an iterable or it should >>>> strictly >>>> be a set or frozenset? >>>> what should be the correct way ? >>> >>> Don't ask us, ask the interpreter, it knows best. >>> And it gives faster answers too. >>> >>> If its answers aren't what you'd expect *then* ask us why? > > I'd perhaps also note that while the answer to the question asked may > indeed be interesting, a lot of the time you actually want to know > _what_ they have in common, and that would be a different operation. > Which you can always truth-test to find out if they indeed had any > common members. The big difference between a.isdisjoint(b) and (not a & b) being that the first can stop after the first common element. Though I wouldn't go so far as to claim that the use cases are disjoint ;) From __peter__ at web.de Sun Mar 21 13:39:55 2021 From: __peter__ at web.de (Peter Otten) Date: Sun, 21 Mar 2021 18:39:55 +0100 Subject: [Tutor] consider a problem of finding if two lists have any item in common In-Reply-To: <37ebf01f-8580-4ea6-39ed-ab459042bcfd@wichmann.us> References: <CAO1OCwbWvCstXP1Rcot8sq5wfLB+yVVVDw7bXMxh6beDF4icwA@mail.gmail.com> <s371h5$p8t$1@ciao.gmane.io> <76195d4b-9a83-cc0a-f2b4-34c3deebfe81@web.de> <37ebf01f-8580-4ea6-39ed-ab459042bcfd@wichmann.us> Message-ID: <9719b25f-9ea0-b233-2f15-ecfce5ab63da@web.de> On 21/03/2021 16:01, Mats Wichmann wrote: > On 3/21/21 8:53 AM, Peter Otten wrote: >> On 21/03/2021 09:52, Alan Gauld via Tutor wrote: >>> On 21/03/2021 07:15, Manprit Singh wrote: >>> >>>> lst1 = [2, 4, 6, 9] >>>> lst2 = [2, 5, 9, 0] >>>> this can be done using isdisjoint(other), it will return True if the >>>> lists >>>> have no items in common .The official documentation says nothing about >>>> other , just need to know if other can be an iterable or it should >>>> strictly >>>> be a set or frozenset? >>>> what should be the correct way ? >>> >>> Don't ask us, ask the interpreter, it knows best. >>> And it gives faster answers too. >>> >>> If its answers aren't what you'd expect *then* ask us why? > > I'd perhaps also note that while the answer to the question asked may > indeed be interesting, a lot of the time you actually want to know > _what_ they have in common, and that would be a different operation. > Which you can always truth-test to find out if they indeed had any > common members. The big difference between a.isdisjoint(b) and (not a & b) being that the first can stop after the first common element. Though I wouldn't go so far as to claim that the use cases are disjoint ;) From joel.goldstick at gmail.com Sun Mar 21 14:42:47 2021 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Sun, 21 Mar 2021 14:42:47 -0400 Subject: [Tutor] consider a problem of finding if two lists have any item in common In-Reply-To: <9719b25f-9ea0-b233-2f15-ecfce5ab63da@web.de> References: <CAO1OCwbWvCstXP1Rcot8sq5wfLB+yVVVDw7bXMxh6beDF4icwA@mail.gmail.com> <s371h5$p8t$1@ciao.gmane.io> <76195d4b-9a83-cc0a-f2b4-34c3deebfe81@web.de> <37ebf01f-8580-4ea6-39ed-ab459042bcfd@wichmann.us> <9719b25f-9ea0-b233-2f15-ecfce5ab63da@web.de> Message-ID: <CAPM-O+y1oDg8KznEMZ6D0teF+fmOS2E+5wOzZVAP4Rn9kQce6g@mail.gmail.com> On Sun, Mar 21, 2021 at 1:41 PM Peter Otten <__peter__ at web.de> wrote: > > On 21/03/2021 16:01, Mats Wichmann wrote: > > On 3/21/21 8:53 AM, Peter Otten wrote: > >> On 21/03/2021 09:52, Alan Gauld via Tutor wrote: > >>> On 21/03/2021 07:15, Manprit Singh wrote: > >>> > >>>> lst1 = [2, 4, 6, 9] > >>>> lst2 = [2, 5, 9, 0] > >>>> this can be done using isdisjoint(other), it will return True if the > >>>> lists > >>>> have no items in common .The official documentation says nothing about > >>>> other , just need to know if other can be an iterable or it should > >>>> strictly > >>>> be a set or frozenset? > >>>> what should be the correct way ? > >>> > >>> Don't ask us, ask the interpreter, it knows best. > >>> And it gives faster answers too. > >>> > >>> If its answers aren't what you'd expect *then* ask us why? > > > > I'd perhaps also note that while the answer to the question asked may > > indeed be interesting, a lot of the time you actually want to know > > _what_ they have in common, and that would be a different operation. > > Which you can always truth-test to find out if they indeed had any > > common members. > > The big difference between a.isdisjoint(b) and (not a & b) being that > the first can stop after the first common element. Though I wouldn't go > so far as to claim that the use cases are disjoint ;) +1 > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor -- Joel Goldstick http://joelgoldstick.com/blog http://cc-baseballstats.info/stats/birthdays From jarkmx at gmail.com Sun Mar 21 18:31:19 2021 From: jarkmx at gmail.com (jark AJ) Date: Sun, 21 Mar 2021 18:31:19 -0400 Subject: [Tutor] string not found in variable Message-ID: <CAAaDumTQ5oVQTnCFsSGtxh0hcTmC11DR9EcaZ2rgJPS39Qr9Ew@mail.gmail.com> Hi All, There are two actions I am trying to achieve here. 1) To check if the connectivity exists between the source and destination 2) To run the test five times to ensure there is no intermittent packet loss we experience. looking to use the nc command here in python, On a bash shell, we do it using for i in {1..5}; do nc -zvw 4 www.google.com 443 ; done I have the following questions: 1) I am storing the subprocess output in a variable called output which is of type string. It has succeeded in the output; however, when I use print("succeeded!" in output), its returning false 2) Is there a way we could keep appending the output like a list in the string. trying to do the success/failure logic at the end based on the results of all five tests. Looking to achieve this using the variable which stores the output. Thank you in advance! ___ import subprocess class Command(object): def __init__(self, cmd): self.cmd = cmd def run(self, shell=False): try: print("Executing Command >>", self.cmd) output = subprocess.Popen( self.cmd.split(), stdout=subprocess.PIPE, shell=shell ) return output.communicate()[0] except subprocess.CalledProcessError as e: return e.returncode def __str__(self): return str(self.cmd) count = 0 while count <= 5: nc_command = Command("nc -zvw 4 www.google.com 443") output = Command.run(nc_command) count += 1 print(output) print(type(output)) print("succeeded!" in output) ___ Output: __ ('Executing Command >>', 'nc -zvw 4 www.google.com 443') Connection to www.google.com port 443 [tcp/https] succeeded! <type 'str'> False __ From PyTutor at DancesWithMice.info Sun Mar 21 18:41:52 2021 From: PyTutor at DancesWithMice.info (dn) Date: Mon, 22 Mar 2021 11:41:52 +1300 Subject: [Tutor] string not found in variable In-Reply-To: <CAAaDumTQ5oVQTnCFsSGtxh0hcTmC11DR9EcaZ2rgJPS39Qr9Ew@mail.gmail.com> References: <CAAaDumTQ5oVQTnCFsSGtxh0hcTmC11DR9EcaZ2rgJPS39Qr9Ew@mail.gmail.com> Message-ID: <1604fd2a-0bc3-9e74-4671-a063632f21ed@DancesWithMice.info> On 22/03/2021 11.31, jark AJ wrote: > Hi All, > > There are two actions I am trying to achieve here. > > 1) To check if the connectivity exists between the source and destination > 2) To run the test five times to ensure there is no intermittent packet > loss we experience. > > looking to use the nc command here in python, On a bash shell, we do it > using > for i in {1..5}; do nc -zvw 4 www.google.com 443 ; done > > I have the following questions: > > 1) I am storing the subprocess output in a variable called output which is > of type string. It has succeeded in the output; however, when I use > print("succeeded!" in output), its returning false > 2) Is there a way we could keep appending the output like a list in the > string. trying to do the success/failure logic at the end based on the > results of all five tests. Looking to achieve this using the variable which > stores the output. > > Thank you in advance! > > ___ > > import subprocess > > class Command(object): > def __init__(self, cmd): > self.cmd = cmd > > def run(self, shell=False): > try: > print("Executing Command >>", self.cmd) > output = subprocess.Popen( > self.cmd.split(), stdout=subprocess.PIPE, shell=shell > ) > return output.communicate()[0] > except subprocess.CalledProcessError as e: > return e.returncode > > def __str__(self): > return str(self.cmd) > > count = 0 > while count <= 5: > nc_command = Command("nc -zvw 4 www.google.com 443") > output = Command.run(nc_command) > count += 1 > print(output) > print(type(output)) > print("succeeded!" in output) > > ___ > > > Output: > __ > > ('Executing Command >>', 'nc -zvw 4 www.google.com 443') > Connection to www.google.com port 443 [tcp/https] succeeded! > > <type 'str'> > False Is it type byte, cf str? https://docs.python.org/3/library/subprocess.html#using-the-subprocess-module -- Regards, =dn From alan.gauld at yahoo.co.uk Sun Mar 21 20:06:21 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 22 Mar 2021 00:06:21 +0000 Subject: [Tutor] string not found in variable In-Reply-To: <CAAaDumTQ5oVQTnCFsSGtxh0hcTmC11DR9EcaZ2rgJPS39Qr9Ew@mail.gmail.com> References: <CAAaDumTQ5oVQTnCFsSGtxh0hcTmC11DR9EcaZ2rgJPS39Qr9Ew@mail.gmail.com> Message-ID: <s38n1t$fuj$1@ciao.gmane.io> On 21/03/2021 22:31, jark AJ wrote: > for i in {1..5}; do nc -zvw 4 www.google.com 443 ; done > > I have the following questions: > > 1) I am storing the subprocess output in a variable called output which is > of type string. It has succeeded in the output; however, when I use > print("succeeded!" in output), its returning false Just to be picky, the variable is a name it has no type. The output that you are storing has a type however. > 2) Is there a way we could keep appending the output like a list in the > string. A list of strings would seem the most logical option. > trying to do the success/failure logic at the end based on the > results of all five tests. Looking to achieve this using the variable which > stores the output. I'm less clear what you mean here. > import subprocess > > class Command(object): > def __init__(self, cmd): > self.cmd = cmd > > def run(self, shell=False): > try: > print("Executing Command >>", self.cmd) > output = subprocess.Popen( > self.cmd.split(), stdout=subprocess.PIPE, shell=shell > ) > return output.communicate()[0] > except subprocess.CalledProcessError as e: > return e.returncode There's an aphorism "When you write a class that only has one method and an init you should really write a function. This is compounded when you only create one instance. Its all a bit of overkill. > def __str__(self): > return str(self.cmd) You don't appear to ever call the str() method. > count = 0 > while count <= 5: This is probably easier written as for _ in range(6): But I suspect from your preamble you really meant for _ in range(5): to match your shell example. > nc_command = Command("nc -zvw 4 www.google.com 443") > output = Command.run(nc_command) You could probably replace all that with a simple call to the subprocess module's check_output() function. > print(output) > print(type(output)) > print("succeeded!" in output) > Output: > __ > > ('Executing Command >>', 'nc -zvw 4 www.google.com 443') > Connection to www.google.com port 443 [tcp/https] succeeded! > > <type 'str'> > False I see David has already suggested the output might in fact by bytes and the docs say: ======= Captured stdout from the child process. A bytes sequence, or a string if run() was called with an encoding, errors, or text=True ========= So it looks like output should be bytes despite what the type() result says. In that case you need to decode() the bytes into a string before comparing with a string. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From cs at cskk.id.au Sun Mar 21 20:11:08 2021 From: cs at cskk.id.au (Cameron Simpson) Date: Mon, 22 Mar 2021 11:11:08 +1100 Subject: [Tutor] string not found in variable In-Reply-To: <CAAaDumTQ5oVQTnCFsSGtxh0hcTmC11DR9EcaZ2rgJPS39Qr9Ew@mail.gmail.com> References: <CAAaDumTQ5oVQTnCFsSGtxh0hcTmC11DR9EcaZ2rgJPS39Qr9Ew@mail.gmail.com> Message-ID: <YFfgnD8/JPtlCdit@cskk.homeip.net> On 21Mar2021 18:31, jark AJ <jarkmx at gmail.com> wrote: > There are two actions I am trying to achieve here. > >1) To check if the connectivity exists between the source and destination >2) To run the test five times to ensure there is no intermittent packet >loss we experience. > >looking to use the nc command here in python, On a bash shell, we do it >using >for i in {1..5}; do nc -zvw 4 www.google.com 443 ; done > >I have the following questions: > >1) I am storing the subprocess output in a variable called output which is >of type string. It has succeeded in the output; however, when I use >print("succeeded!" in output), its returning false Well, that means "succeeded!" is not in the output :-) You'll notice that .communicate() returns (stdout_data,stderr_data). I would expect that "succeeded!" is in the stderr_data error output. In the UNIX model, processes by default have an input, an output and an error output. The separation of the standard output from the rror output is what lets you write a pipeline like this: nc .... | grep something-fromthe-server | ... and still see the error messages - only the standard error output is directed through the pipe. Also, the error output is _not_ only for error messages - it is generally used for "noise", such as progress reporting, which is not to be mixed in with the commands intended output. Supposing you'd connected some API and wanted to process maybe some JSON response. You'd find that 'succeeded!" message polluting the JSON very troublesome. Such a bug, where informative messages are littering the data stream is called a Plug in the Taxonimy of Bugs :-) The other thing I want to recommend is to _not_ rely on finding cheery strings like "succeeded!" in the output (or error output) as an indication of success/failure. (I suspect you're not, you're just trying to debug, but still...) All programmes should exit with a meaningful exit code, 0 for success, nonzero for failure. Test that. Testing the output, particularly the error output is (a) very very programme specific, whereas the exit code ("returncode" in the subprocess docs) is reliable and general and (b) very very fragile to changes in the tool you're testing. For example, running your nmap command here (MacOS) _does not_ show the string "succeeded!". Same programme, just a different revision. >2) Is there a way we could keep appending the output like a list in the >string. trying to do the success/failure logic at the end based on the >results of all five tests. Looking to achieve this using the variable which >stores the output. Of course. Make a list: outputs = [] ..... communicate .... outputs.append(error_data) Cheers, Cameron Simpson <cs at cskk.id.au> From jarkmx at gmail.com Sun Mar 21 20:55:43 2021 From: jarkmx at gmail.com (jark AJ) Date: Sun, 21 Mar 2021 20:55:43 -0400 Subject: [Tutor] string not found in variable In-Reply-To: <70of5gtkoaeo3eoe0ff2nsoa4tq97so1g9@4ax.com> References: <CAAaDumTQ5oVQTnCFsSGtxh0hcTmC11DR9EcaZ2rgJPS39Qr9Ew@mail.gmail.com> <p0lf5gdlm022gplat2910l4fjriosnhf2o@4ax.com> <70of5gtkoaeo3eoe0ff2nsoa4tq97so1g9@4ax.com> Message-ID: <CAAaDumSwfAhQD+DU=WOL2Ta97wC1sXfPaxvkT8p05-YH2asa4w@mail.gmail.com> Thank you so much Cameron,Dennis,dn and Alan Hi Cameron/Dennis, Yes, it's logged in the stderr :). Thank you so much once again!!! Hi Alan, Yes, Understood, the idea behind using a class instead of a function is that looking to use the same class for any commands that we may need to run in future. Thank you again for your feedback, I will try to improve it. THANK YOU ALL!!!!! ____ ('Executing Command >>', 'nc -zvw 4 www.google.com 443') Connection to www.google.com port 443 [tcp/https] succeeded! <type 'str'> True ___ import subprocess class Command(object): def __init__(self, cmd): self.cmd = cmd def run(self, shell=False): try: print("Executing Command >>", self.cmd) output = subprocess.Popen( self.cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=shell ) return output.communicate() except subprocess.SubprocessError as error: return error def __str__(self): return str(self.cmd) for i in range(5): command = Command("nc -zvw 4 www.google.com 443") output, my_check = command.run() print(my_check) print(type(output)) print("succeeded!" in my_check) __ On Sun, Mar 21, 2021 at 8:34 PM Dennis Lee Bieber <wlfraed at ix.netcom.com> wrote: > o/~ Talking to myself in public o/~ > > On Sun, 21 Mar 2021 19:28:41 -0400, Dennis Lee Bieber > <wlfraed at ix.netcom.com> declaimed the following: > > > As mentioned above, I'd likely just replace the above with the > contents > >of the run method, modified to put the actual command in needed places. > > > > On a whim, I installed netcat into a Windows Subsystem for Linux > Debian > instance, and tried to produce "my version" of the above. > > Unfortunately, that version isn't producing any usable output -- a > lot > of warnings to stderr... > > wulfraed at ElusiveUnicorn:~$ python3 nctest.py > DNS fwd/rev mismatch: www.google.com != dfw06s48-in-f100.1e100.net > www.google.com [216.58.194.100] 443 (https) open > DNS fwd/rev mismatch: www.google.com != dfw25s25-in-f4.1e100.net > www.google.com [172.217.1.228] 443 (https) open > DNS fwd/rev mismatch: www.google.com != dfw25s41-in-f4.1e100.net > www.google.com [142.250.68.164] 443 (https) open > DNS fwd/rev mismatch: www.google.com != dfw06s49-in-f4.1e100.net > www.google.com [216.58.194.132] 443 (https) open > DNS fwd/rev mismatch: www.google.com != dfw28s04-in-f4.1e100.net > www.google.com [172.217.12.36] 443 (https) open > b'' > False > b'' > False > b'' > False > b'' > False > b'' > False > wulfraed at ElusiveUnicorn:~$ > > The code is: > > wulfraed at ElusiveUnicorn:~$ cat nctest.py > import subprocess > > def runner(): > try: > myProcess = subprocess.Popen( > "nc -zvw 4 www.google.com 443".split(), > stdout=subprocess.PIPE, > shell=False) > return myProcess.communicate()[0] > except subprocess.CalledProcessError as e: > return str(e.returncode) > > output = [runner() for _ in range(5)] > for ln in output: > print(ln) > print(b"success!" in ln) > > wulfraed at ElusiveUnicorn:~$ > > Trying it in a full Debian running in Oracle VirtualBox produces > similar results... > > wulfraed at debian:~$ python3 /media/VB_Shared/nctest.py > DNS fwd/rev mismatch: www.google.com != dfw28s04-in-f4.1e100.net > www.google.com [172.217.12.36] 443 (https) open > Warning: inverse host lookup failed for 142.250.138.106: Host name lookup > failure > Warning: inverse host lookup failed for 142.250.138.103: Host name lookup > failure > Warning: inverse host lookup failed for 142.250.138.105: Host name lookup > failure > Warning: inverse host lookup failed for 142.250.138.104: Host name lookup > failure > Warning: inverse host lookup failed for 142.250.138.147: Host name lookup > failure > Warning: inverse host lookup failed for 142.250.138.99: Host name lookup > failure > www.google.com [142.250.138.106] 443 (https) open > DNS fwd/rev mismatch: www.google.com != dfw28s04-in-f4.1e100.net > www.google.com [172.217.12.36] 443 (https) open > DNS fwd/rev mismatch: www.google.com != ord38s09-in-f4.1e100.net > www.google.com [172.217.9.68] 443 (https) open > DNS fwd/rev mismatch: www.google.com != ord38s09-in-f4.1e100.net > www.google.com [172.217.9.68] 443 (https) open > b'' > False > b'' > False > b'' > False > b'' > False > b'' > False > wulfraed at debian:~$ > > Changing the code to make the output more legible (or meaningful) > > wulfraed at debian:~$ cat /media/VB_Shared/nctest.py > import subprocess > > def runner(): > try: > myProcess = subprocess.Popen( > "nc -zvw 4 www.google.com 443".split(), > stdout=subprocess.PIPE, > stderr=subprocess.PIPE, > shell=False) > return myProcess.communicate() > except subprocess.CalledProcessError as e: > return str(e.returncode) > > output = [runner() for _ in range(5)] > for ln in output: > print("\n\nstdout = %s\nstderr = %s\n" % ln) > print("\tTest: %s" % (b"success!" in ln[0])) > > wulfraed at debian:~$ python3 /media/VB_Shared/nctest.py > > > stdout = b'' > stderr = b'DNS fwd/rev mismatch: www.google.com != > lga15s49-in-f4.1e100.net\nwww.google.com [172.217.5.4] 443 (https) open\n' > > Test: False > > > stdout = b'' > stderr = b'DNS fwd/rev mismatch: www.google.com != > ord37s07-in-f4.1e100.net\nwww.google.com [172.217.1.36] 443 (https) > open\n' > > Test: False > > > stdout = b'' > stderr = b'DNS fwd/rev mismatch: www.google.com != > dfw25s17-in-f4.1e100.net\nwww.google.com [172.217.6.164] 443 (https) > open\n' > > Test: False > > > stdout = b'' > stderr = b'DNS fwd/rev mismatch: www.google.com != > lga15s46-in-f36.1e100.net\nwww.google.com [172.217.4.36] 443 (https) > open\n' > > Test: False > > > stdout = b'' > stderr = b'DNS fwd/rev mismatch: www.google.com != > ord37s08-in-f4.1e100.net\nwww.google.com [172.217.8.164] 443 (https) > open\n' > > Test: False > wulfraed at debian:~$ > > > > -- > Wulfraed Dennis Lee Bieber AF6VN > wlfraed at ix.netcom.com > http://wlfraed.microdiversity.freeddns.org/ > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From jarkmx at gmail.com Sun Mar 21 19:59:26 2021 From: jarkmx at gmail.com (jark AJ) Date: Sun, 21 Mar 2021 19:59:26 -0400 Subject: [Tutor] string not found in variable In-Reply-To: <p0lf5gdlm022gplat2910l4fjriosnhf2o@4ax.com> References: <CAAaDumTQ5oVQTnCFsSGtxh0hcTmC11DR9EcaZ2rgJPS39Qr9Ew@mail.gmail.com> <p0lf5gdlm022gplat2910l4fjriosnhf2o@4ax.com> Message-ID: <CAAaDumS-3a2hAK1vBeMEjVyb9=spQNVf9BpxSrBRWCWEb2CP+w@mail.gmail.com> Thank you so much Dennis and dn for your response. I am using Python Python 2.7.16 Python Python 2.7.16 Output ___ ('Executing Command >>', 'nc -zvw 4 www.google.com 443') Connection to www.google.com port 443 [tcp/https] succeeded! <type 'str'> False __ ___ import subprocess class Command(object): def __init__(self, cmd): self.cmd = cmd def run(self, shell=False): try: print("Executing Command >>", self.cmd) output = subprocess.Popen( self.cmd.split(), stdout=subprocess.PIPE, shell=shell ) return output.communicate()[0] except subprocess.SubprocessError as error: return error def __str__(self): return str(self.cmd) for i in range(5): nc_command = Command("nc -zvw 4 www.google.com 443") output = nc_command.run() print(output) print(type(output)) print("succeeded!" in output) ___ I tested using Python 3.8.3 and there was well, I got the result as For Python 3 - I had to use universal_newlines=True in the subprocess.popen to get the result as a string. However still I see the same result as python 2.7 One strange thing I noticed when tried using Jupyter notebook, the print(output) is not displayed, however the result are getting logged in the terminal [image: image.png] On Sun, Mar 21, 2021 at 7:30 PM Dennis Lee Bieber <wlfraed at ix.netcom.com> wrote: > On Sun, 21 Mar 2021 18:31:19 -0400, jark AJ <jarkmx at gmail.com> declaimed > the following: > > > >import subprocess > > > >class Command(object): > > def __init__(self, cmd): > > self.cmd = cmd > > > > def run(self, shell=False): > > try: > > print("Executing Command >>", self.cmd) > > output = subprocess.Popen( > > self.cmd.split(), stdout=subprocess.PIPE, shell=shell > > ) > > return output.communicate()[0] > > except subprocess.CalledProcessError as e: > > return e.returncode > > > > def __str__(self): > > return str(self.cmd) > > > > For some reason, I find creating a class, when the methods of that > class are called in sequence, and then the class instance is disposed of. > > >count = 0 > >while count <= 5: > > for _ in range(5): > > and get rid of count completely > > > > nc_command = Command("nc -zvw 4 www.google.com 443") > > output = Command.run(nc_command) > > As mentioned above, I'd likely just replace the above with the > contents > of the run method, modified to put the actual command in needed places. > > Note: since you created a class instance, normal usage would be > > output = nc_command.run() > > > count += 1 > > print(output) > > print(type(output)) > > print("succeeded!" in output) > > > >___ > > > > > >Output: > >__ > > > >('Executing Command >>', 'nc -zvw 4 www.google.com 443') > >Connection to www.google.com port 443 [tcp/https] succeeded! > > > ><type 'str'> > > Which version of Python are you running? > > >>> output = "Connection to www.google.com port 443 [tcp/https] > succeeded!\n" > >>> print(output) > Connection to www.google.com port 443 [tcp/https] succeeded! > > >>> print("succeeded!" in output) > True > >>> type(output) > <class 'str'> <<<<<< > >>> > > > -- > Wulfraed Dennis Lee Bieber AF6VN > wlfraed at ix.netcom.com > http://wlfraed.microdiversity.freeddns.org/ > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From joao.oliveira at ufob.edu.br Sun Mar 21 18:04:22 2021 From: joao.oliveira at ufob.edu.br (Joao Carlos Silva de Oliveira Matos) Date: Sun, 21 Mar 2021 19:04:22 -0300 Subject: [Tutor] Parsing dictionary to Match headers with values Message-ID: <CAN8ghrB=N5mWJJFeu-2JfRCyq3Ojn++NNifT8=TXr2MGGO0_Dg@mail.gmail.com> Hello everyone, This is my first time posting, so I apologize if I'm doing something wrong. So, I am parsing a pdf file using the "PDF READER" package. It's going really well and I'm very impressed with this library. Now, I'm trying to match the keys with their values. Here's the logic: A key can be recognized when the column "Fonte" has a value equal to F1. The values can be known when the column "Fonte" has a value equal to F2. In order to match those values, you need to look at Column X and match them by their coordinate. There are two problems. The first one is to concatenate values that have Font F2 and the same coordinate (See green arrows). The second problem is to break the loop when another F1 value is found since we don't want to get other keys. In my code, I have a dictionary, but I turned it into a data frame so Yu can see the logic with visual help. TL;DR: I want to creating a dictionary from a PDF file matching their keys with their values. But I have to break it before another dictionary comes along. [image: image.png] Here it follows a print of the dictionary: [image: image.png] -- From PythonList at DancesWithMice.info Sun Mar 21 18:40:30 2021 From: PythonList at DancesWithMice.info (dn) Date: Mon, 22 Mar 2021 11:40:30 +1300 Subject: [Tutor] string not found in variable In-Reply-To: <CAAaDumTQ5oVQTnCFsSGtxh0hcTmC11DR9EcaZ2rgJPS39Qr9Ew@mail.gmail.com> References: <CAAaDumTQ5oVQTnCFsSGtxh0hcTmC11DR9EcaZ2rgJPS39Qr9Ew@mail.gmail.com> Message-ID: <b668f74b-6852-5df4-fa42-f66b4d65ca33@DancesWithMice.info> On 22/03/2021 11.31, jark AJ wrote: > Hi All, > > There are two actions I am trying to achieve here. > > 1) To check if the connectivity exists between the source and destination > 2) To run the test five times to ensure there is no intermittent packet > loss we experience. > > looking to use the nc command here in python, On a bash shell, we do it > using > for i in {1..5}; do nc -zvw 4 www.google.com 443 ; done > > I have the following questions: > > 1) I am storing the subprocess output in a variable called output which is > of type string. It has succeeded in the output; however, when I use > print("succeeded!" in output), its returning false > 2) Is there a way we could keep appending the output like a list in the > string. trying to do the success/failure logic at the end based on the > results of all five tests. Looking to achieve this using the variable which > stores the output. > > Thank you in advance! > > ___ > > import subprocess > > class Command(object): > def __init__(self, cmd): > self.cmd = cmd > > def run(self, shell=False): > try: > print("Executing Command >>", self.cmd) > output = subprocess.Popen( > self.cmd.split(), stdout=subprocess.PIPE, shell=shell > ) > return output.communicate()[0] > except subprocess.CalledProcessError as e: > return e.returncode > > def __str__(self): > return str(self.cmd) > > count = 0 > while count <= 5: > nc_command = Command("nc -zvw 4 www.google.com 443") > output = Command.run(nc_command) > count += 1 > print(output) > print(type(output)) > print("succeeded!" in output) > > ___ > > > Output: > __ > > ('Executing Command >>', 'nc -zvw 4 www.google.com 443') > Connection to www.google.com port 443 [tcp/https] succeeded! > > <type 'str'> > False Is it type byte, cf str? -- Regards, =dn From alan.gauld at yahoo.co.uk Mon Mar 22 07:11:43 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 22 Mar 2021 11:11:43 +0000 Subject: [Tutor] string not found in variable In-Reply-To: <CAAaDumS-3a2hAK1vBeMEjVyb9=spQNVf9BpxSrBRWCWEb2CP+w@mail.gmail.com> References: <CAAaDumTQ5oVQTnCFsSGtxh0hcTmC11DR9EcaZ2rgJPS39Qr9Ew@mail.gmail.com> <p0lf5gdlm022gplat2910l4fjriosnhf2o@4ax.com> <CAAaDumS-3a2hAK1vBeMEjVyb9=spQNVf9BpxSrBRWCWEb2CP+w@mail.gmail.com> Message-ID: <s39u1f$4ai$1@ciao.gmane.io> On 21/03/2021 23:59, jark AJ wrote: > Thank you so much Dennis and dn for your response. > > I am using Python Python 2.7.16 Thanks for telling us that, it's quite important since it negates the bytes issue - the default return values in 2.7 are strings. -- 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 jarkmx at gmail.com Mon Mar 22 19:18:42 2021 From: jarkmx at gmail.com (jark AJ) Date: Mon, 22 Mar 2021 19:18:42 -0400 Subject: [Tutor] string not found in variable In-Reply-To: <q8gh5glv0gnqkah8vn5hapubn99g3f7h59@4ax.com> References: <CAAaDumTQ5oVQTnCFsSGtxh0hcTmC11DR9EcaZ2rgJPS39Qr9Ew@mail.gmail.com> <p0lf5gdlm022gplat2910l4fjriosnhf2o@4ax.com> <70of5gtkoaeo3eoe0ff2nsoa4tq97so1g9@4ax.com> <CAAaDumSwfAhQD+DU=WOL2Ta97wC1sXfPaxvkT8p05-YH2asa4w@mail.gmail.com> <q8gh5glv0gnqkah8vn5hapubn99g3f7h59@4ax.com> Message-ID: <CAAaDumTrr46Q0aK9EArm5Yz=m3Pxv2GFMgVX_Eb=OKmSZtDgmw@mail.gmail.com> Thank you so much Dennis and Alan. Yes Dennis, understand, Somehow I got the idea that using the classes would make things easier. thank you again! Couple of follow up questions 1) At what point you would have used classes in this case or what could be the use case that would decide us to choose classes over functions 2) On the package_check() function, I am calling output = get_packages() and it does not call the package_check(). tested by printing it. When I tried to do outside of the function, I was able to get the result back. Could you please let me know why ? I am planning to use the output to perform the success/failure action using python2.7 ____ import subprocess import sys def call(cmd, shell=False): """Function for the subprocess check""" try: output = subprocess.check_call(cmd.split(), shell=shell,) return output except subprocess.CalledProcessError as error: return error.returncode except subprocess.OSError as error: return error def get_packages(): """Function to list the packages required to perform the test. nc package can be present in /usr/bin or /bin/nc""" print("test was invoked") try: nc_check_1 = call("ls /usr/bin/nc") nc_check_2 = call("ls /bin/nc") nc_check = nc_check_1 and nc_check_2 return nc_check except Exception as error: sys.exit("failed due to" + str(error)) def package_check(): """Function to check the packages required to perform the test""" output = get_packages() print(output) __ On Mon, Mar 22, 2021 at 12:17 PM Dennis Lee Bieber <wlfraed at ix.netcom.com> wrote: > On Sun, 21 Mar 2021 20:55:43 -0400, jark AJ <jarkmx at gmail.com> declaimed > the following: > > > Yes, Understood, the idea behind using a class instead of a function > >is that looking to use the same class for any commands that we may need > to > >run in future. > > You are creating an instance of the class for each command -- and > only > invoking it once, then discarding it. > > Just create a function, and pass the command to the function. > > >for i in range(5): > > command = Command("nc -zvw 4 www.google.com 443") > > output, my_check = command.run() > > print(my_check) > > print(type(output)) > > print("succeeded!" in my_check) > > That creates, runs, and disposes of five instances of Command. > > Since the command itself doesn't change inside the loop, it might > be > useful to reorder some statements -- if you insist on a class, rather than > just a function... > > >command = Command("nc -zvw 4 www.google.com 443") > >for i in range(5): > > output, my_check = command.run() > > print(my_check) > > print(type(output)) > > print("succeeded!" in my_check) > > This, at least, makes repeated use of the same instance to run the > same > command. NOTE: I've not gone back to the class definition to ensure that > there is no shared state between .run() except for the command to be run. > > > -- > Wulfraed Dennis Lee Bieber AF6VN > wlfraed at ix.netcom.com > http://wlfraed.microdiversity.freeddns.org/ > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From jarkmx at gmail.com Mon Mar 22 19:41:20 2021 From: jarkmx at gmail.com (jark AJ) Date: Mon, 22 Mar 2021 19:41:20 -0400 Subject: [Tutor] string not found in variable In-Reply-To: <CAAaDumTrr46Q0aK9EArm5Yz=m3Pxv2GFMgVX_Eb=OKmSZtDgmw@mail.gmail.com> References: <CAAaDumTQ5oVQTnCFsSGtxh0hcTmC11DR9EcaZ2rgJPS39Qr9Ew@mail.gmail.com> <p0lf5gdlm022gplat2910l4fjriosnhf2o@4ax.com> <70of5gtkoaeo3eoe0ff2nsoa4tq97so1g9@4ax.com> <CAAaDumSwfAhQD+DU=WOL2Ta97wC1sXfPaxvkT8p05-YH2asa4w@mail.gmail.com> <q8gh5glv0gnqkah8vn5hapubn99g3f7h59@4ax.com> <CAAaDumTrr46Q0aK9EArm5Yz=m3Pxv2GFMgVX_Eb=OKmSZtDgmw@mail.gmail.com> Message-ID: <CAAaDumSPb3+OdY8dn+SOEit+h3mycorjLDgHhSsR5XxwLeh=cg@mail.gmail.com> Sorry. my bad, I never called the package_check() :) Please ignore On Mon, Mar 22, 2021 at 7:18 PM jark AJ <jarkmx at gmail.com> wrote: > Thank you so much Dennis and Alan. > Yes Dennis, understand, Somehow I got the idea that using the classes > would make things easier. thank you again! > Couple of follow up questions > > 1) At what point you would have used classes in this case or what could be > the use case that would decide us to choose classes over functions > 2) On the package_check() function, I am calling output = get_packages() > and it does not call the package_check(). tested by printing it. When I > tried to do outside of the function, I was able to get the result back. > Could you please let me know why ? I am planning to use the output to > perform the success/failure action > > > using python2.7 > > ____ > > import subprocess > import sys > > def call(cmd, shell=False): > """Function for the subprocess check""" > > try: > output = subprocess.check_call(cmd.split(), shell=shell,) > return output > except subprocess.CalledProcessError as error: > return error.returncode > except subprocess.OSError as error: > return error > > > def get_packages(): > """Function to list the packages required to perform the test. > nc package can be present in /usr/bin or /bin/nc""" > > print("test was invoked") > try: > nc_check_1 = call("ls /usr/bin/nc") > nc_check_2 = call("ls /bin/nc") > nc_check = nc_check_1 and nc_check_2 > return nc_check > except Exception as error: > sys.exit("failed due to" + str(error)) > > > def package_check(): > """Function to check the packages required to perform the test""" > output = get_packages() > print(output) > __ > > > On Mon, Mar 22, 2021 at 12:17 PM Dennis Lee Bieber <wlfraed at ix.netcom.com> > wrote: > >> On Sun, 21 Mar 2021 20:55:43 -0400, jark AJ <jarkmx at gmail.com> declaimed >> the following: >> >> > Yes, Understood, the idea behind using a class instead of a >> function >> >is that looking to use the same class for any commands that we may need >> to >> >run in future. >> >> You are creating an instance of the class for each command -- and >> only >> invoking it once, then discarding it. >> >> Just create a function, and pass the command to the function. >> >> >for i in range(5): >> > command = Command("nc -zvw 4 www.google.com 443") >> > output, my_check = command.run() >> > print(my_check) >> > print(type(output)) >> > print("succeeded!" in my_check) >> >> That creates, runs, and disposes of five instances of Command. >> >> Since the command itself doesn't change inside the loop, it might >> be >> useful to reorder some statements -- if you insist on a class, rather than >> just a function... >> >> >command = Command("nc -zvw 4 www.google.com 443") >> >for i in range(5): >> > output, my_check = command.run() >> > print(my_check) >> > print(type(output)) >> > print("succeeded!" in my_check) >> >> This, at least, makes repeated use of the same instance to run >> the same >> command. NOTE: I've not gone back to the class definition to ensure that >> there is no shared state between .run() except for the command to be run. >> >> >> -- >> Wulfraed Dennis Lee Bieber AF6VN >> wlfraed at ix.netcom.com >> http://wlfraed.microdiversity.freeddns.org/ >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> https://mail.python.org/mailman/listinfo/tutor >> > From roel at roelschroeven.net Mon Mar 22 14:58:54 2021 From: roel at roelschroeven.net (Roel Schroeven) Date: Mon, 22 Mar 2021 19:58:54 +0100 Subject: [Tutor] Fwd: Graph question was: Re: (no subject) In-Reply-To: <s2oueq$238$1@ciao.gmane.io> References: <CAHnm4MFAUbXkqDCFkP-mDk0aAurkAMJ30nCcHtfFTCbBoueWAA@mail.gmail.com> <97959d16-764a-f64e-051c-fceeb44539a3@gmail.com> <s2oueq$238$1@ciao.gmane.io> Message-ID: <s3apdf$kgp$1@ciao.gmane.io> Alan Gauld via Tutor schreef op 16/03/2021 om 1:34: > On 15/03/2021 23:56, Alan Gauld wrote: > > I assume this should be written: > > 3 > 4 > 0,1 > 1,2 > 2,0 > 0,2 > 2 > > ie 3 nodes, 4 edges and the mystery node is 2. That is indeed almost certainly the case. IIRC problems on HackerRank, and possibly other sites like it, use an input format like that. > The solution is apparently 0,1 but I'm not sure why? I think I understand it. We have a set of directed edges i -> j. Then we are given a node k, find all edges that go towards that node k, and finally enumerate all starting nodes of those edges. In the example, we have 4 edges: - 0 -> 1 - 1 -> 2 - 2 -> 0 - 0 -> 2 We are asked to find all edges that end in 2 (because k is given as 2). Those are: - 1 -> 2 - 0 -> 2 The starting nodes therefore are 1 and 0 (or 0 and 1; it looks like the order doesn't matter). Or in code: [s for (s, j) in edges if j == k] -- "Honest criticism is hard to take, particularly from a relative, a friend, an acquaintance, or a stranger." -- Franklin P. Jones Roel Schroeven From roel at roelschroeven.net Mon Mar 22 15:04:29 2021 From: roel at roelschroeven.net (Roel Schroeven) Date: Mon, 22 Mar 2021 20:04:29 +0100 Subject: [Tutor] How to write a loop in python to find HTML tags in a text file In-Reply-To: <tg495g9dmdidbdvifdm8flqcu5ondj2rhk@4ax.com> References: <CAKXx11y2wW9k83zotgroThrROhJeqnkbTfjv2NQ0KPY=bMQ_HA@mail.gmail.com> <s2sm0m$11v5$1@ciao.gmane.io> <CAKXx11w+UBpyrpXyWeLJDpE9C44tQM5LVt0nSvSm1PCXSWQjuQ@mail.gmail.com> <s2th7v$p4t$1@ciao.gmane.io> <CAPStRW-2x5rZ1guFiv+izW6MPBpBAB0yot0aEKu7O+DA5Nh6iA@mail.gmail.com> <tg495g9dmdidbdvifdm8flqcu5ondj2rhk@4ax.com> Message-ID: <s3apnu$5fb$1@ciao.gmane.io> Dennis Lee Bieber schreef op 19/03/2021 om 13:07: > On Thu, 18 Mar 2021 11:11:36 -0400, Adam Eyring <adameyring at gmail.com> > declaimed the following: > >> I don't work with Beautiful Soup, but this chapter in the free PDF book > > Heh! You must have a different definition of "free"... > > https://automatetheboringstuff.com/ > """ > Purchase directly from the publisher to get free PDF, Kindle, and epub > ebook copies. > """ > https://nostarch.com/automatestuff2 > """ > Print Book and FREE Ebook, $39.95 > Ebook (PDF, Mobi, and ePub), $31.95 > """ > > The printed book and ebooks in PDF/Mobi/ePub are not free (well, if you buy the printed book, you get the ebook for free); but the online HTML version is free to read (scroll down to "Table of Contents"). -- "Honest criticism is hard to take, particularly from a relative, a friend, an acquaintance, or a stranger." -- Franklin P. Jones Roel Schroeven From alan.gauld at yahoo.co.uk Mon Mar 22 21:20:27 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 23 Mar 2021 01:20:27 +0000 Subject: [Tutor] string not found in variable In-Reply-To: <CAAaDumTrr46Q0aK9EArm5Yz=m3Pxv2GFMgVX_Eb=OKmSZtDgmw@mail.gmail.com> References: <CAAaDumTQ5oVQTnCFsSGtxh0hcTmC11DR9EcaZ2rgJPS39Qr9Ew@mail.gmail.com> <p0lf5gdlm022gplat2910l4fjriosnhf2o@4ax.com> <70of5gtkoaeo3eoe0ff2nsoa4tq97so1g9@4ax.com> <CAAaDumSwfAhQD+DU=WOL2Ta97wC1sXfPaxvkT8p05-YH2asa4w@mail.gmail.com> <q8gh5glv0gnqkah8vn5hapubn99g3f7h59@4ax.com> <CAAaDumTrr46Q0aK9EArm5Yz=m3Pxv2GFMgVX_Eb=OKmSZtDgmw@mail.gmail.com> Message-ID: <s3bfor$k16$1@ciao.gmane.io> On 22/03/2021 23:18, jark AJ wrote: > 1) At what point you would have used classes in this case or what could be > the use case that would decide us to choose classes over functions A function is a single operation. It performs a task. It takes in data and returns a result. It does not have state(generator functions being a notable exception!) So if you are thinking of a class to perform a single task you probably want a function instead. Classes represent objects. As such they have *attributes* that manage the *internal state*. They also have a set of *operations* by which the outside world communicates with the objects (and indeed by which the *objects can communicate* with each other). So if, in your solution, you have some state data that needs to have various things done to it, especially if those things are done over time, then a class is appropriate. Also, if you need several instances of something represented by a common group of data attributes then you probably want a class (If there are no operations then maybe you just need a dictionary plus factory function instead) To repeat the aphorism: "If you have a class with a single method plus init() then you really wanted a function." There is a lot of programming overhead in creating a class, and a lot of runtime overhead in instantiating a class and calling its methods (compared to calling a function). Don't create classes needlessly. Classes do not necessarily imply better code. When they are appropriate they are very useful, but only when appropriate. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From PyTutor at DancesWithMice.info Mon Mar 22 22:48:16 2021 From: PyTutor at DancesWithMice.info (dn) Date: Tue, 23 Mar 2021 15:48:16 +1300 Subject: [Tutor] string not found in variable In-Reply-To: <s3bfor$k16$1@ciao.gmane.io> References: <CAAaDumTQ5oVQTnCFsSGtxh0hcTmC11DR9EcaZ2rgJPS39Qr9Ew@mail.gmail.com> <p0lf5gdlm022gplat2910l4fjriosnhf2o@4ax.com> <70of5gtkoaeo3eoe0ff2nsoa4tq97so1g9@4ax.com> <CAAaDumSwfAhQD+DU=WOL2Ta97wC1sXfPaxvkT8p05-YH2asa4w@mail.gmail.com> <q8gh5glv0gnqkah8vn5hapubn99g3f7h59@4ax.com> <CAAaDumTrr46Q0aK9EArm5Yz=m3Pxv2GFMgVX_Eb=OKmSZtDgmw@mail.gmail.com> <s3bfor$k16$1@ciao.gmane.io> Message-ID: <0b265ffe-62b8-c6f5-3fe5-059632dde8fb@DancesWithMice.info> If I suffered from any higher-level of paranoia I'd be sure that @Alan was 'pressing my buttons'... On 23/03/2021 14.20, Alan Gauld via Tutor wrote: > On 22/03/2021 23:18, jark AJ wrote: > >> 1) At what point you would have used classes in this case or what could be >> the use case that would decide us to choose classes over functions > > A function is a single operation. It performs a task. > It takes in data and returns a result. It does not have > state(generator functions being a notable exception!) > So if you are thinking of a class to perform a single > task you probably want a function instead. > > Classes represent objects. As such they have *attributes* > that manage the *internal state*. They also have a set > of *operations* by which the outside world communicates > with the objects (and indeed by which the *objects can > communicate* with each other). > > So if, in your solution, you have some state data that > needs to have various things done to it, especially if > those things are done over time, then a class is appropriate. > > Also, if you need several instances of something > represented by a common group of data attributes > then you probably want a class (If there are no > operations then maybe you just need a dictionary plus > factory function instead) > > To repeat the aphorism: > "If you have a class with a single method plus init() > then you really wanted a function." > > There is a lot of programming overhead in creating a > class, and a lot of runtime overhead in instantiating > a class and calling its methods (compared to calling > a function). Don't create classes needlessly. Classes > do not necessarily imply better code. When they are > appropriate they are very useful, but only when > appropriate. I agree with the aphorism, and @wlfraed's comment (earlier in thread) that creating a class only to call its methods one line after another and/or only once, should be viewed with suspicion! In this application, the loop is to hammer on the door of the one web site, five times, apparently in a bid to detect packet-loss. Accordingly, the instantiation (inclusive of the URL) is required only once. Also, please observe that the post-processing of any feedback is simplistic (the OP may have edited to minimal-code for posting). Thus, apart from one aspect, which will be discussed in a moment, a function is likely 'cleaner', and almost certainly more-efficient! When I construct a similar facility, over and over, in multiple applications, two things happen: - I become bored/lazy, ie not this again! and - my old-age/advanced-decrepitude/failing-memory means that I forget 'stuff'. Accordingly, I do have a class which acts as a 'wrapper' around the 'common bits that I use' of the (perfectly reasonable) subprocess library. Because this custom-module has been constructed and documented to my satisfaction (?peculiarities), the second issue has been addressed with a bunch of reminders in the form of 'give me a BASH command here' or 'what's the URL?' parameters, or outright docstring 'reminders', ie tell me again: why do I need to do this. Secondly, or rather primarily, I sat down and worked-out where errors might occur, what the options are for gathering feedback, etc. Thus, whacking a new application's command through the class means that I 'inherit' all that research and checking, without having to remember it (and without risk of 'forgetting' to program[me] defensively) this time and every time. (one of my aphorisms: you have to work hard to be this lazy!) Accordingly, let's consider a similar application that's a little more challenging. How about testing all of my [clients'] web sites dynamically (ie using similar code to the OP, rather than scanning logs, etc)? In this case, we could start with such entities/resources as the class and a list containing the URLs. We instantiate the class during the application's initialisation phase, and the central loop runs 'down' the list. Inside the loop, the .run() call passes the applicable-URL as an argument to instance.run(), and thus the popen() is performed. In this scenario the 'cost' of the class-init is spread across the however-many URLs populating the list, and tends towards irrelevant. In other applications, the post-processing might be considerably more involved than the OP's needs. Which is why I might even (really make @Alan grumble by) sub-class. Given that one application's post-processing is likely quite different to another's, creating a purpose-built sub-class enables both a customisation of the post-processing, eg doing 'something' to a retrieved web-page or an opened file; and keeping all this related functionality together. In this thinking, keeping all the data used to construct the popen() call, together with all the feedback, is not "internal state" per-se. However, it is a means of keeping (like) things together. Thus the rationale of "class" or "module" as a "namespace" is extended into the same ideas as keeping all your fruit in a fruit-bowl; and all your pens, pencils, etc, in a pencil-case/drawer/pen-holder/desk-organiser. NB this 'bowl of data and functionality' is not meant to be, and is by no means an impenetrable barrier - we can still refer to instance.URL or instance.BASH_command from the 'mainline' code. My habit is to 'encapsulate' all I/O - which may mean using a custom-function or a class (and yes dealing with subprocesses is classified as a form of I/O for the purposes of this discussion). The rationale for such is (a) modularity, and (b) that when it comes to debugging/testing, substitution is easy - if you keeping banging-away on Google's front door, or somebody's API, their IDS or other defences may decide against you! (and you don't want that!); similarly, replacing some long/expensive process with a 'mock'... @Alan knows I'm a classy-guy, even though is yet to admit that I am (also) better-looking - and we'll debate a point if only to illustrate that there are often 'choices'... So, although I claim to favor classes, there are many situations where a function is the better choice, no argument. Sometimes a class offers an advantage (or two). Possibly the same advantage might be obtained from judicious use of modules... It's definitely worth understanding each of the points made in this thread's posts. The one that works for you today is the 'right' one - and maybe a different choice will be 'right' tomorrow... PS +1 on the major differences between Python 2.n and 3, and our need/the relevance of being told such constraints! -- Regards, =dn From singh000taran at gmail.com Wed Mar 24 14:39:27 2021 From: singh000taran at gmail.com (=?UTF-8?B?4Kmn?=) Date: Thu, 25 Mar 2021 00:09:27 +0530 Subject: [Tutor] (no subject) Message-ID: <CAHnm4MERT2qWppxOOmF7U9C0U8H92e_Mud=V2N-tohGthD4sJg@mail.gmail.com> You've saved the password to access your Bitcoins on your personal computer. You have created an application that encrypts the password. To decrypt, you need to perform an operation between two integers, say A and B, as described below - 1) Let X and Y be the numbers obtained by REVERSING A and B. For example, the reverse of 123 is 321, and the reverse of 320 is 23 (leading zeroes are discarded) 2) If both X and Y are PRIME, then the answer is X + Y 3) If EXACTLY ONE of X and Y is PRIME, then the answer is A + B 4) Otherwise, the answer is A * B Your task is to write a program to help you perform this operation. Input: Two COMMA-separated integers A and B. There can be any number of white-spaces in the input. Output : A single integer that is the result of applying the operation described above on the input integers. Constraints: 1?A,B?100001?A,B?10000 Example: Input: 142 ,123 Output: 265 Explanation : The reverse of 142 is 241 (prime) and that of 123 is 321 (not prime). Hence the answer is A + B = 142 + 123 = 265 IMPORTANT : Make sure your code is modular i.e. you REUSE as much of the code as possible USING FUNCTIONS. You may LOSE MARKS if this guideline is not followed. My code is not working. How to solve it A, B = int(input()) rev1 = A[::-1] rev2 = B[::-1] X = rev1 Y = rev2 qw = min(X, Y) for i in range(1, qw+1): if X&i == 0 and Y%i == 0: hcf = i if hcf == 1: print(X+Y) elif: for j in range(2, X): if X%j == 0 or Y%j == 0: print(A+B) else: print(A*B) From jarkmx at gmail.com Wed Mar 24 20:25:01 2021 From: jarkmx at gmail.com (jark AJ) Date: Wed, 24 Mar 2021 20:25:01 -0400 Subject: [Tutor] string not found in variable In-Reply-To: <0b265ffe-62b8-c6f5-3fe5-059632dde8fb@DancesWithMice.info> References: <CAAaDumTQ5oVQTnCFsSGtxh0hcTmC11DR9EcaZ2rgJPS39Qr9Ew@mail.gmail.com> <p0lf5gdlm022gplat2910l4fjriosnhf2o@4ax.com> <70of5gtkoaeo3eoe0ff2nsoa4tq97so1g9@4ax.com> <CAAaDumSwfAhQD+DU=WOL2Ta97wC1sXfPaxvkT8p05-YH2asa4w@mail.gmail.com> <q8gh5glv0gnqkah8vn5hapubn99g3f7h59@4ax.com> <CAAaDumTrr46Q0aK9EArm5Yz=m3Pxv2GFMgVX_Eb=OKmSZtDgmw@mail.gmail.com> <s3bfor$k16$1@ciao.gmane.io> <0b265ffe-62b8-c6f5-3fe5-059632dde8fb@DancesWithMice.info> Message-ID: <CAAaDumSPfdEQ3FBuzTOd4nA8eqY-QKDn43grbGusasxv+8+TGg@mail.gmail.com> Thank you so much, Alan and dn for the detailed explanation.! Yes, I did not use the classes for this use case, and I think I somewhat made it complicated at the beginning by trying to use class :) Thank you once again to all of you!! On Mon, Mar 22, 2021 at 10:49 PM dn via Tutor <tutor at python.org> wrote: > If I suffered from any higher-level of paranoia I'd be sure that @Alan > was 'pressing my buttons'... > > > On 23/03/2021 14.20, Alan Gauld via Tutor wrote: > > On 22/03/2021 23:18, jark AJ wrote: > > > >> 1) At what point you would have used classes in this case or what could > be > >> the use case that would decide us to choose classes over functions > > > > A function is a single operation. It performs a task. > > It takes in data and returns a result. It does not have > > state(generator functions being a notable exception!) > > So if you are thinking of a class to perform a single > > task you probably want a function instead. > > > > Classes represent objects. As such they have *attributes* > > that manage the *internal state*. They also have a set > > of *operations* by which the outside world communicates > > with the objects (and indeed by which the *objects can > > communicate* with each other). > > > > So if, in your solution, you have some state data that > > needs to have various things done to it, especially if > > those things are done over time, then a class is appropriate. > > > > Also, if you need several instances of something > > represented by a common group of data attributes > > then you probably want a class (If there are no > > operations then maybe you just need a dictionary plus > > factory function instead) > > > > To repeat the aphorism: > > "If you have a class with a single method plus init() > > then you really wanted a function." > > > > There is a lot of programming overhead in creating a > > class, and a lot of runtime overhead in instantiating > > a class and calling its methods (compared to calling > > a function). Don't create classes needlessly. Classes > > do not necessarily imply better code. When they are > > appropriate they are very useful, but only when > > appropriate. > > > I agree with the aphorism, and @wlfraed's comment (earlier in thread) > that creating a class only to call its methods one line after another > and/or only once, should be viewed with suspicion! > > > In this application, the loop is to hammer on the door of the one web > site, five times, apparently in a bid to detect packet-loss. > Accordingly, the instantiation (inclusive of the URL) is required only > once. > > Also, please observe that the post-processing of any feedback is > simplistic (the OP may have edited to minimal-code for posting). > > Thus, apart from one aspect, which will be discussed in a moment, a > function is likely 'cleaner', and almost certainly more-efficient! > > > When I construct a similar facility, over and over, in multiple > applications, two things happen: > > - I become bored/lazy, ie not this again! and > - my old-age/advanced-decrepitude/failing-memory means that I forget > 'stuff'. > > > Accordingly, I do have a class which acts as a 'wrapper' around the > 'common bits that I use' of the (perfectly reasonable) subprocess > library. Because this custom-module has been constructed and documented > to my satisfaction (?peculiarities), the second issue has been addressed > with a bunch of reminders in the form of 'give me a BASH command here' > or 'what's the URL?' parameters, or outright docstring 'reminders', ie > tell me again: why do I need to do this. > > Secondly, or rather primarily, I sat down and worked-out where errors > might occur, what the options are for gathering feedback, etc. Thus, > whacking a new application's command through the class means that I > 'inherit' all that research and checking, without having to remember it > (and without risk of 'forgetting' to program[me] defensively) this time > and every time. > (one of my aphorisms: you have to work hard to be this lazy!) > > > Accordingly, let's consider a similar application that's a little more > challenging. How about testing all of my [clients'] web sites > dynamically (ie using similar code to the OP, rather than scanning logs, > etc)? > > In this case, we could start with such entities/resources as the class > and a list containing the URLs. > > We instantiate the class during the application's initialisation phase, > and the central loop runs 'down' the list. > > Inside the loop, the .run() call passes the applicable-URL as an > argument to instance.run(), and thus the popen() is performed. > > In this scenario the 'cost' of the class-init is spread across the > however-many URLs populating the list, and tends towards irrelevant. > > > In other applications, the post-processing might be considerably more > involved than the OP's needs. Which is why I might even (really make > @Alan grumble by) sub-class. Given that one application's > post-processing is likely quite different to another's, creating a > purpose-built sub-class enables both a customisation of the > post-processing, eg doing 'something' to a retrieved web-page or an > opened file; and keeping all this related functionality together. > > In this thinking, keeping all the data used to construct the popen() > call, together with all the feedback, is not "internal state" per-se. > However, it is a means of keeping (like) things together. Thus the > rationale of "class" or "module" as a "namespace" is extended into the > same ideas as keeping all your fruit in a fruit-bowl; and all your pens, > pencils, etc, in a pencil-case/drawer/pen-holder/desk-organiser. NB this > 'bowl of data and functionality' is not meant to be, and is by no means > an impenetrable barrier - we can still refer to instance.URL or > instance.BASH_command from the 'mainline' code. > > > My habit is to 'encapsulate' all I/O - which may mean using a > custom-function or a class (and yes dealing with subprocesses is > classified as a form of I/O for the purposes of this discussion). The > rationale for such is (a) modularity, and (b) that when it comes to > debugging/testing, substitution is easy - if you keeping banging-away on > Google's front door, or somebody's API, their IDS or other defences may > decide against you! (and you don't want that!); similarly, replacing > some long/expensive process with a 'mock'... > > > @Alan knows I'm a classy-guy, even though is yet to admit that I am > (also) better-looking - and we'll debate a point if only to illustrate > that there are often 'choices'... > > So, although I claim to favor classes, there are many situations where a > function is the better choice, no argument. Sometimes a class offers an > advantage (or two). Possibly the same advantage might be obtained from > judicious use of modules... > > It's definitely worth understanding each of the points made in this > thread's posts. The one that works for you today is the 'right' one - > and maybe a different choice will be 'right' tomorrow... > > > PS +1 on the major differences between Python 2.n and 3, and our > need/the relevance of being told such constraints! > -- > Regards, > =dn > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From alan.gauld at yahoo.co.uk Wed Mar 24 20:54:47 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 25 Mar 2021 00:54:47 +0000 Subject: [Tutor] (no subject) In-Reply-To: <CAHnm4MERT2qWppxOOmF7U9C0U8H92e_Mud=V2N-tohGthD4sJg@mail.gmail.com> References: <CAHnm4MERT2qWppxOOmF7U9C0U8H92e_Mud=V2N-tohGthD4sJg@mail.gmail.com> Message-ID: <s3gn0n$113b$1@ciao.gmane.io> On 24/03/2021 18:39, ? wrote: This is obviously homework of some sort so we wont solve it for you but we can provide hints... First, let's see if we can express this more clearly: > You have created an application that encrypts the password. To > decrypt, you need to perform an operation between two integers, say A and > B, as described below - > 1) Let X and Y be the numbers obtained by REVERSING A and B. > (leading zeroes are discarded)> 2) If both X and Y are PRIME, then the answer is X + Y > 3) If EXACTLY ONE of X and Y is PRIME, then the answer is A + B > 4) Otherwise, the answer is A * B > Your task is to write a program to help you perform this operation. > Input: Two COMMA-separated integers A and > B. There can be any number of white-spaces in the input. > Output : A single integer that is the result of applying the operation > described above on the input integers. > Constraints: 1?A, B?100001?A, B?10000 > Example: Input: 142,123 Output: 265 > Explanation : The reverse of 142 is 241 (prime) and that > of 123 is 321 (not prime). Hence the answer is A + B = 142 + 123 = 265 > My code is not working. How to solve it > > A, B = int(input()) This does not work because input() returns a string and int() converts that to a number. A single number. You are expecting a string like 142, 123 You need to split it and strip it of white space before converting to integers. It's good to try things out in the interpreter before putting them in code: >>> A,B = int(input('?> ')) ?> 142, 123 Traceback (most recent call last): File "<pyshell#0>", line 1, in <module> A,B = int(input('?> ')) ValueError: invalid literal for int() with base 10: '142, 123' >>> So it doesn't work. Do you know how to split a string? Do you know how to strip whitespace? Maybe you could write a function that reads a string, splits and strips it and reverses the characters. > rev1 = A[::-1] > rev2 = B[::-1] If your code above had succeeded you would be trying to reverse integers. That wouldn't work either. You need to keep the string form until after you reverse them. Then convert to int before testing for prime. > X = rev1 > Y = rev2 This is pointless. Just assign X and Y instead of rev1,rev2. > qw = min(X, Y) > for i in range(1, qw+1): > if X&i == 0 and Y%i == 0: Do you really mean to use bitwise AND(&) in that first test? > hcf = i and is the indentation correct here? > if hcf == 1: > print(X+Y) > > elif: > for j in range(2, X): > if X%j == 0 or Y%j == 0: > print(A+B) > > else: > print(A*B) Maybe you should write a function that tests if a number is prime? def is_prime(n): # returns True or false You can then test that in the interpreter to be sure it works before copying it into your code. The interpreter is a powerful tool, be sure to use it. Then you can use the function to apply the rules you were given rather than trying to mix the prime test and rules test into a single set of code. That might also avoid you losing marks as mentioned in the 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 PyTutor at DancesWithMice.info Wed Mar 24 21:22:11 2021 From: PyTutor at DancesWithMice.info (dn) Date: Thu, 25 Mar 2021 14:22:11 +1300 Subject: [Tutor] (no subject) In-Reply-To: <s3gn0n$113b$1@ciao.gmane.io> References: <CAHnm4MERT2qWppxOOmF7U9C0U8H92e_Mud=V2N-tohGthD4sJg@mail.gmail.com> <s3gn0n$113b$1@ciao.gmane.io> Message-ID: <13f4bfb0-6da4-081f-3776-58d9b5387cba@DancesWithMice.info> On 25/03/2021 13.54, Alan Gauld via Tutor wrote: > On 24/03/2021 18:39, ? wrote: > > This is obviously homework of some sort so we wont solve it > for you but we can provide hints... and response from you afterwards... > First, let's see if we can express this more clearly: > >> You have created an application that encrypts the password. To >> decrypt, you need to perform an operation between two integers, say A and >> B, as described below - >> 1) Let X and Y be the numbers obtained by REVERSING A and B. >> (leading zeroes are discarded)> 2) If both X and Y are PRIME, then the answer is X + Y >> 3) If EXACTLY ONE of X and Y is PRIME, then the answer is A + B >> 4) Otherwise, the answer is A * B > >> Your task is to write a program to help you perform this operation. Because options 1 thru/through 4 are mutually-exclusive, I foresee an (improved) if...else...elif 'ladder' in your future! For the same reason (@Alan +1), 'lazy execution' is to be recommended, ie that the prime? calculation only be considered if/when it becomes relevant (and no sooner). Additionally, (maybe) the order of execution is not necessarily the best sequence of coding tasks, as ... >> Input: Two COMMA-separated integers A and >> B. There can be any number of white-spaces in the input. >> Output : A single integer that is the result of applying the operation >> described above on the input integers. >> Constraints: 1?A, B?100001?A, B?10000 >> Example: Input: 142,123 > Output: 265 >> Explanation : The reverse of 142 is 241 (prime) and that >> of 123 is 321 (not prime). Hence the answer is A + B = 142 + 123 = 265 >> My code is not working. How to solve it >> >> A, B = int(input()) > > This does not work because input() returns a string and int() converts > that to a number. A single number. > > You are expecting a string like > > 142, 123 > > You need to split it and strip it of white space before converting > to integers. > > It's good to try things out in the interpreter before putting > them in code: > >>>> A,B = int(input('?> ')) > ?> 142, 123 > Traceback (most recent call last): > File "<pyshell#0>", line 1, in <module> > A,B = int(input('?> ')) > ValueError: invalid literal for int() with base 10: '142, 123' >>>> > > So it doesn't work. Do you know how to split a string? > Do you know how to strip whitespace? +1 on the 'use the interpreter' advice. and I thought +1 on the two string functions advised. However, taking the 'interpreter' advice myself, revealed/reminded that it is not necessary to strip white-space before passing a string to int(). >>> int( '23' ) 23 >>> int( ' 23 ' ) 23 >>> int( '023 ' ) 23 That said, remembering that condition (1) requires strings not integers, combined with the 'lazy execution' advice, means that the int() is only relevant should the logic call for consideration of condition (2)! > Maybe you could write a function that reads a string, > splits and strips it and reverses the characters. > >> rev1 = A[::-1] >> rev2 = B[::-1] > > If your code above had succeeded you would be trying to > reverse integers. That wouldn't work either. You need > to keep the string form until after you reverse them. > Then convert to int before testing for prime. > >> X = rev1 >> Y = rev2 > > This is pointless. Just assign X and Y instead of rev1,rev2. > >> qw = min(X, Y) >> for i in range(1, qw+1): >> if X&i == 0 and Y%i == 0: > > Do you really mean to use bitwise AND(&) in that first test? > >> hcf = i > > and is the indentation correct here? > >> if hcf == 1: >> print(X+Y) >> >> elif: >> for j in range(2, X): >> if X%j == 0 or Y%j == 0: >> print(A+B) >> >> else: >> print(A*B) > > Maybe you should write a function that tests if > a number is prime? > > def is_prime(n): > # returns True or false > > You can then test that in the interpreter to > be sure it works before copying it into your code. > The interpreter is a powerful tool, be sure to use it. > > Then you can use the function to apply the rules you > were given rather than trying to mix the prime test > and rules test into a single set of code. That might > also avoid you losing marks as mentioned in the > assignment. All excellent advice! Consider each of the steps involved and coding (and testing) a separate function for each, ie each function executes a single task. Once the sub-problems have all been solved, then it becomes a matter of writing code which calls them, if/as necessary - like making a pipeline or constructing a building one floor/tier at a time. -- Regards, =dn From phillor9 at gmail.com Thu Mar 25 01:29:02 2021 From: phillor9 at gmail.com (Phil) Date: Thu, 25 Mar 2021 16:29:02 +1100 Subject: [Tutor] Printing without a line feed Message-ID: <aefe872e-476d-c743-adf3-943c3b293cde@gmail.com> Thank you for reading this. I want to print a range of numbers with a space between them but without a space at the end. For instance: ??? for i in range(6): ??????? print(i, end=' ') How do I do this without the end space? I've spent hours on this even though I've managed to do what I want not long ago but I cannot remember how I did it. It's very frustrating. -- Regards, Phil From PyTutor at DancesWithMice.info Thu Mar 25 02:35:33 2021 From: PyTutor at DancesWithMice.info (dn) Date: Thu, 25 Mar 2021 19:35:33 +1300 Subject: [Tutor] Printing without a line feed In-Reply-To: <aefe872e-476d-c743-adf3-943c3b293cde@gmail.com> References: <aefe872e-476d-c743-adf3-943c3b293cde@gmail.com> Message-ID: <9eefef95-731a-f75f-2f57-6d67ecafb5b4@DancesWithMice.info> On 25/03/2021 18.29, Phil wrote: > Thank you for reading this. > > I want to print a range of numbers with a space between them but without > a space at the end. For instance: > > ??? for i in range(6): > ??????? print(i, end=' ') > > How do I do this without the end space? I've spent hours on this even > though I've managed to do what I want not long ago but I cannot remember > how I did it. It's very frustrating. end= and sep= https://docs.python.org/3/library/functions.html#print NB if there is an end=nullstring within the loop, the cursor remains at the end of the 'printed' line. Thus, afterwards, another print() will 'end' the line! Alternately, use the loop to build a string or a list, which can then be join-ed with space(s) - and print after the loop terminates. -- Regards, =dn From krassen at fmi.uni-sofia.bg Thu Mar 25 03:43:35 2021 From: krassen at fmi.uni-sofia.bg (Krassen Stefanov) Date: Thu, 25 Mar 2021 09:43:35 +0200 Subject: [Tutor] Printing without a line feed In-Reply-To: <9eefef95-731a-f75f-2f57-6d67ecafb5b4@DancesWithMice.info> References: <aefe872e-476d-c743-adf3-943c3b293cde@gmail.com> <9eefef95-731a-f75f-2f57-6d67ecafb5b4@DancesWithMice.info> Message-ID: <b87606ed-d446-7835-0b43-b3de8aefdfe0@fmi.uni-sofia.bg> You can use the loop from your example to print all the numbers but the last one, and then in one final print with defaults to print the last number only. This should do the job. Best, Krassen On 25.3.2021 ?. 8:35, dn via Tutor wrote: > > On 25/03/2021 18.29, Phil wrote: >> Thank you for reading this. >> >> I want to print a range of numbers with a space between them but without >> a space at the end. For instance: >> >> ??? for i in range(6): >> ??????? print(i, end=' ') >> >> How do I do this without the end space? I've spent hours on this even >> though I've managed to do what I want not long ago but I cannot remember >> how I did it. It's very frustrating. > end= and sep= > https://docs.python.org/3/library/functions.html#print > > NB if there is an end=nullstring within the loop, the cursor remains at > the end of the 'printed' line. > Thus, afterwards, another print() will 'end' the line! > > Alternately, use the loop to build a string or a list, which can then be > join-ed with space(s) - and print after the loop terminates. From ming at pgp.cool Thu Mar 25 03:06:45 2021 From: ming at pgp.cool (Ming) Date: Thu, 25 Mar 2021 15:06:45 +0800 Subject: [Tutor] Printing without a line feed In-Reply-To: <aefe872e-476d-c743-adf3-943c3b293cde@gmail.com> References: <aefe872e-476d-c743-adf3-943c3b293cde@gmail.com> Message-ID: <20210325070645.GA15@pgp.cool> On Thu, Mar 25, 2021 at 04:29:02PM +1100, Phil wrote: > Thank you for reading this. > > I want to print a range of numbers with a space between them but without a > space at the end. For instance: > > ??? for i in range(6): > ??????? print(i, end=' ') > > How do I do this without the end space? I've spent hours on this even though > I've managed to do what I want not long ago but I cannot remember how I did > it. It's very frustrating. Hello, I don?t think you need to print each number separately. Try to use str.join() to join them into a string, and then print it all at once. For example: a = list(range(6)) # a == [0, 1, 2, 3, 4, 5] s = " ".join(map(str,a)) # s == '0 1 2 3 4 5' print(s) Note that join can only operate on lists where all elements are strings, so map(str, list) is used here to convert all elements into strings. -- OpenPGP fingerprint: 3C47 5977 4819 267E DD64 C7E4 6332 5675 A739 C74E From narasimha928 at gmail.com Thu Mar 25 02:29:52 2021 From: narasimha928 at gmail.com (Narasimharao Nelluri) Date: Wed, 24 Mar 2021 23:29:52 -0700 Subject: [Tutor] Printing without a line feed In-Reply-To: <aefe872e-476d-c743-adf3-943c3b293cde@gmail.com> References: <aefe872e-476d-c743-adf3-943c3b293cde@gmail.com> Message-ID: <CAB1e+aMsd_-tFEMhrN=ukSoKTstQUrmv_ymnbzhMWNJVHseASQ@mail.gmail.com> Hi Phil, Let me does it help below code. >>> for i in range(6): ...: print(i,end=" ") ...: else: ...: print() 0 1 2 3 4 5 Thanks Narasimha On Wed, Mar 24, 2021 at 10:29 PM Phil <phillor9 at gmail.com> wrote: > Thank you for reading this. > > I want to print a range of numbers with a space between them but without > a space at the end. For instance: > > for i in range(6): > print(i, end=' ') > > How do I do this without the end space? I've spent hours on this even > though I've managed to do what I want not long ago but I cannot remember > how I did it. It's very frustrating. > > -- > Regards, > Phil > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From __peter__ at web.de Thu Mar 25 05:19:44 2021 From: __peter__ at web.de (Peter Otten) Date: Thu, 25 Mar 2021 10:19:44 +0100 Subject: [Tutor] Printing without a line feed In-Reply-To: <aefe872e-476d-c743-adf3-943c3b293cde@gmail.com> References: <aefe872e-476d-c743-adf3-943c3b293cde@gmail.com> Message-ID: <fe7eb5c3-a746-064d-7b64-642ee5cff681@web.de> On 25/03/2021 06:29, Phil wrote: > Thank you for reading this. > > I want to print a range of numbers with a space between them but without > a space at the end. For instance: > > ??? for i in range(6): > ??????? print(i, end=' ') > > How do I do this without the end space? I've spent hours on this even > though I've managed to do what I want not long ago but I cannot remember > how I did it. It's very frustrating. You can convert the numbers to a single string with >>> print(" ".join(str(i) for i in range(6))) 0 1 2 3 4 5 The argument of "".join() is called "generator expression". Or you can use the star operator: >>> print(*range(6)) 0 1 2 3 4 5 This passes the elements of the range() iterable as individual arguments to print(), i. e. it's equivalent to print(0, 1, 2, 3, 4, 5) From __peter__ at web.de Thu Mar 25 05:19:44 2021 From: __peter__ at web.de (Peter Otten) Date: Thu, 25 Mar 2021 10:19:44 +0100 Subject: [Tutor] Printing without a line feed In-Reply-To: <aefe872e-476d-c743-adf3-943c3b293cde@gmail.com> References: <aefe872e-476d-c743-adf3-943c3b293cde@gmail.com> Message-ID: <fe7eb5c3-a746-064d-7b64-642ee5cff681@web.de> On 25/03/2021 06:29, Phil wrote: > Thank you for reading this. > > I want to print a range of numbers with a space between them but without > a space at the end. For instance: > > ??? for i in range(6): > ??????? print(i, end=' ') > > How do I do this without the end space? I've spent hours on this even > though I've managed to do what I want not long ago but I cannot remember > how I did it. It's very frustrating. You can convert the numbers to a single string with >>> print(" ".join(str(i) for i in range(6))) 0 1 2 3 4 5 The argument of "".join() is called "generator expression". Or you can use the star operator: >>> print(*range(6)) 0 1 2 3 4 5 This passes the elements of the range() iterable as individual arguments to print(), i. e. it's equivalent to print(0, 1, 2, 3, 4, 5) From alan.gauld at yahoo.co.uk Thu Mar 25 07:42:22 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 25 Mar 2021 11:42:22 +0000 Subject: [Tutor] Printing without a line feed In-Reply-To: <fe7eb5c3-a746-064d-7b64-642ee5cff681@web.de> References: <aefe872e-476d-c743-adf3-943c3b293cde@gmail.com> <fe7eb5c3-a746-064d-7b64-642ee5cff681@web.de> Message-ID: <s3hsuu$2co$1@ciao.gmane.io> On 25/03/2021 09:19, Peter Otten wrote: > Or you can use the star operator: > >>>> print(*range(6)) > 0 1 2 3 4 5 Ooh, I like that! I wish I'd thought of it. Very neat. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From phillor9 at gmail.com Thu Mar 25 18:12:54 2021 From: phillor9 at gmail.com (Phil) Date: Fri, 26 Mar 2021 09:12:54 +1100 Subject: [Tutor] Printing without a line feed In-Reply-To: <CAB1e+aMsd_-tFEMhrN=ukSoKTstQUrmv_ymnbzhMWNJVHseASQ@mail.gmail.com> References: <aefe872e-476d-c743-adf3-943c3b293cde@gmail.com> <CAB1e+aMsd_-tFEMhrN=ukSoKTstQUrmv_ymnbzhMWNJVHseASQ@mail.gmail.com> Message-ID: <c65acd0e-ec7f-84ab-c9cf-cf8449bfe755@gmail.com> On 25/3/21 5:29 pm, Narasimharao Nelluri wrote: Thank you all who offered a solution. It was the * operator that I'd used previously but had forgotten. I've used the build-up-a-string technique quite often. Join, being the opposite to split (which I've used a lot) is a useful function that I will try to remember. -- Regards, Phil From mats at wichmann.us Thu Mar 25 19:49:52 2021 From: mats at wichmann.us (Mats Wichmann) Date: Thu, 25 Mar 2021 17:49:52 -0600 Subject: [Tutor] Printing without a line feed In-Reply-To: <c65acd0e-ec7f-84ab-c9cf-cf8449bfe755@gmail.com> References: <aefe872e-476d-c743-adf3-943c3b293cde@gmail.com> <CAB1e+aMsd_-tFEMhrN=ukSoKTstQUrmv_ymnbzhMWNJVHseASQ@mail.gmail.com> <c65acd0e-ec7f-84ab-c9cf-cf8449bfe755@gmail.com> Message-ID: <a519b978-d493-e7a7-827c-6c279bbcadde@wichmann.us> On 3/25/21 4:12 PM, Phil wrote: > On 25/3/21 5:29 pm, Narasimharao Nelluri wrote: > > Thank you all who offered a solution. It was the * operator that I'd > used previously but had forgotten. I've used the build-up-a-string > technique quite often. Join, being the opposite to split (which I've > used a lot) is a useful function that I will try to remember. The packing/unpacking operators - * for lists/tuples and ** for dicts - are very useful and somehow hard to keep track of. We know about them from the relatively standard form: def func(foo, *args, **kwargs): but it's not magic unique to function declarations/signatures, it's a general form usable in many places. (okay, I'm speaking for myself, I have to keep reminding myself, but I keep seeing evidence it's probably a useful reminder for others as well). Unfortunately docs on this packing/unpacking stuff are not as visible as they might be. From phillor9 at gmail.com Sun Mar 28 23:56:26 2021 From: phillor9 at gmail.com (Phil) Date: Mon, 29 Mar 2021 14:56:26 +1100 Subject: [Tutor] creating and writing to a text file Message-ID: <ddc0ff0d-3ce5-d05d-53da-27318b8e10eb@gmail.com> Thank you for reading this. This is a simple task that I've carried out many times in the past but for some reason I've failed this time. I've saved the following code as "file_test.py" with open('test.txt', 'w+') as f: ??? f.write('testing') Entering "python3 file_test.py" creates the following directory entry: 'test'$'\177''.txt' If I open and then read "'test'$'\177''.txt'" the file it does contain "testing". Why isn't the created file named test.txt? Python 3.8 running under Linux. -- Regards, Phil From cs at cskk.id.au Mon Mar 29 00:03:24 2021 From: cs at cskk.id.au (Cameron Simpson) Date: Mon, 29 Mar 2021 15:03:24 +1100 Subject: [Tutor] creating and writing to a text file In-Reply-To: <ddc0ff0d-3ce5-d05d-53da-27318b8e10eb@gmail.com> References: <ddc0ff0d-3ce5-d05d-53da-27318b8e10eb@gmail.com> Message-ID: <YGFRjHllAJvwYfqm@cskk.homeip.net> On 29Mar2021 14:56, Phil <phillor9 at gmail.com> wrote: >Thank you for reading this. > >This is a simple task that I've carried out many times in the past but >for some reason I've failed this time. > >I've saved the following code as "file_test.py" > >with open('test.txt', 'w+') as f: >??? f.write('testing') > >Entering "python3 file_test.py" creates the following directory entry: > >'test'$'\177''.txt' > >If I open and then read "'test'$'\177''.txt'" the file it does contain >"testing". > >Why isn't the created file named test.txt? I think you've embedded a DEL character in your programme and just can't see it. Does your editor really show nothing odd in the 'test.txt' open call()? Cheers, Cameron Simpson <cs at cskk.id.au> From phillor9 at gmail.com Mon Mar 29 01:05:52 2021 From: phillor9 at gmail.com (Phil) Date: Mon, 29 Mar 2021 16:05:52 +1100 Subject: [Tutor] creating and writing to a text file In-Reply-To: <YGFRjHllAJvwYfqm@cskk.homeip.net> References: <ddc0ff0d-3ce5-d05d-53da-27318b8e10eb@gmail.com> <YGFRjHllAJvwYfqm@cskk.homeip.net> Message-ID: <9b281a74-e636-b7e2-a4a6-e531927c543a@gmail.com> On 29/3/21 3:03 pm, Cameron Simpson wrote: > I think you've embedded a DEL character in your programme and just can't > see it. Does your editor really show nothing odd in the 'test.txt' open > call()? Thank you Cameron, I'm using IDLE and I cannot see anything odd anywhere. Anyway, I have solved the problem by starting afresh. I have three other IDEs, I'll see if they show any odd characters. -- Regards, Phil From PyTutor at DancesWithMice.info Mon Mar 29 04:20:22 2021 From: PyTutor at DancesWithMice.info (dn) Date: Mon, 29 Mar 2021 21:20:22 +1300 Subject: [Tutor] creating and writing to a text file In-Reply-To: <9b281a74-e636-b7e2-a4a6-e531927c543a@gmail.com> References: <ddc0ff0d-3ce5-d05d-53da-27318b8e10eb@gmail.com> <YGFRjHllAJvwYfqm@cskk.homeip.net> <9b281a74-e636-b7e2-a4a6-e531927c543a@gmail.com> Message-ID: <bc4aec51-e0ab-b446-55dd-037cd715e242@DancesWithMice.info> On 29/03/2021 18.05, Phil wrote: > On 29/3/21 3:03 pm, Cameron Simpson wrote: >> I think you've embedded a DEL character in your programme and just can't >> see it. Does your editor really show nothing odd in the 'test.txt' open >> call()? > > Thank you Cameron, > > I'm using IDLE and I cannot see anything odd anywhere. Anyway, I have > solved the problem by starting afresh. I have three other IDEs, I'll see > if they show any odd characters. No need for that, keep things simple: delete everything including the apostrophes, then re-type that part of the line, ensuring every keystroke is correct and just for laughs, use double-quotes to surround the file-name. -- Regards, =dn From phillor9 at gmail.com Mon Mar 29 04:27:37 2021 From: phillor9 at gmail.com (Phil) Date: Mon, 29 Mar 2021 19:27:37 +1100 Subject: [Tutor] creating and writing to a text file In-Reply-To: <bc4aec51-e0ab-b446-55dd-037cd715e242@DancesWithMice.info> References: <ddc0ff0d-3ce5-d05d-53da-27318b8e10eb@gmail.com> <YGFRjHllAJvwYfqm@cskk.homeip.net> <9b281a74-e636-b7e2-a4a6-e531927c543a@gmail.com> <bc4aec51-e0ab-b446-55dd-037cd715e242@DancesWithMice.info> Message-ID: <9b3a3a91-9070-3565-7c36-bbaee2c4843f@gmail.com> On 29/3/21 7:20 pm, dn via Tutor wrote: > and just for laughs, use double-quotes to surround > the file-name. I've never been sure when to use single or double quotes. Black confirms that double quotes is the correct option in this case. -- Regards, Phil From akrocks889 at gmail.com Mon Mar 29 03:04:31 2021 From: akrocks889 at gmail.com (Anosh Kaniskar) Date: Mon, 29 Mar 2021 12:34:31 +0530 Subject: [Tutor] ValueError: Unable to coerce to DataFrame, shape must be (842, 1): given (3367, 1); ValueError: cannot reshape array of size 1 into shape (4) Message-ID: <CAN1QjEWvBrY=TK15hCGW3YMkMq3o_T9eGzdTdpF8OPQ25OKxMw@mail.gmail.com> Hi Everyone, Mercedes Benz Project, where you need to cut the time a Mercedes Benz spends on the test bench.. So, before using any model, i performed pca technique to deal with the curse of dimensionality, and I started with the multiple linear regression model and Im not satisfied with the outcome, so I started using Random Forest and I successfully fitted the data on to the random forest model for eg: from sklearn.ensemble import RandomForestRegressor RFReg = RandomForestRegressor(n_estimators=500,max_depth=4, random_state = 50) RFReg.fit(X_train, y_train) y_pred_train = RFReg.predict(X_train) y_pred_train ____________________________________________ errors = abs(y_test - y_pred_train) errors This code gave me an error ~/Desktop/Desktop/anaconda3/lib/python3.8/site-packages/pandas/core/ops/__init__.py in _align_method_FRAME(left, right, axis) 661 662 else:--> 663 raise ValueError( 664 "Unable to coerce to DataFrame, shape " 665 f"must be {left.shape}: given {right.shape}" ValueError: Unable to coerce to DataFrame, shape must be (842, 1): given (3367, 1) _______________________________________________ So later I tried y_pred_train = y_pred_train.reshape(3367, 1) Here I'm looking for explanation for what is going wrong.. Regards Anosh K From alan.gauld at yahoo.co.uk Mon Mar 29 04:43:21 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 29 Mar 2021 09:43:21 +0100 Subject: [Tutor] creating and writing to a text file In-Reply-To: <9b3a3a91-9070-3565-7c36-bbaee2c4843f@gmail.com> References: <ddc0ff0d-3ce5-d05d-53da-27318b8e10eb@gmail.com> <YGFRjHllAJvwYfqm@cskk.homeip.net> <9b281a74-e636-b7e2-a4a6-e531927c543a@gmail.com> <bc4aec51-e0ab-b446-55dd-037cd715e242@DancesWithMice.info> <9b3a3a91-9070-3565-7c36-bbaee2c4843f@gmail.com> Message-ID: <s3s3v9$si1$1@ciao.gmane.io> On 29/03/2021 09:27, Phil wrote: > On 29/3/21 7:20 pm, dn via Tutor wrote: >> and just for laughs, use double-quotes to surround >> the file-name. > > I've never been sure when to use single or double quotes. Black confirms > that double quotes is the correct option in this case. There is no difference between them and you can use whichever suits in a particular case. But double quotes are often less ambiguous than single especially when doubling or tripling them. The most likely cause of your error is that you hit Ctrl-V and tried to delete it by hitting DEL. That would insert a literal DEL into your string, which would be invisible... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alan.gauld at yahoo.co.uk Mon Mar 29 04:47:46 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 29 Mar 2021 09:47:46 +0100 Subject: [Tutor] ValueError: Unable to coerce to DataFrame, shape must be (842, 1): given (3367, 1); ValueError: cannot reshape array of size 1 into shape (4) In-Reply-To: <CAN1QjEWvBrY=TK15hCGW3YMkMq3o_T9eGzdTdpF8OPQ25OKxMw@mail.gmail.com> References: <CAN1QjEWvBrY=TK15hCGW3YMkMq3o_T9eGzdTdpF8OPQ25OKxMw@mail.gmail.com> Message-ID: <s3s47j$ba7$1@ciao.gmane.io> On 29/03/2021 08:04, Anosh Kaniskar wrote: Hi Anosh, The tutor list is focused on the Python language and its standard library. This question seems to be entirely about sklearn and Pandas so you might get a better result asking on the SciPy forum which supports Pandas. However, there are some pandas users here so you may get lucky... > So, before using any model, i performed pca technique to deal with the > curse of dimensionality, and I started with the multiple linear regression > model and Im not satisfied with the outcome, so I started using Random > Forest and I successfully fitted the data on to the random forest model > for eg: > from sklearn.ensemble import RandomForestRegressor > RFReg = RandomForestRegressor(n_estimators=500,max_depth=4, random_state = > 50) > RFReg.fit(X_train, y_train) > > y_pred_train = RFReg.predict(X_train) > y_pred_train > ____________________________________________ > errors = abs(y_test - y_pred_train) > errors > This code gave me an error > > ~/Desktop/Desktop/anaconda3/lib/python3.8/site-packages/pandas/core/ops/__init__.py > in _align_method_FRAME(left, right, axis) 661 662 > else:--> 663 raise ValueError( 664 > "Unable to coerce to DataFrame, shape " 665 > f"must be {left.shape}: given {right.shape}" > > > ValueError: Unable to coerce to DataFrame, shape must be (842, 1): > given (3367, 1) > > _______________________________________________ > > > So later I tried > > y_pred_train = y_pred_train.reshape(3367, 1) > > > Here I'm looking for explanation for what is going wrong.. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From robertvstepp at gmail.com Mon Mar 29 17:48:40 2021 From: robertvstepp at gmail.com (boB Stepp) Date: Mon, 29 Mar 2021 16:48:40 -0500 Subject: [Tutor] creating and writing to a text file In-Reply-To: <9b3a3a91-9070-3565-7c36-bbaee2c4843f@gmail.com> References: <ddc0ff0d-3ce5-d05d-53da-27318b8e10eb@gmail.com> <YGFRjHllAJvwYfqm@cskk.homeip.net> <9b281a74-e636-b7e2-a4a6-e531927c543a@gmail.com> <bc4aec51-e0ab-b446-55dd-037cd715e242@DancesWithMice.info> <9b3a3a91-9070-3565-7c36-bbaee2c4843f@gmail.com> Message-ID: <YGJLOCyJz9XD4geH@Dream-Machine1> On 21/03/29 07:27PM, Phil wrote: > >I've never been sure when to use single or double quotes. Black >confirms that double quotes is the correct option in this case. Just to clear up any possible confusion Black is designed to prefer double quotes over single quotes in every situation unless that is not possible to do. Just because Black double quotes something does *not* mean that is the correct way to use quotes! -- Wishing you only the best, boB Stepp From cs at cskk.id.au Mon Mar 29 19:14:32 2021 From: cs at cskk.id.au (Cameron Simpson) Date: Tue, 30 Mar 2021 10:14:32 +1100 Subject: [Tutor] creating and writing to a text file In-Reply-To: <YGJLOCyJz9XD4geH@Dream-Machine1> References: <YGJLOCyJz9XD4geH@Dream-Machine1> Message-ID: <YGJfWFZpl4PM9rV3@cskk.homeip.net> On 29Mar2021 16:48, boB Stepp <robertvstepp at gmail.com> wrote: >On 21/03/29 07:27PM, Phil wrote: >>I've never been sure when to use single or double quotes. Black >>confirms that double quotes is the correct option in this case. > >Just to clear up any possible confusion Black is designed to prefer double >quotes over single quotes in every situation unless that is not possible to >do. Just because Black double quotes something does *not* mean that is the >correct way to use quotes! And to add to this, black's obsession with replacing my nice clean ''' with horrible noisy """ is, alone, reason for me to refuse to use it. Again, Heuer's Razor: If it can't be turned off, it's not a feature. - Karl Heuer Black is for people who want consistent formatting (it certainly eases diff churn etc once the formatting is done) but _don't_ care about style. I remain a big fan of yapf, and use it by default. Cheers, Cameron Simpson <cs at cskk.id.au> From mats at wichmann.us Mon Mar 29 20:03:21 2021 From: mats at wichmann.us (Mats Wichmann) Date: Mon, 29 Mar 2021 18:03:21 -0600 Subject: [Tutor] creating and writing to a text file In-Reply-To: <YGJfWFZpl4PM9rV3@cskk.homeip.net> References: <YGJLOCyJz9XD4geH@Dream-Machine1> <YGJfWFZpl4PM9rV3@cskk.homeip.net> Message-ID: <dd3943c2-9ebb-801c-9081-a5161802f154@wichmann.us> On 3/29/21 5:14 PM, Cameron Simpson wrote: > Again, Heuer's Razor: > > If it can't be turned off, it's not a feature. - Karl Heuer > > Black is for people who want consistent formatting (it certainly eases > diff churn etc once the formatting is done) but _don't_ care about > style. It's basically to decide "okay, this is just the way it is, we use this and no more arguing about style because the tool will do the style for us". But the quote thing is controversial enough that it's actually one of the vanishingly small number of things you can configure in Black. (example of "controversial": it you enter a string in the Python interpreter (REPL), it will display it back to you in single quotes, but Black turns it to double) > I remain a big fan of yapf, and use it by default. Have to agree, yapf does a nice job. But... From phillor9 at gmail.com Tue Mar 30 00:02:12 2021 From: phillor9 at gmail.com (Phil) Date: Tue, 30 Mar 2021 15:02:12 +1100 Subject: [Tutor] creating and writing to a text file In-Reply-To: <YGJfWFZpl4PM9rV3@cskk.homeip.net> References: <YGJLOCyJz9XD4geH@Dream-Machine1> <YGJfWFZpl4PM9rV3@cskk.homeip.net> Message-ID: <41ed6474-1623-47d0-6e89-2d79fc5a9b11@gmail.com> On 30/3/21 10:14 am, Cameron Simpson wrote: > > Black is for people who want consistent formatting (it certainly eases > diff churn etc once the formatting is done) but _don't_ care about > style. > > I remain a big fan of yapf, and use it by default. I've installed yapf and had a little play using the --style "pep8" and -i options and it seems to work well. Another profiler that I'd used is flake, or is it flake8?. Yapf has fewer options and seems to be easier to use. -- Regards, Phil From PyTutor at DancesWithMice.info Tue Mar 30 00:32:59 2021 From: PyTutor at DancesWithMice.info (dn) Date: Tue, 30 Mar 2021 17:32:59 +1300 Subject: [Tutor] creating and writing to a text file In-Reply-To: <YGJfWFZpl4PM9rV3@cskk.homeip.net> References: <YGJLOCyJz9XD4geH@Dream-Machine1> <YGJfWFZpl4PM9rV3@cskk.homeip.net> Message-ID: <c498bccf-a73a-20dc-dee8-56dec1aefbb6@DancesWithMice.info> On 30/03/2021 12.14, Cameron Simpson wrote: > On 29Mar2021 16:48, boB Stepp <robertvstepp at gmail.com> wrote: >> On 21/03/29 07:27PM, Phil wrote: >>> I've never been sure when to use single or double quotes. Black >>> confirms that double quotes is the correct option in this case. >> >> Just to clear up any possible confusion Black is designed to prefer double >> quotes over single quotes in every situation unless that is not possible to >> do. Just because Black double quotes something does *not* mean that is the >> correct way to use quotes! > > And to add to this, black's obsession with replacing my nice clean ''' > with horrible noisy """ is, alone, reason for me to refuse to use it. > > Again, Heuer's Razor: > > If it can't be turned off, it's not a feature. - Karl Heuer > > Black is for people who want consistent formatting (it certainly eases > diff churn etc once the formatting is done) but _don't_ care about > style. -1 on the unnecessary diff-noise! +1 "noise". I'd always used triple single-quotes for exactly that reason. That said, one day I woke-up and found PEP-257 (https://www.python.org/dev/peps/pep-0257/) which does want """Docstrings.""". For those who want an 'out', review the PEP's extremely strong language: "If you violate these conventions, the worst you'll get is some dirty looks. But some software (such as the Docutils [3] docstring processing system [1] [2]) will be aware of the conventions, so following them will get you the best results". So, the usual 'don't get too excited' disclaimers apply. Fast forward to modern ed/IDE tools, and when such add-ons kept whining about the single-quotes, I asked 'if I let you make the changes, will you go away and leave me in peace...'. What a push-over! -- Regards, =dn From cs at cskk.id.au Tue Mar 30 02:18:29 2021 From: cs at cskk.id.au (Cameron Simpson) Date: Tue, 30 Mar 2021 17:18:29 +1100 Subject: [Tutor] creating and writing to a text file In-Reply-To: <41ed6474-1623-47d0-6e89-2d79fc5a9b11@gmail.com> References: <41ed6474-1623-47d0-6e89-2d79fc5a9b11@gmail.com> Message-ID: <YGLCtRmKNokbdsA+@cskk.homeip.net> On 30Mar2021 15:02, Phil <phillor9 at gmail.com> wrote: >On 30/3/21 10:14 am, Cameron Simpson wrote: >>I remain a big fan of yapf, and use it by default. > >I've installed yapf and had a little play using the --style "pep8" and >-i options and it seems to work well. You can make a .style.yapf file to hold settings too. >Another profiler that I'd used is flake, or is it flake8?. Yapf has >fewer options and seems to be easier to use. If you're talking about pyflakes, it's a linter: https://pypi.org/project/pyflakes/ It actually promises to not complain about style :-) Cheers, Cameron Simpson <cs at cskk.id.au> From learn2program at gmail.com Tue Mar 30 04:23:21 2021 From: learn2program at gmail.com (Alan Gauld) Date: Tue, 30 Mar 2021 09:23:21 +0100 Subject: [Tutor] creating and writing to a text file In-Reply-To: <YGLCtRmKNokbdsA+@cskk.homeip.net> References: <41ed6474-1623-47d0-6e89-2d79fc5a9b11@gmail.com> <YGLCtRmKNokbdsA+@cskk.homeip.net> Message-ID: <f959b4a6-a568-c46d-84c2-47f32ee5685d@yahoo.co.uk> On 30/03/2021 07:18, Cameron Simpson wrote: > If you're talking about pyflakes, it's a linter: > https://pypi.org/project/pyflakes/ > > It actually promises to not complain about style :-) > I'm a big fan of linters but I hate formatters. There's very little evidence that strict code formatting makes much difference to comprehension and in practice I find that different styles apply in different parts of the code specifically as a way of conveying meaning(*). Formatters tend to remove those variations. I avoid them like the plague! (*) an example would be building a menu system from a data structure, it makes sense to lay the data out to look like the menus. but in other places you want it to look like a flat data store. Most formatters insist on? lining everything up according to whatever scheme their author prefers (or if you are lucky a config setting) -- 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 Mar 30 07:07:56 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 30 Mar 2021 12:07:56 +0100 Subject: [Tutor] Ann: New Python curses book. Message-ID: <877432ad-057a-5d3d-1000-13e6b6fb4154@yahoo.co.uk> I've just published, in Kindle and paperback formats, my book on "Programming curses with Python". https://www.amazon.co.uk/dp/B091B85B77/ (It should be available in most other Amazon stores too) It is a complete rewrite of the Linux Documentation Project's HowTo for the C ncurses library. It has the same structure but all-new text and several new chapters. All specific to the Python curses package. I've kept the prices as low as Amazon permits (and allowing for fluctuations in their costs). As such, I will therefore make a few cents profit per book from sales (full disclosure). However, I felt this was the easiest and most effective way to get it "out there" where it could be found. I hope it's useful to somebody. Special thanks to all who provided me with feedback on its various iterations, especially Alex Kleider and Bob Stepp who stuck with it right to the end. Thanks guys. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alexkleider at gmail.com Tue Mar 30 11:37:09 2021 From: alexkleider at gmail.com (Alex Kleider) Date: Tue, 30 Mar 2021 08:37:09 -0700 Subject: [Tutor] managing sensitive data Message-ID: <CAMCEyD5yVZnd0ebQ8WJReD5_F1J2LjPxQ5SJSiJra5etbkOD8A@mail.gmail.com> I saw the following question raised recently on the "PowerfulPython" mailing list "How do I manage sensitive credentials in my Python code?" (One has to sign up for a course to hear the discussion.) I've faced this problem [1] and solved it by putting the 'sensitive' data (passwords) into dot files in my home directory with strict permission settings. I've not tested it but assume that the code will fail when trying to read such a file if the one running the code doesn't have permission to read it. (Not really a 'Python' solution!) Is there a better way to do this? [1] my code sends out emails and to do this needs a password to convince the mail transfer agent to accept the mail. From david at lowryduda.com Tue Mar 30 12:05:25 2021 From: david at lowryduda.com (David Lowry-Duda) Date: Tue, 30 Mar 2021 12:05:25 -0400 Subject: [Tutor] managing sensitive data In-Reply-To: <CAMCEyD5yVZnd0ebQ8WJReD5_F1J2LjPxQ5SJSiJra5etbkOD8A@mail.gmail.com> References: <CAMCEyD5yVZnd0ebQ8WJReD5_F1J2LjPxQ5SJSiJra5etbkOD8A@mail.gmail.com> Message-ID: <YGNMRRB0WzleRZoc@icerm-dld> > "How do I manage sensitive credentials in my Python code?" > I've faced this problem [1] and solved it by putting the 'sensitive' > data (passwords) into dot files in my home directory with strict permission > settings. Is there a better way to do this? My goto method is very simple. I never store passwords/credentials on file. Instead, I typically use environment variables. This looks like ``` import os super_secret_password = os.getenv("MY_APP_SECRET_PASSWORD", '') # do stuff with super_secret_password ``` I set various environment variables before running the code. For things I actually care about, I would do this on a server with a dedicated account that isn't my user account. I should mention that this is very common, and there is lots of discussion about using secure configuration files vs environment variables vs password agents (like ssh-agent) vs others. But environment variables work well for me. As an aside: > I saw the following question raised recently on the "PowerfulPython" > mailing list I'm not familiar with PowerfulPython. I see that it's a site and a book, and there is some sort of newsletter and perhaps some courses. I suppose that the mailing list is exclusively for people who have paid some sort of money. Do you like it? I'm always trying to keep aware of the various ways people learn python (though my goto recommendation of reading "Think Python" to start hasn't changed in a long time). Is the mailing list good? - DLD From alan.gauld at yahoo.co.uk Tue Mar 30 12:15:25 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 30 Mar 2021 17:15:25 +0100 Subject: [Tutor] managing sensitive data In-Reply-To: <CAMCEyD5yVZnd0ebQ8WJReD5_F1J2LjPxQ5SJSiJra5etbkOD8A@mail.gmail.com> References: <CAMCEyD5yVZnd0ebQ8WJReD5_F1J2LjPxQ5SJSiJra5etbkOD8A@mail.gmail.com> Message-ID: <s3viqu$2fv$1@ciao.gmane.io> On 30/03/2021 16:37, Alex Kleider wrote: > "How do I manage sensitive credentials in my Python code?" > I've faced this problem [1] and solved it by putting the 'sensitive' data > (passwords) into dot files in my home directory with strict permission > settings. I've not tested it but assume that the code will fail when trying > to read such a file if the one running the code doesn't have permission to > read it. One obvious addition is to encrypt the sensitive data. Of course if these are passwords to another system you need some form of decryption too, which tends to reduce the strength of the encryption possible. Another option is to store the data in a database rather than a file. The database then needs its own login credentials to access it and the format of native database files tends to be harder to hack than text files (or even regular bespoke binary files) At work we always used a database to store the encrypted credentials and then stored the database on an encrypted file system. That way there were 2 levels of encryption plus two levels of access control to get at them. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From PyTutor at DancesWithMice.info Tue Mar 30 15:56:27 2021 From: PyTutor at DancesWithMice.info (dn) Date: Wed, 31 Mar 2021 08:56:27 +1300 Subject: [Tutor] Ann: New Python curses book. In-Reply-To: <877432ad-057a-5d3d-1000-13e6b6fb4154@yahoo.co.uk> References: <877432ad-057a-5d3d-1000-13e6b6fb4154@yahoo.co.uk> Message-ID: <aace1812-646a-001f-f7bf-6efc91d63c63@DancesWithMice.info> On 31/03/2021 00.07, Alan Gauld via Tutor wrote: > I've just published, in Kindle and paperback formats, > my book on "Programming curses with Python". > > https://www.amazon.co.uk/dp/B091B85B77/ Congratulations on achieving yet another publication @Alan - well done! -- Regards, =dn From cs at cskk.id.au Tue Mar 30 17:21:24 2021 From: cs at cskk.id.au (Cameron Simpson) Date: Wed, 31 Mar 2021 08:21:24 +1100 Subject: [Tutor] creating and writing to a text file In-Reply-To: <f959b4a6-a568-c46d-84c2-47f32ee5685d@yahoo.co.uk> References: <f959b4a6-a568-c46d-84c2-47f32ee5685d@yahoo.co.uk> Message-ID: <YGOWVEOv2tuubeTC@cskk.homeip.net> On 30Mar2021 09:23, Alan Gauld <learn2program at gmail.com> wrote: >On 30/03/2021 07:18, Cameron Simpson wrote: >> If you're talking about pyflakes, it's a linter: >> https://pypi.org/project/pyflakes/ >> >> It actually promises to not complain about style :-) >> >I'm a big fan of linters but I hate formatters. I have mixed feelings, but yapf can be tuned to very close to my preferred style anyway. >There's very little evidence that strict code formatting makes much >difference >to comprehension Aye, but I'm prone to trivial typos (spaces, etc) and poorly formatted code annoying me, doubly so when it is my own poorly formatted code. It _is_ very pleasing to me to have a computer apply that rigour for me. >and in practice I find that different styles apply in >different >parts of the code specifically as a way of conveying meaning(*). Formatters >tend to remove those variations. > >I avoid them like the plague! > >(*) an example would be building a menu system from a data structure, >it makes sense to lay the data out to look like the menus. but in other >places you want it to look like a flat data store. Most formatters >insist on? >lining everything up according to whatever scheme their author prefers >(or if you are lucky a config setting) Yes, this is true. Even yapf doesn't indent dict literals (for eg) as I want. It _does_ at least have a lexical cue in that a trailing comma lines all the values up vertically, and no trailing commas makes it fill the lines. But I'll take it over hand aligning my code. Cheers, Cameron Simpson <cs at cskk.id.au> From mats at wichmann.us Tue Mar 30 17:53:18 2021 From: mats at wichmann.us (Mats Wichmann) Date: Tue, 30 Mar 2021 15:53:18 -0600 Subject: [Tutor] creating and writing to a text file In-Reply-To: <YGOWVEOv2tuubeTC@cskk.homeip.net> References: <f959b4a6-a568-c46d-84c2-47f32ee5685d@yahoo.co.uk> <YGOWVEOv2tuubeTC@cskk.homeip.net> Message-ID: <41d10b2b-a08d-af41-d5e9-826b080b03c6@wichmann.us> Okay, I promise last from me on this subject. Black has become very popular. It solves, in a brute-force way, a particular problem: in a team, there are going to be people with slightly different ideas of style. The organization can grunt their way through producing a style document, and then someone agrees 95% with it, but wants to write a dict with a space before the colon: {'foo' : 12}, or drop the space before a colon in a type annotation ( def foo(arg:int) -> int ) both of which are completely harmless, or not even consciously do something like that but have it unconciously entered in while typing). If those draw review comments on a code submission, that's wasted time to deal with - to make and to respond to. Instead, trust that the tool has fixed it to "the style" and just don't comment on those. So Black wants to have you stop looking at style and focus on content in code review scenarios. But Im' not arguing *for* it, If the above is not a winning argument in a give scenario, fine. Don't use Black. There's no moral imperative to do so. On 3/30/21 3:21 PM, Cameron Simpson wrote: > On 30Mar2021 09:23, Alan Gauld <learn2program at gmail.com> wrote: >> On 30/03/2021 07:18, Cameron Simpson wrote: >>> If you're talking about pyflakes, it's a linter: >>> https://pypi.org/project/pyflakes/ >>> >>> It actually promises to not complain about style :-) >>> >> I'm a big fan of linters but I hate formatters. > > I have mixed feelings, but yapf can be tuned to very close to my > preferred style anyway. And that's absolutely fine, it's your code, use what makes you happy. I really liked yapf at first. Then I started running into format arguments on a particular project. The case for format tools that can be invoked automatically is to keep people from disagreeing about stuff that really shouldn't matter. Not to tell you your style is wrong... though it can sort of have that effect, sometimes. Sigh. >> There's very little evidence that strict code formatting makes much >> difference >> to comprehension > > Aye, but I'm prone to trivial typos (spaces, etc) and poorly formatted > code annoying me, doubly so when it is my own poorly formatted code. It > _is_ very pleasing to me to have a computer apply that rigour for me. > >> and in practice I find that different styles apply in >> different >> parts of the code specifically as a way of conveying meaning(*). Formatters >> tend to remove those variations. >> >> I avoid them like the plague! >> >> (*) an example would be building a menu system from a data structure, >> it makes sense to lay the data out to look like the menus. but in other >> places you want it to look like a flat data store. Most formatters >> insist on >> lining everything up according to whatever scheme their author prefers >> (or if you are lucky a config setting) > > Yes, this is true. Even yapf doesn't indent dict literals (for eg) as I > want. It _does_ at least have a lexical cue in that a trailing comma > lines all the values up vertically, and no trailing commas makes it fill > the lines. If you do feel like a chunk needs particular formatting for readability, alignment to external views, etc. you can usually tell formatters to keep their hands off, just like you can tell checkers to ignore certain things. Even the "unconfigurable" Black can do this: "Your main option of configuring Black is that it doesn't reformat blocks that start with # fmt: off and end with # fmt: on, or lines that ends with # fmt: skip." From alan.gauld at yahoo.co.uk Tue Mar 30 19:03:42 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 31 Mar 2021 00:03:42 +0100 Subject: [Tutor] creating and writing to a text file In-Reply-To: <41d10b2b-a08d-af41-d5e9-826b080b03c6@wichmann.us> References: <f959b4a6-a568-c46d-84c2-47f32ee5685d@yahoo.co.uk> <YGOWVEOv2tuubeTC@cskk.homeip.net> <41d10b2b-a08d-af41-d5e9-826b080b03c6@wichmann.us> Message-ID: <s40aoe$46s$1@ciao.gmane.io> On 30/03/2021 22:53, Mats Wichmann wrote: > particular problem: in a team, there are going to be people with > slightly different ideas of style. The organization can grunt their way > through producing a style document, and then someone agrees 95% with it, I've heard this argument many times, but it breaks down on large projects. The largest project I've worked on had around 500 programmers working in 5 (or 6?) different organizations in 3 different countries. Each organisation had their own style guides. There was no way we could have a single style and frankly, no need. Provided code compiled and followed some very basic rules (indentation between 2-6 characters, some basic variable naming and file structure rules) it was good to go. Style comments were strictly forbidden at reviews. It generated close to 3.6 million lines of C++ and you know what? It worked. And it still does 30 years later. -- 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 Tue Mar 30 19:25:25 2021 From: mats at wichmann.us (Mats Wichmann) Date: Tue, 30 Mar 2021 17:25:25 -0600 Subject: [Tutor] creating and writing to a text file In-Reply-To: <s40aoe$46s$1@ciao.gmane.io> References: <f959b4a6-a568-c46d-84c2-47f32ee5685d@yahoo.co.uk> <YGOWVEOv2tuubeTC@cskk.homeip.net> <41d10b2b-a08d-af41-d5e9-826b080b03c6@wichmann.us> <s40aoe$46s$1@ciao.gmane.io> Message-ID: <62f3da97-c5b4-547a-a7df-1b4066d11b41@wichmann.us> On 3/30/21 5:03 PM, Alan Gauld via Tutor wrote: >Style comments were strictly forbidden at reviews. And: that's a perfectly fine approach. A tool like black deals with the case where you didn't forbid them, so you eliminate them via tool. If style issues are not allowed in reviews, that solves that problem too. From alexkleider at gmail.com Tue Mar 30 21:20:21 2021 From: alexkleider at gmail.com (Alex Kleider) Date: Tue, 30 Mar 2021 18:20:21 -0700 Subject: [Tutor] managing sensitive data In-Reply-To: <s3viqu$2fv$1@ciao.gmane.io> References: <CAMCEyD5yVZnd0ebQ8WJReD5_F1J2LjPxQ5SJSiJra5etbkOD8A@mail.gmail.com> <s3viqu$2fv$1@ciao.gmane.io> Message-ID: <CAMCEyD6YGeCTQ9ugjS9zGa2UoVRzHqmSVmSWP9u5-hGcXVoLqw@mail.gmail.com> Thank you to all who replied and offered suggestions. Cheers, Alex On Tue, Mar 30, 2021 at 9:17 AM Alan Gauld via Tutor <tutor at python.org> wrote: > On 30/03/2021 16:37, Alex Kleider wrote: > > > "How do I manage sensitive credentials in my Python code?" > > > I've faced this problem [1] and solved it by putting the 'sensitive' data > > (passwords) into dot files in my home directory with strict permission > > settings. I've not tested it but assume that the code will fail when > trying > > to read such a file if the one running the code doesn't have permission > to > > read it. > > One obvious addition is to encrypt the sensitive data. > Of course if these are passwords to another system you > need some form of decryption too, which tends to reduce > the strength of the encryption possible. > > Another option is to store the data in a database > rather than a file. The database then needs its own login > credentials to access it and the format of native database > files tends to be harder to hack than text files (or even > regular bespoke binary files) > > At work we always used a database to store the encrypted > credentials and then stored the database on an encrypted > file system. That way there were 2 levels of encryption > plus two levels of access control to get at them. > > -- > 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 Mar 31 05:08:04 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 31 Mar 2021 10:08:04 +0100 Subject: [Tutor] Ann: New Python curses book. In-Reply-To: <877432ad-057a-5d3d-1000-13e6b6fb4154@yahoo.co.uk> References: <877432ad-057a-5d3d-1000-13e6b6fb4154@yahoo.co.uk> Message-ID: <s41e5l$o8a$1@ciao.gmane.io> On 30/03/2021 12:07, Alan Gauld via Tutor wrote: > I've just published, in Kindle and paperback formats, > my book on "Programming curses with Python". > > https://www.amazon.co.uk/dp/B091B85B77/ I've just published the source code from the book as a zip file under a Creative Commons license. http://www.alan-g.me.uk/hills/PythonCursesCode.zip I'll be updating the book later today to include a link. A slight oversight on my part, apologies to anyone who got an early copy... -- 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 shaiddinar17 at gmail.com Wed Mar 31 20:36:06 2021 From: shaiddinar17 at gmail.com (Shaid) Date: Wed, 31 Mar 2021 20:36:06 -0400 Subject: [Tutor] python Message-ID: <910A7BFA-3E60-4199-BD98-AE872534BC0D@hxcore.ol> Write a function that take s a list of numbers as input and returns the sum of the numbers in the list . Sent from [1]Mail for Windows 10 References Visible links 1. https://go.microsoft.com/fwlink/?LinkId=550986 From mats at wichmann.us Wed Mar 31 20:55:17 2021 From: mats at wichmann.us (Mats Wichmann) Date: Wed, 31 Mar 2021 18:55:17 -0600 Subject: [Tutor] python In-Reply-To: <910A7BFA-3E60-4199-BD98-AE872534BC0D@hxcore.ol> References: <910A7BFA-3E60-4199-BD98-AE872534BC0D@hxcore.ol> Message-ID: <12e2c36d-072e-ea1f-de4a-26606b1da80a@wichmann.us> On 3/31/21 6:36 PM, Shaid wrote: > > > Write a function that take s a list of numbers as input and returns the > sum of the numbers in the list . And are you going to try?