Archive

Archive for October, 2013

Remove duplicates from a list AND keep the original order of the elements

October 31, 2013 2 comments

Problem
You have a list of elements. You want to remove the duplicates but you want to keep the original order of the elements too. Example:

input: apple, fruit, dog, fruit, cat, apple, dog, cat

output: apple, fruit, dog, cat

Solution

def remove_duplicates(li):
    my_set = set()
    res = []
    for e in li:
        if e not in my_set:
            res.append(e)
            my_set.add(e)
    #
    return res

The trick list(set(li)) is not acceptable in this case because elements are unordered in a set.

deploy a git project to a remote host

October 23, 2013 Leave a comment

Problem
You have a project that you develop on your local machine. You want to deploy it (upload) to a production server.

For instance, you have a homepage and you want to upload it to your university server.

Solution
I assume your project on the local machine is stored in a dedicated folder. Turn this folder into a git repository. Example:

$ cd /home/jabba/projects/homepage
$ cat index.html
Awesome homepage v0
$ cat .gitignore
.gitignore
deploy.sh
$ git init
$ git add .
$ git commit -m "initial commit"

We also need an uploader script called deploy.sh :

TO_HOST=<username>@<remote_host>
TO_PORT=<port>
TO_DIR=<remote_dir_on_server_with_absolute_path>
git ls-files -z | rsync --files-from - --copy-links -av0 . --rsh="ssh -p$TO_PORT" $TO_HOST:$TO_DIR

Just launch the script and it will copy all the checked in files to the server.

However! If you remove some files on your local machine, this script won’t remove them from the remote host. That is, you will have to delete files which got removed from the project.

Credits
This tip is from here.

Related work

  • Using Git to manage a web site (Shortly: you have a local git repo with your project. You create another git repo on the remote host and you push changes to this remote git repo. A customized post-receive hook will copy everything to the specified destination folder.)
Categories: bash Tags: , ,

Generate a 192-bit random number

October 4, 2013 Leave a comment
import os
os.urandom(24)    # length: 24 bytes, i.e. 24*8=192 bits

See the doc. here.

Formatting its output:

>>> os.urandom(24)
'\x17\x96e\x94]\xa0\xb8\x1e\x8b\xee\xdd\xe9\x91^\x9c\xda\x94\t\xe8S\xa1Oe_'
>>> os.urandom(24).encode('hex')
'cd48e1c22de0961d5d1bfb14f8a66e006cfb1cfbf3f0c0f3'
>>> int(os.urandom(24).encode('hex'), 16)
625318378251135334886162535673249000280269152689162062986L
>>> bin(int(os.urandom(24).encode('hex'), 16))
'0b10100010101001110001101011101111010000111101010010110011111101111101111010100111100000001010100100001000100101010011100001001100011000011000000101101111100001011111011101001110011010001000010'
Categories: python Tags: ,

Python testing frameworks

October 2, 2013 1 comment

The site http://pythontesting.net/start-here/ covers the following Python testing frameworks:

  • doctest
  • unittest
  • nose
  • pytest
Categories: python Tags: , , ,