A "Best of the Best Practices" (BOBP) guide to developing in Python.
- "Build tools for others that you want to be built for you." - Kenneth Reitz
- "Simplicity is alway better than functionality." - Pieter Hintjens
docker images | awk '/([\w\.\-]+)/ { print $3 }' | xargs docker rmi |
cat words.txt | tr -s ' ' '\n' | sort | uniq -c | awk '{print $2" "$1}' |
# Instead of using hardcoded values, one can use the built-in | |
# slice() function, that returns a slice object. This object can | |
# then be used pretty much anywhere in sequences. | |
NAME = slice(2, 5) | |
records = [ | |
(1, "user", "Name1", "Middle Name1", "Surname1", 12345, "Y"), | |
(1, "user", "Name2", "Middle Name2", "Surname2", 12346, "N"), | |
(1, "user", "Name3", "Middle Name3", "Surname3", 12347, "Y"), | |
(1, "user", "Name4", "Middle Name4", "Surname4", 12348, "Y"), | |
(1, "user", "Name5", "Middle Name5", "Surname5", 12349, "N") |
# This works for hashable items in a sequence. This wouldn't work | |
# if the items are dictionaries, for example | |
# Of course, one could easily use set(), but the order wouldn't be | |
# preserved. | |
def dedupe(items): | |
seen = set() | |
for item in items: | |
if item not in seen: | |
yield item | |
seen.add(item) |
# Unpacking arbitrary number of elements | |
records = ["Name", "Email", 12345, 12346, 12347, 12348] | |
name, email, *phone_numbers = records | |
print(phone_numbers) # [12345, 12346, 12347, 12348] | |
records = ["Name", 12345, 12346, 12347, 12348, "Email"] | |
name, *phone_numbers, email = records | |
print(phone_numbers) # [12345, 12346, 12347, 12348] | |
name, *_, email = records |