Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow to select a subset of columns #189

Merged
merged 14 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ crate-type = ["cdylib"]
[dependencies]
calamine = { version = "0.24.0", features = ["dates"] }
chrono = { version = "0.4.34", default-features = false }
pyo3 = { version = "0.20.3", features = ["extension-module", "abi3-py38"] }
# NOTE: "extension-module" is actually required, see comments on features below
pyo3 = { version = "0.20.3", features = ["abi3-py38"] }

[dependencies.arrow]
version = "50.0.0"
Expand All @@ -20,4 +21,14 @@ default-features = false
features = ["pyarrow"]

[dev-dependencies]
pretty_assertions = "1.4.0"
rstest = { version = "0.18.2", default-features = false }

# NOTE: This is a hack to bypass pyo3 limitations when testing:
# https://pyo3.rs/v0.20.3/faq.html#i-cant-run-cargo-test-or-i-cant-build-in-a-cargo-workspace-im-having-linker-issues-like-symbol-not-found-or-undefined-reference-to-_pyexc_systemerror
lukapeschke marked this conversation as resolved.
Show resolved Hide resolved
[features]
extension-module = ["pyo3/extension-module"]
default = ["extension-module"]
# feature for tests only. This makes Python::with_gil auto-initialize Python
# interpreters, which allows us ot instantiate Python objects in tests
PrettyWood marked this conversation as resolved.
Show resolved Hide resolved
tests = ["pyo3/auto-initialize"]
21 changes: 17 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,30 @@ pytest = pytest -v
## Rust
clippy = cargo clippy
fmt = cargo fmt
cargo-test = cargo test
cargo-test = cargo test --no-default-features --features tests
## Docs
pdoc = pdoc -o docs python/fastexcel

lint:
lint-python:
$(ruff)
$(format) --check --diff

lint-rust:
$(mypy)
PrettyWood marked this conversation as resolved.
Show resolved Hide resolved
$(clippy)
format:

lint: lint-rust lint-python

format-python:
$(ruff) --fix
$(format)

format-rust:
$(fmt)
$(clippy) --fix --lib -p fastexcel --allow-dirty --allow-staged

format: format-rust format-python

install-test-requirements:
pip install -U -r test-requirements.txt -r build-requirements.txt

Expand All @@ -39,10 +48,14 @@ dev-install:
prod-install:
./prod_install.sh

test:
test-rust:
$(cargo-test)

test-python:
$(pytest)

test: test-rust test-python

doc:
$(pdoc)

Expand Down
9 changes: 9 additions & 0 deletions python/fastexcel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
CalamineCellError,
CalamineError,
CannotRetrieveCellDataError,
ColumnNotFoundError,
FastExcelError,
InvalidParametersError,
SheetNotFoundError,
Expand Down Expand Up @@ -101,6 +102,7 @@ def load_sheet_by_name(
skip_rows: int = 0,
n_rows: int | None = None,
schema_sample_rows: int | None = 1_000,
use_columns: list[str] | list[int] | None = None,
) -> ExcelSheet:
"""Loads a sheet by name.

Expand All @@ -126,6 +128,7 @@ def load_sheet_by_name(
skip_rows=skip_rows,
n_rows=n_rows,
schema_sample_rows=schema_sample_rows,
use_columns=use_columns,
)
)

Expand All @@ -138,6 +141,7 @@ def load_sheet_by_idx(
skip_rows: int = 0,
n_rows: int | None = None,
schema_sample_rows: int | None = 1_000,
use_columns: list[str] | list[int] | None = None,
) -> ExcelSheet:
"""Loads a sheet by index.

Expand Down Expand Up @@ -165,6 +169,7 @@ def load_sheet_by_idx(
skip_rows=skip_rows,
n_rows=n_rows,
schema_sample_rows=schema_sample_rows,
use_columns=use_columns,
)
)

Expand All @@ -177,6 +182,7 @@ def load_sheet(
skip_rows: int = 0,
n_rows: int | None = None,
schema_sample_rows: int | None = 1_000,
use_columns: list[str] | list[int] | None = None,
PrettyWood marked this conversation as resolved.
Show resolved Hide resolved
) -> ExcelSheet:
"""Loads a sheet by name if a string is passed or by index if an integer is passed.

Expand All @@ -190,6 +196,7 @@ def load_sheet(
skip_rows=skip_rows,
n_rows=n_rows,
schema_sample_rows=schema_sample_rows,
use_columns=use_columns,
)
if isinstance(idx_or_name, int)
else self.load_sheet_by_name(
Expand All @@ -199,6 +206,7 @@ def load_sheet(
skip_rows=skip_rows,
n_rows=n_rows,
schema_sample_rows=schema_sample_rows,
use_columns=use_columns,
)
)

Expand All @@ -224,6 +232,7 @@ def read_excel(path: Path | str) -> ExcelReader:
"CalamineCellError",
"CalamineError",
"SheetNotFoundError",
"ColumnNotFoundError",
"ArrowError",
"InvalidParametersError",
"UnsupportedColumnTypeCombinationError",
Expand Down
13 changes: 3 additions & 10 deletions python/fastexcel/_fastexcel.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class _ExcelReader:
skip_rows: int = 0,
n_rows: int | None = None,
schema_sample_rows: int | None = 1_000,
use_columns: list[str] | list[int] | None = None,
) -> _ExcelSheet: ...
def load_sheet_by_idx(
self,
Expand All @@ -43,16 +44,7 @@ class _ExcelReader:
skip_rows: int = 0,
n_rows: int | None = None,
schema_sample_rows: int | None = 1_000,
) -> _ExcelSheet: ...
def load_sheet(
self,
idx_or_name: int | str,
*,
header_row: int | None = 0,
column_names: list[str] | None = None,
skip_rows: int = 0,
n_rows: int | None = None,
schema_sample_rows: int | None = 1_000,
use_columns: list[str] | list[int] | None = None,
) -> _ExcelSheet: ...
@property
def sheet_names(self) -> list[str]: ...
Expand All @@ -69,5 +61,6 @@ class CannotRetrieveCellDataError(FastExcelError): ...
class CalamineCellError(FastExcelError): ...
class CalamineError(FastExcelError): ...
class SheetNotFoundError(FastExcelError): ...
class ColumnNotFoundError(FastExcelError): ...
class ArrowError(FastExcelError): ...
class InvalidParametersError(FastExcelError): ...
Loading
Loading