Python copy.deepcopy() Examples

The following are 30 code examples of copy.deepcopy(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module copy , or try the search function .
Example #1
Source File: money.py    From indras_net with GNU General Public License v3.0 8 votes vote down vote up
def money_trader_action(agent):
    dic1 = copy.deepcopy(agent["goods"])
    ret = seek_a_trade(agent)
    dic2 = copy.deepcopy(agent["goods"])
    diff = {x: (dic1[x][AMT_AVAIL] - dic2[x][AMT_AVAIL])
            for x in dic1 if x in dic2}
    for good in diff:
        # updated due to change in durability calculation
        # decayed_amt = dic1[good][DUR_DECR] * dic1[good][AMT_AVAIL]
        # if (diff[good] != decayed_amt and diff[good] != 0):
        if diff[good] != 0:
            incr_trade_count(good)
    # print("TRADE COUNT")
    # for good in natures_goods:
    #     print(good, " is traded ",
    #           natures_goods[good]["trade_count"], " times")
    # good_decay(agent["goods"])
    return ret 
Example #2
Source File: OptimizationViewer.py    From VMAttack with MIT License 6 votes vote down vote up
def OptimizeTrace(self, check_box):
        self.undo_stack.append(deepcopy(self.trace))
        self.last_cb = check_box
        optimization = self.opti_map[check_box.text()]
        if check_box.isChecked():
            self.order.append(optimization)
            self.trace = optimization(self.trace)
        else:
            try:
                self.order.remove(optimization)
            except:
                pass
            self.trace = deepcopy(self.orig_trace)
            for optimization in self.order:
                self.trace = optimization(self.trace)
        self.FoldRegs() 
Example #3
Source File: test_transformers.py    From mutatest with 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 loc in mast.locs:
        if loc.lineno == lineno and loc.col_offset == 11:
            assert loc.op_type == mut_op
        else:
            assert loc.op_type in {ast.Eq, ast.Is, ast.In}  # based on compare_file fixture 
Example #4
Source File: test_transformers.py    From mutatest with 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 loc in mast.locs:
        # spot check on mutation from Index_NumNeg to Index_NumPos
        if loc.lineno == lineno and loc.col_offset == col_offset:
            assert loc.op_type == test_mutation

        # spot check on not-mutated location still being None
        if loc.lineno == 4 and loc.col_offset == 23:
            assert loc.op_type == "Index_NumPos" 
Example #5
Source File: test_transformers.py    From mutatest with 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 loc in mast.locs:
        if loc.lineno == 2 and loc.col_offset == 11:
            assert loc.op_type == test_mutation 
Example #6
Source File: test_transformers.py    From mutatest with 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 = [loc for loc in mast.locs if loc.ast_class == "NameConstant"]
    assert len(nc_locs) == 4

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

        # spot check on not-mutated location still being None
        if loc.lineno == 7 and loc.col_offset == 22:
            assert loc.op_type is None 
Example #7
Source File: cap_struct.py    From indras_net with GNU General Public License v3.0 6 votes vote down vote up
def create_entr(name, i, props=None):
    """
    Create an agent.
    """
    starting_cash = DEF_ENTR_CASH
    if props is not None:
        starting_cash = get_prop('entr_starting_cash',
                                 DEF_ENTR_CASH)

    resources = copy.deepcopy(DEF_CAP_WANTED)
    if props is not None:
        total_resources = get_prop('entr_want_resource_total',
                                   DEF_TOTAL_RESOURCES_ENTR_WANT)
        num_resources = len(resources)
        for k in resources.keys():
            resources[k] = int((total_resources * 2)
                               * (random.random() / num_resources))

    return Agent(name + str(i), action=entr_action,
                 attrs={"cash": starting_cash,
                        "wants": resources,
                        "have": {}}) 
Example #8
Source File: test_transformers.py    From mutatest with 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 #9
Source File: test_transformers.py    From mutatest with 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 loc in mast.locs:
        # spot check on mutation from Add tp Div
        if loc.lineno == 1 and loc.col_offset == 4:
            assert loc.op_type == test_mutation

        # spot check on not-mutated location still being Mult
        if loc.lineno == 5 and loc.col_offset == 4:
            assert loc.op_type == "AugAssign_Mult" 
Example #10
Source File: messages.py    From wechatpy with MIT License 6 votes vote down vote up
def __new__(mcs, name, bases, attrs):
        for b in bases:
            if not hasattr(b, "_fields"):
                continue

            for k, v in b.__dict__.items():
                if k in attrs:
                    continue
                if isinstance(v, FieldDescriptor):
                    attrs[k] = copy.deepcopy(v.field)

        mcs = super().__new__(mcs, name, bases, attrs)
        mcs._fields = {}

        for name, field in mcs.__dict__.items():
            if isinstance(field, BaseField):
                field.add_to_class(mcs, name)
        return mcs 
Example #11
Source File: io.py    From vergeml with MIT License 6 votes vote down vote up
def __init__(self, args: dict={}):
        self.meta = {}
        self.args = deepcopy(args)

        # since input-patterns can be used in many different kinds of io sources, grab it too.
        self.input_patterns = args.get('input-patterns', '**/*')
        if isinstance(self.input_patterns, str):
            self.input_patterns = self.input_patterns.split(",")
        self.samples_dir = args.get('samples-dir', 'samples')
        self.cache_dir = args.get('cache-dir', '.cache')
        self.random_seed = args.get('random-seed', 42)
        self.trainings_dir = args.get('trainings-dir', './trainings')

        self._cached_file_state = None

        spltype, splval = parse_split(args.get('val-split', '10%'))
        self.val_dir = splval if spltype == 'dir' else None
        self.val_num = splval if spltype == 'num' else None
        self.val_perc = splval if spltype == 'perc' else None

        spltype, splval = parse_split(args.get('test-split', '10%'))
        self.test_dir = splval if spltype == 'dir' else None
        self.test_num = splval if spltype == 'num' else None
        self.test_perc = splval if spltype == 'perc' else None 
Example #12
Source File: OptimizationViewer.py    From VMAttack with MIT License 6 votes vote down vote up
def OptimizeTrace(self, check_box):
        self.undo_stack.append(deepcopy(self.trace))
        self.last_cb = check_box
        optimization = self.opti_map[check_box.text()]
        if check_box.isChecked():
            self.order.append(optimization)
            self.trace = optimization(self.trace)
        else:
            try:
                self.order.remove(optimization)
            except:
                pass
            self.trace = deepcopy(self.orig_trace)
            for optimization in self.order:
                self.trace = optimization(self.trace)
        self.FoldRegs() 
Example #13
Source File: GameOfLife.py    From BiblioPixelAnimations with MIT License 6 votes vote down vote up
def turn(self):
        """Turn"""
        r = (4, 5, 5, 5)
        nt = copy.deepcopy(self.table)
        for z in range(self.depth):
            for y in range(self.height):
                for x in range(self.width):
                    neighbours = self.liveNeighbours(z, y, x)
                    if self.table[z][y][x] == 0 and (neighbours > r[0] and neighbours <= r[1]):
                        nt[z][y][x] = 1
                    elif self.table[z][y][x] == 1 and (neighbours > r[2] and neighbours < r[3]):
                        nt[z][y][x] = 0

        self._oldStates.append(self.table)
        if len(self._oldStates) > 3:
            self._oldStates.popleft()

        self.table = nt 
Example #14
Source File: GameOfLife.py    From BiblioPixelAnimations with MIT License 6 votes vote down vote up
def turn(self):
        """Turn"""
        nt = copy.deepcopy(self.table)
        for y in range(0, self.height):
            for x in range(0, self.width):
                neighbours = self.liveNeighbours(y, x)
                if self.table[y][x] == 0:
                    if neighbours == 3:
                        nt[y][x] = 1
                else:
                    if (neighbours < 2) or (neighbours > 3):
                        nt[y][x] = 0

        self._oldStates.append(self.table)
        if len(self._oldStates) > 3:
            self._oldStates.popleft()

        self.table = nt 
Example #15
Source File: utils.py    From comet-commonsense with Apache License 2.0 6 votes vote down vote up
def update_generation_losses(losses, nums, micro, macro, bs, length, loss):
    # Update Losses
    losses[micro] += \
        [copy.deepcopy(losses[micro][-1])]
    losses[macro] += \
        [copy.deepcopy(losses[macro][-1])]

    losses[micro][-1] *= nums[micro]
    losses[macro][-1] *= nums[macro]

    nums[macro] += bs

    if isinstance(length, int):
        update_indiv_generation_losses(
            losses, nums, micro, macro, bs, length, loss)
    else:
        update_tensor_generation_losses(
            losses, nums, micro, macro, bs, length, loss) 
Example #16
Source File: model2.py    From controllable-text-attribute-transfer with Apache License 2.0 6 votes vote down vote up
def make_model(d_vocab, N, d_model, d_ff=1024, h=4, dropout=0.1):
    """Helper: Construct a model from hyperparameters."""
    c = copy.deepcopy
    attn = MultiHeadedAttention(h, d_model)
    ff = PositionwiseFeedForward(d_model, d_ff, dropout)
    position = PositionalEncoding(d_model, dropout)
    model = EncoderDecoder(
        Encoder(EncoderLayer(d_model, c(attn), c(ff), dropout), N),
        Decoder(DecoderLayer(d_model, c(attn), c(attn), c(ff), dropout), N),
        nn.GRU(d_model, d_model, 1),
        nn.Sequential(Embeddings(d_model, d_vocab), c(position)),
        nn.Sequential(Embeddings(d_model, d_vocab), c(position)),
        Generator(d_model, d_vocab),
        d_model
    )
    # This was important from their code.
    # Initialize parameters with Glorot / fan_avg.
    for p in model.parameters():
        if p.dim() > 1:
            nn.init.xavier_uniform_(p)
    return model 
Example #17
Source File: __init__.py    From drydock with Apache License 2.0 6 votes vote down vote up
def merge_dicts(child_dict, parent_dict):
        if child_dict is None:
            return parent_dict

        if parent_dict is None:
            return child_dict

        effective_dict = {}

        try:
            # Probably should handle non-string keys
            use_keys = filter(lambda x: ("!" + x) not in child_dict.keys(),
                              parent_dict)

            for k in use_keys:
                effective_dict[k] = deepcopy(parent_dict[k])

            use_keys = filter(lambda x: not x.startswith("!"), child_dict)

            for k in use_keys:
                effective_dict[k] = deepcopy(child_dict[k])
        except TypeError:
            raise TypeError("Error iterating dict argument")

        return effective_dict 
Example #18
Source File: model.py    From controllable-text-attribute-transfer with Apache License 2.0 6 votes vote down vote up
def make_model(d_vocab, N, d_model, latent_size, d_ff=1024, h=4, dropout=0.1):
    """Helper: Construct a model from hyperparameters."""
    c = copy.deepcopy
    attn = MultiHeadedAttention(h, d_model)
    ff = PositionwiseFeedForward(d_model, d_ff, dropout)
    position = PositionalEncoding(d_model, dropout)
    share_embedding = Embeddings(d_model, d_vocab)
    model = EncoderDecoder(
        Encoder(EncoderLayer(d_model, c(attn), c(ff), dropout), N),
        Decoder(DecoderLayer(d_model, c(attn), c(attn), c(ff), dropout), N),
        # nn.Sequential(Embeddings(d_model, d_vocab), c(position)),
        # nn.Sequential(Embeddings(d_model, d_vocab), c(position)),
        nn.Sequential(share_embedding, c(position)),
        nn.Sequential(share_embedding, c(position)),
        Generator(d_model, d_vocab),
        c(position),
        d_model,
        latent_size,
    )
    # This was important from their code.
    # Initialize parameters with Glorot / fan_avg.
    for p in model.parameters():
        if p.dim() > 1:
            nn.init.xavier_uniform_(p)
    return model 
Example #19
Source File: model.py    From controllable-text-attribute-transfer with Apache License 2.0 6 votes vote down vote up
def make_model(d_vocab, N, d_model, latent_size, d_ff=1024, h=4, dropout=0.1):
    """Helper: Construct a model from hyperparameters."""
    c = copy.deepcopy
    attn = MultiHeadedAttention(h, d_model)
    ff = PositionwiseFeedForward(d_model, d_ff, dropout)
    position = PositionalEncoding(d_model, dropout)
    share_embedding = Embeddings(d_model, d_vocab)
    model = EncoderDecoder(
        Encoder(EncoderLayer(d_model, c(attn), c(ff), dropout), N),
        Decoder(DecoderLayer(d_model, c(attn), c(attn), c(ff), dropout), N),
        # nn.Sequential(Embeddings(d_model, d_vocab), c(position)),
        # nn.Sequential(Embeddings(d_model, d_vocab), c(position)),
        nn.Sequential(share_embedding, c(position)),
        nn.Sequential(share_embedding, c(position)),
        Generator(d_model, d_vocab),
        c(position),
        d_model,
        latent_size,
    )
    # This was important from their code.
    # Initialize parameters with Glorot / fan_avg.
    for p in model.parameters():
        if p.dim() > 1:
            nn.init.xavier_uniform_(p)
    return model 
Example #20
Source File: model2.py    From controllable-text-attribute-transfer with Apache License 2.0 6 votes vote down vote up
def make_model(d_vocab, N, d_model, d_ff=1024, h=4, dropout=0.1):
    """Helper: Construct a model from hyperparameters."""
    c = copy.deepcopy
    attn = MultiHeadedAttention(h, d_model)
    ff = PositionwiseFeedForward(d_model, d_ff, dropout)
    position = PositionalEncoding(d_model, dropout)
    model = EncoderDecoder(
        Encoder(EncoderLayer(d_model, c(attn), c(ff), dropout), N),
        Decoder(DecoderLayer(d_model, c(attn), c(attn), c(ff), dropout), N),
        nn.GRU(d_model, d_model, 1),
        nn.Sequential(Embeddings(d_model, d_vocab), c(position)),
        nn.Sequential(Embeddings(d_model, d_vocab), c(position)),
        Generator(d_model, d_vocab),
        d_model
    )
    # This was important from their code.
    # Initialize parameters with Glorot / fan_avg.
    for p in model.parameters():
        if p.dim() > 1:
            nn.init.xavier_uniform_(p)
    return model 
Example #21
Source File: model2.py    From controllable-text-attribute-transfer with Apache License 2.0 5 votes vote down vote up
def clones(module, N):
    """Produce N identical layers."""
    return nn.ModuleList([copy.deepcopy(module) for _ in range(N)]) 
Example #22
Source File: TraceAnalysis.py    From VMAttack with MIT License 5 votes vote down vote up
def find_output(trace, manual=False, update=None):
    """
    Find output operands to the vm_function.
    :param trace: instruction trace
    :param manual: console output y/n
    :return: set of output operands
    """
    if update is not None:
        update.pbar_update(20)
    ex_trace, vmp_seg_start, vmp_seg_end = extract_vm_segment(deepcopy(trace))
    ex_trace.reverse()
    if update is not None:
        update.pbar_update(20)
    pop_lines = []
    lastline = ''
    ctx = {}
    for line in ex_trace:
        if line.disasm[0].startswith('ret'):
            ctx = line.ctx
            lastline = line
            break
        elif line.disasm[0].startswith('pop'):
            ctx = line.ctx
            lastline = line
            break
    if update is not None:
        update.pbar_update(40)
    if manual:
        print ''.join('%s:%s\n' % (c, ctx[c]) for c in ctx.keys() if get_reg_class(c) is not None)
    return set([ctx[get_reg(reg, trace.ctx_reg_size)].upper() for reg in ctx if get_reg_class(reg) is not None]) 
Example #23
Source File: modeling.py    From BERT-Classification-Tutorial with 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 #24
Source File: model2.py    From controllable-text-attribute-transfer with Apache License 2.0 5 votes vote down vote up
def clones(module, N):
    """Produce N identical layers."""
    return nn.ModuleList([copy.deepcopy(module) for _ in range(N)]) 
Example #25
Source File: pre_submission.py    From MPContribs with MIT License 5 votes vote down vote up
def run(mpfile, nmax=None):
    # print json.dumps(mpfile.document, indent=4)
    print mpfile.document["_hdata"].keys()
    #    datasource = mpfile.document['general'].pop('Datasource')
    datasource = mpfile.document["_hdata"]["general"].pop("input_file")
    subdir = os.path.abspath(
        os.path.join(datasource["work_dir"], datasource["directory"])
    )

    # TODO Potentially we have to insert a preprocessing step, probably in msp
    scandata_f = msp.read_scans(subdir, datacounter="Counter 1")
    scan_groups = scandata_f.groupby(datasource["group_by"].split())
    process_template = mpfile.document["general"].pop("process_template")
    translate = get_translate(datasource["work_dir"])
    keys = scan_groups.groups.keys()
    keys.sort()

    for i, g in enumerate(tqdm(keys, leave=True)):
        # TODO: Group information is saved into the output. Rethink?
        comp, sx, sy = translate(g)
        composition = normalize_root_level(comp)[1]
        process_template_copy = copy.deepcopy(process_template)
        process_template_copy["position"] = {"x": sx, "y": sy}
        mpfile.document.rec_update(
            nest_dict(process_template_copy, [composition, "process_chain"])
        )
        sg = scan_groups.get_group(g)
        for process_chain_name in process_template.keys():
            scan_params = mpfile.document[composition]["process_chain"][
                process_chain_name
            ]
            xmcd_frame = treat_xmcd(sg, scan_params, xas_process.process_dict)
            mpfile.add_data_table(
                composition,
                xmcd_frame[["Energy", "XAS", "XMCD"]],
                "_".join(["data", process_chain_name]),
            )
        if nmax is not None and i > nmax:
            break 
Example #26
Source File: model.py    From controllable-text-attribute-transfer with Apache License 2.0 5 votes vote down vote up
def clones(module, N):
    """Produce N identical layers."""
    return nn.ModuleList([copy.deepcopy(module) for _ in range(N)]) 
Example #27
Source File: model2.py    From controllable-text-attribute-transfer with Apache License 2.0 5 votes vote down vote up
def clones(module, N):
    """Produce N identical layers."""
    return nn.ModuleList([copy.deepcopy(module) for _ in range(N)]) 
Example #28
Source File: ClusterViewer.py    From VMAttack with MIT License 5 votes vote down vote up
def ClusterRemoval(self):
        threshold = AskLong(1, 'How many most common clusters do you want removed?')
        self.undo_stack.append(deepcopy(self.trace))
        self.trace = cluster_removal(deepcopy(self.trace), threshold=threshold)
        self.PopulateModel() 
Example #29
Source File: model.py    From controllable-text-attribute-transfer with Apache License 2.0 5 votes vote down vote up
def clones(module, N):
    """Produce N identical layers."""
    return nn.ModuleList([copy.deepcopy(module) for _ in range(N)]) 
Example #30
Source File: model.py    From controllable-text-attribute-transfer with Apache License 2.0 5 votes vote down vote up
def clones(module, N):
    """Produce N identical layers."""
    return nn.ModuleList([copy.deepcopy(module) for _ in range(N)])