51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 4377|回复: 0
打印 上一主题 下一主题

[转贴] Python 3.6 中针对文件系统的神奇方法

[复制链接]
  • TA的每日心情
    擦汗
    昨天 08:46
  • 签到天数: 981 天

    连续签到: 1 天

    [LV.10]测试总司令

    跳转到指定楼层
    1#
    发表于 2021-6-29 09:37:54 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
      Python 3.6 首次发布于 2016 年,尽管它已经发布了一段时间,但它引入的许多特性都没有得到充分利用,而且相当酷。下面是其中的三个。
      分隔数字常数
      快回答哪个更大,10000000 还是 200000?你在看代码时能正确回答吗?根据当地的习惯,在写作中,你会用 10,000,000 或 10.000.000 来表示第一个数字。问题是,Python 使用逗号和句号是用于其他地方。
      幸运的是,从 Python 3.6 开始,你可以使用下划线来分隔数字。这在代码中和使用字符串的 int() 转换器时都可以使用:
    1.   import math
    2.   math.log(10_000_000) / math.log(10)
    3.   7.0
    4.   math.log(int("10_000_000")) / math.log(10)
    5.   7.0
    复制代码
    Tau 是对的
      45 度角用弧度表示是多少?一个正确的答案是 π/4,但这有点难记。记住 45 度角是一个八分之一的转角要容易得多。正如 Tau Manifesto 所解释的,2π,称为 Τ,是一个更自然的常数。
      在 Python 3.6 及以后的版本中,你的数学代码可以使用更直观的常数:
    1.  print("Tan of an eighth turn should be 1, got", round(math.tan(math.tau/8), 2))
    2.   print("Cos of an sixth turn should be 1/2, got", round(math.cos(math.tau/6), 2))
    3.   print("Sin of a quarter turn should be 1, go", round(math.sin(math.tau/4), 2))
    4.   Tan of an eighth turn should be 1, got 1.0
    5.   Cos of an sixth turn should be 1/2, got 0.5
    6.   Sin of a quarter turn should be 1, go 1.0
    复制代码
     os.fspath
      从 Python 3.6 开始,有一个神奇的方法表示“转换为文件系统路径”。当给定一个 str 或 bytes 时,它返回输入。
      对于所有类型的对象,它寻找 __fspath__ 方法并调用它。这允许传递的对象是“带有元数据的文件名”。
      像 open() 或 stat 这样的普通函数仍然能够使用它们,只要 __fspath__ 返回正确的东西。
      例如,这里有一个函数将一些数据写入一个文件,然后检查其大小。它还将文件名记录到标准输出,以便追踪:
    1. def write_and_test(filename):
    2.       print("writing into", filename)
    3.       with open(filename, "w") as fpout:
    4.           fpout.write("hello")
    5.       print("size of", filename, "is", os.path.getsize(filename))
    复制代码
    你可以用你期望的方式来调用它,用一个字符串作为文件名:
    1.  write_and_test("plain.txt")
    2.   writing into plain.txt
    3.   size of plain.txt is 5
    复制代码
    然而,可以定义一个新的类,为文件名的字符串表示法添加信息。这样可以使日志记录更加详细,而不改变原来的功能:
    1. class DocumentedFileName:
    2.       def __init__(self, fname, why):
    3.           self.fname = fname
    4.           self.why = why
    5.       def __fspath__(self):
    6.           return self.fname
    7.       def __repr__(self):
    8.           return f"DocumentedFileName(fname={self.fname!r}, why={self.why!r})"
    复制代码
    用 DocumentedFileName 实例作为输入运行该函数,允许 open 和 os.getsize 函数继续工作,同时增强日志:
    1. write_and_test(DocumentedFileName("documented.txt", "because it's fun"))
    2.   writing into DocumentedFileName(fname='documented.txt', why="because it's fun")
    3.   size of DocumentedFileName(fname='documented.txt', why="because it's fun") is 5
    复制代码








    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /2 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-7-9 07:31 , Processed in 0.067474 second(s), 23 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表