Skip to content

Commit ebc50ce

Browse files
author
yatra9
committed
own 20200510
1 parent 3d3df48 commit ebc50ce

File tree

11 files changed

+220
-68
lines changed

11 files changed

+220
-68
lines changed

atcodertools/client/atcoder.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import getpass
1+
# import getpass
22
import os
33
import re
44
import warnings
@@ -54,8 +54,10 @@ def __call__(cls, *args, **kwargs):
5454

5555

5656
def default_credential_supplier() -> Tuple[str, str]:
57-
username = input('AtCoder username: ')
58-
password = getpass.getpass('AtCoder password: ')
57+
# username = input('AtCoder username: ')
58+
# password = getpass.getpass('AtCoder password: ')
59+
username = 'xxxxxxxx'
60+
password = 'yyyyyyyy'
5961
return username, password
6062

6163

@@ -143,6 +145,25 @@ def download_all_contests(self) -> List[Contest]:
143145
contest_ids = sorted(contest_ids)
144146
return [Contest(contest_id) for contest_id in contest_ids]
145147

148+
def download_contest_languages(self, problem_list: List[Problem]) -> List[str]:
149+
contest_cache = {}
150+
for problem in problem_list:
151+
if problem.contest in contest_cache:
152+
soup = contest_cache[problem.contest]
153+
else:
154+
resp = self._request(problem.contest.get_submit_url())
155+
soup = BeautifulSoup(resp.text, "html.parser")
156+
contest_cache[problem.contest] = soup
157+
task_select_area = soup.find(
158+
'select', attrs={"id": "submit-task-selector"})
159+
task_number = task_select_area.find(
160+
"option", text=re.compile('{} -'.format(problem.get_alphabet()))).get("value")
161+
language_select_area = soup.find(
162+
'select', attrs={"id": "submit-language-selector-{}".format(task_number)})
163+
language_select_area.find_all('option')
164+
problem.set_langs(
165+
[option.text for option in language_select_area.find_all('option')])
166+
146167
def submit_source_code(self, contest: Contest, problem: Problem, lang: Union[str, Language], source: str) -> Submission:
147168
if isinstance(lang, str):
148169
warnings.warn(
@@ -173,6 +194,9 @@ def submit_source_code(self, contest: Contest, problem: Problem, lang: Union[str
173194
language_field_name: language_number,
174195
"source_code": source
175196
}
197+
198+
logger.info("Do Post")
199+
176200
resp = self._request(
177201
contest.get_submit_url(),
178202
data=postdata,

atcodertools/client/models/problem.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ def __init__(self, contest: Contest, alphabet: str, problem_id: str):
77
self.contest = contest
88
self.alphabet = alphabet
99
self.problem_id = problem_id
10+
self.langs = []
1011

1112
def get_contest(self) -> Contest:
1213
return self.contest
@@ -24,6 +25,9 @@ def to_dict(self):
2425
"alphabet": self.alphabet
2526
}
2627

28+
def set_langs(self, langs):
29+
self.langs = langs
30+
2731
@classmethod
2832
def from_dict(cls, dic):
2933
return Problem(

atcodertools/client/models/submission.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,34 @@
22

33
from bs4 import BeautifulSoup
44

5+
TBODY_RE = re.compile(r'<tbody>.*</tbody>', re.DOTALL)
6+
TR_RE = re.compile(r'<tr>.*?</tr>', re.DOTALL)
57
PROB_URL_RE = re.compile(
68
r'"/tasks/([A-Za-z0-9\'~+\-_]+)"')
79
SUBMISSION_URL_RE = re.compile(
810
r'"/submissions/([0-9]+)"')
11+
RESULT_RE = re.compile(r'rel="tooltip".*?>(\w+)')
912

1013

1114
class Submission:
1215

13-
def __init__(self, problem_id: str, submission_id: int):
16+
def __init__(self, problem_id: str, submission_id: int, result: str):
1417
self.problem_id = problem_id
1518
self.submission_id = submission_id
19+
self.result = result
1620

1721
@staticmethod
1822
def make_submissions_from(html: str):
1923
soup = BeautifulSoup(html, "html.parser")
2024
text = str(soup)
21-
submitted_problem_ids = PROB_URL_RE.findall(text)
22-
submission_ids = SUBMISSION_URL_RE.findall(text)
23-
assert len(submitted_problem_ids) == len(submission_ids)
24-
return [Submission(pid, int(sid)) for pid, sid in zip(submitted_problem_ids, submission_ids)]
25+
m = TBODY_RE.search(text)
26+
if not m:
27+
return []
28+
text = TBODY_RE.search(text).group()
29+
30+
def extract(elm):
31+
problem_id = PROB_URL_RE.search(elm).group(1)
32+
submission_id = SUBMISSION_URL_RE.search(elm).group(1)
33+
result = RESULT_RE.search(elm).group(1)
34+
return Submission(problem_id, int(submission_id), result)
35+
return [extract(elm) for elm in TR_RE.findall(text)]

atcodertools/codegen/code_generators/julia.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
Format
99
from atcodertools.fmtprediction.models.type import Type
1010
from atcodertools.fmtprediction.models.variable import Variable
11+
from atcodertools.client.models.problem import Problem
1112
from atcodertools.client.models.sample import Sample
1213

1314

@@ -31,28 +32,42 @@ class JuliaCodeGenerator:
3132
def __init__(self,
3233
format_: Optional[Format[Variable]],
3334
config: CodeStyleConfig,
35+
problem: Problem,
3436
samples: List[Sample] = []):
3537
self._format = format_
3638
self._config = config
39+
self._problem = problem
3740
self._samples = samples
3841

3942
def generate_parameters(self) -> Dict[str, Any]:
43+
submission_lang_pattern = self._config.lang.submission_lang_pattern
44+
langs = list(
45+
filter(lambda lang: submission_lang_pattern.match(lang), self._problem.langs))
46+
julia_version = ''
47+
if len(langs) >= 1:
48+
assert len(langs) == 1
49+
m = re.search(r'\((.*)\)', langs[0])
50+
if m:
51+
julia_version = m.group(1)
52+
4053
if self._format is None:
4154
return dict(prediction_success=False,
4255
indent=self._indent(1),
56+
julia_version=julia_version,
4357
samples=self._sample_part())
4458

4559
return dict(formal_arguments=self._formal_arguments(),
4660
actual_arguments=self._actual_arguments(),
4761
input_part=self._input_part(),
4862
prediction_success=True,
4963
indent=self._indent(1),
64+
julia_version=julia_version,
5065
samples=self._sample_part())
5166

5267
def _sample_part(self):
5368
def reshape(raw):
5469
return '"' + raw.strip().replace('\n', '\\n') + '"'
55-
return 'samples = [' + ', '.join(['({}, {})'.format(reshape(s.get_input()), reshape(s.get_output())) for s in self._samples]) + ']'
70+
return 'const samples = [' + ', '.join(['({}, {})'.format(reshape(s.get_input()), reshape(s.get_output())) for s in self._samples]) + ']'
5671

5772
def _input_part(self):
5873
lines = []
@@ -192,11 +207,14 @@ class NoPredictionResultGiven(Exception):
192207

193208
def main(args: CodeGenArgs) -> str:
194209
code_parameters = JuliaCodeGenerator(
195-
args.format, args.config, samples=args.samples).generate_parameters()
210+
args.format, args.config, problem=args.problem, samples=args.samples).generate_parameters()
196211
return render(
197212
args.template,
198213
mod=args.constants.mod,
199214
yes_str=args.constants.yes_str,
200215
no_str=args.constants.no_str,
216+
contest_id=args.problem.contest.get_id(),
217+
problem_id=args.problem.problem_id,
218+
problem_alphabet=args.problem.alphabet,
201219
**code_parameters
202220
)

atcodertools/codegen/models/code_gen_args.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,22 @@
44
from atcodertools.constprediction.models.problem_constant_set import ProblemConstantSet
55
from atcodertools.fmtprediction.models.format import Format
66
from atcodertools.fmtprediction.models.variable import Variable
7+
from atcodertools.client.models.problem import Problem
78
from atcodertools.client.models.sample import Sample
89

10+
911
class CodeGenArgs:
1012

1113
def __init__(self,
1214
template: str,
1315
format_: Optional[Format[Variable]],
1416
constants: ProblemConstantSet,
1517
config: CodeStyleConfig,
18+
problem: Problem,
1619
samples: List[Sample] = []):
1720
self.template = template
1821
self.format = format_
1922
self.constants = constants
2023
self.config = config
24+
self.problem = problem
2125
self.samples = samples

atcodertools/config/config.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@
1717
from atcodertools.tools.utils import with_color
1818

1919
_POST_PROCESS_CONFIG_KEY = "postprocess"
20-
2120
_CODE_STYLE_CONFIG_KEY = "codestyle"
22-
2321
_RUN_CONFIG_KEY = "run"
22+
_ETC_CONFIG_KEY = "etc"
2423

2524

2625
class ProgramArgs:
@@ -33,7 +32,9 @@ def __init__(
3332
save_no_session_cache: Optional[bool] = None,
3433
lang: Optional[str] = None,
3534
compile_before_testing: Optional[bool] = None,
36-
compile_only_when_diff_detected: Optional[bool] = None
35+
compile_only_when_diff_detected: Optional[bool] = None,
36+
without_problem_directory: Optional[bool] = None,
37+
add_execute_permission: Optional[bool] = None
3738
):
3839
self.template = template
3940
self.workspace = workspace
@@ -43,6 +44,8 @@ def __init__(
4344
self.lang = lang
4445
self.compile_before_testing = compile_before_testing
4546
self.compile_only_when_diff_detected = compile_only_when_diff_detected
47+
self.without_problem_directory = without_problem_directory
48+
self.add_execute_permission = add_execute_permission
4649

4750
@classmethod
4851
def load(cls, program_args: argparse.Namespace):
@@ -55,7 +58,9 @@ def load(cls, program_args: argparse.Namespace):
5558
"save_no_session_cache",
5659
"lang",
5760
"compile_before_testing",
58-
"compile_only_when_diff_detected"
61+
"compile_only_when_diff_detected",
62+
"without_problem_directory",
63+
"add_execute_permission"
5964
)})
6065

