Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

Commit

Permalink
changed comment typing to inline
Browse files Browse the repository at this point in the history
  • Loading branch information
SDelgado-21 committed Feb 27, 2024
1 parent 0697b75 commit 1cc0a97
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions python/selfie-lib/selfie_lib/Slice.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,41 @@
from typing import Optional # noqa: F401
from typing import Union # noqa: F401
from typing import Optional
from typing import Union
from collections import Counter

class Slice:
"""Represents a slice of a base string from startIndex to endIndex."""

def __init__(self, base, startIndex=0, endIndex=None): # type: (str, int, Optional[int]) -> None
def __init__(self, base: str, startIndex: int = 0, endIndex: Optional[int] = None) -> None:
self.base = base
self.base = base
self.startIndex = startIndex
self.endIndex = endIndex if endIndex is not None else len(base)

assert 0 <= self.startIndex <= self.endIndex <= len(base), "Invalid start or end index"

def __len__(self): # type: () -> int
def __len__(self) -> int:
return self.endIndex - self.startIndex

def __getitem__(self, index): # type: (int) -> str
def __getitem__(self, index: int) -> str:
if not (0 <= index < len(self)):
raise IndexError("Index out of range")
return self.base[self.startIndex + index]

def subSequence(self, start, end): # type: (int, int) -> 'Slice'
def subSequence(self, start: int, end: int) -> 'Slice':
return Slice(self.base, self.startIndex + start, self.startIndex + end)

def trim(self): # type: () -> 'Slice'
def trim(self) -> 'Slice':
start, end = 0, len(self)
while start < end and self[start].isspace():
start += 1
while start < end and self[end - 1].isspace():
end -= 1
return self.subSequence(start, end) if start > 0 or end < len(self) else self

def __str__(self): # type: () -> str
def __str__(self) -> str:
return self.base[self.startIndex:self.endIndex]

def sameAs(self, other): # type: (Union['Slice', str]) -> bool
def sameAs(self, other: Union['Slice', str]) -> bool:
if isinstance(other, Slice):
return str(self) == str(other)
elif isinstance(other, str):
Expand All @@ -46,11 +47,11 @@ def sameAs(self, other): # type: (Union['Slice', str]) -> bool
return True
return False

def indexOf(self, lookingFor, startOffset=0): # type: (str, int) -> int
def indexOf(self, lookingFor: str, startOffset: int = 0) -> int:
result = self.base.find(lookingFor, self.startIndex + startOffset, self.endIndex)
return -1 if result == -1 else result - self.startIndex

def unixLine(self, count): # type: (int) -> 'Slice'
def unixLine(self, count: int) -> 'Slice':
assert count > 0, "Count must be positive"
lineStart = 0
for i in range(1, count):
Expand All @@ -60,24 +61,24 @@ def unixLine(self, count): # type: (int) -> 'Slice'
lineEnd = self.indexOf('\n', lineStart)
return Slice(self.base, self.startIndex + lineStart, self.endIndex if lineEnd == -1 else self.startIndex + lineEnd)

def __eq__(self, other): # type: (object) -> bool
def __eq__(self, other: object) -> bool:
if self is other:
return True
if isinstance(other, Slice):
return self.sameAs(other)
return False

def __hash__(self): # type: () -> int
def __hash__(self) -> int:
h = 0
for i in range(len(self)):
h = 31 * h + ord(self[i])
return h

def replaceSelfWith(self, s): # type: (str) -> str
def replaceSelfWith(self, s: str) -> str:
return self.base[:self.startIndex] + s + self.base[self.endIndex:]

def count(self, char):
def count(self, char: str) -> int:
return Counter(self.base[self.startIndex:self.endIndex])[char]

def baseLineAtOffset(self, index): # type: (int) -> int
def baseLineAtOffset(self, index: int) -> int:
return 1 + Slice(self.base, 0, index).count('\n')

0 comments on commit 1cc0a97

Please sign in to comment.