sercol 1.2.0__tar.gz → 1.2.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,14 +1,14 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sercol
3
- Version: 1.2.0
3
+ Version: 1.2.2
4
4
  Summary: Rich collection class with grouping and filtering helpers
5
5
  Author-email: Alex Rubinsteyn <alex.rubinsteyn@unc.edu>
6
+ License-Expression: Apache-2.0
6
7
  Project-URL: Homepage, https://github.com/openvax/sercol
7
8
  Project-URL: Bug Tracker, https://github.com/openvax/sercol/issues
8
9
  Classifier: Development Status :: 4 - Beta
9
10
  Classifier: Environment :: Console
10
11
  Classifier: Operating System :: OS Independent
11
- Classifier: License :: OSI Approved :: Apache Software License
12
12
  Classifier: Programming Language :: Python
13
13
  Classifier: Programming Language :: Python :: 3.11
14
14
  Classifier: Programming Language :: Python :: 3.12
@@ -1,5 +1,5 @@
1
1
  [build-system]
2
- requires = ["setuptools>=64"]
2
+ requires = ["setuptools>=77.0.3"]
3
3
  build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
@@ -7,11 +7,12 @@ name = "sercol"
7
7
  requires-python = ">=3.11"
8
8
  authors = [{name="Alex Rubinsteyn", email="alex.rubinsteyn@unc.edu"}]
9
9
  description = "Rich collection class with grouping and filtering helpers"
