开发者

Best python/Django code generator? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

Closed 3 years ago.

Improve this question 开发者_如何学Python

very quick question. I am wondering if there are any software Django software that generates python coding automatically. If there are any, please let me know.


Let's not run-off new pythonistas with snide remarks... maybe the OP had legitimate needs for code generation.

Try these:

Cheetah

Cog

Jinja2

have fun!


I have never heard of python code generators. Python is a dynamic language where you can do almost anything. Instead of relying on static code generators known from the Java world, I would argue that you can write a couple of equivalent Python lines in most cases which is way more convenient.

In case you are looking for a Python lexer/parser, try pyparsing.


Check out Django Builder... is really cool... upload a models.py file and it generates better models.py, plus CRUD view templates, API/serializers, admin.py, urls.py, even tests!

https://mmcardle.github.io/django_builder/#!/home


The closest thing I have seen in Python for code generation is Python's __metaclass__ feature. For example, here is a simple metaclass to create readonly properties:

class ReadonlyProperties(type):
    def __init__(cls, name, bases, attrs):
        props = attrs.get("props",[])
        if props:
            # generate property for each name in propnames
            def defineProperty(p):
                return property(lambda self: getattr(self, '_'+p))
            for p,_ in props:
                setattr(cls, p, defineProperty(p))

            # generate wrapper for __init__ to initialize property values
            if "__init__" in attrs:
                setattr(cls, "__orig_init__", attrs["__init__"])
            else:
                setattr(cls, "__orig_init__", None)
            def new__init__fn(self, *args, **kwargs):
                if self.__orig_init__:
                    self.__orig_init__(*args)
                for p,pdefault in props:
                    if p in kwargs:
                        setattr(self, '_'+p, kwargs[p])
                    else:
                        setattr(self, '_'+p, pdefault)
            setattr(cls, "__init__", new__init__fn)

            # generate __str__ function
            def __repr__fn(self):
                return "%s(%s)" % (name, ','.join("%s=%s" % (p,getattr(self,p)) for p,_ in props))
            setattr(cls, "__repr__", __repr__fn)

            # don't need this class property any more
            delattr(cls, "props")

Now here is the metaclass in action:

class Coord3D(object):
    __metaclass__ = ReadonlyProperties
    props = [('x',0), ('y',0), ('z',0)]

pt = Coord3D(x=100, y=200)
print repr(pt)
print pt.x
print [n for n in dir(pt) if not n.startswith('__')]

Prints:

Coord3D(x=100,y=200,z=0)
100
['_x', '_y', '_z', 'x', 'y', 'z']

Assigning to pt.x will raise an AttributeError, since this is a readonly property.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