0

I'm writing a SOAP web service for Django which will have a lot of functions. To keep it clean, I would like to split it in several files.

How do I "include" my functions code into my class (like in PHP).

For example:

class SOAPService(ServiceBase):
  #first file
  from myfunctions1 import *
  #second file
  from myfunctionsA import *

Should behave like:

class SOAPService(ServiceBase):
  #first file
  def myfunction1(ctx):
    return

  def myfunction2(ctx, arg1):
    return

  #second file
  def myfunctionA(ctx):
    return

  def myfunctionB(ctx, arg1):
    return

   ...
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Quentin Hayot
  • 7,786
  • 6
  • 45
  • 62

2 Answers2

1

Python lets you inherit from multiple classes, so go ahead an put your methods into a separate base class and inherit from it, too.

Also, python lets you pull in from other files with the import statement.

katerinah
  • 111
  • 4
0

Although there are several ways to do this, the first one I would recommend is to create mixin classes from both files.

So in your main module

import myfunctions1
import myfunctionsA
class SOAPService(ServiceBase,myfunctions1.mixin,myfunctionsA.mixin):
    pass

and in your included modules myfunctions1, myfunctionA etc.

class mixin:
    def myfunction1(self):
        return
    def myfunction2(self, arg1):
        return
Andrew Johnson
  • 3,078
  • 1
  • 18
  • 24
  • The problem is, according to this comment, that I'm not supposed to mix ServiceBase with other classes http://stackoverflow.com/questions/24268065/restrict-spyne-soap-service-with-oauth2-provider#comment37556928_24268065 – Quentin Hayot Aug 15 '14 at 02:19
  • In this case I think it is ok. I didn't see anything in ServiceBase that would cause this mixin style to break. – Andrew Johnson Aug 15 '14 at 03:01