Skip to content

Instantly share code, notes, and snippets.

@ivermac
Last active May 5, 2020 04:43
Show Gist options
  • Save ivermac/e3ed7ec104bf32ad4c3448ae2c658d26 to your computer and use it in GitHub Desktop.
Save ivermac/e3ed7ec104bf32ad4c3448ae2c658d26 to your computer and use it in GitHub Desktop.
My python tips and tricks

To generate random values for secret keys

import os
os.urandom(24).encode('hex')

To view hidden attribute of an object

object._className__attrName

String formatting

owner = "ekisa"
qyear = "2009"
car = "peugot"
print "{owner} has a {year} {car}.".format(**locals())

Creating a single string using a list

row_list = ["john", "dow"]
"%s %s" % tuple(row_list)

# or
" ".join(row_list)

Multiple line string

my_string = ("This is a relatively long string "
             "that we assume you want to store "
             "for some godforsaken reason.")

Profile a script

python -m cProfile <python-script-name>

Reverse a string

a = "ivermac"
a[::-1]

PrettyPrint json file

cat <json-file> | python -m json.tools

Generator expression

g = (x ** 2 for x in xrange(10))
next(g) # call next(g) until you get `StopIteration` error

Invert dict

m = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
{v: k for k, v in m.items()}

The collections package is super useful. Check out Counter, deque, OrderedDict, defaultdict

from collections import defaultdict, Counter

colours = (
    ('Yasoob', 'Yellow'),
    ('Ali', 'Blue'),
    ('Arham', 'Green'),
    ('Ali', 'Black'),
    ('Yasoob', 'Red'),
    ('Ahmed', 'Silver'),
)
     
favourite_colours = defaultdict(list)

for name, colour in colours:
    favourite_colours[name].append(colour)

print(favourite_colours)

# output
# defaultdict(<type 'list'>,
#    {'Arham': ['Green'],
#     'Yasoob': ['Yellow', 'Red'],
#     'Ahmed': ['Silver'],
#     'Ali': ['Blue', 'Black']
# })

colours = (
    ('Yasoob', 'Yellow'),
    ('Ali', 'Blue'),
    ('Arham', 'Green'),
    ('Ali', 'Black'),
    ('Yasoob', 'Red'),
    ('Ahmed', 'Silver'),
)

favs = Counter(name for name, colour in colours)
print(favs)
# Output: Counter({
#    'Yasoob': 2,
#    'Ali': 2,
#    'Arham': 1,
#    'Ahmed': 1
# })

with open('filename', 'rb') as f:
    line_count = Counter(f)
print(line_count)

import antigravity :laughing

Generate a list of 100 random numbers whose values are between 0 and 100

import random
[random.randint(0, 100) for __ in xrange(100)]

Generate binary numbers

import itertools
[''.join(str(x) for x in p) for p in itertools.product([0, 1], repeat=4)]

Calculate factorial

import itertools
len([''.join(str(x) for x in p) for p in itertools.permutations([1, 2, 3, 4])])

More on itertools

>>> print list(itertools.product('ABCD', repeat=2))
[('A', 'A'), ('A', 'B'), ('A', 'C'), ('A', 'D'), ('B', 'A'), ('B', 'B'), ('B', 'C'), ('B', 'D'), ('C', 'A'), ('C', 'B'), ('C', 'C'), ('C', 'D'), ('D', 'A'), ('D', 'B'), ('D', 'C'), ('D', 'D')]
>>> print list(itertools.permutations('ABCD', 2))
[('A', 'B'), ('A', 'C'), ('A', 'D'), ('B', 'A'), ('B', 'C'), ('B', 'D'), ('C', 'A'), ('C', 'B'), ('C', 'D'), ('D', 'A'), ('D', 'B'), ('D', 'C')]
>>> print list(itertools.combinations('ABCD', 2))
[('A', 'B'), ('A', 'C'), ('A', 'D'), ('B', 'C'), ('B', 'D'), ('C', 'D')]
>>> print list(itertools.combinations_with_replacement('ABCD', 2))
[('A', 'A'), ('A', 'B'), ('A', 'C'), ('A', 'D'), ('B', 'B'), ('B', 'C'), ('B', 'D'), ('C', 'C'), ('C', 'D'), ('D', 'D')]
>>> print list(itertools.izip('ABCD', 'xy'))
[('A', 'x'), ('B', 'y')]

Never use mutable defaults. Use None instead.

# instead of the following
def foo(a, b, c=[]):

# use the following instead
def foo(a, b, c=None):

Start local server

# python2
python -m SimpleHTTPServer <port>


# python3
python3 -m SimpleHTTPServer <port>

Using zip function

l = [[1, 2, 3], [4, 5, 6]]
zip(*l)
# [(1, 4), (2, 5), (3, 6)]

l = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8]
zip(*[iter(l)] * 3)
# [(3, 1, 4), (1, 5, 9), (2, 6, 5), (3, 5, 8)]

Readable regular expressions

import re
pattern = """
^                # beginning of string
M{0,4}           # thousands - 0 to 4 M's
(CM|CD|D?C{0,3}) # hundreds - 900 (CM), 400 (CD), 0-300 (0 to 3 C's),
                 #            or 500-800 (D, followed by 0 to 3 C's)
(XC|XL|L?X{0,3}) # tens - 90 (XC), 40 (XL), 0-30 (0 to 3 X's),
                 #        or 50-80 (L, followed by 0 to 3 X's)
(IX|IV|V?I{0,3}) # ones - 9 (IX), 4 (IV), 0-3 (0 to 3 I's),
                 #        or 5-8 (V, followed by 0 to 3 I's)
$                # end of string
"""
re.search(pattern, ‘M’, re.VERBOSE)

Change first letters to uppercase

>>> s = "john doe"
>>> s.title()
'John Doe'

Convert csv to json

python -c "import csv,json;print json.dumps(list(csv.reader(open('csv_file.csv'))))”

One line ipdb

__import__('ipdb').set_trace()

Get first element of dict

def first(s):
    '''Return the first element from an ordered collection
       or an arbitrary element from an unordered collection.
       Raise StopIteration if the collection is empty.
    '''
    return next(iter(s))

Convert int to binary. Got this info from this stackoverflow link

# converting 4 to binary
bin(4) # ’0b100'
'{0:04b}'.format(4) # ’0010'
f'{4:04b}' # '0100' (python 3)

0 is evaluated as false

'Yes' if 0 else 'No' # results to 'No'

'Yes' if 1 else 'No' # results to 'Yes'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment