Skip to content

Commit

Permalink
Merge 63c5b8e into 09f30f0
Browse files Browse the repository at this point in the history
  • Loading branch information
rooterkyberian committed Sep 21, 2017
2 parents 09f30f0 + 63c5b8e commit 3034cfd
Show file tree
Hide file tree
Showing 12 changed files with 17 additions and 513 deletions.
2 changes: 1 addition & 1 deletion docs/basics/install.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ The latest development version can be obtained via git::

$ pip install git+https://github.com/schematics/schematics.git#egg=schematics

Schematics currently supports Python versions 2.6, 2.7, 3.3, and 3.4.
Schematics currently supports Python versions 2.7, 3.3, 3.4, 3.5 and 3.6.


.. _install_dependencies:
Expand Down
146 changes: 0 additions & 146 deletions schematics/datastructures.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,152 +12,6 @@



class OrderedDict(MutableMapping, dict):
"""
An ordered dictionary.
The implementation is based on ``collections.OrderedDict`` of the standard library.
It preserves the original technique of storing the keys as a regular list, whereas
the reference implementation now uses a linked list. The built-in list gives better
performance in use cases that are typical with Schematics.
"""

def __init__(*args, **kwargs):
if not args:
raise TypeError("OrderedDict.__init__() needs an instance as the first argument")
self = args[0]
args = args[1:]
if len(args) > 1:
raise TypeError("OrderedDict() takes at most 1 positional argument, got %d" % len(args))
dict.__init__(self)
if not self:
self._keys = []
MutableMapping.update(self, *args, **kwargs)

__contains__ = dict.__contains__
__getitem__ = dict.__getitem__
__len__ = dict.__len__

get = dict.get

def __setitem__(self, key, item, setitem=dict.__setitem__):
if key not in self:
self._keys.append(key)
setitem(self, key, item)

def __delitem__(self, key, delitem=dict.__delitem__):
delitem(self, key)
self._keys.remove(key)

def __iter__(self):
return iter(self._keys)

def __reversed__(self):
return reversed(self._keys)

def clear(self):
del self._keys[:]
dict.clear(self)

def copy(self):
return self.__class__(self)

__copy__ = copy

def move_to_end(self, key, last=True):
if key not in self:
raise KeyError(key)
self._keys.remove(key)
if last:
self._keys.append(key)
else:
self._keys.insert(0, key)

__token = object()

def pop(self, key, default=__token):
if key in self:
self._keys.remove(key)
return dict.pop(self, key)
elif default is self.__token:
raise KeyError(key)
else:
return default

def popitem(self, last=True):
if not self:
raise KeyError('dictionary is empty')
key = self._keys.pop(-1 if last else 0)
value = dict.pop(self, key)
return key, value

def setdefault(self, key, default=None):
if key in self:
return self[key]
else:
self[key] = default
return default

def sort(self, key=None, reverse=False):
if key is not None:
_key = lambda k: key((k, self[k]))
else:
_key = None
self._keys.sort(key=_key, reverse=reverse)

def reverse(self):
self._keys.reverse()

@classmethod
def fromkeys(cls, iterable, value=None):
return cls((key, value) for key in iterable)

def __eq__(self, other):
if isinstance(other, OrderedDict):
return dict.__eq__(self, other) and all(map(eq, self, other))
else:
return dict.__eq__(self, other)

def __ne__(self, other):
return not self == other

def __reduce_ex__(self, protocol=0):
attrs = vars(self).copy()
for k in vars(self.__class__()):
attrs.pop(k, None)
if protocol <= 2:
# Express tuples as lists to enable proper PyYAML serialization.
items = [[k, self[k]] for k in self]
return (self.__class__, (items,), attrs or None)
else:
# Provide items as an iterator. This variant can handle recursive dictionaries.
return (self.__class__, (), attrs or None, None, iter(self.items()))

__reduce__ = __reduce_ex__

def __repr__(self, memo=set()):
call_key = (id(self), get_ident())
if call_key in memo:
return '...'
else:
memo.add(call_key)
try:
return '%s(%s)' % (self.__class__.__name__, repr(list(self.items())) if self else '')
finally:
memo.remove(call_key)

if PY3:

def keys(self):
return _ODKeysView(self)

def values(self):
return _ODValuesView(self)

def items(self):
return _ODItemsView(self)


class _ODKeysView(KeysView):
def __reversed__(self):
for key in reversed(self._mapping):
Expand Down
3 changes: 2 additions & 1 deletion schematics/deprecated.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

from collections import OrderedDict

from .compat import iteritems
from .datastructures import OrderedDict
from .types.serializable import Serializable
from . import transforms

Expand Down
8 changes: 6 additions & 2 deletions schematics/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

from copy import deepcopy
import inspect
from collections import OrderedDict
from types import FunctionType

from .common import * # pylint: disable=redefined-builtin
from .compat import str_compat, repr_compat, _dict
from .datastructures import OrderedDict, Context, ChainMap, MappingProxyType
from .datastructures import Context, ChainMap, MappingProxyType
from .exceptions import *
from .iteration import atoms
from .transforms import (
Expand Down Expand Up @@ -97,7 +98,10 @@ def __new__(mcs, name, bases, attrs):
fields[key] = value

# Convert declared fields into descriptors for new class
fields.sort(key=lambda i: i[1]._position_hint)
fields = OrderedDict(sorted(
(kv for kv in fields.items()),
key=lambda i: i[1]._position_hint,
))
for key, field in iteritems(fields):
if isinstance(field, BaseType):
attrs[key] = FieldDescriptor(key)
Expand Down
3 changes: 2 additions & 1 deletion schematics/schema.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@

from collections import OrderedDict

from .compat import itervalues
from .common import DEFAULT, NONEMPTY
from .datastructures import OrderedDict
from .types import BaseType
from .types.serializable import Serializable

Expand Down
6 changes: 1 addition & 5 deletions schematics/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import itertools
import types
from collections import OrderedDict

from .common import * # pylint: disable=redefined-builtin
from .datastructures import Context
Expand All @@ -13,11 +14,6 @@
from .iteration import atoms, atom_filter
from .role import Role

try:
from collections import OrderedDict
except ImportError:
from .datastructures import OrderedDict


###
# Transform loops
Expand Down
3 changes: 1 addition & 2 deletions schematics/types/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@
import re
import string
import uuid
from collections import Iterable
from collections import Iterable, OrderedDict

from ..common import * # pylint: disable=redefined-builtin
from ..compat import string_type
from ..datastructures import OrderedDict
from ..exceptions import *
from ..translator import _
from ..undefined import Undefined
Expand Down
2 changes: 1 addition & 1 deletion schematics/types/union.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import inspect
from collections import OrderedDict

from ..common import *
from ..datastructures import OrderedDict
from ..exceptions import ConversionError
from ..translator import _
from ..transforms import get_import_context, get_export_context
Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
'License :: OSI Approved :: BSD License',
'Programming Language :: Python',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.2',
Expand Down
3 changes: 2 additions & 1 deletion tests/test_list_type.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from collections import OrderedDict

import pytest

from schematics.datastructures import OrderedDict
from schematics.models import Model
from schematics.types import IntType, StringType
from schematics.types.compound import ModelType, ListType
Expand Down

0 comments on commit 3034cfd

Please sign in to comment.