1 | import random |
---|
2 | from enum import Enum, unique, auto, IntEnum, IntFlag, Flag |
---|
3 | from datetime import date |
---|
4 | from itertools import islice |
---|
5 | |
---|
6 | class 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 |
---|
26 | class 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 | |
---|
36 | day : Week = Week.Tue |
---|
37 | print( "weekday:", day.isWeekday() ) |
---|
38 | print( "weekend:", day.isWeekend() ) |
---|
39 | print( "today:", Week.today(date.today())) |
---|
40 | |
---|
41 | print( Week.Thu.value == 4 ) |
---|
42 | print( Week.Thu.name == "Thu" ) |
---|
43 | print( Week( 4 ) == Week.Thu ) |
---|
44 | print( Week["Thu"].value == 4 ) |
---|
45 | |
---|
46 | if day <= Week.Fri : |
---|
47 | print( "weekday" ) |
---|
48 | match 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 | |
---|
54 | for day in Week: |
---|
55 | print( day.name, ":", day.value, end=" " ) |
---|
56 | print( "" ) |
---|
57 | for day in islice(Week,0,5): |
---|
58 | print( day.name, ":", day.value, end=" " ) |
---|
59 | print( "" ) |
---|
60 | for day in islice(Week,5,7): |
---|
61 | print( day.name, ":", day.value, end=" " ) |
---|
62 | print( "" ) |
---|
63 | for day in islice(Week,0,7,2): |
---|
64 | print( day.name, ":", day.value, end=" " ) |
---|
65 | print( "" ) |
---|
66 | |
---|
67 | class WeekD(Enum): Mon = 1; Tue = 2; Wed = 3; Thu = 10; Fri = 10; Sat = 10; Sun = 10 |
---|
68 | for day in WeekD: |
---|
69 | print( "Dup", day.name, ":", day.value, end=" " ) |
---|
70 | print( "" ) |
---|
71 | for day in WeekD.__members__: |
---|
72 | print( "Dup", day, ":", end=" " ) |
---|
73 | print( "" ) |
---|
74 | |
---|
75 | print( Week["Sat"] ) |
---|
76 | print( Week( 3 ) ) |
---|
77 | print( Week["Thu"] ) |
---|
78 | print( str( Week.Thu ), Week.Thu ) |
---|
79 | print( Week.Thu.name, Week.Thu.value ) |
---|
80 | print( isinstance(Week.Fri, Week) ) |
---|
81 | |
---|
82 | class WeekE(OrderedEnum): pass |
---|
83 | class WeekDay(WeekE): Mon = 1; Tue = 2; Wed = 3; Thu = 4; Fri = 5; |
---|
84 | class WeekEnd(WeekE): Sat = 6; Sun = 7 |
---|
85 | print( "inheritance" ) |
---|
86 | print( type(WeekE) ) |
---|
87 | dayI : WeekE = WeekDay.Fri |
---|
88 | print( type(dayI), dayI ) |
---|
89 | dayI = WeekEnd.Sat |
---|
90 | print( type(dayI), dayI ) |
---|
91 | |
---|
92 | for day in WeekE: |
---|
93 | print( day.name, ":", day.value, end=" " ) |
---|
94 | print( "" ) |
---|
95 | for day in WeekDay: |
---|
96 | print( day.name, ":", day.value, end=" " ) |
---|
97 | print( "" ) |
---|
98 | for day in WeekEnd: |
---|
99 | print( day.name, ":", day.value, end=" " ) |
---|
100 | print( "" ) |
---|
101 | |
---|
102 | class Week2(Enum): Mon = 1; Tue = 2; Wed = 3; Thu = 10; Fri = auto(); Sat = 4; Sun = auto() |
---|
103 | for day in Week2: |
---|
104 | print( day.name, ":", day.value, end=" " ) |
---|
105 | print( "" ) |
---|
106 | |
---|
107 | #@unique |
---|
108 | #class DupVal(Enum): ONE = 1; TWO = 2; THREE = 3; FOUR = 3 |
---|
109 | |
---|
110 | class RGB(Enum): Red = 1; Green = 2; Blue = 3 |
---|
111 | for c in RGB: |
---|
112 | print( c.name, ":", c.value ) |
---|
113 | |
---|
114 | day = RGB.Red |
---|
115 | print( "X", day ) |
---|
116 | day : Week = RGB.Red |
---|
117 | print( "X", day ) |
---|
118 | |
---|
119 | class 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 |
---|
122 | print( f"0x{repr(WeekF.Weekday.value)} 0x{repr(WeekF.Weekend.value)}" ) |
---|
123 | day : WeekF = WeekF.Mon | WeekF.Tue |
---|
124 | print( type(day) ) |
---|
125 | for day in WeekF: |
---|
126 | print( f"WeekF {day.name}: {day.value}", end=" " ) |
---|
127 | print( "" ) |
---|
128 | weekday = WeekF.Weekday |
---|
129 | for day in WeekF.Mon: |
---|
130 | print( f"WeekF.Mon {day.name}:" |
---|
131 | f" {day.value}", end=" " ) |
---|
132 | print( "" ) |
---|
133 | for day in weekday: |
---|
134 | print( f"weekday {day.name}:" |
---|
135 | f" {day.value}", end=" " ) |
---|
136 | print( "" ) |
---|
137 | weekend = WeekF.Weekend |
---|
138 | for day in weekend: |
---|
139 | print( f"weekend {day.name}:" |
---|
140 | f" {day.value}", end=" " ) |
---|
141 | print( "" ) |
---|
142 | |
---|
143 | class WeekA(Flag): Mon = auto(); Tue = auto(); Wed = auto(); Thu = auto(); Fri = auto(); \ |
---|
144 | Sat = auto(); Sun = auto(); Weekend = Sat | Sun |
---|
145 | for d in WeekA: |
---|
146 | print( f"{d.name}: {d.value}", end=" ") |
---|
147 | print( "" ) |
---|
148 | print(WeekA.Weekend) |
---|
149 | |
---|
150 | class RGBa(Enum): RED = auto(); BLUE = auto(); GREEN = auto() |
---|
151 | for c in RGBa: |
---|
152 | print( f"{c.name} {c.value}" ) |
---|
153 | print( RGBa(1), RGBa(3) ) |
---|
154 | print( RGBa["RED"], RGBa["GREEN"] ) |
---|
155 | member = RGBa.RED |
---|
156 | print( f"{member.name} {member.value}" ) |
---|
157 | |
---|
158 | class Shape(Enum): SQUARE = 2; DIAMOND = 1; CIRCLE = 3; ALIAS_FOR_SQUARE = 2 |
---|
159 | print(Shape.SQUARE) |
---|
160 | |
---|
161 | print( "" ) |
---|
162 | class Diff(Enum): Int = 1; Float = 3.5; Str = "ABC" |
---|
163 | diffval : Diff = Diff.Int |
---|
164 | match 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 ) |
---|
171 | for i in Diff: |
---|
172 | print( f"Diff type {type(i)}, {i}, {i.name}, {i.value} : " ) |
---|
173 | print( "\n" ) |
---|
174 | |
---|
175 | def 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 | |
---|
180 | class 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 | |
---|
201 | class Cats(Enum): |
---|
202 | pass |
---|
203 | |
---|
204 | |
---|
205 | earthWeight : float = 100 |
---|
206 | earthMass : float = earthWeight / ( Planet.EARTH.surfaceGravity() ) |
---|
207 | |
---|
208 | p = by_position( Planet, random.randrange(8) ) # select a random orbiting body |
---|
209 | match 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 | |
---|
217 | for 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: # |
---|