Python copy.deepcopy() Examples
The following are 30 code examples for showing how to use copy.deepcopy(). These examples are extracted from open source projects. 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 check out the related API usage on the sidebar.
You may also want to check out all available functions/classes of the module
copy
, or try the search function
.
Example 1
Project: indras_net Author: gcallah File: money.py License: GNU General Public License v3.0 | 6 votes |
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
Project: indras_net Author: gcallah File: cap_struct.py License: GNU General Public License v3.0 | 6 votes |
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 3
Project: mutatest Author: EvanKepner File: test_transformers.py License: MIT License | 6 votes |
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 4
Project: mutatest Author: EvanKepner File: test_transformers.py License: MIT License | 6 votes |
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 5
Project: mutatest Author: EvanKepner File: test_transformers.py License: MIT License | 6 votes |
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
Project: mutatest Author: EvanKepner File: test_transformers.py License: MIT License | 6 votes |
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 7
Project: mutatest Author: EvanKepner File: test_transformers.py License: MIT License | 6 votes |
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 8
Project: mutatest Author: EvanKepner File: test_transformers.py License: MIT License | 6 votes |
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 9
Project: vergeml Author: mme File: io.py License: MIT License | 6 votes |
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 10
Project: drydock Author: airshipit File: __init__.py License: Apache License 2.0 | 6 votes |
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 11
Project: wechatpy Author: wechatpy File: messages.py License: MIT License | 6 votes |
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 12
Project: controllable-text-attribute-transfer Author: Nrgeup File: model2.py License: Apache License 2.0 | 6 votes |
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 13
Project: controllable-text-attribute-transfer Author: Nrgeup File: model.py License: Apache License 2.0 | 6 votes |
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 14
Project: controllable-text-attribute-transfer Author: Nrgeup File: model.py License: Apache License 2.0 | 6 votes |
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 15
Project: controllable-text-attribute-transfer Author: Nrgeup File: model2.py License: Apache License 2.0 | 6 votes |
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 16
Project: comet-commonsense Author: atcbosselut File: utils.py License: Apache License 2.0 | 6 votes |
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 17
Project: BiblioPixelAnimations Author: ManiacalLabs File: GameOfLife.py License: MIT License | 6 votes |
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 18
Project: BiblioPixelAnimations Author: ManiacalLabs File: GameOfLife.py License: MIT License | 6 votes |
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 19
Project: VMAttack Author: anatolikalysch File: OptimizationViewer.py License: MIT License | 6 votes |
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 20
Project: VMAttack Author: anatolikalysch File: OptimizationViewer.py License: MIT License | 6 votes |
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 21
Project: BERT-Classification-Tutorial Author: Socialbird-AILab File: modeling.py License: Apache License 2.0 | 5 votes |
def to_dict(self): """Serializes this instance to a Python dictionary.""" output = copy.deepcopy(self.__dict__) return output
Example 22
Project: indras_net Author: gcallah File: trade_utils.py License: GNU General Public License v3.0 | 5 votes |
def transfer(to_goods, from_goods, good_nm, amt=None, comp=None): """ Transfer goods between two goods dicts. Use `amt` if it is not None. """ nature = copy.deepcopy(from_goods) if not amt: amt = from_goods[good_nm][AMT_AVAIL] for good in from_goods: if good in to_goods: amt_before_add = to_goods[good][AMT_AVAIL] else: amt_before_add = 0 to_goods[good] = nature[good] if good != good_nm: to_goods[good][AMT_AVAIL] = amt_before_add else: from_goods[good][AMT_AVAIL] -= amt to_goods[good][AMT_AVAIL] = amt_before_add + amt if comp: for g in to_goods: if to_goods[g][AMT_AVAIL] > 0: to_goods[g]['incr'] += amt * STEEP_GRADIENT comp_list = to_goods[g][COMPLEMENTS] for comp in comp_list: to_goods[comp]['incr'] += STEEP_GRADIENT * amt
Example 23
Project: indras_net Author: gcallah File: cap_struct.py License: GNU General Public License v3.0 | 5 votes |
def create_rholder(name, i, props=None): """ Create an agent. """ k_price = DEF_K_PRICE resources = copy.deepcopy(DEF_CAP_WANTED) num_resources = len(resources) price_list = copy.deepcopy(DEF_EACH_CAP_PRICE) if props is not None: k_price = props.get('cap_price', DEF_K_PRICE) for k in price_list.keys(): price_list[k] = float("{0:.2f}".format(float(k_price * random.uniform(0.5, 1.5)))) starting_cash = DEF_RHOLDER_CASH if props is not None: starting_cash = get_prop('rholder_starting_cash', DEF_RHOLDER_CASH) if props is not None: total_resources = get_prop('rholder_starting_resource_total', DEF_TOTAL_RESOURCES_RHOLDER_HAVE) for k in resources.keys(): resources[k] = int((total_resources * 2) * (random.random() / num_resources)) return Agent(name + str(i), action=rholder_action, attrs={"cash": starting_cash, "resources": resources, "price": price_list})
Example 24
Project: indras_net Author: gcallah File: test_trade_utils.py License: GNU General Public License v3.0 | 5 votes |
def test_rand_dist(self): """ Test if trader dic and nature dic are changed after random distribution trade """ trader_before_trade = copy.deepcopy(self.trader["goods"]) nature_before_trade = copy.deepcopy(self.goods) rand_dist(self.trader["goods"], self.goods) print(repr(nature_before_trade)) print(repr(self.goods)) self.assertNotEqual(self.trader["goods"], trader_before_trade) self.assertNotEqual(self.goods, nature_before_trade)
Example 25
Project: mutatest Author: EvanKepner File: test_transformers.py License: MIT License | 5 votes |
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 loc in mast.locs: if ( loc.lineno == 6 and loc.col_offset == 11 and loc.end_lineno == end_lineno and loc.end_col_offset == end_col_offset ): assert loc.op_type == test_mutation
Example 26
Project: jumpserver-python-sdk Author: jumpserver File: test_auth.py License: GNU General Public License v2.0 | 5 votes |
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 27
Project: jumpserver-python-sdk Author: jumpserver File: test_auth.py License: GNU General Public License v2.0 | 5 votes |
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 28
Project: jumpserver-python-sdk Author: jumpserver File: test_auth.py License: GNU General Public License v2.0 | 5 votes |
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 29
Project: jumpserver-python-sdk Author: jumpserver File: test_auth.py License: GNU General Public License v2.0 | 5 votes |
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 30
Project: vergeml Author: mme File: display.py License: MIT License | 5 votes |
def __init__(self, data, style='default', separate='header', terminal=TERMINAL, pad=1, left_align=set()): assert separate in ('header', 'row', 'none') assert style in ('default', 'ascii', 'no-round') self.style = style self.separate = separate self.pad = pad # make all rows the same length max_data = max(map(len, data)) for row in data: if len(row) < max_data: row.extend([""] * (max_data - len(row))) self._original_data = deepcopy(data) self.data = data for ix in range(max_data): col_size = max(map(lambda r: len(str(r[ix])), self.data)) for j, row in enumerate(self.data) or ix in left_align: if j == 0 or ix in left_align: row[ix] = str(" " * pad) + str(row[ix]).ljust(col_size) + str(" " * pad) else: row[ix] = str(" " * pad) + str(row[ix]).rjust(col_size) + str(" " * pad) self.terminal = terminal self.left_align = left_align