http://127.0.0.1:631
Stumbled upon this one today.
This is O.R.Senthil Kumaran's blogspot on programming and technical things. The title is a line from "Zen of Python".
_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
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
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
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?
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
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
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
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.
And the output from the session will be:
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
* 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.
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.
Subscribe to:
Posts (Atom)