RE: Single line if statement with a continue
Unless someone is counting lines of code for some purpose, like number of error found per thousand lines of code, many short one-liners strike me as more readable and especially if followed by a blank line so it is a bit obvious. Consider a similar issue in many languages that use curly braces and where they can be skipped in a one liner like "if (condition) statement" rather than the style some use on multiple lines like: If (condition) { Statement } Or even: If (condition) { Statement } Of course, once you have additional parts following like an "else" that contains multiple statements, it seems more symmetric to do both parts the same style. And in commented code, a one-liner may get long and harder to read as in If (condition) statement # long comment Note the above are not python and the absence of a colon is intentional. No one language is being discussed and some have their own vagaries and variants such as not knowing if your code is done if you change lines in ambiguous situations. And note some languages support methods like the ?: operator or the inline if/else python allows as in this: min = 5 low = 12 x = low if low >= 12 else min x 12 low = 3 x = low if low >= 12 else min result 12 Clearly that is a one-liner that almost has to be a one liner as there is no obvious easy way to wrap it into multiple lines. I mean the following works, albeit darned if I know if any of it should be indented or who wants to read it this way: x = low \ if \ low >= 12 \ else \ min x 5 As many have discussed, it is a matter of taste and people should be flexible enough to program in whatever style others want to see when that applies such as working in a group project or fixing someone else's code. -Original Message- From: Python-list On Behalf Of dn Sent: Thursday, December 15, 2022 12:24 AM To: python-list@python.org Subject: Re: Single line if statement with a continue On 15/12/2022 07.53, Aaron P wrote: > I occasionally run across something like: > > for idx, thing in enumerate(things): > if idx == 103: > continue > do_something_with(thing) > > It seems more succinct and cleaner to use: > > if idx == 103: continue. > > Of course this would be considered an anti-pattern, and Flake8 will complain. > > Any opinions, or feedback on the matter. These aged-eyes prefer the second line and indentation. However, another alternative (given simplicity of example): for ... if idx != 103: do_something ... Which could, in-turn, be boiled-down to a 'one-liner'. -- Regards, =dn -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: Single line if statement with a continue
On Thu, 15 Dec 2022 at 16:29, Thomas Passin wrote: > > PEP-8, which is Guido's style guide and generally good to follow, does > not completely discourage single-line usage like the example. It's not > clear to me how Chris's example fits into the guidelines. > > PEP-8: > "While sometimes it’s okay to put an if/for/while with a small body on > the same line, never do this for multi-clause statements. > ... > # Wrong: > if foo == 'blah': do_blah_thing() > for x in lst: total += x > while t < 10: t = delay() > " > > If the one-liner were not in a multi-statement block, it would be all > right with PEP-8. Not sure what your point is about it being "in" a multi-statement block - PEP 8 has nothing to say about that. What it's saying is that you shouldn't do this: if foo == 'blah': one(); two(); three() And I agree; if you're putting more than one statement after your 'if', it's generally clearest to have it on multiple lines. But a simple "continue" or "break" statement works just fine on the same line as the if. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Single line if statement with a continue
PEP-8, which is Guido's style guide and generally good to follow, does not completely discourage single-line usage like the example. It's not clear to me how Chris's example fits into the guidelines. PEP-8: "While sometimes it’s okay to put an if/for/while with a small body on the same line, never do this for multi-clause statements. ... # Wrong: if foo == 'blah': do_blah_thing() for x in lst: total += x while t < 10: t = delay() " If the one-liner were not in a multi-statement block, it would be all right with PEP-8. OTOH, there is nothing that says one has to fully comply with PEP-8. I personally tend to use if test: return even inside larger blocks. If one is working with other someone else's project and there is a style guide, it's important to follow that guide because the other people involved will find it easier to read and understand your code. If you are working on your own project, PEP-8 is always a good starting point, and flake8 and pylint will be happier. That's worth something. On 12/14/2022 11:35 PM, Chris Angelico wrote: On Thu, 15 Dec 2022 at 14:41, Aaron P wrote: I occasionally run across something like: for idx, thing in enumerate(things): if idx == 103: continue do_something_with(thing) It seems more succinct and cleaner to use: if idx == 103: continue. Of course this would be considered an anti-pattern, and Flake8 will complain. Any opinions, or feedback on the matter. Nothing at all wrong with writing that on a single line. If you have issues with Flake8 not accepting your choices, reconfigure Flake8 :) ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Single line if statement with a continue
On 15/12/2022 07.53, Aaron P wrote: I occasionally run across something like: for idx, thing in enumerate(things): if idx == 103: continue do_something_with(thing) It seems more succinct and cleaner to use: if idx == 103: continue. Of course this would be considered an anti-pattern, and Flake8 will complain. Any opinions, or feedback on the matter. These aged-eyes prefer the second line and indentation. However, another alternative (given simplicity of example): for ... if idx != 103: do_something ... Which could, in-turn, be boiled-down to a 'one-liner'. -- Regards, =dn -- https://mail.python.org/mailman/listinfo/python-list
Re: Single line if statement with a continue
On Thu, 15 Dec 2022 at 14:41, Aaron P wrote: > > I occasionally run across something like: > > for idx, thing in enumerate(things): > if idx == 103: > continue > do_something_with(thing) > > It seems more succinct and cleaner to use: > > if idx == 103: continue. > > Of course this would be considered an anti-pattern, and Flake8 will complain. > > Any opinions, or feedback on the matter. Nothing at all wrong with writing that on a single line. If you have issues with Flake8 not accepting your choices, reconfigure Flake8 :) ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Keeping a list of records with named fields that can be updated
Dictionaries and sets are your friends here. On 12/14/2022 1:50 PM, songbird wrote: I'm relatively new to python but not new to programming in general. The program domain is accounting and keeping track of stock trades and other related information (dates, cash accounts, interest, dividends, transfers of funds, etc.) Assume that all data is CSV format. There are multiple files. Assume there is a coherent starting point and that all data is in order. Assume each line contains a description. The description determines what the line is. The number of fields in the line does not change within the data file but it may happen that later lines in other files may be different other than the fact that they all must contain a description. All descriptions are deterministic (none are recursive or referencing things from the future). All things referenced in the description which do not already exist are added to a list (or perhaps more than one in a few cases) and may contain some basic information (the date, how many and for how much, or a total amount or a fee or ...) If the field of the line isn't a number it is either a symbol or a description. A default action is simply to keep most parts of the line and to adjust any totals of a previously seen description that matches by whatever amounts are on the line. The key is the description. I've already written one program based upon the files I already have which works but what happens is that new descriptions are added (new accounts, new stocks, etc.) and I don't want to have to write new code manually every time a description changes. I started using named tuples (it works for reading in the files and accessing the fields) but I cannot update those so I need to use something else to give me the list of unique descriptions and fields that I need to update. I've not gotten beyond that yet as I'm still learning. Suggestions? Thanks! :) songbird -- https://mail.python.org/mailman/listinfo/python-list
Re: How to get the needed version of a dependency
DFS writes: > On 12/14/2022 3:55 AM, Cecil Westerhof wrote: >> If I want to know the dependencies for requests I use: >> pip show requests >> And one of the lines I get is: >> Requires: certifi, charset-normalizer, idna, urllib3 >> But I want (in this case) to know with version of charset-normalizer >> requests needs. >> How do I get that? > > Check the METADATA file in the *dist-info package files usually found in > Lib\site-packages. > > ie \Python\3.11.0\Lib\site-packages\pandas-1.5.2.dist-info > > Look for config lines beginning with 'Requires': > > Requires-Python: >=3.8 > Requires-Dist: python-dateutil (>=2.8.1) > > $ pip list will show you which version of the package you have > installed, so you can search for the matching .dist-info file Works. In /usr/local/lib/python3.9/dist-packages/requests-2.28.1.dist-info/METADATA I see: Requires-Dist: charset-normalizer (<3,>=2) That already keeps charset-normalizer two months from being updated. Maybe I should contact Kenneth Reitz. -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof -- https://mail.python.org/mailman/listinfo/python-list
Single line if statement with a continue
I occasionally run across something like: for idx, thing in enumerate(things): if idx == 103: continue do_something_with(thing) It seems more succinct and cleaner to use: if idx == 103: continue. Of course this would be considered an anti-pattern, and Flake8 will complain. Any opinions, or feedback on the matter. -- https://mail.python.org/mailman/listinfo/python-list
Keeping a list of records with named fields that can be updated
I'm relatively new to python but not new to programming in general. The program domain is accounting and keeping track of stock trades and other related information (dates, cash accounts, interest, dividends, transfers of funds, etc.) Assume that all data is CSV format. There are multiple files. Assume there is a coherent starting point and that all data is in order. Assume each line contains a description. The description determines what the line is. The number of fields in the line does not change within the data file but it may happen that later lines in other files may be different other than the fact that they all must contain a description. All descriptions are deterministic (none are recursive or referencing things from the future). All things referenced in the description which do not already exist are added to a list (or perhaps more than one in a few cases) and may contain some basic information (the date, how many and for how much, or a total amount or a fee or ...) If the field of the line isn't a number it is either a symbol or a description. A default action is simply to keep most parts of the line and to adjust any totals of a previously seen description that matches by whatever amounts are on the line. The key is the description. I've already written one program based upon the files I already have which works but what happens is that new descriptions are added (new accounts, new stocks, etc.) and I don't want to have to write new code manually every time a description changes. I started using named tuples (it works for reading in the files and accessing the fields) but I cannot update those so I need to use something else to give me the list of unique descriptions and fields that I need to update. I've not gotten beyond that yet as I'm still learning. Suggestions? Thanks! :) songbird -- https://mail.python.org/mailman/listinfo/python-list
Re: Subtracting dates to get hours and minutes
On 12/14/2022 12:55 AM, Gronicus@SGA.Ninja wrote: I realized it had something to do with tupilation The simple fix is to add the * into the original code. Startt = datetime.datetime(*Startt) I am not sure what "dts1 == Startt # True" does It demonstrates that the version with the "*" gives the same result as the first expression. That line is not needed by any code, it's just there to show you that the proposed expression gives the desired result. Thank you. -Original Message- From: Python-list On Behalf Of Thomas Passin Sent: Tuesday, December 13, 2022 11:20 PM To: python-list@python.org Subject: Re: Subtracting dates to get hours and minutes Your problem is that datetime.datetime does not accept a tuple as an argument. It expects an integer value for the first argument, but you supplied a tuple. In Python, you can use a sequence (e.g., tuple or list) the way you want by prefixing it with an asterisk. This causes the sequence of items to be treated as individual arguments. So: Startt = datetime.datetime(2022, 12, 13, 5, 3, 30) st1 = (2022, 12, 13, 5, 3, 30) dts1 = datetime.datetime(*st1) # NOT datetime.datetime(st1) dts1 == Startt # True On 12/13/2022 10:43 PM, Gronicus@SGA.Ninja wrote: As is, Test A works. Comment out Test A and uncomment Test B it fails. In Test B, I move the data into a variable resulting with the report: "TypeError: an integer is required (got type tuple) How do I fix this? #- import datetime #= # Test A Hard coded Date/Time Startt = datetime.datetime(2022, 12, 13, 5, 3, 30) Stopp = datetime.datetime(2022, 12, 12, 21, 15, 30) # = # Test B Date/Time data as a variable #Startt = (2022, 12, 13, 5, 3, 30) #Stopp = (2022, 12, 12, 21, 15, 30) #Startt = datetime.datetime(Startt) #Stopp = datetime.datetime(Stopp) # = c = Startt - Stopp minutes = c.total_seconds() / 60 minutes = c.seconds / 60 hours = 0 while (minutes > 59): minutes = minutes - 60 hours += 1 minutes = round(minutes) print() print (" Hours = <" + str(hours) + ">") print (" Minutes = <" + str(minutes) + ">") # -- --- -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: How to get the needed version of a dependency
On 12/14/2022 3:55 AM, Cecil Westerhof wrote: If I want to know the dependencies for requests I use: pip show requests And one of the lines I get is: Requires: certifi, charset-normalizer, idna, urllib3 But I want (in this case) to know with version of charset-normalizer requests needs. How do I get that? Check the METADATA file in the *dist-info package files usually found in Lib\site-packages. ie \Python\3.11.0\Lib\site-packages\pandas-1.5.2.dist-info Look for config lines beginning with 'Requires': Requires-Python: >=3.8 Requires-Dist: python-dateutil (>=2.8.1) $ pip list will show you which version of the package you have installed, so you can search for the matching .dist-info file -- https://mail.python.org/mailman/listinfo/python-list
How to get the needed version of a dependency
If I want to know the dependencies for requests I use: pip show requests And one of the lines I get is: Requires: certifi, charset-normalizer, idna, urllib3 But I want (in this case) to know with version of charset-normalizer requests needs. How do I get that? -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof -- https://mail.python.org/mailman/listinfo/python-list