mirror of
https://github.com/satwikkansal/wtfpython
synced 2025-07-04 04:18:00 +02:00
A bit of code clean up
This commit is contained in:
parent
502769882a
commit
65a0811e1d
5 changed files with 11 additions and 7 deletions
152
irrelevant/add_categories
vendored
Normal file
152
irrelevant/add_categories
vendored
Normal file
|
@ -0,0 +1,152 @@
|
|||
Skipping lines?
|
||||
a
|
||||
|
||||
Well, something is fishy...
|
||||
a
|
||||
|
||||
Time for some hash brownies!
|
||||
f
|
||||
|
||||
Evaluation time discrepancy
|
||||
f
|
||||
|
||||
Modifying a dictionary while iterating over it
|
||||
c
|
||||
|
||||
Deleting a list item while iterating over it
|
||||
c
|
||||
|
||||
Backslashes at the end of string
|
||||
f
|
||||
|
||||
Brace yourself!
|
||||
t*
|
||||
|
||||
"this" is love :heart:
|
||||
t*
|
||||
|
||||
Okay Python, Can you make me fly?
|
||||
t*
|
||||
|
||||
`goto`, but why?
|
||||
t*
|
||||
|
||||
Let's meet Friendly Language Uncle For Life
|
||||
t*
|
||||
|
||||
Inpinity
|
||||
t*
|
||||
|
||||
Strings can be tricky sometimes
|
||||
f*
|
||||
|
||||
`+=` is faster
|
||||
m
|
||||
|
||||
Let's make a giant string!
|
||||
m
|
||||
|
||||
Yes, it exists!
|
||||
t
|
||||
|
||||
`is` is not what it is!
|
||||
f
|
||||
|
||||
`is not ...` is not `is (not ...)`
|
||||
f
|
||||
|
||||
The function inside loop sticks to the same output
|
||||
f
|
||||
|
||||
Loop variables leaking out of local scope!
|
||||
c
|
||||
|
||||
A tic-tac-toe where X wins in the first attempt!
|
||||
f
|
||||
|
||||
Beware of default mutable arguments!
|
||||
c
|
||||
|
||||
Same operands, different story!
|
||||
c
|
||||
|
||||
Mutating the immutable!
|
||||
f
|
||||
|
||||
Using a variable not defined in scope
|
||||
c
|
||||
|
||||
The disappearing variable from outer scope
|
||||
f
|
||||
|
||||
Return return everywhere!
|
||||
f
|
||||
|
||||
When True is actually False
|
||||
f
|
||||
|
||||
Be careful with chained operations
|
||||
c
|
||||
|
||||
Name resolution ignoring class scope
|
||||
c
|
||||
|
||||
From filled to None in one instruction...
|
||||
f
|
||||
|
||||
Explicit typecast of strings
|
||||
m
|
||||
|
||||
Class attributes and instance attributes
|
||||
f
|
||||
|
||||
Catching the Exceptions!
|
||||
f
|
||||
|
||||
Midnight time doesn't exist?
|
||||
f
|
||||
|
||||
What's wrong with booleans?
|
||||
f
|
||||
|
||||
Needle in a Haystack
|
||||
c
|
||||
|
||||
Teleportation
|
||||
a*
|
||||
|
||||
yielding None
|
||||
f
|
||||
|
||||
The surprising comma
|
||||
f
|
||||
|
||||
For what?
|
||||
f
|
||||
|
||||
not knot!
|
||||
f
|
||||
|
||||
Subclass relationships
|
||||
f*
|
||||
|
||||
Mangling time!
|
||||
t*
|
||||
|
||||
Deep down, we're all the same.
|
||||
f*
|
||||
|
||||
Half triple-quoted strings
|
||||
f
|
||||
|
||||
Implicity key type conversion
|
||||
f*
|
||||
|
||||
Stubborn `del` operator
|
||||
c*
|
||||
|
||||
Let's see if you can guess this?
|
||||
f
|
||||
|
||||
Minor Ones
|
||||
m
|
51
irrelevant/generate_contributions.py
Normal file
51
irrelevant/generate_contributions.py
Normal file
|
@ -0,0 +1,51 @@
|
|||
"""
|
||||
Parses the README.md and generated the table
|
||||
`CONTRIBUTORS.md`.
|
||||
"""
|
||||
|
||||
import pprint
|
||||
import re
|
||||
import requests
|
||||
|
||||
regex = ("[sS]uggested by @(\S+) in \[this\]\(https:\/\/github\.com\/satwikkansal"
|
||||
"\/wtf[pP]ython\/issues\/(\d+)\) issue")
|
||||
|
||||
|
||||
fname = "README.md"
|
||||
contribs = {}
|
||||
|
||||
table_header = """
|
||||
| Contributor | Github | Issues |
|
||||
|-------------|--------|--------|
|
||||
"""
|
||||
|
||||
table_row = '| {} | [{}](https://github.com/{}) | {} |'
|
||||
issue_format = '[#{}](https:/github.com/satwikkansal/wtfpython/issues/{})'
|
||||
rows_so_far = []
|
||||
|
||||
github_rest_api = "https://api.github.com/users/{}"
|
||||
|
||||
|
||||
with open(fname, 'r') as f:
|
||||
file_content = f.read()
|
||||
matches = re.findall(regex, file_content)
|
||||
for match in matches:
|
||||
if contribs.get(match[0]) and match[1] not in contribs[match[0]]:
|
||||
contribs[match[0]].append(match[1])
|
||||
else:
|
||||
contribs[match[0]] = [match[1]]
|
||||
|
||||
for handle, issues in contribs.items():
|
||||
issue_string = ', '.join([issue_format.format(i, i) for i in issues])
|
||||
resp = requests.get(github_rest_api.format(handle))
|
||||
name = handle
|
||||
if resp.status_code is 200:
|
||||
pprint.pprint(resp.json()['name'])
|
||||
else:
|
||||
print(handle, resp.content)
|
||||
rows_so_far.append(table_row.format(name,
|
||||
handle,
|
||||
handle,
|
||||
issue_string))
|
||||
|
||||
print(table_header + "\n".join(rows_so_far))
|
0
irrelevant/generated.md
vendored
Normal file
0
irrelevant/generated.md
vendored
Normal file
2364
irrelevant/initial.md
vendored
Normal file
2364
irrelevant/initial.md
vendored
Normal file
File diff suppressed because it is too large
Load diff
144
irrelevant/parse_readme.py
Normal file
144
irrelevant/parse_readme.py
Normal file
|
@ -0,0 +1,144 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
This inefficient module would parse the README.md in the initial version of
|
||||
WTFPython, and enabl me to categorize and reorder a hell lot of examples with
|
||||
the help of the file `add_categories` (part of which is automatically
|
||||
generated).
|
||||
|
||||
After the refactor, this module would not work now with necessary updates in
|
||||
the code.
|
||||
"""
|
||||
|
||||
fname = "README.md"
|
||||
snippets = []
|
||||
|
||||
with open(fname, 'r') as f:
|
||||
lines = iter(f.readlines())
|
||||
line = lines.next()
|
||||
|
||||
try:
|
||||
while True:
|
||||
# check if it's a H3
|
||||
if line.startswith("### "):
|
||||
title = line.replace("### ", "")
|
||||
description = []
|
||||
next_line = lines.next()
|
||||
|
||||
# store lines till an H4 (explanation) is encountered
|
||||
while not next_line.startswith("#### "):
|
||||
description.append(next_line)
|
||||
next_line = lines.next()
|
||||
|
||||
explanation = []
|
||||
# store lines again until --- or another H3 is encountered
|
||||
while not (next_line.startswith("---") or
|
||||
next_line.startswith("### ")):
|
||||
explanation.append(next_line)
|
||||
next_line = lines.next()
|
||||
|
||||
# Store the results finally
|
||||
snippets.append({
|
||||
"title": title,
|
||||
"description": '\n'.join(description),
|
||||
"explanation": '\n'.join(explanation)
|
||||
})
|
||||
|
||||
line = next_line
|
||||
|
||||
else:
|
||||
line = lines.next()
|
||||
|
||||
except StopIteration:
|
||||
snippets.append({
|
||||
"title": title,
|
||||
"description": '\n'.join(description),
|
||||
"explanation": '\n'.join(explanation)
|
||||
})
|
||||
|
||||
'''
|
||||
# Create a file
|
||||
file_content = "\n\n".join([snip["title"] for snip in snippets])
|
||||
|
||||
with open("add_categories", "w") as f:
|
||||
f.write(file_content)
|
||||
'''
|
||||
|
||||
snips_by_title = {}
|
||||
|
||||
with open("add_categories", "r") as f:
|
||||
content = iter(f.readlines())
|
||||
try:
|
||||
while True:
|
||||
title = content.next()
|
||||
cat = content.next().strip()
|
||||
is_new = True if cat[-1]=="*" else False
|
||||
cat = cat.replace('*','')
|
||||
snips_by_title[title] = {
|
||||
"category": cat,
|
||||
"is_new": is_new
|
||||
}
|
||||
content.next()
|
||||
except StopIteration:
|
||||
|
||||
for idx, snip in enumerate(snippets):
|
||||
snippets[idx]["category"] = snips_by_title[snip["title"]]["category"]
|
||||
snippets[idx]["is_new"] = snips_by_title[snip["title"]]["is_new"]
|
||||
|
||||
|
||||
snips_by_cat = {}
|
||||
for snip in snippets:
|
||||
cat = snip["category"]
|
||||
if not snips_by_cat.get(cat):
|
||||
snips_by_cat[cat] = []
|
||||
snips_by_cat[cat].append(snip)
|
||||
|
||||
snippet_template = """
|
||||
|
||||
### ▶ {title}{is_new}
|
||||
|
||||
{description}
|
||||
|
||||
{explanation}
|
||||
|
||||
---
|
||||
"""
|
||||
|
||||
category_template = """
|
||||
---
|
||||
|
||||
## {category}
|
||||
|
||||
{content}
|
||||
"""
|
||||
|
||||
result = ""
|
||||
|
||||
category_names = {
|
||||
"a": "Appearances are Deceptive!",
|
||||
"t": "The Hiddent treasures",
|
||||
"f": "Strain your Brain",
|
||||
"c": "Be careful of these",
|
||||
"m": "Miscallaneous"
|
||||
}
|
||||
|
||||
categories_in_order = ["a", "t", "f", "c", "m"]
|
||||
|
||||
for category in categories_in_order:
|
||||
snips = snips_by_cat[category]
|
||||
for i, snip in enumerate(snips):
|
||||
print(i, ":", snip["title"])
|
||||
content = ""
|
||||
for _ in snips:
|
||||
snip = snips[int(raw_input())]
|
||||
is_new = " *" if snip["is_new"] else ""
|
||||
content += snippet_template.format(title=snip["title"].strip(),
|
||||
is_new=is_new,
|
||||
description=snip["description"].strip().replace("\n\n", "\n"),
|
||||
explanation=snip["explanation"].strip().replace("\n\n", "\n"))
|
||||
result += category_template.format(category=category_names[category], content=content.replace("\n\n\n", "\n\n"))
|
||||
|
||||
with open("generated.md", "w") as f:
|
||||
f.write(result.replace("\n\n\n", "\n\n"))
|
||||
|
||||
print("Done!")
|
Loading…
Add table
Add a link
Reference in a new issue