Loggers are organized in a hierarchical fashion. A logger named 'foo.bar' is a child of a logger named 'foo'.
getLogger() returns a reference to a logger instance with the specified name if it is provided, or root if not. The names are period-separated hierarchical structures. Multiple calls to getLogger() with the same name will return a reference to the same logger object. Loggers that are further down in the hierarchical list are children of loggers higher up in the list. For example, given a logger with a name of foo, loggers with names of foo.bar, foo.bar.baz ...... read more »
This example shows how to set up a Flask local development server to use a different configuration based on the subdomain of the request. The project I work on has several environments (dev, qa, staging, etc). Each environment has different database and API hostnames. I use this to switch between database and API environments quickly while using my local development server.
This assumes a
create_app function is used to
create the Flask application instance as described in
Application Factories Flask documentation.
create_app function to take a
argument and use it to override the default configuration ...
This example shows how to use Python to generate a Google Static Map URL for a map that contains markers within some dimensions which are smaller than the map image dimensions. This effectively allows for setting minimum X and Y margins around the markers in a map. This is useful for a "fluid" web design where a maximum map size is requested from Google and is then cut off at the edges for small browser windows.
import math def generate_map_url( min_map_width_px, max_map_width_px ...
Do you have a lot of short, single-use, private functions in your
Python code? For example, below is some stubbed out authentication
code I've been working on. It checks if a user's password is correct
and updates the hash algorithm to use bcrypt.
The 4 private functions with the leading underscore are from 1 to 10
lines long and are only used by
check_password function. These functions are part
of a larger module with about 20 functions. I don't like that these
4 functions add clutter to the module and are not grouped with the
Here is how to run a Flask local development server on your local machine and expose it to the public via a remote server you have control over. This uses SSH remote port forwarding which is a converse of local port forwarding described here: How to run a Django local development server on a remote machine and access it in your browser on your local machine using SSH port forwarding
- On the remote host, edit the sshd_config file (mine was located at /etc/ssh/sshd_config)
to allow remote hosts to connect to ports forwarded for the client:
- On ...
The finally block is used to define clean-up actions. Why is the finally block needed? Why can't the clean up actions be put after the try/except/else block? This works in some cases, but if there is a return, break, or continue, or an unhandled exception inside the try, except, or else clauses, that code will never be executed. The finally block executes even in these conditions.
try: print 'Inside try' raise Exception finally: print 'Inside finally' print 'Never get here'
Inside try Inside finally Traceback (most recent call last): File "tmp.py", line 13, in... read more »
I needed to gzip some data in memory that would eventually end up saved to disk as a .gz file. I thought, That's easy, just use Python's built in gzip module.
However, I needed to pass the data to pycurl as a file-like object. I didn't want to write the data to disk and then read it again just to pass to pycurl. I thought, That's easy also-- just use Python's cStringIO module.
The solution did end up being simple, but figuring out the solution was a lot harder than I thought. Below is my ...... read more »
Start a long running process in screen and detach¶
- Ssh to the remote host, myremote:
eliot@mylocal:~$ ssh myremote
- Start a new screen session
- Start a long running process, "sleep 3600":
eliot@myremote:~$ sleep 3600
- Detach from the screen session:
(Hit [CTRL-A], then type a colon character, then type "detach", then hit [ENTER])
eliot@myremote:~$ CTRL-A : detach ENTER
- Exit your remote SSH session:
Reattach to the existing screen session¶
- Ssh to the remote host again:
eliot@mylocal:~$ ssh ...
$ pip install --index-url=https://simple.crate.io yolk
$ pip install --log=my-pip-debug.log --index-url=https://simple.crate.io yolk
How to run a Django local development server on a remote machine and access it in your browser on your local machine using SSH port forwarding
Here is how to run a Django local development server on a remote machine and access it in your browser on your local machine using SSH port forwarding. (This is useful if there is a firewall blocking access to the port of your Django local dev server (port 8000).
- On the local host, SSH to the remote host:
$ ssh -v -L 9000:localhost:8000 firstname.lastname@example.org
- On the remote host, run the Django dev server:
email@example.com:/path/to/my/django/project$ python manage.py runserver 0.0.0.0:8000
- On the local host, go ...