generated from angelamarkos/Mutable-String
-
Notifications
You must be signed in to change notification settings - Fork 0
Feedback #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
github-classroom
wants to merge
2
commits into
feedback
Choose a base branch
from
main
base: feedback
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Feedback #1
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,190 @@ | ||
| from collections.abc import MutableSequence | ||
|
|
||
|
|
||
| class MutableString(MutableSequence): | ||
| def __init__(self, input_str): | ||
| if not isinstance(input_str, str): | ||
| raise Exception("String type is expected") | ||
| self._input_str = list(input_str) | ||
|
|
||
| def __str__(self): | ||
| return "".join(self._input_str) | ||
|
|
||
| def __repr__(self): | ||
| return "".join(self._input_str) | ||
|
|
||
| def __getitem__(self, key): | ||
| return MutableString(self._input_str[key]) | ||
|
|
||
| def __setitem__(self, key, value): | ||
| if not isinstance(value, (MutableString, str)): | ||
| raise Exception("MutableString or str instance is expected") | ||
| self._input_str[key] = value | ||
|
|
||
| def __delitem__(self, key): | ||
| del self._input_str[key] | ||
|
|
||
| def __len__(self): | ||
| return len(self._input_str) | ||
|
|
||
| def __iter__(self): | ||
| return iter(self._input_str) | ||
|
|
||
| def __add__(self, other): | ||
| if isinstance(other, MutableString): | ||
| l_sum = self._input_str + other._input_str | ||
| return MutableString("".join(l_sum)) | ||
| elif isinstance(other, str): | ||
| l_sum = self._input_str + list(other) | ||
| return MutableString("".join(l_sum)) | ||
| else: | ||
| raise Exception("MutableString or str instance is expected") | ||
|
|
||
| def insert(self, i, elem): | ||
| self._input_str.insert(i, elem) | ||
| return self | ||
|
|
||
| def upper(self): | ||
| self._input_str = list(str(self).upper()) | ||
| return self | ||
|
|
||
| def lower(self): | ||
| self._input_str = list(str(self).lower()) | ||
| return self | ||
|
|
||
| def capitalize(self): | ||
| self._input_str = list(str(self).capitalize()) | ||
| return self | ||
|
|
||
| def title(self): | ||
| self._input_str = list(str(self).title()) | ||
| return self | ||
|
|
||
| def startswith(self, string): | ||
| if isinstance(string, (str, tuple)): | ||
| pair = zip(self._input_str, string) | ||
| elif isinstance(string, MutableString): | ||
| pair = zip(self._input_str, string._input_str) | ||
| else: | ||
| raise Exception("MutableString,str or tuple instance is expected") | ||
| if all(item[0] == item[1] for item in pair): | ||
| return True | ||
| return False | ||
|
|
||
| def endswith(self, string): | ||
| if isinstance(string, (str, tuple)): | ||
| pair = zip(self._input_str[::-1], string[::-1]) | ||
| elif isinstance(string, MutableString): | ||
| pair = zip(self._input_str[::-1], string._input_str[::-1]) | ||
| else: | ||
| raise Exception("MutableString,str or tuple instance is expected") | ||
| if all(item[0] == item[1] for item in pair): | ||
| return True | ||
| return False | ||
|
|
||
| def center(self, width, fill=None): | ||
| self._input_str = list(str(self).center(width, fill)) | ||
| return self | ||
|
|
||
| def find(self, string, start=None, end=None): | ||
| if isinstance(string, MutableString): | ||
| string = str(string) | ||
| return str(self).find(string, start, end) | ||
|
|
||
| def rfind(self, string, start=None, end=None): | ||
| if isinstance(string, MutableString): | ||
| string = str(string) | ||
| return str(self).rfind(string, start, end) | ||
|
|
||
| def index(self, string, start=None, end=None): | ||
| if isinstance(string, MutableString): | ||
| string = str(string) | ||
| return str(self).index(string, start, end) | ||
|
|
||
| def rindex(self, string, start=None, end=None): | ||
| if isinstance(string, MutableString): | ||
| string = str(string) | ||
| return str(self).rindex(string, start, end) | ||
|
|
||
| def split(self, symbol=None): | ||
| if isinstance(symbol, MutableString): | ||
| symbol = str(symbol) | ||
| return str(self).split(symbol) | ||
|
|
||
| def replace(self, old, new, count=None): | ||
| if count is None: | ||
| count = len(self) | ||
| if isinstance(old, MutableString): | ||
| old = str(old) | ||
| if isinstance(new, MutableString): | ||
| new = str(new) | ||
| self._input_str = list(str(self).replace(old, new, count)) | ||
| return self | ||
|
|
||
| def rreplace(self, old, new, count=None): | ||
| if count is None: | ||
| count = len(self) | ||
| if isinstance(old, MutableString): | ||
| old = str(old) | ||
| if isinstance(new, MutableString): | ||
| new = str(new) | ||
| self._input_str = list((str(self)[::-1].replace(old[::-1], new[::-1], count)[::-1])) | ||
| return self | ||
|
|
||
| def isdigit(self): | ||
| return str(self).isdigit() | ||
|
|
||
| def isalpha(self): | ||
| return str(self).isalpha() | ||
|
|
||
| def isalnum(self): | ||
| return str(self).isalnum() | ||
|
|
||
| def islower(self): | ||
| return str(self).islower() | ||
|
|
||
| def isupper(self): | ||
| return str(self).isupper() | ||
|
|
||
| def isspace(self): | ||
| return str(self).isspace() | ||
|
|
||
| def istitle(self): | ||
| return str(self).istitle() | ||
|
|
||
| def join(self, array): | ||
| self._input_str = list(str(self).join(array)) | ||
| return self | ||
|
|
||
| def format(self, *args, **kwargs): | ||
| self._input_str = list(str(self).format(*args, **kwargs)) | ||
| return self | ||
|
|
||
| @staticmethod | ||
| def ord(ch): | ||
| if isinstance(ch, MutableString): | ||
| ch = str(ch) | ||
| return ord(ch) | ||
|
|
||
| @staticmethod | ||
| def chr(i): | ||
| return MutableString(chr(i)) | ||
|
|
||
| def count(self, substring, start=None, end=None): | ||
| if isinstance(substring, MutableString): | ||
| substring = str(substring) | ||
| return str(self).count(substring, start, end) | ||
|
|
||
| def strip(self, c=None): | ||
| self._input_str = list(str(self).strip(c)) | ||
| return self | ||
|
|
||
| def lstrip(self, c=None): | ||
| self._input_str = list(str(self).lstrip(c)) | ||
| return self | ||
|
|
||
| def rstrip(self, c=None): | ||
| self._input_str = list(str(self).rstrip(c)) | ||
| return self | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should not modify
selfin these methods