Skip to content

Instantly share code, notes, and snippets.

View csixteen's full-sized avatar
📚
Use the force, read the source.

Pedro R. csixteen

📚
Use the force, read the source.
View GitHub Profile
@csixteen
csixteen / bobp-python.md
Created October 7, 2017 18:52 — forked from sloria/bobp-python.md
A "Best of the Best Practices" (BOBP) guide to developing in Python.

The Best of the Best Practices (BOBP) Guide for Python

A "Best of the Best Practices" (BOBP) guide to developing in Python.

In General

Values

  • "Build tools for others that you want to be built for you." - Kenneth Reitz
  • "Simplicity is alway better than functionality." - Pieter Hintjens
@csixteen
csixteen / remove_all_images.sh
Created January 26, 2017 14:19
Single line command for removing all Docker images
docker images | awk '/([\w\.\-]+)/ { print $3 }' | xargs docker rmi
@csixteen
csixteen / frequency.sh
Created December 13, 2016 19:09
Getting word frequency in a file using cat, pipes, tr, sort, uniq and awk
cat words.txt | tr -s ' ' '\n' | sort | uniq -c | awk '{print $2" "$1}'
@csixteen
csixteen / name_slices.py
Created November 25, 2016 18:30
Naming slices for the sake of readability
# 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")
@csixteen
csixteen / dedupe.py
Last active November 25, 2016 18:14
Remove duplicates from a sequence using generators
# 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)
@csixteen
csixteen / unpacking.py
Last active November 24, 2016 14:01
Unpacking arbitrary number of elements from sequences
# 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