Skip to content

Commit

Permalink
Code satisfies flake8-docstring syntax (#37)
Browse files Browse the repository at this point in the history
* D102

* other flake8 issues
  • Loading branch information
samuelduchesne authored Feb 12, 2021
1 parent 5d70886 commit 7a5c6d9
Show file tree
Hide file tree
Showing 19 changed files with 71 additions and 43 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ description-file = README.md
max-line-length = 88
extend-ignore =
# See https://github.com/PyCQA/pycodestyle/issues/373
E203,
E203, E501
docstring-convention=google
exclude =
.git,
Expand Down
6 changes: 6 additions & 0 deletions trnsystor/anchorpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def studio_anchor(self, other, loc):
return u_loc, v_loc

def find_best_anchors(self, other):
"""Find best anchor points to connect self and other."""
dist = {}
for u in self.anchor_points.values():
for v in other.anchor_points.values():
Expand All @@ -54,15 +55,18 @@ def find_best_anchors(self, other):

@property
def anchor_points(self):
"""Return dict of anchor points str->tuple."""
return self.get_octo_pts_dict(self.offset)

@property
def reverse_anchor_points(self):
"""Return dict of anchor points tuple->str."""
pts = self.get_octo_pts_dict(self.offset)
return {(pt.x, pt.y): key for key, pt in pts.items()}

@property
def studio_anchor_mapping(self):
"""Return dict of anchor mapping str->tuple."""
from shapely.affinity import translate

p_ = {}
Expand All @@ -81,6 +85,7 @@ def studio_anchor_mapping(self):

@property
def studio_anchor_reverse_mapping(self):
"""Return dict of anchor mapping tuple->str."""
return {
(0, 0): "top-left",
(20, 0): "top-center",
Expand Down Expand Up @@ -129,4 +134,5 @@ def get_octo_pts_dict(self, offset=10):

@property
def centroid(self):
"""Return centroid of self."""
return self.model.studio.position
2 changes: 2 additions & 0 deletions trnsystor/canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def bbox(self):

@property
def grid_is_valid(self):
"""Return True if grid is valid."""
if self._grid_valid:
return True
else:
Expand All @@ -44,6 +45,7 @@ def grid(self):
return self._grid

def invalidate_grid(self):
"""Invalidate grid."""
self._grid_valid = False

def resize_canvas(self, width, height):
Expand Down
7 changes: 7 additions & 0 deletions trnsystor/collections/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,15 @@ class ComponentCollection(collections.UserList):

@property
def iloc(self):
"""Access a component by its :attr:`unit_number`."""
return dict({item.unit_number: item for item in self.data})

@property
def loc(self):
"""Access a components by its identify (self).
Examples:
>>> cc = ComponentCollection([tank_type])
>>> assert cc.loc[tank_type] == cc.iloc[tank_type.unit_number]
"""
return dict({item: item for item in self.data})
12 changes: 10 additions & 2 deletions trnsystor/collections/constant.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ def __getitem__(self, key):
"""Get item."""
if isinstance(key, int):
value = list(self.data.values())[key]
elif isinstance(key, slice):
value = list(self.data.values()).__getitem__(key)
else:
value = super().__getitem__(key)
return value
Expand Down Expand Up @@ -103,11 +105,17 @@ def update(self, E=None, **F):
super(ConstantCollection, self).update(_e)

@property
def size(self):
def size(self) -> int:
"""Return len(self)."""
return len(self)

@property
def unit_number(self):
def unit_number(self) -> int:
"""Return the unit_number of self. Negative by design.
Hint:
Only :class:`TrnsysModel` objects have a positive unit_number.
"""
return self._unit * -1

def _to_deck(self):
Expand Down
11 changes: 6 additions & 5 deletions trnsystor/collections/equation.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,18 @@ def update(self, E=None, **F):
_e.update(_f)
super(EquationCollection, self).update(_e)

def setdefault(self, key, value=None):
if key not in self:
self[key] = value
return self[key]

@property
def size(self):
"""Return len(self)."""
return len(self)

@property
def unit_number(self):
"""Return the unit_number of self. Negative by design.
Hint:
Only :class:`TrnsysModel` objects have a positive unit_number.
"""
return self._unit * -1

@property
Expand Down
4 changes: 2 additions & 2 deletions trnsystor/collections/externalfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from path import Path

from trnsystor.externalfile import ExternalFile
from trnsystor.utils import standerdized_name
from trnsystor.utils import standardize_name


class ExternalFileCollection(collections.UserDict):
Expand Down Expand Up @@ -52,7 +52,7 @@ def from_dict(cls, dictionary):
"""
item = cls()
for key in dictionary:
named_key = standerdized_name(dictionary[key].question)
named_key = standardize_name(dictionary[key].question)
item.__setitem__(named_key, dictionary[key])
return item

Expand Down
8 changes: 6 additions & 2 deletions trnsystor/collections/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from trnsystor.statement import Constant, Equation
from trnsystor.typevariable import TypeVariable
from trnsystor.utils import _parse_value, standerdized_name
from trnsystor.utils import _parse_value, standardize_name


class VariableCollection(collections.UserDict):
Expand Down Expand Up @@ -70,9 +70,13 @@ def _to_deck(self):

@classmethod
def from_dict(cls, dictionary):
"""Return VariableCollection from dict.
Sets also the class attribute using ``named_key``.
"""
item = cls()
for key in dictionary:
named_key = standerdized_name(dictionary[key].name)
named_key = standardize_name(dictionary[key].name)
item.__setitem__(named_key, dictionary[key])
setattr(item, named_key, dictionary[key])
return item
Expand Down
2 changes: 2 additions & 0 deletions trnsystor/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,12 @@ def __eq__(self, other):
return self.unit_number == other

def copy(self):
"""Return copy of self."""
pass

@property
def link_styles(self):
"""Return :class:`LinkStyles` of self."""
return [
data["LinkStyle"]
for u, v, key, data in self.UNIT_GRAPH.edges(keys=True, data=True)
Expand Down
1 change: 1 addition & 0 deletions trnsystor/controlcards.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,5 @@ def _to_deck(self):
return version + head + statements

def set_statement(self, statement):
"""Set `statement`."""
self.__setattr__(statement.__class__.__name__.lower(), statement)
7 changes: 4 additions & 3 deletions trnsystor/deck.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ def __str__(self):

@property
def graph(self):
"""Return the :class:`MultiDiGraph` of self."""
import networkx as nx

G = nx.MultiDiGraph()
Expand Down Expand Up @@ -247,6 +248,7 @@ def update_models(self, amodel):
self.models.append(amodel)

def remove_models(self, amodel):
"""Remove `amodel` from self.models."""
if isinstance(amodel, Component):
amodel = [amodel]
for amodel in amodel:
Expand All @@ -261,18 +263,17 @@ def to_file(self, path_or_buf, encoding=None, mode="w"):
"""Save the Deck object to file.
Examples:
>>> from trnsystor.deck import Deck
>>> deck = Deck("Unnamed")
>>> deck.to_file("my_project.dck",None,"w")
Args:
encoding:
mode:
path_or_buf (Union[str, Path, IO[AnyStr]]): str or file handle, default None
File path or object, if None is provided the result is returned as
a string. If a file object is passed it should be opened with
`newline=''`, disabling universal newlines.
encoding (str or None): Encoding to use.
mode (str): Mode to open path_or_buf with.
"""
self.check_deck_integrity()

Expand Down
4 changes: 4 additions & 0 deletions trnsystor/linkstyle.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@


class LinkStyle(object):
"""LinkStyle class."""

def __init__(
self,
u,
Expand Down Expand Up @@ -57,6 +59,7 @@ def __init__(

@property
def path(self):
"""Return the path of self."""
if self._path is None:
u_anchor_name, v_anchor_name = self.anchor_ids
_u = AnchorPoint(self.u).anchor_points[u_anchor_name]
Expand All @@ -74,6 +77,7 @@ def path(self, value):

@property
def anchor_ids(self):
"""Return studio anchor ids."""
if isinstance(self.loc, tuple):
loc_u, loc_v = self.loc
else:
Expand Down
1 change: 1 addition & 0 deletions trnsystor/statement/equation.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ def idx(self):

@property
def unit_number(self):
"""Return the unit number of the EquationCollection self belongs to."""
return self.model.unit_number

def _to_deck(self):
Expand Down
1 change: 1 addition & 0 deletions trnsystor/statement/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def __init__(self, v=(18, 0)):

@classmethod
def from_string(cls, string):
"""Create Version statement from str version number. eg. 18.0."""
return cls(tuple(map(int, string.split("."))))

def _to_deck(self):
Expand Down
14 changes: 2 additions & 12 deletions trnsystor/studio.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,13 @@ def __str__(self):

@classmethod
def from_component(cls, model):
"""Create object from :class:`TrnsysModel`."""
position = Point(50, 50)
layer = ["Main"]
return cls(model.name, model.model, position, layer)

def _to_deck(self):
"""Return deck representation of self.
Examples:
>>>
*$UNIT_NAME Boulder, CO
*$MODEL ./Weather Data Reading and Processing/StandardFormat/TMY2/Type15-2.tmf
*$POSITION 69 182
*$LAYER Main #
Returns:
(str): The string representation of the StudioHeader.
"""
"""Return deck representation of self."""
unit_name = "*$UNIT_NAME {}".format(self.unit_name)
model = "*$MODEL {}".format(self.model.expand())
position = "*$POSITION {} {}".format(self.position.x, self.position.y)
Expand Down
13 changes: 3 additions & 10 deletions trnsystor/trnsysmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ def anchor_points(self) -> dict:

@property
def reverse_anchor_points(self):
"""Reverse anchor points."""
return AnchorPoint(self).reverse_anchor_points

@classmethod
Expand Down Expand Up @@ -596,6 +597,7 @@ def _to_deck(self):
)

def update_meta(self, new_meta):
"""Update self with new :class:`MetaData`."""
for attr in self._meta.__dict__:
if hasattr(new_meta, attr):
setattr(self._meta, attr, getattr(new_meta, attr))
Expand Down Expand Up @@ -631,6 +633,7 @@ def update_meta(self, new_meta):
)

def plot(self):
"""Plot the model."""
import matplotlib.pyplot as plt

G = nx.DiGraph()
Expand All @@ -654,13 +657,3 @@ def plot(self):
)
plt.show()
return ax


class Trace:
# Todo: Implement Trace
pass


class Format:
# Todo: Implement Format
pass
2 changes: 2 additions & 0 deletions trnsystor/typecycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def __repr__(self):

@property
def default(self):
"""Return the default value of self."""
return int(self.minSize)

@property
Expand All @@ -74,6 +75,7 @@ def idxs(self):

@property
def is_question(self):
"""Return True if self is a question."""
return (
any(cycle.question is not None for cycle in self.cycles)
if self.cycles
Expand Down
12 changes: 7 additions & 5 deletions trnsystor/typevariable.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from bs4 import Tag

from trnsystor.linkstyle import LinkStyle
from trnsystor.utils import _parse_value, parse_type, standerdized_name
from trnsystor.utils import _parse_value, parse_type, standardize_name


class TypeVariable(object):
Expand Down Expand Up @@ -205,10 +205,10 @@ def predecessor(self):

@property
def idx(self):
"""The 0-based index of the TypeVariable."""
"""Get the 0-based variable index of self."""
ordered_dict = collections.OrderedDict(
(
standerdized_name(self.model._meta.variables[attr].name),
standardize_name(self.model._meta.variables[attr].name),
[self.model._meta.variables[attr], 0],
)
for attr in sorted(
Expand All @@ -228,10 +228,11 @@ def idx(self):
value[1] = i
i += 1

return ordered_dict[standerdized_name(self.name)][1]
return ordered_dict[standardize_name(self.name)][1]

@property
def one_based_idx(self):
"""Get the 1-based variable index of self such as it appears in Trnsys."""
return self.idx + 1

def connect_to(self, other, link_style_kwargs=None):
Expand Down Expand Up @@ -327,7 +328,8 @@ def __init__(self, val, **kwargs):
self._parse_types()

@property
def is_connected(self):
def is_connected(self) -> bool:
"""Return True of self has any successor."""
return len(self.successors) > 0

@property
Expand Down
Loading

0 comments on commit 7a5c6d9

Please sign in to comment.