Trigraph Idiom - Original?

J

Jeff Winkler

I've come up with a trigraph idiom, am curious if it's been done before
(probably). I like to use trigraphs occasionally.

Scenario: I'm doing an RSS->Javascript conversion for our intranet. I'd
like to use different CSS class if a post is new. Code:

hoursOld=abs(time.time()-time.mktime(i.modified_parsed))/3600
cssClass=['rssLink','rssLinkNew'][hoursOld<12]
entry='<a href="%s" class="%s" target="detail">%s</a>' %
(cssClass,i['link'],i['title'])

So, ['rssLink','rssLinkNew'] is index by boolean value- False:0, or
True:1.

I realize many will find this hideous, but 3 lines of code to do
something simple seems equally bad. Thoughts? Is there a better way?

- Jeff
 
J

John Machin

I've come up with a trigraph idiom, am curious if it's been done before
(probably). I like to use trigraphs occasionally.

Scenario: I'm doing an RSS->Javascript conversion for our intranet. I'd
like to use different CSS class if a post is new. Code:

hoursOld=abs(time.time()-time.mktime(i.modified_parsed))/3600
cssClass=['rssLink','rssLinkNew'][hoursOld<12]
entry='<a href="%s" class="%s" target="detail">%s</a>' %
(cssClass,i['link'],i['title'])

So, ['rssLink','rssLinkNew'] is index by boolean value- False:0, or
True:1.

I realize many will find this hideous, but 3 lines of code to do
something simple seems equally bad. Thoughts? Is there a better way?

1. You appear to be conflating "trigraph" [a truly hideous
little-known lexical feature of C] and "ternary operator".

2. No, it's not original; the idea of using a boolean value as index
to an array of 2 elements probably occurred to somebody at about the
time that subroutines were invented. Look at the threads that break
out periodically in this newsgroup: "why doesn't python have a ternary
operator", "why doesn't 'foo and bar or zot' work sometimes" etc etc
-- it usually gets a mention.

3. Some might say it is hideous, but perhaps less hideous than (a)
using "i" as a reference to data other than a loop index (b) being so
worried about the longevity of your keyboard and/or thumb(s) that you
hit the spacebar only once or twice (other than inside string
literals) in three statements.

4. In the 3rd statement, you appear at best to have gravely misleading
variable names -- cssClass is shoved into the href="%s" but i['link']
is shoved into the class="%s" -- or worse, a stuffup.
 
S

Shane Hathaway

Jeff said:
I've come up with a trigraph idiom, am curious if it's been done before
(probably). I like to use trigraphs occasionally.

Scenario: I'm doing an RSS->Javascript conversion for our intranet. I'd
like to use different CSS class if a post is new. Code:

hoursOld=abs(time.time()-time.mktime(i.modified_parsed))/3600
cssClass=['rssLink','rssLinkNew'][hoursOld<12]
entry='<a href="%s" class="%s" target="detail">%s</a>' %
(cssClass,i['link'],i['title'])

So, ['rssLink','rssLinkNew'] is index by boolean value- False:0, or
True:1.

I realize many will find this hideous, but 3 lines of code to do
something simple seems equally bad. Thoughts? Is there a better way?

The need for ternary expressions seems to center around presentation to
humans. I find myself using such expressions frequently in presentation
templates or in code that prepares information for templates. You're
using it the same way.

I always write it this way:

condition and truecase or falsecase

This works as long as truecase evaluates as a true value. (If it
doesn't, the result of the expression is falsecase.) So I would write
the cssClass assignment like this:

cssClass = (hoursOld < 12) and 'rssLinkNew' or 'rssLink'

The C equivalent is more readable, but this is close enough. It's the
same trick people use in shell scripts.

Shane
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,237
Messages
2,571,189
Members
47,823
Latest member
eipamiri

Latest Threads

Top