×

Loading...

在看俄罗斯同事写的一段python 小程序


:


文字版在下面:

这个function 的定义:

def fun_run_OS_command(in_command, in_fl_to_run = True, in_fl_print_output = True, in_fl_remove_output_spaces = True, fl_print_command = True):
if in_fl_to_run:
if fl_print_command:
print_message("Running", in_command)
(loc_sub_output2, loc_sub_err) = subprocess.Popen(in_command, stdout=subprocess.PIPE, shell=True).communicate()
loc_sub_output = str(loc_sub_output2, 'utf-8')
if in_fl_remove_output_spaces:
loc_str_return = '\n'.join([tmp_line.strip() for tmp_line in loc_sub_output.strip().split("\n")])
if in_fl_print_output:
print(loc_str_return)
else:
if in_fl_print_output:
print(loc_sub_output)
print_sep('-')
return loc_str_return
else:
print_message("Debug mode, skip running", in_command)
return ''

调用这个function:

lc_comm = f"/usr/bin/ls -lrt {os.path.join(lc_log_dir, lc_log_mask)} | /usr/bin/tail -1"
lc_last_log_file_info = ss.fun_run_OS_command(lc_comm, in_fl_to_run = True, in_fl_print_output = False, in_fl_remove_output_spaces = True, fl_print_command = True)

这个f" 中的f 是干啥用的?


Sign in and Reply Report

Replies, comments and Discussions:

  • 工作学习 / 学科技术 / 在看俄罗斯同事写的一段python 小程序 +1


    :


    文字版在下面:

    这个function 的定义:

    def fun_run_OS_command(in_command, in_fl_to_run = True, in_fl_print_output = True, in_fl_remove_output_spaces = True, fl_print_command = True):
    if in_fl_to_run:
    if fl_print_command:
    print_message("Running", in_command)
    (loc_sub_output2, loc_sub_err) = subprocess.Popen(in_command, stdout=subprocess.PIPE, shell=True).communicate()
    loc_sub_output = str(loc_sub_output2, 'utf-8')
    if in_fl_remove_output_spaces:
    loc_str_return = '\n'.join([tmp_line.strip() for tmp_line in loc_sub_output.strip().split("\n")])
    if in_fl_print_output:
    print(loc_str_return)
    else:
    if in_fl_print_output:
    print(loc_sub_output)
    print_sep('-')
    return loc_str_return
    else:
    print_message("Debug mode, skip running", in_command)
    return ''

    调用这个function:

    lc_comm = f"/usr/bin/ls -lrt {os.path.join(lc_log_dir, lc_log_mask)} | /usr/bin/tail -1"
    lc_last_log_file_info = ss.fun_run_OS_command(lc_comm, in_fl_to_run = True, in_fl_print_output = False, in_fl_remove_output_spaces = True, fl_print_command = True)

    这个f" 中的f 是干啥用的?


    • f-string, 格式化字符串,还可以转义,具体放狗,这是PYTHON的基础知识 +3
      • 这也知道?服了! +2
        • 我当年写程序无师自通,从第一次装WIN95到学交大博士玩转SGI图形工作站(UNIX),到考NT的MCSE,到做C++项目(呼叫中心PBX板编程)到做游戏图形(DIRECTX OPENGL) 一共不到一年;我学东西很快 +2
          • 你回复更快,7/24在线,机器人不过如此
            • 羡慕不?你做不上BOSS,可以考虑做个BOTS +1
          • 长发哥确定是个IT专业人士了。 +3
            • 他本来就是。全能 +2
              • 长发哥真心不用再强调你无师自通了,因为大伙都知道你所有的事情都是无师自通的。
            • 我不挨踢, +2
              谁挨踢;要么我玩电脑的,要么我开卡车的,就像在左派看来,一个窗户都没打破的卡车司机是暴徒,我最早写程序时,那帮骂我的还在网吧玩游戏机器死机了只懂扯嗓子大叫“网管网管”,既不承认我是大老粗不识字的卡车司机,也不承认我是同行抢饭碗会说英文会写代码的IT人士,纠结不?
            • 这就能确定IT专业人士了?不懂任何编程的人请教ChatGPT ,几秒钟就有答案。ChatGPT 的回答:The "f" in f" is used to indicate that the string is an f-string, which stands for formatted string. +1
              • 能提出正确的问题就等于解决了一半的问题。 +1
          • 提SGI不用解释是UNIX的,画蛇添足了 +1
            • 当年我20岁,交大CS博士告诉我的
    • 这段code就是典型的Linus Torvalds 说的缺乏Good Taste. 那么多的if, 完全碎嘴子,毫无美感可言。同样的逻辑可以用Pattern Match写成awe-inspiring。很多语言包括Python都支持。 +5
      还有一个明显的错误,loc_sub_return 返回的时候可能没有值。程序会报错。
      • 有返回值,是python 语法。 +1
        • 是逻辑不是语法。loc_sub_return 从一堆if 里取值, 如果if都不满足,就拿不到值。return的时候会报错unboundlocalerror. +5
      • 同感 - invert if statement + early return,再把另外几个 if 顺手 consolidate 一下,应该能减少 50% 缩进,50% code... +4
        • 写法简洁运行会快吗?
          • 看具体情况吧。比如说 early return 不光能让 code 看起来易读,还通常能少运行很多步骤。主要是习惯问题,处处追求简洁优雅,才能做大。 +1
      • 耶鲁妈妈 威武!争取写出来的程序别人都看不懂。
        • 不才。不过看垃圾代码如同看钝刀杀人,难受啊。如果我这业余写码的人都看得出来,我绝对认同这些垃圾代码的缔造者转行去做labor, 每人领一张劳模好人贴 - 他们自己不嫌难受。 +4
          • 事实的情况是:在工作中90%代码不如这个。这个代码还算好了,容易看得懂,整断代码像乐谱一样整洁。虽然有很多的if else, 但不影响阅读。 +2
      • 每个条件都有return 值,不要看楼主的text, 看截屏。这是典型的Python 命名风格啊。 +1
        • 这是典型的C +1
          披了Python的外衣。就命名来说,Ruby and Rust 也是这个风格。同意耶鲁妈妈,这不是一段高质量的代码 - 1 to 10低到高大约在3吧。
    • f string是python 大约3.7左右加入的吧。知道这个的应该是最近一直做IT的。 +1