Skip to content

Commit

Permalink
add SwapKV
Browse files Browse the repository at this point in the history
  • Loading branch information
tandav committed Apr 13, 2024
1 parent 5fdc1c7 commit 1f71006
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 7 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ jobs:
run: python -m twine upload dist/*

- name: Github Release
uses: softprops/action-gh-release@v1
uses: softprops/action-gh-release@v2

mkdocs-github-pages:
if: "!startsWith(github.ref, 'refs/tags/')"
Expand All @@ -93,7 +93,7 @@ jobs:
- uses: actions/setup-python@v5
with:
python-version: 3.12
- uses: actions/cache@v3
- uses: actions/cache@v4
with:
key: ${{ github.ref }}
path: .cache
Expand Down
10 changes: 5 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ ci:
autoupdate_schedule: quarterly
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
rev: v4.6.0
hooks:
- id: check-added-large-files
- id: check-yaml
Expand All @@ -29,19 +29,19 @@ repos:
- id: add-trailing-comma

- repo: https://github.com/asottile/pyupgrade
rev: v3.15.0
rev: v3.15.2
hooks:
- id: pyupgrade
args: [--py311-plus]

- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.1.7
rev: v0.3.7
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]

- repo: https://github.com/PyCQA/pylint
rev: v3.0.1
rev: v3.1.0
hooks:
- id: pylint
additional_dependencies: ["pylint-per-file-ignores"]
Expand All @@ -52,7 +52,7 @@ repos:
# - id: autopep8

- repo: https://github.com/PyCQA/flake8
rev: 6.1.0
rev: 7.0.0
hooks:
- id: flake8
additional_dependencies: [Flake8-pyproject]
8 changes: 8 additions & 0 deletions docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,14 @@ Same as `FilterKeys` but for `v` in `(k, v)` pairs

```

## SwapKV

```py
>>> [(0, 1), (2, 3)] | SwapKV() | Pipe(list)
[(1, 0), (3, 2)]

```

## Grep

```py
Expand Down
2 changes: 2 additions & 0 deletions docs/similar-tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@
- [petl-developers/petl: Python Extract Transform and Load Tables of Data](https://github.com/petl-developers/petl)
- [Show HN: Pypipe – A Python command-line tool for pipeline processing | Hacker News](https://news.ycombinator.com/item?id=37981683)
- [Marcel the Shell | Hacker News](https://news.ycombinator.com/item?id=37991746)
- [cgarciae/pypeln: Concurrent data pipelines in Python >>>](https://github.com/cgarciae/pypeln)
- [sfermigier/awesome-functional-python: A curated list of awesome things related to functional programming in Python.](https://github.com/sfermigier/awesome-functional-python)
1 change: 1 addition & 0 deletions pipe21.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class ValueBy (B): __ror__ = lambda self, it: ((x, self.f(x)) for x in it)
class Append (B): __ror__ = lambda self, it: ((*x, self.f(x)) for x in it)
class Keys (B): __ror__ = lambda self, it: (k for k, v in it)
class Values (B): __ror__ = lambda self, it: (v for k, v in it)
class SwapKV (B): __ror__ = lambda self, it: it | Map(operator.itemgetter(1, 0))
class Grep (B): __ror__ = lambda self, it: it | (FilterFalse if self.kw.get('v', False) else Filter)(re.compile(self.f, flags=re.IGNORECASE if self.kw.get('i', False) else 0).search)
class IterLines (B): __ror__ = lambda self, f: (x.strip() if self.kw.get('strip', True) else x for x in open(f))
class Count (B): __ror__ = lambda self, it: sum(1 for _ in it)
Expand Down
9 changes: 9 additions & 0 deletions tests/pipe_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,15 @@ def test_values(it, expected):
assert it | Values() | Pipe(list) == expected


@pytest.mark.parametrize(
('it', 'expected'), [
([(0, 1), (2, 3)], [(1, 0), (3, 2)]),
],
)
def test_swap_kv(it, expected):
assert it | SwapKV() | Pipe(list) == expected


@pytest.mark.parametrize(
('it', 'grep', 'expected'), [
(['hello foo', 'world', 'awesome FOo'], 'foo', ['hello foo']),
Expand Down

0 comments on commit 1f71006

Please sign in to comment.