kattis-cli 1.0.3__py3-none-any.whl → 1.0.5__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
kattis_cli/download.py CHANGED
@@ -2,14 +2,12 @@
2
2
  and save them to data folders.
3
3
  """
4
4
 
5
- # import sys
6
5
  import shutil
7
6
  from typing import Any, Dict
8
7
  from pathlib import Path
9
8
  import requests
10
9
  import yaml
11
10
  from bs4 import BeautifulSoup
12
-
13
11
  from .utils import config, utility
14
12
  from . import settings
15
13
 
kattis_cli/kattis.py CHANGED
@@ -20,7 +20,7 @@ from rich.align import Align
20
20
  from rich.live import Live
21
21
  from rich.prompt import Confirm
22
22
 
23
- from kattis_cli.utils import utility
23
+ from kattis_cli.utils import languages
24
24
  from kattis_cli.utils import config
25
25
  from kattis_cli import ui
26
26
 
@@ -182,7 +182,7 @@ def confirm_or_die(problem: str, language: str,
182
182
  console.print('Language:', language)
183
183
  console.print('Files:', ', '.join(files))
184
184
  if mainclass:
185
- if language in utility.GUESS_MAINFILE:
185
+ if language in languages.GUESS_MAINFILE:
186
186
  console.print('Main file:', mainclass)
187
187
  else:
188
188
  console.print('Mainclass:', mainclass)
kattis_cli/main.py CHANGED
@@ -7,7 +7,7 @@ build.sh script copies the contents of this file to main.py.
7
7
  Change the __version__ to match in pyproject.toml
8
8
  Has to be higher than the pypi version.
9
9
  """
10
- __version__ = '1.0.3'
10
+ __version__ = '1.0.5'
11
11
 
12
12
  from math import inf
13
13
  from typing import Tuple
@@ -18,7 +18,7 @@ import kattis_cli.download as download
18
18
  import kattis_cli.ui as ui
19
19
  import kattis_cli.test_solution as test_solution
20
20
  import kattis_cli.kattis as kattis
21
- import kattis_cli.utils.utility as utility
21
+ import kattis_cli.utils.languages as languages
22
22
  import kattis_cli.kattis_setup as kattis_setup
23
23
 
24
24
 
