Skip to content

Commit

Permalink
update oop exercises
Browse files Browse the repository at this point in the history
  • Loading branch information
ariefrahmansyah committed Aug 14, 2024
1 parent 55df9df commit 44f5e3d
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 31 deletions.
8 changes: 8 additions & 0 deletions code/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
def display(message, is_warning=False):
if is_warning:
print("Warning!!")
print(message)


def foo():
return "bar"
31 changes: 31 additions & 0 deletions code/inheritance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Parent class
class Animal:
def __init__(self, name):
print(f"Initializing animal named {name}")
self.name = name

def speak(self):
pass


# Child class inheriting from Animal
class Dog(Animal):
def speak(self):
return f"{self.name} says woof!"


class Cat(Animal):
def __init__(self, name, race):
super().__init__(name)
self.race = race
print(f"Initializing cat named {self.name} of {self.race} race")

def speak(self):
return f"{self.name} says meong!"


my_dog = Dog("Buddy")
print(my_dog.speak()) # Calls the speak method of the Dog class

my_cat = Cat("Ocong", "Persian")
print(my_cat.speak())
35 changes: 35 additions & 0 deletions code/mixins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Loggable:
def __init__(self):
self.title = ""

def log(self):
print("Log message from " + self.title)


class Connection:
def __init__(self):
self.server = ""

def connect(self):
print("Connecting to database on " + self.server)


class SqlDatabase(Connection, Loggable):
def __init__(self):
super().__init__()
self.title = "Sql Connection Demo"
self.server = "Some_Server"

def connect(self):
print("Connecting to SQL database on " + self.server)


def framework(item):
if isinstance(item, Connection):
item.connect()
if isinstance(item, Loggable):
item.log()


sql_connection = SqlDatabase()
framework(sql_connection)
3 changes: 3 additions & 0 deletions code/module_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from helpers import display as helpers_display

helpers_display("Not a warning")
1 change: 1 addition & 0 deletions data/csv_example.csv
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
"name","country","city","skills"
"Aurora","Indonesia","Palembang","Python"
"Jokowi","Indonesia","Solo","C++"
8 changes: 7 additions & 1 deletion docs/python/oop/oop_exercises.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# OOP Exercises

Create a class called `PersonAccount`. It has `firstname`, `lastname`, `incomes`, and `expenses` properties. It also has `total_income()`, `total_expense()`, `account_info()`, `add_income()`, `add_expense()` and `account_balance()` methods. Incomes is a dict of income amount and its description. The same goes for expenses.
Create a class called `PersonAccount`. It has `firstname`, `lastname`, `incomes`, and `expenses` properties. It also has `total_income()`, `total_expense()`, `account_info()`, `add_income()`, `add_expense()` and `account_balance()` methods. Incomes is a set of income amount and its description. The same goes for expenses.

Reference:

