Timeline for Automate the Boring Stuff CH 7: password strength test
Current License: CC BY-SA 4.0
23 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Mar 28, 2022 at 14:43 | comment | added | FMc | @alexyorke Tastes vary, I suppose, but I always r-quote regexes to minimize thinking about that particular topic. Looks like the other posted answer follows a similar habit, as do many other Python coders. | |
Mar 28, 2022 at 5:44 | comment | added | alexyorke |
Since none of the regexes contain any characters that need escaping, the r can be removed from the string prefix.
|
|
Mar 27, 2022 at 18:57 | comment | added | FMc | @Reinderien Not convinced that the dict-vs-tuples question matters in this case. But I agree in principle with your other points, especially the simplifications regarding the regexes, which have the added benefit of reinforcing the regex-learning goal of the exercise. Edited the answer to incorporate your good idea and also to implement the earlier suggestion to return all errors rather than just the first. | |
Mar 27, 2022 at 18:54 | history | edited | FMc | CC BY-SA 4.0 |
deleted 8 characters in body
|
Mar 27, 2022 at 18:47 | history | edited | FMc | CC BY-SA 4.0 |
deleted 52 characters in body
|
Mar 27, 2022 at 18:09 | comment | added | Reinderien |
A more uniform implementation of your strategy would be for the second element of each tuple to be a regular expression only, the first being .{8,} . Then your bool cast and search call would be written once and not four times.
|
|
Mar 27, 2022 at 18:07 | comment | added | Reinderien |
I agree that a dict is not appropriate here. It's not about cost; it's about intent: you're not doing any lookups. A sequence of tuples fits the purpose better. Also: don't findall ; just search - you don't care about "all matches"; you care about "at least one match". Finally: returning presentation strings from a business logic function is not a good idea.
|
|
Mar 27, 2022 at 17:29 | comment | added | FMc |
(1) Casting to bool is fine: it plays a minor documentation role and it simplifies type declarations, for those who want to go that route. (2) Initializing a function's return value is also fine: in some circumstances, it's a useful strategy, even though in this case it's not needed/helpful. (3) Making checks a dict is fine as well: in the current case, a list of tuples provides no relative benefits and a dict brings no relative costs ... and it is marginally less hassle to define a dict than a list-of-tuples.
|
|
Mar 27, 2022 at 17:06 | comment | added | Ramza | @Lodinn Could you clarify, what would a better approach besides initializing a return value with a default value? | |
Mar 27, 2022 at 16:19 | comment | added | Ben Millwood |
minor remark on this answer: there's not really any need to make checks a dictionary, since you never want to search it by key. You could just make it a list of tuples, which IMO reflects better how it is used.
|
|
Mar 27, 2022 at 8:48 | comment | added | Lodinn | Somewhat related side note: initializing the return value with some default may be fine in C, but is incredibly non-pythonic. It also introduces a lot of opportunities for bugs to appear and hide in the flow control not being rigorous enough. | |
Mar 27, 2022 at 4:46 | comment | added | alexyorke |
The casts to bool are redundant as the function output is being used in an if statement, which can help shorten this code a bit more.
|
|
Mar 27, 2022 at 3:39 | comment | added | FMc | @ShapeOfMatter Yes, that's a good improvement for the OP to add. | |
Mar 27, 2022 at 3:10 | comment | added | ShapeOfMatter |
For actual use, I would strongly recommend return [err for (err, ok) in checks.items() if not ok] . The point being not the use of a "comprehension" (which are good!), but rather that reporting every error detected is a better UX.
|
|
Mar 26, 2022 at 20:08 | comment | added | Ramza | Thank you for all of the useful information. My mind is still blown by the object you wrote earlier, and I've been thinking about it all morning. It makes perfect sense, though. Why would you store error messages in variables when you can set the strings as keys and the conditionals as values? It would be a nightmare to try tracking down a bunch of variables that are spread out throughout the entirety of the code, along with the conditionals that relate to them. | |
Mar 26, 2022 at 18:20 | comment | added | FMc | @Ramza And here's an answer that provides another concrete example: it's a simple problem where we convert algorithmic complexity into simplicity by defining useful data structures. Once you start thinking in these terms, you'll see opportunities for it everywhere – or, as Rob Pike says, "data dominates". | |
Mar 26, 2022 at 16:37 | comment | added | FMc | @Ramza I don't have a ready-made reading list that would necessarily be helpful for someone just starting out. However, this answer has some relevant discussion in the comments, including some practical advice that you could follow immediately and some suggestions for a few topics you could research on the internet to get at least a preliminary sense of what data-centric coding means and why it's so useful. | |
Mar 26, 2022 at 16:13 | vote | accept | Ramza | ||
Mar 26, 2022 at 16:13 | comment | added | Ramza | And as for the second part of your response, I understand that the idea is focused on "data-centric thinking," as you called it. I will need more time to read and understand this part of your response, but do you recommend any resources, books, or texts that focus on "data-centric" programming? | |
Mar 26, 2022 at 16:08 | history | edited | FMc | CC BY-SA 4.0 |
added 2 characters in body
|
Mar 26, 2022 at 16:06 | comment | added | FMc |
@Ramza That's correct. Python can do a variety of boolean operations in the typical ways using and , or , and not .
|
|
Mar 26, 2022 at 16:01 | comment | added | Ramza |
Thank you for the response. There's lots to unpack here. Am I to understand that your recommended return statement evaluates to a single Boolean value? return lower_groups and upper_groups and num_groups and len(pw) >= 8 . So in other words, if any one of those statements resolves to False, then the return Boolean is False. But if all are True, then my final return Boolean is True?
|
|
Mar 26, 2022 at 15:33 | history | answered | FMc | CC BY-SA 4.0 |