Skip to content

Instantly share code, notes, and snippets.

Caleb Madrigal calebmadrigal

Block or report user

Report or block calebmadrigal

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@calebmadrigal
calebmadrigal / base64_NSString.m
Created Feb 22, 2012
Objective-C method which takes an NSString* and returns an base64-encoded NSString*
View base64_NSString.m
+ (NSString *)base64String:(NSString *)str
{
NSData *theData = [str dataUsingEncoding: NSASCIIStringEncoding];
const uint8_t* input = (const uint8_t*)[theData bytes];
NSInteger length = [theData length];
static char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
NSMutableData* data = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
uint8_t* output = (uint8_t*)data.mutableBytes;
@calebmadrigal
calebmadrigal / FourierCoeff.ipynb
Created Dec 18, 2012
iPython Notebook demonstration of how to find Fourier coefficients.
View FourierCoeff.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@calebmadrigal
calebmadrigal / pythonanywhere_gist.py
Last active Dec 16, 2015
Function to get the PythonAnywhere gist viewer for a given gist file.
View pythonanywhere_gist.py
import urllib2
def get_raw_gist(raw_gist_url):
return urllib2.urlopen(raw_gist_url).read()
def python_anywhere_gist_url(gist_num, gist_file, py_version="python2"):
return "https://www.pythonanywhere.com/gists/{0}/{1}/{2}".format(gist_num, gist_file, py_version)
print get_raw_gist("https://gist.github.com/calebmadrigal/5504352/raw/pythonanywhere_gist.py")
print python_anywhere_gist_url(5504352, "pythonanywhere_gist.py")
@calebmadrigal
calebmadrigal / yolo.py
Created Feb 24, 2015
FIFO, LIFO, YOLO
View yolo.py
import random
from collections import deque
print("#FIFO")
queue = deque()
for i in range(10):
queue.append(i)
print([queue.popleft() for _ in range(len(queue))])
print("#LIFO")
View keybase.md

Keybase proof

I hereby claim:

  • I am calebmadrigal on github.
  • I am calebm (https://keybase.io/calebm) on keybase.
  • I have a public key whose fingerprint is F775 F481 BE72 409E 856C E93E B870 87DD 2D11 1E54

To claim this, I am signing this object:

@calebmadrigal
calebmadrigal / log_example.py
Created Jun 18, 2015
Basic python logging example
View log_example.py
import logging
config = { 'logfile': 'log_example.log', 'loglevel': 'DEBUG' }
logger = logging.getLogger('log_example')
log_handler = logging.FileHandler(config['logfile'])
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
log_handler.setFormatter(formatter)
logger.addHandler(log_handler)
logger.setLevel(logging.getLevelName(config['loglevel']))
@calebmadrigal
calebmadrigal / tkinter_example.py
Created Jun 20, 2015
Drawing individual pixels with Tkinter.
View tkinter_example.py
import random
import math
from tkinter import Tk, Canvas, PhotoImage, mainloop
width = 1000
height = 600
window = Tk()
canvas = Canvas(window, width=width, height=height, bg="#000000")
canvas.pack()
img = PhotoImage(width=width, height=height)
@calebmadrigal
calebmadrigal / evil_child.py
Last active Sep 1, 2015
evil_child() kills all parents and siblings, leaving only itself alive to do what it pleases (like cleaning things up)
View evil_child.py
import os
import signal
import time
from multiprocessing import Process
def kill_all(parent_pid):
os.killpg(os.getpgid(parent_pid), signal.SIGKILL)
def evil_child(parent_pid):
print("Killing my parents and siblings...")
@calebmadrigal
calebmadrigal / for_loop.py
Last active Sep 24, 2015
Python for loop vs list comprehension speed test. This is just testing the speed of performing a particular operation in both a for loop and list comprehension. Note that the list comprehension version uses more memory because it is storing the results. This could be partially responsible for it being slower.
View for_loop.py
import sys
num = int(sys.argv[1])
def test(i):
return i+1
for i in range(num):
t = test(i)
@calebmadrigal
calebmadrigal / example_run.txt
Last active Aug 25, 2017
Python timeout function with ast
View example_run.txt
cmadrigal-MBP:ast_stuff caleb.madrigal$ python3 wrap_function_test.py
Wrapping function: range
Wrapping function: do_sleep
check_timeout_proxy()
check_timeout_proxy()
check_timeout_proxy()
check_timeout_proxy()
Traceback (most recent call last):
File "wrap_function_test.py", line 34, in <module>
run_code_string_with_timeout(code, 3)
You can’t perform that action at this time.