Last active
September 7, 2017 11:30
-
-
Save rlaker/63ad0b6bcc861a989e675015a79104d8 to your computer and use it in GitHub Desktop.
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
import random | |
import argparse | |
import urllib.request | |
import json | |
#top 20 users from contributions | |
users = ['GrahamCampbell','fabpot', 'weierophinney', 'rkh', | |
'josh', 'SamyPesse', 'rstacruz', 'tmm1', 'michalbe', | |
'andrew', 'Ocramius', 'c9s', 'steipete', 'feross', | |
'afc163', 'ornicar', 'afollestad', 'KrauseFx', 'tmcw', | |
'bevacqua'] | |
#found http://githut.info/ which lists the most popular languages on github | |
#Found probability for each of the languages, then made cumulative distribution | |
#the probability of javascript (most popular) = 0.197 so lower bound is 0 and upper bound is 0.197 | |
#prob of Java is 0.14 so lower bound is 0.197 upper bound is 0.333 and so on | |
#[number of watchers for that language, number of repos written in that language, lower bound, upper bound] | |
#HTML wasn't listed but appears occasionally | |
languages = {'Ruby': [0,0,0.617,0.698],'Python':[0,0,0.333,0.433],'CSS':[0,0,0.433,0.533], | |
'Java':[0,0,0.197,0.333],'JavaScript':[0,0,0.0,0.197],'C++':[0,0,0.698,0.751], | |
'C':[0,0,0.751,0.795], 'C#':[0,0,0.835,0.869], 'PHP':[0,0,0.533,0.617], | |
'Emacs Lisp':[0,0,0.979,0.986],'CoffeeScript':[0,0,0.949,0.958], | |
'Shell':[0,0,0.795,0.835],'Swift':[0,0,0.966,0.973], | |
'Go':[0,0,0.926,0.939], 'Objective-C':[0,0,0.869,0.891], | |
'Perl':[0,0,0.939,0.949],'VimL':[0,0,0.912,0.926], | |
'Scala':[0,0,0.973,0.979], 'Clojure':[0,0,0.996,1.0], | |
'Haskell':[0,0,0.986,0.991], 'TeX': [0,0,0.958,0.966], | |
'R':[0,0, 0.891,0.912], 'Lua':[0,0,0.991,0.996], 'HTML': [0,0,2,2]} | |
url = 'https://api.github.com/users/' | |
def get_repos(user): | |
#gets the repos of the user from github API | |
req = urllib.request.Request(url + user + '/repos') | |
with urllib.request.urlopen(req) as response: | |
data = json.load(response) | |
return data | |
def random_lang(languages): | |
#picks a language at random based on the probabilities | |
# if the user has no repos | |
r = random.uniform(0,1) | |
for key in languages.keys(): | |
if r > languages[key][2] and r < languages[key][3]: | |
print('Favourite Language (from probability)',key) | |
def fav_language(data, languages, stat): | |
#compares the language for each repos to the list of languages | |
#adds up total amount of watchers for each language and | |
#also counts the number of repos with that language | |
for i in range(len(data)): | |
for key in languages.keys(): | |
if data[i]['language'] == key: | |
languages[key][0] = languages[key][0]+data[i]['watchers'] | |
languages[key][1] = languages[key][1]+ 1 | |
values = languages.values() | |
watchers = sorted(values, key=lambda x: x[0]) #sorts based on watchers of that language | |
number = sorted(values, key=lambda x: x[1]) #sorts based on number of repos with that language | |
most_watchers = watchers[-1] #picks the highest one | |
highest_number = number[-1] #picks the highest one | |
for key in languages.keys(): | |
if languages[key] == most_watchers and stat == 'watchers': | |
print('Favourite Language (by watcher)', key , most_watchers[0]) | |
if languages[key] == highest_number and stat == 'number': | |
print('Favourite Language (by number)', key, highest_number[1]) | |
def fav(user): | |
repos = get_repos(user) | |
if len(repos) == 0: | |
random_lang(languages) # if no repos find based on probability | |
else: | |
fav_language(repos, languages, 'number') | |
## COMMAND LINE## | |
#using argparse for commandline input | |
# so in the command line type: language_finder username | |
def Main(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument("user", help="Finds users favourite language", type=str) | |
args = parser.parse_args() | |
result = fav(args.user) | |
if __name__== '__main__': | |
Main() | |
"""repos = get_repos('rl4215') | |
if len(repos) == 0: | |
random_lang(languages) # if no repos find based on probability | |
else: | |
fav_language(repos, languages, 'number')""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment