Thursday, December 27, 2007

crazy shell code

for i in $(ack ".+_[^_]+_[^_]+" include/libdodo/*Ex.h|grep -vE "(_STR|_H_)" | sed 's/,//' | gawk '{print $2}' | tr "\n" " "); do a=$(echo $i | sed -r 's/(.*[EX_]{3}).*/\1/'); b=$(echo $i | sed -r 's/.*[EX_]{3}(.*)/\1/'); c=$(echo $b | sed 's/_//g'); d="$a$c"; for j in $(find ./ | grep -E "(\.cc|\.h)$"); do sed -ri "s/$i/$d/g" $j; done; done
(c) Ni@m unix programmers are really lazy. This code searches for strings in 'Ex' headers that contain 'EX_' and some '_'. Then it removes '_' after '.*EX_' in the found string and replaces the old string with the new one in the project tree. Yep, I'm lazy-crazy ;)

Wednesday, December 26, 2007

random vs. urandom

random and urandom are devices in /dev that provide random data for applications. The main difference between random and urandom is how they are pulling random data from kernel. random always takes data from entropy pool. If the pool is empty, random will block the operation until the pool would be filled enough. urandom will genarate data using SHA(or any other algorithm, MD5 sometimes) algorithm in the case kernel entropy pool is empty. urandom will never block the operation. I've been testing them for some time and have found out that kernel's entropy pool is empty quite often. So /dev/random device causes operations to block all the time for a long time. That's not good when you are generating lots of UIDs. In "On entropy and randomness" article was mentioned that random is for paranoid purposes =). urandom provides quite enough random data for everyday usage. urandom RNG algorithm is strong enough to provide random data + it takes data from entropy pool if it's available. If there is no task to generate strong random data on which will depend humans being use urandom instead of random.

Tuesday, December 25, 2007

weakness of scripting languages

I think the biggest weakness of scripting languages - is a lack of strict rules for syntax, interpretation and so on. It's easy to break rules if they were not defined by some committee and were certified by ISO for example. Even every scripting language I used has such issue: perl, python, php, javascript etc. Quick issues:

  • perl: various ways to define the same thing. The screaming example is sub definition.
  • python: code indentation. It's a requirement to indent the code, but there is no strict rules how to do that.
  • php: variable definition. You may use undefined variable. It's only an interpreter's notice that you are using it.

python: code indentation

The most interesting thing in python's syntax is indentation. You can't write non-indented code - interpreter will argue you: you have to indent function's, loop's, condition's etc. bodies. It's a great thing except that the python interpreter doesn't bother how you indent your code. You can indent it with a single space or eight tabs. The thing you have to worry about - to keep one style of indentation in one block(e.g. you can indent function's body with tabs and loop's body inside the function you can indent with spaces for example). Also you may write inline code. So you can write indeed ugly code in python. It seems ugly as for me:

def func():
 for i in xrange(10):
                     if i <= 5: print i + 1
                       print i - 2
So what's the purpose of python's code indentation requirement if you can anyway easily break the code?