首页 > 编程语言 >Python: convert int to mode string

Python: convert int to mode string

时间:2022-11-10 02:11:23浏览次数:41  
标签:case convert Python res list int mode

 

def _convert_mode(mode: int):
    if not 0 <= mode <= 0o777:
        raise RuntimeError
    res = ''

    for v in range(0, 9):
        if mode >> v & 1:
            match v % 3:
                case 0:
                    res = 'x' + res
                case 1:
                    res = 'w' + res
                case 2:
                    res = 'r' + res
        else:
            res = '-' + res
    return res


print(_convert_mode(0o757))

mode_list = dict(zip(range(9), list('rwx') * 3))


def _convert_mode(mode: int):
    if not 0 <= mode <= 0o777:
        raise ValueError

    res = ''
    # 8 -> 0, 7 -> 1  =>  v + x = 8
    for v in range(8, -1, -1):
        if mode >> v & 1:
            res += mode_list[8 - v]
        else:
            res += '-'
    return res


print(_convert_mode(0o577))

 

标签:case,convert,Python,res,list,int,mode
From: https://www.cnblogs.com/dissipate/p/16875763.html

相关文章

  • 基于Python的批量处理execl文件内容
    今天遇到一个棘手的问题,在三个文件夹中将近60个execl表选出所需的特定三列数据,且表名,sheet名,表中的数据类型均不一致,故想到利用Python批量化处理技术手段进行处理。其原理......
  • python发送邮件
    python发送邮件封装#encoding=utf-8importsmtplibfromemail.mime.multipartimportMIMEMultipartfromemail.mime.textimportMIMETextclassEmailMange:......
  • Python10-实战
    实战01(根据当前时间创建文件)importtimedefcreate():globalnamelocalTime=time.strftime("%Y%m%d%H%M%S",time.localtime())name=localTime+'.txt'......
  • Python10-eg
    实例01(创建并打开记录蚂蚁庄园的文件)1print("\n","="*10,"蚂蚁庄园动态","="*10)2file=open('message.tex','w')3print("\n即将显示...........\n")实例02(向......
  • Python 变量类型
    变量类型1.变量赋值Python中变量赋值不需要类型声明。每个变量在使用前必须声明,变量赋值后该变量才会被创建。couter=100#赋值整型变量miles=1000.0#浮点型......
  • python2 递归函数
    importosimportos.pathasospimportsysimportnumpyasnpimportdatetimedefmkdirs_py2(path):#递归创建文件夹路径ifosp.exists(path):return......
  • python选课系统项目详解
    选课系统项目详解选课系统简介及分析选课系统架构设计分析选课系统目录设计管理员视图注册登录创建学校创建课程创建讲师学生视图教师视图选课系统简介及......
  • 【pyfaidx】纯Python实现的FASTA随机索引库
    前言基因组序列的提取,有不少强大的工具像samtools,bedtools,之前也提到pybedtools提取序列。不过pybedtools是对bedtools提供一个Python接口,除了安装pybedtools外,还需......
  • 新的学习历程-python1 Hello World
    1print('helloworld!')2if2>0:3print('ok')4print('yes')56x=3;y=47print(x+y)学习资源来自:张志刚老师python百例 《例解Python:Pyth......
  • python中字符串的使用和数据转换
    #1.输入输出#sep='',步长数据之间以某个东西分割在这里是空格#end='\n'在打印后会额外的加一个数据换行print('1.输入输出')print('同学们晚上好',1,77......