Brainfuck

+++ +++ +++ +[>
+++ +++ +
>
+++ +++ +++ +
<++ .
>+++ ++ .


Brainfuck is easy and interesting.
We work on cells, like if you know c, cell is like ptr = unsigned char *
+ stands for increment like ++*ptr
- stands for decrement like --*ptr
> goes one cell right like ++ptr
< goes one cell left like --ptr
. spits out output like putchar(*ptr)
, expects input. like *ptr = getchar(stdin)
[ is the start of while block test condition (for non-zero) which is like while(*ptr) {
] is the end of the block }
And everything else is comment, like the these sentences.
The above program is designed to output "Hi" and if you include these
sentenses too, it will print "Hii" and wait for some input and enter
presses till the cell becomes zero.

Isn't it cool? :)

BTW, you can run this program using an interpretor called (bf) just an apt-get away.

Kavka's toxin puzzle

In its original form, it is:
An eccentric billionaire places before you a vial of toxin that, if you drink it, will make you painfully ill for a day, but will not threaten your life or have any lasting effects. The billionaire will pay you one million dollars tomorrow morning if, at midnight tonight, you intend to drink the toxin tomorrow afternoon. He emphasizes that you need not drink the toxin to receive the money; in fact, the money will already be in your bank account hours before the time for drinking it arrives, if you succeed. All you have to do is. . . intend at midnight tonight to drink the stuff tomorrow afternoon. You are perfectly free to change your mind after receiving the money and not drink the toxin.

Kavka's theory is that, one cannot intend to do something which one won't do.

There is an analysis of payoffs from different scenarios and the real-world example of this puzzle is:


...the Political Manifesto. Before an election, a political party will release a written document outlining their policies and plans should they win office. Many of these promises may be difficult or impossible to implement in practice. Having won, the party is not obligated to follow the manifesto even if they would have lost without it.

X Window joke

X window: The ultimate bottleneck. Flawed beyond belief. The only thing you have to fear. Somewhere between chaos and insanity. On autopilot to oblivion. The joke that kills. A disgrace you can be proud of. A mistake carried out to perfection. Belongs more to the problem set than the solution set. To err is X windows. Ignorance is our most important resource. Complex nonsolutions to simple nonproblems. Built to fall apart. Nullifying centuries of progress. Falling to new depths of inefficiency. The last thing you need. The defacto substandard. Elevating brain damage to an art form. X window.

Password-less root shell in ubuntu

If you forgot you password for your ubuntu system you can recover using the following steps

1. Turn your computer on.
2. Press ESC at the grub prompt.
3. Press e for edit.
4. Highlight the line that begins kernel ………, press e
5. Go to the very end of the line, add rw init=/bin/bash
6. Press enter, then press b to boot your system.
7. Your system will boot up to a passwordless root shell.

Source

Signature in Evolution

You can have evolution to execute a script as your signature. I had set it so, but whatever I do:


#!/usr/bin/sh
echo "-- "
echo "Senthil"
exec fortune -s



Resulted in something like this:


-- Senthil BOFH excuse #261: The Usenet news is out of date



Finally figured out with the help of this post(thanks man!), that evolution is actually storing every compose action as HTML so

I would need to wrap it in HTML the output of my script to make it a legible signature.

There is a web interface for CUPS

http://127.0.0.1:631

Stumbled upon this one today.

_GLOBAL_DEFAULT_TIMEOUT in socket.py

In python 2.6, the timeout attribute of the socket from various higher level modules (like urllib2) is passed as socket._GLOBAL_DEFAULT_TIMEOUT.
If you look at socket.py in Lib, you will find that

_GLOBAL_DEFAULT_TIMEOUT = object()


a) This kind of a construct was new to me, why should _GLOBAL_DEFAULT_TIMEOUT be set to an object() than say None.

Update: Because None for a timeout is a special value that would set the socket in blocking mode. That is the reason None is not used.

b) If this is just for a global value holding, why not just


global _GLOBAL_DEFAULT_TIMEOUT



Here is my understanding, the reason for having _GLOBAL_DEFAULT_TIMEOUT is to have a global default timeout value that can be accessible via various application layer modules (like ftplib, httplib, urllib).

Update: Correct

