pep8 cleanup

This commit is contained in:
Michael Rice
2013-08-03 06:01:30 +00:00
parent f9d14f6b26
commit 4ef8ba04b0

View File

@ -1,7 +1,12 @@
#!/usr/bin/env python #!/usr/bin/env python
import datetime, math, itertools, urllib2, json
title=''' import datetime
import math
import itertools
import urllib2
import json
TITLE = '''
_ __ _____ __ _ _ __ _____ __ _
____ _(_) /_/ __(_) /_(_) ____ _(_) /_/ __(_) /_(_)
/ __ `/ / __/ /_/ / __/ / / __ `/ / __/ /_/ / __/ /
@ -10,7 +15,7 @@ title='''
/____/ /____/
''' '''
kitty=[ KITTY = [
[0,0,0,4,0,0,0,0,4,0,0,0], [0,0,0,4,0,0,0,0,4,0,0,0],
[0,0,4,2,4,4,4,4,2,4,0,0], [0,0,4,2,4,4,4,4,2,4,0,0],
[0,0,4,2,2,2,2,2,2,4,0,0], [0,0,4,2,2,2,2,2,2,4,0,0],
@ -19,7 +24,7 @@ kitty=[
[2,2,4,2,2,2,2,2,2,4,2,2], [2,2,4,2,2,2,2,2,2,4,2,2],
[0,0,0,3,4,4,4,4,3,0,0,0]] [0,0,0,3,4,4,4,4,3,0,0,0]]
oneup=[ ONEUP = [
[0,4,4,4,4,4,4,4,0], [0,4,4,4,4,4,4,4,0],
[4,3,2,2,1,2,2,3,4], [4,3,2,2,1,2,2,3,4],
[4,2,2,1,1,1,2,2,4], [4,2,2,1,1,1,2,2,4],
@ -28,7 +33,7 @@ oneup=[
[0,4,1,1,1,1,1,4,0], [0,4,1,1,1,1,1,4,0],
[0,0,4,4,4,4,4,0,0]] [0,0,4,4,4,4,4,0,0]]
oneup2=[ ONEUP2 = [
[0,0,4,4,4,4,4,4,4,0,0], [0,0,4,4,4,4,4,4,4,0,0],
[0,4,2,2,1,1,1,2,2,4,0], [0,4,2,2,1,1,1,2,2,4,0],
[4,3,2,2,1,1,1,2,2,3,4], [4,3,2,2,1,1,1,2,2,3,4],
@ -37,7 +42,7 @@ oneup2=[
[0,0,4,1,1,1,1,1,4,0,0], [0,0,4,1,1,1,1,1,4,0,0],
[0,0,0,4,4,4,4,4,0,0,0]] [0,0,0,4,4,4,4,4,0,0,0]]
hackerschool=[ HACKERSCHOOL = [
[4,4,4,4,4,4], [4,4,4,4,4,4],
[4,3,3,3,3,4], [4,3,3,3,3,4],
[4,1,3,3,1,4], [4,1,3,3,1,4],
@ -46,7 +51,7 @@ hackerschool=[
[0,0,4,4,0,0], [0,0,4,4,0,0],
[4,4,4,4,4,4]] [4,4,4,4,4,4]]
octocat=[ OCTOCAT = [
[0,0,0,4,0,0,0,4,0], [0,0,0,4,0,0,0,4,0],
[0,0,4,4,4,4,4,4,4], [0,0,4,4,4,4,4,4,4],
[0,0,4,1,3,3,3,1,4], [0,0,4,1,3,3,3,1,4],
@ -55,7 +60,7 @@ octocat=[
[0,0,4,4,4,4,4,4,4], [0,0,4,4,4,4,4,4,4],
[0,0,4,0,4,0,4,0,4]] [0,0,4,0,4,0,4,0,4]]
octocat2=[ OCTOCAT2 = [
[0,0,4,0,0,4,0], [0,0,4,0,0,4,0],
[0,4,4,4,4,4,4], [0,4,4,4,4,4,4],
[0,4,1,3,3,1,4], [0,4,1,3,3,1,4],
@ -64,7 +69,7 @@ octocat2=[
[0,4,4,4,4,4,0], [0,4,4,4,4,4,0],
[0,0,0,4,4,4,0]] [0,0,0,4,4,4,0]]
hello=[ HELLO = [
[0,1,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,4], [0,1,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,4],
[0,2,0,0,0,0,0,0,0,2,0,2,0,0,0,0,0,4], [0,2,0,0,0,0,0,0,0,2,0,2,0,0,0,0,0,4],
[0,3,3,3,0,2,3,3,0,3,0,3,0,1,3,1,0,3], [0,3,3,3,0,2,3,3,0,3,0,3,0,1,3,1,0,3],
@ -73,7 +78,7 @@ hello=[
[0,2,0,2,0,2,0,0,0,2,0,2,0,2,0,2,0,0], [0,2,0,2,0,2,0,0,0,2,0,2,0,2,0,2,0,0],
[0,1,0,1,0,1,1,1,0,1,0,1,0,1,1,1,0,4]] [0,1,0,1,0,1,1,1,0,1,0,1,0,1,1,1,0,4]]
hireme=[ HIREME = [
[1,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [1,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[3,3,3,0,2,0,3,3,3,0,2,3,3,0,0,3,3,0,3,0,0,2,3,3], [3,3,3,0,2,0,3,3,3,0,2,3,3,0,0,3,3,0,3,0,0,2,3,3],
@ -82,7 +87,7 @@ hireme=[
[2,0,2,0,2,0,2,0,0,0,2,0,0,0,0,2,0,2,0,2,0,2,0,0], [2,0,2,0,2,0,2,0,0,0,2,0,0,0,0,2,0,2,0,2,0,2,0,0],
[1,0,1,0,1,0,1,0,0,0,1,1,1,0,0,1,0,1,0,1,0,1,1,1]] [1,0,1,0,1,0,1,0,0,0,1,1,1,0,0,1,0,1,0,1,0,1,1,1]]
ascii_to_number = { ASCII_TO_NUMBER = {
'_': 0, '_': 0,
'_': 1, '_': 1,
'~': 2, '~': 2,
@ -91,24 +96,21 @@ ascii_to_number = {
} }
def str_to_sprite(content): def str_to_sprite(content):
# Break out lines and filter any excess # Break out lines and filter any excess
lines = content.split('\n') lines = content.split('\n')
def is_empty_line(line): def is_empty_line(line):
return len(line) != 0 return len(line) != 0
lines = filter(is_empty_line, lines) lines = filter(is_empty_line, lines)
# Break up lines into each character
split_lines = map(list, lines)
# Replace each character with its numeric equivalent
for line in split_lines:
for index, char in enumerate(line):
line[index] = ASCII_TO_NUMBER.get(char, 0)
# Return the formatted str
return split_lines
# Break up lines into each character ONEUP_STR = str_to_sprite("""
split_lines = map(list, lines)
# Replace each character with its numeric equivalent
for line in split_lines:
for index, char in enumerate(line):
line[index] = ascii_to_number.get(char, 0)
# Return the formatted str
return split_lines
oneup_str = str_to_sprite("""
******* *******
*=~~-~~=* *=~~-~~=*
*~~---~~* *~~---~~*
@ -118,164 +120,181 @@ oneup_str = str_to_sprite("""
***** *****
""") """)
images={ IMAGES = {
'kitty':kitty, 'kitty': KITTY,
'oneup':oneup, 'oneup':ONEUP,
'oneup2':oneup2, 'oneup2':ONEUP2,
'hackerschool':hackerschool, 'hackerschool':HACKERSCHOOL,
'octocat':octocat, 'octocat':OCTOCAT,
'octocat2':octocat2, 'octocat2':OCTOCAT2,
'hello':hello, 'hello':HELLO,
'hireme':hireme, 'hireme':HIREME,
'oneup_str':oneup_str 'oneup_str':ONEUP_STR
} }
def load_images(imgNames): def load_images(img_names):
"""loads user images from given file(s)""" """loads user images from given file(s)"""
if imgNames[0]=='': if img_names[0] == '':
return dict() return dict()
for imageName in imgNames: for image_name in img_names:
img = open(imageName) img = open(image_name)
loadedImgs = {} loaded_imgs = {}
imgList = '' img_list = ''
imgLine = ' ' img_line = ' '
name = img.readline().replace('\n', '') name = img.readline().replace('\n', '')
name = name[1:] name = name[1:]
while True: while True:
imgLine = img.readline() img_line = img.readline()
if imgLine == '': if img_line == '':
break break
imgLine.replace('\n', '') img_line.replace('\n', '')
if(imgLine[0] == ':'): if(img_line[0] == ':'):
loadedImgs[name] = json.loads(imgList) loaded_imgs[name] = json.loads(img_list)
name = imgLine[1:] name = img_line[1:]
imgList = '' img_list = ''
else: else:
imgList += imgLine img_list += img_line
loadedImgs[name] = json.loads(imgList) loaded_imgs[name] = json.loads(img_list)
return loadedImgs return loaded_imgs
def get_calendar(username): def get_calendar(username, base_url='https://github.com/'):
"""retrieves the github commit calendar data for a username""" """retrieves the github commit calendar data for a username"""
BASEURL='https://github.com/' url = base_url + 'users/' + username + '/contributions_calendar_data'
url = BASEURL + 'users/' + username + '/contributions_calendar_data' page = urllib2.urlopen(url)
page = urllib2.urlopen(url) return json.load(page)
return json.load(page)
def max_commits(input): def max_commits(input):
"""finds the highest number of commits in one day""" """finds the highest number of commits in one day"""
output = set() output = set()
for i, j in enumerate(input): for i, j in enumerate(input):
output.add(input[i][1]) output.add(input[i][1])
output = list(output) output = list(output)
output.sort() output.sort()
output.reverse() output.reverse()
return output[0] return output[0]
def multiplier(max_commits): def multiplier(max_commits):
"""calculates a multiplier to scale github colors to commit history""" """calculates a multiplier to scale github colors to commit history"""
m = max_commits/4.0 m = max_commits/4.0
if m == 0: return 1 if m == 0: return 1
m = math.ceil(m) m = math.ceil(m)
m = int(m) m = int(m)
return m return m
def get_start_date(): def get_start_date():
'''returns a datetime object for the first sunday after one year ago today at 12:00 noon''' """returns a datetime object for the first sunday after one year ago today
d = datetime.datetime.today() at 12:00 noon"""
date = datetime.datetime(d.year-1, d.month, d.day, 12) d = datetime.datetime.today()
weekday = datetime.datetime.weekday(date) date = datetime.datetime(d.year-1, d.month, d.day, 12)
while weekday < 6: weekday = datetime.datetime.weekday(date)
date = date + datetime.timedelta(1) while weekday < 6:
weekday = datetime.datetime.weekday(date) date = date + datetime.timedelta(1)
return date weekday = datetime.datetime.weekday(date)
return date
def date_gen(start_date, offset=0): def date_gen(start_date, offset=0):
'''generator that returns the next date, requires a datetime object as input. The offset is in weeks''' """generator that returns the next date, requires a datetime object as
start = offset * 7 input. The offset is in weeks"""
for i in itertools.count(start): start = offset * 7
yield start_date + datetime.timedelta(i) for i in itertools.count(start):
yield start_date + datetime.timedelta(i)
def values_in_date_order(image, multiplier=1): def values_in_date_order(image, multiplier=1):
height = 7 height = 7
width = len(image[0]) width = len(image[0])
for w in range(width): for w in range(width):
for h in range(height): for h in range(height):
yield image[h][w]*multiplier yield image[h][w]*multiplier
def commit(content, commitdate): def commit(content, commitdate):
template = '''echo %s >> gitfiti\nGIT_AUTHOR_DATE=%s GIT_COMMITTER_DATE=%s git commit -a -m "gitfiti"\n''' template = ("""echo {0} >> gitfiti\n"""
return template % (content, commitdate.isoformat(), commitdate.isoformat()) """GIT_AUTHOR_DATE={1} GIT_COMMITTER_DATE={2}\n"""
"""git commit -a -m "gitfiti" > /dev/null\n""")
return template.format(content, commitdate.isoformat(),
commitdate.isoformat())
def fake_it(image, start_date, username, repo, offset=0, multiplier=1): def fake_it(image, start_date, username, repo, offset=0, multiplier=1,
template = ('#!/bin/bash\n' git_url='git@github.com'):
'REPO=%s\n' template = ('#!/bin/bash\n'
'git init $REPO\n' 'REPO={0}\n'
'cd $REPO\n' 'git init $REPO\n'
'touch README.md\n' 'cd $REPO\n'
'git add README.md\n' 'touch README.md\n'
'touch gitfiti\n' 'git add README.md\n'
'git add gitfiti\n' 'touch gitfiti\n'
'%s\n' 'git add gitfiti\n'
'git remote add origin git@github.com:%s/$REPO.git\n' '{1}\n'
'git pull\n' 'git remote add origin {2}:{3}/$REPO.git\n'
'git push -u origin master\n') 'git pull\n'
strings = [] 'git push -u origin master\n')
for value, date in zip(values_in_date_order(image, multiplier), date_gen(start_date, offset)): strings = []
for i in range(value): for value, date in zip(values_in_date_order(image, multiplier),
strings.append(commit(i, date)) date_gen(start_date, offset)):
return template % (repo, "".join(strings), username) print value, date
for i in range(value):
print i
strings.append(commit(i, date))
return template.format(repo, "".join(strings), git_url, username)
def save(output, filename): def save(output, filename):
"""Saves the list to a given filename""" """Saves the list to a given filename"""
f = open(filename, "w") f = open(filename, "w")
f.write(output) f.write(output)
f.close() f.close()
def main(): def main():
global images global IMAGES
print title print TITLE
print 'Enter your github username:' print 'Enter your github username:'
username = raw_input(">") username = raw_input(">")
cal = get_calendar(username) cal = get_calendar(username)
m = multiplier(max_commits(cal)) m = multiplier(max_commits(cal))
print 'Enter name of the repo to be used by gitfiti:' print 'Enter name of the repo to be used by gitfiti:'
repo = raw_input(">") repo = raw_input(">")
print 'Enter number of weeks to offset the image (from the left):'
offset = raw_input(">")
if offset == None: offset = 0
else: offset = int(offset)
print ('By default gitfiti.py matches the darkest pixel to the highest\n' print 'Enter number of weeks to offset the image (from the left):'
'number of commits found in your github commit/activity calendar,\n' offset = raw_input(">")
'\n' if offset == None:
'Currently this is : %s commits\n' offset = 0
'\n' else:
'Enter the word "gitfiti" to exceed your max\n' offset = int(offset)
'(this option generates WAY more commits)\n'
'Any other input will cause the default matching behavior') % (max_commits(cal),)
match = raw_input(">")
if match == "gitfiti": match = m
else: match = 1
print 'enter file(s) to load images from (blank if not applicable)' print ('By default gitfiti.py matches the darkest pixel to the highest\n'
imgNames = raw_input(">").split(' ') 'number of commits found in your github commit/activity calendar,\n'
images = dict(images, **load_images(imgNames)) '\n'
'Currently this is : {0} commits\n'
'\n'
'Enter the word "gitfiti" to exceed your max\n'
'(this option generates WAY more commits)\n'
'Any other input will cause the default matching behavior'
).format(max_commits(cal),)
match = raw_input(">")
if match == "gitfiti":
match = m
else:
match = 1
print 'enter the image name to gitfiti' print 'enter file(s) to load images from (blank if not applicable)'
print 'images: ' + ", ".join(images.keys()) img_names = raw_input(">").split(' ')
image = raw_input(">") images = dict(IMAGES, **load_images(img_names))
if image == None: image = images['kitty']
else:
try: image = images[image]
except: image = images['kitty']
output = fake_it(image, get_start_date(), username, repo, offset, m*match) print 'enter the image name to gitfiti'
save(output, 'gitfiti.sh') print 'images: ' + ", ".join(images.keys())
print 'gitfiti.sh saved. Create a new(!) repo at: https://github.com/new and run it.' image = raw_input(">")
if image == None:
image = IMAGES['kitty']
else:
try:
image = IMAGES[image]
except:
image = IMAGES['kitty']
if __name__=='__main__': output = fake_it(image, get_start_date(), username, repo, offset, m*match)
main() save(output, 'gitfiti.sh')
print 'gitfiti.sh saved.'
print 'Create a new(!) repo at: https://github.com/new and run it.'
if __name__ == '__main__':
main()