Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some utility to IO class #137

Merged
merged 15 commits into from
Oct 12, 2024
Merged

Conversation

OranPie
Copy link
Contributor

@OranPie OranPie commented Oct 4, 2024

Add a flag make_dirs (to ensure that the folder has been created, if not it will create it)
Add a function clear_content (to truncate the content after position (default is 0))
Add a Enum Class File to clearly show what file would to change

Only modified the IO.py and init.py(import new statement)

添加一个选项make_dirs(以确保文件的上层文件夹已创建,否则将创建它)

添加函数IO.clear_content(截断位置后的内容(默认值为0))

添加枚举类File,以清楚地显示要更改的文件

仅修改了IO.py和__init__.py(导入新语句)

Add originzation.
Add a Enum class to speified use Output & Input.
Add a clear_content to clear Output & Input file.
including IO.File Enum class
Add some notes
Add a flag make_dirs to ensure the dir has created/
Fix some bug.
Copy link
Collaborator

@Mr-Python-in-China Mr-Python-in-China left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

自动创建路径一般default to false吧 免得有人写错了还找不到问题在哪

Copy link
Collaborator

@Mr-Python-in-China Mr-Python-in-China left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不过你这个make_dirs起到啥实际作用了吗

@OranPie
Copy link
Contributor Author

OranPie commented Oct 4, 2024

如果prefix 包含路径:
例如./io/heat
若文件夹./io不存在,自动创建

改了,makedir默认为False

比如:
test_data = IO(file_prefix="./io/data", data_id=i) # 生成 heat[1|2|3].in/out 三组测试数据

Set make_dirs default value to False
@Mr-Python-in-China
Copy link
Collaborator

image

参数传进去完全没有用啊

@OranPie
Copy link
Contributor Author

OranPie commented Oct 4, 2024

我本地测试是没有问题的,会自动创建目录

@Mr-Python-in-China
Copy link
Collaborator

你测试为false的时候会不会报错了吗

@OranPie
Copy link
Contributor Author

OranPie commented Oct 4, 2024

截屏2024-10-04 15 59 12 截屏2024-10-04 15 59 04 运行前 截屏2024-10-04 15 59 28 运行后

@OranPie
Copy link
Contributor Author

OranPie commented Oct 4, 2024

哦,忘了加条件判定了。。。
对不起

Add logic to check make_dirs flag
@OranPie
Copy link
Contributor Author

OranPie commented Oct 4, 2024

OK

Add forget flag make_dirs in __init_file
Copy link
Collaborator

@Mr-Python-in-China Mr-Python-in-China left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

提交前不再处理一下ide的错误和警告信息吗

怎么还能有缺失参数的

Copy link
Collaborator

@Mr-Python-in-China Mr-Python-in-China left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

算了我给你修了

@OranPie
Copy link
Contributor Author

OranPie commented Oct 4, 2024

刚刚看到了,现在没问题
报错正常:
Traceback (most recent call last):
File "/Users/yanyige/PycharmProjects/Ctest/main.py", line 13, in
test_data = IO(file_prefix="./io/data", data_id=i, make_dirs=False)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/yanyige/miniconda3/lib/python3.12/site-packages/cyaron/io.py", line 99, in init
self.__init_file(input_file, data_id, File.INPUT, make_dirs)
File "/Users/yanyige/miniconda3/lib/python3.12/site-packages/cyaron/io.py", line 138, in __init_file
open(filename, 'w+', newline='\n', encoding='utf-8'), data_id,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: './io/data1.in'
Exception ignored in: <function IO.del at 0x15b8a59e0>
Traceback (most recent call last):
File "/Users/yanyige/miniconda3/lib/python3.12/site-packages/cyaron/io.py", line 181, in del
self.close()
File "/Users/yanyige/miniconda3/lib/python3.12/site-packages/cyaron/io.py", line 158, in close
if self.__closed:
^^^^^^^^^^^^^
AttributeError: 'IO' object has no attribute '_IO__closed'

@OranPie
Copy link
Contributor Author

OranPie commented Oct 4, 2024

OK

Copy link
Collaborator

@Mr-Python-in-China Mr-Python-in-China left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

哦我终于看懂你想干什么了

你把input和output的相关函数都合并起来了

我感觉没啥特别的意义啊

而且这个是破坏性的更新

cyaron/io.py Outdated
file.truncate(pos)


def write(self, file: File = File.INPUT, *args, **kwargs):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

你这是啥逻辑

如果file=File.OUTPUT还要往input_file写吗

Copy link
Contributor Author

@OranPie OranPie Oct 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

哪里?引用位置不正确吧

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

write方法

@OranPie
Copy link
Contributor Author

OranPie commented Oct 4, 2024

现在改

Fix: file cannot specified
@OranPie
Copy link
Contributor Author

OranPie commented Oct 4, 2024

OK

@OranPie
Copy link
Contributor Author

OranPie commented Oct 4, 2024

测试正常

@OranPie
Copy link
Contributor Author

OranPie commented Oct 4, 2024

哦我终于看懂你想干什么了

你把input和output的相关函数都合并起来了

我感觉没啥特别的意义啊

而且这个是破坏性的更新

个人认为这样代码会更简洁,组织性强...

@Mr-Python-in-China
Copy link
Collaborator

怎么说呢 对原有 api 大刀阔斧修改的 pr 我原则上是不想合并的

总不能升级个包然后全炸了吧

@Mr-Python-in-China
Copy link
Collaborator

而且感觉你的逻辑有点乱

要不要先 draft 掉然后 rebase 回去重新写

@OranPie
Copy link
Contributor Author

OranPie commented Oct 4, 2024

ok
我把File 那个功能删掉好了

Restore the origin api.
@OranPie
Copy link
Contributor Author

OranPie commented Oct 4, 2024

OK
保持原有API

@OranPie
Copy link
Contributor Author

OranPie commented Oct 4, 2024

现在仅添加了clear_content&make_dirs

Copy link
Collaborator

@Mr-Python-in-China Mr-Python-in-China left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

class TestIO(unittest.TestCase):
    def test_output_clear_content(self):
        with IO("test_clear.in", "test_clear.out") as test:
            test.input_write("This is a test.")
            test.input_clear_content()
            test.input_write("Cleared content.")
            test.output_write("This is a test.")
            test.output_clear_content()
            test.output_write("Cleared content.")
        with open("test_clear.in", encoding="utf-8") as f:
            input_text = f.read()
        with open("test_clear.out", encoding="utf-8") as f:
            output_text = f.read()
        self.assertEqual(input_text, "Cleared content.")
        self.assertEqual(output_text, "Cleared content.")

是这么用的吗 为啥我写的test没过

AssertionError: '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 Cleared content.' != 'Cleared content.'

@OranPie
Copy link
Contributor Author

OranPie commented Oct 4, 2024

OK, truncate后面忘写seek了,导致前面的都是0x00(空)
检测通过

@OranPie
Copy link
Contributor Author

OranPie commented Oct 4, 2024

正在改,原因:is_first_char 没有正确设置。

@Mr-Python-in-China
Copy link
Collaborator

test结果好像还是不太对

Traceback (most recent call last):
  File "/home/mrpython/Desktop/cyaron/cyaron/tests/io_test.py", line 149, in test_output_clear_content
    self.assertEqual(input_text, "Cleared content.")
AssertionError: ' Cleared content.' != 'Cleared content.'
-  Cleared content.
? -
+ Cleared content.

为啥会多一个空格 你那边有这种行为吗

@OranPie
Copy link
Contributor Author

OranPie commented Oct 4, 2024

正在改,问一下,truncate后如果最后一个字符是空格,默认添加数据不加空格吗?
例如:
This aaa
截断后
This<空格>
添加新的数据默认加不加空格?

@OranPie
Copy link
Contributor Author

OranPie commented Oct 4, 2024

现在的实现逻辑,你看行不:
if pos == 0: # 重置self.is_first_char
self.is_first_char[file] = True # 无需添加空格
else:
file.seek(pos - 1)
if file.read(1) != " ": # 若不是空格 重置self.is_first_char
self.is_first_char[file] = False # 需要添加空格
else:
self.is_first_char[file] = True # 无需添加空格

Fix: truncated file will more a space
@OranPie
Copy link
Contributor Author

OranPie commented Oct 4, 2024

Fixed

@Mr-Python-in-China
Copy link
Collaborator

要不重新截断之后直接把空格干掉吧
first_char直接为True

@OranPie
Copy link
Contributor Author

OranPie commented Oct 4, 2024

Ok,如果有末尾有空格直接删除?

@Mr-Python-in-China
Copy link
Collaborator

我意思是 别考虑空格问题了 截断之后直接当成新一行处理

@OranPie
Copy link
Contributor Author

OranPie commented Oct 4, 2024

OK, Fixing

Always set is_first_char[file] to True, igrone space problem
(Review from PR)
@OranPie
Copy link
Contributor Author

OranPie commented Oct 4, 2024

Fixed

@Mr-Python-in-China
Copy link
Collaborator

咱要不提交的时候在本地测试一下?

@OranPie
Copy link
Contributor Author

OranPie commented Oct 4, 2024

我没链接到本地编译器,都是在Github上改的...
Sorry.

@Mr-Python-in-China
Copy link
Collaborator

我没链接到本地编译器,都是在Github上改的... Sorry.

那你用codespace也行啊。。。
你这交一发错一点哪行

@OranPie
Copy link
Contributor Author

OranPie commented Oct 5, 2024

下次一定

@Mr-Python-in-China Mr-Python-in-China marked this pull request as draft October 5, 2024 16:44
@Mr-Python-in-China
Copy link
Collaborator

先draft掉了 等你写好了repoen就可以

@OranPie
Copy link
Contributor Author

OranPie commented Oct 6, 2024

行,我先测试下
测试好了我再reopen

@OranPie
Copy link
Contributor Author

OranPie commented Oct 6, 2024

测试完成(IOTest successful 11/11)

@OranPie OranPie marked this pull request as ready for review October 6, 2024 01:12
@Mr-Python-in-China Mr-Python-in-China merged commit 036d097 into luogu-dev:master Oct 12, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants