This course is challenging us to do exactly what I'm doing here, to blog about our experience with the course. And exactly that: we're not here to blog about computer science, as we're not qualified. We're here to write about what we are qualified to write about. And so while we happily go about ourselves, writing about our own personal experiences with our own personal introductions to the world, at some point it occurs to us that the introductory-computer-science-experience blog is kinda niche, as far as blogs go. Who reads stuff like that? (Who's my audience? I care about that.) People who've always wondered what it's like to take a computer science course, but never have? Or maybe people who are currently taking an introductory computer science course, and are looking for emotional validation?
But wait -- there's more! Have you seen this page? Man, there are a heck of a lot of people blogging about the exact same thing as me. Surely there can't possibly be enough people interested in these that they'll all get read...
Oh, right. "...your peers in this course will read your journal, and compare your experience to theirs". Okay. So you guys are pretty much all sloggers, just like me. I can deal with that. Sweet.
Wait. We're supposed to find other slogs... by using the list of slogs. So we're going to anonymously refer to each others' expositions of emotional validation. That sounds awkward. No, it can't be, that's the idea. Right?
Alright. New question. I don't want to read all the slogs. There are far, far too many. But how do I pick? You know, I have a strong, strong suspicion that the most referenced slogs will be the ones at the top of the [alphabetical] list. Let me do a quick check... none of the top four have comments. Hmmm... Okay, maybe the ones with clever names will be popular.. checking... nope, I guess that isn't true either. Alrighty, or maybe there just isn't a lot of commenting going on (so maybe those sites are actually the most visited, but I don't know how to check that). I hear procrastinating is a thing.
Well, never fear! Now I know my audience! You're all looking for slogs to comment on! Aha! Aren't I clever?
No, seriously, let's give you some. But no bias. I'm not going to just pick the ones at the top. Every so often I'm going to go through that giant list, pick a few at random, then evaluate them. That sounds a bit tedious... so let's automate it.
import urllib.request
Now, as you already know, I'm not an expert on this, and I'm going to cheat a bit maybe, but I want to show you that python can indeed access webpages. This is how you can do it. This works.
slogsite = "http://www.cdf.toronto.edu/~heap/148/W14/slog.html"
x = urllib.request.urlopen(slogsite)
lines = x.readlines()
And there you have it. Now you have the information you need.
You'll notice that the elements of lines are of type 'bytes'. Now, there's probably a good way to deal with that in python, but let's not go in deeper than we have to.
lines = [str(i) for i in lines]
After some careful inspection, I see that the slog urls start on the line containing "The SLOGs (courSe bLOGs) below are by students in Winter 2014 CSC148". Also, there are exactly two lines after the last url.
for i in range(len(lines)):
if "The SLOGs (courSe bLOGs) below" in lines[i]:
place = i
break
lines = lines[place:-2]
Now, I really only want the urls. Again, I'm sure there's a really good way to do this. But hey, who doesn't love the good old brute-force method?
urls = ()
for line in lines:
start = line.index("href=")
end = line.index(">http")
urls += line[start + 6: end - 1],
Beautiful, isn't it? Not really. But at least it works. At least until the instructors decide to reformat their website. (But they made the site automatedly, right? I mean, surely they didn't ask a grad student to type in all 518 or so urls...)
And finally, the random part.
import random
You know the drill. However, for my own nefarious purposes, I want some user-specified number of urls, not just one. So I'll ask.
k = input("How many urls would you like?\nEnter the number '0', or a natural number between 1 and {}, please. ".format(str(len(lines))))
Now I use the randomness. Also, error-checking.
try:
random_urls = random.sample(urls,int(k))
except ValueError:
print("http://en.wikipedia.org/wiki/Natural_number")
random_urls = []
And now I print.
for i in random_urls:
print(i)
And there you go. Running it gives you one or more slog urls, all ready for copy-pasting into your browser.
Man, I have more to say on the subject still. Like, you know, actually looking at some of these slogs, and talking about them. I will soon, I promise. But I've written a ton, and the code snippet inserts make it look even longer. Next time ;)
Thanks for bearing with me. If you have a comment, please please please comment. Alternatively, if you really are looking for emotional validation, I suppose I can try to help with that, too.
-- David
No comments:
Post a Comment