BlockLune's Blog

Home Tags About

Python libraries mentioned in CS50P

published at 2023-07-07
python cs50p pip

This is a list of some Python libraries mentioned in CS50P 2022.

WEEK 4: Libraries

random

See Random

Ex.

import random

coin = random.choice(["heads", "tails"])
print(coin)

number = random.randint(1, 10)
print(number)

cards = ["jack", "queen", "king"]
random.shuffle(cards)
for card in cards:
  print(card)

statistics

See Statistics

Ex.

The mean function takes a list of values and print the average of these values.

import statistics

print(statistics.mean([100, 90]))

sys

See Command-Line Arguments

cowsay

This library generates ascii arts like this:

  ___________
< Hello World >
  ===========
                \
                 \
                   ^__^
                   (oo)\_______
                   (__)\       )\/\
                       ||----w |
                       ||     ||


See Packages

emoji

This library can convert some specified words to emoji. For instance, :thumbsup: can be converted to đź‘Ť.

See emojize

Ex.

import emoji

words = input("Input: ")
print(f"Output: {emoji.emojize(words)}")

pyfiglet

This library can make large letters out of ordinary text:

 _ _ _          _   _     _
| (_) | _____  | |_| |__ (_)___
| | | |/ / _ \ | __| '_ \| / __|
| | |   <  __/ | |_| | | | \__ \
|_|_|_|\_\___|  \__|_| |_|_|___/

See Frank, lan and Glen’s Letters

inflect

This library can correctly generate plurals, singular nouns, ordinals, indefinite articles; convert numbers to words.

See Adieu, Adieu

requests

See Bitcoin Price Index

pylint

See Style

black

See Style

WEEK 5: Unit Tests

pytest

See Pytest

Ex.1.

# fuel.py
def main():
    while True:
        fraction = input("Fraction: ")
        try:
            percentage = convert(fraction)
            break
        except ValueError:
            pass
        except ZeroDivisionError:
            pass
    print(gauge(percentage))


def convert(fraction):
    x, y = [int(bit) for bit in fraction.split("/")]
    if x > y and y != 0:
        raise ValueError
    if y == 0:
        raise ZeroDivisionError
    return int(x / y * 100)

def gauge(percentage):
    if percentage <= 1:
        return "E"
    elif percentage >= 99:
        return "F"
    else:
        return f"{percentage}%"


if __name__ == "__main__":
    main()
# test_fuel.py
from fuel import convert, gauge
import pytest

def test_covert():
    assert convert("0/4") == 0
    assert convert("1/4") == 25
    assert convert("3/4") == 75
    assert convert("4/4") == 100
    with pytest.raises(ValueError):
        assert convert("cat/dog")
        assert convert("three/four")
        assert convert("1.5/3")
        assert convert("4/3")
    with pytest.raises(ZeroDivisionError):
        assert convert("4/0")

def test_guage():
    assert gauge(0) == "E"
    assert gauge(1) == "E"
    assert gauge(25) == "25%"
    assert gauge(99) == "F"
    assert gauge(100) == "F"

Ex.2.

# seasons.py
from datetime import date
import re
import sys
import inflect


DATE_REGEXP = r"^(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)$"


def main():
    date_str = input("Date of Birth: ")
    print(get_ans(date_str))


def get_ans(date_str):
    if is_valid_date(date_str):
        birthday = date.fromisoformat(date_str)
    else:
        print("Invalid date")
        sys.exit(1)
    today = date.today()
    days = (today - birthday).days
    p = inflect.engine()
    return (p.number_to_words(days * 24 * 60, andword="")).capitalize() + " minutes"


def is_valid_date(s):
    return re.search(DATE_REGEXP, s)


if __name__ == "__main__":
    main()
# test_seasons.py
from seasons import get_ans
import pytest

def test_correctness():
    assert get_ans("2022-07-05") == "Five hundred twenty-five thousand, six hundred minutes"
    assert get_ans("2021-07-05") == "One million, fifty-one thousand, two hundred minutes"

def test_invalid_input():
    with pytest.raises(SystemExit):
        get_ans("January 1, 1999")

WEEK 6: File I/O

csv

See csv

pillow

This is a popular Python library that works well with image files.

See Binary Files and PIL

WEEK 7: Regular Expressions

re

See Notes

WEEK 8: Object-Oriented Programming

fpdf2

See CS50 Shirtificate

WEEK 9: Et Cetera

mypy

See Type Hints

argparse

See argparse

pyttsx3

See This was CS50!