nodes: 0, module; 1, function_definition; 2, function_name:sort_dict; 3, parameters; 4, type; 5, block; 6, typed_parameter; 7, typed_default_parameter; 8, typed_default_parameter; 9, attribute; 10, expression_statement; 11, if_statement; 12, return_statement; 13, identifier:d; 14, type; 15, identifier:by; 16, type; 17, string; 18, identifier:allow_duplicates; 19, type; 20, True; 21, identifier:collections; 22, identifier:OrderedDict; 23, comment:"""
      Sort a dictionary by key or value.
  
      The function relies on
      https://docs.python.org/3/library/collections.html#collections.OrderedDict .
      The dulicated are determined based on
      https://stackoverflow.com/questions/9835762/find-and-list-duplicates-in-a-list .
      Parameters
      ----------
      d : dict
          Input dictionary
      by : ['key','value'], optional
          By what to sort the input dictionary
      allow_duplicates : bool, optional
          Flag to indicate if the duplicates are allowed.
      Returns
      -------
      collections.OrderedDict
          Sorted dictionary.
  
      >>> sort_dict({2: 3, 1: 2, 3: 1})
      OrderedDict([(1, 2), (2, 3), (3, 1)])
      >>> sort_dict({2: 3, 1: 2, 3: 1}, by='value')
      OrderedDict([(3, 1), (1, 2), (2, 3)])
      >>> sort_dict({'2': 3, '1': 2}, by='value')
      OrderedDict([('1', 2), ('2', 3)])
      >>> sort_dict({2: 1, 1: 2, 3: 1}, by='value', allow_duplicates=False)
      Traceback (most recent call last):
          ...
      ValueError: There are duplicates in the values: {1}
      >>> sort_dict({1:1,2:3},by=True)
      Traceback (most recent call last):
          ...
      ValueError: by can be 'key' or 'value'.
      """; 24, comparison_operator:by == 'key'; 25, block; 26, elif_clause; 27, else_clause; 28, call; 29, identifier:dict; 30, identifier:str; 31, string_content:key; 32, identifier:bool; 33, identifier:by; 34, string; 35, expression_statement; 36, comparison_operator:by == 'value'; 37, block; 38, block; 39, attribute; 40, argument_list; 41, string_content:key; 42, assignment; 43, identifier:by; 44, string; 45, expression_statement; 46, if_statement; 47, expression_statement; 48, raise_statement; 49, identifier:collections; 50, identifier:OrderedDict; 51, call; 52, identifier:i; 53, integer:0; 54, string_content:value; 55, assignment; 56, boolean_operator; 57, block; 58, assignment; 59, call; 60, identifier:sorted; 61, argument_list; 62, identifier:values; 63, call; 64, comparison_operator:len(values) != len(set(values)); 65, not_operator; 66, expression_statement; 67, raise_statement; 68, identifier:i; 69, integer:1; 70, identifier:ValueError; 71, argument_list; 72, call; 73, keyword_argument; 74, identifier:list; 75, argument_list; 76, call; 77, call; 78, identifier:allow_duplicates; 79, assignment; 80, call; 81, string:"by can be 'key' or 'value'."; 82, attribute; 83, argument_list; 84, identifier:key; 85, lambda; 86, call; 87, identifier:len; 88, argument_list; 89, identifier:len; 90, argument_list; 91, identifier:duplicates; 92, call; 93, identifier:ValueError; 94, argument_list; 95, identifier:d; 96, identifier:items; 97, lambda_parameters; 98, subscript; 99, attribute; 100, argument_list; 101, identifier:values; 102, call; 103, identifier:find_duplicates; 104, argument_list; 105, call; 106, identifier:t; 107, identifier:t; 108, identifier:i; 109, identifier:d; 110, identifier:values; 111, identifier:set; 112, argument_list; 113, identifier:values; 114, attribute; 115, argument_list; 116, identifier:values; 117, string:"There are duplicates in the values: {}"; 118, identifier:format; 119, identifier:duplicates
code: def sort_dict(d: dict, by: str = 'key',
                allow_duplicates: bool = True) -> collections.OrderedDict:
      """
      Sort a dictionary by key or value.
  
      The function relies on
      https://docs.python.org/3/library/collections.html#collections.OrderedDict .
      The dulicated are determined based on
      https://stackoverflow.com/questions/9835762/find-and-list-duplicates-in-a-list .
      Parameters
      ----------
      d : dict
          Input dictionary
      by : ['key','value'], optional
          By what to sort the input dictionary
      allow_duplicates : bool, optional
          Flag to indicate if the duplicates are allowed.
      Returns
      -------
      collections.OrderedDict
          Sorted dictionary.
  
      >>> sort_dict({2: 3, 1: 2, 3: 1})
      OrderedDict([(1, 2), (2, 3), (3, 1)])
      >>> sort_dict({2: 3, 1: 2, 3: 1}, by='value')
      OrderedDict([(3, 1), (1, 2), (2, 3)])
      >>> sort_dict({'2': 3, '1': 2}, by='value')
      OrderedDict([('1', 2), ('2', 3)])
      >>> sort_dict({2: 1, 1: 2, 3: 1}, by='value', allow_duplicates=False)
      Traceback (most recent call last):
          ...
      ValueError: There are duplicates in the values: {1}
      >>> sort_dict({1:1,2:3},by=True)
      Traceback (most recent call last):
          ...
      ValueError: by can be 'key' or 'value'.
      """
      if by == 'key':
          i = 0
      elif by == 'value':
          values = list(d.values())
          if len(values) != len(set(values)) and not allow_duplicates:
              duplicates = find_duplicates(values)
              raise ValueError("There are duplicates in the values: {}".format(duplicates))
          i = 1
      else:
          raise ValueError("by can be 'key' or 'value'.")
  
      return collections.OrderedDict(sorted(d.items(), key=lambda t: t[i]))