Skip to content

Instantly share code, notes, and snippets.

@thesnapdragon
Last active December 13, 2015 13:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thesnapdragon/bedced78b69825adc8b7 to your computer and use it in GitHub Desktop.
Save thesnapdragon/bedced78b69825adc8b7 to your computer and use it in GitHub Desktop.
Generate hostname named after famous physicists
#!/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()
@underyx
Copy link

underyx commented Dec 2, 2015

str.split() splits at whitespace by default and iterable[-1] gives you the last item, so if you only pass the name to make_hostname it can be rewritten as simply:

def make_hostname(name):
  return name.lower().split()[-1]

🎉

@thesnapdragon
Copy link
Author

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