Re: Parsing an HTML a tag
I do not really know, what you want to do. Getting he urls from the a tags of a html file? I think the easiest method would be a regular expression. >>>import urllib, sre >>>html = urllib.urlopen("http://www.google.com";).read() >>>sre.findall('href="([^>]+)"', html) ['/imghp?hl=de&tab=wi&ie=UTF-8', 'http://groups.google.de/grphp?hl=de&tab=wg&ie=UTF-8', '/dirhp?hl=de&tab=wd&ie=UTF-8', 'http://news.google.de/nwshp?hl=de&tab=wn&ie=UTF-8', 'http://froogle.google.de/frghp?hl=de&tab=wf&ie=UTF-8', '/intl/de/options/'] >>> sre.findall('href=[^>]+>([^<]+)', html) ['Bilder', 'Groups', 'Verzeichnis', 'News', 'Froogle', 'Mehr »', 'Erweiterte Suche', 'Einstellungen', 'Sprachtools', 'Werbung', 'Unternehmensangebote', 'Alles \xfcber Google', 'Google.com in English'] Google has some strange html, href without quotation marks: http://www.google.com/ncr>Google.com in English -- http://mail.python.org/mailman/listinfo/python-list
Re: Parsing an HTML a tag
I think for a quick hack, this is as good as a parser. A simple parser would miss some cases as well. RE are nearly not extendable though, so your critic is valid. The point is, what George wants to do. A mixture would be possible as well: Getting all by a RE and then extracting the url with something like a parser. -- http://mail.python.org/mailman/listinfo/python-list
Re: Best practices for dynamically loading plugins at startup
I wrote this one: -- def load_plugin(self, plugin, paths): import imp # check if we haven't loaded it already try: return sys.modules[plugin] except KeyError: pass # ok, the load it fp, filename, desc = imp.find_module(plugin, paths) try: mod = imp.load_module(plugin, fp, filename, desc) finally: if fp: fp.close() # instantiate and put into basket clazz = mod.main(self.config) if "input" in clazz.types: self.inputs.append(clazz) if "parser" in clazz.types: self.parser.append(clazz) if "output" in clazz.types: self.outputs.append(clazz) -- The imp module is the key: http://docs.python.org/lib/module-imp.html The parameters for the load module function, are found by look through the directory. So my plugins had a convention: They have a class called main, which is initialized with one argument, the config object. That is quite a simple plugin system. You can check out the whole thing here: https://developer.berlios.de/projects/feedisto/ -- http://mail.python.org/mailman/listinfo/python-list
Re: What tools are used to write and generate Python Library documentation.
Do you think of pydoc? Just make comments in your code this way: def add10(x): """this function adds ten to the given variable""" Then save this into add.py and now (in the same directory): pydoc add Voila, your documentation. -- http://mail.python.org/mailman/listinfo/python-list
bug or feature?
Coming back from a bug hunt, i am not sure what to think of this python behaviour. Here is a demo program: class A: def __init__(self, lst=[]): self.lst = lst a = A() b = A() b.lst.append("hallo") print a.lst # output: ["hallo"] The point seems to be, that lst=[] creates a class attribute (correct name?), which is shared by all instances of A. So a.lst ist the same object as b.lst, despite the fact, that object a is different to object b. -- http://mail.python.org/mailman/listinfo/python-list
Re: bug or feature?
Thanks for you answer! This copy trick is the most elegant solution i think. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python 2nd favorite language in Linux Journal poll
Hm, you didn't include a link and my google did not find the final results. The results are fluctuating very much. This suggests a small number of votes. Linux Journal may be a big magazine, but i don't think language opinions change that fast. The vote is all done by email this year, which is another strange thing. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to run python scripts with IDLE
What are you developing for? You could write another small python scripts, which calls your script. os.system("python yours.py --param Ünicöde") -- http://mail.python.org/mailman/listinfo/python-list
Changing an AST
Is it possible compiler.parse a statement, then change and then execute/resolve it? Background: I'm probably to lazy to write my own parser. I have such a statement as string: "distance = x**2 + y**2" x and y are undefined, so it is no executable Python code, but it is parseable. Now i'd like traverse through the AST and change Name('x') for the value i have elsewhere. And finally let Python resolve the computation. More Background: I want to write a simulation game with many interdepended values. I don't want to create a class with dozens of functions, but let Python create/evaluate them. I hope this can be understood ;) -- http://mail.python.org/mailman/listinfo/python-list
Re: Changing an AST
Thank you! this "compile/exec in context" is the thing i wanted. It is not that performant i think. But it should ease the prototyping. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python vs Ruby
Depends on your experience. If you know C,C++,Java and the whole C-syntax-bunch. I'd recommend Python just to learn to adapt a different syntax. If you want to learn for the learnings sake, i'd also recommend Haskell to try functional programming, if you do not already know it. Ruby has some interesting concepts, Python (well CPython) does not have. Blocks for example, which make Continuations possible. In Python you need stackless Python (a different implementation) to do this. -- http://mail.python.org/mailman/listinfo/python-list
Re: Popularity of blogging tools used by python programmers
I just see a database error on your blog, perhaps you should have used Python instead of PHP? Ok, kidding ;) Since i can't see your comparison, i have to guess. PHP wins? Would not surprise me, because PHP+MySQL is the easiest hosting to get. Wordpress, Textpattern, ... are also much more polished. btw my blog runs on Python! :D http://beza1e1.tuxen.de -- http://mail.python.org/mailman/listinfo/python-list
Re: Popularity of blogging tools used by python programmers
Uh, no good style to comment myself, sorry. I just found why i could't see your blog entry. Since i read your message through groups.google.de i just clicked on the link. Your Google Highlight plugin seems to be confused about that: Warning: Unknown modifier '/' in /var/www/html/wp/wp-content/plugins/google-hilite.php on line 107 Warning: Unknown modifier 'w' in /var/www/html/wp/wp-content/plugins/google-hilite.php on line 105 Not everybody who comes from google has a search query in his referer ;) -- http://mail.python.org/mailman/listinfo/python-list
Re: Syntax across languages
>>> id("blub") -1210548288 This is not identity in a mathematical view. def identity(x): return x It has is uses. I had some kind of parser and had a dict like this: {case: function, ...} It had to be a dict, because i wanted to dynamically add and remove cases. In some cases nothing had to be done. To represent this in the dict a identity function is needed. There surely are other ways, but identity was the most expressive in my eyes. Nice read, by the way. Thanks for sharing ;) -- http://mail.python.org/mailman/listinfo/python-list
Re: Tricky Areas in Python
let me try. 1) ''.join(lots_of_pieces) 2) This doesn't even work, if something is removed, the list is too short. So: [x for x in somelist if not isbad(x)] well, list comprehension is Python 2.4 and 2.3 is the standard in many OSes, so it is possibly not the most portable solution I had to look up the syntax, because i never use it in my code, yet. 3+4) I never used property - had to look it up. So i learned something :) -- http://mail.python.org/mailman/listinfo/python-list
Re: Python's website does a great disservice to the language
Things, which can be done better are: - seperate content and layout (no table design, no font tags, ...) - blue links on blue background are nearly as ugly as visited-purple links on blue background - he frontpage is overloaded. Ok this is worth a discussion: poweruser vs. marketing -- http://mail.python.org/mailman/listinfo/python-list
functional or object-oriented?
I see myself shifting more and more over to the functional kind of coding. Could be related to the Haskell, we had to learn in CS. Now i was wondering, how other people use Python? With functional i mean my files mostly consist of functions and only rarely i use "class". The library modules seem to be mostly written the object-way on the other hand. If you use both paradigms. What are your criterias to choose the right method for a project? -- http://mail.python.org/mailman/listinfo/python-list
Re: Announce: open 0.2 - a unix application launcherr
I recently found 'gnome-open', which does this. It chooses the Gnome default for the filetype. Like you clicked on it in nautilus. OS X also has this 'open' command and i like it. :) -- http://mail.python.org/mailman/listinfo/python-list
Re: Roguelike programmers needed
I was hacking on something similar. It could be called a collaborative story-telling adventure game or something. My idea was to parse natural language text not "commands". The game manages locations and objects. This is for story-telling roleplay. No stats, levels or monsters (at least no self acting ones). The prototype is nearly ready (Object and location creation lacking). But i am not sure, how far this can go. Natural language processing is quite interesting. Check out what the MIT did recently: http://www.trnmag.com/Stories/2005/032305/Tool_turns_English_to_code_032305.html -- http://mail.python.org/mailman/listinfo/python-list
Re: functional or object-oriented?
You are right, this is not the essence of functional programming. Functional and procedural python code would look quite the same (at least in pydoc). It is the implementation of my functions, wether they are functional or procedural. If i use global variables, it is not functional any more. While python makes it use to work in a functional way, it is nearly impossible to do it exclusive. This is not necessary either, of course ;) -- http://mail.python.org/mailman/listinfo/python-list
Re: functional or object-oriented?
This nails it down, yes. :) I probably was too deep into OOP thinking-mode to work pythonic. So i am now rediscovering the python way. Have you read Paul Grahams On Lisp (or was it one of his essays)? He is strongly in favor of functional programming. Mainly because Lisp favors it. He does say though, simulations and CAD programs are inherently OO. But now i am writing a game modelling engine, i find objects are not the best way anytime in these fields either. -- http://mail.python.org/mailman/listinfo/python-list
Re: functional or object-oriented?
I really should take a look at this CLOS, i think ... thanks for the background information. Do you think FP Python is appropriate or just syntactic sugar of a very sophisticated kind? Now i switching back to OO a bit, but the difference between data.value and date['value'] is not really in Pythons dynamic world. -- http://mail.python.org/mailman/listinfo/python-list
Writing a parser the right way?
I'm writing a parser for english language. This is a simple function to identify, what kind of sentence we have. Do you think, this class wrapping is right to represent the result of the function? Further parsing then checks isinstance(text, Declarative). --- class Sentence(str): pass class Declarative(Sentence): pass class Question(Sentence): pass class Command(Sentence): pass def identify_sentence(text): text = text.strip() if text[-1] == '.': return Declarative(text) elif text[-1] == '!': return Command(text) elif text[-1] == '?': return Question(text) return text --- At first i just returned the class, then i decided to derive Sentence from str, so i can insert the text as well. -- http://mail.python.org/mailman/listinfo/python-list
Re: Writing a parser the right way?
Well, a declarative sentence is essentially subject-predicate-object, while a question is predicate-subject-object. This is important in further processing. So perhaps i should code this order into the classes? I need to think a little bit more about this. Thanks for your feed for thought! :) -- http://mail.python.org/mailman/listinfo/python-list
Re: Organising a python project
I don't know about a typical python way, but i'd like to know as well ;) Personally i have a project for my project foo, which has foo/__init__.py# with all the other modules doc/# documentation is always a good idea script/ # everything executable, which later goes into 'bin' directories by installing setup.py README, INSTALL, ... and other standard stuff Of course often there is other stuff floating around in the base directory, like some quick test scripts or sketches. And then my version control system has an additional directory. My tests are mostly within the modules. def test(): return True if __name__ == "__main__": test() My __init__.py tests every module, if executed directly. So far my system works. It probably would be good to seperate tests, if they get bigger. -- http://mail.python.org/mailman/listinfo/python-list
Re: Writing a parser the right way?
Thanks for the hints. I just found NLTK and MontyLingua. And yes, it is just adventure game language. This means every tense except present tense is discarded as "not changing world". Furthermore the parser will make a lot of assumptions, which are perhaps 90% right, not perfect: if word[-2:] == "ly": return Adverb(word) Note that uppercase words are identified before, so Willy is parsed correctly as a noun. On the other hand "silly boy", will not return a correct result. Currently it is just a proof-of-concept. Maybe i can integrate a better parser engine later. The idea is a kind of mud, where you talk correct sentences instead of "go north". I envision a difference like Diablo to Pen&Paper. I'd call it more a collaborative story telling game, than a actual RPG. I fed it your sentences, Paul. Result: <['I', 'drive', 'a']> <['red']> <['truck']> should be: <['I']> <['drive']> <['a', 'red', 'truck']> Verbs are the tricky part i think. There is no way to recognice them. So i will have to get a database ... work to do. ;) -- http://mail.python.org/mailman/listinfo/python-list
string to datetime parser?
Is there a library which can parse strings and output a datetime object? It should be as magical as possible and allow things like: 12:30 tomorrow 10.10.2005 02-28-00 28/03/95 1995-03-28 1996.Feb.29 (Thu) 16:45:23.7 Is there anything like that out there? My Google can't find anything useful ... -- http://mail.python.org/mailman/listinfo/python-list
Re: string to datetime parser?
A good solution may be to specify a language to determine the order. The default would be (something like) "en-US" and thus early October in the example. -- http://mail.python.org/mailman/listinfo/python-list