Thursday, June 30, 2011

Yesterday date

$yesterday = date('Y-m-d', mktime(0, 0, 0, date("m") , date("d") - 1, date("Y")))

UPD: (thanks to stamm)
$yesterday = date('Y-m-d', strtotime('- 1 day')); 

Tuesday, June 28, 2011

Kwargs in Python

Recently I noticed that function cannot accept unicode strings as parameter names given in kwargs. They must be converted to strings.

I made a sample script
import sys

print "Version: %d.%d.%d\n" % sys.version_info[:3]

string_params = {'a':1, 'b': 2}
unicode_params = {u'a':1, u'b': 2}

def f(a,b):
 print "params: ", a, b

print "Run with string params:"
f(**string_params)

print "\nRun with unicode params:"
f(**unicode_params)

As output I got
Version: 2.5.1

Run with string params:
params:  1 2

Run with unicode params:
Traceback (most recent call last):
  Line 15, in 
    f(**unicode_params)
TypeError: f() keywords must be strings

To check out it online.

Disable Firefox cache for developing

To disable caches while you're developing and testing in Firefox:
  1. Type about:config in FF address bar;
  2. Set to false the following params:
    • network.http.use-cache
    • browser.cache.offline.enable
    • browser.cache.disk.enable
    • browser.cache.disk_cache_ssl
    • browser.cache.memory.enable
Beware, it totally disables cache in FF!

Monday, June 27, 2011

Make string html-safe

To make a string HTML-safe (convert & to &, > to > and so forth):

>>> import cgi
>>>
>>> test_string = "This is <unsafe> string & it contains wrong symbols"
>>>
>>> print cgi.escape(test_string)
This is &lt;unsafe&gt; string &amp; it contains wrong symbols

Wednesday, June 22, 2011

Run Django via WSGI without web server

Recently I faced a problem - I needed to test updated Django WSGI-runner script. But I didn't have a web server on my computer. And I didn't want to install it. So, how to deal with that?

I found out about Werkzeug. It was supposed to solve my issue. To install it run system-wide
sudo pip install werkzeug
I made a runner script run.py that makes a dev server with Werkzeug and voila:

run.py
from werkzeug.serving import run_simple
from wsgi import application

run_simple('127.0.0.1', 4000, application)

where wsgi.py is former django.wsgi file.

Thursday, May 26, 2011

Adding a new user

To create a new Postgres user.
First, create a new Linux user:
sudo useradd -M -N username # (do not create hoMe dir; do not add to group "userName")
sudo passwd username # enter password 'username'

And add user with the same name to Postgres:
psql -h 127.0.0.1 -p 5432 -U username -W -d template1 # insert your hostname and port if different 
In psql:
CREATE USER username WITH PASSWORD 'username';
ALTER USER username CREATEDB; -- allow user to create databases 

Monday, April 11, 2011

How to satisfy dependencies for your python projects

You need a text file in your project with your dependencies, i.e. REQUIREMENTS, with a list of packages your project depends on.

PIP's requirement file format (detailed):
<package-name>[==<version>]
-e <repository address>#egg=<desirable-package-name>

And when you need to init the project in a new place you'd like to run:
# init or activate a virtual environment
(virtual-env)$ pip install -r REQUIREMENTS

That's all. You packages will be automatically installed. And you can run that command whenever you want (i.e. a new dependency has been added) - it just recheck all mentioned packages and install new ones.

Thursday, April 7, 2011

Python's virtual environment

You probably would like to use separate sets of packages for your projects and whilst not to spoil your system's environment. This problem can be solved by using virtual environments.

Make initial setup:


To configure virtualenvwrapper:

  1. Create a directory for the virtual environments:
    mkdir ~/projects/.envs
  2. Add to ~/.bashrc:
    export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python
    export WORKON_HOME=~/projects/.envs
    export PROJECT_HOME=~/projects
    
    source /usr/local/bin/virtualenvwrapper.sh
  3. Run:
    source ~/.bashrc

Initial setup has been done. Now let's create a new virtual enviroment for Test project in ~/projects/test
mkvirtualenv test
workon test
Now you're in the 'test' environment. To install new packages you can use, for instance, pip.

Friday, April 1, 2011

Big brother is watching you

Do you doubt whether Google knows about your Wi-Fi router location? Try this on your *nix-machine:

iwlist wlan0 scan | sed -n 's/.* Address: //p;T;s/ //g;q' | sed 's/.*/{version:1.1.0,host:maps.google.com,request_address:true,address_language:'${LANG/.*/}',wifi_towers:[{mac_address:"&",signal_strength:8,age:0}]}/' | curl -sX POST -d @- www.google.com/loc/json | sed -e 'h;s/.*latitude":\([^,]*\).*/\1/;G;s/\n[^\n]*longitude":\([^,]*\).*/,\1\n/;s|^|http://maps.google.com/maps?q=|;x;s/[,{]/\n/g;s/["}]//g;s/:/\t/g;s/\n//;G'

Nice, isn't it?

Wednesday, March 30, 2011

Friday, March 25, 2011

Vim necessary commands

Reload the file with selected encoding:
:e ++enc=<encoding>

Friday, February 25, 2011

Force FF not to fill up input by its 'memorized' state after refresh

If it is a very annoying thing for you, use autocomplete="off" for every input you want to avoid memorizing.

Wednesday, February 16, 2011

Patching with git diff

To create patch file:

git diff --no-prefix > filename

To apply it:

patch -p0 < filename

If you have a patch file that's been created without --no-prefix, apply it

patch -p1 < filename