Skip to content

Instantly share code, notes, and snippets.

@cmrfrd
Last active March 3, 2020 23:10
Show Gist options
  • Save cmrfrd/83e677d4092db2aab643c0fb423e2829 to your computer and use it in GitHub Desktop.
Save cmrfrd/83e677d4092db2aab643c0fb423e2829 to your computer and use it in GitHub Desktop.
snake_string2.py
import _collections_abc
from operator import itemgetter as _itemgetter, eq as _eq
from keyword import iskeyword as _iskeyword
import sys as _sys
from functools import wraps
MAX_DEPTH = 1024
class SnakeString(_collections_abc.Sequence):
"""
SnakeString is a class that constructs strings from attributes
and args/kwargs
"""
seperator = "."
def __init__(self, seq=[]):
self.___data = [] + seq
@property
def __data(self):
return self.seperator.join(filter(bool,self.___data))
def __getattr__(self, attr):
if 0 <= len(self.___data) < MAX_DEPTH:
# print("less", attr, self.__data)
return SnakeString(self.___data + [attr])
else:
# print("over", attr, self.__data)
return self.__data
def s(self, *args, **kwargs):
if 0 <= len(self.___data) < MAX_DEPTH:
# print("less", attr, self.__data)
return SnakeString(self.___data + [self.seperator.join(map(str, list(args) + list(kwargs.values())))])
else:
# print("over", attr, self.__data)
return self.__data
def __collapse(f):
@wraps(f)
def wrapper(self, *args, **kwargs):
res = f(self, *args, **kwargs)
while len(self.___data) < MAX_DEPTH:
self.___data.append(None)
res = f(self, *args, **kwargs)
return res
return wrapper
@__collapse
def __str__(self):
return str(self.__data)
def __repr__(self):
return str(self.__data)
@__collapse
def __int__(self):
return int(self.__data)
@__collapse
def __float__(self):
return float(self.__data)
@__collapse
def __complex__(self):
return complex(self.__data)
@__collapse
def __hash__(self):
return hash(self.__data)
@__collapse
def __getnewargs__(self):
return (self.__data[:],)
@__collapse
def __eq__(self, string):
if isinstance(string, SnakeString):
return self.__data == string.__data
return self.__data == string
@__collapse
def __lt__(self, string):
if isinstance(string, SnakeString):
return self.__data < string.__data
return self.__data < string
@__collapse
def __le__(self, string):
if isinstance(string, SnakeString):
return self.__data <= string.__data
return self.__data <= string
@__collapse
def __gt__(self, string):
if isinstance(string, SnakeString):
return self.__data > string.__data
return self.__data > string
@__collapse
def __ge__(self, string):
if isinstance(string, SnakeString):
return self.__data >= string.__data
return self.__data >= string
@__collapse
def __contains__(self, char):
if isinstance(char, SnakeString):
char = char.__data
return char in self.__data
def __len__(self): return len(self.__data)
def __getitem__(self, index): return self.__class__(self.__data[index])
@__collapse
def __add__(self, other):
return self.__data + str(other)
@__collapse
def __radd__(self, other):
return str(other) + self.__data
def __mul__(self, n):
return self.__class__(self.__data*n)
__rmul__ = __mul__
def __mod__(self, args):
return self.__data.startswith(str(args))
def __rmod__(self, template):
return str(template) % self
# the following methods are defined in alphabetical order:
def capitalize(self): return self.__class__(self.__data.capitalize())
def casefold(self):
return self.__class__(self.__data.casefold())
def center(self, width, *args):
return self.__class__(self.__data.center(width, *args))
def count(self, sub, start=0, end=_sys.maxsize):
if isinstance(sub, SnakeString):
sub = sub.__data
return self.__data.count(sub, start, end)
def encode(self, encoding='utf-8', errors='strict'):
encoding = 'utf-8' if encoding is None else encoding
errors = 'strict' if errors is None else errors
return self.__data.encode(encoding, errors)
def endswith(self, suffix, start=0, end=_sys.maxsize):
return self.__data.endswith(suffix, start, end)
def expandtabs(self, tabsize=8):
return self.__class__(self.__data.expandtabs(tabsize))
def find(self, sub, start=0, end=_sys.maxsize):
if isinstance(sub, SnakeString):
sub = sub.__data
return self.__data.find(sub, start, end)
def format(self, /, *args, **kwds):
return self.__data.format(*args, **kwds)
def format_map(self, mapping):
return self.__data.format_map(mapping)
def index(self, sub, start=0, end=_sys.maxsize):
return self.__data.index(sub, start, end)
def isalpha(self): return self.__data.isalpha()
def isalnum(self): return self.__data.isalnum()
def isascii(self): return self.__data.isascii()
def isdecimal(self): return self.__data.isdecimal()
def isdigit(self): return self.__data.isdigit()
def isidentifier(self): return self.__data.isidentifier()
def islower(self): return self.__data.islower()
def isnumeric(self): return self.__data.isnumeric()
def isprintable(self): return self.__data.isprintable()
def isspace(self): return self.__data.isspace()
def istitle(self): return self.__data.istitle()
def isupper(self): return self.__data.isupper()
def join(self, seq): return self.__data.join(seq)
def ljust(self, width, *args):
return self.__class__(self.__data.ljust(width, *args))
def lower(self): return self.__class__(self.__data.lower())
def lstrip(self, chars=None): return self.__class__(self.__data.lstrip(chars))
maketrans = str.maketrans
def partition(self, sep):
return self.__data.partition(sep)
def replace(self, old, new, maxsplit=-1):
if isinstance(old, SnakeString):
old = old.__data
if isinstance(new, SnakeString):
new = new.__data
return self.__class__(self.__data.replace(old, new, maxsplit))
def rfind(self, sub, start=0, end=_sys.maxsize):
if isinstance(sub, SnakeString):
sub = sub.__data
return self.__data.rfind(sub, start, end)
def rindex(self, sub, start=0, end=_sys.maxsize):
return self.__data.rindex(sub, start, end)
def rjust(self, width, *args):
return self.__class__(self.__data.rjust(width, *args))
def rpartition(self, sep):
return self.__data.rpartition(sep)
def rstrip(self, chars=None):
return self.__class__(self.__data.rstrip(chars))
def split(self, sep=None, maxsplit=-1):
return self.__data.split(sep, maxsplit)
def rsplit(self, sep=None, maxsplit=-1):
return self.__data.rsplit(sep, maxsplit)
def splitlines(self, keepends=False): return self.__data.splitlines(keepends)
def startswith(self, prefix, start=0, end=_sys.maxsize):
return self.__data.startswith(prefix, start, end)
def strip(self, chars=None): return self.__class__(self.__data.strip(chars))
def swapcase(self): return self.__class__(self.__data.swapcase())
def title(self): return self.__class__(self.__data.title())
def translate(self, *args):
return self.__class__(self.__data.translate(*args))
def upper(self): return self.__class__(self.__data.upper())
def zfill(self, width): return self.__class__(self.__data.zfill(width))
u = SnakeString()
print(u.q.w.e.r)
print('---')
print(u.a.b.c)
print('---')
a = u.a.b
print(a)
print('---')
print(u.z.w.v.g.b.b.b.b)
print('---')
print(u.asdf.qwer.zxcv)
print('---')
b = {}
b[u.asdf] = u.qwer
print(b)
print(b["asdf"])
print(b[u.asdf])
print('---')
u.a.b.c
print(u.c.b.a)
print('---')
print("q.w.e.r" in u.q.w.e.r)
print('---')
print(u.a.b.s(1, u.a))
print('---')
print(getattr(u.a.b.s(1, u.a), "_SnakeString___data"))
print('---')
print(u.a + u.b)
print('---')
print(u.a.b.c % u.a)
print('---')
print(u.a in u.a.b.c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment