t | class SubString: | t | class SubString: |
| def __init__(self, s): | | def __init__(self, s): |
| self.s = str(s) | | self.s = str(s) |
| | | |
| def __sub__(self, other): | | def __sub__(self, other): |
| res = '' | | res = '' |
| f = other.s if isinstance(other, SubString) else other | | f = other.s if isinstance(other, SubString) else other |
| tosub = {} | | tosub = {} |
| for a in f: | | for a in f: |
| if a in tosub: | | if a in tosub: |
| tosub[a] += 1 | | tosub[a] += 1 |
| else: | | else: |
| tosub[a] = 1 | | tosub[a] = 1 |
| for ch in self.s: | | for ch in self.s: |
| if ch in tosub and tosub[ch] > 0: | | if ch in tosub and tosub[ch] > 0: |
| tosub[ch] -= 1 | | tosub[ch] -= 1 |
| else: | | else: |
| res += ch | | res += ch |
| return SubString(res) | | return SubString(res) |
| | | |
| def __add__(self, other): | | def __add__(self, other): |
| f = other.s if isinstance(other, SubString) else other | | f = other.s if isinstance(other, SubString) else other |
| return SubString(self.s + f) | | return SubString(self.s + f) |
| | | |
| def __radd__(self, other): | | def __radd__(self, other): |
| f = other.s if isinstance(other, SubString) else other | | f = other.s if isinstance(other, SubString) else other |
| return SubString(f + self.s) | | return SubString(f + self.s) |
| | | |
| def __mul__(self, other): | | def __mul__(self, other): |
| return SubString(self.s * other) | | return SubString(self.s * other) |
| | | |
| def __getitem__(self, item): | | def __getitem__(self, item): |
| return SubString(self.s[item]) | | return SubString(self.s[item]) |
| | | |
| def __repr__(self): | | def __repr__(self): |
| return self.s | | return self.s |
| | | |
| def __str__(self): | | def __str__(self): |
| return self.s | | return self.s |
| | | |
| def __contains__(self, item): | | def __contains__(self, item): |
| f = item.s if isinstance(item, SubString) else item | | f = item.s if isinstance(item, SubString) else item |
| return self.s.__contains__(f) | | return self.s.__contains__(f) |
| | | |
| def __eq__(self, other): | | def __eq__(self, other): |
| f = other.s if isinstance(other, SubString) else other | | f = other.s if isinstance(other, SubString) else other |
| return self.s.__eq__(f) | | return self.s.__eq__(f) |
| | | |
| def __gt__(self, other): | | def __gt__(self, other): |
| f = other.s if isinstance(other, SubString) else other | | f = other.s if isinstance(other, SubString) else other |
| return self.s.__gt__(f) | | return self.s.__gt__(f) |
| | | |
| def __ge__(self, other): | | def __ge__(self, other): |
| f = other.s if isinstance(other, SubString) else other | | f = other.s if isinstance(other, SubString) else other |
| return self.s.__ge__(f) | | return self.s.__ge__(f) |
| | | |
| def __len__(self): | | def __len__(self): |
| return self.s.__len__() | | return self.s.__len__() |
| | | |
| def __getattr__(self, name): | | def __getattr__(self, name): |
| attr = getattr(self.s, name) | | attr = getattr(self.s, name) |
| if not callable(attr): | | if not callable(attr): |
| return attr | | return attr |
| | | |
| def mapper(a): | | def mapper(a): |
| if isinstance(a, SubString): | | if isinstance(a, SubString): |
| return a.s | | return a.s |
| return a | | return a |
| | | |
| def method(*args): | | def method(*args): |
| if len(args) > 0: | | if len(args) > 0: |
| args = tuple(map(mapper, args)) | | args = tuple(map(mapper, args)) |
| return SubString(attr(*args)) | | return SubString(attr(*args)) |
| return SubString(attr()) | | return SubString(attr()) |
| | | |
| return method | | return method |
| | | |