Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Putting polars API extensions in dedicated module – How to import from target module?

I want to extend polars API as described in the docs, like this:

@pl.api.register_expr_namespace("greetings")
class Greetings:
    def __init__(self, expr: pl.Expr):
        self._expr = expr

    def hello(self) -> pl.Expr:
        return (pl.lit("Hello ") + self._expr).alias("hi there")

    def goodbye(self) -> pl.Expr:
        return (pl.lit("SayĹŤnara ") + self._expr).alias("bye")

If I were to put the actual registration in a dedicated module (extensions.py), how am I supposed to import the methods from the respective class from within another module?

Going with the dataframe example in the docs, let’s say I put the following code in a module called target.py.
I need to make the greetings-namespace available. How can I do it, i.e. how excactly should the import look like?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

pl.DataFrame(data=["world", "world!", "world!!"]).select(
    [
        pl.all().greetings.hello(),
        pl.all().greetings.goodbye(),
    ]
)

>Solution :

You could just import extensions(assuming that the file is relative) in your target.py file to register the greetings namespace.

import polars as pl
import extensions # noqa: F401

pl.DataFrame(data=["world", "world!", "world!!"]).select(
    [
        pl.all().greetings.hello(),
        pl.all().greetings.goodbye(),
    ]
)

See also:

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading