-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
||
|
||
# Uncomment the lines below to test example | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
This removes the following comments ( why? ):
|
||
|
||
# Uncomment the lines below to test example | ||
""" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
Based on Socratica videos | ||
''' | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
|
||
|
||
# Operations: + - * / | ||
|
||
x = 28 | ||
|
@@ -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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
# -*- coding: utf-8 -*- | ||
"""""" | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
|
||
''' | ||
File: if_then_else.py | ||
Project: basics | ||
|
@@ -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.") |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
# -*- coding: utf-8 -*- | ||
"""""" | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
This removes the following comments ( why? ):
|
||
''' | ||
File: lists_.py | ||
Project: basics | ||
|
@@ -19,7 +20,7 @@ | |
''' | ||
"""""" | ||
# A simple list definition | ||
example = list() | ||
example = [] | ||
print(example) | ||
print(type(example)) | ||
# Another simple definition | ||
|
@@ -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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
# -*- coding: utf-8 -*- | ||
"""""" | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
|
||
''' | ||
File: sets_.py | ||
Project: basics | ||
|
@@ -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 | ||
|
@@ -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)) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
# -*- coding: utf-8 -*- | ||
"""""" | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
This removes the following comments ( why? ):
|
||
''' | ||
File: text_files.py | ||
Project: basics | ||
|
@@ -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: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
# -*- coding: utf-8 -*- | ||
"""""" | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
This removes the following comments ( why? ):
|
||
''' | ||
File: tuples_.py | ||
Project: basics | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
except FileNotFoundError as err: | ||
logger.error(err) | ||
raise | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
# -*- coding: utf-8 -*- | ||
"""""" | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
|
||
''' | ||
File: random_numbers.py | ||
Project: intermediate | ||
|
@@ -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 | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
|
||
print(random.choice(outcomes)) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
def dance(self): | ||
return "{} is now dancing".format(self.name) | ||
return f"{self.name} is now dancing" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
|
||
def main(): | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
# call our instance methods | ||
print(blu.sing("'Happy'")) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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}") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
def setMaxPrice(self, price): | ||
self.__maxprice = price | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
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)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
def dispSides(self): | ||
for i in range(self.n): | ||
|
There was a problem hiding this comment.
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:assign-if-exp
)