def make_dictionary_catalog_2d(self): self._catalog.sort(key=_first_then_second_index) i_sorted = make_sorted_distinct_sequence(i for (i, _), _ in self._catalog) j_sorted = make_sorted_distinct_sequence(j for (_, j), _ in self._catalog) return DictionaryCatalog2D(i_sorted, j_sorted, self._catalog)
def ensure_superset(superset, subset): """Ensure that one collection is a subset of another. Args: superset: A sequence containing all items. subset: Subset must either be a collection the elements of which are a subset of superset, or a slice object, in which case the subset items will be sliced from superset. Returns: A sorted, distinct collection which is a subset of superset. Raises: ValueError: If the items in subset are not a subset of the items in all_items. """ if subset is None: return superset elif isinstance(subset, slice): return superset[subset] else: subset = make_sorted_distinct_sequence(subset) if not is_superset(superset, subset): raise ValueError("subset_or_slice {!r} is not a subset of all_items {!r}" .format(subset, superset)) return subset
def _create_catalog_2(self): """Create a catalog for two-dimensional integer keys. Each key must be a two-element sequence. """ i_sorted = make_sorted_distinct_sequence(i for (i, j), value in self._catalog) j_sorted = make_sorted_distinct_sequence(j for (i, j), value in self._catalog) i_is_regular = isinstance(i_sorted, range) j_is_regular = isinstance(j_sorted, range) if i_is_regular and j_is_regular: is_rm, diff = self._is_row_major(i_sorted, j_sorted) if is_rm: return RowMajorCatalog2D(i_sorted, j_sorted, diff) return DictionaryCatalog2D(i_sorted, j_sorted, self._catalog)
def make_sorted_ranges(self): i_sorted = make_sorted_distinct_sequence(i for (i, _), _ in self._catalog) j_sorted = make_sorted_distinct_sequence(j for (_, j), _ in self._catalog) if len(i_sorted) * len(j_sorted) != len(self._catalog): # The do not match so use a dictionary-based mapping return None vs = [v for (_, _), v in self._catalog] # Are the values unique and in ascending or descending order? if not (is_sorted(vs, reverse=False, distinct=True) or is_sorted(vs, reverse=True, distinct=True)): # The values are not both unique and sorted, so use a dictionary-based mapping return None v_sorted = make_sorted_distinct_sequence(vs, sense=None) i_is_regular = isinstance(i_sorted, range) j_is_regular = isinstance(j_sorted, range) v_is_regular = isinstance(v_sorted, range) if not (i_is_regular and j_is_regular and v_is_regular): # The i, j and v values are not regularly spaced, so use a dictionary-based mapping return None return i_sorted, j_sorted, v_sorted
def _create_catalog_2(self): """Create a catalog for two-dimensional integer keys. Each key must be a two-element sequence. """ i_sorted = make_sorted_distinct_sequence( i for (i, j), value in self._catalog) j_sorted = make_sorted_distinct_sequence( j for (i, j), value in self._catalog) i_is_regular = isinstance(i_sorted, range) j_is_regular = isinstance(j_sorted, range) if i_is_regular and j_is_regular: is_rm, diff = self._is_row_major(i_sorted, j_sorted) if is_rm: return RowMajorCatalog2D(i_sorted, j_sorted, diff) return DictionaryCatalog2D(i_sorted, j_sorted, self._catalog)
def cdp_numbers(self): """A sorted immutable collection of CDP numbers. Test for membership in this collection to determine if a particular CDP exists or iterate over this collection to generate all CDP numbers in order. Returns: A sorted immutable collection of CDP numbers which supports the Sized, Iterable, Container and Sequence protocols. """ if self._cdp_numbers is None: self._cdp_numbers = make_sorted_distinct_sequence(self._cdp_catalog.keys()) return self._cdp_numbers
def xline_numbers(self): """A sorted immutable collection of crossline numbers. Test for membership in this collection to determine if a particular crossline exists or iterate over this collection to generate all crossline numbers in order. Returns: A sorted immutable collection of crossline numbers which supports the Sized, Iterable, Container and Sequence protocols. """ if self._xline_numbers is None: if hasattr(self._line_catalog, 'j_range'): self._xline_numbers = self._line_catalog.j_range else: self._xline_numbers = make_sorted_distinct_sequence(j for i, j in self._line_catalog) return self._xline_numbers
def test_descending_range_result_is_descending(self, r): seq = make_sorted_distinct_sequence(r, sense=SortSense.descending) assert seq.step < 0
def test_list_with_invalid_sense_value_raises_type_error(self): with raises(TypeError): make_sorted_distinct_sequence([1, 2], 42)
def test_sequence_sense_is_preserved(self, r, b): s = sorted(r, reverse=b) seq = make_sorted_distinct_sequence(s, sense=None) ascending = is_sorted(seq, reverse=False, distinct=True) descending = is_sorted(seq, reverse=True, distinct=True) assert b == descending != ascending
def test_descending_sequence_result_is_descending(self, r): s = sorted(r, reverse=True) seq = make_sorted_distinct_sequence(s, sense=SortSense.descending) assert is_sorted(seq, reverse=True, distinct=True)
def test_range_sense_is_preserved(self, r): seq = make_sorted_distinct_sequence(r, sense=None) assert sgn(r.step) == sgn(seq.step)