Archive Page 2
Fast Prime Number Generator in Python
I had this post saved as a draft for quite awhile. Python’s reliance on indentions made finding a code formatting plugin mandatory. This isn’t the fastest Python prime number generator, but it’s pretty good. I saw one somewhere else that was twice as fast, but it was a bit obtuse. This is a slight variation on the Sieve of Eratosthenes.
#------------------------------------------------------–
# optimized sieve - pretty good and still understandable
# generates primes up to 'limit'
#------------------------------------------------------–
def fast_primes( limit ):
if limit < 2:
return []
elif limit == 2:
return [2]
# fill the array with odds
map = range(3,limit+1,2)
stopat = limit ** 0.5
cur = mstart = 3
# only go up to the sqrt of the limit
while cur <= stopat:
multy = limit / cur
while( mstart <= multy ):
idx = ( cur*mstart-3 )/2
map[idx] = 0
mstart += 2
# get rid of this multiplier
mstart = cur + 2
# move up to the next digit
cur += 2
return [2] + [x for x in map if x]
Top 10 Things That Digg Got Wrong
Despite the fact that I read Digg all day, there are several glaring things that are just plain retarded, and am very confused as to why they haven’t been fixed.
0. It does NOT keep me logged in. Why? I visit digg several times a day, everyday. Why can’t it keep me logged in more than 3 days then? Am I afraid someone’s going to sit down at my computer and cause massive fraud…. with my digg account? Oh no, he dugg that 137th story about the Wii to make it to the front page that I didn’t care about for the 137th time, my life is gonna be over. There should at least be an option that says “Keep me logged in, thanks”.
1. So, when I forget, and don’t notice that it logged me out for no reason, and I go to digg something … I get a nice little DOM popup that asks me to login. So why can’t that let me log in?? Why do I have to leave the front page? Just put a username/password form on the DOM popup and let me freakin login right there, and then record that Digg It I just clicked by the way too.
2. Why is the default view Technology, and more importantly, why can’t I set my own default? I like the videos, and the technology, and the world news. Its real annoying to have to click “View All” every single time I visit. If Digg is supposed to only be a technology news site, then get rid of the other categories. Otherwise, I should be able to view what I want by default, however I want.
3. That commenting system has to literally be the worst commenting system I’ve ever used. Obviously since we develop forum software we know a bit about this, but some of this crap is inexcusable. Like the semi-flat, semi-nested structure? What is this, a joke? Hi, I can reply to a post, and it will start a nested structure… oh but thats it, child, thats it. You’ve had enough nesting for one day, don’t you think? Now just go ahead and line up in a flat structure with the rest of the replies, so no one can tell if you’re actually replying to the parent, or to your sibling. It’s like they took the best of two good ideas, and then made it completely retarded.
4. This then leads to the next obvious question. Since the semi flat structure does exist, why can’t you QUOTE people? In nested structures, your reply is tucked under the parent. With flat forums, people use quoting systems. Is it that hard to make a quick and easy quoting system (no, its not, cause we came up with a badass one).
5. Why does it seem like the first comment on every single story gets modded down -267 points for no reason. Even if its a perfectly good comment, it gets modded down. Everytime. Are people just jealous that they didn’t get the FP? Well, maybe you should have been reading the upcoming stories instead of just waiting for other people to do the work to get the cool stuff to the front page, eh?
6. Why the hell can’t the story have a default link included that links to duggmirror? Hello, are we pretending like the /. effect doesn’t exist? I want to just read the freaking article, not have to sift through the comments to find someone who posted the link to duggmirror, cause I can’t even just GO to duggmirror and find it myself. Save the world, save some poor guys servers and just link to the mirror already.
7. Why is the timestamp associated with articles listed as when the person submitted it? I don’t care. I want to know when it hit, when it made it to the front page. If its on the front page, tell us when it made it there. If its not, then default back to when it was posted.
8. When you click the “Digg It” link on a story to digg it, why is it so slow? Well, I know the answer, cause its hitting the server first and then reporting back. Thats completely unnecessary though. When someone clicks that, just up the number by 1 immediately. Instant feedback is the holy grail of AJAX programming, waiting to contact the server completely ruins the whole magical effect. Once the server responds back with the new digg count, you can still update it then too. But having to wait 2 or 10 seconds makes me feel like “gosh did I actually click it? Whats going on? Anxiety attack!”
9. The avatars are too freaking small. Whats the point of having a 7 pixel by 7 pixel avatar. “Oh I know that guy, he’s the guy with the black dot avatar”. Yeah real freaking useful.
Digg, you guys had a nice idea, but you’re starting to turn into a one trick pony. The whole user moderated, user generated thing is great, but now you need to just make your site *work* the way a top Alexa-ranked site SHOULD work, especially since you’re pretending to be one of the forerunners of the “Web2.0″ revolution.
House of cards
Weekends are programming killers, I swear. Friday I got pretty deep into rewriting the methodology to display threads dynamically that reside above your current threshold. Now monday rolls around and I’m trying to get back into it, and I’ll be damned if I don’t even remember where I was. What was I doing? How far did I get? Even as I’m breaking it down again and trying to get back to where I was, I can remember thinking through the same things last friday but I can’t remember what I came up with. As steven said, its like building a big house of cards and then saturday comes and WHOOOSH knocks it all down.
Who wants to donate!
I’m not quite sure why we never did this before … likely for the same reason that I’m still not holding my breath for it to work. Either way, if for some really strange reason you’d like to help us buy ramen while we’re living in our darkened squalor, hacking mercelessly and living on a steady stream of mountain dew, please feel free to click the following link and donate whatever you’d like to the Cause.
The reason this came about has to do with the forum signups. Its not that we’re running out of server space, or bandwidth. It comes down to a competition of time allocation. As with most startups I’m sure, the time we have is very precious. It’s gotten to the point now that every few days I find myself faced with 25 to 30 forum requests waiting in the queue to be setup. If we were running on a dedicated setup it’d be easy, but unfortunately to setup these free forums I have to go through dreamhost’s slow and bloated control panel, as well as manually do the SVN export of the latest code base. It ends up taking 5 to 6 minutes if I’m babysitting it, longer if I try to go off and do other things. Multiply that by 25, ah, carry the 1… um… yeah it takes a while. Plus its such monotonously boring work that I just can’t stand it.
So, we made the decision to institute a very small setup fee. Firstly to hopefully curb the number of forum requests, but also to supplement our pathetic Adsense revenue. It’s kind of a bittersweet decision, because on one hand you want your little project to grow and spread and get as many people using it as possible — but on the other, when the mundane management of these free forums begins to take up a significant chunk of your day, is it really worth it? I’m literally having to stop hacking on MF itself, just to take care of these hosted forums. So do you try to keep it growing, or do you work on getting it done?
In the mean time I hope this is a happy medium. Hopefully it will drag in a bit of revenue, possibly weed out the forum requesters who earnestly want a forum they’re going to promote and use, as well as (maybe?) raise the image of MF just a little. I mean, Abercrombie can charge $55 for their crappy tshirts, so obviously that model works pretty well.
As with everything we do though, its an experiment. We stumble along and try to make the best decisions, learn from our mistakes, and vow to get ‘em tomorrow.
Keeping Your Lines Wrapped When Using the Pre Tag
The pre tag is great for taking text written and formatted somewhere else and making sure it looks right (ie, as the author intended). It’s commonly used for code snippets as it maintains line breaks and formatting (all those tab and newline characters).
I was pulling textual data from a database for a report and realized the user-inputted line breaks were hosed. I therefore wrapped the output in a pre tag, which showed the formatting (yay!) but busted the page width by allowing the lines to extend far beyond the containing div.
I played around with the white-space CSS declaration, to no real avail. Then I found this article on a hack that can be used to trick a variety of browsers into wrapping the lines while still preserving the other formatting elements. The hack is by T. Longren, and looks like this:
pre {
white-space: pre-wrap; /* css-3 */
white-space: -moz-pre-wrap !important; /* Mozilla */
white-space: -pre-wrap; /* Opera 4-6 */
white-space: -o-pre-wrap; /* Opera 7 */
word-wrap: break-word; /* Internet Explorer 5.5+ */
}
Getting the Interface Right
Like Brian, I’ve been frustrated by something inane lately, only it doesn’t have to do with code that’s not working. It’s simply that something doesn’t feel right about my interface. I have a series of navigational tabs on the top, then a column of content (a graph that consumes the width of my column). Below the graph, I need to show information about the book I’m charting, and provide some navigational and things-you-can-do type links. What I’m talking about is similar to the existing interface. Yet for this redesign, that general structure wasn’t working. I really just had this vague uneasiness about how it looked, and I experienced some frustration. When that happens, I don’t work much. In fact, tonight I ended up watching Saw II instead of working.
After the movie, I came to my desk and removed that sidebar. I widened the section that pertains to the book, enlarged the book image, and suddenly felt some relief. The vertical flow of the page seems less broken up now. It looked right, if in need of some styling.
I certainly don’t consider myself a design expert, but I did read Jenifer Tidwell’s Designing Interfaces book and took a course on human factors in college. The rest I’m gleaning from looking at a lot of web pages in my spare time. The fact that I can feel relief from “fixing” something like this gives me hope.
When Codings got you down
I’ve spent the last 3 hours debugging the most inane little bug. Its driving me crazy. I have a series of divs in the normal flow and for some reason there’s this unexplainable block of blank space between them. There’s nothing in the CSS that is setting any kind of top or bottom margins, there are no line breaks anywhere, there’s just absolutely nothing I can find that is causing this danged space to be showing up. The only way to “fix” it (if you can call it that) is to set the margin-top of each div to be roughly -22px. This pulls each div up to where it should be, but that kinda crap aint gonna fly around here. There HAS to be a reason its doing this, I just can’t find it.
I hate days like these. Sometimes I come in and I just fly, complex things end up being super simple. Then there are days like today where I rack my brain for hours on end, with nothing to show for it.
At times like these, its important to just stop. Do something else. Something completely unrelated. I got up, got some water, had a sandwich, and am now working on something else. I’ll probably have a dream tonight about how to fix it or whats wrong, which oddly enough happens quite often.
Smart Grouping Using XSLT
XSL is a relatively small language, yet it can do some pretty neat things. I’m glad for that, as I thought I had hit a road block and was going to have to abandon all hope.
If you have some XML that needs to be grouped uniquely by something, you can use an XPath expression and the apply-templates element. Let’s say you have a multiuser blog and want to format some output showing the user name and all of their posts. Assume each node is laid out similar to this:
<post>
<user>John</user>
<title>My blog post</title
</post>
<post>
<user>John</user>
<title>Another blog post</title
</post>
<post>
<user>Julie</user>
<title>Hello there</title
</post>
Normally, issuing a for-each and value-of statements over the post element would work, but it’d print John twice. It’d be nicer to print each person’s name once, then list all their posts beneath their name.
You can do this using the Muenchian method.
First you need to create a key that indexes all post’s. Immediately beneath the xsl:stylesheet definition, do this:
<xsl:key name="authors" match="post" use="name" />
This names the key authors, and looks at all post nodes, using the name element to index.
Now add the following to wherever you want this list to appear in your document.
<xsl:apply-templates select="post[generate-id(.) = generate-id(key('authors',name)[1])]" />
That’s confusing to me, but it’s pretty much straight out of the document linked to above. Somehow we’re selecting each author and grouping all their posts together.
Finally, write a template that uses this data and outputs it. I was outputting html, so I had to move the xsl:template definition outside my current template section (I have one main xsl:template declaration that wraps the html body and end body elements) Not to worry, the xsl:apply-templates will invoke the output of the one we’re about to create, so we can place the list into the body of the html.
<xsl:template match="post" >
<xsl:value-of select="name" />
<xsl:for-each select="key('authors',name)" >
<xsl:value-of: select="title" />
</xsl:for-each>
For someone expert on this, check out Jeni Tennison.
How to make a comma seperated list that looks nice
Several times throughout the code, I want to print out a list of items, seperated correctly by a comma. Example:
Users browsing this thread: briantech, spinlock, bobjones, jamesgreer, nancyreagan
It would be painfully easy to just iterate through your array, printing out the array value followed by a comma. The problem with that though, is at the end of your list, your last element is going to have a comma at the end, which looks silly. So, instead you can just do this:
for ($i=0;$i < length($array);$i++)
{
print $array[$i];
if ($i+1 != length($array) )
print ",";
}
This way it puts a comma after your value for every value except the last one.
Salting SHA1 Passwords
It was brought to my attention during a discusion of passwords that apparently SHA1, by itself, is not adequate for protecting passwords. Namely because for 5 or 6 letter passwords it can actually be forced somewhat quickly enough that it would be worth it, but in addition there are reverse lookup SHA1 databases out there for hundreds of thousands of commonly used passwords, or derivatives there of.
Consequently, it is a good idea to go one step beyond SHA1, by “salting” the password before storing it. Salting is where you come up with some kind of random string when the password is initially set. This can be anything… let the user mash the keyboard, and generate some random series of alpha numerics. However you want to do it, doesn’t matter. You then take this salt string and prepend it or append it to the the user’s actual password, and *then* run SHA1 on it and store that SHA1 string as the password.
Then its simply a matter of storing that salt string in a settings file or a database table, and everytime you do a user authentication process, grab that salt string, prepend it to the password, then compare SHA1 hashes for authenticity. This virtually guarantees that no one could ever brute force or guess the password, since not even the user knows what their *actual* password is.
Search
You are currently browsing the Code in Focus weblog archives.


