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(python): Enable creation of independently reusable Config instances #20053

Merged
merged 1 commit into from
Nov 29, 2024

Conversation

alexander-beedie
Copy link
Collaborator

@alexander-beedie alexander-beedie commented Nov 28, 2024

At the moment we set the options on Config init (or via the associated @staticmethod); when used as a context manger or function decorator this means we need to explicitly supply all the options on each usage - we cannot currently reuse an instance because scope exit resets the options to the state they were in at scope entry, and having the same object enter a new scope will not set the options again (as the options are set at init).

  • This PR enables reusable Config instances. A new init parameter "apply_on_context_enter" is introduced which defers applying the indicated options until context/function scope is entered.

  • Because the options are now stored and set on scope entry, bundles of Config options can now be created/managed centrally and then cleanly reused throughout a codebase without having to set all the options explicitly each time.

  • Minor drive-by; added proper __eq__ and __ne__ comparison methods for Config instances.

  • Backwards-compatible update; no change to the current default behaviour.

Example

Declare some Config instances that take effect only when entering context/function scope:

import polars as pl

cfg_verbose = pl.Config(
  verbose=True,
  apply_on_context_enter=True,
)

cfg_markdown = pl.Config(
  tbl_formatting="MARKDOWN",
  float_precision=3,
  apply_on_context_enter=True,
)

Can now use (and reuse) these objects as decorators or context managers without having to explicitly re-declare all the parameters:

@cfg_markdown
def write_frame_to_stdout(df: pl.DataFrame) -> None:
    sys.stdout.write(str(df))

with cfg_markdown:
    print(df)

@cfg_verbose
def do_various_things():
    ...

@github-actions github-actions bot added enhancement New feature or an improvement of an existing feature python Related to Python Polars labels Nov 28, 2024
Copy link

codecov bot commented Nov 28, 2024

Codecov Report

Attention: Patch coverage is 90.47619% with 2 lines in your changes missing coverage. Please review.

Project coverage is 79.52%. Comparing base (0b218d8) to head (3dfb310).
Report is 11 commits behind head on main.

Files with missing lines Patch % Lines
py-polars/polars/config.py 90.47% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #20053      +/-   ##
==========================================
+ Coverage   79.50%   79.52%   +0.01%     
==========================================
  Files        1562     1563       +1     
  Lines      216939   217160     +221     
  Branches     2459     2462       +3     
==========================================
+ Hits       172479   172693     +214     
- Misses      43901    43907       +6     
- Partials      559      560       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@ritchie46 ritchie46 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice one!

@ritchie46 ritchie46 merged commit 44ddbc2 into pola-rs:main Nov 29, 2024
26 checks passed
@alexander-beedie alexander-beedie deleted the reusable-config-instances branch November 29, 2024 08:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or an improvement of an existing feature python Related to Python Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants