Question

I am using Flask-Restful for implementing REST api in my python application. I have a model module, a business module and a controller module. This is one of my controllers defined in a login_controller.py file

from flask.ext.restful import Resource,request,reqparse
from app.business.login import Login
from app.models.models import User, Address

class Login_Controller(Resource):

    def __init__(self):
        pass
    def get(self):
        loginBO=Login()
        obj=loginBO.getAllUsers()
        return {"users":obj}, 201
    def post(self):
       pass
    def delete(self):
        loginBO=Login()
        status =loginBO.deleteUser(request.json)
        if status:
            return {"status":"true"},201
        else:
            return {"status":"false"},401
    def put(self):
        loginBO=Login()
        status =loginBO.addUser(request.json)
        if status:
            return {"status":"true"},201
        else:
            return {"status":"false"},401

Now i am not really satisfied with naming the above file a controller? What is the "PYTHONIC" way to

  1. Name the file
  2. Name the class.

I have seen some people naming these classes as views. This seems fine if i was routing to a template but i am serving plain old json ? What say you guys ?

Was it helpful?

Solution

Python standard naming conventions are shown in PEP 8. You asked about filenames and class names. The convention for package and module names (which typically are your filenames):

Modules should have short, all-lowercase names. Underscores can be used in the module name if it improves readability. Python packages should also have short, all-lowercase names, although the use of underscores is discouraged.

Since module names are mapped to file names, and some file systems are case insensitive and truncate long names, it is important that module names be chosen to be fairly short -- this won't be a problem on Unix, but it may be a problem when the code is transported to older Mac or Windows versions, or DOS.

When an extension module written in C or C++ has an accompanying Python module that provides a higher level (e.g. more object oriented) interface, the C/C++ module has a leading underscore (e.g. _socket).

And for class names:

Almost without exception, class names use the CapWords convention. Classes for internal use have a leading underscore in addition.

That's it. There are no conventions for whether you call it a "controller" or "view" or "resource", or even if you even have something like "Controller" or "View" in the name at all. There are no official "python conventions" here, especially considering that web applications are only one small part of what Python can do. As long as the other developers on your project understand what is meant by "Controller" versus "View", you should be fine. Don't over think this one, and don't expect someone else to have the right answer as to what to name something; that is just our burden as developers :)

But, if you will call it LoginController, make sure it's LoginController, not Login_Controller.

OTHER TIPS

You can follow the PEP 8 (http://www.python.org/dev/peps/pep-0008/#package-and-module-names), it is more pythonic way. You can also use the Google Style Guide, I think fine.

In this specific case I would use ONE file named controller for all controllers, and my class would be named Login. But, if you realy prefer login.py for your module, then I think better named your controller with Controller.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top