Skip to content

Instantly share code, notes, and snippets.

@justinmoon
justinmoon / gist:7b2089bcc71653a691be
Created August 14, 2014 22:31
cool way to define factorials
class F:
def __init__(self):
self.cache = {}
def __call__(self, n):
if n in self.cache.keys():
return self.cache[n]
else:
if n == 1:
@justinmoon
justinmoon / main_file.py
Created August 27, 2014 20:13
attempt to demonstrate "if __name__ == '__main__' " pattern.
from other import name
if __name__ == '__main__':
print "main.py's __name__ is: " + __name__
print "imprt.py's __name__ is: {}".format(name)
@justinmoon
justinmoon / validation_and_serialization.py
Last active August 29, 2015 14:22
Code from Flask Meetup at Zerocater
# this is how to highlight code for powerpoint: `pbpaste | highlight --syntax=py -O rtf | pbcopy`
# api_jsonify()
from decimal import Decimal
from uuid import UUID
from datetime import datetime
from flask import Response
from json import JSONEncoder, dumps
class JsonApiEncoder(JSONEncoder):
function doGet(e) {
// All Udacity spreadsheet are accessible by agent (the student) running this script
var fileId = e.parameters.fileId;
// Get the file, make copy with same name, and grab new URL
var existingFile = DriveApp.getFileById(fileId);
var fileName = existingFile.getName();
var copiedFile = existingFile.makeCopy(fileName);
var newUrl = copiedFile.getUrl();
@justinmoon
justinmoon / unimported.py
Created June 22, 2016 07:00
First-order hack at finding dead python code (unimported python files)
import os
import re
from pprint import pprint
def import_from_path(path):
try:
path, file_extension = path.split('.')
_, karmic_path = path.split(os.getcwd())
return re.sub('/', '.', karmic_path)
@justinmoon
justinmoon / cleanse.py
Created June 22, 2016 07:06
Delete your unused git branches
import subprocess
from git import Repo
def delete_branch(branch):
branch_name = branch.name[7:] # chop off "origin/"
exit_code = subprocess.call(['git', 'push', 'origin', '--delete', branch_name])
if exit_code != 0:
print('problem deleting', branch_name)
@justinmoon
justinmoon / Discussion.md
Last active June 25, 2016 21:01
See Discussion.md

Problem statement

  • When I attmept to exercise a certain API using the main.py script below, the server usually (but not always) sends a 400.
  • When I take a given url that generated a 400, and:
    • Paste it into Chrome, I get a 200!
    • Fire up python REPL and feed it to requests.get, I get a 200! I can do this repeatedly from "for loop" and always get 200's!
    • Open up Chrome debug tools and request if using fetch method, I get a 200!

Observations

@justinmoon
justinmoon / hang.log
Last active July 16, 2016 22:38
From a hanging unittest
=================================== FAILURES ===================================
_____________________________ test_multilline_num ______________________________
def test_multilline_num():
code = ('x = 1\n'
'ls -l\n') # this second line wil be transformed
> tree = check_parse(code)
tests/test_ast.py:31:
#!/usr/bin/python
# btpeer.py
from __future__ import print_function
import socket
import struct
import threading
import time
import traceback
@justinmoon
justinmoon / demo.py
Created August 14, 2018 19:32
Check if thread is dead in Python
import threading
import time
def doom():
raise RuntimeError()
def zoom():
while True: