Paul McGuire wrote:

"Frans Englich" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
#--------------------------------------------------------------
def detectMimeType( filename ):

extension = filename[-3:]

You might consider using os.path.splitext() here, instead of always assuming that the last three characters are the extension. That way you'll be consistent even with extensions like .c, .cc, .h, .gz, etc.


Note that os.path.splitext() does include the extension separator (the dot), so that you'll need to test against, e.g., ".php" and ".cpp".

Since the majority of your tests will be fairly direct 'extension "XYZ"
means mimetype "aaa/bbb"', this really sounds like a dictionary type
solution is called for.

I strongly agree with this. The vast majority of your cases seem to be a direct mapping of extension-string to mimetype-string; using a dictionary (i.e. mapping ;) ) for this is ideal. For those cases where you can't key off of an extension string (such as makefiles), you can do special-case processing if the dictionary lookup fails.



    if extension.endswith("cc"):
        return extToMimeDict["cpp"]

If the intent of this is to catch .cc files, it's easy to add an extra entry into the dict to map '.cc' to the same string as '.cpp'.


Jeff Shannon
Technician/Programmer
Credit International

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to