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

Additional comments in "Start" and fix of incorrect values in "Finished" #7

Open
wants to merge 9 commits into
base: main
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
8 changes: 4 additions & 4 deletions Finished/Ch 4/challenge.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ def __lt__(self, other):

# ~~~~~~~~~ TEST CODE ~~~~~~~~~
stocks = [
Stock("MSFT", 342.0, "Microsoft Corp"),
Stock("GOOG", 135.0, "Google Inc"),
Stock("META", 275.0, "Meta Platforms Inc"),
Stock("AMZN", 120.0, "Amazon Inc")
Stock(342.0, "MSFT", "Microsoft Corp"),
Stock(135.0, "GOOG", "Google Inc"),
Stock(275.0, "META", "Meta Platforms Inc"),
Stock(120.0, "AMZN", "Amazon Inc")
]

bonds = [
Expand Down
6 changes: 4 additions & 2 deletions Start/Ch 2/abstract_start.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
# Python Object Oriented Programming by Joe Marini course example
# Using Abstract Base Classes to enforce class constraints

#TODO: Import "ABC"(abstract base classes) and abstractmethod from abc

#TODO: Inherit from ABC indicates that this is an abstract base class
class GraphicShape:
def __init__(self):
super().__init__()

# declaring a method as abstract requires a subclass to implement it
def calcArea(self):
pass


#TODO: Create specific versions of "calcArea" for different shapes
class Circle(GraphicShape):
def __init__(self, radius):
self.radius = radius
Expand Down
9 changes: 8 additions & 1 deletion Start/Ch 2/composition_start.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Python Object Oriented Programming by Joe Marini course example
# Using composition to build complex objects


#TODO: Arrange the composition of the Book class to operate on subclasses
class Book:
def __init__(self, title, price, authorfname, authorlname):
self.title = title
Expand All @@ -15,6 +15,13 @@ def __init__(self, title, price, authorfname, authorlname):
def addchapter(self, name, pages):
self.chapters.append((name, pages))

#TODO: Method for counting the total number of pages from all chapters

#TODO: Create classes "Chapter"(with attributes "name" and "pages")
# and "Author" with ("fname" and "lname") and a method __str__ returning author details


#TODO: Create an author object and add it to the book

b1 = Book("War and Peace", 39.0, "Leo", "Tolstoy")

Expand Down
7 changes: 6 additions & 1 deletion Start/Ch 2/inheritance_start.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
# Python Object Oriented Programming by Joe Marini course example
# Understanding class inheritance

#TODO: Create a class "Publication" with attributes "title" and "price"

#TODO: Create a class "Perdiodicaly" that inherits from "Publication"
# and additionally has attributes "period" and "publisher"

#TODO: Allow the "Book" class to inherit attributes from Publication
class Book:
def __init__(self, title, author, pages, price):
self.title = title
self.price = price
self.author = author
self.pages = pages


#TODO: The Magazine and Newspaper classes can inherit all atributes from "Perdiodicaly"
class Magazine:
def __init__(self, title, publisher, price, period):
self.title = title
Expand Down
3 changes: 3 additions & 0 deletions Start/Ch 2/interface_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ def __init__(self):
def calcArea(self):
pass

# TODO: Add abstract class "JSONify" with abstract method "toJSON"

class Circle(GraphicShape):
def __init__(self, radius):
self.radius = radius

def calcArea(self):
return 3.14 * (self.radius ** 2)

# TODO: Override "toJSON" method to return a JSON representation of "calcArea"


c = Circle(10)
Expand Down
5 changes: 3 additions & 2 deletions Start/Ch 2/multiple_start.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Python Object Oriented Programming by Joe Marini course example
# Understanding multiple inheritance


#TODO: For A and B class create an attribute "name" that will take different values
class A:
def __init__(self):
super().__init__()
Expand All @@ -17,6 +17,7 @@ def __init__(self):
class C(A, B):
def __init__(self):
super().__init__()

#TODO: Create the method "showprops" returning the attributes it owns

c = C()
#TODO: You can use showprops() and also call "Class.__mro__" to find out all the parent classes
1 change: 1 addition & 0 deletions Start/Ch 4/dataclass_start.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Python Object Oriented Programming by Joe Marini course example
# Using data classes to represent data objects

#TODO: Import dataclasses from dataclass and apply decorator

class Book:
def __init__(self, title, author, pages, price):
Expand Down
5 changes: 5 additions & 0 deletions Start/Ch 4/datadefault_start.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# Python Object Oriented Programming by Joe Marini course example
# implementing default values in data classes

# TODO: Import "field" function from dataclasses
from dataclasses import dataclass

# TODO: Define a function that returns a random number between 20-40.
# Don't forget to import the necessary module


@dataclass
class Book:
Expand All @@ -11,3 +15,4 @@ class Book:
author: str
pages: int
price: float
# TODO: Use the field function to get default price as random number
2 changes: 1 addition & 1 deletion Start/Ch 4/immutable_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ImmutableClass:
value2: int = 0


obj = ImmutableClass()
obj = ImmutableClass() # You can set the values of frozen class only with initialization
print(obj.value1)

# TODO: attempting to change the value of an immutable class throws an exception
Expand Down