Skip to content

Commit

Permalink
fixed imports
Browse files Browse the repository at this point in the history
  • Loading branch information
Hemant27031999 committed Aug 27, 2020
1 parent c8b850a commit 67d3af1
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/cobra/core/metadata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
ExternalResources,
Qualifier,
)
from cobra.core.metadata.history import Creator, HistoryDatetime, History
from cobra.core.metadata.history import Creator, History, HistoryDatetime
from cobra.core.metadata.keyvaluepairs import KeyValuePairs
from cobra.core.metadata.metadata import MetaData
from cobra.core.metadata.notes import Notes
54 changes: 29 additions & 25 deletions src/cobra/core/metadata/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
model objects. The history allows to encode who created or modified
objects in a model with respective time stamps.
"""
from typing import Iterable, Dict, List, Union
from datetime import datetime
from typing import Dict, Iterable, List, Union


class History:
Expand All @@ -19,11 +19,12 @@ class History:
modified_dates : list
A list of datetimes when the object was modified.
"""

def __init__(
self,
creators: List['Creator'] = None,
created_date: 'HistoryDatetime' = None,
modified_dates: List['HistoryDatetime'] = None,
creators: List["Creator"] = None,
created_date: "HistoryDatetime" = None,
modified_dates: List["HistoryDatetime"] = None,
):
self._creators = list()
self._created_date = None
Expand All @@ -39,7 +40,7 @@ def creators(self) -> List:
return self._creators

@creators.setter
def creators(self, values: Iterable['Creator']) -> None:
def creators(self, values: Iterable["Creator"]) -> None:
self._creators = list()
if values:
self._creators = [Creator.from_data(v) for v in values]
Expand All @@ -49,22 +50,21 @@ def created_date(self) -> "HistoryDatetime":
return self._created_date

@created_date.setter
def created_date(self, date: Union[str, 'HistoryDateTime']) -> None:
def created_date(self, date: Union[str, "HistoryDateTime"]) -> None:
self._created_date = HistoryDatetime(date)

@property
def modified_dates(self) -> List:
return self._modified_dates

@modified_dates.setter
def modified_dates(self, dates: Iterable[
Union[str, 'HistoryDateTime']]) -> None:
def modified_dates(self, dates: Iterable[Union[str, "HistoryDateTime"]]) -> None:
self._modified_dates = list()
if dates:
self._modified_dates = [HistoryDatetime(d) for d in dates]

@staticmethod
def from_data(data: Union[Dict, 'History']) -> 'History':
def from_data(data: Union[Dict, "History"]) -> "History":
"""Parse history from data."""
if data is None:
return History()
Expand All @@ -88,7 +88,7 @@ def is_empty(self) -> bool:
return False
return True

def __eq__(self, history: 'History') -> bool:
def __eq__(self, history: "History") -> bool:
""" Checking equality of two history objects.
A history is equal if all attributes are equal.
Expand Down Expand Up @@ -119,10 +119,10 @@ def to_dict(self):
for modified_date in self._modified_dates:
modified_dates.append(modified_date.datetime)
return {
"creators": [c.to_dict() for c in self.creators],
"created_date": self.created_date.datetime,
"modified_dates": modified_dates
}
"creators": [c.to_dict() for c in self.creators],
"created_date": self.created_date.datetime,
"modified_dates": modified_dates,
}

def __str__(self) -> str:
return str(self.to_dict())
Expand Down Expand Up @@ -155,7 +155,7 @@ def __init__(
self.organization_name = organization_name # type: str

@staticmethod
def from_data(data: Union[Dict, 'Creator']) -> 'Creator':
def from_data(data: Union[Dict, "Creator"]) -> "Creator":
"""Parse creator from data."""
if not data:
return Creator()
Expand Down Expand Up @@ -183,11 +183,11 @@ def __eq__(self, creator_obj: "Creator") -> bool:

def to_dict(self):
return {
"first_name": self.first_name,
"last_name": self.last_name,
"email": self.email,
"organization_name": self.organization_name,
}
"first_name": self.first_name,
"last_name": self.last_name,
"email": self.email,
"organization_name": self.organization_name,
}

def __str__(self) -> str:
return str(self.to_dict())
Expand All @@ -207,6 +207,7 @@ class HistoryDatetime:
datetime: str, datetime
date in the form of a string
"""

def __init__(self, history_datetime: str = None):
self._datetime = None # type: str
self.datetime = history_datetime
Expand Down Expand Up @@ -237,7 +238,7 @@ def parse_datetime(self, value: str) -> str:
)

