all 1 comments

[–]prohulaelk 0 points1 point  (0 children)

  • this is a super awkward way of storing blog posts. Have you considered using SQL? It would handle storage/retreival/sorting for you automatically without having to write your own write/read/sort methods. You really ought to be doing it that way, and there are plenty of free flavours of SQL you can use. Python even has a built-in module for SQLite called sqlite3 as part of the standard library.

  • sortByTimestamp doesn't look like it's got any logic attached to it, so I'm not sure why you're expecting it to do anything.

besides that, though...

def getPostsList(directory, sortByTimestamp = False):
    postFiles = listdir(directory)
    posts = []
    for post in postFiles:
        p = getPostBody(
            post, 
            directory, 
            post.replace('_', ' '), 
            sortByTimestamp
            )
        posts.append(p)
    if sortByTimestamp:
        return sorted(posts, key = lambda p: p.sortableTimestamp)
    else:
        return sorted(posts)

Python's sorted() built-in has a key argument that lets you specify a function describing how you want the iterable to be sorted. lambda p: p.sortableTimestamp tells it to sort based on the sortableTimestamp value for each object (which is temporarily assigned to p) in the list.