Skip to content

Commit

Permalink
Ruff format
Browse files Browse the repository at this point in the history
  • Loading branch information
dbrattli committed Nov 3, 2023
1 parent 89e54df commit 7d87c35
Show file tree
Hide file tree
Showing 28 changed files with 160 additions and 412 deletions.
10 changes: 3 additions & 7 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@ repos:
- hooks:
- id: ruff
args: ["--fix"]

- id: ruff-format
args: [--check]
repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.0.287
- hooks:
- id: black
exclude: ^docs/
repo: https://github.com/psf/black
rev: 23.1.0
rev: v0.1.3
- hooks:
- id: pyright
name: pyright
Expand Down
44 changes: 11 additions & 33 deletions expression/collections/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,7 @@ class TypeCode(Enum):
Any = "a"


def array_from_typecode(
type_code: TypeCode, initializer: Iterable[Any] | None
) -> _Array[Any]:
def array_from_typecode(type_code: TypeCode, initializer: Iterable[Any] | None) -> _Array[Any]:
arr: _Array[Any] = list(initializer if initializer else [])
if type_code == TypeCode.Byte:
return bytearray(arr)
Expand Down Expand Up @@ -200,9 +198,7 @@ def map(self, mapping: Callable[[_TSource], _TResult]) -> TypedArray[_TResult]:
result = builtins.map(mapping, self.value)
return TypedArray(result)

def choose(
self, chooser: Callable[[_TSource], Option[_TResult]]
) -> TypedArray[_TResult]:
def choose(self, chooser: Callable[[_TSource], Option[_TResult]]) -> TypedArray[_TResult]:
"""Choose items from the list.
Applies the given function to each element of the list. Returns
Expand All @@ -222,9 +218,7 @@ def mapper(x: _TSource) -> TypedArray[_TResult]:

return self.collect(mapper)

def collect(
self, mapping: Callable[[_TSource], TypedArray[_TResult]]
) -> TypedArray[_TResult]:
def collect(self, mapping: Callable[[_TSource], TypedArray[_TResult]]) -> TypedArray[_TResult]:
mapped = builtins.map(mapping, self.value)
xs = (y for x in mapped for y in x)
return TypedArray(xs)
Expand Down Expand Up @@ -255,13 +249,9 @@ def filter(self, predicate: Callable[[_TSource], bool]) -> TypedArray[_TSource]:
A list containing only the elements that satisfy the
predicate.
"""
return TypedArray(
builtins.filter(predicate, self.value), typecode=self.typecode
)
return TypedArray(builtins.filter(predicate, self.value), typecode=self.typecode)

