Share Code Tips

Steven D'Aprano steve+comp.lang.python at pearwood.info
Fri Jul 19 13:59:10 EDT 2013


On Fri, 19 Jul 2013 09:51:23 -0400, Devyn Collier Johnson wrote:

> def KDE_VERSION():
>     print(subprocess.getoutput('kded4 --version | awk -F:
>     \'NR == 2 {print $2}\'').strip()) ##Get KDE version##

I run KDE 3, and the above does not work for me.

*half a wink*

By the way, a comment that doesn't tell you anything that you don't 
already know is worse than useless. The function is called "KDE_VERSION, 
what else would it do other than return the KDE version? 


x += 1  # add 1 to x

Worse than just being useless, redundant comments are dangerous, because 
as a general rule comments that don't say anything useful eventually 
become out-of-date, they become *inaccurate* rather than *redundant*, and 
that's worse than being useless.


> Need a case-insensitive if-statement? Check this out:
> 
> if 'YOUR_STRING'.lower() in SOMEVAR.lower():

Case-insensitivity is very hard. Take German for example:

STRASSE <-> straße

Or Turkish:

İ <-> i
I <-> ı


In Python 3.3, you should use casefold rather than lowercase or uppercase:

if some_string.casefold() in another_string.casefold(): ...


but even that can't always take into account localised rules, e.g. in 
German, you should not convert SS to ß for placenames or person names, so 
for example Herr Meißner and Herr Meissner are two different people. This 
is one of the motivating reasons for introducing the uppercase ß.

http://opentype.info/blog/2011/01/24/capital-sharp-s/



-- 
Steven



More information about the Python-list mailing list