Posts Tagged ‘python’

get registered domain in Python and Javascript

April 5, 2011

reg-dom-libs are a set of libraries for C, PHP and Perl to convert an arbitrary domain name to the registered domain name.

  • for simple domains, like http://www.amazon.com or news.ycombinator.com, the task is trivial.
  • for more complicated ones, like http://www.ebay.co.uk or http://www.japantimes.co.jp, handling the second level subdomain is a little painful.
  • for exoteric ones, like nic.com.ai or http://www.nic.net.ge or 公司.cn, the problem becomes virtually impossible.
  • after seeing stupid ones, like http://www.comune.caserta.it (believe it or not, the registered domain is comune.caserta.it!), I gave up finding an elegant algorithm for the problem.
  • a full list of valid registered domain is necessary. luckily, it is available (and nightly updated) here.

inspired by reg-dom-libs, I’ve ported the algorithm to Python and Javascript.
see the tests at the end of each file for an example of the usage.

a demo is availble here.

Advertisements

twill revived

March 11, 2011

after reading how to do automated testing in python, I’ve started using twill, by C. Titus Brown

twill is a simple language that allows users to browse the Web from a command-line interface. With twill, you can navigate through Web sites that use forms, cookies, and most standard Web features.

unfortunately, development on twill seems to have stopped 3 years ago.

I’ve cloned the project on Github, fixed some deprecation warnings and added a cool new feature to inspect headers, as well as html.

testing is more interesting than I thought.

tdaemon for python3

February 20, 2011

tdaemon is a neat little utility to do continuous testing in python. unfortunately, the original version only works with python2.

I’ve spent a little time to make it work with python3.

enjoy!

missing Dec_0 and friends from python decimal

July 29, 2010

This fails in py2.4, py2.5.1 and py2.6:

from decimal import Dec_0

But it works in all the other py2.5 subversions!

It seems that exposing Dec_0 (and other similar constants) was considered a bug and “fixed” on Jan 3rd 2009 by renaming it _Dec_0.

Indeed, if I had looked at the source code, I would have seen that Dec_0 was for “internal use only”.

From /usr/lib/python2.5/decimal.py:5159

##### Useful Constants (internal use only) ################################

# Reusable defaults
Inf = Decimal('Inf')
negInf = Decimal('-Inf')
NaN = Decimal('NaN')
Dec_0 = Decimal(0)
Dec_p1 = Decimal(1)
Dec_n1 = Decimal(-1)

Those definitions are missing in py2.4 and py2.5.1, and renamed in py2.6.

Damn: never use undocumented features!

wordpress python library

June 6, 2010

today, I found this.

it’s a wonderfully simple Python library to manage your WordPress blog via XML-RPC.

enjoy!

vim and python comments

August 12, 2009

I’m currently undergoing a profound revision (read “cleanup”) of my .vimrc and I recently had to choose a decent plug-in to comment python code.

the best so far is EnhancedCommentify. it’s highly configurable, powerful and it supports a different number of languages (not only python).

the only one thing that wasn’t easy to setup if the behaviour described here: being able to comment a block, respecting the indent, in both visual and normal mode.

a clean solution, without the need to define any function is to stick these lines into your .vimrc:


let g:EnhCommentifyPretty='Yes'
let g:EnhCommentifyRespectIndent='Yes'
let g:EnhCommentifyUserBindings="Yes"
let g:EnhCommentifyUseBlockIndent='Yes'


vmap ,c (Plug)VisualComment
vmap ,d (Plug)VisualDeComment
vmap ,g (Plug)VisualTraditional
vmap ,f (Plug)VisualFirstLine
nmap ,c (Plug)Comment
nmap ,d (Plug)DeComment
nmap ,g (Plug)Traditional
nmap ,f (Plug)FirstLine

(but remember to change “(” to “<” and “)” to “>” — wordpress is strict about “<” and “>”…)

enjoy!

qwerty vs dvorak vs colemak to edit python code

February 2, 2009

yesterday, I was intrigued by the idea of choosing a more efficient keyboard layout to work with everyday.

as long as I type python code 90% of my time, I decided to evaluate the efficiency of the first 3 most widespread keyboard layouts: QWERTY, Dvorak and Colemak.

I used this excellent tool to compare the three layouts over the first 50000 characters of the webpy source code.

here are the results:

The Qwerty Keyboard Layout Vs The Dvorak Keyboard Layout

:: UP ::Overall effort

Layout Effort % improvement over worst layout
Qwerty 306,509.6 0.0 %
Dvorak 283,868.7 7.4 %
Colemak 275,347.6 10.2 %

