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 / 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
@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 / 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: anandpillai@letterboxes.org
"""
import sys
class LEDMatrix(object):
@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 / 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 / docli.log
Last active November 6, 2019 07:29
Error when running docli
$ docli droplet --snapshot 163461766
╒═══════════════╤═════════════════════════════╕
│ Fields │ Values │
╞═══════════════╪═════════════════════════════╡
│ SnapShot Id │ 53919120 │
├───────────────┼─────────────────────────────┤
│ SnapShot Name │ pfaas-sg-ps-1-1571760580915 │
├───────────────┼─────────────────────────────┤
│ Distribution │ Debian │
├───────────────┼─────────────────────────────┤
"""
A simple get function which caches responses for a URL
using a LFU cache
"""
import requests
from collections import Counter
class LFUCache(object):
""" Least frequently used cache with caching done on SET """
@pythonhacker
pythonhacker / prime_anagrams.rs
Last active June 25, 2020 06:19
@fermatslibrary - Anagram finder implemented using Prime Numbers in Rust
// Courtesy: @Fermatslibrary
// Reference: https://twitter.com/fermatslibrary/status/1275066521450975234
// Copyright: Anand B Pillai @skeptichacker
// LICENSE: MIT
use std::env;
use std::collections::HashMap;
pub fn is_prime(n: u64) -> bool {
@pythonhacker
pythonhacker / toggle_xcompositor.sh
Last active March 25, 2021 16:24
Toggle X compositor on or off in shell (linux)
function toggle_xcomp() {
# toggle X compositor
ret=`xfconf-query -c xfwm4 -p /general/use_compositing`
case "$ret" in
"false") echo "turning compositor on";xfconf-query -c xfwm4 -p /general/use_compositing -t bool -s true;;
*) echo "turning compositor off";xfconf-query -c xfwm4 -p /general/use_compositing -t bool -s false;;
esac
}
@pythonhacker
pythonhacker / thinkfan_lenovo.md
Last active September 5, 2023 05:20
Controlling fan speed on Lenovo Thinkpad running antiX (or other Debian derivatives)

About

This is a write up on how to use thinkfan on Lenovo ThinkPads running antiX Linux. I am writing from my (bad) experience after installing antiX Grup Yorum 21 on a Lenovo ThinkPad P15s with the fan running constantly at max speed.

This is based on the guide provided at https://gist.github.com/Yatoom/1c80b8afe7fa47a938d3b667ce234559 however since the thinkfan utility does not seem to be available in the default repos of antiX Linux, it also includes steps to build the utility from source code - plus other tweaks for Thinkpad.

Try installing from repo

 $ sudo apt install thinkfan