Last active
December 13, 2015 13:24
-
-
Save thesnapdragon/bedced78b69825adc8b7 to your computer and use it in GitHub Desktop.
Generate hostname named after famous physicists
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
# Milán Unicsovics, u.milan at gmail dot com | |
# usage: physicist_hostname.py [-h] [-q] | |
# optional arguments: | |
# -h, --help show this help message and exit | |
# -q, --quiet suppress output verbosity | |
# generates a hostname | |
import requests | |
import random | |
from bs4 import BeautifulSoup | |
import argparse | |
def parse_args(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument("-q", "--quiet", help="suppress output verbosity", action="store_true") | |
return parser.parse_args() | |
def get_physicists(): | |
html = requests.get('https://en.wikipedia.org/wiki/List_of_physicists').text | |
parsed_html = BeautifulSoup(html, 'html.parser') | |
links = [uls.find_all('a') for uls in parsed_html.body.find_all('ul')[1:26]] | |
return [{'name': link.text, 'link': 'https://en.wikipedia.org' + link.get('href')} for _ in links for link in _] | |
def select_physicist(names): | |
return random.choice(names) | |
def make_hostname(physicist): | |
return physicist['name'].split()[-1].lower() | |
def main(): | |
args = parse_args() | |
physicist = select_physicist(get_physicists()) | |
hostname = make_hostname(physicist) | |
if args.quiet: | |
print(hostname) | |
else: | |
print('{0} - ( {1}: {2} )'.format(hostname, physicist['name'], physicist['link'])) | |
if __name__ == '__main__': | |
main() |
yeah, its more idiomatic this way :) almost forgot to write pythonic code
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
str.split()
splits at whitespace by default anditerable[-1]
gives you the last item, so if you only pass the name to make_hostname it can be rewritten as simply:🎉