Skip to content

Commit

Permalink
Math Assistant WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
graninas committed Feb 19, 2020
1 parent 300d532 commit 719ce1f
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__pycache__
61 changes: 61 additions & 0 deletions lecture03/math-research-assistant/main.py
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)
23 changes: 23 additions & 0 deletions lecture03/math-research-assistant/math_object.py
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
23 changes: 23 additions & 0 deletions lecture03/math-research-assistant/resource_downloader.py
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
9 changes: 9 additions & 0 deletions lecture03/math-research-assistant/resource_tag.py
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

0 comments on commit 719ce1f

Please sign in to comment.