From 67d3af189d061160be37a96e44e3f1fafdc36ec6 Mon Sep 17 00:00:00 2001 From: HemantYadav Date: Thu, 27 Aug 2020 17:02:55 +0530 Subject: [PATCH] fixed imports --- src/cobra/core/metadata/__init__.py | 2 +- src/cobra/core/metadata/history.py | 54 ++++++++++--------- src/cobra/core/metadata/keyvaluepairs.py | 16 ++++-- src/cobra/core/metadata/metadata.py | 6 +-- src/cobra/core/metadata/notes.py | 3 +- .../test_core/test_metadata/test_history.py | 5 +- .../test_metadata/test_keyvaluepair.py | 16 +++--- 7 files changed, 59 insertions(+), 43 deletions(-) diff --git a/src/cobra/core/metadata/__init__.py b/src/cobra/core/metadata/__init__.py index 9ee60e8ad..27fdea1e4 100644 --- a/src/cobra/core/metadata/__init__.py +++ b/src/cobra/core/metadata/__init__.py @@ -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 diff --git a/src/cobra/core/metadata/history.py b/src/cobra/core/metadata/history.py index b84f5b469..f0e817646 100644 --- a/src/cobra/core/metadata/history.py +++ b/src/cobra/core/metadata/history.py @@ -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: @@ -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 @@ -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] @@ -49,7 +50,7 @@ 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 @@ -57,14 +58,13 @@ 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() @@ -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. @@ -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()) @@ -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() @@ -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()) @@ -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 @@ -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") @@ -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: @@ -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 @@ -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: diff --git a/src/cobra/core/metadata/keyvaluepairs.py b/src/cobra/core/metadata/keyvaluepairs.py index b6faae808..02fcd3fc8 100644 --- a/src/cobra/core/metadata/keyvaluepairs.py +++ b/src/cobra/core/metadata/keyvaluepairs.py @@ -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: @@ -16,8 +16,15 @@ 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 @@ -25,7 +32,7 @@ def __init__(self, id: str = None, name: str = None, 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() @@ -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: diff --git a/src/cobra/core/metadata/metadata.py b/src/cobra/core/metadata/metadata.py index f43edd6df..840943522 100644 --- a/src/cobra/core/metadata/metadata.py +++ b/src/cobra/core/metadata/metadata.py @@ -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 @@ -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 diff --git a/src/cobra/core/metadata/notes.py b/src/cobra/core/metadata/notes.py index 628162c1e..8418e86e7 100644 --- a/src/cobra/core/metadata/notes.py +++ b/src/cobra/core/metadata/notes.py @@ -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 "

key : value

" type string PATTERN_PTAG = re.compile( r"<(?P(\w+:)?)p[^>]*>(?P.*?)", @@ -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 diff --git a/src/cobra/test/test_core/test_metadata/test_history.py b/src/cobra/test/test_core/test_metadata/test_history.py index e7bb5572b..9fcbc03e4 100644 --- a/src/cobra/test/test_core/test_metadata/test_history.py +++ b/src/cobra/test/test_core/test_metadata/test_history.py @@ -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): diff --git a/src/cobra/test/test_core/test_metadata/test_keyvaluepair.py b/src/cobra/test/test_core/test_metadata/test_keyvaluepair.py index 90c540d92..7a4dddb9a 100644 --- a/src/cobra/test/test_core/test_metadata/test_keyvaluepair.py +++ b/src/cobra/test/test_core/test_metadata/test_keyvaluepair.py @@ -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)