Toribash
Original Post
Vox <3 Python!
Playing around with ToriNet's new Support bot I decided to make a quick python script to count the number of FAQ commands, list the commands and list the links given in the FAQ, turned out this was so damn easy I just had to praise python a tad.

Here is all the code needed for 3 methods, and downloading a page:


[EDIT] FINALLY to finish this off I made the whole thing into 1 line of code:
fst = dict(zip(["listItems", "findCom", "findID"],map((lambda sides:(lambda p:map(lambda pat:(__import__('re').findall(pat,str(__import__('urllib').urlopen("http://faq.torinet.de/ausgabe1.php").read()))),([sides[0] + str(p) + sides[1]])))),[("<tr><[^<]+>(\d+)<[^<]+><[^<]+>([^<]+)<[^<]+><[^<]+>([^<]+)<[^<]+>","</tr>"),(">","<[^<]+><[^<]+>([^<]+)<"),(">(\d+)<[^<]+><[^<]+>","<")])))

(Note: Code updated, old version below current version)
(Updates from last version: I've turned the whole thing into a map of patterns to page using lambda, outputting all results into a single list.
Also, all the previous versions used python 3.2, this uses python 2.7)

Python code:
import urllib, re
page = str(urllib.urlopen("http://faq.torinet.de/ausgabe1.php").read())
pats = [">(\d+)<", "\d+<.*>([^<>]+)", "(http[^\s<>]+)"]
output = map(lambda pat: (re.findall(pat,page)), pats)


The _docs_ output is bellow, you can use these to try and understand the above code:

_docs_




(Updates from last version: this version is mathematically abstracted to make it more extendible)

Old code




(Updates from last version: this version is smaller because I compressed the functions to avoid code duplication)

Old, old code



Old, old, old code



So now we shall have an 'opinions on python thread', I'd be interested if anyone can justify disliking python.

(For fun, here is all the output from those three commands, for command() and links() the output is larger than the program:)

output

Last edited by Vox; May 31, 2011 at 01:22 PM. Reason: code!
I never had the chance to learn it, but
I like how it simplify the output to a minimun.
One question, do you find Python more complex or simpler than
HTML and PHP ?. I might learn it
Last edited by Neru; May 12, 2011 at 08:04 PM.
⋐⋑
<ishi> 2MEGPOID4ME
Originally Posted by p0w View Post
I never had the chance to learn it, but
I like how it simplify the output to a minimun.
One question, do you find Python more complex or simpler than
HTML and PHP ?. I might learn it

Python is about as nice as languages get to the programmer, just don't get too used to languages being nice to you, that's all I'd say.

HTML would be easy, if every damn browser didn't display it differently, that makes it harder, and PHP I'd say is in the Perl/Python crowed, but python is certainly easier to get off the ground.

[EDIT] For fun I compressed those three commands into one command to avoid code duplication:

Python code:
import urllib.request, re

urlList = str(urllib.request.urlopen("http://faq.torinet.de/ausgabe1.php").read())
pats = [">(\d+)<", "\d+<[^<>]*><[^<>]*>([^<>]+)", "(http://[^\s<>]+)"]

def query(n): return (re.findall(pats[n],urlList),len(re.findall(pats[n],urlList)))


[EDIT] For fun I mathematically abstracted the functions and then made it build the functions during run time, to add more functions simply add more patterns.

Python code:
import urllib.request, re
urlList = str(urllib.request.urlopen("http://faq.torinet.de/ausgabe1.php").read())
pats = [">(\d+)<", "\d+<[^<>]*><[^<>]*>([^<>]+)", "(http[^\s<>]+)"]; fs = []
def makeQ(pat): return lambda: (re.findall(pat,urlList),len(re.findall(pat,urlList)))
for pat in pats: fs.append(makeQ(pat))


This new function creates functions into the array fs, here is a sample set of the output:
>>> fs
[<function <lambda> at 0x0000000003283748>, <function <lambda> at 0x00000000033F80C8>, <function <lambda> at 0x00000000033F8148>]
>>> fs[0]
<function <lambda> at 0x0000000003283748>
>>> fs[0]()
(['25', '29', '34', '35', '36', '37', '38', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '70', '71', '72', '73', '74'], 42)


[EDIT] (yes again): I've turned the whole thing into a map of patterns to page using lambda, outputting all results into a single list.
Python code:
import urllib, re
page = str(urllib.urlopen("http://faq.torinet.de/ausgabe1.php").read())
pats = [">(\d+)<", "\d+<.*>([^<>]+)", "(http[^\s<>]+)"]
output = map(lambda pat: (re.findall(pat,page)), pats)


The _docs_ output is bellow, you can use these to try and understand the above code:

_docs_




[EDIT] FINALLY to finish this off I made the whole thing into 1 line of code:
fst = dict(zip(["listItems", "findCom", "findID"],map((lambda sides:(lambda p:map(lambda pat:(__import__('re').findall(pat,str(__import__('urllib').urlopen("http://faq.torinet.de/ausgabe1.php").read()))),([sides[0] + str(p) + sides[1]])))),[("<tr><[^<]+>(\d+)<[^<]+><[^<]+>([^<]+)<[^<]+><[^<]+>([^<]+)<[^<]+>","</tr>"),(">","<[^<]+><[^<]+>([^<]+)<"),(">(\d+)<[^<]+><[^<]+>","<")])))
Last edited by Vox; May 13, 2011 at 11:13 AM.
You have learned to copy paste from computer science text books, don't you feel special?
Yes, we all know this, python is good because it has rapid development, C is good because it compiles down to a very fast application, but how is this relevant to the thread? Last I checked this isn't a thread for facts about python/c >.>
Vox, can you help me a bit, i've run into a loophole in a basic BJ;

Hole code:

ansr = input("Yes or No?: ")
while sum1 > 21:
if ansr == "Yes":
Now doing recoloring for people not in the clan as-well, PM for more info!
PROUD OWNER OF THORN'S GOOD ENOUGH WRITER AWARD!
I don't understand what the problem is, can you try to explain what you want to do and so forth? If I don't know what A) the problem is and B) what you want to try to do, then I can't help.
well, basically it created a loophole, due to a reason i cant find, but nvm, i solved it by rewriting it as a function.
Now doing recoloring for people not in the clan as-well, PM for more info!
PROUD OWNER OF THORN'S GOOD ENOUGH WRITER AWARD!