Skip to content

Commit

Permalink
Merge branch 'master' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
weilycoder authored Oct 8, 2024
2 parents ea647b7 + c92f60d commit d5f7980
Show file tree
Hide file tree
Showing 18 changed files with 1,194 additions and 471 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Python package

on:
- push
- pull_request

jobs:
build:
strategy:
matrix:
platform: [ubuntu-20.04, windows-latest]
runs-on: ${{ matrix.platform }}
steps:
- uses: actions/checkout@v3
- name: Set up Python 3.6
uses: actions/setup-python@v4
with:
python-version: '3.6'
- name: Set up Python 3.8
uses: actions/setup-python@v4
with:
python-version: '3.8'
- name: Set up Python 3.10
uses: actions/setup-python@v4
with:
python-version: '3.10'
- name: Set up Python 3.12
uses: actions/setup-python@v4
with:
python-version: '3.12'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install tox
- name: Test with tox
run: python -m tox
10 changes: 9 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
config.py
*.in
*.out
*.exe

# Created by .ignore support plugin (hsz.mobi)
### Python template
Expand Down Expand Up @@ -131,4 +132,11 @@ docs/_build/
target/

# Pycharm
venv
venv/

*.DS_Store

# VS Code
.vscode

.python-version
3 changes: 3 additions & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[MASTER]
py-version=3.5
disable=R0902,R0903,R0913,R0917,R0912
22 changes: 12 additions & 10 deletions cyaron/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@
"""

from __future__ import absolute_import

from random import choice, randint, random, randrange, uniform

#from .visual import visualize
from . import log
from .compare import Compare
from .consts import *
from .graph import Edge, Graph
from .io import IO
from .graph import Graph, Edge
from .string import String
from .math import *
from .merger import Merger
from .polygon import Polygon
from .sequence import Sequence
from .string import String
from .utils import *
from .consts import *
from .vector import Vector
from .polygon import Polygon
from .compare import Compare
from .math import *
from .merger import Merger
#from .visual import visualize
from . import log
from random import randint, randrange, uniform, choice, random
135 changes: 105 additions & 30 deletions cyaron/compare.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import absolute_import, print_function
from cyaron import IO, log
from .io import IO
from . import log
from cyaron.utils import *
from cyaron.consts import *
from cyaron.graders import CYaRonGraders
Expand All @@ -17,7 +18,7 @@ def __init__(self, name, mismatch):
self.mismatch = mismatch

def __str__(self):
return 'In program: \'{}\'. {}'.format(self.name,self.mismatch)
return "In program: '{}'. {}".format(self.name, self.mismatch)


class Compare:
Expand All @@ -37,7 +38,7 @@ def __process_file(file):
file.output_file.seek(0)
return file.output_filename, file.output_file.read()
else:
with open(file, "r", newline='\n') as f:
with open(file, "r", newline="\n") as f:
return file, f.read()

@staticmethod
Expand All @@ -50,26 +51,43 @@ def __normal_max_workers(workers):

@classmethod
def output(cls, *files, **kwargs):
kwargs = unpack_kwargs('output', kwargs, ('std', ('grader', DEFAULT_GRADER), ('max_workers', -1),
('job_pool', None), ('stop_on_incorrect', None)))
std = kwargs['std']
grader = kwargs['grader']
max_workers = kwargs['max_workers']
job_pool = kwargs['job_pool']
if kwargs['stop_on_incorrect'] is not None:
kwargs = unpack_kwargs(
"output",
kwargs,
(
"std",
("grader", DEFAULT_GRADER),
("max_workers", -1),
("job_pool", None),
("stop_on_incorrect", None),
),
)
std = kwargs["std"]
grader = kwargs["grader"]
max_workers = kwargs["max_workers"]
job_pool = kwargs["job_pool"]
if kwargs["stop_on_incorrect"] is not None:
log.warn("parameter stop_on_incorrect is deprecated and has no effect.")

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

with ThreadPoolExecutor(max_workers=max_workers) as job_pool:
return cls.output(*files, std=std, grader=grader, max_workers=max_workers, job_pool=job_pool)
return cls.output(
*files,
std=std,
grader=grader,
max_workers=max_workers,
job_pool=job_pool
)
except ImportError:
pass

def get_std():
return cls.__process_file(std)[1]

if job_pool is not None:
std = job_pool.submit(get_std).result()
else:
Expand All @@ -86,61 +104,118 @@ def do(file):

@classmethod
def program(cls, *programs, **kwargs):
kwargs = unpack_kwargs('program', kwargs, ('input', ('std', None), ('std_program', None),
('grader', DEFAULT_GRADER), ('max_workers', -1),
('job_pool', None), ('stop_on_incorrect', None)))
input = kwargs['input']
std = kwargs['std']
std_program = kwargs['std_program']
grader = kwargs['grader']
max_workers = kwargs['max_workers']
job_pool = kwargs['job_pool']
if kwargs['stop_on_incorrect'] is not None:
kwargs = unpack_kwargs(
"program",
kwargs,
(
"input",
("std", None),
("std_program", None),
("grader", DEFAULT_GRADER),
("max_workers", -1),
("job_pool", None),
("stop_on_incorrect", None),
),
)
input = kwargs["input"]
std = kwargs["std"]
std_program = kwargs["std_program"]
grader = kwargs["grader"]
max_workers = kwargs["max_workers"]
job_pool = kwargs["job_pool"]
if kwargs["stop_on_incorrect"] is not None:
log.warn("parameter stop_on_incorrect is deprecated and has no effect.")

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

with ThreadPoolExecutor(max_workers=max_workers) as job_pool:
return cls.program(*programs, input=input, std=std, std_program=std_program, grader=grader, max_workers=max_workers, job_pool=job_pool)
return cls.program(
*programs,
input=input,
std=std,
std_program=std_program,
grader=grader,
max_workers=max_workers,
job_pool=job_pool
)
except ImportError:
pass

if not isinstance(input, IO):
raise TypeError("expect {}, got {}".format(type(IO).__name__, type(input).__name__))
raise TypeError(
"expect {}, got {}".format(type(IO).__name__, type(input).__name__)
)
input.flush_buffer()
input.input_file.seek(0)

if std_program is not None:

def get_std():
with open(os.dup(input.input_file.fileno()), 'r', newline='\n') as input_file:
content = make_unicode(subprocess.check_output(std_program, shell=(not list_like(std_program)), stdin=input.input_file, universal_newlines=True))
with open(
os.dup(input.input_file.fileno()), "r", newline="\n"
) as input_file:
content = make_unicode(
subprocess.check_output(
std_program,
shell=(not list_like(std_program)),
stdin=input.input_file,
universal_newlines=True,
)
)
input_file.seek(0)
return content

if job_pool is not None:
std = job_pool.submit(get_std).result()
else:
std = get_std()
elif std is not None:

def get_std():
return cls.__process_file(std)[1]

if job_pool is not None:
std = job_pool.submit(get_std).result()
else:
std = get_std()
else:
raise TypeError('program() missing 1 required non-None keyword-only argument: \'std\' or \'std_program\'')
raise TypeError(
"program() missing 1 required non-None keyword-only argument: 'std' or 'std_program'"
)

def do(program_name):
timeout = None
if list_like(program_name) and len(program_name) == 2 and int_like(program_name[-1]):
if (
list_like(program_name)
and len(program_name) == 2
and int_like(program_name[-1])
):
program_name, timeout = program_name
with open(os.dup(input.input_file.fileno()), 'r', newline='\n') as input_file:
with open(
os.dup(input.input_file.fileno()), "r", newline="\n"
) as input_file:
if timeout is None:
content = make_unicode(subprocess.check_output(program_name, shell=(not list_like(program_name)), stdin=input_file, universal_newlines=True))
content = make_unicode(
subprocess.check_output(
program_name,
shell=(not list_like(program_name)),
stdin=input_file,
universal_newlines=True,
)
)
else:
content = make_unicode(subprocess.check_output(program_name, shell=(not list_like(program_name)), stdin=input_file, universal_newlines=True, timeout=timeout))
content = make_unicode(
subprocess.check_output(
program_name,
shell=(not list_like(program_name)),
stdin=input_file,
universal_newlines=True,
timeout=timeout,
)
)
input_file.seek(0)
cls.__compare_two(program_name, content, std, grader)

Expand Down
Loading

0 comments on commit d5f7980

Please sign in to comment.