[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Implement C's Switch in Python 3

When a client demanded his way on this issue, the action we took was, as 
below, to create a list (called ordinal) and to use the dd (day) value 
as an index.

>>>>> [ nthSuffix(day) for day in range(1,32)]
>> ['1st', '2nd', '3rd', '4th', '5th', '6th', '7th', '8th', '9th', '10th',
>> '11th', '12th', '13th', '14th', '15th', '16th', '17th', '18th', '19th',
>> '20th', '21st', '22nd', '23rd', '24th', '25th', '26th', '27th', '28th',
>> '29th', '30th', '31st']

I'm not sure if employing a dict with hashed retrieval would be any more 
efficient than indexing into a list.

> Not having a default case as in switch forced you to write out all possible combinations.
> I think the intent and readbility of switch statements is a bit nicer.

Perhaps not. A list look-up is self-documenting, and the fact that every 
choice is covered reduces error-risk.

The criticism above, "demanded", is justified by the international 
standard for dates (ISO 8601).

This would normally see us coding "2019-02-03". The arrangement of 
larger to ever more precise time-units is very useful in databases and 
applications such as file-names, because it sequences logically.

However, that is not the way 'normal people' like to write their dates. 
The trouble with 'common practice' though, is that it is localised. 
There is great confusion between the way different cultures (even 
staying within the English-speaking world) express dates. Is 3/2/2019 
referring to "3rd February" or "March 2nd"?

Not part of the standard, but given the differences between US and 
European commonly-used date formats, I used to insist upon dd-mmm-yy or 
ccyy, thus 3-Feb-2019. Before handy PSL date utilities, this raised the 
need for another list - of month names/abbreviations.

Why lists? The other advantage is realised when we move out of English.

It is easy to have multiple lists - one for each language (assuming 
Gregorian calendar - given that other calendar systems are quite another 
discussion!) and thus select by date and by location.

Now back to ordinal dates - the "st", "th", etc suffixes only work in 
English. You'd need another list (but no great coding complexity) to 
cope with a second, third, ... language!

(ACK: the OP didn't specify such extensive needs)

Regards =dn