#!/usr/bin/python from string import maketrans import sys, getopt, os, re def normalize(item): # The normalization actions listed below should be applied in the order listed # remove undesired punctuation (need to decide which and whether to substitute with '_' or similar) # remove extraneous white space, or, after the above, # replace strings of more than one '_' with exactly one '_' # Add your code between here normalized_item = item.lower().replace('&', 'and') intab = '();/\ ' outtab = "[],::_" trantab = maketrans(intab, outtab) normalized_item = normalized_item.translate(trantab, '"!?\'') normalized_item = re.sub('_+', '_', normalized_item) # normalized_item = re.sub('_+', '_', normalized_item) # and here return normalized_item def normalize_all(a, e, s): print "\n\nartist: ", a, "\nevent: ", e, "\nsong: ", s na = normalize(a) # Now check for leading 'the ' and remove it, if found (just do this for artist name) ne = normalize(e) ns = normalize(s) return os.path.join(na, ne, ns) def help(): print sys.argv[0], " filename" print sys.argv[0], " -a 'artist name' -e 'event or album name' -s 'song name'" print " if any of the above are omitted, 'unknown' is assumed," print " if all are omitted, internal test data are used" sys.exit(2) def main(argv): try: artist = 'unknown' event = 'unknown' song = 'unknown' opts, args = getopt.getopt(argv,"a:e:hs:") except getopt.GetoptError: help() for opt, arg in opts: if opt == '-a': artist = arg elif opt == '-e': event = arg elif opt == '-s': song = arg elif opt == '-h': help() else: help() if (artist == 'unknown' and event == 'unknown' and song == 'unknown'): path = normalize_all('The Rolling Stones', 'The Rolling Stones, Now!', '(I Can\'t Get No) Satisfaction') print "path: ", path, "\ndesire: rolling_stones/the_rolling_stones,_now/[i_cant_get_no]_satisfaction" path = normalize_all('The Rolling Stones', 'Through The Past, Darkly (Big Hits Vol. 2)', 'Jumpin\' Jack Flash') print "path: ", path, "\ndesire: rolling_stones/through_the_past,_darkly_[big_hits_vol._2]/jumpin_jack_flash" path = normalize_all('Mott The Hoople', 'All The Young Dudes [Bonus Tracks]', 'All The Young Dudes [Bowie/Hunter vocal version]') print "path: ", path, "\ndesire: mott_the_hoople/all_the_young_dudes_[bonus_tracks]/all_the_young_dudes_[bowie:hunter_vocal_version]" path = normalize_all('Motley Crue', 'Dr. Feelgood', 'Autographs & Apologies') print "path: ", path, "\ndesire: motley_crue/dr._feelgood/autographs_and_apologies" # Need to figure out how to declare (and pass) unicode (cut-n-paste Motley Crue from catalog file) path = normalize_all('Silly Testin\' with many "embedded" spaces', ' and leading spaces', 'And more stuff!!') print "path: ", path, '\ndesire: silly_testin_with_many_embedded_spaces/and_leading_spaces/and_more_stuff' else: path = normalize_all(artist, event, song) print "path: ", path, "\ndesire: you decide if this is right!" if __name__ == "__main__": main(sys.argv[1:])
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question