Skip to content

Commit 49a998e

Browse files
format code; init format config
1 parent 1d7dc6b commit 49a998e

17 files changed

+605
-108
lines changed

Diff for: cyaron/compare.py

+30-38
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313

1414
class CompareMismatch(ValueError):
15+
1516
def __init__(self, name, mismatch):
1617
super(CompareMismatch, self).__init__(name, mismatch)
1718
self.name = name
@@ -22,6 +23,7 @@ def __str__(self):
2223

2324

2425
class Compare:
26+
2527
@staticmethod
2628
def __compare_two(name, content, std, grader):
2729
(result, info) = CYaRonGraders.invoke(grader, content, std)
@@ -67,21 +69,20 @@ def output(cls, *files, **kwargs):
6769
max_workers = kwargs["max_workers"]
6870
job_pool = kwargs["job_pool"]
6971
if kwargs["stop_on_incorrect"] is not None:
70-
log.warn("parameter stop_on_incorrect is deprecated and has no effect.")
72+
log.warn(
73+
"parameter stop_on_incorrect is deprecated and has no effect.")
7174

7275
if (max_workers is None or max_workers >= 0) and job_pool is None:
7376
max_workers = cls.__normal_max_workers(max_workers)
7477
try:
7578
from concurrent.futures import ThreadPoolExecutor
7679

7780
with ThreadPoolExecutor(max_workers=max_workers) as job_pool:
78-
return cls.output(
79-
*files,
80-
std=std,
81-
grader=grader,
82-
max_workers=max_workers,
83-
job_pool=job_pool
84-
)
81+
return cls.output(*files,
82+
std=std,
83+
grader=grader,
84+
max_workers=max_workers,
85+
job_pool=job_pool)
8586
except ImportError:
8687
pass
8788

@@ -124,47 +125,44 @@ def program(cls, *programs, **kwargs):
124125
max_workers = kwargs["max_workers"]
125126
job_pool = kwargs["job_pool"]
126127
if kwargs["stop_on_incorrect"] is not None:
127-
log.warn("parameter stop_on_incorrect is deprecated and has no effect.")
128+
log.warn(
129+
"parameter stop_on_incorrect is deprecated and has no effect.")
128130

129131
if (max_workers is None or max_workers >= 0) and job_pool is None:
130132
max_workers = cls.__normal_max_workers(max_workers)
131133
try:
132134
from concurrent.futures import ThreadPoolExecutor
133135

134136
with ThreadPoolExecutor(max_workers=max_workers) as job_pool:
135-
return cls.program(
136-
*programs,
137-
input=input,
138-
std=std,
139-
std_program=std_program,
140-
grader=grader,
141-
max_workers=max_workers,
142-
job_pool=job_pool
143-
)
137+
return cls.program(*programs,
138+
input=input,
139+
std=std,
140+
std_program=std_program,
141+
grader=grader,
142+
max_workers=max_workers,
143+
job_pool=job_pool)
144144
except ImportError:
145145
pass
146146

147147
if not isinstance(input, IO):
148-
raise TypeError(
149-
"expect {}, got {}".format(type(IO).__name__, type(input).__name__)
150-
)
148+
raise TypeError("expect {}, got {}".format(
149+
type(IO).__name__,
150+
type(input).__name__))
151151
input.flush_buffer()
152152
input.input_file.seek(0)
153153

154154
if std_program is not None:
155155

156156
def get_std():
157-
with open(
158-
os.dup(input.input_file.fileno()), "r", newline="\n"
159-
) as input_file:
157+
with open(os.dup(input.input_file.fileno()), "r",
158+
newline="\n") as input_file:
160159
content = make_unicode(
161160
subprocess.check_output(
162161
std_program,
163162
shell=(not list_like(std_program)),
164163
stdin=input.input_file,
165164
universal_newlines=True,
166-
)
167-
)
165+
))
168166
input_file.seek(0)
169167
return content
170168

@@ -188,24 +186,19 @@ def get_std():
188186

189187
def do(program_name):
190188
timeout = None
191-
if (
192-
list_like(program_name)
193-
and len(program_name) == 2
194-
and int_like(program_name[-1])
195-
):
189+
if (list_like(program_name) and len(program_name) == 2
190+
and int_like(program_name[-1])):
196191
program_name, timeout = program_name
197-
with open(
198-
os.dup(input.input_file.fileno()), "r", newline="\n"
199-
) as input_file:
192+
with open(os.dup(input.input_file.fileno()), "r",
193+
newline="\n") as input_file:
200194
if timeout is None:
201195
content = make_unicode(
202196
subprocess.check_output(
203197
program_name,
204198
shell=(not list_like(program_name)),
205199
stdin=input_file,
206200
universal_newlines=True,
207-
)
208-
)
201+
))
209202
else:
210203
content = make_unicode(
211204
subprocess.check_output(
@@ -214,8 +207,7 @@ def do(program_name):
214207
stdin=input_file,
215208
universal_newlines=True,
216209
timeout=timeout,
217-
)
218-
)
210+
))
219211
input_file.seek(0)
220212
cls.__compare_two(program_name, content, std, grader)
221213

Diff for: cyaron/consts.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import absolute_import
22
import math
33
import string
4-
54
"""Constants Package.
65
Constants:
76
ALPHABET_SMALL -> All the lower ascii letters
@@ -18,7 +17,7 @@
1817
ALPHABET_CAPITAL = string.ascii_uppercase
1918
ALPHABET = ALPHABET_SMALL + ALPHABET_CAPITAL
2019
NUMBERS = string.digits
21-
SENTENCE_SEPARATORS = ',,,,,,,;;:' # 70% ',' 20% ';' 10% ':'
22-
SENTENCE_TERMINATORS = '....!' # 80% '.' 20% '!'
20+
SENTENCE_SEPARATORS = ',,,,,,,;;:' # 70% ',' 20% ';' 10% ':'
21+
SENTENCE_TERMINATORS = '....!' # 80% '.' 20% '!'
2322

2423
DEFAULT_GRADER = "NOIPStyle"

Diff for: cyaron/graders/fulltext.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
from .graderregistry import CYaRonGraders
33
from .mismatch import HashMismatch
44

5+
56
@CYaRonGraders.grader("FullText")
67
def fulltext(content, std):
78
content_hash = hashlib.sha256(content.encode('utf-8')).hexdigest()
89
std_hash = hashlib.sha256(std.encode('utf-8')).hexdigest()
9-
return (True, None) if content_hash == std_hash else (False, HashMismatch(content, std, content_hash, std_hash))
10-
10+
return (True, None) if content_hash == std_hash else (
11+
False, HashMismatch(content, std, content_hash, std_hash))

Diff for: cyaron/graders/graderregistry.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ class GraderRegistry:
22
_registry = dict()
33

44
def grader(self, name):
5+
56
def wrapper(func):
67
self._registry[name] = func
78
return func
@@ -15,4 +16,4 @@ def check(self, name):
1516
return name in self._registry
1617

1718

18-
CYaRonGraders = GraderRegistry()
19+
CYaRonGraders = GraderRegistry()

Diff for: cyaron/graders/mismatch.py

+19-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
class Mismatch(ValueError):
22
"""exception for content mismatch"""
3+
34
def __init__(self, content, std, *args):
45
"""
56
content -> content got
@@ -9,25 +10,38 @@ def __init__(self, content, std, *args):
910
self.content = content
1011
self.std = std
1112

13+
1214
class HashMismatch(Mismatch):
1315
"""exception for hash mismatch"""
16+
1417
def __init__(self, content, std, content_hash, std_hash):
1518
"""
1619
content -> content got
1720
std -> content expected
1821
content_hash -> hash of content
1922
std_hash -> hash of std
2023
"""
21-
super(HashMismatch, self).__init__(content, std, content_hash, std_hash)
24+
super(HashMismatch, self).__init__(content, std, content_hash,
25+
std_hash)
2226
self.content_hash = content_hash
2327
self.std_hash = std_hash
2428

2529
def __str__(self):
26-
return "Hash mismatch: read %s, expected %s" % (self.content_hash, self.std_hash)
30+
return "Hash mismatch: read %s, expected %s" % (self.content_hash,
31+
self.std_hash)
32+
2733

2834
class TextMismatch(Mismatch):
2935
"""exception for text mismatch"""
30-
def __init__(self, content, std, err_msg, lineno=None, colno=None, content_token=None, std_token=None):
36+
37+
def __init__(self,
38+
content,
39+
std,
40+
err_msg,
41+
lineno=None,
42+
colno=None,
43+
content_token=None,
44+
std_token=None):
3145
"""
3246
content -> content got
3347
std -> content expected
@@ -37,7 +51,8 @@ def __init__(self, content, std, err_msg, lineno=None, colno=None, content_token
3751
content_token -> the token of content mismatch
3852
std_token -> the token of std
3953
"""
40-
super(TextMismatch, self).__init__(content, std, err_msg, lineno, colno, content_token, std_token)
54+
super(TextMismatch, self).__init__(content, std, err_msg, lineno,
55+
colno, content_token, std_token)
4156
self.err_msg = err_msg.format(lineno, colno, content_token, std_token)
4257
self.lineno = lineno
4358
self.colno = colno

Diff for: cyaron/graders/noipstyle.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ def noipstyle(content, std):
2121
i + 1, j + 1, content_lines[i][j:j + 5],
2222
std_lines[i][j:j + 5]))
2323
if len(std_lines[i]) > len(content_lines[i]):
24-
return False, TextMismatch(
25-
content, std, 'Too short on line {}.', i + 1, j + 1,
26-
content_lines[i][j:j + 5], std_lines[i][j:j + 5])
24+
return False, TextMismatch(content, std,
25+
'Too short on line {}.', i + 1,
26+
j + 1, content_lines[i][j:j + 5],
27+
std_lines[i][j:j + 5])
2728
if len(std_lines[i]) < len(content_lines[i]):
28-
return False, TextMismatch(
29-
content, std, 'Too long on line {}.', i + 1, j + 1,
30-
content_lines[i][j:j + 5], std_lines[i][j:j + 5])
29+
return False, TextMismatch(content, std,
30+
'Too long on line {}.', i + 1,
31+
j + 1, content_lines[i][j:j + 5],
32+
std_lines[i][j:j + 5])
3133

