C:\Users\kurose\workspace>python
Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> raise SystemExit("bye")
bye
C:\Users\kurose\workspace>
Kind of like "hey guys, check it out you can just duct tape down the dead-man's switch on this power tool and use it one handed". In Python.
Thursday, October 31, 2013
Wednesday, October 30, 2013
gEvent friendly REPL
I was surprised how little code this was. The fileobject module added in gEvent 1.0 and the standard library code module make this trivial.
import sys
import code
from gevent import fileobject
_green_stdin = fileobject.FileObject(sys.stdin)
_green_stdout = fileobject.FileObject(sys.stdout)
def _green_raw_input(prompt):
_green_stdout.write(prompt)
return _green_stdin.readline()[:-1]
def run_console(local=None, prompt=">>>"):
code.interact(prompt, _green_raw_input, local=local or {})
if __name__ == "__main__":
run_console()
import sys
import code
from gevent import fileobject
_green_stdin = fileobject.FileObject(sys.stdin)
_green_stdout = fileobject.FileObject(sys.stdout)
def _green_raw_input(prompt):
_green_stdout.write(prompt)
return _green_stdin.readline()[:-1]
def run_console(local=None, prompt=">>>"):
code.interact(prompt, _green_raw_input, local=local or {})
if __name__ == "__main__":
run_console()
Tuesday, October 29, 2013
missing the __line__ macro from C?
Frame objects to the rescue!
import sys
def __line__():
f = sys._getframe().f_back
return f.f_lineno + f.f_code.co_firstlineno
import sys
def __line__():
f = sys._getframe().f_back
return f.f_lineno + f.f_code.co_firstlineno
Wednesday, October 16, 2013
sigfigs
def _sigfigs(n, sigfigs=3):
'helper function to round a number to significant figures'
if n == 0 or math.isnan(n): # avoid math domain errors
return n
return round(float(n), -int(math.floor(math.log10(abs(n))) - sigfigs + 1))
'helper function to round a number to significant figures'
if n == 0 or math.isnan(n): # avoid math domain errors
return n
return round(float(n), -int(math.floor(math.log10(abs(n))) - sigfigs + 1))
Wednesday, October 9, 2013
Attribution
>>> class Attribution(object):
... def __init__(self):
... self.instance = "instance"
... type = "type"
... def __getattr__(self, key):
... return "__getattr__"
...
>>> a = Attribution()
>>> a.instance
'instance'
>>> a.type
'type'
>>> a.b
'__getattr__'
... def __init__(self):
... self.instance = "instance"
... type = "type"
... def __getattr__(self, key):
... return "__getattr__"
...
>>> a = Attribution()
>>> a.instance
'instance'
>>> a.type
'type'
>>> a.b
'__getattr__'
Thursday, October 3, 2013
time THIS
>>> timeit.timeit("s.recv(1, socket.MSG_PEEK", '''
... import socket
... s = socket.create_connection( ("google.com", 80) )
... s.send("blah blah\\n\\r\\n\\r\\n\\r\\n\\r\\n\\rblahblah blah"
... ''')
Traceback (most recent call last):
File "<stdin>", line 5, in <module>
File "C:\Python27\lib\timeit.py", line 230, in timeit
return Timer(stmt, setup, timer).timeit(number)
File "C:\Python27\lib\timeit.py", line 136, in __init__
code = compile(src, dummy_src_name, "exec")
File "<timeit-src>", line 11
_t1 = _timer()
SyntaxError: invalid syntax
... import socket
... s = socket.create_connection( ("google.com", 80) )
... s.send("blah blah\\n\\r\\n\\r\\n\\r\\n\\r\\n\\rblahblah blah"
... ''')
Traceback (most recent call last):
File "<stdin>", line 5, in <module>
File "C:\Python27\lib\timeit.py", line 230, in timeit
return Timer(stmt, setup, timer).timeit(number)
File "C:\Python27\lib\timeit.py", line 136, in __init__
code = compile(src, dummy_src_name, "exec")
File "<timeit-src>", line 11
_t1 = _timer()
SyntaxError: invalid syntax
Subscribe to:
Posts (Atom)