Skip to content

Instantly share code, notes, and snippets.

View manuphatak's full-sized avatar
🎯
Focusing

Manu Phatak manuphatak

🎯
Focusing
View GitHub Profile
# coding=utf-8
def is_valid(input_):
"""validation rules"""
return input_ in '012345ABC'
while True:
choice = raw_input()
if is_valid(choice):
break
# coding=utf-8
import collections
data = {'one': 1, 'two': 2, 'three': 3, 'a': 2}
sorted_data = collections.OrderedDict(sorted(data.items()))
print sorted_data
print sorted_data['three']
@manuphatak
manuphatak / long_lines.py
Created April 2, 2015 15:56
long strings
url = 'https://www.reddit.com/r/learnpython/comments' \
'/2ztwnt/how_to_use_string_questions_on_quotes_str_i_have/'
alt = ('https://www.reddit.com/r/learnpython/comments'
'/2ztwnt/how_to_use_string_questions_on_quotes_str_i_have/')
@manuphatak
manuphatak / cache_requests.py
Last active August 29, 2015 14:19
Persistent lru caching of the requests library. Stop spamming apis while you develop, starting now.
#!/usr/bin/env python
# coding=utf-8
"""
cache_requests
--------------
This module implements a basic LRU decorator that syncs calls with a redislite database.
ELI5: If you call the same function with the same parameters, it does not recalculate
the function. Instead, the first time, the results are stored, the second time the
@manuphatak
manuphatak / css_class.py
Created September 20, 2015 21:43
Django css class tag. Combine conditional classes.
# Python Libraries
import re
# Django Packages
from django import template
register = template.Library()
_re_camel_humps = re.compile('([a-z])([A-Z0-9])')
"""
@manuphatak
manuphatak / default_args_class_decorator.py
Created September 20, 2015 22:08
Class decorator that overrides default kwargs.
from functools import wraps
def defaults(method='__init__', **default_args):
"""Class decorator. Overrides method default arguments."""
def decorate(cls):
func = getattr(cls, method)
@wraps(func)
@manuphatak
manuphatak / atexit_setup.py
Created December 8, 2015 05:47
Dealing with `atexit` and setup.py test command deleting modules.
from setuptools import setup
from setuptools.command.test import test as TestCommand
class PyTest(TestCommand):
def finalize_options(self):
TestCommand.finalize_options(self)
self.test_args = []
self.test_suite = True
def run_tests(self):
@manuphatak
manuphatak / pipeline.py
Last active December 12, 2015 07:03
Idiomatic text processing. Inverting reduce: instead of running one function on a list of values, run a list of functions on one value.
from functools import reduce
def pipeline(steps, initial=None):
def apply(result, step):
yield from step(result)
yield from reduce(apply, steps, initial)
if __name__ == '__main__':
# BEFORE
@manuphatak
manuphatak / convert-posts-to-portfolio.md
Last active July 25, 2018 19:38
WordPress: convert posts to jetpack portfolio entries

Executive summary

Use the import + export tool and a text editor to replace a few data points.

  1. Export your posts to xml
  2. Use a text editor with a search + replace tool to replace a few fields
  3. Import the updated xml

Search + replace details

@manuphatak
manuphatak / run.py
Last active September 8, 2017 17:21
Run hackerrank maze escape locally. https://www.hackerrank.com/challenges/maze-escape/
# /usr/bin/env python
# coding=utf-8
from __future__ import print_function
import json
import os.path
import shutil
import subprocess
import sys
from argparse import ArgumentParser, FileType