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

Small blogging script

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

Problem

I've recently re-written a Python script I use to run a couple of lightweight blogs. Looking over the horrible code I'd written before, I decided to rewrite it using object-oriented concepts. I wanted to submit it to get feedback, best practices, and other areas I should look at. This is my first script that uses any object oriented ideas.

```
import markdown
import jinja2
import re
import datetime
import time
import glob
import cgi

class Article:
def __init__(self, local_dir, local_file):
local_file = local_file.replace('/','')
self.local_file_name = local_file
with open(local_dir + '/' + local_file) as f:
self.lines = f.readlines()
self.file_text = ''.join(self.lines[4:])

def text(self):
return self.file_text

def title(self):
title = self.get_metadata(self.lines[0])
return title

def html_filename(self):
html_filename = re.sub('.txt','.html',self.local_file_name)
return html_filename

def date_text(self):
date_text = self.get_metadata(self.lines[2])
return date_text

def date_datetime(self):
date_txt = self.date_text()
date_obj = datetime.datetime.strptime(date_txt, '%d %B %Y')
return date_obj

def date_rss(self):
date = time.strptime(self.date_text(), '%d %B %Y')
rss_date = time.strftime('%a, %d %b %Y 06:%M:%S +0000', date)
return rss_date

def summary(self):
summary = re.sub('','', self.html())[0:200]
summary = re.sub('\n',' ',summary)
return summary

def html(self):
md = markdown.Markdown()
converted_text = md.convert(self.file_text).encode('utf-8')
return converted_text

def get_metadata(self,line):
element = re.sub('\n|Author: |Date: |Title: ','',line)
element = cgi.escape(element).strip()
return element

class FileList:
def __init__(self, dir, ignore_list):
self.textfiles = glob

Solution

Overall your design looks good to me. A couple of relatively minor things to note:

Use new-style classes

In Python 2.x you should make sure your classes inherit from object. Such classes are known as new-style classes. (In Python 3.x all classes are new-style classes.) New-style classes offer:

  • support for the built-in function super() to refer to the base class (or other parent class) without hard-coding; more info: http://rhettinger.wordpress.com/2011/05/26/super-considered-super/



  • improved method resolution order (MRO); more info: http://python-history.blogspot.com/2010/06/method-resolution-order.html



  • support for descriptors: methods that behave like attributes; more info: http://docs.python.org/2/reference/datamodel.html#invoking-descriptors



Use the standard-library to make your code more portable

When building up paths you may do so in a cross-platform way by using os.path.join. This is just one example, but there are other places in your code where you can use this:

import os
file_path = os.path.join( local_dir, local_file )
with open(file_path) as f:
    ...


Miscellaneous

There is a comment which doesn't agree with the code.

'DIR': '/dir/to/your/html/files/', # no final slash

Code Snippets

import os
file_path = os.path.join( local_dir, local_file )
with open(file_path) as f:
    ...
'DIR': '/dir/to/your/html/files/', # no final slash

Context

StackExchange Code Review Q#24250, answer score: 4

Revisions (0)

No revisions yet.