Archive for November, 2007

Repetitive programming

November 30, 2007

Hint for the lazy programmer. How many times did you need to make switches/If clauses/… with lots of conditions, maybe coming from the n-th field of a each line in a file?

You’re lucky: bash and awk are here to help. This example generates (brrr…) VB code:

#!/bin/bash
while read line
do
echo “If variable = `echo $line | awk {‘print $1;’}` Then ” >> new_file
echo ” boolean = True” >> new_file
echo “End If” >> new_file
echo ” ” >> new_file
done < file_to_read[/sourcecode]

latex (pdflatex) include png jpg … images

November 29, 2007

I always forget the package name to include, so here it is

\usepackage[pdftex]{graphicx}

Once included, the package provides the following term

\includegraphics[key_1 = . . . , key_2 = . . . ,  key_n=....]{filename}

Options (keys) include:

• scale = number — a magnification factor
• width = length — the width to which the figure should be scaled
• height = length — the height to which the figure should be scaled2
• totalheight = length — height plus depth of figure (to be used if figure  is rotated)
• keepaspectratio = true/false — maintains the height/width ratio
• angle = number — angle (in degrees) by which the figure is to be rotated  counter clockwise
• origin = location3 — the point about which rotation is to occur
• draft = true/false — prevents figure from being imported, but created   a named box with the dimensions of the figure (this option is used to speed  up processing)
• clip = true/false — excludes whatever is outside the bounding box
• bb = llx lly urx ury — enters the bounding box coordinates, which are   given by default in points (1/72 inch), manually (the bounding box might  be missing or you might want to alter it)
• viewport = llx lly urx ury — specifies bounding box w.r.t. bottom left of existing bounding box; used with clip to select a part of the image (or to  clear unwanted margins).
• trim = dllx dlly durx dury — reduces the bounding box by the amount  specified
• hiresbb = true/false — reads the bounding box information from the  line %%HiResBoundingBox in figure file

Detailed spec is here or a quickie can be found here

Running applications remotely with ‘screen’

November 29, 2007

If you need to run an experiment on a remote machine, but you cannot be logged in via ssh for the whole time for any reason (maybe you need to go home in between), use the screen command.
When screen is called, it creates a single window with a shell in it. You can run your application in this new shell and then leave it by pressing “Ctrl+a Ctrl+d“. This will detach the screen. You can then even end your ssh session and log out. The next time you connect to the remote machine you can go back to the shell with the command “screen -r”. Your application will still be running.

screen can do much more than this, try ‘man screen‘ for a better overview or see this online manpage.

There is also a quick tutorial here.

Executing commands remotely via ssh

November 29, 2007

You can execute commands on the remote machine via ssh if you want to. This example will read a local file and append it to a file on the remote machine. Note that the stuff between the ” ” is the command executed remotely.


cat localfile.txt | ssh chris@remotehost "cat - >> remotefile.txt"

MD5 SHA1 Reverse Lookup

November 29, 2007

If you have an MD5 or SHA-1 hash and you need the value that created the hash, try to look it up here. They also provide a hashing function and will store in their database, whatever you hash.

How to read from a file in C++

November 28, 2007

This is a basic example how to read from a file in C++ without much error checking or anything. If you want to have a look at the ifstream reference, it’s here.

#include
#include
#include
using namespace std;
int main() {
ifstream file;
string line;
file.open(“./myfile.txt”);
if (!file.is_open()){
cerr << "Oops. Could not open file." << endl; return -1; } while (!file.eof()) { getline(file, line); cout << line << endl; } file.close(); } [/sourcecode]

Threading in Python

November 28, 2007

Just a simple example on Python threading:

 

#!/usr/bin/env python

import time
from threading import Thread

class MyThread(Thread):
    def __init__(self):
        Thread.__init__(self)

    def run(self):
        while True:
            print 'hello Thready World!!!'
            time.sleep(2)

t = MyThread()

t.start()

How to post source code on WordPress blogs

November 28, 2007

If you wonder how to post your source code in a blog, read this article. Basically, you need to wrap your code in these tags: [sourcecode language='css']…[/sourcecode]. Replace the language “css” with the language that you’re writing in. Currently they support syntax highlighting for the following language codes: cpp, csharp, css, delphi, java, jscript, php, python, ruby, sql, vb, xml.

extracting compressed files

November 28, 2007

Fed up of remembering the syntax for tar / tar.gz / zip / rar / Z … try adding this simple ‘extract’ function your .bashrc

#extract - extract most common compression types
function extract () {
    echo Extracting $1 ...
    if [ -f $1 ] ; then
        case $1 in
            *.tar.bz2)   tar xjf $1  ;;
            *.tar.gz)    tar xzf $1  ;;
            *.bz2)       bunzip2 $1  ;;
            *.rar)       rar x $1    ;;
            *.gz)        gunzip $1   ;;
            *.tar)       tar xf $1   ;;
            *.tbz2)      tar xjf $1  ;;
            *.tgz)       tar xzf $1  ;;
            *.zip)       unzip $1   ;;
            *.Z)         uncompress $1  ;;
            *.7z)        7z x $1  ;;
            *)        echo "'$1' cannot be extracted via extract()" ;;
        esac
    else
        echo "'$1' is not a valid file"
    fi
} 

About this blog

November 28, 2007

This blog is about the little information you always have to look up several times, and you will never remember it. For example, how do I parse a text file in C++ again? What’s the Python routine to plot histograms? Why doesn’t Latex put the picture where I want it to be? All these things, you always forget and then you need to look them up again. This is quite annoying and a waste of your time. That’s why we want to write this information down and label it properly, so it’s easy to find for us and maybe also for you.