The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 17, 2019, 05:42:25 PM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: Expand a list of trackbacks - python  (Read 7271 times)
arms
Expert
****
Offline Offline

Posts: 235



View Profile
« on: August 22, 2007, 08:08:04 PM »

there's no python section so i guess i'll put this here. this is just for educational purposes.

the function 'expand' takes a list of trackback urls with the form: http:// someblog.com/blah/1234 and a number.
for example the number 2 would yield 4 more urls per url. with the url http:// someblog.com/blah/1234 and the number 2 you would get:
http:// someblog.com/blah/1232
http:// someblog.com/blah/1233
http:// someblog.com/blah/1235
http:// someblog.com/blah/1236

Code:
import re

def up(num, dif):
    vals = []
    for i in xrange(dif):
        num += 1
        vals.append(num)
    return vals

def down(num, dif):
    vals = []
    if num > 1:
        count = 0
        while num > 1 and count < dif:
            num -= 1
            vals.append(num)
            count += 1
        return vals
    else:
        return [num]

def expand(trackbacks, dif):
    new_urls = []
    for trackback in trackbacks:
        m = re.match('(http[^"\s]+/)([0-9]+)[/]?$', trackback)
        if m:
            url = m.group(1)
            post_id = int(m.group(2))
            ids = []
            ids.extend(up(post_id, dif))
            ids.extend(down(post_id, dif))
            new_urls.extend([url + str(id) for id in ids])
    return new_urls
Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #1 on: August 22, 2007, 10:22:12 PM »

There is now arms... thanks a bunch for the addition and the push to get a Python board up!

Please don't be shy... Wink

/p
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!