Skip to content

Instantly share code, notes, and snippets.

View pythonhacker's full-sized avatar
💭
New Beginnings

Anand B Pillai pythonhacker

💭
New Beginnings
View GitHub Profile
@pythonhacker
pythonhacker / scrapy_settings.txt
Created February 13, 2019 10:40
Find Scrapy HTTP cache directory
$ scrapy shell -s HTTPCACHE_ENABLED=True
2017-02-28 10:41:41 [scrapy.utils.log] INFO: Scrapy 1.3.2 started (bot: httpbin)
(...)
>>> from scrapy.utils.misc import load_object
>>> storage = load_object(settings['HTTPCACHE_STORAGE'])(settings)
>>> storage.cachedir
'/home/paul/scrapy/httpbin/.scrapy/httpcache'
@pythonhacker
pythonhacker / homogeneity.py
Created January 16, 2018 13:28
Test homogeneity of a Python data structure using a one liner
# For homogenous inputs, no assertion error
def homogeneity(data):
assert(len(dict(map(lambda x: (type(x), 1), data))) == 1)
@pythonhacker
pythonhacker / led_digit.py
Created February 28, 2017 12:47
Light an LED digit on the console using Python
"""
A Python program to light an LED digit on console
Author: [email protected]
"""
import sys
class LEDMatrix(object):
@pythonhacker
pythonhacker / ttldict.py
Created October 12, 2016 05:54
A Python dictionary type with keys having specific time to live (TTL)
import threading
import time
class TTLDict(dict):
""" A dictionary with keys having specific time to live """
def __init__(self, ttl=5):
self._ttl = ttl
# Internal key mapping keys of _d
# to times of access
@pythonhacker
pythonhacker / state.py
Last active June 22, 2022 07:01
Example of State Pattern in Python implementing showing a minimal State Machine
"""
The State design pattern.
In Python this is done dynamically by changing the __class__ attribute.
Author : Anand B Pillai <anandpillai@letterboxes.org>
License: Public Domain
Ref: http://harkablog.com/dynamic-state-machines.html