greynode » programming http://greynode.org ingénierie de tous les aspects de vie Fri, 15 Apr 2011 16:12:00 +0000 en hourly 1 http://wordpress.org/?v=3.1.1 Python 2 to 3 http://greynode.org/2009/12/02/python-2-to-3/ http://greynode.org/2009/12/02/python-2-to-3/#comments Wed, 02 Dec 2009 18:11:12 +0000 enum http://greynode.org/?p=157

Read the rest of this entry »

]]>
Updating to Python 3? I found a nice cheat sheet by Mark Summerfield that shows a concise summary of the differences between Python 2 and Python 3, as well as some of the more common features of Python 3.

This document is freely available from InformIT’s website, but I have also mirrored it. Download the cheat sheet below.

Python2Python3.pdf from greynode.org

Python2Python3.pdf from InformIT’s site

]]>
http://greynode.org/2009/12/02/python-2-to-3/feed/ 0
Automake and Autoconf Made Simple http://greynode.org/2009/11/02/automake-and-autoconf-made-simple/ http://greynode.org/2009/11/02/automake-and-autoconf-made-simple/#comments Tue, 03 Nov 2009 08:15:14 +0000 enum http://greynode.org/?p=142

Read the rest of this entry »

]]>
Instead of randomly pasting configuration code from other projects when you decide you want to use automake/autoconf, just check out all you should really know about autoconf and automake. Albeit, it may not be comprehensive, but it should get you started.

]]>
http://greynode.org/2009/11/02/automake-and-autoconf-made-simple/feed/ 0
Ruby from Other Languages http://greynode.org/2009/10/25/ruby-from-other-languages/ http://greynode.org/2009/10/25/ruby-from-other-languages/#comments Mon, 26 Oct 2009 05:06:30 +0000 enum http://greynode.org/?p=134 I was just browsing around for some material on ruby and I found this page very helpful:

http://www.ruby-lang.org/en/documentation/ruby-from-other-languages/

It’s not a ruby bible or anything, but I think it is a good intro for experienced programmers.

]]>
http://greynode.org/2009/10/25/ruby-from-other-languages/feed/ 0
Prefixes for Binary Multiples http://greynode.org/2009/06/26/prefixes-for-binary-multiples/ http://greynode.org/2009/06/26/prefixes-for-binary-multiples/#comments Fri, 26 Jun 2009 21:57:47 +0000 enum http://greynode.org/?p=83

Read the rest of this entry »

]]>
I have become increasingly frustrated with people refusing to adopt the IEEE 1541-2002 standard. There is no real good reason why it should not be adopted. I think the most common argument I have heard against it is, “That’s stupid! The people who made that up are stupid! You’re stupid for using it!” That is to say, there are no very good arguments against it.

In fact the longer people continue to use the old terminology, the longer the hard drive manufacturers have to use the old terminology to rip off consumers. Did you ever wonder why your hard drive isn’t a full 80GiB? That’s because it said 80GB on the box.

Some argue that it makes no sense to have the extra terminology because computers operate using base 2. The problem with this argument is that the International Bureau of Weights and Measures published a paper in 1998 stating that SI prefixes should only be used with powers of 10.

I think anyone familiar with science understands the necessity for proper measurements. Without them, your data is bad. Let me present a quick example. Let’s say you were looking for a place to store copies of your digital movies. Your computer says they are 4.7GB (but it really means GiB), so you decide to get a 1TB hard drive in which you think you can store 1,000 / 4.7 = 212 full movies. However, the 1TB hard drive is really only 909GiB, which means you can only store about 193 movies. If you leave 20% free space to keep performance up, then that’s only 154 movies.

But the data on the hard drive box was not misrepresented, it was misunderstood. The reason it was misunderstood is because the neglect of the standard passes from the programmer to the average user via the software they use. There I said it, it is your fault programmers.

If you think you have a real reason why not to adopt it, please leave a comment.

A quick explanation of the standard, courtesy of wikipedia:

