forked from enzoblindow/project-euler
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreate_solution.py
More file actions
112 lines (93 loc) · 3.39 KB
/
Copy pathcreate_solution.py
File metadata and controls
112 lines (93 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Quickly add new solution from the euler project list and create the barebone
python main.py file for it.
Also adds a new item to the list in the repositories README.md
"""
import logging
import os
import requests
import click
from bs4 import BeautifulSoup
@click.command()
@click.option('--pid', prompt='Project #: ', help='Number of the problem.')
def create(pid):
zid = str(pid).zfill(3)
cwd = os.getcwd()
if os.path.isdir('{}/solutions/p{}'.format(cwd, zid)):
logging.warn('Solution already exists, aborting..')
return
filename = '{}/solutions/p{}/README.md'.format(cwd, zid)
url = 'https://projecteuler.net/problem={}'.format(pid)
r = requests.get(url)
data = r.text
soup = BeautifulSoup(data, 'lxml')
# Grab problem title and body from project euler website
title = soup.h2.string
assignment = []
for i in soup.findAll("div", {"class": "problem_content"}):
for k in i.contents:
try:
for s in k.stripped_strings:
assignment += repr(s)
assignment += ['\n\n']
except:
pass
assignment = ''.join(assignment)
assignment = assignment.replace("u'", "").replace("'", " ")
logging.info('Euler problem title and assignment fetched')
# Create directory
if not os.path.exists(os.path.dirname(filename)):
try:
os.makedirs(os.path.dirname(filename))
logging.info('p{} directory created'.format(zid))
except OSError as e:
logging.error(e.message)
return
# Create euler problems README.md
with open(filename, 'w') as f:
f.write('# {}\n'.format(title))
f.write('### Problem {}\n'.format(zid))
f.write('\n{}'.format(assignment))
logging.info('/solutions/p{}/README.md created'.format(zid))
f.close()
# Create euler problems main.py
filename = '{}/solutions/p{}/__main__.py'.format(cwd, zid)
MAIN_PY = """#!/usr/bin/env python
# -*- coding: utf-8 -*-
from euler import euler
@euler(pid={}, update_readme=False)
def solve():
return 'WIP'
if __name__ == '__main__':
print solve()
""".format(pid)
with open(filename, 'w') as f:
f.write(MAIN_PY)
logging.info('/solutions/p{}/__main__.py created'.format(zid))
f.close()
# Add new entry to the repository README.md
filename = '{}/README.md'.format(cwd)
readme_line = "| {} | {} |".format(zid, title)
readme_line += " [Euler](https://projecteuler.net/problem={}) |".format(pid)
readme_line += " [Solution](https://github.com/enzoblindow/project-euler/tree/master/solutions/p{}) |".format(zid)
readme_line += " [Python](https://github.com/enzoblindow/project-euler/blob/master/solutions/p{}/__main__.py) |".format(zid)
readme_line += " |"
with open(filename, "r+") as f:
content = f.readlines()
for idx, line in enumerate(content):
try:
sid = int(line[2:5])
except ValueError:
continue
if sid > int(pid):
content.insert(idx, readme_line + '\n')
logging.info('Added row in project README.md'.format(zid))
break
f.seek(0)
f.write(''.join(content))
f.close()
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
create()