From phillor9 at gmail.com Mon Nov 1 01:41:28 2021 From: phillor9 at gmail.com (Phil) Date: Mon, 1 Nov 2021 15:41:28 +1000 Subject: [Tutor] Creating a two dimension set In-Reply-To: References: <9e2be6f5-b825-5291-ff5b-103c74454f97@gmail.com> Message-ID: <74eb1922-ae3f-87e6-34f6-470b729239ba@gmail.com> On 1/11/21 06:08, Cameron Simpson wrote: > On 31Oct2021 15:56, Phil wrote: Thank you Cameron for answering my fool question. I've moved from our camping site with a frustrating Internet connection >> As an example, if I wanted to create a two dimension list I would write: >> >> num_rows = 9 >> num_cols = 9 >> >> self.solution = [[None] * num_cols for _ in range(num_rows)] > Keeping in mind that this is a list of lists. Also, you're preallocating > num_cols*nul_rows of storage. That can be wasteful if you're not filling > much of it in. This is a 9 x 9 grid that I use to solve Sudoku puzzles. It was working perfectly until I tried to make some improvements without saving the original file. A mistake that I haven't learned from. > I do not fully understand your use case, partly because your second > example is not sane Python. > > I'm also not sure you mean "set". Can you tell us in more detail what > you're doing, maybe a very small example programme? I had originally used 9 x 9 numpy arrays. I then decided to use a list of lists just as an exercise. The next version used sets because I wanted to experiment with set intersections. This worked well enough until I recently found a puzzle that my solver wouldn't solve correctly. Say I have a 9 x 9 array, or a list of lists, and I want to remove a number from grid[0][7]. What simple method would I use? I've tried all sorts of complicated methods but I keep running into problems. Removing an element from a set or a list is straight forward: a = {1,2,3,4,5,6,7,8,9} if 3 in a: ??? print('found') ??? a.remove(3) print(a) However, removing an element from a list of lists is problematic: a = [['1','2','3'],['4','5','6']] a[0][0] = '9' a.remove('9') print(a[0][0]) Traceback (most recent call last): ? File "/usr/lib/python3.9/idlelib/run.py", line 559, in runcode ??? exec(code, self.locals) ? File "/home/phil/Python/set_test.py", line 29, in ??? a.remove('9') ValueError: list.remove(x): x not in list > Might I suggest a dict indexed by a 2-tuple of (row,col)? Thank you for your detailed description of dictionaries, I'm not sure that a dictionary would be suitable for my case. Can a dictionary contain a set rather than a single number, it probably can? Like this: grid = {} grid[0,0] = {1,2,3,4,5,6,7,8,9} What about removing a number, won't that fail as in a list of lists? Each cell contains a set of candidate numbers (1 to 9). I run through a set (not to be confused with a mathematical set) of rules removing numbers from the candidates until the puzzle is solved. I'm a bit frazzled from driving today, but I'll see if I can experiment with dictionaries later. -- Regards, Phil From phillor9 at gmail.com Mon Nov 1 03:18:33 2021 From: phillor9 at gmail.com (Phil) Date: Mon, 1 Nov 2021 17:18:33 +1000 Subject: [Tutor] Creating a two dimension set In-Reply-To: References: <9e2be6f5-b825-5291-ff5b-103c74454f97@gmail.com> Message-ID: <78e19bac-e5a9-4c47-2680-60869f13e7f6@gmail.com> On 1/11/21 06:08, Cameron Simpson wrote: Problem solved. Actually, the solution had nothing to do with my question. Too much looking and not enough seeing. The following works: num_rows = 2 num_cols = 2 grid = [[str] * num_cols for _ in range(num_rows)] # I changed None to str but I don't thing it matters. for row in range(2): ??? for col in range(2): ??????? grid[row][col] = set({'1', '2', '3', '4', '5', '6', '7', '8', '9'}) grid[0][0].remove('3') print(grid[0][0]) I'm open to any improvements. -- Regards, Phil From cs at cskk.id.au Mon Nov 1 04:59:24 2021 From: cs at cskk.id.au (Cameron Simpson) Date: Mon, 1 Nov 2021 19:59:24 +1100 Subject: [Tutor] Creating a two dimension set In-Reply-To: <74eb1922-ae3f-87e6-34f6-470b729239ba@gmail.com> References: <74eb1922-ae3f-87e6-34f6-470b729239ba@gmail.com> Message-ID: On 01Nov2021 15:41, Phil wrote: > >On 1/11/21 06:08, Cameron Simpson wrote: >>On 31Oct2021 15:56, Phil wrote: >Thank you Cameron for answering my fool question. I've moved from our >camping site with a frustrating Internet connection >>>As an example, if I wanted to create a two dimension list I would write: >>> >>>num_rows = 9 >>>num_cols = 9 >>> >>>self.solution = [[None] * num_cols for _ in range(num_rows)] >>Keeping in mind that this is a list of lists. Also, you're preallocating >>num_cols*nul_rows of storage. That can be wasteful if you're not filling >>much of it in. > >This is a 9 x 9 grid that I use to solve Sudoku puzzles. Tiny then. Don't worry about the space; do the simplest thing. >It was working perfectly until I tried to make some improvements >without saving the original file. A mistake that I haven't learned >from. Use revision control. It's just great! >I had originally used 9 x 9 numpy arrays. I guess that lets you do the grid directly. But numpy is kind of overkill for Sudoku in most regards. Your followon post has a list of lists of sets, which covers things off nicely. [...] >>Might I suggest a dict indexed by a 2-tuple of (row,col)? > >Thank you for your detailed description of dictionaries, I'm not sure >that a dictionary would be suitable for my case. Can a dictionary >contain a set rather than a single number, it probably can? Like this: > >grid = {} >grid[0,0] = {1,2,3,4,5,6,7,8,9} Yes. List elements are Python references, just like any other Python variable. Referencing a set is no different to referencing a str or an int. >What about removing a number, won't that fail as in a list of lists? List of lists of sets. grid[0,0].remove(3) >Each cell contains a set of candidate numbers (1 to 9). I run through >a set (not to be confused with a mathematical set) of rules removing >numbers from the candidates until the puzzle is solved. Python sets are deliberately quite similar mathematical sets. Cheers, Cameron Simpson From cs at cskk.id.au Mon Nov 1 04:52:49 2021 From: cs at cskk.id.au (Cameron Simpson) Date: Mon, 1 Nov 2021 19:52:49 +1100 Subject: [Tutor] Creating a two dimension set In-Reply-To: <78e19bac-e5a9-4c47-2680-60869f13e7f6@gmail.com> References: <78e19bac-e5a9-4c47-2680-60869f13e7f6@gmail.com> Message-ID: On 01Nov2021 17:18, Phil wrote: >Problem solved. Actually, the solution had nothing to do with my >question. Too much looking and not enough seeing. The following works: > >num_rows = 2 >num_cols = 2 > >grid = [[str] * num_cols for _ in range(num_rows)] # I changed None to >str but I don't thing it matters. Bare "str" isn't a good thing. Use None, it is the idiomatic Python value for "no value here" (there's a value, but it isn't in the expected range). Bare "str" means you've filled your lists with references to the str type. Kind of weird. >for row in range(2): >??? for col in range(2): >??????? grid[row][col] = set({'1', '2', '3', '4', '5', '6', '7', '8', '9'}) You don't need to say "set({....})". The "{.....}" stuff is already a set. Except for "{}" which is an empty dict - for an empty set you need to say "set()". I wouldn't use strings. If you're doing Sudoku don't you want to do arithmetic to check the sum? May as well make a set of ints: {1, 2, 3, 4, 5, 6, 7, 8, 9} >grid[0][0].remove('3') Yes. Of course, using ints would be grid[0][0][.remove(3). Adjust to suit. If you wanted to simplify the setup you could fill the grid with empty sets instead and have it represent rejected values instead of potential values. Of course you'd have to turn some other logic upside down. Cheers, Cameron Simpson From alan.gauld at yahoo.co.uk Mon Nov 1 07:54:07 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 1 Nov 2021 11:54:07 +0000 Subject: [Tutor] Creating a two dimension set In-Reply-To: <74eb1922-ae3f-87e6-34f6-470b729239ba@gmail.com> References: <9e2be6f5-b825-5291-ff5b-103c74454f97@gmail.com> <74eb1922-ae3f-87e6-34f6-470b729239ba@gmail.com> Message-ID: On 01/11/2021 05:41, Phil wrote: > Say I have a 9 x 9 array, or a list of lists, and I want to remove a > number from grid[0][7]. What simple method would I use? I've tried all > sorts of complicated methods but I keep running into problems. Normally using a 2D table i wouldn't use remove() I'd just assign a new null value. However... > However, removing an element from a list of lists is problematic: > > a = [['1','2','3'],['4','5','6']] > > a[0][0] = '9' > a.remove('9') a contains 2 lists. So there is no element '9' You need to access the list that contains the 9: a[0].remove('9') But if you know the cell just reassign it directly a[0][0] = '' Its also safer since remove removes the first occurrence but if you have multiple occurrences that might not be what you want. > Thank you for your detailed description of dictionaries, I'm not sure > that a dictionary would be suitable for my case. Can a dictionary > contain a set rather than a single number, A dict can contain any python object no matter how complex. Recall that python uses references, it does not store the actual object in the dict but rather a reference. So the reference can be to anything. > grid[0,0] = {1,2,3,4,5,6,7,8,9} > > What about removing a number, won't that fail as in a list of lists? You call remove on the set not on the dict. grid[0,0].remove(num) grid[0,0] returns the set and remove() is then called on the set. -- 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 wlfraed at ix.netcom.com Mon Nov 1 11:30:16 2021 From: wlfraed at ix.netcom.com (Dennis Lee Bieber) Date: Mon, 01 Nov 2021 11:30:16 -0400 Subject: [Tutor] Creating a two dimension set References: <78e19bac-e5a9-4c47-2680-60869f13e7f6@gmail.com> Message-ID: On Mon, 1 Nov 2021 19:52:49 +1100, Cameron Simpson declaimed the following: >I wouldn't use strings. If you're doing Sudoku don't you want to do >arithmetic to check the sum? May as well make a set of ints: > No arithmetic done in Sudoku... You can replace the 1..9 with A..I, or even emojis (if you can mentally keep track of such). The only thing one is doing is eliminating candidates for a cell if that candidate was already used on the row/column. -- Wulfraed Dennis Lee Bieber AF6VN wlfraed at ix.netcom.com http://wlfraed.microdiversity.freeddns.org/ From cs at cskk.id.au Mon Nov 1 17:28:33 2021 From: cs at cskk.id.au (Cameron Simpson) Date: Tue, 2 Nov 2021 08:28:33 +1100 Subject: [Tutor] Creating a two dimension set In-Reply-To: References: Message-ID: On 01Nov2021 11:30, Dennis Lee Bieber wrote: >On Mon, 1 Nov 2021 19:52:49 +1100, Cameron Simpson >declaimed the following: > >>I wouldn't use strings. If you're doing Sudoku don't you want to do >>arithmetic to check the sum? May as well make a set of ints: >> > No arithmetic done in Sudoku... You can replace the 1..9 with A..I, or >even emojis (if you can mentally keep track of such). The only thing one is >doing is eliminating candidates for a cell if that candidate was already >used on the row/column. Ah, my Sudoku inexperience is revealed. Thanks for the correction! Cheers, Cameron Simpson From PyTutor at DancesWithMice.info Mon Nov 1 18:23:43 2021 From: PyTutor at DancesWithMice.info (dn) Date: Tue, 2 Nov 2021 11:23:43 +1300 Subject: [Tutor] Creating a two dimension set In-Reply-To: References: Message-ID: <770240d9-aa5f-6d27-2d5a-4fa5dcf9f8e3@DancesWithMice.info> On 02/11/2021 10.28, Cameron Simpson wrote: > On 01Nov2021 11:30, Dennis Lee Bieber wrote: >> On Mon, 1 Nov 2021 19:52:49 +1100, Cameron Simpson >> declaimed the following: >> >>> I wouldn't use strings. If you're doing Sudoku don't you want to do >>> arithmetic to check the sum? May as well make a set of ints: >>> >> No arithmetic done in Sudoku... You can replace the 1..9 with A..I, or >> even emojis (if you can mentally keep track of such). The only thing one is >> doing is eliminating candidates for a cell if that candidate was already >> used on the row/column. > > Ah, my Sudoku inexperience is revealed. Thanks for the correction! May be, may be not! Rule: each column and each row must include the digits one to nine. Each digit appearing once and only once. Thus, re-stating the above slightly, the easiest way to verify a Sudoku's completeness (assuming the use of integers - and remembering sum() ) is to add the contents of each column and each row. If such a total is not 45, there's something wrong! NB I saw a code-competition where one ever-so keen fellow took this idea and figured that the 9x9 tableau should also sum() to a unique number. Was very proud of his 'improvement' until it was pointed-out that (if that was the only check) complementary-errors would be 'missed'... Sudoku, even the tic-tac-toe/noughts-and-crosses of my youth, is a very good problem to stretch peoples' minds regarding concepts of data-visualisation and thus data-structures. The most obvious approach is probably to figure-out how to represent the tableau with a bunch of digits arranged in rows and columns (just as on-paper). However, some solutions make very clever use of indices/indexing. In which case the 'actual data', can be a simple list() or dict(). That said, the OP's idea of comparing a column (say) as a list, with the same list coerced to a set and then len()-s compared, is a good way to detect duplicates! -- Regards, =dn From manpritsinghece at gmail.com Thu Nov 4 08:39:07 2021 From: manpritsinghece at gmail.com (Manprit Singh) Date: Thu, 4 Nov 2021 18:09:07 +0530 Subject: [Tutor] Method overloading in Python Message-ID: Dear sir, Although Method overloading can't be truly achieved in Python , But a similar effect can be made to happen - like if a method or __init__ () can behave differently in case of different types of inputs. I have written an example below to show it : In this example i have made a class Awards, My purpose of this class is to get the name of award based on category for category "A" or 1 award is "TV" for category "B" or 2 award is "AC" for category "C" or 3 award is "Fridge" The program allows user to give input of category either in string from or in integer from, means the program must give result as "TV" when the category is either A or 1 class Awards: cat1 = {"A": "TV", "B": "AC", "C": "Fridge"} cat2 = {1: "TV", 2: "AC", 3: "Fridge"} def __init__(self, cat=None): if isinstance(cat, str): self.award = self.cat1[cat] elif isinstance(cat, int): self.award = self.cat2[cat] def showaward(self): return self.award prize = Awards(1) print(prize.showaward()) # prints "TV which is right answer for category 1 prize = Awards("A") # prints "TV which is right answer for category "A print(prize.showaward()) Here I have overloaded the __init__() , as it is behaving differently in case of integer input and differently in case of string input. It will assign "TV" to self.award if the object creation is done either with 1 or "A" as argument passed during class instantiation. Is my thinking correct ? My second question is if a class can have more than one class variable, here I have put 2 class variables in the class cat1 and cat2 . Kindly comment . Kindly address any other shortcoming too . Regards Manprit Singh From alan.gauld at yahoo.co.uk Thu Nov 4 11:39:46 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 4 Nov 2021 15:39:46 +0000 Subject: [Tutor] Method overloading in Python In-Reply-To: References: Message-ID: On 04/11/2021 12:39, Manprit Singh wrote: > Dear sir, > > Although Method overloading can't be truly achieved in Python , But a > similar effect can be made to happen - like if a method or __init__ () can > behave differently in case of different types of inputs. > I have written an example below to show it : While it is possible to do that, it's usually a bad code smell. I'd question whether you might be better defining a few classmethods as factory functions? Alternatively, could you subclass the object with each subclass taking different types of input? The reason for these suggestions is that a class that needs multiple constructors is usually loading from network or data store (in which case use factory methods) or has some fundamentally different form of internal data storage which should be split into two subclasses. > In this example i have made a class Awards, My purpose of this class is to > get the name of award based on category > for category "A" or 1 award is "TV" > for category "B" or 2 award is "AC" > for category "C" or 3 award is "Fridge" So you could have a LetterAward and a StringAward? (Or just convert the category to a string and use '1','2' etc instead? > The program allows user to give input of category either in string from or > in integer from, means the program must give result as "TV" when the > category is either A or 1 > > class Awards: > cat1 = {"A": "TV", "B": "AC", "C": "Fridge"} > cat2 = {1: "TV", 2: "AC", 3: "Fridge"} > > def __init__(self, cat=None): > if isinstance(cat, str): > self.award = self.cat1[cat] > elif isinstance(cat, int): > self.award = self.cat2[cat] > > def showaward(self): > return self.award > Here I have overloaded the __init__() , as it is behaving differently in > case of integer input and differently in case of string input. It will > assign "TV" to self.award if the object creation is done either with 1 or > "A" as argument passed during class instantiation. Is my thinking correct > ? It works, but has several problems with regard to maintainability, not least being the potential of multiple if/elif options and having to keep the inputs in step - the same 3 possible inputs. If you add a new category you need to add a new input check. etc. It would probably be better in this specific case to just create the awards class with a dict. But I know this is just a toy so lets not get bogged down in example specific details. But the point is that this is often a problem with this type of overloaded initialiser. You wind up giving yourself maintenance headaches. > My second question is if a class can have more than one class variable, Sure, as many as you 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 mats at wichmann.us Thu Nov 4 12:04:21 2021 From: mats at wichmann.us (Mats Wichmann) Date: Thu, 4 Nov 2021 10:04:21 -0600 Subject: [Tutor] Method overloading in Python In-Reply-To: References: Message-ID: On 11/4/21 06:39, Manprit Singh wrote: > Dear sir, > > Although Method overloading can't be truly achieved in Python , But a > similar effect can be made to happen - like if a method or __init__ () can > behave differently in case of different types of inputs. > I have written an example below to show it : > > In this example i have made a class Awards, My purpose of this class is to > get the name of award based on category > for category "A" or 1 award is "TV" > for category "B" or 2 award is "AC" > for category "C" or 3 award is "Fridge" > > The program allows user to give input of category either in string from or > in integer from, means the program must give result as "TV" when the > category is either A or 1 > > class Awards: > cat1 = {"A": "TV", "B": "AC", "C": "Fridge"} > cat2 = {1: "TV", 2: "AC", 3: "Fridge"} > > def __init__(self, cat=None): > if isinstance(cat, str): > self.award = self.cat1[cat] > elif isinstance(cat, int): > self.award = self.cat2[cat] Notice you have a default of None, meaning you're allowing the user to call with no arguments also, but you have no case for that. On the other hand, this can have some advantages too, see below. > > def showaward(self): > return self.award > > prize = Awards(1) > print(prize.showaward()) # prints "TV which is right answer for > category 1 > > prize = Awards("A") # prints "TV which is right answer for > category "A > print(prize.showaward()) > > Here I have overloaded the __init__() , as it is behaving differently in > case of integer input and differently in case of string input. It will > assign "TV" to self.award if the object creation is done either with 1 or > "A" as argument passed during class instantiation. Is my thinking correct > ? There are a couple of ways to achieve this "overloading". One is how you've done it, which is to have the initializer handle different ways of calling. Another way is to write a factory function that fronts the class definition, fiddling the arguments into some kind of common style, or passing some additional data like the already looked up data from the catalogs, and then your init method just becomes something like: def __init__(self, award): self.award = award A another way is to provide an alternate initializer something like this: @classmethod def from_int(cls, cat): """ alternate initializer that takes an int argument """ self = cls() self.award = self.cat2[cat] That has the disadvantage that the "overloading" is user-visible, Which is also an advantage - depending on whether you think explicit is good or bad here. Yet another approach is to use the functools module, which has singledispatch and singledispatchmethod calls - singldispatchmethod would be the one for this case. I guess some people would consider that "advanced" Python so I won't go further into it here, but feel free to read up on it. > My second question is if a class can have more than one class variable, > here I have put 2 class variables in the class cat1 and cat2 . Kindly > comment . There''s no limit on number of class vaiables. From kamenijames at yahoo.com Fri Nov 5 11:29:42 2021 From: kamenijames at yahoo.com (James Kameni) Date: Fri, 5 Nov 2021 15:29:42 +0000 (UTC) Subject: [Tutor] Help for coding References: <1892921862.504576.1636126182435.ref@mail.yahoo.com> Message-ID: <1892921862.504576.1636126182435@mail.yahoo.com> Hello,i am trying to solve the order batching problem using the tabu search in Jupyter Notebook and it doesn't work as i need. Problem's definition:In manual order picking systems, order pickers walk or drive through a distribution warehouse (ref.file "Traversal.png") in order to collect items which are requested by (internal or external) customers. In order to perform these operations effciently, it is usually required that customer orders are combined into (more substantial) picking orders?(Batch)? of limited size. The Order Batching Problem deals with the question of how a given set of customer orders should be combined such that the total length of all tours is minimized which are necessary to collect all items. data: list of items and distances : "TD.txt"customers orders and quantities: "Auftrag.txt" code: order batching problem.ipynb Error:?I actually can't collect all items from orders requested by customers and put them into a batch. I only have a single solution. can you give me tipps or some help? ThanksJames -------------- next part -------------- o b 3 o e 8 o h 14 b a 5 a f 8 a g 14 f g 9 f c 2 f o 12 c d 1 c o 10 d e 2 d o 9 e h 9 e o 7 h i 4 h o 13 i g 1 i o 17 g j 13 g o 18 j o 20 -------------- next part -------------- 1 a 2 1 b 3 1 c 1 2 d 5 2 e 2 2 a 7 3 f 2 3 g 6 3 i 9 4 h 1 4 i 2 4 j 4 From PyTutor at DancesWithMice.info Fri Nov 5 15:12:45 2021 From: PyTutor at DancesWithMice.info (dn) Date: Sat, 6 Nov 2021 08:12:45 +1300 Subject: [Tutor] Help for coding In-Reply-To: <1892921862.504576.1636126182435@mail.yahoo.com> References: <1892921862.504576.1636126182435.ref@mail.yahoo.com> <1892921862.504576.1636126182435@mail.yahoo.com> Message-ID: On 06/11/2021 04.29, James Kameni via Tutor wrote: > Hello,i am trying to solve the order batching problem using the tabu search in Jupyter Notebook and it doesn't work as i need. > Problem's definition:In manual order picking systems, order pickers walk or drive through a distribution warehouse (ref.file "Traversal.png") in order to collect items which are requested by (internal or external) customers. In order to perform these operations effciently, it is usually required that customer orders are combined into (more substantial) picking orders?(Batch)? of limited size. The Order Batching Problem deals with the question of how a given set of customer orders should be combined such that the total length of all tours is minimized which are necessary to collect all items. > data: > list of items and distances : "TD.txt"customers orders and quantities: "Auftrag.txt" > code: order batching problem.ipynb > Error:?I actually can't collect all items from orders requested by customers and put them into a batch. I only have a single solution. > > can you give me tipps or some help? > ThanksJames James, it is your homework, designed to help you prove your learning. We'll be happy to help you with a Python problem. What code have you written? Please show it (or the pertinent routines) here. Then we might be able to point-out where the code requires expansion or correction... -- Regards, =dn From alan.gauld at yahoo.co.uk Sat Nov 6 07:05:03 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 6 Nov 2021 11:05:03 +0000 Subject: [Tutor] Help for coding In-Reply-To: <1892921862.504576.1636126182435@mail.yahoo.com> References: <1892921862.504576.1636126182435.ref@mail.yahoo.com> <1892921862.504576.1636126182435@mail.yahoo.com> Message-ID: On 05/11/2021 15:29, James Kameni via Tutor wrote: > Hello,i am trying to solve the order batching problem using the tabu search in Jupyter Notebook Caveat: I don't know how to solve the order batching program, I don't know what the tabu search is and I don;t use Jupyter noteboook. However.... > list of items and distances : "TD.txt"customers orders and quantities: "Auftrag.txt" You are lucky in that the list does not accept attachments - except for pure text. So these two files made it through. > code: order batching problem.ipynb But this one didn't since the server doesn't recognise the extension and even if it did wouldn't pass an executable file, > can you give me tipps or some help? Looking at your data files its not at all clear what they are! However, I'm guessing its some kind of mesh or graph definition and therefore the solution to your problem will involve investigating mathematical graph theory. To get more help here I think you'll need to send some code and explain what the data files mean. Its best if you embed those in the text of your mail rather that attaching files. For example in TD.txt, what does o b 3 o e 8 o h 14 b a 5 a f 8 a g 14 ... mean? And in Auftrag.txt what does: 1 a 2 1 b 3 1 c 1 2 d 5 2 e 2 ... mean. And how are the two related? It is not immediately obvious! -- 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 Sat Nov 6 10:53:45 2021 From: manpritsinghece at gmail.com (Manprit Singh) Date: Sat, 6 Nov 2021 20:23:45 +0530 Subject: [Tutor] Need help on regular expresions Message-ID: Dear Sir , Let's say i have to find if a string in question is of length 10, contains at least four digits, one underscore and all other 5 characters must be alphabets (upper or lowercase) How would I do it with regular expressions ? Since this problem will be very ugly with regular string methods, that's why I would like to try it with re's. The below given example only checks if the string x is made up of 10 characters and contains numbers from 0-9 an underscore and alphabets a-z and A-Z, but no count checks of numbers and alphabets and underscore . How to do it? import re x = "aG4cD1_23fg" if re.fullmatch(r'\w{10}', x): print("Match found") else: print("No match") Kindly guide Regards manprit Singh From manpritsinghece at gmail.com Sat Nov 6 11:01:17 2021 From: manpritsinghece at gmail.com (Manprit Singh) Date: Sat, 6 Nov 2021 20:31:17 +0530 Subject: [Tutor] Need help on regular expresions In-Reply-To: References: Message-ID: Dear sir , A little modification in my last mail, Dear Sir , Let's say i have to find if a string in question is of length 10, contains four digits, one underscore and all other 5 characters must be alphabets (upper or lowercase) How would I do it with regular expressions ? Since this problem will be very ugly with regular string methods, that's why I would like to try it with re's. The below given example only checks if the string x is made up of 10 characters and contains numbers from 0-9 an underscore and alphabets a-z and A-Z, but no count checks of numbers and alphabets and underscore . How to do it? import re x = "aG4cD1_23fg" if re.fullmatch(r'\w{10}', x): print("Match found") else: print("No match") Kindly guide Regards manprit Sing On Sat, Nov 6, 2021 at 8:23 PM Manprit Singh wrote: > Dear Sir , > Let's say i have to find if a string in question is of length 10, contains > at least four digits, one underscore and all other 5 characters must be > alphabets (upper or lowercase) > > How would I do it with regular expressions ? Since this problem will be > very ugly with regular string methods, that's why I would like to try it > with re's. > > The below given example only checks if the string x is made up of 10 > characters and contains numbers from 0-9 an underscore and alphabets a-z > and A-Z, but no count checks of numbers and alphabets and underscore . How > to do it? > > import re > x = "aG4cD1_23fg" > if re.fullmatch(r'\w{10}', x): > print("Match found") > else: > print("No match") > > Kindly guide > > Regards > manprit Singh > > > From Richard at Damon-Family.org Sat Nov 6 11:29:04 2021 From: Richard at Damon-Family.org (Richard Damon) Date: Sat, 6 Nov 2021 11:29:04 -0400 Subject: [Tutor] Need help on regular expresions In-Reply-To: References: Message-ID: <61f87b19-62d5-28b7-398d-860064001dd0@Damon-Family.org> My first thought is that this sounds very much like a case of having a hammer, so you want to treat something as a nail even if it isn't really one. If this is a homework problem about thinking about regular expressions, that would be one thing, but if the problem really is identifying if the string has the right character membership, regular expressions are not the right tool. Simplest would seem to be to iterate through the string, classify each character and keep a count, and then check those counts. A regular expression can test a string for the presence of at least 3 digits with a pattern something like (in english): 0 or more characters, 1 digit, 0 or more characters, 1 digit, 0 or more characters, 1 digit, 0 or more characters. Using patterns like this you can check for each category requirement with a separate expression. Getting a regular expression to check multiple parallel checks at the same times gets into much more advanced topics, and I would need to double check that python supports that level of syntax, but even if it does, unless there is some real reason it needs to be a single regular expression, it is much clearer to use multiple expressions for each requirement. On 11/6/21 11:01 AM, Manprit Singh wrote: > Dear sir , > > A little modification in my last mail, > > Dear Sir , > Let's say i have to find if a string in question is of length 10, contains > four digits, one underscore and all other 5 characters must be alphabets > (upper or lowercase) > > How would I do it with regular expressions ? Since this problem will be > very ugly with regular string methods, that's why I would like to try it > with re's. > > The below given example only checks if the string x is made up of 10 > characters and contains numbers from 0-9 an underscore and alphabets a-z > and A-Z, but no count checks of numbers and alphabets and underscore . How > to do it? > > import re > x = "aG4cD1_23fg" > if re.fullmatch(r'\w{10}', x): > print("Match found") > else: > print("No match") > > Kindly guide > > Regards > manprit Sing > > On Sat, Nov 6, 2021 at 8:23 PM Manprit Singh > wrote: > >> Dear Sir , >> Let's say i have to find if a string in question is of length 10, contains >> at least four digits, one underscore and all other 5 characters must be >> alphabets (upper or lowercase) >> >> How would I do it with regular expressions ? Since this problem will be >> very ugly with regular string methods, that's why I would like to try it >> with re's. >> >> The below given example only checks if the string x is made up of 10 >> characters and contains numbers from 0-9 an underscore and alphabets a-z >> and A-Z, but no count checks of numbers and alphabets and underscore . How >> to do it? >> >> import re >> x = "aG4cD1_23fg" >> if re.fullmatch(r'\w{10}', x): >> print("Match found") >> else: >> print("No match") >> >> Kindly guide >> >> Regards >> manprit Singh >> >> >> > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor -- Richard Damon From wlfraed at ix.netcom.com Sat Nov 6 12:09:54 2021 From: wlfraed at ix.netcom.com (Dennis Lee Bieber) Date: Sat, 06 Nov 2021 12:09:54 -0400 Subject: [Tutor] Need help on regular expresions References: Message-ID: On Sat, 6 Nov 2021 20:23:45 +0530, Manprit Singh declaimed the following: > >How would I do it with regular expressions ? Since this problem will be >very ugly with regular string methods, that's why I would like to try it >with re's. > I wouldn't... To my understanding, regex do not do COUNTING, so your first criteria is not possible. You CAN specify that a regex matches, say, four CONSECUTIVE digits (using \d{4] ), but you can't say "match four SCATTERED digits". https://docs.microsoft.com/en-us/dotnet/standard/base-types/quantifiers-in-regular-expressions It also doesn't seem that difficult using string methods... -=-=-=- TESTTEXT = "aG4cD1_23fg" TESTTEXT2 = "Ag4C1_23Fg" TESTTEXT3 = "Ag4Cd123Fg" def checkText(text, lenLimit, reqDigits, reqUnderscore, reqAlpha): if lenLimit != sum([reqDigits, reqUnderscore, reqAlpha]): raise ValueError("Sum of required characters does not match length limit") for ch in text: if ch == "_": reqUnderscore -= 1 if ch.isdecimal(): reqDigits -= 1 #or maybe .isdigit() if ch.isalpha(): reqAlpha -= 1 #or maybe .isascii() return (len(text) == lenLimit and reqDigits == 0 and reqAlpha == 0 and reqUnderscore == 0) if __name__ == "__main__": print("%s\t%s" % (TESTTEXT, checkText(TESTTEXT, 10, 4, 1, 5))) print("%s\t%s" % (TESTTEXT2, checkText(TESTTEXT2, 10, 4, 1, 5))) print("%s\t%s" % (TESTTEXT3, checkText(TESTTEXT3, 10, 4, 1, 5))) print("%s\t%s" % (TESTTEXT3, checkText(TESTTEXT3, 10, 4, 2, 5))) -=-=-=- aG4cD1_23fg False Ag4C1_23Fg True Ag4Cd123Fg False Traceback (most recent call last): File "C:\Python38\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 326, in RunScript exec(codeObject, __main__.__dict__) File "C:\Users\Wulfraed\Documents\_Hg-Repositories\Python Progs\CheckText.py", line 23, in print("%s\t%s" % (TESTTEXT3, checkText(TESTTEXT3, 10, 4, 2, 5))) File "C:\Users\Wulfraed\Documents\_Hg-Repositories\Python Progs\CheckText.py", line 8, in checkText raise ValueError("Sum of required characters does not match length limit") ValueError: Sum of required characters does not match length limit -=-=-=- -- Wulfraed Dennis Lee Bieber AF6VN wlfraed at ix.netcom.com http://wlfraed.microdiversity.freeddns.org/ From manpritsinghece at gmail.com Sat Nov 6 12:11:24 2021 From: manpritsinghece at gmail.com (Manprit Singh) Date: Sat, 6 Nov 2021 21:41:24 +0530 Subject: [Tutor] Need help on regular expresions In-Reply-To: <61f87b19-62d5-28b7-398d-860064001dd0@Damon-Family.org> References: <61f87b19-62d5-28b7-398d-860064001dd0@Damon-Family.org> Message-ID: Dear Richard, I know this can be done with string methods, I was just willing to know if this can be done using re in an easy way or not . Thanks for your mail. Regards Manprit Singh On Sat, Nov 6, 2021 at 9:00 PM Richard Damon wrote: > My first thought is that this sounds very much like a case of having a > hammer, so you want to treat something as a nail even if it isn't really > one. > > If this is a homework problem about thinking about regular expressions, > that would be one thing, but if the problem really is identifying if the > string has the right character membership, regular expressions are not > the right tool. > > Simplest would seem to be to iterate through the string, classify each > character and keep a count, and then check those counts. > > A regular expression can test a string for the presence of at least 3 > digits with a pattern something like (in english): > > 0 or more characters, 1 digit, 0 or more characters, 1 digit, 0 or more > characters, 1 digit, 0 or more characters. > > Using patterns like this you can check for each category requirement > with a separate expression. > > Getting a regular expression to check multiple parallel checks at the > same times gets into much more advanced topics, and I would need to > double check that python supports that level of syntax, but even if it > does, unless there is some real reason it needs to be a single regular > expression, it is much clearer to use multiple expressions for each > requirement. > > > On 11/6/21 11:01 AM, Manprit Singh wrote: > > Dear sir , > > > > A little modification in my last mail, > > > > Dear Sir , > > Let's say i have to find if a string in question is of length 10, > contains > > four digits, one underscore and all other 5 characters must be alphabets > > (upper or lowercase) > > > > How would I do it with regular expressions ? Since this problem will be > > very ugly with regular string methods, that's why I would like to try it > > with re's. > > > > The below given example only checks if the string x is made up of 10 > > characters and contains numbers from 0-9 an underscore and alphabets a-z > > and A-Z, but no count checks of numbers and alphabets and underscore . > How > > to do it? > > > > import re > > x = "aG4cD1_23fg" > > if re.fullmatch(r'\w{10}', x): > > print("Match found") > > else: > > print("No match") > > > > Kindly guide > > > > Regards > > manprit Sing > > > > On Sat, Nov 6, 2021 at 8:23 PM Manprit Singh > > wrote: > > > >> Dear Sir , > >> Let's say i have to find if a string in question is of length 10, > contains > >> at least four digits, one underscore and all other 5 characters must be > >> alphabets (upper or lowercase) > >> > >> How would I do it with regular expressions ? Since this problem will be > >> very ugly with regular string methods, that's why I would like to try > it > >> with re's. > >> > >> The below given example only checks if the string x is made up of 10 > >> characters and contains numbers from 0-9 an underscore and alphabets a-z > >> and A-Z, but no count checks of numbers and alphabets and underscore . > How > >> to do it? > >> > >> import re > >> x = "aG4cD1_23fg" > >> if re.fullmatch(r'\w{10}', x): > >> print("Match found") > >> else: > >> print("No match") > >> > >> Kindly guide > >> > >> Regards > >> manprit Singh > >> > >> > >> > > _______________________________________________ > > Tutor maillist - Tutor at python.org > > To unsubscribe or change subscription options: > > https://mail.python.org/mailman/listinfo/tutor > > > -- > Richard Damon > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From alan.gauld at yahoo.co.uk Sat Nov 6 18:54:11 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 6 Nov 2021 22:54:11 +0000 Subject: [Tutor] Need help on regular expresions In-Reply-To: References: Message-ID: <23A2C51A-7127-4ABF-BE50-832D85BB63FF@yahoo.co.uk> > Let's say i have to find if a string in question is of length 10, contains > four digits, one underscore and all other 5 characters must be alphabets > (upper or lowercase) > A slightly different approach: s = "1a2b3c4dE_" s2 = ''.join(sorted(s.lower())) print(s2) num,ltr = s2.split('_') print(num,ltr) if len(s) == 10 and len(num) == 4 and len(ltr) == 5: print("Success!") > How would I do it with regular expressions ? Since this problem will be > very ugly with regular string methods, Reyes looks for patterns but you do not have a pattern, just a set of count based rules. Regex is not a good solution in this case. Regex is great for complex patterns but when you try to force it into other roles it doesn?t work so well. Alan G. (Sent from my iPad coz my PC is offline?) From kaushalshriyan at gmail.com Sat Nov 6 05:41:45 2021 From: kaushalshriyan at gmail.com (Kaushal Shriyan) Date: Sat, 6 Nov 2021 15:11:45 +0530 Subject: [Tutor] ModuleNotFoundError: No module named 'mysql' in Python 3.10.0 (default, Nov 6 2021, 14:56:25) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux Message-ID: Hi, I have downloaded Python 3.10.0 on CentOS Linux release 7.9.2009 (Core). I have the below openssl and mariadb package installed on CentOS 7.9 # rpm -qa | grep openssl openssl-libs-1.0.2k-22.el7_9.x86_64 openssl-devel-1.0.2k-22.el7_9.x86_64 openssl-1.0.2k-22.el7_9.x86_64 #rpm -qa | grep mariadb mariadb-libs-5.5.68-1.el7.x86_64 mariadb-5.5.68-1.el7.x86_64 mariadb-server-5.5.68-1.el7.x86_64 mariadb-devel-5.5.68-1.el7.x86_64 # #mysql -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 2 Server version: 5.5.68-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> Bye # #wget https://www.python.org/ftp/python/3.10.0/Python-3.10.0.tgz #tar xzf Python-3.10.0.tgz #cd Python-3.10.0 #./configure --enable-optimizations #make altinstall # python3.10 Python 3.10.0 (default, Nov 6 2021, 14:56:25) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import mysql.connector Traceback (most recent call last): File "", line 1, in ModuleNotFoundError: No module named 'mysql' >>> #cat demo_mysql_test.py import mysql.connector # python3.10 demo_mysql_test.py Traceback (most recent call last): File "/root/demo_mysql_test.py", line 1, in import mysql.connector ModuleNotFoundError: No module named 'mysql' # # pip3.10 install mysql-connector-python WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available. WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/mysql-connector-python/ WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/mysql-connector-python/ WARNING: Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/mysql-connector-python/ WARNING: Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/mysql-connector-python/ WARNING: Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/mysql-connector-python/ Could not fetch URL https://pypi.org/simple/mysql-connector-python/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host=' pypi.org', port=443): Max retries exceeded with url: /simple/mysql-connector-python/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping ERROR: Could not find a version that satisfies the requirement mysql-connector-python (from versions: none) ERROR: No matching distribution found for mysql-connector-python WARNING: You are using pip version 21.2.3; however, version 21.3.1 is available. You should consider upgrading via the '/usr/local/bin/python3.10 -m pip install --upgrade pip' command. Please guide me and let me know if I am missing anything. Thanks in advance and I look forward to hearing from you. Best Regards, Kaushal From alan.gauld at yahoo.co.uk Sat Nov 6 21:08:48 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 7 Nov 2021 01:08:48 +0000 Subject: [Tutor] Need help on regular expresions In-Reply-To: References: <61f87b19-62d5-28b7-398d-860064001dd0@Damon-Family.org> Message-ID: On 06/11/2021 16:11, Manprit Singh wrote: > Dear Richard, > > I know this can be done with string methods, I was just willing to know if > this can be done using re in an easy way or not . For some definition of easy... if ((len(s) == 10) and (len(re.findall(r'\d',s)) == 4) and (len(re.findall(r'[a-zA-Z]',s)) == 5) and '_' in s): print("Success") else: print("Fail!") But I'm not sure that's any easier than using the plain string methods. -- 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 ewenjamet73 at gmail.com Sat Nov 6 20:47:35 2021 From: ewenjamet73 at gmail.com (Ewen Jamet) Date: Sat, 6 Nov 2021 20:47:35 -0400 Subject: [Tutor] Help Message-ID: Hello, Does someone can help me i face an issur with plotting on cartopy and it is really important that I achieve that tonight.. i would be really greatful Best Ewen Jamet From alan.gauld at yahoo.co.uk Sat Nov 6 21:17:49 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 7 Nov 2021 01:17:49 +0000 Subject: [Tutor] Help In-Reply-To: References: Message-ID: On 07/11/2021 00:47, Ewen Jamet wrote: > Hello, > Does someone can help me i face an issur with plotting on cartopy and it is > really important that I achieve that tonight.. i would be really greatful You are posting to an email list (email can take up to 24 hours to arrive) based all around the world - so where are you and what time is "night" where you live? And you are asking about "plotting on cartopy" which are not standard Python modules (this list is specifically for the core language and standard library). Finally, you ask for help on "an issue" but give no clue to what the issue is, nor do you show us any code, data, error messages etc. In short, you are not helping yourself to get answers, far less by "tonight" whenever that is. Should you find yourself in a similar position in the future think carefully about all of the above points before sending out a request for help and you may have more success. -- 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 ewenjamet73 at gmail.com Sat Nov 6 22:46:57 2021 From: ewenjamet73 at gmail.com (Ewen Jamet) Date: Sat, 6 Nov 2021 22:46:57 -0400 Subject: [Tutor] Fwd: Help In-Reply-To: References: <771BC361-7C05-4EBD-BE68-974A9E9926B7@me.com> Message-ID: ---------- Forwarded message --------- De : Ewen Jamet Date: sam. 6 nov. 2021 ? 21:35 Subject: Re: [Tutor] Help To: Brett Leach Hello!!! Thank you very much for answering, i am in trouble if i don't fix this.. Well the problem is: I use cartopy to plot geophysics data, and cartopy feature (coastline etc, the map basicaly) are disappearing when I plot the data, as you can see on the screenshot sent, I can not understand why.. Thank you if you take time to look at this, I would be very grateful. Best, Ewen Jamet Python 3.8.10 DISTRIB_ID=Ubuntu DISTRIB_RELEASE=20.04 DISTRIB_CODENAME=focal DISTRIB_DESCRIPTION="Ubuntu 20.04.3 LTS" NAME="Ubuntu" VERSION="20.04.3 LTS (Focal Fossa)" ID=ubuntu ID_LIKE=debian PRETTY_NAME="Ubuntu 20.04.3 LTS" VERSION_ID="20.04" HOME_URL="https://www.ubuntu.com/" SUPPORT_URL="https://help.ubuntu.com/" BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/" PRIVACY_POLICY_URL=" https://www.ubuntu.com/legal/terms-and-policies/privacy-policy" VERSION_CODENAME=focal UBUNTU_CODENAME=focal import os import subprocess import sys import numpy as np import xarray as xr import matplotlib.pyplot as plt import matplotlib.animation as animation from matplotlib.artist import Artist import time import datetime from netCDF4 import Dataset as Dataset import getpass import cartopy.crs as ccrs from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER import cartopy.feature as cfeat from matplotlib import cm import warnings warnings.filterwarnings('ignore') from function_wm import xr_opening, mapCARTOPY projection = ccrs.LambertAzimuthalEqualArea(central_latitude=90, central_longitude= -90) fig=plt.figure(figsize=(25,25)) plt.gca() cornerx=9024309 #meters, from NL.hdr cornery=cornerx newcornerx=cornerx/2. newcornery=newcornerx newextent=[-newcornerx,newcornerx,-newcornery,newcornery] ax1 = fig.add_subplot(221,projection=projection) ax1.coastlines(lw=1) #ax1.stock_img() #ax1.set_extent(newextent,projection) ax2 = fig.add_subplot(222,projection=ccrs.NorthPolarStereo()) ax2.add_feature(cfeat.COASTLINE,edgecolor='black',zorder=10) #ax2.set_extent(newextent,ccrs.NorthPolarStereo()) ax3 = fig.add_subplot(223,projection=projection) ax3.coastlines(lw=1) #ax3.set_extent(newextent,projection) ax4= fig.add_subplot(224,projection=projection) ax4.coastlines(lw=1) #def anim(i): name='ice_type_nh_ease2-250_icdr-v2p0_202110181200.nc' F1=xr_opening(name) name2=' ice_drift_mosaic_polstereo_sarbased_north_20190101000000-20190102000000.nc.nc ' F2=xr_opening(name2) name3=' ice_drift_mosaic_polstereo_sarbased_north_20190104120000-20190105120000.nc.nc ' F3=xr_opening(name3) name4='ice_type_nh_ease2-250_icdr-v2p0_202110181200.nc' F4=xr_opening(name4) F1['ice_type'].plot(ax=ax1) ax2.pcolormesh(F2.xc, F2.yc, F2.divergence[0,:,:]) #data['revelant_value'].plot(ax=ax3) F1['ice_type'].plot(ax=ax4) Le sam. 6 nov. 2021 ? 21:19, Brett Leach a ?crit : > Please post your code (inline, not as an attachment) with any errors that > occur, Python version, OS, etc. We need more information to assist you. > > Brett Leach > > > On Nov 6, 2021, at 7:47 PM, Ewen Jamet wrote: > > > > Hello, > > Does someone can help me i face an issur with plotting on cartopy and it > is > > really important that I achieve that tonight.. i would be really greatful > > Best > > Ewen Jamet > > _______________________________________________ > > Tutor maillist - Tutor at python.org > > To unsubscribe or change subscription options: > > https://mail.python.org/mailman/listinfo/tutor > > From mats at wichmann.us Sun Nov 7 09:03:24 2021 From: mats at wichmann.us (Mats Wichmann) Date: Sun, 7 Nov 2021 07:03:24 -0700 Subject: [Tutor] ModuleNotFoundError: No module named 'mysql' in Python 3.10.0 (default, Nov 6 2021, 14:56:25) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux In-Reply-To: References: Message-ID: <976f24e1-2ce9-b157-995d-ed2d26fc7e05@wichmann.us> On 11/6/21 03:41, Kaushal Shriyan wrote: > Please guide me and let me know if I am missing anything. Well, the error traceback keeps telling you what you're missing: > WARNING: pip is configured with locations that require TLS/SSL, however the > ssl module in Python is not available. > WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, > status=None)) after connection broken by 'SSLError("Can't connect to HTTPS > URL because the SSL module is not available.")': Getting ssl configured right turns out to be a bit tricky. Do you have the openssl developer package installed? You should be able to find some more stuff on the Internet on getting this set up. From mats at wichmann.us Sun Nov 7 09:54:48 2021 From: mats at wichmann.us (Mats Wichmann) Date: Sun, 7 Nov 2021 07:54:48 -0700 Subject: [Tutor] Need help on regular expresions In-Reply-To: References: Message-ID: <2575ab55-d657-ef1b-2c64-c0ce18915174@wichmann.us> On 11/6/21 10:09, Dennis Lee Bieber wrote: > On Sat, 6 Nov 2021 20:23:45 +0530, Manprit Singh > declaimed the following: > >> >> How would I do it with regular expressions ? Since this problem will be >> very ugly with regular string methods, that's why I would like to try it >> with re's. >> > I wouldn't... > > To my understanding, regex do not do COUNTING, so your first criteria > is not possible. You CAN specify that a regex matches, say, four > CONSECUTIVE digits (using \d{4] ), but you can't say "match four SCATTERED > digits". > > https://docs.microsoft.com/en-us/dotnet/standard/base-types/quantifiers-in-regular-expressions > > It also doesn't seem that difficult using string methods... > > -=-=-=- > > TESTTEXT = "aG4cD1_23fg" > TESTTEXT2 = "Ag4C1_23Fg" > TESTTEXT3 = "Ag4Cd123Fg" Just for grins, here's a slightly different take on the same, hopefully reinforcing that what first seemed ugly maybe isn't so ugly (by all means, do learn regular expressions, though, there are places they are valuable, although we keep rehasing the old saw here: "I have a problem. I know, I'll use regular expressions". "Now you have two problems!"): import sys from collections import Counter from string import ascii_letters, digits under = '_' s = "aG4cD1_23f" if len(s) != 10: print(f"String is not 10 chars long ({len(s)})") sys.exit(1) # set targets cnt = Counter(letter=5, digit=4, underscore=1) for l in s: if l in ascii_letters: cnt['letter'] -= 1 elif l in digits: cnt['digit'] -= 1 elif l in under: cnt['underscore'] -= 1 if any([cnt['underscore'], cnt['digit'], cnt['letter']]): print("Invalid string") From matt.ruffalo at gmail.com Sun Nov 7 09:13:52 2021 From: matt.ruffalo at gmail.com (Matt Ruffalo) Date: Sun, 7 Nov 2021 09:13:52 -0500 Subject: [Tutor] ModuleNotFoundError: No module named 'mysql' in Python 3.10.0 (default, Nov 6 2021, 14:56:25) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux In-Reply-To: References: Message-ID: <7b560225-2fc9-0eec-b3f1-7d61d3358b51@gmail.com> On 06/11/21 05:41, Kaushal Shriyan wrote: > Hi, > > I have downloaded Python 3.10.0 on CentOS Linux release 7.9.2009 (Core). I > have the below openssl and mariadb package installed on CentOS 7.9 > > # rpm -qa | grep openssl > openssl-libs-1.0.2k-22.el7_9.x86_64 > openssl-devel-1.0.2k-22.el7_9.x86_64 > openssl-1.0.2k-22.el7_9.x86_64 Hello- As of Python 3.10, OpenSSL 1.1.1 or newer is required: https://www.python.org/dev/peps/pep-0644/ This means that you have effectively compiled Python with no SSL/TLS support, hence pip failing to download packages. MMR... From wlfraed at ix.netcom.com Sun Nov 7 12:06:02 2021 From: wlfraed at ix.netcom.com (Dennis Lee Bieber) Date: Sun, 07 Nov 2021 12:06:02 -0500 Subject: [Tutor] Fwd: Help References: <771BC361-7C05-4EBD-BE68-974A9E9926B7@me.com> Message-ID: On Sat, 6 Nov 2021 22:46:57 -0400, Ewen Jamet declaimed the following: >data, as you can see on the screenshot sent, I can not understand why.. This is a text-only forum -- attachments (other than those that a blatantly just text) are stripped from posts. If you must use images to elucidate your problem, you will have to find some image hosting service on which you can post the image, and then include a URL to that image in your message text. Caveat: If the URL is some hashed value that does not present itself with an identifiable file-type, many of us will not click on it as it could be a link to anything. http://some.hosting.service/screen_img1.png is okay. http://some.hosting.service/aeE1nMowhAtEv13 will likely not get clicked... -- Wulfraed Dennis Lee Bieber AF6VN wlfraed at ix.netcom.com http://wlfraed.microdiversity.freeddns.org/ From phillor9 at gmail.com Sun Nov 7 23:44:32 2021 From: phillor9 at gmail.com (Phil) Date: Mon, 8 Nov 2021 14:44:32 +1000 Subject: [Tutor] Creating a two dimension set In-Reply-To: <770240d9-aa5f-6d27-2d5a-4fa5dcf9f8e3@DancesWithMice.info> References: <770240d9-aa5f-6d27-2d5a-4fa5dcf9f8e3@DancesWithMice.info> Message-ID: <6cf555da-f48d-b4f8-1c86-2c4a3d06c89a@gmail.com> I now have access to the Internet again and I thank everyone for their advice. I have now solved the error that I had with a particular Sudoku example. It turned out to be an error that only showed itself under certain conditions. Not having meaningful function names (the function names are still not ideal) and not understanding my own overly complex code that I wrote a few years ago was really the cause of my confusion and had nothing to do with the use of sets. I don't think that there is any advantage in the use of a set of candidates over a list of candidates and I think that I'll return to the list method once I get through the 100s of e-mails that I've just downloaded. If nothing else, programming keeps me occupied. -- Regards, Phil From juliushamilton100 at gmail.com Mon Nov 8 08:33:28 2021 From: juliushamilton100 at gmail.com (Julius Hamilton) Date: Mon, 8 Nov 2021 14:33:28 +0100 Subject: [Tutor] html2text expects bytes-like object Message-ID: Hey, I would like to convert a webpage to plaintext with html2text. I tried: import requests import html2text r = requests.get("https://www.google.com") html = r.content html2text.html2text(html) But this returns: TypeError: a bytes-like object is required, not 'str' In the documentation, it shows the above html2text method working for what appears to be a string. Why am I getting the response that I need to pass it a "bytes-like object"? Thanks very much, Julius From alan.gauld at yahoo.co.uk Mon Nov 8 13:31:16 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 8 Nov 2021 18:31:16 +0000 Subject: [Tutor] html2text expects bytes-like object In-Reply-To: References: Message-ID: On 08/11/2021 13:33, Julius Hamilton wrote: > html2text.html2text(html) > > But this returns: > > TypeError: a bytes-like object is required, not 'str' So try converting html to bytes: > html2text.html2text(bytes(html,encoding='utf8')) > In the documentation, it shows the above html2text method working for what > appears to be a string. Strings and bytes are easily confused visually. > Why am I getting the response that I need to pass it a "bytes-like object"? Probably because that's what it needs. A lot of low level C library functions require a bytes object rather than a {Python Unicode string. So, if the module uses such functions, it probably wants a bytes object passed to it. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From popity98 at gmail.com Wed Nov 10 22:57:52 2021 From: popity98 at gmail.com (jimmah9) Date: Wed, 10 Nov 2021 22:57:52 -0500 Subject: [Tutor] how to analyze thresholded features in an array Message-ID: I have an array of numerical values (surface pressure on a lat/lon grid). Using a constant threshold value I'd like to determine the maximum dimension of each thresholded feature in the array. I've made a mask with the thresholded value. Any guidance on how to define a feature and determine geometric properties of each feature? Thanks in advance. From alan.gauld at yahoo.co.uk Thu Nov 11 07:17:59 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 11 Nov 2021 12:17:59 +0000 Subject: [Tutor] how to analyze thresholded features in an array In-Reply-To: References: Message-ID: On 11/11/2021 03:57, jimmah9 wrote: > I have an array of numerical values (surface pressure on a lat/lon grid). > Using a constant threshold value I'd like to determine the maximum > dimension of each thresholded feature in the array. I've made a mask with > the thresholded value. Any guidance on how to define a feature and > determine geometric properties of each feature? Thanks in advance. Is there a question about Python in there somewhere? It looks like you are asking about the algorithm or math? Maybe if you gave us some idea of what your data looks like and what you are trying to do with it? Remember, this list is composed of Python programmers not domain experts in your field. You need to show us what you are trying to accomplish. For example, what does "making a mask" mean? It's not a Python thing... And what does the "maximum dimension of each thresholded feature" mean? Again, it's not Python speak. -- 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 s9814246136 at gmail.com Fri Nov 12 07:26:12 2021 From: s9814246136 at gmail.com (Sahilpreet Singh) Date: Fri, 12 Nov 2021 17:56:12 +0530 Subject: [Tutor] Negative step in Python slicing Message-ID: Hi there, I am unable to understand how the following works in Python: mylist[::k] Where mylist is any iterable and k is any constant value. It would be very helpful if you can explain with some examples. From s9814246136 at gmail.com Fri Nov 12 09:39:49 2021 From: s9814246136 at gmail.com (Sahilpreet Singh) Date: Fri, 12 Nov 2021 20:09:49 +0530 Subject: [Tutor] Negative step in Python slicing In-Reply-To: References: Message-ID: Sorry I wanted to know about this mylist[::-k] On Fri, Nov 12, 2021, 17:56 Sahilpreet Singh wrote: > Hi there, > I am unable to understand how the following works in Python: > mylist[::k] > Where mylist is any iterable and k is any constant value. > It would be very helpful if you can explain with some examples. > From alan.gauld at yahoo.co.uk Fri Nov 12 09:50:01 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 12 Nov 2021 14:50:01 +0000 Subject: [Tutor] Negative step in Python slicing In-Reply-To: References: Message-ID: On 12/11/2021 12:26, Sahilpreet Singh wrote: > I am unable to understand how the following works in Python: > mylist[::k] > Where mylist is any iterable and k is any constant value. Slicing consists of 3 values: mylist[start:end:step] where start defines the first position, end the last position and step the number of intermediate entries In addition it helps to think of the slice operating with a "knife blade" inserted to the left of the start/end values Thus mylist[0:10:1] extracts every element(step=1) from the start at 0 to the end at 10. ie elements 0 through 9 given the "knife" rule above. Each of these parameters has a default value: start = 0, end = len(mylist), step = 1 So we can define mylist[::] and get a copy of mylist. We can also miss out the second colon for the same result. The step value states how many elements in the original sequence must be stepped over for each element in the slice. Thus 2 returns every second element, 3 every third etc. We can also use negative value to indicate direction of traversal. Thus -1 indicates an index of the last element, or a reverse direction of step Thus mylist[::-1] returns a reversed list And mylist[:5:-1] returns the reverse of the list as far as the 5th element (using the knife rule the index is on the right side for a reverse traversal) Thus turning to your example mylist[::k] is the same as: mylist[0:len(mylist):k] Lets look at some real examples: >>> s = list(range(10)) The original list >>> s[::] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> s[:] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Start at 3 and get every second item >>> s[3::2] [3, 5, 7, 9] Reverse the list >>> s[::-1] [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] Start at position 3 but go backwards >>> s[3::-1] [3, 2, 1, 0] End at position 6 >>> s[:6:-1] [9, 8, 7] I hope that helps, carry on trying things out yourself for other cases. slicing is very powerful and flexible but not always intuitive. -- 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 mlb121980 at gmail.com Fri Nov 12 16:40:46 2021 From: mlb121980 at gmail.com (matt) Date: Fri, 12 Nov 2021 15:40:46 -0600 Subject: [Tutor] ValueError: invalid literal for int() with base 10: '' is outcome in line 89--statSet.addNumber(int(n)) Message-ID: <51D86F03-8568-4FC1-85D5-8ECA30405F6C@hxcore.ol> My code is ( i did try to give (int(float(n))) but it didn?t work either): ? #statistics calc from math import sqrt ? #def stat set funk. class StatSet: ??? def __init__(self): ??????? self.data = 0 ? #def get data ??? def getData(self): ??????? return self.data ? #def add num. ??? def addNumber(self,x): ??????? if isinstance(x,(int,float)): ??????????? data = self.data ??????????? data.append(x) ??????????? self.data = data ??????????? return self #def mean funk. ??? def mean(self): ??????? nums = self.data ? #check (if) cond. ??????? if len(nums)>0: ???? ???????return sum(nums)/len(nums) ??????? else: ??????????? return 0 ? #def med funk. ??? def median(self): ??????? nums = self.data ??????? nums.sort() ??????? length = len(nums) ??????? remainder = length%2 ??????? med = 0 ? #if to check length ??????? if length == 0: ??????????? return Non ??????? elif remainder ==0: ??????????? med = (nums[length//2] + nums[length//2-1])/2 ??????? else: ??????????? med = nums[length//2] ??????? return med ? #def funk. stdDev ??? def stdDev(self): ??????? data = self.data ??????? N = self.count() ??????? mean = self.mean() ??????? devSum = 0 ? ??????? for i in range(N): ??????????? devSum = devSum + (data[i] - mean)**2 ??????? return sqrt(devSum) ? #def count funk. ??? def count(self): ??????? count = len(self.data) ??????? return count ? # def min funk. ??? def min(self): ??????? nums = self.data ??????? nums.sort() ??????? minNum = nums[0] ??????? return minNum ? #def max funk. ??? def max(self): ??????? nums = self.data ??????? nums.sort() ??????? maxNum = nums[-1] ??????? return maxNum ? #main prog. def main(): ??????? print("Program test of StatSet class") ? #class constructor ??????? statSet = StatSet() ??????? while True: ??????????? n = input("Enter a number (to quit): ") ??????????? if n =="": ??????????????? break #call addnumber funk. ??????? statSet.addNumber(int(n)) ????????????????????????? ????????print("Data: ", statSet.getData()) ??????? print("Mean: ", statSet.mean()) ??????? print("Median: ", statSet.median()) ??????? print("Standard deviation: ", statSet.stdDev()) ??????? print("Min: ", statSet.min()) ??????? print("Max: ", statSet.max()) ??????? print("Data: ", statSet.getData()) ??????? print("Count: ", statSet.count()) ? ??????? if __name__ == '__main__': ??????????? main() main() ? ? when I enter values I get an error message at the end for line 89 ?statSet.addNumber(int(n))? Sent from [1]Mail for Windows ? References Visible links 1. https://go.microsoft.com/fwlink/?LinkId=550986 From phillor9 at gmail.com Sat Nov 13 00:04:43 2021 From: phillor9 at gmail.com (Phil) Date: Sat, 13 Nov 2021 15:04:43 +1000 Subject: [Tutor] A dictionary question Message-ID: <6aea6665-372a-55b0-a69e-34f10bcb54a4@gmail.com> Thank you for taking the time to look at this. I'd like to find the number of pairs in the rows list of sets. The pair sets are {7, 3} and {4, 6} because they appear twice each. I'd also like to have count_dict dictionary show the two rows where the sets were found. What I have is this: {'row': 5, 'count': 2, 'set': {4, 6}} Which shows the last row, the count and the set found. I'd like the dictionary to also show where the other set {4, 6} was found which is row 1. This is my best effort so far. I also tried counter from the collections module but I doesn't seem to work with sets, at least I couldn't get it to work with sets. rows = [{7,3},{4,6,8},{7,8},{9,3},{7,3},{4,6,3}] set_list = [{1, 2}, {1, 3}, {1, 4}, {1, 5}, {1, 6}, {1, 7}, {1, 8}, {1, 9}, ??????????? {2, 3}, {2, 4}, {2, 5}, {2, 6}, {2, 7}, {2, 8}, {2, 9}, ??????????? {3, 4}, {3, 5}, {3, 6}, {3, 7}, {3, 8}, {3, 9}, ??????????? {4, 5}, {4, 6}, {4, 7}, {4, 8}, {4,9}, ??????????? {5, 6}, {5, 7}, {5, 8}, {5, 9}, ??????????? {6, 7}, {6, 8}, {6, 9}, ??????????? {7, 8}, {7, 9}, ??????????? {8, 9} ??????????? ] count_dict = {} for i in range(len(set_list)): ??? count = 0 ??? for j in range(6): ??????? if set_list[i].issubset(rows[j]): ??????????? found_set = set_list[i] ??????????? count += 1 ??????????? count_dict['row'] = j ??????????? count_dict['count'] = count ??????????? count_dict['set'] = found_set ??? if count == 2: ??????? print(set_list[i], ' is in row ',count, ' times') ??????? print(count_dict) Thank you again. -- Regards, Phil From PyTutor at DancesWithMice.info Sat Nov 13 00:45:26 2021 From: PyTutor at DancesWithMice.info (dn) Date: Sat, 13 Nov 2021 18:45:26 +1300 Subject: [Tutor] A dictionary question In-Reply-To: <6aea6665-372a-55b0-a69e-34f10bcb54a4@gmail.com> References: <6aea6665-372a-55b0-a69e-34f10bcb54a4@gmail.com> Message-ID: <1faf831f-e2b8-7035-adc2-f59374486166@DancesWithMice.info> On 13/11/2021 18.04, Phil wrote: > Thank you for taking the time to look at this. > > I'd like to find the number of pairs in the rows list of sets. The pair > sets are {7, 3} and {4, 6} because they appear twice each. I'd also like > to have count_dict dictionary show the two rows where the sets were > found. What I have is this: > > {'row': 5, 'count': 2, 'set': {4, 6}} > > Which shows the last row, the count and the set found. I'd like the > dictionary to also show where the other set {4, 6} was found which is > row 1. ... > for i in range(len(set_list)): > ??? count = 0 > ??? for j in range(6): > ??????? if set_list[i].issubset(rows[j]): > ??????????? found_set = set_list[i] > ??????????? count += 1 > ??????????? count_dict['row'] = j > ??????????? count_dict['count'] = count > ??????????? count_dict['set'] = found_set > > ??? if count == 2: > ??????? print(set_list[i], ' is in row ',count, ' times') > ??????? print(count_dict) What a ghastly exercise. Is it homework? Note that "count_dict['row'] = j" only allows for one set-location to be noted. Accordingly, change the value of the dictionary to be a list. Sometimes the list will only be one item long - which seems like a waste. However, when the counter goes to 2 (or more), then the list will contain multiple items. Declare the list at the same time as the count is reset. Append the set-location to the list whenever a 'hit' is noted. The last print() will then look something like: {'row': [1, 5], 'count': 2, 'set': {4, 6}} Once you've got that working (and not before - "make it work before you make it better!", you'll realise that the counter is unnecessary because the length of the list yields the same information! -- Regards, =dn From phillor9 at gmail.com Sat Nov 13 02:14:36 2021 From: phillor9 at gmail.com (Phil) Date: Sat, 13 Nov 2021 17:14:36 +1000 Subject: [Tutor] A dictionary question In-Reply-To: <1faf831f-e2b8-7035-adc2-f59374486166@DancesWithMice.info> References: <6aea6665-372a-55b0-a69e-34f10bcb54a4@gmail.com> <1faf831f-e2b8-7035-adc2-f59374486166@DancesWithMice.info> Message-ID: On 13/11/21 15:45, dn via Tutor wrote: > What a ghastly exercise. Is it homework? No, just a project to keep myself occupied. There must be easier way to find the two pairs. > > Note that "count_dict['row'] = j" only allows for one set-location to be > noted. Accordingly, change the value of the dictionary to be a list. Thank you dn, I didn't think of that in my frazzled state. > The last print() will then look something like: > > {'row': [1, 5], 'count': 2, 'set': {4, 6}} Indeed it does. Thank you again. -- Regards, Phil From alan.gauld at yahoo.co.uk Sat Nov 13 06:04:19 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 13 Nov 2021 11:04:19 +0000 Subject: [Tutor] ValueError: invalid literal for int() with base 10: '' is outcome in line 89--statSet.addNumber(int(n)) In-Reply-To: <51D86F03-8568-4FC1-85D5-8ECA30405F6C@hxcore.ol> References: <51D86F03-8568-4FC1-85D5-8ECA30405F6C@hxcore.ol> Message-ID: On 12/11/2021 21:40, matt wrote: > My code is ( i did try to give (int(float(n))) but it didn?t work either): Always paste the entire error message not just the summary line. There is a lot of information contained in the error but we can't see it if you don't paste the whole thing. > #statistics calc BTW You did notice that there is a statistics module in the standard library that does all the things you are trying to do here? You could study the code for that to see how they do it. > from math import sqrt > class StatSet: > > ??? def __init__(self): > ??????? self.data = 0 > > ??? def getData(self): > ??????? return self.data In Python we don't usually create setters/getters, there is no need. > ??? def addNumber(self,x): > > ??????? if isinstance(x,(int,float)): > ??????????? data = self.data > ??????????? data.append(x) > ??????????? self.data = data This makes no sense. self.data is a number, 0 by default. You cannot append() to a number. You would need to create self.data as a list in the __init__() > ??? def mean(self): > ??????? nums = self.data > #check (if) cond. Your comments are just replicating the code, they don't add anything of value and make the code more cluttered. Use comments to explain *why* the code does things, not *what* the code does. > ??????? if len(nums)>0: > ???? ???????return sum(nums)/len(nums) > ??????? else: > ??????????? return 0 That might result in confusing or even erroneous results. It would be better to raise an exception than pass back a wrong answer! > ??? def median(self): > ??????? nums = self.data > ??????? nums.sort() > ??????? length = len(nums) > ??????? remainder = length%2 > ??????? med = 0 > ??????? if length == 0: > ??????????? return Non > ??????? elif remainder ==0: > ??????????? med = (nums[length//2] + nums[length//2-1])/2 > ??????? else: > ??????????? med = nums[length//2] > ??????? return med > ??? def stdDev(self): > ??????? data = self.data > ??????? N = self.count() > ??????? mean = self.mean() > ??????? devSum = 0 > ??????? for i in range(N): > ??????????? devSum = devSum + (data[i] - mean)**2 > ??????? return sqrt(devSum) > ??? def count(self): > ??????? count = len(self.data) > ??????? return count > ??? def min(self): > ??????? nums = self.data > ??????? nums.sort() > ??????? minNum = nums[0] > ??????? return minNum > ??? def max(self): > ??????? nums = self.data > ??????? nums.sort() > ??????? maxNum = nums[-1] > ??????? return maxNum You realize there are builtin functions min() and max() for getting the min and max of a sequence? def main() > ??????? statSet = StatSet() > ??????? while True: > ??????????? n = input("Enter a number (to quit): ") > ??????????? if n =="": > ??????????????? break > ??????? statSet.addNumber(int(n)) Note that the addNumber() is outside the loop so you only ever add one number. The subject line would suggest this is probably where the error occurs but without seeing the error trace (and ideally the data entered!) its hard to be sure. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From learn2program at gmail.com Sat Nov 13 06:10:36 2021 From: learn2program at gmail.com (Alan Gauld) Date: Sat, 13 Nov 2021 11:10:36 +0000 Subject: [Tutor] Negative step in Python slicing In-Reply-To: References: Message-ID: Always use Reply-All or Rely-List when responding to tutor emails. On 13/11/2021 01:13, Sahilpreet Singh wrote: > Thanks Alan? > But can you explain some cases where step is like -2 or -3. It is exactly like -1 except it steps by the number shown. In the same way as 2 or 3 step forward. The easiest way is to try it in the interpreter: >>> s = [1,2,3,4,5,6,7,8,9] >>> s[::-2]? #every second item in reverse... [9, 7, 5, 3, 1] >>> s[::-3]? #every third item in reverse... [9, 6, 3] >>> As I said... > Slicing consists of 3 values: > > mylist[start:end:step] > > where > start defines the first position, > end the last position and > step the number of intermediate entries > > ... > > Each of these parameters has a default value: > start = 0, end = len(mylist), step = 1 > > ... > The step value states how many elements in the original > sequence must be stepped over for each element in the > slice. Thus 2 returns every second element, 3 every third etc. > > We can also use negative value to indicate direction > of traversal. Thus -1 indicates an index of the last element, > or a reverse direction of step > > Thus mylist[::-1] > > returns a reversed list > > >>> s[:] > [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] > > Start at 3 and get every second item > >>> s[3::2] > [3, 5, 7, 9] > > Reverse the list > >>> s[::-1] > [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] > > -- > 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 s9814246136 at gmail.com Sat Nov 13 09:06:58 2021 From: s9814246136 at gmail.com (Sahilpreet Singh) Date: Sat, 13 Nov 2021 19:36:58 +0530 Subject: [Tutor] Negative step in Python slicing In-Reply-To: References: Message-ID: Thanks for your help.This community is best. On Sat, Nov 13, 2021, 16:40 Alan Gauld wrote: > Always use Reply-All or Rely-List when responding to tutor emails. > > On 13/11/2021 01:13, Sahilpreet Singh wrote: > > Thanks Alan > > But can you explain some cases where step is like -2 or -3. > > It is exactly like -1 except it steps by the number shown. > In the same way as 2 or 3 step forward. > > The easiest way is to try it in the interpreter: > > >>> s = [1,2,3,4,5,6,7,8,9] > >>> s[::-2] #every second item in reverse... > [9, 7, 5, 3, 1] > >>> s[::-3] #every third item in reverse... > [9, 6, 3] > >>> > > As I said... > > > > Slicing consists of 3 values: > > > > mylist[start:end:step] > > > > where > > start defines the first position, > > end the last position and > > step the number of intermediate entries > > > > ... > > > > > Each of these parameters has a default value: > > start = 0, end = len(mylist), step = 1 > > > > > ... > > > > The step value states how many elements in the original > > sequence must be stepped over for each element in the > > slice. Thus 2 returns every second element, 3 every third etc. > > > > We can also use negative value to indicate direction > > of traversal. Thus -1 indicates an index of the last element, > > or a reverse direction of step > > > > Thus mylist[::-1] > > > > returns a reversed list > > > > >>> s[:] > > [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] > > > > Start at 3 and get every second item > > >>> s[3::2] > > [3, 5, 7, 9] > > > > Reverse the list > > >>> s[::-1] > > [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] > > > > -- > > > 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 coskunarslan at yahoo.com Sat Nov 13 09:43:59 2021 From: coskunarslan at yahoo.com (coskun arslan) Date: Sat, 13 Nov 2021 14:43:59 +0000 (UTC) Subject: [Tutor] prime numbers References: <1604045709.1384989.1636814639752.ref@mail.yahoo.com> Message-ID: <1604045709.1384989.1636814639752@mail.yahoo.com> Want to list primes to 20.Where is mistake?? my_list=[i for i in range(2,21)]for i in my_list:? ? ? ?for k in range(2,i):? ? ? ? ? ? ? if i%k==0:? ? ? ? ? ? ? ? ? my_list.remove(i)print(my_list) It gives eror List.remove(i) i is not in list.What is wronge?? ? ? ? ? Android?de Yahoo Postadan g?nderildi From alan.gauld at yahoo.co.uk Sat Nov 13 19:16:44 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 14 Nov 2021 00:16:44 +0000 Subject: [Tutor] prime numbers In-Reply-To: <1604045709.1384989.1636814639752@mail.yahoo.com> References: <1604045709.1384989.1636814639752.ref@mail.yahoo.com> <1604045709.1384989.1636814639752@mail.yahoo.com> Message-ID: On 13/11/2021 14:43, coskun arslan via Tutor wrote: Please always post code in plain text, the HTML text gets messed up, and formatting is very important in Python. In this case I'll try to reformat it as you probably had it... my_list=[i for i in range(2,21)] for i in my_list: ? ? ? ?for k in range(2,i): ? ? ? ? ? ? ? if i%k==0: ? ? ? ? ? ? ? ? ? my_list.remove(i) print(my_list) > It gives eror List.remove(i) i is not in list.What is wronge?? ? ? ? ? Please always post the full error text not a summary, there is a lot of useful information contained in the error text. But in this case you are iterating over my list while at the same time removing elements of the list. That's a bit like cutting down the branch of the tree that you are sitting on. Python goes looking for the item at position 6, say, but in the meantime you have deleted items 3 and 4 so item 6 is no longer at position 6. Python gets confused! To fix that you can iterate over a copy of the list (mylist[:] is an easy way to create a copy) and remove from the original list. By the way, there are much more efficient algorithms for finding prime numbers. For example: Think about how high you need to go to find the factors. Take 24 as an example: (1,24),(2,12),(3,8),(4,6),(6,4), ... but wait, 6,4 is the same pair as 4,6. We are repeating ourselves. So we only need to test up to the square root of the target, int(24**0.5) => 4. That saves a bunch of tests. But there are even more powerful algorithms you can use. Try a google search... -- 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 Mon Nov 15 02:30:40 2021 From: phillor9 at gmail.com (Phil) Date: Mon, 15 Nov 2021 17:30:40 +1000 Subject: [Tutor] A dictionary question In-Reply-To: <1faf831f-e2b8-7035-adc2-f59374486166@DancesWithMice.info> References: <6aea6665-372a-55b0-a69e-34f10bcb54a4@gmail.com> <1faf831f-e2b8-7035-adc2-f59374486166@DancesWithMice.info> Message-ID: Thank you once again for taking the time to look at this. I've refined the requirements from my previous question. What I need is to check for the existence of a pair of sets from the set_list in the row list of sets twice and not in any other set. {4,6} in the first row list of sets fulfills the requirement and there are no sets in the second row list of sets that meets this requirement. The code that follows is the basis of what I've been working on. The print statements show what's needed but I cannot think of a clever way to code this requirement. I've experimented with dictionaries but I've come to the conclusion that a boolean test is what's needed to weed out the sets that don't meet the requirements. Any code snippets or ideas will be greatly appreciated. #row = [{7,3},{5},{4,8,6},{7,8},{1},{9,3},{7,9},{4,6,3},{2}] row = [{5},{6},{3},{4,7,5},{1,2,4,5},{1,7,5},{1,2,4,7,9},{8},{1,2,4,7,9}] set_list = [{1, 2}, {1, 3}, {1, 4}, {1, 5}, {1, 6}, {1, 7}, {1, 8}, {1, 9}, ??????????? {2, 3}, {2, 4}, {2, 5}, {2, 6}, {2, 7}, {2, 8}, {2, 9}, ??????????? {3, 4}, {3, 5}, {3, 6}, {3, 7}, {3, 8}, {3, 9}, ??????????? {4, 5}, {4, 6}, {4, 7}, {4, 8}, {4,9}, ??????????? {5, 6}, {5, 7}, {5, 8}, {5, 9}, ??????????? {6, 7}, {6, 8}, {6, 9}, ??????????? {7, 8}, {7, 9}, ??????????? {8, 9} ??????????? ] count_dict = {} for i in range(len(set_list)): ??? count = 0 ??? row_list = [] ??? for j in range(9): ??????? if set_list[i].issubset(row[j]) and len(row[j]) > 2: ??????????? row_list.append(j) ??????????? count += 1 ??????????? count_dict['row'] = row_list ??????????? count_dict['count'] = count ??????????? count_dict['set'] = set_list[i] ??????? print(set_list[i], ' ', row_list) ??????? print(count_dict) -- Regards, Phil From phillor9 at gmail.com Mon Nov 15 03:00:48 2021 From: phillor9 at gmail.com (Phil) Date: Mon, 15 Nov 2021 18:00:48 +1000 Subject: [Tutor] A dictionary question In-Reply-To: <1faf831f-e2b8-7035-adc2-f59374486166@DancesWithMice.info> References: <6aea6665-372a-55b0-a69e-34f10bcb54a4@gmail.com> <1faf831f-e2b8-7035-adc2-f59374486166@DancesWithMice.info> Message-ID: I think I have to start from scratch. The same pair of numbers has to exist twice in the row set plus each number (not a pair set) cannot exist in any other row set. That being the case, I now have to rethink the idea of using a pair list lookup table which is cumbersome and tedious to debug. -- Regards, Phil From abba6 at mac.com Mon Nov 15 05:05:45 2021 From: abba6 at mac.com (=?utf-8?Q?Asbj=C3=B8rn_Bakken?=) Date: Mon, 15 Nov 2021 11:05:45 +0100 Subject: [Tutor] First try Message-ID: Hi there, I am brand-new to python. In my younger days I did some programming in Fortran and Cobolt (ages ago) but it is all lost. So I started out with Python for Kids for dummies.It worked nice on my macpro untill I upgraded to the latest IOS 12.0.1. Useing 2.7 (continued to work) but IDLE went in black (useless). All because 2.7 is no longer supported? Anything I can do or must I jump to latest version of Python? ASbak From vinay.g_ece2019 at svce.edu.in Mon Nov 15 13:01:07 2021 From: vinay.g_ece2019 at svce.edu.in (481 Vinay) Date: Mon, 15 Nov 2021 23:31:07 +0530 Subject: [Tutor] Tutor Digest, Vol 213, Issue 17 In-Reply-To: References: Message-ID: Code for this one On Sun, Nov 14, 2021, 10:31 PM wrote: > Send Tutor mailing list submissions to > tutor at python.org > > To subscribe or unsubscribe via the World Wide Web, visit > https://mail.python.org/mailman/listinfo/tutor > or, via email, send a message with subject or body 'help' to > tutor-request at python.org > > You can reach the person managing the list at > tutor-owner at python.org > > When replying, please edit your Subject line so it is more specific > than "Re: Contents of Tutor digest..." > Today's Topics: > > 1. prime numbers (coskun arslan) > 2. Re: prime numbers (Alan Gauld) > > > > ---------- Forwarded message ---------- > From: coskun arslan > To: "tutor at python.org" > Cc: > Bcc: > Date: Sat, 13 Nov 2021 14:43:59 +0000 (UTC) > Subject: [Tutor] prime numbers > Want to list primes to 20.Where is mistake? > my_list=[i for i in range(2,21)]for i in my_list: for k in > range(2,i): if i%k==0: > my_list.remove(i)print(my_list) > It gives eror List.remove(i) i is not in list.What is wronge? > > Android?de Yahoo Postadan g?nderildi > > > > ---------- Forwarded message ---------- > From: Alan Gauld > To: tutor at python.org > Cc: > Bcc: > Date: Sun, 14 Nov 2021 00:16:44 +0000 > Subject: Re: [Tutor] prime numbers > On 13/11/2021 14:43, coskun arslan via Tutor wrote: > Please always post code in plain text, the HTML text gets messed > up, and formatting is very important in Python. > > In this case I'll try to reformat it as you probably had it... > > > my_list=[i for i in range(2,21)] > for i in my_list: > for k in range(2,i): > if i%k==0: > my_list.remove(i) > print(my_list) > > > It gives eror List.remove(i) i is not in list.What is wronge? > > Please always post the full error text not a summary, > there is a lot of useful information contained in the > error text. > > But in this case you are iterating over my list while at the > same time removing elements of the list. That's a bit like > cutting down the branch of the tree that you are sitting on. > > Python goes looking for the item at position 6, say, but > in the meantime you have deleted items 3 and 4 so item 6 > is no longer at position 6. Python gets confused! > To fix that you can iterate over a copy of the list > (mylist[:] is an easy way to create a copy) and remove > from the original list. > > By the way, there are much more efficient algorithms for > finding prime numbers. For example: > Think about how high you need to go to find the factors. > Take 24 as an example: > (1,24),(2,12),(3,8),(4,6),(6,4), ... > but wait, 6,4 is the same pair as 4,6. > We are repeating ourselves. So we only need to test > up to the square root of the target, int(24**0.5) => 4. > > That saves a bunch of tests. > > But there are even more powerful algorithms you can use. > Try a google search... > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.amazon.com/author/alan_gauld > Follow my photo-blog on Flickr at: > http://www.flickr.com/photos/alangauldphotos > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > https://mail.python.org/mailman/listinfo/tutor > From alan.gauld at yahoo.co.uk Mon Nov 15 14:02:32 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 15 Nov 2021 19:02:32 +0000 Subject: [Tutor] First try In-Reply-To: References: Message-ID: On 15/11/2021 10:05, Asbj?rn Bakken via Tutor wrote: > I started out with Python for Kids for dummies.It worked nice on my macpro I don;t use Python on a mac but... > untill I upgraded to the latest IOS 12.0.1. > Useing 2.7 (continued to work) but IDLE went in black (useless). 2.7 is indeed out of support but it should still work. Can you try it in the terminal app and see if it works there? It may be a Tcl/Tk issue which is causing IDLE to fail. (IDLE is built using tkinter which, in turn, uses Tcl/Tk) > Anything I can do or must I jump to latest version of Python? It should work, but I'd strongly encourage you to jump to Python 3. Your tutorial might not work of course but there are many, many, online tutorials(including mine!) based on Python 3. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From mats at wichmann.us Mon Nov 15 14:06:51 2021 From: mats at wichmann.us (Mats Wichmann) Date: Mon, 15 Nov 2021 12:06:51 -0700 Subject: [Tutor] A dictionary question In-Reply-To: References: <6aea6665-372a-55b0-a69e-34f10bcb54a4@gmail.com> <1faf831f-e2b8-7035-adc2-f59374486166@DancesWithMice.info> Message-ID: On 11/15/21 01:00, Phil wrote: > I think I have to start from scratch. > > The same pair of numbers has to exist twice in the row set plus each > number (not a pair set) cannot exist in any other row set. That being > the case, I now have to rethink the idea of using a pair list lookup > table which is cumbersome and tedious to debug. > Doesn't sound too bad. Count how many times the pairs appear in the various elements of your rowlist. I'd probably do something like this: from collections import Counter pairs = Counter() for s in set_list: for r in row: if s.issubset(r): pairs[tuple(sorted(s))] += 1 since sets are unordered I would sort to get a reproducible pair, then have to convert to tuple so it's hashable and able to serve as a dictionary key. Now walk through the counts, finding candidates - namely pairs with counts of 2 (or whatever number you pick), and do a quick pass counting up how many times each item of the pair appears in the rowlist elements. If those counts aren't also two, your candidate is eliminated. Remembering that Counter is a dict, you can start this loop like: for item, count in pairs.items(): does this help? From alan.gauld at yahoo.co.uk Mon Nov 15 14:09:46 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 15 Nov 2021 19:09:46 +0000 Subject: [Tutor] Tutor Digest, Vol 213, Issue 17 In-Reply-To: References: Message-ID: On 15/11/2021 18:01, 481 Vinay wrote: > Code for this one I'm not sure what you mean by that? But please don't repost the entire digest (only two messages here but often much more). Many people pay for their internet by the byte so sending stuff we have all seen already is not helpful. Select the parts that are relevant to your message and send those for context. Alan G. Moderator From mats at wichmann.us Mon Nov 15 14:14:23 2021 From: mats at wichmann.us (Mats Wichmann) Date: Mon, 15 Nov 2021 12:14:23 -0700 Subject: [Tutor] First try In-Reply-To: References: Message-ID: On 11/15/21 03:05, Asbj?rn Bakken via Tutor wrote: > Hi there, > > I am brand-new to python. In my younger days I did some programming in Fortran and Cobolt (ages ago) but it is all lost. So I started out with Python for Kids for dummies.It worked nice on my macpro untill I upgraded to the latest IOS 12.0.1. Useing 2.7 (continued to work) but IDLE went in black (useless). > > All because 2.7 is no longer supported? > > Anything I can do or must I jump to latest version of Python? don't use the built-in Python on macOS. There are several ways to get a usable Python3 onto the Mac, not least of which is grabbing an installer from python.org. As Alan surmised, there was indeed a problem that broke tk, which breaks the binding tkinter, which IDLE is built on. There's a lengthy thread on the Python bugtracker and elsewhere on this, and I believe new installers were issued which let IDLE work on 12.x - I think it's a workaround for now, until tk itself gets fixed - but the tk folks can't fix the one that's pre-installed on macOS, just the version that will be bundled with new Python installers. some notes here: https://blog.python.org/2021/11/ and the general page may be useful too: https://docs.python.org/3/using/mac.html From phillor9 at gmail.com Mon Nov 15 15:57:32 2021 From: phillor9 at gmail.com (Phil) Date: Tue, 16 Nov 2021 06:57:32 +1000 Subject: [Tutor] A dictionary question In-Reply-To: References: <6aea6665-372a-55b0-a69e-34f10bcb54a4@gmail.com> <1faf831f-e2b8-7035-adc2-f59374486166@DancesWithMice.info> Message-ID: On 16/11/21 05:06, Mats Wichmann wrote: > > On 11/15/21 01:00, Phil wrote: >> I think I have to start from scratch. >> >> The same pair of numbers has to exist twice in the row set plus each >> number (not a pair set) cannot exist in any other row set. That being >> the case, I now have to rethink the idea of using a pair list lookup >> table which is cumbersome and tedious to debug. >> > > Doesn't sound too bad. > > Count how many times the pairs appear in the various elements of your > rowlist. I'd probably do something like this: Thank you Mats, To find a solution to a problem, a university lecturer was fond of telling his first year students to explain the problem to their dog if they didn't have anyone else to talk to. This list is my dog and I don't always post my questions. Anyway, I had a bright idea (after I posted my question) that I could use a list of dictionaries and make two passes through the row list; once to search for pairs and a second time to eliminate those that have members that also appear in another set. I had experimented with counter from the collections module but ran into problems with counting sets. I knew that it worked with tuples but it didn't occur to me to convert the set to a tuple. I'm about to move on from this campsite and I won't have Internet access for three or four days, however, I expect to make some progress with this project thanks to your reply. -- Regards, Phil From juliushamilton100 at gmail.com Mon Nov 15 14:02:56 2021 From: juliushamilton100 at gmail.com (Julius Hamilton) Date: Mon, 15 Nov 2021 20:02:56 +0100 Subject: [Tutor] A simple sentence reader with pre-established libraries Message-ID: Hey, I would like to try to make a simple sentence-by-sentence reader, using as many pre-established software libraries as possible, to keep the code very simple. First, I get the html and extract the plaintext. I can do this with wget or python requests, then in theory html2text. However, I have found html2text sometimes breaks sentences into different lines, like how this sentence I am writing is broken. I?m not sure if this is a bug or if there?s some option I don?t know about. However, I have a Beautiful Soup method to do this as well: https://stackoverflow.com/questions/69680184/how-do-i-retrieve-the-text-of-a-webpage-without-sentences-being-broken-by-newlin Then, I just wanted to segment the text on sentences. I?ll probably use Spacy, since it seems to be the most modern, either rule-based or with AI. But I also have used NLTK in the past. At that point, I just want to create the simplest command line application which shows the list one element at a time, with simple navigation options like backwards and forwards and quit, and maybe save progress. Here?s the essential question: instead of writing this myself, what are the chances that there is yet again some great pre-existing tool out there that I could make use of? Is there some software library where with one line of code I could ?read? a list, or a plaintext document, at the command line, sentence by sentence, or element by element, with or without some navigation functionalities? Thanks very much, Julius From cs at cskk.id.au Mon Nov 15 18:09:42 2021 From: cs at cskk.id.au (Cameron Simpson) Date: Tue, 16 Nov 2021 10:09:42 +1100 Subject: [Tutor] First try In-Reply-To: References: Message-ID: On 15Nov2021 12:14, Mats Wichmann wrote: >On 11/15/21 03:05, Asbj?rn Bakken via Tutor wrote: >>Hi there, >> >>I am brand-new to python. In my younger days I did some programming in Fortran and Cobolt (ages ago) but it is all lost. So I started out with Python for Kids for dummies.It worked nice on my macpro untill I upgraded to the latest IOS 12.0.1. Useing 2.7 (continued to work) but IDLE went in black (useless). >> >>All because 2.7 is no longer supported? Perhaps. You should shift to Python 3 anyway. The differences are small if you're just starting out; the most overt one is that print() is now a function and requires parentheses. You Mac will have a "python3" command which runs a modern version of Python; it should be quite recent. The plain "python" command runs Python 2. >>Anything I can do or must I jump to latest version of Python? > >don't use the built-in Python on macOS. Indeed, althought the Python 3 on a Mac is normally pretty current (as of the OS version you are running). >There are several ways to get a usable Python3 onto the Mac, not least >of which is grabbing an installer from python.org. By coincidence, I did exactly that on Monday. Works fine! But if you just need to shift to Python 3, the "python3" already on your Mac will also work fine. Cheers, Cameron Simpson From phillor9 at gmail.com Tue Nov 16 00:07:25 2021 From: phillor9 at gmail.com (Phil) Date: Tue, 16 Nov 2021 15:07:25 +1000 Subject: [Tutor] A dictionary question In-Reply-To: References: <6aea6665-372a-55b0-a69e-34f10bcb54a4@gmail.com> <1faf831f-e2b8-7035-adc2-f59374486166@DancesWithMice.info> Message-ID: <678af609-ee0b-d298-bc6a-ec12bc96ed57@gmail.com> Just a quick one while I stop for afternoon tea. Using the example provide by Mats, if I print(pairs) I get: Counter({(4, 6): 2, (3, 4): 1, (3, 6): 1 etc. How would I print the pairs so they look like this: (4,6) 2 (3,4) 1 (3,6) 1 My feeble attempt is under the example code. I have looked at a couple of collections tutorials but I cannot relate what I'm seeing to the example below. I'm also wondering how I might do something like this: if count == 2: ??? do this row = [{7,3},{5},{4,6,8},{7,8},{1},{9,3},{7,9},{4,6,3},{2}] #row = [{5},{6},{3},{4,7,5},{1,2,4,5},{1,7,5},{1,2,4,7,9},{8},{1,2,4,7,9}] set_list = [{1, 2}, {1, 3}, {1, 4}, {1, 5}, {1, 6}, {1, 7}, {1, 8}, {1, 9}, ??????????? {2, 3}, {2, 4}, {2, 5}, {2, 6}, {2, 7}, {2, 8}, {2, 9}, ??????????? {3, 4}, {3, 5}, {3, 6}, {3, 7}, {3, 8}, {3, 9}, ??????????? {4, 5}, {4, 6}, {4, 7}, {4, 8}, {4,9}, ??????????? {5, 6}, {5, 7}, {5, 8}, {5, 9}, ??????????? {6, 7}, {6, 8}, {6, 9}, ??????????? {7, 8}, {7, 9}, ??????????? {8, 9} ??????????? ] from collections import Counter pairs = Counter() for s in set_list: ??? for r in row: ??????? if s.issubset(r): ??????????? pairs[tuple(sorted(s))] += 1 print(pairs) for i in range(9): ??? print(pairs[i]) -- Regards, Phil From martin at linux-ip.net Tue Nov 16 01:11:42 2021 From: martin at linux-ip.net (Martin A. Brown) Date: Mon, 15 Nov 2021 22:11:42 -0800 Subject: [Tutor] A dictionary question In-Reply-To: <678af609-ee0b-d298-bc6a-ec12bc96ed57@gmail.com> References: <6aea6665-372a-55b0-a69e-34f10bcb54a4@gmail.com> <1faf831f-e2b8-7035-adc2-f59374486166@DancesWithMice.info> <678af609-ee0b-d298-bc6a-ec12bc96ed57@gmail.com> Message-ID: <3c57b666-6ee-cb21-9bcc-67a4bdb7a545@wonderfrog.net> Hello, > Just a quick one while I stop for afternoon tea. > > Using the example provide by Mats, if I print(pairs) I get: > > Counter({(4, 6): 2, (3, 4): 1, (3, 6): 1 etc. > > How would I print the pairs so they look like this: > > (4,6) 2 > (3,4) 1 > (3,6) 1 The collections.Counter behaves like a dictionary. Same methods should work. So, try something like these basic loops that take advantage of the items() method. c = Counter({(4, 6): 2, (3, 4): 1, (3, 6): 1}) # -- loop through all of the key, value items in the dict-like object for k, v in c.items(): print( k, v) # -- same but only print where the counter for that entry is 2 for k, v in c.items(): if v == 2: print( k, v) Good luck, enjoy the tea (for a change of pace) and leave your dictionaries cleaner than when you arrived. Only you can stop code rot! -Martin > My feeble attempt is under the example code. I have looked at a couple of > collections tutorials but I cannot relate what I'm seeing to the example > below. > > I'm also wondering how I might do something like this: > > if count == 2: > > ??? do this > > row = [{7,3},{5},{4,6,8},{7,8},{1},{9,3},{7,9},{4,6,3},{2}] > #row = [{5},{6},{3},{4,7,5},{1,2,4,5},{1,7,5},{1,2,4,7,9},{8},{1,2,4,7,9}] > > set_list = [{1, 2}, {1, 3}, {1, 4}, {1, 5}, {1, 6}, {1, 7}, {1, 8}, {1, 9}, > ??????????? {2, 3}, {2, 4}, {2, 5}, {2, 6}, {2, 7}, {2, 8}, {2, 9}, > ??????????? {3, 4}, {3, 5}, {3, 6}, {3, 7}, {3, 8}, {3, 9}, > ??????????? {4, 5}, {4, 6}, {4, 7}, {4, 8}, {4,9}, > ??????????? {5, 6}, {5, 7}, {5, 8}, {5, 9}, > ??????????? {6, 7}, {6, 8}, {6, 9}, > ??????????? {7, 8}, {7, 9}, > ??????????? {8, 9} > ??????????? ] > > from collections import Counter > > pairs = Counter() > > for s in set_list: > ??? for r in row: > ??????? if s.issubset(r): > ??????????? pairs[tuple(sorted(s))] += 1 > > print(pairs) > > for i in range(9): > ??? print(pairs[i]) > > (Repeated/resent from the correct email address.) -- Martin A. Brown http://linux-ip.net/ From georget22 at brookings.k12.or.us Tue Nov 16 01:52:53 2021 From: georget22 at brookings.k12.or.us (George Thompson) Date: Mon, 15 Nov 2021 22:52:53 -0800 Subject: [Tutor] PIP INSTALL face_recognition Message-ID: When i use the command pip install face_recognition in the command interface i get this, C:\WINDOWS\system32>pip install face_recognition Collecting face_recognition Using cached face_recognition-1.3.0-py2.py3-none-any.whl (15 kB) Collecting dlib>=19.7 Using cached dlib-19.22.1.tar.gz (7.4 MB) Preparing metadata (setup.py) ... done Requirement already satisfied: Click>=6.0 in c:\users\ironbeagle\appdata\local\packages\pythonsoftwarefoundation.python.3.9_qbz5n2kfra8p0\localcache\local-packages\python39\site-packages (from face_recognition) (7.1.2) Requirement already satisfied: Pillow in c:\users\ironbeagle\appdata\local\packages\pythonsoftwarefoundation.python.3.9_qbz5n2kfra8p0\localcache\local-packages\python39\site-packages (from face_recognition) (8.1.0) Requirement already satisfied: numpy in c:\users\ironbeagle\appdata\local\packages\pythonsoftwarefoundation.python.3.9_qbz5n2kfra8p0\localcache\local-packages\python39\site-packages (from face_recognition) (1.21.4) Requirement already satisfied: face-recognition-models>=0.3.0 in c:\users\ironbeagle\appdata\local\packages\pythonsoftwarefoundation.python.3.9_qbz5n2kfra8p0\localcache\local-packages\python39\site-packages (from face_recognition) (0.3.0) Using legacy 'setup.py install' for dlib, since package 'wheel' is not installed. Installing collected packages: dlib, face-recognition Running setup.py install for dlib ... error ERROR: Command errored out with exit status 1: command: 'C:\Users\ironbeagle\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\python.exe' -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\ironbeagle\\AppData\\Local\\Temp\\pip-install-9ilr4sfv\\dlib_6e1e13a836eb4b43848b64834f8aeea9\\setup.py'"'"'; __file__='"'"'C:\\Users\\ironbeagle\\AppData\\Local\\Temp\\pip-install-9ilr4sfv\\dlib_6e1e13a836eb4b43848b64834f8aeea9\\setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\ironbeagle\AppData\Local\Temp\pip-record-lg6p900f\install-record.txt' --single-version-externally-managed --user --prefix= --compile --install-headers 'C:\Users\ironbeagle\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\Include\dlib' cwd: C:\Users\ironbeagle\AppData\Local\Temp\pip-install-9ilr4sfv\dlib_6e1e13a836eb4b43848b64834f8aeea9\ Complete output (8 lines): running install running build running build_py package init file 'tools\python\dlib\__init__.py' not found (or not a regular file) running build_ext ERROR: CMake must be installed to build dlib ---------------------------------------- ERROR: Command errored out with exit status 1: 'C:\Users\ironbeagle\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\python.exe' -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\ironbeagle\\AppData\\Local\\Temp\\pip-install-9ilr4sfv\\dlib_6e1e13a836eb4b43848b64834f8aeea9\\setup.py'"'"'; __file__='"'"'C:\\Users\\ironbeagle\\AppData\\Local\\Temp\\pip-install-9ilr4sfv\\dlib_6e1e13a836eb4b43848b64834f8aeea9\\setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\ironbeagle\AppData\Local\Temp\pip-record-lg6p900f\install-record.txt' --single-version-externally-managed --user --prefix= --compile --install-headers 'C:\Users\ironbeagle\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\Include\dlib' Check the logs for full command output. Any help? my program claims no module named "face_recogniiton" and no module names "cv2" pip install cv2 give me this, C:\WINDOWS\system32>pip install cv2 ERROR: Could not find a version that satisfies the requirement cv2 (from versions: none) ERROR: No matching distribution found for cv2 Thank you!! From martin at wonderfrog.net Tue Nov 16 00:26:46 2021 From: martin at wonderfrog.net (Martin A. Brown) Date: Mon, 15 Nov 2021 21:26:46 -0800 Subject: [Tutor] A dictionary question In-Reply-To: <678af609-ee0b-d298-bc6a-ec12bc96ed57@gmail.com> References: <6aea6665-372a-55b0-a69e-34f10bcb54a4@gmail.com> <1faf831f-e2b8-7035-adc2-f59374486166@DancesWithMice.info> <678af609-ee0b-d298-bc6a-ec12bc96ed57@gmail.com> Message-ID: Hello Phil, > Just a quick one while I stop for afternoon tea. > > Using the example provide by Mats, if I print(pairs) I get: > > Counter({(4, 6): 2, (3, 4): 1, (3, 6): 1 etc. > > How would I print the pairs so they look like this: > > (4,6) 2 > (3,4) 1 > (3,6) 1 > > My feeble attempt is under the example code. I have looked at a > couple of collections tutorials but I cannot relate what I'm > seeing to the example below. The collections.Counter behaves like a dictionary. Same methods should work. So, try something like these basic loops that take advantage of the items() method. c = Counter({(4, 6): 2, (3, 4): 1, (3, 6): 1}) # -- loop through all of the key, value items in the dict-like object for k, v in c.items(): print( k, v) # -- same but only print where the counter for that entry is 2 for k, v in c.items(): if v == 2: print( k, v) Good luck, enjoy the tea (for a change of pace) and leave your dictionaries cleaner than when you arrived. Only you can stop code rot! -Martin > I'm also wondering how I might do something like this: > > if count == 2: > > ??? do this > > row = [{7,3},{5},{4,6,8},{7,8},{1},{9,3},{7,9},{4,6,3},{2}] > #row = [{5},{6},{3},{4,7,5},{1,2,4,5},{1,7,5},{1,2,4,7,9},{8},{1,2,4,7,9}] > > set_list = [{1, 2}, {1, 3}, {1, 4}, {1, 5}, {1, 6}, {1, 7}, {1, 8}, {1, 9}, > ??????????? {2, 3}, {2, 4}, {2, 5}, {2, 6}, {2, 7}, {2, 8}, {2, 9}, > ??????????? {3, 4}, {3, 5}, {3, 6}, {3, 7}, {3, 8}, {3, 9}, > ??????????? {4, 5}, {4, 6}, {4, 7}, {4, 8}, {4,9}, > ??????????? {5, 6}, {5, 7}, {5, 8}, {5, 9}, > ??????????? {6, 7}, {6, 8}, {6, 9}, > ??????????? {7, 8}, {7, 9}, > ??????????? {8, 9} > ??????????? ] > > from collections import Counter > > pairs = Counter() > > for s in set_list: > ??? for r in row: > ??????? if s.issubset(r): > ??????????? pairs[tuple(sorted(s))] += 1 > > print(pairs) > > for i in range(9): > ??? print(pairs[i]) > > -- Martin A. Brown --- Wonderfrog Heavy Industries --- martin at wonderfrog.net From alan.gauld at yahoo.co.uk Tue Nov 16 05:49:56 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 16 Nov 2021 10:49:56 +0000 Subject: [Tutor] PIP INSTALL face_recognition In-Reply-To: References: Message-ID: On 16/11/2021 06:52, George Thompson wrote: > When i use the command pip install face_recognition in the command > interface i get this, > Installing collected packages: dlib, face-recognition > Running setup.py install for dlib ... error > ERROR: Command errored out with exit status 1: > command: > 'C:\Users\ironbeagle\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\python.exe' > -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = > '"'"'C:\\Users\\ironbeagle\\AppData\\Local\\Temp\\pip-install-9ilr4sfv\\dlib_6e1e13a836eb4b43848b64834f8aeea9\\setup.py'"'"'; > __file__='"'"'C:\\Users\\ironbeagle\\AppData\\Local\\Temp\\pip-install-9ilr4sfv\\dlib_6e1e13a836eb4b43848b64834f8aeea9\\setup.py'"'"';f > = getattr(tokenize, '"'"'open'"'"', open)(__file__) if > os.path.exists(__file__) else io.StringIO('"'"'from setuptools import > setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', > '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' > install --record > 'C:\Users\ironbeagle\AppData\Local\Temp\pip-record-lg6p900f\install-record.txt' > --single-version-externally-managed --user --prefix= --compile > --install-headers > 'C:\Users\ironbeagle\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\Include\dlib' > cwd: > C:\Users\ironbeagle\AppData\Local\Temp\pip-install-9ilr4sfv\dlib_6e1e13a836eb4b43848b64834f8aeea9\ > Complete output (8 lines): > running install > running build > running build_py > package init file 'tools\python\dlib\__init__.py' not found (or not a > regular file) > running build_ext > > ERROR: CMake must be installed to build dlib This seems to be the first error. Do you have cmake installed? Although that is likely to just trigger more errors because... this may imply it is going to try to build the package from C source, do you have a suitable C build environment available? But I'm no expert in pip and the install tools. I don't use many 3rd party modules. -- 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 abba6 at mac.com Tue Nov 16 05:56:45 2021 From: abba6 at mac.com (=?utf-8?Q?Asbj=C3=B8rn_Bakken?=) Date: Tue, 16 Nov 2021 11:56:45 +0100 Subject: [Tutor] IDLE Message-ID: Hey, Thanks for all the answers to my problem. I am going to start with 3.10 and follow your advice Alan, Thank you very much Asbj?rn Bakken From gawad at unal.edu.co Tue Nov 16 07:47:53 2021 From: gawad at unal.edu.co (Gabriel A Awad Aubad) Date: Tue, 16 Nov 2021 07:47:53 -0500 Subject: [Tutor] Is web scraping allowed? Message-ID: Hi everybody!!! I would like to use The Tutor Archives: https://mail.python.org/pipermail/tutor/ to give examples of web scraping to my students. Is web scraping of this repository allowed? Thanks in advance for your reply. Best regards, *Gabriel AWAD* *Profesor Asociado* *Departamento de Ciencias de la Computaci?n y de la Decisi?n* Universidad Nacional de Colombia - Sede Medell?n Carrera 80 # 65 - 223 Bloque M8A Of. 208 Medell?n, Colombia C?digo postal: 050041 e-mail: gawad at unal.edu.co LinkedIn Twitter Facebook Skype: gabriel.awad *"M?s puede en m? el azar que yo". * * Miguel de Montaigne* Libre de virus. www.avg.com <#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2> -- *Aviso legal:*?El contenido de este mensaje y los archivos adjuntos son confidenciales y de uso exclusivo de la Universidad Nacional de Colombia. Se encuentran dirigidos s?lo para el uso del destinatario al cual van enviados. La reproducci?n, lectura y/o copia se encuentran prohibidas a cualquier persona diferente a este y puede ser ilegal. Si usted lo ha recibido por error, inf?rmenos y elim?nelo de su correo. Los Datos Personales ser?n tratados conforme a la Ley 1581 de 2012 y a nuestra Pol?tica de Datos Personales que podr? consultar en la p?gina web? www.unal.edu.co .*?*Las opiniones, informaciones, conclusiones y cualquier otro tipo de dato contenido en este correo electr?nico, no relacionados con la actividad de la Universidad Nacional de Colombia, se entender? como personales y de ninguna manera son avaladas por la Universidad. From manpritsinghece at gmail.com Tue Nov 16 11:33:36 2021 From: manpritsinghece at gmail.com (Manprit Singh) Date: Tue, 16 Nov 2021 22:03:36 +0530 Subject: [Tutor] A toy example to have clarity on Sqlite3 module Message-ID: Dear Sir , I have written an example of sqlite 3 module, without using a cursor object . and with exception handling as given below: import sqlite3 lang_list = [ ("Fortran", 1957), ("Python", 1991), ("Go", 2009), ] try: con = sqlite3.connect("example.db") # Create the table con.execute("create table lang(name, first_appeared)") # Fill the table con.executemany("insert into lang values (?, ?)", lang_list) except sqlite3.OperationalError: # It can handle if table already exists print("Error! You may be trying to make a new table with same name") else: for row in con.execute("select * from lang"): print(row) finally: con.close() This example makes a database example.db, and a table lang. In try block i have placed the creation of connection object along with the execute method being called on this connection object to create the table, then executemany method called on this connection object to insert the sequence of rows data. Since an error can occur in all three existing parts of the try block, i have simply put all three in to try block only. Secondarily here there is a sequence of rows to be inserted, that can only be done with executemany, this can't be done with execute. Secondarily - execute , executemany can be called on connection objects also. Secondarily I have put OperationalError: with except block that can handle exception when it is produced due to making a table with same name which is already existing. In else block i am printing all the rows of the data. Secondarily Inside finally block, con.close() to close the connection at last Just need to know, is this the correct implementation ? any suggestions Regards Manprit Singh From wlfraed at ix.netcom.com Tue Nov 16 15:22:37 2021 From: wlfraed at ix.netcom.com (Dennis Lee Bieber) Date: Tue, 16 Nov 2021 15:22:37 -0500 Subject: [Tutor] A toy example to have clarity on Sqlite3 module References: Message-ID: On Tue, 16 Nov 2021 22:03:36 +0530, Manprit Singh declaimed the following: >Just need to know, is this the correct implementation ? any suggestions If it runs, then it is technically a correct implementation. Is it a desirable implementation depends on one's environment. I believe the ability to do operations directly with the Connection, without using a Cursor, is an EXTENSION to the DB-API specification. Not all database connectors may support this type of extension. One of the goals of the DB-API specification is to abstract out differences between actual database engines. In an ideal situation, one would change the SQLite import to import some other connector module, and only have to change the call to create the Connection. Things aren't quite that "ideal", and different connectors use different styles of argument passing -- which one can query the database (after connecting) to determine how things are passed, and can programmatically modify the application SQL syntax to work with that method. So, for maximum compatibility, I'd stick with the "create connection"/"create cursor" (a second reason may be that the extension version may only be doing "autocommit"; using cursors usually allows for disabling autocommit and manually grouping a sequence of SQL as a single transaction, allowing one to rollback the entire group). -- Wulfraed Dennis Lee Bieber AF6VN wlfraed at ix.netcom.com http://wlfraed.microdiversity.freeddns.org/ From alan.gauld at yahoo.co.uk Tue Nov 16 17:35:52 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 16 Nov 2021 22:35:52 +0000 Subject: [Tutor] A toy example to have clarity on Sqlite3 module In-Reply-To: References: Message-ID: On 16/11/2021 16:33, Manprit Singh wrote: > Dear Sir , > > I have written an example of sqlite 3 module, without using a cursor > object . and with exception handling as given below: > > try: > con = sqlite3.connect("example.db") > con.execute("create table lang(name, first_appeared)") > con.executemany("insert into lang values (?, ?)", lang_list) > except sqlite3.OperationalError: # It can handle if table already exists > print("Error! You may be trying to make a new table with same name") > else: > for row in con.execute("select * from lang"): > print(row) > finally: > con.close() > Just need to know, is this the correct implementation ? any suggestions As you say it's a toy example and therefore sufficient for it's purpose. It is not good database programming style however. You should create a cursor for anything non-trivial. There are several reasons for this but one is that if you need to execute multiple different queries during your app you will need a cursor for each, otherwise each new query will wipe out the result of the previous one. If there is not much data being returned you can copy the results into local variables but on a real-world database that isn't always practical. Using "select * from table" is also not recommended since your code will probably break if the structure of the table is modified - eg. columns are added. So it is always better to specify exactly which columns you expect to get back. Also creating a database table without primary keys (and usually an ID field) leads to problems in coding (how to handle duplicates etc) Using a catch-all try/except is also not usually recommended since you want to know what part caused the error, so you would need to introduce code to examine the error in detail. Closing in a finally clause, however, is good practice. In particular you should close all open resources including cursors and connection(s) - there may be more than one! -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alan.gauld at yahoo.co.uk Tue Nov 16 17:36:54 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 16 Nov 2021 22:36:54 +0000 Subject: [Tutor] Is web scraping allowed? In-Reply-To: References: Message-ID: On 16/11/2021 12:47, Gabriel A Awad Aubad wrote: > Hi everybody!!! > > I would like to use The Tutor Archives: > https://mail.python.org/pipermail/tutor/ > > to give examples of web scraping to my students. > > Is web scraping of this repository allowed? You should probably ask on the main Python list, the devs and site owners are represented there. -- 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 iken124 at gmail.com Tue Nov 16 13:11:17 2021 From: iken124 at gmail.com (Ken Lai) Date: Tue, 16 Nov 2021 10:11:17 -0800 Subject: [Tutor] Verifying password hash is SHA512 Message-ID: Hi All, I have a program that hashes passwords into SHA512, but I need to confirm. I found PHP code on the net: $hashed = hash("sha512", $password); Does Python have an equivalent? TIA, From iken124 at gmail.com Tue Nov 16 13:13:11 2021 From: iken124 at gmail.com (Ken Lai) Date: Tue, 16 Nov 2021 10:13:11 -0800 Subject: [Tutor] Verifying password hash is base64 Message-ID: Hi All, I have a program that hashes passwords into base64, but I need to confirm. I found C# code on the net: public static bool IsBase64String(this string s) { s = s.Trim(); return (s.Length % 4 == 0) && Regex.IsMatch(s, @"^[a-zA-Z0-9\+/]*={0,3}$", RegexOptions.None); } Does Python have an equivalent? TIA, From alan.gauld at yahoo.co.uk Tue Nov 16 18:37:37 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 16 Nov 2021 23:37:37 +0000 Subject: [Tutor] Verifying password hash is base64 In-Reply-To: References: Message-ID: On 16/11/2021 18:13, Ken Lai wrote: > Hi All, > > I have a program that hashes passwords into base64, but I need to confirm. > I found C# code on the net: > Does Python have an equivalent? Probably not equivalent but Python does have a base64 module that should do what you need. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alan.gauld at yahoo.co.uk Tue Nov 16 18:39:27 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 16 Nov 2021 23:39:27 +0000 Subject: [Tutor] Verifying password hash is SHA512 In-Reply-To: References: Message-ID: On 16/11/2021 18:11, Ken Lai wrote: > Hi All, > > I have a program that hashes passwords into SHA512, but I need to confirm. > I found PHP code on the net: > > $hashed = hash("sha512", $password); > > Does Python have an equivalent? Probably not equivalent but it does have the crypt module which supports SHA512. It might do what you need? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From manpritsinghece at gmail.com Tue Nov 16 19:51:51 2021 From: manpritsinghece at gmail.com (Manprit Singh) Date: Wed, 17 Nov 2021 06:21:51 +0530 Subject: [Tutor] A toy example to have clarity on Sqlite3 module In-Reply-To: References: Message-ID: Dear Sir, Kindly look at this modified example, need your comments on using con.rollback() and con.commit() & fetchone(), fetchmany() and fetchall() import sqlite3 lang_list = [ ("Fortran", 1957), ("Python", 1991), ("Go", 2009), ] try: # Connection object represents the database con = sqlite3.connect("example.db") # Cursor object - To execute the queries & Fetch the records cur = con.cursor() # Create the table cur.execute("create table lang(name, first_appeared)") # Fill the table cur.executemany("insert into lang values (?, ?)", lang_list) except sqlite3.Error: # base class of all exceptions of sqlite3 module con.rollback() # Rolls back any changes to database print("Database related error is there") else: con.commit() # Commits the current transaction cur.execute("select name from lang") print(cur.fetchone()) # Fetches next row print("-------------------") print(cur.fetchmany(size=1)) # fetches next rows(size given) print("-------------------") print(cur.fetchall()) # Fetches all(remaining) rows finally: cur.close() con.close() Placing con.rollback() in the except block will rollback all changes in the database if any exception occurs, that's why I have placed it in the except block. con.commit() will commit all changes done so I have placed it in the else block to ensure if no exception is raised all changes must be committed.. Need your comments . Regards Manprit Singh On Wed, Nov 17, 2021 at 4:07 AM Alan Gauld via Tutor wrote: > On 16/11/2021 16:33, Manprit Singh wrote: > > Dear Sir , > > > > I have written an example of sqlite 3 module, without using a cursor > > object . and with exception handling as given below: > > > > > try: > > con = sqlite3.connect("example.db") > > con.execute("create table lang(name, first_appeared)") > > con.executemany("insert into lang values (?, ?)", lang_list) > > except sqlite3.OperationalError: # It can handle if table already > exists > > print("Error! You may be trying to make a new table with same name") > > else: > > for row in con.execute("select * from lang"): > > print(row) > > finally: > > con.close() > > > > Just need to know, is this the correct implementation ? any suggestions > > As you say it's a toy example and therefore sufficient for it's purpose. > > It is not good database programming style however. You should create > a cursor for anything non-trivial. There are several reasons for > this but one is that if you need to execute multiple different > queries during your app you will need a cursor for each, otherwise > each new query will wipe out the result of the previous one. > > If there is not much data being returned you can copy the results > into local variables but on a real-world database that isn't > always practical. > > Using "select * from table" is also not recommended since your > code will probably break if the structure of the table is > modified - eg. columns are added. So it is always better > to specify exactly which columns you expect to get back. > > Also creating a database table without primary keys (and > usually an ID field) leads to problems in coding (how to > handle duplicates etc) > > Using a catch-all try/except is also not usually recommended since > you want to know what part caused the error, so you would need > to introduce code to examine the error in detail. > > Closing in a finally clause, however, is good practice. In particular > you should close all open resources including cursors and > connection(s) - there may be more than one! > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.amazon.com/author/alan_gauld > Follow my photo-blog on Flickr at: > http://www.flickr.com/photos/alangauldphotos > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From mats at wichmann.us Tue Nov 16 20:20:28 2021 From: mats at wichmann.us (Mats Wichmann) Date: Tue, 16 Nov 2021 18:20:28 -0700 Subject: [Tutor] Verifying password hash is SHA512 In-Reply-To: References: Message-ID: On 11/16/21 11:11, Ken Lai wrote: > Hi All, > > I have a program that hashes passwords into SHA512, but I need to confirm. > I found PHP code on the net: > > $hashed = hash("sha512", $password); > > Does Python have an equivalent? it's not really that clear what you're asking. In Python, the hashlib module is how you deal with hashes: https://docs.python.org/3/library/hashlib.html it supports many different hash formats, many of which are implemented behind the covers by a non-Python library. From manpasco1 at gmail.com Wed Nov 17 08:51:49 2021 From: manpasco1 at gmail.com (Pascal Maniriho) Date: Thu, 18 Nov 2021 00:51:49 +1100 Subject: [Tutor] How to remove all empty elements from a list of list Message-ID: Dear all, I have the following list in Python and would like to remove all empty elements without invalidating indices. mylist = [['write', 'create', 'draw', 'book', 'mkay', '', '', '', '', '', ''], ['hey', 'mykey', 'ange', 'kiki', 'rose', '', '', '', '', '', '', '', '', '', ''],['name','age','address', 'nationality', '', '', '', '', '', '', '', '', '', '', '', '']] I need the output like this: mylist = [['write', 'create', 'draw', 'book', 'mkay'], ['hey', 'mykey', 'ange', 'kiki', 'rose']] I have tried to use the following line of code, but it does not give the expected output. mylist1 = list(filter(None, mylist)) Kindly help ...... pasco From alan.gauld at yahoo.co.uk Wed Nov 17 18:01:57 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 17 Nov 2021 23:01:57 +0000 Subject: [Tutor] How to remove all empty elements from a list of list In-Reply-To: References: Message-ID: On 17/11/2021 13:51, Pascal Maniriho wrote: > Dear all, > > I have the following list in Python and would like to remove all empty > elements without invalidating indices. I don;t know what you mean by the last comment. If you remove elements it must affect the indices, there is no way to avoid that. > mylist = [['write', 'create', 'draw', 'book', 'mkay', '', '', '', '', '', > ''], ['hey', 'mykey', 'ange', 'kiki', 'rose', '', '', '', '', '', '', '', > '', '', ''],['name','age','address', 'nationality', '', '', '', '', '', '', > '', '', '', '', '', '']] > > I need the output like this: > > mylist = [['write', 'create', 'draw', 'book', 'mkay'], ['hey', 'mykey', > 'ange', 'kiki', 'rose']] > > I have tried to use the following line of code, but it does not give the > expected output. > > mylist1 = list(filter(None, mylist)) First you need to realize that you actually have a list of lists so you will need a loop. Also to change the embedded lists in the loop you will need the index which you can get from the enumerate() function. Something like: for index,sublist in mylist: mylist[index] = [item for item in sublist if item] should work. However, one other query remains. In your example of output you only have two sublists. Why is the third not included? Is that deliberate? If so, slicing could remove the last 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 mats at wichmann.us Wed Nov 17 19:21:52 2021 From: mats at wichmann.us (Mats Wichmann) Date: Wed, 17 Nov 2021 17:21:52 -0700 Subject: [Tutor] How to remove all empty elements from a list of list In-Reply-To: References: Message-ID: On 11/17/21 06:51, Pascal Maniriho wrote: > Dear all, > > I have the following list in Python and would like to remove all empty > elements without invalidating indices. > > mylist = [['write', 'create', 'draw', 'book', 'mkay', '', '', '', '', '', > ''], ['hey', 'mykey', 'ange', 'kiki', 'rose', '', '', '', '', '', '', '', > '', '', ''],['name','age','address', 'nationality', '', '', '', '', '', '', > '', '', '', '', '', '']] > > I need the output like this: > > mylist = [['write', 'create', 'draw', 'book', 'mkay'], ['hey', 'mykey', > 'ange', 'kiki', 'rose']] > > I have tried to use the following line of code, but it does not give the > expected output. > > mylist1 = list(filter(None, mylist)) It's an interesting problem, and stating it correctly is important as far as picking solutions. There are no empty elements in mylist so from your problem statement there is nothing to remove. There are clearly empty elements in the members of mylist, so you have to work a level down (see Alan's reply). It's often a problem when you try to modify a list while looping over it, for which the solution is to loop over a copy so it's safe to delete items from the original. But... your try creates a new list anyway, so it doesn't seem you have to do modify-in-place (we can't fully guess your requirements unless you tell us). From the nature of the data, here's a random thought: this seems to have come from somewhere that probably wasn't originally a Python list - perhaps processing a text or csv file or some such. Is it possible it's better to strip out trailing blanks - in this example they're all trailing, but again, what are the real requirements? - *before* converting to a list? That might even be a cleaner way. From scarletswish11 at gmail.com Thu Nov 18 12:59:04 2021 From: scarletswish11 at gmail.com (Scar 43) Date: Thu, 18 Nov 2021 12:59:04 -0500 Subject: [Tutor] (no subject) Message-ID: Is there a way to get the click coordinates on an HTML image? I'm using Jupiter notebook, I've seen many ways that track the click coordinates of downloaded images, but not when you grab the image from a website and display it in a code cell. From alan.gauld at yahoo.co.uk Thu Nov 18 16:28:38 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 18 Nov 2021 21:28:38 +0000 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: On 18/11/2021 17:59, Scar 43 wrote: > Is there a way to get the click coordinates on an HTML image? I'm using > Jupiter notebook, I've seen many ways that track the click coordinates of > downloaded images, but not when you grab the image from a website and > display it in a code cell. I confess that I don't really understand what you mean. What is the click coordinate? Do you mean the position of the mouse when it clicks on the image? But that only makes sense in the context of some kind of app? If its a web app that is processed by the server. Is that seever your code? If so what framework are you using CGI? Django? Flask? I'm also not sure what you mean by a "code cell" - is that a Jupityr notebook concept? Maybe if you could describe the use case scenario it would help? Where is the image, what is clicking on it, how do you expect to access it? And what environment are you using (eg web framework, html parsers 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 phillor9 at gmail.com Fri Nov 19 00:00:53 2021 From: phillor9 at gmail.com (Phil) Date: Fri, 19 Nov 2021 16:00:53 +1100 Subject: [Tutor] A dictionary question In-Reply-To: <3c57b666-6ee-cb21-9bcc-67a4bdb7a545@wonderfrog.net> References: <6aea6665-372a-55b0-a69e-34f10bcb54a4@gmail.com> <1faf831f-e2b8-7035-adc2-f59374486166@DancesWithMice.info> <678af609-ee0b-d298-bc6a-ec12bc96ed57@gmail.com> <3c57b666-6ee-cb21-9bcc-67a4bdb7a545@wonderfrog.net> Message-ID: <2b78fb1a-329c-ba8b-e407-bacf7ebc7e1a@gmail.com> > The collections.Counter behaves like a dictionary. Same methods > should work. So, try something like these basic loops that take > advantage of the items() method. > > c = Counter({(4, 6): 2, (3, 4): 1, (3, 6): 1}) > > # -- loop through all of the key, value items in the dict-like object > for k, v in c.items(): > print( k, v) > > # -- same but only print where the counter for that entry is 2 > for k, v in c.items(): > if v == 2: > print( k, v) Thank you Martin that solved that problem, however, it hasn't help me solve the original problem. I've spent a week or more on this and I don't seem to be getting anywhere. I'm now wondering if I really need to use a dictionary and if I'm really attacking this problem correctly. The problem is, given a list of sets find a pair of numbers that occur in the same row and in no other set in that row. row = [{7,3},{5},{4,6,8},{7,8},{1},{9,3},{7,9},{4,6,3},{2}] The pair in this set is {4, 6}. This is an easy one to solve because neither the 4 nor the 6 is in any other set. My clumsy code gave me the correct answer but it fails if the 4 or the 6 is repeated in another row set. There is no solution for the follow row because there is no unique pair: row = [{5},{6},{3},{4,7,5},{1,2,4,5},{1,7,5},{1,2,4,7,9},{8},{1,2,4,7,9}] I just had a bright idea while I'm typing this and the following code almost offer a complete solution: row = [{7,3},{5},{4,6,8},{7,8},{1},{9,3},{7,9},{4,6,3},{2}] number_list = [] row_list = [] for r in range(9): ??? for i in range(1, 10): ??????? if i in row[r] and len(row[r]) > 2: ??????????? number_list.append(i) ??????????? row_list.append(r) for i in range(len(number_list)): ??? print(number_list[i], end=' ') print() for i in range(len(row_list)): ??? print(row_list[i], end=' ') At this point I have: 4 6 8 3 4 6 2 2 2 7 7 7 What I need to do now is feed this result into a function that returns the? tuple (4, 6). This is just for my own education and is not homework. I expect to have Internet access the day after tomorrow and in the meantime I will see if I can massage this code further towards a solution. -- Regards, Phil From alan.gauld at yahoo.co.uk Fri Nov 19 07:14:46 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 19 Nov 2021 12:14:46 +0000 Subject: [Tutor] A dictionary question In-Reply-To: <2b78fb1a-329c-ba8b-e407-bacf7ebc7e1a@gmail.com> References: <6aea6665-372a-55b0-a69e-34f10bcb54a4@gmail.com> <1faf831f-e2b8-7035-adc2-f59374486166@DancesWithMice.info> <678af609-ee0b-d298-bc6a-ec12bc96ed57@gmail.com> <3c57b666-6ee-cb21-9bcc-67a4bdb7a545@wonderfrog.net> <2b78fb1a-329c-ba8b-e407-bacf7ebc7e1a@gmail.com> Message-ID: On 19/11/2021 05:00, Phil wrote: > The problem is, given a list of sets find a pair of numbers that occur > in the same row and in no other set in that row. > > row = [{7,3},{5},{4,6,8},{7,8},{1},{9,3},{7,9},{4,6,3},{2}] > > The pair in this set is {4, 6}. This is an easy one to solve because > neither the 4 nor the 6 is in any other set. Your explanation doesn't make sense to me. row is a list of sets. Your description says find a pair in the same row(ie same list of sets) and in no other set in that row(list) But 4,6 appears in 2 sets within the list? > correct answer but it fails if the 4 or the 6 is repeated in another row > set. Again I'm not sure what that means. Does it mean repeated in a different list of sets? or a different (third?) set within the original row(list)? > There is no solution for the follow row because there is no unique pair: > > row = [{5},{6},{3},{4,7,5},{1,2,4,5},{1,7,5},{1,2,4,7,9},{8},{1,2,4,7,9}] Why not 4,5? it appears in 2 sets, which seems to be the criteria? > I just had a bright idea while I'm typing this and the following code > almost offer a complete solution: > > row = [{7,3},{5},{4,6,8},{7,8},{1},{9,3},{7,9},{4,6,3},{2}] > > number_list = [] > row_list = [] > > for r in range(9): > ??? for i in range(1, 10): > ??????? if i in row[r] and len(row[r]) > 2: > ??????????? number_list.append(i) > ??????????? row_list.append(r) > > for i in range(len(number_list)): > ??? print(number_list[i], end=' ') > > print() > > for i in range(len(row_list)): > ??? print(row_list[i], end=' ') > > At this point I have: > > 4 6 8 3 4 6 > 2 2 2 7 7 7 > > What I need to do now is feed this result into a function that returns > the? tuple (4, 6). This is just for my own education and is not homework. Again you've lost me. Why return 4,6 based on your data. What makes that the right value? And which 4,6? There is a 4,6 pair at both ends of your first row? Maybe if you can more precisely explain your algorithm and rules we can provide more specific help. I seem to recall you saying somewhere that it was a sudoku puzzle? Is this still part of that? If so it might help to describe your problem in terms of the problem domain rather than your current solution design? What do the sets represent? (cells, and their guesses so far maybe?) What are you trying to find within 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 mats at wichmann.us Fri Nov 19 10:09:52 2021 From: mats at wichmann.us (Mats Wichmann) Date: Fri, 19 Nov 2021 08:09:52 -0700 Subject: [Tutor] A dictionary question In-Reply-To: References: <6aea6665-372a-55b0-a69e-34f10bcb54a4@gmail.com> <1faf831f-e2b8-7035-adc2-f59374486166@DancesWithMice.info> <678af609-ee0b-d298-bc6a-ec12bc96ed57@gmail.com> <3c57b666-6ee-cb21-9bcc-67a4bdb7a545@wonderfrog.net> <2b78fb1a-329c-ba8b-e407-bacf7ebc7e1a@gmail.com> Message-ID: On 11/19/21 05:14, Alan Gauld via Tutor wrote: >> There is no solution for the follow row because there is no unique pair: >> >> row = [{5},{6},{3},{4,7,5},{1,2,4,5},{1,7,5},{1,2,4,7,9},{8},{1,2,4,7,9}] > > Why not 4,5? it appears in 2 sets, which seems to be the criteria? because 5 appears by itself in several other sets, as does 4 (that second qualifier was not, if I recall, in the original description) === and since my partial bit the other day didn't lead to a finished solution, I'll repaste it with the second half also included (not prettied up): from collections import Counter row = [{7,3},{5},{4,8,6},{7,8},{1},{9,3},{7,9},{4,6,3},{2}] #row = [{5},{6},{3},{4,7,5},{1,2,4,5},{1,7,5},{1,2,4,7,9},{8},{1,2,4,7,9}] set_list = [{1, 2}, {1, 3}, {1, 4}, {1, 5}, {1, 6}, {1, 7}, {1, 8}, {1, 9}, {2, 3}, {2, 4}, {2, 5}, {2, 6}, {2, 7}, {2, 8}, {2, 9}, {3, 4}, {3, 5}, {3, 6}, {3, 7}, {3, 8}, {3, 9}, {4, 5}, {4, 6}, {4, 7}, {4, 8}, {4,9}, {5, 6}, {5, 7}, {5, 8}, {5, 9}, {6, 7}, {6, 8}, {6, 9}, {7, 8}, {7, 9}, {8, 9} ] pairs = Counter() for s in set_list: for r in row: if s.issubset(r): pairs[tuple(sorted(s))] += 1 for item, count in pairs.items(): if count == 2: # candidate i, j = item # since we know we're only counting two things, # no need to use a Counter or dict here icount, jcount = 0, 0 for r in row: if i in r: icount += 1 if j in r: jcount += 1 if icount == 2 and jcount == 2: print(f"{set(item)} found") From breamoreboy at gmail.com Fri Nov 19 07:29:13 2021 From: breamoreboy at gmail.com (Mark Lawrence) Date: Fri, 19 Nov 2021 12:29:13 +0000 Subject: [Tutor] A dictionary question In-Reply-To: <2b78fb1a-329c-ba8b-e407-bacf7ebc7e1a@gmail.com> References: <6aea6665-372a-55b0-a69e-34f10bcb54a4@gmail.com> <1faf831f-e2b8-7035-adc2-f59374486166@DancesWithMice.info> <678af609-ee0b-d298-bc6a-ec12bc96ed57@gmail.com> <3c57b666-6ee-cb21-9bcc-67a4bdb7a545@wonderfrog.net> <2b78fb1a-329c-ba8b-e407-bacf7ebc7e1a@gmail.com> Message-ID: <9b02095b-1853-7e08-788a-b01793724bc9@gmail.com> On 19/11/2021 05:00, Phil wrote: [big snip] > for r in range(9): > ??? for i in range(1, 10): > ??????? if i in row[r] and len(row[r]) > 2: > ??????????? number_list.append(i) > ??????????? row_list.append(r) > > for i in range(len(number_list)): > ??? print(number_list[i], end=' ') > > print() > > for i in range(len(row_list)): > ??? print(row_list[i], end=' ') > Besides the difficulties of understanding what you actually want, as has been raised elsewhere, please start writing standard Python for loops, e.g. :- for row in row_list: print(row, end=' ') If you need the index use https://docs.python.org/3/library/functions.html#enumerate -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From joao.oliveira at ufob.edu.br Fri Nov 19 12:43:42 2021 From: joao.oliveira at ufob.edu.br (Joao Carlos Silva de Oliveira Matos) Date: Fri, 19 Nov 2021 14:43:42 -0300 Subject: [Tutor] Getting extra characters when copying CURL from devtools Message-ID: Since I installed win11 I'm getting extra characters when I copy CURL as CMD or BASH. It's basically the encoding generating characters like ^ and line breaks. I know I can replace them, but I used to just copy it and paste in Convert curl commands to code (curlconverter.com) to translate to Python. And now the website would not recognize this encoding. Is there any way to select the type of curl encoding when using Devtools? Oh, and it's happening in Chrome and Edge. -- [image: Ficheiro:Bras?o da UFOB.png ? Wikip?dia, a enciclop?dia livre] Jo?o Carlos Silva de Oliveira Matos Bolsista de Inova??o e Tecnologia PROFNIT - Centro das Humanidades - UFOB Mat. 2020100150 From alan.gauld at yahoo.co.uk Fri Nov 19 14:59:39 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 19 Nov 2021 19:59:39 +0000 Subject: [Tutor] Getting extra characters when copying CURL from devtools In-Reply-To: References: Message-ID: On 19/11/2021 17:43, Joao Carlos Silva de Oliveira Matos via Tutor wrote: > Since I installed win11 I'm getting extra characters when I copy CURL as > CMD or BASH. It's basically the encoding generating characters like ^ and > line breaks. > > I know I can replace them, but I used to just copy it and paste in Convert > curl commands to code (curlconverter.com) to > translate to Python. And now the website would not recognize this encoding. > Is there any way to select the type of curl encoding when using Devtools? This doesn't really sound like a Python issue, it's all about your OS and terminal/app combination. There might be somebody here who uses your tool and might be able to help but its not exactly mainstream. You might be better asking on curlconvertor, or even a general curl forum. It looks like they have a bug tracker page, try reporting it there. -- 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 Fri Nov 19 16:27:20 2021 From: PyTutor at DancesWithMice.info (dn) Date: Sat, 20 Nov 2021 10:27:20 +1300 Subject: [Tutor] A dictionary question In-Reply-To: <2b78fb1a-329c-ba8b-e407-bacf7ebc7e1a@gmail.com> References: <6aea6665-372a-55b0-a69e-34f10bcb54a4@gmail.com> <1faf831f-e2b8-7035-adc2-f59374486166@DancesWithMice.info> <678af609-ee0b-d298-bc6a-ec12bc96ed57@gmail.com> <3c57b666-6ee-cb21-9bcc-67a4bdb7a545@wonderfrog.net> <2b78fb1a-329c-ba8b-e407-bacf7ebc7e1a@gmail.com> Message-ID: <2f61eea8-2cc2-c7b5-3516-559a9b175399@DancesWithMice.info> On 19/11/2021 18.00, Phil wrote: > >> The collections.Counter behaves like a dictionary.? Same methods >> should work.? So, try something like these basic loops that take >> advantage of the items() method. >> >> ?? c = Counter({(4, 6): 2, (3, 4): 1, (3, 6): 1}) >> ?? ?? # -- loop through all of the key, value items in the dict-like >> object >> ?? for k, v in c.items(): >> ?????? print( k, v) >> >> ?? # -- same but only print where the counter for that entry is 2 >> ?? for k, v in c.items(): >> ?????? if v == 2: >> ?????????? print( k, v) > > Thank you Martin that solved that problem, however, it hasn't help me > solve the original problem. I've spent a week or more on this and I > don't seem to be getting anywhere. I'm now wondering if I really need to > use a dictionary and if I'm really attacking this problem correctly. > > The problem is, given a list of sets find a pair of numbers that occur > in the same row and in no other set in that row. > > row = [{7,3},{5},{4,6,8},{7,8},{1},{9,3},{7,9},{4,6,3},{2}] > > The pair in this set is {4, 6}. This is an easy one to solve because > neither the 4 nor the 6 is in any other set. My clumsy code gave me the > correct answer but it fails if the 4 or the 6 is repeated in another row > set. > > There is no solution for the follow row because there is no unique pair: > > row = [{5},{6},{3},{4,7,5},{1,2,4,5},{1,7,5},{1,2,4,7,9},{8},{1,2,4,7,9}] > > I just had a bright idea while I'm typing this and the following code > almost offer a complete solution: > > row = [{7,3},{5},{4,6,8},{7,8},{1},{9,3},{7,9},{4,6,3},{2}] > > number_list = [] > row_list = [] > > for r in range(9): > ??? for i in range(1, 10): > ??????? if i in row[r] and len(row[r]) > 2: > ??????????? number_list.append(i) > ??????????? row_list.append(r) > > for i in range(len(number_list)): > ??? print(number_list[i], end=' ') > > print() > > for i in range(len(row_list)): > ??? print(row_list[i], end=' ') > > At this point I have: > > 4 6 8 3 4 6 > 2 2 2 7 7 7 > > What I need to do now is feed this result into a function that returns > the? tuple (4, 6). This is just for my own education and is not homework. This problem has all the flavor of self-flagellation! Some relief may be available through "Chapter 8 - Advanced Iterators" of Mark Pilgrim's (online) book "Dive into Python 3" (https://diveintopython3.net/index.html). Whilst not identical, the example used is quite similar, in requiring pattern-matching techniques across "collections". Thus, 'beg, borrow, or steal' ideas/algorithms to solve this problem... Also, you may find the book as an whole, an useful adjunct to whatever learning-tool currently employed... -- Regards, =dn From phillor9 at gmail.com Fri Nov 19 19:37:44 2021 From: phillor9 at gmail.com (Phil) Date: Sat, 20 Nov 2021 11:37:44 +1100 Subject: [Tutor] A dictionary question In-Reply-To: References: <6aea6665-372a-55b0-a69e-34f10bcb54a4@gmail.com> <1faf831f-e2b8-7035-adc2-f59374486166@DancesWithMice.info> <678af609-ee0b-d298-bc6a-ec12bc96ed57@gmail.com> <3c57b666-6ee-cb21-9bcc-67a4bdb7a545@wonderfrog.net> <2b78fb1a-329c-ba8b-e407-bacf7ebc7e1a@gmail.com> Message-ID: Thank you Mats. I just arrived at our camping site and I'm amazed that there is some mobile phone service here. Once I've settled in I'll run through your code to understand how it works. > pairs = Counter() > > for s in set_list: > ??? for r in row: > ??????? if s.issubset(r): > ??????????? pairs[tuple(sorted(s))] += 1 > > for item, count in pairs.items(): > ??? if count == 2:? # candidate > ??????? i, j = item > ??????? # since we know we're only counting two things, > ??????? # no need to use a Counter or dict here > ??????? icount, jcount = 0, 0 > ??????? for r in row: > ??????????? if i in r: > ??????????????? icount += 1 > ??????????? if j in r: > ??????????????? jcount += 1 > ??????? if icount == 2 and jcount == 2: > ??????????? print(f"{set(item)} found") -- Regards, Phil From phillor9 at gmail.com Fri Nov 19 19:40:18 2021 From: phillor9 at gmail.com (Phil) Date: Sat, 20 Nov 2021 11:40:18 +1100 Subject: [Tutor] A dictionary question In-Reply-To: <2f61eea8-2cc2-c7b5-3516-559a9b175399@DancesWithMice.info> References: <6aea6665-372a-55b0-a69e-34f10bcb54a4@gmail.com> <1faf831f-e2b8-7035-adc2-f59374486166@DancesWithMice.info> <678af609-ee0b-d298-bc6a-ec12bc96ed57@gmail.com> <3c57b666-6ee-cb21-9bcc-67a4bdb7a545@wonderfrog.net> <2b78fb1a-329c-ba8b-e407-bacf7ebc7e1a@gmail.com> <2f61eea8-2cc2-c7b5-3516-559a9b175399@DancesWithMice.info> Message-ID: On 20/11/21 08:27, dn via Tutor wrote: > > This problem has all the flavor of self-flagellation! > > Some relief may be available through "Chapter 8 - Advanced Iterators" of > Mark Pilgrim's (online) book "Dive into Python 3" > (https://diveintopython3.net/index.html). Thanks dn for the tip, it could be just what I need. -- Regards, Phil From phillor9 at gmail.com Fri Nov 19 19:48:39 2021 From: phillor9 at gmail.com (Phil) Date: Sat, 20 Nov 2021 11:48:39 +1100 Subject: [Tutor] A dictionary question In-Reply-To: <9b02095b-1853-7e08-788a-b01793724bc9@gmail.com> References: <6aea6665-372a-55b0-a69e-34f10bcb54a4@gmail.com> <1faf831f-e2b8-7035-adc2-f59374486166@DancesWithMice.info> <678af609-ee0b-d298-bc6a-ec12bc96ed57@gmail.com> <3c57b666-6ee-cb21-9bcc-67a4bdb7a545@wonderfrog.net> <2b78fb1a-329c-ba8b-e407-bacf7ebc7e1a@gmail.com> <9b02095b-1853-7e08-788a-b01793724bc9@gmail.com> Message-ID: <6a0792d5-5431-f931-7a6a-edae0da67fec@gmail.com> > please start writing standard Python for loops, e.g. :- > > for row in row_list: > ??? print(row, end=' ') for i in range(len(row_list)): ??? print(row_list[i], end=' ') The result is the same, however, I suppose it does matter and I will try to improve. -- Regards, Phil From alan.gauld at yahoo.co.uk Sat Nov 20 04:03:00 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 20 Nov 2021 09:03:00 +0000 Subject: [Tutor] A dictionary question In-Reply-To: <6a0792d5-5431-f931-7a6a-edae0da67fec@gmail.com> References: <6aea6665-372a-55b0-a69e-34f10bcb54a4@gmail.com> <1faf831f-e2b8-7035-adc2-f59374486166@DancesWithMice.info> <678af609-ee0b-d298-bc6a-ec12bc96ed57@gmail.com> <3c57b666-6ee-cb21-9bcc-67a4bdb7a545@wonderfrog.net> <2b78fb1a-329c-ba8b-e407-bacf7ebc7e1a@gmail.com> <9b02095b-1853-7e08-788a-b01793724bc9@gmail.com> <6a0792d5-5431-f931-7a6a-edae0da67fec@gmail.com> Message-ID: On 20/11/2021 00:48, Phil wrote: > >> please start writing standard Python for loops, e.g. :- >> >> for row in row_list: >> ??? print(row, end=' ') > > for i in range(len(row_list)): > ??? print(row_list[i], end=' ') > > The result is the same, however, I suppose it does matter The code is easier to read and more reliable, easier to maintain. (It's less liable to index errors) It's also more Pythonic so other Python programmers (and probably even you in 6 months time) will understand it faster. But it also makes a difference to performance since using indexes Python has to do a lot of extra work finding the element each time it's accessed. So, although you won't notice the difference in 99% of normal code if you are trying to do something performance intensive or on very large lists it will make a difference. Finally, there is the matter of credibility. Posting code like this will immediately mark you out as a beginner in the eyes of experienced python coders and so they will tend to respond to any questions on that basis. They won't be deliberately patronising but it will just be an automatic assumption if they see an indexed for loop. -- 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 archinagaraj at gmail.com Fri Nov 19 16:51:54 2021 From: archinagaraj at gmail.com (Nagaraj S) Date: Sat, 20 Nov 2021 03:21:54 +0530 Subject: [Tutor] Principles of code reuse error (Nagaraj S) Message-ID: HI Can you please help me with this? Below is the question, answer and the result error I received. In this code, identify the repeated pattern and replace it with a function called month_days, that receives the name of the *month *and the number of *days* in that month as parameters. Adapt the rest of the code so that the result is the same. Confirm your results by making a function call with the correct parameters for both months listed. def month_days(month, days): print(month +" has " + days +" days") month_days("june", "30") month_days("july", "31") Here is your output: june has 30 days july has 31 days Not quite. Let your function do most of the work and just pass the name of the month and the associated days as parameters. From manpritsinghece at gmail.com Sat Nov 20 06:52:08 2021 From: manpritsinghece at gmail.com (Manprit Singh) Date: Sat, 20 Nov 2021 17:22:08 +0530 Subject: [Tutor] Appropriate use of assignment expression Message-ID: Dear sir, Consider a problem of list of numbers as given below : ls = [234, 5445, 613, 959, 3441, 212, 239] Now i only need those numbers from the list ls which are palindrome, into a new list, doing this way gives the result : ans = [ele for ele in ls if (y:=str(ele))==y[::-1]] print(ans) # Gives result given below which is the right answer [5445, 959, 212] Is it ok to use assignment expressions in this way ? Regards Manprit Singh From alan.gauld at yahoo.co.uk Sat Nov 20 09:03:18 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 20 Nov 2021 14:03:18 +0000 Subject: [Tutor] Appropriate use of assignment expression In-Reply-To: References: Message-ID: On 20/11/2021 11:52, Manprit Singh wrote: > Dear sir, > > Consider a problem of list of numbers as given below : > > ls = [234, 5445, 613, 959, 3441, 212, 239] > > Now i only need those numbers from the list ls which are palindrome, into a > new list, doing this way gives the result : > > ans = [ele for ele in ls if (y:=str(ele))==y[::-1]] > print(ans) # Gives result given below which is the right answer > [5445, 959, 212] > > Is it ok to use assignment expressions in this way ? If it works it must be OK. There has been a lot of debate just recently on the main Python list about how and when you should use the new "walrus" operator. But it is so new there has not been time for a community consensus to form over the best practice. So for now, if it works, you can use it. Personally, I've been burned so many times with assignments inside tests in C that I try my best to avoid it if at all possible. (I have found occasional cases where it just makes things so much simpler that it's foolish not to - this example may even be such a case! -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alan.gauld at yahoo.co.uk Sat Nov 20 09:11:59 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 20 Nov 2021 14:11:59 +0000 Subject: [Tutor] Principles of code reuse error (Nagaraj S) In-Reply-To: References: Message-ID: On 19/11/2021 21:51, Nagaraj S wrote: > In this code, identify the repeated pattern and replace it with a function You didn't show us the original code so the error might be something beyond the parameter types. > *days* in that month as parameters. Adapt the rest of the code so that the > result is the same. Without seeing the code we can't tell what the original "result" might have been. > Confirm your results by making a function call with the > correct parameters for both months listed. > > def month_days(month, days): > print(month +" has " + days +" days") Given the subject line I feel bound to point out that for reuse your function should not print() the result but return it as a string. This will make your function reusable in other contexts such as a web page or GUI application. The current version only works in a command line environment. That change would then require you to print() the result of the function in the main body of your code. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From Richard at Damon-Family.org Sat Nov 20 09:33:53 2021 From: Richard at Damon-Family.org (Richard Damon) Date: Sat, 20 Nov 2021 09:33:53 -0500 Subject: [Tutor] Appropriate use of assignment expression In-Reply-To: References: Message-ID: On 11/20/21 9:03 AM, Alan Gauld via Tutor wrote: > On 20/11/2021 11:52, Manprit Singh wrote: >> Dear sir, >> >> Consider a problem of list of numbers as given below : >> >> ls = [234, 5445, 613, 959, 3441, 212, 239] >> >> Now i only need those numbers from the list ls which are palindrome, into a >> new list, doing this way gives the result : >> >> ans = [ele for ele in ls if (y:=str(ele))==y[::-1]] >> print(ans) # Gives result given below which is the right answer >> [5445, 959, 212] >> >> Is it ok to use assignment expressions in this way ? > If it works it must be OK. > > There has been a lot of debate just recently on the main > Python list about how and when you should use the new > "walrus" operator. But it is so new there has not been > time for a community consensus to form over the best > practice. So for now, if it works, you can use it. > > Personally, I've been burned so many times with assignments > inside tests in C that I try my best to avoid it if at all > possible. (I have found occasional cases where it just > makes things so much simpler that it's foolish not to - this > example may even be such a case! > One BIG thing to point out is that C leaves a lot of details about the order things happen to up to the implementation, so this sort of statement might not work there. Python defines the order much more strictly, so we KNOW that y will be set to str(ele) before we reverse it with y[::-1] so we have the needed promises to be able to say that it will 'work'. In this case, the question seems to not really be 'Does it work' but is it 'good' in the sense of being readable, and that question doesn't always have a clean answer, as like many things, it depends. Personally, I tend to like to decompose into clearly defined functions so I might create as is_palindrome function which takes a string and returns a bool that it true for a palindrome input string. That gives a LONGER program, and maybe a bit slower (due to the added call) but one easier to mentally parse, and thus one I would feel more confident that it will work. -- Richard Damon From alan.gauld at yahoo.co.uk Sat Nov 20 12:01:07 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 20 Nov 2021 17:01:07 +0000 Subject: [Tutor] Appropriate use of assignment expression In-Reply-To: References: Message-ID: On 20/11/2021 14:33, Richard Damon wrote: >> Personally, I've been burned so many times with assignments >> inside tests in C that I try my best to avoid it > One BIG thing to point out is that C leaves a lot of details about the > order things happen to up to the implementation, so this sort of > statement might not work there. To be honest there are many features of Python and its implementation of the walrus assignment that are tighter than C so my fears are not really justified, it's just painful experience making me wary. (And to be fair C++ has tightened up many of C's shortcomings in those areas too) > Personally, I tend to like to decompose into clearly defined functions Exactly, and that's what I tend to do in places where many might use the walrus. It's more expressive, easier to debug and extend. The walrus is one of those 'nice to have' things that is mainly used by those who think there are prizes for writing the shortest code. It's just not true. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From wlfraed at ix.netcom.com Sat Nov 20 12:45:34 2021 From: wlfraed at ix.netcom.com (Dennis Lee Bieber) Date: Sat, 20 Nov 2021 12:45:34 -0500 Subject: [Tutor] Principles of code reuse error (Nagaraj S) References: Message-ID: On Sat, 20 Nov 2021 03:21:54 +0530, Nagaraj S declaimed the following: > >Below is the question, answer and the result error I received. > > >In this code, identify the repeated pattern and replace it with a function You have failed to show US "this code". We have no idea what the "repeated pattern" might be. >called month_days, that receives the name of the *month *and the number of >*days* in that month as parameters. Adapt the rest of the code so that the >result is the same. Confirm your results by making a function call with the >correct parameters for both months listed. > >def month_days(month, days): > print(month +" has " + days +" days") It's been mentioned that you may need to return the result rather than directly print it... >month_days("june", "30") >month_days("july", "31") > You are passing the NUMBER of days as STRINGs, not numbers. >Here is your output: >june has 30 days >july has 31 days > >Not quite. Let your function do most of the work and just >pass the name of the month and the associated days as >parameters. Again, seeing the "original" code may be required. Technically, you are passing the month name, and (as string) the days in the month. Though personally, I'd consider this function to be error-prone -- consider: month_days("February", "32"). The function requires the caller to KNOW the days in the month, and is just formatting them for output. I'd have expected some example where one provides the name of the month, and the function /returns/ the days in that month. >_______________________________________________ >Tutor maillist - Tutor at python.org >To unsubscribe or change subscription options: >https://mail.python.org/mailman/listinfo/tutor -- Wulfraed Dennis Lee Bieber AF6VN wlfraed at ix.netcom.com http://wlfraed.microdiversity.freeddns.org/ From wlfraed at ix.netcom.com Sat Nov 20 13:07:26 2021 From: wlfraed at ix.netcom.com (Dennis Lee Bieber) Date: Sat, 20 Nov 2021 13:07:26 -0500 Subject: [Tutor] Appropriate use of assignment expression References: Message-ID: On Sat, 20 Nov 2021 17:22:08 +0530, Manprit Singh declaimed the following: >Consider a problem of list of numbers as given below : > >ls = [234, 5445, 613, 959, 3441, 212, 239] > >Now i only need those numbers from the list ls which are palindrome, into a >new list, doing this way gives the result : > >ans = [ele for ele in ls if (y:=str(ele))==y[::-1]] >print(ans) # Gives result given below which is the right answer >[5445, 959, 212] > >Is it ok to use assignment expressions in this way ? > And once again, you are really asking about STYLE -- if the code produces the result you want, it is not wrong. But would a corporate environment accept this? They may have style guides. I'd consider ... str(ele) == str(ele)[::-1] to be more legible in most constructs. But since even the [::-1] may be confusing to those not familiar with some features of slicing, I'm really more likely to use ... str(ele) == "".join(reversed(str(ele))) as it is more explicit about what is being compared. An "assignment expression" would make more sense IF the name that receives the result is used in later statements. Compare if not (errcode := somefunction(...)): #no error, continue processing else: #assumes non-zero is an error if errcode = 1: #do something elsif errcode = 2: #do something else else: #do something for all other error codes vs errcode = somefunction(...) if not errcode: #no error, continue processing else: #assumes non-zero is an error if errcode = 1: #do something elsif errcode = 2: #do something else else: #do something for all other error codes -- Wulfraed Dennis Lee Bieber AF6VN wlfraed at ix.netcom.com http://wlfraed.microdiversity.freeddns.org/ From PyTutor at DancesWithMice.info Sat Nov 20 15:05:28 2021 From: PyTutor at DancesWithMice.info (dn) Date: Sun, 21 Nov 2021 09:05:28 +1300 Subject: [Tutor] Appropriate use of assignment expression In-Reply-To: References: Message-ID: <767aa7db-4819-b666-19e9-46933f754616@DancesWithMice.info> On 21/11/2021 06.01, Alan Gauld via Tutor wrote: > On 20/11/2021 14:33, Richard Damon wrote: > >>> Personally, I've been burned so many times with assignments >>> inside tests in C that I try my best to avoid it > >> One BIG thing to point out is that C leaves a lot of details about the >> order things happen to up to the implementation, so this sort of >> statement might not work there. > > To be honest there are many features of Python and its > implementation of the walrus assignment that are tighter > than C so my fears are not really justified, it's just > painful experience making me wary. (And to be fair C++ > has tightened up many of C's shortcomings in those > areas too) > >> Personally, I tend to like to decompose into clearly defined functions > > Exactly, and that's what I tend to do in places where > many might use the walrus. It's more expressive, easier > to debug and extend. > > The walrus is one of those 'nice to have' things that is > mainly used by those who think there are prizes for > writing the shortest code. It's just not true. There are some situations where the Walrus is invaluable. The 'top' of a while-loop with some sort of incremental-control may be one. I coded an assignment-expression yesterday, and was almost sad when refactoring suggested 'a better way' that saw it removed. (sigh) However, whilst some may regard 'short' as 'elegant', the earlier (posts) comments about readability are inescapable. On which theme, such comment applies equally to the list-comprehension component of the construction (and other comprehensions generally). All of the above criticisms are confirmed by the Python interpreter. In the sense that if there is a problem, it will flag the entire line. Beyond simple syntax failings, it is no more able to guide you to the actual clause at-issue, than a human reader! Is it the assignment, the for, the if, the slice, or the walrus? As a gentle guide, many would suggest that learners 'construct' one-liners in their 'full-fat code' version first. In which case, both Python and a debugger will be more helpful and less frustrating (when needed). Also, once the construct is tested/proven, one can?should then ask the question: what do I gain by condensing (part of) this? Yes, a more skilled pythonista might be able to 'think in comprehensions' and that is fine for him/her. Whither the rest of the team? People sometimes talk of 'code-smells' - eg certain constructions which indicate a coder's lack of Python-technique, or a failure to appreciate the use of a recognised (and time-tested) pattern. There is a similar warning sign when a unit of code fails its tests, for some non-trivial reason, and the author stubbornly refuses to 'unwind' a one-liner! Ego vs efficacy? Even with complete cooperation, if that is in-fact the source of the fault, one has to then ask (again): what did I (really) gain by condensing this? (counter-argument: yes there are applications where minute performance-gains are necessary, and thus such skills, valuable) -- Regards, =dn From leamhall at gmail.com Sat Nov 20 16:05:19 2021 From: leamhall at gmail.com (Leam Hall) Date: Sat, 20 Nov 2021 15:05:19 -0600 Subject: [Tutor] Appropriate use of assignment expression In-Reply-To: <767aa7db-4819-b666-19e9-46933f754616@DancesWithMice.info> References: <767aa7db-4819-b666-19e9-46933f754616@DancesWithMice.info> Message-ID: On 11/20/21 14:05, dn via Tutor wrote: > As a gentle guide, many would suggest that learners 'construct' > one-liners in their 'full-fat code' version first. And you can build your tests much more easily when your mind easily understands the code. I find myself doing a very small bit at a time and running the tests. Often, taking a slow and methodical approach helps me see places where a method can be extracted and the code be even cleaner. > (counter-argument: yes there are applications where minute > performance-gains are necessary, and thus such skills, valuable) At which point I would ask, is Python the right tool for the task? In general, Python seems able to do a lot of things "fast enough" for most of us. If you really need faster you can drop in some C code or re-write in Go or Rust. Leam -- Systems Programmer (reuel.net/resume) Scribe: The Domici War (domiciwar.net) General Ne'er-do-well (github.com/LeamHall) From phillor9 at gmail.com Sat Nov 20 19:10:37 2021 From: phillor9 at gmail.com (Phil) Date: Sun, 21 Nov 2021 11:10:37 +1100 Subject: [Tutor] A dictionary question In-Reply-To: References: <6aea6665-372a-55b0-a69e-34f10bcb54a4@gmail.com> <1faf831f-e2b8-7035-adc2-f59374486166@DancesWithMice.info> <678af609-ee0b-d298-bc6a-ec12bc96ed57@gmail.com> <3c57b666-6ee-cb21-9bcc-67a4bdb7a545@wonderfrog.net> <2b78fb1a-329c-ba8b-e407-bacf7ebc7e1a@gmail.com> <9b02095b-1853-7e08-788a-b01793724bc9@gmail.com> <6a0792d5-5431-f931-7a6a-edae0da67fec@gmail.com> Message-ID: On 20/11/21 20:03, Alan Gauld via Tutor wrote: > > The code is easier to read and more reliable, easier to maintain. > (It's less liable to index errors) It's also more Pythonic so > other Python programmers (and probably even you in 6 months > time) will understand it faster. OK Alan, understood. Just one thing though, a snippet from the code that Mats generously gave me. for r in row: ??? if i in r: ??? ??? icount += 1 I need the set position within the row list so I did this: for r in range(num_rows): ??? if i in row[i]: ??? ??? icount += 1 ?? ?? ? irow = row[i] Is this a task for enumerate? A brief test showed this not to be the case but I will experiment some more. -- Regards, Phil From tcm2118 at columbia.edu Sat Nov 20 12:14:36 2021 From: tcm2118 at columbia.edu (Tristin Cara Moone) Date: Sat, 20 Nov 2021 12:14:36 -0500 Subject: [Tutor] Appropriate use of assignment expression In-Reply-To: References: Message-ID: On Sat, Nov 20, 2021 at 12:04 PM Alan Gauld via Tutor wrote: > On 20/11/2021 14:33, Richard Damon wrote: > > >> Personally, I've been burned so many times with assignments > >> inside tests in C that I try my best to avoid it > > > One BIG thing to point out is that C leaves a lot of details about the > > order things happen to up to the implementation, so this sort of > > statement might not work there. > > To be honest there are many features of Python and its > implementation of the walrus assignment that are tighter > than C so my fears are not really justified, it's just > painful experience making me wary. (And to be fair C++ > has tightened up many of C's shortcomings in those > areas too) > > > Personally, I tend to like to decompose into clearly defined functions > > Exactly, and that's what I tend to do in places where > many might use the walrus. It's more expressive, easier > to debug and extend. > > The walrus is one of those 'nice to have' things that is > mainly used by those who think there are prizes for > writing the shortest code. It's just not true. > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.amazon.com/author/alan_gauld > Follow my photo-blog on Flickr at: > http://www.flickr.com/photos/alangauldphotos > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Thank you. From phillor9 at gmail.com Sat Nov 20 20:21:39 2021 From: phillor9 at gmail.com (Phil) Date: Sun, 21 Nov 2021 12:21:39 +1100 Subject: [Tutor] A dictionary question In-Reply-To: References: <6aea6665-372a-55b0-a69e-34f10bcb54a4@gmail.com> <1faf831f-e2b8-7035-adc2-f59374486166@DancesWithMice.info> <678af609-ee0b-d298-bc6a-ec12bc96ed57@gmail.com> <3c57b666-6ee-cb21-9bcc-67a4bdb7a545@wonderfrog.net> <2b78fb1a-329c-ba8b-e407-bacf7ebc7e1a@gmail.com> <9b02095b-1853-7e08-788a-b01793724bc9@gmail.com> <6a0792d5-5431-f931-7a6a-edae0da67fec@gmail.com> Message-ID: <2234fdcb-4969-df6b-5f1c-355017d51f65@gmail.com> Please ignore my previous message regarding enumerate, problem solved. -- Regards, Phil From phillor9 at gmail.com Sun Nov 21 19:13:15 2021 From: phillor9 at gmail.com (Phil) Date: Mon, 22 Nov 2021 11:13:15 +1100 Subject: [Tutor] The Python way and two dimensional lists Message-ID: I hope my questions are not becoming too tiresome. It will require a major effort to rewrite my use of lists in the Python way and so I want to make sure that the first steps are correct. The first index refers to the columns and the second to the rows. num_rows = 9 num_cols = 9 solution = [[0 for i in range(num_cols)] for j in range(num_rows)] solution[0][0] = {7,3} solution[1][0] = {5} solution[2][0] = {4,8,6} solution[3][0] = {7,8} solution[4][0] = {1} solution[5][0] = {9,3} solution[6][0] = {7,9} solution[7][0] = {4,6,3} solution[8][0] = {2} solution[0][1] = {1,2,3} #print(solution) for col in solution: ??? print(col[0]) print() for row in solution: ??? print(row[1]) If I want to refer to column 6, is it the Python way to say x = col[6]? Also regarding enumeration. The following is a slight modification to Mats code and it gives me exactly what I want: row = [{7,3},{5},{4,8,6},{7,8},{1},{9,3},{7,9},{4,6,3},{2}] enum_row = enumerate(row) row_position = [] for index, enum_row in enumerate(row): ??? print('enum_row ',enum_row ) ??? ??? if i in enum_row:#r: ??????? ??? icount += 1 ??????????? row_position.append(index) Now returning to the two dimensional list above and assuming that col[0] is referring to a row (just for this question only). I'm not sure if this is correct because of an error in another section of the code enum_row = enumerate(col[0]) # instead or row for index, enum_row in enumerate(col[0]): # instead or row ??? print('enum_row ',enum_row ) ??? ??? if i in enum_row: ??????? ??? icount += 1 This is where the error is occurring (also from Mats code): for s in set_list: ??? for r in col[0]: # instead of row: ??????? if s.issubset(r): ??????????? pairs[tuple(sorted(s))] += 1 Traceback (most recent call last): ? File "/usr/lib/python3.9/idlelib/run.py", line 559, in runcode ??? exec(code, self.locals) ? File "/home/phil/Python/set_count2.py", line 75, in ??? if s.issubset(r): TypeError: 'int' object is not iterable Confusion is setting in. I think my use of enumerate is correct and I think the way that I'm referencing a row from the solution list is correct. If both of those aren't the source of the error then it has to do with my use of sets. for r in col[0] doesn't give me the sets {7,3},{5} etc. Searching the Internet for information hasn't made me any wiser yet and again I'm sorry to burden the list members with my problems. -- Regards, Phil From alan.gauld at yahoo.co.uk Sun Nov 21 19:55:20 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 22 Nov 2021 00:55:20 +0000 Subject: [Tutor] The Python way and two dimensional lists In-Reply-To: References: Message-ID: On 22/11/2021 00:13, Phil wrote: > I hope my questions are not becoming too tiresome. Not at all, its the only way to learn. > It will require a major effort to rewrite my use of lists in the Python > way and so I want to make sure that the first steps are correct. Good idea! > first index refers to the columns and the second to the rows. You can use the indices any way you like but this way will make your table appear sideways if you print it out with print() But... > solution = [[0 for i in range(num_cols)] for j in range(num_rows)] This is a list of lists. Each inner list is the number of *columns* wide - so it must represent a *row* not a column. And you have as many inner lists as you have rows so that means each position inside a row represents a column entry. > solution[1][0] = {5} So this represents the second row, first column. > ... > solution[0][1] = {1,2,3} And this is the first row, second column. > for col in solution: > ??? print(col[0]) > for row in solution: > ??? print(row[1]) These two loops are both extracting the same objects, just printing a different item. Let's rewrite it using different names for the variable (since the loop doesn't care about the name): for x in solution: print(x[0]) for x in solution: print(x[1]) I reused the name x since the loop doesn't care. But can you see how you are repeating the exact same loop. Meaningful variable names are great because they show your intentions. But they can trick us into thinking they mean something that they don't, just because we want them to! > If I want to refer to column 6, is it the Python way to say x = col[6]? Yes, provided you have first set x to a row. And of course 6 returns the 7th column since its zero based indexing. Also, using python "2D" lists there is no way to access the entire column (in your example) you can only access a whole row. You can build a copy of a column easily enough with: col6 = [row[5] for row in solution] But if you change any values you need to regenerate the copy so it's not exactly efficient. > Also regarding enumeration. The following is a slight modification to > Mats code and it gives me exactly what I want: > > row = [{7,3},{5},{4,8,6},{7,8},{1},{9,3},{7,9},{4,6,3},{2}] > > enum_row = enumerate(row) This is unused and gets thrown away as soon as you enter the loop. > row_position = [] > > for index, enum_row in enumerate(row): enum_row is a bad name to use here IMHO. its actually each set within the row. And the sets represent the cell content (as I understand your design) So I'd prefer for index,cell in enumerate(row): > ??? ??? if i in enum_row:#r: > ??????? ??? icount += 1 > ??????????? row_position.append(index) And this becomes if i in cell: icount += 1 row_position.append(index) > Now returning to the two dimensional list above and assuming that col[0] > is referring to a row (just for this question only). I'm not sure if > this is correct because of an error in another section of the code I think you need to swap your understanding of rows/cols. You access the rows directly, the columns you need to construct on demand. col[0] doesn't refer to anything because col doesn't exist. Even if you created the col as illustrated above col[0] would be the first cell only, not a row. But you can get the row using solution[0] > enum_row = enumerate(col[0]) # instead or row Again, I don't know what you think this is doing but it returns a generator(pedantically, an enumerate object) which you never use and then: > for index, enum_row in enumerate(col[0]): # instead or row Assuming col should be solution... This sets enum_row to the second value of the first returned tuple from enumerate(), ie the first row. So now you want(I think) to test every cell within that row using the exact code you had above. So you need two nested loops. > ??? print('enum_row ',enum_row ) > ??? ??? if i in enum_row: > ??????? ??? icount += 1 > > This is where the error is occurring (also from Mats code): > > for s in set_list: Where is set_list created? > ??? for r in col[0]: # instead of row: > ??????? if s.issubset(r): > ??????????? pairs[tuple(sorted(s))] += 1 > > Traceback (most recent call last): > ? File "/usr/lib/python3.9/idlelib/run.py", line 559, in runcode > ??? exec(code, self.locals) > ? File "/home/phil/Python/set_count2.py", line 75, in > ??? if s.issubset(r): > TypeError: 'int' object is not iterable > > Confusion is setting in. I think my use of enumerate is correct and I > think the way that I'm referencing a row from the solution list is > correct. Sadly, I think both of those assumptions are wrong! What I think you are trying to do, and using the name you started with is: for irow, row in enumerate(solution): for icell, cell in enumerate(row): if value in cell: pairs[tuple(sorted(cell))] += 1 table_position.append(irow,icell) # I think... Which hopefully reads more clearly and makes sense. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From phillor9 at gmail.com Sun Nov 21 20:05:26 2021 From: phillor9 at gmail.com (Phil) Date: Mon, 22 Nov 2021 12:05:26 +1100 Subject: [Tutor] The Python way and two dimensional lists In-Reply-To: References: Message-ID: If I substitute rr where row was originally used that all is good again. It seems to be a bit of a work-around. Is there a more correct Python way? rr = [] for col in solution: ??? print(col[0]) ??? rr.append(col[0]) -- Regards, Phil From phillor9 at gmail.com Sun Nov 21 20:14:10 2021 From: phillor9 at gmail.com (Phil) Date: Mon, 22 Nov 2021 12:14:10 +1100 Subject: [Tutor] The Python way and two dimensional lists In-Reply-To: References: Message-ID: Thank you Alan, you've given me a lot to digest. I knew that I was, and perhaps I still am, confusing rows and columns. More thought required on my part. -- Regards, Phil From PyTutor at DancesWithMice.info Sun Nov 21 20:34:45 2021 From: PyTutor at DancesWithMice.info (dn) Date: Mon, 22 Nov 2021 14:34:45 +1300 Subject: [Tutor] The Python way and two dimensional lists In-Reply-To: References: Message-ID: <1586327c-cc90-c889-b8cc-81c5c047ff67@DancesWithMice.info> On 22/11/2021 13.13, Phil wrote: > I hope my questions are not becoming too tiresome. @Alan seems to have jumped-in first, so I've edited this response, and hope it has not become too disjointed. First question: have you located (and at least dipped-into) Pilgrim's book (as mentioned earlier). He certainly discusses lists, and (I haven't looked (again)) may well cover 2D... Later there is a specific explanation, but the reason for reading is not (only) that you pick-up Python-syntax, but also that you can see the idioms of the language - the way 'bits' work together, and how the language designers were thinking things should go (mental models make another appearance below). Thus, time worth investing... Incidentally, have a look at the bottom of @Alan's posts, because such may (also) be covered in his tutorial. (in both cases, a while since I've looked for specifics in either) > It will require a major effort to rewrite my use of lists in the Python > way and so I want to make sure that the first steps are correct. The > first index refers to the columns and the second to the rows. > > num_rows = 9 > num_cols = 9 > > solution = [[0 for i in range(num_cols)] for j in range(num_rows)] Python's storage allocation is dynamic. It is not necessary to 'reserve' space. > solution[0][0] = {7,3} > solution[1][0] = {5} > solution[2][0] = {4,8,6} > solution[3][0] = {7,8} > solution[4][0] = {1} > solution[5][0] = {9,3} > solution[6][0] = {7,9} > solution[7][0] = {4,6,3} > solution[8][0] = {2} As per @Alan, I also kept feeling that this was 'the wrong way around'! Firstly, I'm a navigator, so I'm used to looking at Cartesian Co-ordinate systems - across before up/down. Secondly, it shouldn't really matter which way around one considers the rows and the columns - so time to get my head out of ... That said, consistency is key. If *you* don't have a visualisation (a mental model) of how this system will work, then all is lost! (at sea?) - and the rest of us have 'no chance'... Next point: Python's lists are not arrays. Notably (but largely irrelevant in this case) many languages require the elements of an array to be of the same type. Items in Python lists can vary, as desired! Lists are typically built either from another "collection" or incrementally. Thus: l = list( ( 1, 2, 3 ) ) or l = list() # or [] l.append( 1 ) l.append( 2 ) ... ... > row = [{7,3},{5},{4,8,6},{7,8},{1},{9,3},{7,9},{4,6,3},{2}] May I suggest that you ignore the 2D issue, for now. Work with the above. Identify the match(es) in this one row, and prove that part of the algorithm. This is what we (in the business) call "Stepwise Decomposition" (plenty of web-links). The idea is to break a large problem into smaller problems. Then break those into even smaller problems. Continuing until the size of the problem becomes 'easily manageable'. In your case, as a Python-Apprentice, this might be a smaller unit than someone else, but that is *not* important. In short everyone's definition of 'small-enough' is going to be different (aka YMMV)! Now, with a 'small' problem, you can wrap it into a Python-function (and when you come to linking small-solutions into the solution of a larger-problem, it becomes a matter of stringing-together a bunch of function calls! (he says, breezily) This also picking-up your point about wanting to be sure that 'the core' of the idea is correct, cf risking spending a lot of time 'flogging a dead horse'. Conversely, and as I was reminding myself when I feared wasting my time on testing and refactoring existing code this morning (cf feeling I was 'solving the emergent problem, as needed') - testing and becoming confident in the capacities of the 'building blocks' becomes a series of small encouragements, and "nothing succeeds like success"! Another advantage is that you can write some (automated) tests, eg row = [{7,3}, etc result = find_duplicates( row ) assert result == x,y,z (if you've not previously met "assert", its boolean-expression must result in True, otherwise it fails - and the exception report will draw your (much-needed) attention to the disparity!) > Confusion is setting in. I think my use of enumerate is correct and I > think the way that I'm referencing a row from the solution list is > correct. If both of those aren't the source of the error then it has to > do with my use of sets. for r in col[0] doesn't give me the sets > {7,3},{5} etc. This, and something you said earlier, represent other good reasons for building short functions. Even if you don't want to risk 'breaking' working-code, they are easy to copy-paste, and thus allow you to tinker with a 'disposable' copy. Such can thus be regarded as a 'test-bench' which you can use to experiment, and compare different ideas as they occur to you. (it's what many of us do, even if you can't see us...) > Searching the Internet for information hasn't made me any wiser yet and > again I'm sorry to burden the list members with my problems. See first question (above). That said, we like intelligent questions posed by people who make an effort... -- Regards, =dn From phillor9 at gmail.com Sun Nov 21 21:13:37 2021 From: phillor9 at gmail.com (Phil) Date: Mon, 22 Nov 2021 13:13:37 +1100 Subject: [Tutor] The Python way and two dimensional lists In-Reply-To: <1586327c-cc90-c889-b8cc-81c5c047ff67@DancesWithMice.info> References: <1586327c-cc90-c889-b8cc-81c5c047ff67@DancesWithMice.info> Message-ID: <3bd3036c-a4a5-936c-f18f-b404b8885a9b@gmail.com> On 22/11/21 12:34, dn via Tutor wrote: > On 22/11/2021 13.13, Phil wrote: >> I hope my questions are not becoming too tiresome. > @Alan seems to have jumped-in first, so I've edited this response, and > hope it has not become too disjointed. > > First question: have you located (and at least dipped-into) Pilgrim's > book (as mentioned earlier). He certainly discusses lists, and (I > haven't looked (again)) may well cover 2D... Thank you dn, more to digest. Yes, I have downloaded the book. I found the suggested chapter 8 hard going and I couldn't relate it to what I'm doing. I've scanned some of the other chapters and will read the list chapter. I've added the book to my calibre library of Python books. -- Regards, Phil From wlfraed at ix.netcom.com Sun Nov 21 21:15:01 2021 From: wlfraed at ix.netcom.com (Dennis Lee Bieber) Date: Sun, 21 Nov 2021 21:15:01 -0500 Subject: [Tutor] The Python way and two dimensional lists References: Message-ID: On Mon, 22 Nov 2021 11:13:15 +1100, Phil declaimed the following: > >for s in set_list: > ??? for r in col[0]: # instead of row: > ??????? if s.issubset(r): > ??????????? pairs[tuple(sorted(s))] += 1 > >Traceback (most recent call last): > ? File "/usr/lib/python3.9/idlelib/run.py", line 559, in runcode > ??? exec(code, self.locals) > ? File "/home/phil/Python/set_count2.py", line 75, in > ??? if s.issubset(r): >TypeError: 'int' object is not iterable Have you looked at the definition of .issubset()? The object one applies it to must be a set. The parameter passed must be a superset (or equal). It returns true if the object -- in your example, that would be s -- is a subset of the parameter r . But your r is not a set -- "for r in col[0]" is selecting the first element of whatever col contains, which appears to be a single integer at this point. I suspect you need to: 1) ensure that "r" is a SET, and possible 2) reverse the call r.issubset(s) -- Wulfraed Dennis Lee Bieber AF6VN wlfraed at ix.netcom.com http://wlfraed.microdiversity.freeddns.org/ From phillor9 at gmail.com Sun Nov 21 22:35:30 2021 From: phillor9 at gmail.com (Phil) Date: Mon, 22 Nov 2021 14:35:30 +1100 Subject: [Tutor] The Python way and two dimensional lists In-Reply-To: References: Message-ID: <22c41561-b7f3-5d7c-2ae2-690b252b1cbe@gmail.com> > The object one applies it to must be a set. The parameter passed must > be a superset (or equal). It returns true if the object -- in your example, > that would be s -- is a subset of the parameter r . Thank you Dennis, I had posted a method I'd used to correct this problem. Although does work correctly. I suspect that it's not an efficient method. -- Regards, Phil From alexkleider at gmail.com Sun Nov 21 22:40:57 2021 From: alexkleider at gmail.com (Alex Kleider) Date: Sun, 21 Nov 2021 19:40:57 -0800 Subject: [Tutor] curses.textpad.Textbox.gather() Message-ID: """ I expect this will be of interest only to those using the curses module. I found that the Textbox.gather method seems incapable of returning a string without a trailing space. The problem is easily solved by using str.strip() as in the last line of my function (provided below) but I'm wondering if there's a better way. Specifically to provide the user with the option of having or not having a trailing space. """ import sys import curses as cur from curses.textpad import Textbox def edited_text(scr, text, y,x): """ Provides the editing capability: Returns the edited (or not) version of text. Editing window begins at (y,x) of the een. """ scr.addstr(y,0, "Edit the text then Ctrl-G to exit", cur.A_BOLD) scr.refresh() # create the text box with border around the outside tb_border = cur.newwin(3,52,y+1,x) tb_border.box() tb_border.refresh() tb_body = cur.newwin(1,50,y+2,x+1) tb = Textbox(tb_body) for ch in text: # insert starting text tb.do_command(ch) tb.edit() # start the editor running, Ctrl-G ends s2 = tb.gather() # fetch the contents scr.clear() # clear the screen # return s2 return s2.strip() def main(scr): text = "Some text to be or not to be edited." new_text = edited_text(scr, text, 5,4) scr.clear() scr.addstr(8,0, 'Your modified text: "{}"'.format(new_text)) scr.getch() # does a refresh cur.wrapper(main) close = """ TIA Cheers, Alex Kleider """ From phillor9 at gmail.com Mon Nov 22 01:02:34 2021 From: phillor9 at gmail.com (Phil) Date: Mon, 22 Nov 2021 17:02:34 +1100 Subject: [Tutor] The Python way and two dimensional lists In-Reply-To: References: Message-ID: > Also, using python "2D" lists there is no way to > access the entire column (in your example) you can > only access a whole row. This has always confused me even when I had it working correctly using the non Python way. It worked so I accepted it. I now have this: num_rows = 9 num_cols = 9 solution = [[0 for i in range(num_cols)] for j in range(num_rows)] # solution row column solution[0][0] = {7,3}, {5}, {4,8,6}, {7,8}, {1}, {9,3}, {7,9}, {4,6,3}, {2} solution[0][1] = {1} solution[0][2] = {2} solution[0][3] = {3} solution[0][4] = {4} solution[0][5] = {5} solution[0][6] = {6} solution[0][7] = {7} solution[0][8] = {8} So row 0 is {7,3}, {5} etc and row 1 is {1} followed by 8 zeros for row in solution: ??? print(row[0]) ({3, 7}, {5}, {8, 4, 6}, {8, 7}, {1}, {9, 3}, {9, 7}, {3, 4, 6}, {2}) 0 0 0 0 0 0 0 0 Why do the sets represent only one position and the eight zeros make up the remaining nine positions? > You can build a copy of a column easily enough with: > > col6 = [row[5] for row in solution] col0 = [row[0] for row in solution] print('column ', col0) prints row 0 with 8 extra zeros: column? [({3, 7}, {5}, {8, 4, 6}, {8, 7}, {1}, {9, 3}, {9, 7}, {3, 4, 6}, {2}), 0, 0, 0, 0, 0, 0, 0, 0] It's those extra zeros again. col0 = [row[6] for row in solution] print('column ', col0) prints and this is what I did expect: column? [{6}, 0, 0, 0, 0, 0, 0, 0, 0] > But if you change any values you need to regenerate > the copy so it's not exactly efficient. Is there a Python way to access any point on the grid with something like x = row[6][col7]. This is how I had originally accessed the grid. I'm happy to change the entire design so that I have a static grid that I can access with a row, col combination. Having to build a column every time I want to access it seems like a lot of unnecessary extra effort. Should I investigate the array module rather that persisting with lists? Arrays make more sense to me. -- Regards, Phil From alan.gauld at yahoo.co.uk Mon Nov 22 04:58:41 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 22 Nov 2021 09:58:41 +0000 Subject: [Tutor] The Python way and two dimensional lists In-Reply-To: References: Message-ID: On 22/11/2021 06:02, Phil wrote: > I now have this: > > num_rows = 9 > num_cols = 9 > > solution = [[0 for i in range(num_cols)] for j in range(num_rows)] > > # solution row column > > solution[0][0] = {7,3}, {5}, {4,8,6}, {7,8}, {1}, {9,3}, {7,9}, {4,6,3}, > {2} I think you actually wanted: solution[0] = [{7,3}, {5}, {4,8,6}, {7,8}, {1}, {9,3}, {7,9}, {4,6,3}, {2}] That initialised the first row to the list of sets. What you have creates a tuple and inserts all of those sets into the first cell. I'm fairly sure that's not what you meant to do. > solution[0][1] = {1} > solution[0][2] = {2} > solution[0][3] = {3} > solution[0][4] = {4} > solution[0][5] = {5} > solution[0][6] = {6} > solution[0][7] = {7} > solution[0][8] = {8} > > So row 0 is {7,3}, {5} etc No, your code made the first cell of row 0 contain all those sets. The subsequent assignments filled the remaining cells with 1-8 respectively. > and row 1 is {1} followed by 8 zeros No, you haven't put anything into row 1. So it is all zeros (from the list comprehension). One thing you might consider is using the Python interactive prompt (>>>) to experiment before you code to see what each value returns. The prompt is a very useful tool when programming but for some reason many programmers neglect to use it while writing code. Another option that could work here, and is often used in professional code, is to just do the initialisation in a hard coded list format: solution = [ [{7,3}, {5}, {4,8,6}, {7,8}, {1}, {9,3}, {7,9}, {4,6,3}, {2}], [{1},0,0,0,0,0,0,0,0], [{2},0,0,0,0,0,0,0,0], [{3},0,0,0,0,0,0,0,0], [{4},0,0,0,0,0,0,0,0], [{5},0,0,0,0,0,0,0,0], [{6},0,0,0,0,0,0,0,0], [{7},0,0,0,0,0,0,0,0], [{8},0,0,0,0,0,0,0,0] ] You can then insert any other values you need into the cells directly. Obviously this technique isn't effective with huge data sets, but for this purpose should work well and may help you visualize things better. Note that this duplicates what I think you were trying to do with your assignments. Also note the discrepancy in data types between the zeros you used in initialising the lists and the sets you use in your assignments. Might it be better to initialize with empty sets instead of 0s? (Empty sets are created with set()) > > for row in solution: > ??? print(row[0]) > > ({3, 7}, {5}, {8, 4, 6}, {8, 7}, {1}, {9, 3}, {9, 7}, {3, 4, 6}, {2}) > 0 > 0 > 0 > 0 > 0 > 0 > 0 > 0 > > Why do the sets represent only one position and the eight zeros make up > the remaining nine positions? Try creating 2D lists using values other than 0 in the >>> prompt: test_table = [[c for c in 'abcdefgh'] for n in range(9)] Then try printing rows and columns using the prompt and different indices until you feel comfortable extracting what you need. You will learn a lot that way. >> You can build a copy of a column easily enough with: >> >> col6 = [row[5] for row in solution] > col0 = [row[0] for row in solution] > > print('column ', col0) > > prints row 0 with 8 extra zeros: > > column? [({3, 7}, {5}, {8, 4, 6}, {8, 7}, {1}, {9, 3}, {9, 7}, {3, 4, > 6}, {2}), 0, 0, 0, 0, 0, 0, 0, 0] > > It's those extra zeros again. Hopefully the above explanation shows where they came from. And the interactive session has clarified what you need to do? > col0 = [row[6] for row in solution] > print('column ', col0) > > prints and this is what I did expect: > > column? [{6}, 0, 0, 0, 0, 0, 0, 0, 0] really? It's not what I would expect! > >> But if you change any values you need to regenerate >> the copy so it's not exactly efficient. > > Is there a Python way to access any point on the grid with something > like x = row[6][col7]. This is how I had originally accessed the grid. Yes that's exactly what you can do using 2 indices. What you can't do is access whole columns. > I'm happy to change the entire design so that I have a static grid that > I can access with a row, col combination. Having to build a column every > time I want to access it seems like a lot of unnecessary extra effort. > Should I investigate the array module rather that persisting with lists? > Arrays make more sense to me. There is very little difference in python between arrays and lists. But lists are such a flexible and all pervasive data structure in Python that you really should stick with them. They are a fundamental skill for every Python user. Just remember that row and column are solution concepts that Python does not understand. All python knows is that a list is a sequence of values that can be accessed using an index. Those values may or may not be other lists. In a 2D list structure the top level list does not know that the sublists are lists. To it they are just value objects like any other. It certainly doesn't know that they represent a table with rows and columns. That concept resides solely in the programmer's(ie. your) head. -- 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 Nov 22 05:14:59 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 22 Nov 2021 10:14:59 +0000 Subject: [Tutor] curses.textpad.Textbox.gather() In-Reply-To: References: Message-ID: On 22/11/2021 03:40, Alex Kleider wrote: > """ > I expect this will be of interest only to those using the curses > module. > > I found that the Textbox.gather method seems incapable of returning > a string without a trailing space. So it seems although I'd not noticed until,your post... > but I'm wondering if there's a better way. Specifically to provide the > user with the option of having or not having a trailing space. As you say its easy to remove the spaces with rstrip() And its equally easy to add a space if needed. So you could write a wrapper that took a space parameter and added/removed spaces as needed. > # create the text box with border around the outside > tb_border = cur.newwin(3,52,y+1,x) > tb_border.box() > tb_border.refresh() > tb_body = cur.newwin(1,50,y+2,x+1) > tb = Textbox(tb_body) > for ch in text: # insert starting text > tb.do_command(ch) > tb.edit() # start the editor running, Ctrl-G ends > s2 = tb.gather() # fetch the contents Hmm, this all feels oddly familiar.... :-) -- 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 Mon Nov 22 11:43:37 2021 From: alexkleider at gmail.com (Alex Kleider) Date: Mon, 22 Nov 2021 08:43:37 -0800 Subject: [Tutor] curses.textpad.Textbox.gather() In-Reply-To: References: Message-ID: > > Hmm, this all feels oddly familiar.... :-) > Blatant plagiarism for sure! Remember, "imitation is the most sincere form of flattery." On a more serious note, Bob Stepp sent me a link which sounded promising but his followup indicated not so. I haven't looked into it yet. All the best (to both of you, and indeed to the whole list.) Alex > > ___________________________________________ > From wlfraed at ix.netcom.com Mon Nov 22 11:45:40 2021 From: wlfraed at ix.netcom.com (Dennis Lee Bieber) Date: Mon, 22 Nov 2021 11:45:40 -0500 Subject: [Tutor] The Python way and two dimensional lists References: Message-ID: On Mon, 22 Nov 2021 17:02:34 +1100, Phil declaimed the following: > >solution[0][0] = {7,3}, {5}, {4,8,6}, {7,8}, {1}, {9,3}, {7,9}, {4,6,3}, >{2} > >So row 0 is {7,3}, {5} etc > >and row 1 is {1} followed by 8 zeros > >for row in solution: > ??? print(row[0]) > >({3, 7}, {5}, {8, 4, 6}, {8, 7}, {1}, {9, 3}, {9, 7}, {3, 4, 6}, {2}) > >Why do the sets represent only one position and the eight zeros make up >the remaining nine positions? > Because that is what you told it to be... Note the () around the printed value -- that indicates a tuple. Tuples don't need (), they need the comma. You bound the nine-element tuple to the [0][0] element of your list-of-lists. Python does not automatically unpack tuples unless you provide enough destination spaces for it. >>> solution[0][0:8] = {7,3}, {5}, {4,8,6}, {7,8}, {1}, {9,3}, {7,9}, {4,6,3}, {2} Here I provide 9 destination spaces for the nine elements on the right. >>> print(solution[0]) [{3, 7}, {5}, {8, 4, 6}, {8, 7}, {1}, {9, 3}, {9, 7}, {3, 4, 6}, {2}, [2]] >>> And here is a LIST of nine elements as the result, and they are individually accessible >>> solution[0][1] {5} >>> The other way is to provide a LIST on the right side >>> solution2 = [0] * 9 Create a nine element (column) list >>> solution2 [0, 0, 0, 0, 0, 0, 0, 0, 0] >>> solution2[0] = [ {7,3}, {5}, {4,8,6}, {7,8}, {1}, {9,3}, {7,9}, {4,6,3}, {2} ] Replace the first element with the nine element (row) list >>> solution2 [[{3, 7}, {5}, {8, 4, 6}, {8, 7}, {1}, {9, 3}, {9, 7}, {3, 4, 6}, {2}], 0, 0, 0, 0, 0, 0, 0, 0] >>> solution2[0] [{3, 7}, {5}, {8, 4, 6}, {8, 7}, {1}, {9, 3}, {9, 7}, {3, 4, 6}, {2}] >>> solution2[0][1] {5} >>> -- Wulfraed Dennis Lee Bieber AF6VN wlfraed at ix.netcom.com http://wlfraed.microdiversity.freeddns.org/ From wlfraed at ix.netcom.com Mon Nov 22 12:13:42 2021 From: wlfraed at ix.netcom.com (Dennis Lee Bieber) Date: Mon, 22 Nov 2021 12:13:42 -0500 Subject: [Tutor] The Python way and two dimensional lists References: Message-ID: On Mon, 22 Nov 2021 11:45:40 -0500, Dennis Lee Bieber declaimed the following: > >>>> solution[0][0:8] = {7,3}, {5}, {4,8,6}, {7,8}, {1}, {9,3}, {7,9}, {4,6,3}, {2} > > Here I provide 9 destination spaces for the nine elements on the right. > Whoops... slight miscount (and the next is showing where I had a previous error... Let me try again... Create 9x9 of empty sets >>> solution = [ [ set() for _ in range(9)] for _ in range(9)] >>> solution [[set(), set(), set(), set(), set(), set(), set(), set(), set()], [set(), set(), set(), set(), set(), set(), set(), set(), set()], [set(), set(), set(), set(), set(), set(), set(), set(), set()], [set(), set(), set(), set(), set(), set(), set(), set(), set()], [set(), set(), set(), set(), set(), set(), set(), set(), set()], [set(), set(), set(), set(), set(), set(), set(), set(), set()], [set(), set(), set(), set(), set(), set(), set(), set(), set()], [set(), set(), set(), set(), set(), set(), set(), set(), set()], [set(), set(), set(), set(), set(), set(), set(), set(), set()]] Bind 9 element tuple to the 9 spaces in the first "row" >>> solution[0][0:9] = {7,3}, {5}, {4,8,6}, {7,8}, {1}, {9,3}, {7,9}, {4,6,3}, {2} >>> solution [[{3, 7}, {5}, {8, 4, 6}, {8, 7}, {1}, {9, 3}, {9, 7}, {3, 4, 6}, {2}], [set(), set(), set(), set(), set(), set(), set(), set(), set()], [set(), set(), set(), set(), set(), set(), set(), set(), set()], [set(), set(), set(), set(), set(), set(), set(), set(), set()], [set(), set(), set(), set(), set(), set(), set(), set(), set()], [set(), set(), set(), set(), set(), set(), set(), set(), set()], [set(), set(), set(), set(), set(), set(), set(), set(), set()], [set(), set(), set(), set(), set(), set(), set(), set(), set()], [set(), set(), set(), set(), set(), set(), set(), set(), set()]] Extract first row >>> solution[0] [{3, 7}, {5}, {8, 4, 6}, {8, 7}, {1}, {9, 3}, {9, 7}, {3, 4, 6}, {2}] Extract third column from first row >>> solution[0][2] {8, 4, 6} >>> -- Wulfraed Dennis Lee Bieber AF6VN wlfraed at ix.netcom.com http://wlfraed.microdiversity.freeddns.org/ From kaushalshriyan at gmail.com Mon Nov 22 11:48:19 2021 From: kaushalshriyan at gmail.com (Kaushal Shriyan) Date: Mon, 22 Nov 2021 22:18:19 +0530 Subject: [Tutor] Enable Python feature in vim editor Message-ID: Hi, I am using vim editor to write python scripts. Is there a way to enable syntax, help and indentation of python script? Thanks in advance. I look forward to hearing from you. Best Regards, Kaushal From leamhall at gmail.com Mon Nov 22 14:06:35 2021 From: leamhall at gmail.com (Leam Hall) Date: Mon, 22 Nov 2021 13:06:35 -0600 Subject: [Tutor] Enable Python feature in vim editor In-Reply-To: References: Message-ID: <855852f9-3481-2b87-4a4c-9108a90c4002@gmail.com> Hey Kaushal, Here's part of my ~/.vimrc. You can do a lot with it, and there are likely Python specific plugins. ### set shiftwidth=2 set shiftround set tabstop=4 set softtabstop=4 set noexpandtab set number set ruler set noautoindent set nosmartindent set noincsearch syntax enable ### On 11/22/21 10:48, Kaushal Shriyan wrote: > Hi, > > I am using vim editor to write python scripts. Is there a way to enable > syntax, help and indentation of python script? > > Thanks in advance. I look forward to hearing from you. > > Best Regards, > > Kaushal -- Systems Programmer (reuel.net/resume) Scribe: The Domici War (domiciwar.net) General Ne'er-do-well (github.com/LeamHall) From learn2program at gmail.com Mon Nov 22 14:12:55 2021 From: learn2program at gmail.com (Alan Gauld) Date: Mon, 22 Nov 2021 19:12:55 +0000 Subject: [Tutor] Enable Python feature in vim editor In-Reply-To: References: Message-ID: <4fad91b0-3cfa-27c9-531f-16db0a5f111c@yahoo.co.uk> On 22/11/2021 16:48, Kaushal Shriyan wrote: > I am using vim editor to write python scripts. Is there a way to enable > syntax, help and indentation of python script? vim can do most things but you need to configure it. Out of the box it should do syntax highlighting and content aware indentation when editing a .py script. You can modify the colour scheme etc. too You can also turn on various? advanced features. There are several YouTube videos showing you how. Just search for "vim and Python" If, OYOH you want to trurn on Python mode for writing vim macros then you need a build of vim with Python included in the build. If you have that you can enable python macros using an option, google is your friend! If that doesn't get you the answer come back here, several of us, including me, use vim as our primary editor for python.? The problem is I've been doing so for ages and can't recall all the options I've turned on/off over the years! But I'm happy to hunt for anything specific you need. I might even learn some new tricks myself! :-) -- 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 Mon Nov 22 14:45:07 2021 From: alexkleider at gmail.com (Alex Kleider) Date: Mon, 22 Nov 2021 11:45:07 -0800 Subject: [Tutor] Enable Python feature in vim editor In-Reply-To: References: Message-ID: Since Leam provided his .vimrc I thought I'd do the same. (Thanks, Leam. I'm interested in having a close look at it.) It's commented enough that hopefully you'll be able to make sense of the plugin(s). If I remember correctly my main goal regarding the plugin(s) was to get automatic tag completion when editing html/xml etc (which I seldom do anymore.) I'm sending it as an attachment since I believe text_only attachments do come through- if not (and you are interested) let me know and I'll resend (in the email body.) My other motive is to see if anyone has any comments/criticism (constructive or otherwise.) Alex On Mon, Nov 22, 2021 at 11:00 AM Kaushal Shriyan wrote: > Hi, > > I am using vim editor to write python scripts. Is there a way to enable > syntax, help and indentation of python script? > > Thanks in advance. I look forward to hearing from you. > > Best Regards, > > Kaushal > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From phillor9 at gmail.com Mon Nov 22 15:05:41 2021 From: phillor9 at gmail.com (Phil) Date: Tue, 23 Nov 2021 07:05:41 +1100 Subject: [Tutor] The Python way and two dimensional lists In-Reply-To: References: Message-ID: > I think you actually wanted: > > solution[0] = [{7,3}, {5}, {4,8,6}, {7,8}, {1}, {9,3}, {7,9}, {4,6,3}, > {2}] > Thank you once again Alan for your helpful advice. Once I'd given myself a chance to give this some thought I realised my mistake. I'd been fixated on doing things the Python way. > One thing you might consider is using the Python > interactive prompt I use VS Code for my main projects and IDLE for simple tasks like this one and often, maybe not often enough, use the interactive prompt to test ideas. > Another option that could work here, and is often > used in professional code, is to just do the initialisation > in a hard coded list format: That's the way I had originally set up the grid. I thought I was acting like the big boys by using list comprehension. > Obviously this technique isn't effective with huge data > sets, but for this purpose should work well and may help > you visualize things better. I use grids and look-up tables in many of my projects and hard coding them ensures that they are the right way around instead of sideways. I've always found the list comprehension method anything but comprehensible. -- Regards, Phil From phillor9 at gmail.com Mon Nov 22 15:19:37 2021 From: phillor9 at gmail.com (Phil) Date: Tue, 23 Nov 2021 07:19:37 +1100 Subject: [Tutor] The Python way and two dimensional lists In-Reply-To: References: Message-ID: > Create 9x9 of empty sets > >>>> solution = [ [ set() for _ in range(9)] for _ in range(9)] >>>> solution Thanks Dennis. I had originally used 'None' where you have 'set()'. > Bind 9 element tuple to the 9 spaces in the first "row" > >>>> solution[0][0:9] = {7,3}, {5}, {4,8,6}, {7,8}, {1}, {9,3}, {7,9}, {4,6,3}, {2} >>>> solution I haven't seen slicing used that way before. I'll keep it in mind. -- Regards, Phil From learn2program at gmail.com Mon Nov 22 18:36:59 2021 From: learn2program at gmail.com (Alan Gauld) Date: Mon, 22 Nov 2021 23:36:59 +0000 Subject: [Tutor] Enable Python feature in vim editor In-Reply-To: References: Message-ID: <7b90e0aa-6535-6ac5-6731-26581bc9f7ea@yahoo.co.uk> On 22/11/2021 19:45, Alex Kleider wrote: > I'm sending it as an attachment since I believe text_only attachments do > come through- if not (and you are interested) let me know and I'll resend > (in the email body.) I can't see it. I think its .txt attachments that come thru' An unextended filename would probably be regarded as potentially dangerous. -- 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 Nov 22 18:56:34 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 22 Nov 2021 23:56:34 +0000 Subject: [Tutor] The Python way and two dimensional lists In-Reply-To: References: Message-ID: On 22/11/2021 20:05, Phil wrote: >> One thing you might consider is using the Python >> interactive prompt > > I use VS Code for my main projects and IDLE for simple tasks like this > one and often, maybe not often enough, use the interactive prompt to > test ideas. When I'm coding a big project I have 3 windows open: 1) The editor or IDE I'm using(depends on language) 2) The interactive interpreter (depends on language) 3) an OS prompt set to run the program (I don't fully trust IDE tools to run my code, I always do a final test as an end user would) In (1) I edit, debug, navigate and version control my code In (2) I try out ideas, test function operations etc. Often copying code snippets from her into the editor. In (3) I run the "working" code from the IDE. Its amazing how often new errors or warnings pop up when you run code outside of the development tools! Nowadays there will almost certainly be a few web pages open too... But if there is an interpreter I'll almost always use it. Smalltalk programmers have the best implementation of this way of working. Python's implementation is a close second IMHO. Lisp programmers are another group who have traditionally worked this way. >> Another option that could work here, and is often >> used in professional code, is to just do the initialisation >> in a hard coded list format: > > That's the way I had originally set up the grid. I thought I was acting > like the big boys by using list comprehension. comprehensions are fine in some situations, especially single dimensions or if the initial data is derivable by formula(eg a constant). But if the data is arbitrary - like a Sudoku starting position - then the static code approach is much easier to read, write and edit. > I've always found the list comprehension method anything but comprehensible. They can quickly become incomprehensible, especially when dealing with multiple dimensions. Splitting the comp into multiple lines often helps, but somehow seems to go against the idea of a cmp in the first place.... For example: solution = [[somefunc(i) for i in data if i > limit] for n in range(20)] But then unwinding to regular code looks clearer still: solution = [] for n in range(20): row = [] for i in data: if i > limit: row.append(somefunc(i)) The art is in deciding which approach is most maintainable and performant(if that's significant, it usually isn't) -- 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 Nov 22 18:59:58 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 22 Nov 2021 23:59:58 +0000 Subject: [Tutor] The Python way and two dimensional lists In-Reply-To: References: Message-ID: On 22/11/2021 20:19, Phil wrote: >>>>> solution[0][0:9] = {7,3}, {5}, {4,8,6}, {7,8}, {1}, {9,3}, {7,9}, {4,6,3}, {2} >>>>> solution > > I haven't seen slicing used that way before. I'll keep it in mind. slice assignments are powerful but very easy to get wrong (off by one errors etc). Even Denis got it wrong first time round! That's why I prefer the more direct approach: solution[0] = [{7,3},{5},{4,8,6},{7,8},{1},{9,3},{7,9},{4,6,3},{2}] -- 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 Mon Nov 22 19:47:32 2021 From: alexkleider at gmail.com (Alex Kleider) Date: Mon, 22 Nov 2021 16:47:32 -0800 Subject: [Tutor] Enable Python feature in vim editor In-Reply-To: <7b90e0aa-6535-6ac5-6731-26581bc9f7ea@yahoo.co.uk> References: <7b90e0aa-6535-6ac5-6731-26581bc9f7ea@yahoo.co.uk> Message-ID: Foolish of me not to have added the ".txt" suffix. Hopefully this'll come thru. Vundle appears to be a way of managing vim plugins and I use it to give me tag completion: not related to Python at all. It involved 'git clone'ing two repos into the appropriate ~/.vim subdirectory ('bundle'). I remember spending a lot of time figuring out how to make copy and paste work. Also not related to Python except indirectly: I use it a lot to copy paste pearls I glean from this mailing list (and other sources) into my ~/Notes/Py/.. files. On Mon, Nov 22, 2021 at 3:38 PM Alan Gauld wrote: > > On 22/11/2021 19:45, Alex Kleider wrote: > > I'm sending it as an attachment since I believe text_only attachments do > > come through- if not (and you are interested) let me know and I'll resend > > (in the email body.) > > I can't see it. I think its .txt attachments that come thru' An > unextended filename > would probably be regarded as potentially dangerous. > > -- > > 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 > -------------- next part -------------- set autoindent set shiftwidth=4 " number of spaces line is moved by << or >> set expandtab " change tab to requred number of spaces set tabstop=4 " width of tab set softtabstop=4 " columns of whitespace removed by backspace set textwidth=70 set scrolljump=2 set scrolloff=2 " Keep at least n lines above/below cursor " set mousehide " Hide the mouse when typing " set number " Set line numbering syntax on " In insert mode, use ctrl+v to insert your system's clipboard content " (Paste mode) ! from Aaron: imap :set paste"+P :set nopastea " The following is to do with Vundle: !!! VUNDLE !!! " https://www.linuxfordevices.com/tutorials/linux/vundle set nocompatible " be iMproved, required filetype off " required " set the runtime path to include Vundle and initialize Vundle " The directory should be changed in case you download it somewhere else set rtp+=~/.vim/bundle/Vundle.vim call vundle#begin() " VundleBeginListing " let Vundle manage Vundle, required Plugin 'VundleVim/Vundle.vim' " All the plugins to be installed are to be mentioned here " inside the vundle#begin and vundle#end Plugin 'alvan/vim-closetag' call vundle#end() " VundleEndListing filetype plugin indent on " required " ...end of Vundle. !!! VundleEnd !! let g:closetag_filenames = '*.html,*.xhtml,*.phtml' " Don't know where the following came from or what it does: if has("autocmd") au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif endif From alexkleider at gmail.com Mon Nov 22 19:59:56 2021 From: alexkleider at gmail.com (Alex Kleider) Date: Mon, 22 Nov 2021 16:59:56 -0800 Subject: [Tutor] The Python way and two dimensional lists In-Reply-To: References: Message-ID: When I'm coding a big project I have 3 windows open: > 1) The editor or IDE I'm using(depends on language) > 2) The interactive interpreter (depends on language) > 3) an OS prompt set to run the program (I don't > fully trust IDE tools to run my code, I always do > a final test as an end user would) > > > In (1) I edit, debug, navigate and version control my code > In (2) I try out ideas, test function operations etc. > Often copying code snippets from her into the editor. > So (assuming you use vim) you must also have set vim up to allow copy/paste to/from system clipboard & vim. I'd be interested how you did that. (Perhaps it's set up that way by default in some Linux distributions but not so for Debian/GNU Linux (the last two releases.) From phillor9 at gmail.com Mon Nov 22 22:33:40 2021 From: phillor9 at gmail.com (Phil) Date: Tue, 23 Nov 2021 14:33:40 +1100 Subject: [Tutor] The Python way and two dimensional lists In-Reply-To: References: Message-ID: <4a8f4afe-0014-462e-6957-fa702c9b2237@gmail.com> On 23/11/21 10:56, Alan Gauld via Tutor wrote: > > When I'm coding a big project I have 3 windows open: > 1) The editor or IDE I'm using(depends on language) > 2) The interactive interpreter (depends on language) > 3) an OS prompt set to run the program (I don't > fully trust IDE tools to run my code, I always do > a final test as an end user would) I only have a smallish laptop and so I don't have enough screen space to view multiple windows. VS Code takes up the whole screen but I sometimes have IDLE running at the same time so that I can copy working bits to VS Code. I now have my Sudoku solver fully working in Python (I built the same project almost 20 years ago with QT and C++ and although it works, the code is a real mess). All I have to do now is modify some bits so that they comply with the Python way and remove the debugging print statements. -- Regards, Phil From alexkleider at gmail.com Tue Nov 23 02:38:48 2021 From: alexkleider at gmail.com (Alex Kleider) Date: Mon, 22 Nov 2021 23:38:48 -0800 Subject: [Tutor] python programming using the vim editor (was 'The Python way..') Message-ID: Bob Stepp wrote: """ Do you have a system clipboard setup? I had to get xsel going to use it with Neovim. Once you have a recognized (by Vim/Neovim) system clipboard configured, then you use the appropriate register(s). In Vim/Neovim type ":h registers", which will describe them. For instance if I have copied something to the system clipboard with the mouse and I am back in Vim/Neovim in insert mode I type "Ctrl-r, +" to paste it in. If you are in normal mode you would use either "p" or "P" as you would with one of the alphabetic registers, but instead substitute "+". """ Thanks for your attempt to help, Bob, but I must confess to not understanding! I don't know of xsel or of Neovim. I do have copy and paste working using just leftclick+C(opy or leftclick+Paste and am happy with that. I assume (but am not certain and for sure don't understand what the line is really doing) that the following line: " imap :set paste"+P :set nopastea" in my .vimrc is providing me that functionality . Having said that, I did copy some of this text into the system clipboard and then while in insert mode in vim, "Ctrl-r, +" did accomplish a paste (the same as if I'd done 'leftclick, p'! ) That's good to know. How do you 'copy' a highlighted segment from vim into your system clipboard? Cheers, Alex From alan.gauld at yahoo.co.uk Tue Nov 23 03:58:31 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 23 Nov 2021 08:58:31 +0000 Subject: [Tutor] The Python way and two dimensional lists In-Reply-To: References: Message-ID: On 23/11/2021 00:59, Alex Kleider wrote: > When I'm coding a big project I have 3 windows open: > >> 1) The editor or IDE I'm using(depends on language) >> 2) The interactive interpreter (depends on language) >> 3) an OS prompt set to run the program (I don't >> fully trust IDE tools to run my code, I always do >> a final test as an end user would) >> >> >> In (1) I edit, debug, navigate and version control my code >> In (2) I try out ideas, test function operations etc. >> Often copying code snippets from her into the editor. >> > > So (assuming you use vim) you must also have set vim up to allow copy/paste > to/from system clipboard & vim. I'd be interested how you did that. I use gvim which has buttons on the toolbar for that. If I use console vim I use the regular X cut n paste using the middle mouse button. There are vim tools for doing it, including accessing multiple clipboards etc, but I never use those. -- 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 Tue Nov 23 09:11:50 2021 From: robertvstepp at gmail.com (boB Stepp) Date: Tue, 23 Nov 2021 08:11:50 -0600 Subject: [Tutor] python programming using the vim editor (was 'The Python way..') In-Reply-To: References: Message-ID: On 21/11/22 11:38PM, Alex Kleider wrote: > >Bob Stepp wrote: >""" >Do you have a system clipboard setup?? I had to get xsel going to use it with >Neovim. > >Once you have a recognized (by Vim/Neovim) system clipboard configured, then >you use the appropriate register(s).? In Vim/Neovim type ":h registers", which >will describe them.? For instance if I have copied something to the system >clipboard with the mouse and I am back in Vim/Neovim in insert mode I type >"Ctrl-r, +" to paste it in.? If you are in normal mode you would use either >"p" or "P" as you would with one of the alphabetic registers, but instead >substitute "+". >""" >Thanks for your attempt to help, Bob, but I must confess to not understanding! >I don't know of xsel or of Neovim... xsel is one of a few *nix programs for interacting with the X server clipboard selections. It has nothing to do with the Vim/Neovim editors other than they can make use of them. You can Google for xsel or use your man pages for more info. Neovim is just a fork of Vim that so far retains compatibility with Vim, so for most purposes Vim and Neovim are interchangeable. >? ...I do have copy and paste working using just >leftclick+C(opy or leftclick+Paste and am happy with that. >I assume (but am not certain and for sure don't understand what the line is >really doing) that the following line: >" imap :set paste"+P :set nopastea" >in my .vimrc is providing me that functionality . >Having said that, I did copy some of this text into the system clipboard and >then while in insert mode in vim, "Ctrl-r, +" did accomplish a paste (the same >as if I'd done 'leftclick, p'! )? That's good to know. If this worked than you can forget about xsel. Sounds like you did not have to worry about the issues I originally had. >How do you 'copy' a highlighted segment from vim into your system clipboard? Just yank the selection into the "+" register just like you would yank text into a lettered register. Hope I'm clearer this time. Must go to work! BTW, you might want to Google or manpage xsel or whatever your system uses. X Server/X Windows has THREE selections it deals with: Primary, Secondary and Clipboard. It is useful to know the differences for both your mouse functionality and using Vim and whatnot. -- Wishing you only the best, boB Stepp "Human life begins of the far side of despair." -- Jean-Paul Sartre From mats at wichmann.us Tue Nov 23 09:39:05 2021 From: mats at wichmann.us (Mats Wichmann) Date: Tue, 23 Nov 2021 07:39:05 -0700 Subject: [Tutor] Enable Python feature in vim editor In-Reply-To: <855852f9-3481-2b87-4a4c-9108a90c4002@gmail.com> References: <855852f9-3481-2b87-4a4c-9108a90c4002@gmail.com> Message-ID: On 11/22/21 12:06, Leam Hall wrote: > Hey Kaushal, > > Here's part of my ~/.vimrc. You can do a lot with it, and there are > likely Python specific plugins. > > ### > set shiftwidth=2 > set shiftround > set tabstop=4 > set softtabstop=4 > set noexpandtab > set number > set ruler > set noautoindent > set nosmartindent > set noincsearch > > syntax enable > ### those are some pretty odd settings... for Python autoindent would seem more natural than noautoindent, and noexpandtab means tabs have the unfortunate opportunity to live in your edited content - you're pretty much agreeing to not use the tab key in Python coding in that case. Curious how you came to these? From k.d.jantzen at mailbox.org Tue Nov 23 10:39:41 2021 From: k.d.jantzen at mailbox.org (Klaus Jantzen) Date: Tue, 23 Nov 2021 16:39:41 +0100 Subject: [Tutor] Enable Python feature in vim editor In-Reply-To: References: Message-ID: Hi, in my .vimrc I have the following Python related entries: ============ au BufRead, BufNewFile *.py call SetPythonOptions() function SetPythonOptions() ??? autocmd! ??? set tabstop=4 ??? set softtabstop=4 ??? set shiftwidth=4 ??? " use spaces instead of tab ??? set expandtab ??? set autoindent endfunction " remove trailing blanks in Python files au BufWritePre *.py %s/\s\+$//e ============== K.D.J. On 11/22/21 5:48 PM, Kaushal Shriyan wrote: > Hi, > > I am using vim editor to write python scripts. Is there a way to enable > syntax, help and indentation of python script? > > Thanks in advance. I look forward to hearing from you. > > Best Regards, > > Kaushal > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From leamhall at gmail.com Tue Nov 23 11:01:35 2021 From: leamhall at gmail.com (Leam Hall) Date: Tue, 23 Nov 2021 10:01:35 -0600 Subject: [Tutor] Enable Python feature in vim editor In-Reply-To: References: <855852f9-3481-2b87-4a4c-9108a90c4002@gmail.com> Message-ID: On 11/23/21 08:39, Mats Wichmann wrote: > On 11/22/21 12:06, Leam Hall wrote: >> Hey Kaushal, >> >> Here's part of my ~/.vimrc. You can do a lot with it, and there are likely Python specific plugins. > > those are some pretty odd settings...? for Python autoindent would seem more natural than noautoindent, and noexpandtab means tabs have the unfortunate opportunity to live in your edited content - you're pretty much agreeing to not use the tab key in Python coding in that case. > > Curious how you came to these? Hey Mats! When I first learned Python, the "whitespace is significant" thing made a lot of sense. After that my shell, Perl, and Ruby code looked a lot like my Python. :) I've been on a Go phase for a bit, and those are Go settings, to match "go fmt". I'd agree though, for Python I prefer two spaces as indent, and no tabs in code at all. Leam -- Systems Programmer (reuel.net/resume) Scribe: The Domici War (domiciwar.net) General Ne'er-do-well (github.com/LeamHall) From tariqkhasiri at gmail.com Tue Nov 23 06:51:52 2021 From: tariqkhasiri at gmail.com (Tariq Khasiri) Date: Tue, 23 Nov 2021 17:51:52 +0600 Subject: [Tutor] code explanation Message-ID: ``` data = [[col, df[col].nunique()] for col in df.columns.difference(["Reviews"])] uniques = pd.DataFrame(data=data, columns=["columns", "num of unique values"]) ``` The first line is explaining the dataframe ( df) unique number of elements in a column but i dont understand the the second half of the first line of coding neither the second line of code. Can anyone help ? From padowns at gmail.com Tue Nov 23 13:04:51 2021 From: padowns at gmail.com (Peter Downs) Date: Tue, 23 Nov 2021 10:04:51 -0800 Subject: [Tutor] Enable Python feature in vim editor In-Reply-To: References: Message-ID: <6e0e6524-7479-cf1f-0734-e53327434119@gmail.com> Hey Klaus, I like how you wrap the language specific settings in a function instead of having a bunch of separate autocmd lines like I do. I also map function keys to tools like linters and the interpreter: ==8<== autocmd BufNewFile,BufRead *.py nnoremap :w:exec '!clear; pylint' shellescape(@%, 1) autocmd BufNewFile,BufRead *.py nnoremap :w:exec '!clear; pylama' shellescape(@%, 1) autocmd BufNewFile,BufRead *.py nnoremap :w:exec '!clear; python3' shellescape(@%, 1) ==8<== that way you can test the code or check the syntax without leaving vim or switching windows. Peter On 11/23/21 7:39 AM, Klaus Jantzen via Tutor wrote: > Hi, > > in my .vimrc I have the following Python related entries: > > ============ > > > au BufRead, BufNewFile *.py call SetPythonOptions() > function SetPythonOptions() > ??? autocmd! > ??? set tabstop=4 > ??? set softtabstop=4 > ??? set shiftwidth=4 > ??? " use spaces instead of tab > ??? set expandtab > ??? set autoindent > endfunction > > " remove trailing blanks in Python files > au BufWritePre *.py %s/\s\+$//e > > ============== > > K.D.J. > > On 11/22/21 5:48 PM, Kaushal Shriyan wrote: >> Hi, >> >> I am using vim editor to write python scripts. Is there a way to enable >> syntax, help and indentation of python script? >> >> Thanks in advance. I look forward to hearing from you. >> >> Best Regards, >> >> Kaushal >> _______________________________________________ >> Tutor maillist? -? Tutor at python.org >> To unsubscribe or change subscription options: >> https://mail.python.org/mailman/listinfo/tutor > _______________________________________________ > Tutor maillist? -? Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From david at lowryduda.com Tue Nov 23 14:30:10 2021 From: david at lowryduda.com (David Lowry-Duda) Date: Tue, 23 Nov 2021 14:30:10 -0500 Subject: [Tutor] Enable Python feature in vim editor In-Reply-To: References: Message-ID: On Mon, Nov 22, 2021 at 10:18:19PM +0530, Kaushal Shriyan wrote: > I am using vim editor to write python scripts. Is there a way to > enable syntax, help and indentation of python script? Welcome to the vim way! I also use vim as my primary python editor. I'll note that there has been a recent surge in power from language servers, and tools like vim-ale (https://github.com/dense-analysis/ale) or coc.nvim (https://github.com/neoclide/coc.nvim) [which I am led to believe works also for vim and not just neovim, but in fact I have not used it] might be very helpful. These enable various forms of autocompletion, definitions, signature help, mypy-type checking, and so on, from within vim. I suggest that you consider these and whether or not you think they would be helpful in your setup. And though it's actually quite old, I also recommend using some form of ctags for large projects, such as (https://github.com/universal-ctags/ctags). This facilitates quickly jumping to definitions, functions, and classes in a project. - DLD From cs at cskk.id.au Tue Nov 23 17:30:05 2021 From: cs at cskk.id.au (Cameron Simpson) Date: Wed, 24 Nov 2021 09:30:05 +1100 Subject: [Tutor] Enable Python feature in vim editor In-Reply-To: References: Message-ID: On 23Nov2021 10:01, Leam Hall wrote: >When I first learned Python, the "whitespace is significant" thing made >a lot of sense. After that my shell, Perl, and Ruby code looked a lot >like my Python. :) I've been on a Go phase for a bit, and those are >Go settings, to match "go fmt". I'd agree though, for Python I prefer >two spaces as indent, and no tabs in code at all. I've got quite a lot in my vi and vim settings, so I'll run through the things I've got which seem applicable to Python (and coding in general): First my "vi" settings (sourced by my vimrc): set autoindent Great for almost all coding; remember that at the start of a line ^D (ctrl-D) steps you backwards one indent step when you need to out-indent. I've got some autoindenting syntax stuff turned on too, so often I instead write the next line, let the auto stuff mangle the intended indent, and adjust with "<<" to left shift the line. Oh, were you aware of the "<" and ">" keystrokes? Like many vim commands, followed by a cursor motion (or doubled to just affect the current line). So "<<" shifts the current line, "<}" shifts the lines from here the next empty line (great for shifting a whole code block), "20<<" shifts the next 20 lines including the current one, etc. It honours the "shiftwidth" setting below; mine is set to 2. Note that having autoformatting (see below) reduces the need for this, but it is still very useful - I use it a lot. Including to indent the settings listing for this message! set autowrite Writes the file when I switch to another, so switching files is just ":n filename". I pretty much always use revision control, so I am not very hung up on unwanted changes - a diff will always show them to me. set ignorecase Just makes searching easier all around. Note that a mixed case search has a degree of case sensitivity anyway, so when you care about the case you can be more precise. " set nomesg set nowrapscan I like to know when I've hit the last match instead of cycling to the top of the file - in a big file that isn't always obvious. set showmatch Bounces the cursor to the matching bracket when you type a closing bracket, and also highlights the starting bracking. Quite ahndy. set shiftwidth=2 Doesn't everyone indent (and therefore shift) by 2 spaces? " set tabstop=8 set tabstop=4 Not too big, not too small. I use expandtabs, so I don't embed TABs in my code. set noterse set writeany Goes with autowrite. map! \c cs at cskk.id.au map! \C Cameron Simpson <\c> Handy for writing my email address. " Markdown: `code` a word map `` lbi`ea` I write MarkDown docstrings and like MarkDown in general for a lot of simple doco. This defines a `` macro to put backticks around the current word, very handy for embedding "computer names" in text. map!  :stop a map!  a map!  i map!  lli map!  I Suspend (^Z) and some emacs-like cursor motions. The suspend is pretty ancient, I think that works transparently in vim these days. And I rarely suspend in a multiwindow/multitab/multipane environment. map # :n # z. So handy. Switch between this file and the previous with a single keystroke. map g Gz. I find typing an uppercase G to go to a line tiresome. map t :ta "t" shorthand for :td (go to tag). I use tagfiles agressively, and they work well in Python. Note that ctags also makes tags for filenames, so finding a source file in a large hierarchy is also much easier. I've got a couple of shell scripts to make regenerating tags files easier too: https://hg.sr.ht/~cameron-simpson/css/browse/bin/ctags-update and a shell function "uptags" to run it: uptags () { >> tags ctags-update -o tags ${1+"$@"} ls -ld tags } Note in particular the ctags options I use at the bottom of "ctags-update". Then the vim settings: source ~/rc/vi Of course. set encoding=utf-8 scriptencoding utf-8 This is the way. set bg=dark colorscheme cameron syntax reset syntax on Colours. set autoread See and load the file if it is not modified and the original changes. This means I can have multiple vims watching the same file. I reflexively save quite often (and autosave above does it a lot too) so my buffers are usually unmodified. set backupcopy=yes I write in place (hard link paranoia) so a backup is handy during rewrites. set clipboard=autoselect Doesn't work as well as I've like. set cursorline I find it useful to highlight the current line - I've usualy got at least 2 buffers visible so this aids not losing track. set expandtab Of course. I sometimes like TABs to typing an indent, but not for the text itself, which should have spaces. set fileformats=unix,mac,dos Just cope and don't mangle things. set ignorecase set incsearch Nicer searching. set list set listchars=tab:?\ ,trail:_,extends:>,precedes:<,conceal:* Stuff I like visible in "list" mode. set mouse= set mousefocus=on set mousehide set mousemodel=extend What I've landed on for mouse integration. I found the other modes either annoying or not working seamlessly. In particular I like to GUI-level copy/paste with the mouse and mouse integration broke that just for the vim windows, so annoying. set nofsync Overuse of fsync is just bad. set scrolloff=2 2 lines of text visible at the top/bottom of the screen WRT to the cursor line. set smartcase I think this is the clever "mixed case searches are case senstivie" thing. set t_Co=256 I've got a 256 colour terminal emulator (IIRC). set ttyfast An ancient setting I think. set updatetime=2000 Polling interval for external changes? set wildignore+=*.swp,*.zip,*.exe,*.a,*.o,*.pyc set wildmenu wildmode=list:longest Files to ignore when autocompleting filenames. map :split % map :new:n "vim window mode", so useful. I use ^W^T to do a horizontal split (matches with Cmd-Shift-T in iTerm to do that same with panes), ^W^V for a vertical split (atches with Cmd-Shift-V in iTerm). I've got similar bindings in tmux, too. map ,f :silent !format % The big winner. Macro to autoformat this file using a code formatter. The "format" script is my own, here: https://hg.sr.ht/~cameron-simpson/css/browse/bin-cs/format which runs my preferred formatter (language specific) on the file. See the script for details, write your own! Or adopt and adapt! autocmd CursorHold * checktime | call feedkeys("") autocmd FocusGained,BufEnter * :checktime These are tickers to drive the automatic detection of external changes. So useful! " Remove ALL autocommands for the current group. " autocmd! autocmd BufWritePost *.go :silent !echo; format -t .autoformat % autocmd BufWritePost *.php :silent !echo; format -t .autoformat % autocmd BufWritePost *.js :silent !echo; format -t .autoformat % autocmd BufWritePost *.py :silent !echo; format -t .autoformat % This automatically autoformats various file extensions on save, _if_ the source tree has a nonempty ".autoformat" file in it. So I go: echo 1 >.autoformat in my code tree and off we go! There are trees where you don't want magic autoformatting, particularly existing codebases where you don't want autoformat noise in your commits. let @t=system('tags=$(findup tags) || tags=tags; echo "set tags=$tags"') @t Set the "tags" vi/vim setting if you've just made a fresh tags file. autocmd BufWritePost * :let @t=system('vim-ctags-update ' . shellescape(@%)) autocmd BufWritePost * :@t autocmd BufWritePost * :redraw! Update the tags when you save a file. autocmd BufRead,BufNewFile haproxy* set ft=haproxy Recognise an addition file syntax based on filename. if has("gui_macvim") set macthinstrokes endif Setting for GUI Vim on a Mac, I never use it. Cheers, Cameron Simpson From leamhall at gmail.com Tue Nov 23 21:07:05 2021 From: leamhall at gmail.com (Leam Hall) Date: Tue, 23 Nov 2021 20:07:05 -0600 Subject: [Tutor] Enable Python feature in vim editor In-Reply-To: References: Message-ID: <1607f623-4125-bdc9-251c-0e78cc282a21@gmail.com> On 11/23/21 16:30, Cameron Simpson wrote: ... A lot of cool stuff. ... Seriously, man, you need to write that in a tutorial! Leam -- Systems Programmer (reuel.net/resume) Scribe: The Domici War (domiciwar.net) General Ne'er-do-well (github.com/LeamHall) From alexkleider at gmail.com Wed Nov 24 12:00:00 2021 From: alexkleider at gmail.com (Alex Kleider) Date: Wed, 24 Nov 2021 09:00:00 -0800 Subject: [Tutor] Enable Python feature in vim editor In-Reply-To: <1607f623-4125-bdc9-251c-0e78cc282a21@gmail.com> References: <1607f623-4125-bdc9-251c-0e78cc282a21@gmail.com> Message-ID: On Tue, Nov 23, 2021 at 6:08 PM Leam Hall wrote: > On 11/23/21 16:30, Cameron Simpson wrote: > > ... > A lot of cool stuff. > ... > > Seriously, man, you need to write that in a tutorial! > > Leam > > ++ I for one would certainly be interested in such a write up (especially if it was flushed out a bit so I could more fully understand the code.) > -- > Systems Programmer (reuel.net/resume) > Scribe: The Domici War (domiciwar.net) > General Ne'er-do-well (github.com/LeamHall) > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From kaushalshriyan at gmail.com Wed Nov 24 12:15:48 2021 From: kaushalshriyan at gmail.com (Kaushal Shriyan) Date: Wed, 24 Nov 2021 22:45:48 +0530 Subject: [Tutor] Enable Python feature in vim editor In-Reply-To: References: <1607f623-4125-bdc9-251c-0e78cc282a21@gmail.com> Message-ID: On Wed, Nov 24, 2021 at 10:30 PM Alex Kleider wrote: > On Tue, Nov 23, 2021 at 6:08 PM Leam Hall wrote: > > > On 11/23/21 16:30, Cameron Simpson wrote: > > > > ... > > A lot of cool stuff. > > ... > > > > Seriously, man, you need to write that in a tutorial! > > > > Leam > > > > > ++ > > I for one would certainly be interested in such a write up (especially if > it was flushed out a bit so I could more fully understand the code.) > > > -- > > Systems Programmer (reuel.net/resume) > > Scribe: The Domici War (domiciwar.net) > > General Ne'er-do-well (github.com/LeamHall) > > _______________________________________________ > > Tutor maillist - Tutor at python.org > > To unsubscribe or change subscription options: > > https://mail.python.org/mailman/listinfo/tutor > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor Hi, I am running CentOS Linux release 7.9.2009 (Core) and have enabled below entry in .vimrc file $cat .vimrc set shiftwidth=2 set shiftround set tabstop=4 set softtabstop=4 set noexpandtab set number set ruler set noautoindent set nosmartindent set noincsearch syntax enable $ when i do vim testpythonscript.py I am facing the below Pymode error $vim testpythonscript.py [Pymode]: error: Pymode requires vim compiled with +python3 (exclusively). Most of features will be disabled. 0,0-1 All $vim --version VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Dec 15 2020 16:44:08) Included patches: 1-207, 209-629 Modified by Compiled by Huge version without GUI. Features included (+) or not (-): +acl +farsi +mouse_netterm +syntax +arabic +file_in_path +mouse_sgr +tag_binary +autocmd +find_in_path -mouse_sysmouse +tag_old_static -balloon_eval +float +mouse_urxvt -tag_any_white -browse +folding +mouse_xterm -tcl ++builtin_terms -footer +multi_byte +terminfo +byte_offset +fork() +multi_lang +termresponse +cindent +gettext -mzscheme +textobjects -clientserver -hangul_input +netbeans_intg +title -clipboard +iconv +path_extra -toolbar +cmdline_compl +insert_expand +perl +user_commands +cmdline_hist +jumplist +persistent_undo +vertsplit +cmdline_info +keymap +postscript +virtualedit +comments +langmap +printer +visual +conceal +libcall +profile +visualextra +cryptv +linebreak +python/dyn +viminfo +cscope +lispindent -python3 +vreplace +cursorbind +listcmds +quickfix +wildignore +cursorshape +localmap +reltime +wildmenu +dialog_con -lua +rightleft +windows +diff +menu +ruby/dyn +writebackup +digraphs +mksession +scrollbind -X11 -dnd +modify_fname +signs -xfontset -ebcdic +mouse +smartindent -xim +emacs_tags -mouseshape -sniff -xsmp +eval +mouse_dec +startuptime -xterm_clipboard +ex_extra +mouse_gpm +statusline -xterm_save +extra_search -mouse_jsbterm -sun_workshop -xpm system vimrc file: "/etc/vimrc" user vimrc file: "$HOME/.vimrc" 2nd user vimrc file: "~/.vim/vimrc" user exrc file: "$HOME/.exrc" fall-back for $VIM: "/etc" f-b for $VIMRUNTIME: "/usr/share/vim/vim74" Compilation: gcc -c -I. -Iproto -DHAVE_CONFIG_H -O2 -g -pipe -Wall -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D__linux__ -D_REENTRANT -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1 Linking: gcc -L. -Wl,-z,relro -fstack-protector -rdynamic -Wl,-export-dynamic -Wl,--enable-new-dtags -Wl,-rpath,/usr/lib64/perl5/CORE -Wl,-z,relro -L/usr/local/lib -Wl,--as-needed -o vim -lm -lnsl -lselinux -lncurses -lacl -lattr -lgpm -ldl -Wl,--enable-new-dtags -Wl,-rpath,/usr/lib64/perl5/CORE -fstack-protector -L/usr/lib64/perl5/CORE -lperl -lresolv -lnsl -ldl -lm -lcrypt -lutil -lpthread -lc # Please guide. Thanks in advance. Best Regards, Kaushal From marjababernard at gmail.com Wed Nov 24 15:17:15 2021 From: marjababernard at gmail.com (Bernard Marjaba) Date: Wed, 24 Nov 2021 15:17:15 -0500 Subject: [Tutor] Error When Using CoolProp Message-ID: Hello I am having trouble using CoolProp with python. I have macOS Big Sur 11.5.1 with python version 3.10. import CoolProp.CoolProp as CP is giving me the error ModuleNotFoundError: No module named ?CoolProp.CoolProp? because it is not installed. I am however using it with MATLAB with no issues. I assume each software has its own wrapper? So I tried installing CoolProp using pip install coolprop, and got the following error: Collecting coolprop Using cached CoolProp-6.4.1.tar.gz (12.9 MB) Preparing metadata (setup.py) ... done Using legacy 'setup.py install' for coolprop, since package 'wheel' is not installed. Installing collected packages: coolprop Running setup.py install for coolprop ... error ERROR: Command errored out with exit status 1: command: '/Users/Bernard/PycharmProjects/Isentropic SUS Model/venv/bin/python' -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/1j/jv39r0t12cb4dhscxkdz97740000gn/T/pip-install-z5afcrzx/coolprop_6739af7137474652994855ae32a03dd4/setup.py'"'"'; __file__='"'"'/private/var/folders/1j/jv39r0t12cb4dhscxkdz97740000gn/T/pip-install-z5afcrzx/coolprop_6739af7137474652994855ae32a03dd4/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /private/var/folders/1j/jv39r0t12cb4dhscxkdz97740000gn/T/pip-record-8453i4i7/install-record.txt --single-version-externally-managed --compile --install-headers '/Users/Bernard/PycharmProjects/Isentropic SUS Model/venv/include/site/python3.9/coolprop' cwd: /private/var/folders/1j/jv39r0t12cb4dhscxkdz97740000gn/T/pip-install-z5afcrzx/coolprop_6739af7137474652994855ae32a03dd4/ Complete output (79 lines): xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun OSX build detected, targetting 10.9 using clang/gcc v0.0. Cython will not be used; cy_ext is cpp running install /Users/Bernard/PycharmProjects/Isentropic SUS Model/venv/lib/python3.9/site-packages/setuptools/command/install.py:34: SetuptoolsDeprecationWarning: setup.py install is deprecated. Use build and pip and other standards-based tools. warnings.warn( running build running build_py creating build creating build/lib.macosx-10.9-x86_64-3.9 creating build/lib.macosx-10.9-x86_64-3.9/CoolProp copying CoolProp/constants.py -> build/lib.macosx-10.9-x86_64-3.9/CoolProp copying CoolProp/__init__.py -> build/lib.macosx-10.9-x86_64-3.9/CoolProp copying CoolProp/BibtexParser.py -> build/lib.macosx-10.9-x86_64-3.9/CoolProp copying CoolProp/HumidAirProp.py -> build/lib.macosx-10.9-x86_64-3.9/CoolProp copying CoolProp/State.py -> build/lib.macosx-10.9-x86_64-3.9/CoolProp creating build/lib.macosx-10.9-x86_64-3.9/CoolProp/tests copying CoolProp/tests/runner.py -> build/lib.macosx-10.9-x86_64-3.9/CoolProp/tests copying CoolProp/tests/test_plots.py -> build/lib.macosx-10.9-x86_64-3.9/CoolProp/tests copying CoolProp/tests/test_Props.py -> build/lib.macosx-10.9-x86_64-3.9/CoolProp/tests copying CoolProp/tests/__init__.py -> build/lib.macosx-10.9-x86_64-3.9/CoolProp/tests copying CoolProp/tests/test_CoolPropState.py -> build/lib.macosx-10.9-x86_64-3.9/CoolProp/tests creating build/lib.macosx-10.9-x86_64-3.9/CoolProp/GUI copying CoolProp/GUI/__init__.py -> build/lib.macosx-10.9-x86_64-3.9/CoolProp/GUI copying CoolProp/GUI/CoolPropGUI.py -> build/lib.macosx-10.9-x86_64-3.9/CoolProp/GUI copying CoolProp/GUI/PsychScript.py -> build/lib.macosx-10.9-x86_64-3.9/CoolProp/GUI creating build/lib.macosx-10.9-x86_64-3.9/CoolProp/Plots copying CoolProp/Plots/ConsistencyPlots_pcsaft.py -> build/lib.macosx-10.9-x86_64-3.9/CoolProp/Plots copying CoolProp/Plots/PsychChart.py -> build/lib.macosx-10.9-x86_64-3.9/CoolProp/Plots copying CoolProp/Plots/SimpleCycles.py -> build/lib.macosx-10.9-x86_64-3.9/CoolProp/Plots copying CoolProp/Plots/__init__.py -> build/lib.macosx-10.9-x86_64-3.9/CoolProp/Plots copying CoolProp/Plots/psy.py -> build/lib.macosx-10.9-x86_64-3.9/CoolProp/Plots copying CoolProp/Plots/Plots.py -> build/lib.macosx-10.9-x86_64-3.9/CoolProp/Plots copying CoolProp/Plots/Common.py -> build/lib.macosx-10.9-x86_64-3.9/CoolProp/Plots copying CoolProp/Plots/SimpleCyclesCompression.py -> build/lib.macosx-10.9-x86_64-3.9/CoolProp/Plots copying CoolProp/Plots/ConsistencyPlots.py -> build/lib.macosx-10.9-x86_64-3.9/CoolProp/Plots copying CoolProp/Plots/PsychScript.py -> build/lib.macosx-10.9-x86_64-3.9/CoolProp/Plots copying CoolProp/Plots/Tests.py -> build/lib.macosx-10.9-x86_64-3.9/CoolProp/Plots copying CoolProp/Plots/SimpleCyclesExpansion.py -> build/lib.macosx-10.9-x86_64-3.9/CoolProp/Plots copying CoolProp/typedefs.pxd -> build/lib.macosx-10.9-x86_64-3.9/CoolProp copying CoolProp/CoolProp.pxd -> build/lib.macosx-10.9-x86_64-3.9/CoolProp copying CoolProp/State.pxd -> build/lib.macosx-10.9-x86_64-3.9/CoolProp copying CoolProp/cAbstractState.pxd -> build/lib.macosx-10.9-x86_64-3.9/CoolProp copying CoolProp/constants_header.pxd -> build/lib.macosx-10.9-x86_64-3.9/CoolProp copying CoolProp/AbstractState.pxd -> build/lib.macosx-10.9-x86_64-3.9/CoolProp copying CoolProp/Plots/psyrc -> build/lib.macosx-10.9-x86_64-3.9/CoolProp/Plots running build_ext creating private creating private/var creating private/var/folders creating private/var/folders/1j creating private/var/folders/1j/jv39r0t12cb4dhscxkdz97740000gn creating private/var/folders/1j/jv39r0t12cb4dhscxkdz97740000gn/T creating private/var/folders/1j/jv39r0t12cb4dhscxkdz97740000gn/T/pip-install-z5afcrzx creating private/var/folders/1j/jv39r0t12cb4dhscxkdz97740000gn/T/pip-install-z5afcrzx/coolprop_6739af7137474652994855ae32a03dd4 gcc -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -arch x86_64 -g -I/Users/Bernard/PycharmProjects/Isentropic SUS Model/venv/include -I/Library/Frameworks/Python.framework/Versions/3.9/include/python3.9 -c /private/var/folders/1j/jv39r0t12cb4dhscxkdz97740000gn/T/pip-install-z5afcrzx/coolprop_6739af7137474652994855ae32a03dd4/tmp0hdk2tsu.cpp -o private/var/folders/1j/jv39r0t12cb4dhscxkdz97740000gn/T/pip-install-z5afcrzx/coolprop_6739af7137474652994855ae32a03dd4/tmp0hdk2tsu.o xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun gcc -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -arch x86_64 -g -I/Users/Bernard/PycharmProjects/Isentropic SUS Model/venv/include -I/Library/Frameworks/Python.framework/Versions/3.9/include/python3.9 -c /private/var/folders/1j/jv39r0t12cb4dhscxkdz97740000gn/T/pip-install-z5afcrzx/coolprop_6739af7137474652994855ae32a03dd4/tmp0r3vfej0.cpp -o private/var/folders/1j/jv39r0t12cb4dhscxkdz97740000gn/T/pip-install-z5afcrzx/coolprop_6739af7137474652994855ae32a03dd4/tmp0r3vfej0.o xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun gcc -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -arch x86_64 -g -I/Users/Bernard/PycharmProjects/Isentropic SUS Model/venv/include -I/Library/Frameworks/Python.framework/Versions/3.9/include/python3.9 -c /private/var/folders/1j/jv39r0t12cb4dhscxkdz97740000gn/T/pip-install-z5afcrzx/coolprop_6739af7137474652994855ae32a03dd4/tmp93394q_4.cpp -o private/var/folders/1j/jv39r0t12cb4dhscxkdz97740000gn/T/pip-install-z5afcrzx/coolprop_6739af7137474652994855ae32a03dd4/tmp93394q_4.o xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun Adding these shared_ptr compilation macros: [] building 'CoolProp.CoolProp' extension creating build/temp.macosx-10.9-x86_64-3.9 creating build/temp.macosx-10.9-x86_64-3.9/CoolProp creating build/temp.macosx-10.9-x86_64-3.9/src creating build/temp.macosx-10.9-x86_64-3.9/src/Backends creating build/temp.macosx-10.9-x86_64-3.9/src/Backends/Cubics creating build/temp.macosx-10.9-x86_64-3.9/src/Backends/Helmholtz creating build/temp.macosx-10.9-x86_64-3.9/src/Backends/Helmholtz/Fluids creating build/temp.macosx-10.9-x86_64-3.9/src/Backends/IF97 creating build/temp.macosx-10.9-x86_64-3.9/src/Backends/Incompressible creating build/temp.macosx-10.9-x86_64-3.9/src/Backends/PCSAFT creating build/temp.macosx-10.9-x86_64-3.9/src/Backends/REFPROP creating build/temp.macosx-10.9-x86_64-3.9/src/Backends/Tabular creating build/temp.macosx-10.9-x86_64-3.9/src/Tests gcc -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -arch x86_64 -g -I. -I./include -I./src -I./externals/Eigen -I./externals/fmtlib -I./externals/msgpack-c/include -I/Users/Bernard/PycharmProjects/Isentropic SUS Model/venv/include -I/Library/Frameworks/Python.framework/Versions/3.9/include/python3.9 -c CoolProp/CoolProp.cpp -o build/temp.macosx-10.9-x86_64-3.9/CoolProp/CoolProp.o xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun error: command '/usr/bin/gcc' failed with exit code 1 ---------------------------------------- ERROR: Command errored out with exit status 1: '/Users/Bernard/PycharmProjects/Isentropic SUS Model/venv/bin/python' -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/1j/jv39r0t12cb4dhscxkdz97740000gn/T/pip-install-z5afcrzx/coolprop_6739af7137474652994855ae32a03dd4/setup.py'"'"'; __file__='"'"'/private/var/folders/1j/jv39r0t12cb4dhscxkdz97740000gn/T/pip-install-z5afcrzx/coolprop_6739af7137474652994855ae32a03dd4/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /private/var/folders/1j/jv39r0t12cb4dhscxkdz97740000gn/T/pip-record-8453i4i7/install-record.txt --single-version-externally-managed --compile --install-headers '/Users/Bernard/PycharmProjects/Isentropic SUS Model/venv/include/site/python3.9/coolprop' Check the logs for full command output. Thanks and regards, Bernard Marjaba (514) 922-9807 From alan.gauld at yahoo.co.uk Thu Nov 25 03:52:01 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 25 Nov 2021 08:52:01 +0000 Subject: [Tutor] code explanation In-Reply-To: References: Message-ID: On 23/11/2021 11:51, Tariq Khasiri wrote: > ``` > data = [[col, df[col].nunique()] for col in > df.columns.difference(["Reviews"])] > uniques = pd.DataFrame(data=data, columns=["columns", "num of unique > values"]) > > ``` > > The first line is explaining the dataframe ( df) unique number of > elements in a column but i dont understand the the second half of the first > line of coding neither the second line of code. Can anyone help ? I'm not sure what aspect you don't understand so I'll start by stating that the first two lines of code are a single line of Python split for readability. I'll simplify it so that it can be seen on a single line: func1 = df[0].nunique #0 is arbitrary, assumes all items of same type func2 = df.columns.difference data = [[cl, func()] for cl in func2(["Reviews"])] So the line is a list comprehension making use of two functions. If that is not what was confusing you please repost with more specifics. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alan.gauld at yahoo.co.uk Thu Nov 25 03:58:13 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 25 Nov 2021 08:58:13 +0000 Subject: [Tutor] Error When Using CoolProp In-Reply-To: References: Message-ID: On 24/11/2021 20:17, Bernard Marjaba wrote: > Hello > > I am having trouble using CoolProp with python. I have macOS Big Sur 11.5.1 with python version 3.10. > > import CoolProp.CoolProp as CP > is giving me the error ModuleNotFoundError: No module named ?CoolProp.CoolProp? because it is not installed. I am however using it with MATLAB with no issues. I assume each software has its own wrapper? I've never heard of coolprop but, in general, binary packages will have separate wrappers for each application that uses them, so Python and Matlab will each need their own wrapper. As to what's going wrong with the pip I have no idea. Are you using a virtual environment? Are you calling pip directly or using "python -m pip..." Perhaps somebody else can respond. Or you might like to post on any coolprop support fora. > Running setup.py install for coolprop ... error > ERROR: Command errored out with exit status 1: -- 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 Fri Nov 26 11:06:08 2021 From: manpritsinghece at gmail.com (Manprit Singh) Date: Fri, 26 Nov 2021 21:36:08 +0530 Subject: [Tutor] Using super() in classes Message-ID: Dear sir, Consider the below given examples : class ABC: def show(self): print("HELLO") class ABCD(ABC): def show(self): print("HI") super().show() obj = ABCD() obj.show() will give output as : HI HELLO Which is my desired output, here i have used super() inside class ABCD, so that I can call show() of class ABC inside ABCD. Here If I write self.show() instead of super().show(), it will call show() of class ABCD and it will proceed like a recursive function . Just need to know if my explanation is correct or not . Secondarily the above given given example can be written in the below written way or not ? Here instead of writing super().show() in class ABCD i have written ABC.show(self) .According to me it will serve the same purpose as that of the above written classes. Am I correct ? class ABC: def show(self): print("HELLO") class ABCD(ABC): def show(self): print("HI") ABC.show(self) obj = ABCD() obj.show() My second Example where i am calling show() of parent class inside a method of child class whose name is not show(), but child class has a method with name show, You can see i have called show() of parent class by writing super().show() inside the greeting method of child class . It it ok to do like this ? class ABC: def show(self): print("HELLO") class ABCD(ABC): def show(self): print("HI") def greeting(self, Name): super().show() print(Name) obj = ABCD() obj.greeting("Ravi") Which gives the desired output as below: HELLO Ravi Kindly guidr From alan.gauld at yahoo.co.uk Fri Nov 26 11:52:37 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 26 Nov 2021 16:52:37 +0000 Subject: [Tutor] Using super() in classes In-Reply-To: References: Message-ID: On 26/11/2021 16:06, Manprit Singh wrote: > class ABC: > def show(self): > print("HELLO") > > class ABCD(ABC): > def show(self): > print("HI") > super().show() > > obj = ABCD() > obj.show() > Just need to know if my explanation is correct or not . Yes, all good so far. > given example can be written in the below written way or not ? Here instead > of writing super().show() in class ABCD i have written ABC.show(self) .According > to me it will serve the same purpose as that of the above written classes. > > Am I correct ? In the case of single inheritance 9ie only one superclass) yes. And that is how most Python code prior to v3 was written. And for single inheritance, often still is. But for multiple inheritance super() may behave differently, especially when you have inheritance "diamonds" at work. eg class A; pass class B(A): pass class C(A): pass class D(B,C) : pass Now in the init of D you have the choice of calling B.init, C.init, super().init. And since both B and C inherit from A how often should A.init be called and when? super is designed to resolve those issues. As an experiment try it with print() methods as you did above to show which class is which. See if you find a difference. The other case to consider is what happens if only one of B or C implements a method that is implemented by A. Now you decide override that method in D. How do you get all 3 methods called? What if you have multiple levels of inheritance - 3 or 4 deep say? It all starts to get very complicated if you need to track back to find which superclass gets called for which method. > > class ABCD(ABC): > def show(self): > print("HI") > > def greeting(self, Name): > super().show() > print(Name) Not only OK but the preferred way to do it. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From breamoreboy at gmail.com Fri Nov 26 12:36:53 2021 From: breamoreboy at gmail.com (Mark Lawrence) Date: Fri, 26 Nov 2021 17:36:53 +0000 Subject: [Tutor] Using super() in classes In-Reply-To: References: Message-ID: <2da9398c-9cc2-1b4c-8d8e-6d60265b094d@gmail.com> On 26/11/2021 16:52, Alan Gauld via Tutor wrote: snipped to pieces as I just want to point people to https://rhettinger.wordpress.com/2011/05/26/super-considered-super/ and https://www.youtube.com/watch?v=EiOglTERPEo -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From manpritsinghece at gmail.com Fri Nov 26 20:30:37 2021 From: manpritsinghece at gmail.com (Manprit Singh) Date: Sat, 27 Nov 2021 07:00:37 +0530 Subject: [Tutor] Clarity about MRO (method resolution order ) Message-ID: Dear sir, Consider the code given below : class ABC: def show(self): print("HELLO") class ABCD(ABC): def show(self): print("hello") class ABCDE(ABC): def show(self): print("Hello") class ABC12(ABCD, ABCDE): pass obj = ABC12() obj.show() # gives the output hello Just need to make myself clear about the output i got after executing this code The classes ABC, ABCD, ABCDE all have a method with the same name show(). obj is the instance of class ABC12. executing obj.show() will execute the show () of ABCD class, the answer lies with the MRO of ABC12 class ABC12.mro() will result in [__main__.ABC12, __main__.ABCD, __main__.ABCDE, __main__.ABC, object] According to this MRO, show() will be searched in the classes in the order given in the output of ABC12.mro(). show() was first searched in ABC12 class, it was not there , and hence it was searched in ABCD class, it was present and hence executed. Am I right with this explanation ? From manpritsinghece at gmail.com Fri Nov 26 20:51:48 2021 From: manpritsinghece at gmail.com (Manprit Singh) Date: Sat, 27 Nov 2021 07:21:48 +0530 Subject: [Tutor] Multiple inheritance - Need clarity Message-ID: Dear sir, Below given is a toy example , that calculates the surface area of a cylinder (area of circles on both sides + area of cylinder wall) import math class Circle: def __init__(self, radius): self.radius = radius def circle_area(self): return math.pi * self.radius**2 class Rectangle: def __init__(self, s1, s2): self.side1 = s1 self.side2 = s2 def rectangle_area(self): return self.side1 * self.side2 class Cylinder(Circle, Rectangle): def __init__(self, rad, ht): Circle.__init__(self, rad) Rectangle.__init__(self, 2*math.pi*rad, ht) def cylinder_surface_area(self): return 2 * self.circle_area() + self.rectangle_area() cyl = Cylinder(3, 10) cyl.cylinder_surface_area() Gives the answer = 245.04422698000386 Just need to know that the way i have called __init__ of circle & rectangle inside the init of Cylinder ok or not ? the way i have used circle_area & rectangle_area inside cylinder_surface_area is correct or not ? Kindly guide to do this problem in a better way Regards Manprit Singh From wlfraed at ix.netcom.com Fri Nov 26 22:14:18 2021 From: wlfraed at ix.netcom.com (Dennis Lee Bieber) Date: Fri, 26 Nov 2021 22:14:18 -0500 Subject: [Tutor] Multiple inheritance - Need clarity References: Message-ID: On Sat, 27 Nov 2021 07:21:48 +0530, Manprit Singh declaimed the following: >Dear sir, > >Below given is a toy example , that calculates the surface area of a >cylinder (area of circles on both sides + area of cylinder wall) > >import math >class Circle: > > def __init__(self, radius): > self.radius = radius > > def circle_area(self): > return math.pi * self.radius**2 > >class Rectangle: > > def __init__(self, s1, s2): > self.side1 = s1 > self.side2 = s2 > > def rectangle_area(self): > return self.side1 * self.side2 > >class Cylinder(Circle, Rectangle): > > def __init__(self, rad, ht): > Circle.__init__(self, rad) > Rectangle.__init__(self, 2*math.pi*rad, ht) > > def cylinder_surface_area(self): > return 2 * self.circle_area() + self.rectangle_area() > >cyl = Cylinder(3, 10) >cyl.cylinder_surface_area() > >Gives the answer = > >245.04422698000386 > >Just need to know that the way i have called __init__ of circle & rectangle >inside the init of Cylinder ok or not ? the way i have used circle_area & >rectangle_area inside cylinder_surface_area is correct or not ? > >Kindly guide to do this problem in a better way And again you are asking for stylistic approval, not if the code is "correct" or "incorrect". Personally, I would not use inheritance for this example but composition. cyl.cylinder_surface_area() is somewhat redundant. You just want the surface area of /whatever/ object is of interest. Consider the situation of... objs = [ cylinder, cube, dodecahedron ] for obj in objs: sa = obj.surface_area() You would want a common name for surface area of 3D objects -- not a name specific to each object. But that also means your 2D components (circle and rectangle) should similarily have just an obj.area() method to be called. But with your multiple inheritance, you would have to explicitly call top_bottom_area = 2 * Circle.area() column_area = Rectangle.area() as using super... will only call the first item in the MRO. Using composition it would be something like... class Cylinder(): def __init__(self, height, radius): top_bottom = Circle(radius) column = Rectangle(height, radius) def area(self): return self.top_bottom.area() * 2 + self.column.area() class Sphere(): #I'm not going to derive the area of dodecahedron def __init__(self, radius): self.radius = radius def area(self): return 4 * math.pi * self.radius ** 2 There is no inheritance, and no overridden methods, in this structure. There is only the .area() method for all classes which allows a list of all object types to be processed with the same logic. Obviously, for 2D Circle() and Rectangle() objects, .area() returns the planar area, while for 3D Cylinder() and Sphere() it returns surface area. objs = [ Cylinder(10, 3), Circle(4), Rectangle(3, 4), Sphere(5) ] for obj in objs: print( obj.area() ) -- Wulfraed Dennis Lee Bieber AF6VN wlfraed at ix.netcom.com http://wlfraed.microdiversity.freeddns.org/ From wlfraed at ix.netcom.com Fri Nov 26 22:46:56 2021 From: wlfraed at ix.netcom.com (Dennis Lee Bieber) Date: Fri, 26 Nov 2021 22:46:56 -0500 Subject: [Tutor] Multiple inheritance - Need clarity References: Message-ID: <8393qg9qqr2m4sjaahht5lsia10djr38ii@4ax.com> o/~ Talking to myself in public o/~ On Fri, 26 Nov 2021 22:14:18 -0500, Dennis Lee Bieber declaimed the following: > Personally, I would not use inheritance for this example but >composition. > To better clarify... Inheritance is used if/when the subclass IS also a parent class A Cylinder is NOT a Circle, NOR is it a Rectangle. Class Carnivore(): ... Class Mammal(): ... Class Fish(): ... Class Cat(Mammal, Carnivore): ... Class Shark(Fish, Carnivore): ... A Cat IS A Mammal. A Cat IS also A Carnivore. A Shark IS A Fish. A Shark IS also A Carnivore. Composition is used when the class is made up of other objects. A Cylinder HAS A Circle (actually, two identical for top and bottom), A Cylinder also HAS A Rectangle (using your style for the column) Class Vehicle(): ... Class Wheel(): ... Class Engine(): ... Class InternalCombustion(Engine): #internal combustion IS A engine Class Diesel(Engine): #similarly Class PassengerCar(): def __init__(self, engine): self.wheels = { "LF" : Wheel(...), "RF": Wheel(...), "LR" : Wheel(...), "RR" : Wheel(...) ] self.engine = engine A PassengerCar HAS multiple Wheels, a PassengerCar HAS an engine. (some sports cars may have larger rear tires than front tires -- I've only hinted that this could be the case her -- would have to add parameters to __init__ to allow for such. Personally, I wouldn't even use composition for your Cylinder() -- I'd just save radius and height directly, rather than the indirect radius (in Circle()) and height/width (in Rectangle) -- do you expect anyone to ask for cylinder.rectangle.width ? I'd think anyone working with a cylinder would be more likely to ask for cylinder.radius cylinder.height NOT cylinder.circle.radius cylinder.rectangle.height (who thinks of /rectangles/ when discussing a cylinder?) Even worse, your rectangle just has side1 and side2 -- how does a user know which is the height, and which is the width? -- Wulfraed Dennis Lee Bieber AF6VN wlfraed at ix.netcom.com http://wlfraed.microdiversity.freeddns.org/ From alan.gauld at yahoo.co.uk Sat Nov 27 05:46:39 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 27 Nov 2021 10:46:39 +0000 Subject: [Tutor] Clarity about MRO (method resolution order ) In-Reply-To: References: Message-ID: On 27/11/2021 01:30, Manprit Singh wrote: > ABC12.mro() will result in > > [__main__.ABC12, __main__.ABCD, __main__.ABCDE, __main__.ABC, object] > > According to this MRO, show() will be searched in the classes in the order > given in the output of ABC12.mro(). show() was first searched in ABC12 class, > it was not there , and hence it was searched in ABCD class, it was present > and hence executed. > > Am I right with this explanation ? Yes, absolutely. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alan.gauld at yahoo.co.uk Sat Nov 27 06:03:20 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 27 Nov 2021 11:03:20 +0000 Subject: [Tutor] Multiple inheritance - Need clarity In-Reply-To: References: Message-ID: On 27/11/2021 01:51, Manprit Singh wrote: > Below given is a toy example , that calculates the surface area of a > cylinder (area of circles on both sides + area of cylinder wall) One of the problems with OOP is that trying to understand it using toy examples tends to result in bad OO code and hence unclear understanding. Themore realistic an example you can come up with the more likely you are to understand how the code works and why. > import math > class Circle: > def __init__(self, radius): > self.radius = radius > def circle_area(self): > return math.pi * self.radius**2 > > class Rectangle: > def __init__(self, s1, s2): > self.side1 = s1 > self.side2 = s2 > def rectangle_area(self): > return self.side1 * self.side2 > > class Cylinder(Circle, Rectangle): And here is the problem. Inheritance implies an IS-A relationship. This code says that Cylinder IS A Circle and at the same time IS also a Rectangle. ie. That you could insert a Cylinder instance anywhere that a Circle or Rectangle was expected. However a cylinder is not a Circle or a Rectangle, rather it is *composed* of 2 circles and (arguably) a rectangle. One of the big problems with inheritance in general, and especially with Multiple inheritance, is that it gets abused as a method to reuse code rather than as a model of the problem domain. Very often people use inheritance when they should use composition/delegation. > def __init__(self, rad, ht): > Circle.__init__(self, rad) > Rectangle.__init__(self, 2*math.pi*rad, ht) But this is where you should use super() Otherwise you will call the object.init twice. (In this case, not a problem but it could be in real code. For example, say the root object maintains a counter of instances, calling it twice would lead to double counting). Also, you will make your Cylinder non-compatible with super() in any derived Cylinders(such as ColouredCylinder say...) > Just need to know that the way i have called __init__ of circle & rectangle > inside the init of Cylinder ok or not ? the way i have used circle_area & > rectangle_area inside cylinder_surface_area is correct or not ? Syntactically correct but stylistically and idiomatically wrong. And wrong primarily because you are trying to use inheritance where you should be using composition. A valid superclass for all of these objects might be Shape which in turn might be sub-classed into 2DShape and 3DShape. Circle and rectangle would be sub-classes of 2DShape while Cylinder would be a subclass of 3DShape. But I would be the first to agree that OOP is difficult to grasp without using it on larger projects. Its value is not always evident in small scale code. But you need small scale to learn the techniques. So often you have to just work with it and trust that it will make sense 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 alan.gauld at yahoo.co.uk Sat Nov 27 06:06:10 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 27 Nov 2021 11:06:10 +0000 Subject: [Tutor] Multiple inheritance - Need clarity In-Reply-To: References: Message-ID: On 27/11/2021 03:14, Dennis Lee Bieber wrote: > But with your multiple inheritance, you would have to explicitly call > > top_bottom_area = 2 * Circle.area() > column_area = Rectangle.area() > > as using super... will only call the first item in the MRO. Ah, good point. My recommendation to use super() here was misplaced. You need to call both. -- 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 tariqkhasiri at gmail.com Fri Nov 26 21:20:28 2021 From: tariqkhasiri at gmail.com (Tariq Khasiri) Date: Sat, 27 Nov 2021 08:20:28 +0600 Subject: [Tutor] Explanation of one - hot encoding Message-ID: This following line is from a kernel I am trying to replicate from Spotify songs data. Could anyone kindly explain what this line means ? ? In terms of encoding our artists and our genres we see one-hot encoding is a bad idea here considering the cardinality of those features therefore we will dummy encode the genres and replace the artist names with numerical values closer to the model selection stage. ? From alan.gauld at yahoo.co.uk Sat Nov 27 07:27:41 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 27 Nov 2021 12:27:41 +0000 Subject: [Tutor] Explanation of one - hot encoding In-Reply-To: References: Message-ID: On 27/11/2021 02:20, Tariq Khasiri wrote: > This following line is from a kernel I am trying to replicate from Spotify > songs data. Could anyone kindly explain what this line means ? > > ? In terms of encoding our artists and our genres we see one-hot encoding > is a bad idea here considering the cardinality of those features therefore > we will dummy encode the genres and replace the artist names with numerical > values closer to the model selection stage. ? This has nothing to do with Python and should really be addressed to the Spotify community I suspect. I certainly have no clue what most of it means. My guess is that one-hot is a typo for one-shot but that is purely a guess! -- 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 lggeorgieva at gmail.com Sat Nov 27 07:30:17 2021 From: lggeorgieva at gmail.com (Lilia Georgieva) Date: Sat, 27 Nov 2021 12:30:17 +0000 Subject: [Tutor] Explanation of one - hot encoding In-Reply-To: References: Message-ID: Hi, Here is a Wikipedia article on one hot encoding. This is fairly common. https://en.m.wikipedia.org/wiki/One-hot Lilia On Sat, Nov 27, 2021, 12:28 Alan Gauld via Tutor wrote: > On 27/11/2021 02:20, Tariq Khasiri wrote: > > This following line is from a kernel I am trying to replicate from > Spotify > > songs data. Could anyone kindly explain what this line means ? > > > > ? In terms of encoding our artists and our genres we see one-hot encoding > > is a bad idea here considering the cardinality of those features > therefore > > we will dummy encode the genres and replace the artist names with > numerical > > values closer to the model selection stage. ? > > This has nothing to do with Python and should really be > addressed to the Spotify community I suspect. I certainly > have no clue what most of it means. > > My guess is that one-hot is a typo for one-shot but that > is purely a guess! > > -- > 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 manpritsinghece at gmail.com Sat Nov 27 10:36:14 2021 From: manpritsinghece at gmail.com (Manprit Singh) Date: Sat, 27 Nov 2021 21:06:14 +0530 Subject: [Tutor] Multiple inheritance - Need clarity In-Reply-To: References: Message-ID: Once again many many thanks to Dennis and Alan sir for this mail . Actually I do work with Scientific computing (Numpy and pandas & matplotlib and Scikit-learn the most ), my work is rarely related to OOPS (Just making some simple classes for transformations etc.) . Just trying to learn.... Regards Manprit Singh On Sat, Nov 27, 2021 at 4:40 PM Alan Gauld via Tutor wrote: > On 27/11/2021 03:14, Dennis Lee Bieber wrote: > > > But with your multiple inheritance, you would have to explicitly > call > > > > top_bottom_area = 2 * Circle.area() > > column_area = Rectangle.area() > > > > as using super... will only call the first item in the MRO. > > Ah, good point. My recommendation to use super() here was > misplaced. You need to call both. > > -- > 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 manpritsinghece at gmail.com Sat Nov 27 10:45:24 2021 From: manpritsinghece at gmail.com (Manprit Singh) Date: Sat, 27 Nov 2021 21:15:24 +0530 Subject: [Tutor] Explanation of one - hot encoding In-Reply-To: References: Message-ID: Dear sir, One hot encoding is basically a preprocessing step in machine learning , where a categorical feature is encoded as a numeric array of 0 and 1 . One hot encoding is not considered good when a feature has a large number of unique values in it . Regards On Sat, Nov 27, 2021 at 5:08 PM Tariq Khasiri wrote: > This following line is from a kernel I am trying to replicate from Spotify > songs data. Could anyone kindly explain what this line means ? > > ? In terms of encoding our artists and our genres we see one-hot encoding > is a bad idea here considering the cardinality of those features therefore > we will dummy encode the genres and replace the artist names with numerical > values closer to the model selection stage. ? > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From wlfraed at ix.netcom.com Sat Nov 27 11:48:01 2021 From: wlfraed at ix.netcom.com (Dennis Lee Bieber) Date: Sat, 27 Nov 2021 11:48:01 -0500 Subject: [Tutor] Multiple inheritance - Need clarity References: Message-ID: On Sat, 27 Nov 2021 21:06:14 +0530, Manprit Singh declaimed the following: >Once again many many thanks to Dennis and Alan sir for this mail . >Actually I do work with Scientific computing (Numpy and pandas & >matplotlib and Scikit-learn the most ), my work is rarely related to OOPS >(Just making some simple classes for transformations etc.) . Just trying to >learn.... > Numpy, matplotlib, et al. are loaded with OOP -- just spend some time studying the source files (at least those in Python; for speed most of those libraries will eventually devolve into calls to C/C++ libraries) Also to be aware of: OOAD (object oriented analysis and design) and OOP (object oriented programming) are related, but separate (one can coerce object designs into non-OOP languages by using a coding style where the object [a record structure] is manually passed to methods, though managing inheritance is difficult -- after all, original C++ was a massive set of macros that translated the C++ syntax of the time into regular K&R C). -- Wulfraed Dennis Lee Bieber AF6VN wlfraed at ix.netcom.com http://wlfraed.microdiversity.freeddns.org/ From tariqkhasiri at gmail.com Sat Nov 27 11:41:30 2021 From: tariqkhasiri at gmail.com (Tariq Khasiri) Date: Sat, 27 Nov 2021 22:41:30 +0600 Subject: [Tutor] Explanation of one - hot encoding In-Reply-To: References: Message-ID: so grateful to everyone who has been kind enough to give their valuable feedback. now i have a better grip over the whole thing - thanks to all of you ! On Sat, Nov 27, 2021 at 6:29 PM Alan Gauld via Tutor wrote: > On 27/11/2021 02:20, Tariq Khasiri wrote: > > This following line is from a kernel I am trying to replicate from > Spotify > > songs data. Could anyone kindly explain what this line means ? > > > > ? In terms of encoding our artists and our genres we see one-hot encoding > > is a bad idea here considering the cardinality of those features > therefore > > we will dummy encode the genres and replace the artist names with > numerical > > values closer to the model selection stage. ? > > This has nothing to do with Python and should really be > addressed to the Spotify community I suspect. I certainly > have no clue what most of it means. > > My guess is that one-hot is a typo for one-shot but that > is purely a guess! > > -- > 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 Sat Nov 27 12:48:36 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 27 Nov 2021 17:48:36 +0000 Subject: [Tutor] Multiple inheritance - Need clarity In-Reply-To: References: Message-ID: On 27/11/2021 16:48, Dennis Lee Bieber wrote: > inheritance is difficult -- after all, original C++ was a massive set of > macros that translated the C++ syntax of the time into regular K&R C). Indeed, and ultimately all languages devolve to machine code and none of those are even remotely object oriented! -- 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 Sat Nov 27 22:51:02 2021 From: phillor9 at gmail.com (Phil) Date: Sun, 28 Nov 2021 14:51:02 +1100 Subject: [Tutor] pylint(too-many-nested-blocks) Message-ID: <2c34791d-dc21-4070-82d2-1c98d67616ce@gmail.com> "https://pycodequ.al/docs/pylint-messages/r1702-too-many-nested-blocks.html" describes this error as: "Used when a function or method has too many nested blocks. This makes the code less understandable and maintainable." The following is one of several functions that fits this description and I'm wondering how I might reduce the number of for-loops. If the number of for-loops cannot be reduced then I suppose the function should be broken up into smaller functions. The problem is, as I see it, little of the code is reusable in other functions that also have too many nested blocks. ??? def pointingPairRow(self): ??????? """ ??????? If a candidate is present in only two cells of a box, then it must be the ??????? solution for one of these two cells. If these two cells belong to the same row, ??????? then this candidate can not be the solution in any other cell of the same row. ??????? """ ??????? box_start = [(0, 0), (0, 3), (0, 6), ??????????????????? (3, 0), (3, 3), (3, 6), ??????????????????? (6, 0), (6, 3), (6, 6) ??????????????????? ] ??????? for x, y in box_start: ??????????? pairs = Counter() ??????????? for number in range(1, 10): ??????????????? number= str(number) ??????????????? for row in range(x, x + 3): ??????????????????? for col in range(y, y + 3): ??????????????????????? if len(self.solution[row][col]) > 1 and number in self.solution[row][col]: ??????????????????????????? pairs[number] += 1 ??????????? for item, count in pairs.items(): ??????????????? if count == 2:? # candidate ??????????????????? for row in range(x, x + 3): ??????????????????????? icount = 0 ??????????????????????? col_list = [] ??????????????????????? for col in range(y, y + 3): ??????????????????????????? if item in self.solution[row][col]: ??????????????????????????????? icount += 1 ??????????????????????????????? col_list.append(col) ??????????????????????????? if icount == 2: ??????????????????????????????? for c in range(self.num_cols): ??????????????????????????????????? if? len(self.solution[row][c]) > 1 and item in self.solution[row][c] and c not in col_list: ??????????????????????????????????????? self.solution[row][c] -= set(item) Also, the 3 for-loops before "if count == 2" don't need to be repeated once the candidate has been removed from the row with "self.solution[row][c] -= set(item)". Setting a boolean switch here to prevent the for-loops from continuing does the job but it adds yet another "if" statement. The function works and so it's logically correct but it's messy and inefficient. -- Regards, Phil From PyTutor at DancesWithMice.info Sun Nov 28 03:45:50 2021 From: PyTutor at DancesWithMice.info (dn) Date: Sun, 28 Nov 2021 21:45:50 +1300 Subject: [Tutor] pylint(too-many-nested-blocks) In-Reply-To: <2c34791d-dc21-4070-82d2-1c98d67616ce@gmail.com> References: <2c34791d-dc21-4070-82d2-1c98d67616ce@gmail.com> Message-ID: <703ee90c-2d81-5ea3-91aa-7b2a399e4531@DancesWithMice.info> On 28/11/2021 16.51, Phil wrote: > "https://pycodequ.al/docs/pylint-messages/r1702-too-many-nested-blocks.html" > describes this error as: > > "Used when a function or method has too many nested blocks. This makes > the code less understandable and maintainable." > > The following is one of several functions that fits this description and > I'm wondering how I might reduce the number of for-loops. If the number > of for-loops cannot be reduced then I suppose the function should be > broken up into smaller functions. The problem is, as I see it, little of > the code is reusable in other functions that also have too many nested > blocks. > > ??? def pointingPairRow(self): > ??????? """ > ??????? If a candidate is present in only two cells of a box, then it > must be the > ??????? solution for one of these two cells. If these two cells belong > to the same row, > ??????? then this candidate can not be the solution in any other cell of > the same row. > ??????? """ > ??????? box_start = [(0, 0), (0, 3), (0, 6), > ??????????????????? (3, 0), (3, 3), (3, 6), > ??????????????????? (6, 0), (6, 3), (6, 6) > ??????????????????? ] > > ??????? for x, y in box_start: > ??????????? pairs = Counter() > > ??????????? for number in range(1, 10): > ??????????????? number= str(number) > > ??????????????? for row in range(x, x + 3): > ??????????????????? for col in range(y, y + 3): > ??????????????????????? if len(self.solution[row][col]) > 1 and number > in self.solution[row][col]: > ??????????????????????????? pairs[number] += 1 > > ??????????? for item, count in pairs.items(): > ??????????????? if count == 2:? # candidate > ??????????????????? for row in range(x, x + 3): > ??????????????????????? icount = 0 > ??????????????????????? col_list = [] > > ??????????????????????? for col in range(y, y + 3): > ??????????????????????????? if item in self.solution[row][col]: > ??????????????????????????????? icount += 1 > ??????????????????????????????? col_list.append(col) > > ??????????????????????????? if icount == 2: > ??????????????????????????????? for c in range(self.num_cols): > ??????????????????????????????????? if? len(self.solution[row][c]) > 1 > and item in self.solution[row][c] and c not in col_list: > ??????????????????????????????????????? self.solution[row][c] -= set(item) > > Also, the 3 for-loops before "if count == 2" don't need to be repeated > once the candidate has been removed from the row with > "self.solution[row][c] -= set(item)". Setting a boolean switch here to > prevent the for-loops from continuing does the job but it adds yet > another "if" statement. > > The function works and so it's logically correct but it's messy and > inefficient. Does Python itself see this as an error and stop working, or is it only the code-checker s/w? If Python, then I've never reached that point, which makes you better than me - or does it? Were we conducting a Code Review, this function would definitely attract criticism. You have already recognised the complexity, but seem somewhat satisfied - on the grounds that the whole works. (which may be as good a measure as any other - depending upon application!) The two (easily agreed) criticisms are "messy" and "inefficient". Here are two more ideas: "cyclomatic complexity" and "readability". Some say that code should be written with the view that it is more important that it can be read by humans than by computers. This seems contra-indicated when the process of programming is to 'instruct the computer'. However, consider the life-time of a program[me]. Also, that computers 'read' code with a very narrow and dogmatic point-of-view, compared with us-peoples. We also talk about your future-self, meaning you in (a notional) six-months' time. In other words, someone who is reading this for the first time ever, or you, after sufficient time has passed that a lot of your 'thinking' that went into the construction of this code has disappeared from even the back of your mind. Question: is it/will it be easy to read? Here's where things become rather subjective. You (no criticism) seem to have a mathematical view of programming, as revealed by your choice of example-problems and love of abbreviated identifiers. That said, a docstring describes the function - so gold-star for that - even if "candidate" and "solution" are beyond my ken. Perhaps the point to invite some thought into comparing the 'how' and the 'why' of a piece of code - two important questions in the mind of a reader/reviewer! I have been criticised (but exhibit little remorse) for breaking things down into "smaller" units of code than others feel necessary. Maybe you'd agree with that. Like I said "subjective"! Hasn't "stepwise decomposition" been mentioned before? Now is a good time pick-up the testing tool's feedback: "understandable and maintainable". It's not merely a matter of "readability"/"understandability", but also maintainability - and that of the objectives of the tool: "testability". How can you prove to me that this code actually does do 'what it says on the tin [/can]'? Let's say that there is a suite of testing code, and one of those indicates that there is an error somewhere 'around' the innermost/most deeply nested (and last) for-loop. How can you (or I, or A.N.Other) proceed to correct ("maintain") this code? How much larger will the problem seem, if it is not revealed by the test-suite, but is 'discovered' by some (not-so) innocent user in the proverbial six-month's time? How easy will it be to see if this notional problem (I'm not saying there is one!) if 'inside' that for-loop, or is somewhere in the code which manipulates the data which is subsequently 'fed' into the loop? Another consideration: if the preceding steps (prior to that inner for-loop) are 'expensive', particularly in terms of time, how many test-cycles, checks, and detection-runs can you afford? If it were 'cheaper'/faster to test, how many now? I like the old saw: that if "debugging" is taking the bugs out of code, then programming must be the process of putting them in! Accordingly, there is a lesson many find hard to learn/admit to themselves - it is better to program[me] 'defensively' (in the expectation of errors), that to code arrogantly (overly-impressed with one's own abilities, assumptions of perfection, feelings of invincibility, ...). - or maybe that's an admission that I'm just not very good at it? If the for-loop is detached from the rest of the code, can you give it a name? If-then, you can also test it in isolation from the rest of the functionality. This morning I needed to add the ability to remove a single item (pop) from a multi-dimensional data-structure, and further functionality to remove an entire 'row' (sound familiar?). I could have 'tested' these 'within' the application which uses the data-structure. Why not? That's where the code is actually being used - and if it works there, it must 'work'! Right? When the two test harnesses were constructed and the code seemed to be working, one of them failed at the last test-step. It had 'passed' before! It failed because of a 'fix', installed to cover another previously-failing test-step. Oops! (yes, big surprise, I make programming errors - but don't bother to circle this date in the calendar, you'll quickly run out of ink, and obliterate your calendar should you monitor me that closely!) The reason why the small(er) code-units were worth what may have seemed like 'more effort than necessary', was because it was so much quicker and easier to test the code as I wrote it*, it was easy to find/be shown errors as they were made, and it was easy to see that a 'fix' in one place, caused an issue under another set of circumstances. All while the actual code was fresh in my mind, and not when I would be more interested in the application doing what I expect of it. Incidentally, should a 'six-month' embarrassment appear, the test-suite will still be available - and will continue to ensure any future 'fixes' don't cause similar regression-errors!) * in fact, the tests were written before the code - but considering all-else I'm throwing at you, "Test-Driven Development" may be one (slightly less-topical) reference, too many There has been plenty of research into the cost of errors. All really boil-down to the idea that the 'later' an error is discovered, the more expensive will be 'the fix'! There are many 'guides' claiming to know how long is the 'right' length for a function/procedure/block of code. However, the number of lines of code in a function is no guide to its complexity. A list-comprehension might fit into one line of code, but it is more complex than its two, three, or even four line 'basic' equivalent. A better measure is "McCabe Complexity" which is like establishing a 'reading level' for code (instead of books for children of different ages/language abilities). The wider term is "Cyclomatic Complexity". Cyclomatic Complexity measures the number of coding-constructs there are in a unit of code, eg if statements, loops, etc. The more there are, the more logical 'paths' exist between 'start' and 'end'. This is the concern of your static-test tool. The more paths, the harder it is to test that each possibility has been tested/checked/proven, and the more combinations of events and conditions that must (all?) be considered. Here we can mention the KISS principle (not a principle of programming per-se). Nuff said? Earlier the idea of naming a chunk of code was mentioned. "Chunking" is a term from psychology. It is a unit of thought. When coding a routine, that unit might be a single line-of-code (it will vary with experience and expertise). When a bunch of lines can be wrapped up into a single unit - and that unit named, now we have a much larger 'chunk', and reduced psychological/mental effort to remember what it does and where it 'fits in'! A related thought: If an author cannot describe what a section of code is doing, there's a problem no amount of Python will fix! If that name includes a conjunction such as "and", eg "choose which row and wipe its data", then this indicates (at least to this author) that the function is trying to do (two) too much. This assessment comes from a programming ideal known as SRP - the "Single Responsibility Principle". The idea that a piece of code has one objective also means that it becomes much easier to locate where in a mass of code a problem is likely to have arisen. It also makes the block very easy to test - either it does ("what it says on ...") or it fails! There are also ComSc concepts of "dependent" and "independent" linkages. If code-unit "b" can't run unless unit "a" has previously executed, these two are not independent. Certainly "b" is not "reusable" as and by itself! High dependence is one enemy of "testability" and thus probably also maintainability, etc, etc. Think about list.append(). It does require that there is a list, and some data to add - but they are inputs, not dependencies. It will add what is 'input' to what is there already. There is no requirement that the list be anything more than an empty list. Similarly, there is no problem if the list already contains many, many items. The 'new stuff' is added to the existing 'end' of the list. There is no requirement that we tell the list how long it is - and thus 'where' to add the new stuff. So, we can use list.append() without first using Len( list ). Similarly, if a unit of code performs some action on one or more of the rows of your data, does it need to know of previous calculations or arrangements performed on those rows of data previously? Probably not - and if it currently does, the code can likely be rearranged to obviate such dependence. Now, before dreams of nirvana over-take us, the cyclomatic complexity and/or a replacement concept still exists in the way one must network or 'stitch-together' all of the smaller code-modules. However, (back to "chunking") if one has a function called "search_row_for_palindromes()", the thinking required to fit that into a series of similarly well-named routines takes place at a much 'higher level' of problem-solving, than the thought-processes involved in writing an if-statement inside a for-loop which calls a palindrome-detection function! Which all goes to show: there's no silver bullet! (I'll leave you to research the topics-mentioned that may interest you, as you see fit...) -- Regards, =dn From cs at cskk.id.au Sun Nov 28 06:00:41 2021 From: cs at cskk.id.au (Cameron Simpson) Date: Sun, 28 Nov 2021 22:00:41 +1100 Subject: [Tutor] pylint(too-many-nested-blocks) In-Reply-To: <2c34791d-dc21-4070-82d2-1c98d67616ce@gmail.com> References: <2c34791d-dc21-4070-82d2-1c98d67616ce@gmail.com> Message-ID: On 28Nov2021 14:51, Phil wrote: >"https://pycodequ.al/docs/pylint-messages/r1702-too-many-nested-blocks.html" >describes this error as: > >"Used when a function or method has too many nested blocks. This makes >the code less understandable and maintainable." > >The following is one of several functions that fits this description >and I'm wondering how I might reduce the number of for-loops. If the >number of for-loops cannot be reduced then I suppose the function >should be broken up into smaller functions. The problem is, as I see >it, little of the code is reusable in other functions that also have >too many nested blocks. Remember that a linter is a large collection of guidelines when it comes to things like "too many". You could break this function up, but it doesn't seem unreasonable to me. When I lint I'm making an informed decision about the linter's complaints. If you're happy that the function is sensible, you can do this: # pylint: disable=too-many-nested-blocks def pointingPairRow(self): for that specific function. My goal with a linter is clean output (no complaints). Oftne I ifx things to make it happy - these checks are generally sensble - but sometimes you know better than the linter, and pylint's disable comments come in very useful there. Why silence? So that _new_ messages are worth checking out and are not buried in a sea of things I've already considered. Cheers, Cameron Simpson From marcus.luetolf at bluewin.ch Sun Nov 28 14:14:08 2021 From: marcus.luetolf at bluewin.ch (marcus.luetolf at bluewin.ch) Date: Sun, 28 Nov 2021 20:14:08 +0100 Subject: [Tutor] computation problem Message-ID: <004001d7e48c$1e900bb0$5bb02310$@bluewin.ch> Hello Experts I'd like to write a python code to find all possible combinations of a list of n items. If, for example, a list L consists of 4 items, n = 4, L = ['abcd'], the code should print out 24 different combinations from ['abcd'] to ...['dcba'] I tried several solutions posted on stackoverflow using itertools but none gave the sought for combinations. I'm aware the complexity of a a code using for loops might be exponential . Could anybody tell me a text or tutorial how to approach tist task by python ? From mats at wichmann.us Sun Nov 28 14:17:59 2021 From: mats at wichmann.us (Mats Wichmann) Date: Sun, 28 Nov 2021 12:17:59 -0700 Subject: [Tutor] pylint(too-many-nested-blocks) In-Reply-To: <2c34791d-dc21-4070-82d2-1c98d67616ce@gmail.com> References: <2c34791d-dc21-4070-82d2-1c98d67616ce@gmail.com> Message-ID: On 11/27/21 20:51, Phil wrote: > "https://pycodequ.al/docs/pylint-messages/r1702-too-many-nested-blocks.html" > describes this error as: > > "Used when a function or method has too many nested blocks. This makes > the code less understandable and maintainable." > > The following is one of several functions that fits this description and > I'm wondering how I might reduce the number of for-loops. If the number > of for-loops cannot be reduced then I suppose the function should be > broken up into smaller functions. The problem is, as I see it, little of > the code is reusable in other functions that also have too many nested > blocks. ... > The function works and so it's logically correct but it's messy and > inefficient. > Just on the pylint topic in general: pylint is intended to be tuned so that it's useful for your project, there's no expectation that it's perfect for every project out of the box. If it made you think, and you agree with it that something is hard to read because of many levels of nesting, then sure, think about how you could refactor. You don't have to do that right away. Of the different levels of commentary from pylint, this complaint is probably at the least critical level. There are EXXXX errors, WXXXX warnings and RXXXX recommendations. Yours is an 'R'. Take that for whatever it's worth... You can make a .pylintrc file - there's an option to dump a default and then you tune it to turn on/off things as you like. I'm not fond of using this option too much - and especially not without commenting - and you lose track of *why*. Or you can put as comments in your code stanzas to quiet pylint on specific areas of complaint. This is usually better, because you've noted in the exact location of the compliant that "yes I mean it". If you want to "yes I'm happy with this for now, but want to revisit later" you can add a TODO comment to that effect as well. If you have really special cases, you can write pylint plugins that handle your situation. Usually, though, this is to check more, not less, though I've actually used it to define some accepted names inside a program as a nicer way of quieting pylint grumbles about non-PEP8 naming choices - I don't turn off the whole warning class, but say that this specific set is accepted. Many follow a model (I wish I could with my big project!) of getting pylint down to silence through fixes and settings and exclusions. That way you have a very clear notice when a change introduces a new warning - in other words, you get yourself to where you're only looking at deltas. From mats at wichmann.us Sun Nov 28 14:30:42 2021 From: mats at wichmann.us (Mats Wichmann) Date: Sun, 28 Nov 2021 12:30:42 -0700 Subject: [Tutor] computation problem In-Reply-To: <004001d7e48c$1e900bb0$5bb02310$@bluewin.ch> References: <004001d7e48c$1e900bb0$5bb02310$@bluewin.ch> Message-ID: <5d6108bd-ae30-0c3d-b017-acaa587ebcd2@wichmann.us> On 11/28/21 12:14, marcus.luetolf at bluewin.ch wrote: > Hello Experts > > > > I'd like to write a python code to find all possible combinations of a list > of n items. > > If, for example, a list L consists of 4 items, n = 4, L = ['abcd'], the > code should print out 24 different combinations from ['abcd'] to ...['dcba'] > > > > I tried several solutions posted on stackoverflow using itertools but none > gave the sought for combinations. > > > > I'm aware the complexity of a a code using for loops might be exponential . > > > > Could anybody tell me a text or tutorial how to approach tist task by python > ? > maybe if you showed what you've tried, and showed why itertools doesn't work for you... ['abcd'] isn't a list of four items, it's a list of one item. If you want to treat the characters in the string as separate items, you have to break it apart and, if you want it displayed like you indicate above, combine it back together again. Do you know how to do that? itertools.permutations will definitely do the "heavy lifting" for you. >>> from itertools import permutations >>> len(list(permutations(['a', 'b', 'c', 'd']))) 24 the list() is because permutations returns an iterator, not a list. Normally you'd end up iterating over the result, which is why it doesn't take the time to force it to a list itself. From breamoreboy at gmail.com Sun Nov 28 14:34:23 2021 From: breamoreboy at gmail.com (Mark Lawrence) Date: Sun, 28 Nov 2021 19:34:23 +0000 Subject: [Tutor] computation problem In-Reply-To: <004001d7e48c$1e900bb0$5bb02310$@bluewin.ch> References: <004001d7e48c$1e900bb0$5bb02310$@bluewin.ch> Message-ID: <412a1016-3310-853d-7f07-6a748cb3b522@gmail.com> On 28/11/2021 19:14, marcus.luetolf at bluewin.ch wrote: > Hello Experts > > > > I'd like to write a python code to find all possible combinations of a list > of n items. > > If, for example, a list L consists of 4 items, n = 4, L = ['abcd'], the > code should print out 24 different combinations from ['abcd'] to ...['dcba'] > > I tried several solutions posted on stackoverflow using itertools but none > gave the sought for combinations. > > I'm aware the complexity of a a code using for loops might be exponential . > > Could anybody tell me a text or tutorial how to approach tist task by python > ? > You have a list which contains one item, a string 'abcd'. If you only want the string used the following works. python3.9 Python 3.9.7 (default, Sep 10 2021, 14:59:43) [GCC 11.2.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> from itertools import permutations >>> l='abcd' >>> for a in permutations(l):print(a) ... ('a', 'b', 'c', 'd') ('a', 'b', 'd', 'c') ('a', 'c', 'b', 'd') ('a', 'c', 'd', 'b') ('a', 'd', 'b', 'c') ('a', 'd', 'c', 'b') ('b', 'a', 'c', 'd') ('b', 'a', 'd', 'c') ('b', 'c', 'a', 'd') ('b', 'c', 'd', 'a') ('b', 'd', 'a', 'c') ('b', 'd', 'c', 'a') ('c', 'a', 'b', 'd') ('c', 'a', 'd', 'b') ('c', 'b', 'a', 'd') ('c', 'b', 'd', 'a') ('c', 'd', 'a', 'b') ('c', 'd', 'b', 'a') ('d', 'a', 'b', 'c') ('d', 'a', 'c', 'b') ('d', 'b', 'a', 'c') ('d', 'b', 'c', 'a') ('d', 'c', 'a', 'b') ('d', 'c', 'b', 'a') >>> -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From cs at cskk.id.au Sun Nov 28 18:24:26 2021 From: cs at cskk.id.au (Cameron Simpson) Date: Mon, 29 Nov 2021 10:24:26 +1100 Subject: [Tutor] Enable Python feature in vim editor In-Reply-To: References: Message-ID: On 24Nov2021 22:45, Kaushal Shriyan wrote: >I am running CentOS Linux release 7.9.2009 (Core) and have enabled >below >entry in .vimrc file > >$cat .vimrc >set shiftwidth=2 >set shiftround >set tabstop=4 >set softtabstop=4 >set noexpandtab >set number >set ruler >set noautoindent >set nosmartindent >set noincsearch >syntax enable >$ > >when i do vim testpythonscript.py I am facing the below Pymode error > >$vim testpythonscript.py > >[Pymode]: error: Pymode requires vim compiled with +python3 (exclusively). >Most of features will be disabled. This means that the vim you're invoking does not have the "python3" feature compiled into it. Indeed, in the `vim --version` output you include there is this: $vim --version VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Dec 15 2020 16:44:08) Included patches: 1-207, 209-629 Modified by Compiled by Huge version without GUI. Features included (+) or not (-): [...] +cryptv +linebreak +python/dyn +viminfo +cscope +lispindent -python3 +vreplace [...] So you can see that "python3" has been omitted from that build. Do a "yum search vim" and see if there is a vim variant with "python3" included (I haven't got a Centos host available to me). You could try replacing the installed vim with the python3 enabled vim if that is available. Cheers, Cameron Simpson From genichka at gmail.com Sun Nov 28 17:59:24 2021 From: genichka at gmail.com (Zhenya Sakal) Date: Sun, 28 Nov 2021 17:59:24 -0500 Subject: [Tutor] computation problem In-Reply-To: <412a1016-3310-853d-7f07-6a748cb3b522@gmail.com> References: <004001d7e48c$1e900bb0$5bb02310$@bluewin.ch> <412a1016-3310-853d-7f07-6a748cb3b522@gmail.com> Message-ID: Hello! You are looking into creating a powerset. There are many solutions to this problem but I think the one through bit notation is the shortest. def powerSet(items): N = len(items) # enumerate the 2 ** N possible combinations for i in range(2 ** N): combo = [] for j in range(N): #test bit j-th of integer i -> that is turning base 10 into binary for i if (i >> j) % 2 == 1: combo.append(items[j]) yield combo On Sun, Nov 28, 2021 at 4:49 PM Mark Lawrence wrote: > On 28/11/2021 19:14, marcus.luetolf at bluewin.ch wrote: > > Hello Experts > > > > > > > > I'd like to write a python code to find all possible combinations of a > list > > of n items. > > > > If, for example, a list L consists of 4 items, n = 4, L = ['abcd'], the > > code should print out 24 different combinations from ['abcd'] to > ...['dcba'] > > > > I tried several solutions posted on stackoverflow using itertools but > none > > gave the sought for combinations. > > > > I'm aware the complexity of a a code using for loops might be > exponential . > > > > Could anybody tell me a text or tutorial how to approach tist task by > python > > ? > > > > You have a list which contains one item, a string 'abcd'. If you only > want the string used the following works. > > python3.9 > Python 3.9.7 (default, Sep 10 2021, 14:59:43) > [GCC 11.2.0] on linux > Type "help", "copyright", "credits" or "license" for more information. > >>> from itertools import permutations > > >>> l='abcd' > >>> for a in permutations(l):print(a) > ... > ('a', 'b', 'c', 'd') > ('a', 'b', 'd', 'c') > ('a', 'c', 'b', 'd') > ('a', 'c', 'd', 'b') > ('a', 'd', 'b', 'c') > ('a', 'd', 'c', 'b') > ('b', 'a', 'c', 'd') > ('b', 'a', 'd', 'c') > ('b', 'c', 'a', 'd') > ('b', 'c', 'd', 'a') > ('b', 'd', 'a', 'c') > ('b', 'd', 'c', 'a') > ('c', 'a', 'b', 'd') > ('c', 'a', 'd', 'b') > ('c', 'b', 'a', 'd') > ('c', 'b', 'd', 'a') > ('c', 'd', 'a', 'b') > ('c', 'd', 'b', 'a') > ('d', 'a', 'b', 'c') > ('d', 'a', 'c', 'b') > ('d', 'b', 'a', 'c') > ('d', 'b', 'c', 'a') > ('d', 'c', 'a', 'b') > ('d', 'c', 'b', 'a') > >>> > > -- > My fellow Pythonistas, ask not what our language can do for you, ask > what you can do for our language. > > Mark Lawrence > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Ievgeniia Sakal, PhD she/her/hers, Yale University, Department of History From tariqkhasiri at gmail.com Sun Nov 28 18:03:59 2021 From: tariqkhasiri at gmail.com (Tariq Khasiri) Date: Mon, 29 Nov 2021 05:03:59 +0600 Subject: [Tutor] Explanation of smote and upsampling Message-ID: ??? oversample = SMOTE()X, y = oversample.fit_resample(c_data[c_data.columns[1:]], c_data[c_data.columns[0]])usampled_df = X.assign(Churn = y) ??? Does this portion of code mean that I am upsampling column 0 and column 1 of my data frame ( c_data ) ? Please kindly advise. Not understanding this part of code ?????-> c_data[c_data.columns[1:] From manpritsinghece at gmail.com Sun Nov 28 20:53:17 2021 From: manpritsinghece at gmail.com (Manprit Singh) Date: Mon, 29 Nov 2021 07:23:17 +0530 Subject: [Tutor] Multi threading and I/O bound process Message-ID: Dear sir, Just need to know if the below example is a correct example of multithreading used with I/O bound process : 1) Without multi threading import time import urllib.request # a function to dsplay first 25 bytes of a webpage def readpage(urladd): with urllib.request.urlopen(urladd) as fobj: print(fobj.read(25)) t0 = time.time() for _ in range(6): readpage("https://www.xyz.com/") t1 = time.time() print(t1-t0) will display the first 25 bytes of the webpage 6 times in a sequential order. and measure the time 2) Multithreading Using constructor : t0= time.time() import threading threadsgrp = [] for _ in range(6): thread = threading.Thread(target=readpage,args=("https://www.xyz.com/ ",)) threadsgrp.append(thread) thread.start() for th in threadsgrp: th.join() t1 =time.time() print(t1-t0) 3) Multithreading using subclass : t1 = time.time() class Techlivethread(threading.Thread): def __init__(self, add): threading.Thread.__init__(self) self.add = add def run(self): readpage(self.add) grp =[] for _ in range(6): th1 = Techlivethread("https://www.xyz.com/") grp.append(th1) th1.start() for ele in grp: ele.join() t2 = time.time() print(t2-t1) Kindly comment From alexkleider at gmail.com Sun Nov 28 21:10:46 2021 From: alexkleider at gmail.com (Alex Kleider) Date: Sun, 28 Nov 2021 18:10:46 -0800 Subject: [Tutor] Enable Python feature in vim editor In-Reply-To: References: Message-ID: Interesting (re python3 'in vim') I'm running Debian (current stable release- #11 I believe it is, and 'my vim' (vi is installed by default but to get vim requires apt install vim) seems to have it included... (p10) alex at X1:~/Notes/Py$ vim --version | grep python +comments +libcall -python +visualextra +conceal +linebreak +python3 +viminfo ......... On Sun, Nov 28, 2021 at 3:25 PM Cameron Simpson wrote: > On 24Nov2021 22:45, Kaushal Shriyan wrote: > >I am running CentOS Linux release 7.9.2009 (Core) and have enabled > >below > >entry in .vimrc file > > > >$cat .vimrc > >set shiftwidth=2 > >set shiftround > >set tabstop=4 > >set softtabstop=4 > >set noexpandtab > >set number > >set ruler > >set noautoindent > >set nosmartindent > >set noincsearch > >syntax enable > >$ > > > >when i do vim testpythonscript.py I am facing the below Pymode error > > > >$vim testpythonscript.py > > > >[Pymode]: error: Pymode requires vim compiled with +python3 (exclusively). > >Most of features will be disabled. > > This means that the vim you're invoking does not have the "python3" > feature compiled into it. Indeed, in the `vim --version` output you > include there is this: > > $vim --version > VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Dec 15 2020 16:44:08) > Included patches: 1-207, 209-629 > Modified by > Compiled by > Huge version without GUI. Features included (+) or not (-): > [...] > +cryptv +linebreak +python/dyn +viminfo > +cscope +lispindent -python3 +vreplace > [...] > > So you can see that "python3" has been omitted from that build. > > Do a "yum search vim" and see if there is a vim variant with "python3" > included (I haven't got a Centos host available to me). You could try > replacing the installed vim with the python3 enabled vim if that is > available. > > Cheers, > Cameron Simpson > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From alan.gauld at yahoo.co.uk Mon Nov 29 09:03:43 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 29 Nov 2021 14:03:43 +0000 Subject: [Tutor] Explanation of smote and upsampling In-Reply-To: References: Message-ID: On 28/11/2021 23:03, Tariq Khasiri wrote: > ??? > > oversample = SMOTE()X, y = > oversample.fit_resample(c_data[c_data.columns[1:]], > c_data[c_data.columns[0]])usampled_df = X.assign(Churn = y) > > > ??? > > > Does this portion of code mean that I am upsampling column 0 and > column 1 of my data frame ( c_data ) ? Please kindly advise. Not > understanding this part of code ?????-> c_data[c_data.columns[1:] It helps if you tell us what libraries you are using. This list is really for questions about the core language and standard library. Anything extra needs some explanation. Reformatting your code as I think it should be... oversample = SMOTE() X, y = oversample.fit_resample( c_data[c_data.columns[1:]], c_data[c_data.columns[0]] ) usampled_df = X.assign(Churn = y) As to the part you don't understand, that will largely depend on the definition of c_data, which I assume is an instance of a class in your library? But without knowing the library we can only guess at what it does. 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 Mon Nov 29 09:22:27 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 29 Nov 2021 14:22:27 +0000 Subject: [Tutor] Multi threading and I/O bound process In-Reply-To: References: Message-ID: On 29/11/2021 01:53, Manprit Singh wrote: > 1) Without multi threading > > import time > import urllib.request > # a function to dsplay first 25 bytes of a webpage > def readpage(urladd): > with urllib.request.urlopen(urladd) as fobj: > print(fobj.read(25)) > > t0 = time.time() Note you start the timer just before the loop after the intialisation and function definition... > for _ in range(6): > readpage("https://www.xyz.com/") > t1 = time.time() > print(t1-t0) > will display the first 25 bytes of the webpage 6 times in a sequential > order. and measure the time > > 2) Multithreading Using constructor : > > t0= time.time() Here you include the thread import and all of the initialisatoion and class/function definitions that that includes. Not really a fair comparison, especially on such a small scale example where every mS is significant. > import threading > threadsgrp = [] > for _ in range(6): > thread = threading.Thread(target=readpage,args=("https://www.xyz.com/ > ",)) > threadsgrp.append(thread) > thread.start() > for th in threadsgrp: > th.join() > t1 =time.time() > print(t1-t0) > > 3) Multithreading using subclass : > > t1 = time.time() And the same here, you are including the class definition time in the total. The init/definition code should only run once in your program so it is not representative of the time that threading/non-threading takes in a real work program where threading is called many times in different places. > class Techlivethread(threading.Thread): > def __init__(self, add): > threading.Thread.__init__(self) > self.add = add > def run(self): > readpage(self.add) It would be more efficient to transfer the readpage code into run() directly since all you do is call it. > grp =[] > for _ in range(6): > th1 = Techlivethread("https://www.xyz.com/") > grp.append(th1) > th1.start() > for ele in grp: > ele.join() > t2 = time.time() > print(t2-t1) -- 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 joao.oliveira at ufob.edu.br Mon Nov 29 16:59:32 2021 From: joao.oliveira at ufob.edu.br (Joao Carlos Silva de Oliveira Matos) Date: Mon, 29 Nov 2021 18:59:32 -0300 Subject: [Tutor] Send SOAP to Proxy Message-ID: I've been trying to access a WSDL Service via a SOAP protocol. But first I need to get to a proxy in my University because the server will only accept it as a client. I found Zeep as the main python library for this job. But I have not been successful in my tries. I know that I should use "HTTP". Another important note: IT from the University says my request are getting there as Web Application, meanwhile other requests are labeled as SOAP. I hope you could help me find a better way to write this script without getting error 403. These are some structures I've already tried: #01 session = Session() session.proxies = { 'http': 'http://proxy-ip-adress:8080' } transport=Transport(session=session) client = Client('http://webservice/path?wsdl', transport=transport) result = client.service.getMethod('params') print(result) --------------------------------------------------------------------------------------------------------- #02 client = Client('http://proxy-ip-adress:8080/path?wsdl') result = client.service.getMethod('params') print(result) I've also tried the transport docs from Transports ? Zeep 4.1.0 documentation (python-zeep.org) and I've tried setting session.trust_env = False. -- [image: Ficheiro:Bras?o da UFOB.png ? Wikip?dia, a enciclop?dia livre] Jo?o Carlos Silva de Oliveira Matos Bolsista de Inova??o e Tecnologia PROFNIT - Centro das Humanidades - UFOB Mat. 2020100150 From marjababernard at gmail.com Mon Nov 29 19:01:04 2021 From: marjababernard at gmail.com (Bernard Marjaba) Date: Mon, 29 Nov 2021 19:01:04 -0500 Subject: [Tutor] Error When Using CoolProp In-Reply-To: References: Message-ID: Hello, Yeah sure I?m checking with them as well. Meanwhile, this is a purely python related error following what you suggested to me about installing the wheel package: I tried installing the wheel package using (venv) Bernards-MacBook-Pro:Python Bernard$ pip install /Users/Bernard/Downloads/CoolProp-6.4.1-cp38-cp38-macosx_10_9_x86_64.whl but it gave me this error ERROR: CoolProp-6.4.1-cp38-cp38-macosx_10_9_x86_64.whl is not a supported wheel on this platform. I assume this means that i need python 3.8 for this wheel to work, which I have. I typed python3 in terminal it gave me 3.8.10, i typed python3 in PyCharm and it gave me 3.9.5, how come? So i searched for python 3.9 in my MacBook and i couldn?t find any traces of it, except for in a project folder: /Users/Bernard/PycharmProjects/Isentropic SUS Model/venv/lib/python3.9 However, 3.8 exists in the Applications folder. Any idea how to remove all traces of 3.9? Thanks and regards, Bernard Marjaba (514) 922-9807 > On Nov 24, 2021, at 6:53 PM, Mats Wichmann wrote: > > On 11/24/21 13:17, Bernard Marjaba wrote: >> Hello >> I am having trouble using CoolProp with python. I have macOS Big Sur 11.5.1 with python version 3.10. >> import CoolProp.CoolProp as CP >> is giving me the error ModuleNotFoundError: No module named ?CoolProp.CoolProp? because it is not installed. I am however using it with MATLAB with no issues. I assume each software has its own wrapper? >> So I tried installing CoolProp using pip install coolprop, and got the following error: >> Collecting coolprop >> Using cached CoolProp-6.4.1.tar.gz (12.9 MB) >> Preparing metadata (setup.py) ... done >> Using legacy 'setup.py install' for coolprop, since package 'wheel' is not installed. >> Installing collected packages: coolprop >> Running setup.py install for coolprop ... error >> ERROR: Command errored out with exit status 1: >> command: '/Users/Bernard/PycharmProjects/Isentropic SUS Model/venv/bin/python' -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/1j/jv39r0t12cb4dhscxkdz97740000gn/T/pip-install-z5afcrzx/coolprop_6739af7137474652994855ae32a03dd4/setup.py'"'"'; __file__='"'"'/private/var/folders/1j/jv39r0t12cb4dhscxkdz97740000gn/T/pip-install-z5afcrzx/coolprop_6739af7137474652994855ae32a03dd4/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /private/var/folders/1j/jv39r0t12cb4dhscxkdz97740000gn/T/pip-record-8453i4i7/install-record.txt --single-version-externally-managed --compile --install-headers '/Users/Bernard/PycharmProjects/Isentropic SUS Model/venv/include/site/python3.9/coolprop' >> cwd: /private/var/folders/1j/jv39r0t12cb4dhscxkdz97740000gn/T/pip-install-z5afcrzx/coolprop_6739af7137474652994855ae32a03dd4/ >> Complete output (79 lines): >> xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun >> OSX build detected, targetting 10.9 using clang/gcc v0.0. >> Cython will not be used; cy_ext is cpp >> running install >> /Users/Bernard/PycharmProjects/Isentropic SUS Model/venv/lib/python3.9/site-packages/setuptools/command/install.py:34: SetuptoolsDeprecationWarning: setup.py install is deprecated. Use build and pip and other standards-based tools. >> warnings.warn( >> running build >> running build_py >> creating build >> creating build/lib.macosx-10.9-x86_64-3.9 >> creating build/lib.macosx-10.9-x86_64-3.9/CoolProp >> copying CoolProp/constants.py -> build/lib.macosx-10.9-x86_64-3.9/CoolProp >> copying CoolProp/__init__.py -> build/lib.macosx-10.9-x86_64-3.9/CoolProp >> copying CoolProp/BibtexParser.py -> build/lib.macosx-10.9-x86_64-3.9/CoolProp >> copying CoolProp/HumidAirProp.py -> build/lib.macosx-10.9-x86_64-3.9/CoolProp >> copying CoolProp/State.py -> build/lib.macosx-10.9-x86_64-3.9/CoolProp >> creating build/lib.macosx-10.9-x86_64-3.9/CoolProp/tests >> copying CoolProp/tests/runner.py -> build/lib.macosx-10.9-x86_64-3.9/CoolProp/tests >> copying CoolProp/tests/test_plots.py -> build/lib.macosx-10.9-x86_64-3.9/CoolProp/tests >> copying CoolProp/tests/test_Props.py -> build/lib.macosx-10.9-x86_64-3.9/CoolProp/tests >> copying CoolProp/tests/__init__.py -> build/lib.macosx-10.9-x86_64-3.9/CoolProp/tests >> copying CoolProp/tests/test_CoolPropState.py -> build/lib.macosx-10.9-x86_64-3.9/CoolProp/tests >> creating build/lib.macosx-10.9-x86_64-3.9/CoolProp/GUI >> copying CoolProp/GUI/__init__.py -> build/lib.macosx-10.9-x86_64-3.9/CoolProp/GUI >> copying CoolProp/GUI/CoolPropGUI.py -> build/lib.macosx-10.9-x86_64-3.9/CoolProp/GUI >> copying CoolProp/GUI/PsychScript.py -> build/lib.macosx-10.9-x86_64-3.9/CoolProp/GUI >> creating build/lib.macosx-10.9-x86_64-3.9/CoolProp/Plots >> copying CoolProp/Plots/ConsistencyPlots_pcsaft.py -> build/lib.macosx-10.9-x86_64-3.9/CoolProp/Plots >> copying CoolProp/Plots/PsychChart.py -> build/lib.macosx-10.9-x86_64-3.9/CoolProp/Plots >> copying CoolProp/Plots/SimpleCycles.py -> build/lib.macosx-10.9-x86_64-3.9/CoolProp/Plots >> copying CoolProp/Plots/__init__.py -> build/lib.macosx-10.9-x86_64-3.9/CoolProp/Plots >> copying CoolProp/Plots/psy.py -> build/lib.macosx-10.9-x86_64-3.9/CoolProp/Plots >> copying CoolProp/Plots/Plots.py -> build/lib.macosx-10.9-x86_64-3.9/CoolProp/Plots >> copying CoolProp/Plots/Common.py -> build/lib.macosx-10.9-x86_64-3.9/CoolProp/Plots >> copying CoolProp/Plots/SimpleCyclesCompression.py -> build/lib.macosx-10.9-x86_64-3.9/CoolProp/Plots >> copying CoolProp/Plots/ConsistencyPlots.py -> build/lib.macosx-10.9-x86_64-3.9/CoolProp/Plots >> copying CoolProp/Plots/PsychScript.py -> build/lib.macosx-10.9-x86_64-3.9/CoolProp/Plots >> copying CoolProp/Plots/Tests.py -> build/lib.macosx-10.9-x86_64-3.9/CoolProp/Plots >> copying CoolProp/Plots/SimpleCyclesExpansion.py -> build/lib.macosx-10.9-x86_64-3.9/CoolProp/Plots >> copying CoolProp/typedefs.pxd -> build/lib.macosx-10.9-x86_64-3.9/CoolProp >> copying CoolProp/CoolProp.pxd -> build/lib.macosx-10.9-x86_64-3.9/CoolProp >> copying CoolProp/State.pxd -> build/lib.macosx-10.9-x86_64-3.9/CoolProp >> copying CoolProp/cAbstractState.pxd -> build/lib.macosx-10.9-x86_64-3.9/CoolProp >> copying CoolProp/constants_header.pxd -> build/lib.macosx-10.9-x86_64-3.9/CoolProp >> copying CoolProp/AbstractState.pxd -> build/lib.macosx-10.9-x86_64-3.9/CoolProp >> copying CoolProp/Plots/psyrc -> build/lib.macosx-10.9-x86_64-3.9/CoolProp/Plots >> running build_ext >> creating private >> creating private/var >> creating private/var/folders >> creating private/var/folders/1j >> creating private/var/folders/1j/jv39r0t12cb4dhscxkdz97740000gn >> creating private/var/folders/1j/jv39r0t12cb4dhscxkdz97740000gn/T >> creating private/var/folders/1j/jv39r0t12cb4dhscxkdz97740000gn/T/pip-install-z5afcrzx >> creating private/var/folders/1j/jv39r0t12cb4dhscxkdz97740000gn/T/pip-install-z5afcrzx/coolprop_6739af7137474652994855ae32a03dd4 >> gcc -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -arch x86_64 -g -I/Users/Bernard/PycharmProjects/Isentropic SUS Model/venv/include -I/Library/Frameworks/Python.framework/Versions/3.9/include/python3.9 -c /private/var/folders/1j/jv39r0t12cb4dhscxkdz97740000gn/T/pip-install-z5afcrzx/coolprop_6739af7137474652994855ae32a03dd4/tmp0hdk2tsu.cpp -o private/var/folders/1j/jv39r0t12cb4dhscxkdz97740000gn/T/pip-install-z5afcrzx/coolprop_6739af7137474652994855ae32a03dd4/tmp0hdk2tsu.o >> xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun >> gcc -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -arch x86_64 -g -I/Users/Bernard/PycharmProjects/Isentropic SUS Model/venv/include -I/Library/Frameworks/Python.framework/Versions/3.9/include/python3.9 -c /private/var/folders/1j/jv39r0t12cb4dhscxkdz97740000gn/T/pip-install-z5afcrzx/coolprop_6739af7137474652994855ae32a03dd4/tmp0r3vfej0.cpp -o private/var/folders/1j/jv39r0t12cb4dhscxkdz97740000gn/T/pip-install-z5afcrzx/coolprop_6739af7137474652994855ae32a03dd4/tmp0r3vfej0.o >> xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun >> gcc -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -arch x86_64 -g -I/Users/Bernard/PycharmProjects/Isentropic SUS Model/venv/include -I/Library/Frameworks/Python.framework/Versions/3.9/include/python3.9 -c /private/var/folders/1j/jv39r0t12cb4dhscxkdz97740000gn/T/pip-install-z5afcrzx/coolprop_6739af7137474652994855ae32a03dd4/tmp93394q_4.cpp -o private/var/folders/1j/jv39r0t12cb4dhscxkdz97740000gn/T/pip-install-z5afcrzx/coolprop_6739af7137474652994855ae32a03dd4/tmp93394q_4.o >> xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun >> Adding these shared_ptr compilation macros: [] >> building 'CoolProp.CoolProp' extension >> creating build/temp.macosx-10.9-x86_64-3.9 >> creating build/temp.macosx-10.9-x86_64-3.9/CoolProp >> creating build/temp.macosx-10.9-x86_64-3.9/src >> creating build/temp.macosx-10.9-x86_64-3.9/src/Backends >> creating build/temp.macosx-10.9-x86_64-3.9/src/Backends/Cubics >> creating build/temp.macosx-10.9-x86_64-3.9/src/Backends/Helmholtz >> creating build/temp.macosx-10.9-x86_64-3.9/src/Backends/Helmholtz/Fluids >> creating build/temp.macosx-10.9-x86_64-3.9/src/Backends/IF97 >> creating build/temp.macosx-10.9-x86_64-3.9/src/Backends/Incompressible >> creating build/temp.macosx-10.9-x86_64-3.9/src/Backends/PCSAFT >> creating build/temp.macosx-10.9-x86_64-3.9/src/Backends/REFPROP >> creating build/temp.macosx-10.9-x86_64-3.9/src/Backends/Tabular >> creating build/temp.macosx-10.9-x86_64-3.9/src/Tests >> gcc -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -arch x86_64 -g -I. -I./include -I./src -I./externals/Eigen -I./externals/fmtlib -I./externals/msgpack-c/include -I/Users/Bernard/PycharmProjects/Isentropic SUS Model/venv/include -I/Library/Frameworks/Python.framework/Versions/3.9/include/python3.9 -c CoolProp/CoolProp.cpp -o build/temp.macosx-10.9-x86_64-3.9/CoolProp/CoolProp.o >> xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun >> error: command '/usr/bin/gcc' failed with exit code 1 >> ---------------------------------------- >> ERROR: Command errored out with exit status 1: '/Users/Bernard/PycharmProjects/Isentropic SUS Model/venv/bin/python' -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/1j/jv39r0t12cb4dhscxkdz97740000gn/T/pip-install-z5afcrzx/coolprop_6739af7137474652994855ae32a03dd4/setup.py'"'"'; __file__='"'"'/private/var/folders/1j/jv39r0t12cb4dhscxkdz97740000gn/T/pip-install-z5afcrzx/coolprop_6739af7137474652994855ae32a03dd4/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /private/var/folders/1j/jv39r0t12cb4dhscxkdz97740000gn/T/pip-record-8453i4i7/install-record.txt --single-version-externally-managed --compile --install-headers '/Users/Bernard/PycharmProjects/Isentropic SUS Model/venv/include/site/python3.9/coolprop' Check the logs for full command output. > > > looks like you probably don't have the command-line developer tools installed - it's separate from the main xcode install on MacOS. > > also, you might be able to install without the effort to build if you installed the wheel package first - see this error from early on: > > > Using legacy 'setup.py install' for coolprop, since package 'wheel' is not installed. > > I have no idea what CoolProp is, never heard of it, but a check here: > > https://pypi.org/project/CoolProp/#files > > shows there *is* a macosx wheel available for installation - at least as long as you're using no newer than Python 3.8. The project looks like it hasn't updated for a good long time so they haven't done anything for 3.9 or 3.10. That's probably cause for some worry... > > You'd be far better asking this kind of thing directly to the project in question, we won't be able to answer any further stuff from here. From manpritsinghece at gmail.com Tue Nov 30 12:05:14 2021 From: manpritsinghece at gmail.com (Manprit Singh) Date: Tue, 30 Nov 2021 22:35:14 +0530 Subject: [Tutor] __str__ Vs __repr__ Message-ID: Dear Sir, After assigning a value to a variable, Lets say variable is a, and the value assigned to it is 5, On an interactive shell after writing a and then hitting Enter key, it prints the value of a, that is 5, This happens due to __str__ or __repr__? What should I use(__str__ or __repr__) if I want something to be printed when I print an object of a user defined class ? From alexkleider at gmail.com Tue Nov 30 12:53:45 2021 From: alexkleider at gmail.com (Alex Kleider) Date: Tue, 30 Nov 2021 09:53:45 -0800 Subject: [Tutor] __str__ Vs __repr__ In-Reply-To: References: Message-ID: https://www.tutorialspoint.com/str-vs-repr-in-python On Tue, Nov 30, 2021 at 9:06 AM Manprit Singh wrote: > Dear Sir, > > After assigning a value to a variable, Lets say variable is a, and the > value assigned to it is 5, > On an interactive shell after writing a and then hitting Enter key, it > prints the value of a, that is 5, This happens due to __str__ or __repr__? > > What should I use(__str__ or __repr__) if I want something to be printed > when I print an object of a user defined class ? > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From wlfraed at ix.netcom.com Tue Nov 30 12:58:43 2021 From: wlfraed at ix.netcom.com (Dennis Lee Bieber) Date: Tue, 30 Nov 2021 12:58:43 -0500 Subject: [Tutor] __str__ Vs __repr__ References: Message-ID: On Tue, 30 Nov 2021 22:35:14 +0530, Manprit Singh declaimed the following: >After assigning a value to a variable, Lets say variable is a, and the >value assigned to it is 5, >On an interactive shell after writing a and then hitting Enter key, it >prints the value of a, that is 5, This happens due to __str__ or __repr__? > Using that same shell, you can easily determine which is used. Note that __str__() formats "human readable" text, while __repr__() formats "program" text. PythonWin 3.8.2 (default, Aug 25 2020, 15:54:26) [MSC v.1900 64 bit (AMD64)] on win32. Portions Copyright 1994-2018 Mark Hammond - see 'Help/About PythonWin' for further copyright information. >>> a = 5 >>> a 5 >>> b = "5" >>> b '5' >>> print(a, b) 5 5 >>> print(str(a), str(b)) 5 5 >>> print (repr(a), repr(b)) 5 '5' >>> Note how the sell displays b with quote marks -- which is what you get when asking for repr() of a string object. >What should I use(__str__ or __repr__) if I want something to be printed >when I print an object of a user defined class ? You would have to write a __str__() method to explicitly format some form of the object for user output, or __repr__() for some form that might be used in the code as a literal. Note that, for many classes, there is no reasonable __repr__() that generates a literal that could be used in the program -- hence why one often gets just text stating "class" (or "type") instance "ID" (for common Python, memory address). >>> class C(): ... def __init__(self, arg): ... self.arg = arg ... def __repr__(self): ... return "Class C instance with arg = %r" % self.arg ... >>> c1 = C(5) >>> c2 = C("5") >>> c1 Class C instance with arg = 5 >>> c2 Class C instance with arg = '5' >>> %r invokes repr() which invokes the object __repr__() %s invokes str() which invokes __str__() IF IT EXISTS, otherwise it uses __repr__() >>> str(c1) 'Class C instance with arg = 5' >>> str(c2) "Class C instance with arg = '5'" Note how str(c2) shows the quoted '5' which matches the repr() of the object. Now, add a __str__() method using %s translation >>> class D(): ... def __init__(self, arg): ... self.arg = arg ... def __repr__(self): ... return "Class D instance with arg = %r" % self.arg ... def __str__(self): ... return "Class D instance with arg = %s" % self.arg ... >>> d1 = D(5) >>> d2 = D("6") >>> d1 Class D instance with arg = 5 >>> d2 Class D instance with arg = '6' >>> str(d1) 'Class D instance with arg = 5' >>> str(d2) 'Class D instance with arg = 6' >>> repr(d1) 'Class D instance with arg = 5' >>> repr(d2) "Class D instance with arg = '6'" >>> The shell still uses the repr() format, but str() uses the other format, which doesn't add quotes. >>> print(d1, d2) Class D instance with arg = 5 Class D instance with arg = 6 >>> print(c1, c2) Class C instance with arg = 5 Class C instance with arg = '5' >>> print() uses the __str__() form where possible, otherwise devolves to __repr__() form. >_______________________________________________ >Tutor maillist - Tutor at python.org >To unsubscribe or change subscription options: >https://mail.python.org/mailman/listinfo/tutor -- Wulfraed Dennis Lee Bieber AF6VN wlfraed at ix.netcom.com http://wlfraed.microdiversity.freeddns.org/ From phillor9 at gmail.com Tue Nov 30 16:57:40 2021 From: phillor9 at gmail.com (Phil) Date: Wed, 1 Dec 2021 08:57:40 +1100 Subject: [Tutor] pylint(too-many-nested-blocks) In-Reply-To: <703ee90c-2d81-5ea3-91aa-7b2a399e4531@DancesWithMice.info> References: <2c34791d-dc21-4070-82d2-1c98d67616ce@gmail.com> <703ee90c-2d81-5ea3-91aa-7b2a399e4531@DancesWithMice.info> Message-ID: Thank you dn for taking the time to write a lengthy informative reply and thank you once again Cameron and Mats. I will endeavor to make good use of the advice given. > Does Python itself see this as an error and stop working, or is it only > the code-checker s/w? It's not so much an error message, rather a recommendation and one that I feel that I should act on. > Were we conducting a Code Review, this function would definitely attract > criticism. You have already recognised the complexity, but seem somewhat > satisfied - on the grounds that the whole works. I think I've only ever produced one application that was truly useful. My current project doesn't do anything that others cannot, it's just a my-way project and so I could leave it as it is or try to make it look more a work of art. > Here's where things become rather subjective. You (no criticism) seem to > have a mathematical view of programming, Isn't computer programming a pursuit based on maths? Even so, I suppose the result doesn't need to look like a dog's breakfast. > I have been criticised (but exhibit little remorse) for breaking things > down into "smaller" units of code than others feel necessary. I can see where I could split the function that I posted into two and one of those parts could be reused in a yet-to-be-written function. I do try to reuse code where I can. I'll need to give a lot more thought to how I might reduce the complexity. > Now is a good time pick-up the testing tool's feedback: "understandable > and maintainable". It's not merely a matter of > "readability"/"understandability", but also maintainability - and that > of the objectives of the tool: "testability". I know that testing tools exist, however, I've always considered their use to be beyond the understanding of the neophyte especially this amateur programmer. > Earlier the idea of naming a chunk of code was mentioned. I find it difficult to come up with meaningful names for functions and even variables. I know that 'x' or 'y' is often a poor choice and I often leave them in place until I can think of something better. Often I don't and so they stay, and of course, I forget what they mean the next time I look at the code again. > (I'll leave you to research the topics-mentioned that may interest you, > as you see fit...) I haven't spent anytime on the laptop or the Internet for the fast few days and so I haven't had a chance to study the topic of cylomatic complexity, however, I do have a Wikipedia page open and I will try to gain some understanding of it after I post this e-mail. -- Regards, Phil From cs at cskk.id.au Tue Nov 30 16:40:45 2021 From: cs at cskk.id.au (Cameron Simpson) Date: Wed, 1 Dec 2021 08:40:45 +1100 Subject: [Tutor] Send SOAP to Proxy In-Reply-To: References: Message-ID: On 29Nov2021 18:59, Joao Carlos Silva de Oliveira Matos wrote: >I've been trying to access a WSDL Service via a SOAP protocol. But first I >need to get to a proxy in my University because the server will only accept >it as a client. Skipping the complexities of the library, if it is using the builtin Python libraries underneath I expect it to honour the normal environment variables $http_proxy and $https_proxy, meaning your code does not need to specify a proxy. If you set these in your environment and try your code with no proxy specification in it, do things work? Bourne shell syntax to set these: http_proxy='http://proxy-ip-adress:8080' https_proxy='http://proxy-ip-adress:8080' export http_proxy https_proxy If you set them in your environment, do things work (provided you strip the settings out of the inline code itself i.e. comment out the "session.proxies" stuff? Cheers,Cameron Simpson From cs at cskk.id.au Tue Nov 30 15:43:09 2021 From: cs at cskk.id.au (Cameron Simpson) Date: Wed, 1 Dec 2021 07:43:09 +1100 Subject: [Tutor] __str__ Vs __repr__ In-Reply-To: References: Message-ID: On 30Nov2021 22:35, Manprit Singh wrote: >After assigning a value to a variable, Lets say variable is a, and the >value assigned to it is 5, >On an interactive shell after writing a and then hitting Enter key, it >prints the value of a, that is 5, This happens due to __str__ or __repr__? __repr__, because that generally provides greater detail. Ideally it provides text which you could run as a Python expression to get the value. __str__ is intended for "normal output", whatever that means :-( The print() function calls __str__ on every object it prints. So when you go: print('foo') you get a bare: foo without any quotes. Generally an object's __str__ should be concise and not have much punctuation in it. It should make sense when the reader already knows what type of thing is being printed. >What should I use(__str__ or __repr__) if I want something to be printed >when I print an object of a user defined class ? That depends entirely on context, unfortunately. It isn't "what version should I print?" but "what version does the user want?" If you're talking to an end user (instead of you, the programmer, at the interactive prompt) you probably want __str__ (because print() will do that for you automatically), and a better question is instead: what should __str__ return? Something concise and readable. __repr__ is for the programmer doing debugging. Cheers, Cameron Simpson From PyTutor at DancesWithMice.info Tue Nov 30 18:19:11 2021 From: PyTutor at DancesWithMice.info (dn) Date: Wed, 1 Dec 2021 12:19:11 +1300 Subject: [Tutor] pylint(too-many-nested-blocks) In-Reply-To: References: <2c34791d-dc21-4070-82d2-1c98d67616ce@gmail.com> <703ee90c-2d81-5ea3-91aa-7b2a399e4531@DancesWithMice.info> Message-ID: <34dc6d5f-99f7-7826-95f3-1e5965ddfb87@DancesWithMice.info> On 01/12/2021 10.57, Phil wrote: > Thank you dn for taking the time to write a lengthy informative reply > and thank you once again Cameron and Mats. I will endeavor to make good > use of the advice given. Glad to hear it's helpful. It is evident that there are plenty of other folk 'here', who read posts which are not explicitly addressed to them, and who gain in some small, or maybe, large way. That's one of the functions (hah!) and benefits of the list. (also, you can believe that I collect that these are re-worked and become multi-purpose - ooh, somewhat similar to a Python function. Oh boy, he's on-a-roll today, as you will see...) >> Does Python itself see this as an error and stop working, or is it only >> the code-checker s/w? > > It's not so much an error message, rather a recommendation and one that > I feel that I should act on. Right answer! >> Were we conducting a Code Review, this function would definitely attract >> criticism. You have already recognised the complexity, but seem somewhat >> satisfied - on the? grounds that the whole works. > > I think I've only ever produced one application that was truly useful. > My current project doesn't do anything that others cannot, it's just a > my-way project and so I could leave it as it is or try to make it look > more a work of art. The "Ikea effect", like home-cooking*, is predicated largely on one's feelings of satisfaction at accomplishment, over-riding other factors, eg constructing an Ikea cot for baby, but only one side of the cot will slide up-and-down its rails due to a construction error - but hey, I built it with my own (?)fair hands! * we had a rule in my student flat/'frat house' (last century) that "if the cook eats the food, no-one else is allowed to complain". OTOH, the non-practical pursuit of perfection leads to one never being 'finished'! Where is your 'happy medium'? (rhetorical question) There are (apparently) employers who will look at your portfolio of work (should you ever be applying for such a job), and thus it doesn't matter whether you are repeating others' projects, or making one that is only useful to yourself... Then there is: "practice makes perfect"! >> Here's where things become rather subjective. You (no criticism) seem to >> have a mathematical view of programming, > > Isn't computer programming a pursuit based on maths? Even so, I suppose > the result doesn't need to look like a dog's breakfast. Apologies, I suspected you'd fall into this pattern - and many others 'here' (and amongst your correspondents) will ask/challenge "and what's wrong with that?". Programming as a hobby may well be a solitary activity, but few professionals work in-isolation (says he, who works in a country on the opposite side of the world from all of his colleagues, whilst dealing with folk from all corners of the globe*). Accordingly, the line about code being read by humans! So, if you are writing for humans, how mathematical do you expect us to be? (free advice: don't expect it to be 'much') Things only roll (rapidly) 'down-hill' from there: what about programs being used by "users"? The 'mental model' that users have of any computer system is almost certainly, and quite possibly, radically-different to the way we 'see' it. If the authors want a system to be used, this needs to be taken into account. Now you've left Math way-behind, and are slogging through the slough of "User Experience", ie Psychology and Sociology. It may not matter in this case, indeed in any 'hobby' scenario. However, good habits are by-definition "good" - and bad habits are hard to lose ("become vices")! Also, please recall that 'six month you' concept - a human ex machina! * gratuitous use of English-expression, with the possible outcome of irritating mathematicians everywhere. (Heh, heh, heh - cackle, cackle...) >> I have been criticised (but exhibit little remorse) for breaking things >> down into "smaller" units of code than others feel necessary. > > I can see where I could split the function that I posted into two and > one of those parts could be reused in a yet-to-be-written function. I do > try to reuse code where I can. I'll need to give a lot more thought to > how I might reduce the complexity. As mentioned, "re-use" is not the only justification for building/abstracting code away and into a function. I'm currently reviewing "Learn Python Programming, 2nd Edn" by Fabrizio Romano, Packt, 2018. Chapter 4 is entitled "Functions, the Building Blocks of Code". After the chapter-intro, he invites us to consider: "Why use functions?". Here are his answers:- - they reduce code duplication in a program... [exactly what I was first taught back in the days of "spaghetti code" and "monolithic programs"] - they help in splitting a complex task or procedure into smaller blocks, each of which becomes a function [you may have read something similar, recently] - they hide the implementation details from their users [I didn't introduce the term "Information/Implementation Hiding" before - you had/still have enough to catch-up on. That said, it is an important concept, following-on from the previous point] - they improve traceability [another word related to debugging and testing] - they improve readability ['nuff said] (the chapter thereafter details each of these points) NB I'm not recommending the book (haven't read it (all) yet!) per-se, simply taking-advantage of the serendipitous opportunity. It may be worth your while to purchase a (reasonably-recent) text-book, to give you some guide - what we call a "learning path"! Incidentally, there's an impression that you've adopted a 'grey gypsy' life-style. However, if you can gain decent Internet access, perhaps an online course might confer similar additional/alternate benefit? >> Now is a good time pick-up the testing tool's feedback: "understandable >> and maintainable". It's not merely a matter of >> "readability"/"understandability", but also maintainability - and that >> of the objectives of the tool: "testability". > > I know that testing tools exist, however, I've always considered their > use to be beyond the understanding of the neophyte especially this > amateur programmer. Well then, you can color me impressed, that you are using any such 'testing tool' at all. Few trainees see the need (a direct consequence of trainers' employing 'toy examples' which are short and easily(?) digested - but quite unlike 'real life' - and as such, the "need" for much beyond the 'mathematical side' of programming is not adequately established!) I cheerfully blather-on about pytest and TDD. Some of the irritation is (that I'm irritating) because folk don't like to have un-learn/re-learn 'stuff'! It's hard work - and harder than learning something for 'the first time'! Are you using an IDE such as PyCharm or Codium (the F/LOSS-y alternative to VS-Code)? If so, there are plenty of "linters" (as others mentioned earlier) and other learning-tools. I'm keen on Sourcery (British, hence the spelling) which spots opportunities to show how things could be better-expressed and/or accomplished more efficiently or more 'pythonically'. That said, add too many of these tools, and you will quickly feel overwhelmed by the volume of 'feedback'. Hence YMMV! >> Earlier the idea of naming a chunk of code was mentioned. > > I find it difficult to come up with meaningful names for functions and > even variables. I know that 'x' or 'y' is often a poor choice and I > often leave them in place until I can think of something better. Often I > don't and so they stay, and of course, I forget what they mean the next > time I look at the code again. Welcome to the club! On another serendipitous note, a few weeks ago I was tracking-down the source of a quotation (about our work being the invention of 'names'), and fellow list-members here (or maybe the Python list) were able to remind me of "The Mental Game of Python" a one-hour talk given by Raymond Hettinger - one of the "giants" of Python, ie one whose "shoulders" it is worth "standing upon" (https://www.youtube.com/watch?v=UANN2Eu6ZnM) Well worth the time - and maybe you have a YouTube downloader if Internet-reliability is an issue... Back to the earlier question about IDEs. I'm using PyCharm at the moment (I use whatever other project-members use/insist I use). PyCharm makes a point of trying to 'understand' the Python code (which requires a larger/faster computer than a Notepad-level editor!), and that empowers functionality such as being able to "refactor" by renaming. Thus, every use of that name is refactored auto-magically and in one-step (don't forget to re-run all your pytests afterwards just be sure!). To which short-cuts, this lazy-boy says, "Oh yeah"! >> (I'll leave you to research the topics-mentioned that may interest you, >> as you see fit...) > > I haven't spent anytime on the laptop or the Internet for the fast few > days and so I haven't had a chance to study the topic of cylomatic > complexity, however, I do have a Wikipedia page open and I will try to > gain some understanding of it after I post this e-mail. Now you have even more... Isn't life great? -- Regards, =dn From alan.gauld at yahoo.co.uk Tue Nov 30 19:33:22 2021 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 1 Dec 2021 00:33:22 +0000 Subject: [Tutor] pylint(too-many-nested-blocks) In-Reply-To: <34dc6d5f-99f7-7826-95f3-1e5965ddfb87@DancesWithMice.info> References: <2c34791d-dc21-4070-82d2-1c98d67616ce@gmail.com> <703ee90c-2d81-5ea3-91aa-7b2a399e4531@DancesWithMice.info> <34dc6d5f-99f7-7826-95f3-1e5965ddfb87@DancesWithMice.info> Message-ID: On 30/11/2021 23:19, dn via Tutor wrote: > If so, there are plenty of "linters" (as others mentioned > earlier) and other learning-tools. I'm keen on Sourcery (British, hence > the spelling) huh? We spell sorcery like so. (pronounced like horse-ery) We spell source code like so. (pronounced as s-oh-rce) I'm not sure where British spelling comes in? Confused -- 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 Nov 30 19:41:17 2021 From: mats at wichmann.us (Mats Wichmann) Date: Tue, 30 Nov 2021 17:41:17 -0700 Subject: [Tutor] pylint(too-many-nested-blocks) In-Reply-To: References: <2c34791d-dc21-4070-82d2-1c98d67616ce@gmail.com> <703ee90c-2d81-5ea3-91aa-7b2a399e4531@DancesWithMice.info> <34dc6d5f-99f7-7826-95f3-1e5965ddfb87@DancesWithMice.info> Message-ID: <1A5E3038-D275-495D-9AF0-5BFE7760FF83@wichmann.us> On November 30, 2021 5:33:22 PM MST, Alan Gauld via Tutor wrote: >On 30/11/2021 23:19, dn via Tutor wrote: >> If so, there are plenty of "linters" (as others mentioned >> earlier) and other learning-tools. I'm keen on Sourcery (British, hence >> the spelling) > >huh? > >We spell sorcery like so. (pronounced like horse-ery) >We spell source code like so. (pronounced as s-oh-rce) > >I'm not sure where British spelling comes in? > >Confused sourcery is just a kind of pun... and a Pratchett book, if I remember right. -- Sent from a mobile device with K-9 Mail. Please excuse my brevity. From PyTutor at DancesWithMice.info Tue Nov 30 20:05:22 2021 From: PyTutor at DancesWithMice.info (dn) Date: Wed, 1 Dec 2021 14:05:22 +1300 Subject: [Tutor] pylint(too-many-nested-blocks) In-Reply-To: References: <2c34791d-dc21-4070-82d2-1c98d67616ce@gmail.com> <703ee90c-2d81-5ea3-91aa-7b2a399e4531@DancesWithMice.info> <34dc6d5f-99f7-7826-95f3-1e5965ddfb87@DancesWithMice.info> Message-ID: <30b00a08-143e-b3cd-3166-35d34c8f5fa9@DancesWithMice.info> On 01/12/2021 13.33, Alan Gauld via Tutor wrote: > On 30/11/2021 23:19, dn via Tutor wrote: >> If so, there are plenty of "linters" (as others mentioned >> earlier) and other learning-tools. I'm keen on Sourcery (British, hence >> the spelling) > > huh? > > We spell sorcery like so. (pronounced like horse-ery) > We spell source code like so. (pronounced as s-oh-rce) > > I'm not sure where British spelling comes in? > > Confused That makes two of us! Blame my trans-Atlantic education (if indeed it achieved anything) in that I'm often aware of multiple spellings, but too confused to remember which is which. Apparently a play-on-words: source and sorcery - just like a Terry Pratchett book IIRC. Apologies to all anglophones, and if you wouldn't mind, ever so very, please pass the HP sauce... -- Regards, =dn From alexkleider at gmail.com Tue Nov 30 20:49:34 2021 From: alexkleider at gmail.com (Alex Kleider) Date: Tue, 30 Nov 2021 17:49:34 -0800 Subject: [Tutor] pylint(too-many-nested-blocks) In-Reply-To: References: <2c34791d-dc21-4070-82d2-1c98d67616ce@gmail.com> <703ee90c-2d81-5ea3-91aa-7b2a399e4531@DancesWithMice.info> Message-ID: This might be of interest (re cyclomatic complexity) to you. https://audiolion.github.io/python/2016/10/17/reducing-cyclomatic-complexity.html On Tue, Nov 30, 2021 at 1:59 PM Phil wrote: > Thank you dn for taking the time to write a lengthy informative reply > and thank you once again Cameron and Mats. I will endeavor to make good > use of the advice given. > > Does Python itself see this as an error and stop working, or is it only > > the code-checker s/w? > > It's not so much an error message, rather a recommendation and one that > I feel that I should act on. > > > Were we conducting a Code Review, this function would definitely attract > > criticism. You have already recognised the complexity, but seem somewhat > > satisfied - on the grounds that the whole works. > > I think I've only ever produced one application that was truly useful. > My current project doesn't do anything that others cannot, it's just a > my-way project and so I could leave it as it is or try to make it look > more a work of art. > > > > Here's where things become rather subjective. You (no criticism) seem to > > have a mathematical view of programming, > > Isn't computer programming a pursuit based on maths? Even so, I suppose > the result doesn't need to look like a dog's breakfast. > > > > I have been criticised (but exhibit little remorse) for breaking things > > down into "smaller" units of code than others feel necessary. > > I can see where I could split the function that I posted into two and > one of those parts could be reused in a yet-to-be-written function. I do > try to reuse code where I can. I'll need to give a lot more thought to > how I might reduce the complexity. > > > > Now is a good time pick-up the testing tool's feedback: "understandable > > and maintainable". It's not merely a matter of > > "readability"/"understandability", but also maintainability - and that > > of the objectives of the tool: "testability". > > I know that testing tools exist, however, I've always considered their > use to be beyond the understanding of the neophyte especially this > amateur programmer. > > > > Earlier the idea of naming a chunk of code was mentioned. > > I find it difficult to come up with meaningful names for functions and > even variables. I know that 'x' or 'y' is often a poor choice and I > often leave them in place until I can think of something better. Often I > don't and so they stay, and of course, I forget what they mean the next > time I look at the code again. > > > > (I'll leave you to research the topics-mentioned that may interest you, > > as you see fit...) > > I haven't spent anytime on the laptop or the Internet for the fast few > days and so I haven't had a chance to study the topic of cylomatic > complexity, however, I do have a Wikipedia page open and I will try to > gain some understanding of it after I post this e-mail. > > -- > > Regards, > Phil > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From cs at cskk.id.au Tue Nov 30 21:19:22 2021 From: cs at cskk.id.au (Cameron Simpson) Date: Wed, 1 Dec 2021 13:19:22 +1100 Subject: [Tutor] pylint(too-many-nested-blocks) In-Reply-To: References: Message-ID: On 01Dec2021 08:57, Phil wrote: >>Now is a good time pick-up the testing tool's feedback: >>"understandable >>and maintainable". It's not merely a matter of >>"readability"/"understandability", but also maintainability - and that >>of the objectives of the tool: "testability". > >I know that testing tools exist, however, I've always considered their >use to be beyond the understanding of the neophyte especially this >amateur programmer. Doctest is pretty simple, though it lends itself mostly to pretty simple tests. But its so handy for some things that I try to use it as examples in the documentation. Here's an example from my own personal kit. The function's got quite a lot of if/else branches and is slightly hard to inspect for correctness. But a doctest lets me at least run some simple examples I'm actually expecting to use and verify that they get correct output. That said, here's the function. The docstring is in MarkDown format and get written to the module documentation. The doctests are the ">>> " prefixed stuff, as in the Python interactive prompt. # pylint: disable=redefined-outer-name def strip_prefix_n(s, prefix, n=None): ''' Strip a leading `prefix` and numeric value `n` from the start of a string. Return the remaining string, or the original string if the prefix or numeric value do not match. Parameters: * `s`: the string to strip * `prefix`: the prefix string which must appear at the start of `s` * `n`: optional integer value; if omitted any value will be accepted, otherwise the numeric part must match `n` Examples: >>> strip_prefix_n('s03e01--', 's', 3) 'e01--' >>> strip_prefix_n('s03e01--', 's', 4) 's03e01--' >>> strip_prefix_n('s03e01--', 's') 'e01--' ''' s0 = s if prefix: s = cutprefix(s, prefix) if s is s0: # no match, return unchanged return s0 else: s = s0 if not s or not s[0].isdigit(): # no following digits, return unchanged return s0 if n is None: # strip all following digits s = s.lstrip(digits) else: # evaluate the numeric part s = s.lstrip('0') # pylint: disable=no-member if not s or not s[0].isdigit(): # all zeroes, leading value is 0 sn = 0 pos = 0 else: pos = 1 slen = len(s) while pos < slen and s[pos].isdigit(): pos += 1 sn = int(s[:pos]) if sn != n: # wrong numeric value return s0 s = s[pos:] return s So those lines are literally cut/paste from exercises in the interactive prompt. To run the tests: python3 -m doctest cs/lex.py which runs all the doctests in the file. "doctest is part of the standard library. This basic level is great for simple assertions which verify basic operation of your functions, particularly the cases which mattered to you when you wrote them, and various simple corner cases. There are more elaborate things, and I try to use them, but doctest is a great first start, as so easy! >>Earlier the idea of naming a chunk of code was mentioned. > >I find it difficult to come up with meaningful names for functions and >even variables. I know that 'x' or 'y' is often a poor choice and I >often leave them in place until I can think of something better. Often >I don't and so they stay, and of course, I forget what they mean the >next time I look at the code again. Just regular nouns like "word", "vertex" etc are good basics. But don't eschew the concise when it matters: for i, vertex in vertices: print("index", i, "has vertex", vertex) Nothing wrong with "i" there! Cheers, Cameron Simpson