Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Factory method in python

Tags:

python

I'm working to the following little project: https://github.com/AndreaCrotti/project-organizer

Which in short aims to manage many different projects much more easily. One of the useful things is a way to detect automatically the kind of project that I'm working on, to set up correctly some commands.

At the moment I'm using a classmethod "match" function and a detect function which iterates over the various "match". I'm sure there might be a better design for this, but can't find it.

Any ideas?

class ProjectType(object):
    build_cmd = ""

    @classmethod
    def match(cls, _):
        return True


class PythonProject(ProjectType):
    build_cmd = "python setup.py develop --user"

    @classmethod
    def match(cls, base):
        return path.isfile(path.join(base, 'setup.py'))


class AutoconfProject(ProjectType):
    #TODO: there should be also a way to configure it
    build_cmd = "./configure && make -j3"

    @classmethod
    def match(cls, base):
        markers = ('configure.in', 'configure.ac', 'makefile.am')
        return any(path.isfile(path.join(base, x)) for x in markers)


class MakefileOnly(ProjectType):
    build_cmd = "make"

    @classmethod
    def match(cls, base):
        # if we can count on the order the first check is not useful
        return (not AutoconfProject.match(base)) and \
            (path.isfile(path.join(base, 'Makefile')))


def detect_project_type(path):
    prj_types = (PythonProject, AutoconfProject, MakefileOnly, ProjectType)
    for p in prj_types:
        if p.match(path):
            return p()
like image 912
andrea_crotti Avatar asked Nov 14 '11 22:11

andrea_crotti


People also ask

What is a factory class in Python?

A Class Factory is a function that creates and returns a class. It is one of the powerful patterns in Python.

What is the use of Factory Method?

Factory Method Pattern allows the sub-classes to choose the type of objects to create. It promotes the loose-coupling by eliminating the need to bind application-specific classes into the code.

What is meant by Factory Method?

Factory method is a creational design pattern which solves the problem of creating product objects without specifying their concrete classes. The Factory Method defines a method, which should be used for creating objects instead of using a direct constructor call ( new operator).

Is Factory pattern useful in Python?

Usage examples: The Factory Method pattern is widely used in Python code. It's very useful when you need to provide a high level of flexibility for your code. Identification: Factory methods can be recognized by creation methods that construct objects from concrete classes.


1 Answers

This was a reasonable use of a factory function as a class method.

One possible improvement is to have all the classes inherit from a single parent class which would have a single classmethod that incorporated all the logic in detect_project_type.

Perhaps something like this would work:

class ProjectType(object):
    build_cmd = ""
    markers = []

    @classmethod
    def make_project(cls, path):
        prj_types = (PythonProject, AutoconfProject, MakefileOnly, ProjectType)
        for p in prj_types:
            markers = p.markers
            if any(path.isfile(path.join(path, x)) for x in markers):
                return p()

class PythonProject(ProjectType):
    build_cmd = "python setup.py develop --user"
    markers = ['setup.py']

class AutoconfProject(ProjectType):
    #TODO: there should be also a way to configure it
    build_cmd = "./configure && make -j3"
    markers = ['configure.in', 'configure.ac', 'makefile.am']

class MakefileOnly(ProjectType):
    build_cmd = "make"
    markers = ['Makefile']
like image 152
Raymond Hettinger Avatar answered Oct 05 '22 09:10

Raymond Hettinger