3 ms·
You mean something like https://pyotr.readthedocs.io https://pyotr.readthedocs.io
by BerislavLopac 5y ago
You mean something like https://pyotr.readthedocs.io https://pyotr.readthedocs.io
- KronisLV 5y agoThanks for the link, but not necessarily. How WSDL and the code generation around it worked, was that you'd have a specification of the web API (much like OpenAPI attempts to do), which you could feed into any number of code generators, to get output code which has no coupling to the actual generator at runtime, whereas Pyotr is geared more towards validation and goes into the opposite direction: https://pyotr.readthedocs.io/en/latest/client/ https://pyotr.readthedocs.io/en/latest/client/ The best analogy that i can think of is how you can also do schema first application development - you do your SQL migrations (ideally in an automated way as well) and then just run a command locally to generate all of the data access classes and/or models for your database tables within your application. That way, you save your time for 80% of the boring and repetitive stuff while minimizing the risks of human error and inconsistencies, with nothing preventing you from altering the generated code if you have specific needs (outside of needing to make it non overrideable, for example, a child class of a generated class). Of course, there's no reason why this can't be applied to server code either - write the spec first and generate stubs for endpoints that you'll just fill out. Similarly there shouldn't be a need for a special client to generate stubs for OpenAPI, the closest that Python in particular has for now is this https://github.com/openapi-generators/openapi-python-client https://github.com/openapi-generators/openapi-python-client However, for some reason, model driven development never really took off, outside of niche frameworks, like JHipster: https://www.jhipster.tech/ https://www.jhipster.tech/ Furthermore, for whatever reason formal specs for REST APIs also never really got popular and aren't regarded as the standard, which to me seems silly: every bit of client code that you write will need a specific version to work against, which should be formalized.
- BerislavLopac 5y agoWell, in Python, code generation is an anti-pattern.
- KronisLV 5y ago> Well, in Python, code generation is an anti-pattern. Hmm, i don't think that i've ever heard of this. Would you care to provide any sources, since that sounds like an interesting stance to take? So far, it seems like frameworks like Django don't have an issue with CLI tools to generate bits of code, i.e. https://docs.djangoproject.com/en/3.2/intro/tutorial01/ https://docs.djangoproject.com/en/3.2/intro/tutorial01/ If this is your first time using Django, you’ll have to take care of some initial setup. Namely, you’ll need to auto-generate some code that establishes a Django project – a collection of settings for an instance of Django, including database configuration, Django-specific options and application-specific settings. $ django-admin startproject mysite Similarly, PyCharm doesn't seem to have an issue with offering to generate methods for classes (ALT + INSERT), such as override methods (__class__, __init__, __new__, __setattr__, __eq__, __ne__, __str__, __repr__, __hash__, __format__, __getattribute__, __delattr__, __sizeof__, __reduce__, __reduce_ex__, __dir__, __init__), implementing methods, generating tests and copyright information. I don't see why CLI tools would be treated any differently or why code generation should be considered an anti-pattern since it's additive in nature and is entirely optional, hence asking to learn more.
- BerislavLopac 5y agoFirst of all, just because a tool or project uses a pattern, it doesn't mean that it's a good idea. Second, code generation as part of IDE or one-time setup is something else. I need to clarify: when I say that "code generation" is an anti-pattern, I'm talking about the traditional, two-step process where you generate some code in one process, and then execute it in another. But Python works really well with a different type of "code generation". Someone once said that the only thing missing from Python is a macro language; but that is not true - Python has its own macro language, and it's called Python. Python is dynamically evaluated and executed, so there is no reason why we need two separate steps when generating code dynamically; in Python, the right way is not to dynamically construct the textual representation of code, but rather to dynamically construct runtime entities (classes, functions etc), and then use them straight away, in the same process. Unless you're dynamically building hundreds of such constructs (and if you do you have a bigger problem), any performance impact is negligible.
- nsonha 5y ago> model driven development never really took off same as to why REST is now not a hot thing anymore, the idea that your API is just a dumb wrapper around data model is poor api design. API-driven development didn't really took off either, that is write your spec in grpc/OpenAPI and have the plumbing code generated in both ends. It's technically already there with various tools, but because of dogma like "code generation is bad", quality of code generators, or whatever reason, we're still writting "API code"