Skip to content

Instantly share code, notes, and snippets.

View MMohan1's full-sized avatar

Man Mohan Sharma MMohan1

  • Edge Networks Pvt Ltd
  • Bangalore
View GitHub Profile
@MMohan1
MMohan1 / parkinglot.py
Last active March 26, 2021 12:53
Multi Parking lot
"""
N level are there in multi story parking
X number slots are there on each level
to park a vehicle take reg number and color
Question which your program need to answer
1.Give me all slots for given a color
2.Give me slot number for a give red number
"""
@MMohan1
MMohan1 / lruCachingPython.py
Created March 25, 2021 15:53
LRU caching using python
class Caching:
def __init__(self, size=5):
self.store = {}
self.size = 5
def update_recently_used(self, key):
value = self.store.pop(key)
self.store[key] = value
def get(self, key):
@MMohan1
MMohan1 / nested_dict_opration.py
Last active March 4, 2019 09:29
Python update and get data from nested dict
from functools import reduce
def deep_get(dictionary, keys_list, default=None):
keys = ".".join(keys_list)
return reduce(lambda d, key: d.get(key, default) if isinstance(d, dict) else default, keys.split("."),
dictionary)
def setInDict(dataDict, mapList, value):
if mapList[:-1]:
if deep_get(dataDict, mapList[:-1]):
deep_get(dataDict, mapList[:-1])[mapList[-1]] = value
@MMohan1
MMohan1 / mongoelastic.py
Created January 16, 2018 07:03
mongo_to_elastic_dump
'''
Tested with:
Mongo 3.0.12
pymongo 3.3.0
Elasticsearch 2.1.2
Kibana 4.3.3
elasticsearch python 2.1.0
'''
@MMohan1
MMohan1 / nested_7z.py
Last active December 26, 2017 10:51
recursively Extract 7z files
import py7zlib
import os
import re
class SevenZFile(object):
@classmethod
def is_7zfile(cls, filepath):
'''
Class method: determine if file path points to a valid 7z archive.
@MMohan1
MMohan1 / nested_zip.py
Last active June 1, 2023 19:45
recursively Extract zip files
import os
import zipfile
import re
def extract_nested_zip(zippedFile, toFolder):
""" Extract a zip file including any nested zip files
Delete the zip file(s) after extraction
"""
if not os.path.exists(zippedFile):
return
@MMohan1
MMohan1 / titleCase.pipe.js
Last active September 19, 2017 05:07
Title case with filtering Prepositions
import {Pipe, PipeTransform} from '@angular/core';
/*
* Changes the case of the first letter of a world by avoiding the prepositions.
*/
@Pipe({name: 'titlecase'})
export class TitleCase implements PipeTransform {
transform(input:string):string {
let words = input.split(" ");
@MMohan1
MMohan1 / gist:b6155e41d41c63ec6473d1815043cf23
Last active September 12, 2018 13:43
Python date time python-dateutil package issue
Q - HOW TO GET THE DATETIME PYTHON OBJ FROM A DATE STRING WITHOUT KNOWING THE STRING DATETIME FORMAT.
A- we use "delorean" (pip install delorean) package to make that string in to python datetime
object. How to use it --
pip install delorean
Go to python shell and try this
>>> datetime_str = "2017-07-17"
>>> from delorean import parse
>>> parse(datetime_str.strip()).datetime.month
7
>>> parse(datetime_str.strip()).datetime.day