Diaoul / babelfish

BabelFish is a Python library to work with countries and languages
BSD 3-Clause "New" or "Revised" License
25 stars 14 forks source link

Import error and weirdness #5

Closed Diaoul closed 10 years ago

Diaoul commented 10 years ago

There are some tricks with babelfish currently. I'm not sure it is the right thing to do to load the entry points during import.

Workflow:

Boom.

Should we use lazy loading?

Toilal commented 10 years ago

Instead of loading converters in __init__.py of babelfish package, a load_converters() method declared in babelfish __init__.py could be called manually to load converters (language and country) from entry points, inside subliminal initialisation code ...

wackou commented 10 years ago

Another issue that I have with the setuptools entrypoints is that they don't seem to load from within ipython (have no idea why, tried googling it but to no avail...)

$ python -c "import babelfish; c = babelfish.Language('eng'); print c.alpha2"
en

$ ipython -c "import babelfish; c = babelfish.Language('eng'); print c.alpha2"
WARNING: Attempting to work in a virtualenv. If you encounter problems, please install IPython inside the virtualenv.
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-1-c83b941aa497> in <module>()
----> 1 import babelfish; c = babelfish.Language('eng'); print c.alpha2

/Users/wackou/.virtualenvs/babelfish/lib/python2.7/site-packages/babelfish/language.pyc in __getattr__(self, name)
     65     def __getattr__(self, name):
     66         if name not in CONVERTERS:
---> 67             raise AttributeError
     68         return CONVERTERS[name].convert(self.alpha3, self.country.alpha2 if self.country is not None else None)
     69

AttributeError:
Diaoul commented 10 years ago

@Toilal: Indeed we can add load_converters() in subliminal.__init__.py after all imports and it should work as expected however I liked the idea not to depend on any extra code to get it working. @wackou: Can you try iterating over entry points manually with ipython?

I was thinking about removing the code that fill the CONVERTERS and make a converters lazy load: load them when we need them. Combined with the idea of @Toilal of being able to manually load them. Something like a get_converter(name) function. This would've the same purpose of the CONVERTERS dict.

Diaoul commented 10 years ago

@wackou: also, what version of setuptools are you using?

Diaoul commented 10 years ago

Should be fixed with 61ca703f7e1df5b00ff8d3c2f3af383f9db9eeb4 Sorry for the big commit, its late. FYI I use 120 char wrap column, this is approx GitHub limit so it displays nicely, plus it's 2013, we all have at least 19' screens and 120 fits well in it.

TODO before release: update docs + changelog

Diaoul commented 10 years ago

e73b1451a8d8bf0d4e22535009f90ddadf135f79 fixes lazy loading from reverse conversions Language.frommycode

Documentation is up to date, I'll do the changelog tomorrow. @wackou: have a look please, I think we're ready for a release