Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This question is wildly subjective - but I'll give it a shot.</p> <ul> <li><p>First of all, always keep database and data code separate to the web code. What I do is have lots of small files with one class each in a <code>DB/</code> folder which are all joined together into a <code>Base.py</code> file, e.g: <PRE> Web/ &nbsp; Base.py - The main "base" class, which includes the classes in other &nbsp; web files, starts the web server in <code>__init__</code> &nbsp; Users.py - The class which includes methods generally from "DB/Users.py" &nbsp; which checks permissions etc before returning (you may &nbsp; wish to add DB-level security later though) &nbsp; ... DB/ &nbsp; Base.py - The main base DB class, includes the other DB classes. Creates &nbsp; new SQLAlchemy/whatever instances and create database schemas if &nbsp; they don't etc. May pay to have database-wide methods &nbsp; here to keep creating connections etc in one place if you &nbsp; decide to change databases later &nbsp; Users.py - The user/password etc DB storage class file &nbsp; ... Templates/ &nbsp; (HTML templates go here) Static/ &nbsp; (Static images/CSS/javscript etc go here) </PRE> Don't forget the <code>__init__.py</code> in each module directory of course so python can find the modules in subdirectories</p></li> <li><p>It doesn't always matter what methods you use for structuring code in my opinion, but be consistent. I write a document up with all my conventions with my justifications for using them and try to follow them up to the point it makes sense to, but as always <code>a foolish consistency is the hobgoblin of small minds</code>, as quoting the <a href="http://www.python.org/dev/peps/pep-0008/" rel="noreferrer">python style docs</a> :-)</p></li> <li>Try to use classes rather than straight functions. It mightn't matter for small projects, but for anything non-trivial things can become difficult. It's my preference to have many files with a specific purpose and only a single class in a single file except where it makes sense to have multiple<P></li> <li>This one is probably controversial - I usually name my classes <code>Class</code> and just reference it by the module name. I'll give an example of Base.py:<pre><code>import Users class Base(Users.Class): &nbsp; def <code>__init__</code>(self): &nbsp; Users.Class.<code>__init__</code>(self) </code></pre> This helps to reduce problems when other modules reference each other when importing, as <code>from Users import Users</code> will conflict if <code>Users.py</code> has <code>from Base import x</code> so I always reference by module name. This is just a personal preference though, so do what you want :-P</li> </ul> <p>Hopefully you should get an idea from this post though.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload