Skip to content

📦 NEW: get cpu process #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Oct 26, 2022
Merged

📦 NEW: get cpu process #16

merged 8 commits into from
Oct 26, 2022

Conversation

TechWiz-3
Copy link
Contributor

In this PR I've added a directory called get_cpu_process

It includes:

  • get_cpu_process.py which contains three functions relating to getting the information on a process.
  • cpu a CLI interface for the get_cpu_process functions which has 3 commands
    1. ./cpu <process> - returns True if the process is running
    2. ./cpu --get-pid <process> - returns the process id of the specified process
    3. ./cpu --run-time <process> - returns how long the specified process has been running

Let me know what you think 👍

Comment on lines 5 to 7
from get_cpu_process import get_pid
from get_cpu_process import get_process_run_time
from get_cpu_process import is_running
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to immediately import all the necessary entities in one request

from get_cpu_process import get_pid, get_process_run_time, is_running

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 👍

Comment on lines 10 to 25
def help():
print("")
print("Simple CPU related script written by Zac the Wise utilising psutil")
print("")
print("Basic usage: cpu \"<process-name>\"")
print("")
print("Example: cpu \"amethyst\"")
print("Returns 'running' or 'not running'")
print("")
print("")
print("Advanced usage: cpu <option> <arguement>")
print("")
print(" --get-pid \"<process-name>\" returns the process id")
print("")
print(" --run-time \"<process-name>\"> returns the process running-time")
print("")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use multilingual strings, then it would be possible to beautifully describe the entire text and would not have to escape quotes
However, if you use https://docs.python.org/3/library/argparse.html#example then this is not needed - it generates help text

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 👍. First time learning and using argparse so pls let me know if I can improve anything

Comment on lines 1 to 2
import psutil
# https://thispointer.com/python-check-if-a-process-is-running-by-name-and-find-its-process-id-pid/
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm used to doing it this way :)

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


# SOURCE: https://thispointer.com/python-check-if-a-process-is-running-by-name-and-find-its-process-id-pid/

# pip install psutil
import psutil

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 👍

Comment on lines 5 to 9
def is_running(provided_process_name: str) -> bool:
"""
Takes the name of a process and
returns True if it is running,
False if it isn't"""
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to me that docstring looks more beautiful - the text is separated from the literal

def is_running(provided_process_name: str) -> bool:
    """
    Takes the name of a process and
    returns True if it is running,
    False if it isn't
    """

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 👍

"""
Takes the name of a process and
returns the process runtime"""
from datetime import datetime
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is better to import at the beginning of the file.
And for datetime I use import datetime as DT (DT.datetime, DT.date, DT.timedelta) - a handy module shorthand

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 👍

@gil9red gil9red merged commit e2415c6 into gil9red:master Oct 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants