Skip to content

Commit 8c8cc88

Browse files
committed
Remove copy mechanism for now.
1 parent e464a68 commit 8c8cc88

11 files changed

+29
-206
lines changed

order/__init__.py

-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
"Adapter", "Materialized", "DataProvider",
1414
"UniqueObject", "LazyUniqueObject", "UniqueObjectIndex",
1515
"DuplicateObjectException", "DuplicateNameException", "DuplicateIdException",
16-
"CopyMixin",
1716
"ProcessIndex", "Process", "LazyProcess",
1817
"DatasetIndex", "Dataset", "LazyDataset", "DatasetVariation", "GenOrder",
1918
"Campaign",
@@ -35,7 +34,6 @@
3534
UniqueObject, LazyUniqueObject, UniqueObjectIndex, DuplicateObjectException,
3635
DuplicateNameException, DuplicateIdException,
3736
)
38-
from order.models.mixins import CopyMixin
3937
from order.models.process import ProcessIndex, Process, LazyProcess
4038
from order.models.dataset import DatasetIndex, Dataset, LazyDataset, DatasetVariation, GenOrder
4139
from order.models.campaign import Campaign

order/__meta__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
__author__ = "CMS Collaboration"
88
__email__ = "[email protected]"
9-
__copyright__ = "Copyright 2023, CMS Collaboration"
9+
__copyright__ = "Copyright 2023-2024, CMS Collaboration"
1010
__credits__ = ["Marcel Rieger", "Davide Valsecchi"]
1111
__contact__ = "https://github.com/cms-cat/order"
1212
__license__ = "BSD-3-Clause"

order/adapters/base.py

