Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sourcery refactored master branch #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 4 additions & 12 deletions advanced/recursion_fibonacci_memoization.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,12 @@ def fibonacci(n):
raise TypeError("n must be a positive int")
if n < 0:
raise ValueError("n must be a positive int or zero")

# Compute the n-th term
if n == 0:
# Position 0
return 0
if n == 1:
return 1
else:
return fibonacci(n - 1) + fibonacci(n - 2)
return 1 if n == 1 else fibonacci(n - 1) + fibonacci(n - 2)
Comment on lines -34 to +39
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function fibonacci refactored with the following changes:



# Uncomment the lines below to test example
Expand Down Expand Up @@ -101,23 +98,18 @@ def fibonacci_(n):
"""

@lru_cache(maxsize = 1000)
# Now we will use the lru_cache from functools module. LRU means Least Recently Used Cache
# Look the following link for higher values of maxsize, https://stackoverflow.com/questions/5061582/setting-stacksize-in-a-python-script/16248113#16248113
def fibonacci_lru(n):
# Check that the input is a positive integer
if (type(n) != int):
raise TypeError("n must be a positive int")
if n < 0:
raise ValueError("n must be a positive int or zero")

# Compute the n-th term
if n == 0:
# Position 0
return 0
if n == 1:
return 1
else:
return fibonacci_lru(n - 1) + fibonacci_lru(n - 2)
return 1 if n == 1 else fibonacci_lru(n - 1) + fibonacci_lru(n - 2)
Comment on lines -104 to +112
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function fibonacci_lru refactored with the following changes:

This removes the following comments ( why? ):

# Look the following link for higher values of maxsize, https://stackoverflow.com/questions/5061582/setting-stacksize-in-a-python-script/16248113#16248113
# Now we will use the lru_cache from functools module. LRU means Least Recently Used Cache


# Uncomment the lines below to test example
"""
Expand Down
3 changes: 2 additions & 1 deletion basics/arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
Based on Socratica videos
'''

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 22-22 refactored with the following changes:

  • Replace assignment with augmented assignment (aug-assign)


# Operations: + - * /

x = 28
Expand All @@ -19,7 +20,7 @@

Z = 2.00
print(type(Z))
Z = Z + 0.32j
Z += 0.32j
print(type(Z))

# The error below occurs because floats are narrower than complex numbers
Expand Down
5 changes: 3 additions & 2 deletions basics/if_then_else.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
""""""

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 44-45 refactored with the following changes:

  • Combine two compares on same value into a chained compare (chain-compares)

'''
File: if_then_else.py
Project: basics
Expand Down Expand Up @@ -41,8 +42,8 @@


if (a != b) and (b != c) and (a != c):
print("This is a scalene triangle.")
elif (a == b) and (b == c):
print("This is a scalene triangle.")
elif a == b == c:
print("This is an equilateral triangle.")
else:
print("This is an isosceles triangle.")
7 changes: 3 additions & 4 deletions basics/lists_.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
""""""

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 22-72 refactored with the following changes:

This removes the following comments ( why? ):

# To add more items

'''
File: lists_.py
Project: basics
Expand All @@ -19,7 +20,7 @@
'''
""""""
# A simple list definition
example = list()
example = []
print(example)
print(type(example))
# Another simple definition
Expand Down Expand Up @@ -67,9 +68,7 @@
letters.sort()
print(letters)

# To add more items
primes.append(17)
primes.append(19)
primes.extend((17, 19))
print(primes)

# To remove items
Expand Down
11 changes: 6 additions & 5 deletions basics/sets_.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
""""""

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 52-65 refactored with the following changes:

'''
File: sets_.py
Project: basics
Expand Down Expand Up @@ -49,7 +50,7 @@
example.discard(42)
print(example)
# We can start a set starts from a list
example2 = set([1, True, 'Europe', 2.71828])
example2 = {1, True, 'Europe', 2.71828}
print(len(example2))

# Let's clear our set
Expand All @@ -58,11 +59,11 @@

# If we have 2 sets, the combination of all elements from the two sets is denoted with Union
# The intersection is the set of elements inside both A and B sets
odds = set([1, 3, 5, 7, 9])
evens = set([2, 4, 6, 8, 10])
primes = set([2, 3, 5, 7])
odds = {1, 3, 5, 7, 9}
evens = {2, 4, 6, 8, 10}
primes = {2, 3, 5, 7}
# composite integers - the integers which can be factored
composites = set([4, 6, 8, 9, 10])
composites = {4, 6, 8, 9, 10}

print(odds.union(evens))
print(evens.union(odds))
Expand Down
13 changes: 3 additions & 10 deletions basics/text_files.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
""""""

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 20-29 refactored with the following changes:

This removes the following comments ( why? ):

# 2 - Binary files: compiled code, app data, media files(audio, video)
# print(help(open))
# 1 - Text files: plain text, xml, json, souce code, etc.
# The simple way to open a file. It's a dangerous way.
# There are two types of files:

'''
File: text_files.py
Project: basics
Expand All @@ -17,16 +18,8 @@
---------- --- ---------------------------------------------------------
'''
""""""
# There are two types of files:
# 1 - Text files: plain text, xml, json, souce code, etc.
# 2 - Binary files: compiled code, app data, media files(audio, video)
# print(help(open))

# The simple way to open a file. It's a dangerous way.
f = open("guido_bio.txt")
text = f.read()
f.close()

with open("guido_bio.txt") as f:
text = f.read()
# The code below is more security than above. When we uses open method
# we don't need to close the file.
with open("guido_bio.txt") as fobj:
Expand Down
16 changes: 4 additions & 12 deletions basics/tuples_.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
""""""

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 38-124 refactored with the following changes:

This removes the following comments ( why? ):

# After this point we can modify the list. Lists are mutable.
# The last is not inclusive
# Create a random tuple
# After this point the tuple can not be modify becayse it is immutable.
# Create a random list.
# Lists are mutable

'''
File: tuples_.py
Project: basics
Expand Down Expand Up @@ -35,17 +36,8 @@
TEST_SIZE = 100


