[Python-Dev] Maintenance burden of str.swapcase
Terry Reedy
tjreedy at udel.edu
Tue Sep 6 21:05:16 CEST 2011
On 9/6/2011 12:58 PM, Tres Seaver wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> On 09/06/2011 12:59 PM, Stephen J. Turnbull wrote:
>> Joao S. O. Bueno writes:
>>
>>> Removing it would mean explicitly "batteries removal".
>>
>> That's what we usually do with a dead battery, no?<wink />
>
> Normally one "replaces" dead batteries. :)
Not if it is dead and leaking because the device has been unused for years.
https://www.google.com/codesearch#search/&q=lang:^python$%20swapcase%20case:yes&type=cs
returns a mere 300 hits. At least half are definitions of the function,
or tests thereof, or inclusions in lists. Some actual uses:
1.http://pytof.googlecode.com/svn/trunk/pytof/utils.py
def ListCurrentDirFileFromExt(ext, path):
""" list file matching extension from a list
in the current directory
emulate a `ls *.{(',').join(ext)` with ext in both upper and
downcase}"""
import glob
extfiles = []
for e in ext:
extfiles.extend(glob.glob(join(path,'*' + e)))
extfiles.extend(glob.glob(join(path,'*' + e.swapcase())))
If e is all upper or lower, using e.upper() and e.lower() will do same.
If e is mixed, using .upper and .lower is required to fulfill the spec.
On *nix, where matching of letters is case sensitive, both will fail
with '.Jpg'. On Windows, where letter matching ignores case, the above
code will list everything twice.
2.http://ydict.googlecode.com/svn/trunk/ydict
k is random word from database.
result.replace(k, "####").replace(k.upper(),
"####").replace(k[0].swapcase()+k[1:].lower(),"####")
If k is lowercase, .lower() is redundant and
k[0].swapcase()+k[1:].lower() == k.title(). If k is uppercase, previous
.upper() is redundant. If k is mixed case, code may have problems.
3. http://migrid.googlecode.com/svn/trunk/mig/sftp-mount/migaccess.py
# This is how we could add stub extended attribute handlers...
# (We can't have ones which aptly delegate requests to the underlying fs
# because Python lacks a standard xattr interface.)
#
# def getxattr(self, path, name, size):
# val = name.swapcase() + '@' + path
# if size == 0:
# # We are asked for size of the value.
# return len(val)
# return val
This is not actually used. Passing a name with all cases swapped from
what they should be is a bit strange.
4.
elif char >= 'A' and char <= 'Z':
element = element + char.swapcase()
uppercasechar.swapcase() == uppercasechar.lower()
My perusal of the first 70 of 300 hits suggests that .swapcase is more
of an attractive nuisance or redundant rather than actually useful.
--
Terry Jan Reedy
More information about the Python-Dev
mailing list