HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Markdown static blogger program

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
markdownprogramstaticblogger

Problem

I was hoping I might get some of the brains in Stack Overflow to take a look at my Python static blogger application. I've been using it a few years in a pretty hacked up form. Lately I decided to clean it up and put it up on Github. I'd love some smarter Python programmers to give me advice and wisdom into ways to improve, optimize, and simplify the code.

The program is here: https://github.com/mshea/Pueblo

Some philosophies:

-
I don't want more features. I want it as simple as it can be.

-
I always prefer native modules. My ISP doesn't let me install new modules so the Markdown one is the only one I use outside of the defaults.

-
I'd like to keep it to a single script unless splitting it up makes things much easier or simple.

-
I'm particularly interested in any potential security concerns. Right now I don't see any.

-
I'm not big into flexibility. I'd prefer it to do it one way really well rather than many ways poorly. If people want a flexible blogging platform, go with WordPress.

```
#!/usr/local/bin/python
#
# Pueblo: Python Markdown Static Blogger
#
# 17 December 2011
#
# A single Python script to build a simple blog from a directory full of markdown files.
#
# This script requires the Markdown python implementation available at:
# http://pypi.python.org/pypi/Markdown/2.1.0
#
# This script requires markdown files using the following multimarkdown metadata as the first three lines
# of the processed .txt markdown files as follows:
#
# Title: the Title of your Document
# Author: Joe Blow
# Date: 15 December 2011
#
# The program will generate an index.html homepage file, an archive.html archive file,
# and an index.xml RSS file.
#
# Header and footer data can be edited in the variables throughout the program.
#
# This script expects the following additional files:
# style.css: The main site's stylesheet.
# iphone.css: The mobile version of the site's stylesheet.
# sidebar.html: A secondary set of data usually displayed as a sidebar.
#
# Instru

Solution

Here are a few suggestions:

String comparisons should be done with == (and !=) instead of is (and is not). is works, but implies you are comparing identity (which is usually a memory address), whereas == is comparing value. See https://stackoverflow.com/a/2988117/331473 for more information.

Your boolean configs (eg: minify_html) should be actual boolean values True/False instead of 1/0. Also, when you are checking for these, you should drop the comparison. Example:

if minify_html == 1:     # or minify_html == True 
   ...                   #    (if you've converted these to booleans)


can be written as:

if minify_html:
    ...


Using module level vars as configs is generally ok for a few things, but once you've got a whole catalog of things, it can be a bit much to keep track of. Several times while reviewing your code I found myself saying "Where did that var come from?".

If you want to address this, you could put these in a dictionary so it's "namespaced" so to speak. Example:

config = {
    "site_url": "...",
    "site_name": "..."
}


then in your code, you can more easily spot config bits:

if config['minify_html']:
    ....


There's more cleanup that can be done, but these are just the first few things that popped out at me. One more thing... I don't have time address it now, but the long chain of if/elses in buildhtmlheader could be refactored a bit to make things a little less redundant.

Code Snippets

if minify_html == 1:     # or minify_html == True 
   ...                   #    (if you've converted these to booleans)
if minify_html:
    ...
config = {
    "site_url": "...",
    "site_name": "..."
}
if config['minify_html']:
    ....

Context

StackExchange Code Review Q#6923, answer score: 8

Revisions (0)

No revisions yet.