Skip to content

Commit

Permalink
Add copy_shallow to all objects.
Browse files Browse the repository at this point in the history
  • Loading branch information
riga committed Feb 17, 2023
1 parent ead2d20 commit 376f08f
Show file tree
Hide file tree
Showing 13 changed files with 408 additions and 41 deletions.
38 changes: 35 additions & 3 deletions order/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
__all__ = ["Channel", "Category"]


from order.unique import UniqueObject, unique_tree
from order.unique import UniqueObject, UniqueObjectIndex, unique_tree
from order.mixins import CopyMixin, AuxDataMixin, TagMixin, SelectionMixin, LabelMixin
from order.util import to_root_latex

Expand Down Expand Up @@ -37,8 +37,15 @@ class Category(UniqueObject, CopyMixin, AuxDataMixin, TagMixin, SelectionMixin,
**Copy behavior**
``copy()``
The :py:attr:`channel` attribute is carried over as a reference, all remaining attributes are
copied. Note that the copied dataset is also registered in the channel.
copied. Note that the copied category is also registered in the channel.
``copy_shallow()``
All attributs are copied except for the :py:attr:`channel` and (child) :py:attr:`categories`
which are set to default values instead.
**Example**
Expand Down Expand Up @@ -137,7 +144,18 @@ class Category(UniqueObject, CopyMixin, AuxDataMixin, TagMixin, SelectionMixin,

# attributes for copying
copy_specs = (
[{"attr": "_channel", "ref": True}] +
[
{
"attr": "_channel",
"ref": True,
"skip_shallow": True,
},
{
"attr": "_categories",
"skip_shallow": True,
"skip_value": CopyMixin.Deferred(lambda: UniqueObjectIndex(cls=Category)),
},
] +
UniqueObject.copy_specs +
AuxDataMixin.copy_specs +
TagMixin.copy_specs +
Expand Down Expand Up @@ -243,8 +261,15 @@ class Channel(UniqueObject, CopyMixin, AuxDataMixin, TagMixin, LabelMixin):
**Copy behavior**
``copy()``
All attributes are copied.
``copy_shallow()``
All attributs are copied except for child :py:attr:`categories` which is set to a default value
instead.
**Example**
.. code-block:: python
Expand Down Expand Up @@ -286,6 +311,13 @@ class Channel(UniqueObject, CopyMixin, AuxDataMixin, TagMixin, LabelMixin):

# attributes for copying
copy_specs = (
[
{
"attr": "_categories",
"skip_shallow": True,
"skip_value": CopyMixin.Deferred(lambda: UniqueObjectIndex(cls=Category)),
},
] +
UniqueObject.copy_specs +
AuxDataMixin.copy_specs +
TagMixin.copy_specs +
Expand Down
66 changes: 63 additions & 3 deletions order/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
__all__ = ["Campaign", "Config"]


from order.unique import UniqueObject, unique_tree
from order.unique import UniqueObject, UniqueObjectIndex, unique_tree
from order.mixins import CopyMixin, AuxDataMixin, TagMixin
from order.shift import Shift
from order.dataset import Dataset
Expand All @@ -35,8 +35,15 @@ class Campaign(UniqueObject, CopyMixin, AuxDataMixin, TagMixin):
**Copy behavior**
``copy()``
All attributes are copied.
``copy_shallow()``
All attributs are copied except for contained :py:attr:`datasets` which are set to a default
value instead.
**Example**
.. code-block:: python
Expand Down Expand Up @@ -75,6 +82,13 @@ class Campaign(UniqueObject, CopyMixin, AuxDataMixin, TagMixin):
cls_name_plural = "campaigns"

copy_specs = (
[
{
"attr": "_datasets",
"skip_shallow": True,
"skip_value": CopyMixin.Deferred(lambda: UniqueObjectIndex(cls=Dataset)),
},
] +
UniqueObject.copy_specs +
AuxDataMixin.copy_specs +
TagMixin.copy_specs
Expand Down Expand Up @@ -170,9 +184,17 @@ class Config(UniqueObject, CopyMixin, AuxDataMixin, TagMixin):
**Copy behavior**
``copy()``
The :py:attr:`campaign` and :py:attr:`analysis` attributes are carried over as references, all
remaining attributes are copied. Note that the copied config is also registered in the analysis.
``copy_shallow()``
All attributs are copied except for the :py:attr:`analysis` and :py:attr:`campaign`, as well as
contained :py:attr:`datasets`, :py:attr:`processes`, :py:attr:`channels`, :py:attr:`variables`
and :py:attr:`shifts` which are set to default values instead.
**Example**
.. code-block:: python
Expand Down Expand Up @@ -228,8 +250,46 @@ class Config(UniqueObject, CopyMixin, AuxDataMixin, TagMixin):

copy_specs = (
[
{"attr": "_campaign", "ref": True},
{"attr": "_analysis", "ref": True},
{
"attr": "_campaign",
"ref": True,
"skip_shallow": True,
},
{
"attr": "_analysis",
"ref": True,
"skip_shallow": True,
},
{
"attr": "_datasets",
"skip_shallow": True,
"skip_value": CopyMixin.Deferred(lambda: UniqueObjectIndex(cls=Dataset)),
},
{
"attr": "_processes",
"skip_shallow": True,
"skip_value": CopyMixin.Deferred(lambda: UniqueObjectIndex(cls=Process)),
},
{
"attr": "_channels",
"skip_shallow": True,
"skip_value": CopyMixin.Deferred(lambda: UniqueObjectIndex(cls=Channel)),
},
{
"attr": "_categories",
"skip_shallow": True,
"skip_value": CopyMixin.Deferred(lambda: UniqueObjectIndex(cls=Category)),
},
{
"attr": "_variables",
"skip_shallow": True,
"skip_value": CopyMixin.Deferred(lambda: UniqueObjectIndex(cls=Variable)),
},
{
"attr": "_shifts",
"skip_shallow": True,
"skip_value": CopyMixin.Deferred(lambda: UniqueObjectIndex(cls=Shift)),
},
] +
UniqueObject.copy_specs +
AuxDataMixin.copy_specs +
Expand Down
22 changes: 20 additions & 2 deletions order/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import six

from order.unique import UniqueObject, unique_tree
from order.unique import UniqueObject, UniqueObjectIndex, unique_tree
from order.mixins import CopyMixin, AuxDataMixin, TagMixin, DataSourceMixin, LabelMixin
from order.process import Process
from order.shift import Shift
Expand Down Expand Up @@ -48,9 +48,16 @@ class Dataset(UniqueObject, CopyMixin, AuxDataMixin, TagMixin, DataSourceMixin,
**Copy behavior**
``copy()``
The :py:attr:`campaign` attribute is carried over as a reference, all remaining attributes are
copied. Note that the copied dataset is also registered in the campaign.
``copy_shallow()``
All attributs are copied except for the :py:attr:`campaign` and containd :py:attr:`processes`
which are set to default values instead.
**Example**
.. code-block:: python
Expand Down Expand Up @@ -145,7 +152,18 @@ class Dataset(UniqueObject, CopyMixin, AuxDataMixin, TagMixin, DataSourceMixin,

# attributes for copying
copy_specs = (
[{"attr": "_campaign", "ref": True}] +
[
{
"attr": "_campaign",
"ref": True,
"skip_shallow": True,
},
{
"attr": "_processes",
"skip_shallow": True,
"skip_value": CopyMixin.Deferred(lambda: UniqueObjectIndex(cls=Process)),
},
] +
UniqueObject.copy_specs +
AuxDataMixin.copy_specs +
TagMixin.copy_specs +
Expand Down
Loading

0 comments on commit 376f08f

Please sign in to comment.