Skip to content

Instantly share code, notes, and snippets.

View blacksmithop's full-sized avatar
😉
Preparing

Abhinav blacksmithop

😉
Preparing
View GitHub Profile
@blacksmithop
blacksmithop / iterate-walrus.py
Created August 10, 2022 12:03
Iteration using walrus operator
items = list(range(1,11))
i_items = iter(items)
while el := next(i_items):
print(el)
@blacksmithop
blacksmithop / ouroboros.py
Created August 10, 2022 11:54
Infinite iterator
from itertools import chain
def gen_iterables():
while True:
for i in range(1, 6): # memory equivalent to that used by `range` is consumed at any moment
yield range(i)
gen = chain.from_iterable(gen_iterables())
for i in range(20):
@blacksmithop
blacksmithop / drop_duplicate_if_null.py
Last active August 3, 2022 09:08
Drop duplicate row if column is null
>>> df1
name flag
0 1 1
1 2 1
2 3 1
3 4 1
>>> df2
name flag
0 3 NaN
1 4 NaN
@blacksmithop
blacksmithop / closest_key_in_dict.py
Created July 19, 2022 08:43
Get key with closest match* when accessing a dict item by key, otherwise return the 'first' key
class mdict(dict):
def __missing__(self, key):
keys = list(self.keys())
return next(in_key for in_key in keys if key in in_key) or keys[0]
if __name__ == "__main__":
item = mdict()
item["abc"] = 123
@blacksmithop
blacksmithop / readme.md
Last active July 6, 2022 09:00
ISPOR API

ISPOR

API

URL

https://isporsearch302.aws.mtxgp.net//searchblox/servlet/SearchServlet?query=*&default=AND&xsl=json&facet=on&col=2&pagesize=10&f.events.filter=2019-11%2C%20ISPOR%20Europe%202019%2C%20Copenhagen%2C%20Denmark&page=1&sort=relevance&facet.field=citable&f.citable.size=1000&facet.field=categories&f.categories.size=1000&facet.field=areaofstudy&f.areaofstudy.size=1000&facet.field=disease&f.disease.size=1000&facet.field=events&f.events.size=1000

@blacksmithop
blacksmithop / switch_case_list_elements.py
Created June 21, 2022 06:15
python 3.10 switch case example list elements
# Python 3.10 switch case example for a list
def file_handler_v1(command):
match command.split():
case ["show"]:
print("List all files and directories: ")
# code to list files
case ["remove", *files]:
print("Removing files: {}".format(files))
# code to remove files
case _:
@blacksmithop
blacksmithop / Search my gists.md
Created June 13, 2022 12:30 — forked from santisbon/Search my gists.md
How to search gists

Enter this in the search box along with your search terms:

Get all gists from the user santisbon.
user:santisbon

Find all gists with a .yml extension.
extension:yml

Find all gists with HTML files.
language:html

@blacksmithop
blacksmithop / aggregate_percentage.py
Last active June 8, 2022 11:25
Group a pandas Dataframe by a column with percentage data
from collections import Counter
import pandas as pd
data = [["A", 1], ["A", 2], ["B", 5], ["B", 5], ["B", 4], ["C", 6]]
df = pd.DataFrame(data, columns=["name", "id"])
id_all = df.groupby("name")["id"].agg(list)
id_unique = df.groupby("name")["id"].agg(set)
@blacksmithop
blacksmithop / main.py
Created May 27, 2022 10:22
Sanitized file names from excel columns, compiled into a single json file
from os import listdir, path, remove, walk
from json import dump, load
from re import sub
dir = "data"
def createJSON():
fileName = input("> Title: ")