Skip to content

Instantly share code, notes, and snippets.

@igniteflow
igniteflow / redis-producer-consumer.py
Created February 20, 2012 16:37
A very simple implementation of a Redis producer-consumer messaging queue in Python
import redis
import time
import sys
def producer():
r = redis.Redis()
i = 0
while True:
r.rpush('queue', 'Message %d' % i)
@igniteflow
igniteflow / pyenv-virtualenv-commands.sh
Last active January 22, 2026 15:50
pyenv virtualenv commands
# Install prerequisites
brew install pyenv pyenv-virtualenv
# Install Python version 3.13.7
pyenv install 3.13.7
# Create a virtual environment named 'bag-end' using Python 3.13.7
pyenv virtualenv 3.13.7 bag-end
# Activate the 'bag-end' virtual environment
@igniteflow
igniteflow / mock-object-property.py
Created October 3, 2016 11:03
How to mock an object property in Python
import mock
with mock.patch('path.to.ObjectClass.my_property', new_callable=mock.PropertyMock) as mock_my_property:
mock_my_property.return_value = 'my value'
@igniteflow
igniteflow / gist:4474040
Last active October 9, 2024 08:05
Create a folder and set user:group with Python's os module
import os
import pwd
file_path = '/tmp/example'
if not os.path.exists(file_path):
os.makedirs(file_path) # creates with default perms 0777
uid, gid = pwd.getpwnam('root').pw_uid, pwd.getpwnam('www-data').pw_uid
os.chown(file_path, uid, gid) # set user:group as root:www-data
# go check with ls -lah /tmp/example
@igniteflow
igniteflow / corsdevserver.py
Created April 22, 2013 15:34
A simple CORS compliant web server in Python, useful for development (with @alex-moon)
import BaseHTTPServer
import cgi
from pprint import pformat
PORT = 6969
FILE_TO_SERVE = 'path/to/your/response/content.json'
class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
"""
@igniteflow
igniteflow / gist:2889573
Created June 7, 2012 15:47
Displaying HTML in Django admin
from django.utils.safestring import SafeUnicode
"""
When overriding Django admin templates |safe and autoescape off don't work, so do this instead...
"""
# for a foreign key field in the change form, if you want to override the unicode method, use a proxy
class UserProxy(User):
"""
Using a proxy to present the required formatting: username, email, full name
@igniteflow
igniteflow / slack-images.py
Created October 8, 2018 08:48
Download all images from a Slack channel
import datetime
import requests
import os.path
TOKEN = 'xxxx-xxxx'
TARGET_DIR = './images/'
@igniteflow
igniteflow / business-days-in-month.py
Last active March 2, 2024 06:39
Print the number of business days and weekend days in a month
#!/usr/bin/python3
"""
Print the number of business days and weekend days in a month
Example usage:
$ python ./business-days-in-month.py 2024 2
February, 2024 has 21 weekdays and 8 weekend days
Optional: Make the script executable
@igniteflow
igniteflow / mock-django-cache.py
Created July 6, 2015 12:57
Mock Django's cache in unit tests
"""
myapp/mymodule.py:
from django.core.cache import cache
def set_cache():
cache.set('foo', 'bar')
def get_cache():
return cache.get('foo')
@igniteflow
igniteflow / git-commands.sh
Last active December 14, 2023 06:31
Git commands
# pull changes from a single file inside a commit (useful when hotpatching)
git diff ..<hash> -- <filename> | git apply
# view changes in a file historically
git diff 'HEAD@{3 weeks ago}'..HEAD -- foo/my_module.py
# view commits with patches in this branch only (assuming it was branched from master and is up-to-date)
# useful when reviewing tickets
git log master.. --patch --reverse