Skip to content

Commit

Permalink
codacity issues
Browse files Browse the repository at this point in the history
  • Loading branch information
mailund committed Jul 2, 2021
1 parent af0127d commit cae5248
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
20 changes: 14 additions & 6 deletions pystr/alphabet.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Alphabet:

def __init__(self, reference: str) -> None:
"""Creates an alphabet with the letters found in reference.
An alphabet always has a sentinel symbol, byte zero, regardless of whether
it is found in reference."""
self._map = {
Expand All @@ -36,13 +37,17 @@ def __len__(self) -> int:

def map(self, x: typing.Iterable[str]) -> bytearray:
"""Maps the characters in x to their corresponding letters in the alphabet,
and returns the result as a bytearray. If x contains a letter not in the alphabet,
and returns the result as a bytearray.
If x contains a letter not in the alphabet,
map raises a KeyError."""
return bytearray(self._map[a] for a in x)

def map_with_sentinel(self, x: typing.Iterable[str]) -> bytearray:
"""Maps the characters in x to their corresponding letters in the alphabet,
and returns the result as a bytearray. If x contains a letter not in the alphabet,
and returns the result as a bytearray.
If x contains a letter not in the alphabet,
map raises a KeyError. The result has the sentinel added to it, so the last
character in the result is the zero byte."""
b = self.map(x)
Expand All @@ -68,6 +73,7 @@ def mapped_string(x: str) -> tuple[bytearray, Alphabet]:
def mapped_subseq(x: str) -> tuple[SubSeq[int], Alphabet]:
"""Creates an alphabet from x, maps x to theh alphabet,
then returns the mapped string and the alphabet.
The resulting string is a SubSeq[int], unlike mapped_string(x) which
returns a bytearray for the mapped string."""
x_, alpha = Alphabet.mapped_string(x)
Expand All @@ -78,16 +84,18 @@ def mapped_string_with_sentinel(
x: str
) -> tuple[bytearray, Alphabet]:
"""Creates an alphabet from x, maps x to theh alphabet,
then returns the mapped string and the alphabet. The mapped
string is terminated by the sentinel (zero) byte."""
then returns the mapped string and the alphabet.
The mapped string is terminated by the sentinel (zero) byte."""
alpha = Alphabet(x)
return alpha.map_with_sentinel(x), alpha

@staticmethod
def mapped_subseq_with_sentinel(x: str) -> tuple[SubSeq[int], Alphabet]:
"""Creates an alphabet from x, maps x to theh alphabet,
then returns the mapped string and the alphabet. The mapped
string is terminated by the sentinel (zero) byte.
then returns the mapped string and the alphabet.
The mapped string is terminated by the sentinel (zero) byte.
The resulting string is a SubSeq[int], unlike
mapped_string_with_sentinel(x) which
returns a bytearray for the mapped string."""
Expand Down
3 changes: 2 additions & 1 deletion pystr/bv.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@

class BitVector:

"""A bit vector."""

bytes: bytearray
size: int

def __init__(self, size: int):
"""Creates a BitVector that can hold size bits"""
"""Creates a BitVector that can hold size bits."""
self.size = size
self.bytes = bytearray((size+8-1)//8)

Expand Down
9 changes: 5 additions & 4 deletions pystr/subseq.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ def __gt__(self, other: object) -> bool: ...

# Then the functional stuff...
class SubSeq(typing.Generic[T], typing.Sequence[T]):

"""This is a wrapper around lists and strings that lets us slice them
without copying them.
"""
without copying them.
"""
_x: typing.Sequence[T]
_i: int
_j: int
Expand Down Expand Up @@ -96,8 +97,8 @@ def __getitem__(self: S, idx: int) -> T: ...
def __getitem__(self: S, idx: slice) -> S: ...

def __getitem__(self: S, idx: int | slice) -> T | S:
"""Get the value at an index, or a new subseq (of the current kind) if you index
with a slice."""
"""Get the value at an index, or a new subseq (of the current kind) if you index with a slice."""

if isinstance(idx, int):
# FIXME: the cast is needed here because mypy can't
# figure out generic bound vars, so it doesn't know
Expand Down

0 comments on commit cae5248

Please sign in to comment.