Mixed Compound Data

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import sys
import unittest
from dataclasses import dataclass
from typing import *
sys.setrecursionlimit(10**6)

## we can combine mixed and compound
## to get mixed compound data, a sum of products

Periodical = Union['Newspaper','Magazine']

@dataclass(frozen=True)
class Newspaper:
    paper_width : float # in cm
    paper_height : float # in cm
    name : str
    city : str

@dataclass(frozen=True)
class Magazine:
    publisher_id : int
    title : str
    first_year : int

this_year : int = 2025

# is this perodical trustworthy?
# NB THESE RULES ARE SILLY AND PREPOSTEROUS
def is_trustworthy(p : Periodical) -> bool:
    match p:
        case Newspaper(w,h,_,_):
            return w * h > 150
        case Magazine(_,_,first_year):
            return (this_year - first_year) > 1000


class MyTests(unittest.TestCase):
    def test_trustworthy(self):
        self.assertEqual(False,is_trustworthy(Newspaper(15,20,"The Building Bulletin","Oklahoma City")))
        self.assertEqual(True,is_trustworthy(Newspaper(15,2000,"The Verylong News","Tampa")))
        self.assertEqual(False,is_trustworthy(Magazine(2412,"Waggo Maggo",1974)))
        self.assertEqual(True,is_trustworthy(Magazine(21098,"Fauna of the Ancient World",-48700000)))