Skip to content

Commit

Permalink
improve addition for snowpack and atmosphere. The order of the additi…
Browse files Browse the repository at this point in the history
…on is important and checked more carefully.
  • Loading branch information
ghislainp committed Jun 1, 2020
1 parent e4aa322 commit 2d69e21
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
23 changes: 22 additions & 1 deletion smrt/core/atmosphere.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,29 @@


from .error import SMRTError
from .snowpack import Snowpack


# this is a temporary solution to deal with Atmosphere.
# user should not rely on this object. For temporary internal use only !!

class AtmosphereBase(object):
# has no special properties yet, we just use the type.
pass

def __add__(self, other):
"""Return a new snowpack made by setting the atmosphere
:param other: the snowpack to add.
"""

if not isinstance(other, Snowpack):
raise SMRTError("Attempt to add an incorrect object to an atmopsher. Only adding an atmosphere and a snowpack (in that order)"
" is a valid operation.")

return Snowpack(layers=other.layers,
interfaces=other.interfaces,
substrate=other.substrate,
atmosphere=self)

def __iadd__(self, other):
raise SMRTError("Inplace addition with an atmosphere is not a valid operation.")
18 changes: 7 additions & 11 deletions smrt/core/snowpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
from ..interface.flat import Flat # core should not depend on something defined in interface...
from .layer import Layer
from .interface import SubstrateBase
from .atmosphere import AtmosphereBase


class Snowpack(object):
Expand Down Expand Up @@ -117,12 +116,15 @@ def basic_check(self):

def check_addition_validity(self, other):

# import here to avoid circular reference
from .atmosphere import AtmosphereBase

if isinstance(other, SubstrateBase):
if self.substrate is not None:
raise SMRTError("Adding a substrate to a snowpack that already has one is not valid. Unset the substrate first")
raise SMRTError("Adding a substrate to a snowpack that already has a substrate set is not valid."
" Unset the substrate first.")
elif isinstance(other, AtmosphereBase):
if self.atmosphere is not None:
raise SMRTError("Adding an atmosphere to a snowpack that already has one is not valid. Unset the atmosphere first")
raise SMRTError("Adding an atmosphere to a snowpack is not allowed. Add an atmosphere and a snowpack.")
elif not (hasattr(other, "layers") and hasattr(other, "interfaces") and hasattr(other, "substrate")):
raise SMRTError("Addition of snowpacks requires two instances of class Snowpack or equivalent compatible objects")

Expand Down Expand Up @@ -150,11 +152,6 @@ def __add__(self, other):
interfaces=self.interfaces,
atmosphere=self.atmosphere,
substrate=other)
if isinstance(other, AtmosphereBase):
return Snowpack(layers=self.layers,
interfaces=self.interfaces,
substrate=self.substrate,
atmosphere=other)
else:
return Snowpack(layers=self.layers + other.layers,
interfaces=self.interfaces + other.interfaces,
Expand All @@ -167,6 +164,7 @@ def __radd__(self, other):
return self
else:
# should never be called
raise SMRTError("The addition operator is not commutative for snowpacks")
return other.__add__(self)

def __iadd__(self, other): # just for optimization
Expand All @@ -177,8 +175,6 @@ def __iadd__(self, other): # just for optimization

if isinstance(other, SubstrateBase):
self.substrate = other
elif isinstance(other, AtmosphereBase):
self.atmosphere = other
else:
self.layers += other.layers
self.interfaces += other.interfaces
Expand Down

0 comments on commit 2d69e21

Please sign in to comment.