Trey Hunner 🐍

@treyhunner
2.5K Followers
310 Following
4.4K Posts

#Python & #Django educator & team trainer

I help folks sharpen their Python skills with https://PythonMorsels.com🐍πŸͺ

#pythonoddity

Also: ostrovegan, sentientist, YIMBY, and I think economics is highly underrated. I don't post about any of those topics very often.

he/him

πŸ’Œ My Weekly Newsletterhttps://pym.dev/newsletter
🐍 Python Exerciseshttps://www.pythonmorsels.com
πŸ“Ί YouTubehttps://www.youtube.com/@PythonMorsels
πŸ•Έ Personal Bloghttps://treyhunner.com

Don't use regular expressions for very simple operations.

If the "in" operator or a string method will do what you need in an easier to understand fashion, just use those!

Seem like I'm contradicting tip 89? Check it again. I find those methods less understandable than regular expressions.

https://pym.dev/string-methods/

🧡 (2/2)

Python's String Methods

Python's strings have dozens of methods, but some are much more useful than others. Let's discuss the dozen-ish must-know string methods and why the other methods aren't so essential.

Python Tip #92 (of 365):

Don't overuse regular expressions

If a containment check of a common string method will do, don't use a regular expression.

Instead of this:
if re‍.search(r"^http://", string):

Do this:
if string.startswith("http://"):

Instead of this:
if re‍.search(r"#", string):

Do this:
if "#" in string:

And instead of this:
parts = re.findall(r"\S+", string)

Do this:
parts = string.split()

🧡 (1/2)

#Python #DailyPythonTip

β€œThere is one feature of self-documenting expressions that I really appreciate: you can put spaces around the equal sign to make the result a little bit more readable.”

Read more πŸ‘‰ https://pym.dev/debugging-with-f-strings/

#Python

Debugging with f-strings

If you're debugging Python code with print calls, consider using f-strings with self-documenting expressions to make your debugging a little bit easier.

β€œIterating over a dictionary object in Python will give you keys, no matter what Python feature you're using to do that iteration.”

Read more πŸ‘‰ https://pym.dev/all-iteration-is-the-same/

#Python

All iteration is the same in Python

In Python, for loops, list comprehensions, tuple unpacking, and * unpacking all use the same iteration mechanism.

Python programmers tend to underuse verbose mode when writing regular expressions.

If you're writing a regex that's more complex than \d+, consider using re.VERBOSE.

Your future self (and your teammates) will thank you.

🧡(4/4)

Regular expressions are a programming language without comments or whitespace.

That's why I recommend using re.VERBOSE for nearly every regex you write.

But... what if we need to match actual whitespace or a "#" character?

2 options:
1. Escape with a backslash
2. Use a character class: [ ] or [#]

🧡(3/4)

Verbose mode tells Python to ignore whitespace in the regex, so you can add whitespace for readability.

But it gets even better... with re.VERBOSE, you can add comments!

```
match = re.‍search(r"""
^ # beginning of the string
\d{5} # 5 digits
( - \d{4} )? # optionally, - followed by 4 digits
$ # end of the string
""", zip_code, flags=re.VERBOSE)
```

🧡(2/4)

Python Tip #91 (of 365):

Use re.VERBOSE to make your regular expressions more readable

Regexes are extremely dense: one character is often a whole expression, and they're usually written without whitespace or comments:

match = re.‍search(r"^\d{5}(-\d{4})?$", zip_code)

But they don't have to be so dense!

You can use the re.VERBOSE flag to space them out:

match = re.‍search(r"""
^
\d{5}
( - \d{4} )?
$
""", zip_code, flags=re.VERBOSE)

#Python #DailyPythonTip

🧡(1/4)

RE: https://indieweb.social/@keithamus/116328410312008711

I did better than I expected on this... especially given how infrequently I find myself hand crafting CSS anymore.

I got 51/80.

https://www.keithcirkel.co.uk/css-or-bs/?r=MxTf_PA

#CSSorBS

We could make this a little bit more readable by splitting our code up over multiple lines (thanks to implicit line continuations), with each line starting with a Boolean operator

Read more πŸ‘‰ https://trey.io/f98u5c

#Python

Refactoring long boolean expressions

You can improve the readability of long boolean expressions by splitting your code, naming sub-expressions, or rewriting your Boolean logic.