Python notes
Programming language
Object Oriented concepts in Python
The rule of thumb is, don't introduce a new attribute outside of the init method, otherwise you've given the caller an object that isn't fully initialized.
- Why you need self as the first positional argument of a method
In Python, this:
my_object.method("foo")
is syntactic sugar, which the interpreter translates behind the scenes into:MyClass.method(my_object, "foo")
which, as you can see, does indeed have two arguments - it's just that the first one is implicit, from the point of view of the caller.This is because most methods do some work with the object they're called on, so there needs to be some way for that object to be referred to inside the method. By convention, this first argument is called self inside the method definition.
Environment isolation
Same database engine in all the environments
The same database engine (e.g. PostgreSQL, sqlite, MySQL), should be used in all the different environments of a project.
It is a common bad practice to have a lightweight database in the development environment and something more powerful in production, all of them should use the same engine to avoid the different ways each one handle the data.
This way it is possible to:
- examine the production environment data locally.
- avoid the different types and constraints each database engine has. (e.g. sqlite does not support foreign key constraint, while PostgreSQL does )
Project packages
Project environment isolation with pip and virtualenv is recommended, so each project can handle their own dependencies and language versions.
OS isolation
If there is more than one computer in the development process, there would be probably different Operating Systems and software versions involved that can lead to inconsistencies in source code. There are two applications that play nice together:
Vagrant creates reproducible development environments. In addition to being useful in setting up developers environments, it makes easier to configure development, staging and production environment.
VirtualBox is a virtualization product, makes it easy to have the same OS that the Vagrant file is built for.
Project structure
- http://docs.python-guide.org/en/latest/writing/structure/
- https://jeffknupp.com/blog/2013/08/16/open-sourcing-a-python-project-the-right-way/
Database
Peewee
A small ORM that supports postgresql, mysql and sqlite. Can be used with models like Django.
References
*[ORM]: Object Relational Mapping
- Python Tools To Write Better CodeNovember 7, 2017
- Python Language Main Concepts And SummaryJune 11, 2017
- Python notes
Python Language Concepts
- Understanding How Python Packages Modules And Imports WorkJanuary 2, 2017
- Python Language Basic ConceptsJune 14, 2016
Python Environment
- Understanding Python 3 virtual environments different approachesJanuary 15, 2019
- Python Projects Isolation Using Virtual EnvironmentsJune 10, 2016
Articles
Subcategories
Except as otherwise noted, the content of this page is licensed under CC BY-NC-ND 4.0 . Terms and Policy.
Powered by SimpleIT Hugo Theme
·