track comment scraping system created, rest_api exercise tweaked

This commit is contained in:
Xevion
2019-07-23 18:22:25 -05:00
parent 498f8a7e86
commit 2d7dc2da99
9 changed files with 804 additions and 9 deletions

6
.vscode/settings.json vendored Normal file
View File

@@ -0,0 +1,6 @@
{
"python.pythonPath": "A:\\Installations\\Anaconda\\python.exe",
"python.linting.flake8Enabled": true,
"python.linting.enabled": false,
"python.linting.pylintEnabled": false
}

View File

@@ -1,9 +1,12 @@
# Exercism
# Comments
## Purpose
This file encompasses all comments from all exercises in the {} track. There are currently {} exercises, and {} that have comments.
This is just a repo for storing all of my Exercism exercises somewhere off-site, as well as to build up my git experience and usage.
## Hello World
## Sections
[File Link][1f] | [Submission Link][1s]
* **[Python](./python/README.md)**
Easy enough exercise. :3
[1f]: ./test.html
[1s]: ./

287
python/COMMENTS.md Normal file
View File

@@ -0,0 +1,287 @@
# Python Track Comments
This page represents all my comments from my solutions currently hosted on [Exercism.io](https://exercism.io/). You can view my profile [here](https://exercism.io/profiles/Xevion).
The reason for this is simply to have a place where I can collect my comments, as well as just have some fun with Python and webscraping. Exercise file and exercise submission links will be provided for each and every exercise.
This file is for the **Python** track, contains **47** submissions, **17** of which have comments. This file was built on **23-07-2019** at **23:47:58 UTC**.
## Word Count
[Link to File](./word-count/word_count.py) | [Link to Submission](https://exercism.io/tracks/python/exercises/word-count/solutions/b8740c62231043358e4bdaa590764dbe)
Not exactly proud of this one really. I had to use regex to do the splitting as I couldn't think of a way to split on both arbitrary length whitespace AND punctuation. Luckily, the string library has access to all the punctuation I needed, so I just rolled with it. This isn't exactly the most elegant solution I could come up with, but it's definitely short and that's what matters to me. It uses list/dictionary comprehension, which is huge to me, so it should probably be moderately fast in the long run.
## Sieve
[Link to File](./sieve/sieve.py) | [Link to Submission](https://exercism.io/tracks/python/exercises/sieve/solutions/9e38df44da5b4bc48d2d6e57ac7b5c2a)
## Hello World
[Link to File](./hello-world/hello_world.py) | [Link to Submission](https://exercism.io/tracks/python/exercises/hello-world/solutions/e926fb7c9400480a80f75e957fe1b027)
## Leap
[Link to File](./leap/leap.py) | [Link to Submission](https://exercism.io/tracks/python/exercises/leap/solutions/299afd8086d543ccb59fa20a3375c3bc)
## Reverse String
[Link to File](./reverse-string/reverse_string.py) | [Link to Submission](https://exercism.io/tracks/python/exercises/reverse-string/solutions/80bc2033cd98458a89c53d5487b701d7)
## Isogram
[Link to File](./isogram/isogram.py) | [Link to Submission](https://exercism.io/tracks/python/exercises/isogram/solutions/57fdfd4c98644a2191b9fed067526d14)
I'm kind of annoyed that there is no other way to do it that is 2 lines that doesn't have it calculate string without the spaces and hyphens twice, but whatever.
## Pangram
[Link to File](./pangram/pangram.py) | [Link to Submission](https://exercism.io/tracks/python/exercises/pangram/solutions/928a4d5c41274472bf2b6feee833b444)
## RNA Transcription
[Link to File](./rna-transcription/rna_transcription.py) | [Link to Submission](https://exercism.io/tracks/python/exercises/rna-transcription/solutions/3fc8fca40565475dbeb36f5e8048281f)
## ISBN Verifier
[Link to File](./isbn-verifier/isbn_verifier.py) | [Link to Submission](https://exercism.io/tracks/python/exercises/isbn-verifier/solutions/fa2d5d0d7cc8469586d7227f197cff9f)
## Hamming
[Link to File](./hamming/hamming.py) | [Link to Submission](https://exercism.io/tracks/python/exercises/hamming/solutions/ab3e897d3758468ca0f44cd68b2c9d33)
## Gigasecond
[Link to File](./gigasecond/gigasecond.py) | [Link to Submission](https://exercism.io/tracks/python/exercises/gigasecond/solutions/2959cc74dfae4961a8f592df2e7a00ad)
## Bob
[Link to File](./bob/bob.py) | [Link to Submission](https://exercism.io/tracks/python/exercises/bob/solutions/7961709ebcf94c47b27720337d4a1515)
## Yacht
[Link to File](./yacht/yacht.py) | [Link to Submission](https://exercism.io/tracks/python/exercises/yacht/solutions/81c9206cf8c54284bd7431449526f47b)
## Run Length Encoding
[Link to File](./run-length-encoding/run_length_encoding.py) | [Link to Submission](https://exercism.io/tracks/python/exercises/run-length-encoding/solutions/d794356bb2f64e1998420f4ed754c3e0)
## Meetup
[Link to File](./meetup/meetup.py) | [Link to Submission](https://exercism.io/tracks/python/exercises/meetup/solutions/5ca86ad3921745e98e7e06e1ae02d394)
I think I should have changed up the constants to be dynamic so the solution would be smaller, but honestly, in the end it's probably better to go with a pretty and longer solution.
I feel like my else method of handling the `week` parameter was pretty weak.
## Armstrong Numbers
[Link to File](./armstrong-numbers/armstrong_numbers.py) | [Link to Submission](https://exercism.io/tracks/python/exercises/armstrong-numbers/solutions/f7936f73fb564c4b9da704ea5b5de31a)
## Rotational Cipher
[Link to File](./rotational-cipher/rotational_cipher.py) | [Link to Submission](https://exercism.io/tracks/python/exercises/rotational-cipher/solutions/83ee13c77e1d4de898c100f544771cf7)
## Difference Of Squares
[Link to File](./difference-of-squares/difference_of_squares.py) | [Link to Submission](https://exercism.io/tracks/python/exercises/difference-of-squares/solutions/79021d92f5b24650af12a464a9f6e9d6)
## Anagram
[Link to File](./anagram/anagram.py) | [Link to Submission](https://exercism.io/tracks/python/exercises/anagram/solutions/c00ab57051fd4bc8ad350a2cd42349f7)
I'm not sure if my .lower() optimization really made a difference, as it's duplicating the list. Originally, I was making .lower() on every single word, so I changed it, but now I'm wondering if it really made a difference as now I'm introducing `enumerate` and a second list of words. It got a bit longer (3 -> 4 lines), too...
## Allergies
[Link to File](./allergies/allergies.py) | [Link to Submission](https://exercism.io/tracks/python/exercises/allergies/solutions/c7cf9ff0b73d460aa125b5433fc72182)
I took a bit more time with this one because I wanted to properly solve the problem you encounter when a score above 255 is entered, meaning a value not present is given. It took me a couple of minutes to remember how this all worked, but in the process, I learned a couple of long-forgotten math principles.
## Series
[Link to File](./series/series.py) | [Link to Submission](https://exercism.io/tracks/python/exercises/series/solutions/c04eea9d5a0f478ea81039d232a66e7f)
I'm kind of bad at math and thinking in general, so I kind of just skimmed the thinking part via itertools. Oops.
## Robot Simulator
[Link to File](./robot-simulator/robot_simulator.py) | [Link to Submission](https://exercism.io/tracks/python/exercises/robot-simulator/solutions/257266d04daa4093a7d60012d0c003ef)
## Atbash Cipher
[Link to File](./atbash-cipher/atbash_cipher.py) | [Link to Submission](https://exercism.io/tracks/python/exercises/atbash-cipher/solutions/5a7e43952bd94c4d9a1d698c45748d49)
The extra grouping and sanitization was super confusing to figure out, I had honestly no idea what it wanted from me at that point.
## Sum Of Multiples
[Link to File](./sum-of-multiples/sum_of_multiples.py) | [Link to Submission](https://exercism.io/tracks/python/exercises/sum-of-multiples/solutions/376b2f4ace694adf9de021c6cc10e243)
## Acronym
[Link to File](./acronym/acronym.py) | [Link to Submission](https://exercism.io/tracks/python/exercises/acronym/solutions/b73a4ba276de4e158424e6799e885eb3)
Not exactly excited to be using regex and string again for an exercise, but if I don't want to be spending forever on it, I suppose this is a far better solution. Regex just feels like cheating sometimes, haha.
## Say
[Link to File](./say/say.py) | [Link to Submission](https://exercism.io/tracks/python/exercises/say/solutions/c5c81bf1586047a8bf7913ce10e4e7c5)
This exercise should be renamed 'Cardinal Numbers' or something along those lines. 'Say' is too broad and doesn't really mean anything. Anyways, my solution is honestly pretty hard to understand, some of the more complex code at the bottom handled was just working out the quirks that come with recursion (which was mostly a random solution I came up with, I did not plan on using recursion, I was expecting to have to make more than one function).
## Kindergarten Garden
[Link to File](./kindergarten-garden/kindergarten_garden.py) | [Link to Submission](https://exercism.io/tracks/python/exercises/kindergarten-garden/solutions/860766a18d484ef3922418d7416b519f)
Added comments for clarification on what stuff does. For some reason Pytest fails, I believe it's got some kind of problem with it, as only 1 of the 5 tests fails, and I don't exactly see the reason why, so I have reason to believe the pytest itself is incorrect.
## Grade School
[Link to File](./grade-school/grade_school.py) | [Link to Submission](https://exercism.io/tracks/python/exercises/grade-school/solutions/fe5fbb3e83c14bb392fcc8c1a0275e57)
## Flatten Array
[Link to File](./flatten-array/flatten_array.py) | [Link to Submission](https://exercism.io/tracks/python/exercises/flatten-array/solutions/d6dcaaa2dc0f4276ab4255f47dfb570a)
I decided that the empty nested tuple thing wasn't worth trying to fix, it's such a dumb test case.
## Roman Numerals
[Link to File](./roman-numerals/roman_numerals.py) | [Link to Submission](https://exercism.io/tracks/python/exercises/roman-numerals/solutions/878a032e3e874bc3bf6fbe5d6a087962)
## Space Age
[Link to File](./space-age/space_age.py) | [Link to Submission](https://exercism.io/tracks/python/exercises/space-age/solutions/22ab7b841514483ca523bc4c9995969d)
I don't like this solution since the measurements are off and they don't provide proper timings. I went and got my measurements (Oribital Sidereal Period (days) / ratio) from the Nasa factsheet here: https://nssdc.gsfc.nasa.gov/planetary/factsheet/neptunefact.html
## Grains
[Link to File](./grains/grains.py) | [Link to Submission](https://exercism.io/tracks/python/exercises/grains/solutions/adff732869fb4477a4fff084db71a9c2)
## Luhn
[Link to File](./luhn/luhn.py) | [Link to Submission](https://exercism.io/tracks/python/exercises/luhn/solutions/8aa8a2cb386f4dcfa99e2b18c8c7b805)
The pytester looks wrong to me. I tested mine online and double checked that it's doing it correctly, and the solutions on the Instructions page look right, yet mine do not solve test correctly through the pytester. I dunno, it feels weird for it to be wrong for such a simple problem.
## Scrabble Score
[Link to File](./scrabble-score/scrabble_score.py) | [Link to Submission](https://exercism.io/tracks/python/exercises/scrabble-score/solutions/ee423be717314687b974302d5cc82503)
## Robot Name
[Link to File](./robot-name/robot_name.py) | [Link to Submission](https://exercism.io/tracks/python/exercises/robot-name/solutions/f2053e5f37aa4e7594658ff52cd743a7)
## Matrix
[Link to File](./matrix/matrix.py) | [Link to Submission](https://exercism.io/tracks/python/exercises/matrix/solutions/a911d0005ac2488fa1386754d609a929)
## Twelve Days
[Link to File](./twelve-days/twelve_days.py) | [Link to Submission](https://exercism.io/tracks/python/exercises/twelve-days/solutions/1b45c94291dd447d9544ca5670288981)
I forgot that a couple things could be omitted, and decided to trim the nouns so that it's smaller. Script went from 10 or 11 lines to 7 lines for building the string.
## Clock
[Link to File](./clock/clock.py) | [Link to Submission](https://exercism.io/tracks/python/exercises/clock/solutions/5ea6c6c4c840489da2850d5d3dacd63f)
## Tournament
[Link to File](./tournament/tournament.py) | [Link to Submission](https://exercism.io/tracks/python/exercises/tournament/solutions/6617ee41fac443be992d3090e650a16e)
This code is honestly pretty cryptic. I added comments for the portions that honestly make sense. I'm hoping it looks good enough for people to understand how the formatting works. It uses ljust and rjust built-in string methods so that the tally supports up to 99 matches before it breaks. Maybe I should add some code to make variable spacing?
## Protein Translation
[Link to File](./protein-translation/protein_translation.py) | [Link to Submission](https://exercism.io/tracks/python/exercises/protein-translation/solutions/49d2d2bace424729a9f9baf382856be7)
## Two Fer
[Link to File](./two-fer/two_fer.py) | [Link to Submission](https://exercism.io/tracks/python/exercises/two-fer/solutions/15e2514a48434abdaa898d197a718caf)
## Collatz Conjecture
[Link to File](./collatz-conjecture/collatz_conjecture.py) | [Link to Submission](https://exercism.io/tracks/python/exercises/collatz-conjecture/solutions/ce1c77cfccd84f15be130ca55382058e)
Truly cursed.
## Markdown
[Link to File](./markdown/markdown.py) | [Link to Submission](https://exercism.io/tracks/python/exercises/markdown/solutions/75a6c2148fe940eb841a8ebc24e8b6ad)
## Raindrops
[Link to File](./raindrops/raindrops.py) | [Link to Submission](https://exercism.io/tracks/python/exercises/raindrops/solutions/95285ff036d04de1a103805ed7145f20)
## Rest API
[Link to File](./rest-api/rest_api.py) | [Link to Submission](https://exercism.io/tracks/python/exercises/rest-api/solutions/1c88ea7c565c4133a44bcc020617803d)
Comments are included in the code.
## High Scores
[Link to File](./high-scores/high_scores.py) | [Link to Submission](https://exercism.io/tracks/python/exercises/high-scores/solutions/4ff6fff98ed0460b900579ac2c1e7a2f)
## Darts
[Link to File](./darts/darts.py) | [Link to Submission](https://exercism.io/tracks/python/exercises/darts/solutions/dda1870d13b645768af9267f8beb8514)

View File

@@ -0,0 +1 @@
{"track":"python","exercise":"grep","id":"c5d464b7392248d6adaab310e49803f5","url":"https://exercism.io/my/solutions/c5d464b7392248d6adaab310e49803f5","handle":"Xevion","is_requester":true,"auto_approve":false}

114
python/grep/README.md Normal file
View File

@@ -0,0 +1,114 @@
# Grep
Search a file for lines matching a regular expression pattern. Return the line
number and contents of each matching line.
The Unix [`grep`](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/grep.html) command can be used to search for lines in one or more files
that match a user-provided search query (known as the *pattern*).
The `grep` command takes three arguments:
1. The pattern used to match lines in a file.
2. Zero or more flags to customize the matching behavior.
3. One or more files in which to search for matching lines.
Your task is to implement the `grep` function, which should read the contents
of the specified files, find the lines that match the specified pattern
and then output those lines as a single string. Note that the lines should
be output in the order in which they were found, with the first matching line
in the first file being output first.
As an example, suppose there is a file named "input.txt" with the following contents:
```text
hello
world
hello again
```
If we were to call `grep "hello" input.txt`, the returned string should be:
```text
hello
hello again
```
### Flags
As said earlier, the `grep` command should also support the following flags:
- `-n` Print the line numbers of each matching line.
- `-l` Print only the names of files that contain at least one matching line.
- `-i` Match line using a case-insensitive comparison.
- `-v` Invert the program -- collect all lines that fail to match the pattern.
- `-x` Only match entire lines, instead of lines that contain a match.
If we run `grep -n "hello" input.txt`, the `-n` flag will require the matching
lines to be prefixed with its line number:
```text
1:hello
3:hello again
```
And if we run `grep -i "HELLO" input.txt`, we'll do a case-insensitive match,
and the output will be:
```text
hello
hello again
```
The `grep` command should support multiple flags at once.
For example, running `grep -l -v "hello" file1.txt file2.txt` should
print the names of files that do not contain the string "hello".
## Exception messages
Sometimes it is necessary to raise an exception. When you do this, you should include a meaningful error message to
indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. Not
every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include
a message.
To raise a message with an exception, just write it as an argument to the exception type. For example, instead of
`raise Exception`, you should write:
```python
raise Exception("Meaningful message indicating the source of the error")
```
## Running the tests
To run the tests, run the appropriate command below ([why they are different](https://github.com/pytest-dev/pytest/issues/1629#issue-161422224)):
- Python 2.7: `py.test grep_test.py`
- Python 3.4+: `pytest grep_test.py`
Alternatively, you can tell Python to run the pytest module (allowing the same command to be used regardless of Python version):
`python -m pytest grep_test.py`
### Common `pytest` options
- `-v` : enable verbose output
- `-x` : stop running tests on first failure
- `--ff` : run failures from previous test before running other test cases
For other options, see `python -m pytest -h`
## Submitting Exercises
Note that, when trying to submit an exercise, make sure the solution is in the `$EXERCISM_WORKSPACE/python/grep` directory.
You can find your Exercism workspace by running `exercism debug` and looking for the line that starts with `Workspace`.
For more detailed information about running tests, code style and linting,
please see [Running the Tests](http://exercism.io/tracks/python/tests).
## Source
Conversation with Nate Foster. [http://www.cs.cornell.edu/Courses/cs3110/2014sp/hw/0/ps0.pdf](http://www.cs.cornell.edu/Courses/cs3110/2014sp/hw/0/ps0.pdf)
## Submitting Incomplete Solutions
It's possible to submit an incomplete solution so you can see how others have completed the exercise.

2
python/grep/grep.py Normal file
View File

@@ -0,0 +1,2 @@
def grep(pattern, flags, files):
pass

298
python/grep/grep_test.py Normal file
View File

@@ -0,0 +1,298 @@
import unittest
try:
import builtins
except ImportError:
import __builtin__ as builtins
from grep import grep
# Tests adapted from `problem-specifications//canonical-data.json` @ v1.2.0
ILIADFILENAME = 'iliad.txt'
ILIADCONTENTS = '''Achilles sing, O Goddess! Peleus' son;
His wrath pernicious, who ten thousand woes
Caused to Achaia's host, sent many a soul
Illustrious into Ades premature,
And Heroes gave (so stood the will of Jove)
To dogs and to all ravening fowls a prey,
When fierce dispute had separated once
The noble Chief Achilles from the son
Of Atreus, Agamemnon, King of men.'''
MIDSUMMERNIGHTFILENAME = 'midsummer-night.txt'
MIDSUMMERNIGHTCONTENTS = '''I do entreat your grace to pardon me.
I know not by what power I am made bold,
Nor how it may concern my modesty,
In such a presence here to plead my thoughts;
But I beseech your grace that I may know
The worst that may befall me in this case,
If I refuse to wed Demetrius.'''
PARADISELOSTFILENAME = 'paradise-lost.txt'
PARADISELOSTCONTENTS = '''Of Mans First Disobedience, and the Fruit
Of that Forbidden Tree, whose mortal tast
Brought Death into the World, and all our woe,
With loss of Eden, till one greater Man
Restore us, and regain the blissful Seat,
Sing Heav'nly Muse, that on the secret top
Of Oreb, or of Sinai, didst inspire
That Shepherd, who first taught the chosen Seed'''
FILENAMES = [
ILIADFILENAME,
MIDSUMMERNIGHTFILENAME,
PARADISELOSTFILENAME,
]
FILES = {}
class File(object):
def __init__(self, name=''):
self.name = name
self.contents = ''
def read(self):
return self.contents
def readlines(self):
return [line + '\n' for line in self.read().split('\n') if line]
def write(self, data):
self.contents += data
def __enter__(self):
return self
def __exit__(self, *args):
pass
# Store builtin definition of open()
builtins.oldopen = builtins.open
def open(name, mode='r', *args, **kwargs):
# if name is a mocked file name, lookup corresponding mocked file
if name in FILENAMES:
if mode == 'w' or name not in FILES:
FILES[name] = File(name)
return FILES[name]
# else call builtin open()
else:
return builtins.oldopen(name, mode, *args, **kwargs)
builtins.open = open
# remove mocked file contents
def remove_file(file_name):
del FILES[file_name]
def create_file(name, contents):
with open(name, 'w') as f:
f.write(contents)
class GrepTest(unittest.TestCase):
@classmethod
def setUpClass(self):
# Override builtin open() with mock-file-enabled one
builtins.open = open
create_file(ILIADFILENAME, ILIADCONTENTS)
create_file(MIDSUMMERNIGHTFILENAME, MIDSUMMERNIGHTCONTENTS)
create_file(PARADISELOSTFILENAME, PARADISELOSTCONTENTS)
self.maxDiff = None
@classmethod
def tearDownClass(self):
remove_file(ILIADFILENAME)
remove_file(MIDSUMMERNIGHTFILENAME)
remove_file(PARADISELOSTFILENAME)
# Restore builtin open()
builtins.open = builtins.oldopen
def test_one_file_one_match_no_flags(self):
self.assertMultiLineEqual(
grep("Agamemnon", "", [ILIADFILENAME]),
"Of Atreus, Agamemnon, King of men.\n"
)
def test_one_file_one_match_print_line_numbers_flag(self):
self.assertMultiLineEqual(
grep("Forbidden", "-n", [PARADISELOSTFILENAME]),
"2:Of that Forbidden Tree, whose mortal tast\n"
)
def test_one_file_one_match_case_insensitive_flag(self):
self.assertMultiLineEqual(
grep("FORBIDDEN", "-i", [PARADISELOSTFILENAME]),
"Of that Forbidden Tree, whose mortal tast\n"
)
def test_one_file_one_match_print_file_names_flag(self):
self.assertMultiLineEqual(
grep("Forbidden", "-l", [PARADISELOSTFILENAME]),
PARADISELOSTFILENAME + '\n')
def test_one_file_one_match_match_entire_lines_flag(self):
self.assertMultiLineEqual(
grep("With loss of Eden, till one greater Man",
"-x", [PARADISELOSTFILENAME]),
"With loss of Eden, till one greater Man\n")
def test_one_file_one_match_multiple_flags(self):
self.assertMultiLineEqual(
grep("OF ATREUS, Agamemnon, KIng of MEN.",
"-n -i -x", [ILIADFILENAME]),
"9:Of Atreus, Agamemnon, King of men.\n")
def test_one_file_several_matches_no_flags(self):
self.assertMultiLineEqual(
grep("may", "", [MIDSUMMERNIGHTFILENAME]),
"Nor how it may concern my modesty,\n"
"But I beseech your grace that I may know\n"
"The worst that may befall me in this case,\n")
def test_one_file_several_matches_print_line_numbers_flag(self):
self.assertMultiLineEqual(
grep("may", "-n", [MIDSUMMERNIGHTFILENAME]),
"3:Nor how it may concern my modesty,\n"
"5:But I beseech your grace that I may know\n"
"6:The worst that may befall me in this case,\n")
def test_one_file_several_matches_match_entire_lines_flag(self):
self.assertMultiLineEqual(
grep("may", "-x", [MIDSUMMERNIGHTFILENAME]),
"")
def test_one_file_several_matches_case_insensitive_flag(self):
self.assertMultiLineEqual(
grep("ACHILLES", "-i", [ILIADFILENAME]),
"Achilles sing, O Goddess! Peleus' son;\n"
"The noble Chief Achilles from the son\n")
def test_one_file_several_matches_inverted_flag(self):
self.assertMultiLineEqual(
grep("Of", "-v", [PARADISELOSTFILENAME]),
"Brought Death into the World, and all our woe,\n"
"With loss of Eden, till one greater Man\n"
"Restore us, and regain the blissful Seat,\n"
"Sing Heav'nly Muse, that on the secret top\n"
"That Shepherd, who first taught the chosen Seed\n")
def test_one_file_one_match_file_flag_takes_precedence_over_line(self):
self.assertMultiLineEqual(
grep("ten", "-n -l", [ILIADFILENAME]),
ILIADFILENAME + '\n')
def test_one_file_no_matches_various_flags(self):
self.assertMultiLineEqual(
grep("Gandalf", "-n -l -x -i", [ILIADFILENAME]),
"")
def test_multiple_files_one_match_no_flags(self):
self.assertMultiLineEqual(
grep("Agamemnon", "", FILENAMES),
"iliad.txt:Of Atreus, Agamemnon, King of men.\n")
def test_multiple_files_several_matches_no_flags(self):
self.assertMultiLineEqual(
grep("may", "", FILENAMES),
"midsummer-night.txt:Nor how it may concern my modesty,\n"
"midsummer-night.txt:But I beseech your grace that I may know\n"
"midsummer-night.txt:The worst that may befall me in this case,\n")
def test_multiple_files_several_matches_print_line_numbers_flag(self):
expected = (
"midsummer-night.txt:5:But I beseech your grace that I may know\n"
"midsummer-night.txt:6:The worst that may befall me in this case,"
"\nparadise-lost.txt:2:Of that Forbidden Tree, whose mortal tast\n"
"paradise-lost.txt:6:Sing Heav'nly Muse, that on the secret top\n")
self.assertMultiLineEqual(grep("that", "-n", FILENAMES), expected)
def test_multiple_files_one_match_print_file_names_flag(self):
self.assertMultiLineEqual(
grep("who", "-l", FILENAMES),
ILIADFILENAME + '\n' + PARADISELOSTFILENAME + '\n')
def test_multiple_files_several_matches_case_insensitive_flag(self):
expected = (
"iliad.txt:Caused to Achaia's host, sent many a soul\n"
"iliad.txt:Illustrious into Ades premature,\n"
"iliad.txt:And Heroes gave (so stood the will of Jove)\n"
"iliad.txt:To dogs and to all ravening fowls a prey,\n"
"midsummer-night.txt:I do entreat your grace to pardon me.\n"
"midsummer-night.txt:In such a presence here to plead my thoughts;"
"\nmidsummer-night.txt:If I refuse to wed Demetrius.\n"
"paradise-lost.txt:Brought Death into the World, and all our woe,"
"\nparadise-lost.txt:Restore us, and regain the blissful Seat,\n"
"paradise-lost.txt:Sing Heav'nly Muse, that on the secret top\n")
self.assertMultiLineEqual(grep("TO", "-i", FILENAMES), expected)
def test_multiple_files_several_matches_inverted_flag(self):
self.assertMultiLineEqual(
grep("a", "-v", FILENAMES),
"iliad.txt:Achilles sing, O Goddess! Peleus' son;\n"
"iliad.txt:The noble Chief Achilles from the son\n"
"midsummer-night.txt:If I refuse to wed Demetrius.\n"
)
def test_multiple_files_one_match_match_entire_lines_flag(self):
self.assertMultiLineEqual(
grep("But I beseech your grace that I may know", "-x", FILENAMES),
"midsummer-night.txt:But I beseech your grace that I may know\n")
def test_multiple_files_one_match_multiple_flags(self):
self.assertMultiLineEqual(
grep("WITH LOSS OF EDEN, TILL ONE GREATER MAN", "-n -i -x",
FILENAMES),
"paradise-lost.txt:4:With loss of Eden, till one greater Man\n")
def test_multiple_files_no_matches_various_flags(self):
self.assertMultiLineEqual(
grep("Frodo", "-n -l -x -i", FILENAMES),
""
)
def test_multiple_files_several_matches_file_flag_takes_precedence(self):
self.assertMultiLineEqual(
grep("who", "-n -l", FILENAMES),
ILIADFILENAME + '\n' + PARADISELOSTFILENAME + '\n')
def test_multiple_files_several_matches_inverted_match_entire_lines(self):
expected = (
"iliad.txt:Achilles sing, O Goddess! Peleus' son;\n"
"iliad.txt:His wrath pernicious, who ten thousand woes\n"
"iliad.txt:Caused to Achaia's host, sent many a soul\n"
"iliad.txt:And Heroes gave (so stood the will of Jove)\n"
"iliad.txt:To dogs and to all ravening fowls a prey,\n"
"iliad.txt:When fierce dispute had separated once\n"
"iliad.txt:The noble Chief Achilles from the son\n"
"iliad.txt:Of Atreus, Agamemnon, King of men.\n"
"midsummer-night.txt:I do entreat your grace to pardon me.\n"
"midsummer-night.txt:I know not by what power I am made bold,\n"
"midsummer-night.txt:Nor how it may concern my modesty,\n"
"midsummer-night.txt:In such a presence here to plead my thoughts;"
"\nmidsummer-night.txt:But I beseech your grace that I may know\n"
"midsummer-night.txt:The worst that may befall me in this case,\n"
"midsummer-night.txt:If I refuse to wed Demetrius.\n"
"paradise-lost.txt:Of Mans First Disobedience, and the Fruit\n"
"paradise-lost.txt:Of that Forbidden Tree, whose mortal tast\n"
"paradise-lost.txt:Brought Death into the World, and all our woe,"
"\nparadise-lost.txt:With loss of Eden, till one greater Man\n"
"paradise-lost.txt:Restore us, and regain the blissful Seat,\n"
"paradise-lost.txt:Sing Heav'nly Muse, that on the secret top\n"
"paradise-lost.txt:Of Oreb, or of Sinai, didst inspire\n"
"paradise-lost.txt:That Shepherd, who first taught the chosen Seed"
"\n"
)
self.assertMultiLineEqual(
grep("Illustrious into Ades premature,", "-x -v", FILENAMES),
expected
)
if __name__ == '__main__':
unittest.main()

View File

@@ -29,13 +29,13 @@ class RestAPI(object):
def post(self, url, payload=None):
if not payload:
raise("No payload for URL \'{}\'".foramt(url))
raise("No payload for URL \'{}\'".format(url))
elif url == '/add':
return self.add(payload) or json.dumps('?')
elif url == '/iou':
return self.iou(payload) or json.dumps('?')
else:
raise ValueError("Invalid URL \'{}\'".foramt(url))
raise ValueError("Invalid URL \'{}\'".format(url))
def get_user(self, name):
found = [user for user in self.database['users'] if user['name'] == name]
@@ -43,7 +43,6 @@ class RestAPI(object):
return found[0]
else:
raise ValueError('User \'{}\' does not exist.'.format(name))
return False
def set_user(self, name, data):
found = [(index, user) for index, user in enumerate(self.database['users']) if user['name'] == name]
@@ -53,7 +52,6 @@ class RestAPI(object):
print(self.database['users'][found[0][0]])
else:
raise ValueError('User \'{}\' does not exist.'.format(name))
return False
# Add a user to the database
def add(self, payload):

86
track_comments.py Normal file
View File

@@ -0,0 +1,86 @@
import requests
import pprint
import datetime
import bs4
import re
import os
import sys
import time
# A basic function for handling responses from the request module, as well as handling timing and data sizes of the web pages downloaded.
def simpleReq(url):
start = time.time()
data = requests.get(url)
if data.status_code != 200:
raise ConnectionError(f'A status code other than 200 was received. ({data.status_code} @ {url})')
end = time.time()
request_timings.append((end - start, len(data.text.encode('utf-16-le'))))
return data.text
def parseComment(name, url, soup):
comment = soup.find(get_reflection)
if comment['class'] == ['reflection']:
return descend(comment)[3].text
return ''
# This script works on the assumption that you have a setup similar to mine.
# I have a GitHub repository linked so that on any computer I can have my Exercism progress kept in one place.
# This script may break if you don't have a track folder available (i.e you have a `java` track started but no folder on the computer available.)
# Constants & Reused Lambdas
username = 'Xevion' # CaSe SeNsItIvE Username from `Exercism.io`.
t1 = time.time()
request_timings = []
descend = lambda thing : list(thing.children)
get_solutions = lambda tag : ['solution'] == tag['class'] if tag.has_attr('class') else False
get_reflection = lambda tag : any(['reflection' in classtag for classtag in tag['class']]) if tag.has_attr('class') else False
get_url = lambda url : re.findall(r'exercism.io\/tracks\/([a-z-]+)\/exercises\/', url)[0]
get_name_from_url = lambda url : re.search(r'exercism.io\/tracks\/[a-z-]+\/exercises\/([a-z-]+)\/solutions\/', url).group(1)
pp = pprint.PrettyPrinter()
print('Requesting Profile Page Data')
data = simpleReq('https://exercism.io/profiles/{}'.format(username))
soup = bs4.BeautifulSoup(data, 'html.parser')
comments_top = """# {0} Track Comments\n\nThis page represents all my comments from my solutions currently hosted on [Exercism.io](https://exercism.io/). You can view my profile [here](https://exercism.io/profiles/Xevion).
The reason for this is simply to have a place where I can collect my comments, as well as just have some fun with Python and webscraping. Exercise file and exercise submission links will be provided for each and every exercise.
This file is for the **{0}** track, contains **{1}** submissions, **{2}** of which have comments. This file was built {3}.\n\n"""
# Find all completed exercises, find the URL to the Solution by the user and create for parsing.
# Also creates dictionary of all available tracks
solutions = soup.find_all(get_solutions)
solutions = [(descend(descend(solution)[2])[1].text, 'https://exercism.io{}'.format(descend(solution.parent)[1]['href'])) for solution in solutions]
tracks = {k : list() for k in dict.fromkeys(list(map(lambda i : get_url(i[1]), solutions)))}
# Get all comment data & parse, then put into track dictionary
print('Requesting Page Data for {} solution{} from {} {}'.format(len(solutions), 's' if len(solutions) > 1 else '', len(tracks.keys()), 'different tracks' if abs(len(tracks.keys())) != 1 else 'track'))
solutions = [(solution[0], solution[1], bs4.BeautifulSoup(simpleReq(solution[1]), 'html.parser')) for solution in solutions]
solutions = [{'name' : solution[0], 'url' : solution[1], 'comment' : parseComment(*solution)} for solution in solutions]
# Send all the solutions to their appropriate tracks.
for solution in solutions:
track = get_url(solution['url'])
tracks[track].append(solution)
# Parse into a readable markdown format
print('Parsing all solution comments')
for track in tracks.keys():
# Getting the path and formatting the top portion of the markdown file.
path = os.path.join(sys.path[0], track, 'COMMENTS.md')
submission_comments = len(list(filter(lambda item : item['comment'] != '', tracks[track])))
top = comments_top.format(track.title(), len(tracks[track]), submission_comments, datetime.datetime.utcnow().strftime('on **%d-%m-%Y** at **%H:%M:%S UTC**'))
# Adding all the comments with proper formatting and links.
markdown_comments = []
for submission in tracks[track]:
true_name = get_name_from_url(submission['url'])
file_url = './{}/{}'.format(true_name, true_name.replace('-', '_') + '.py')
comment = "## {}\n\n[Link to File]({}) | [Link to Submission]({})\n\n{}".format(submission['name'], file_url, submission['url'], submission['comment'])
markdown_comments.append(comment)
# Join into a single string and then write it into a file.
markdown = top + '\n\n'.join(markdown_comments)
with open(path, 'w+') as file:
file.write(markdown)
print('Wrote {} KiB file for {} track'.format(round(os.path.getsize(path) / 1024, 2), track))
t2 = time.time()
# Sorry for this ridiculously long line. ;-;
print('Downloaded {} MiB in webpages.\nDownloaded & Parsed in {} seconds with {}ms on average request time.'.format(round(sum([i[1] for i in request_timings]) / (1024 ** 2), 2), round(t2 - t1, 2), round((sum(i[0] for i in request_timings) / len(request_timings)) * 1000, 2)))