Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-10 of 11 total  RSS 

Logging in + posting reactivated

There were some problems with a software upgrade. You can now log in and post again! :)

Using multiple tags on Code Snippets

I spent a lot of time trying to select snippets with
contain multiple tags. (series60 and sql)
There is no '+' links I can click anymore.
After about 10 minutes of trial and error, I find out how.
This also answer another question on correct use of URL.
# for tag 'series60' AND 'sql'
http://www.bigbold.com/snippets/tags/series60/sql/

# this doesn't work
http://www.bigbold.com/snippets/tag/series60/sql/

# all of these work (single tag case)
http://bigbold.com/snippets/tag/series60/
http://bigbold.com/snippets/tags/series60/
http://www.bigbold.com/snippets/tag/series60/
http://www.bigbold.com/snippets/tags/series60/

My conclusion is that.
For a single tag, use '/tag/...'.
For multiple tags, use '/tags/...'.
The first is quicker to type, while the second is flexible in case you want to filter further.

Backup all of your Code Snippets posts

We've just added support for backing up ALL your posts here at Snippets!

If you're logged in.. http://www.bigbold.com/snippets/api/posts/all will return an RSS file with ALL of your posts.

For occasional use.. http://www.bigbold.com/snippets/api/posts/all?username=whatever&password=whatever will get you all of your posts.

Snippets 2 Launches!

If you're a regular visitor to the site, you'll notice the layout has change a lot around here.. well, actually.. most of the code has too! Yes, you're now using Snippets 2, a total rewrite. It still lacks some of the things the old one lacked, but the infrastructure is now there. The old Snippets was notoriously unstable on the newer versions of Rails.

New features include.. syntax coloring (for Ruby, primarily, at the moment), the ability to mark posts as 'private', an all new design, and better URLs and RSS, etc.

Source access for this new version will be via SVN and will be made available in the next couple of weeks as the bugs are wriggled out of this version and I fix up all of the 'hacks'! Post your comments/praise/bug reports on the new version here..

A simple python class to browse snippets website (with beautifoulsoup)

if you got some path/enhancements, you can mail me at my pseudo at gmail.com, i'll update it.
(you should install the marvellous beautifulsoup module, http://www.crummy.com/software/BeautifulSoup/documentation.html)

the snippets.py file :
from BeautifulSoup import BeautifulSoup
import urllib

class Keyword: # top tags
    def __init__(self,tag,nb):
        self.tag=tag
        self.nb=int(nb)
    def __repr__(self):
        return "<Keyword '%s' : %d>" % (self.tag,self.nb)

class Snippet:
    def __init__(self,title,code,tags):
        self.title=title
        self.code=code
        self.tags = tags
    def __repr__(self):
        return "<Snippet '%s' : tags %s>" % (self.title,str(self.tags))

class Snippets:
    urlForTags = "http://www.bigbold.com/snippets/tags"
    
    def __init__(self,l=[]):
        url = self.__getUrlForTags(l)
        
        #load the url
        fu = urllib.urlopen(url)
        content = fu.read()
        fu.close()

        self.tags = l
        self.keywords,self.snippets = self.__extractContent(content)

    def __repr__(self):
        return "<Snippets for tags:%s>" % (str(self.tags))

    def __getUrlForTags(self, l ):
        assert type(l)==list
        l = [Snippets.urlForTags] + l
        return "/".join(l)
    
    def __extractContent(self,content):
        
        soup = BeautifulSoup( content ) 
            
        # get the keywords
        tagTable=soup('div', {'id' : "sidebar"})[0].table
        keywords=[]
        for i in tagTable("tr"):
            td = i("td")
            
            # add this keyword
            try:
                # extract from the empty selection page "/tags"
                keywords.append( Keyword(td[1].span.a.string , td[0].string) )
            except TypeError:
                # extract from a selected selection page "/tag/something"
                keywords.append( Keyword(td[2].span.a.string , td[1].string) )
        
        # get the snippets
        postList=soup('div', {'class' : "post"})
        snippets=[]
        for i in postList:
            divs = i("div")
            
            # get title and tags
            title =  divs[0].h3.a.string # title
            tags = [j.string for j in divs[1]("a")][:-1] #don't get the user ;-)

            # get code of the snippet
            list = [j for j in divs[0]][1:]# zap the first (h3)
            code=""
            for i in list: 
                try:
                    if i.name == "pre":
                        try:
                            code+=i.string
                        except TypeError:
                            pass
                except AttributeError:
                    # transform "out-pre-text" in comment
                    out = str(i).strip()
                    if out:
                        code+="#| "+out+"\n" 
            
            # add this snippet
            snippets.append( Snippet(title,code,tags) )
            
        return keywords,snippets


and an example (all returned "strings" are in utf-8):
from snippets import Snippets

s = Snippets(["python","xml"])
print s
print s.keywords # the "top tags" column
for i in s.snippets:
    print i
print s.snippets[6].title # the title of the 6th
print s.snippets[6].code  # the code of the 6th

Apologies for downtime - weird Rails issues

I apologise for the downtime on Sunday and early Monday. I am buying a new server to spread the load.

One of the biggest problems with this server is that after an hour or two, Snippets' dispatch.fcgi is using tons of CPU and not serving requests. Despite setting FCGIConfig properly, these processes are not getting killed.

I've written and cronned a Perl script that does a killall -9 on ruby and dispatch.fcgi processes whenever load goes over a certain amount.

Bloocy weird, and highly annoying. It's like dispatch.fcgi is getting caught someplace.

How Snippets gets general post counts for tags

This is in models/tag.rb

def find_all_with_count
    self.find_by_sql("SELECT t.*, COUNT(pt.post_id) AS count FROM tags t, posts_tags pt WHERE pt.tag_id = t.id GROUP BY t.id ORDER BY count DESC;")
end

This is a basic non-specific overview of all tags, as you would see on Snippets' front page. So:

@tag_info = Tag.find_all_with_count
@tag_info.each { |t| .. you now have t.name, t.count, etc .. }

Snippets' routes

These routes allow URLs like tags/whatever/whatever/whatever to put 'whatever', 'whatever', and 'whatever' into the @params[:tags] array, etc.

map.connect '', :controller => "posts", :action => "index"
map.connect 'tags/*tags', :controller => 'tag', :action => 'show'
map.connect 'user/:user', :controller => 'user', :action => 'show'
map.connect 'user/:user/tags/*tags', :controller => 'tag', :action => 'show'


Find items with similar (or as many as possible) relationships - for a 'related posts' box etc

If we have the id for a post in postid and a limit of num and we want to find posts which share as many tags as possible with postid's post, the following SQL will get you there.

SELECT p.*, COUNT(pt2.post_id) AS count FROM posts p, posts_tags pt, tags t, posts_tags pt2 WHERE pt.post_id=#{postid} AND t.id = pt.tag_id AND pt2.post_id != pt.post_id AND pt2.tag_id=pt.tag_id AND p.id = pt2.post_id GROUP BY pt2.post_id ORDER BY count DESC LIMIT #{num};")
« Newer Snippets
Older Snippets »
Showing 1-10 of 11 total  RSS