Skip to content

Commit

Permalink
Merge pull request #184 from kmyk/feature/typehint
Browse files Browse the repository at this point in the history
#183: add type hints
  • Loading branch information
kmyk authored Nov 3, 2018
2 parents f4391e7 + 70d7bfc commit 0f627b4
Show file tree
Hide file tree
Showing 18 changed files with 344 additions and 249 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
language: python
python:
- 3.5
- 3.6
install:
- pip install mypy
- pip install .
script:
- mypy --ignore-missing-imports oj
- python setup.py test
branches:
only:
Expand Down
22 changes: 13 additions & 9 deletions onlinejudge/anarchygolf.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,33 @@
import posixpath
import bs4
import requests
from typing import *


@utils.singleton
class AnarchyGolfService(onlinejudge.service.Service):

def get_url(self):
def get_url(self) -> str:
return 'http://golf.shinh.org/'

def get_name(self):
def get_name(self) -> str:
return 'anarchygolf'

@classmethod
def from_url(cls, s):
def from_url(cls, s: str) -> Optional['AnarchyGolfService']:
# example: http://golf.shinh.org/
result = urllib.parse.urlparse(s)
if result.scheme in ('', 'http', 'https') \
and result.netloc == 'golf.shinh.org':
return cls()
return None


class AnarchyGolfProblem(onlinejudge.problem.Problem):
def __init__(self, problem_id):
def __init__(self, problem_id: str):
self.problem_id = problem_id

def download(self, session=None):
def download(self, session: Optional[requests.Session] = None) -> List[onlinejudge.problem.TestCase]:
session = session or utils.new_default_session()
# get
resp = utils.request('GET', self.get_url(), session=session)
Expand All @@ -46,7 +48,7 @@ def download(self, session=None):
samples.add(s, name)
return samples.get()

def _parse_sample_tag(self, tag):
def _parse_sample_tag(self, tag: bs4.Tag) -> Optional[Tuple[str, str]]:
assert isinstance(tag, bs4.Tag)
assert tag.name == 'h2'
name = tag.contents[0]
Expand All @@ -61,22 +63,24 @@ def _parse_sample_tag(self, tag):
else:
s = ''
return s, name
return None

def get_url(self):
def get_url(self) -> str:
return 'http://golf.shinh.org/p.rb?{}'.format(self.problem_id)

def get_service(self):
def get_service(self) -> AnarchyGolfService:
return AnarchyGolfService()

@classmethod
def from_url(cls, s):
def from_url(cls, s: str) -> Optional['AnarchyGolfProblem']:
# example: http://golf.shinh.org/p.rb?The+B+Programming+Language
result = urllib.parse.urlparse(s)
if result.scheme in ('', 'http', 'https') \
and result.netloc == 'golf.shinh.org' \
and utils.normpath(result.path) == '/p.rb' \
and result.query:
return cls(result.query)
return None


onlinejudge.dispatch.services += [ AnarchyGolfService ]
Expand Down
30 changes: 17 additions & 13 deletions onlinejudge/aoj.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# -*- coding: utf-8 -*-
import onlinejudge.service
import onlinejudge.problem
from onlinejudge.problem import LabeledString, TestCase
import onlinejudge.dispatch
import onlinejudge.implementation.utils as utils
import onlinejudge.implementation.logging as log
Expand All @@ -13,6 +14,7 @@
import zipfile
import collections
import itertools
from typing import *


@utils.singleton
Expand All @@ -25,24 +27,25 @@ def get_name(self):
return 'aoj'

@classmethod
def from_url(cls, s):
def from_url(cls, s: str) -> Optional['AOJService']:
# example: http://judge.u-aizu.ac.jp/onlinejudge/
result = urllib.parse.urlparse(s)
if result.scheme in ('', 'http', 'https') \
and result.netloc == 'judge.u-aizu.ac.jp':
return cls()
return None


class AOJProblem(onlinejudge.problem.Problem):
def __init__(self, problem_id):
self.problem_id = problem_id

def download(self, session=None, is_system=False):
def download(self, session: Optional[requests.Session] = None, is_system: bool = False) -> List[TestCase]:
if is_system:
return self.download_system(session=session)
else:
return self.download_samples(session=session)
def download_samples(self, session=None):
def download_samples(self, session: Optional[requests.Session] = None) -> List[TestCase]:
session = session or utils.new_default_session()
# get
resp = utils.request('GET', self.get_url(), session=session)
Expand All @@ -65,34 +68,34 @@ def download_samples(self, session=None):
name = hn.string
samples.add(s, name)
return samples.get()
def download_system(self, session=None):
def download_system(self, session: Optional[requests.Session] = None) -> List[TestCase]:
session = session or utils.new_default_session()
get_url = lambda case, type: 'http://analytic.u-aizu.ac.jp:8080/aoj/testcase.jsp?id={}&case={}&type={}'.format(self.problem_id, case, type)
testcases = []
testcases: List[TestCase] = []
for case in itertools.count(1):
# input
# get
resp = utils.request('GET', get_url(case, 'in'), session=session, raise_for_status=False)
if resp.status_code != 200:
break
in_txt = resp.text
if case == 2 and testcases[0]['input']['data'] == in_txt:
if case == 2 and testcases[0].input.data == in_txt:
break # if the querystring case=??? is ignored
# output
# get
resp = utils.request('GET', get_url(case, 'out'), session=session)
out_txt = resp.text
testcases += [ {
'input': { 'data': in_txt, 'name': 'in%d.txt' % case },
'output': { 'data': out_txt, 'name': 'out%d.txt' % case },
} ]
testcases += [ TestCase(
LabeledString('in%d.txt' % case, in_txt),
LabeledString('out%d.txt' % case, out_txt),
) ]
return testcases

def get_url(self):
def get_url(self) -> str:
return 'http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id={}'.format(self.problem_id)

@classmethod
def from_url(cls, s):
def from_url(cls, s: str) -> Optional['AOJProblem']:
# example: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1169
# example: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_1_A&lang=jp
result = urllib.parse.urlparse(s)
Expand All @@ -104,8 +107,9 @@ def from_url(cls, s):
and len(querystring['id']) == 1:
n, = querystring['id']
return cls(n)
return None

def get_service(self):
def get_service(self) -> AOJService:
return AOJService()


Expand Down
Loading

0 comments on commit 0f627b4

Please sign in to comment.