首页 > 其他分享 >一套小工具

一套小工具

时间:2024-09-12 21:51:59浏览次数:8  
标签:License GNU program 一套 file print path 工具

本文作者 Yile Wang,在 GNU Free Documentation License version 1.3 下发布。不提供任何担保。

本文给出的部分代码在 GNU General Public License version 3, or any later version 下发布,详见对应代码的文件头声明。

COPYING

Copyright (c)  2024  Yile Wang
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3
or any later version published by the Free Software Foundation;
with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
A copy of the license is included in the section entitled "GNU
Free Documentation License".

BRIEFING

这是一个小工具集合,作者希望它有用,但不提供任何担保。

MENU

random_prime

在 \([l, r]\) 之间随机选择一个数,找出第一个不小于它的质数并输出。

import math

def chkprime(x: int) -> bool:
    p = int(math.sqrt(x))
    if x == 1:
        return False
    if x == 2:
        return True
    for i in range(2, p + 1):
        if x % i == 0:
            return False
    return True

import random

l = int(1e9)
r = int(2e9)

x = random.randint(l, r)
while not chkprime(x):
    x += 1
print(x)

checker

简单的文件比对,推荐更强大的比对工具 GNU Diffutils

out = "/path/to/out"
ans = "/path/to/ans"

with open(out) as file:
    out = file.readlines()
with open(ans) as file:
    ans = file.readlines()

for i in range(len(ans)):
    if len(out) <= i:
        print("miss")
        break
    if out[i] != ans[i]:
        print("err:", i + 1)

nospace

去除 C++ 代码文件中多余的空格,你可以通过调整代码开头的选项改变其行为。对代码的修改也应使用 GNU 通用公共许可证(第三版以上)发布。

"""
nospace.py
Copyright (C) 2024  Yile Wang

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.

======================================================================

You can contact the author by email. (mailto:[email protected])

This program expects to do as follow: remove unnecessary space from
C++ source file.
"""

FIRST_OF_ALL = """
nospace.py  Copyright (C) 2024  Yile Wang
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions.
Learn more in the copyright statement at the beginning
of the source code.
"""

print(FIRST_OF_ALL)

# 忽略注释 NotImplemented
ignore_comment = False
# 忽略行尾空格
ignore_ending_space = False
# 忽略预编译指令
ignore_precopile_command = True

path = "/path/to/cpp"

with open(path, "r", encoding="utf-8") as file:
    text = file.read().splitlines()

def chk(x: str, y: str) -> bool:
    flag1 = flag2 = False
    if x.islower() or x.isupper() or x.isdigit():
        flag1 = True
    if y.islower() or y.isupper() or y.isdigit():
        flag2 = True
    return (flag1 and flag2) or x == " " or y == " "

ret = []
for i in range(len(text)):
    if ignore_ending_space:
        para = text[i]
    else:
        para = text[i].rstrip()
    if ignore_precopile_command and len(para) != 0 and para[0] == "#":
        ret.append(para)
        continue
    ret.append("")
    for j in range(len(para)):
        ch = para[j]
        if ch == " ":
            if j > 0 and j < len(para) - 1 and not chk(para[j - 1], para[j + 1]):
                continue
        ret[-1] += ch

with open(path + ".ret.cpp", "w", encoding="utf-8") as file:
    for para in ret:
        file.write(para)
        file.write("\n")

sweeper

寻找当前工作目录下可能无用的文件,并询问是否删除。

你可以通过修改源代码自定义程序的行为,部分参数在代码中有注释说明。请注意,你修改后的程序仍应以 GNU 通用公共许可证(第三版以上)发布。

"""
sweeper.py
Copyright (C) 2024  Yile Wang

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.

======================================================================

You can contact the author by email. (mailto:[email protected])

This program expects to do as follow: walk current directory then find
maybe-useless files.
"""

FIRST_OF_ALL = """
sweeper.py  Copyright (C) 2024  Yile Wang
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions.
Learn more in the copyright statement at the beginning
of the source code.
"""

print(FIRST_OF_ALL)

import os

def isexec(path: str):
    """
    如果 path 指向 ELF 文件,返回 True;否则返回 False
    """
    try:
        file = open(path, "rb")
        if file.read(4)[1:] == b"ELF":
            return True
        return False
    except OSError:
        return False

ignore = [] # 不扫描的子文件夹,相对路径和绝对路径均可
def isignore(path: str) -> bool:
    """
    如果 path 指向被忽略的文件夹,返回 True;否则返回 False
    """
    path = os.path.normpath(path)
    for dir in ignore:
        if dir.startswith("."):
            dir = os.path.join(os.getcwd(), dir)
        dir = os.path.normpath(dir)
        if path == dir:
            print(f"\033[34mignore:\033[0m {path}")
            return True
        elif path.startswith(dir):
            return True

exts = [] # 被认为“无用”的文件拓展名
def iswaste(path: str) -> bool:
    """
    如果 path 指向无用文件,返回 True;否则返回 False
    """
    for ext in exts:
        if path.endswith(ext):
            return True
    if isexec(path):
        return True
    return False

def sweep(path: str):
    """
    清扫 path 指向的文件夹
    """
    aim = []
    for dirpath, dirnames, filenames in os.walk(path):
        if isignore(dirpath):
            continue
        print(f"\033[34msweeping:\033[0m {dirpath}:")
        for file in filenames:
            print(f"\tchecking: {file}...", end="")
            path = os.path.join(dirpath, file)
            flag = iswaste(path)
            if flag == True:
                print("\033[33mwaste.\033[0m")
                aim.append(path)
            elif flag is None:
                print("\033[31mpermission denied.\033[0m")
            else:
                print("\033[32mok.\033[0m")
    if len(aim) == 0:
        print("won't remove any file.")
        return
    elif len(aim) == 1:
        print("would remove 1 file:")
    else:
        print(f"would remove {len(aim)} files:")
    for file in aim:
        print(f"\t{file}")
    confirm = input("confirm ? [Y/n] ").strip()
    if confirm == "Y" or confirm == "y":
        failed = []
        for file in aim:
            try:
                os.remove(file)
            except Exception as msg:
                failed.append((file, msg))
        if len(failed) > 0:
            print("\033[31mfailed\033[0m while removing files below:")
            for file, err in failed:
                print(f"\t{file}: {err}")
        else:
            print("\033[32msuccess.\033[0m")

if __name__ == "__main__":
    try:
        os.chdir(os.path.dirname(__file__))
    except OSError:
        pass
    sweep(os.getcwd())

find_problem

尝试寻找可能为算法竞赛题目编号的字段并输出。

"""
find_problem.py
Copyright (C) 2024  Yile Wang

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.

======================================================================

You can contact the author by email. (mailto:[email protected])

This program expects to do as follow: read a Markdown file,
find and output possible problem from competitive programming.
"""

FIRST_OF_ALL = """
find_problem.py  Copyright (C) 2024  Yile Wang
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions.
Learn more in the copyright statement at the beginning
of the source code.
"""

print(FIRST_OF_ALL)

path = input("Type or paste the path to file here: ")

import re

patterns = [
    r"\bLuogu [BP][0-9]+\b",
    r"\bCodeForces [1-9][0-9]*[A-Z][1-9]?\b",
    r"\b((AGC)|(ABC)|(ARC))[0-9]+[A-Z]\b",
    r"\bSPOJ [A-Z]+[2-9]?\b",
    r"\bUVA[1-9][0-9]*\b",
    r"\bUOJ[1-9][0-9]*\b",
    r"\bLOJ[1-9][0-9]*\b",
]

try:
    ret = []
    with open(path, "r", encoding="utf-8") as file:
        ln = 0
        for line in file.readlines():
            ln += 1
            if line.strip().startswith("-") or line.strip().startswith(">"):
                continue
            for pattern in patterns:
                cur = re.findall(pattern, line)
                ret += cur
                for id in cur:
                    print(f"ln {ln}: {id}")
except Exception as err:
    print(f"error: {err}")
else:
    print()
    print(f"In total: {len(ret)}")
    print("Problem ID are as follow:")
    for id in ret:
        print(id, end=", ")
    print()

标签:License,GNU,program,一套,file,print,path,工具
From: https://www.cnblogs.com/bluewindde/p/18411176

相关文章

  • 金百达F6Pro带缓存sm2256K开卡成功 附工具 AD, 3A, 18, A3, 61, 25 ,H27UDG8M2M
     开卡工具 SM2256AB_MPO0811A_FWO0803A_SLCFirst.zip      SM2256AB_MPO0811A_FWO0803A_SLCFirst.zip ......
  • kingdian S400固态SM2258xt开卡成功 附工具软件 AD, 5E, 28, 22, 10, 90 ,H25QFT8A1A
    开卡工具SM2258XT_HY3D-V4_PKGS0402A_FWS0330B0.zip 拆开以后用镊子短接ROM标识那俩个孔   SM2258XT_HY3D-V4_PKGS0402A_FWS0330B0.zip......
  • VisualStudio 2022 找不到内存 反汇编 寄存器调试工具
    本文将告诉大家如何解决在VisualStudio2022的调试-窗口里面找不到内存、反汇编、寄存器这三个调试工具的问题找不到的原因是没有启用地址级调试只需要在“工具”(或“调试”)>“选项”>“调试”中选择“启用地址级调试”然后进行调试即可看到开启之后,即可在调试-窗口......
  • C#中设置自定义控件工具箱图标
    在设计自定义控件时,系统默认生成的图标比较单一且难看,如何为控件设计自己的图标呢,这里给出了一种基于ToolBoxBitmap 属性设置自定义控件工具箱图标的方法。1、首先将图标文件名改为自定义控件名,如自定义控件类为: public partial class UserDefindControl: UserControl {......
  • 我的搬砖工具由 VS Code 变成 Cursor 了
    作者:老余捞鱼原创不易,转载请标明出处及原作者。写在前面的话:     本文介绍了我从VSCode转向Cursor的原因,强调了Cursor的人工智能交互流畅性以及其他一些优于VSCode的特性。    VSCode是免费的,而且运行起来非常出色。我一直很喜欢VS......
  • 抖音商家电话采集工具使用教程
    抖音商家电话采集工具通常具有以下功能:关键词搜索采集4:用户可以输入特定的关键词,如行业、产品、品牌等,工具会根据这些关键词在抖音平台上搜索相关的商家,并采集其电话等信息。例如,输入“服装”,就能采集到抖音上从事服装销售或相关业务的商家电话。地域筛选:可以根据地区范......
  • 第21篇 TortoiseGit(大乌龟)版本管理工具的使用
    一、下载安装1.下载git必须先下载并安装git,再TortoiseGit下载安装git安装参考教程:https://blog.csdn.net/mukes/article/details/1156938332.TortoiseGit下载与安装TortoiseGit,Git客户端,32/64位最新版及对应的语言包下载地址:https://tortoisegit.org/download/进入下载页面,......
  • 课程题目生成工具V1.0
    服务器本工具使用阿里云八代实例(g8i)+xFasterTransformer+Qwen-7B-Chat模型搭建而成通义千问-7B(Qwen-7B-Chat)本工具基于通义千问-7B进行开发,通义千问-7B(Qwen-7B)是阿里云研发的通义千问大模型系列的70亿参数规模模型。Qwen-7B是基于Transformer的大语言模型,在超大规模的预训练数据上......
  • 【转】常用的判空工具类
    常用判空的工具对象的判空推荐统一使用java.util包的Objects.nonNull()等方法。集合的判空推荐统一使用org.apache.commons.collections.CollectionUtils包的.isNotEmpty()等方法。Map对象判空推荐统一使用Map自带的.isEmpty()、.containsKey()、.equals()这......
  • 一个用于管理多个 Node.js 版本的安装和切换开源工具
    大家好,今天给大家分享一个用于管理多个Node.js版本的工具 NVM(NodeVersionManager),它允许开发者在同一台机器上安装和使用不同版本的Node.js,解决了版本兼容性问题,为开发者提供了极大的便利。在开发环境中,特别是在处理多个项目时,每个项目可能依赖于不同版本的Node.js,NVM提供......