Help beautify ugly heuristic code

Lonnie Princehouse finite.automaton at gmail.com
Wed Dec 8 18:39:15 EST 2004


Regular expressions.

It takes a while to craft the expressions, but this will be more
elegant, more extensible, and considerably faster to compute (matching
compiled re's is fast).

Example using the top five from your function's comments:

. host_patterns = [
.     '^1Cust\d+\.tnt\d+\..*\.da\.uu\.net$',
.     '^user\d+\.net\d+\.mo\.sprint-hsd\.net$',
.     '^.*\.roadrunner\.nf\.net$',
. ]
.
. host_expr = re.compile('|'.join(host_patterns))
.
. # only implementing host string matching, but you get the idea.
. def is_dynip(host):
.     # host names are case insensitive
.     host = host.lower()
.     return host_expr.match(host) is not None

>> is_dynip("1Cust200.tnt8.bne1.da.uu.ne")
True
>> is_dynip("google.com")
False


(the dots preceding code are to fool indentation stripping... please
ignore them)




More information about the Python-list mailing list