def fold(
self, folder: Callable[[_TState, _TSource], _TState], state: _TState
) -> _TState:
def fold(self, folder: Callable[[_TState, _TSource], _TState], state: _TState) -> _TState:
"""Fold array.
Applies a function to each element of the array,
Expand Down Expand Up @@ -363,9 +353,7 @@ def skip(self, count: int) -> TypedArray[_TSource]:
def skip_last(self, count: int) -> TypedArray[_TSource]:
return TypedArray(self.value[:-count])

def sort(
self: TypedArray[_TSourceSortable], reverse: bool = False
) -> TypedArray[_TSourceSortable]:
def sort(self: TypedArray[_TSourceSortable], reverse: bool = False) -> TypedArray[_TSourceSortable]:
"""Sort array directly.
Returns a new sorted collection.
Expand All @@ -378,9 +366,7 @@ def sort(
"""
return TypedArray(builtins.sorted(self.value, reverse=reverse))

def sort_with(
self, func: Callable[[_TSource], Any], reverse: bool = False
) -> TypedArray[_TSource]:
def sort_with(self, func: Callable[[_TSource], Any], reverse: bool = False) -> TypedArray[_TSource]:
"""Sort array with supplied function.
Returns a new sorted collection.
Expand Down Expand Up @@ -498,9 +484,7 @@ def __repr__(self) -> str:


@curry_flip(1)
def map(
source: TypedArray[_TSource], mapper: Callable[[_TSource], _TResult]
) -> TypedArray[_TResult]:
def map(source: TypedArray[_TSource], mapper: Callable[[_TSource], _TResult]) -> TypedArray[_TResult]:
"""Map array.
Builds a new array whose elements are the results of applying
Expand All @@ -521,9 +505,7 @@ def empty() -> TypedArray[Any]:


@curry_flip(1)
def filter(
source: TypedArray[_TSource], predicate: Callable[[_TSource], bool]
) -> TypedArray[_TSource]:
def filter(source: TypedArray[_TSource], predicate: Callable[[_TSource], bool]) -> TypedArray[_TSource]:
"""Filter array.
Returns a new array containing only the elements of the
Expand Down Expand Up @@ -612,9 +594,7 @@ def sum(source: TypedArray[_TSourceSum]) -> int:


@curry_flip(1)
def sum_by(
source: TypedArray[_TSource], projection: Callable[[_TSource], _TSourceSum]
) -> int:
def sum_by(source: TypedArray[_TSource], projection: Callable[[_TSource], _TSourceSum]) -> int:
return builtins.sum(source.map(projection).value)


Expand Down Expand Up @@ -664,9 +644,7 @@ def try_head(source: TypedArray[_TSource]) -> Option[_TSource]:


@curry_flip(1)
def unfold(
state: _TState, generator: Callable[[_TState], Option[tuple[_TSource, _TState]]]
) -> TypedArray[_TSource]:
def unfold(state: _TState, generator: Callable[[_TState], Option[tuple[_TSource, _TState]]]) -> TypedArray[_TSource]:
"""Unfold array.
Returns a list that contains the elements generated by the
Expand Down
8 changes: 2 additions & 6 deletions expression/collections/asyncseq.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@ async def range(*args: int, **kw: int) -> AsyncIterable[int]:
yield value


def filter(
predicate: Callable[[TSource], bool]
) -> Callable[[AsyncIterable[TSource]], AsyncIterable[TSource]]:
def filter(predicate: Callable[[TSource], bool]) -> Callable[[AsyncIterable[TSource]], AsyncIterable[TSource]]:
async def _filter(source: AsyncIterable[TSource]) -> AsyncIterable[TSource]:
async for value in source:
if predicate(value):
Expand All @@ -97,9 +95,7 @@ async def _filter(source: AsyncIterable[TSource]) -> AsyncIterable[TSource]:
return _filter


def map(
mapper: Callable[[TSource], TResult]
) -> Callable[[AsyncIterable[TSource]], AsyncIterable[TResult]]:
def map(mapper: Callable[[TSource], TResult]) -> Callable[[AsyncIterable[TSource]], AsyncIterable[TResult]]:
async def _map(source: AsyncIterable[TSource]) -> AsyncIterable[TResult]:
async for value in source:
yield mapper(value)
Expand Down
84 changes: 21 additions & 63 deletions expression/collections/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,7 @@ def append(self, other: Block[_TSource]) -> Block[_TSource]:
"""Append other block to end of the block."""
return Block(self._value + other._value)

def choose(
self, chooser: Callable[[_TSource], Option[_TResult]]
) -> Block[_TResult]:
def choose(self, chooser: Callable[[_TSource], Option[_TResult]]) -> Block[_TResult]:
"""Choose items from the list.
Applies the given function to each element of the list. Returns
Expand All @@ -132,9 +130,7 @@ def mapper(x: _TSource) -> Block[_TResult]:

return self.collect(mapper)

def collect(
self, mapping: Callable[[_TSource], Block[_TResult]]
) -> Block[_TResult]:
def collect(self, mapping: Callable[[_TSource], Block[_TResult]]) -> Block[_TResult]:
mapped = builtins.map(mapping, self._value)
xs = (y for x in mapped for y in x)
return Block(xs)
Expand Down Expand Up @@ -163,9 +159,7 @@ def filter(self, predicate: Callable[[_TSource], bool]) -> Block[_TSource]:
"""
return Block(builtins.filter(predicate, self._value))

def fold(
self, folder: Callable[[_TState, _TSource], _TState], state: _TState
) -> _TState:
def fold(self, folder: Callable[[_TState, _TSource], _TState], state: _TState) -> _TState:
"""Fold block.
Applies a function to each element of the collection,
Expand Down Expand Up @@ -264,9 +258,7 @@ def map(self, mapping: Callable[[_TSource], _TResult]) -> Block[_TResult]:
return Block((*builtins.map(mapping, self),))

@overload
def starmap(
self: Block[tuple[_T1, _T2]], mapping: Callable[[_T1, _T2], _TResult]
) -> Block[_TResult]:
def starmap(self: Block[tuple[_T1, _T2]], mapping: Callable[[_T1, _T2], _TResult]) -> Block[_TResult]:
...

@overload
Expand Down Expand Up @@ -301,9 +293,7 @@ def starmap(self: Block[Any], mapping: Callable[..., Any]) -> Block[Any]:
def sum(self: Block[_TSourceSum | Literal[0]]) -> _TSourceSum | Literal[0]:
return builtins.sum(self._value)

def sum_by(
self: Block[_TSourceSum], projection: Callable[[_TSourceSum], _TResult]
) -> _TResult:
def sum_by(self: Block[_TSourceSum], projection: Callable[[_TSourceSum], _TResult]) -> _TResult:
return pipe(self, sum_by(projection))

def mapi(self, mapping: Callable[[int, _TSource], _TResult]) -> Block[_TResult]:
Expand Down Expand Up @@ -337,9 +327,7 @@ def of_seq(xs: Iterable[_TSource]) -> Block[_TSource]:
def of_option(option: Option[_TSource]) -> Block[_TSource]:
return of_option(option)

def partition(
self, predicate: Callable[[_TSource], bool]
) -> tuple[Block[_TSource], Block[_TSource]]:
def partition(self, predicate: Callable[[_TSource], bool]) -> tuple[Block[_TSource], Block[_TSource]]:
"""Partition block.
Splits the collection into two collections, containing the
Expand Down Expand Up @@ -429,9 +417,7 @@ def tail(self) -> Block[_TSource]:
_, *tail = self._value
return Block(tail)

def sort(
self: Block[_TSourceSortable], reverse: bool = False
) -> Block[_TSourceSortable]:
def sort(self: Block[_TSourceSortable], reverse: bool = False) -> Block[_TSourceSortable]:
"""Sort list directly.
Returns a new sorted collection.
Expand All @@ -444,9 +430,7 @@ def sort(
"""
return Block(builtins.sorted(self._value, reverse=reverse))

def sort_with(
self, func: Callable[[_TSource], Any], reverse: bool = False
) -> Block[_TSource]:
def sort_with(self, func: Callable[[_TSource], Any], reverse: bool = False) -> Block[_TSource]:
"""Sort list with supplied function.
Returns a new sorted collection.
Expand Down Expand Up @@ -509,9 +493,7 @@ def try_head(self) -> Option[_TSource]:
return Nothing

@staticmethod
def unfold(
generator: Callable[[_TState], Option[tuple[_TSource, _TState]]], state: _TState
) -> Block[_TSource]:
def unfold(generator: Callable[[_TState], Option[tuple[_TSource, _TState]]], state: _TState) -> Block[_TSource]:
"""Unfold block.
Returns a list that contains the elements generated by the
Expand Down Expand Up @@ -591,16 +573,12 @@ def append(source: Block[_TSource], other: Block[_TSource]) -> Block[_TSource]:


@curry_flip(1)
def choose(
source: Block[_TSource], chooser: Callable[[_TSource], Option[_TResult]]
) -> Block[_TResult]:
def choose(source: Block[_TSource], chooser: Callable[[_TSource], Option[_TResult]]) -> Block[_TResult]:
return source.choose(chooser)


@curry_flip(1)
def collect(
source: Block[_TSource], mapping: Callable[[_TSource], Block[_TResult]]
) -> Block[_TResult]:
def collect(source: Block[_TSource], mapping: Callable[[_TSource], Block[_TResult]]) -> Block[_TResult]:
"""Collect block.
For each element of the list, applies the given function.
Expand Down Expand Up @@ -637,9 +615,7 @@ def cons(head: _TSource, tail: Block[_TSource]) -> Block[_TSource]:


@curry_flip(1)
def filter(
source: Block[_TSource], predicate: Callable[[_TSource], bool]
) -> Block[_TSource]:
def filter(source: Block[_TSource], predicate: Callable[[_TSource], bool]) -> Block[_TSource]:
"""Filter elements in block.
Returns a new collection containing only the elements of the
Expand Down Expand Up @@ -750,9 +726,7 @@ def is_empty(source: Block[Any]) -> bool:


@curry_flip(1)
def map(
source: Block[_TSource], mapper: Callable[[_TSource], _TResult]
) -> Block[_TResult]:
def map(source: Block[_TSource], mapper: Callable[[_TSource], _TResult]) -> Block[_TResult]:
"""Map list.
Builds a new collection whose elements are the results of applying
Expand Down Expand Up @@ -798,16 +772,12 @@ def reduce(


@overload
def starmap(
mapper: Callable[[_T1, _T2], _TResult]
) -> Callable[[Block[tuple[_T1, _T2]]], Block[_TResult]]:
def starmap(mapper: Callable[[_T1, _T2], _TResult]) -> Callable[[Block[tuple[_T1, _T2]]], Block[_TResult]]:
...


@overload
def starmap(
mapper: Callable[[_T1, _T2, _T3], _TResult]
) -> Callable[[Block[tuple[_T1, _T2, _T3]]], Block[_TResult]]:
def starmap(mapper: Callable[[_T1, _T2, _T3], _TResult]) -> Callable[[Block[tuple[_T1, _T2, _T3]]], Block[_TResult]]:
...


Expand Down Expand Up @@ -838,22 +808,16 @@ def mapper_(args: tuple[Any, ...]) -> Any:
return map(mapper_)


def map2(
mapper: Callable[[_T1, _T2], _TResult]
) -> Callable[[Block[tuple[_T1, _T2]]], Block[_TResult]]:
def map2(mapper: Callable[[_T1, _T2], _TResult]) -> Callable[[Block[tuple[_T1, _T2]]], Block[_TResult]]:
return starmap(mapper)


def map3(
mapper: Callable[[_T1, _T2, _T3], _TResult]
) -> Callable[[Block[tuple[_T1, _T2, _T3]]], Block[_TResult]]:
def map3(mapper: Callable[[_T1, _T2, _T3], _TResult]) -> Callable[[Block[tuple[_T1, _T2, _T3]]], Block[_TResult]]:
return starmap(mapper)


@curry_flip(1)
def mapi(
source: Block[_TSource], mapper: Callable[[int, _TSource], _TResult]
) -> Block[_TResult]:
def mapi(source: Block[_TSource], mapper: Callable[[int, _TSource], _TResult]) -> Block[_TResult]:
"""Map list with index.
Builds a new collection whose elements are the results of
Expand Down Expand Up @@ -980,9 +944,7 @@ def sort(


@curry_flip(1)
def sort_with(
source: Block[_TSource], func: Callable[[_TSource], Any], reverse: bool = False
) -> Block[_TSource]:
def sort_with(source: Block[_TSource], func: Callable[[_TSource], Any], reverse: bool = False) -> Block[_TSource]:
"""Returns a new collection sorted using "func" key function.
Args:
Expand All @@ -1001,9 +963,7 @@ def sum(source: Block[_TSourceSum | Literal[0]]) -> _TSourceSum | Literal[0]:


@curry_flip(1)
def sum_by(
source: Block[_TSourceSum], projection: Callable[[_TSourceSum], _TResult]
) -> _TResult:
def sum_by(source: Block[_TSourceSum], projection: Callable[[_TSourceSum], _TResult]) -> _TResult:
xs = source.map(projection)
return builtins.sum(xs) # type: ignore

Expand Down Expand Up @@ -1062,9 +1022,7 @@ def try_head(source: Block[_TSource]) -> Option[_TSource]:


@curry_flip(1)
def unfold(
state: _TState, generator: Callable[[_TState], Option[tuple[_TSource, _TState]]]
) -> Block[_TSource]:
def unfold(state: _TState, generator: Callable[[_TState], Option[tuple[_TSource, _TState]]]) -> Block[_TSource]:
"""Unfold block.
Returns a list that contains the elements generated by the
Expand Down
Loading

0 comments on commit 7d87c35

Please sign in to comment.