Last updated:
0 purchases
protecc 0.0.2
protecc
Access modifiers for python.
Setup
PIP install
pip install protecc
From the source
git clone https://github.com/avinashshenoy97/protecc
cd protecc
python setup.py install
Usage
Old-Style Classes
Inherit from/extend the protecc class.
from protecc import protecc
class regulatedClass(protecc):
def __init__(self):
self.publicVariable = 'public value'
self._privateVariable = 'private value'
def publicMethod(self):
return True
def __privateMethod(self):
return True
def getPrivateVariable(self):
return self._privateVariable
def privateMethodProxy(self):
return self.__privateMethod()
New-Style Classes (type)
If you prefer metaclass (for better readability/what not), use metaProtecc.
class metaRegulatedClass(metaclass=metaProtecc):
def __init__(self):
self.publicVariable = 'public value'
self._privateVariable = 'private value'
def publicMethod(self):
return True
def __privateMethod(self):
return True
def getPrivateVariable(self):
return self._privateVariable
def privateMethodProxy(self):
return self.__privateMethod()
When accessing "private" methods (i.e, methods that begin with either a single or two underscores), this exception is raised
>>> r = regulatedClass()
>>> r.__privateMethod()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/avinashshenoy/STUFF/projects/protecc/protecc.py", line 23, in __protecced_getattribute__
raise AccessException('Cannot access ' + varType + ' member ' + name) from None
protecc.AccessException: Cannot access private member __privateMethod
Private methods can still be accessed from within other class methods, as is expected:
>>> r = regulatedClass()
>>> r.privateMethodProxy()
True
Note
This is, by no means, fool-proof access protection.
"Private members" are those members whose names are mangled by Python, as per PEP 8 conventions, i.e, starting with 2 underscores and having not more than one trailing underscore.
Additionally, "protected members" are those members whose names are preceded by a single underscores. These members cannot be accessed from outside the class as well, as is expected.
Primary Contributors
Avinash Shenoy
License
This project is released under the MIT License.
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.