On Mar 6, 2009, at 10:37 PM, [email protected] wrote:
>
> I have some metadata on table and some of the columns and would like
> to surface these as docstrings on the mapped class and columns.
>
> If table foo has columns i, j, k with comments 'apple', 'banana',
> 'pear', respectively, and the table is mapped via class Foo then I
> would like the programmer to do a help(Foo) and see something like:
>
> i()
> apple
>
> j()
> banana
>
> k()
> pear
>
> or whatever the common Python idiom is. I am looking for something
> that will work well for interactive work with ipython. In our case,
> the comments are MS_Description properties from the
> sys.extended_properties table in SQL Server. I searched through the
> list archives and see that there has already been some discussion
> about providing support for comments in the DDL producerers. Although
> I am more interested in the mapper side of things, I can see how
> sp_addextendedproperty, sp_updateextendedproperty could be used to
> set the comments as part of the DDL generation.
>
> Since I am code-generating all the alchemy models right now, it seems
> reasonable to poke in the comment into the docstring of the class and
> the the __doc__ attribute column properties after the mapper()
> invocation. Or should one use some magic Python hook to get the value
> of the docstring from elsewhere? For reference, I append the a
> fragment of the code that retrieves the table and column
> MS_Description properties that are widely used by a number of SQL
> Server tools.
>
reflection of column notes can be added, im not sure if any dialects
support that currently. the plumbing to propagate that from info
column to instrumented descriptor could be done using an
InstrumentationManager, which allows you to receive events as
descriptors are placed on classes.
from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import attributes, interfaces, properties
Base = declarative_base()
class AttachNotesAsDocstring(interfaces.InstrumentationManager):
def instrument_attribute(self, class_, key, attr):
if isinstance(attr.property, properties.ColumnProperty):
attr.__doc__ = attr.property.columns[0].info.get('notes')
attributes.instrumentation_finders.insert(0, lambda cls:
AttachNotesAsDocstring)
class User(Base):
__tablename__ = "users"
id = Column(Integer, primary_key=True, info={'notes':'the primary
key'})
name = Column(String, info={'notes':'the name'})
help(User)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---