Skip to content

Commit 2ecd16b

Browse files
committed
Merge branch 'master' of https://github.com/luogu-dev/cyaron
2 parents 441faa7 + e7b8f2f commit 2ecd16b

File tree

9 files changed

+107
-12
lines changed

9 files changed

+107
-12
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
CYaRon: Yet Another Random Olympic-iNformatics test data generator
33

44
By Luogu
5-
![](https://travis-ci.org/luogu-dev/cyaron.svg?branch=master)
5+
[![](https://travis-ci.org/luogu-dev/cyaron.svg?branch=master)](https://travis-ci.org/luogu-dev/cyaron)
66

77
你是否遇到以下情况:
88
- 希望在5分钟内写出一组随机数据

cyaron/io.py

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,26 @@ def __del__(self):
5757
except Exception:
5858
pass
5959

60+
def __enter__(self):
61+
return self
62+
63+
def __exit__(self, exc_type, exc_val, exc_tb):
64+
"""__del__(self) -> None
65+
Exit the context of the IO object and close the input file and the output file
66+
"""
67+
try:
68+
self.input_file.close()
69+
self.output_file.close()
70+
except Exception:
71+
pass
72+
6073
@staticmethod
6174
def __write(file, *args, **kwargs):
62-
"""__write(file, *args) -> None
75+
"""__write(file, *args, **kwargs) -> None
6376
Write every element in *args into file. If the element isn't "\n", insert a space. It will convert every element into str
6477
file file -> the file object to write
78+
**kwargs:
79+
str separator = " " -> a string used to separate every element
6580
"""
6681
separator = kwargs.get("separator", " ")
6782
for arg in args:
@@ -73,14 +88,18 @@ def __write(file, *args, **kwargs):
7388
file.write(separator)
7489

7590
def input_write(self, *args, **kwargs):
76-
"""input_write(self, *args) -> None
91+
"""input_write(self, *args, **kwargs) -> None
7792
Write every element in *args into the input file. Splits with spaces. It will convert every element into string
93+
**kwargs:
94+
str separator = " " -> a string used to separate every element
7895
"""
7996
IO.__write(self.input_file, *args, **kwargs)
8097

8198
def input_writeln(self, *args, **kwargs):
82-
"""input_writeln(self, *args) -> None
99+
"""input_writeln(self, *args, **kwargs) -> None
83100
Write every element in *args into the input file and turn to a new line. Splits with spaces. It will convert every element into string
101+
**kwargs:
102+
str separator = " " -> a string used to separate every element
84103
"""
85104
args = list(args)
86105
args.append("\n")
@@ -93,19 +112,23 @@ def output_gen(self, shell_cmd):
93112
"""
94113
self.input_file.close()
95114
with open(self.input_filename, 'r') as f:
96-
self.output_file.write(subprocess.check_output(shell_cmd, shell=True, stdin=f))
115+
self.output_file.write(subprocess.check_output(shell_cmd, shell=True, stdin=f).decode('ascii'))
97116

98117
self.input_file = open(self.input_filename, 'a')
99118

100119
def output_write(self, *args, **kwargs):
101-
"""output_write(self, *args) -> None
120+
"""output_write(self, *args, **kwargs) -> None
102121
Write every element in *args into the output file. Splits with spaces. It will convert every element into string
122+
**kwargs:
123+
str separator = " " -> a string used to separate every element
103124
"""
104125
IO.__write(self.output_file, *args, **kwargs)
105126

106127
def output_writeln(self, *args, **kwargs):
107-
"""output_writeln(self, *args) -> None
128+
"""output_writeln(self, *args, **kwargs) -> None
108129
Write every element in *args into the output file and turn to a new line. Splits with spaces. It will convert every element into string
130+
**kwargs:
131+
str separator = " " -> a string used to separate every element
109132
"""
110133
args = list(args)
111134
args.append("\n")

cyaron/sequence.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ class Sequence:
44
"""Class Sequence: the tool class for sequences.
55
"""
66

7-
def __init__(self, formula, initial_values=[]):
8-
"""__init__(self, formula, initial_values=[]) -> None
7+
def __init__(self, formula, initial_values=()):
8+
"""__init__(self, formula, initial_values=() -> None
99
Create a sequence object.
1010
int formula(int, function) -> the formula function ...
1111
"""

cyaron/tests/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
from .sequence_test import TestSequence
1+
from .sequence_test import TestSequence
2+
from .io_test import TestIO
3+
from .str_test import TestString

cyaron/tests/io_test.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import unittest
2+
import os
3+
import shutil
4+
import tempfile
5+
from cyaron import IO
6+
7+
8+
class TestIO(unittest.TestCase):
9+
10+
def setUp(self):
11+
self.temp_directory = tempfile.mkdtemp()
12+
os.chdir(self.temp_directory)
13+
14+
def tearDown(self):
15+
shutil.rmtree(self.temp_directory)
16+
17+
def test_create_files_simple(self):
18+
IO("test_simple.in", "test_simple.out")
19+
self.assertTrue(os.path.exists("test_simple.in"))
20+
self.assertTrue(os.path.exists("test_simple.out"))
21+
22+
def test_create_files_prefix_id(self):
23+
IO(file_prefix="test_prefix", data_id=233, input_suffix=".inp", output_suffix=".ans")
24+
self.assertTrue(os.path.exists("test_prefix233.inp"))
25+
self.assertTrue(os.path.exists("test_prefix233.ans"))
26+
27+
def test_write_stuff(self):
28+
with IO("test_write.in", "test_write.out") as test:
29+
test.input_write(1, 2, 3)
30+
test.input_writeln([4, 5, 6])
31+
test.input_writeln(7, [8, 9])
32+
test.output_write([9, 8], 7)
33+
test.output_writeln(6, 5, 4)
34+
test.output_writeln([3], 2, [1])
35+
36+
with open("test_write.in") as f:
37+
input = f.read()
38+
with open("test_write.out") as f:
39+
output = f.read()
40+
self.assertEqual(input.split(), ['1', '2', '3', '4', '5', '6', '7', '8', '9'])
41+
self.assertEqual(output.split(), ['9', '8', '7', '6', '5', '4', '3', '2', '1'])
42+
self.assertEqual(input.count("\n"), 2)
43+
self.assertEqual(output.count("\n"), 2)
44+
45+
def test_output_gen(self):
46+
with IO("test_gen.in", "test_gen.out") as test:
47+
test.output_gen("echo 233")
48+
49+
with open("test_gen.out") as f:
50+
output = f.read()
51+
self.assertEqual(output.strip("\n"), "233")

cyaron/tests/sequence_test.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import unittest
2-
32
from cyaron import Sequence
43

54

cyaron/tests/str_test.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import unittest
2+
from cyaron import String
3+
4+
5+
class TestString(unittest.TestCase):
6+
7+
def test_random_word(self):
8+
test = String.random(100, charset="abc")
9+
self.assertTrue(all(a in "abc" for a in test))
10+
11+
def test_random_word_from_dict(self):
12+
my_dict = ["lorem", "ipsum", "dolor", "sit", "amet"]
13+
test = String.random(None, charset=my_dict)
14+
self.assertTrue(test in my_dict)
15+
16+
def test_random_sentence(self):
17+
sentence = String.random_sentence(10, sentence_terminators=".")
18+
self.assertTrue(sentence[0].isupper())
19+
self.assertTrue(sentence[-1] == ".")
20+
self.assertTrue(sentence.count(" ") == 9)

requirements.txt

Whitespace-only changes.

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setup(
44
name='cyaron',
5-
version='0.1.5',
5+
version='0.1.6',
66
keywords='olympic informatics luogu aqours cyaron lovelive sunshine online judge',
77
description='CYaRon: Yet Another Random Olympic-iNformatics test data generator, A library for automatically generating test data for Online Judge, Olympic Informatics or automatic application testing',
88
license='LGPLv3',

0 commit comments

Comments
 (0)