Saltycrane logo

Sofeng's Blog

Notes on Python, Django, and web development on Ubuntu Linux

    

Creating remote server nicknames with .ssh/config


Using the ~/.ssh/config file is an easy way to give your remote machines nicknames and reduce the number of keystrokes needed to login with ssh, rsync, hg push/pull/clone, access files via Emacs Tramp (Transparent Remote (file) Access, Multiple Protocol), or use any other SSH-based tool.

Here is part of my ~/.ssh/config file. It defines the nicknames turk, tyran, tuna, and tally for some EC2 servers I've been working with.

Host turk
  User root
  HostName ec2-67-202-21-122.compute-1.amazonaws.com

Host tuna
  User root
  HostName ec2-75-101-178-62.compute-1.amazonaws.com

Host tyran
  User root
  HostName ec2-67-202-43-207.compute-1.amazonaws ...
Read more...

Python datetime / time conversions


from datetime import datetime
import time

#-------------------------------------------------
# conversions to strings
#-------------------------------------------------
# datetime object to string
dt_obj = datetime(2008, 11, 10, 17, 53, 59)
date_str = dt_obj.strftime("%Y-%m-%d %H:%M:%S")
print date_str

# time tuple to string
time_tuple = (2008, 11, 12, 13, 51, 18, 2, 317, 0)
date_str = time.strftime("%Y-%m-%d %H:%M:%S", time_tuple)
print date_str

#-------------------------------------------------
# conversions to datetime objects
#-------------------------------------------------
# time tuple to datetime object
time_tuple = (2008, 11, 12, 13, 51, 18, 2, 317, 0)
dt_obj = datetime(*time_tuple[0:6])
print repr(dt_obj)

# date string to datetime object
date_str = "2008-11-10 17:53:59"
dt_obj = datetime.strptime(date_str, "%Y- ...
Read more...

Python UnicodeEncodeError: 'ascii' codec can't encode character


UnicodeEncodeError: 'ascii' codec can't encode character u'\xa1' 
in position 0: ordinal not in range(128)

If you've ever gotten this error, Django's smart_str function might be able to help. I found this from James Bennett's article, Unicode in the real world. He provides a very good explanation of Python's Unicode and bytestrings, their use in Django, and using Django's Unicode utilities for working with non-Unicode-friendly Python libraries. Here are my notes from his article as it applies to the above error. Much of the wording is directly from James Bennett's article.

This ...

Read more...

How to escape (percent-encode) a URL with Python


import urllib
print urllib.quote_plus("http://www.yahoo.com/")
print urllib.quote_plus("Kruder & Dorfmeister")

Results:

http%3A%2F%2Fwww.yahoo.com%2F
Kruder+%26+Dorfmeister

It is easy to be drawn to the urlencode function in the Python urllib module documentation. But for simple escaping, only quote_plus, or possibly quote is needed. I believe this is the appropriate solution to Python urlencode annoyance and O'Reilly's Amazon Hack #92.

For reference: Percent-encoding on Wikipedia

Read more...

How to set the font for new frames in Emacs 23


I had been using the following elisp to set my font in Emacs 23. However, it did not work when creating new frames with C-x 5 2.

(if (>= emacs-major-version 23)
    (set-default-font "Monospace-11"))

Here is how I set the font for all frames:

(if (>= emacs-major-version 23)
    (modify-all-frames-parameters
     '((font . "Monospace-11"))))

See the documentation for modify-all-frames-parameters for more information.

Read more...
Created with Django | Hosted by Webfaction