Backporting of the OrderedDict
class from the collection
package that can be used in legacy Python 2 scripts (tested on Python 2.1).
Quoting from RealPython's tutorial:
Python’s
OrderedDict
is adict
subclass that preserves the order in which key-value pairs, commonly known as items, are inserted into the dictionary.
- When you iterate over an OrderedDict object, items are traversed in the original order.
- If you update the value of an existing key, then the order remains unchanged.
- If you remove an item and reinsert it, then the item is added at the end of the dictionary.
Since OrderedDict
is a dict
subclass. we need to implement all dictionary methods:
-
.clear()
-
.copy()
-
.get(key, default)
-
.update(object)
-
.setdefault(key, default)
-
.pop(key, default)
-
.popitem()
-
.move_to_end(key, last)
-
.fromkeys(keylist, value)
The original version was uploaded by @Amina Bouabdallah on May 26, 2012 (11 years ago at the moment of writing).
I just blew the dust off the repo, adjusted the code to work on Jython 2.1 and added some features.
-
Clone the repository.
-
Install all the development dependencies with:
poetry install --with dev
-
Experiment
-
Create and run tests
Tests ensure that changes to the codebase do not break existing features and that anything works as expected.
Please be sure to...
- ... keep existing tests up-to-date with the latest changes;
- ... write relative tests when adding new features.
When writing tests please keep the in mind that:
- test files MUST be written for
unittest
; - test cases names MUST be a descriptive name written in PascalCase and end with
TestCase
(e.g.class MyTestCase(unittest.TestCase):
); - test names MUST be a descriptive name written in snake_case (all lowercase, with words separated with an underscore) and start with
test_
(e.g.def test_feature(self):
); - MAY use the
setUp()
andteardown()
methods to define instructions that will be executed before and after each test method; - each test MUST contain at least one
self.assert*
method call (we don't want empty no-op tests);
The following is an example of agood test from the official Python documentation:
import unittest
class WidgetTestCase(unittest.TestCase):
def setUp(self):
self.widget = Widget('The widget')
def tearDown(self):
self.widget.dispose()
def test_default_widget_size(self):
self.assertEqual(
self.widget.size(),
(50,50),
'incorrect default size'
)
def test_widget_resize(self):
self.widget.resize(100,150)
self.assertEqual(
self.widget.size(),
(100,150),
'wrong size after resize'
)