kind of newbie question

32 views
Skip to first unread message

Richard Vézina

unread,
Oct 19, 2010, 6:14:17 PM10/19/10
to web...@googlegroups.com
Hello,

I would do this :

def grid():
    if request.args[0] in tableSubSet:
        allFieldsSet=set(db['table1'].fields) # all the fields in the table
        fieldsBlackListSet=set(['f1ToRem','f2ToRem','f1ToRem']) # fields to be remove from the grid
        allFieldsSet-=fieldsBlackListSet # removing fields to be removed
        fieldsRequested=[]
        for field in allFieldsSet:
            fieldsRequestedList.append('db.test_activity.'+field) # building the select list of elements

        a=','.join(fieldsRequestedList) # transform the list into a string

        rows = db().select(a) # BLOCKED BECAUSE OF THE QUOTES ('db.table1.field1, db.table1.field2, etc.')!!!
        table=SQLTABLE(rows)
        return dict(table=table)

Thanks for your help.

Richard

DenesL

unread,
Oct 19, 2010, 10:52:00 PM10/19/10
to web2py-users
Hello Richard,

if I understood correctly, you want:

...
BlackList = [ 'fieldx', 'fieldy', ...]
rows = db().select(*[f for f in db['table1'].fields if f not in
BlackList])
...

Denes

On Oct 19, 6:14 pm, Richard Vézina <ml.richard.vez...@gmail.com>
wrote:
> Hello,
>
> I would do this :
>
> def grid():
>     if request.args[0] in tableSubSet:
>         allFieldsSet=set(db['table1'].fields) # all the fields in the table
>         fieldsBlackListSet=set(['f1ToRem','f2ToRem','f1ToRem']) # fields to
> be remove from the grid
>         allFieldsSet-=fieldsBlackListSet # removing fields to be removed
>         fieldsRequested=[]
>         for field in allFieldsSet:
>             fieldsRequestedList.append('db.test_activity.'+field) # building
> the select list of elements
>
>         a=','.join(fieldsRequestedList) # transform the list into a string
>
>         rows = db().select(a) # BLOCKED BECAUSE OF THE QUOTES
> (*'*db.table1.field1,
> db.table1.field2, etc.*'*)!!!

Richard Vézina

unread,
Oct 20, 2010, 11:16:34 AM10/20/10
to web...@googlegroups.com
Near of it... Your list comprehension is doing much simpler then I were doing what it should do.

But the problem is still there...

First the 


[f for f in db['table1'].fields if f not in BlackList]

Return a list (['field1','field2', etc.]) without the rest of the information needed : "db.table"

I was having in a :

'db.table.field1, db.table.field2, etc.'

Second it is a list.

My fundamental problem is that I try tu use the content of a variable as code for my app and that should not be a common thing in programmation since I am not a experienced programmer.

I don't know if I should do something else to resolve my problem... 

Basically I would do exactly the inverse of columns= do in SQLTABLE

Richard

DenesL

unread,
Oct 20, 2010, 10:31:09 PM10/20/10
to web2py-users
It really depends on how you select and which row indexing you want.

To use db().select(...)
you have to specify table.field pairs in the select (without the db
part)
and to obtain the value of field1 you use row['table.field1'].

If you are selecting fields from only one table the alternative would
be
db(db.table.id>0).select(*[list of fields without table prefix])
and each value is accessible as row['field'].

To use db().select(...) as in your first post
you would build the comprehension with:
L=[ 'table2.%s'%f for f in db['table1'].fields if f not in blacklist ]

which creates a list L with:
'table2.field1', 'table2.field2', ...
where field1, field2, ... are field names from table1 that are not
black listed,

and that list can be used directly in the select:
rows=db().select(*L) # note the * before the list


Hope this helps,
Denes


On Oct 20, 11:16 am, Richard Vézina <ml.richard.vez...@gmail.com>
wrote:
> Near of it... Your list comprehension is doing much simpler then I were
> doing what it should do.
>
> But the problem is still there...
>
> First the
>
> [f for f in db['table1'].fields if f not in BlackList]
>
> Return a list (['field1','field2', etc.]) without the rest of the
> information needed : "db.table"
>
> I was having in a :
>
> 'db.table.field1, db.table.field2, etc.'
>
> Second it is a list.
>
> My fundamental problem is that I try tu use the content of a variable as
> code for my app and that should not be a common thing in programmation since
> I am not a experienced programmer.
>
> I don't know if I should do something else to resolve my problem...
>
> Basically I would do exactly the inverse of columns= do in SQLTABLE
>
> Richard
>

Richard Vézina

unread,
Oct 21, 2010, 10:27:06 AM10/21/10
to web...@googlegroups.com
Do I have to import a python module... In Ipython I was having synthax error with *[list comp exp of yesterday]

Richard

Richard Vézina

unread,
Oct 21, 2010, 10:32:53 AM10/21/10
to web...@googlegroups.com
In [4]: *[f for f in db['test_activity'].fields if f not in fieldsBlackList]------------------------------------------------------------
   File "<ipython console>", line 1
     *[f for f in db['test_activity'].fields if f not in fieldsBlackList]
     ^
SyntaxError: invalid syntax


In [5]: (*[f for f in db['test_activity'].fields if f not in fieldsBlackList])
------------------------------------------------------------
   File "<ipython console>", line 1
     (*[f for f in db['test_activity'].fields if f not in fieldsBlackList])
      ^
SyntaxError: invalid syntax


And it raise :

In [6]: db().select(*[f for f in db['test_activity'].fields if f not in fieldsBlackList])
------------------------------------------------------------
SyntaxError: Set: no tables selected

In the select expression...

Richard

Richard Vézina

unread,
Oct 21, 2010, 10:37:54 AM10/21/10
to web...@googlegroups.com
Ok,

It works with :

L=[ 'table2.%s'%f for f in db['table1'].fields if f not in blacklist ]
rows=db().select(*L)

What does the * exactly it multiplier sign but in that particular case it's not seems to act like iterator...

Richard

On Wed, Oct 20, 2010 at 10:31 PM, DenesL <dene...@yahoo.ca> wrote:

mdipierro

unread,
Oct 21, 2010, 10:38:16 AM10/21/10
to web2py-users
You are passing to select a list of field names and not a list fo
fields. Try

db().select(*[f for f in db['test_activity'] if not f.name in
fieldsBlackList])


On Oct 21, 9:32 am, Richard Vézina <ml.richard.vez...@gmail.com>
wrote:

Richard Vézina

unread,
Oct 21, 2010, 11:00:17 AM10/21/10
to web...@googlegroups.com
Thank you very much I got it to work.

But I think it just a kind of hack because db.table.readable=False not seems to work for crud.select and SQLTABLE.

I sent an other e-mail about that today.


Richard

DenesL

unread,
Oct 21, 2010, 4:36:53 PM10/21/10
to web2py-users
I hope you are certain that is what you want.

db().select(*[f for f in db['test_activity'] if not f.name in
fieldsBlackList])

will get the field names from test_activity and not from table1 as per
your initial post, but table1 might have only a subset of the fields,
so the resulting list after the blacklist filter is applied can be
different.


Denes


On Oct 21, 11:00 am, Richard Vézina <ml.richard.vez...@gmail.com>
wrote:
> Thank you very much I got it to work.
>
> But I think it just a kind of hack because db.table.readable=False not seems
> to work for crud.select and SQLTABLE.
>
> I sent an other e-mail about that today.
>
> Richard
>

Richard Vézina

unread,
Oct 22, 2010, 10:14:56 AM10/22/10
to web...@googlegroups.com
I finally get it works like I was expecting like this :

@auth.requires_login()
def select():
    if request.args(0) in actualTablesList:
        if db[request.args(0)].id>0:
            sampleIdEqId={}
            listId=[]
            for r in db(db[request.args(0)].id>0).select(db[request.args(0)].id):
                listId.append(r.id)
            i=0  
            for row in db(db[request.args(0)].id>0).select(db[request.args(0)].sample_id):
                sampleIdEqId[row.sample_id]=listId[i]
                i=i+1
            db[request.args(0)].sample_id.represent=\
                lambda sample_id: A("%(sample)s" %db.v_sample_num_all[sample_id],\
                _href=URL(r=request,f='read',args=request.args(0)+'/'+str(sampleIdEqId.get(sample_id))))

            # Should be replace by db.TABLE.FIELDNAME.readable=False when it will be available for SQLTABLE and crud.select
            # Begin
            fieldsBlackList=[
                db[request.args(0)].id.name,
               'FIELD1',
               'FIELD2'
                ]
            field2Show=[ request.args(0)+'.%s'%f for f in db[request.args(0)].fields if f not in fieldsBlackList ] 
            rows=db().select(*field2Show)
            # End
            # Should be replace if lable are use in SQLTABLE in the future
            # Begin
            headersRepresent={}
            for f in db[request.args(0)].fields:
                headersRepresent[str(db[request.args(0)][f])]=db[request.args(0)][f].label
            # End
            
            table=SQLTABLE(rows,headers=headersRepresent,_class='sortable',truncate=None)
    
            return dict(table=table)
        response.flash = T('There is no data')


And Massimo fixed a major bug that relates to all these post I made this week ;-)

I will look at it later today if I have time.

I really appreciate your help!

Richard
Reply all
Reply to author
Forward
0 new messages