Subclassing processes

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Subclassing processes

David Huard
Hi all, 

I often find myself in situations where it would be useful to subclass a Process and avoid copy-pasting shared inputs and outputs. The way most processes are written does not lend itself well to this, inputs being instance objects rather than class objects. I was wondering if any of you had experience with an elegant way to subclass pywps processes that is readable and does not pollute the class namespace too much. 

Thanks, 

david

_______________________________________________
pywps-dev mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/pywps-dev
Reply | Threaded
Open this post in threaded view
|

Re: Subclassing processes

Luí­s Moreira de Sousa
Hi David,

I have done this in the past, pretty much as you say, by promoting the inputs and outputs objects to class properties. Other elements like title, abstract, etc, can be treated in the same way. This would all go into an "abstract" class from which actual process inherited. Perhaps there might be more elegant solutions, but to me this setting served well (I am a man of simple tastes).

All that said, it could make sense defining inputs, outputs, etc as class properties in the Process class itself. Something to think about.

Happy coding. Cheers.

--
Luís Moreira de Sousa
Bowlespark 8
6701 DN Wageningen
The Netherlands
Phone: +31 628 544 755
RingID: ring:7ca91d83f4f9dec82fec9f1144b8e5c1ef2a110c

Sent with ProtonMail Secure Email.

‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
On April 11, 2018 11:51 PM, David Huard <[hidden email]> wrote:

Hi all, 

I often find myself in situations where it would be useful to subclass a Process and avoid copy-pasting shared inputs and outputs. The way most processes are written does not lend itself well to this, inputs being instance objects rather than class objects. I was wondering if any of you had experience with an elegant way to subclass pywps processes that is readable and does not pollute the class namespace too much. 

Thanks, 

david


_______________________________________________
pywps-dev mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/pywps-dev
Reply | Threaded
Open this post in threaded view
|

Re: Subclassing processes

David Huard
Hi Luis, 

Isn't there a risk for different Process instances to share the same Input instances ? It seems to me that that could lead to weird behavior. 

Cheers, 

David

On Sat, Apr 14, 2018 at 2:18 PM Luí­s Moreira de Sousa <[hidden email]> wrote:
Hi David,

I have done this in the past, pretty much as you say, by promoting the inputs and outputs objects to class properties. Other elements like title, abstract, etc, can be treated in the same way. This would all go into an "abstract" class from which actual process inherited. Perhaps there might be more elegant solutions, but to me this setting served well (I am a man of simple tastes).

All that said, it could make sense defining inputs, outputs, etc as class properties in the Process class itself. Something to think about.

Happy coding. Cheers.

--
Luís Moreira de Sousa
Bowlespark 8
6701 DN Wageningen
The Netherlands
Phone: +31 628 544 755
RingID: ring:7ca91d83f4f9dec82fec9f1144b8e5c1ef2a110c

Sent with ProtonMail Secure Email.

‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
On April 11, 2018 11:51 PM, David Huard <[hidden email]> wrote:

Hi all, 

I often find myself in situations where it would be useful to subclass a Process and avoid copy-pasting shared inputs and outputs. The way most processes are written does not lend itself well to this, inputs being instance objects rather than class objects. I was wondering if any of you had experience with an elegant way to subclass pywps processes that is readable and does not pollute the class namespace too much. 

Thanks, 

david

_______________________________________________
pywps-dev mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/pywps-dev

_______________________________________________
pywps-dev mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/pywps-dev
Reply | Threaded
Open this post in threaded view
|

Re: Subclassing processes

Luí­s Moreira de Sousa
Hi again David,

that is the quark, strangeness and charm of Python, class properties are simultaneously static and instance properties. If you are not aware of this, then yes, you might risk "misreferencing" properties. This usually happens when you invoke an instance property that has not yet been modified/assigned. I do not find think this to be a major problem, as long as you always access properties through the instances.

Cheers.

--
Luís Moreira de Sousa
Bowlespark 8
6701 DN Wageningen
The Netherlands
Phone: +31 628 544 755
RingID: ring:7ca91d83f4f9dec82fec9f1144b8e5c1ef2a110c

Sent with ProtonMail Secure Email.

‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
On April 16, 2018 3:40 PM, David Huard <[hidden email]> wrote:

Hi Luis, 

Isn't there a risk for different Process instances to share the same Input instances ? It seems to me that that could lead to weird behavior. 

Cheers, 

David

