-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
__pycache__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
### Lecture 2. Inversion of Control | ||
### 01-3. Math Knowledge Base | ||
|
||
import sys | ||
import random | ||
from abc import * | ||
from enum import Enum | ||
|
||
import math_object | ||
import resource_downloader | ||
|
||
class IUserCommand(metaclass = ABCMeta): | ||
@abstractmethod | ||
def get_command_tag(self): pass | ||
|
||
class Finish(IUserCommand): | ||
def get_command_tag(self): | ||
return "finish" | ||
|
||
def make_commands_dict(cmd_lst): | ||
cmd_dict = dict() | ||
for cmd in cmd_lst: | ||
cmd_dict[cmd.get_command_tag().lower().strip()] = cmd | ||
return cmd_dict | ||
|
||
supported_commands = make_commands_dict( | ||
[ Finish() | ||
|
||
]) | ||
|
||
def is_finish_command(cmd): | ||
return cmd.get_command_tag() == Finish().get_command_tag() | ||
|
||
def is_invalid_command(cmd): | ||
return cmd == None | ||
|
||
def normalize_command(user_input): | ||
return user_input.strip().lower() | ||
|
||
def parse_user_input(user_input): | ||
norm_cmd = normalize_command(user_input) | ||
if norm_cmd in supported_commands: | ||
return supported_commands[norm_cmd] | ||
return None | ||
|
||
def eval_command(cmd): | ||
pass | ||
|
||
if __name__ == "__main__": | ||
print("Math Research Assistant") | ||
|
||
do_loop = True | ||
while do_loop: | ||
user_input = input("$> ") | ||
mb_cmd = parse_user_input(user_input) | ||
if is_invalid_command(mb_cmd): | ||
print("Invalid command.") | ||
elif is_finish_command(mb_cmd): | ||
do_loop = False | ||
else: | ||
eval_command(mb_cmd) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import sys | ||
import random | ||
from abc import * | ||
from enum import Enum | ||
|
||
import requests | ||
|
||
"""Represents an abstract math concept.""" | ||
class MathObject(metaclass = ABCMeta): | ||
|
||
"""Returns a name.""" | ||
@abstractmethod | ||
def get_name(self): pass | ||
|
||
"""Returns a short description.""" | ||
@abstractmethod | ||
def get_description(self): pass | ||
|
||
"""Returns a dictionary of resources: | ||
dict<ResourceTag, ResourceUrl> | ||
""" | ||
@abstractmethod | ||
def get_resources(): pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import sys | ||
import random | ||
from abc import * | ||
from enum import Enum | ||
|
||
import requests | ||
from math_object import MathObject | ||
from resource_tag import ResourceTag | ||
|
||
"""Resource downloader.""" | ||
class ResourceDownloader(): | ||
|
||
def __init__(self, math_object : MathObject, resource_tag: ResourceTag): | ||
self.__math_object = math_object | ||
self.__resource_tag = resource_tag | ||
|
||
def download_content(self): | ||
resources = self.__math_object.get_resources() | ||
if self.__resource_tag in resources: | ||
url = resources[self.__resource_tag] | ||
r = requests.get(url) | ||
return r.content | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import sys | ||
import random | ||
from abc import * | ||
from enum import Enum | ||
|
||
# Explicit knowledge about resources is not good. | ||
|
||
class ResourceTag(Enum): | ||
WIKIPEDIA = 1 |