Skip to content

Backporting of the `OrderedDict` class to support legacy Python versions down to 2.1

License

Notifications You must be signed in to change notification settings

LukeSavefrogs/python21-OrderedDict

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Python version License

Run tests Open issues

OrderedDictionary

Backporting of the OrderedDict class from the collection package that can be used in legacy Python 2 scripts (tested on Python 2.1).

What is an OrderedDict?

Quoting from RealPython's tutorial:

Python’s OrderedDict is a dict 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.

Roadmap

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)

Special thanks

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.

Development

  1. Clone the repository.
  2. Install all the development dependencies with:
    poetry install --with dev
    
  3. Experiment
  4. Create and run tests

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.

Tests code style

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() and teardown() 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'
		)

About

Backporting of the `OrderedDict` class to support legacy Python versions down to 2.1

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Languages

  • Python 100.0%