Skip to content

Instantly share code, notes, and snippets.

View premchalmeti's full-sized avatar
🎯
Focusing

Premkumar Chalmeti premchalmeti

🎯
Focusing
View GitHub Profile
@premchalmeti
premchalmeti / safe_urljoin.py
Created January 17, 2023 07:01
Safe version of urljoin, joins the URIs carefully considering the prefixes and trailing slashes.
def safe_urljoin(*uris) -> str:
"""
Joins the URIs carefully considering the prefixes and trailing slashes.
The trailing slash for the end URI is handled separately.
>>> safe_urljoin("https://px.com/", "adunits/", "/both/", "/left")
>>> 'https://px.com/adunits/both/left'
>>> safe_urljoin("https://px.com/", "adunits/", "/both/", "right/")
>>> 'https://px.com/adunits/both/right/'
@premchalmeti
premchalmeti / highlighter.py
Created December 11, 2022 13:51
Elasticsearch: custom highlighter implementation for proximity search implementation using slop query
doc = 'The quick brown fox jumps over the lazy dog. The quick brown fox over the lazy dog. The quick brown fox jumps over the lazy dog.'
cur_pos = -1
end_pos = 0
distance = 2
tracked_words = []
start_pos = 0
search_words = ['quick', 'fox', 'over']
highlights = []
@premchalmeti
premchalmeti / udemyCompleteCourse.js
Last active October 22, 2021 08:26
Udemy: Mark a course as completed/not completed given a flag
/*
Disclaimer: This is only used for educational purpose only and does not promote or encourage any illegal activies
*/
const markCourseCompleted = true;
var selectorExpr = `ul li input[type=checkbox]:${completeCourse?'not(:checked)':'checked'}`;
// first find right side sections container
var sectionsContainer = document.querySelector('[data-purpose="curriculum-section-container"]')
@premchalmeti
premchalmeti / elasticsearch_migration.py
Created August 28, 2021 17:22
Migrate elasticsearch mapping and data from source to target cluster
INDICES = [
"documents_index",
"england_geographies"
]
SRC_HOST = 'src_host'
SRC_PORT = 9200
DEST_HOST = 'dest_host'
@premchalmeti
premchalmeti / deprecated_decorator.py
Created August 28, 2021 13:54
Decorator to deprecate function with optional warning msg
def deprecated(msg=None):
def wrapped(func):
def inner(*args, **kwargs):
import warnings;
warnings.simplefilter('always', DeprecationWarning)
warnings.warn(
msg or '%s() is deprecated' % func.__name__,
category=DeprecationWarning
)
warnings.simplefilter('default', DeprecationWarning)
@premchalmeti
premchalmeti / terminal_auto_load_.env.sh
Last active August 28, 2021 17:23
Load folder specific `.env` file. Append below the code inside `.bashrc` file
function loadenv() {
if [ -f .env ];
then
source .env
fi
}
loadenv
function cd() {
@premchalmeti
premchalmeti / parsing_algorithm.txt
Created July 25, 2021 16:09
The CLISH XML parsing algortihm
0. START
1. Traverse the CmdTree node from the root CALL process_node(CmdTree.root)
2. START process_node:
If the CmdNode is not a leaf node then
2.1. Process all the child nodes of the current node first. CALL process_node(node)
2.2. After processing all child CmdNode. Set current node visibility
If all the childs are removed then
node.visible = False
Else
node.visible = True
@premchalmeti
premchalmeti / user_network.xml
Created July 12, 2021 15:32
user_network.xml file generated by clish parser
<?xml version="1.0" encoding="UTF-8"?>
<CLISH_MODULE xmlns="http://clish.sourceforge.net/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://clish.sourceforge.net/XMLSchema
http://clish.sourceforge.net/XMLSchema/clish.xsd">
<COMMAND name="network show"
help="Show currently added devices">
<ACTION>network show</ACTION>
</COMMAND>
@premchalmeti
premchalmeti / network_schema.json
Created July 12, 2021 15:31
Network schema file for clish parser
{
"version": "2.0.0",
"created_on": "2021-06-28T17:29:37.714428",
"module": {
"name": "network",
"source_file": "network.xml",
"commands": [
{
"cmd": "network show",
"meta": {
@premchalmeti
premchalmeti / admin_network.xml
Last active July 12, 2021 15:33
admin CLI network file for clish parser
<?xml version="1.0" encoding="UTF-8"?>
<CLISH_MODULE xmlns="http://clish.sourceforge.net/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://clish.sourceforge.net/XMLSchema
http://clish.sourceforge.net/XMLSchema/clish.xsd">
<!--=======================================================-->
<PTYPE name="STRING"
pattern="[^\-]+"
help="String"/>