OrdDict (version 1.0.0) | index OrdDict.py |
This package implements tools to build python package and tools.
>>> my_ord_dict = OrdDict({"key1": "value1", "key2": "value2"}, {"key3": "value3"}, key4="value4")
>>> my_ord_dict["key6"] = "value6"
>>> my_ord_dict.insert(4, "key5", "value5")
>>> len(my_ord_dict)
6
>>> my_ord_dict["key1"]
'value1'
>>> my_ord_dict["key2"]
'value2'
>>> my_ord_dict["key3"]
'value3'
>>> my_ord_dict["key4"]
'value4'
>>> my_ord_dict["key5"]
'value5'
>>> my_ord_dict["key6"]
'value6'
>>> my_ord_dict.index_value(1)
'value2'
>>> my_ord_dict.index_value(2)
'value3'
>>> my_ord_dict.index_value(3)
'value4'
>>> my_ord_dict.index_value(4)
'value5'
>>> my_ord_dict.index_value(5)
'value6'
>>> my_ord_dict.to_dict()
{'key1': 'value1', 'key2': 'value2', 'key3': 'value3', 'key4': 'value4', 'key5': 'value5', 'key6': 'value6'}
>>> my_ord_dict.move(1, "key3")
>>> my_ord_dict.index_value(1)
'value3'
>>> my_ord_dict.index_value(2)
'value2'
>>> my_ord_dict.delete("key3")
>>> my_ord_dict.index_value(1)
'value2'
>>> len(my_ord_dict)
5
>>> my_ord_dict["key2"] = "new value"
>>> my_ord_dict["key2"]
'new value'
>>> my_ord_dict.insert(0, "new key", "new value")
>>> my_ord_dict["new key"]
'new value'
>>> my_ord_dict.index_value(0)
'new value'
>>> my_ord_dict.index_value(1)
'value1'
>>> my_ord_dict.insert(1, "new key", "new value")
Traceback (most recent call last):
...
ValueError: Can't insert 'new key', item exists.
>>> my_ord_dict.insert(1, "new key", "new value", error=False)
>>> my_ord_dict.index_value(1)
'new value'
>>> my_ord_dict.index_value(0)
'value1'
>>> del my_ord_dict["new key"]
>>> my_ord_dict.index_key(1)
'key2'
>>> my_ord_dict.update({"update1": "update", "key2": "value2"})
>>> my_ord_dict["update1"]
'update'
>>> my_ord_dict["key2"]
'value2'
>>> my_ord_dict.index_key(1)
'key2'
>>> my_ord_dict.index_key(-1)
'update1'
>>> my_ord_dict.clear()
>>> len(my_ord_dict)
0
>>> my_ord_dict.to_dict()
{}
>>> my_ord_dict.update({"key1": "value1", "key2": "value2"})
>>> next(my_ord_dict)
('key1', 'value1')
>>> next(my_ord_dict)
('key2', 'value2')
>>> next(my_ord_dict)
Traceback (most recent call last):
...
StopIteration
>>> str(my_ord_dict)
"OrdDict({'key1': 'value1', 'key2': 'value2'})"
>>> print(repr(my_ord_dict))
OrdDict({'key1': 'value1', 'key2': 'value2'})
>>> my_ord_dict == {'key1': 'value1', 'key2': 'value2'}
True
>>> my_ord_dict == OrdDict({'key1': 'value1', 'key2': 'value2'})
True
>>> my_ord_dict == {'key2': 'value2', 'key1': 'value1'}
False
>>> my_ord_dict == {'key1': 'value1', 'key2': 'value3'}
False
>>> 'key1' in my_ord_dict
True
>>> 'value1' in my_ord_dict
False
>>> my_ord_dict + {'key1': 'new value', 'key3': 'value3'}
{'key1': 'new value', 'key2': 'value2', 'key3': 'value3'}
>>> my_ord_dict += {'key1': 'new value', 'key3': 'value3'}
>>> my_ord_dict
OrdDict({'key1': 'new value', 'key2': 'value2', 'key3': 'value3'})
>>> my_ord_dict != {'key1': 'new value', 'key2': 'value2', 'key3': 'value3'}
False
>>> my_ord_dict != OrdDict({'key': 'value'})
True
>>> list(reversed(my_ord_dict))
[('key3', 'value3'), ('key2', 'value2'), ('key1', 'new value')]
>>> OrdDict() | {"abc": "def"}
OrdDict({'abc': 'def'})
>>> my_ord_dict | OrdDict({'key1': 'value1', 'abc': 'def'})
OrdDict({'key1': 'value1', 'key2': 'value2', 'key3': 'value3', 'abc': 'def'})
>>> my_ord_dict.add('new key', 'value4')
>>> my_ord_dict.add('key1', 'value1')
>>> my_ord_dict['key1']
'value1'
>>> my_ord_dict.get_key_value(0)
('key1', 'value1')
>>> my_ord_dict.get_key_value(-1)
('new key', 'value4')
>>> my_ord_dict.modify('new key', 'new value')
>>> my_ord_dict.get_key_value(-1)
('new key', 'new value')
>>> my_ord_dict.modify('not exists', None)
Traceback (most recent call last):
...
ValueError: Can't modify 'not exists', item doesn't exists.
>>> my_ord_dict.modify('not exists', 'value', error=False)
>>> my_ord_dict.get_key_value(-1)
('not exists', 'value')
>>> list(my_ord_dict.values())
['value1', 'value2', 'value3', 'new value', 'value']
>>> list(my_ord_dict.keys())
['key1', 'key2', 'key3', 'new key', 'not exists']
>>> my_ord_dict.popitem()
('not exists', 'value')
>>> my_ord_dict.pop('new key')
'new value'
>>> my_ord_dict.pop_index(1)
('key2', 'value2')
>>> my_ord_dict
OrdDict({'key1': 'value1', 'key3': 'value3'})
>>> list(my_ord_dict.items())
[('key1', 'value1'), ('key3', 'value3')]
>>> list(iter(my_ord_dict))
[('key1', 'value1'), ('key3', 'value3')]
>>> my_ord_dict.extend({'key1': 'new value', 'key2': 'value2'})
>>> my_ord_dict
OrdDict({'key1': 'value1', 'key3': 'value3', 'key2': 'value2'})
>>> my_ord_dict.sort()
>>> my_ord_dict
OrdDict({'key1': 'value1', 'key2': 'value2', 'key3': 'value3'})
>>> my_ord_dict |= {'key1': 'new value', 'key4': 'value4'}
>>> my_ord_dict
OrdDict({'key1': 'new value', 'key2': 'value2', 'key3': 'value3', 'key4': 'value4'})
>>> my_ord_dict |= OrdDict({'key4': 'new value', 'key5': 'value5'})
>>> my_ord_dict
OrdDict({'key1': 'new value', 'key2': 'value2', 'key3': 'value3', 'key4': 'new value', 'key5': 'value5'})
>>> my_ord_dict.remove(0)
>>> my_ord_dict
OrdDict({'key2': 'value2', 'key3': 'value3', 'key4': 'new value', 'key5': 'value5'})
>>> my_ord_dict.get('key2')
'value2'
>>> my_ord_dict.setdefault('key2', 'new value')
'value2'
>>> my_ord_dict.setdefault('key6', 'value6')
'value6'
>>> my_ord_dict
OrdDict({'key2': 'value2', 'key3': 'value3', 'key4': 'new value', 'key5': 'value5', 'key6': 'value6'})
>>> my_ord_dict.reverse()
>>> my_ord_dict
OrdDict({'key6': 'value6', 'key5': 'value5', 'key4': 'new value', 'key3': 'value3', 'key2': 'value2'})
>>> my_ord_dict.index('key6')
0
>>>
Tests:
~# python3 -m doctest -v OrdDict.py
97 tests in 44 items.
97 passed and 0 failed.
Test passed.
~# coverage run -m doctest OrdDict.py
~# coverage run OrdDict.py
~# coverage report
Name Stmts Miss Cover
--------------------------------
OrdDict.py 172 0 100%
--------------------------------
TOTAL 172 0 100%
~#
Classes | ||||||||||
|
Data | ||
__all__ = ['OrdDict'] __author_email__ = 'mauricelambert434@gmail.com' __copyright__ = '\nPythonToolsKit Copyright (C) 2022 Maurice Lam...ome to redistribute it\nunder certain conditions.\n' __description__ = '\nThis package implements tools to build python p...mport doctest\n\n doctest.testmod(verbose=True)\n' __license__ = 'GPL-3.0 License' __maintainer__ = 'Maurice Lambert' __maintainer_email__ = 'mauricelambert434@gmail.com' __url__ = 'https://github.com/mauricelambert/PythonToolsKit' |
Author | ||
Maurice Lambert |