Skip to main content
edited tags
Link
Reinderien
  • 71.2k
  • 5
  • 76
  • 257
edited tags
Link
Toby Speight
  • 88.7k
  • 14
  • 104
  • 327
deleted 31 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

I've created a basic Python cliCLI script, which takes a Github username, and returns some basic information associated with said username. I use the requests for HTTP requests (I'm new to this area, BTW) and docopt for command-line handling. The code is as follows:

#!/usr/bin/env python3

"""Github User Info

Get basic information on a Github user

Usage:
  github_user_info.py USERNAME
  github_user_info.py (-h | --help)
  github_user_info.py (-v | --version)

Arguments:
  USERNAME     Github username

Options:
  -h --help     Show this screen.
  -v --version  Show version.
"""

from docopt import docopt
import requests

import sys


def main():
    if len(sys.argv) == 1:
        sys.argv.append('-h')  # Show help screen if no arguments are passed

    cmd_arguments = docopt(__doc__, version='Github User Info v1.0')

    username = cmd_arguments["USERNAME"]

    if len(username.split()) != 1:
        print('Github usernames must be one word in length! Exiting!')
        return

    response = requests.get(f'https://api.github.com/users/{username}')

    if response.status_code != requests.codes.ok:
        if response.status_code == 404:  # No such user was found
            print(f'No Github user was found with username {username}!')
        else:
            print(f'An unexpected error occured! Exiting with error code: '
                  f'{response.status_code}')
        return

    responses_json = response.json()

    print(f'The following information was found on user: {username}',
          end='\n\n')
    print(f'Name: {responses_json["name"]}')
    print(f'Bio: {responses_json["bio"]}')
    print(f'Followers: {responses_json["followers"]}')
    print(f'Following: {responses_json["following"]}')


if __name__ == '__main__':
    main()

I'd mainly like to know if this is the correct way to handle HTTP requests (in terms of getting them, error handling, etc.). I'd also like to make sure that I follow good pythonPython conventions.

I've created a basic Python cli script, which takes a Github username, and returns some basic information associated with said username. I use the requests for HTTP requests (I'm new to this area, BTW) and docopt for command-line handling. The code is as follows:

#!/usr/bin/env python3

"""Github User Info

Get basic information on a Github user

Usage:
  github_user_info.py USERNAME
  github_user_info.py (-h | --help)
  github_user_info.py (-v | --version)

Arguments:
  USERNAME     Github username

Options:
  -h --help     Show this screen.
  -v --version  Show version.
"""

from docopt import docopt
import requests

import sys


def main():
    if len(sys.argv) == 1:
        sys.argv.append('-h')  # Show help screen if no arguments are passed

    cmd_arguments = docopt(__doc__, version='Github User Info v1.0')

    username = cmd_arguments["USERNAME"]

    if len(username.split()) != 1:
        print('Github usernames must be one word in length! Exiting!')
        return

    response = requests.get(f'https://api.github.com/users/{username}')

    if response.status_code != requests.codes.ok:
        if response.status_code == 404:  # No such user was found
            print(f'No Github user was found with username {username}!')
        else:
            print(f'An unexpected error occured! Exiting with error code: '
                  f'{response.status_code}')
        return

    responses_json = response.json()

    print(f'The following information was found on user: {username}',
          end='\n\n')
    print(f'Name: {responses_json["name"]}')
    print(f'Bio: {responses_json["bio"]}')
    print(f'Followers: {responses_json["followers"]}')
    print(f'Following: {responses_json["following"]}')


if __name__ == '__main__':
    main()

I'd mainly like to know if this is the correct way to handle HTTP requests (in terms of getting them, error handling, etc.). I'd also like to make sure that I follow good python conventions.

I've created a basic Python CLI script, which takes a Github username, and returns some basic information associated with said username. I use the requests for HTTP requests (I'm new to this area) and docopt for command-line handling.

#!/usr/bin/env python3

"""Github User Info

Get basic information on a Github user

Usage:
  github_user_info.py USERNAME
  github_user_info.py (-h | --help)
  github_user_info.py (-v | --version)

Arguments:
  USERNAME     Github username

Options:
  -h --help     Show this screen.
  -v --version  Show version.
"""

from docopt import docopt
import requests

import sys


def main():
    if len(sys.argv) == 1:
        sys.argv.append('-h')  # Show help screen if no arguments are passed

    cmd_arguments = docopt(__doc__, version='Github User Info v1.0')

    username = cmd_arguments["USERNAME"]

    if len(username.split()) != 1:
        print('Github usernames must be one word in length! Exiting!')
        return

    response = requests.get(f'https://api.github.com/users/{username}')

    if response.status_code != requests.codes.ok:
        if response.status_code == 404:  # No such user was found
            print(f'No Github user was found with username {username}!')
        else:
            print(f'An unexpected error occured! Exiting with error code: '
                  f'{response.status_code}')
        return

    responses_json = response.json()

    print(f'The following information was found on user: {username}',
          end='\n\n')
    print(f'Name: {responses_json["name"]}')
    print(f'Bio: {responses_json["bio"]}')
    print(f'Followers: {responses_json["followers"]}')
    print(f'Following: {responses_json["following"]}')


if __name__ == '__main__':
    main()

I'd mainly like to know if this is the correct way to handle HTTP requests (in terms of getting them, error handling, etc.). I'd also like to make sure that I follow good Python conventions.

Tweeted twitter.com/StackCodeReview/status/1028068791501185024
edited tags
Link
200_success
  • 145.7k
  • 22
  • 191
  • 481
Loading
Source Link
Loading