Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class SecureString(str):
- MASKED = '*****'
- @classmethod
- def __masked__(cls, *args, **kwargs):
- return SecureString.MASKED
- def __repr__(self):
- return SecureString.__masked__()
- def __str__(self):
- return SecureString.__masked__()
- def __getitem__(self, item):
- if isinstance(item, slice): # detect slicing
- return SecureString.__masked__()
- def __getattribute__(self, item):
- if 'get' == item: # Return normal string
- return super(SecureString, self).__str__
- else: # Return masked string
- return SecureString.__masked__
- password = SecureString('abc')
- # good
- print('\n>>> positive')
- print(password.get())
- print(password.get().lower())
- # bad
- print('\n>>> negative')
- print(password)
- print(password.lower())
- print(password.upper())
- print(repr(password))
- print(str(password))
- print(password[::-1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement