Posts

Showing posts from July, 2017

Validate SQL formatted date

https://stackoverflow.com/questions/16870663/how-do-i-validate-a-date-string-format-in-python def is_sql_date(str_val):     date_text = str_val.strip()     try:         datetime.datetime.strptime(date_text, '%Y-%m-%d')         return date_text     #except ValueError:     #    raise ValueError("Incorrect data format, should be YYYY-MM-DD")     except:         return None ### test date validation test_date = is_sql_date('') test_date2 = is_sql_date('2017') test_date3 = is_sql_date('2017-07-24')

De-dupe and sort list at the same time

myList = ['b','c','a','a'] myList = sorted ( set ( myList )) Credits: https://stackoverflow.com/questions/479897/how-to-remove-duplicates-from-python-list-and-keep-order