Last active
August 5, 2021 10:18
-
-
Save yashbonde/62df9d16858a43775c22a6af00a8d707 to your computer and use it in GitHub Desktop.
This file has many functions I use daily. As a gist so I can download any where!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# whole bunch of utility functions I use in day to day | |
# - @yashbonde / https://github.com/yashbonde | |
# | |
# Now this is a simple script and cannot be loaded like a package | |
# so you'll need to import it. This is how you can do it | |
""" | |
try: | |
from daily import * | |
except ImportError as e: | |
import requests | |
x = requests.get(<url_to_raw>).content | |
with open("daily.py", "wb") as f: | |
f.write(x) | |
from daily import * | |
""" | |
import logging | |
logging.basicConfig(level="INFO") | |
def log(x: str, *args): | |
x = str(x) | |
for y in args: | |
x += " " + str(y) | |
logging.info(x) | |
def fetch(url): | |
# efficient loading of URLS | |
import os, tempfile, hashlib, requests | |
fp = os.path.join(tempfile.gettempdir(), hashlib.md5(url.encode('utf-8')).hexdigest()) | |
if os.path.isfile(fp) and os.stat(fp).st_size > 0: | |
with open(fp, "rb") as f: | |
dat = f.read() | |
else: | |
print("fetching", url) | |
dat = requests.get(url).content | |
with open(fp+".tmp", "wb") as f: | |
f.write(dat) | |
os.rename(fp+".tmp", fp) | |
return dat | |
def get_files_in_folder(folder, ext = [".txt"]): | |
# this method is faster than glob | |
import os | |
all_paths = [] | |
for root,_,files in os.walk(folder): | |
for f in files: | |
for e in ext: | |
if f.endswith(e): | |
all_paths.append(os.path.join(root,f)) | |
return all_paths | |
def json_load(path): | |
# load any JSON like file with comment strings '//' | |
# json files should have description strings so it's more friendly | |
# but it won't load with json.load(f) so read the file, remove the | |
# comments and json.loads(text) | |
import json, re | |
with open(path, 'r') as f: | |
text = f.read() | |
text = re.sub(r"\s*(\/{2}.*)\n", "\n", text) | |
config = json.loads(text) | |
return config | |
def folder(x): | |
# get the folder of this file path | |
import os | |
return os.path.split(os.path.abspath(x))[0] | |
# class to handle hashing methods | |
class Hashlib: | |
# local imports don't work! | |
# import hashlib | |
def sha256(x): | |
import hashlib | |
x = x if isinstance(x, bytes) else x.encode("utf-8") | |
return hashlib.sha256(x).hexdigest() | |
def md5(x): | |
import hashlib | |
x = x if isinstance(x, bytes) else x.encode("utf-8") | |
return hashlib.md5(x).hexdigest() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Daily Functions
These are just code snippets found from the almighty internet that I use almost daily (also pure python!). Documentation:
fetch
: Load any url and also cache it as tempfile so it gets removed on rebootget_files_in_folder
: find all the files of particular extension in the folderjson_load
: Load json files that can have commits starting with//
folder
: get the folder of the input pathHashlib
: don't you sometimes feel like hashing should work right out of the boxMore will be added! Some WIP methods:
Honourable mentions:
install.sh