@staticmethod
def utcnow() -> 'HistoryDatetime':
def utcnow() -> "HistoryDatetime":
"""HistoryDatetime with current UTC time."""
utcnow = datetime.utcnow()
value = utcnow.strftime("%Y-%m-%dT%H:%M:%S%z")
Expand All @@ -250,8 +251,9 @@ def validate_datetime(datetime_str: str) -> bool:
Raises ValueError if not valid.
"""
if not isinstance(datetime_str, str):
raise TypeError(f"The date passed must be of "
f"type string: {datetime_str}")
raise TypeError(
f"The date passed must be of " f"type string: {datetime_str}"
)

# python 3.6 doesn't allow : (colon) in the utc offset.
try:
Expand All @@ -260,7 +262,9 @@ def validate_datetime(datetime_str: str) -> bool:
# checking for python 3.6
if "Z" in datetime_str:
try:
datetime.strptime(datetime_str.replace("Z", ""), "%Y-%m-%dT%H:%M:%S")
datetime.strptime(
datetime_str.replace("Z", ""), "%Y-%m-%dT%H:%M:%S"
)
except ValueError as e1:
raise ValueError(str(e1))
return False
Expand All @@ -277,7 +281,7 @@ def validate_datetime(datetime_str: str) -> bool:

return True

def __eq__(self, history_datetime: 'HistoryDatetime') -> bool:
def __eq__(self, history_datetime: "HistoryDatetime") -> bool:
return self.datetime == history_datetime.datetime

def __str__(self) -> str:
Expand Down
16 changes: 12 additions & 4 deletions src/cobra/core/metadata/keyvaluepairs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from collections import OrderedDict
from typing import Dict, Union, Iterable
from collections.abc import MutableMapping
from typing import Dict, Iterable, Union


class KeyValueEntry:
Expand All @@ -16,16 +16,23 @@ class KeyValueEntry:
value : str
uri : str
"""
def __init__(self, id: str = None, name: str = None,
key: str = None, value: str = None, uri: str = None):

def __init__(
self,
id: str = None,
name: str = None,
key: str = None,
value: str = None,
uri: str = None,
):
self.id = id
self.name = name
self.key = key
self.value = value
self.uri = uri

@staticmethod
def from_data(data: Union[Dict, 'KeyValueEntry']) -> 'KeyValueEntry':
def from_data(data: Union[Dict, "KeyValueEntry"]) -> "KeyValueEntry":
""" Makes a KeyValueDict object using the data passed. """
if data is None:
return KeyValueEntry()
Expand Down Expand Up @@ -60,6 +67,7 @@ class KeyValuePairs(MutableMapping):
entries : Iterable
an iterable containing entry information
"""

def __init__(self, entries: Iterable[Union[Dict, KeyValueEntry]] = None):
self.mapping = OrderedDict() # type: OrderedDict[str, KeyValueEntry]
if entries:
Expand Down
6 changes: 3 additions & 3 deletions src/cobra/core/metadata/metadata.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from collections import OrderedDict, MutableMapping
from collections import MutableMapping, OrderedDict
from typing import Dict, Iterator, List, Union

from cobra.core.metadata.cvterm import CVTerms
from cobra.core.metadata.history import History, Creator, HistoryDatetime
from cobra.core.metadata.history import Creator, History, HistoryDatetime
from cobra.core.metadata.keyvaluepairs import KeyValuePairs


Expand Down Expand Up @@ -128,7 +128,7 @@ def to_dict(self) -> Dict:
return d

@staticmethod
def from_dict(data: Dict) -> 'MetaData':
def from_dict(data: Dict) -> "MetaData":
cvterms = data["cvterms"] if "cvterms" in data else None
history = data["history"] if "history" in data else None
keyValuepairs = data["keyvaluepairs"] if "keyvaluepairs" in data else None
Expand Down
3 changes: 2 additions & 1 deletion src/cobra/core/metadata/notes.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class Notes(collections.MutableMapping):
notes_xhtml : string
The complete notes (xhtml) data in the form of a string.
"""

# pattern checking for "<p> key : value </p>" type string
PATTERN_PTAG = re.compile(
r"<(?P<prefix>(\w+:)?)p[^>]*>(?P<content>.*?)</(?P=prefix)p>",
Expand Down Expand Up @@ -80,7 +81,7 @@ def notes_xhtml(self, value: str) -> None:
else:
raise TypeError(f"notes data must be of type string: {value}")

def __eq__(self, other: 'Notes') -> bool:
def __eq__(self, other: "Notes") -> bool:
if not isinstance(other, Notes):
return False
return self._notes_xhtml == other._notes_xhtml
Expand Down
5 changes: 3 additions & 2 deletions src/cobra/test/test_core/test_metadata/test_history.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import os
import pytest
from datetime import datetime

from cobra.io import read_sbml_model
import pytest

from cobra.core.metadata.history import Creator, History, HistoryDatetime
from cobra.io import read_sbml_model


def _read_ecoli_annotation_model(data_directory):
Expand Down
16 changes: 9 additions & 7 deletions src/cobra/test/test_core/test_metadata/test_keyvaluepair.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ def test_keyvaluepairs():
"value": "45",
"uri": "https://tinyurl.com/ybyr7b62",
}
entry2 = KeyValueEntry.from_data({
"id": "id2",
"name": "abc_xyz2",
"key": "key2",
"value": "48",
"uri": "https://tinyurl2.com/ybyr7b62",
})
entry2 = KeyValueEntry.from_data(
{
"id": "id2",
"name": "abc_xyz2",
"key": "key2",
"value": "48",
"uri": "https://tinyurl2.com/ybyr7b62",
}
)

kvp = KeyValuePairs(entries=[entry1, entry2])
print(kvp)
Expand Down

0 comments on commit 67d3af1

Please sign in to comment.