Skip to content

Instantly share code, notes, and snippets.

Avatar
🍕
Seven days without pizza makes one weak

Rajiv Bakulesh Shah brainix

🍕
Seven days without pizza makes one weak
View GitHub Profile
@brainix
brainix / flatten.py
Last active Feb 9, 2021
Flatten dict tech screen
View flatten.py
import collections
def flatten(dict_, *, separator='.', raise_on_collision=True):
flattened = {}
for key, value in dict_.items():
if isinstance(value, collections.abc.Mapping):
tmp_dict = flatten(value)
for tmp_key, tmp_value in tmp_dict.items():
tmp_key = separator.join((key, tmp_key))
@brainix
brainix / runlength.py
Last active Feb 7, 2021
Run-length encoding tech screen
View runlength.py
# https://twitter.com/brainix/status/1358343863354552324
def runlength_encoding(s):
if s:
count = 1
for c1, c2 in zip(s[:-1], s[1:]):
if c1 == c2:
count += 1
else:
yield c1, count
View hanoi.py
>>> def hanoi(num, from_, using, to):
... if num:
... yield from hanoi(num-1, from_, to, using)
... yield f'move disc {num} from peg {from_} to peg {to}'
... yield from hanoi(num-1, using, from_, to)
...
>>> for move in hanoi(4, 1, 2, 3):
... print(move)
...
move disc 1 from peg 1 to peg 2
View sieve.py
>>> def naturals(start):
... yield start
... yield from naturals(start+1)
...
>>> def sieve(prev_sieve):
... prime = next(prev_sieve)
... yield prime
... yield from (num for num in prev_sieve if num % prime != 0)
...
>>> s = sieve(naturals(2))
View wat.py
>>> j = 256
>>> k = 256
>>> j is k
True
>>> j = 257
>>> k = 257
>>> j is k
False
>>> j, k = 257, 257
>>> j is k
View assertion.py
import random
def assertion_sort(list_):
try:
assert all(x <= y for x, y in zip(list_, list_[1:]))
except AssertionError:
random.shuffle(list_)
assertion_sort(list_)
View encrypt.py
#!/usr/bin/env python3
import sys
# Map the uppercase letters forwards:
_MAPPING = {
'Q': 'P',
'W': 'O',
@brainix
brainix / rss.xml
Created Jun 23, 2016
iTunes top songs
View rss.xml
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns:im="http://itunes.apple.com/rss" xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
<id>https://itunes.apple.com/us/rss/topmusicvideos/limit=200/explicit=true/xml</id><title>iTunes Store: Top Music Videos</title><updated>2016-06-22T20:09:58-07:00</updated><link rel="alternate" type="text/html" href="https://itunes.apple.com/WebObjects/MZStore.woa/wa/viewTop?cc=us&amp;id=1&amp;popId=5"/><link rel="self" href="https://itunes.apple.com/us/rss/topmusicvideos/limit=200/explicit=true/xml"/><icon>http://itunes.apple.com/favicon.ico</icon><author><name>iTunes Store</name><uri>http://www.apple.com/itunes/</uri></author><rights>Copyright 2008 Apple Inc.</rights>
<entry>
<updated>2016-06-22T20:09:58-07:00</updated>
<id im:id="1125405366">https://itunes.apple.com/us/music-video/this-is-what-you-came-for/id1125405366?uo=2</id>
View nettools.py
import jnettool.tools.Routing
import jnettool.tools.elements.NetworkElement
class NetworkElementError(Exception):
pass
class NetworkElement(object):
View p_vs_np_2.py
from nettools import NetworkElement
with NetworkElement('171.0.2.45') as ne:
for route in ne.routing_table:
print '%15s -> %s' % (route.name, route.ipaddr)