librelist archives

« back to archive

Refactoring views into submodules

Refactoring views into submodules

From:
Jack Maney
Date:
2014-09-20 @ 00:02
Hello,

I'm still new to Flask, and still fairly new to Python in general (picked
it up because it's used at my newest job for data munging and analysis).
I've only used Flask on my own time, hacking away on a side project.

As seems to be the standard with Flask apps, I currently have all of my
routes sitting in a single file (views.py). In other words, my file
structure is like this:

```
.
|-- LICENSE
|-- README.md
|-- app
|   |-- __init__.py
|   |-- decorators
|   |   |-- __init__.py
|   |-- forms.py
|   |-- mailer.py
|   |-- models.py
|   |-- templates
|   |   |-- base.html
|   |   |-- <etc>
|   |-- views.py <--all of the views
|-- config.py
|-- <other irrelevant stuff>
```

However, the views file is getting to be fairly large (over 1100 lines at
the time of this writing). I would like to instead create a module,
replacing views.py with a module:

|   |-- views
|   |   |-- __init__.py
|   |   |-- users.py
|   |   |-- messages.py
<etc>

Now, I know how to do this. My question (which may be more of a general
Python question than a Flask question, per se) is this: if I do this
refactoring, I'll need to import a lot of stuff in each of the (non
__init__) files within the views module (including several of my SQLAlchemy
models, several things from flask and flask-login, markdown, itsdangerous,
etc). Will all of these extra imports use up a lot more memory than if I
stick with the current design (keeping all of my routes in one file)?

Thank you for your time,

Jack


-- 
"Structures are the weapons of the mathematician."
--Bourbaki

Re: [flask] Refactoring views into submodules

From:
Charles
Date:
2014-09-21 @ 02:17
On Sat, Sep 20, 2014 at 7:02 AM, Jack Maney <jackmaney@gmail.com> wrote:
> However, the views file is getting to be fairly large (over 1100 lines at
> the time of this writing). I would like to instead create a module,
> replacing views.py with a module:
>
> |   |-- views
> |   |   |-- __init__.py
> |   |   |-- users.py
> |   |   |-- messages.py
> <etc>

A module inside a directory is more specifically called a package.

> Now, I know how to do this. My question (which may be more of a general
> Python question than a Flask question, per se) is this: if I do this
> refactoring, I'll need to import a lot of stuff in each of the (non
> __init__) files within the views module (including several of my SQLAlchemy
> models, several things from flask and flask-login, markdown, itsdangerous,
> etc). Will all of these extra imports use up a lot more memory than if I
> stick with the current design (keeping all of my routes in one file)?

Imports are cached in a dictionary in module sys: sys.modules. If a module
has been imported before, it will use the cached value in sys.modules.

Re: [flask] Refactoring views into submodules

From:
Moritz Emanuel Beber
Date:
2014-09-20 @ 03:07
Hey,

On 20/09/14 02:02, Jack Maney wrote:
> Hello,
>
> I'm still new to Flask, and still fairly new to Python in general 
> (picked it up because it's used at my newest job for data munging and 
> analysis). I've only used Flask on my own time, hacking away on a side 
> project.
>
> As seems to be the standard with Flask apps, I currently have all of 
> my routes sitting in a single file (views.py). In other words, my file 
> structure is like this:
>
> ```
> .
> |-- LICENSE
> |-- README.md
> |-- app
> |   |-- __init__.py
> |   |-- decorators
> |   |   |-- __init__.py
> |   |-- forms.py
> |   |-- mailer.py
> |   |-- models.py
> |   |-- templates
> |   |   |-- base.html
> |   |   |-- <etc>
> |   |-- views.py <--all of the views
> |-- config.py
> |-- <other irrelevant stuff>
> ```
>
> However, the views file is getting to be fairly large (over 1100 lines 
> at the time of this writing). I would like to instead create a module, 
> replacing views.py with a module:
>
> |   |-- views
> |   |   |-- __init__.py
> |   |   |-- users.py
> |   |   |-- messages.py
> <etc>
>
> Now, I know how to do this. My question (which may be more of a 
> general Python question than a Flask question, per se) is this: if I 
> do this refactoring, I'll need to import a lot of stuff in each of the 
> (non __init__) files within the views module (including several of my 
> SQLAlchemy models, several things from flask and flask-login, 
> markdown, itsdangerous, etc). Will all of these extra imports use up a 
> lot more memory than if I stick with the current design (keeping all 
> of my routes in one file)?

That is a general Python question and the answer is basically no, the 
modules and objects will only exist once in memory. Don't worry about 
it. If you haven't already, you should also give blueprints a read in 
the Flask docs.

>
> Thank you for your time,
>
> Jack
>
>
> -- 
> "Structures are the weapons of the mathematician."
> --Bourbaki

Cheers,
Moritz