@@ -75,11 +75,11 @@ def test(
75
75
  """Test solution with sample files.
76
76
  """
77
77
  problemid, loc_language, mainclass, _files, root_folder, lang_config = \
78
- utility.update_args(problemid, language, mainclass, list(files))
78
+ languages.update_args(problemid, language, mainclass, list(files))
79
79
  # print('After - ', f'{problemid=} {language=} {mainclass=} {_files=}')
80
80
  # lang_config = config.parse_config(language)
81
81
  if not mainclass:
82
- mainclass = utility.guess_mainfile(
82
+ mainclass = languages.guess_mainfile(
83
83
  language, _files, problemid, lang_config)
84
84
 
85
85
  test_solution.test_samples(
@@ -111,14 +111,14 @@ def submit(problemid: str, language: str,
111
111
  """Submit a solution to Kattis.
112
112
  """
113
113
  problemid, language, mainclass, _files, _, lang_config = \
114
- utility.update_args(
114
+ languages.update_args(
115
115
  problemid, language, mainclass, list(files))
116
116
  # Finally, submit the solution
117
117
  # print(f'{problemid=} {language=} {mainclass=} {tag=} {force=} {_files=}')
118
118
  if not mainclass:
119
- mainclass = utility.guess_mainfile(
119
+ mainclass = languages.guess_mainfile(
120
120
  language, _files, problemid, lang_config)
121
- kat_lang = utility.LOCAL_TO_KATTIS[language]
121
+ kat_lang = languages.LOCAL_TO_KATTIS[language]
122
122
  kattis.submit_solution(_files, problemid,
123
123
  kat_lang, mainclass,
124
124
  tag, force)
@@ -1,8 +1,8 @@
1
1
  """Tester module for Kattis.
2
2
  """
3
3
 
4
- from math import inf
5
4
  from typing import Any, List, Dict
5
+ from math import inf
6
6
  import glob
7
7
  import time
8
8
  import os
@@ -17,26 +17,7 @@ from rich.prompt import Confirm
17
17
  from rich.markup import escape
18
18
 
19
19
  from kattis_cli import kattis
20
- from kattis_cli.utils import run_program, utility
21
-
22
-
23
- def compare_floats(expected: str, ans: str, places: float) -> bool:
24
- """Compare two floating point numbers with given accuracy.
25
-
26
- Args:
27
- expected (str): expected result
28
- ans (str): actual result
29
- places (float): decimal places for approximation
30
-
31
- Returns:
32
- bool: True if the two numbers are equal within the given accuracy
33
- """
34
- try:
35
- flt_expected = float(expected)
36
- flt_ans = float(ans)
37
- return abs(flt_expected - flt_ans) <= 10**(-places)
38
- except ValueError:
39
- return False
20
+ from kattis_cli.utils import languages, run_program, utility
40
21
 
41
22
 
42
23
  def test_samples(
@@ -138,27 +119,31 @@ def test_samples(
138
119
  input_content = f.read()
139
120
  input_content.replace(b'\r\n', b'\n') # Windows fix
140
121
  out_file = in_file.replace('.in', '.ans')
141
- with open(out_file, 'rb') as f:
142
- expected = f.read()
143
- expected.replace(b'\r\n', b'\n')
122
+ try:
123
+ with open(out_file, 'rb') as f:
124
+ expected = f.read()
125
+ expected.replace(b'\r\n', b'\n')
126
+ except FileNotFoundError:
127
+ try:
128
+ out_file = in_file.replace('.in', '.out')
129
+ with open(out_file, 'rb') as f:
130
+ expected = f.read()
131
+ expected.replace(b'\r\n', b'\n')
132
+ except FileNotFoundError:
133
+ expected = b"No .ans or .out file found!"
144
134
  # Run the program
145
135
  code, ans, error = run_program.run(lang_config, mainclass, in_file)
146
- expected = expected.strip()
147
136
  if code != 0:
148
137
  ans = error
149
138
  # console.print(f"{ans=} {error=}")
150
- if accuracy == inf: # string comparison
151
- if expected == ans.encode('utf-8').strip():
152
- result = "[bold green]✅[/bold green]"
153
- count += 1
154
- else:
155
- result = "[bold red]❌[/bold red]"
156
- else: # floating point comparison
157
- if compare_floats(expected.decode('utf-8'), ans, accuracy):
158
- result = "[bold green]✅[/bold green]"
159
- count += 1
160
- else:
161
- result = "[bold red]❌[/bold red]"
139
+
140
+ # floating point comparison
141
+ if utility.check_answer(expected.decode('utf-8'),
142
+ ans, accuracy):
143
+ result = "[bold green]✅[/bold green]"
144
+ count += 1
145
+ else:
146
+ result = "[bold red]❌[/bold red]"
162
147
 
163
148
  # UI Table Row ---
164
149
  in_filename = Path(in_file).parts[-1]
@@ -188,7 +173,7 @@ def test_samples(
188
173
  "Awesome... Time to submit it to :cat: Kattis! :cat:",
189
174
  style="bold green")
190
175
  if Confirm.ask("Submit to Kattis?", default=True):
191
- kat_language = utility.LOCAL_TO_KATTIS.get(loc_language, '')
176
+ kat_language = languages.LOCAL_TO_KATTIS.get(loc_language, '')
192
177
  kattis.submit_solution(files, problemid,
193
178
  kat_language, mainclass,
194
179
  tag="", force=True)
@@ -9,7 +9,7 @@ import configparser
9
9
  from tomlkit import load
10
10
  import yaml
11
11
 
12
- from .utility import find_problem_root_folder
12
+ from kattis_cli.utils.utility import find_problem_root_folder
13
13
 
14
14
  _DEFAULT_CONFIG = Path.home().joinpath('.kattisrc')
15
15
 
@@ -0,0 +1,332 @@
1
+ """Module for language utilities.
2
+ """
3
+
4
+ from typing import List, Dict, Any
5
+ import sys
6
+ import os
7
+ import re
8
+ from pathlib import Path
9
+ from rich.console import Console
10
+ from kattis_cli.utils import config
11
+ from kattis_cli.utils.utility import find_problem_root_folder
12
+
13
+
14
+ LANGUAGE_GUESS = {
15
+ '.c': 'c',
16
+ '.c++': 'cpp',
17
+ '.cc': 'cpp',
18
+ '.c#': 'csharp',
19
+ '.cpp': 'cpp',
20
+ '.cs': 'csharp',
21
+ '.cxx': 'cpp',
22
+ '.cbl': 'cobol',
23
+ '.cob': 'cobol',
24
+ '.cpy': 'cobol',
25
+ '.fs': 'fsharp',
26
+ '.go': 'go',
27
+ '.hs': 'haskell',
28
+ '.java': 'java',
29
+ '.js': 'nodejs',
30
+ '.ts': 'typescript',
31
+ '.kt': 'kotlin',
32
+ '.lisp': 'lisp',
33
+ '.cl': 'lisp',
34
+ '.m': 'objective-c',
35
+ '.ml': 'ocaml',
36
+ '.pas': 'pascal',
37
+ '.php': 'php',
38
+ '.pl': 'prolog',
39
+ '.py': 'python3',
40
+ '.rb': 'ruby',
41
+ '.rs': 'rust',
42
+ '.scala': 'scala',
43
+ '.f90': 'fortran',
44
+ '.f': 'fortran',
45
+ '.for': 'fortran',
46
+ '.sh': 'bash',
47
+ '.apl': 'apl',
48
+ '.ss': 'gerbil',
49
+ '.jl': 'julia',
50
+ '.vb': 'vb',
51
+ '.dart': 'dart',
52
+ '.zig': 'zig',
53
+ '.swift': 'swift',
54
+ '.nim': 'nim',
55
+ }
56
+
57
+ GUESS_MAINCLASS = {'Java', 'Kotlin', 'Scala'}
58
+
59
+ GUESS_MAINFILE = {
60
+ 'APL',
61
+ 'Bash',
62
+ 'Dart',
63
+ 'Gerbil',
64
+ 'JavaScript (Node.js)',
65
+ 'Julia',
66
+ 'Common Lisp',
67
+ 'Pascal',
68
+ 'PHP',
69
+ 'Python 3',
70
+ 'Ruby',
71
+ 'Rust',
72
+ 'TypeScript',
73
+ 'Zig'
74
+ }
75
+
76
+ # mapping is used for .kattis-cli.toml file configuration
77
+ LOCAL_TO_KATTIS = {
78
+ 'python3': 'Python 3',
79
+ 'java': 'Java',
80
+ 'cpp': 'C++',
81
+ 'c++': 'C++',
82
+ 'nodejs': 'JavaScript (Node.js)',
83
+ 'typescript': 'TypeScript',
84
+ 'csharp': 'C#',
85
+ 'kotlin': 'Kotlin',
86
+ 'scala': 'Scala',
87
+ 'rust': 'Rust',
88
+ 'pascal': 'Pascal',
89
+ 'go': 'Go',
90
+ 'haskell': 'Haskell',
91
+ 'ruby': 'Ruby',
92
+ 'php': 'PHP',
93
+ 'lisp': 'Common Lisp',
94
+ 'fortran': 'Fortran',
95
+ 'bash': 'Bash',
96
+ 'apl': 'APL',
97
+ 'gerbil': 'Gerbil',
98
+ 'julia': 'Julia',
99
+ 'vb': 'Visual Basic',
100
+ 'dart': 'Dart',
101
+ 'zig': 'Zig',
102
+ 'swift': 'Swift',
103
+ 'nim': 'Nim',
104
+ 'ocaml': 'OCaml',
105
+ 'fsharp': 'F#',
106
+ 'cobol': 'COBOL',
107
+ 'prolog': 'Prolog',
108
+ 'objective-c': 'Objective-C',
109
+ 'c': 'C',
110
+ 'c#': 'C#'
111
+ }
112
+
113
+
114
+ def guess_language(ext: str, files: List[str]) -> str:
115
+ """Guess the language.
116
+
117
+ Args:
118
+ ext (str): File extension.
119
+ files (List[str]): Tuple of files.
120
+
121
+ Returns:
122
+ str: guessed language
123
+ """
124
+ if ext == ".C":
125
+ return "cpp"
126
+ ext = ext.lower()
127
+ if ext == ".h":
128
+ if any(f.endswith(".c") for f in files):
129
+ return "c"
130
+ else:
131
+ return "cpp"
132
+ if ext == ".py":
133
+ return "python3"
134
+ return LANGUAGE_GUESS.get(ext, '')
135
+
136
+
137
+ # flake8: noqa: C901
138
+ def guess_mainfile(
139
+ kat_language: str,
140
+ files: List[str],
141
+ problemid: str,
142
+ lang_config: Dict[Any, Any]) -> Any:
143
+ """Guess the main file.
144
+
145
+ Args:
146
+ kat_language (str): programming language name used by Kattis
147
+ files (List[str]): Tuple of files
148
+
149
+ Returns:
150
+ str: main file
151
+ """
152
+ if len(files) == 1:
153
+ return files[0]
154
+ # check .kattis-cli.toml file
155
+ if 'mainfile' in lang_config:
156
+ return lang_config['mainfile'].replace(('{problemid}'), problemid)
157
+ for filename in files:
158
+ if os.path.splitext(os.path.basename(filename))[0] in ['main', 'Main']:
159
+ return filename
160
+ if problemid and os.path.splitext(
161
+ os.path.basename(filename))[0] == problemid:
162
+ return filename
163
+ for filename in files:
164
+ try:
165
+ with open(filename, 'r', encoding='utf-8') as f:
166
+ conts = f.read()
167
+ if kat_language in [
168
+ 'Java', 'Rust', 'Scala', 'Kotlin'] and re.search(
169
+ r' main\s*\(', conts):
170
+ return filename
171
+ if kat_language == 'Pascal' and re.match(
172
+ r'^\s*[Pp]rogram\b', conts):
173
+ return filename
174
+ except IOError:
175
+ pass
176
+ # main file is the one with problemid
177
+ return files[0]
178
+
179
+
180
+ def guess_mainclass(
181
+ problemid: str,
182
+ kat_language: str,
183
+ files: List[str],
184
+ lang_config: Any) -> Any:
185
+ """Guess the main class.
186
+
187
+ Args:
188
+ kat_language (str): programming language name used by Kattis
189
+ files (List[str]): List of files to guess mainclass from
190
+
191
+ Returns:
192
+ str: mainclass name or empty string
193
+ """
194
+ if kat_language in GUESS_MAINFILE and len(files) > 1:
195
+ return os.path.basename(
196
+ guess_mainfile(
197
+ kat_language,
198
+ files,
199
+ problemid,
200
+ lang_config))
201
+ if kat_language in GUESS_MAINCLASS:
202
+ mainfile = os.path.basename(
203
+ guess_mainfile(
204
+ kat_language,
205
+ files,
206
+ problemid,
207
+ lang_config))
208
+ name = os.path.splitext(mainfile)[0]
209
+ if kat_language == 'Kotlin':
210
+ return name[0].upper() + name[1:] + 'Kt'
211
+ return name
212
+ return ''
213
+
214
+
215
+ def validate_language(loc_language: str) -> bool:
216
+ """Check if valid language.
217
+
218
+ Args:
219
+ loc_language (str): programming language provided by user
220
+
221
+ Returns:
222
+ bool: True if valid language, exit otherwise.
223
+ """
224
+ if loc_language in LOCAL_TO_KATTIS:
225
+ return True
226
+ console = Console()
227
+ console.print(f'Invalid language: "{loc_language}"', style='bold red')
228
+ console.print('Valid languages are:', style='bold green')
229
+ for lang in sorted(LOCAL_TO_KATTIS.keys()):
230
+ console.print(f'\t\t - {lang}')
231
+ exit(1)
232
+
233
+
234
+ def valid_extension(file: str) -> bool:
235
+ """Check if valid extension.
236
+
237
+ Args:
238
+ file (str): File name.
239
+
240
+ Returns:
241
+ bool: True if valid extension, False otherwise.
242
+ """
243
+ if os.path.isfile(file):
244
+ ext = os.path.splitext(file)
245
+ if len(ext) != 2:
246
+ return False
247
+ return ext[1] in LANGUAGE_GUESS
248
+ return False
249
+
250
+
251
+ # flake8: noqa: C901
252
+ def update_args(problemid: str,
253
+ loc_language: str,
254
+ mainclass: str,
255
+ files: List[str]) -> Any:
256
+ """Check if problemid, language, mainclass, and program files are valid.
257
+
258
+ Args:
259
+ problemid (str): problemid
260
+ loc_language (str): programming language provided by user
261
+ mainclass (str): main class
262
+ files (List[str]): List of files
263
+
264
+ Returns:
265
+ Tuple[str]: Update problemid, kattis_language, mainclass, and files
266
+ """
267
+
268
+ console = Console()
269
+ if not files:
270
+ files = get_coding_files()
271
+ # check if problemid is given
272
+ # if not problemid:
273
+ cur_folder = Path.cwd()
274
+ root_folder = Path.cwd()
275
+ if not problemid:
276
+ for f in files:
277
+ try:
278
+ root_folder = find_problem_root_folder(
279
+ cur_folder, f.lower())
280
+ # if not problemid:
281
+ problemid = root_folder.name
282
+ break
283
+ except FileNotFoundError:
284
+ # print(ex)
285
+ pass
286
+ if not problemid:
287
+ try:
288
+ root_folder = find_problem_root_folder(
289
+ cur_folder, '*.yaml')
290
+ problemid = root_folder.name
291
+ except FileNotFoundError:
292
+ console.print(f'''No problemid specified and I failed to guess
293
+ problemid and root problem folder from filename(s) and cwd: {cur_folder}.''',
294
+ style='bold red')
295
+ sys.exit(1)
296
+ # check if language
297
+ if not loc_language:
298
+ _, ext = os.path.splitext(os.path.basename(files[0]))
299
+ # Guess language from files
300
+ loc_language = guess_language(ext, files)
301
+ if not loc_language:
302
+ console.print(f'''\
303
+ No language specified, and I failed to guess language from
304
+ filename extension "{ext}"''')
305
+ sys.exit(1)
306
+ # check if valid language
307
+ validate_language(loc_language)
308
+ lang_config = config.parse_config(loc_language)
309
+ kat_language = LOCAL_TO_KATTIS[loc_language]
310
+ if not mainclass:
311
+ mainclass = guess_mainclass(
312
+ problemid, kat_language, files, lang_config)
313
+ # print(f'Returning...{problemid=} {language=} {mainclass=} {files=}')
314
+ return problemid, loc_language, mainclass, files, root_folder, lang_config
315
+
316
+
317
+ def get_coding_files() -> List[str]:
318
+ """Get coding files from current directory.
319
+
320
+ Returns:
321
+ List[str]: List of coding files.
322
+ """
323
+ cur_folder = str(Path.cwd())
324
+ console = Console()
325
+ files = [
326
+ f for f in os.listdir(cur_folder) if valid_extension(f)]
327
+ if not files:
328
+ console.print(
329
+ 'No source file(s) found in the current folder!',
330
+ style='bold red')
331
+ exit(1)
332
+ return files
@@ -1,251 +1,10 @@
1
- """Module for language utilities.
2
- """
1
+ """ Utility functions. """
3
2
 
4
- from typing import List, Union, Any, Dict
5
- import sys
6
- import os
7
- import re
8
3
  from pathlib import Path
4
+ import os
5
+ from math import inf
6
+ from typing import Union
9
7
  import yaml
10
- from rich.console import Console
11
- from kattis_cli.utils import config
12
-
13
-
14
- LANGUAGE_GUESS = {
15
- '.c': 'c',
16
- '.c++': 'cpp',
17
- '.cc': 'cpp',
18
- '.c#': 'csharp',
19
- '.cpp': 'cpp',
20
- '.cs': 'csharp',
21
- '.cxx': 'cpp',
22
- '.cbl': 'cobol',
23
- '.cob': 'cobol',
24
- '.cpy': 'cobol',
25
- '.fs': 'fsharp',
26
- '.go': 'go',
27
- '.hs': 'haskell',
28
- '.java': 'java',
29
- '.js': 'nodejs',
30
- '.ts': 'typescript',
31
- '.kt': 'kotlin',
32
- '.lisp': 'lisp',
33
- '.cl': 'lisp',
34
- '.m': 'objective-c',
35
- '.ml': 'ocaml',
36
- '.pas': 'pascal',
37
- '.php': 'php',
38
- '.pl': 'prolog',
39
- '.py': 'python3',
40
- '.rb': 'ruby',
41
- '.rs': 'rust',
42
- '.scala': 'scala',
43
- '.f90': 'fortran',
44
- '.f': 'fortran',
45
- '.for': 'fortran',
46
- '.sh': 'bash',
47
- '.apl': 'apl',
48
- '.ss': 'gerbil',
49
- '.jl': 'julia',
50
- '.vb': 'vb',
51
- '.dart': 'dart',
52
- '.zig': 'zig',
53
- '.swift': 'swift',
54
- '.nim': 'nim',
55
- }
56
-
57
- GUESS_MAINCLASS = {'Java', 'Kotlin', 'Scala'}
58
-
59
- GUESS_MAINFILE = {
60
- 'APL',
61
- 'Bash',
62
- 'Dart',
63
- 'Gerbil',
64
- 'JavaScript (Node.js)',
65
- 'Julia',
66
- 'Common Lisp',
67
- 'Pascal',
68
- 'PHP',
69
- 'Python 3',
70
- 'Ruby',
71
- 'Rust',
72
- 'TypeScript',
73
- 'Zig'
74
- }
75
-
76
- # mapping is used for .kattis-cli.toml file configuration
77
- LOCAL_TO_KATTIS = {
78
- 'python3': 'Python 3',
79
- 'java': 'Java',
80
- 'cpp': 'C++',
81
- 'c++': 'C++',
82
- 'nodejs': 'JavaScript (Node.js)',
83
- 'typescript': 'TypeScript',
84
- 'csharp': 'C#',
85
- 'kotlin': 'Kotlin',
86
- 'scala': 'Scala',
87
- 'rust': 'Rust',
88
- 'pascal': 'Pascal',
89
- 'go': 'Go',
90
- 'haskell': 'Haskell',
91
- 'ruby': 'Ruby',
92
- 'php': 'PHP',
93
- 'lisp': 'Common Lisp',
94
- 'fortran': 'Fortran',
95
- 'bash': 'Bash',
96
- 'apl': 'APL',
97
- 'gerbil': 'Gerbil',
98
- 'julia': 'Julia',
99
- 'vb': 'Visual Basic',
100
- 'dart': 'Dart',
101
- 'zig': 'Zig',
102
- 'swift': 'Swift',
103
- 'nim': 'Nim',
104
- 'ocaml': 'OCaml',
105
- 'fsharp': 'F#',
106
- 'cobol': 'COBOL',
107
- 'prolog': 'Prolog',
108
- 'objective-c': 'Objective-C',
109
- 'c': 'C',
110
- 'c#': 'C#'
111
- }
112
-
113
-
114
- def guess_language(ext: str, files: List[str]) -> str:
115
- """Guess the language.
116
-
117
- Args:
118
- ext (str): File extension.
119
- files (List[str]): Tuple of files.
120
-
121
- Returns:
122
- str: guessed language
123
- """
124
- if ext == ".C":
125
- return "cpp"
126
- ext = ext.lower()
127
- if ext == ".h":
128
- if any(f.endswith(".c") for f in files):
129
- return "c"
130
- else:
131
- return "cpp"
132
- if ext == ".py":
133
- return "python3"
134
- return LANGUAGE_GUESS.get(ext, '')
135
-
136
-
137
- # flake8: noqa: C901
138
- def guess_mainfile(
139
- kat_language: str,
140
- files: List[str],
141
- problemid: str,
142
- lang_config: Dict[Any, Any]) -> Any:
143
- """Guess the main file.
144
-
145
- Args:
146
- kat_language (str): programming language name used by Kattis
147
- files (List[str]): Tuple of files
148
-
149
- Returns:
150
- str: main file
151
- """
152
- if len(files) == 1:
153
- return files[0]
154
- # check .kattis-cli.toml file
155
- if 'mainfile' in lang_config:
156
- return lang_config['mainfile'].replace(('{problemid}'), problemid)
157
- for filename in files:
158
- if os.path.splitext(os.path.basename(filename))[0] in ['main', 'Main']:
159
- return filename
160
- if problemid and os.path.splitext(
161
- os.path.basename(filename))[0] == problemid:
162
- return filename
163
- for filename in files:
164
- try:
165
- with open(filename, 'r', encoding='utf-8') as f:
166
- conts = f.read()
167
- if kat_language in [
168
- 'Java', 'Rust', 'Scala', 'Kotlin'] and re.search(
169
- r' main\s*\(', conts):
170
- return filename
171
- if kat_language == 'Pascal' and re.match(
172
- r'^\s*[Pp]rogram\b', conts):
173
- return filename
174
- except IOError:
175
- pass
176
- # main file is the one with problemid
177
- return files[0]
178
-
179
-
180
- def guess_mainclass(
181
- problemid: str,
182
- kat_language: str,
183
- files: List[str],
184
- lang_config: Any) -> Any:
185
- """Guess the main class.
186
-
187
- Args:
188
- kat_language (str): programming language name used by Kattis
189
- files (List[str]): List of files to guess mainclass from
190
-
191
- Returns:
192
- str: mainclass name or empty string
193
- """
194
- if kat_language in GUESS_MAINFILE and len(files) > 1:
195
- return os.path.basename(
196
- guess_mainfile(
197
- kat_language,
198
- files,
199
- problemid,
200
- lang_config))
201
- if kat_language in GUESS_MAINCLASS:
202
- mainfile = os.path.basename(
203
- guess_mainfile(
204
- kat_language,
205
- files,
206
- problemid,
207
- lang_config))
208
- name = os.path.splitext(mainfile)[0]
209
- if kat_language == 'Kotlin':
210
- return name[0].upper() + name[1:] + 'Kt'
211
- return name
212
- return ''
213
-
214
-
215
- def validate_language(loc_language: str) -> bool:
216
- """Check if valid language.
217
-
218
- Args:
219
- loc_language (str): programming language provided by user
220
-
221
- Returns:
222
- bool: True if valid language, exit otherwise.
223
- """
224
- if loc_language in LOCAL_TO_KATTIS:
225
- return True
226
- console = Console()
227
- console.print(f'Invalid language: "{loc_language}"', style='bold red')
228
- console.print('Valid languages are:', style='bold green')
229
- for lang in sorted(LOCAL_TO_KATTIS.keys()):
230
- console.print(f'\t\t - {lang}')
231
- exit(1)
232
-
233
-
234
- def valid_extension(file: str) -> bool:
235
- """Check if valid extension.
236
-
237
- Args:
238
- file (str): File name.
239
-
240
- Returns:
241
- bool: True if valid extension, False otherwise.
242
- """
243
- if os.path.isfile(file):
244
- ext = os.path.splitext(file)
245
- if len(ext) != 2:
246
- return False
247
- return ext[1] in LANGUAGE_GUESS
248
- return False
249
8
 
250
9
 
251
10
  def find_problem_root_folder(
@@ -302,85 +61,34 @@ def find_problem_root_folder(
302
61
  raise FileNotFoundError("Error: Problem root folder not found.")
303
62
 
304
63
 
305
- # flake8: noqa: C901
306
- def update_args(problemid: str,
307
- loc_language: str,
308
- mainclass: str,
309
- files: List[str]) -> Any:
310
- """Check if problemid, language, mainclass, and program files are valid.
64
+ def check_answer(expected: str, ans: str, places: float = inf) -> bool:
65
+ """Compare two numeric strings with given precision.
311
66
 
312
67
  Args:
313
- problemid (str): problemid
314
- loc_language (str): programming language provided by user
315
- mainclass (str): main class
316
- files (List[str]): List of files
68
+ expected (str): expected result
69
+ ans (str): actual result
70
+ places (float): decimal places for approximation;
71
+ default inf for string comparison
317
72
 
318
73
  Returns:
319
- Tuple[str]: Update problemid, kattis_language, mainclass, and files
74
+ bool: True if the two values are equal, False otherwise
320
75
  """
76
+ expect_ans = expected.strip().split('\n')
77
+ actual_ans = ans.strip().split('\n')
78
+ if len(expect_ans) != len(actual_ans):
79
+ return False
321
80
 
322
- console = Console()
323
- if not files:
324
- files = get_coding_files()
325
- # check if problemid is given
326
- # if not problemid:
327
- cur_folder = Path.cwd()
328
- root_folder = Path.cwd()
329
- if not problemid:
330
- for f in files:
331
- try:
332
- root_folder = find_problem_root_folder(
333
- cur_folder, f.lower())
334
- # if not problemid:
335
- problemid = root_folder.name
336
- break
337
- except FileNotFoundError:
338
- # print(ex)
339
- pass
340
- if not problemid:
341
- try:
342
- root_folder = find_problem_root_folder(
343
- cur_folder, '*.yaml')
344
- problemid = root_folder.name
345
- except FileNotFoundError:
346
- console.print(f'''No problemid specified and I failed to guess
347
- problemid and root problem folder from filename(s) and cwd: {cur_folder}.''',
348
- style='bold red')
349
- sys.exit(1)
350
- # check if language
351
- if not loc_language:
352
- _, ext = os.path.splitext(os.path.basename(files[0]))
353
- # Guess language from files
354
- loc_language = guess_language(ext, files)
355
- if not loc_language:
356
- console.print(f'''\
357
- No language specified, and I failed to guess language from
358
- filename extension "{ext}"''')
359
- sys.exit(1)
360
- # check if valid language
361
- validate_language(loc_language)
362
- lang_config = config.parse_config(loc_language)
363
- kat_language = LOCAL_TO_KATTIS[loc_language]
364
- if not mainclass:
365
- mainclass = guess_mainclass(
366
- problemid, kat_language, files, lang_config)
367
- # print(f'Returning...{problemid=} {language=} {mainclass=} {files=}')
368
- return problemid, loc_language, mainclass, files, root_folder, lang_config
369
-
370
-
371
- def get_coding_files() -> List[str]:
372
- """Get coding files from current directory.
373
-
374
- Returns:
375
- List[str]: List of coding files.
376
- """
377
- cur_folder = str(Path.cwd())
378
- console = Console()
379
- files = [
380
- f for f in os.listdir(cur_folder) if valid_extension(f)]
381
- if not files:
382
- console.print(
383
- 'No source file(s) found in the current folder!',
384
- style='bold red')
385
- exit(1)
386
- return files
81
+ if places == inf:
82
+ if expect_ans == actual_ans:
83
+ return True
84
+ else:
85
+ return False
86
+ try:
87
+ for i, ex_ans in enumerate(expect_ans):
88
+ flt_expected = float(ex_ans)
89
+ flt_ans = float(actual_ans[i])
90
+ if abs(flt_expected - flt_ans) > 10**(-places):
91
+ return False
92
+ return True
93
+ except ValueError:
94
+ return False
@@ -1,10 +1,10 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: kattis-cli
3
- Version: 1.0.3
3
+ Version: 1.0.5
4
4
  Summary: A command-line tool for Kattis
5
5
  Home-page: https://github.com/rambasnet/kattis-cli
6
6
  Author: Ram Basnet
7
- Author-email: rambasnet@gmail.com
7
+ Author-email: rbasnet@coloradomesa.edu
8
8
  Requires-Python: >=3.8,<4.0
9
9
  Classifier: License :: OSI Approved :: MIT License
10
10
  Classifier: Operating System :: OS Independent
@@ -0,0 +1,19 @@
1
+ kattis_cli/.kattis-cli.toml,sha256=kGh0gmpFAnivyUC9hR8Y1ZqYJeTBWNyBQ9zOg4AInZ0,898
2
+ kattis_cli/__init__.py,sha256=afN92pog2fGyicY6KNBofYbCBYbYj4Fpi_INUSpsc-E,402
3
+ kattis_cli/download.py,sha256=kvUJdqRW3_ETvTd4XMruEs87RWPVHp_3a4VUhcYZEz8,5863
4
+ kattis_cli/kattis.py,sha256=uZct7vjbEW2HA5FJr1ItSZ-b07P9JQHv5SccV4Pqjo8,13771
5
+ kattis_cli/kattis_setup.py,sha256=rq_-Fz8oafxFJ8MOcxJqHQT8-bezgVHZXY2hm4EQF3U,4357
6
+ kattis_cli/main.py,sha256=paMidWBPOCInt_o9WsyS9HoYdNIU346u-KTn9YB2Ymk,4475
7
+ kattis_cli/settings.py,sha256=d5q4dYj9VqDSqPalleh2oZWtND-1bPB0T2IwdajFrBg,591
8
+ kattis_cli/test_solution.py,sha256=6lXaj9V5Vhfw95OywVls3Ekv86OvSYDrWI-s29YBdpk,6190
9
+ kattis_cli/ui.py,sha256=dI06yncjm8sdLQkupPxGRODvQ6Gbawwuqn-67FrWg6I,2682
10
+ kattis_cli/utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
11
+ kattis_cli/utils/config.py,sha256=6SIuiXA9sFADo8RZUj8A6rXb3uqmrWrmmArfZrqNQdw,2293
12
+ kattis_cli/utils/languages.py,sha256=wlhl1Zng0gQAtsWW6FLxdkdzqeeY_qQ94wqa3NLypEU,8758
13
+ kattis_cli/utils/run_program.py,sha256=NWQ6vtTeWgkaW75r91FIHGXR5cAbeu8yMb5hwzpYFsg,2613
14
+ kattis_cli/utils/utility.py,sha256=iPmfsHi-DwYxP9mKU_VurNnLJXf0dngwYPxUdqckgYo,2907
15
+ kattis_cli-1.0.5.dist-info/LICENSE,sha256=JmBa4SEKBCDWEgiOZcISU4tUCpli6xSpVlSYgkBXSNQ,1067
16
+ kattis_cli-1.0.5.dist-info/METADATA,sha256=0WYu19PZ0_ih3Y49Uxgj1JnZRi23fYgoDFYGCUSWN6Q,6603
17
+ kattis_cli-1.0.5.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
18
+ kattis_cli-1.0.5.dist-info/entry_points.txt,sha256=kyzGN20VqUPR_H0J_jJUKT-10-cAMFLVegQ6C7tbHss,47
19
+ kattis_cli-1.0.5.dist-info/RECORD,,
@@ -1,4 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: poetry-core 1.9.0
2
+ Generator: poetry-core 1.8.1
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
@@ -1,18 +0,0 @@
1
- kattis_cli/.kattis-cli.toml,sha256=kGh0gmpFAnivyUC9hR8Y1ZqYJeTBWNyBQ9zOg4AInZ0,898
2
- kattis_cli/__init__.py,sha256=afN92pog2fGyicY6KNBofYbCBYbYj4Fpi_INUSpsc-E,402
3
- kattis_cli/download.py,sha256=aa0hBg_6Ou8SGrufgkNUjfWEHdmiu7U-W4d-yWHZNOA,5877
4
- kattis_cli/kattis.py,sha256=t5fog-tx1f1GeM-D3aa2YhuhxTnai_kV-x7miZbs6SI,13767
5
- kattis_cli/kattis_setup.py,sha256=rq_-Fz8oafxFJ8MOcxJqHQT8-bezgVHZXY2hm4EQF3U,4357
6
- kattis_cli/main.py,sha256=hTr63pQfRXxxIL-4G6EZ1g6qDFgaPd8PI35850PL0vs,4461
7
- kattis_cli/settings.py,sha256=d5q4dYj9VqDSqPalleh2oZWtND-1bPB0T2IwdajFrBg,591
8
- kattis_cli/test_solution.py,sha256=KKYedt3ISQYLIoahLPgPizpApLQnOrBhmkew5aHW9BM,6627
9
- kattis_cli/ui.py,sha256=dI06yncjm8sdLQkupPxGRODvQ6Gbawwuqn-67FrWg6I,2682
10
- kattis_cli/utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
11
- kattis_cli/utils/config.py,sha256=cr5ssEVWolDbqP8krts0YSK5vSLatKTveeHBRNsLRC8,2277
12
- kattis_cli/utils/run_program.py,sha256=NWQ6vtTeWgkaW75r91FIHGXR5cAbeu8yMb5hwzpYFsg,2613
13
- kattis_cli/utils/utility.py,sha256=JkypTfPOlDSxmZsh7KKcAVG2BC58t66corMBtcdvU7E,10532
14
- kattis_cli-1.0.3.dist-info/LICENSE,sha256=JmBa4SEKBCDWEgiOZcISU4tUCpli6xSpVlSYgkBXSNQ,1067
15
- kattis_cli-1.0.3.dist-info/METADATA,sha256=pOGeql7dMWdo0fVMb6N2TMYcb3zEHgq4a1aDDyzmfGw,6598
16
- kattis_cli-1.0.3.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
17
- kattis_cli-1.0.3.dist-info/entry_points.txt,sha256=kyzGN20VqUPR_H0J_jJUKT-10-cAMFLVegQ6C7tbHss,47
18
- kattis_cli-1.0.3.dist-info/RECORD,,