Can anyone help make this code shorter? | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
0

Can anyone help make this code shorter?

Its a password validator in regex &python it works but it quite long i would love any ideas to shorten the lines of code https://code.sololearn.com/ct8i16N6FwQx/?ref=app

5th Jun 2022, 6:17 AM
bedah
bedah - avatar
4 ответов
+ 3
Hi! For now you don’t need any lowercase letters in the password, but you must have uppercase letters. Is that how you want it to work? I don’t think the code is extremly long. I think it better to focus on readability according to PEP8. That will give more than what a shorter code will do. For example: write: pat4 = r”[a-z]” re.search(pattern, pas) instead of: pat4=r”[a-z]” re.search(pattern,pas) It is not a (good) way to do the code shorter through writing “else:” and “print('invalid')” at the same line, like this: if a > b: do_this() else: print('invalid') It’s is a good (and correct) way to use a line for it: if a > b: do_this() else: print('invalid')
5th Jun 2022, 7:00 AM
Per Bratthammar
Per Bratthammar - avatar
+ 5
Advice: Don't. Don't make it shorter, rather, make it longer. You have several reasons to reject a password. These should be differentiated. Yes, you can make this shorter, with a single line regex. But it will be hard to read, hard to maintain, hard to understand, hard to change, and error prone. I personally would even make it a pipe-and-filter pattern with several filters attachable to the stream. Each well tested, each concerned with one aspect, each easy to understand, easy to change, easy to adjust by adding or removing filters. Much better. Incidentally: Thank you for reading if you have come to this point :) Also, your password check is faulty, as it does not verify that a lowercase character is a part of the password. For example SOLOLEARN#18 will pass, but your task requirements specify "uppercase & lowecase (sic!) char".
5th Jun 2022, 7:03 AM
Ani Jona 🕊
Ani Jona 🕊 - avatar
+ 2
# last lines if re.search(r"[^A-Z0-9a-z]", pas): print(f'{pas} is valid') else: print('invalid')
5th Jun 2022, 6:39 AM
SoloProg
SoloProg - avatar
+ 2
Wow great help guys thank you
5th Jun 2022, 7:30 AM
bedah
bedah - avatar