IEEE 1541 recommends:

  • a set of units to refer to quantities used in digital electronics and computing:
    • bit (symbol ‘b’), a binary digit;
    • byte (symbol ‘B’), a set of adjacent bits (usually, but not necessarily, eight) operated on as a group;
    • octet (symbol ‘o’), a group of eight bits;
  • a set of prefixes to indicate binary multiples of the aforesaid units:
    • kibi (symbol ‘Ki’), 2101024;
    • mebi (symbol ‘Mi’), 2201048576;
    • gibi (symbol ‘Gi’), 2301073741824;
    • tebi (symbol ‘Ti’), 2401099511627776;
    • pebi (symbol ‘Pi’), 2501125899906842624;
    • exbi (symbol ‘Ei’), 2601152921504606846976;
  • that the first part of the binary prefix is pronounced as the analogous SI prefix, and the second part is pronounced as bee;
  • that SI prefixes are not used to indicate binary multiples.
]]>
http://greynode.org/2009/06/26/prefixes-for-binary-multiples/feed/ 4
Multicore Programming Primer http://greynode.org/2009/06/26/multicore-programming-primer/ http://greynode.org/2009/06/26/multicore-programming-primer/#comments Fri, 26 Jun 2009 16:41:02 +0000 enum http://greynode.org/?p=79

Read the rest of this entry »

]]>
MIT has been offering free videos of various courses for quite a while now. Today I came across this video series on multicore programming. I haven’t watched but a few minutes of it so far, but I am hoping to watch all of them before the next semester starts if it proves to be useful in some way. It uses the PS3 framework — I am not sure if this poses an issue with using the videos yet.

]]>
http://greynode.org/2009/06/26/multicore-programming-primer/feed/ 0
Functional Programming with the Unix Command Line http://greynode.org/2009/06/25/functional-programming-with-the-unix-command-line/ http://greynode.org/2009/06/25/functional-programming-with-the-unix-command-line/#comments Fri, 26 Jun 2009 06:29:30 +0000 enum http://greynode.org/?p=66

Read the rest of this entry »

]]>
I was pondering the unix command line the other day while reading sed & awk, and I came to the following revelation: variables aside, the unix command line is a lot like functional programming. Using the command line, you can build a functional program by redirecting I/O between various programs, or functions. Many of the programs one uses to build one-liners are essentially pure functions. An example of a pure function on the unix command line might be the programs uniq or sort. Various other functions such as ps and wget are not so pure, as their output relies on I/O with the operating sytem or server.

All theory aside, here are a few good one-liners.

Uploading Files

tar cjvf - yourDirectory | ssh uname@host "cat > yourDirectory.tar.bz2"

This command will create an archive of “yourDirectory” and upload it to your server via SSH, all in one chained command. Quite handy for uploading local content for sharing or backup.

Edit: Apparently this command transfers large files extremely slow. Oops.

Checking FreeBSD UPDATING File Automatically

portversion | awk '/</ {print $1}' |
xargs -I '{}' awk '/AFFECTS:.*{}/ {print}' /usr/ports/UPDATING > updates.txt

Ok. So this command pertains to the FreeBSD ports system. It is not recommended that you upgrade all your ports at once. There is often information contained in the file /usr/ports/UPDATING about special instructions you may have to follow while upgrading a package. Often times you may have to recompile other packages, or add a line to a configuration file, etc. The string above will check all of the ports tht are out of date, and compare them to the UPDATING file to see if the package is contained. If it is, it writes the name of that port to updates.txt. You can then use this file to know which ports have special instructions. You should probably write an alias for this one.

Converting Line Endings

The following two commands should help with reformatting files. Often times I find myself getting web designs from someone who uses windows. Most editors on windows will save files with DOS line endings. When you open one of these files in unix (in certain editors), the line endings won’t appear properly. The following commands will circumvent this problem, in either situation.

Convert from unix (\n) to DOS (\r\n)

awk '{sub(/$/, "\r")};1' unix_endings.txt > dos_endings.txt

Convert from DOS (\r\n) to unix (\n)

