Last Day of the Month
When a blog has never been visited my dissertation software retrieves any posts from the past 24 hours. This isn’t hard:
from datetime import datetime lastvisit = datetime.utcnow().replace(day=(datetime.utcnow().day-1))
But when this happens and it’s the first day of the month you get:
ValueError: day is out of range for month
Since my project is only going to run for this summer, I did changed it so that it will work this year but not next year (sorry, WordPress strips out all of the tabs…):
from datetime import datetime
n = datetime.utcnow()
if n.day != 1:
lastvisit = datetime.utcnow().replace(day=(datetime.utcnow().day-1))
elif n.month - 1 in (1, 3, 5, 7, 8, 10, 12):
lastvisit = datetime.utcnow().replace(month=(n.month-1), day=31)
elif n.month - 1 in (4, 6, 9, 11):
lastvisit = datetime.utcnow().replace(month=(n.month-1), day=30)
elif n.month - 1 == 2:
try:
lastvisit = datetime.utcnow().replace(month=(n.month-1), day=29)
except ValueError:
lastvisit = datetime.utcnow().replace(month=(n.month-1), day=2
Today I ran across this article on ASPN. And now here’s what I would write (omitting some of the boilerplate from above):
if n.day == 1:
days_in_month = [calendar.monthrange(year,month)[1] for year in [n.year] for month in range(1,13)]
datetime.utcnow().replace(day=days_in_month[n.month-2], month=n.month-1)
elif:
datetime.utcnow().replace(day=n.day-1)
If I would have spent 5 minutes looking for The Right WayTM to do this, I would have saved a little time and had cleaner code. Now I don’t want to change what I’ve got working well enough.
Dear Lazyweb: How do I post pretty code snippets?
http://ethancodes.com/entry/10
Nice. But I can’t do any CSS on this blog, as it’s hosted on WordPress.com… I think that means it’s time to move it to my own space….
Baloney: http://codex.wordpress.org/Writing_Code_in_Your_Posts
Fooey.
Poppy cock.
Hogwash.
I tried using pre and code tags, but WordPress still munges things up pretty good. And they aren’t exactly what I’m looking for.
I ran across a snippet of code a while back that generated syntax-highlighted html versions of code listings… I can’t seem to find it anymore.
Found this: http://wordpress.org/extend/plugins/wp-syntax/
Using here: http://ethancodes.com/?p=4
And here: http://ethancodes.com/?p=5
Thanks. I haven’t figured out how to add plugins to a WP.com hosted blog. It’s annoying, but that’s how it goes… I’m trying to free up enough cognitive bandwidth to move this to my own server or write up a simple custom rails app to blog on… (overkill, I know).