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): allow use of Config object as a function decorator #9307

Merged
merged 1 commit into from
Jun 9, 2023

Conversation

alexander-beedie
Copy link
Collaborator

@alexander-beedie alexander-beedie commented Jun 8, 2023

A nice usability trick I picked up recently; transparent use of context managers as function decorators. Allows for the application of specific Config options to be scoped to the duration of a given function:

Examples

@pl.Config( set_ascii_tables=True )
def write_ascii_frame_to_stdout( df: pl.DataFrame ) -> None:
    sys.stdout.write( str(df) )
@pl.Config( verbose=True )
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 Jun 8, 2023
@erinov1
Copy link

erinov1 commented Jun 8, 2023

This would be really nice to have for StringCache as well!

@alexander-beedie
Copy link
Collaborator Author

alexander-beedie commented Jun 8, 2023

This would be really nice to have for StringCache as well!

@erinov1: you read my mind... :)
Done - #9309

@ritchie46
Copy link
Member

ritchie46 commented Jun 9, 2023

That's really nice! So this only gets activated on function call?

Just read about the https://docs.python.org/3/library/contextlib.html#contextlib.ContextDecorator. Nice stuff!

@ritchie46 ritchie46 merged commit 9a673f5 into pola-rs:main Jun 9, 2023
12 checks passed
@alexander-beedie alexander-beedie deleted the config-as-context-manager branch June 9, 2023 05:54
SeanTroyUWO pushed a commit to SeanTroyUWO/polars that referenced this pull request Jun 20, 2023
c-peters pushed a commit to c-peters/polars that referenced this pull request Jul 14, 2023
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.

None yet

3 participants