nodes: 0, module; 1, function_definition; 2, function_name:slice; 3, parameters; 4, block; 5, identifier:self; 6, list_splat_pattern; 7, dictionary_splat_pattern; 8, expression_statement; 9, comment:# handle `sorted_by` kwarg with a default value in a python2-compatible way; 10, expression_statement; 11, if_statement; 12, comment:# follow Python's slice syntax; 13, if_statement; 14, if_statement; 15, return_statement; 16, identifier:slice_args; 17, identifier:kwargs; 18, comment:"""Create a new SampleSet with rows sliced according to standard Python
          slicing syntax.
  
          Args:
              start (int, optional, default=None):
                  Start index for `slice`.
  
              stop (int):
                  Stop index for `slice`.
  
              step (int, optional, default=None):
                  Step value for `slice`.
  
              sorted_by (str/None, optional, default='energy'):
                  Selects the record field used to sort the samples before
                  slicing. Note that `sorted_by` determines the sample order in
                  the returned SampleSet.
  
          Returns:
              :obj:`.SampleSet`
  
          Examples:
  
              >>> import numpy as np
              ...
              >>> sampleset = dimod.SampleSet.from_samples(np.diag(range(1, 11)), dimod.BINARY, energy=range(10))
              >>> print(sampleset)
                 0  1  2  3  4  5  6  7  8  9 energy num_oc.
              0  1  0  0  0  0  0  0  0  0  0      0       1
              1  0  1  0  0  0  0  0  0  0  0      1       1
              2  0  0  1  0  0  0  0  0  0  0      2       1
              3  0  0  0  1  0  0  0  0  0  0      3       1
              4  0  0  0  0  1  0  0  0  0  0      4       1
              5  0  0  0  0  0  1  0  0  0  0      5       1
              6  0  0  0  0  0  0  1  0  0  0      6       1
              7  0  0  0  0  0  0  0  1  0  0      7       1
              8  0  0  0  0  0  0  0  0  1  0      8       1
              9  0  0  0  0  0  0  0  0  0  1      9       1
              ['BINARY', 10 rows, 10 samples, 10 variables]
  
              >>> # the first 3 samples by energy == truncate(3)
              >>> print(sampleset.slice(3))
                 0  1  2  3  4  5  6  7  8  9 energy num_oc.
              0  1  0  0  0  0  0  0  0  0  0      0       1
              1  0  1  0  0  0  0  0  0  0  0      1       1
              2  0  0  1  0  0  0  0  0  0  0      2       1
              ['BINARY', 3 rows, 3 samples, 10 variables]
  
              >>> # the last 3 samples by energy
              >>> print(sampleset.slice(-3, None))
                 0  1  2  3  4  5  6  7  8  9 energy num_oc.
              0  0  0  0  0  0  0  0  1  0  0      7       1
              1  0  0  0  0  0  0  0  0  1  0      8       1
              2  0  0  0  0  0  0  0  0  0  1      9       1
              ['BINARY', 3 rows, 3 samples, 10 variables]
  
              >>> # every second sample in between (skip the top and the bottom 3)
              >>> print(sampleset.slice(3, -3, 2))
                 0  1  2  3  4  5  6  7  8  9 energy num_oc.
              0  0  0  0  1  0  0  0  0  0  0      3       1
              1  0  0  0  0  0  1  0  0  0  0      5       1
              ['BINARY', 2 rows, 2 samples, 10 variables]
  
          """; 19, assignment; 20, identifier:kwargs; 21, comment:# be strict about allowed kwargs: throw the same error as python3 would; 22, block; 23, identifier:slice_args; 24, block; 25, else_clause; 26, comparison_operator:sorted_by is None; 27, block; 28, else_clause; 29, call; 30, identifier:sorted_by; 31, call; 32, raise_statement; 33, expression_statement; 34, block; 35, identifier:sorted_by; 36, None; 37, expression_statement; 38, block; 39, call; 40, argument_list; 41, attribute; 42, argument_list; 43, call; 44, assignment; 45, expression_statement; 46, assignment; 47, expression_statement; 48, expression_statement; 49, identifier:type; 50, argument_list; 51, identifier:record; 52, attribute; 53, call; 54, attribute; 55, identifier:kwargs; 56, identifier:pop; 57, string; 58, string; 59, identifier:TypeError; 60, argument_list; 61, identifier:selector; 62, call; 63, assignment; 64, identifier:record; 65, subscript; 66, assignment; 67, assignment; 68, identifier:self; 69, identifier:self; 70, identifier:variables; 71, attribute; 72, argument_list; 73, identifier:self; 74, identifier:vartype; 75, string_content:sorted_by; 76, string_content:energy; 77, call; 78, identifier:slice; 79, argument_list; 80, identifier:selector; 81, call; 82, attribute; 83, identifier:selector; 84, identifier:sort_indices; 85, call; 86, identifier:record; 87, subscript; 88, identifier:copy; 89, identifier:deepcopy; 90, attribute; 91, attribute; 92, argument_list; 93, list_splat; 94, identifier:slice; 95, argument_list; 96, identifier:self; 97, identifier:record; 98, attribute; 99, argument_list; 100, attribute; 101, subscript; 102, identifier:self; 103, identifier:info; 104, concatenated_string; 105, identifier:format; 106, subscript; 107, identifier:slice_args; 108, None; 109, identifier:np; 110, identifier:argsort; 111, subscript; 112, identifier:self; 113, identifier:record; 114, identifier:sort_indices; 115, identifier:selector; 116, string; 117, string; 118, call; 119, integer:0; 120, attribute; 121, identifier:sorted_by; 122, string_content:slice got an unexpected; 123, string_content:keyword argument {!r}; 124, attribute; 125, argument_list; 126, identifier:self; 127, identifier:record; 128, identifier:kwargs; 129, identifier:popitem
code: def slice(self, *slice_args, **kwargs):
          """Create a new SampleSet with rows sliced according to standard Python
          slicing syntax.
  
          Args:
              start (int, optional, default=None):
                  Start index for `slice`.
  
              stop (int):
                  Stop index for `slice`.
  
              step (int, optional, default=None):
                  Step value for `slice`.
  
              sorted_by (str/None, optional, default='energy'):
                  Selects the record field used to sort the samples before
                  slicing. Note that `sorted_by` determines the sample order in
                  the returned SampleSet.
  
          Returns:
              :obj:`.SampleSet`
  
          Examples:
  
              >>> import numpy as np
              ...
              >>> sampleset = dimod.SampleSet.from_samples(np.diag(range(1, 11)), dimod.BINARY, energy=range(10))
              >>> print(sampleset)
                 0  1  2  3  4  5  6  7  8  9 energy num_oc.
              0  1  0  0  0  0  0  0  0  0  0      0       1
              1  0  1  0  0  0  0  0  0  0  0      1       1
              2  0  0  1  0  0  0  0  0  0  0      2       1
              3  0  0  0  1  0  0  0  0  0  0      3       1
              4  0  0  0  0  1  0  0  0  0  0      4       1
              5  0  0  0  0  0  1  0  0  0  0      5       1
              6  0  0  0  0  0  0  1  0  0  0      6       1
              7  0  0  0  0  0  0  0  1  0  0      7       1
              8  0  0  0  0  0  0  0  0  1  0      8       1
              9  0  0  0  0  0  0  0  0  0  1      9       1
              ['BINARY', 10 rows, 10 samples, 10 variables]
  
              >>> # the first 3 samples by energy == truncate(3)
              >>> print(sampleset.slice(3))
                 0  1  2  3  4  5  6  7  8  9 energy num_oc.
              0  1  0  0  0  0  0  0  0  0  0      0       1
              1  0  1  0  0  0  0  0  0  0  0      1       1
              2  0  0  1  0  0  0  0  0  0  0      2       1
              ['BINARY', 3 rows, 3 samples, 10 variables]
  
              >>> # the last 3 samples by energy
              >>> print(sampleset.slice(-3, None))
                 0  1  2  3  4  5  6  7  8  9 energy num_oc.
              0  0  0  0  0  0  0  0  1  0  0      7       1
              1  0  0  0  0  0  0  0  0  1  0      8       1
              2  0  0  0  0  0  0  0  0  0  1      9       1
              ['BINARY', 3 rows, 3 samples, 10 variables]
  
              >>> # every second sample in between (skip the top and the bottom 3)
              >>> print(sampleset.slice(3, -3, 2))
                 0  1  2  3  4  5  6  7  8  9 energy num_oc.
              0  0  0  0  1  0  0  0  0  0  0      3       1
              1  0  0  0  0  0  1  0  0  0  0      5       1
              ['BINARY', 2 rows, 2 samples, 10 variables]
  
          """
          # handle `sorted_by` kwarg with a default value in a python2-compatible way
          sorted_by = kwargs.pop('sorted_by', 'energy')
          if kwargs:
              # be strict about allowed kwargs: throw the same error as python3 would
              raise TypeError('slice got an unexpected '
                              'keyword argument {!r}'.format(kwargs.popitem()[0]))
  
          # follow Python's slice syntax
          if slice_args:
              selector = slice(*slice_args)
          else:
              selector = slice(None)
  
          if sorted_by is None:
              record = self.record[selector]
          else:
              sort_indices = np.argsort(self.record[sorted_by])
              record = self.record[sort_indices[selector]]
  
          return type(self)(record, self.variables, copy.deepcopy(self.info),
                            self.vartype)