On Sat, Apr 14, 2018 at 2:18 PM Luí­s Moreira de Sousa <[hidden email]> wrote:
Hi David,

I have done this in the past, pretty much as you say, by promoting the inputs and outputs objects to class properties. Other elements like title, abstract, etc, can be treated in the same way. This would all go into an "abstract" class from which actual process inherited. Perhaps there might be more elegant solutions, but to me this setting served well (I am a man of simple tastes).

All that said, it could make sense defining inputs, outputs, etc as class properties in the Process class itself. Something to think about.

Happy coding. Cheers.

--
Luís Moreira de Sousa
Bowlespark 8
6701 DN Wageningen
The Netherlands
Phone: +31 628 544 755
RingID: ring:7ca91d83f4f9dec82fec9f1144b8e5c1ef2a110c

Sent with ProtonMail Secure Email.

‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
On April 11, 2018 11:51 PM, David Huard <[hidden email]> wrote:

Hi all, 

I often find myself in situations where it would be useful to subclass a Process and avoid copy-pasting shared inputs and outputs. The way most processes are written does not lend itself well to this, inputs being instance objects rather than class objects. I was wondering if any of you had experience with an elegant way to subclass pywps processes that is readable and does not pollute the class namespace too much. 

Thanks, 

david

_______________________________________________
pywps-dev mailing list


_______________________________________________
pywps-dev mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/pywps-dev
Reply | Threaded
Open this post in threaded view
|

Re: Subclassing processes

David Huard
Yes, but imagine ProcessB is a subclass of ProcessA, and ProcessA's inputs include LiteralInput instances. Then, ProcessB's inputs refer to the same instances as ProcessA, which seems risky. In my mind, ProcessB should instantiate its own LiteralInput instances, but I haven't found a way yet to do this that doesn't feel clunky and unpythonic. 

To clarify: 

class ProcessA:
    in1 = LiteralInput(...)

    def __init__(...):
        ...
        inputs=[self.in1,...]


class ProcessB:
    in2 = LiteralInput()

        def __init__(...):
            ...
            inputs=[self.in1, self.in2, ...]

I suppose one option is to deep copy the input instances: 

inputs=[self.in1.clone(), self.in2.clone(), ...]



On Mon, Apr 16, 2018 at 1:06 PM Luí­s Moreira de Sousa <[hidden email]> wrote:
Hi again David,

that is the quark, strangeness and charm of Python, class properties are simultaneously static and instance properties. If you are not aware of this, then yes, you might risk "misreferencing" properties. This usually happens when you invoke an instance property that has not yet been modified/assigned. I do not find think this to be a major problem, as long as you always access properties through the instances.

Cheers.

--
Luís Moreira de Sousa
Phone: +31 628 544 755
RingID: ring:7ca91d83f4f9dec82fec9f1144b8e5c1ef2a110c

Sent with ProtonMail Secure Email.

‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
On April 16, 2018 3:40 PM, David Huard <[hidden email]> wrote:

Hi Luis, 

Isn't there a risk for different Process instances to share the same Input instances ? It seems to me that that could lead to weird behavior. 

Cheers, 

David

On Sat, Apr 14, 2018 at 2:18 PM Luí­s Moreira de Sousa <[hidden email]> wrote:
Hi David,

I have done this in the past, pretty much as you say, by promoting the inputs and outputs objects to class properties. Other elements like title, abstract, etc, can be treated in the same way. This would all go into an "abstract" class from which actual process inherited. Perhaps there might be more elegant solutions, but to me this setting served well (I am a man of simple tastes).

All that said, it could make sense defining inputs, outputs, etc as class properties in the Process class itself. Something to think about.

Happy coding. Cheers.

--
Luís Moreira de Sousa
Phone: +31 628 544 755
RingID: ring:7ca91d83f4f9dec82fec9f1144b8e5c1ef2a110c

Sent with ProtonMail Secure Email.

‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
On April 11, 2018 11:51 PM, David Huard <[hidden email]> wrote:

Hi all, 

I often find myself in situations where it would be useful to subclass a Process and avoid copy-pasting shared inputs and outputs. The way most processes are written does not lend itself well to this, inputs being instance objects rather than class objects. I was wondering if any of you had experience with an elegant way to subclass pywps processes that is readable and does not pollute the class namespace too much. 

Thanks, 

david

_______________________________________________
pywps-dev mailing list


_______________________________________________
pywps-dev mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/pywps-dev