How about that Facevertising?

    There has been quite a bit of publicity lately regarding Facebooks (relatively) low ad revenue and how much better MySpace is doing in terms of advertising. Well, hows this for advertising? I never thought I would see the day when a company would pay you (via gift card) to become a fan of theirs on Facebook. Initially I scoffed at the idea, I mean paying people to become their fan on Facebook? Seems bizarre to say the least, I mean why does Sears care about having fans on Facebook?
    The more I think about it, the more clever the idea is. Think about this, although Sears is a tried and true name in their business, their reputation with younger audiences is less...inviting. Yes, if my dad is looking to buy a new washer and drier he'd probably look at Sears, but what do I need there? Sears is about to tell us (the younger audience).
    This advertising methodology relies on two main components. The first part is very similar to rebates, sure it all sounds good on paper but how many people will actually go through with it? Sears has eased the process by shipping you the card, but in order to give them your information you need to register on a website within 24 hours of becoming a fan. I'm quite sure that many of the people who do become a fan will miss this little tid-bit and end up giftcard-less.
    Now that Sears has you as a registered "fan" of their store, they have a direct communication channel with you. They are now prime to "update their fans" on all of the new and exciting offerings from Sears, think about how many eyes will see these updates relative to advertising in the newspaper, and at what cost. Furthermore, updates on their site will be displayed on the fans news feed (you know, this one) Thats a nice piece of real-estate that Sears will get to inhabit when posting updates. That is the first page the 125 million Facebook users see when they login to their account, which apparently over 50% do daily (oft quoted but apparently never cited, sorry).
    All in all, although the kneejerk reaction isn't a goood one, I think Sears may be onto something. It is definately an appropriate campaign for them, and I suspect you will start to see quite a few like it pop up quite soon, especially with holiday season coming up.

Read a random line in a (large) file in Python

I've been working on a project which requires that I read random lines from a set of files. Unfortunately these files range between 200MB and 2GB so reading the entire file into memory is incredibly slow if possible at all. I looked around for a simple way to do this with large files and I was unable to find a good one, so here you go:

#!/usr/bin/python

import os,random

filename="averylargefile"
file = open(filename,'r')

#Get the total file size
file_size = os.stat(filename)[6]

while 1:
      #Seek to a place in the file which is a random distance away
      #Mod by file size so that it wraps around to the beginning
      file.seek((file.tell()+random.randint(0,file_size-1))%file_size)

      #dont use the first readline since it may fall in the middle of a line
      file.readline()
      #this will return the next (complete) line from the file
      line = file.readline()

      #here is your random line in the file
      print line


Using this method has shown to be MUCH faster then doing something along the lines of:
for line in file:
     #do something
and furthermore, this method does not make it easy to get a random entry in the file.


Enjoy!

Update: Just a note on the "file.seek" line above, the "file.tell()" call is not strictly necessary since what line you are on currently has no bearing on the next line you will be going to. That would also remove the need for the mod operator since the randint would never be larger than the file. Furthermore, since you are advancing the file pointer to a random spot in the file, if all lines are not an equal length then you will not get true randomness, longer lines will in fact be more likely to be hit. Since, however, the algorithm takes the line after random spot seeked to in the file, this issue is lessened but not resolved. I'm looking for a better and more efficient solution in the meantime.

I (We?) still dont know how to read RSS feeds

      From the time I discovered what exactly that little orange square with the three lines in it was I have been using RSS feeds to keep track of all of the sites I frequent. I think most readers agree that RSS feeds are in fact far superior to keeping up-to-date on all of the websites and news that we want to read. It took the old model of us going out and getting what we want to the new model of what I want comes to me. Its absolutely a huge step and although you wouldn't know it by the numbers, RSS feeds are going to quickly become the norm.
With that in mind...
     One thing that is infinitely entertaining to study and see is the techniques that we all employ dealing with the different parts of our daily lives. Things like email, that we all use, and all have our own ways of managing. For example, when I check email I do my best to ensure that when I am done that my inbox has 0 unread messages. Of course I don't read every email, I tend to do the "mark as read" bit quite often and it seems as if many other people do the same given how prominently placed it is on just about every email site. I use labels/folders and filters to separate out emails into different groups, nothing wild there. I often catch glimpses of other peoples mailboxes and it is interesting to see how they manage theirs. I'm not going to go into details on all the interesting and bizarre things I've seen, but it seems to work for them (at least for the time being).
     So now we arrive at RSS feeds, which similar to email, requires a some time to figure out how to manage. I know that I have 22 RSS feeds which I subscribe to and the range of post frequency between them varies quite widely. On one end I subscribe the the feeds of a few authors books who I've enjoyed and generally post a few times a week if that, and on the other end of the spectrum there are things like "deal" websites which have a few 100 posts a day. In the middle I have the gamut of tech sites (a la Slashdot, Gizmodo, etc) which tend to have roughly 10-20 posts per day.
     Obviously the sites I subscribe to contain a wide range of things I want to keep track of, but at the same time is brings all of those things together, for better or worse. Before, if I wanted to get my news I would browse through a few sites with how many depending on how much time or interest I had at that particular time. In general I tend to look at things in groups, so may start with regular news, then tech news, then a few blogs, etc. So I would get all the information from a type of site that I was interested in, then move on to another. With RSS feeds everything is all in one location, so I can quickly see posts on all of the sites, and it tells me when there are new articles to read. Unfortunately that means I frequently find myself switching from Techbargains to xkcd a bit too quick and I dont properly "context switch" between them. As a result I often lacklusterly browse the posts in the feed when I really wasnt in the mood to read that particular information at that time, and if I had read it later I would likely have done a much better or more thorough job. Given that most people, including myself employ the "only show me unread messages/posts" feature (if I already saw it, why see it again?), we will likely not see that post again.
     All this leads me to the fact that I simply dont know how to read RSS feeds. I am working on my own technique, but it simply takes time to develop. I thought that after already having one for email the rest would be easy, but thats not quite the case.
     I think RSS is one of those technologies that is going to be hear for quite a while, its really a viable way for information transfer (once we figure out how to use it). For a testament to it, and how acceptable it is becoming, open up your Facebook. What is the first thing you see, yep, your "News Feed". Surprised?