3234
return True, None

Diff for: cyaron/log.py

+31-5
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@
55
try:
66
import colorful
77
except ImportError:
8+
89
class colorful:
10+
911
def __getattr__(self, attr):
1012
return lambda st: st
13+
1114
colorful = colorful()
1215
from .utils import make_unicode
1316

1417
__print = print
18+
19+
1520
def _print(*args, **kwargs):
1621
flush = False
1722
if 'flush' in kwargs:
@@ -21,22 +26,28 @@ def _print(*args, **kwargs):
2126
if flush:
2227
kwargs.get('file', sys.stdout).flush()
2328

29+
2430
def _join_dict(a, b):
2531
"""join two dict"""
2632
c = a.copy()
2733
for k, v in b.items():
2834
c[k] = v
2935
return c
3036

37+
3138
_log_funcs = {}
3239
_log_lock = Lock()
40+
41+
3342
def log(funcname, *args, **kwargs):
3443
"""log with log function specified by ``funcname``"""
3544
_log_lock.acquire()
36-
rv = _log_funcs.get(funcname, lambda *args, **kwargs: None)(*args, **kwargs)
45+
rv = _log_funcs.get(funcname, lambda *args, **kwargs: None)(*args,
46+
**kwargs)
3747
_log_lock.release()
3848
return rv
3949

50+
4051
"""5 log levels
4152
1. debug: debug info
4253
2. info: common info
@@ -51,6 +62,7 @@ def log(funcname, *args, **kwargs):
5162
warn = partial(log, 'warn')
5263
error = partial(log, 'error')
5364

65+
5466
def register_logfunc(funcname, func):
5567
"""register logfunc
5668
str funcname -> name of logfunc
@@ -64,17 +76,28 @@ def register_logfunc(funcname, func):
6476
except KeyError:
6577
pass
6678

67-
_nb_print = lambda *args, **kwargs: _print(*args, **_join_dict(kwargs, {'flush': True}))
68-
_nb_print_e = lambda *args, **kwargs: _print(*args, **_join_dict(kwargs, {'file': sys.stderr, 'flush': True}))
69-
_cl_print = lambda color, *args, **kwargs: _nb_print(*[color(make_unicode(item)) for item in args], **kwargs) if sys.stdout.isatty() else _nb_print(*args, **kwargs)
70-
_cl_print_e = lambda color, *args, **kwargs: _nb_print_e(*[color(make_unicode(item)) for item in args], **kwargs) if sys.stderr.isatty() else _nb_print_e(*args, **kwargs)
79+
80+
_nb_print = lambda *args, **kwargs: _print(
81+
*args, **_join_dict(kwargs, {'flush': True}))
82+
_nb_print_e = lambda *args, **kwargs: _print(
83+
*args, **_join_dict(kwargs, {
84+
'file': sys.stderr,
85+
'flush': True
86+
}))
87+
_cl_print = lambda color, *args, **kwargs: _nb_print(
88+
*[color(make_unicode(item)) for item in args], **kwargs
89+
) if sys.stdout.isatty() else _nb_print(*args, **kwargs)
90+
_cl_print_e = lambda color, *args, **kwargs: _nb_print_e(
91+
*[color(make_unicode(item)) for item in args], **kwargs
92+
) if sys.stderr.isatty() else _nb_print_e(*args, **kwargs)
7193

7294
_default_debug = partial(_cl_print, colorful.cyan)
7395
_default_info = partial(_cl_print, colorful.blue)
7496
_default_print = _nb_print
7597
_default_warn = partial(_cl_print_e, colorful.yellow)
7698
_default_error = partial(_cl_print_e, colorful.red)
7799

100+
78101
def set_quiet():
79102
"""set log mode to "quiet" """
80103
register_logfunc('debug', None)
@@ -83,6 +106,7 @@ def set_quiet():
83106
register_logfunc('warn', None)
84107
register_logfunc('error', _default_error)
85108

109+
86110
def set_normal():
87111
"""set log mode to "normal" """
88112
register_logfunc('debug', None)
@@ -91,6 +115,7 @@ def set_normal():
91115
register_logfunc('warn', _default_warn)
92116
register_logfunc('error', _default_error)
93117

118+
94119
def set_verbose():
95120
"""set log mode to "verbose" """
96121
register_logfunc('debug', _default_debug)
@@ -99,4 +124,5 @@ def set_verbose():
99124
register_logfunc('warn', _default_warn)
100125
register_logfunc('error', _default_error)
101126

127+
102128
set_normal()

0 commit comments

Comments
 (0)