Skip to content

Commit 4ee0e62

Browse files
author
cclauss
committed
Modernize Python 2 code to get ready for Python 3 AGAIN
1 parent 54f6d1f commit 4ee0e62

File tree

6 files changed

+31
-30
lines changed

6 files changed

+31
-30
lines changed

File_Transfer_Protocol/ftp_client_server.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@
88
s.bind((host, port)) # Bind to the port
99
s.listen(5) # Now wait for client connection.
1010

11-
print 'Server listening....'
11+
print('Server listening....')
1212

1313
while True:
1414
conn, addr = s.accept() # Establish connection with client.
15-
print 'Got connection from', addr
15+
print('Got connection from', addr)
1616
data = conn.recv(1024)
1717
print('Server received', repr(data))
1818

19-
filename='mytext.txt'
20-
f = open(filename,'rb')
21-
l = f.read(1024)
22-
while (l):
23-
conn.send(l)
24-
print('Sent ',repr(l))
25-
l = f.read(1024)
19+
filename = 'mytext.txt'
20+
f = open(filename, 'rb')
21+
in_data = f.read(1024)
22+
while (in_data):
23+
conn.send(in_data)
24+
print('Sent ', repr(in_data))
25+
in_data = f.read(1024)
2626
f.close()
2727

2828
print('Done sending')
@@ -42,7 +42,7 @@
4242
s.send("Hello server!")
4343

4444
with open('received_file', 'wb') as f:
45-
print 'file opened'
45+
print('file opened')
4646
while True:
4747
print('receiving data...')
4848
data = s.recv(1024)
@@ -55,4 +55,4 @@
5555
f.close()
5656
print('Successfully get the file')
5757
s.close()
58-
print('connection closed')
58+
print('connection closed')

Project Euler/Problem 29/solution.py

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
def main():
2-
"""
2+
"""
33
Consider all integer combinations of ab for 2 <= a <= 5 and 2 <= b <= 5:
44
55
22=4, 23=8, 24=16, 25=32
66
32=9, 33=27, 34=81, 35=243
77
42=16, 43=64, 44=256, 45=1024
88
52=25, 53=125, 54=625, 55=3125
9-
If they are then placed in numerical order, with any repeats removed, we get the following sequence of 15 distinct terms:
9+
If they are then placed in numerical order, with any repeats removed,
10+
we get the following sequence of 15 distinct terms:
1011
1112
4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125
1213
13-
How many distinct terms are in the sequence generated by ab for 2 <= a <= 100 and 2 <= b <= 100?
14+
How many distinct terms are in the sequence generated by ab
15+
for 2 <= a <= 100 and 2 <= b <= 100?
1416
"""
1517

1618
collectPowers = set()
@@ -19,15 +21,12 @@ def main():
1921

2022
N = 101 # maximum limit
2123

22-
for a in range(2,N):
23-
24-
for b in range (2,N):
25-
24+
for a in range(2, N):
25+
for b in range(2, N):
2626
currentPow = a**b # calculates the current power
2727
collectPowers.add(currentPow) # adds the result to the set
28-
2928

30-
print "Number of terms ", len(collectPowers)
29+
print("Number of terms ", len(collectPowers))
3130

3231

3332
if __name__ == '__main__':

data_structures/Trie/Trie.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def __init__(self):
1212
self.nodes = dict() # Mapping from char to TrieNode
1313
self.is_leaf = False
1414

15-
def insert_many(self, words: [str]): # noqa: F821 This syntax is Python 3 only
15+
def insert_many(self, words: [str]): # noqa: E999 This syntax is Python 3 only
1616
"""
1717
Inserts a list of words into the Trie
1818
:param words: list of string words
@@ -21,7 +21,7 @@ def insert_many(self, words: [str]): # noqa: F821 This syntax is Python 3 only
2121
for word in words:
2222
self.insert(word)
2323

24-
def insert(self, word: str): # noqa: F821 This syntax is Python 3 only
24+
def insert(self, word: str): # noqa: E999 This syntax is Python 3 only
2525
"""
2626
Inserts a word into the Trie
2727
:param word: word to be inserted
@@ -34,7 +34,7 @@ def insert(self, word: str): # noqa: F821 This syntax is Python 3 only
3434
curr = curr.nodes[char]
3535
curr.is_leaf = True
3636

37-
def find(self, word: str) -> bool: # noqa: F821 This syntax is Python 3 only
37+
def find(self, word: str) -> bool: # noqa: E999 This syntax is Python 3 only
3838
"""
3939
Tries to find word in a Trie
4040
:param word: word to look for
@@ -48,7 +48,7 @@ def find(self, word: str) -> bool: # noqa: F821 This syntax is Python 3 only
4848
return curr.is_leaf
4949

5050

51-
def print_words(node: TrieNode, word: str): # noqa: F821 This syntax is Python 3 only
51+
def print_words(node: TrieNode, word: str): # noqa: E999 This syntax is Python 3 only
5252
"""
5353
Prints all the words in a Trie
5454
:param node: root node of Trie

dynamic_programming/abbreviation.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
a=daBcd and b="ABC"
1111
daBcd -> capitalize a and c(dABCd) -> remove d (ABC)
1212
"""
13+
14+
1315
def abbr(a, b):
1416
n = len(a)
1517
m = len(b)
@@ -26,4 +28,4 @@ def abbr(a, b):
2628

2729

2830
if __name__ == "__main__":
29-
print abbr("daBcd", "ABC") # expect True
31+
print(abbr("daBcd", "ABC")) # expect True

dynamic_programming/fastfibonacci.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77

88

99
# returns F(n)
10-
def fibonacci(n: int): # noqa: F821 This syntax is Python 3 only
10+
def fibonacci(n: int): # noqa: E999 This syntax is Python 3 only
1111
if n < 0:
1212
raise ValueError("Negative arguments are not supported")
1313
return _fib(n)[0]
1414

1515

1616
# returns (F(n), F(n-1))
17-
def _fib(n: int): # noqa: F821 This syntax is Python 3 only
17+
def _fib(n: int): # noqa: E999 This syntax is Python 3 only
1818
if n == 0:
1919
# (F(0), F(1))
2020
return (0, 1)

machine_learning/scoring_functions.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ def mbd(predict, actual):
7070
difference = predict - actual
7171
numerator = np.sum(difference) / len(predict)
7272
denumerator = np.sum(actual) / len(predict)
73-
print str(numerator)
74-
print str(denumerator)
73+
print(numerator)
74+
print(denumerator)
7575

7676
score = float(numerator) / denumerator * 100
7777

78-
return score
78+
return score

0 commit comments

Comments
 (0)