1. [Classes](./classes.md)
2. [Sets](../python_fundamentals_1/sets.ipynb)
3. [Student Grade Program](../../tutorials/student_grade_with_oo.ipynb)
99 changes: 69 additions & 30 deletions docs/python/python_fundamentals_2/file_handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,20 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 13,
"id": "5cbfcd4b-9ffc-4d62-afe7-2f4dbae3a3ed",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<_io.TextIOWrapper name='../../../data/reading_file_example.txt' mode='r' encoding='UTF-8'>\n"
"<_io.TextIOWrapper name='reading_file_example.txt' mode='r' encoding='UTF-8'>\n"
]
}
],
"source": [
"f = open(\"../../../data/reading_file_example.txt\")\n",
"f = open(\"reading_file_example.txt\")\n",
"print(f)"
]
},
Expand All @@ -64,22 +64,22 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 14,
"id": "61337471-a25c-4933-92be-f1481d8f46aa",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This is an example to show how to open a file and read.\n",
"This is the second line of the text.\n",
"I love python.\n"
"Halo, ini adalah contoh membaca file dengan Python.\n",
"Sebagai bagian dari training Python oleh Lab Fasilkom Unsri.\n",
"I <3 Python.\n"
]
}
],
"source": [
"f = open(\"../../../data/reading_file_example.txt\")\n",
"f = open(\"reading_file_example.txt\")\n",
"text = f.read()\n",
"print(text)\n",
"f.close()"
Expand All @@ -95,20 +95,20 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 15,
"id": "495c581d-f591-4678-97d8-a5330779cbbb",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This is an\n"
"Halo, ini \n"
]
}
],
"source": [
"f = open(\"../../../data/reading_file_example.txt\")\n",
"f = open(\"reading_file_example.txt\")\n",
"text = f.read(10)\n",
"print(text)\n",
"f.close()"
Expand All @@ -126,21 +126,21 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 16,
"id": "a9345163-7194-4529-a13d-d410736ded73",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This is an example to show how to open a file and read.\n",
"Halo, ini adalah contoh membaca file dengan Python.\n",
"\n"
]
}
],
"source": [
"f = open(\"../../../data/reading_file_example.txt\")\n",
"f = open(\"reading_file_example.txt\")\n",
"text = f.readline()\n",
"print(text)\n",
"f.close()"
Expand All @@ -158,20 +158,20 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 17,
"id": "314e27ef-dbd0-4ea7-9e50-fb3551474c44",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['This is an example to show how to open a file and read.\\n', 'This is the second line of the text.\\n', 'I love python.']\n"
"['Halo, ini adalah contoh membaca file dengan Python.\\n', 'Sebagai bagian dari training Python oleh Lab Fasilkom Unsri.\\n', 'I <3 Python.']\n"
]
}
],
"source": [
"f = open(\"../../../data/reading_file_example.txt\")\n",
"f = open(\"reading_file_example.txt\")\n",
"lines = f.readlines()\n",
"print(lines)\n",
"f.close()"
Expand All @@ -187,20 +187,20 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 18,
"id": "e035163b-7210-4742-9dbc-7bb3989633fe",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['This is an example to show how to open a file and read.', 'This is the second line of the text.', 'I love python.']\n"
"['Halo, ini adalah contoh membaca file dengan Python.', 'Sebagai bagian dari training Python oleh Lab Fasilkom Unsri.', 'I <3 Python.']\n"
]
}
],
"source": [
"f = open(\"../../../data/reading_file_example.txt\")\n",
"f = open(\"reading_file_example.txt\")\n",
"lines = f.read().splitlines()\n",
"print(lines)\n",
"f.close()"
Expand All @@ -216,20 +216,20 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 19,
"id": "6cae899c-d779-490e-aae6-fe2b88b6c783",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['This is an example to show how to open a file and read.', 'This is the second line of the text.', 'I love python.']\n"
"['Halo, ini adalah contoh membaca file dengan Python.', 'Sebagai bagian dari training Python oleh Lab Fasilkom Unsri.', 'I <3 Python.']\n"
]
}
],
"source": [
"with open(\"../../../data/reading_file_example.txt\") as f:\n",
"with open(\"reading_file_example.txt\") as f:\n",
" lines = f.read().splitlines()\n",
" print(lines)"
]
Expand All @@ -251,13 +251,13 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 23,
"id": "83430941-ea5c-45a2-9e4f-34fb15e0609d",
"metadata": {},
"outputs": [],
"source": [
"with open(\"../../../data/writing_file_example.txt\", \"a\") as f:\n",
" f.write(\"This text has to be appended at the end.\")"
"with open(\"appending_file_example.txt\", \"a\") as f:\n",
" f.write(\"This text has to be appended at the end.\\n\")"
]
},
{
Expand Down Expand Up @@ -298,12 +298,12 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 25,
"id": "6f08d98c-a8d4-4fdf-baf6-0ead0a1ceb01",
"metadata": {},
"outputs": [],
"source": [
"with open(\"../../../data/creating_file_example.txt\", \"w\") as f:\n",
"with open(\"creating_file_example.txt\", \"w\") as f:\n",
" f.write(\"This text will be written in a newly created file.\")"
]
},
Expand Down Expand Up @@ -351,7 +351,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 26,
"id": "0258b5c3-2f6b-461b-a738-10425d11bcc5",
"metadata": {},
"outputs": [
Expand All @@ -361,7 +361,8 @@
"text": [
"Column names are: name, country, city, skills\n",
"\tAurora is a student. He lives in Palembang, Indonesia.\n",
"Number of lines: 2\n"
"\tJokowi is a student. He lives in Solo, Indonesia.\n",
"Number of lines: 3\n"
]
}
],
Expand All @@ -380,6 +381,44 @@
" line_count += 1\n",
" print(f\"Number of lines: {line_count}\")"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "5d0c0177-137d-4d6b-95b8-30359aa44e32",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Column names are: \"name\",\"country\",\"city\",\"skills\"\n",
"\n",
"\t\"Aurora\" is a student. He lives in \"Palembang\", \"Indonesia\".\n",
"\t\"Jokowi\" is a student. He lives in \"Solo\", \"Indonesia\".\n"
]
}
],
"source": [
"with open(\"../../../data/csv_example.csv\") as f:\n",
" lines = f.readlines()\n",
" line_count = 0\n",
" for line in lines:\n",
" if line_count == 0:\n",
" print(f\"Column names are: {line}\")\n",
" else:\n",
" row = line.split(\",\")\n",
" print(f\"\\t{row[0]} is a student. He lives in {row[2]}, {row[1]}.\")\n",
" line_count += 1"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dbd32558-1316-4cfc-b66a-d0751c4c1360",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down

0 comments on commit 44f5e3d

Please sign in to comment.