Skip to content

Commit 4e06949

Browse files
author
cclauss
committed
Modernize Python 2 code to get ready for Python 3
1 parent a03b2ea commit 4e06949

File tree

95 files changed

+583
-524
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+583
-524
lines changed

File_Transfer_Protocol/ftp_send_receive.py

+18-18
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
1-
"""
2-
File transfer protocol used to send and receive files using FTP server.
3-
Use credentials to provide access to the FTP client
1+
"""
2+
File transfer protocol used to send and receive files using FTP server.
3+
Use credentials to provide access to the FTP client
44
5-
Note: Do not use root username & password for security reasons
6-
Create a seperate user and provide access to a home directory of the user
7-
Use login id and password of the user created
8-
cwd here stands for current working directory
9-
"""
5+
Note: Do not use root username & password for security reasons
6+
Create a seperate user and provide access to a home directory of the user
7+
Use login id and password of the user created
8+
cwd here stands for current working directory
9+
"""
1010

1111
from ftplib import FTP
12-
ftp = FTP('xxx.xxx.x.x') """ Enter the ip address or the domain name here """
12+
ftp = FTP('xxx.xxx.x.x') # Enter the ip address or the domain name here
1313
ftp.login(user='username', passwd='password')
1414
ftp.cwd('/Enter the directory here/')
1515

16-
"""
17-
The file which will be received via the FTP server
18-
Enter the location of the file where the file is received
19-
"""
16+
"""
17+
The file which will be received via the FTP server
18+
Enter the location of the file where the file is received
19+
"""
2020

2121
def ReceiveFile():
2222
FileName = 'example.txt' """ Enter the location of the file """
2323
LocalFile = open(FileName, 'wb')
24-
ftp.retrbinary('RETR ' + filename, LocalFile.write, 1024)
24+
ftp.retrbinary('RETR ' + FileName, LocalFile.write, 1024)
2525
ftp.quit()
2626
LocalFile.close()
2727

28-
"""
29-
The file which will be sent via the FTP server
30-
The file send will be send to the current working directory
31-
"""
28+
"""
29+
The file which will be sent via the FTP server
30+
The file send will be send to the current working directory
31+
"""
3232

3333
def SendFile():
3434
FileName = 'example.txt' """ Enter the name of the file """

Graphs/a_star.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from __future__ import print_function
12

