Skip to content

Commit

Permalink
refactor: remove FrozenDict, return copy instead
Browse files Browse the repository at this point in the history
  • Loading branch information
Midnighter authored and hredestig committed May 7, 2017
1 parent 2a6b8cd commit 90c94a4
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 76 deletions.
3 changes: 1 addition & 2 deletions cobra/core/reaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
from cobra.util.context import resettable, get_context
from cobra.util.solver import (
linear_reaction_coefficients, set_objective, check_solver_status)
from cobra.util.util import FrozenDict

# precompiled regular expressions
# Matches and/or in a gene reaction rule
Expand Down Expand Up @@ -375,7 +374,7 @@ def reduced_cost(self):
# read-only
@property
def metabolites(self):
return FrozenDict(self._metabolites)
return self._metabolites.copy()

@property
def genes(self):
Expand Down
31 changes: 1 addition & 30 deletions cobra/test/test_util.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function
from __future__ import absolute_import

import re
from copy import copy, deepcopy
Expand All @@ -9,17 +9,6 @@
from six.moves import range

from cobra import DictList, Object
from cobra.util import FrozenDict


@pytest.fixture(scope="session")
def seed():
return 1234


@pytest.fixture(scope="session")
def frozen_dict():
return FrozenDict({"A": 1, "B": 2, "C": 3, "D": 4, "E": [2, 3, 4, 5]})


@pytest.fixture(scope="function")
Expand Down Expand Up @@ -304,21 +293,3 @@ def test_union(self, dict_list):
# should only add 1 element
assert len(test_list) == 2
assert test_list.index("test2") == 1


class FrozenDictTestCase:
def test_frozen_attributes(self, frozen_dict):
with pytest.raises(AttributeError):
frozen_dict.popitem()
with pytest.raises(AttributeError):
frozen_dict.pop("A")
with pytest.raises(AttributeError):
frozen_dict.__setitem__("C", 1)
with pytest.raises(AttributeError):
frozen_dict.setdefault("K")
with pytest.raises(AttributeError):
frozen_dict.__delitem__("A")
with pytest.raises(AttributeError):
frozen_dict.update()

assert hasattr(frozen_dict, "__hash__")
44 changes: 0 additions & 44 deletions cobra/util/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,6 @@

from __future__ import absolute_import

import sympy
import logging
import operator
from collections import Mapping

from six import iteritems


class FrozenDict(Mapping):
def __init__(self, *args, **kwargs):
super(FrozenDict, self).__init__()
self.__dict = dict(*args, **kwargs)
self.__hash = None
self.__items = None

def __getitem__(self, item):
return self.__dict[item]

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

def __len__(self):
return len(self.__dict)

def __hash__(self):
if self.__items is None:
self.__items = sorted(iteritems(self.__dict))
if self.__hash is None:
self.__hash = hash(self.__items)
return self.__hash

def __repr__(self):
if self.__items is None:
self.__items = sorted(iteritems(self.__dict))
return '{}({!r})'.format(self.__class__.__name__, self.__items)

def copy(self, *args, **kwargs):
new_dict = self.__dict.copy()

if args or kwargs:
new_dict.update(dict(*args, **kwargs))

return self.__class__(new_dict)


class AutoVivification(dict):
"""Implementation of perl's autovivification feature. Checkout
Expand Down

0 comments on commit 90c94a4

Please sign in to comment.