opendal icon indicating copy to clipboard operation
opendal copied to clipboard

feat: Use factory mode to init the operator instance in Python binding

Open Zheaoli opened this issue 2 years ago • 12 comments

          This could be better
class Operator:
    @classmethod
    def from_config(scheme: str, **kwargs)-> Operator: ...

    @classmethod
    def from_async_operator(operator: AsyncOperator) -> Operator: ...

class AsyncOperator:
    @classmethod
    def from_config(scheme: str, **kwargs)-> AsyncOperator: ...

    @classmethod
    def from_operator(operator: Operator) -> AsyncOperator: ...

We use the factory mode instead of __init__ completely

Originally posted by @Zheaoli in https://github.com/apache/incubator-opendal/pull/3514#discussion_r1386123571

Zheaoli avatar Nov 08 '23 14:11 Zheaoli

I want to preserve the naming of from_config to make space for us to introduce RFC-3197: Config for bindings.

Xuanwo avatar Nov 08 '23 14:11 Xuanwo

By the way, is the API style like from_xxx widely used in python?

Xuanwo avatar Nov 09 '23 08:11 Xuanwo

By the way, is the API style like from_xxx widely used in python?

Nope, a normal way

https://github.com/mitmproxy/mitmproxy/blob/746537e0511e0316a144e05e7ba8cc6f6e44768b/mitmproxy/coretypes/serializable.py#L30-L37

https://github.com/mitmproxy/mitmproxy/blob/746537e0511e0316a144e05e7ba8cc6f6e44768b/mitmproxy/certs.py#L378-L406

Zheaoli avatar Nov 09 '23 08:11 Zheaoli

Nope, a normal way

I don't understand this statement.

Nope, a normal way means it does be widely used?

Xuanwo avatar Nov 09 '23 08:11 Xuanwo

Nope, a normal way

I don't understand this statement.

Nope, a normal way means it does be widely used?

Sorry for confusing

I mean, this is a normal style from_xxx in Python. Many project use this conversion in factory mode

https://github.com/search?q=%22def+from_%22+repo%3Atensorflow%2Ftensorflow&type=code&ref=advsearch

Zheaoli avatar Nov 09 '23 09:11 Zheaoli

I mean, this is a normal style from_xxx in Python.

Got it, thanks for sharing.

Xuanwo avatar Nov 09 '23 09:11 Xuanwo

I mean, this is a normal style from_xxx in Python.

Got it, thanks for sharing.

BTW, from_xxx is good pattern which is suggested in PEP8 "Explicit is better than implicit"

Zheaoli avatar Nov 09 '23 09:11 Zheaoli

Seems cool, would you like to propose an RFC for python binding about this change? We can place them inside bindings/python/docs/rfcs.


This is the first RFC for python binding!

Xuanwo avatar Nov 09 '23 09:11 Xuanwo

Seems cool, would you like to propose an RFC for python binding about this change? We can place them inside bindings/python/docs/rfcs.

This is the first RFC for python binding!

Of course!

Zheaoli avatar Nov 09 '23 09:11 Zheaoli

@Xuanwo BTW, would you mind me adding a new label "RFC needed` to represent an issue which okay but needs a RFC before submitting the code

Zheaoli avatar Nov 09 '23 09:11 Zheaoli

@Xuanwo BTW, would you mind me adding a new label "RFC needed` to represent an issue which okay but needs a RFC before submitting the code

I don't think we need this.

Xuanwo avatar Nov 09 '23 09:11 Xuanwo

I check the code, https://github.com/apache/opendal/pull/3514 added the instance method, not class method, and https://github.com/apache/incubator-opendal/pull/3197 is merged, so any other things need to be solved?

asukaminato0721 avatar Feb 26 '25 23:02 asukaminato0721