nodes: 0, module; 1, function_definition; 2, function_name:natsort_keygen; 3, parameters; 4, block; 5, default_parameter; 6, default_parameter; 7, expression_statement; 8, try_statement; 9, comment:# Add the NS_DUMB option if the locale library is broken.; 10, if_statement; 11, comment:# Set some variables that will be passed to the factory functions; 12, if_statement; 13, expression_statement; 14, comment:# Create the functions that will be used to split strings.; 15, expression_statement; 16, expression_statement; 17, expression_statement; 18, comment:# Create the high-level parsing functions for strings, bytes, and numbers.; 19, expression_statement; 20, if_statement; 21, expression_statement; 22, expression_statement; 23, comment:# Return the natsort key with the parsing path pre-chosen.; 24, return_statement; 25, identifier:key; 26, None; 27, identifier:alg; 28, attribute; 29, comment:"""
      Generate a key to sort strings and numbers naturally.
  
      This key is designed for use as the `key` argument to
      functions such as the `sorted` builtin.
  
      The user may customize the generated function with the
      arguments to `natsort_keygen`, including an optional
      `key` function.
  
      Parameters
      ----------
      key : callable, optional
          A key used to manipulate the input value before parsing for
          numbers. It is **not** applied recursively.
          It should accept a single argument and return a single value.
  
      alg : ns enum, optional
          This option is used to control which algorithm `natsort`
          uses when sorting. For details into these options, please see
          the :class:`ns` class documentation. The default is `ns.INT`.
  
      Returns
      -------
      out : function
          A function that parses input for natural sorting that is
          suitable for passing as the `key` argument to functions
          such as `sorted`.
  
      See Also
      --------
      natsorted
      natsort_key
  
      Examples
      --------
      `natsort_keygen` is a convenient way to create a custom key
      to sort lists in-place (for example).::
  
          >>> a = ['num5.10', 'num-3', 'num5.3', 'num2']
          >>> a.sort(key=natsort_keygen(alg=ns.REAL))
          >>> a
          [{u}'num-3', {u}'num2', {u}'num5.10', {u}'num5.3']
  
      """; 30, block; 31, except_clause; 32, boolean_operator; 33, block; 34, binary_operator:alg & ns.NUMAFTER; 35, block; 36, else_clause; 37, assignment; 38, assignment; 39, assignment; 40, assignment; 41, assignment; 42, binary_operator:alg & ns.PATH; 43, block; 44, assignment; 45, assignment; 46, call; 47, identifier:ns; 48, identifier:DEFAULT; 49, expression_statement; 50, identifier:TypeError; 51, block; 52, binary_operator:alg & ns.LOCALEALPHA; 53, call; 54, expression_statement; 55, identifier:alg; 56, attribute; 57, if_statement; 58, expression_statement; 59, block; 60, identifier:regex; 61, call; 62, identifier:input_transform; 63, call; 64, identifier:component_transform; 65, call; 66, identifier:final_transform; 67, call; 68, identifier:string_func; 69, call; 70, identifier:alg; 71, attribute; 72, expression_statement; 73, identifier:bytes_func; 74, call; 75, identifier:num_func; 76, call; 77, identifier:partial; 78, argument_list; 79, binary_operator:ns.DEFAULT | alg; 80, expression_statement; 81, raise_statement; 82, identifier:alg; 83, attribute; 84, attribute; 85, argument_list; 86, augmented_assignment; 87, identifier:ns; 88, identifier:NUMAFTER; 89, binary_operator:alg & ns.LOCALEALPHA; 90, block; 91, else_clause; 92, assignment; 93, if_statement; 94, expression_statement; 95, attribute; 96, argument_list; 97, attribute; 98, argument_list; 99, attribute; 100, argument_list; 101, attribute; 102, argument_list; 103, attribute; 104, argument_list; 105, identifier:ns; 106, identifier:PATH; 107, assignment; 108, attribute; 109, argument_list; 110, attribute; 111, argument_list; 112, attribute; 113, keyword_argument; 114, keyword_argument; 115, keyword_argument; 116, keyword_argument; 117, attribute; 118, identifier:alg; 119, assignment; 120, call; 121, identifier:ns; 122, identifier:LOCALEALPHA; 123, attribute; 124, identifier:dumb_sort; 125, identifier:alg; 126, identifier:NS_DUMB; 127, identifier:alg; 128, attribute; 129, expression_statement; 130, block; 131, identifier:pre_sep; 132, attribute; 133, binary_operator:alg & ns.LOCALEALPHA; 134, block; 135, else_clause; 136, assignment; 137, identifier:utils; 138, identifier:regex_chooser; 139, identifier:alg; 140, identifier:utils; 141, identifier:input_string_transform_factory; 142, identifier:alg; 143, identifier:utils; 144, identifier:string_component_transform_factory; 145, identifier:alg; 146, identifier:utils; 147, identifier:final_data_transform_factory; 148, identifier:alg; 149, identifier:sep; 150, identifier:pre_sep; 151, identifier:utils; 152, identifier:parse_string_factory; 153, identifier:alg; 154, identifier:sep; 155, attribute; 156, identifier:input_transform; 157, identifier:component_transform; 158, identifier:final_transform; 159, identifier:string_func; 160, call; 161, identifier:utils; 162, identifier:parse_bytes_factory; 163, identifier:alg; 164, identifier:utils; 165, identifier:parse_number_factory; 166, identifier:alg; 167, identifier:sep; 168, identifier:pre_sep; 169, identifier:utils; 170, identifier:natsort_key; 171, identifier:key; 172, identifier:key; 173, identifier:string_func; 174, identifier:string_func; 175, identifier:bytes_func; 176, identifier:bytes_func; 177, identifier:num_func; 178, identifier:num_func; 179, identifier:ns; 180, identifier:DEFAULT; 181, identifier:msg; 182, string:"natsort_keygen: 'alg' argument must be from the enum 'ns'"; 183, identifier:ValueError; 184, argument_list; 185, attribute; 186, identifier:locale; 187, identifier:ns; 188, identifier:LOCALEALPHA; 189, assignment; 190, expression_statement; 191, attribute; 192, identifier:null_string_max; 193, identifier:alg; 194, attribute; 195, expression_statement; 196, block; 197, identifier:pre_sep; 198, attribute; 199, identifier:regex; 200, identifier:split; 201, attribute; 202, argument_list; 203, binary_operator:msg + ", got {}".format(py23_str(alg)); 204, identifier:natsort; 205, identifier:compat; 206, identifier:sep; 207, attribute; 208, assignment; 209, attribute; 210, identifier:locale; 211, identifier:ns; 212, identifier:LOCALEALPHA; 213, assignment; 214, expression_statement; 215, attribute; 216, identifier:null_string; 217, identifier:utils; 218, identifier:parse_path_factory; 219, identifier:string_func; 220, identifier:msg; 221, call; 222, attribute; 223, identifier:null_string_locale_max; 224, identifier:sep; 225, attribute; 226, identifier:natsort; 227, identifier:compat; 228, identifier:sep; 229, attribute; 230, assignment; 231, attribute; 232, identifier:locale; 233, attribute; 234, argument_list; 235, attribute; 236, identifier:locale; 237, attribute; 238, identifier:null_string_max; 239, attribute; 240, identifier:null_string_locale; 241, identifier:sep; 242, attribute; 243, identifier:natsort; 244, identifier:compat; 245, string:", got {}"; 246, identifier:format; 247, call; 248, identifier:natsort; 249, identifier:compat; 250, attribute; 251, identifier:locale; 252, attribute; 253, identifier:locale; 254, attribute; 255, identifier:null_string; 256, identifier:py23_str; 257, argument_list; 258, identifier:natsort; 259, identifier:compat; 260, identifier:natsort; 261, identifier:compat; 262, attribute; 263, identifier:locale; 264, identifier:alg; 265, identifier:natsort; 266, identifier:compat
code: def natsort_keygen(key=None, alg=ns.DEFAULT):
      """
      Generate a key to sort strings and numbers naturally.
  
      This key is designed for use as the `key` argument to
      functions such as the `sorted` builtin.
  
      The user may customize the generated function with the
      arguments to `natsort_keygen`, including an optional
      `key` function.
  
      Parameters
      ----------
      key : callable, optional
          A key used to manipulate the input value before parsing for
          numbers. It is **not** applied recursively.
          It should accept a single argument and return a single value.
  
      alg : ns enum, optional
          This option is used to control which algorithm `natsort`
          uses when sorting. For details into these options, please see
          the :class:`ns` class documentation. The default is `ns.INT`.
  
      Returns
      -------
      out : function
          A function that parses input for natural sorting that is
          suitable for passing as the `key` argument to functions
          such as `sorted`.
  
      See Also
      --------
      natsorted
      natsort_key
  
      Examples
      --------
      `natsort_keygen` is a convenient way to create a custom key
      to sort lists in-place (for example).::
  
          >>> a = ['num5.10', 'num-3', 'num5.3', 'num2']
          >>> a.sort(key=natsort_keygen(alg=ns.REAL))
          >>> a
          [{u}'num-3', {u}'num2', {u}'num5.10', {u}'num5.3']
  
      """
      try:
          ns.DEFAULT | alg
      except TypeError:
          msg = "natsort_keygen: 'alg' argument must be from the enum 'ns'"
          raise ValueError(msg + ", got {}".format(py23_str(alg)))
  
      # Add the NS_DUMB option if the locale library is broken.
      if alg & ns.LOCALEALPHA and natsort.compat.locale.dumb_sort():
          alg |= NS_DUMB
  
      # Set some variables that will be passed to the factory functions
      if alg & ns.NUMAFTER:
          if alg & ns.LOCALEALPHA:
              sep = natsort.compat.locale.null_string_locale_max
          else:
              sep = natsort.compat.locale.null_string_max
          pre_sep = natsort.compat.locale.null_string_max
      else:
          if alg & ns.LOCALEALPHA:
              sep = natsort.compat.locale.null_string_locale
          else:
              sep = natsort.compat.locale.null_string
          pre_sep = natsort.compat.locale.null_string
      regex = utils.regex_chooser(alg)
  
      # Create the functions that will be used to split strings.
      input_transform = utils.input_string_transform_factory(alg)
      component_transform = utils.string_component_transform_factory(alg)
      final_transform = utils.final_data_transform_factory(alg, sep, pre_sep)
  
      # Create the high-level parsing functions for strings, bytes, and numbers.
      string_func = utils.parse_string_factory(
          alg, sep, regex.split, input_transform, component_transform, final_transform
      )
      if alg & ns.PATH:
          string_func = utils.parse_path_factory(string_func)
      bytes_func = utils.parse_bytes_factory(alg)
      num_func = utils.parse_number_factory(alg, sep, pre_sep)
  
      # Return the natsort key with the parsing path pre-chosen.
      return partial(
          utils.natsort_key,
          key=key,
          string_func=string_func,
          bytes_func=bytes_func,
          num_func=num_func,
      )