Python copy.copy() Examples
The following are 30
code examples of copy.copy().
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: composite.py From indras_net with GNU General Public License v3.0 | 6 votes |
def __add__(self, other): """ This implements set union and returns a new Composite that is self union other. If other is an atomic agent, just add it to this group. """ if other is None: return self new_dict = copy(self.members) if is_composite(other): new_dict.update(other.members) else: new_dict[other.name] = other new_grp = grp_from_nm_dict(self.name + "+" + other.name, new_dict) self.add_group(new_grp) other.add_group(new_grp) return new_grp
Example #2
Source File: __main__.py From vergeml with MIT License | 6 votes |
def _forgive_wrong_option_order(argv): first_part = [] second_part = [] rest = copy(argv) while rest: arg = rest.pop(0) if arg.startswith("--"): argname = arg.lstrip("--") if "=" in argname: argname = argname.split("=")[0] is_vergeml_opt = bool(argname in _VERGEML_OPTION_NAMES) lst = (first_part if is_vergeml_opt else second_part) if arg.endswith("=") or not "=" in arg: if not rest: # give up second_part.append(arg) else: lst.append(arg) lst.append(rest.pop(0)) else: lst.append(arg) else: second_part.append(arg) return first_part + second_part
Example #3
Source File: bbs.py From cat-bbs with MIT License | 6 votes |
def add_border(self, val, img_shape=None): if val == 0: return self.copy() else: if isinstance(val, int): rect = Rectangle(x1=self.x1-val, x2=self.x2+val, y1=self.y1-val, y2=self.y2+val) elif isinstance(val, float): rect = Rectangle(x1=int(self.x1 - self.width*val), x2=int(self.x2 + self.width*val), y1=int(self.y1 - self.height*val), y2=int(self.y2 + self.height*val)) elif isinstance(val, tuple): assert len(val) == 4, str(len(val)) if all([isinstance(subval, int) for subval in val]): rect = Rectangle(x1=self.x1-val[3], x2=self.x2+val[1], y1=self.y1-val[0], y2=self.y2+val[2]) elif all([isinstance(subval, float) or subval == 0 for subval in val]): # "or subval==0" da sonst zB (0.1, 0, 0.1, 0) einen fehler erzeugt (0 ist int) rect = Rectangle(x1=int(self.x1 - self.width*val[3]), x2=int(self.x2 + self.width*val[1]), y1=int(self.y1 - self.height*val[0]), y2=int(self.y2 + self.height*val[2])) else: raise Exception("Tuple of all ints or tuple of all floats expected, got %s" % (str([type(v) for v in val]),)) else: raise Exception("int or float or tuple of ints/floats expected, got %s" % (type(val),)) if img_shape is not None: rect.fix_by_image_dimensions(height=img_shape[0], width=img_shape[1]) return rect
Example #4
Source File: bbs.py From cat-bbs with MIT License | 6 votes |
def draw_on_image(self, img, color=[0, 255, 0], alpha=1.0, copy=True, from_img=None): if copy: img = np.copy(img) orig_dtype = img.dtype if alpha != 1.0 and img.dtype != np.float32: img = img.astype(np.float32, copy=False) for rect in self: if from_img is not None: rect.resize(from_img, img).draw_on_image(img, color=color, alpha=alpha, copy=False) else: rect.draw_on_image(img, color=color, alpha=alpha, copy=False) if orig_dtype != img.dtype: img = img.astype(orig_dtype, copy=False) return img
Example #5
Source File: GameOfLife.py From BiblioPixelAnimations with 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 #6
Source File: GameOfLife.py From BiblioPixelAnimations with MIT License | 6 votes |
def create_time_table(self, t): t = time.localtime(t) hr = t.tm_hour if not self.mil_time: hr = hr % 12 hrs = str(hr).zfill(2) mins = str(t.tm_min).zfill(2) val = hrs + ":" + mins w, h = font.str_dim(val, font=self.font_name, font_scale=self.scale, final_sep=False) x = (self.width - w) // 2 y = (self.height - h) // 2 old_buf = copy.copy(self.layout.colors) self.layout.all_off() self.layout.drawText(val, x, y, color=COLORS.Red, font=self.font_name, font_scale=self.scale) table = [] for y in range(self.height): table.append([0] * self.width) for x in range(self.width): table[y][x] = int(any(self.layout.get(x, y))) self.layout.setBuffer(old_buf) return table
Example #7
Source File: zmirror.py From zmirror with MIT License | 6 votes |
def generate_our_response(): """ 生成我们的响应 :rtype: Response """ # copy and parse remote response resp = copy_response(is_streamed=parse.streamed_our_response) if parse.time["req_time_header"] >= 0.00001: parse.set_extra_resp_header('X-Header-Req-Time', "%.4f" % parse.time["req_time_header"]) if parse.time.get("start_time") is not None and not parse.streamed_our_response: # remote request time should be excluded when calculating total time parse.set_extra_resp_header('X-Body-Req-Time', "%.4f" % parse.time["req_time_body"]) parse.set_extra_resp_header('X-Compute-Time', "%.4f" % (process_time() - parse.time["start_time"])) parse.set_extra_resp_header('X-Powered-By', 'zmirror/%s' % CONSTS.__VERSION__) if developer_dump_all_traffics and not parse.streamed_our_response: dump_zmirror_snapshot("traffic") return resp
Example #8
Source File: fake_cloud_client_test.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_copy(self): entity1 = fake_cloud_client.make_entity(('abc', '1')) entity1['k1'] = ['v1'] self.assertEqual(entity1.key, fake_cloud_client.FakeDatastoreKey('abc', '1')) self.assertEqual(dict(entity1), {'k1': ['v1']}) entity2 = copy.copy(entity1) entity2['k1'].append('v2') entity2['k3'] = 'v3' self.assertIsInstance(entity2, fake_cloud_client.FakeDatastoreEntity) self.assertEqual(entity1.key, fake_cloud_client.FakeDatastoreKey('abc', '1')) self.assertEqual(dict(entity1), {'k1': ['v1', 'v2']}) self.assertEqual(entity2.key, fake_cloud_client.FakeDatastoreKey('abc', '1')) self.assertEqual(dict(entity2), {'k1': ['v1', 'v2'], 'k3': 'v3'})
Example #9
Source File: fake_cloud_client_test.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_deep_copy(self): entity1 = fake_cloud_client.make_entity(('abc', '1')) entity1['k1'] = ['v1'] self.assertEqual(entity1.key, fake_cloud_client.FakeDatastoreKey('abc', '1')) self.assertEqual(dict(entity1), {'k1': ['v1']}) entity2 = copy.deepcopy(entity1) entity2['k1'].append('v2') entity2['k3'] = 'v3' self.assertIsInstance(entity2, fake_cloud_client.FakeDatastoreEntity) self.assertEqual(entity1.key, fake_cloud_client.FakeDatastoreKey('abc', '1')) self.assertEqual(dict(entity1), {'k1': ['v1']}) self.assertEqual(entity2.key, fake_cloud_client.FakeDatastoreKey('abc', '1')) self.assertEqual(dict(entity2), {'k1': ['v1', 'v2'], 'k3': 'v3'})
Example #10
Source File: filemap.py From neuropythy with GNU Affero General Public License v3.0 | 6 votes |
def _check_tarball(self, *path_parts): rpath = self.join(*path_parts) # start by checking our base path: if self.base_path is not None and self.base_path != '': (tbloc, tbinternal) = split_tarball_path(self.base_path) if tbloc is not None: if tbinternal == '': # we're fine; we just need to cache the current file... return (self._cache_tarball(''), rpath) else: # We copy ourselves to handle this base-path tmp = copy.copy(self) object.__setattr__(tmp, 'base_path', tbloc) rpath = self.join(tbinternal, rpath) # we defer to this path object with the new relative path: return (tmp._cache_tarball(''), rpath) # okay, next check the relative path fpath = self.join('' if self.base_path is None else self.base_path, rpath) (tbloc, tbinternal) = split_tarball_path(fpath) if tbloc is not None: tbp = self._cache_tarball(tbloc) return (tbp, tbinternal) # otherwise, we have no tarball on the path and just need to return ourselves as we are: return (self, rpath)
Example #11
Source File: non_matching_header.py From goodtables-py with MIT License | 6 votes |
def _check_without_ordering(cells): errors = [] for cell in copy(cells): if cell.get('field') is not None: header = cell.get('header') if header != cell['field'].name and header is not None: # Add error message_substitutions = { 'field_name': '"{}"'.format(cell['field'].name), 'header': '"{}"'.format(cell.get('header')), } error = Error( 'non-matching-header', cell, message_substitutions=message_substitutions ) errors.append(error) if _slugify(header) != _slugify(cell['field'].name): # Remove cell cells.remove(cell) return errors
Example #12
Source File: missing_value.py From goodtables-py with MIT License | 6 votes |
def missing_value(cells): """ missing-value: A row has less columns than the header. """ errors = [] for cell in copy(cells): # Skip if cell has value # There is a difference between: # - not having value at all - there is no `value` key # - having a value which is falsy (None, False, '', etc) # (so we don't use something like `if cell.get('value')`) if 'value' in cell or cell.get('is-virtual'): continue # Add error error = Error('missing-value', cell) errors.append(error) # Remove cell cells.remove(cell) return errors
Example #13
Source File: replay_memory.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
def copy(self): # TODO Test the copy function replay_memory = copy.copy(self) replay_memory.states = numpy.zeros(self.states.shape, dtype=self.states.dtype) replay_memory.actions = numpy.zeros(self.actions.shape, dtype=self.actions.dtype) replay_memory.rewards = numpy.zeros(self.rewards.shape, dtype='float32') replay_memory.terminate_flags = numpy.zeros(self.terminate_flags.shape, dtype='bool') replay_memory.states[numpy.arange(self.top-self.size, self.top), ::] = \ self.states[numpy.arange(self.top-self.size, self.top)] replay_memory.actions[numpy.arange(self.top-self.size, self.top)] = \ self.actions[numpy.arange(self.top-self.size, self.top)] replay_memory.rewards[numpy.arange(self.top-self.size, self.top)] = \ self.rewards[numpy.arange(self.top-self.size, self.top)] replay_memory.terminate_flags[numpy.arange(self.top-self.size, self.top)] = \ self.terminate_flags[numpy.arange(self.top-self.size, self.top)] return replay_memory
Example #14
Source File: block.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
def initialize(self, init=initializer.Uniform(), ctx=None, verbose=False, force_reinit=False): """Initializes :py:class:`Parameter` s of this :py:class:`Block` and its children. Equivalent to ``block.collect_params().initialize(...)`` Parameters ---------- init : Initializer Global default Initializer to be used when :py:meth:`Parameter.init` is ``None``. Otherwise, :py:meth:`Parameter.init` takes precedence. ctx : Context or list of Context Keeps a copy of Parameters on one or many context(s). verbose : bool, default False Whether to verbosely print out details on initialization. force_reinit : bool, default False Whether to force re-initialization if parameter is already initialized. """ self.collect_params().initialize(init, ctx, verbose, force_reinit)
Example #15
Source File: utils.py From pypika with Apache License 2.0 | 6 votes |
def builder(func: Callable) -> Callable: """ Decorator for wrapper "builder" functions. These are functions on the Query class or other classes used for building queries which mutate the query and return self. To make the build functions immutable, this decorator is used which will deepcopy the current instance. This decorator will return the return value of the inner function or the new copy of the instance. The inner function does not need to return self. """ import copy def _copy(self, *args, **kwargs): self_copy = copy.copy(self) if getattr(self, "immutable", True) else self result = func(self_copy, *args, **kwargs) # Return self if the inner function returns None. This way the inner function can return something # different (for example when creating joins, a different builder is returned). if result is None: return self_copy return result return _copy
Example #16
Source File: queries.py From pypika with Apache License 2.0 | 6 votes |
def replace_table(self, current_table: Optional[Table], new_table: Optional[Table]) -> "JoinUsing": """ Replaces all occurrences of the specified table with the new table. Useful when reusing fields across queries. :param current_table: The table to be replaced. :param new_table: The table to replace with. :return: A copy of the join with the tables replaced. """ self.item = new_table if self.item == current_table else self.item self.fields = [ field.replace_table(current_table, new_table) for field in self.fields ]
Example #17
Source File: tf_util.py From cs294-112_hws with MIT License | 6 votes |
def switch(condition, then_expression, else_expression): '''Switches between two operations depending on a scalar value (int or bool). Note that both `then_expression` and `else_expression` should be symbolic tensors of the *same shape*. # Arguments condition: scalar tensor. then_expression: TensorFlow operation. else_expression: TensorFlow operation. ''' x_shape = copy.copy(then_expression.get_shape()) x = tf.cond(tf.cast(condition, 'bool'), lambda: then_expression, lambda: else_expression) x.set_shape(x_shape) return x # Extras # ----------------------------------------
Example #18
Source File: image_transformer_2d.py From fine-lm with MIT License | 6 votes |
def body(self, features): hparams = copy.copy(self._hparams) inputs = features["inputs"] targets = features["targets"] targets_shape = common_layers.shape_list(targets) if not (tf.get_variable_scope().reuse or hparams.mode == tf.contrib.learn.ModeKeys.INFER): tf.summary.image("targets", targets, max_outputs=1) decoder_input, rows, cols = cia.prepare_decoder( targets, hparams) # Add class label to decoder input. if not hparams.unconditional: decoder_input += tf.reshape(inputs, [targets_shape[0], 1, 1, hparams.hidden_size]) decoder_output = cia.transformer_decoder_layers( decoder_input, None, hparams.num_decoder_layers, hparams, attention_type=hparams.dec_attention_type, name="decoder") output = cia.create_output(decoder_output, rows, cols, targets, hparams) return output
Example #19
Source File: lstm.py From fine-lm with MIT License | 6 votes |
def lstm_seq2seq_internal_attention_bid_encoder(inputs, targets, hparams, train): """LSTM seq2seq model with attention, main step used for training.""" with tf.variable_scope("lstm_seq2seq_attention_bid_encoder"): inputs_length = common_layers.length_from_embedding(inputs) # Flatten inputs. inputs = common_layers.flatten4d3d(inputs) # LSTM encoder. encoder_outputs, final_encoder_state = lstm_bid_encoder( inputs, inputs_length, hparams, train, "encoder") # LSTM decoder with attention shifted_targets = common_layers.shift_right(targets) # Add 1 to account for the padding added to the left from shift_right targets_length = common_layers.length_from_embedding(shifted_targets) + 1 hparams_decoder = copy.copy(hparams) hparams_decoder.hidden_size = 2 * hparams.hidden_size decoder_outputs = lstm_attention_decoder( common_layers.flatten4d3d(shifted_targets), hparams_decoder, train, "decoder", final_encoder_state, encoder_outputs, inputs_length, targets_length) return tf.expand_dims(decoder_outputs, axis=2)
Example #20
Source File: tf_util.py From lirpg with MIT License | 6 votes |
def switch(condition, then_expression, else_expression): """Switches between two operations depending on a scalar value (int or bool). Note that both `then_expression` and `else_expression` should be symbolic tensors of the *same shape*. # Arguments condition: scalar tensor. then_expression: TensorFlow operation. else_expression: TensorFlow operation. """ x_shape = copy.copy(then_expression.get_shape()) x = tf.cond(tf.cast(condition, 'bool'), lambda: then_expression, lambda: else_expression) x.set_shape(x_shape) return x # ================================================================ # Extras # ================================================================
Example #21
Source File: ddpg.py From lirpg with MIT License | 6 votes |
def setup_param_noise(self, normalized_obs0): assert self.param_noise is not None # Configure perturbed actor. param_noise_actor = copy(self.actor) param_noise_actor.name = 'param_noise_actor' self.perturbed_actor_tf = param_noise_actor(normalized_obs0) logger.info('setting up param noise') self.perturb_policy_ops = get_perturbed_actor_updates(self.actor, param_noise_actor, self.param_noise_stddev) # Configure separate copy for stddev adoption. adaptive_param_noise_actor = copy(self.actor) adaptive_param_noise_actor.name = 'adaptive_param_noise_actor' adaptive_actor_tf = adaptive_param_noise_actor(normalized_obs0) self.perturb_adaptive_policy_ops = get_perturbed_actor_updates(self.actor, adaptive_param_noise_actor, self.param_noise_stddev) self.adaptive_policy_distance = tf.sqrt(tf.reduce_mean(tf.square(self.actor_tf - adaptive_actor_tf)))
Example #22
Source File: ddpg.py From lirpg with MIT License | 6 votes |
def adapt_param_noise(self): if self.param_noise is None: return 0. # Perturb a separate copy of the policy to adjust the scale for the next "real" perturbation. batch = self.memory.sample(batch_size=self.batch_size) self.sess.run(self.perturb_adaptive_policy_ops, feed_dict={ self.param_noise_stddev: self.param_noise.current_stddev, }) distance = self.sess.run(self.adaptive_policy_distance, feed_dict={ self.obs0: batch['obs0'], self.param_noise_stddev: self.param_noise.current_stddev, }) mean_distance = MPI.COMM_WORLD.allreduce(distance, op=MPI.SUM) / MPI.COMM_WORLD.Get_size() self.param_noise.adapt(mean_distance) return mean_distance
Example #23
Source File: lex.py From SublimeKSP with GNU General Public License v3.0 | 6 votes |
def get_caller_module_dict(levels): try: raise RuntimeError except RuntimeError: e,b,t = sys.exc_info() f = t.tb_frame while levels > 0: f = f.f_back levels -= 1 ldict = f.f_globals.copy() if f.f_globals != f.f_locals: ldict.update(f.f_locals) return ldict # ----------------------------------------------------------------------------- # _funcs_to_names() # # Given a list of regular expression functions, this converts it to a list # suitable for output to a table file # -----------------------------------------------------------------------------
Example #24
Source File: composite.py From indras_net with GNU General Public License v3.0 | 5 votes |
def __sub__(self, other): """ This implements set difference and returns a new Composite that is self - other. """ new_dict = copy(self.members) if is_composite(other): for mem in other.members: if mem in self.members: del new_dict[mem] else: if other.name in self: del new_dict[other.name] return grp_from_nm_dict(self.name + "-" + other.name, new_dict)
Example #25
Source File: composite.py From indras_net with GNU General Public License v3.0 | 5 votes |
def __mul__(self, other): """ This implements set intersection and returns a new Composite that is self intersect other. This has no useful meaning if `other` is an atom. """ new_dict = copy(self.members) for mbr in self.members: if mbr not in other.members: del new_dict[mbr] return grp_from_nm_dict(str(self) + "X" + str(other), new_dict)
Example #26
Source File: augment.py From vergeml with MIT License | 5 votes |
def transform_sample(self, sample): if self.apply.intersection(set(SPLITS)) \ and sample.meta['split'] not in self.apply: yield sample else: for _ in range(self.variants): yield copy(sample)
Example #27
Source File: evillib.py From wafw00f with BSD 3-Clause "New" or "Revised" License | 5 votes |
def __init__(self, target='https://example.com', debuglevel=0, path='/', proxies=None, redir=True, head=None): self.target = target self.debuglevel = debuglevel self.requestnumber = 0 self.path = path self.redirectno = 0 self.allowredir = redir self.proxies = proxies self.log = logging.getLogger('wafw00f') if head: self.headers = head else: self.headers = copy(def_headers) #copy object by value not reference. Fix issue #90
Example #28
Source File: bbs.py From cat-bbs with MIT License | 5 votes |
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 #29
Source File: bbs.py From cat-bbs with MIT License | 5 votes |
def draw_on_image(self, img, color=[0, 255, 0], alpha=1.0, thickness=1, copy=copy): assert img.dtype in [np.uint8, np.float32, np.int32, np.int64] result = np.copy(img) if copy else img for i in range(thickness): y = [self.y1-i, self.y1-i, self.y2+i, self.y2+i] x = [self.x1-i, self.x2+i, self.x2+i, self.x1-i] rr, cc = draw.polygon_perimeter(y, x, shape=img.shape) if alpha >= 0.99: result[rr, cc, 0] = color[0] result[rr, cc, 1] = color[1] result[rr, cc, 2] = color[2] else: if result.dtype == np.float32: result[rr, cc, 0] = (1 - alpha) * result[rr, cc, 0] + alpha * color[0] result[rr, cc, 1] = (1 - alpha) * result[rr, cc, 1] + alpha * color[1] result[rr, cc, 2] = (1 - alpha) * result[rr, cc, 2] + alpha * color[2] result = np.clip(result, 0, 255) else: result = result.astype(np.float32) result[rr, cc, 0] = (1 - alpha) * result[rr, cc, 0] + alpha * color[0] result[rr, cc, 1] = (1 - alpha) * result[rr, cc, 1] + alpha * color[1] result[rr, cc, 2] = (1 - alpha) * result[rr, cc, 2] + alpha * color[2] result = np.clip(result, 0, 255).astype(np.uint8) return result
Example #30
Source File: bbs.py From cat-bbs with MIT License | 5 votes |
def draw_on_image_filled_binary(self, img, copy=True): if copy: img = np.copy(img) h, w = img.shape[0], img.shape[1] x1 = np.clip(self.x1, 0, w-1) x2 = np.clip(self.x2, 0, w-1) y1 = np.clip(self.y1, 0, h-1) y2 = np.clip(self.y2, 0, h-1) if x1 < x2 and y1 < y2: img[self.y1:self.y2, self.x1:self.x2] = 1 return img