Skip to content

Instantly share code, notes, and snippets.

View pythonhacker's full-sized avatar
💭
Full Reset

Anand B Pillai pythonhacker

💭
Full Reset
View GitHub Profile
@pythonhacker
pythonhacker / threading_decision_table.md
Created January 19, 2026 11:45
Decision table with Python threads
Problem Pattern
Background notification Fire-and-forget + daemon
Delayed action threading.Timer
Long-lived worker Subclass Thread
I/O fan-out ThreadPoolExecutor
Pipelines Threads + Queue
Best-effort work Daemon threads
@pythonhacker
pythonhacker / ollama_shell_demo.py
Last active January 13, 2026 11:40
Interaction with Ollama LLM console
ollama> Hi - my name is Anand. I shall call you Amy.
Hello Anand! It's nice to meet you. I'm Amy, and I'll do my best to assist you with any
questions or tasks you may have. How can I help you today?
ollama> What is my name?
Your name is Anand. Is there anything else I can help you with?
ollama> Identify yourself please
I'd be happy to identify myself. I am Amy, a highly advanced language assistant. I'm a
computer program designed to provide accurate and helpful information on a wide range of
topics, from science and history to entertainment and culture. I'm constantly learning
and improving, so there may be occasions where my responses seem uncertain or unclear.
@pythonhacker
pythonhacker / elastic_console.py
Last active January 13, 2026 15:50
A Python cmd.Cmd console as a devops helper to Elasticsearch
import cmd
import json
import os
from pathlib import Path
from elasticsearch import Elasticsearch
from elasticsearch.helpers import bulk
import warnings
warnings.filterwarnings("ignore")
class QueryHelper:
@pythonhacker
pythonhacker / ollama_shell.py
Last active January 13, 2026 15:45
A Python cmd.Cmd shell for talking with Ollama model with context
import ollama
import cmd
class OllamaShell(cmd.Cmd):
""" A console to ask questions to any LLM supported by ollama """
prompt = "ollama> "
# Ollama service needs to be running locally
def __init__(self, model='llama3.2:1b'):
self.model = model
@pythonhacker
pythonhacker / calc_console.py
Created January 13, 2026 07:11
A Python cmd.Cmd console supporting arithmetic operations
import cmd
import shlex
import operator
from functools import reduce
class CalcConsole(cmd.Cmd):
""" A console that helps to perform regular arithmetic """
prompt = "Calc> "
@pythonhacker
pythonhacker / addshell.py
Created January 13, 2026 07:09
A simple Python cmd.Cmd shell supporting addition
import cmd
import shlex
class AddShell(cmd.Cmd):
""" A simple cmd shell that adds numbers at the prompt """
prompt = "Add> "
def default(self, arg):
items = shlex.split(arg)
nums = [float(i) for i in items]
@pythonhacker
pythonhacker / diceroll.py
Created January 13, 2026 07:07
A Python cmd console representing rolling of a dice
import cmd
import random
import time
class DiceRoll(cmd.Cmd):
""" A custom command prompt for a rolling dice """
prompt = "dice>>> "
def __init__(self):
@pythonhacker
pythonhacker / search_word_demo.py
Last active January 6, 2026 14:02
Demo of the search_word program with bisect module
>>> filename='/usr/share/dict/words'
>>> bt.search_word(filename, 'calculate')
['calculate', 'calculated', 'calculates']
>>> bt.search_word(filename, 'code')
['code', "code's", 'coded', 'codeine', "codeine's", 'codependency', "codependency's", 'codependent',
"codependent's", 'codependents', 'codes', 'codex', "codex's"]
>>> bt.search_word(filename, 'search')
['search', "search's", 'searched', 'searcher', "searcher's", 'searchers', 'searches', 'searching',
'searchingly', 'searchlight', "searchlight's", 'searchlights']
@pythonhacker
pythonhacker / search_word.py
Last active January 6, 2026 14:10
Word search using prefix search with the bisect module
def search_word(filename, prefix):
""" Search a file containing a wordlist and print matching words by prefix """
wordlist = sorted(filter(None, [item.lower().strip() for item in open(filename)]))
words = []
idx = bisect.bisect_left(wordlist, prefix)
for idx in range(idx, len(wordlist)):
word = wordlist[idx]
@pythonhacker
pythonhacker / iq_range_dict.py
Created January 6, 2026 13:25
IQ function rewritten using the custom range dictionary
def IQ(score):
""" IQ function using range dictionary """
scores_class_dict={0: 'intellectually disabled',
70: 'below average',
85: 'average',
115: 'bright',
130: 'gifted',
145: 'highly gifted',
160: 'exceptionally gifted',