Skip to content

Add a silly automatic registry of known formatter classes - #2852

Merged
mdboom merged 3 commits into
astropy:masterfrom
embray:units/formatter-registry
Aug 25, 2014
Merged

mdboom merged 3 commits into
astropy:masterfrom
embray:units/formatter-registry

Conversation

@embray

@embray embray commented Aug 13, 2014

Copy link
Copy Markdown
Member

One advantage of doing things this way, is that it enables user-defined unit formatters (all one has to do is define a subclass of astropy.units.format.Base). This should generally be discouraged, but at the same time it's good to have the ability available when absolutely necessary.

This also allows the name used to look up a formatter to be different from the class name. This was motivated primarily by my rambling here. All formatter classes now have a .name class attribute which contains the name they are looked up by. By default this is just __class__.__name__.lower(), but it may be customized. For example in the case of #2622 the class can be defined:

class LatexInline(Base):
    name = 'latex_inline'

where the name 'latex_inline' is perhaps a bit easier on the eyes than 'latexinline'.

…antage of enabling user-defined formatters. astropy.units.format.__init__ should still import all the built-in formatters to ensure they are added to the registry, so this is a minimal change.
@embray

embray commented Aug 13, 2014

Copy link
Copy Markdown
Member Author

It appears this did not go over so well on Python 2 (I use Python 3 by default now). I'll get to the bottom of things...

…ase) and include a type check in case the format is not a valid string.
@embray

embray commented Aug 13, 2014

Copy link
Copy Markdown
Member Author

Updated with a few small fixes for cross-compat, as well as some extra type checking that wasn't there before, along with a more useful exception message for when the requested formatter isn't found.

@embray embray added this to the v1.0.0 milestone Aug 19, 2014
@embray

embray commented Aug 19, 2014

Copy link
Copy Markdown
Member Author

@mdboom Any objections to this?

@mhvk

mhvk commented Aug 19, 2014

Copy link
Copy Markdown
Contributor

@embray - looks good to me.

@mdboom

mdboom commented Aug 25, 2014

Copy link
Copy Markdown
Contributor

Looks good to me. I restarted Travis now that (I think) the Numpy doc issue is fixed.

mdboom added a commit that referenced this pull request Aug 25, 2014
Add a silly automatic registry of known formatter classes
@mdboom
mdboom merged commit 88d9db8 into astropy:master Aug 25, 2014
@embray
embray deleted the units/formatter-registry branch August 26, 2014 18:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants