Fix Python – Copy file with pathlib in Python

I try to copy a file with pathlib
import pathlib
import shutil

my_file=pathlib.Path(‘/etc/hosts’)
to_file=pathlib.Path(‘/tmp/foo’)
shutil.copy(my_file, to_file)

I get this exception:
/home/foo_egs_d/bin/python /home/foo_egs_d/src/test-pathlib-copy.py
Traceback (most recent call last):
File “/home/foo_egs_d/src/test-pathlib-copy.py”, line 6, in….

Fix Python – PyTorch preferred way to copy a tensor

There seems to be several ways to create a copy of a tensor in PyTorch, including
y = tensor.new_tensor(x) #a

y = x.clone().detach() #b

y = torch.empty_like(x).copy_(x) #c

y = torch.tensor(x) #d

b is explicitly preferred over a and d according to a UserWarning I get if I execute either a or d. Why is it preferred? Performance? I’d argue it’s l….

Fix Python – How to deep copy a list?

After E0_copy = list(E0), I guess E0_copy is a deep copy of E0 since id(E0) is not equal to id(E0_copy). Then I modify E0_copy in the loop, but why is E0 not the same after?
E0 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
for k in range(3):
E0_copy = list(E0)
E0_copy[k][k] = 0
#print(E0_copy)
print E0 # -> [[0, 2, 3], [4, 0, 6], [7, 8, 0]]

….

Fix Python – What is the difference between shallow copy, deepcopy and normal assignment operation?

import copy

a = “deepak”
b = 1, 2, 3, 4
c = [1, 2, 3, 4]
d = {1: 10, 2: 20, 3: 30}

a1 = copy.copy(a)
b1 = copy.copy(b)
c1 = copy.copy(c)
d1 = copy.copy(d)

print(“immutable – id(a)==id(a1)”, id(a) == id(a1))
print(“immutable – id(b)==id(b1)”, id(b) == id(b1))
print(“mutable – id(c)==id(c1)”, id(c) == id(c1))
print(“mutable – id(d)==id(d1)”, id(….

Fix Python – Understanding dict.copy() – shallow or deep?

While reading up the documentation for dict.copy(), it says that it makes a shallow copy of the dictionary. Same goes for the book I am following (Beazley’s Python Reference), which says:

The m.copy() method makes a shallow
copy of the items contained in a
mapping object and places them in a
new mapping object.

Consider this:
>>> original….