I'm working with a database that has typos in the values. To compensate for them I've made a function called
standardizer() which removes spaces and converts all letters to uppercase. I do this so the value red in from the database can correctly be interpreted by the program. Also in values starting with the prefix
'sp_etc_', I noticed a common mistake that the
't' is left out, giving
'sp_ec_' so I compensate for this as well. Bellow is an example:
import sys #remove all spaces and convert to uppercase, for comparision purposes def standardizer(str): str = str.replace("sp_ec_", "sp_etc_")#this is a common typo in the db, the 't' is left out str = str.replace(" ", "") str = str.upper() return str #this function is for testing purposes and would actually read in values from db def getUserInput(): return ' this is a test' def main(): str1 = "I'mok" str2 = 'this is a test' str3 = 'two spaces' str4 = ' spaces at front and end ' str5 = 'sp_ec_blah' print(str1, standardizer(str1)) print(str2, standardizer(str2)) print(str3, standardizer(str3)) print(str4, standardizer(str4)) print(str5, standardizer(str5)) #this is an example of how the function would actually be used print(standardizer(str2), standardizer(getUserInput())) if standardizer(str2) == standardizer(getUserInput()): print('matched') else: print('not a match') if __name__ == '__main__': main()
Any suggestions on the
standardizer() function? First off I think it needs a better name. I'm wondering if I should break it into two functions, one being for the missing 't' and the other being for removing spaces and making upper case (by the way, from what I've seen it's more common to convert everything to upper case than lower case, for comparison purposes). Also, how would you comment something like this?