Python: Reimaging Constants

Search for a command to run...

No comments yet. Be the first to comment.
Transferring files between your local machine and a virtual machine (VM) is one of the most common tasks in tech. We often use scp because it comes natively installed and has similar utilization to ssh, but it is the clunkiest tool for the job. In th...

Alias Alchemy is a Deno-powered server that fetches aliases from a GitHub repository based on your query. Think of it as your instant alias spellbook, perfect for setting up new environments in seconds. ⚡ Usage: $ alias-alchemy.ra101.dev/?q=py,shell,...

Reader discretion is advised!A good Sus-Saturday to all. In this article, I drag and drown you with my paranoia about AI. Let the mass psychosis begin! What are AI hallucinations?AI hallucinations are incorrect or misleading results that AI models ge...

I recently joined a company, that follows squash-rebase flow here, and as my Git KT was going on, I figured I should build these commands before actually starting development, now that I have started development, these turned out to be quite handy! ...

Aim: To create an iterable blockchain, which could work with an extendable transaction class. Here the link GitHub repo! Blockchain in Brief: Blockchain is fundamentally a ledger maintaining records of transaction, This ledger is publicly distribute...

A different implementation of constant for python
I couldn't find a good implementation of constants, so created my own! Let's get started!
Do checkout the complete code at https://gist.github.com/ra101/aa27ff6e437f74ca56027a8c6b166882
How it Should work
class Links(ConstantClass):
GITHUB = "https://github.com/ra101"
WEB = "https://ra101.github.io/"
LINKS = "dev.ra.101@protonmail.com"
class Author(ConstantClass):
NAME = "〈 RA 〉"
WEB = Links.WEB
LINKS = Links
Now Links.GITHUB should return https://github.com/ra101 and Link.GITHUB = 'new_value' or Links.NEW_CONTANTS = NEW_VALUE both should return None, without assigning or creating new field
class ConstantMeta(type):
"""
Meta Class for Constant, How Constant class would behave
"""
def __setattribute__(self, *args, **kwargs):
# Override set method to make it immutable
pass
def __setattr__(self, *args, **kwargs):
# Override set method to make it immutable
pass
def __setitem__(self, *args, **kwargs):
# Override set method to make it immutable
pass
def __set__(self, *args, **kwargs):
# Override set method to make it immutable
pass
1.5) Lets add a external setter
def __force_set__(self, name, value):
# A external set method to make sure we can update value in __new__
return super().__setattr__(name, value)
__new__ to make the actual class def __new__(cls, clsname, bases, clsdict):
"""
adding __keys__, __values__ fields
and keys(), values() methods
"""
obj = super().__new__(cls, clsname, bases, clsdict)
obj.__force_set__("__values__", [])
obj.__force_set__("__keys__", [])
for key, val in vars(obj).items():
if not key.startswith("__"):
obj.__values__.append(val)
obj.__keys__.append(key)
obj.__force_set__("keys", lambda: obj.__keys__)
obj.__force_set__("values", lambda: obj.__values__)
return obj
2.5) Add below methods for polishing the class
def __getitem__(cls, key):
# adding __getitem__ to make objects "subscriptable"
return getattr(cls, key)
def __iter__(cls):
# In case of for loops
return zip(cls.__keys__, cls.__values__)
class ConstantClass(metaclass=ConstantMeta):
"""
Now this class can be inherited whenever required to make constants
"""
pass
Finally! We are Done! We Created a almost immutable class that could be used as constants
Input:
class Links(ConstantClass):
GITHUB = "https://github.com/ra101"
WEB = "https://ra101.github.io/"
LINKS = "dev.ra.101@protonmail.com"
class Author(ConstantClass):
NAME = "〈 RA 〉"
WEB = Links.WEB
LINKS = Links
Links.WEB = 'any_new_value'
print(f"\nLinks.WEB: {Links.WEB}")
print(f"\nAuthor.values(): {Author.values()}")
print(f"\ndict(Author): {dict(Author)}")
print("\nfor key in Author.LINKS.keys()")
for key in Author.LINKS.keys():
print(f'getattr(Author.LINKS, "{key}"): {getattr(Author.LINKS, key)}')
Author.values(): ['〈 RA 〉', 'https://ra101.github.io/', <main.ConstantClass object at 0x00000293A94768E0>]
dict(Author): {'NAME': '〈 RA 〉', 'WEB': 'https://ra101.github.io/', 'LINKS': <main.ConstantClass object at 0x00000293A94768E0>}
for key in Author.LINKS.keys() getattr(Author.LINKS, "GITHUB"): https://github.com/ra101 getattr(Author.LINKS, "WEB"): https://ra101.github.io/ getattr(Author.LINKS, "EMAIL"): dev.ra.101@protonmail.com
Do checkout the complete code at https://gist.github.com/ra101/aa27ff6e437f74ca56027a8c6b166882