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 from SHA-1 to 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 ...
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 ...
Our website is served over HTTPS. To more easily test certain issues (e.g. mixed mode content warnings, or Mapquest SSL tile servers), I wanted to access my Flask local development server over HTTPS. These two articles describe how to do this using stunnel: Testing HTTPS with Django's Development Server, Django Development Server with HTTPS. Using stunnel, you can hit pages on your Django/Flask local dev server over HTTPS instead of HTTP. Here is how I installed it on Ubuntu Precise 12.04:
- Install SSL development files
$ sudo apt-get install libssl-dev
- Go to https://www.stunnel.org/downloads ...
Setting up MythTV involves a little pain, but once it's set up, it's pretty great. And you don't have to spend lots of money on a DVR from the cable company. With my modest hardware specs, playback is smooth and clear, however Picture in Picture is too jittery to be useful. Here's what I did to get my MythTV DVR running on my Ubuntu machine.
- Ubuntu 12.04 LTS (Precise Pangolin)
- MythTV 0.25
- Hauppauge 1196 WinTV-HVR-1250 PCI-E x1 TV Tuner 1196 (The included remote does not work with Linux. However this Anker Mini Bluetooth ...
Some of our Python unit tests have docstrings. I find it annoying that, when using a verbosity level >= 2, nose prints the docstring instead of the class name and method name. Here's a hack to prevent it from doing that: Add a shortDescription() method to the test case class that returns None.
Here is an example of normal behavior:
import unittest class MyTestCase(unittest.TestCase): def test_with_docstring(self): """Test that something does something """ def test_without_docstring(self): pass
$ nosetests --verbosity=2 tmp.py Test that something does something ... ok test_without_docstring (tmp.MyTestCase) ... ok
Here is an example with the hack ...... read more »
I want to share and sync (in real time) Google (Gmail) contacts with my wife on our Android 2.3.6 Gingerbread phones. Google does not make this easy to do. Here's the best solution I could come up with (ref whitenack on androidcentral). (Note: these are not our real email addresses.)
- This contact list resides only on the firstname.lastname@example.org account.
- Contacts are removed from the "My Contacts" group and instead stored in groups called "Angela" and/or "Eliot". For shared contacts, the contact is in both groups. (Contact groups are like tags. A contact can be ...