Skip to content

Instantly share code, notes, and snippets.

View ofelix03's full-sized avatar

Felix Otoo ofelix03

View GitHub Profile
@ofelix03
ofelix03 / computing-total-using-for-loop-bad.py
Created April 11, 2021 00:43
computing-total-using-for-loop-bad
fruits = [('orange', 1), ('mangoes', 2), ('pineapple', 3), ('banana', 4)]
total_fruits = 0
for fruit in fruits:
total_fruits = total_fruits + fruit[1]
print("total_fruits: ", total_fruits)
@ofelix03
ofelix03 / computing-total-using-for-loop-bad.py
Created April 11, 2021 00:45
computing-total-using-for-loop-bad
fruits = [('orange', 1), ('mangoes', 2), ('pineapple', 3), ('banana', 4)]
total_fruits = sum([fruit[1] for fruit in fruits])
print("total_fruits: ", total_fruits)
@ofelix03
ofelix03 / if-for-conditions-bad.py
Created April 17, 2021 02:02
if-for-conditions-bad
cheques = [...]
if cheques:
for cheque in cheques:
# run some action on cheque
pass
@ofelix03
ofelix03 / if-for-conditions-better.py
Created April 17, 2021 02:03
if-for-conditions-better
cheques = [...]
for cheque in cheques:
# run some action on cheque
pass
@ofelix03
ofelix03 / refactor-function-with-high-code-cognitive-complexity.py
Last active May 6, 2021 15:12
refactor-function-with-high-code-cognitive-complexity
# this method was extract from a cheque class
def create(self, vals):
if "amount" not in vals:
raise ValidationError(_("Cheque amount must be provided"))
if "amount" in vals and vals["amount"] <= 0:
raise ValidationError(_("Cheque amount must be greater than zero"))
if "special_clearing" in vals and vals["special_clearing"]:
vals["special_note"] = "Special Clearing"
@ofelix03
ofelix03 / class-instantiation.py
Created May 2, 2021 13:12
class-instantiation
class Book(object):
def __init__(self, name=None, isbn=None, published_date=None, author=None):
self.name = name
self.isbn = isbn
self.author = author
# first instance of Book class
refactoring_book = Book(name='Refactor', isbn='978-0201485677')
@ofelix03
ofelix03 / function-parameters-and-arguments.py
Last active May 2, 2021 20:23
function-parameters-and-arguments
# defining a function with parameters
function send_money(amount, currency_code, recipient_name):
# code for sending money goes here
return True
# calling a function with arguments
money_sent = send_money(2000, 'USD', 'Felix Otoo')
@ofelix03
ofelix03 / atm-class.py
Last active May 2, 2021 20:22
atm-class
class ATM(object):
@classmethod
def _authenticate(cls, pin):
# perform PIN verification and authentication
return True
@classmethod
def withdraw_money(cls, pin, amount):
if not cls._authenticate(pin):
@ofelix03
ofelix03 / code-with-complexity-issues-refactory.py
Created May 3, 2021 22:48
code-with-complexity-issues-refactored
@api.model
def create(self, vals):
if "amount" not in vals:
raise ValidationError(_("Cheque amount must be provided"))
if "amount" in vals and vals["amount"] <= 0:
raise ValidationError(_("Cheque amount must be greater than zero"))
if "special_clearing" in vals and vals["special_clearing"]:
vals["special_note"] = "Special Clearing"
@ofelix03
ofelix03 / refactor-merging-conditional-guards.py
Last active May 6, 2021 22:14
refactor-merging-conditional-guards
if "amount" not in vals and vals['amount'] > 0:
raise ValidationError(_("Cheque amount must be provided and must be greater than zero"))