Thomas Sampson


Python Program Entry Point

This is a small snippet of code which allows you to specify a program entry point for your python application.

def main():
    #program entry point is now here
if __name__ == "__main__":
    main()
Advertisements


Regular Expression Rules

Period / Full Stop

These are used in regular expressions to represent a wildcard, where a . can represent any character at all. For example

t...s

matches

trees
trams
teens

Important: The wildcard represents ONE character only, not an undefined number of characters, hence t…s would not match the word “tracks”.

Question Mark

The question mark is used to represent the presence of the previous character, or rather the meaninglessness of its presence.

For Example,

colou?r

Would match both the English and American spelling “colour” and “color”. The ? character signifies that the presence of the previous character is not necessary to form a match.

Asterisk

The asterisk character * is used to represent zero or more instances of the previous character. For example,

www\.my.*\.com

would match…

http://www.mypage.com

http://www.my.com

http://www.myspace.com

http://www.myownpersonalhomepage.com

Note: – In this example the character previous to the asterisk is a . meaning we will accept any number of wildcards. Backslashes are used simply to escape the . symbol which we need to represent as a literal string in our url.

Plus

The plus symbol + is very similar to the asterisk * symbol, however it will only match one or more instances of the previous character. Using the previous example..

www\.my.+\.com

would match..

http://www.myspace.com

http://www.mypage.com

etc

but would not match…

http://www.my.com

{N} Number

This method allows us to specify how many of the previous character we will match, For example

w{3}\.mysite\.com

will only match

http://www.mysite.com

and would not match

w.mysite.com or

ww.mysite.com

Ranges {min,max}

This method allows us to specify a minimum and maximum number of the previous characters that we will match, for example

10{1,3} years

Would match the following…

10 years

100 years

1000 years

but would not match

1 years

10000 years

100000 years

Summary

Quantifier Description
? Matches any preceding element 0 or 1 times.
* Matches the preceding element 0 or more times.
+ Matches the preceding element 1 or more times.
{num} Matches the preceding element num times.
{min, max} Matches the preceding element at least min times, but not more than max times.

taken from http://docs.activestate.com/komodo/4.4/regex-intro.html

The | Symbol

Using the | symbol allows us to use or statements in our regular expression. Here is an example

Model (R|S)1000

would match

Model R1000

Model S1000

but would not match

Model X1000

Model Z1000

Grouping with ( )

Parentheses “()” are used to group characters and expressions within larger, more complex regular expressions. Quantifiers that immediately follow the group apply to the whole group. For example:

(abc){2,3}

matches

abcabc
abcabcabc

More rules can be found at http://docs.activestate.com/komodo/4.4/regex-intro.html


Executing multiple executables

I knew this could be achieved easily through a batch file (Windows only). I wanted to launch 3 programs at once, I would use this script at uni to launch a bunch of portable apps I use almost every logon session. I first tried the following, simply putting a different executable path on each line…

F:\MyWork\Apps\PidginPortable\PidginPortable.exe

F:\MyWork\Apps\FirefoxPortable\FirefoxPortable.exe

F:\MyWork\Apps\Notepad++Portable\Notepad++Portable.exe

It turns out this half worked, but wont laucnh the program on line 2 until program 1 closes. I then found that to get all the 3 applications to launch simultaneously the following code is necessary…

start “” “F:\MyWork\Apps\PidginPortable\PidginPortable.exe”
start “” “F:\MyWork\Apps\FirefoxPortable\FirefoxPortable.exe”
start “” “F:\MyWork\Apps\Notepad++Portable\Notepad++Portable.exe”
exit

( the exit command just closes the command file once the script has completed)


Regular Expressins in Python

Really usefull link to help getting to grips with regular expressions. Although the code examples around regular expressions are written in Python, don’t be put off if you are not familiar with the language. The article provides an overview of regular expressions and how to use them, giving you the understanding to implement them in your language of choice.

http://docs.activestate.com/komodo/4.4/regex-intro.html


Using a QueryPerformanceTimer in C++

“Query Performance Counters” can be used to create timers within your code, usually used to capture the time it takes to carry out a given function or set of instructions. Query Performance Counters can be particularly useful when you are looking to optimise a piece of code and need to run comparisons on your optimisations.

To start using performance timers first make sure to include the following..

#include <windows.h>
#include “stdio.h”

using namespace std;

Next you need to create some variables to hold the timing data, these are of the type LARGE_INTEGER.

LARGE_INTEGER start, end, freq;

Now we are ready to set up the timer. First pick a place where you are wanting to start and stop your timer. Once you are sure insert the following pieces of code in the appropriate positions

QueryPerformanceCounter(&start); //To start the times

// Code here

QueryPerformanceCounter(&end);  //Same function stops the timer (notice different return variable)

LARGE_INTEGER numTicks=(end.LowPart – start.LowPart); // Save the number of ticks

The amount of time passed by is recorded in “ticks” rather than seconds or milliseconds, we can deal with this later. Ticks are units that relate to the processing speed of your CPU, therefore every machine will have different tick durations and tick speeds. Therefore we need a way to convert ticks into something useful. This is quite a simple calculation as we can query the processor to tell us how many ticks occur per second. This is achieved with the following code and should be placed right after the timer has finished…

QueryPerformanceFrequency(&freq);

So now we have two important numbers…

  1. numTicks
  2. freq (number of ticks per second)

A simple calculation brings us to the following result…

float elapsedTime = (float)ticks/frequency;

Where elapsedTime represents the number of Seconds the timer took!


1 Comment

Making an ISO file from terminal in Ubuntu

Today I discovered the mkisofs command, a general linux command not specific to Ubuntu. This command enables you to make an ISO disk image of any folder or group of files on your hard drive or external media. I used it today to make an ISO file of a CD which appears to have worked perfectly using the following command

mkisofs -o EPSON.iso /media/cdrom0

The -o parameter specifies the desired output file-name (in this case EPSON.iso). This command has literally hundreds of paramaters which are explained better than I would even attempt here.