But the default value of the timeout is written at the lower layer socket module and is returned by
socket.getdefaulttimeout()
and may be set via socket.setdefaulttimeout or via timeout parameters at the application opening functions (like urllib2.urlopen('url',timeout=42).


Update: So what?. Wrong.


Get socket.getdefaulttimeout() is implemented in C; and to be preserve the value between C methods and the python library methods, an object() would be essential instead of a global.

object() might essentially provide an address location for the variable which would be global.


Am I confused? The way the C implementation works is the value is set to a global variable inside of C and its accessed by interfaces like getdefaulttime and setdefaulttime.

Nothing to maintain global values between Python and modules written in C. Are they sharable? I dont know.

This kind of construct is used to have a object to maintain a global state.

global myobj is just a declaration to the compiler that it is global. declared somewhere else and start using the same value. It is not setting things or even defining for the myobj to be used as global. OKay?


a = 100
print id(a)

def foo():
global a
print a, id(a)

foo()



[12:17:45 senthil]$python a1.py
23379248
100 23379248


So, in effect:

_GLOBAL_DEFAULT_TIMEOUT = object() is a just creating a empty object whose value can be shared.

Thats it and None is not a good option because, None in this case means that socket be a blocking one.


Here are some experimentations with urllib2 and new socket._GLOBAL_DEFAULT_TIMEOUT



Python 2.7a0 (trunk:72879M, May 24 2009, 12:51:19)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> print socket.getdefaulttimeout()
None
>>> socket.setdefaulttimeout(42)
>>> print socket.getdefaulttimeout()
42.0
>>> import urllib2
>>> obj = urllib2.urlopen("http://www.google.com")
>>> dir(obj)
['__doc__', '__init__', '__iter__', '__module__', '__repr__', 'close', 'code', 'fileno', 'fp', 'getcode', 'geturl', 'headers', 'info', 'msg', 'next', 'read', 'readline', 'readlines', 'url']
>>> dir(obj.fp)
['__class__', '__del__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__iter__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__slots__', '__str__', '__subclasshook__', '_close', '_getclosed', '_rbuf', '_rbufsize', '_sock', '_wbuf', '_wbuf_len', '_wbufsize', 'bufsize', 'close', 'closed', 'default_bufsize', 'fileno', 'flush', 'mode', 'name', 'next', 'read', 'readline', 'readlines', 'softspace', 'write', 'writelines']
>>> dir(obj.fp._sock)
['__doc__', '__init__', '__module__', '_check_close', '_method', '_read_chunked', '_read_status', '_safe_read', 'begin', 'chunk_left', 'chunked', 'close', 'debuglevel', 'fp', 'getheader', 'getheaders', 'isclosed', 'length', 'msg', 'read', 'reason', 'recv', 'status', 'strict', 'version', 'will_close']
>>> dir(obj.fp._sock.fp)
['__class__', '__del__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__iter__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__slots__', '__str__', '__subclasshook__', '_close', '_getclosed', '_rbuf', '_rbufsize', '_sock', '_wbuf', '_wbuf_len', '_wbufsize', 'bufsize', 'close', 'closed', 'default_bufsize', 'fileno', 'flush', 'mode', 'name', 'next', 'read', 'readline', 'readlines', 'softspace', 'write', 'writelines']
>>> dir(obj.fp._sock.fp._sock)
['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'accept', 'bind', 'close', 'connect', 'connect_ex', 'dup', 'family', 'fileno', 'getpeername', 'getsockname', 'getsockopt', 'gettimeout', 'listen', 'makefile', 'proto', 'recv', 'recv_into', 'recvfrom', 'recvfrom_into', 'send', 'sendall', 'sendto', 'setblocking', 'setsockopt', 'settimeout', 'shutdown', 'timeout', 'type']
>>> dir(obj.fp._sock.fp._sock.gettimeout)
['__call__', '__class__', '__cmp__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__self__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
>>> print(obj.fp._sock.fp._sock.gettimeout)

>>> print(obj.fp._sock.fp._sock.gettimeout())
42.0

Redirect business again

.htaccess file has

Redirect 302 /index.html "http://localhost/new file.html"

But still when I do:

>>>obj = urllib2.urlopen("http://localhost/index.html")
>>>print obj.code()
200
>>>
Funny, what am I doing and why am I getting it this way? Figuring that out. Because the direct is happening transparently, one is not able to capture the redirect code.

If one needs to the capture redirect code, here is how it is done.


import urllib2

class SmartRedirectHandler(urllib2.HTTPRedirectHandler):
def http_error_302(self, req, fp, code, msg, headers):
result = urllib2.HTTPRedirectHandler.http_error_302(self, req, fp,code, msg,headers)

result.status = code
return result

request = urllib2.Request("http://localhost/index.html")
opener = urllib2.build_opener(SmartRedirectHandler())
obj = opener.open(request)
print 'I capture the http redirect code:', obj.status
print 'Its been redirected to:', obj.url


And the output from the session will be:


[06:59:14 senthil]$python smartredirecthandler.py
I capture the http redirect code: 302
Its been redirected to: http://localhost/new%20file.html

The pasta theory of design

The pasta theory of design:

* Spaghetti: each piece of code interacts with every other piece of code [can be implemented with GOTO, functions, objects]

* Lasagna: code has carefully designed layers. Each layer is, in theory independent. However low-level layers usually cannot be used easily, and high-level layers depend on low-level layers.

* Ravioli: each part of the code is useful by itself. There is a thin layer of interfaces between various parts [the sauce]. Each part can be usefully be used elsewhere.

* ...but sometimes, the user just wants to order "Ravioli", so one coarse-grain easily definable layer of abstraction on top of it all can be useful.

An application design using Twisted's Aspect Oriented Programming Design is that special Ravioli.

http://twistedmatrix.com/projects/core/documentation/howto/tutorial/library.html

Dismal Google Search

Every technical query returns, results from:

www.daniweb.com/forums/thread97401.html
www.daniweb.com/forums/thread110548.html
www.gidforums.com/t-3560.html
www.velocityreviews.com/forums/t280081-floating-point-exception.html

And those pages are invariably filled with advertisements and and sometimes asks me to register to view the result too. I am dismayed, either got to have custom google search to exclude these or try other search engine.