Skip to content

Instantly share code, notes, and snippets.

@bwagner
bwagner / clip_magic.py
Last active August 5, 2023 10:22 — forked from cphyc/clip_magic.py
copy to clipboard ipython magic
"""
Add copy to clipboard from IPython!
To install, just copy it to your profile/startup directory, typically:
~/.ipython/profile_default/startup/
updated version: https://gist.github.com/bwagner/270da7c7d31af7ffaca32674557fc172
Example usage:
@disconnect3d
disconnect3d / makefile
Last active November 17, 2022 01:58
Minimal & universal makefile for C language
CC=gcc
CFLAGS=-Wall -Wextra -Wpedantic
LDFLAGS=
SOURCES=$(wildcard *.c )
OBJECTS=$(SOURCES:.c=.o)
EXECUTABLE=exec
all: $(EXECUTABLE)
./$(EXECUTABLE)
@jgillman
jgillman / restore.sh
Last active May 20, 2024 19:56
pg_restore a local db dump into Docker
# Assumes the database container is named 'db'
DOCKER_DB_NAME="$(docker-compose ps -q db)"
DB_HOSTNAME=db
DB_USER=postgres
LOCAL_DUMP_PATH="path/to/local.dump"
docker-compose up -d db
docker exec -i "${DOCKER_DB_NAME}" pg_restore -C --clean --no-acl --no-owner -U "${DB_USER}" -d "${DB_HOSTNAME}" < "${LOCAL_DUMP_PATH}"
docker-compose stop db
@lysender
lysender / using-aws-kms.php
Created November 10, 2015 09:13
Using AWS KMS API via PHP SDK
<?php
use Aws\Kms\KmsClient;
// Somewhere in the controller or model
$this->load->config('aws');
// Not needed for EC2 instance role based authorization - for my local instance only
$key = $this->config->item('aws_s3_access_key');
$secret = $this->config->item('aws_s3_secret_key');
@nephthys
nephthys / base.html
Last active February 27, 2018 13:05
Highlight current page / app in Django
<ul class="nav navbar-nav">
<li{% if app_name == 'pages' %} class="active"{% endif %}>
<a href="{% url "page_list" %}">{% trans "Pages" %}</a>
</li>
</ul>
@Zearin
Zearin / python_decorator_guide.md
Last active May 10, 2024 14:26
The best explanation of Python decorators I’ve ever seen. (An archived answer from StackOverflow.)

NOTE: This is a question I found on StackOverflow which I’ve archived here, because the answer is so effing phenomenal.


Q: How can I make a chain of function decorators in Python?


If you are not into long explanations, see [Paolo Bergantino’s answer][2].

@odyniec
odyniec / test_temp_directory.py
Last active January 30, 2023 11:12
An example Python unittest test case that creates a temporary directory before a test is run and removes it when it's done.
import shutil, tempfile
from os import path
import unittest
class TestExample(unittest.TestCase):
def setUp(self):
# Create a temporary directory
self.test_dir = tempfile.mkdtemp()
def tearDown(self):
@shawnbutts
shawnbutts / bytesto.py
Created October 17, 2012 17:33
bytes to to mb, gb, etc in python
def bytesto(bytes, to, bsize=1024):
"""convert bytes to megabytes, etc.
sample code:
print('mb= ' + str(bytesto(314575262000000, 'm')))
sample output:
mb= 300002347.946
"""
a = {'k' : 1, 'm': 2, 'g' : 3, 't' : 4, 'p' : 5, 'e' : 6 }
@stober
stober / average.py
Created July 13, 2012 22:00
A generator for incremental averaging in Python
def consumer(func):
"""
See: http://www.dabeaz.com/generators/
"""
def start(*args,**kwargs):
c = func(*args,**kwargs)
c.next()
return c
return start
@jboner
jboner / latency.txt
Last active May 21, 2024 18:29
Latency Numbers Every Programmer Should Know
Latency Comparison Numbers (~2012)
----------------------------------
L1 cache reference 0.5 ns
Branch mispredict 5 ns
L2 cache reference 7 ns 14x L1 cache
Mutex lock/unlock 25 ns
Main memory reference 100 ns 20x L2 cache, 200x L1 cache
Compress 1K bytes with Zippy 3,000 ns 3 us
Send 1K bytes over 1 Gbps network 10,000 ns 10 us
Read 4K randomly from SSD* 150,000 ns 150 us ~1GB/sec SSD