Skip to content

Instantly share code, notes, and snippets.

View mohi048's full-sized avatar
💭
:)

Mohit mohi048

💭
:)
View GitHub Profile
@mohi048
mohi048 / program.cpp
Created February 10, 2026 12:21
arduino uno random blinks
// Define the pins for your 5 LEDs
int ledPins[] = {2, 3, 4, 5, 6};
void setup() {
// Initialize all 5 pins as OUTPUT
for (int i = 0; i < 5; i++) {
pinMode(ledPins[i], OUTPUT);
}
}
@mohi048
mohi048 / disk_usage_thresold.py
Last active January 22, 2026 15:10
test_gist
import subprocess
import json
# Your command string
command = """
df --output=source,size,used,avail,pcent,target -B G | \
grep '^/dev/' | \
awk '{print $1, $2, $3, $4, $5, $6}' | \
jq -R -s '[split("\\n") | .[] | select(length > 0) | split(" ") | {Filesystem: .[0], Size_GB: .[1], Used_GB: .[2], Avail_GB: .[3], Use_Percent: .[4], Mounted_on: .[5]}]'
"""
@mohi048
mohi048 / 1.conventions.txt
Created August 29, 2017 12:30
Ansible: Basics
All files with a .yml extension.
Tabs are NOT allowed, use spaces ONLY.
You MUST indent your properties and lists with 1 or more spaces.
All keys/properties are case-sensitive. ("ThIs", is not the same as "thiS")
For Ansible, nearly every YAML file starts with a list.
Each item in the list is a list of key/value pairs, commonly called a “hash” or a “dictionary”.
So, we need to know how to write lists and dictionaries in YAML.
All YAML files (regardless of their association with Ansible or not) can optionally begin with --- and end with ....
@mohi048
mohi048 / Dijkastra_class.py
Created August 7, 2017 11:52
Python: Dijkstra algorithm class implementation
from collections import defaultdict
class Graph:
def __init__(self):
self.graph = defaultdict(dict)
def addEdge(self,x,y,w):
if x in self.graph.keys():
#self.graph[x].update({y:w})
self.graph[x][y] = w
@mohi048
mohi048 / bellman_ford.py
Created August 6, 2017 14:29
Python: Bellman ford Algorithim
def bellman_ford(graph,source,dest):
pred,dist = initialize_graph(graph,src)
for i in range(len(graph)-1):
for k in graph:
for v in graph[k]:
relax(graph,k,v,dist,pred)
def relax(graph,source,Neighbour,dist,pred):
if dist[source] + graph[source][Neighbour] < dist[Neighbour]:
@mohi048
mohi048 / list_flattern.py
Last active August 3, 2017 06:57
Python: flattern a list of lists in python
>>> def flatout_list(somelist):
... flat_list = []
... def check_list(alist):
... for elem in alist:
... if isinstance(elem,list):
... #if type(elem) is list: Alternative way
... check_list(elem)
... else:
... flat_list.append(elem)
... check_list(somelist)
@mohi048
mohi048 / pool_design.py
Created July 18, 2017 12:25
creational patterns -: Object Pool
'''
Provides performance boost when Construction and destruction of object can become very expensive operation if it occurs very often.
Object pool is a managed set of reusable objects.
Clients then “check out” objects from the pool return them back when they don’t need them any more. But it’s not that easy as it sounds.
The manager of the pool has to deal with various problems.
'''
# Source - http://radek.io/2011/08/04/design-pattern-object-pool/
@mohi048
mohi048 / Python_Descriptors.py
Last active March 19, 2019 11:27
Python Descriptors
'''
Provide the ability to add managed attributes to objects.
The methods needed to create a descriptor are __get__, __set__ and __delete__.
If you define any of these methods, then you have created a descriptor.
Idea is to get, set or delete attributes from your object’s dictionary.
When you access a class attribute, this starts the lookup chain
__get__(self, obj, type=None), returns value
__set__(self, obj, value), returns None
__delete__(self, obj), returns None
@mohi048
mohi048 / Borg_Pattern.py
Created July 18, 2017 11:27
Monostate pattern is a way to implement singleton behavior, but instead of having only one instance of a class, there are multiple instances that share the same state
class Same_State(object):
__shared_state = {}
def __init__(self):
self.__dict__ = self.__shared_state
self.state = 'Init'
def __str__(self):
return self.state
@mohi048
mohi048 / bfs.py
Created July 7, 2017 12:59
Breadth First Traversal or BFS for a Graph
from collections import defaultdict
class Graph:
def __init__(self):
self.graph = defaultdict(list)
def addEdge(self,u,v):
self.graph[u].append(v)
def bfs(self,vertex):