473,387 Members | 1,502 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Trigraph Idiom - Original?

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

Jul 19 '05 #1
3 3079
On 27 Apr 2005 16:26:02 -0700, "Jeff Winkler" <wi******@gmail.com>
wrote:
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.

Jul 19 '05 #2
Jeff Winkler wrote:
I've come up with... cssClass=['rssLink','rssLinkNew'][hoursOld<12]


Not hideous, but:
cssClass=('rssLink','rssLinkNew')[hoursOld<12]
is IMO, slightly less surprising,

and in this context:
cssClass = hoursOld<12 and 'rssLinkNew' or 'rssLink'


reads better too

Michael

Jul 19 '05 #3
Jeff Winkler wrote:
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
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Thomas Philips | last post by:
I recently had the need to sort a large number of lists of lists, and wondered if an improvement to the Decorate-Sort-Undecorate idiom is in the works. Ideally, I would like to sort the list of...
3
by: David Morgenthaler | last post by:
In many of my scripts I've used the following idiom for accessing data files placed nearby: BASEDIR = os.path.dirname(__file__) .. .. .. fp = file(os.path.join(BASEDIR,"somefile.txt")) .. ..
28
by: Fábio Mendes | last post by:
I'm sorry if it's an replicate. Either my e-mail program is messing with things or the python-list sent my msg to /dev/null. I couldn't find anything related in previous PEP's, so here it goes a...
7
by: Icosahedron | last post by:
I've been going through some old code trying to clean it up and rearchitect it based on more modern C++ idioms. In the old code I often used the Pimpl idiom on a class by class basis, creating...
2
by: Peteris Krumins | last post by:
Hello! I was playing around pimpl idiom and discovered that there is a problem with it if a class member template function exists which has to access private data, since only the forward...
2
by: Peteris Krumins | last post by:
Hello, while reading through iso c standard (August 3, 1998), I noticed something I had never heard of in C: "Trigraph sequences", 5.2.1.1 The standard says: "All occurrences in a source file...
2
by: Ravi | last post by:
Why is the trigraph processing reqd.?
8
by: Emin | last post by:
Dear Experts, When writing large classes, I sometimes find myself needing to copy a lot of parameters from the argument of __init__ into self. Instead of having twenty lines that all basically...
31
by: eliben | last post by:
Hello, In a Python program I'm writing I need to dynamically generate functions and store them in a dict. eval() can't work for me because a function definition is a statement and not an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.