nodes: 0, module; 1, function_definition; 2, function_name:default_sort_key; 3, parameters; 4, block; 5, identifier:item; 6, default_parameter; 7, expression_statement; 8, import_from_statement; 9, import_from_statement; 10, import_from_statement; 11, if_statement; 12, if_statement; 13, return_statement; 14, identifier:order; 15, None; 16, comment:"""Return a key that can be used for sorting.
  
      The key has the structure:
  
      (class_key, (len(args), args), exponent.sort_key(), coefficient)
  
      This key is supplied by the sort_key routine of Basic objects when
      ``item`` is a Basic object or an object (other than a string) that
      sympifies to a Basic object. Otherwise, this function produces the
      key.
  
      The ``order`` argument is passed along to the sort_key routine and is
      used to determine how the terms *within* an expression are ordered.
      (See examples below) ``order`` options are: 'lex', 'grlex', 'grevlex',
      and reversed values of the same (e.g. 'rev-lex'). The default order
      value is None (which translates to 'lex').
  
      Examples
      ========
  
      >>> from sympy import S, I, default_sort_key
      >>> from sympy.core.function import UndefinedFunction
      >>> from sympy.abc import x
  
      The following are equivalent ways of getting the key for an object:
  
      >>> x.sort_key() == default_sort_key(x)
      True
  
      Here are some examples of the key that is produced:
  
      >>> default_sort_key(UndefinedFunction('f'))
      ((0, 0, 'UndefinedFunction'), (1, ('f',)), ((1, 0, 'Number'),
          (0, ()), (), 1), 1)
      >>> default_sort_key('1')
      ((0, 0, 'str'), (1, ('1',)), ((1, 0, 'Number'), (0, ()), (), 1), 1)
      >>> default_sort_key(S.One)
      ((1, 0, 'Number'), (0, ()), (), 1)
      >>> default_sort_key(2)
      ((1, 0, 'Number'), (0, ()), (), 2)
  
  
      While sort_key is a method only defined for SymPy objects,
      default_sort_key will accept anything as an argument so it is
      more robust as a sorting key. For the following, using key=
      lambda i: i.sort_key() would fail because 2 doesn't have a sort_key
      method; that's why default_sort_key is used. Note, that it also
      handles sympification of non-string items likes ints:
  
      >>> a = [2, I, -I]
      >>> sorted(a, key=default_sort_key)
      [2, -I, I]
  
      The returned key can be used anywhere that a key can be specified for
      a function, e.g. sort, min, max, etc...:
  
      >>> a.sort(key=default_sort_key); a[0]
      2
      >>> min(a, key=default_sort_key)
      2
  
      Note
      ----
  
      The key returned is useful for getting items into a canonical order
      that will be the same across platforms. It is not directly useful for
      sorting lists of expressions:
  
      >>> a, b = x, 1/x
  
      Since ``a`` has only 1 term, its value of sort_key is unaffected by
      ``order``:
  
      >>> a.sort_key() == a.sort_key('rev-lex')
      True
  
      If ``a`` and ``b`` are combined then the key will differ because there
      are terms that can be ordered:
  
      >>> eq = a + b
      >>> eq.sort_key() == eq.sort_key('rev-lex')
      False
      >>> eq.as_ordered_terms()
      [x, 1/x]
      >>> eq.as_ordered_terms('rev-lex')
      [1/x, x]
  
      But since the keys for each of these terms are independent of ``order``'s
      value, they don't sort differently when they appear separately in a list:
  
      >>> sorted(eq.args, key=default_sort_key)
      [1/x, x]
      >>> sorted(eq.args, key=lambda i: default_sort_key(i, order='rev-lex'))
      [1/x, x]
  
      The order of terms obtained when using these keys is the order that would
      be obtained if those terms were *factors* in a product.
  
      See Also
      ========
  
      sympy.core.expr.as_ordered_factors, sympy.core.expr.as_ordered_terms
  
      """; 17, dotted_name; 18, dotted_name; 19, dotted_name; 20, dotted_name; 21, dotted_name; 22, dotted_name; 23, dotted_name; 24, dotted_name; 25, call; 26, block; 27, call; 28, block; 29, else_clause; 30, expression_list; 31, identifier:sympy; 32, identifier:core; 33, identifier:S; 34, identifier:Basic; 35, identifier:sympy; 36, identifier:core; 37, identifier:sympify; 38, identifier:sympify; 39, identifier:SympifyError; 40, identifier:sympy; 41, identifier:core; 42, identifier:compatibility; 43, identifier:iterable; 44, identifier:isinstance; 45, argument_list; 46, return_statement; 47, identifier:iterable; 48, argument_list; 49, if_statement; 50, expression_statement; 51, if_statement; 52, expression_statement; 53, block; 54, tuple; 55, identifier:args; 56, call; 57, attribute; 58, identifier:item; 59, identifier:Basic; 60, call; 61, identifier:item; 62, keyword_argument; 63, call; 64, block; 65, elif_clause; 66, else_clause; 67, assignment; 68, identifier:unordered; 69, comment:# e.g. dict, set; 70, block; 71, assignment; 72, if_statement; 73, comment:# e.g. str; 74, expression_statement; 75, identifier:cls_index; 76, integer:0; 77, attribute; 78, attribute; 79, argument_list; 80, identifier:S; 81, identifier:One; 82, attribute; 83, argument_list; 84, identifier:exclude; 85, identifier:string_types; 86, identifier:isinstance; 87, argument_list; 88, expression_statement; 89, expression_statement; 90, call; 91, block; 92, comment:# e.g. tuple, list; 93, block; 94, identifier:args; 95, list_comprehension; 96, expression_statement; 97, pattern_list; 98, expression_list; 99, not_operator; 100, block; 101, assignment; 102, attribute; 103, identifier:__name__; 104, attribute; 105, identifier:sort_key; 106, identifier:item; 107, identifier:sort_key; 108, keyword_argument; 109, identifier:item; 110, identifier:dict; 111, assignment; 112, assignment; 113, identifier:isinstance; 114, argument_list; 115, expression_statement; 116, expression_statement; 117, expression_statement; 118, expression_statement; 119, call; 120, for_in_clause; 121, assignment; 122, identifier:cls_index; 123, identifier:args; 124, integer:10; 125, tuple; 126, call; 127, try_statement; 128, pattern_list; 129, expression_list; 130, identifier:item; 131, identifier:__class__; 132, identifier:S; 133, identifier:One; 134, identifier:order; 135, identifier:order; 136, identifier:args; 137, call; 138, identifier:unordered; 139, True; 140, identifier:item; 141, identifier:set; 142, assignment; 143, assignment; 144, assignment; 145, assignment; 146, identifier:default_sort_key; 147, argument_list; 148, identifier:arg; 149, identifier:args; 150, identifier:args; 151, call; 152, call; 153, call; 154, identifier:isinstance; 155, argument_list; 156, block; 157, except_clause; 158, else_clause; 159, identifier:cls_index; 160, identifier:args; 161, integer:0; 162, tuple; 163, attribute; 164, argument_list; 165, identifier:args; 166, identifier:item; 167, identifier:unordered; 168, True; 169, identifier:args; 170, call; 171, identifier:unordered; 172, False; 173, identifier:arg; 174, keyword_argument; 175, identifier:sorted; 176, argument_list; 177, identifier:len; 178, argument_list; 179, identifier:tuple; 180, argument_list; 181, identifier:item; 182, identifier:string_types; 183, expression_statement; 184, identifier:SympifyError; 185, comment:# e.g. lambda x: x; 186, block; 187, block; 188, integer:1; 189, tuple; 190, identifier:item; 191, identifier:items; 192, identifier:list; 193, argument_list; 194, identifier:order; 195, identifier:order; 196, identifier:args; 197, identifier:args; 198, identifier:args; 199, assignment; 200, pass_statement; 201, if_statement; 202, comment:# e.g. UndefinedFunction; 203, call; 204, identifier:item; 205, identifier:item; 206, call; 207, call; 208, comment:# e.g int -> Integer; 209, block; 210, identifier:str; 211, argument_list; 212, identifier:sympify; 213, argument_list; 214, identifier:isinstance; 215, argument_list; 216, return_statement; 217, identifier:item; 218, identifier:item; 219, identifier:item; 220, identifier:Basic; 221, call; 222, identifier:default_sort_key; 223, argument_list; 224, identifier:item
