Stephen Bunn

@stephen-bunn

May contain Pythonic ranting

1,057 words

http://stephen.bunn.io/ @stephenbunnio Thank
You'll only receive email when Stephen Bunn publishes a new post

PEP498 - Literal String Interpolation

I hate myself for not knowing that this PEP has existing for Python 3.6.
While checking out task processing frameworks (mainly dramatiq), I found an example using f-strings and my mouth dropped to the floor.


Previously I was doing a lot of my string formatting using the **locals() trick, which is considered a bad practice.
This trick allows me to auto-format a format string with local variables without too much hassle.

(name, value,) = ('favorite number', 37,)
print('{name} is {value}'.format(**locals()))
# favorite number is 37

This worked fine for basic quick variable substitution, but was inefficient, featureless, and most importantly not-elegant.
Because I'm passing so many different variables (however are locally defined) to the format method, there is the feeling that a lot of unneeded effort is put on the back of Python's runtime.
Mainly, that feeling is the reason why f-strings were introduced to Python 3.61.

This new feature allows you to essentially do the following:

(name, value,) = ('favorite number', 37,)
print(f'{name} is {value}')
# favorite number is 37

And not only can you do this, but you can also use several format specifiers to quickly convert variables using builtin methods!
These are the current allowed sepcifiers:

Specifier Effect
variable!s str(varaible)
varaible!r repr(variable)
variable!a ascii(varaible

There are so many ridiculously useful features of this PEP that now I'm spending my days replacing many of my str.format instances with uses of f-strings.
This link has been really helpful in that process.

Some of these features include:

  • Math evaluation
  • Date formatting
  • Function calls

So basically, if your using Python 3.6 exclusively for a project, I would recommend trying out f-strings rather than using the default str.format.
It's so much more elegant.

1: Briefer string format: https://mail.python.org/pipermail/python-ideas/2015-July/034671.html

Learning Resources

Over the last couple years I've come to continually recommend several resources and online utlities to fellow developers trying to learn something new. A lot of people I work and hang out with ask me for resources on how to quickstart with a new language (typically Python) or a framework.

So, here is a quick listing of some things I've mentally bookmarked as useful.


Python

Because I sorta specialize in Python development, I tried to pick some actually useful resources to mid-level developers who already know the basics (what a string is, etc...).

Python Patterns – A collection of Python design patterns

If you have any respect or background in software design principles, this repository is something you should definitely bookmark, star, whatever... This repository is broken up into sections such as behavioral, creational, structural, etc. which examples of real design patterns that every developer should understand.

Python201 – A short book on using Python's basic modules

A good book to read if you just leared the basics of programming using Python (hence the name). Covers some of the more useful modules and how to utilize them.

The Hitchhiker’s Guide to Python – The best resource for real Python development

A large collection of small examples on how to do many important things when developing in Python. This includes descriptions and examples of major external Python modules.

Awesome Python – A curated list of awesome Python projects

If you want some encouragement or inspiration when developing in Python, I always find it invigorating to go through some of the things considered to be "awesome". This repository has a categorized list of some really cool projects that might inspire you on what to create or what technologies to use in a current project.


Regex

No matter what your trying to do, if your a developer, you should know regular expressions inside and out. If you don't yet have that much experience with regular expressions, you can use some of these tools for prototyping and learning how regular expressions work in various languages.

Regex101 – Online regex prototyping tool

This is by far my favorite regex prototyping tool.
Whenever I need to create a regular expression for some reason, I immediately open up this site, drop in some data, and quickly throw together a regex that works.

This site supports multiple language variations and is very responsive and informative in comparison to its competitors.
Another nice thing is the collection of community published regexes that are also available on this site. This is super useful for both learning how regular expressions work and finding a good one for a use case (such as ip addresses, email addresses, etc...).

Regex101

Regexr – Another online regex prototyping tool

A similar site to Regex101.
Choosing between the two is mostly just personal preference.

Regexr

Learn Regex the Easy Way – The quicker way to get familiar with Regex

This collection of README documents is a great way to learn the basic syntax of regular expressions. The author shows how regular expressions capture strings and visualizes how things such as greedy vs. lazy matching occurs.


Angular

Out of the web frameworks I typically go for, Angular is my favorite.
Django, Flask, React, Vue.js definitely have their place, but for real intensive and responsive web applications the easiest way to create a well-structured web application is to use Angular.

Become a ninja with Angular – A tutorial (start to finish) on the basics of complete Angular development

For people trying to get started with Angular 2+, I can not recommend this book enough.
The author does an amazing job introducing all the reasons why angular was created and how it works from scratch.
Definitely a worthwhile purchase.

ng-book – Considered to be the best book on Angular

Another really good book on learning Angular from scratch.
If you need a complete overview of Angular that is continually updated as Angular proceeds through its development cycle, you should get this book.


Miscellaneous

LearnXinYminutes – The quickest way to learn a new language's syntax

Repl.it – A feature rich online REPL for many languages

CodeWars – An alternative to Codecademy (focused on challenges)

I'm not a fan of sites like Codecademy at all...
They are good for nothing but introducing a language's syntax (and there are much better resources for that).

For people who want to understand what programming in the real world is like, Codecademy is not it. Try Codewars instead.