File: copy.py

package info (click to toggle)
thunderbird 1%3A143.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 4,703,968 kB
  • sloc: cpp: 7,770,492; javascript: 5,943,842; ansic: 3,918,754; python: 1,418,263; xml: 653,354; asm: 474,045; java: 183,079; sh: 111,238; makefile: 20,410; perl: 14,359; objc: 13,059; yacc: 4,583; pascal: 3,405; lex: 1,720; ruby: 999; exp: 762; sql: 715; awk: 580; php: 436; lisp: 430; sed: 69; csh: 10
file content (47 lines) | stat: -rw-r--r-- 1,577 bytes parent folder | download | duplicates (21)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from typing import Any

from taskgraph.task import Task
from taskgraph.util.readonlydict import ReadOnlyDict

immutable_types = {int, float, bool, str, type(None), ReadOnlyDict}


def deepcopy(obj: Any) -> Any:
    """Perform a deep copy of an object with a tree like structure.

    This is a re-implementation of Python's `copy.deepcopy` function with a few key differences:

    1. Unlike the stdlib, this does *not* support copying graph-like structure,
    which allows it to be more efficient than deepcopy on tree-like structures
    (such as Tasks).
    2. This special cases support for `taskgraph.task.Task` objects.

    Args:
        obj: The object to deep copy.

    Returns:
        A deep copy of the object.
    """
    ty = type(obj)
    if ty in immutable_types:
        return obj
    if ty is dict:
        return {k: deepcopy(v) for k, v in obj.items()}
    if ty is list:
        return [deepcopy(elt) for elt in obj]
    if ty is Task:
        task = Task(
            kind=deepcopy(obj.kind),
            label=deepcopy(obj.label),
            attributes=deepcopy(obj.attributes),
            task=deepcopy(obj.task),
            description=deepcopy(obj.description),
            optimization=deepcopy(obj.optimization),
            dependencies=deepcopy(obj.dependencies),
            soft_dependencies=deepcopy(obj.soft_dependencies),
            if_dependencies=deepcopy(obj.if_dependencies),
        )
        if obj.task_id:
            task.task_id = obj.task_id
        return task
    raise NotImplementedError(f"copying '{ty}' from '{obj}'")