Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/adamniederer/setdict
A dictionary which supports set operations
https://github.com/adamniederer/setdict
dict python python27 python3 set
Last synced: about 1 month ago
JSON representation
A dictionary which supports set operations
- Host: GitHub
- URL: https://github.com/adamniederer/setdict
- Owner: AdamNiederer
- License: agpl-3.0
- Created: 2017-04-19T04:23:28.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-05-14T05:27:58.000Z (over 6 years ago)
- Last Synced: 2024-10-16T11:14:33.424Z (3 months ago)
- Topics: dict, python, python27, python3, set
- Language: Python
- Size: 13.7 KB
- Stars: 1
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.org
- License: LICENSE.txt
Awesome Lists containing this project
README
* SetDict
[[https://pypi.python.org/pypi/setdict][https://img.shields.io/pypi/v/setdict.svg]]
[[https://pypi.python.org/pypi/setdict][https://img.shields.io/pypi/pyversions/setdict.svg]]It's a dict; it's a set; it's a SetDict!
It behaves exactly like a dict
#+BEGIN_SRC python
from setdict import SetDicta = SetDict(a=3, b=2)
b = SetDict({"a": 3, "b": 2})print(a) # => {'a': 3, 'b': 2}
print(a["a"]) # => 3
print({k: v + 1 for k, v in b.items()}) # => {'a': 4, 'b': 3}
#+END_SRCIt supports set operations as well. If there's ever a clash in values, the
leftmost SetDict's value is used.#+BEGIN_SRC python
from setdict import SetDictfoo = SetDict(a=1, b=2, c=3)
bar = SetDict(b=3, c=4, d=5)print(foo | bar) # => {'a': 1, 'b': 2, 'c': 3, 'd': 5}
print(foo & bar) # => {'b': 2, 'c': 3}
print(foo ^ bar) # => {'a': 1, 'd': 5}
print(foo - bar) # => {'a': 1}
#+END_SRCSetDicts work with regular dictionaries and sets as well
#+BEGIN_SRC python
from setdict import SetDictfoo = SetDict(a=1, b=2, c=3)
bar = dict(b=3, c=4, d=5)print(foo | bar) # => {'a': 1, 'b': 2, 'c': 3, 'd': 5}
print(foo & bar) # => {'b': 2, 'c': 3}
print(foo ^ bar) # => {'a': 1, 'd': 5}
print(foo - bar) # => {'a': 1}print(bar | foo) # => {'a': 1, 'b': 2, 'c': 3, 'd': 5}
print(bar & foo) # => {'b': 2, 'c': 3}
print(bar ^ foo) # => {'a': 1, 'd': 5}
print(bar - foo) # => {'a': 1}
#+END_SRCAny set operation between a SetDict and a dict returns a SetDict, and operations
between sets and SetDicts return sets where returning dicts wouldn't make any
sense.#+BEGIN_SRC python
from setdict import SetDictfoo = SetDict(a=1, b=2, c=3)
bar = {'a', 'c', 'd'}print(foo | bar) # => {'a', 'b', 'c', 'd'}
print(foo & bar) # => {'a': 1, 'c': 3}
print(foo ^ bar) # => {'b', 'd'}
print(foo - bar) # => {'b': 2}print(bar | foo) # => {'a', 'b', 'c', 'd'}
print(bar & foo) # => {'a': 1, 'c': 3}
print(bar ^ foo) # => {'b', 'd'}
print(bar - foo) # => {'d'}
#+END_SRCHere's an exhaustive list of what is returned when
| | SetDict, SetDict | set, SetDict | SetDict, set |
|----------------------+------------------+--------------+--------------|
| Union | SetDict | set | set |
| Intersection | SetDict | SetDict | SetDict |
| Difference | SetDict | set | SetDict |
| Symmetric Difference | SetDict | set | set |Superset and subset operators, and set- and dict-equality are also supported
#+BEGIN_SRC python
from setdict import SetDictfoo = SetDict(a=1, b=2, c=3)
bar = SetDict(a=2, b=3, c=4)
bar = {'a', 'b', 'c'}print(foo == bar) # => False
print(foo == baz) # => True
print(bar == baz) # => Trueprint(foo >= bar) # => True
print(foo <= bar) # => True
print(foo > bar) # => False
print(foo < bar) # => Falseprint(foo >= baz) # => True
print(foo <= baz) # => True
print(foo > baz) # => False
print(foo < baz) # => False
#+END_SRCSetDict supports:
- Set operators <, <=, >, >=, &, |, ^, -, ==, !=
- Compound assignments &=, |=, ^=, -=
- Dictionary methods
- Set methods
- All aforementioned operations between SetDicts, sets, and dicts
- Iteration