2010 1.09: Difference between revisions

From XPUB & Lens-Based wiki
No edit summary
 
(8 intermediate revisions by 2 users not shown)
Line 1: Line 1:
== Read an RSS feed from a URL given on the command line ==
* [[Displaying the titles of an RSS feed]]
* [[Turning a text in an alphabetical list of unique words]]
* [[Displaying a list of words from a text followed by the number of times they appear]]


<source lang="python">
#!/usr/bin/env python
import sys, feedparser
try:
    url = sys.argv[1]
except IndexError:
    url = "http://feeds.bbci.co.uk/news/rss.xml"
feed = feedparser.parse(url)
for e in feed.entries:
    print e.title.encode("utf-8")
</source>


== Words ==
== Permutations ==


Turns a text in an alphabetical list of unique words. Attempts to strip punctuation and lowercases everything.
bla bla


<source lang="python">
<source lang="python">
#!/usr/bin/env python
#!/usr/bin/python


import sys, string
import sys, codecs, string, random


words = {}
sys.stdin = codecs.getreader("utf-8")(sys.stdin)
count = {}
for line in sys.stdin:
for line in sys.stdin:
    for word in line.split():
for w in line.split():
        word = word.lower().strip(string.punctuation)
w = w.strip(string.punctuation).lower()
        words[word] = words.get(word, 0) + 1
if w:
count[w] = count.get(w,0)+1
 
#for w,c in sorted(count.items()):
# print w,c


for word in sorted(words.keys()):
words = count.keys()
    print word,
#words.sort()
print
random.shuffle(words)
</source>
words = words[0:5]


import itertools


== Word counts ==
for ws in itertools.permutations(words):
for w in ws:
print w,
print
</source>


Grab words (as above) and display one per line followed by the number of times the word appears.
== Headline Permutation CGI! ==


<source lang="python">
<source lang="python">
#!/usr/bin/env python
#!/usr/bin/env python
#-*- coding:utf-8 -*-
print "Content-type: text/html"
print


import sys, string
import cgi
args = cgi.FieldStorage()
n = int(args.getvalue("n", "0"))


words = {}
print """
for line in sys.stdin:
<body>
    for word in line.split():
"""
        word = word.lower().strip(string.punctuation)
 
        words[word] = words.get(word, 0) + 1
import feedparser
feed = feedparser.parse("http://feeds.bbci.co.uk/news/rss.xml")


for (word, count) in sorted(words.items()):
import itertools
     print word, count
words = feed.entries[n].title.split()
orderings = list(itertools.permutations(words))
for ws in orderings:
    print " ".join(ws)
    print "<br />"
   
#for e in feed.entries[:options.num]:
#    print "<p>"
#    print e.title.encode("utf-8")
#    print "</p>"
      
print """
</body>
"""
</source>
</source>
[http://pzwart3.wdka.hro.nl/~mmurtaugh/cgi-bin/headlines.cgi?n=0 1][http://pzwart3.wdka.hro.nl/~mmurtaugh/cgi-bin/headlines.cgi?n=1 2][http://pzwart3.wdka.hro.nl/~mmurtaugh/cgi-bin/headlines.cgi?n=2 3]

Latest revision as of 13:15, 16 March 2011


Permutations

bla bla

#!/usr/bin/python

import sys, codecs, string, random

sys.stdin = codecs.getreader("utf-8")(sys.stdin)
count = {}
for line in sys.stdin:
	for w in line.split():
		w = w.strip(string.punctuation).lower()
		if w:
			count[w] = count.get(w,0)+1

#for w,c in sorted(count.items()):
#	print w,c

words = count.keys()
#words.sort()
random.shuffle(words)
words = words[0:5]

import itertools

for ws in itertools.permutations(words):
	for w in ws:
		print w,
	print

Headline Permutation CGI!

#!/usr/bin/env python
#-*- coding:utf-8 -*-

print "Content-type: text/html"
print

import cgi
args = cgi.FieldStorage()
n = int(args.getvalue("n", "0"))

print """
<body>
"""

import feedparser
feed = feedparser.parse("http://feeds.bbci.co.uk/news/rss.xml")

import itertools
words = feed.entries[n].title.split()
orderings = list(itertools.permutations(words))
for ws in orderings:
    print " ".join(ws)
    print "<br />"
    
#for e in feed.entries[:options.num]:
#    print "<p>"
#    print e.title.encode("utf-8")
#    print "</p>"
    
print """
</body>
"""

123