Python copy.deepcopy() Examples

The following are code examples for showing how to use copy.deepcopy(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: PEAKachu   Author: tbischler   File: window.py    ISC License 6 votes vote down vote up
def init_libraries(self, paired_end, max_insert_size, ctr_libs,
                       exp_libs):
        self._paired_end = paired_end
        self._max_insert_size = max_insert_size
        self._ctr_lib_list = [splitext(basename(lib_file))[0]
                              for lib_file in ctr_libs]
        self._exp_lib_list = [splitext(basename(lib_file))[0]
                              for lib_file in exp_libs]
        # add libs to lib_dict
        for lib_file in exp_libs + ctr_libs:
            if not isfile(lib_file):
                sys.stderr.write("ERROR: The library file {} does not exist.\n"
                                 .format(lib_file))
                sys.exit(1)
            self._lib_dict[splitext(basename(lib_file))[0]] = Library(
                paired_end, max_insert_size, lib_file,
                deepcopy(self._replicon_dict))
        self._lib_names_list = list(self._lib_dict.keys())
        print("The following libraries were initialized:\n"
              "# Experiment libraries\n{0}\n"
              "# Control libraries\n{1}".format(
                  '\n'.join(self._exp_lib_list),
                  '\n'.join(self._ctr_lib_list))) 
Example 2
Project: mutatest   Author: EvanKepner   File: test_transformers.py    MIT License 6 votes vote down vote up
def test_MutateAST_visit_read_only(binop_file):
    """Read only test to ensure locations are aggregated."""
    tree = Genome(binop_file).ast
    mast = MutateAST(readonly=True)
    testing_tree = deepcopy(tree)
    mast.visit(testing_tree)

    # four locations from the binary operations in binop_file
    assert len(mast.locs) == 4

    # tree should be unmodified
    assert ast.dump(tree) == ast.dump(testing_tree)


####################################################################################################
# GENERIC TRANSFORMER NODE TESTS
# These represent the basic pattern for visiting a node in the MutateAST class and applying a
# mutation without running the full test suite against the cached files.
#################################################################################################### 
Example 3
Project: mutatest   Author: EvanKepner   File: test_transformers.py    MIT License 6 votes vote down vote up
def test_MutateAST_visit_augassign(augassign_file, augassign_expected_locs):
    """Test mutation for AugAssign: +=, -=, /=, *=."""
    tree = Genome(augassign_file).ast
    test_mutation = "AugAssign_Div"

    testing_tree = deepcopy(tree)
    mutated_tree = MutateAST(target_idx=augassign_expected_locs[0], mutation=test_mutation).visit(
        testing_tree
    )

    mast = MutateAST(readonly=True)
    mast.visit(mutated_tree)

    assert len(mast.locs) == 4

    for l in mast.locs:
        # spot check on mutation from Add tp Div
        if l.lineno == 1 and l.col_offset == 4:
            assert l.op_type == test_mutation

        # spot check on not-mutated location still being Mult
        if l.lineno == 5 and l.col_offset == 4:
            assert l.op_type == "AugAssign_Mult" 
Example 4
Project: mutatest   Author: EvanKepner   File: test_transformers.py    MIT License 6 votes vote down vote up
def test_MutateAST_visit_boolop(boolop_file, boolop_expected_loc):
    """Test mutation of AND to OR in the boolop."""
    tree = Genome(boolop_file).ast
    test_mutation = ast.Or

    # apply the mutation to the original tree copy
    testing_tree = deepcopy(tree)
    mutated_tree = MutateAST(target_idx=boolop_expected_loc, mutation=test_mutation).visit(
        testing_tree
    )

    # revisit in read-only mode to gather the locations of the new nodes
    mast = MutateAST(readonly=True)
    mast.visit(mutated_tree)

    # four locations from the binary operations in binop_file
    assert len(mast.locs) == 1

    # there will only be one loc, but this still works
    # basedon the col and line offset in the fixture for compare_expected_loc
    for l in mast.locs:
        if l.lineno == 2 and l.col_offset == 11:
            assert l.op_type == test_mutation 
Example 5
Project: mutatest   Author: EvanKepner   File: test_transformers.py    MIT License 6 votes vote down vote up
def test_MutateAST_visit_compare(idx, mut_op, lineno, compare_file, compare_expected_locs):
    """Test mutation of the == to != in the compare op."""
    tree = Genome(compare_file).ast

    # apply the mutation to the original tree copy
    testing_tree = deepcopy(tree)
    mutated_tree = MutateAST(target_idx=compare_expected_locs[idx], mutation=mut_op).visit(
        testing_tree
    )

    # revisit in read-only mode to gather the locations of the new nodes
    mast = MutateAST(readonly=True)
    mast.visit(mutated_tree)

    assert len(mast.locs) == 3

    # check that the lineno marked for mutation is changed, otherwise original ops should
    # still be present without modification
    for l in mast.locs:
        if l.lineno == lineno and l.col_offset == 11:
            assert l.op_type == mut_op
        else:
            assert l.op_type in {ast.Eq, ast.Is, ast.In}  # based on compare_file fixture 
Example 6
Project: mutatest   Author: EvanKepner   File: test_transformers.py    MIT License 6 votes vote down vote up
def test_MutateAST_visit_index_neg(
    i_order, lineno, col_offset, mut, index_file, index_expected_locs
):
    """Test mutation for Index: i[0], i[1], i[-1]."""
    tree = Genome(index_file).ast
    test_mutation = mut

    testing_tree = deepcopy(tree)
    mutated_tree = MutateAST(target_idx=index_expected_locs[i_order], mutation=test_mutation).visit(
        testing_tree
    )

    mast = MutateAST(readonly=True)
    mast.visit(mutated_tree)

    assert len(mast.locs) == 4

    for l in mast.locs:
        # spot check on mutation from Index_NumNeg to Index_NumPos
        if l.lineno == lineno and l.col_offset == col_offset:
            assert l.op_type == test_mutation

        # spot check on not-mutated location still being None
        if l.lineno == 4 and l.col_offset == 23:
            assert l.op_type == "Index_NumPos" 
Example 7
Project: mutatest   Author: EvanKepner   File: test_transformers.py    MIT License 6 votes vote down vote up
def test_MutateAST_visit_nameconst(nameconst_file, nameconst_expected_locs):
    """Test mutation for nameconst: True, False, None."""
    tree = Genome(nameconst_file).ast
    test_mutation = False

    testing_tree = deepcopy(tree)
    mutated_tree = MutateAST(target_idx=nameconst_expected_locs[0], mutation=test_mutation).visit(
        testing_tree
    )

    mast = MutateAST(readonly=True)
    mast.visit(mutated_tree)

    # if statement is included with this file that will be picked up
    nc_locs = [l for l in mast.locs if l.ast_class == "NameConstant"]
    assert len(nc_locs) == 4

    for l in nc_locs:
        # spot check on mutation from True to False
        if l.lineno == 1 and l.col_offset == 14:
            assert l.op_type == test_mutation

        # spot check on not-mutated location still being None
        if l.lineno == 7 and l.col_offset == 22:
            assert l.op_type is None 
Example 8
Project: invenio-openaire   Author: inveniosoftware   File: tasks.py    MIT License 6 votes vote down vote up
def create_or_update_record(data, pid_type, id_key, minter):
    """Register a funder or grant."""
    resolver = Resolver(
        pid_type=pid_type, object_type='rec', getter=Record.get_record)

    try:
        pid, record = resolver.resolve(data[id_key])
        data_c = deepcopy(data)
        del data_c['remote_modified']
        record_c = deepcopy(record)
        del record_c['remote_modified']
        # All grants on OpenAIRE are modified periodically even if nothing
        # has changed. We need to check for actual differences in the metadata
        if data_c != record_c:
            record.update(data)
            record.commit()
            record_id = record.id
            db.session.commit()
            RecordIndexer().index_by_id(str(record_id))
    except PIDDoesNotExistError:
        record = Record.create(data)
        record_id = record.id
        minter(record.id, data)
        db.session.commit()
        RecordIndexer().index_by_id(str(record_id)) 
Example 9
Project: pyblish-win   Author: pyblish   File: cookielib.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def revert(self, filename=None,
               ignore_discard=False, ignore_expires=False):
        """Clear all cookies and reload cookies from a saved file.

        Raises LoadError (or IOError) if reversion is not successful; the
        object's state will not be altered if this happens.

        """
        if filename is None:
            if self.filename is not None: filename = self.filename
            else: raise ValueError(MISSING_FILENAME_TEXT)

        self._cookies_lock.acquire()
        try:

            old_state = copy.deepcopy(self._cookies)
            self._cookies = {}
            try:
                self.load(filename, ignore_discard, ignore_expires)
            except (LoadError, IOError):
                self._cookies = old_state
                raise

        finally:
            self._cookies_lock.release() 
Example 10
Project: pyblish-win   Author: pyblish   File: copy.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def _deepcopy_tuple(x, memo):
    y = []
    for a in x:
        y.append(deepcopy(a, memo))
    d = id(x)
    try:
        return memo[d]
    except KeyError:
        pass
    for i in range(len(x)):
        if x[i] is not y[i]:
            y = tuple(y)
            break
    else:
        y = x
    memo[d] = y
    return y 
Example 11
Project: pyblish-win   Author: pyblish   File: copy.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def _deepcopy_inst(x, memo):
    if hasattr(x, '__deepcopy__'):
        return x.__deepcopy__(memo)
    if hasattr(x, '__getinitargs__'):
        args = x.__getinitargs__()
        args = deepcopy(args, memo)
        y = x.__class__(*args)
    else:
        y = _EmptyClass()
        y.__class__ = x.__class__
    memo[id(x)] = y
    if hasattr(x, '__getstate__'):
        state = x.__getstate__()
    else:
        state = x.__dict__
    state = deepcopy(state, memo)
    if hasattr(y, '__setstate__'):
        y.__setstate__(state)
    else:
        y.__dict__.update(state)
    return y 
Example 12
Project: pyblish-win   Author: pyblish   File: sets.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def __deepcopy__(self, memo):
        """Return a deep copy of a set; used by copy module."""
        # This pre-creates the result and inserts it in the memo
        # early, in case the deep copy recurses into another reference
        # to this same set.  A set can't be an element of itself, but
        # it can certainly contain an object that has a reference to
        # itself.
        from copy import deepcopy
        result = self.__class__()
        memo[id(self)] = result
        data = result._data
        value = True
        for elt in self:
            data[deepcopy(elt, memo)] = value
        return result

    # Standard set operations: union, intersection, both differences.
    # Each has an operator version (e.g. __or__, invoked with |) and a
    # method version (e.g. union).
    # Subtle:  Each pair requires distinct code so that the outcome is
    # correct when the type of other isn't suitable.  For example, if
    # we did "union = __or__" instead, then Set().union(3) would return
    # NotImplemented instead of raising TypeError (albeit that *why* it
    # raises TypeError as-is is also a bit subtle). 
Example 13
Project: pyblish-win   Author: pyblish   File: test_sysconfig.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def setUp(self):
        """Make a copy of sys.path"""
        super(TestSysConfig, self).setUp()
        self.sys_path = sys.path[:]
        self.makefile = None
        # patching os.uname
        if hasattr(os, 'uname'):
            self.uname = os.uname
            self._uname = os.uname()
        else:
            self.uname = None
            self._uname = None
        os.uname = self._get_uname
        # saving the environment
        self.name = os.name
        self.platform = sys.platform
        self.version = sys.version
        self.sep = os.sep
        self.join = os.path.join
        self.isabs = os.path.isabs
        self.splitdrive = os.path.splitdrive
        self._config_vars = copy(sysconfig._CONFIG_VARS)
        self.old_environ = deepcopy(os.environ) 
Example 14
Project: pyblish-win   Author: pyblish   File: test_collections.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_copying(self):
        # Check that ordered dicts are copyable, deepcopyable, picklable,
        # and have a repr/eval round-trip
        pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
        od = OrderedDict(pairs)
        update_test = OrderedDict()
        update_test.update(od)
        for i, dup in enumerate([
                    od.copy(),
                    copy.copy(od),
                    copy.deepcopy(od),
                    pickle.loads(pickle.dumps(od, 0)),
                    pickle.loads(pickle.dumps(od, 1)),
                    pickle.loads(pickle.dumps(od, 2)),
                    pickle.loads(pickle.dumps(od, -1)),
                    eval(repr(od)),
                    update_test,
                    OrderedDict(od),
                    ]):
            self.assertTrue(dup is not od)
            self.assertEqual(dup, od)
            self.assertEqual(list(dup.items()), list(od.items()))
            self.assertEqual(len(dup), len(od))
            self.assertEqual(type(dup), type(od)) 
Example 15
Project: pyblish-win   Author: pyblish   File: test_descr.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def set2op_test(self, a, b, c, res, stmt="a[b]=c", meth="__setitem__"):
        d = {'a': deepcopy(a), 'b': b, 'c': c}
        exec stmt in d
        self.assertEqual(d['a'], res)
        t = type(a)
        m = getattr(t, meth)
        while meth not in t.__dict__:
            t = t.__bases__[0]
        # in some implementations (e.g. PyPy), 'm' can be a regular unbound
        # method object; the getattr() below obtains its underlying function.
        self.assertEqual(getattr(m, 'im_func', m), t.__dict__[meth])
        d['a'] = deepcopy(a)
        m(d['a'], b, c)
        self.assertEqual(d['a'], res)
        d['a'] = deepcopy(a)
        bm = getattr(d['a'], meth)
        bm(b, c)
        self.assertEqual(d['a'], res) 
Example 16
Project: pyblish-win   Author: pyblish   File: test_descr.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def set3op_test(self, a, b, c, d, res, stmt="a[b:c]=d", meth="__setslice__"):
        dictionary = {'a': deepcopy(a), 'b': b, 'c': c, 'd': d}
        exec stmt in dictionary
        self.assertEqual(dictionary['a'], res)
        t = type(a)
        while meth not in t.__dict__:
            t = t.__bases__[0]
        m = getattr(t, meth)
        # in some implementations (e.g. PyPy), 'm' can be a regular unbound
        # method object; the getattr() below obtains its underlying function.
        self.assertEqual(getattr(m, 'im_func', m), t.__dict__[meth])
        dictionary['a'] = deepcopy(a)
        m(dictionary['a'], b, c, d)
        self.assertEqual(dictionary['a'], res)
        dictionary['a'] = deepcopy(a)
        bm = getattr(dictionary['a'], meth)
        bm(b, c, d)
        self.assertEqual(dictionary['a'], res) 
Example 17
Project: pyblish-win   Author: pyblish   File: test_copy.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_reconstruct_state_setstate(self):
        class C(object):
            def __reduce__(self):
                return (C, (), self.__dict__)
            def __setstate__(self, state):
                self.__dict__.update(state)
            def __cmp__(self, other):
                return cmp(self.__dict__, other.__dict__)
            __hash__ = None # Silence Py3k warning
        x = C()
        x.foo = [42]
        y = copy.copy(x)
        self.assertEqual(y, x)
        y = copy.deepcopy(x)
        self.assertEqual(y, x)
        self.assertTrue(y.foo is not x.foo) 
Example 18
Project: BERT-Classification-Tutorial   Author: Socialbird-AILab   File: modeling.py    Apache License 2.0 5 votes vote down vote up
def to_dict(self):
        """Serializes this instance to a Python dictionary."""
        output = copy.deepcopy(self.__dict__)
        return output 
Example 19
Project: fs_image   Author: facebookincubator   File: test_extent.py    MIT License 5 votes vote down vote up
def test_copy(self):
        e = Extent.empty().write(offset=5, length=5)
        self.assertIs(e, copy.deepcopy(e))
        self.assertIs(e, copy.copy(e)) 
Example 20
Project: fs_image   Author: facebookincubator   File: deepcopy_test.py    MIT License 5 votes vote down vote up
def check_deepcopy_at_each_step(
        self, gen_fn: Callable[[], Generator[Tuple[str, Any], Any, None]],
    ) -> None:
        '''
        `gen_fn` makes a generator that yields `(step_name, obj)`, and gets
        sent `obj` or ``deepcopy(obj)`.
        '''
        steps = self._check_deepcopy(gen_fn)
        for deepcopy_step, expected_name in enumerate(steps):
            with self.subTest(deepcopy_step=expected_name):
                self.assertEqual(
                    steps,
                    self._check_deepcopy(gen_fn, deepcopy_step, expected_name),
                ) 
Example 21
Project: PEAKachu   Author: tbischler   File: window.py    ISC License 5 votes vote down vote up
def _combine_windows(self, df):
        peak_list = []
        peak = {"peak_start": None, "peak_end": None}
        for index, window in df.iterrows():
            # significant window
            if window.loc["significant"]:
                # start new peak region if no peak region was started before
                if peak["peak_start"] is None:
                    peak["peak_start"] = window.loc["w_start"]
                    peak["peak_end"] = window.loc["w_end"]
                # start new peak region while still inside previous peak region
                #   *this is due to gaps in the windows caused by pre-filtering
                #   *add previous peak region to output
                # +1 -> combine adjacent peaks
                elif window.loc["w_start"] > peak["peak_end"] + 1:
                    peak_list.append(deepcopy(peak))
                    peak["peak_start"] = window.loc["w_start"]
                    peak["peak_end"] = window.loc["w_end"]
                # elongate peak if window overlaps
                else:
                    peak["peak_end"] = window.loc["w_end"]
            # non-significant window
            else:
                # jump to next window if outside of peak region
                # or current position upstream of peak end
                # +1 -> combine adjacent peaks
                if (peak["peak_start"] is None or window.loc[
                        "w_start"] <= peak["peak_end"] + 1):
                    continue
                # otherwise end peak region and append to output list
                peak_list.append(deepcopy(peak))
                peak["peak_start"] = None
                peak["peak_end"] = None
        # append peak if last window in data frame was significant
        if peak["peak_start"] is not None:
            peak_list.append(deepcopy(peak))
        peak_df = pd.DataFrame(peak_list, columns=["peak_start", "peak_end"])
        return peak_df 
Example 22
Project: mutatest   Author: EvanKepner   File: test_transformers.py    MIT License 5 votes vote down vote up
def test_MutateAST_visit_binop_37(binop_file):
    """Read only test to ensure locations are aggregated."""
    tree = Genome(binop_file).ast

    # Py 3.7 vs. Py 3.8
    end_lineno = None if sys.version_info < (3, 8) else 6
    end_col_offset = None if sys.version_info < (3, 8) else 17

    test_idx = LocIndex(
        ast_class="BinOp",
        lineno=6,
        col_offset=11,
        op_type=ast.Add,
        end_lineno=end_lineno,
        end_col_offset=end_col_offset,
    )
    test_mutation = ast.Pow

    # apply the mutation to the original tree copy
    testing_tree = deepcopy(tree)
    mutated_tree = MutateAST(target_idx=test_idx, mutation=test_mutation).visit(testing_tree)

    # revisit in read-only mode to gather the locations of the new nodes
    mast = MutateAST(readonly=True)
    mast.visit(mutated_tree)

    # four locations from the binary operations in binop_file
    assert len(mast.locs) == 4

    # locs is an unordered set, cycle through to thd target and check the mutation
    for l in mast.locs:
        if (
            l.lineno == 6
            and l.col_offset == 11
            and l.end_lineno == end_lineno
            and l.end_col_offset == end_col_offset
        ):
            assert l.op_type == test_mutation 
Example 23
Project: jumpserver-python-sdk   Author: jumpserver   File: test_auth.py    GNU General Public License v2.0 5 votes vote down vote up
def test_load_from_conf_env(self):
        os.environ[self.key_env_var] = self.access_key_val
        app_access_key = deepcopy(self.app_access_key)
        app_access_key.load_from_conf_env()
        self.assertEqual(app_access_key, self.access_key) 
Example 24
Project: jumpserver-python-sdk   Author: jumpserver   File: test_auth.py    GNU General Public License v2.0 5 votes vote down vote up
def test_load_from_conf_val(self):
        app_access_key = deepcopy(self.app_access_key)
        app_access_key.load_from_conf_val()
        self.assertEqual(app_access_key, self.access_key) 
Example 25
Project: jumpserver-python-sdk   Author: jumpserver   File: test_auth.py    GNU General Public License v2.0 5 votes vote down vote up
def test_load_from_conf_file(self):
        with open(self.key_file, 'wt') as f:
            f.write(self.access_key_val)
        app_access_key = deepcopy(self.app_access_key)
        app_access_key.load_from_conf_file()
        self.assertEqual(app_access_key, self.access_key) 
Example 26
Project: jumpserver-python-sdk   Author: jumpserver   File: test_auth.py    GNU General Public License v2.0 5 votes vote down vote up
def test_load(self):
        with open(self.key_file, 'wt') as f:
            f.write(self.access_key_val)
        app_access_key = deepcopy(self.app_access_key)
        app_access_key.load()
        self.assertEqual(app_access_key, self.access_key) 
Example 27
Project: kicker-module   Author: EvanTheB   File: backend.py    GNU General Public License v3.0 5 votes vote down vote up
def get_players_games(self):
        with open(self.log_file, 'r+') as log:
            with LockFile(log):
                self._load_from_json(log)

        return copy.deepcopy(dict(self.players)), copy.deepcopy(list(self.games)) 
Example 28
Project: chainer-openai-transformer-lm   Author: soskek   File: model_py.py    MIT License 5 votes vote down vote up
def __init__(self, cfg, vocab=40990, n_ctx=512):
        super(Model, self).__init__()
        self.vocab = vocab
        with self.init_scope():
            self.embed = L.EmbedID(vocab, cfg.n_embd,
                                   initializers.Normal(scale=0.02))
            self.drop = lambda x: F.dropout(x, cfg.embd_pdrop)
            block = Block(n_ctx, cfg, scale=True)
            self.h = chainer.ChainList(*[copy.deepcopy(block)
                                         for _ in range(cfg.n_layer)])
        self.decoder = lambda x: F.linear(x, self.embed.W)
        # To reproduce the noise_shape parameter of TF implementation
        self.clf_dropout = lambda x: F.dropout(x, cfg.clf_pdrop) 
Example 29
Project: malcode   Author: moonsea   File: gramfreq.py    GNU General Public License v3.0 5 votes vote down vote up
def getTotalTF(content, tf, df):
    tmp = copy.deepcopy(df)
    for line in content:
        # print line
        tf[line.strip()] = tf.get(line.strip(), 0) + 1
        df[line.strip()] = tmp.get(line.strip(), 0) + 1 
Example 30
Project: cat-bbs   Author: aleju   File: bbs.py    MIT License 5 votes vote down vote up
def copy(self, x1=None, x2=None, y1=None, y2=None):
        rect = copy.deepcopy(self)
        if x1 is not None:
            rect.x1 = x1
        if x2 is not None:
            rect.x2 = x2
        if y1 is not None:
            rect.y1 = y1
        if y2 is not None:
            rect.y2 = y2
        return rect 
Example 31
Project: alfred-yubikey-otp   Author: robertoriv   File: workflow.py    MIT License 5 votes vote down vote up
def _load(self):
        """Load cached settings from JSON file `self._filepath`."""
        data = {}
        with LockFile(self._filepath, 0.5):
            with open(self._filepath, 'rb') as fp:
                data.update(json.load(fp))

        self._original = deepcopy(data)

        self._nosave = True
        self.update(data)
        self._nosave = False 
Example 32
Project: pyblish-win   Author: pyblish   File: analyze_dxp.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def snapshot_profile():
    """Returns the cumulative execution profile until this call."""
    with _profile_lock:
        merge_profile()
        return copy.deepcopy(_cumulative_profile) 
Example 33
Project: pyblish-win   Author: pyblish   File: copy.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def _deepcopy_list(x, memo):
    y = []
    memo[id(x)] = y
    for a in x:
        y.append(deepcopy(a, memo))
    return y 
Example 34
Project: pyblish-win   Author: pyblish   File: copy.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def _deepcopy_method(x, memo): # Copy instance methods
    return type(x)(x.im_func, deepcopy(x.im_self, memo), x.im_class) 
Example 35
Project: pyblish-win   Author: pyblish   File: copy.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def _keep_alive(x, memo):
    """Keeps a reference to the object x in the memo.

    Because we remember objects by their id, we have
    to assure that possibly temporary objects are kept
    alive by referencing them.
    We store a reference at the id of the memo, which should
    normally not be used unless someone tries to deepcopy
    the memo itself...
    """
    try:
        memo[id(memo)].append(x)
    except KeyError:
        # aha, this is the first one :-)
        memo[id(memo)]=[x] 
Example 36
Project: pyblish-win   Author: pyblish   File: support.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def setUp(self):
        super(EnvironGuard, self).setUp()
        self.old_environ = deepcopy(os.environ) 
Example 37
Project: pyblish-win   Author: pyblish   File: mailbox.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def __init__(self, message=None):
        """Initialize a Message instance."""
        if isinstance(message, email.message.Message):
            self._become_message(copy.deepcopy(message))
            if isinstance(message, Message):
                message._explain_to(self)
        elif isinstance(message, str):
            self._become_message(email.message_from_string(message))
        elif hasattr(message, "read"):
            self._become_message(email.message_from_file(message))
        elif message is None:
            email.message.Message.__init__(self)
        else:
            raise TypeError('Invalid message type: %s' % type(message)) 
Example 38
Project: pyblish-win   Author: pyblish   File: weakref.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def __deepcopy__(self, memo):
        from copy import deepcopy
        new = self.__class__()
        for key, wr in self.data.items():
            o = wr()
            if o is not None:
                new[deepcopy(key, memo)] = o
        return new 
Example 39
Project: pyblish-win   Author: pyblish   File: weakref.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def __deepcopy__(self, memo):
        from copy import deepcopy
        new = self.__class__()
        for key, value in self.data.items():
            o = key()
            if o is not None:
                new[o] = deepcopy(value, memo)
        return new 
Example 40
Project: pyblish-win   Author: pyblish   File: test_fractions.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_copy_deepcopy_pickle(self):
        r = F(13, 7)
        dr = DummyFraction(13, 7)
        self.assertEqual(r, loads(dumps(r)))
        self.assertEqual(id(r), id(copy(r)))
        self.assertEqual(id(r), id(deepcopy(r)))
        self.assertNotEqual(id(dr), id(copy(dr)))
        self.assertNotEqual(id(dr), id(deepcopy(dr)))
        self.assertTypedEquals(dr, copy(dr))
        self.assertTypedEquals(dr, deepcopy(dr)) 
Example 41
Project: pyblish-win   Author: pyblish   File: test_bytes.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_copy(self):
        a = ByteArraySubclass(b"abcd")
        a.x = 10
        a.y = ByteArraySubclass(b"efgh")
        for copy_method in (copy.copy, copy.deepcopy):
            b = copy_method(a)
            self.assertNotEqual(id(a), id(b))
            self.assertEqual(a, b)
            self.assertEqual(a.x, b.x)
            self.assertEqual(a.y, b.y)
            self.assertEqual(type(a), type(b))
            self.assertEqual(type(a.y), type(b.y)) 
Example 42
Project: pyblish-win   Author: pyblish   File: test_collections.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_copy(self):
        p = TestNT(x=10, y=20, z=30)
        for copier in copy.copy, copy.deepcopy:
            q = copier(p)
            self.assertEqual(p, q)
            self.assertEqual(p._fields, q._fields) 
Example 43
Project: pyblish-win   Author: pyblish   File: test_collections.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_copying(self):
        # Check that counters are copyable, deepcopyable, picklable, and
        #have a repr/eval round-trip
        words = Counter('which witch had which witches wrist watch'.split())
        update_test = Counter()
        update_test.update(words)
        for i, dup in enumerate([
                    words.copy(),
                    copy.copy(words),
                    copy.deepcopy(words),
                    pickle.loads(pickle.dumps(words, 0)),
                    pickle.loads(pickle.dumps(words, 1)),
                    pickle.loads(pickle.dumps(words, 2)),
                    pickle.loads(pickle.dumps(words, -1)),
                    cPickle.loads(cPickle.dumps(words, 0)),
                    cPickle.loads(cPickle.dumps(words, 1)),
                    cPickle.loads(cPickle.dumps(words, 2)),
                    cPickle.loads(cPickle.dumps(words, -1)),
                    eval(repr(words)),
                    update_test,
                    Counter(words),
                    ]):
            msg = (i, dup, words)
            self.assertTrue(dup is not words)
            self.assertEqual(dup, words)
            self.assertEqual(len(dup), len(words))
            self.assertEqual(type(dup), type(words)) 
Example 44
Project: pyblish-win   Author: pyblish   File: test_descr.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_deepcopy_recursive(self):
        # Testing deepcopy of recursive objects...
        class Node:
            pass
        a = Node()
        b = Node()
        a.b = b
        b.a = a
        z = deepcopy(a) # This blew up before 
Example 45
Project: pyblish-win   Author: pyblish   File: test_descr.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_copy_setstate(self):
        # Testing that copy.*copy() correctly uses __setstate__...
        import copy
        class C(object):
            def __init__(self, foo=None):
                self.foo = foo
                self.__foo = foo
            def setfoo(self, foo=None):
                self.foo = foo
            def getfoo(self):
                return self.__foo
            def __getstate__(self):
                return [self.foo]
            def __setstate__(self_, lst):
                self.assertEqual(len(lst), 1)
                self_.__foo = self_.foo = lst[0]
        a = C(42)
        a.setfoo(24)
        self.assertEqual(a.foo, 24)
        self.assertEqual(a.getfoo(), 42)
        b = copy.copy(a)
        self.assertEqual(b.foo, 24)
        self.assertEqual(b.getfoo(), 24)
        b = copy.deepcopy(a)
        self.assertEqual(b.foo, 24)
        self.assertEqual(b.getfoo(), 24) 
Example 46
Project: pyblish-win   Author: pyblish   File: test_defaultdict.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_deep_copy(self):
        d1 = defaultdict(foobar, {1: [1]})
        d2 = copy.deepcopy(d1)
        self.assertEqual(d2.default_factory, foobar)
        self.assertEqual(d2, d1)
        self.assertTrue(d1[1] is not d2[1])
        d1.default_factory = list
        d2 = copy.deepcopy(d1)
        self.assertEqual(d2.default_factory, list)
        self.assertEqual(d2, d1) 
Example 47
Project: pyblish-win   Author: pyblish   File: test_set.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_deep_copy(self):
        dup = copy.deepcopy(self.set)
        self.assertSetEqual(dup, self.set)

#------------------------------------------------------------------------------ 
Example 48
Project: pyblish-win   Author: pyblish   File: test_itertools.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_count(self):
        self.assertEqual(zip('abc',count()), [('a', 0), ('b', 1), ('c', 2)])
        self.assertEqual(zip('abc',count(3)), [('a', 3), ('b', 4), ('c', 5)])
        self.assertEqual(take(2, zip('abc',count(3))), [('a', 3), ('b', 4)])
        self.assertEqual(take(2, zip('abc',count(-1))), [('a', -1), ('b', 0)])
        self.assertEqual(take(2, zip('abc',count(-3))), [('a', -3), ('b', -2)])
        self.assertRaises(TypeError, count, 2, 3, 4)
        self.assertRaises(TypeError, count, 'a')
        self.assertEqual(list(islice(count(maxsize-5), 10)), range(maxsize-5, maxsize+5))
        self.assertEqual(list(islice(count(-maxsize-5), 10)), range(-maxsize-5, -maxsize+5))
        c = count(3)
        self.assertEqual(repr(c), 'count(3)')
        c.next()
        self.assertEqual(repr(c), 'count(4)')
        c = count(-9)
        self.assertEqual(repr(c), 'count(-9)')
        c.next()
        self.assertEqual(repr(count(10.25)), 'count(10.25)')
        self.assertEqual(c.next(), -8)
        for i in (-sys.maxint-5, -sys.maxint+5 ,-10, -1, 0, 10, sys.maxint-5, sys.maxint+5):
            # Test repr (ignoring the L in longs)
            r1 = repr(count(i)).replace('L', '')
            r2 = 'count(%r)'.__mod__(i).replace('L', '')
            self.assertEqual(r1, r2)

        # check copy, deepcopy, pickle
        for value in -3, 3, sys.maxint-5, sys.maxint+5:
            c = count(value)
            self.assertEqual(next(copy.copy(c)), value)
            self.assertEqual(next(copy.deepcopy(c)), value)
            self.assertEqual(next(pickle.loads(pickle.dumps(c))), value) 
Example 49
Project: pyblish-win   Author: pyblish   File: test_deque.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_deepcopy(self):
        mut = [10]
        d = deque([mut])
        e = copy.deepcopy(d)
        self.assertEqual(list(d), list(e))
        mut[0] = 11
        self.assertNotEqual(id(d), id(e))
        self.assertNotEqual(list(d), list(e)) 
Example 50
Project: pyblish-win   Author: pyblish   File: test_array.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_deepcopy(self):
        import copy
        a = array.array(self.typecode, self.example)
        b = copy.deepcopy(a)
        self.assertNotEqual(id(a), id(b))
        self.assertEqual(a, b) 
Example 51
Project: pyblish-win   Author: pyblish   File: test_urllib2.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def http_open(self, req):
        import mimetools, httplib, copy
        from StringIO import StringIO
        self.requests.append(copy.deepcopy(req))
        if self._count == 0:
            self._count = self._count + 1
            name = httplib.responses[self.code]
            msg = mimetools.Message(StringIO(self.headers))
            return self.parent.error(
                "http", req, MockFile(), self.code, name, msg)
        else:
            self.req = req
            msg = mimetools.Message(StringIO("\r\n\r\n"))
            return MockResponse(200, "OK", msg, "", req.get_full_url()) 
Example 52
Project: pyblish-win   Author: pyblish   File: test_copy.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_copy_inst_getstate_setstate(self):
        class C:
            def __init__(self, foo):
                self.foo = foo
            def __getstate__(self):
                return self.foo
            def __setstate__(self, state):
                self.foo = state
            def __cmp__(self, other):
                return cmp(self.foo, other.foo)
        x = C(42)
        self.assertEqual(copy.copy(x), x)

    # The deepcopy() method 
Example 53
Project: pyblish-win   Author: pyblish   File: test_copy.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_deepcopy_basic(self):
        x = 42
        y = copy.deepcopy(x)
        self.assertEqual(y, x) 
Example 54
Project: pyblish-win   Author: pyblish   File: test_copy.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_deepcopy_memo(self):
        # Tests of reflexive objects are under type-specific sections below.
        # This tests only repetitions of objects.
        x = []
        x = [x, x]
        y = copy.deepcopy(x)
        self.assertEqual(y, x)
        self.assertTrue(y is not x)
        self.assertTrue(y[0] is not x[0])
        self.assertTrue(y[0] is y[1]) 
Example 55
Project: pyblish-win   Author: pyblish   File: test_copy.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_deepcopy_issubclass(self):
        # XXX Note: there's no way to test the TypeError coming out of
        # issubclass() -- this can only happen when an extension
        # module defines a "type" that doesn't formally inherit from
        # type.
        class Meta(type):
            pass
        class C:
            __metaclass__ = Meta
        self.assertEqual(copy.deepcopy(C), C) 
Example 56
Project: pyblish-win   Author: pyblish   File: test_copy.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_deepcopy_deepcopy(self):
        class C(object):
            def __init__(self, foo):
                self.foo = foo
            def __deepcopy__(self, memo=None):
                return C(self.foo)
        x = C(42)
        y = copy.deepcopy(x)
        self.assertEqual(y.__class__, x.__class__)
        self.assertEqual(y.foo, x.foo) 
Example 57
Project: pyblish-win   Author: pyblish   File: test_copy.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_deepcopy_reduce_ex(self):
        class C(object):
            def __reduce_ex__(self, proto):
                return ""
            def __reduce__(self):
                raise test_support.TestFailed, "shouldn't call this"
        x = C()
        y = copy.deepcopy(x)
        self.assertTrue(y is x) 
Example 58
Project: pyblish-win   Author: pyblish   File: test_copy.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_deepcopy_reduce(self):
        class C(object):
            def __reduce__(self):
                return ""
        x = C()
        y = copy.deepcopy(x)
        self.assertTrue(y is x) 
Example 59
Project: pyblish-win   Author: pyblish   File: test_copy.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_deepcopy_cant(self):
        class C(object):
            def __getattribute__(self, name):
                if name.startswith("__reduce"):
                    raise AttributeError, name
                return object.__getattribute__(self, name)
        x = C()
        self.assertRaises(copy.Error, copy.deepcopy, x)

    # Type-specific _deepcopy_xxx() methods 
Example 60
Project: pyblish-win   Author: pyblish   File: test_copy.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_deepcopy_atomic(self):
        class Classic:
            pass
        class NewStyle(object):
            pass
        def f():
            pass
        tests = [None, 42, 2L**100, 3.14, True, False, 1j,
                 "hello", u"hello\u1234", f.func_code,
                 NewStyle, xrange(10), Classic, max]
        for x in tests:
            self.assertTrue(copy.deepcopy(x) is x, repr(x)) 
Example 61
Project: pyblish-win   Author: pyblish   File: test_copy.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_deepcopy_list(self):
        x = [[1, 2], 3]
        y = copy.deepcopy(x)
        self.assertEqual(y, x)
        self.assertTrue(x is not y)
        self.assertTrue(x[0] is not y[0]) 
Example 62
Project: pyblish-win   Author: pyblish   File: test_copy.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_deepcopy_tuple(self):
        x = ([1, 2], 3)
        y = copy.deepcopy(x)
        self.assertEqual(y, x)
        self.assertTrue(x is not y)
        self.assertTrue(x[0] is not y[0]) 
Example 63
Project: pyblish-win   Author: pyblish   File: test_copy.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_deepcopy_reflexive_tuple(self):
        x = ([],)
        x[0].append(x)
        y = copy.deepcopy(x)
        self.assertRaises(RuntimeError, cmp, y, x)
        self.assertTrue(y is not x)
        self.assertTrue(y[0] is not x[0])
        self.assertTrue(y[0][0] is y) 
Example 64
Project: pyblish-win   Author: pyblish   File: test_copy.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_deepcopy_dict(self):
        x = {"foo": [1, 2], "bar": 3}
        y = copy.deepcopy(x)
        self.assertEqual(y, x)
        self.assertTrue(x is not y)
        self.assertTrue(x["foo"] is not y["foo"]) 
Example 65
Project: pyblish-win   Author: pyblish   File: test_copy.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_deepcopy_reflexive_dict(self):
        x = {}
        x['foo'] = x
        y = copy.deepcopy(x)
        self.assertRaises(RuntimeError, cmp, y, x)
        self.assertTrue(y is not x)
        self.assertTrue(y['foo'] is y)
        self.assertEqual(len(y), 1) 
Example 66
Project: pyblish-win   Author: pyblish   File: test_copy.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_deepcopy_keepalive(self):
        memo = {}
        x = 42
        y = copy.deepcopy(x, memo)
        self.assertTrue(memo[id(x)] is x) 
Example 67
Project: pyblish-win   Author: pyblish   File: test_copy.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_deepcopy_inst_deepcopy(self):
        class C:
            def __init__(self, foo):
                self.foo = foo
            def __deepcopy__(self, memo):
                return C(copy.deepcopy(self.foo, memo))
            def __cmp__(self, other):
                return cmp(self.foo, other.foo)
        x = C([42])
        y = copy.deepcopy(x)
        self.assertEqual(y, x)
        self.assertTrue(y is not x)
        self.assertTrue(y.foo is not x.foo) 
Example 68
Project: pyblish-win   Author: pyblish   File: test_copy.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_deepcopy_inst_getinitargs(self):
        class C:
            def __init__(self, foo):
                self.foo = foo
            def __getinitargs__(self):
                return (self.foo,)
            def __cmp__(self, other):
                return cmp(self.foo, other.foo)
        x = C([42])
        y = copy.deepcopy(x)
        self.assertEqual(y, x)
        self.assertTrue(y is not x)
        self.assertTrue(y.foo is not x.foo) 
Example 69
Project: pyblish-win   Author: pyblish   File: test_copy.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_deepcopy_inst_getstate(self):
        class C:
            def __init__(self, foo):
                self.foo = foo
            def __getstate__(self):
                return {"foo": self.foo}
            def __cmp__(self, other):
                return cmp(self.foo, other.foo)
        x = C([42])
        y = copy.deepcopy(x)
        self.assertEqual(y, x)
        self.assertTrue(y is not x)
        self.assertTrue(y.foo is not x.foo) 
Example 70
Project: pyblish-win   Author: pyblish   File: test_copy.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_deepcopy_inst_setstate(self):
        class C:
            def __init__(self, foo):
                self.foo = foo
            def __setstate__(self, state):
                self.foo = state["foo"]
            def __cmp__(self, other):
                return cmp(self.foo, other.foo)
        x = C([42])
        y = copy.deepcopy(x)
        self.assertEqual(y, x)
        self.assertTrue(y is not x)
        self.assertTrue(y.foo is not x.foo) 
Example 71
Project: pyblish-win   Author: pyblish   File: test_copy.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_deepcopy_inst_getstate_setstate(self):
        class C:
            def __init__(self, foo):
                self.foo = foo
            def __getstate__(self):
                return self.foo
            def __setstate__(self, state):
                self.foo = state
            def __cmp__(self, other):
                return cmp(self.foo, other.foo)
        x = C([42])
        y = copy.deepcopy(x)
        self.assertEqual(y, x)
        self.assertTrue(y is not x)
        self.assertTrue(y.foo is not x.foo) 
Example 72
Project: pyblish-win   Author: pyblish   File: test_copy.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_reconstruct_string(self):
        class C(object):
            def __reduce__(self):
                return ""
        x = C()
        y = copy.copy(x)
        self.assertTrue(y is x)
        y = copy.deepcopy(x)
        self.assertTrue(y is x) 
Example 73
Project: pyblish-win   Author: pyblish   File: test_copy.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_reconstruct_nostate(self):
        class C(object):
            def __reduce__(self):
                return (C, ())
        x = C()
        x.foo = 42
        y = copy.copy(x)
        self.assertTrue(y.__class__ is x.__class__)
        y = copy.deepcopy(x)
        self.assertTrue(y.__class__ is x.__class__) 
Example 74
Project: pyblish-win   Author: pyblish   File: test_copy.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_reconstruct_state(self):
        class C(object):
            def __reduce__(self):
                return (C, (), self.__dict__)
            def __cmp__(self, other):
                return cmp(self.__dict__, other.__dict__)
            __hash__ = None # Silence Py3k warning
        x = C()
        x.foo = [42]
        y = copy.copy(x)
        self.assertEqual(y, x)
        y = copy.deepcopy(x)
        self.assertEqual(y, x)
        self.assertTrue(y.foo is not x.foo) 
Example 75
Project: fs_image   Author: facebookincubator   File: deepcopy_test.py    MIT License 4 votes vote down vote up
def _check_deepcopy(
        self,
        gen_fn: Callable[[], Generator[Tuple[str, Any], Any, List[str]]],
        replace_step=None, expected_name=None, *, _replace_by=None,
    ) -> List[str]:
        '''
        Steps through `deepcopy_original`, optionally replacing the ID map
        by deepcopy at a specific step of the test.
        '''
        obj = None
        steps = []
        deepcopy_original = None

        with while_not_exited(gen_fn()) as ctx:
            while True:
                step, obj = ctx.send(obj)
                if len(steps) == replace_step:
                    self.assertEqual(expected_name, step)
                    if _replace_by is None:
                        deepcopy_original = obj
                        obj = copy.deepcopy(obj)
                    else:
                        obj = _replace_by
                steps.append(step)

        # Don't repeat step names
        self.assertEqual([], [s for s, n in Counter(steps).items() if n > 1])

        # We just replaced the map with a deepcopy at a specific step.  Now,
        # we run the test one more time up to the same step, and replace the
        # map with the pre-deepcopy original to ensure it has not changed.
        if replace_step is not None and _replace_by is None:
            self.assertIsNotNone(deepcopy_original)
            with self.subTest(deepcopy_original=True):
                self.assertEqual(steps, self._check_deepcopy(
                    gen_fn,
                    replace_step,
                    expected_name,
                    _replace_by=deepcopy_original,
                ))

        return steps 
Example 76
Project: fs_image   Author: facebookincubator   File: subvolume_set.py    MIT License 4 votes vote down vote up
def new(
        cls, subvol_set: SubvolumeSet, subvol_item: SendStreamItem,
    ) -> 'SubvolumeSetMutator':
        if not isinstance(subvol_item, (
            SendStreamItems.subvol, SendStreamItems.snapshot,
        )):
            raise RuntimeError(f'{subvol_item} must specify subvolume')

        my_id = SubvolumeID(
            uuid=subvol_item.uuid.decode(), transid=subvol_item.transid
        )
        parent_id = SubvolumeID(
            uuid=subvol_item.parent_uuid.decode(),
            transid=subvol_item.parent_transid,
        ) if isinstance(subvol_item, SendStreamItems.snapshot) else None
        description = SubvolumeDescription(
            name=subvol_item.path, id=my_id, parent_id=parent_id,
            name_uuid_prefix_counts=subvol_set.name_uuid_prefix_counts,
        )
        if isinstance(subvol_item, SendStreamItems.snapshot):
            parent_subvol = subvol_set.uuid_to_subvolume[parent_id.uuid]
            # `SubvolumeDescription` references a part `SubvolumeSet`, so it
            # is not correctly `deepcopy`able as part of a `Subvolume`.  And
            # we want to modify the `InodeIDMap`'s `description` in any
            # case, so let's just bulk-replace the old description instance.
            # This would not be sane if the old instance were of a type that
            # may be interned by the runtime, like `int`, hence the assert.
            assert isinstance(
                parent_subvol.id_map.inner.description, SubvolumeDescription
            )
            subvol = copy.deepcopy(parent_subvol, memo={
                id(parent_subvol.id_map.inner.description): description,
            })
        else:
            subvol = Subvolume.new(
                id_map=InodeIDMap.new(description=description),
            )

        dup_subvol = subvol_set.uuid_to_subvolume.get(my_id.uuid)
        if dup_subvol is not None:
            raise RuntimeError(f'{my_id} is already in use: {dup_subvol}')
        subvol_set.uuid_to_subvolume[my_id.uuid] = subvol

        # insertion can fail, so update the description disambiguator last.
        subvol_set.name_uuid_prefix_counts.update(
            description.name_uuid_prefixes()
        )

        return cls(subvolume=subvol, subvolume_set=subvol_set) 
Example 77
Project: fs_image   Author: facebookincubator   File: update_package_db.py    MIT License 4 votes vote down vote up
def _get_updated_db(
    *,
    existing_db: PackageTagDb,
    update_existing: bool,
    create_items: ExplicitUpdates,
    replace_items: ExplicitUpdates,
    get_db_info_fn: GetDbInfoFn,
) -> PackageTagDb:

    # The `updates` map tells us the packages, for which we have to fetch
    # new DB entries.
    updates = {
        package: {tag: {} for tag in tag_to_info}
            for package, tag_to_info in existing_db.items()
    } if update_existing else {}
    # Merge any `ExplictUpdates` into `updates.  "replace" precedes "create"
    # to make us fail if a `--replace` conflicts with a `--create`.
    for explicit_updates in [replace_items, create_items]:
        for package, in_tag_to_update_opts in explicit_updates.items():
            out_tag_to_update_opts = updates.setdefault(package, {})
            for tag, update_opts in in_tag_to_update_opts.items():
                seen_before = (
                    tag in out_tag_to_update_opts or
                    tag in existing_db.get(package, {})
                )
                if explicit_updates is create_items:
                    assert not seen_before, (package, tag)
                elif explicit_updates is replace_items:
                    # Since `replace` is applied first, this will never
                    # erroneously `replace` a conflicting `create`.
                    assert seen_before, (package, tag)
                else:  # pragma: no cover
                    raise AssertionError('Not reached')
                out_tag_to_update_opts[tag] = update_opts

    # If we are not updating the existing DB (i.e. explicit updates only),
    # start with a copy of the existing DB, and replace infos as we go.
    new_db = copy.deepcopy(existing_db) if not update_existing else {}

    # Apply all `updates` to `new_db`
    for package, tag_to_update_opts in updates.items():
        new_tag_to_info = new_db.setdefault(package, {})
        for tag, update_opts in tag_to_update_opts.items():
            log.info(f'Querying {package}:{tag} with options {update_opts}')
            info = get_db_info_fn(package, tag, update_opts)
            new_tag_to_info[tag] = info
            log.info(f'New info for {package}:{tag} -> {info}')

    return new_db 
Example 78
Project: cat-bbs   Author: aleju   File: bbs.py    MIT License 4 votes vote down vote up
def copy(self, rect=None, x1=None, x2=None, y1=None, y2=None, shape=None, class_label=None, score=None):
        """
        rectimg = copy.deepcopy(self)
        if rect is not None:
            rectimg.rect = rect
        if x1 is not None:
            rectimg.x1 = x1
        if x2 is not None:
            rectimg.x2 = x2
        if y1 is not None:
            rectimg.y1 = y1
        if y2 is not None:
            rectimg.y2 = y2
        if shape is not None:
            assert isinstance(shape, tuple)
            rectimg.shape = shape
        if class_label is not None:
            rectimg.class_label = class_label
        if score is not None:
            rectimg.score = score
        #return self.copy_with_new_rect(self.rect.copy())
        """

        # neue variante hier weil deepcopy wohl sehr langsam ist(?)
        if rect is None:
            rectimg = RectangleOnImage(
                x1=self.rect.x1 if x1 is None else x1,
                x2=self.rect.x2 if x2 is None else x2,
                y1=self.rect.y1 if y1 is None else y1,
                y2=self.rect.y2 if y2 is None else y2,
                shape=tuple(self.shape) if shape is None else shape,
                class_label=self.class_label if class_label is None else class_label,
                score=self.score if score is None else score
            )
        else:
            assert x1 is None
            assert x2 is None
            assert y1 is None
            assert y2 is None
            rectimg = RectangleOnImage(
                x1=rect.x1,
                x2=rect.x2,
                y1=rect.y1,
                y2=rect.y2,
                shape=tuple(self.shape) if shape is None else shape,
                class_label=self.class_label if class_label is None else class_label,
                score=self.score if score is None else score
            )

        return rectimg 
Example 79
Project: pyblish-win   Author: pyblish   File: copy.py    GNU Lesser General Public License v3.0 4 votes vote down vote up
def deepcopy(x, memo=None, _nil=[]):
    """Deep copy operation on arbitrary Python objects.

    See the module's __doc__ string for more info.
    """

    if memo is None:
        memo = {}

    d = id(x)
    y = memo.get(d, _nil)
    if y is not _nil:
        return y

    cls = type(x)

    copier = _deepcopy_dispatch.get(cls)
    if copier:
        y = copier(x, memo)
    else:
        try:
            issc = issubclass(cls, type)
        except TypeError: # cls is not a class (old Boost; see SF #502085)
            issc = 0
        if issc:
            y = _deepcopy_atomic(x, memo)
        else:
            copier = getattr(x, "__deepcopy__", None)
            if copier:
                y = copier(memo)
            else:
                reductor = dispatch_table.get(cls)
                if reductor:
                    rv = reductor(x)
                else:
                    reductor = getattr(x, "__reduce_ex__", None)
                    if reductor:
                        rv = reductor(2)
                    else:
                        reductor = getattr(x, "__reduce__", None)
                        if reductor:
                            rv = reductor()
                        else:
                            raise Error(
                                "un(deep)copyable object of type %s" % cls)
                y = _reconstruct(x, rv, 1, memo)

    memo[d] = y
    _keep_alive(x, memo) # Make sure x lives at least as long as d
    return y 
Example 80
Project: pyblish-win   Author: pyblish   File: copy.py    GNU Lesser General Public License v3.0 4 votes vote down vote up
def _reconstruct(x, info, deep, memo=None):
    if isinstance(info, str):
        return x
    assert isinstance(info, tuple)
    if memo is None:
        memo = {}
    n = len(info)
    assert n in (2, 3, 4, 5)
    callable, args = info[:2]
    if n > 2:
        state = info[2]
    else:
        state = {}
    if n > 3:
        listiter = info[3]
    else:
        listiter = None
    if n > 4:
        dictiter = info[4]
    else:
        dictiter = None
    if deep:
        args = deepcopy(args, memo)
    y = callable(*args)
    memo[id(x)] = y

    if state:
        if deep:
            state = deepcopy(state, memo)
        if hasattr(y, '__setstate__'):
            y.__setstate__(state)
        else:
            if isinstance(state, tuple) and len(state) == 2:
                state, slotstate = state
            else:
                slotstate = None
            if state is not None:
                y.__dict__.update(state)
            if slotstate is not None:
                for key, value in slotstate.iteritems():
                    setattr(y, key, value)

    if listiter is not None:
        for item in listiter:
            if deep:
                item = deepcopy(item, memo)
            y.append(item)
    if dictiter is not None:
        for key, value in dictiter:
            if deep:
                key = deepcopy(key, memo)
                value = deepcopy(value, memo)
            y[key] = value
    return y