From e9d152560dcd807fa76390942a0e38b362e0ec89 Mon Sep 17 00:00:00 2001 From: mjfernez Date: Wed, 9 Jun 2021 18:56:20 -0400 Subject: Force UTF-8 encoding --- server.py | 4 ++-- templates/base.html | 4 +++- templates/site/tutorials/python/py-style.html | 31 +++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 templates/site/tutorials/python/py-style.html diff --git a/server.py b/server.py index 692fe8c..e2e93f0 100644 --- a/server.py +++ b/server.py @@ -119,8 +119,8 @@ def render_file(path): ) if os.path.isfile(abs_path): if abs_path.endswith('.html'): - with open(abs_path) as f: - content = f.read() + with open(abs_path, 'rb') as f: + content = f.read().decode("UTF-8") return render_template_string(CONTENT_BLOCK.replace('$', content), **context) elif abs_path.endswith('.html!'): return render_template("site/" + path, **context) diff --git a/templates/base.html b/templates/base.html index b2e8e74..5c4e189 100644 --- a/templates/base.html +++ b/templates/base.html @@ -1,6 +1,8 @@ - + + {{ domain }}/{{ title }} + {% block css%} {% endblock %} diff --git a/templates/site/tutorials/python/py-style.html b/templates/site/tutorials/python/py-style.html new file mode 100644 index 0000000..d766e39 --- /dev/null +++ b/templates/site/tutorials/python/py-style.html @@ -0,0 +1,31 @@ +

Coding Style Guide

+

The purpose of this document is twofold: 1) To ensure that anyone who might like to make my code better understands why I write python the way I do 2) to ensure I adhere to my own style because I’m terribly inconsistent

+

Being terribly inconsistent, the guidelines are not set in stone and if you have a good argument for doing things a particular, I don’t really care.

+

BUT first and foremost, code must comply with PEP8 first. This is easy to automate. I like coala since it’s friendly but there’ plenty of advanced linters out there.

+

That aside, I have the following idiosyncracies:

+

1) Strings are double-quoted. Keys and chars are single-quoted.

+

This is really just because I like how C does it. And Cpython’s C-based so why not?

+

Like so: string = "This is a phrase" word = "word" cur_char = 'a' newline = '\n' # note, two characters, but it's still ONE char in output # keys are single-quoted to avoid confusion dictionary = { 'key' : "1245dqw3w431", 'return': newline }

+

The only exception is for strings with quotes in them (anything to avoid escapes, really) quoted_string = ( '"You miss 100% of the shots you don't take - Wayne Gretsky" - Michael Scott' ) That brings me to my next point.

+

2) Long strings belong in parentheses

+

As in: ``` longboi = ( “This is a really long string usefull when making help menus. Be” “sure to leave s space at the end of each line, or add a new line” “when needed.”

+
    "Try your best to keep formatting accurate like this."
+)
+```
+

3) Tabs are four spaces and spaces are ALWAYS prefered to tabs

+

Again, see PEP8.

+

4) Always add spaces between arithmetic, but never for brackets

+

It’s a pain to read: 1/(2*sqrt(pi))*exp(x**2) Do this 1 / (2 * sqrt(pi)) * exp(x ** 2) The same goes for logic operators true & false ^ true

+

5) EVERYTHING should be snake_case

+

This is python. Unless there’s a compatibility thing (like a library’s code was written that way, or it matches an API variable), snake_case is preferred. ```

+
user_input = int(input()) # variable
+MAX_INPUT = 1000 # constant
+def judge_input(_input, _max): # function
+    if _max > _input:
+        print("Too big!")
+
+judge_input(user_input, MAX_INPUT
+class Input_Judger: # a class
+    # etc etc
+```
+

Example exception # this doesn't actually work, but you get the idea r = requests.get("www.debian.org") pageSize = r.json()['pageSize'] # camel case ok

-- cgit v1.2.3