Python: Hashing files

Wednesday 6. February, 2008

Reminder-post:

Once again I decided I needed to make some md5sums for files, “Pythonic”-way. I have done it before, but I didn’t quite remember it, so some Googling gave the instructions, I write them down here, in case I’ll need it again


import hashlib
md5sum = hashlib.md5(open('tehfile','rb').read()).hexdigest()
sha1sum = hashlib.sha1(open('tehfile','rb').read()).hexdigest()

(I since sha1sum is longer, I decided to go with it instead of md5)

Basically the code above imports the hashlib-module and then generates md5 and sha1 sums in hexadecimal presentation for ‘tehfile’ opened in for reading in binary mode.

Advertisements

After midnight, part 4: LaTeX

Tuesday 11. December, 2007

What has happened since the last blog entry? Well, I’m not sick anymore and I live on my own now (okay, it’s relative “on my own”).

Unlike the previous After midnight -posts, this isn’t actually about a programming language.

Do you know “Structured text processing” (Rakenteellinen tekstinkäsittely, in Finnish)? It was the fancier name for our university’s LaTeX-typesetting course. It’s like text processing, but not at all like it. Today everybody uses these “What You See, Is What You Get” software (M$ Word, OpenOffice.Org Writer), but no one has ever hear that you could type those documents with a TeX-system. Usage of LaTeX is quite a bit different from those wysiwyg-programs as you don’t see what you get until you have compiled the tex file into something more readable such as DVI- or PostScript-file.

Personally I’m impressed by the looks of the documents I have made so far.

If you want to try it out, you need a LaTeX-environment, see MikTex. Then you need the Ghostscript-software. You probably want GSView too. You can write tex-files with Notepad or any text editor, as those files are more or less pure ascii-text. If you really start using LaTeX, I suggest you to download TexMaker. It’s free (open source) LaTeX editor and available for Linux systems too or if you are using Linux, you can also try out Kile editor and if you are using Ubuntu, you can install all you need with “apt-get install kile”. Besides the editor, this should pull all required packages including LaTeX-system.

I suppose our text processing environment is now installed and working (well, you probably don’t know if it is working yet)

Open up you editor, so we can start writing our first LaTeX-document. It all begins with lines

\documentclass[12pt]{report}
\begin{document}
Hello this is our very first \LaTeX-document.
\end{document}

Save it and if you are using Texmaker, there should be a button with text “LATEX”, push it. If your specified paths are correct (you can check them in settings), the latex-system now compiles our document into a dvi-file. The animal button next to LATEX-button opens up a dvi-file viewer and you should see your document there.

The commands given above first define the type of our document, which is “report” with font size 12pt. All options in [] are optional, where {} are required. Without required parameters the compilation will fail. You probably have noticed that all Latex commands begins with \. The \begin{document} defines a document environment and \end{document} will end it. Everything you write and want it to be seen in the final document must be written inside the document environment.

For more information on LaTeX resides in the “doc”-folder inside your miktex installation (if documentation was installed). There’s also a folder named ‘guide’ or ‘guides’ in which resides a tutorial to LaTeX-system. You can also find a lot of information on latex with the help of Google.

(I can’t write a complete tutorial here as this is only a blog posting nor could I do it anyway as I’m not very familiar with LaTeX by myself. If you find these instructions too complicated to understand, leave a comment and I’ll try to help)

Hello? Are you still standing there?

Sunday 25. November, 2007

[DIARY ENTRY WARNING] 

Who can guess where that line is taken from? Here’s a hint: It’s a computer game and it’s name starts with P.

Apparently I have got a new obsession related to that game. I hope it’s nothing serious, though. I still want a big blue tower.

On the other news, it seems there’s something wrong somewhere as my laptop’s hard drive seem to be under massive use in both Fedora and Ubuntu. I have no idea what’s going on there, the system just uses hard drive. I suppose it’s somehow related to swapping, but it didn’t do that in previous versions of Ubuntu. Also, when ever it’s updating/installing/removing packages (aka using the hd) the whole system goes nearly unusable state. It’s not nice at all.  I already downloaded a Vector Linux installation cd image as they say it’s a lightweight distribution, but if the problem lies some where in the hardware changing distro won’t help.

I don’t remember having this sort of problem in my personally built Prospekt Linux. In case I get tired of Vector too Prospekt is the next installation. Hmm… I should write some sort of installation program for it…

I had to do a practice work (in Java) to the programming course I mentioned in an earlier post. So far I have a working implementation of “program that calculates the age of a person in months and years. Current date is taken from the system.” I still have to do some testing, write API documentation using Javadoc and a small report containing the testing material, short instructions and stuff. (How much instructions users need for a program that asks their birthday?)

Last Friday the teacher did a small review on the current state of our projects. He told my program was great (I’m not really sure how to translate the adjective he used) and needed only testing. I was quite sure that wrong input format wouldn’t crash it, but he still managed to do it. Apparently I never tested what would happen if there’s some extra spaces before the actual birthday. It was quite easy to fix, though, and I’m somewhat proud of it. Now it “cleans” all extra space from the input string by utilizing Java’s StringTokenizer-class.

Yet another news: I’m sick. My throat hurts and I’ve got fever. Usually I’ve been sick around December 6th, but it came early this year, it seems. A quick check to my calendar shows that the 6th day is Thursday and we’ll have final test in Calculus III on Friday, so I guess my immune-system predicted the test and decided I must get sick this weekend instead so I can attend to the test. It also knew that getting sick next weekend would have compromised the final test in Fotonics or my possible moving to Joensuu.

