extracting a class name from a python object Apr
01
1
0

You can dynamically extract an object's class name in Python like so:

my_object.__class__.__name__

comments

ssh sftp connection with public key authentication Mar
31
1
0

Depending on how your server is set up you might be required to log into it using a public key (for me I needed this for my Amazon instances). This is fine with SSH, you can just link to it directly when like so:

ssh user@example.com -i /path/to/example.pem

I had a problem with SFTP since it doesn't have the -i option. I got around this issue by associating the public key file to the ssh connection.

Create a <config> file in your .ssh folder, and write an entry similar to the following example:

host example.com
IdentityFile ~/.ssh/example.pem

You should then be able to log in through ssh and sftp without having to specify a public key file since it's automatically associated with the host.

comments

virtualenv pip cheatsheet Mar
31
1
0

Virtualenv and pip install typically go together so I'm combining their cheatsheets. These are the commands I use the most. This is pasted here for quick reference.

# create new virtual environment
virtualenv <name>
virtualenv <name> --no-site-packages

# activate and deactivate virtualenv 
source <name>/bin/activate
deactivate
# pip install
pip install PACKAGE
pip install PACKAGE==VERSON
pip install PACKAGE>=VERSION
pip install PACKAGE --upgrade
pip install -r /path/to/requirements.txt

# pip uninstall
pip uninstall PACKAGE

# pip freeze
pip freeze > requirements.txt

# pip install tar bundle
pip install http://example.com/path/to/tar/package.tgz

# git
pip install -e git://github.com/user/MyProject.git#egg=MyProject
pip install -e git+http://git.myproject.org/MyProject/#egg=MyProject
pip install -e git+ssh://git@myproject.org/MyProject/#egg=MyProject

pip install -e git://git.myproject.org/MyProject.git@master#egg=MyProject
pip install -e git://git.myproject.org/MyProject.git@v1.0#egg=MyProject
pip install -e git://git.myproject.org/MyProject.git@da39a3ee5e6b4b0d3255bfef95601890afd80709#egg=MyProject

# mercurial
pip install -e hg+http://hg.myproject.org/MyProject/#egg=MyProject
pip install -e hg+https://hg.myproject.org/MyProject/#egg=MyProject
pip install -e hg+ssh://hg@myproject.org/MyProject/#egg=MyProject

pip install -e hg+http://hg.myproject.org/MyProject/@da39a3ee5e6b#egg=MyProject
pip install -e hg+http://hg.myproject.org/MyProject/@2019#egg=MyProject
pip install -e hg+http://hg.myproject.org/MyProject/@v1.0#egg=MyProject
pip install -e hg+http://hg.myproject.org/MyProject/@special_feature#egg=MyProject

# subversion
pip install -e svn+http://svn.myproject.org/svn/MyProject/trunk@2019#egg=MyProject

# bazaar
pip install -e bzr+http://bzr.myproject.org/MyProject/trunk/#egg=MyProject
pip install -e bzr+sftp://user@myproject.org/MyProject/trunk/#egg=MyProject
pip install -e bzr+ssh://user@myproject.org/MyProject/trunk/#egg=MyProject
pip install -e bzr+ftp://user@myproject.org/MyProject/trunk/#egg=MyProject

pip install -e bzr+https://bzr.myproject.org/MyProject/trunk/@2019#egg=MyProject
pip install -e bzr+http://bzr.myproject.org/MyProject/trunk/@v1.0#egg=MyProject

comments

local python mail server patch Mar
31
1
0

Sometimes in development it's handy to run a local smtp mail server. This shell script will dump any smtp requests out to the terminal if you point your application to localhost with the correct port.

#!/usr/bin/env sh

python -m smtpd -n -c DebuggingServer localhost:1025

comments

how to type the look of disapproval Mar
30
1
0

Depending on which sites you frequent you might come across the common internet meme the "look of disapproval". I'm on Reddit quite a bit and I see it a couple times a day.

There are times where you might want to respond with the look of disapproval, but how do you type the damn thing?

I'm a long time copy-and-paster, but I've recently took the time to figure out how to make this on my own. Not surprisingly it's fairly straight forward. Of course it differs depending on which OS you're running.

Mac OSX: Option+0CA0
Windows: ALT+3232
Ubuntu: hold Ctrl+Shift U+0CA0

comments

magic file: hide a zip archive inside a jpeg image file Mar
30
1
0

I'm not an expert on file formats so I can't explain why this actually works, but it is possible to append zip files onto a jpeg image. I've been told this works because both .jpg and .zip interpreters are pretty hardy. Word on the street is they both have well defined headers that explicitly locate the relevant bits and ignores any extraneous information, but I can't verify this.

In any case, If you type something like this in the terminal:

cat picture.jpg program.zip > magicfile.jpg

you'll end up with what will look like an ordinary image file that will display just fine on your web page. However, if you change the extension on <magicfile.jpg> to <magicfile.zip> you get a file that you can then unzip; or if you're feeling frisky you can just go ahead and directly unzip the .jpg file.

You can use this to hide files, create unique distribution packages, or land that cute geeky chick in the corner that you've had a crush on all year. I don't know what you'd actually use this for, perhaps sneaking files past your email firewall, but it's nice to know it works.

So venture forth now that I've got you unzipping everyone's jpegs searching for porn archives and password files.

comments

recursively grep through an entire directory Mar
22
1
0

Sometimes it's really helpful if you're searching for a file that has a particular string to recursively grep through a directory.

grep -ir [pattern] [directory]

comments

get the current site object in a django view Mar
22
1
0

You can use the default Sites framework to get the current site in a django view.

from django.contrib.sites.models import Site

site = Site.objects.get_current()
domain = Site.objects.get_current().domain

or

from django.contrib.sites.models import Site
from django.conf import settings

site = Site.objects.get(id=settings.SITE_ID)

comments

have your django project use an external SMTP server Mar
22
1
0

Most of the time I want to have my Django project send mail through and an external SMTP server. You can easily set this up by adding the following variables to you settings file.

EMAIL_HOST = 'mail.example.com'
EMAIL_PORT = '25'
EMAIL_HOST_USER = 'test'
EMAIL_HOST_PASSWORD = '******'
DEFAULT_FROM_EMAIL = 'test@example.com'
SERVER_EMAIL = 'test@example.com'

comments

create relative paths for your django settings Mar
22
1
0

I wish Django used this by default, but you should be setting up your project using relative paths in your settings. By not hard coding your paths makes your project easier to manage.

Start by setting a variable to use as the base that pulls it's path dynamically through os.path; I use PROJECT PATH.

import os

PROJECT_PATH = os.path.realpath(os.path.dirname(__file__))

You can then append to the PROJECT_PATH variable when setting up your other paths that are based on your project's root path.

MEDIA_ROOT = PROJECT_PATH + '/media/'

TEMPLATE_DIRS = (
    PROJECT_PATH + '/templates/'
)

comments