23
grid = [[0, 1, 0, 0, 0, 0],
34
[0, 1, 0, 0, 0, 0],#0 are free path whereas 1's are obstacles
@@ -80,7 +81,7 @@ def search(grid,init,goal,cost,heuristic):
8081
y = goal[1]
8182
invpath.append([x, y])#we get the reverse path from here
8283
while x != init[0] or y != init[1]:
83-
x2 = x - delta[action[x][y]][0]
84+
x2 = x - delta[action[x][y]][0]
8485
y2 = y - delta[action[x][y]][1]
8586
x = x2
8687
y = y2
@@ -89,13 +90,13 @@ def search(grid,init,goal,cost,heuristic):
8990
path = []
9091
for i in range(len(invpath)):
9192
path.append(invpath[len(invpath) - 1 - i])
92-
print "ACTION MAP"
93+
print("ACTION MAP")
9394
for i in range(len(action)):
94-
print action[i]
95+
print(action[i])
9596

9697
return path
9798

9899
a = search(grid,init,goal,cost,heuristic)
99100
for i in range(len(a)):
100-
print a[i]
101+
print(a[i])
101102

Graphs/basic-graphs.py

+24-10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
from __future__ import print_function
2+
3+
try:
4+
raw_input # Python 2
5+
except NameError:
6+
raw_input = input # Python 3
7+
8+
try:
9+
xrange # Python 2
10+
except NameError:
11+
xrange = range # Python 3
12+
113
# Accept No. of Nodes and edges
214
n, m = map(int, raw_input().split(" "))
315

@@ -48,15 +60,15 @@
4860

4961
def dfs(G, s):
5062
vis, S = set([s]), [s]
51-
print s
63+
print(s)
5264
while S:
5365
flag = 0
5466
for i in G[S[-1]]:
5567
if i not in vis:
5668
S.append(i)
5769
vis.add(i)
5870
flag = 1
59-
print i
71+
print(i)
6072
break
6173
if not flag:
6274
S.pop()
@@ -76,14 +88,14 @@ def dfs(G, s):
7688

7789
def bfs(G, s):
7890
vis, Q = set([s]), deque([s])
79-
print s
91+
print(s)
8092
while Q:
8193
u = Q.popleft()
8294
for v in G[u]:
8395
if v not in vis:
8496
vis.add(v)
8597
Q.append(v)
86-
print v
98+
print(v)
8799

88100

89101
"""
@@ -116,7 +128,7 @@ def dijk(G, s):
116128
path[v[0]] = u
117129
for i in dist:
118130
if i != s:
119-
print dist[i]
131+
print(dist[i])
120132

121133

122134
"""
@@ -140,7 +152,7 @@ def topo(G, ind=None, Q=[1]):
140152
if len(Q) == 0:
141153
return
142154
v = Q.popleft()
143-
print v
155+
print(v)
144156
for w in G[v]:
145157
ind[w] -= 1
146158
if ind[w] == 0:
@@ -175,7 +187,8 @@ def adjm():
175187
"""
176188

177189

178-
def floy((A, n)):
190+
def floy(xxx_todo_changeme):
191+
(A, n) = xxx_todo_changeme
179192
dist = list(A)
180193
path = [[0] * n for i in xrange(n)]
181194
for k in xrange(n):
@@ -184,7 +197,7 @@ def floy((A, n)):
184197
if dist[i][j] > dist[i][k] + dist[k][j]:
185198
dist[i][j] = dist[i][k] + dist[k][j]
186199
path[i][k] = k
187-
print dist
200+
print(dist)
188201

189202

190203
"""
@@ -246,14 +259,15 @@ def edglist():
246259
"""
247260

248261

249-
def krusk((E, n)):
262+
def krusk(xxx_todo_changeme1):
250263
# Sort edges on the basis of distance
264+
(E, n) = xxx_todo_changeme1
251265
E.sort(reverse=True, key=lambda x: x[2])
252266
s = [set([i]) for i in range(1, n + 1)]
253267
while True:
254268
if len(s) == 1:
255269
break
256-
print s
270+
print(s)
257271
x = E.pop()
258272
for i in xrange(len(s)):
259273
if x[0] in s[i]:

Graphs/minimum_spanning_tree_kruskal.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from __future__ import print_function
12
num_nodes, num_edges = list(map(int,input().split()))
23

34
edges = []

Graphs/scc_kosaraju.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from __future__ import print_function
12
# n - no of nodes, m - no of edges
23
n, m = list(map(int,input().split()))
34

Multi_Hueristic_Astar.py

+22-16
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
from __future__ import print_function
12
import heapq
23
import numpy as np
34
import math
45
import copy
56

7+
try:
8+
xrange # Python 2
9+
except NameError:
10+
xrange = range # Python 3
11+
612

713
class PriorityQueue:
814
def __init__(self):
@@ -95,22 +101,22 @@ def do_something(back_pointer, goal, start):
95101
for i in xrange(n):
96102
for j in range(n):
97103
if (i, j) == (0, n-1):
98-
print grid[i][j],
99-
print "<-- End position",
104+
print(grid[i][j], end=' ')
105+
print("<-- End position", end=' ')
100106
else:
101-
print grid[i][j],
102-
print
107+
print(grid[i][j], end=' ')
108+
print()
103109
print("^")
104110
print("Start position")
105-
print
111+
print()
106112
print("# is an obstacle")
107113
print("- is the path taken by algorithm")
108114
print("PATH TAKEN BY THE ALGORITHM IS:-")
109115
x = back_pointer[goal]
110116
while x != start:
111-
print x,
117+
print(x, end=' ')
112118
x = back_pointer[x]
113-
print x
119+
print(x)
114120
quit()
115121

116122
def valid(p):
@@ -239,24 +245,24 @@ def multi_a_star(start, goal, n_hueristic):
239245
expand_state(get_s, 0, visited, g_function, close_list_anchor, close_list_inad, open_list, back_pointer)
240246
close_list_anchor.append(get_s)
241247
print("No path found to goal")
242-
print
248+
print()
243249
for i in range(n-1,-1, -1):
244250
for j in range(n):
245251
if (j, i) in blocks:
246-
print '#',
252+
print('#', end=' ')
247253
elif (j, i) in back_pointer:
248254
if (j, i) == (n-1, n-1):
249-
print '*',
255+
print('*', end=' ')
250256
else:
251-
print '-',
257+
print('-', end=' ')
252258
else:
253-
print '*',
259+
print('*', end=' ')
254260
if (j, i) == (n-1, n-1):
255-
print '<-- End position',
256-
print
261+
print('<-- End position', end=' ')
262+
print()
257263
print("^")
258264
print("Start position")
259-
print
265+
print()
260266
print("# is an obstacle")
261267
print("- is the path taken by algorithm")
262-
multi_a_star(start, goal, n_hueristic)
268+
multi_a_star(start, goal, n_hueristic)

Neural_Network/convolution_neural_network.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
Date: 2017.9.20
1616
- - - - - -- - - - - - - - - - - - - - - - - - - - - - -
1717
'''
18+
from __future__ import print_function
1819

1920
import numpy as np
2021
import matplotlib.pyplot as plt
@@ -192,8 +193,8 @@ def _calculate_gradient_from_pool(self,out_map,pd_pool,num_map,size_map,size_poo
192193
def trian(self,patterns,datas_train, datas_teach, n_repeat, error_accuracy,draw_e = bool):
193194
#model traning
194195
print('----------------------Start Training-------------------------')
195-
print(' - - Shape: Train_Data ',np.shape(datas_train))
196-
print(' - - Shape: Teach_Data ',np.shape(datas_teach))
196+
print((' - - Shape: Train_Data ',np.shape(datas_train)))
197+
print((' - - Shape: Teach_Data ',np.shape(datas_teach)))
197198
rp = 0
198199
all_mse = []
199200
mse = 10000
@@ -262,7 +263,7 @@ def draw_error():
262263
plt.grid(True, alpha=0.5)
263264
plt.show()
264265
print('------------------Training Complished---------------------')
265-
print(' - - Training epoch: ', rp, ' - - Mse: %.6f' % mse)
266+
print((' - - Training epoch: ', rp, ' - - Mse: %.6f' % mse))
266267
if draw_e:
267268
draw_error()
268269
return mse
@@ -271,7 +272,7 @@ def predict(self,datas_test):
271272
#model predict
272273
produce_out = []
273274
print('-------------------Start Testing-------------------------')
274-
print(' - - Shape: Test_Data ',np.shape(datas_test))
275+
print((' - - Shape: Test_Data ',np.shape(datas_test)))
275276
for p in range(len(datas_test)):
276277
data_test = np.asmatrix(datas_test[p])
277278
data_focus1, data_conved1 = self.convolute(data_test, self.conv1, self.w_conv1,

Neural_Network/perceptron.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
p2 = 1
1010
1111
'''
12+
from __future__ import print_function
1213

1314
import random
1415

@@ -52,7 +53,7 @@ def trannig(self):
5253
epoch_count = epoch_count + 1
5354
# if you want controle the epoch or just by erro
5455
if erro == False:
55-
print('\nEpoch:\n',epoch_count)
56+
print(('\nEpoch:\n',epoch_count))
5657
print('------------------------\n')
5758
#if epoch_count > self.epoch_number or not erro:
5859
break
@@ -66,10 +67,10 @@ def sort(self, sample):
6667
y = self.sign(u)
6768

6869
if y == -1:
69-
print('Sample: ', sample)
70+
print(('Sample: ', sample))
7071
print('classification: P1')
7172
else:
72-
print('Sample: ', sample)
73+
print(('Sample: ', sample))
7374
print('classification: P2')
7475

7576
def sign(self, u):

Project Euler/Problem 01/sol1.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@
44
we get 3,5,6 and 9. The sum of these multiples is 23.
55
Find the sum of all the multiples of 3 or 5 below N.
66
'''
7+
from __future__ import print_function
8+
try:
9+
raw_input # Python 2
10+
except NameError:
11+
raw_input = input # Python 3
712
n = int(raw_input().strip())
8-
sum=0;
13+
sum=0
914
for a in range(3,n):
1015
if(a%3==0 or a%5==0):
1116
sum+=a
12-
print sum;
17+
print(sum)

Project Euler/Problem 01/sol2.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
we get 3,5,6 and 9. The sum of these multiples is 23.
55
Find the sum of all the multiples of 3 or 5 below N.
66
'''
7+
from __future__ import print_function
8+
try:
9+
raw_input # Python 2
10+
except NameError:
11+
raw_input = input # Python 3
712
n = int(raw_input().strip())
813
sum = 0
914
terms = (n-1)/3
@@ -12,4 +17,4 @@
1217
sum+= ((terms)*(10+(terms-1)*5))/2
1318
terms = (n-1)/15
1419
sum-= ((terms)*(30+(terms-1)*15))/2
15-
print sum
20+
print(sum)

0 commit comments

Comments
 (0)