+3
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,9 @@ def check_cache(
236236
cre = re.compile(rf"^{h}(|_\d+)\.json$")
237237

238238
def find(directory: str, ts: int, invalidate: bool) -> str | None:
239+
if not os.path.exists(directory):
240+
return None
241+
239242
files = {
240243
int(m.group(1)[1:] or 0): os.path.join(directory, elem)
241244
for elem in os.listdir(directory)

order/magics.py

-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@
66

77
from __future__ import annotations
88

9-
109
__all__ = []
1110

12-
1311
import argparse
1412
import shlex
1513

order/models/base.py

-35
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@
66

77
from __future__ import annotations
88

9-
109
__all__ = ["BaseModel", "Model", "AdapterModel"]
1110

12-
1311
from contextlib import contextmanager
1412

1513
from pydantic import BaseModel as PDBaseModel, ConfigDict, Field
@@ -168,10 +166,6 @@ class BaseModel(PDBaseModel):
168166
def __init__(self: BaseModel, *args, **kwargs) -> None:
169167
super().__init__(*args, **kwargs)
170168

171-
# flag that is True for the duration of the model_copy base call to better control __copy__
172-
# and __deepcopy__ depending on whether mode_copy or another object triggered the copy
173-
self._copy_triggered_by_model = False
174-
175169
# setup objects
176170
self._setup_objects()
177171

@@ -237,35 +231,6 @@ def __repr_adapter__(self, adapter_model: "AdapterModel") -> str | Repr:
237231
def __repr_circular__(self) -> str:
238232
return self.__class__.__name__
239233

240-
def model_copy(
241-
self,
242-
*,
243-
update: dict[str, Any] | None = None,
244-
deep: bool = False,
245-
) -> "BaseModel":
246-
# set the copy flag since model_copy triggered the copy
247-
orig_copy_flag = self._copy_triggered_by_model
248-
self._copy_triggered_by_model = True
249-
250-
# super call
251-
copied = super().model_copy(update=update, deep=deep)
252-
253-
# reset the copy flag of _this_ and the copied instance
254-
copied._copy_triggered_by_model = orig_copy_flag
255-
self._copy_triggered_by_model = orig_copy_flag
256-
257-
# the copied instance already contains updated variables, but pydantic does not re-validate
258-
# them, so we need to do this manually here
259-
if update:
260-
for f, v in update.items():
261-
with copied._unfreeze_field(f):
262-
setattr(copied, f, v)
263-
264-
# setup objects
265-
copied._setup_objects()
266-
267-
return copied
268-
269234
def model_show(
270235
self,
271236
*,

order/models/campaign.py

+1-23
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
from pydantic import Field
1414

15-
from order.types import Lazy, NonEmptyStrictStr, StrictFloat, Any
15+
from order.types import Lazy, NonEmptyStrictStr, StrictFloat
1616
from order.util import has_attr
1717
from order.models.unique import UniqueObject
1818
from order.models.dataset import DatasetIndex, Dataset, LazyDataset
@@ -52,28 +52,6 @@ def _setup_datasets(self) -> None:
5252
for dataset in self.datasets.objects:
5353
self._dataset_add_callback(dataset)
5454

55-
def __copy__(self) -> Campaign:
56-
copied = super().__copy__()
57-
58-
# drop references to datasets for consistency as they still point to the original campaign
59-
with copied._unfreeze_field("datasets"):
60-
copied.datasets = copied.model_fields["datasets"].default_factory()
61-
62-
# setup objects if not triggered by model_copy
63-
if not self._copy_triggered_by_model:
64-
copied._setup_objects()
65-
66-
return copied
67-
68-
def __deepcopy__(self, memo: dict[int, Any] | None = None) -> Campaign:
69-
copied = super().__deepcopy__(memo=memo)
70-
71-
# setup objects if not triggered by model_copy
72-
if not self._copy_triggered_by_model:
73-
copied._setup_objects()
74-
75-
return copied
76-
7755
def _dataset_materialize_callback(self, dataset: Dataset) -> None:
7856
dataset.campaign = self
7957

order/models/dataset.py

+2-53
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,15 @@
77

88

99
import enum
10-
import copy
1110

1211
from pydantic import Field, field_validator
1312

1413
from order.types import (
15-
Union, List, Dict, NonEmptyStrictStr, PositiveStrictInt, Lazy, ClassVar, GeneratorType, Any,
14+
Union, List, Dict, NonEmptyStrictStr, PositiveStrictInt, Lazy, ClassVar, GeneratorType,
1615
)
17-
from order.util import has_attr, validated
16+
from order.util import validated
1817
from order.models.base import Model
1918
from order.models.unique import UniqueObjectBase, UniqueObject, LazyUniqueObject, UniqueObjectIndex
20-
# from order.models.mixins import CopyMixin
2119

2220

2321
class DatasetIndex(UniqueObjectIndex):
@@ -83,19 +81,6 @@ def validate_gen_order(cls, gen_order: str) -> str:
8381
except KeyError:
8482
raise ValueError(f"unknown gen_order '{gen_order}'")
8583

86-
def __copy__(self) -> "DatasetVariation":
87-
inst = super().__copy__()
88-
89-
# shallow copy of keys
90-
with self._unfreeze_field("keys"):
91-
inst.keys = copy.copy(inst.keys)
92-
93-
# shallow copy of the lfns if not materialized yet
94-
if isinstance(inst.lazy_lfns, list):
95-
inst.lfns = copy.copy(inst.lfns)
96-
97-
return inst
98-
9984

10085
class Dataset(UniqueObject):
10186

@@ -127,42 +112,6 @@ def campaign(self, campaign: "Campaign" | None) -> "Campaign":
127112

128113
return campaign
129114

130-
def __copy__(self) -> "Dataset":
131-
copied = super().__copy__()
132-
133-
# shallow copy the variations dict
134-
with copied._unfreeze_field("variations"):
135-
copied.variations = copy.copy(copied.variations)
136-
137-
# drop the campaign reference
138-
copied.campaign = None
139-
140-
# setup objects if not triggered by model_copy
141-
if not self._copy_triggered_by_model:
142-
copied._setup_objects()
143-
144-
return copied
145-
146-
def __deepcopy__(self, memo: dict[int, Any] | None = None) -> Dataset:
147-
copied = super().__deepcopy__(memo=memo)
148-
149-
# setup objects if not triggered by model_copy
150-
if not self._copy_triggered_by_model:
151-
copied._setup_objects()
152-
153-
return copied
154-
155-
def model_copy(self, *args, **kwargs) -> "Dataset":
156-
copied = super().model_copy(*args, **kwargs)
157-
158-
# since campaign is not a proper field, a duplicate of the copied instance is added to the
159-
# campaign's dataset index, so replace it
160-
if has_attr(getattr(copied, "campaign", None), "datasets"):
161-
copied.campaign._setup_datasets()
162-
copied.campaign.datasets.add(copied, overwrite=True)
163-
164-
return copied
165-
166115
def __getitem__(self, name: str) -> DatasetVariation:
167116
return self.get_info(name)
168117

order/models/mixins.py

+3-34
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,7 @@
22

33
from __future__ import annotations
44

5+
__all__ = []
56

6-
__all__ = ["CopyMixin"]
7-
8-
9-
from order.types import Any
10-
from order.models.base import Model
11-
12-
13-
class CopyMixin(Model):
14-
"""
15-
TODO.
16-
"""
17-
18-
# shallow specs:
19-
# set_none
20-
# re_copy
21-
# field_default (default if not PydanticUndefined, otherwise default_factory())
22-
# custom: e.g. run setup
23-
24-
# deep specs:
25-
# set_none
26-
# field_default
27-
# custom: e.g. re-add to index to overwrite duplicate copy, see datasets
28-
29-
# create a way to have specs apply to both shallow and deep copies, or to just one!
30-
31-
# also, there should be an easy way to inject conditions for applying specs in the first place,
32-
# such as laziness checks, which are done on the corresponding lazy attribute to avoid
33-
# materialization, see e.g. DatasetInfo.lfns
34-
35-
def __copy__(self) -> "CopyMixin":
36-
return super().__copy__()
37-
38-
def __deepcopy__(self, memo: dict[int, Any] | None = None) -> "CopyMixin":
39-
return super().__deepcopy__(memo=memo)
7+
from order.types import Any # noqa
8+
from order.models.base import Model # noqa

order/models/process.py

+11-30
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from pydantic import Field
1010

1111
from order.types import (
12-
Union, List, Dict, NonEmptyStrictStr, Lazy, ClassVar, Any, StrictFloat,
12+
Union, List, Dict, NonEmptyStrictStr, Lazy, ClassVar, StrictFloat,
1313
)
1414
from order.util import has_attr
1515
from order.models.unique import UniqueObjectBase, UniqueObject, LazyUniqueObject, UniqueObjectIndex
@@ -49,8 +49,16 @@ class Process(UniqueObject):
4949
default_factory=dict,
5050
frozen=True,
5151
)
52-
processes: ProcessIndex = Field(default_factory=ProcessIndex, frozen=True)
53-
parent_processes: ProcessIndex = Field(default_factory=ProcessIndex, frozen=True)
52+
processes: ProcessIndex = Field(
53+
default_factory=ProcessIndex,
54+
frozen=True,
55+
)
56+
parent_processes: ProcessIndex = Field(
57+
default_factory=ProcessIndex,
58+
frozen=True,
59+
# exclude from exports to avoid circular references
60+
exclude=True,
61+
)
5462

5563
lazy_cls: ClassVar[UniqueObjectBase] = LazyProcess
5664

@@ -97,33 +105,6 @@ def _setup_parent_processes(self) -> None:
97105
for parent_process in self.parent_processes.objects:
98106
self._parent_process_add_callback(parent_process)
99107

100-
def __copy__(self) -> "Process":
101-
copied = super().__copy__()
102-
103-
# TODO: handle cross sections here after above TODOs are resolved
104-
105-
# drop references to child and parent processes for consistency
106-
with copied._unfreeze_field("processes"):
107-
copied.processes = copied.model_fields["processes"].default_factory()
108-
109-
with copied._unfreeze_field("parent_processes"):
110-
copied.parent_processes = copied.model_fields["parent_processes"].default_factory()
111-
112-
# setup objects if not triggered by model_copy
113-
if not self._copy_triggered_by_model:
114-
copied._setup_objects()
115-
116-
return copied
117-
118-
def __deepcopy__(self, memo: dict[int, Any] | None = None) -> Process:
119-
copied = super().__deepcopy__(memo=memo)
120-
121-
# setup objects if not triggered by model_copy
122-
if not self._copy_triggered_by_model:
123-
copied._setup_objects()
124-
125-
return copied
126-
127108
def _process_materialize_callback(self, process: "Process") -> None:
128109
process.parent_processes.add(self, overwrite=True)
129110

order/models/unique.py

+1-22
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@
66

77
from __future__ import annotations
88

9-
109
__all__ = [
1110
"UniqueObject", "LazyUniqueObject", "UniqueObjectIndex",
1211
"DuplicateObjectException", "DuplicateNameException", "DuplicateIdException",
1312
]
1413

15-
1614
from abc import abstractmethod
1715
from contextlib import contextmanager
1816

@@ -242,7 +240,7 @@ class LazyUniqueObject(UniqueObjectBase, WrapsUniqueClass):
242240
@abstractmethod
243241
def create_lazy_dict(cls) -> dict[str, Any]:
244242
# must be implemented by subclasses
245-
return
243+
...
246244

247245
@classmethod
248246
def create(cls, *args, **kwargs) -> "LazyUniqueObject":
@@ -368,25 +366,6 @@ def __init__(self, *args, **kwargs) -> None:
368366
# sync indices initially
369367
self._sync_indices()
370368

371-
def __copy__(self) -> "UniqueObjectIndex":
372-
inst = super().__copy__()
373-
374-
# make a copy of the objects list
375-
inst.objects = list(inst.objects)
376-
377-
# fully reset indices
378-
self._reset_indices()
379-
380-
return inst
381-
382-
def __deepcopy__(self, memo: dict[int, Any] | None = None) -> "UniqueObjectIndex":
383-
inst = super().__deepcopy__(memo=memo)
384-
385-
# fully reset indices
386-
self._reset_indices()
387-
388-
return inst
389-
390369
def __len__(self) -> int:
391370
"""
392371
Returns the number of objects in the index.

requirements_dev.txt

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
ipython~=8.8
2+
pytest~=7.1
3+
pytest-cov~=3.0
4+
flake8~=5.0
5+
flake8-commas~=2.1
6+
flake8-quotes~=3.3
7+
pipdeptree~=2.13
18
cloudpickle>=1.3
2-
flake8>=4.0,<6.0
3-
flake8-commas>=2.1
4-
flake8-quotes>=3.3
5-
pytest-cov>=3.0
69
mermaidmro~=0.1.2

0 commit comments

Comments
 (0)