awk '{sub(/\r$/, "")};1' dos_endings.txt > unix_endings.txt

And yes, I realize that I use too many commas when I write: I’m working on it.

]]>
http://greynode.org/2009/06/25/functional-programming-with-the-unix-command-line/feed/ 0
Updating Rails on OS X http://greynode.org/2009/06/23/updating-rails-on-os-x/ http://greynode.org/2009/06/23/updating-rails-on-os-x/#comments Wed, 24 Jun 2009 02:04:17 +0000 enum http://greynode.org/?p=60

Read the rest of this entry »

]]>
If you install XCode for OS X 10.5, it automatically comes with a version of rails. This is (generally) not the latest version however, and you must take a few steps to update it. First you need to update rubygems with the following:

macintosh$ sudo gem install rubygems-update
macintosh$ sudo update_rubygems

After updating rubygems, you should be able to update the rest of your gems without a problem (including rails) via the following:

macintosh$ sudo gem update

After that you should be ready to build a rails app.

]]>
http://greynode.org/2009/06/23/updating-rails-on-os-x/feed/ 0
Scripting Languages http://greynode.org/2009/06/22/scripting-languages/ http://greynode.org/2009/06/22/scripting-languages/#comments Mon, 22 Jun 2009 22:02:04 +0000 enum http://www.greynode.org/?p=48

Read the rest of this entry »

]]>
I just thought I’d share with you Random thoughts on scripting languages by Brian Kernighan.

After reading this, I looked up the source code to the unix utility wc. The GNU wc program was written in C, and amounted to about 800 lines with comments. A similar program written in awk is a mere 2 lines long:

{ nc += length($0) + 1; nw += NF }
END { print NR, "lines", nw, "words", nc, "characters" }

I know you are probably thinking that I just made an unfair comparison. Yes, I did.

]]>
http://greynode.org/2009/06/22/scripting-languages/feed/ 0
Learning Haskell http://greynode.org/2009/05/17/9/ http://greynode.org/2009/05/17/9/#comments Mon, 18 May 2009 06:56:20 +0000 enum http://www.greynode.org/?p=9

Read the rest of this entry »

]]>
I have recently been continuing my journey with Haskell, the purely functional programming language. I had dabbled with it a little bit over the winter, but haven’t had much practice since then. It is my hope that over the summer I will be able to get a fair amount of practice with the language.

I have settled on using the book Real World Haskell as my first stepping stone. It has gotten good reviews, and was recommended to me by one of my professors. There is a free version of it online, if you wish to check it out. It uses quite a bit of comparison with imperative languages, at least in the early chapters, so I don’t recommend it to someone who has never programmed before.

For those of you who are not familiar with functional programming, I am going to present to you a few problems and the elegant solutions provided by Haskell. The first I will show you is an implementation of quicksort.

qsort (x:xs) = qsort (filter (< x) xs) ++ [x] ++ qsort (filter (>= x) xs)

This implementation of quicksort is so clean it makes me sick. It is essentially the definition of the quicksort algorithm. Because of the facilities provided by Haskell, it translates quite nicely.

Another problem I’d like to show you involves counting. Say you had a string of words, and you would like to count the number of words that start with an upper-case letter. In an imperative language, this might involve several loops and/or the use of regular expressions. With Haskell, its a simple matter of function composition. Have a look:

capCount = length . filter (isUpper . head) . words

This might look odd if you are not familiar with functional programming or function composition, but it is a really simple function. The ‘.’ operator simply takes the output from the function on the right, and feeds it into the function on the left. The ‘words’ function breaks a string up into a list of words wherever there is a space. The ‘head’ function returns the first element of the list, ‘isUpper’ will return a boolean value saying whether or not a Character is upper-case, ‘filter’ applies a function over a list and removes items that evaluate to False, and ‘length’ returns the length of the list. As you can see, it’s much easier to describe with code than with words.

Anyway, I will be posting anything I find interesting on here. Hopefully someone other than myself will benefit from it.

]]>
http://greynode.org/2009/05/17/9/feed/ 0