Python Snippets

Sunday, March 24 2024

I always find myself writing these snippets over and over again. Maybe they already exist in some libraries I haven't discovered yet?

 

def make_path(*filepaths):
    return os.path.join(os.path.abspath(os.path.dirname(__file__)), *filepaths)
def get_random_float(N, M):
    assert N <= M
    return round(random.uniform(N, M), 2)
import datetime, pytz
def get_current_datetime(timezone_str='America/Toronto'):
    utc_now = datetime.datetime.now()
    timezone = pytz.timezone(timezone_str)
    now_tz = utc_now.replace(tzinfo=pytz.utc).astimezone(timezone)
    return now_tz

I'll add more here as time goes on, and I work on my projects. Feel free to share yours too, and I can include them in this post!

Comment
Optional
No comments yet...