Skip to content

Instantly share code, notes, and snippets.

View ahmedshahriar's full-sized avatar
🏠
Working from home

Ahmed Shahriar Sakib ahmedshahriar

🏠
Working from home
View GitHub Profile
@ahmedshahriar
ahmedshahriar / date_time_parse_from_few_social_media_post_cases.py
Last active November 28, 2020 14:59
Code snippet to calculate exact date-time from a text using date-time denoted numbers(1,2,3,4...) and words('week', 'a week', 'day', 'hour') . Applied for some social media posts texts which denotes time such as "1 hour ago" , '3 weeks ago' etc
from datetime import datetime, timedelta
import re
'''
examples cases:
4 DAYS AGO
1 HOUR AGO
Listed 4 days ago in East Wenatchee, WA
Listed a week ago in East Wenatchee, WA
@ahmedshahriar
ahmedshahriar / nba_player_stats_parser.py
Created January 26, 2021 11:22
This snippet will parse NBA player statistics from www.basketball-reference.com website using pandas
# sample Url https://www.basketball-reference.com/leagues/NBA_2021_per_game.html
import pandas as pd
def parse_data(year: str):
url = "https://www.basketball-reference.com/leagues/NBA_" + year + "_per_game.html"
parsed_df = pd.read_html(url, header=0)[0]
parsed_df = parsed_df.drop(parsed_df[parsed_df['Age'] == 'Age'].index) # to remove duplicate headers
parsed_df = parsed_df.fillna(0)
parsed_df = parsed_df.drop(['Rk'], axis=1) # to index(Rk column)
@ahmedshahriar
ahmedshahriar / pandas_to_csv_downloader.py
Created January 26, 2021 11:24
The snippet has a function which takes pandas DataFrame as an argument and convert it to csv file and return an href link to download the csv
import pandas as pd
import base64
def download_dataset(dataset):
csv = dataset.to_csv(index=False)
b64 = base64.b64encode(csv.encode()).decode() # strings to bytes conversions
href_link = f'<a href="data:file/csv;base64,{b64}" download="player_stats.csv">Download CSV File</a>'
return href_link
@ahmedshahriar
ahmedshahriar / run_streamlit_app_with_colab.ipynb
Created February 10, 2021 10:56
run_streamlit_app_with_colab.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@ahmedshahriar
ahmedshahriar / alphabet_filter_django_admin.py
Created February 10, 2021 19:01
Alphabet filter in Django admin
"""
Suppose you have a django model in models.py:
class Animal(models.Model):
animal_name = models.CharField(u'name', max_length=255)
to create an alphabetfilter in django admin panel you need to create a class which inherits
admin.SimpleListFilter in admin.py
here is the code snippet below for admin.py file
@ahmedshahriar
ahmedshahriar / faster_http_with_session.py
Created February 10, 2021 19:42
Faster HTTP Requests with Session
"""
requests with session gives faster results
the below code demonstrates a comparison in speed between requests with session vs requests not with session
"""
import requests
import datetime
from bs4 import BeautifulSoup
def scrape_no_session(x):
@ahmedshahriar
ahmedshahriar / check_dir_files_current_dir.py
Created February 13, 2021 19:11
This will print the folders and files inside a directory
# input
import os
for dirname, _, filenames in os.walk('/files/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
# Install qrcode https://pypi.org/project/qrcode/
# pip install qrcode[pil]
import qrcode
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
import pandas as pd
# using pandas datetime converter
df = pd.DataFrame(pd.Series(['2021-02-04T09:15:00+05:30', '2021-02-04T09:15:00+05:30', '2021-02-04T09:15:00+05:30']))
pd.to_datetime(df[0]).dt.date
pd.to_datetime(df[0]).dt.time
# using dateutil library
from dateutil.parser import parse
df[0].map(lambda x: parse(x).date())