Skip to content

Instantly share code, notes, and snippets.

@mjmikulski
Last active March 18, 2021 00:01
Show Gist options
  • Save mjmikulski/d1e39260acf5adbcbe0e4385f67e7850 to your computer and use it in GitHub Desktop.
Save mjmikulski/d1e39260acf5adbcbe0e4385f67e7850 to your computer and use it in GitHub Desktop.
Ultimate NoOp for python
class _NoOp:
""" No-op that can be put in lieu of almost anything.
Any actions on that object will be just ignored. You can
call it, get its atributes, use a a context, anything :)
Cool feature
------------
NoOp tracks its levels of absurdity.
Examples
--------
>>> from no_op import NoOp as xyz
# Now all calls to xyz are simply ignored without any exception:
>>> print(xyz.get('qwerty').wtf['dupa'].callme('tesla', model='X')[5, ...])
Just no-op on level 7.
Copyright
---------
(c) 2021 Maciej J. Mikulski
Feel free to use it, I put NoOp in public domain.
"""
def __init__(self, level=0):
self.level = level
def __getattr__(self, attr):
return _NoOp(self.level + 1)
def __call__(self, *args, **kwargs):
return _NoOp(self.level + 1)
def __getitem__(self, item):
return _NoOp(self.level + 1)
def __enter__(self):
return _NoOp(self.level + 1)
def __exit__(self):
pass
def __repr__(self):
return f'Just no-op on level {self.level}.'
NoOp = _NoOp()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment