How to know if you're getting a Set when you're expecting a dict?

376 views
Skip to first unread message

Bill Thayer

unread,
Nov 12, 2012, 7:09:21 PM11/12/12
to web...@googlegroups.com
<type 'exceptions.TypeError'> 'Set' object has no attribute '__getitem__' 
db.person._after_insert.append(lambda f,id: pprint(f,id)) #book's example
#my code in model file after table definition code.
db
.extracted_linear._after_insert.append(
   
lambda f, id:wiki_update_or_insert(
       
'extracted_linear',
        f
["name"],
        table_slug
(f["name"]),
        id
))
The book says Here f is a dict of fields passed to insert or update, id is the id of the newly inserted record, s is the Set object used for update or delete.

Bill Thayer

unread,
Nov 12, 2012, 9:36:49 PM11/12/12
to web...@googlegroups.com
After finding some code in a class called RecordUpdater in gluon/dal.py i think I figured out how to get past my arguments as a set or a dict....well....I was getting a keyerr for 'name' so I put it in a try-catch but now I'm getting a keyerror for 'id'
In the code below I've only been working on the _after_update.

Wonder why 'name' and 'id' throw the error but 'tablename' does not?

#This is after my table definition inside my model file
db.extracted_linear._after_insert.append(
    lambda f, id:wiki_update_or_insert(
        tablename='extracted_linear',
        id=id,
        name=f['name'] or f['measuremnt']))
db.extracted_linear._after_update.append(
    lambda s, id:wiki_update_or_insert(
        tablename='extracted_linear',
        id=id,
        colset=s(db.extracted_linear.id==id)))

#Here is function in the controller
def wiki_update_or_insert(**fields):
    newfields = fields or dict(colset)
    try:
        title=newfields['name'] or newfields['tablename'] + ' ' + newfields['id']['id']
    except:
        title=newfields['tablename'] + ' ' + newfields['id']['id']
    db.wiki_page.update_or_insert(
        title=title,
        slug=table_slug(title),
        body=MARKMIN('#ADD CODE TO DISPLAY GRAPHS HERE'),
        tablename=newfields['tablename'],
        record_id=newfields['id']['id'])




Traceback (most recent call last):
File "C:\web2py_src_2.2.1\web2py\gluon\restricted.py", line 212, in restricted
exec ccode in environment
File "C:/web2py_src_2.2.1/web2py/applications/PROD/controllers/default.py", line 173, in <module>
File "C:\web2py_src_2.2.1\web2py\gluon\globals.py", line 188, in <lambda>
self._caller = lambda f: f()
File "C:\web2py_src_2.2.1\web2py\gluon\tools.py", line 2911, in f
return action(*a, **b)
File "C:/web2py_src_2.2.1/web2py/applications/PROD/controllers/default.py", line 67, in linear_manage
form = SQLFORM.smartgrid(db.extracted_linear)
File "C:\web2py_src_2.2.1\web2py\gluon\sqlhtml.py", line 2376, in smartgrid
user_signature=user_signature, **kwargs)
File "C:\web2py_src_2.2.1\web2py\gluon\sqlhtml.py", line 1882, in grid
next=referrer)
File "C:\web2py_src_2.2.1\web2py\gluon\html.py", line 2170, in process
self.validate(**kwargs)
File "C:\web2py_src_2.2.1\web2py\gluon\html.py", line 2109, in validate
if self.accepts(**kwargs):
File "C:\web2py_src_2.2.1\web2py\gluon\sqlhtml.py", line 1473, in accepts
self.id_field_name]).update(**fields)
File "C:\web2py_src_2.2.1\web2py\gluon\dal.py", line 8814, in update
ret and [f(self,update_fields) for f in table._after_update]
File "C:/web2py_src_2.2.1/web2py/applications/PROD/models/db_wizard.py", line 118, in <lambda>
colset=s(db.extracted_linear.id==id)))
File "C:/web2py_src_2.2.1/web2py/applications/PROD/controllers/default.py", line 44, in wiki_update_or_insert
title=newfields['tablename'] + ' ' + newfields['id']['id']
KeyError: 'id'


Bill Thayer

unread,
Nov 13, 2012, 7:53:01 PM11/13/12
to
Looks like I worked most of the bugs out of this. Hope it saves someone a lot of time. If you know of a more obvious or elegant solution please post it. Also, my slug field is returning a tuple now. Still works as a slug but I wish I didn't have the |slug|None| format. (edited to add the _after_insert function.)



def insert_wiki_after_insert(table, f, id):
    """Used for auto creation of wiki pages when a new item is added to database
       To use:
       In model file after table definition enter lines:
       db.mytable.wiki_page.required=False
       db.mytable._after_insert.append(lambda f,id: \
                                    insert_wiki_after_insert(db.mytable, f, id))
       @see_also gluon.dal.RecordUpdater,
    """
    tablename=table._tablename
    
    name = f['name'] or f['title'] or tablename + ' ' + id
    wiki_page=db.wiki_page.update_or_insert(
                db.wiki_page.title==name,
                title=name.strip(),
                slug=table_slug(name.strip()),
                body='#ADD PLUGIN CODE TO DISPLAY GRAPHS HERE\n' + \
                         '##Use WIKI menu to edit or delete this page',
                changelog='inserted after ' + tablename + ' table update',
                tags=None,
                tablename=tablename,
                record_id=id)
    db(table.id==id).update_naive(wiki_page=wiki_page)
    return

def insert_wiki_if_not_exists(table, s, f):
    """Used for auto creation of wiki pages for use with items already
       in your table. On any update to the table the wiki page will be create if it
       doesn't exist.
       To use:
       In model file after table definition enter lines:
       db.mytable._after_update.append(lambda s,f: \
                                    insert_wiki_if_not_exist(db.mytable, s, f))
       @see_also gluon.dal.RecordUpdater,
    """
    tablename=table._tablename
    table_wikis_set=db(db.wiki_page.tablename==tablename)

    for s_row in s.select():
        for row in table_wikis_set.select():
                id = s_row.id
                name = s_row.name or tablename + ' ' + id
                f['wiki_page']=db.wiki_page.update_or_insert(
                    db.wiki_page.title==name,
                    title=name,
                    slug=table_slug(name.strip()),
                    body='#ADD PLUGIN CODE TO DISPLAY GRAPHS HERE\n' + \
                             '##Use WIKI menu to edit or delete this page',
                    changelog='inserted after ' + tablename + ' table update',
                    tags=None,
                    tablename=tablename,
                    record_id=id)
                s.update_naive()
                return
        
Reply all
Reply to author
Forward
0 new messages