Goodies of the Python Standard Library¶

The Python Standard Libary is part of your Python installation. It contains a wide range of packages which may be helpful while building your Python masterpieces. This notebook lists some of the commonly used packages and their main functionalities.

datetime for working with dates and times¶

In [1]:
import datetime as dt

local_now = dt.datetime.now()
print(f"local now: {local_now}")

utc_now = dt.datetime.utcnow()
print(f"utc now: {utc_now}")

# You can access any value separately:
print(
    f"{local_now.year} {local_now.month} {local_now.day} {local_now.hour} {local_now.minute} {local_now.second}"
)

print(f"date: {local_now.date()}")
print(f"time: {local_now.time()}")
local now: 2023-04-24 13:19:22.350525
utc now: 2023-04-24 10:19:22.350692
2023 4 24 13 19 22
date: 2023-04-24
time: 13:19:22.350525

strftime()¶

For string formatting the datetime

In [2]:
formatted1 = local_now.strftime("%Y/%m/%d-%H:%M:%S")
print(formatted1)

formatted2 = local_now.strftime("date: %Y-%m-%d time:%H:%M:%S")
print(formatted2)
2023/04/24-13:19:22
date: 2023-04-24 time:13:19:22

strptime()¶

For converting a datetime string into a datetime object

In [3]:
my_dt = dt.datetime.strptime("2000-01-01 10:00:00", "%Y-%m-%d %H:%M:%S")
print(f"my_dt: {my_dt}")
my_dt: 2000-01-01 10:00:00

timedelta¶

For working with time difference.

In [4]:
tomorrow = local_now + dt.timedelta(days=1)
print(f"tomorrow this time: {tomorrow}")

delta = tomorrow - local_now
print(f"tomorrow - now = {delta}")
print(f"days: {delta.days}, seconds: {delta.seconds}")
print(f"total seconds: {delta.total_seconds()}")
tomorrow this time: 2023-04-25 13:19:22.350525
tomorrow - now = 1 day, 0:00:00
days: 1, seconds: 0
total seconds: 86400.0

Working with timezones¶

In [5]:
import datetime as dt
from zoneinfo import ZoneInfo

naive_utc_now = dt.datetime.utcnow()
print(f"naive utc now: {naive_utc_now}, tzinfo: {naive_utc_now.tzinfo}")

# Localizing naive datetimes
UTC_TZ = ZoneInfo("UTC")
utc_now = naive_utc_now.replace(tzinfo=UTC_TZ)
print(f"utc now: {utc_now}, tzinfo: {utc_now.tzinfo}")

# Converting localized datetimes to different timezone
PARIS_TZ = ZoneInfo("Europe/Paris")
paris_now = utc_now.astimezone(PARIS_TZ)
print(f"Paris: {paris_now}, tzinfo: {paris_now.tzinfo}")

NEW_YORK_TZ = ZoneInfo("America/New_York")
ny_now = utc_now.astimezone(NEW_YORK_TZ)
print(f"New York: {ny_now}, tzinfo: {ny_now.tzinfo}")
naive utc now: 2023-04-24 10:19:22.431004, tzinfo: None
utc now: 2023-04-24 10:19:22.431004+00:00, tzinfo: UTC
Paris: 2023-04-24 12:19:22.431004+02:00, tzinfo: Europe/Paris
New York: 2023-04-24 06:19:22.431004-04:00, tzinfo: America/New_York

NOTE: If your project uses datetimes heavily, you may want to take a look at external libraries, such as Pendulum and Maya, which make working with datetimes easier for certain use cases.

logging¶

In [6]:
import logging

# Handy way for getting a dedicated logger for every module separately
logger = logging.getLogger(__name__)
logger.setLevel(logging.WARNING)

logger.debug("This is debug")
logger.info("This is info")
logger.warning("This is warning")
logger.error("This is error")
logger.critical("This is critical")
This is warning
This is error
This is critical

Logging expections¶

There's a neat exception function in logging module which will automatically log the stack trace in addition to user defined log entry.

In [7]:
try:
    path_calculation = 1 / 0
except ZeroDivisionError:
    logging.exception("All went south in my calculation")
ERROR:root:All went south in my calculation
Traceback (most recent call last):
  File "/var/folders/q4/_vnp7m_d625g5prgrz28h_fr0000gn/T/ipykernel_59325/3366486048.py", line 2, in <module>
    path_calculation = 1 / 0
ZeroDivisionError: division by zero

Formatting log entries¶

In [8]:
import logging

# This is only required for Jupyter notebook environment
from importlib import reload

reload(logging)

my_format = "%(asctime)s | %(name)-12s | %(levelname)-10s | %(message)s"
logging.basicConfig(format=my_format)

logger = logging.getLogger("MyLogger")

logger.warning("Something bad is going to happen")
logger.error("Uups, it already happened")
2023-04-24 13:19:22,459 | MyLogger     | WARNING    | Something bad is going to happen
2023-04-24 13:19:22,460 | MyLogger     | ERROR      | Uups, it already happened

Logging to a file¶

In [9]:
import logging
from pathlib import Path

# This is only required for Jupyter notebook environment
from importlib import reload

reload(logging)

logger = logging.getLogger("MyFileLogger")

# Let's define a file_handler for our logger
log_path = Path.cwd() / "my_log.txt"
file_handler = logging.FileHandler(log_path)

# And a nice format
formatter = logging.Formatter(
    "%(asctime)s | %(name)-12s | %(levelname)-10s | %(message)s"
)
file_handler.setFormatter(formatter)

logger.addHandler(file_handler)

# If you want to see it also in the console, add another handler for it
# logger.addHandler(logging.StreamHandler())

logger.warning("Oops something is going to happen")
logger.error("John Doe visits our place")

random for random number generation¶

In [10]:
import random

rand_int = random.randint(1, 100)
print(f"random integer between 1-100: {rand_int}")

rand = random.random()
print(f"random float between 0-1: {rand}")
random integer between 1-100: 22
random float between 0-1: 0.010400736609471939

If you need pseudo random numbers, you can set the seed for random. This will reproduce the output (try running the cell multiple times):

In [11]:
import random

random.seed(5)  # Setting the seed

# Let's print 10 random numbers
for _ in range(10):
    print(random.random())
0.6229016948897019
0.7417869892607294
0.7951935655656966
0.9424502837770503
0.7398985747399307
0.922324996665417
0.029005228283614737
0.46562265437810535
0.9433567169983137
0.6489745531369242

re for regular expressions¶

Searching occurences¶

In [12]:
import re

secret_code = "qwret 8sfg12f5 fd09f_df"
# "r" at the beginning means raw format, use it with regular expression patterns
search_pattern = r"(g12)"

match = re.search(search_pattern, secret_code)
print(f"match: {match}")
print(f"match.group(): {match.group()}")

numbers_pattern = r"[0-9]"
numbers_match = re.findall(numbers_pattern, secret_code)
print(f"numbers: {numbers_match}")
match: <re.Match object; span=(9, 12), match='g12'>
match.group(): g12
numbers: ['8', '1', '2', '5', '0', '9']

Variable validation¶

In [13]:
import re


def validate_only_lower_case_letters(to_validate):
    pattern = r"^[a-z]+$"
    return bool(re.match(pattern, to_validate))


print(validate_only_lower_case_letters("thisshouldbeok"))
print(validate_only_lower_case_letters("thisshould notbeok"))
print(validate_only_lower_case_letters("Thisshouldnotbeok"))
print(validate_only_lower_case_letters("thisshouldnotbeok1"))
print(validate_only_lower_case_letters(""))
True
False
False
False
False