Package: datatype-tools
Data Type ToolsIncludes useful helper methods for Python's immutable data types using theforbiddenfruitlibrary.Installpython3 -m pip install datatype-toolsUsageImport single datatypefrom datatype_tools.lib import FloatImport all datatypesfrom datatype_tools.lib import *Dict Toolssort_by_valSorts dictionary by values, expects dictionary with a depth of 1>>>obj={'a':3,'b':1,'c':2}>>>obj.sort_by_val(sort_type='int'){'b':1,'c':2,'a':3}Argumentsself: dictionaryasc: boolean (True)Ascending order when True, Descending when Falsesort_type: string ('string')Data Type for sort, includes 'string', 'int', 'float', and 'date' typesDate Type is a string representing a dateData does not need to match type i.e. a string with an int value '1'Returns original data typedate_input: string ('mdy')Date input formati.e. 'ymd', 'mdy', etc.Returns sorted dictsort_by_keySorts dictionary by keys, expects dictionary with a depth of 1>>>obj={'c':1,'a':3,'b':2}>>>obj.sort_by_key(sort_type='string'){'a':3,'b':2,'c':1}Argumentsself: dictionaryasc: boolean (True)Ascending order when True, Descending when Falsesort_type: string ('string')Data Type for sort, includes 'string', 'int', 'float', and 'date' typesDate Type is a string representing a dateData does not need to match type i.e. a string with an int value '1'Returns original data typedate_input: string ('mdy')Date input formati.e. 'ymd', 'mdy', etc.Returns sorted dictnested_sortSorts dictionary by nested key value>>>obj={'a':{'b':{'c':2}},'b':{'b':{'c':1}}}>>>obj.nested_sort(nested_args=[{'keys':['b','c'],'sort_type':'int'}]){'b':{'b':{'c':1}},'a':{'b':{'c':2}}}Argumentsself: dictionaryasc: boolean (True)Ascending order when True, Descending when Falsenested_args: listList of sort argument dicts, can include multiple to sort by multiple nested keys respectivelyArgskeys: listlist of nested keys respectively i.e. ['b', 'c', 'd'] will sort by key ['b']['c']['d']sort_type: string ('string')Data Type for sort, includes 'string', 'int', 'float', and 'date' typesDate Type is a string representing a dateData does not need to match type i.e. a string with an int value '1'Returns original data typedate_input: string ('mdy')Date input formati.e. 'ymd', 'mdy', etc.Returns sorted dictList Toolssort_by_valSorts list of dictionaries by values, expects dictionaries with a depth of 1>>>li=[{'a':3},{'b':1},{'c':2}]>>>li.sort_by_val(sort_type='int')[{'b':1},{'c':2},{'a':3}]Argumentsself: listasc: boolean (True)Ascending order when True, Descending when Falsesort_type: string ('string')Data Type for sort, includes 'string', 'int', 'float', and 'date' typesDate Type is a string representing a dateData does not need to match type i.e. a string with an int value '1'Returns original data typedate_input: string ('mdy')Date input formati.e. 'ymd', 'mdy', etc.Returns sorted listsort_by_keySorts list of dictionaries by keys, expects dictionaries with a depth of 1>>>li=[{'c':1},{'a':3},{'b':2}]>>>li.sort_by_key(sort_type='string')[{'a':3},{'b':2},{'c':1}]Argumentsself: listasc: boolean (True)Ascending order when True, Descending when Falsesort_type: string ('string')Data Type for sort, includes 'string', 'int', 'float', and 'date' typesDate Type is a string representing a dateData does not need to match type i.e. a string with an int value '1'Returns original data typedate_input: string ('mdy')Date input formati.e. 'ymd', 'mdy', etc.Returns sorted listsort_by_key_valSorts list of dictionaries by specific key value, expects dictionaries with a depth of 1>>>li=[{'a':1,'b':2},{'a':2,'b':1}]>>>li.sort_by_key_val(key='b',sort_type='string')[{'a':2,'b':1},{'a':1,'b':2}]Argumentsself: listasc: boolean (True)Ascending order when True, Descending when Falsesort_type: string ('string')Data Type for sort, includes 'string', 'int', 'float', and 'date' typesDate Type is a string representing a dateData does not need to match type i.e. a string with an int value '1'Returns original data typedate_input: string ('mdy')Date input formati.e. 'ymd', 'mdy', etc.Returns sorted listnested_sortSorts list of dictionaries by nested key value>>>li=[{'a':{'b':{'c':2}}},{'b':{'b':{'c':1}}}]>>>li.nested_sort(nested_args=[{'keys':['b','c'],'sort_type':'int'}])[{'b':{'b':{'c':1}}},{'a':{'b':{'c':2}}}]Argumentsself: listasc: boolean (True)Ascending order when True, Descending when Falsenested_args: listList of sort argument dicts, can include multiple to sort by multiple nested keys respectivelyArgskeys: listlist of nested keys respectively i.e. ['b', 'c', 'd'] will sort by key ['b']['c']['d']sort_type: string ('string')Data Type for sort, includes 'string', 'int', 'float', and 'date' typesDate Type is a string representing a dateData does not need to match type i.e. a string with an int value '1'Returns original data typedate_input: string ('mdy')Date input formati.e. 'ymd', 'mdy', etc.Returns sorted listString Toolsreplace_multipleReplace multiple substrings or characters in a string with their respective replacements>>>'The Apple is Red'.replace_multiple({'Apple':'Grass','Red':'Green'})'The Grass is Green'Argumentsself: stringdictionary: dictdict keys are the values to replace, and key values will be their respective replacements in the stringReturns string with replacementsformat_dateFormats string representing date in multiple formats>>>'1/01/75'.format_date(date_input='mdy',date_format='mmddyyyy',delimiter='-')'01-01-1975'Argumentsself: stringdate_input: string ('mdy')date input order, i.e. 'mdy' for month, day, year, and 'ymd' for year, month, daydate_formatstring ('mmddyy')date output format, the number of each letter correspond to date digits, i.e. a date with 'mmddyyyy' will have 4 digits for year while a date with 'mmddyy' will only have 2 digits for yeardelimiter: string ('')delimiter for date output, i.e. '/' yields 01/01/2019Returns formatted date stringfind_nthFind nth occurence of substring in string>>>'apple picking'.find_nth('p',3)11Argumentsself: stringstring: stringsubstring to search in stringn: int1-based occurence number of substring to find in string.Returns int indexb64_encBase64 encode string>>>'plain_text'.b64_enc()'cGxhaW5fdGV4dA=='Argumentsself: stringReturns stringb64_decBase64 decode string>>>'cGxhaW5fdGV4dA=='.b64_dec()'plain_text'Argumentsself: stringReturns stringget_iso_dateGet ISO Date from string>>>'2019-05-27T19:09:04.285211Z'.get_iso_date(){'date':'05/27/2019','time':'19:09:04','micros':'285211','date_and_time':'05/27/2019-19:09:04','full_date':'05/27/2019-19:09:04.285211'}Argumentsself: stringiso_format: string, ISO datetime format for strftimedate_format: string, date output formatdelimiter: string, date output delimiterReturns dictFloat ToolsroundRound value to n decimal places (scientific rounding, fixes python rounding errors)>>>4.055.round(2)4.06>>>4.054(2)4.05Argumentsself: floatplaces: int (2)Number of places to round toReturns rounded float