code: def default_sort_key(item, order=None):
      """Return a key that can be used for sorting.
  
      The key has the structure:
  
      (class_key, (len(args), args), exponent.sort_key(), coefficient)
  
      This key is supplied by the sort_key routine of Basic objects when
      ``item`` is a Basic object or an object (other than a string) that
      sympifies to a Basic object. Otherwise, this function produces the
      key.
  
      The ``order`` argument is passed along to the sort_key routine and is
      used to determine how the terms *within* an expression are ordered.
      (See examples below) ``order`` options are: 'lex', 'grlex', 'grevlex',
      and reversed values of the same (e.g. 'rev-lex'). The default order
      value is None (which translates to 'lex').
  
      Examples
      ========
  
      >>> from sympy import S, I, default_sort_key
      >>> from sympy.core.function import UndefinedFunction
      >>> from sympy.abc import x
  
      The following are equivalent ways of getting the key for an object:
  
      >>> x.sort_key() == default_sort_key(x)
      True
  
      Here are some examples of the key that is produced:
  
      >>> default_sort_key(UndefinedFunction('f'))
      ((0, 0, 'UndefinedFunction'), (1, ('f',)), ((1, 0, 'Number'),
          (0, ()), (), 1), 1)
      >>> default_sort_key('1')
      ((0, 0, 'str'), (1, ('1',)), ((1, 0, 'Number'), (0, ()), (), 1), 1)
      >>> default_sort_key(S.One)
      ((1, 0, 'Number'), (0, ()), (), 1)
      >>> default_sort_key(2)
      ((1, 0, 'Number'), (0, ()), (), 2)
  
  
      While sort_key is a method only defined for SymPy objects,
      default_sort_key will accept anything as an argument so it is
      more robust as a sorting key. For the following, using key=
      lambda i: i.sort_key() would fail because 2 doesn't have a sort_key
      method; that's why default_sort_key is used. Note, that it also
      handles sympification of non-string items likes ints:
  
      >>> a = [2, I, -I]
      >>> sorted(a, key=default_sort_key)
      [2, -I, I]
  
      The returned key can be used anywhere that a key can be specified for
      a function, e.g. sort, min, max, etc...:
  
      >>> a.sort(key=default_sort_key); a[0]
      2
      >>> min(a, key=default_sort_key)
      2
  
      Note
      ----
  
      The key returned is useful for getting items into a canonical order
      that will be the same across platforms. It is not directly useful for
      sorting lists of expressions:
  
      >>> a, b = x, 1/x
  
      Since ``a`` has only 1 term, its value of sort_key is unaffected by
      ``order``:
  
      >>> a.sort_key() == a.sort_key('rev-lex')
      True
  
      If ``a`` and ``b`` are combined then the key will differ because there
      are terms that can be ordered:
  
      >>> eq = a + b
      >>> eq.sort_key() == eq.sort_key('rev-lex')
      False
      >>> eq.as_ordered_terms()
      [x, 1/x]
      >>> eq.as_ordered_terms('rev-lex')
      [1/x, x]
  
      But since the keys for each of these terms are independent of ``order``'s
      value, they don't sort differently when they appear separately in a list:
  
      >>> sorted(eq.args, key=default_sort_key)
      [1/x, x]
      >>> sorted(eq.args, key=lambda i: default_sort_key(i, order='rev-lex'))
      [1/x, x]
  
      The order of terms obtained when using these keys is the order that would
      be obtained if those terms were *factors* in a product.
  
      See Also
      ========
  
      sympy.core.expr.as_ordered_factors, sympy.core.expr.as_ordered_terms
  
      """
  
      from sympy.core import S, Basic
      from sympy.core.sympify import sympify, SympifyError
      from sympy.core.compatibility import iterable
  
      if isinstance(item, Basic):
          return item.sort_key(order=order)
  
      if iterable(item, exclude=string_types):
          if isinstance(item, dict):
              args = item.items()
              unordered = True
          elif isinstance(item, set):
              args = item
              unordered = True
          else:
              # e.g. tuple, list
              args = list(item)
              unordered = False
  
          args = [default_sort_key(arg, order=order) for arg in args]
  
          if unordered:
              # e.g. dict, set
              args = sorted(args)
  
          cls_index, args = 10, (len(args), tuple(args))
      else:
          if not isinstance(item, string_types):
              try:
                  item = sympify(item)
              except SympifyError:
                  # e.g. lambda x: x
                  pass
              else:
                  if isinstance(item, Basic):
                      # e.g int -> Integer
                      return default_sort_key(item)
                  # e.g. UndefinedFunction
  
          # e.g. str
          cls_index, args = 0, (1, (str(item),))
  
      return (cls_index, 0, item.__class__.__name__
              ), args, S.One.sort_key(), S.One