10
+ license = "Apache-2.0"
11
+ license-files = ["LICENSE"]
10
12
  classifiers = [
11
13
  "Development Status :: 4 - Beta",
12
14
  "Environment :: Console",
13
15
  "Operating System :: OS Independent",
14
- "License :: OSI Approved :: Apache Software License",
15
16
  "Programming Language :: Python",
16
17
  "Programming Language :: Python :: 3.11",
17
18
  "Programming Language :: Python :: 3.12",
@@ -13,6 +13,6 @@
13
13
 
14
14
  from .collection import Collection
15
15
 
16
- __version__ = "1.2.0"
16
+ __version__ = "1.2.2"
17
17
 
18
18
  __all__ = ["Collection", "__version__"]
@@ -25,6 +25,9 @@ _OPTIONAL_STATE = ("distinct", "sort_key", "sources")
25
25
  # Elements which to_dataframe() puts in a single "value" column.
26
26
  _SCALAR_TYPES = (str, bytes, numbers.Number, type(None))
27
27
 
28
+ # Number of elements from each end of a collection which __hash__() looks at.
29
+ _HASHED_ELEMENTS_PER_END = 4
30
+
28
31
 
29
32
  class Collection(serializable.Serializable):
30
33
  """
@@ -368,7 +371,44 @@ class Collection(serializable.Serializable):
368
371
  return iter(self.elements)
369
372
 
370
373
  def __hash__(self):
371
- return hash(len(self))
374
+ """
375
+ Hash the class, the number of elements and the first and last few
376
+ elements, so collections with different contents rarely share a
377
+ hash while hashing a large collection stays cheap. Collections which
378
+ differ only in their middle elements share a hash.
379
+
380
+ An element which is itself a collection contributes only its class
381
+ and its number of elements, so nested collections are cheap to hash.
382
+
383
+ If hashing one of the sampled elements raises ``TypeError``, as
384
+ unhashable elements such as dicts and lists do, no element is used
385
+ and only the class and the number of elements are hashed. Any other
386
+ exception raised by an element's ``__hash__`` is passed on.
387
+
388
+ Equal collections have equal hashes, with one exception: when an
389
+ unhashable element of one collection equals a hashable element of
390
+ the other, such as a ``set`` and a ``frozenset`` or a ``bytearray``
391
+ and ``bytes``, the two collections are equal but hash differently.
392
+
393
+ The hash is computed from the current elements. Changing ``elements``
394
+ while the collection is in a set or is a dict key makes it
395
+ unreachable there, as with any mutable object which is hashed by
396
+ value.
397
+ """
398
+ elements = self.elements
399
+ count = len(elements)
400
+ if count > 2 * _HASHED_ELEMENTS_PER_END:
401
+ elements = [
402
+ *elements[:_HASHED_ELEMENTS_PER_END],
403
+ *elements[-_HASHED_ELEMENTS_PER_END:]]
404
+ sampled = tuple(
405
+ (element.__class__, len(element))
406
+ if isinstance(element, Collection) else element
407
+ for element in elements)
408
+ try:
409
+ return hash((self.__class__, count, sampled))
410
+ except TypeError:
411
+ return hash((self.__class__, count))
372
412
 
373
413
  def __getitem__(self, idx):
374
414
  return self.elements[idx]
@@ -1,14 +1,14 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sercol
3
- Version: 1.2.0
3
+ Version: 1.2.2
4
4
  Summary: Rich collection class with grouping and filtering helpers
5
5
  Author-email: Alex Rubinsteyn <alex.rubinsteyn@unc.edu>
6
+ License-Expression: Apache-2.0
6
7
  Project-URL: Homepage, https://github.com/openvax/sercol
7
8
  Project-URL: Bug Tracker, https://github.com/openvax/sercol/issues
8
9
  Classifier: Development Status :: 4 - Beta
9
10
  Classifier: Environment :: Console
10
11
  Classifier: Operating System :: OS Independent
11
- Classifier: License :: OSI Approved :: Apache Software License
12
12
  Classifier: Programming Language :: Python
13
13
  Classifier: Programming Language :: Python :: 3.11
14
14
  Classifier: Programming Language :: Python :: 3.12
@@ -215,6 +215,104 @@ def test_eq_compares_sources():
215
215
  neq_(Collection([1]), [1])
216
216
 
217
217
 
218
+ def test_hash_differs_for_different_contents():
219
+ assert hash(Collection([1, 2])) != hash(Collection(["x", "y"]))
220
+ assert hash(Collection([1, 2])) != hash(Collection([1, 3]))
221
+ assert hash(Collection([1, 2])) != hash(Collection([1, 2, 3]))
222
+ # The last elements count too, so a shared prefix doesn't collide.
223
+ assert hash(Collection(range(100))) != hash(Collection(list(range(99)) + [0]))
224
+ assert hash(Collection([1, 2])) != hash(RenamedElements([1, 2]))
225
+
226
+
227
+ def test_equal_collections_have_equal_hashes():
228
+ for elements in ([], [1], ["a", "b", "c"], list(range(100))):
229
+ eq_(hash(Collection(elements)), hash(Collection(list(elements))))
230
+ eq_(hash(Collection([1, 2])), hash(Collection([1.0, 2.0])))
231
+ eq_(Collection([1, 2]), Collection([1.0, 2.0]))
232
+ eq_(
233
+ hash(Collection([1], sources={"a.vcf"})),
234
+ hash(Collection([1], sources=["a.vcf"])))
235
+
236
+
237
+ def test_hash_of_unhashable_elements_does_not_raise():
238
+ first = Collection([{"x": 1}, {"x": 2}])
239
+ second = Collection([{"x": 1}, {"x": 2}])
240
+ eq_(first, second)
241
+ eq_(hash(first), hash(second))
242
+ assert hash(first) != hash(Collection([{"x": 1}]))
243
+ assert hash(first) != hash(RenamedElements([{"x": 1}, {"x": 2}]))
244
+
245
+
246
+ def test_hash_of_each_short_length():
247
+ for length in range(1, 12):
248
+ elements = list(range(length))
249
+ eq_(hash(Collection(elements)), hash(Collection(list(elements))))
250
+ changed = [-1] + elements[1:]
251
+ assert hash(Collection(elements)) != hash(Collection(changed))
252
+ changed = elements[:-1] + [-1]
253
+ assert hash(Collection(elements)) != hash(Collection(changed))
254
+
255
+
256
+ def test_hash_ignores_middle_elements_of_long_collections():
257
+ first = Collection(range(9))
258
+ second = Collection([0, 1, 2, 3, 99, 5, 6, 7, 8])
259
+ neq_(first, second)
260
+ eq_(hash(first), hash(second))
261
+ # An unhashable element outside the sampled ends doesn't matter either.
262
+ third = Collection([0, 1, 2, 3, {}, 5, 6, 7, 8])
263
+ eq_(hash(first), hash(third))
264
+
265
+
266
+ def test_hash_of_nested_collections_is_cheap():
267
+ nested = Collection([1])
268
+ for _ in range(200):
269
+ nested = Collection([nested])
270
+ same = Collection([1])
271
+ for _ in range(200):
272
+ same = Collection([same])
273
+ eq_(hash(nested), hash(same))
274
+ eq_(hash(Collection([Collection([1, 2])])), hash(Collection([Collection([1, 2])])))
275
+ eq_(Collection([Collection([1, 2])]), Collection([Collection([1, 2])]))
276
+ assert hash(Collection([Collection([1, 2])])) != hash(Collection([Collection([1])]))
277
+ assert hash(Collection([Collection([1])])) != hash(Collection([RenamedElements([1])]))
278
+
279
+
280
+ def test_hash_of_collection_which_contains_itself():
281
+ collection = Collection([1])
282
+ collection.elements.append(collection)
283
+ eq_(hash(collection), hash(collection))
284
+
285
+
286
+ def test_hash_of_subclass_instances():
287
+ eq_(hash(RenamedElements([1, 2])), hash(RenamedElements([1, 2])))
288
+ eq_(RenamedElements([1, 2]), RenamedElements([1, 2]))
289
+
290
+
291
+ def test_hash_passes_on_other_errors_from_elements():
292
+ class BrokenHash:
293
+ def __hash__(self):
294
+ raise ValueError("broken")
295
+
296
+ with pytest.raises(ValueError, match="broken"):
297
+ hash(Collection([BrokenHash()]))
298
+
299
+
300
+ def test_equal_elements_of_different_hashability_hash_differently():
301
+ # Documented exception to "equal collections have equal hashes".
302
+ first = Collection([{1}])
303
+ second = Collection([frozenset({1})])
304
+ eq_(first, second)
305
+ assert hash(first) != hash(second)
306
+
307
+
308
+ def test_collections_as_set_members_and_dict_keys():
309
+ collections = [Collection([i, i + 1]) for i in range(50)]
310
+ eq_(len(set(collections + [Collection([0, 1])])), 50)
311
+ lookup = {collection: collection[0] for collection in collections}
312
+ eq_(lookup[Collection([7, 8])], 7)
313
+ eq_(len({hash(collection) for collection in collections}), 50)
314
+
315
+
218
316
  def test_to_dataframe_of_scalars():
219
317
  df = Collection([1, 2]).to_dataframe()
220
318
  eq_(list(df.columns), ["value"])
File without changes
File without changes
File without changes