0

Imagine, we have a some files:
scheme.py
scheme.One.py
scheme.Two.py
sceme.*.py
...

In file 'scheme.py' we have the common class code with all class attributes we need.

class Scheme:
    _VAR = "Variable"
    def function_common(self):
        pass
    def function_first(self):
        return "Main_Scheme"
    def function_second(self):
        return "Common_Scheme"

In other files we have the ONLY PARTICULAR attributes, which we want to replace in common class.

File 'scheme.One.py':

class Scheme:
    _VAR = "Scheme1"
    def function_first(self):
        return "Scheme_One"

File 'scheme.Two.py':

class Scheme:
    _VAR = "Scheme2"
    def function_second(self):
        return "Second_Scheme"

We need to determine the close scheme by some parameters (not in question) and get the appropriate class Scheme.
What is the best practice for this case, if we need to get like a "Factory of classes" in file 'scheme.py'?
I'm not PRO in Python.
Please, thorough answer...
Any Python version (>=3.74), appreciate for modern solutions...
Thanks very much!!!

Joe
  • 6,758
  • 2
  • 26
  • 47
siroBS
  • 29
  • 3

2 Answers2

0

You can have a factory which encapsulates all your specified files.

Imagine you have another separate module with a function: makeS3()

class Main_Scheme:
    _VAR = "Variable"
    def function_common(self):
        pass
    def function_first(self):
        return "Main_Scheme"
    def function_second(self):
        return "Common_Scheme"

class Scheme1:
    _VAR = "Scheme1"
    def function_first(self):
        return "Scheme_One"

class Scheme2:
    _VAR = "Scheme2"
    def function_second(self):
        return "Second_Scheme"

_mixins = {"Scheme1":Scheme1, "Scheme2":Scheme2}

def makeS3(mixin):
    class Scheme3(_mixins[mixin], Main_Scheme):
        pass
    return Scheme3()

Your clients can specify which mixin to take:

s3 = makeS3('Scheme1')
print(s3._VAR)

s3 = makeS3('Scheme2')
print(s3._VAR)
quamrana
  • 37,849
  • 12
  • 53
  • 71
  • Thanks,but..Firstly,the crucial idea to have a specific file for each 'scheme case',where part of the filename we can use as information for a script which determines case we can apply.So,we don't need a dict "_mixins",as in your example.Plus,it's really simple for end-users,who want to use this system-just create one file with some new class attributes in file begin and same class name-just "Scheme". Secondly,the 'dark side' for me is composition of function which construct destination class from copy "Scheme" class by replacing in it particular attributes from selected file with new scheme. – siroBS Nov 29 '19 at 19:05
  • So do you really mean that end-users supply their own file with: `class SchemeX(Main_Scheme): _VAR = "SchemeX" #plus methods` ? – quamrana Nov 29 '19 at 20:26
  • Yep. In _VAR they are can put specific information about applicability their own scheme (in each file with the scheme is only one class "Scheme"). The determine script observe all such files, recognize the appropriate file with a scheme, get the main class Scheme from file 'Scheme.py' with all possible attributes and replace those of it, which present in a particular file. – siroBS Nov 29 '19 at 20:52
0

I have a modified version of @quamrana's solution. It uses a factory class instead of a factory function, so you might be more flexible if you want to use __init__() somewhere:

class Main_Scheme:
    _VAR = "Variable"
    def function_common(self):
        pass
    def function_first(self):
        return "Main_Scheme"
    def function_second(self):
        return "Common_Scheme"

class Scheme1(Main_Scheme):
    _VAR = "Scheme1"
    def function_first(self):
        return "Scheme_One"

class Scheme2(Main_Scheme):
    _VAR = "Scheme2"
    def function_second(self):
        return "Second_Scheme"

_mixins = {"Scheme1":Scheme1, "Scheme2":Scheme2}


class Scheme:

    def __new__(cls, mixin):

         return _mixins[mixin]()


s3 = Scheme('Scheme1')
print(s3._VAR)

s3 = Scheme('Scheme2')
print(s3._VAR)

(Inspired by https://stackoverflow.com/a/5953974/7919597)

Joe
  • 6,758
  • 2
  • 26
  • 47
  • Thanks, but... It seems to me, class "Main_Scheme" isn't used in your example. What is if class "Scheme1" doesn't have the attribute "_VAR"? In this case, our 'Factory' must return the object "Scheme" with attribute "_VAR" from "Main_Scheme". 'Factory' always must return the whole "Main_Scheme" class but replace those attributes from appropriate file (Scheme.One.py, Scheme.Two.py) which present in this file. – siroBS Nov 29 '19 at 20:29