No subject


Sat Dec 10 13:40:36 UTC 2011


"Port to Python 3. This adds a dependency on six and removes support for
Python 2.5"

So if we want to use OrderedDict (which is from Python >= 2.7), we just
need to bridge the Python 2.6 version with the ordereddict package:

http://pypi.python.org/pypi/ordereddict/1.1

This would introduce a new dependency for zope.schema on Python 2.6, I
don't know if that's acceptable or not. 

In setup.py one could specify the extra dependency only for Python <
2.7:

> import sys
> 
> REQUIRES = ['setuptools', 
> 	    'zope.interface >= 3.6.0', 
> 	    'zope.event',
> 	    'six', 
> 	   ]
> 
> if sys.version_info < (2 , 7):
>     REQUIRES += ['ordereddict'],
> 
> setup(
> # [...]
>     install_requires=REQUIRES,
> # [...]
> )

On Thu, 2012-01-26 at 15:42 +0100, Charlie Clark wrote:
> 
> Back to bike-shedding. As I was intrigued by the whole thing I've spent  
> some time looking at the code. I'm not too happy on the use of nested  
> functions as I find they obscure code, particularly when used recursively.  
> I think that "createTree" and "recurse" should be written as separately  
> testable generators. 

Ok, I've refactored the nested methods out into class or instance
methods. 

I however don't see how one could use a generator for the recursive
methods that return dictionaries.

With regards to the "recurse" method (now called _getPathToTreeNode), I
don't see how one could use a generator in a more efficient manner than
the normal recursion that's being used currently.

I played around with it and the best I could come up with is this:

        def generator(_dict, value, path=[]):
            if value in _dict.keys():
                yield path+[value]

            for key in _dict.keys():
                for path in recurse(_dict[key], value, path+[key]):
                    if value in path:
                        yield path


You still have to recurse through the different branches until you find
the node that you are looking for and you still need to store the path
in a list. So what would be the added value?

What's more, the generator returns a list within a list, like so:
[['Regions', 'Germany', 'Bavaria']], which I find clunky.

> I also don't see a need for createTerm in this  
> particular class and the subsequent awkward call from createTree. As it  
> stands it is copy & paste both in method and test. 

The reason for the "createTerm" method in the SimpleVocabulary is to
allow people to override it to provide support for other term types.

This also applies to the TreeVocabulary.

> If you must have it with the same implementation

> createTree = SimpleVocabulary.createTree 

> does the job just fine 

I guess you mean "createTerm" ?

I'm not convinced that this is better. 

This creates a dependency on a method from another class that's not
being subclassed.

What if createTerm is changed in the SimpleVocabulary in such a way that
doesn't take the TreeVocabulary into account?

> but I don't see the advantage of  
> cls.createTerm(*args) over SimpleTerm(*args)

See above. "createTerm" is there to let developers override it and
provide their own term objects.

> As I said this is bike-shedding but I think our source code should be  
> written with a view to being read and probably copied verbatim. 
> With that in mind I prefer readability and testability over integration. 

So why then cannot I copy "createTerm" from SimpleVocabulary? :D

> In the end it tends to make things easier to use. The exceptions where refactoring to  
> produce slightly uglier code but with significant performance hopefully  
> prove the rule.

Well, your suggestions concerning the nested methods had me thinking and
in the end resulted in significant refactoring. I think it was worth it
though, so thanks.

J-C



More information about the Zope-Dev mailing list