Package: lerspy
LIGHTWEIGHT ENUMS, RUST-STYLED! (LERS)This module serves the purpose of doing exactly what the name implies:
providing a lightweight enum with little but base functionality, all whilst
attempting to replicate Rust enums' style, syntax, and features. In short, the
core principles listed are:Lightweight(very small module.)Simple(minimum amount of syntax, should feel like native python!)Rust-styled(features, syntax, behaviour. Only inspiration, not strict.)Creating an enum is straightforward, but highly customizable for improved user
experience. You can create an enum variant in a few styles to your preference,
with little to no functional difference between any options. Observe the three
different ways of defining an enum variant:classExample(Enum):Foo=evar()Bar={}Baz=()(figure 0)All three variants create identical enum variants. (with the exception that each
variant is considered its very own type, and thus, Foo != Bar != Baz.)The benefit of using LERS over the built-in enum module lies in just a few, but
important differences. One of these important differences is the support of
match statements!...# cont'd from figure 0foo=Example.Foo()matchfoo:caseExample.Foo:print('I am Foo!')caseExample.Bar:print('I am Bar!')caseExample.Baz:print('I am Baz!')case_:print("I am not an Example.")(figure 1)The expected output of course being "I am Foo!". Very nifty little feature
which denounces long if-elif-else chains, though you can still do that if
you so choose.assertExample.Foo()==Example.Foo(figure 2)Okay, I'll admit, alittlecursed. This is, however, what makes match
statements work the way they do, with so little extra user input.Another advantage of LERS is a very useful Rust enum feature, that being that
enum variants can have properties! In fact, LERS enums can have properties,
ordered and named like Rust, but can also have both at the same time! (unlike
Rust.)classAnimal(Enum):Insect=evar(leg_count=int,eye_count=int)SheepMatrix=evar(int,int,int)InsectMatrix=evar(int,int,int,leg_count=int,eye_count=int)(figure 3)A slightly silly example, though I'm sure you'll have more practical uses for
this module than sheep math.If you don't like the syntactical look of evar, good news! This module has a
pinch of sugar for a sweeter look, should you so choose. You can instead use
dictionaries and tuples to define an enum variant, although to define an enum
variant which has both ordered and named properties, evar will be necessary.classEmployee(Enum):Boss={'name':str,"salary":str}MindlessWorker=(int)(figure 4)Accessing the properties of an enum variant is also extremely straightforward.
Just make sure that the variant you are working on is correct, lest you raise
an exception.classExample(Enum):Foo={'prop':int}Bar=(int)Baz=evar(int,'prop':int)f=Example.Foo(prop=15)# properties MUST be assigned using keyword arguments!b=Example.Bar(30)z=Example.Baz(45,prop=60)assertf.prop==15assertb[0]==30assertz[0]==45andz.prop==60(figure 5)Properties can also be assigned just like how you would expect:...# cont'd from figure 5f.prop+=1b[0]=40Z[0]=-Z[0]z.prop*=z.prop(figure 6)Now for one more crucially important feature! Methods! You can call methods on
your enum variants, but where would you define them...? Why, the same place
you would in Rust. This is inspired by Rust, after all!classExample(Enum):Foo=evar()Bar={}Baz=(int)defwho_am_i(self):matchself:caseExample.Foo:print('I am Foo!')caseExample.Bar:print('I am Bar!')caseExample.Baz:print(f'I am Baz, and I have a value of{self[0]}!')Example.Foo().who_am_i()# outputs 'I am Foo!'Example.Bar().who_am_i()# outputs 'I am Bar!'Example.Baz(50).who_am_i()# outputs 'I am Baz, and I have a value of 50!'Enum variants are expected to have common behaviour afterall, so they cannot
each have their own functions. Instead, when creating a function within an enum,
all enum variants inherit this function.Like what you see?Thanks! Don't be afraid to make suggestions, fork, or contribute! This project
is licensed under MIT, and I encourage you to do the same with your project.
(though totally of your own discretion, not required as per the license's terms.)
Thank you for choosing LERS!