:: UP ::Keys for each finger (stronger fingers should be used more often)

Layout Thumbs LPinky LRing LMiddle LIndex RIndex RMiddle RRing RPinky Total
Qwerty 25.9 % 12.4 % 5.3 % 9.5 % 11.6 % 7.6 % 5.6 % 8.9 % 13.2 % 54,237.0 keys
Dvorak 25.9 % 13.0 % 5.2 % 8.1 % 7.3 % 7.5 % 7.5 % 8.2 % 17.3 % 54,237.0 keys
Colemak 25.9 % 12.7 % 4.8 % 7.5 % 9.5 % 8.4 % 10.1 % 6.6 % 14.5 % 54,237.0 keys
Layout Thumbs Left hand Right hand Total
Qwerty 25.9 % 38.8 % 35.2 % 54,237.0 keys
Dvorak 25.9 % 33.6 % 40.5 % 54,237.0 keys
Colemak 25.9 % 34.5 % 39.6 % 54,237.0 keys

:: UP ::Finger Travel Distance (only the horizontal)

Layout Thumbs LPinky LRing LMiddle LIndex RIndex RMiddle RRing RPinky Total
Qwerty 0.0 % 18.1 % 2.0 % 10.5 % 15.5 % 12.1 % 8.6 % 9.9 % 23.4 % 1,441.5 m
Dvorak 0.0 % 21.1 % 4.1 % 3.5 % 10.0 % 12.5 % 7.6 % 10.8 % 30.4 % 1,210.3 m
Colemak 0.0 % 23.7 % 2.5 % 6.1 % 10.2 % 10.2 % 9.3 % 9.3 % 28.6 % 1,131.2 m
Layout Thumbs Left hand Right hand Total
Qwerty 0.0 % 46.0 % 54.0 % 1,441.5 m
Dvorak 0.0 % 38.8 % 61.2 % 1,210.3 m
Colemak 0.0 % 42.6 % 57.4 % 1,131.2 m

:: UP ::% home keys (out of all character keys plus SHIFT and ENTER, but excluding keys operated by thumbs)

Qwerty 20.4 %
Dvorak 33.7 %
Colemak 38.7 %

:: UP ::% keys typed with the same finger as the previous key (excluding repeating keys like ss)

Qwerty 4.5 %
Dvorak 3.8 %
Colemak 3.6 %

:: UP ::% keys typed with the same hand as the previous key (ex: in Qwerty sd or ss are included, but s s is not since you type the space with the other hand)

Qwerty 26.8 %
Dvorak 20.0 %
Colemak 24.6 %

:: UP ::% keys typed with the same hand as the previous key and jumping a row (ex: in Qwerty ev is a row jump – you jump over the middle row – and it’s awkward to type; ef is not a row jump)

Qwerty 10.3 %
Dvorak 5.7 %
Colemak 4.1 %

:: UP ::% keys typed with the same hand as the previous key and in reverse order (ex: in Qwerty df or kj are typed in the easier order pinky-ring-middle-index, whereas fd and jk are in reverse order index-middle-ring-pinky, and harder to type)

Qwerty 15.4 %
Dvorak 11.9 %
Colemak 15.2 %

:: UP ::% keys that need the SHIFT modifier

Qwerty 14.3 %
Dvorak 14.3 %
Colemak 14.3 %

:: UP ::Pairs of consecutive keys typed with the same finger in Qwerty

:_NewLine_ 253 times 0.47 %
de 200 times 0.37 %
tr 158 times 0.29 %
rt 102 times 0.19 %
ol 99 times 0.18 %
un 98 times 0.18 %
q_LeftShift_ 87 times 0.16 %
ce 86 times 0.16 %
“_NewLine_ 81 times 0.15 %
ed 80 times 0.15 %
}_NewLine_ 62 times 0.11 %

:: UP ::Pairs of consecutive keys typed with the same finger in Dvorak

ct 196 times 0.36 %
s_RightShift_ 169 times 0.31 %
db 152 times 0.28 %
l_RightShift_ 137 times 0.25 %
‘_LeftShift_ 133 times 0.25 %
rn 115 times 0.21 %
ls 100 times 0.18 %
}_NewLine_ 62 times 0.11 %
=_RightShift_ 59 times 0.11 %
“_LeftShift_ 58 times 0.11 %
e. 58 times 0.11 %

:: UP ::Pairs of consecutive keys typed with the same finger in Colemak

