Skip to content

Instantly share code, notes, and snippets.

@yashbonde
Last active August 5, 2021 10:18
Show Gist options
  • Save yashbonde/62df9d16858a43775c22a6af00a8d707 to your computer and use it in GitHub Desktop.
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!
# 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()
@yashbonde
Copy link
Author

yashbonde commented May 20, 2021

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 reboot
  • get_files_in_folder: find all the files of particular extension in the folder
  • json_load: Load json files that can have commits starting with //
  • folder: get the folder of the input path
  • Hashlib: don't you sometimes feel like hashing should work right out of the box

More will be added! Some WIP methods:

import re
alphabet = re.compile(r'^[a-zA-Z]+$')

def is_word(token):
  if len(token) == 1 and alphabet.match(token) == None:
    return False
  return True

Honourable mentions:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment