I could definitely add the table name to a list as you propose before hand
and then worry about grabbing the table names later.  That is an option.  I
just have to edit the script each time a new client comes on thatÂ’s all.

So in this example, you are returning Test class, this name can be anything
correct?  Because the __classname__ is changed here right?  That is
definitely going to work, perfect!!!

Have you had to use similar code for yourself?

Thank you Ian!

-Lee

-----Original Message-----
From: [email protected] [mailto:[EMAIL PROTECTED] On
Behalf Of Ian Wilson
Sent: Friday, January 26, 2007 6:43 PM
To: [email protected]
Subject: [TurboGears] Re: help with sqlobject classes at runtime


Yeah that makes way more sense.  I found kind of what you were looking
for on the sqlobject mailing list after trying some other things I had
thought of. I don't know how to get the tables names but this will
help after that.  It should make all your table classes in a list.  So
that tableClasses[0] will be an SQLObject class(not an instance):

def getTableNames():
    #not quite sure what to put here but I'll just assume
    return ['table1', 'table2', 'table3']

def getTableClasses(tableNames):
    tableClasses = []
    for tableName in tableNames:
        tableClasses.append(makeTable(tableName))
    return tableClasses

def makeTable(name):
    """ Copied from here
        http://article.gmane.org/gmane.comp.python.sqlobject/7719
    """
    class Test(SQLObject):
        class sqlmeta:
           table = name

        def __classinit__(cls, new_attrs):
            cls.__name__ = name
            SQLObject.__classinit__(cls, new_attrs)

    return Test

Do you know the table names you need to get classes for at run time ?

-Ian

On 1/26/07, Lee Connell <[EMAIL PROTECTED]> wrote:
> Not a problem, I didn't take it as criticism.  The tables currently exist
in
> the database, but at anytime a new table can be added to the database that
> contains the same structure as all the other tables im interested in here.
>
> The existing application is a syslog application that pulls data from SNMP
> and fills these tables with data.  At anytime it can create a new table
> based on new SNMP captures, meaning new a new client comes on board.
>
> I am building a script that queries every single table in the database and
> grabs information based on a specific SNMP level, in my case anything
> "critical" within the last couple hours, grab it and send an email alert
to
> a specific recipient.
>
> I want to loop through each table, make the necessary queries and send an
> email based on the results.
>
> I was hoping, to make it easier i could query the database and grab all
the
> tables and make classes out of them during runtime.
>
> Does this make sense?
>
>
> On 1/26/07, Ian Wilson <[EMAIL PROTECTED]> wrote:
> >
> > Sorry earlier I wasn't critisizing your design I know how it is to try
> > and match other system designs.  It sucks.  But I am kind of confused
> > still on what actually needs to happen.  Do the tables exist in the
> > database and you just need matching sqlobject classes?  Or do you need
> > both to create the tables in the database and get a representative
> > sqlobject class ?
> >
> > -Ian
> >
> > On 1/25/07, Lee Connell < [EMAIL PROTECTED]> wrote:
> > >
> > > Hi,
> > >
> > > The reason I am doing this is because I have multiple tables in my
> database
> > > that have all the same fields and I want to be able to add tables to
my
> > > script at anytime.  These tables hold information about new clients.
> The DB
> > > design was not mine and it's how it works for a syslog application we
> are
> > > using.
> > >
> > > I want to be able to run through all of these tables and query
> information
> > > and send emails based on the data pulled.  Since each table is going
to
> be
> > > accessed the same way I just want to loop through each table/sqlobject
> > > class, query data and send an email.
> > >
> > > Is there an easier way to do this?
> > >
> > > -----Original Message-----
> > > From: [email protected] [mailto:[EMAIL PROTECTED]
> On
> > > Behalf Of Ian Wilson
> > > Sent: Thursday, January 25, 2007 8:18 PM
> > > To: [email protected]
> > > Subject: [TurboGears] Re: help with sqlobject classes at runtime
> > >
> > >
> > > I am confused on why or what you are doing but try this:
> > >
> > > Set the default to empty for level 2 and you won't get that error:
> > >
> > > level2 = StringCol(default='')
> > >
> > >
> > >
> > > On 1/25/07, Lee Connell <[EMAIL PROTECTED]> wrote:
> > > >
> > > > /// ERROR (if I don't override __init__ in my sqlobject class)///
> > > >
> /////////////////////////////////////////////////////////////////
> > > >     c = help.classFromModule('__main__', 'SyslogPoll2')()
> > > >   File
> > > >
> > > "C:\Python24\lib\site-packages\sqlobject-
> 0.7.2-py2.4.egg\sqlobject\declarati
> > > > ve.py", line 93, in _wrapper
> > > >     return fn(self, *args, **kwargs)
> > > >   File
> > > >
> > >
> "C:\Python24\lib\site-packages\sqlobject-0.7.2-py2.4.egg\sqlobject\main.py
> ",
> > > > line 1203, in __init__
> > > >     self._create(id, **kw)
> > > >   File
> > > >
> > >
>
"C:\Python24\lib\site-packages\sqlobject-0.7.2-py2.4.egg\sqlobject\main.py",
> > > > line 1222, in _create
> > > >     raise TypeError, "%s() did not get expected keyword argument %s"
%
> > > > (self.__class__.__name__, column.name)
> > > > TypeError: SyslogPoll2() did not get expected keyword argument
level2
> > > >
> > > >
> > > >
> > > > /// Calling Code ///
> > > > ////////////////////
> > > > class SyslogPoll2(SQLObject):
> > > >     class sqlmeta:
> > > >         table = "SyslogPoll"
> > > >
> > > >     level2 = StringCol()
> > > >
> > > > help = DBHelper()
> > > > c = help.classFromModule('__main__', 'SyslogPoll2')()
> > > >
> > > >
> > > >
> > > > /// Helper Code ///
> > > > ///////////////////
> > > > class DBHelper:
> > > >     def classFromModule (self, module, className):
> > > >         mod = __import__ (module)
> > > >         return getattr (mod, className)
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > > -----Original Message-----
> > > > From: [email protected] [mailto:
> [EMAIL PROTECTED] On
> > > > Behalf Of Patrick Lewis
> > > > Sent: Tuesday, January 23, 2007 12:58 AM
> > > > To: TurboGears
> > > > Subject: [TurboGears] Re: help with sqlobject classes at runtime
> > > >
> > > >
> > > > Not quite sure what the real problem you are facing is, but if all
you
> > > > want to do is call the 'send' method without instantiating your
class,
> > > > can you use the @staticmethod (or perhaps @classmethod) decorator?
> > > > e.g.
> > > >
> > > > class SyslogPoll2(SQLObject):
> > > >
> > > >     @staticmethod
> > > >     def send(data):
> > > >         print data
> > > >
> > > > I don't think you want to be overriding sqlobject's __init__
> > > >
> (http://www.sqlobject.org/SQLObject.html#initializing-the-objects),
> > > > apparently that is Very Bad.
> > > >
> > > > If you want to do some introspection of the class to know what the
> > > > class needs to initialize, you should be able to get everything you
> > > > need out of SyslogPoll2.sqlmeta.columns attribute.
> > > >
> > > >
> > > >
> > > > --
> > > > No virus found in this incoming message.
> > > > Checked by AVG Free Edition.
> > > > Version: 7.5.432 / Virus Database: 268.17.8/648 - Release Date:
> 1/23/2007
> > > > 11:04 AM
> > > >
> > > >
> > > > --
> > > > No virus found in this outgoing message.
> > > > Checked by AVG Free Edition.
> > > > Version: 7.5.432 / Virus Database: 268.17.11/652 - Release Date:
> 1/25/2007
> > > > 3:32 PM
> > > >
> > > >
> > > >
> > > > >
> > > >
> > >
> > >
> > > --
> > > No virus found in this incoming message.
> > > Checked by AVG Free Edition.
> > > Version: 7.5.432 / Virus Database: 268.17.11/652 - Release Date:
> 1/25/2007
> > > 3:32 PM
> > >
> > >
> > > --
> > > No virus found in this outgoing message.
> > > Checked by AVG Free Edition.
> > > Version: 7.5.432 / Virus Database: 268.17.11/652 - Release Date:
> 1/25/2007
> > > 3:32 PM
> > >
> > >
> > >
> > > >
> > >
> >
> >
>
>
>  >
>


-- 
No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.5.432 / Virus Database: 268.17.12/653 - Release Date: 1/26/2007
11:11 AM
 

-- 
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.5.432 / Virus Database: 268.17.12/653 - Release Date: 1/26/2007
11:11 AM
 


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears" 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/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to