source: doc/theses/jiada_liang_MMath/test.py @ d3b33d5

Last change on this file since d3b33d5 was 7568e5c, checked in by JiadaL <j82liang@…>, 3 months ago

Minor update on the thesis (add auto initialization and update future work

  • Property mode set to 100644
File size: 6.2 KB
Line 
1import random
2from enum import Enum, unique, auto, IntEnum, IntFlag, Flag
3from datetime import date
4from itertools import islice
5
6class OrderedEnum(Enum):
7        def __ge__(self, other):
8                if self.__class__ is other.__class__:
9                        return self.value >= other.value
10                return NotImplemented
11        def __gt__(self, other):
12                if self.__class__ is other.__class__:
13                        return self.value > other.value
14                return NotImplemented
15        def __le__(self, other):
16                if self.__class__ is other.__class__:
17                        return self.value <= other.value
18                return NotImplemented
19        def __lt__(self, other):
20                if self.__class__ is other.__class__:
21                        return self.value < other.value
22                return NotImplemented
23
24#class Week(Enum):
25#       Mon = 1; Tue = 2; Wed = 3; Thu = 10; Fri = 10; Sat = 16; Sun = 17
26class Week(OrderedEnum):
27        Mon = 1; Tue = 2; Wed = 3; Thu = 4; Fri = 5; Sat = 6; Sun = 0
28        def isWeekday(self):
29                return Week(self.value) <= Week.Fri
30        def isWeekend(self):
31                return Week.Fri < Week(self.value) 
32        @classmethod
33        def today(cls, date):
34                return cls(date.isoweekday())
35
36day : Week = Week.Tue
37print( "weekday:", day.isWeekday() )
38print( "weekend:", day.isWeekend() )
39print( "today:", Week.today(date.today()))
40
41print( Week.Thu.value == 4 )
42print( Week.Thu.name == "Thu" )
43print( Week( 4 ) == Week.Thu )
44print( Week["Thu"].value == 4 )
45
46if day <= Week.Fri :
47        print( "weekday" )
48match day:
49        case Week.Mon | Week.Tue | Week.Wed | Week.Thu | Week.Fri:
50                print( "weekday" )
51        case Week.Sat | Week.Sun:
52                print( "weekend" )
53
54for day in Week:
55        print( day.name, ":", day.value, end=" " )
56print( "" )
57for day in islice(Week,0,5):
58        print( day.name, ":", day.value, end=" " )
59print( "" )
60for day in islice(Week,5,7):
61        print( day.name, ":", day.value, end=" " )
62print( "" )
63for day in islice(Week,0,7,2):
64        print( day.name, ":", day.value, end=" " )
65print( "" )
66
67class WeekD(Enum): Mon = 1; Tue = 2; Wed = 3; Thu = 10; Fri = 10; Sat = 10; Sun = 10
68for day in WeekD:
69        print( "Dup", day.name, ":", day.value, end=" " )
70print( "" )
71for day in WeekD.__members__:
72        print( "Dup", day, ":", end=" " )
73print( "" )
74
75print( Week["Sat"] )
76print( Week( 3 ) )
77print( Week["Thu"] )
78print( str( Week.Thu ), Week.Thu )
79print( Week.Thu.name, Week.Thu.value )
80print( isinstance(Week.Fri, Week) )
81
82class WeekE(OrderedEnum): pass
83class WeekDay(WeekE): Mon = 1; Tue = 2; Wed = 3; Thu = 4; Fri = 5;
84class WeekEnd(WeekE): Sat = 6; Sun = 7
85print( "inheritance" )
86print( type(WeekE) )
87dayI : WeekE = WeekDay.Fri
88print( type(dayI), dayI )
89dayI = WeekEnd.Sat
90print( type(dayI), dayI )
91
92for day in WeekE:
93        print( day.name, ":", day.value, end=" " )
94print( "" )
95for day in WeekDay:
96        print( day.name, ":", day.value, end=" " )
97print( "" )
98for day in WeekEnd:
99        print( day.name, ":", day.value, end=" " )
100print( "" )
101
102class Week2(Enum): Mon = 1; Tue = 2; Wed = 3; Thu = 10; Fri = auto(); Sat = 4; Sun = auto()
103for day in Week2:
104        print( day.name, ":", day.value, end=" " )
105print( "" )
106
107#@unique
108#class DupVal(Enum): ONE = 1; TWO = 2; THREE = 3; FOUR = 3
109
110class RGB(Enum): Red = 1; Green = 2; Blue = 3
111for c in RGB:
112        print( c.name, ":", c.value )
113
114day = RGB.Red
115print( "X", day )
116day : Week = RGB.Red
117print( "X", day )
118
119class WeekF(Flag): Mon = 1; Tue = 2; Wed = 4; Thu = auto(); Fri = 16; Sat = 32; Sun = 64; \
120      Weekday = Mon | Tue | Wed | Thu | Fri; \
121      Weekend = Sat | Sun
122print( f"0x{repr(WeekF.Weekday.value)} 0x{repr(WeekF.Weekend.value)}" )
123day : WeekF = WeekF.Mon | WeekF.Tue
124print( type(day) )
125for day in WeekF:
126        print( f"WeekF {day.name}: {day.value}", end=" " )
127print( "" )
128weekday = WeekF.Weekday
129for day in WeekF.Mon:
130        print( f"WeekF.Mon {day.name}:"
131           f" {day.value}", end=" " )
132print( "" )
133for day in weekday:
134        print( f"weekday {day.name}:"
135           f" {day.value}", end=" " )
136print( "" )
137weekend = WeekF.Weekend
138for day in weekend:
139        print( f"weekend {day.name}:"
140           f" {day.value}", end=" " )
141print( "" )
142
143class WeekA(Flag): Mon = auto(); Tue = auto(); Wed = auto(); Thu = auto(); Fri = auto();  \
144                                                        Sat = auto(); Sun = auto(); Weekend = Sat | Sun
145for d in WeekA:
146        print( f"{d.name}: {d.value}", end=" ")
147print( "" )
148print(WeekA.Weekend)
149
150class RGBa(Enum): RED = auto(); BLUE = auto(); GREEN = auto()
151for c in RGBa:
152        print( f"{c.name} {c.value}" )
153print( RGBa(1), RGBa(3) )
154print( RGBa["RED"], RGBa["GREEN"] )
155member = RGBa.RED
156print( f"{member.name} {member.value}" )
157
158class Shape(Enum): SQUARE = 2; DIAMOND = 1; CIRCLE = 3; ALIAS_FOR_SQUARE = 2
159print(Shape.SQUARE)
160
161print( "" )
162class Diff(Enum): Int = 1; Float = 3.5; Str = "ABC"
163diffval : Diff = Diff.Int
164match diffval:
165        case Diff.Int:
166                print( "diffval", diffval.value )
167        case Diff.Float:
168                print( "diffval", diffval.value )
169        case Diff.Str:
170                print( "diffval", diffval.value )
171for i in Diff:
172        print( f"Diff type {type(i)}, {i}, {i.name}, {i.value} : " )
173print( "\n" )
174
175def by_position(enum_type, position):
176        for index, value in enumerate(enum_type):
177                if position == index: return value
178        raise Exception("by_position out of range")
179
180class Planet(Enum):
181        #                  mass (kg)  radius (km)
182        MERCURY = ( 0.330E24, 2.4397E6 )
183        VENUS   = ( 4.869E24, 6.0518E6 )
184        EARTH   = ( 5.976E24, 6.3781E6 )
185        MOON    = ( 7.346E22, 1.7380E6 ) # not a planet
186        MARS    = ( 0.642E24, 3.3972E6 )
187        JUPITER = ( 1898.E24, 71.492E6 )
188        SATURN  = ( 568.8E24, 60.268E6 )
189        URANUS  = ( 86.86E24, 25.559E6 )
190        NEPTUNE = ( 102.4E24, 24.746E6 )
191        def __init__(self, mass, radius):
192                self.mass = mass                # in kilograms
193                self.radius = radius    # in meters
194        def surfaceGravity(self):
195                # universal gravitational constant  (m3 kg-1 s-2)
196                G = 6.67300E-11
197                return G * self.mass / (self.radius * self.radius)
198        def surfaceWeight(self, otherMass):
199                return otherMass * self.surfaceGravity()
200
201class Cats(Enum):
202        pass
203
204
205earthWeight : float = 100
206earthMass : float = earthWeight / ( Planet.EARTH.surfaceGravity() )
207
208p = by_position( Planet, random.randrange(8) ) # select a random orbiting body
209match p:
210        case Planet.MERCURY | Planet.VENUS | Planet.EARTH | Planet.MARS:
211                print( f"{p.name} is a rocky planet" )
212        case Planet.JUPITER | Planet.SATURN | Planet.URANUS | Planet.NEPTUNE:
213                print( f"{p.name} is a gas-giant planet" )
214        case _:
215                print( f"{p.name} is not a planet" )
216
217for p in Planet:
218        print( f"Your weight on {p.name} is {p.surfaceWeight(earthMass):1.1f} kg" )
219
220# Local Variables: #
221# tab-width: 4 #
222# compile-command: "python3.13 test.py" #
223# End: #
Note: See TracBrowser for help on using the repository browser.