Skip to content

Instantly share code, notes, and snippets.

@EricsonWillians
Last active August 29, 2015 14:23
Show Gist options
  • Save EricsonWillians/0a658fae19c1d161dde6 to your computer and use it in GitHub Desktop.
Save EricsonWillians/0a658fae19c1d161dde6 to your computer and use it in GitHub Desktop.
Implementation of a custom non-punctuated and purely alphabetic string object.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# pure_string.py
#
# Copyright 2015 Ericson Willians (Rederick Deathwill) <EricsonWRP@ERICSONWRP-PC>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
from string import ascii_letters
class astr(str):
"""
astr is a custom non-punctuated and purely alphabetic string
that returns a string of zeros if an impure string is provided.
"""
def __new__(self, content):
if (lambda s: True if False not in [False for c in s if c not in ascii_letters] else False)(content):
return str.__new__(self, content)
else:
return str.__new__(self, ''.join([str(0) for c in content]))
if __name__ == '__main__':
x = astr("Normalstring")
y = astr("alphanumeric7234852fsd36asd4fgh232")
z = astr("Ácçènts")
print(x)
print(y)
print(z)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment