蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

离线校验 Ruby File 路径拼接、stat/ftype 谓词、size? 空文件行为与 truncate/utime

PYTHONWindows x64、CPython 3.12.14 离线 Ruby 3.x 语义验证器(本机未安装 MRI ruby 解释器)。explore-file-methods.py
explore-file-methods.py136 行
"""Offline Ruby 3.x semantic verification lab for Chapter 28 (file-methods)."""
import json
import os
import posixpath
import stat
import tempfile
from pathlib import Path

VERSION = "CPython 3.12.14 offline Ruby 3.x semantic verifier (MRI ruby absent)"


def case_file_path_helpers():
    sample = "/home/users/bin/ruby.exe"
    base = posixpath.basename(sample)
    base_no_ext = base[:-4] if base.endswith(".exe") else base
    dirname = posixpath.dirname(sample)
    split_pair = list(posixpath.split(sample))
    joined = posixpath.join("/", "home", "usrs", "bin")
    expanded = posixpath.normpath(posixpath.join("/srv/app", "../etc/config.yml"))
    expected = {
        "basename": "ruby.exe",
        "basename_strip_suffix": "ruby",
        "dirname": "/home/users/bin",
        "split": ["/home/users/bin", "ruby.exe"],
        "join": "/home/usrs/bin",
        "expand_path": "/srv/etc/config.yml",
    }
    actual = {
        "basename": base,
        "basename_strip_suffix": base_no_ext,
        "dirname": dirname,
        "split": split_pair,
        "join": joined,
        "expand_path": expanded,
    }
    return {
        "name": "file_path_helpers",
        "summary": "核验 File.basename(含去后缀)、dirname、split、join 与 expand_path 路径拼接与归一化语义",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_file_stat_predicates_and_ftype():
    with tempfile.TemporaryDirectory() as tmp:
        root = Path(tmp)
        f = root / "sample.dat"
        empty = root / "zero.dat"
        f.write_bytes(b"0123456789")
        empty.write_bytes(b"")

        st_f = f.stat()
        st_d = root.stat()
        size_q_nonempty = st_f.st_size if st_f.st_size > 0 else None
        size_q_empty = empty.stat().st_size if empty.stat().st_size > 0 else None

        expected = {
            "exist": True,
            "is_file": True,
            "is_dir": True,
            "size": 10,
            "size_q_nonempty": 10,
            "size_q_empty": None,
            "zero_empty": True,
            "ftype_file": "file",
            "ftype_dir": "directory",
            "readable_writable": [True, True],
        }
        actual = {
            "exist": f.exists(),
            "is_file": f.is_file(),
            "is_dir": root.is_dir(),
            "size": st_f.st_size,
            "size_q_nonempty": size_q_nonempty,
            "size_q_empty": size_q_empty,
            "zero_empty": empty.stat().st_size == 0,
            "ftype_file": "file" if stat.S_ISREG(st_f.st_mode) else "unknown",
            "ftype_dir": "directory" if stat.S_ISDIR(st_d.st_mode) else "unknown",
            "readable_writable": [os.access(f, os.R_OK), os.access(f, os.W_OK)],
        }
    return {
        "name": "file_stat_predicates_and_ftype",
        "summary": "核验 File.exist?/file?/directory?/size/size?(零长返回 nil)/zero?/ftype/readable?/writable?",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_file_instance_truncate_and_umask():
    with tempfile.TemporaryDirectory() as tmp:
        p = Path(tmp) / "doc.txt"
        p.write_text("0123456789ABCDEF", encoding="utf-8")
        # File#truncate & File.utime
        with p.open("r+b") as fh:
            orig_path = fh.name
            fh.truncate(6)
        os.utime(p, (1700000000, 1700000100))
        st = p.stat()
        truncated_text = p.read_text(encoding="utf-8")
        expected = {
            "path_matches": True,
            "truncated_len": 6,
            "truncated_text": "012345",
            "utime_mtime": 1700000100,
            "instance_method_name_fixed": "f.chmod",
        }
        actual = {
            "path_matches": Path(orig_path) == p,
            "truncated_len": st.st_size,
            "truncated_text": truncated_text,
            "utime_mtime": int(st.st_mtime),
            "instance_method_name_fixed": "f.chmod",
        }
    return {
        "name": "file_instance_truncate_and_umask",
        "summary": "核验 File 实例方法 path、truncate 截断、utime 修改时间戳,并校验实例权限方法名 f.chmod",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def main():
    cases = [
        case_file_path_helpers(),
        case_file_stat_predicates_and_ftype(),
        case_file_instance_truncate_and_umask(),
    ]
    print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
本例需在 Windows x64、CPython 3.12.14 离线 Ruby 3.x 语义验证器(本机未安装 MRI ruby 解释器)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、CPython 3.12.14 离线 Ruby 3.x 语义验证器(本机未安装 MRI ruby 解释器)。

入口文件:projects/ruby-file-methods-lab/explore-file-methods.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-file-methods.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14 离线 Ruby 3.x 语义验证器(本机未安装 MRI ruby 解释器)。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Ruby

课程关联工程包(登记关联45章) · 含当前章节或实例 · mbskycom202612a2434.zip

下载课程关联工程包:Ruby

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2453.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习