TIL: 11-18-22

I came across an issue today with the generator I've written. If I write multiple posts in a day, the RSS feed may not pick up the newest post, because all the original post dates were set to today's date, but a time of 00:00:00 CST. So if it doesn't sort it properly, a feed reader may not pick up the newest post of the day because it could get pushed to the bottom of the list of today's posts instead of the top.

To fix that, I introduced datetimes to my "create a post" script, and then had to manipulate that datetime string to do what I needed in the creation of the data structures for the site. E.g., I wanted only the date (not the time) to show up on the index page, but I want them in order of writing in reverse. I also wanted the feed to be in reverse order of writing so that even if I post multiple times a day, feed readers will pick up the newest post.

The problem is, I have multiple functions doing the work of creating posts, manipulating data, creating the index page vs. the RSS feed vs. actual posts. But there is a single reference list of dictionaries I'm passing into these functions. In the script, my "makeindex" function happens upstream of the "makerss" function. And even though the index function was getting the right order, somehow, after I was passing in and manipulating the data in the function, the original list was getting manipulated even though I assigned it to a new variable.

Apparently, this is a feature/bug in Python (I'm sure someone likes that this happens in Python... I didn't experience that particular joy today). For example, if I have a list of lists called "posts" and then say postscopy = posts, if I manipulate values within the postscopy list, any values I change there will also change the values in the reference list. That's bonkers, but whatever. After a couple of hours of cursing the void, I found my answer here. Not only do you need to assign the values to a new variable, you have to make a "deepcopy" of the original list and assign it (if the list in question has other data structures inside it). So instead of

`

posts_copy = copy

`

I needed to do:

`

from copy import deepcopy

posts_copy = deepcopy(posts)

`

From there, I was able to manipulate values in the function without worrying about overwriting data in the original list.

I like programming, but that was maddening.

Tagged: TIL, python,