REFACTORME

Mar 04, 2009 18:49


def _otherParty(n=0): """Return function that converts input fields [and state] to other party number/URL. The conversion function will return at most `n' characters; 0 stands for infinity (default). """ def conv(fs, *_): if _channel(fs, ['VOICE']): if n: return fs[29][:n] else: return fs[29 ( Read more... )

lazyweb, code, english, python

Leave a comment

Comments 4

stereotype441 March 4 2009, 20:08:45 UTC
My first thought is this:

def _otherParty(n=0):
if n: trim = lambda str: str[:n]
else: trim = lambda str: str
def conv(fs, *_):
if _channel(fs, ['VOICE']):
return trim(fs[29])
if _channel(fs, ['WAP', 'WEB']):
return _cropURL(trim(fs[26]))
return trim(fs[26])

But I'm suspicious of this line: if n: return _cropURL(fs[26][:n]).

Did you possibly mean this instead? if n: return _cropURL(fs[26])[:n]

If so, then you could refactor further:

def _otherPartyUntrimmed(fs, *_):
if _channel(fs, ['VOICE']): return fs[29]
if _channel(fs, ['WAP', 'WEB']): return _cropURL(fs[26])
return fs[26]

def _otherParty(n=0):
if n:
return lambda fs, *_: _otherPartyUntrimmed(fs)[:n]
else:
return _otherPartyUntrimmed

BTW, how did you get the nice syntax highlighting in LJ?

Reply

vorotylo March 4 2009, 21:47:45 UTC

> But I'm suspicious of this line: if n: return _cropURL(fs[26][:n]).

> Did you possibly mean this instead? if n: return _cropURL(fs[26])[:n]

I think, both statements are equivalent in my context:

_cropURL = lambda s: s.split('?', 1)[0]

fs[26][:n] inside
parentheses may be an unnoticeable bit more efficient as long
as [:n] is O(1) and str.split()
- O(n)...

I am going to use the second of your solutions, though both are good.

> BTW, how did you get the nice syntax highlighting in LJ?

M-x htmlize-region. Then some CSS inlining (replacing `class="ABC"' entries with `style="color:XYZ"').

Thanks, Paul! :-)

Reply

palm_mute March 5 2009, 08:43:46 UTC
You don't have to do CSS inlining by hand, it's just a matter of M-x customizing-group.

Reply

vorotylo March 5 2009, 09:31:16 UTC

Indeed!

(setq htmlize-output-type 'inline-css) ;) Thanks a lot!

Reply


Leave a comment

Up