6166

@@ -92,7 +97,7 @@ def load(cls, fp: TextIO, args: Optional[ProgramArgs] = None):
9297
_CODE_STYLE_CONFIG_KEY, {})
9398

9499
postprocess_config_dic = config_dic.get(_POST_PROCESS_CONFIG_KEY, {})
95-
etc_config_dic = config_dic.get('etc', {})
100+
etc_config_dic = config_dic.get(_ETC_CONFIG_KEY, {})
96101
run_config_dic = config_dic.get(_RUN_CONFIG_KEY, {})
97102
code_style_config_dic = {**common_code_style_config_dic}
98103

@@ -126,6 +131,10 @@ def load(cls, fp: TextIO, args: Optional[ProgramArgs] = None):
126131
run_config_dic = _update_config_dict(run_config_dic,
127132
lang_specific_config_dic[_RUN_CONFIG_KEY])
128133

134+
if _ETC_CONFIG_KEY in lang_specific_config_dic: # e.g. [cpp.etc]
135+
etc_config_dic = _update_config_dict(etc_config_dic,
136+
lang_specific_config_dic[_ETC_CONFIG_KEY])
137+
129138
if args:
130139
code_style_config_dic = _update_config_dict(
131140
code_style_config_dic,
@@ -139,7 +148,9 @@ def load(cls, fp: TextIO, args: Optional[ProgramArgs] = None):
139148
parallel_download=args.parallel,
140149
save_no_session_cache=args.save_no_session_cache,
141150
compile_before_testing=args.compile_before_testing,
142-
compile_only_when_diff_detected=args.compile_only_when_diff_detected
151+
compile_only_when_diff_detected=args.compile_only_when_diff_detected,
152+
without_problem_directory=args.without_problem_directory,
153+
add_execute_permission=args.add_execute_permission
143154
)
144155
)
145156

atcodertools/config/etc_config.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ def __init__(self,
88
out_example_format: str = "out_{}.txt",
99
compile_before_testing: bool = False,
1010
compile_only_when_diff_detected: bool = True,
11+
without_problem_directory: bool = False,
12+
add_execute_permission: bool = False
1113
):
1214
self.download_without_login = download_without_login
1315
self.parallel_download = parallel_download
@@ -16,3 +18,5 @@ def __init__(self,
1618
self.out_example_format = out_example_format
1719
self.compile_before_testing = compile_before_testing
1820
self.compile_only_when_diff_detected = compile_only_when_diff_detected
21+
self.without_problem_directory = without_problem_directory
22+
self.add_execute_permission = add_execute_permission

0 commit comments

Comments
 (0)