Do you want my frozenset implemenation using this lib?
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Idoneità per principianti
- 20/100
Direzione di ricerca
Esamina la classe ImmutableSet proposta e le relative funzioni create, add, remove, contains, union e intersection, insieme ai test inclusi. Determina innanzitutto se il progetto vuole aggiungere questa astrazione e dove dovrebbe essere collocata; l’issue non definisce un criterio di accettazione né una posizione nel progetto. Il lavoro sarebbe considerato completato quando i maintainer avranno concordato l’API, l’ambito dell’integrazione e i test.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
if you do here it is:
from typing import Iterable
import immutables
# Design choices:
# - Using a class to allow for typing
# - The class has no methods to ensure all logic is in the functions below.
# - The wrapped map is kept private.
# - To prevent the user from making subtle mistake, we override `__eq__` to raise an error.
# Corollaries:
# - Will not work with operators ootb, e.g. `in`, `==` or `len`.
class ImmutableSet:
def __init__(self, inner):
self._inner = inner
def __eq__(self, _):
raise NotImplementedError(
"Use the functions in this module instead of operators.",
)
def create(iterable: Iterable) -> ImmutableSet:
return ImmutableSet(immutables.Map(map(lambda x: (x, None), iterable)))
EMPTY: ImmutableSet = create([])
def equals(s1: ImmutableSet, s2: ImmutableSet) -> bool:
return s1._inner == s2._inner # noqa: SF01
def length(set: ImmutableSet) -> int:
return len(set._inner) # noqa: SF01
def add(set: ImmutableSet, element) -> ImmutableSet:
return ImmutableSet(set._inner.set(element, None)) # noqa: SF01
def remove(set: ImmutableSet, element) -> ImmutableSet:
return ImmutableSet(set._inner.delete(element)) # noqa: SF01
def contains(set: ImmutableSet, element) -> bool:
return element in set._inner # noqa: SF01
def union(set1: ImmutableSet, set2: ImmutableSet) -> ImmutableSet:
smaller, larger = sorted([set1, set2], key=length)
return ImmutableSet(larger._inner.update(smaller._inner)) # noqa: SF01
def intersection(set1: ImmutableSet, set2: ImmutableSet) -> ImmutableSet:
smaller, larger = sorted([set1, set2], key=length)
for element in smaller._inner: # noqa: SF01
if not contains(larger, element):
smaller = remove(smaller, element)
return smaller
and tests:
import time
def test_add():
assert immutable_set.equals(
immutable_set.add(
immutable_set.create([1, 2, 3]),
4,
),
immutable_set.create(
[1, 2, 3, 4],
),
)
def test_remove():
assert immutable_set.equals(
immutable_set.remove(
immutable_set.create([1, 2, 3]),
2,
),
immutable_set.create([1, 3]),
)
def test_contains():
assert immutable_set.contains(immutable_set.create([1, 2, 3]), 3)
def test_not_contains():
assert not immutable_set.contains(immutable_set.create([1, 2, 3]), 4)
def test_union():
assert immutable_set.equals(
immutable_set.union(
immutable_set.create([1, 2, 3, 4]),
immutable_set.create([1, 2, 3]),
),
immutable_set.create([1, 2, 3, 4]),
)
def _is_o_of_1(f, arg1, arg2):
start = time.perf_counter()
f(arg1, arg2)
return time.perf_counter() - start < 0.0001
_large_number = 9999
def test_intersection():
assert immutable_set.equals(
immutable_set.intersection(
immutable_set.create([1, 2]),
immutable_set.create([2]),
),
immutable_set.create([2]),
)
def test_performance_sanity():
assert not _is_o_of_1(
immutable_set.union,
immutable_set.create(range(_large_number)),
immutable_set.create(range(_large_number)),
)
def test_union_performance():
assert _is_o_of_1(
immutable_set.union,
immutable_set.create(range(_large_number)),
immutable_set.create(range(_large_number // 64, _large_number // 32)),
)
def test_intersection_performance():
assert _is_o_of_1(
immutable_set.intersection,
immutable_set.create(range(_large_number)),
immutable_set.create(range(1)),
)
- Lingua principale
- C
- Stelle
- 1.2k
- Fork
- 60
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Guida per i contributori
Nessuna guida per i contributori indicizzata per questo repository
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di MagicStack/immutables
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 38/100
MagicStack/immutables#121 · 6 reazioni ·
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 25/100
MagicStack/immutables#107 ·
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 32/100
MagicStack/immutables#102 ·
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 25/100
MagicStack/immutables#75 · 1 commento ·
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 30/100
MagicStack/immutables#55 · 3 commenti ·
Tutte le issue di MagicStack/immutables
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
BasedHardware/omi#15662 · 1 commento ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 84/100
-
level/task module/gcp type/bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 85/100
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 86/100
hapostgres/pg_auto_failover#1190 ·
-
docs
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 85/100