标签 Python 下的文章

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import argparse

parser = argparse.ArgumentParser(description="程序描述")

# 互斥组(组内的各参数不能同时使用)
group = parser.add_mutually_exclusive_group()
group.add_argument("-a", "--aaa", help="a参数描述", action="store_true")
group.add_argument("-b", "--bbb", help="b参数描述", type=int)
group.add_argument("-c", "--ccc", help="c参数描述")
args = parser.parse_args()

if args.aaa:
    pass
elif args.bbb:
    pass
elif arg.ccc:
    pass
else:
    parser.print_help()

The Python Standard Library » Generic Operating System Services » argparse — Parser for command-line options, arguments and sub-commands

安装

sudo dnf install gcc python3-devel`
pip install psutil

使用

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import psutil

# 遍历所有进程,输出进程ID,CPU占比,内存,进程名称
for pid in psutil.pids():
    p = psutil.Process(pid)
    print("{},{},{},{}".format(
        p.pid, p.cpu_percent(), p.memory_info().rss, p.name()));

文档

https://psutil.readthedocs.io/en/latest

PyGithub是GitHub API的Python版,玩了一下,还是蛮有趣的。最初是在chromium-source-tarball项目中一个Python脚本里发现的,竟然能通过Python操作GitHub,再一次证明Python太强大了,TM简直啥都能做,哈哈。

写个小工具,GitHub里Follow我的,我也Follow回敬一下,礼尚往来。

安装PyGithub:

pip install pygithub

完整脚本

#!/usr/bin/env python
# coding: utf-8

# GitHub
# Follow我的, Follow之
# 未Follow我的,从Following中删除之
# 即同步互Follow关系

import getpass
from github import Github

# 始终Follow的大神
ALWAYS_FOLLOW = [
    'torvalds',
    'michaelliao',
    'zcbenz',
    'ruanyf',
]

def sync():
    username = raw_input('Username:')
    password = getpass.getpass()
    print 'Please wait...'

    g = Github(username, password)
    me = g.get_user()

    followers = me.get_followers()
    following = me.get_following()
    followers_names = [x.login for x in followers]
    following_names = [x.login for x in following]

    all_is_ok = True  # 意思是已是同步状态,无需操作
    for user in following_names:
        is_my_follower = user in followers_names
        in_always_list = user in ALWAYS_FOLLOW

        if (not is_my_follower) and (not in_always_list):
            me.remove_from_following(g.get_user(user))
            print 'User \'%s\' has been removed from following list!' % user
            all_is_ok = False

    for user in followers_names:
        if user not in following_names:
            me.add_to_following(g.get_user(user))
            print 'New user \'%s\' has been followed!' % user
            all_is_ok = False

    if all_is_ok:
        print 'All is OK!'

if __name__ == "__main__":
    sync()

还可以再完善一下,比如还没有加上异常处理。