:_NewLine_ 253 times 0.47 %
ue 237 times 0.44 %
db 152 times 0.28 %
e, 89 times 0.16 %
q_LeftShift_ 87 times 0.16 %
“_NewLine_ 81 times 0.15 %
}_NewLine_ 62 times 0.11 %
]_NewLine_ 46 times 0.08 %
pt 44 times 0.08 %
‘_RightShift_ 41 times 0.08 %
“_RightShift_ 41 times 0.08 %

:: UP ::Key frequency in your text

_Space_ 14067 times 25.94 %
_LeftShift_ 4391 times 8.10 %
e 3239 times 5.97 %
t 2184 times 4.03 %
s 2170 times 4.00 %
r 1890 times 3.48 %
a 1619 times 2.99 %
o 1557 times 2.87 %
l 1530 times 2.82 %
n 1519 times 2.80 %
_NewLine_ 1511 times 2.79 %
i 1372 times 2.53 %
_RightShift_ 1357 times 2.50 %
d 1014 times 1.87 %
f 971 times 1.79 %
c 855 times 1.58 %
u 851 times 1.57 %
m 716 times 1.32 %
p 715 times 1.32 %
( 674 times 1.24 %
) 673 times 1.24 %
. 658 times 1.21 %
623 times 1.15 %
, 573 times 1.06 %
b 571 times 1.05 %
563 times 1.04 %
_ 554 times 1.02 %
= 518 times 0.96 %
: 490 times 0.90 %
q 463 times 0.85 %
y 423 times 0.78 %
h 416 times 0.77 %
w 352 times 0.65 %
> 348 times 0.64 %
g 304 times 0.56 %
v 257 times 0.47 %
x 253 times 0.47 %
k 231 times 0.43 %
< 154 times 0.28 %
/ 125 times 0.23 %
[ 120 times 0.22 %
] 119 times 0.22 %
; 113 times 0.21 %
0 86 times 0.16 %
` 85 times 0.16 %
+ 84 times 0.15 %
1 81 times 0.15 %
{ 80 times 0.15 %
} 79 times 0.15 %
78 times 0.14 %
j 76 times 0.14 %
* 72 times 0.13 %
# 69 times 0.13 %
2 67 times 0.12 %
% 55 times 0.10 %
$ 52 times 0.10 %
3 31 times 0.06 %
6 29 times 0.05 %
8 18 times 0.03 %
@ 17 times 0.03 %
\ 14 times 0.03 %
z 13 times 0.02 %
5 13 times 0.02 %
4 8 times 0.01 %
? 8 times 0.01 %
9 7 times 0.01 %

:: UP ::Frequent pairs of keys in your text

_Space__Space_ 9243 times 17.04 %
_NewLine__Space_ 1233 times 2.27 %
_Space__LeftShift_ 805 times 1.48 %
_LeftShift_( 674 times 1.24 %
_LeftShift_) 673 times 1.24 %
_LeftShift_” 563 times 1.04 %
_LeftShift__ 554 times 1.02 %
se 549 times 1.01 %
,_Space_ 543 times 1.00 %
_LeftShift_: 490 times 0.90 %
el 405 times 0.75 %
er 377 times 0.70 %
in 364 times 0.67 %
_Space__RightShift_ 362 times 0.67 %
or 352 times 0.65 %
_LeftShift_> 348 times 0.64 %
_Space_i 344 times 0.63 %
_Space_s 341 times 0.63 %
te 340 times 0.63 %
e_LeftShift_ 338 times 0.62 %
=_Space_ 332 times 0.61 %
_Space_= 321 times 0.59 %
re 306 times 0.56 %
lf 294 times 0.54 %
)_NewLine_ 281 times 0.52 %
e_Space_ 281 times 0.52 %

actually, I was disappointed: there seem to be no clear advantage of Dvorak or Colemak, wrt QWERTY and, given the popularity of the latter, switching does not seem to be a good idea…

tab completion in python shell

June 25, 2008

recently I saw with this little piece of code to give the standard python shell tab completion capabilities:

# setup tab completion in python shell
import rlcompleter
import readline
readline.parse_and_bind("tab: complete")

obviously, you can use the environmental variable PYTHONSTART to execute this few lines anytime you start your python shell.

enjoy!

bvp

April 14, 2008

for you, Python lover, who always needs Matlab functions: boundary value problems are not a problem anymore.

Googling for a bvp4c clone, I ended up here:

This is a Python wrapper for a modified version of the COLNEW boundary value problem solver by U. Ascher and G. Bader. Modifications made include vectorization over mesh points and better compatibility with Python.

there are little differences in the usage, though. I tried to reproduce the examples in this tutorial, to clarify things. You can download the first two examples here.

enjoy!