# Create a random tuple
list_int = []
tuple_int = [random.randint(0, 1) for i in range(TEST_SIZE)]
# After this point the tuple can not be modify becayse it is immutable.
# Lists are mutable

# Create a random list.
for i in range(TEST_SIZE):
list_int.append(random.randint(0, 1))
# After this point we can modify the list. Lists are mutable.

tuple_int = [random.randint(0, 1) for _ in range(TEST_SIZE)]
list_int = [random.randint(0, 1) for _ in range(TEST_SIZE)]
print("LIST\t\tTUPLE")
for i in range(TEST_SIZE):
# We can iterate lists and tuples with a simple for
Expand Down Expand Up @@ -121,7 +113,7 @@

# Let's try slicing a tuple
my_tuple = ('m', 'y', 'n', 'a', 'm', 'e', 'i', 's')
print(my_tuple[0:4]) # The last is not inclusive
print(my_tuple[:4])
print(my_tuple[:]) # Begin to end
print(my_tuple[:5]) # Begin to 5-1
print(my_tuple[:-2]) # Begin to end-2
Expand Down
3 changes: 1 addition & 2 deletions intermediate/exceptions_.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ def read_file_timed(path):
start_time = time.time()
try:
f = open(path, mode="rb")
data = f.read()
return data
return f.read()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function read_file_timed refactored with the following changes:

except FileNotFoundError as err:
logger.error(err)
raise
Expand Down
15 changes: 8 additions & 7 deletions intermediate/random_numbers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
""""""

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 30-30 refactored with the following changes:

'''
File: random_numbers.py
Project: intermediate
Expand Down Expand Up @@ -27,7 +28,7 @@
print(help(random.random))

# Display 10 random numbers between 0 and 1
for i in range(10):
for _ in range(10):
print(random.random())

# Generate random numbers from interval 3-7. Way #0
Expand All @@ -39,26 +40,26 @@ def my_random():


# Print 10 random numbers from call function my_random()
for i in range(10):
for _ in range(10):
print(my_random())


# Generate random numbers from interval 3-7. Way #1 (uniform function)
print(help(random.uniform))
for i in range(10):
for _ in range(10):
print(random.uniform(3, 7))

# Generate random numbers from a Gaussian Distribution
print(help(random.normalvariate))
for i in range(10):
for _ in range(10):
print(random.normalvariate(0, 10))

# Generante random int numbers
print(help(random.randint))
for i in range(10):
for _ in range(10):
print(random.randint(0, 9))

# Generate from a random from a list
outcomes = ['rock', 'paper', 'scissor']
for i in range(10):
for _ in range(10):
Comment on lines -42 to +64
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 42-63 refactored with the following changes:

print(random.choice(outcomes))
12 changes: 6 additions & 6 deletions objects_and_class/0_basics_.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ def __init__(self, name, age=0):

# instance method
def sing(self, song):
return "{} sings {}".format(self.name, song)
return f"{self.name} sings {song}"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Parrot.sing refactored with the following changes:


def dance(self):
return "{} is now dancing".format(self.name)
return f"{self.name} is now dancing"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Parrot.dance refactored with the following changes:



def main():
Expand All @@ -42,12 +42,12 @@ def main():
woo = Parrot("Woo", 15)

# access the class attributes
print("Blu is a {}".format(blu.__class__.species))
print("Woo is also a {}".format(woo.__class__.species))
print(f"Blu is a {blu.__class__.species}")
print(f"Woo is also a {woo.__class__.species}")

# access the instance attributes
print("{} is {} years old".format(blu.name, blu.age))
print("{} is {} years old".format(woo.name, woo.age))
print(f"{blu.name} is {blu.age} years old")
print(f"{woo.name} is {woo.age} years old")
Comment on lines -45 to +50
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function main refactored with the following changes:


# call our instance methods
print(blu.sing("'Happy'"))
Expand Down
2 changes: 1 addition & 1 deletion objects_and_class/1_encapsulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def __init__(self):
self.__maxprice = 900

def sell(self):
print("Selling Price: {}".format(self.__maxprice))
print(f"Selling Price: {self.__maxprice}")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Computer.sell refactored with the following changes:


def setMaxPrice(self, price):
self.__maxprice = price
Expand Down
5 changes: 2 additions & 3 deletions objects_and_class/5_inheritance_advanced.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,10 @@ class DerivedClass(BaseClass):
class Polygon:
def __init__(self, no_of_sides):
self.n = no_of_sides
self.sides = [0 for i in range(no_of_sides)]
self.sides = [0 for _ in range(no_of_sides)]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Polygon.__init__ refactored with the following changes:


def inputSides(self):
self.sides = [float(input("Enter side "+str(i+1)+" : "))
for i in range(self.n)]
self.sides = [float(input(f"Enter side {str(i+1)} : ")) for i in range(self.n)]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Polygon.inputSides refactored with the following changes:


def dispSides(self):
for i in range(self.n):
Expand Down