Skip to content

Instantly share code, notes, and snippets.

We couldn’t find that file to show.
@mohitsh
mohitsh / get_now_ist.py
Last active March 14, 2021 14:09
get now() in IST
from pytz import timezone
now_ist = datetime.now(tz=timezone("Asia/Kolkata"))
# set timezone
datetime_object.astimezone(timezone("Asia/Kolkata"))
@mohitsh
mohitsh / concurrency.py
Last active November 23, 2020 17:35
multithreading in python using concurrent.futures module
import concurrent.futures
import multiprocessing
processing_thread_count =processing_thread_count = multiprocessing.cpu_count() -1
with concurrent.futures.ThreadPoolExecutor(max_workers=processing_thread_count) as executor:
future_to_response = {executor.submit(function, arg1, arg2): i for i, msg in enumerate(a_list)}
for future in concurrent.futures.as_completed(future_to_response):
response = future_to_resonse[future]
import concurrent.futures
processing_thread_count = multiprocessing.cpu_count() - 1
with concurrent.futures.ThreadPoolExecutor(max_workers=processing_thread_count) as executor:
future_to_response = {executor.submit(function, arg1, arg2): i for i, msg in enumerate(a_list)}
for future in concurrent.futures.as_completed(future_to_response):
response = future_to_resonse[future]
@mohitsh
mohitsh / plotting.py
Last active July 1, 2020 08:23
multiple plots using matplotlib
import matplotlib.pyplot as plt
plt.plot(x, y, "o")
plt.plot(x, y_curve)
plt.show()
#################
def plot(x, y, title="Plot Title", xlabel='Xaxis', ylabel='Yaxis', dpi=100):
plt.figure(figsize=(16, 5), dpi=dpi)
plt.scatter(x, y, color='tab:red')
@mohitsh
mohitsh / round_time_stamp_column.py
Created June 2, 2020 12:00
round datafrme timestamp column to minute level
df["time_stamp"].dt.floor('min')
@mohitsh
mohitsh / read_write_file.py
Last active April 13, 2020 04:59
file reading using native python function
with open(file_path, 'r') as f:
token = f.readline()
with open(file_path, 'w', encoding='utf-8') as f:
f.write(string_to_write)
@mohitsh
mohitsh / read_large_csv_file.py
Created April 2, 2020 09:12
reading large csv files in pandas with filter
import pandas as pd
iter_csv = pd.read_csv('file.csv', iterator=True, chunksize=1000)
df = pd.concat([chunk[chunk['field'] > constant] for chunk in iter_csv])
@mohitsh
mohitsh / python_post_request
Created March 25, 2020 16:06
making post request using Python's Requests module
headers = {'header-key': 'header-val'}
payload = {'key1':'val1', 'key2':'val2', 'key3':'val3'}
r = requests.post(endpoint, headers=headers, data=payload)
ipython kernel install --user --name=venv