Selfie
Loading...
Searching...
No Matches
selfie_lib.Slice.Slice Class Reference

Public Member Functions

None __init__ (self, str base, int startIndex=0, Optional[int] endIndex=None)
 
int __len__ (self)
 
str __getitem__ (self, int index)
 
"Slice" subSequence (self, int start, int end)
 
"Slice" trim (self)
 
str __str__ (self)
 
bool sameAs (self, Union["Slice", str] other)
 
int indexOf (self, str lookingFor, int startOffset=0)
 
"Slice" unixLine (self, int count)
 
bool __eq__ (self, object other)
 
int __hash__ (self)
 
str replaceSelfWith (self, str s)
 
int count (self, str char)
 
int baseLineAtOffset (self, int index)
 
bool starts_with (self, str prefix)
 

Public Attributes

 base
 
 startIndex
 
 endIndex
 

Detailed Description

Represents a slice of a base string from startIndex to endIndex.

Definition at line 5 of file Slice.py.

Constructor & Destructor Documentation

◆ __init__()

None selfie_lib.Slice.Slice.__init__ (   self,
str  base,
int   startIndex = 0,
Optional[int]   endIndex = None 
)

Definition at line 8 of file Slice.py.

10 ) -> None:
11 self.base = base
12 self.base = base
13 self.startIndex = startIndex
14 self.endIndex = endIndex if endIndex is not None else len(base)
15
16 assert (
17 0 <= self.startIndex <= self.endIndex <= len(base)
18 ), "Invalid start or end index"
19

Member Function Documentation

◆ __eq__()

bool selfie_lib.Slice.Slice.__eq__ (   self,
object  other 
)

Definition at line 71 of file Slice.py.

71 def __eq__(self, other: object) -> bool:
72 if self is other:
73 return True
74 if isinstance(other, Slice):
75 return self.sameAs(other)
76 return False
77

◆ __getitem__()

str selfie_lib.Slice.Slice.__getitem__ (   self,
int  index 
)

Definition at line 23 of file Slice.py.

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

◆ __hash__()

int selfie_lib.Slice.Slice.__hash__ (   self)

Definition at line 78 of file Slice.py.

78 def __hash__(self) -> int:
79 h = 0
80 for i in range(len(self)):
81 h = 31 * h + ord(self[i])
82 return h
83

◆ __len__()

int selfie_lib.Slice.Slice.__len__ (   self)

Definition at line 20 of file Slice.py.

20 def __len__(self) -> int:
21 return self.endIndex - self.startIndex
22

◆ __str__()

str selfie_lib.Slice.Slice.__str__ (   self)

Definition at line 39 of file Slice.py.

39 def __str__(self) -> str:
40 return self.base[self.startIndex : self.endIndex]
41

◆ baseLineAtOffset()

int selfie_lib.Slice.Slice.baseLineAtOffset (   self,
int  index 
)

Definition at line 90 of file Slice.py.

90 def baseLineAtOffset(self, index: int) -> int:
91 return 1 + Slice(self.base, 0, index).count("\n")
92

◆ count()

int selfie_lib.Slice.Slice.count (   self,
str  char 
)

Definition at line 87 of file Slice.py.

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

◆ indexOf()

int selfie_lib.Slice.Slice.indexOf (   self,
str  lookingFor,
int   startOffset = 0 
)

Definition at line 51 of file Slice.py.

51 def indexOf(self, lookingFor: str, startOffset: int = 0) -> int:
52 result = self.base.find(
53 lookingFor, self.startIndex + startOffset, self.endIndex
54 )
55 return -1 if result == -1 else result - self.startIndex
56

◆ replaceSelfWith()

str selfie_lib.Slice.Slice.replaceSelfWith (   self,
str  s 
)

Definition at line 84 of file Slice.py.

84 def replaceSelfWith(self, s: str) -> str:
85 return self.base[: self.startIndex] + s + self.base[self.endIndex :]
86

◆ sameAs()

bool selfie_lib.Slice.Slice.sameAs (   self,
Union["Slice", str]  other 
)

Definition at line 42 of file Slice.py.

42 def sameAs(self, other: Union["Slice", str]) -> bool:
43 if isinstance(other, Slice):
44 return str(self) == str(other)
45 elif isinstance(other, str):
46 if len(self) != len(other):
47 return False
48 return all(self[i] == other[i] for i in range(len(self)))
49 return False
50

◆ starts_with()

bool selfie_lib.Slice.Slice.starts_with (   self,
str  prefix 
)

Definition at line 93 of file Slice.py.

93 def starts_with(self, prefix: str) -> bool:
94 if len(prefix) > len(self):
95 return False
96 return all(self[i] == prefix[i] for i in range(len(prefix)))

◆ subSequence()

"Slice" selfie_lib.Slice.Slice.subSequence (   self,
int  start,
int  end 
)

Definition at line 28 of file Slice.py.

28 def subSequence(self, start: int, end: int) -> "Slice":
29 return Slice(self.base, self.startIndex + start, self.startIndex + end)
30

◆ trim()

"Slice" selfie_lib.Slice.Slice.trim (   self)

Definition at line 31 of file Slice.py.

31 def trim(self) -> "Slice":
32 start, end = 0, len(self)
33 while start < end and self[start].isspace():
34 start += 1
35 while start < end and self[end - 1].isspace():
36 end -= 1
37 return self.subSequence(start, end) if start > 0 or end < len(self) else self
38

◆ unixLine()

"Slice" selfie_lib.Slice.Slice.unixLine (   self,
int  count 
)

Definition at line 57 of file Slice.py.

57 def unixLine(self, count: int) -> "Slice":
58 assert count > 0, "Count must be positive"
59 lineStart = 0
60 for i in range(1, count):
61 lineStart = self.indexOf("\n", lineStart)
62 assert lineStart >= 0, f"This string has only {i - 1} lines, not {count}"
63 lineStart += 1
64 lineEnd = self.indexOf("\n", lineStart)
65 return Slice(
66 self.base,
67 self.startIndex + lineStart,
68 self.endIndex if lineEnd == -1 else self.startIndex + lineEnd,
69 )
70

Member Data Documentation

◆ base

selfie_lib.Slice.Slice.base

Definition at line 11 of file Slice.py.

◆ endIndex

selfie_lib.Slice.Slice.endIndex

Definition at line 14 of file Slice.py.

◆ startIndex

selfie_lib.Slice.Slice.startIndex

Definition at line 13 of file Slice.py.


The documentation for this class was generated from the following file: