Conditions | 3 |
Total Lines | 52 |
Code Lines | 39 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | #!/usr/bin/python |
||
34 | @logging_decorator("system") |
||
35 | def system(bot, update): |
||
36 | update.message.chat.send_action(ChatAction.TYPING) |
||
37 | |||
38 | # System information |
||
39 | uname = platform.uname() |
||
40 | a = "="*10, " 💻 System Info ", "="*9 |
||
41 | sys_header = "".join(a) |
||
42 | sys_info = "{}\n\nSystem: {}\nNode Name: {}\nVersion: {}\n".format(sys_header, uname.system, uname.node, uname.version) |
||
43 | |||
44 | # Uptime |
||
45 | b = "="*14, " Uptime ", "="*13 |
||
46 | upt_header = "".join(b) |
||
47 | f = int(uptime()) |
||
48 | pcuptime = seconds_to_str(f) |
||
49 | up = "{}\n\nUptime: {}\n".format(upt_header, pcuptime) |
||
50 | |||
51 | # CPU information |
||
52 | c = "="*15, " CPU ", "="*15 |
||
53 | cpu_header = "".join(c) |
||
54 | cpumodel = cpuinfo.get_cpu_info()['brand'] |
||
55 | cpufreq = psutil.cpu_freq() |
||
56 | cpu_info = "{}\n\n{}\nPhysical cores: {}\nTotal cores: {}\nMax Frequency: {:.2f} Mhz\nCurrent Frequency: {:.2f} Mhz\nCPU Usage: {}%\n".format( |
||
57 | cpu_header, cpumodel, psutil.cpu_count(logical=False), psutil.cpu_count(logical=True), cpufreq.max, cpufreq.current, psutil.cpu_percent(percpu=False, interval=1)) |
||
58 | |||
59 | # RAM Information |
||
60 | d = "="*15, " RAM ", "="*15 |
||
61 | ram_header = "".join(d) |
||
62 | svmem = psutil.virtual_memory() |
||
63 | |||
64 | ram_info = "{}\n\nTotal: {}\nAvailable: {} ({:.2f}%)\nUsed: {} ({:.2f}%)\n".format( |
||
65 | ram_header, get_size(svmem.total), get_size(svmem.available), 100-svmem.percent, get_size(svmem.used), svmem.percent) |
||
66 | |||
67 | # HDD Information |
||
68 | e = "="*15, " HDD ", "="*15, "\n" |
||
69 | hdd_header = "".join(e) |
||
70 | hdd_info = hdd_header |
||
71 | partitions = psutil.disk_partitions() |
||
72 | for partition in partitions: |
||
73 | try: |
||
74 | partition_usage = psutil.disk_usage(partition.mountpoint) |
||
75 | except PermissionError: |
||
76 | continue |
||
77 | disk_info = "\nDevice: {}\nFile system: {}\nTotal Size: {}\nUsed: {} ({:.2f}%)\nFree: {} ({:.2f}%)\n".format( |
||
78 | partition.device, partition.fstype, get_size(partition_usage.total), |
||
79 | get_size(partition_usage.used), partition_usage.percent, get_size(partition_usage.free), |
||
80 | 100-partition_usage.percent) |
||
81 | hdd_info = "{}{}".format(hdd_info, disk_info) |
||
82 | |||
83 | # Combine everything |
||
84 | server_status = "```\n{}\n{}\n{}\n{}\n{}\n```".format(sys_info, up, cpu_info, ram_info, hdd_info) |
||
85 | update.message.reply_text(server_status, parse_mode="Markdown") |
||
86 |