
I am developer/code-reviewer/debugger/bug-fixer/architect/teacher/builder from dubai, uae
Search for a command to run...

I am developer/code-reviewer/debugger/bug-fixer/architect/teacher/builder from dubai, uae
No comments yet. Be the first to comment.
with T.A.C.T

Isolation is part of a database's ACID guarantees. It ensures that concurrent transactions don't affect each other. The goal is to maintain the state of the data as if transactions are run serially. H

You Should Tell Yourself

A URL shortener is a proxy service that provides a mapping between the short and full representation of a URL. The short URL has the advantage of being small. The service can also provide useful analy

Listening to the Mel Robbins's podcast on regrets was a 'ear'-opener. One can have reqrets of action or inaction. 4 types of reqrets foundation - should've done the work boldness - should've taken t

__getattribute__ is always called for attribute lookup. However if not found, __getattr__ is called.
We can leverage this mechanism falling back to getting attributes from anything else.
Below we use a dict for look-ups and if it's not found, raise the expected AttributeError
import dataclasses
import logging
logging.basicConfig(level='INFO')
logger = logging.getLogger(__name__)
@dataclasses.dataclass
class DictAttrib:
# class attribute
name: str
# runtime user defined attributes
data: dict = dataclasses.field(default_factory=dict)
def __str__(self):
return f'{self.name}| {self.data}'
def __getattr__(self, name):
"""called when attribute not found after __getattribute__"""
try:
# check if in dict
return self.data[name]
except KeyError:
# to avoid nested exception error
pass
raise AttributeError(name)
if __name__ == '__main__':
foo = DictAttrib('foo')
bar = DictAttrib('bar', data={'bar': 'hello'})
try:
logger.info(f'{foo}')
logger.info(f'{foo.bar}')
except AttributeError:
logger.exception('failure')
try:
logger.info(f'{bar}')
logger.info(f'{bar.bar}')
logger.info(f'{bar.a}')
except AttributeError:
logger.exception('failure')