Pleased

Thursday 18. October, 2007

I noticed that my little article on Intel 945GM chipset and Half-Life 2 has got a pair of new comments in this month. I’m very pleased because of that. Every comment received means that some has reacted on my writings. Those little comments motivated me to write once again a blog entry.

The last week has been very pleasing too. Last Friday I purchased the Orange box on Steam (that may sound a bit funny if one doesn’t know what Valve’s Steam is… 🙂 ). Well, the rest of the day was spent downloading the content and making room on the hard drive for it by moving stuff around and removing unnecessary stuff. I could finally play Half-Life 2 Episode Two on Saturday and I actually beated the game in the same day. Well… As always, I cheated a little on tough and scary places… (I simply hate that red screen of death nearly as much as the blue one on another “game”).

Half-Life 2 Episode Two is a great expansion and it was longer than Episode One, thanks Valve. As the game is not yet in real life stores, I’m not going to write what happens in the game as that potentially could ruin the game experience of others. But some things aren’t suprises for anyone:

  • The game has some new creatures such as
    • Antlion grubs
    • nastier form of antlions
    • Combine Hunters
    • some others too
  • We learn new things about “Our mutual friend”
  • Citadel has been blown apart (Such a shame… It was a beautiful tower, I want one)

As for the actual plot, I think it doesn’t ruin anything if I say that I was shocked on the way the Episode ended…

The Orange Box also contained Portal and Team Fortress 2 as “worth mentioning games”. Now I have Half-Life 2 and Episode One to give away as gifts, but I haven’t yet decided who to give them to… … maybe my brother if he would play them…

Portal is a cool game. I love the computer voice explaining things, she’s so formal and cold. “The floor in this test will kill you. Try to avoid it” or “… can cause permanent disabilities such as vaporization…”. Various things have very cool names such as “Aperture Science emergency intelligence incinerator”, “Aperture Science We-don’t-know-what-it-does-thing” or something like that…

hmm… It might be time to log off and close this computer for this night…

Lover’s Math

Saturday 6. October, 2007

If you happen to own a graphical calculator or plotter software, try what following functions look like:

f(x)=sqrt(1-(sqrt(x^2)-1)^2)

g(x)=-3sqrt(1-sqrt(sqrt(x^2)/2))

Or these

f(x)=2/3*((x^2+sqrt(x^2)-6)/(x^2+sqrt(x^2)+2)-sqrt(36-x^2))

g(x)=2/3*((x^2+sqrt(x^2)-6)/(x^2+sqrt(x^2)+2)+sqrt(36-x^2))

Note: the sqrt(x^2) is the same thing as |x|, but as most calculators don’t have |x|-function, I replaced it with sqrt(x^2) which is also always positive.

(I guess WordPress doesn’t have tools for mathematical expressions?)

Edit: Add missing ) to the last g(x)

After midnight, part 3: Java

Friday 5. October, 2007

As I took the Programming course at the University, I was faced with this beast. The course is about programming and how to program in Java. Even though I have never before even seen actual Java code, I’ve always been hating it. Some Java programs just seem to be so goddamn slow even if they don’t appear doing anything special (well, Azureus might have been the app in question, not sure about it).

Anyhow, here’s the Hello World in Java:

public class HelloThere {

public static void main(String args[]) {

System.out.println("Hello there!");

}

}

Save in a file with exactly the same name as the class + “.java” in this case “HelloThere.java”. As you have installed Java Development Kit, or the SDK, (Java runtime is NOT enough), compile the java file with

javac HelloThere.java


This will create a corresponding class-file which is the bytecode version of the source code, and as far as I know, the closest form of a binary you can get with javac. And run it with

java HelloThere


Note: If you are using Linux environment you might not need Sun’s Java environments at all. Gnu Compiler Collection (GCC) contains a Java compiler (gcj), that can produce even native binary code. Be warned though, the gcj is compatible with 1.4-series, which means that newer features won’t be available such as the java.util.Scanner -class for reading user input in console applications.

Search the net and you should be able to find Java tutorials easily and among them a lot of posts on various forums telling you you shouldn’t use Java in the first place.

New Prospekts

Monday 20. August, 2007

Since it has been very boring, I have been rebuilding my little Prospekt Linux -project. This time I have followed Linux From Scratch book quite closely, except for gcc+glibc+binutils combination. I know these are critical packages but some distros are going on glibc 2.6.1, gcc 4.2.1 and binutils 2.17.50.0.18 and so am I.

Currently the desktop of choice is GNOME 2.18.3? as I haven’t yet built KDE. I was thinking that I would build KDE4 when it’s released and not KDE3. I know that would radically limit the KDE software available, but that would be less compiling for me 🙂 .

As for package management, I have returned to RPM for some reason. I just like it (and Conary didn’t let me forget my mistakes of various sizes, I know, I shouldn’t have commited those recipes in the first place).

I did try to use rPath Linux Development as my base, but as their installation dvd was badly outdated, Conary proposed over 900 updates and actually removed some troves due to missing dependencies in their repository. I spent several hours updating until I got fed up and did rm -rf / (of course everything important were unmounted at the time of destruction).

I have thought of using my own package manager, but it isn’t very reliable (like I would trust on something I have written myself…) and has bugs and lots of non-implemented features.