Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python typeddict

"""
TypeDict allows to declare a structure that maps
the keys of a dictionary to their corresponding 
data types. 
Mypy would issue no error messages when applied 
to the code below. 
"""

from typing import TypedDict

class SalesSummary(TypedDict):
    sales: int
    country: str
    product_codes: list[str]


def get_sales_summary() -> SalesSummary:
    """Return summary for yesterday’s sales."""
    return {
        "sales": 1_000,
        "country": "UK",
        "product_codes": ["SUYDT"],
    }

# Mypy knows that sales variable is an int
# As such, it does not complain when it processes code below
sales_summary = get_sales_summary()
sales = sales_summary["sales"]
print("Sales per hour:", round(sales / 24, 2))
Comment

PREVIOUS NEXT
Code Example
Python :: drop duplicate rows pandas except nan 
Python :: python list of all characters 
Python :: how to import pygame 
Python :: right angle triangle in python 
Python :: Get the Type 
Python :: load saved model tensorflow 
Python :: using while loop in python taking input until it matches the desired answer 
Python :: datetimes to day of year python 
Python :: discord.py check if message has certain reaction 
Python :: how to create a new virtualenv 
Python :: python sqlite dict 
Python :: sort the dictionary in python 
Python :: boto3 upload file to s3 
Python :: python weekday 
Python :: get list of files in directory python 
Python :: How to install XGBoost package in python on Windows 
Python :: sort df by column 
Python :: create models in django 
Python :: pandas drop na in column 
Python :: show image python 
Python :: write text in list to text file python 
Python :: declare numpy zeros matrix python 
Python :: ipynb to py online 
Python :: python getter decorator 
Python :: python print for loop one line 
Python :: remove comments from python file 
Python :: how to restart program in python 
Python :: pyspark check all columns for null values 
Python :: how to pair up two lists in python 
Python :: django check if queryset is empty 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =