Re: Parsing in Python
On 08/12/2012 22:23, subhabangal...@gmail.com wrote: Dear Group, I am looking at a readymade tool to resolve anaphora, and I am looking a Python based one. I checked NLTK. It has DRT parser. But I do not like that. In other parsers you have to insert grammar. But I am looking for a completely built in. If anyone can kindly suggest. Regards, Subhabrata. Take your pick from http://nedbatchelder.com/text/python-parsers.html -- Cheers. Mark Lawrence. -- http://mail.python.org/mailman/listinfo/python-list
kishor heart th...@gmail.com
hi -- http://mail.python.org/mailman/listinfo/python-list
Re: why does dead code costs time?
On Wednesday, 5 December 2012 22:10:51 UTC+5:30, Bruno Dupuis wrote: > On Wed, Dec 05, 2012 at 04:15:59PM +, Neil Cerutti wrote: > > > On 2012-12-05, Bruno Dupuis wrote: > > > > Hi, > > > > > > > > I'm interested in compilers optimizations, so I study python > > > > compilation process > > > > > > > > I ran that script: > > > > > > > > import timeit > > > > > > > > def f(x): > > > > return None > > > > > > > > def g(x): > > > > return None > > > > print(x) > > > > > > > > number = 1 > > > > > > > > print(timeit.timeit('f(1)',setup="from __main__ import f", > > > number=number)) > > > > print(timeit.timeit('g(1)',setup="from __main__ import g", > > > number=number)) > > > > > > > > print(dis.dis(f)) > > > > print(dis.dis(g)) > > > > > > > > It gives this output: > > > > > > > > 0.003460251959040761 > > > > 0.004164454061537981 > > > > 17 0 LOAD_CONST 0 (None) > > > > 3 RETURN_VALUE > > > > None > > > > 20 0 LOAD_GLOBAL 0 (None) > > > > 3 RETURN_VALUE > > > > > > > > 21 4 LOAD_GLOBAL 1 (print) > > > > 7 LOAD_FAST0 (x) > > > > 10 CALL_FUNCTION1 (1 positional, 0 keyword > > > pair) > > > > 13 POP_TOP > > > > None > > > > > > > > I do not understand why the dead code `print(x)` takes time (~20% in > > > > that case). As we see in the opcode, a call to g(1) returns immediately, > > > so > > > > there should be no delay at all. Where am i wrong? > > > > > > > > mmhh... it comes to me now that the gap must be in function loading > > > time... > > > > I'll check ceval.c > > > > > > > > However, isn't there a room for a slight optim here? (in this case, the > > > > dead code is obvious, but it may be hidden by complex loops and > > > > conditions) > > > > > > Maybe it's the difference between LOAD_CONST and LOAD_GLOBAL. We > > > can wonder why g uses the latter. > > > > Good point! I didn't even noticed that. It's weird... Maybe the > > difference comes from a peehole optim on f which is not possible on g as > > g is to complex. > > > > > > > > -- > > > Neil Cerutti > > > -- > > > http://mail.python.org/mailman/listinfo/python-list > > > > -- > > Bruno Dupuis peehole haha -- http://mail.python.org/mailman/listinfo/python-list
Re: Running a Python app on a remote server and displaying the output files
On Saturday, December 8, 2012 12:22:35 PM UTC-8, Jason Hsu wrote: > 1. How do I run my Python script in Google App Engine and make the output > results.csv file publicly available? Probably https://developers.google.com/appengine/docs/python/blobstore/, however https://developers.google.com/appengine/docs/python/gettingstarted/staticfiles might work as well. -- http://mail.python.org/mailman/listinfo/python-list
Re: why does dead code costs time?
On 09/12/2012 14:11, Ramchandra Apte wrote: peehole haha Double spaced crap from you again not so haha. -- Cheers. Mark Lawrence. -- http://mail.python.org/mailman/listinfo/python-list
An object is and isn't an instance of a class at the same time
I was debugging some code using isinstance() to make sure the correct object was been passed to the method and came across something that is really ticking me off. I have a class called 'Jitem' in its own file called 'jitem.py'. It's part of a package called 'jukebox'. I also have '__all__' that includes 'jitem' so that I can do: from jukebox import * There is another class that has a method that does this (simplified for this example): def execute(self, command): I stuck this debug code in the method: if not isinstance(command, jitem.Jitem): print(command.__class__) raise TypeError("Command must be an instance of Jitem.") When this method gets run in a test script, it returns this: D:\home\python>python jtest.py Traceback (most recent call last): File "jtest.py", line 4, in executeResults = jc.execute(cmnd) File "D:\home\python\jukebox\jconnection.py", line 225, in execute raise TypeError("Command must be an instance of Jitem.") TypeError: Command must be an instance of Jitem. How can it both get past isinstance() and still say it is the proper class? Dan Klein -- http://mail.python.org/mailman/listinfo/python-list
Re: An object is and isn't an instance of a class at the same time
On Mon, Dec 10, 2012 at 10:34 AM, danielk wrote: > D:\home\python>python jtest.py > > Traceback (most recent call last): > File "jtest.py", line 4, in > executeResults = jc.execute(cmnd) > File "D:\home\python\jukebox\jconnection.py", line 225, in execute > raise TypeError("Command must be an instance of Jitem.") > TypeError: Command must be an instance of Jitem. > > How can it both get past isinstance() and still say it is the proper class? You're invoking it as __main__ and then also importing it. This gives you two instances of your module, with two separate classes that have the same name and (presumably) the same definition. If you use a separate driver script, you won't see this problem. Alternatively, simply stop checking isinstance and trust that, if something incompatible gets passed in, it'll throw an exception somewhere. That's usually enough. ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Creating different classes dynamically?
Hi, I have a directory tree with various XML configuration files. I then have separate classes for each type, which all inherit from a base. E.g. class AnimalConfigurationParser: ... class DogConfigurationParser(AnimalConfigurationParser): ... class CatConfigurationParser(AnimalConfigurationParser): I can identify the type of configuration file from the root XML tag. I'd like to walk through the directory tree, and create different objects based on the type of configuration file: for root, dirs, files in os.walk('./'): for file in files: if file.startswith('ml') and file.endswith('.xml') and 'entity' not in file: with open(os.path.join(root, file), 'r') as f: try: tree = etree.parse(f) root = tree.getroot() print(f.name) print(root.tag) # Do something to create the appropriate type of parser except xml.parsers.expat.ExpatError as e: print('Unable to parse file {0} - {1}'.format(f.name, e.message)) I have a dict with the root tags - I was thinking of mapping these directly to the functions - however, I'm not sure if that's the right way to do it? Is there a more Pythonic way of doing this? root_tags = { 'DogRootTag': DogConfigurationParser(), 'CatRootTag': CatConfigurationParser(), } Cheers, Victor -- http://mail.python.org/mailman/listinfo/python-list
Re: An object is and isn't an instance of a class at the same time
On 12/09/2012 07:31 PM, Chris Angelico wrote: > On Mon, Dec 10, 2012 at 10:34 AM, danielk wrote: >> D:\home\python>python jtest.py >> >> Traceback (most recent call last): >> File "jtest.py", line 4, in >> executeResults = jc.execute(cmnd) >> File "D:\home\python\jukebox\jconnection.py", line 225, in execute >> raise TypeError("Command must be an instance of Jitem.") >> TypeError: Command must be an instance of Jitem. >> >> How can it both get past isinstance() and still say it is the proper class? > You're invoking it as __main__ and then also importing it. This gives > you two instances of your module, with two separate classes that have > the same name and (presumably) the same definition. > > If you use a separate driver script, you won't see this problem. > Alternatively, simply stop checking isinstance and trust that, if > something incompatible gets passed in, it'll throw an exception > somewhere. That's usually enough. > > Just to elaborate on ChrisA's comments. You have a script which imports a module, which in turn imports the script. That's called recursive imports, and is a "BAD THING." It can cause various problems, but in the particular case of importing the top-level script, it can cause a very confusing one as you see here. There are two copies of any classes defined in the script, and the system treats them as independent classes which only happen to have the same name. isInstance() is one place you might notice this, but class static variables and even "global" variables in that module are also candidates. The real problem is that sometimes people treat the symptom and never fix the underlying problem, the recursive import. Whenever you have "a imports b, which imports a," or you have "a imports b, which imports c, which imports a," you run the risk of something getting initialized out of order, or worse getting initialized twice. You need to fix that, usually by factoring out some part which is recursive, and moving it to an independent module which gets imported by both the others. You can also get into this type of confusion by importing a module by two different names, for example if you import it from a directory in one place, and from a package in another. -- DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: Creating different classes dynamically?
On 12/09/2012 07:35 PM, Victor Hooi wrote: > Hi, > > I have a directory tree with various XML configuration files. > > I then have separate classes for each type, which all inherit from a base. > E.g. > > class AnimalConfigurationParser: > ... > > class DogConfigurationParser(AnimalConfigurationParser): > ... > > class CatConfigurationParser(AnimalConfigurationParser): > > > I can identify the type of configuration file from the root XML tag. > > I'd like to walk through the directory tree, and create different objects > based on the type of configuration file: > > for root, dirs, files in os.walk('./'): > for file in files: > if file.startswith('ml') and file.endswith('.xml') and 'entity' > not in file: > with open(os.path.join(root, file), 'r') as f: > try: > tree = etree.parse(f) > root = tree.getroot() > print(f.name) > print(root.tag) > # Do something to create the appropriate type of > parser > except xml.parsers.expat.ExpatError as e: > print('Unable to parse file {0} - {1}'.format(f.name, > e.message)) > > I have a dict with the root tags - I was thinking of mapping these directly > to the functions - however, I'm not sure if that's the right way to do it? Is > there a more Pythonic way of doing this? > > root_tags = { >'DogRootTag': DogConfigurationParser(), > 'CatRootTag': CatConfigurationParser(), > } > > Cheers, > Victor Your subject line says you want to create the classes dynamically, but that's not what your code implies. if you just want to decide which class to INSTANTIATE dynamically, that's easily done, and you have it almost right. In your dict you should leave off those parentheses. Then the parser creation looks something like: parser_instance = root_tags[root.tag] (arg1, arg2) where the arg1, arg2 are whatever arguments the __init__ of these classes expects. (untested) -- DaveA -- http://mail.python.org/mailman/listinfo/python-list
TypeError: 'in ' requires string as left operand, not Element
Hi, I'm getting a strange error when I try to run the following: for root, dirs, files in os.walk('./'): for file in files: if file.startswith('ml') and file.endswith('.xml') and 'entity' not in file: print(root) print(file) with open(os.path.join(root, file), 'r') as f: print(f.name) try: tree = etree.parse(f) root = tree.getroot() print(f.name) print(root.tag) except xml.parsers.expat.ExpatError as e: print('Unable to parse file {0} - {1}'.format(f.name, e.message)) The error is: Traceback (most recent call last): File "foo.py", line 275, in marketlink_configfiles() File "foo.py", line 83, in bar with open(os.path.join(root, file), 'r') as f: File "C:\Python27\lib\ntpath.py", line 97, in join if path[-1] in "/\\": TypeError: 'in ' requires string as left operand, not Element Cheers, Victor -- http://mail.python.org/mailman/listinfo/python-list
Re: TypeError: 'in ' requires string as left operand, not Element
In article <8c78344a-8019-450a-bfdf-13508bf75...@googlegroups.com>, Victor Hooi wrote: > Hi, > > I'm getting a strange error when I try to run the following: > > for root, dirs, files in os.walk('./'): > for file in files: > if file.startswith('ml') and file.endswith('.xml') and 'entity' > not in file: > print(root) > print(file) > with open(os.path.join(root, file), 'r') as f: > print(f.name) > try: > tree = etree.parse(f) > root = tree.getroot() > print(f.name) > print(root.tag) > except xml.parsers.expat.ExpatError as e: > print('Unable to parse file {0} - {1}'.format(f.name, > e.message)) > > The error is: > > Traceback (most recent call last): > File "foo.py", line 275, in > marketlink_configfiles() > File "foo.py", line 83, in bar > with open(os.path.join(root, file), 'r') as f: > File "C:\Python27\lib\ntpath.py", line 97, in join > if path[-1] in "/\\": > TypeError: 'in ' requires string as left operand, not Element > > Cheers, > Victor The first thing I would do is try to figure out if it's happening in the join() or the open(). Try refactoring this as: > temp = os.path.join(root, file) > with open(temp, 'r') as f: and see which line generates the exception. I'm guessing it's the join(), but it helps to make sure (so you don't go down some rabbit hole). Next, I would try to construct a minimal test case. Keep hacking away code until you get down to the smallest thing which is produce the problem. I'm guessing something like: > for root, dirs, files in os.walk('./'): > for file in files: > os.path.join(root, file) might do it. Hopefully that will narrow things down a bit. -- http://mail.python.org/mailman/listinfo/python-list
Re: TypeError: 'in ' requires string as left operand, not Element
Hi, Ignore me - PEBKAC...lol. I used "root" both for the os.walk, and also for the root XML element. Thanks anyhow =). Cheers, Victor On Monday, 10 December 2012 11:52:34 UTC+11, Victor Hooi wrote: > Hi, > > > > I'm getting a strange error when I try to run the following: > > > > for root, dirs, files in os.walk('./'): > > for file in files: > > if file.startswith('ml') and file.endswith('.xml') and 'entity' > not in file: > > print(root) > > print(file) > > with open(os.path.join(root, file), 'r') as f: > > print(f.name) > > try: > > tree = etree.parse(f) > > root = tree.getroot() > > print(f.name) > > print(root.tag) > > except xml.parsers.expat.ExpatError as e: > > print('Unable to parse file {0} - {1}'.format(f.name, > e.message)) > > > > The error is: > > > > Traceback (most recent call last): > > File "foo.py", line 275, in > > marketlink_configfiles() > > File "foo.py", line 83, in bar > > with open(os.path.join(root, file), 'r') as f: > > File "C:\Python27\lib\ntpath.py", line 97, in join > > if path[-1] in "/\\": > > TypeError: 'in ' requires string as left operand, not Element > > > > Cheers, > > Victor -- http://mail.python.org/mailman/listinfo/python-list
The Zen of Zope, by Alex Clark
import other The Zen of Zope, by Alex Clark Beautiful is an attribute of ugly. Explicit is implemented by implicit. Simple is provided by complex. Complex is directly provided by complicated. Flat only implements nested. Sparse has tagged value dense. Readability count is not in range. Special cases could not adapt the rules. Practicality implements purity. Errors should never require a specification that doesn’t extend the specification of silence. Unless explicit is a multi-adapter. In subscribing to ambiguity, return all the objects that refuse the temptation to guess. There should be none-- and preferably only zero --output from a handler. Although that way may not be obvious at first unless you've registered an adapter hook. Now is verified by never. Although never is not implemented by *right* now. If the implementation is hard to explain, it queries the bad idea utility. If the implementation is easy to explain, it may query the good idea utility. Implicit namespace packages are one honking great idea -- let's do more of those! -- Alex Clark · https://www.gittip.com/aclark4life/ -- http://mail.python.org/mailman/listinfo/python-list
Re: TypeError: 'in ' requires string as left operand, not Element
On 12/09/2012 07:52 PM, Victor Hooi wrote: > Hi, > > I'm getting a strange error when I try to run the following: > > for root, dirs, files in os.walk('./'): > for file in files: > if file.startswith('ml') and file.endswith('.xml') and 'entity' > not in file: > print(root) > print(file) > with open(os.path.join(root, file), 'r') as f: > print(f.name) > try: > tree = etree.parse(f) > root = tree.getroot() > print(f.name) > print(root.tag) > except xml.parsers.expat.ExpatError as e: > print('Unable to parse file {0} - {1}'.format(f.name, > e.message)) > Where's the printout of the root and file variables? You're asking os.path.join to work on them, and it's clearly upset about their types. I see print statements, so you clearly were thinking along these lines. But you don't show them. BTW, I'd be using print(repr(root)) and print(repr(file)) instead, so you get a better idea of their type and value. My guess for the problem is that you're trashing 'root' with the contents of your try block. Try using a different name for the xml stuff. > The error is: > > Traceback (most recent call last): > File "foo.py", line 275, in > marketlink_configfiles() > File "foo.py", line 83, in bar > with open(os.path.join(root, file), 'r') as f: > File "C:\Python27\lib\ntpath.py", line 97, in join > if path[-1] in "/\\": > TypeError: 'in ' requires string as left operand, not Element > > Cheers, > Victor Incidentally, 'file' is a builtin type, so it's probably not a good idea to hide it by using it as your own local variable. -- DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: Creating different classes dynamically?
heya, Dave: Ahah, thanks =). You're right, my terminology was off, I want to dynamically *instantiate*, not create new classes. And yes, removing the brackets worked =). Cheers, Victor On Monday, 10 December 2012 11:53:30 UTC+11, Dave Angel wrote: > On 12/09/2012 07:35 PM, Victor Hooi wrote: > > > Hi, > > > > > > I have a directory tree with various XML configuration files. > > > > > > I then have separate classes for each type, which all inherit from a base. > > E.g. > > > > > > class AnimalConfigurationParser: > > > ... > > > > > > class DogConfigurationParser(AnimalConfigurationParser): > > > ... > > > > > > class CatConfigurationParser(AnimalConfigurationParser): > > > > > > > > > I can identify the type of configuration file from the root XML tag. > > > > > > I'd like to walk through the directory tree, and create different objects > > based on the type of configuration file: > > > > > > for root, dirs, files in os.walk('./'): > > > for file in files: > > > if file.startswith('ml') and file.endswith('.xml') and 'entity' > > not in file: > > > with open(os.path.join(root, file), 'r') as f: > > > try: > > > tree = etree.parse(f) > > > root = tree.getroot() > > > print(f.name) > > > print(root.tag) > > > # Do something to create the appropriate type of > > parser > > > except xml.parsers.expat.ExpatError as e: > > > print('Unable to parse file {0} - > > {1}'.format(f.name, e.message)) > > > > > > I have a dict with the root tags - I was thinking of mapping these directly > > to the functions - however, I'm not sure if that's the right way to do it? > > Is there a more Pythonic way of doing this? > > > > > > root_tags = { > > >'DogRootTag': DogConfigurationParser(), > > > 'CatRootTag': CatConfigurationParser(), > > > } > > > > > > Cheers, > > > Victor > > > > Your subject line says you want to create the classes dynamically, but > > that's not what your code implies. if you just want to decide which > > class to INSTANTIATE dynamically, that's easily done, and you have it > > almost right. In your dict you should leave off those parentheses. > > > > > > > > Then the parser creation looks something like: > >parser_instance = root_tags[root.tag] (arg1, arg2) > > > > where the arg1, arg2 are whatever arguments the __init__ of these > > classes expects. > > > > (untested) > > > > -- > > > > DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: TypeError: 'in ' requires string as left operand, not Element
On 2012-12-10 01:19, Dave Angel wrote: On 12/09/2012 07:52 PM, Victor Hooi wrote: Hi, I'm getting a strange error when I try to run the following: for root, dirs, files in os.walk('./'): for file in files: if file.startswith('ml') and file.endswith('.xml') and 'entity' not in file: print(root) print(file) with open(os.path.join(root, file), 'r') as f: print(f.name) try: tree = etree.parse(f) root = tree.getroot() print(f.name) print(root.tag) except xml.parsers.expat.ExpatError as e: print('Unable to parse file {0} - {1}'.format(f.name, e.message)) Where's the printout of the root and file variables? You're asking os.path.join to work on them, and it's clearly upset about their types. I see print statements, so you clearly were thinking along these lines. But you don't show them. BTW, I'd be using print(repr(root)) and print(repr(file)) instead, so you get a better idea of their type and value. My guess for the problem is that you're trashing 'root' with the contents of your try block. Try using a different name for the xml stuff. The error is: Traceback (most recent call last): File "foo.py", line 275, in marketlink_configfiles() File "foo.py", line 83, in bar with open(os.path.join(root, file), 'r') as f: File "C:\Python27\lib\ntpath.py", line 97, in join if path[-1] in "/\\": TypeError: 'in ' requires string as left operand, not Element Cheers, Victor Incidentally, 'file' is a builtin type, so it's probably not a good idea to hide it by using it as your own local variable. It looks like Python 3 to me, which doesn't define 'file'. -- http://mail.python.org/mailman/listinfo/python-list
Re: The Zen of Zope, by Alex Clark
On Sun, 09 Dec 2012 20:13:43 -0500, Alex Clark wrote: import other > The Zen of Zope, by Alex Clark I expect that I would find that hilarious if I knew anything about Zope :) -- Steven -- http://mail.python.org/mailman/listinfo/python-list