thanks Denes,

I think I understand it now.

I often make this mistake,
I start as
  A.select ( 'Name', 'Module' )

now if the selct list becomes too large, I put it in an internediate variable, 
just by cutting and
pasting
  Select = 'Name', 'Module'
  A.select ( Select )
and yes it's Python, now Select is packed :-(

Or I start with a single Field name
  Select = 'Name'
  A.select ( Select )
And in my ignorance I add an extra fieldname to Select
  Select = 'Name', 'Module'
same story :-(

As web2py is so tolerant,
I wonder why web2py doesn't it support the last remaining possibilities:

Set.select ( x ), works if x =
+ nothing
+ DB.Table.ALL
+ fieldname as a string
+ FieldObject as DB.Table.Field
+ FieldObject as DB.Table [ 'Field' ]
+ unpacked iterable of fieldnames (each as a string)
+ unpacked iterable of FieldObjects

Set.select ( x), doesn't work if x =
- empty string
- None
- string, where fields are comma separated
- packed iterable of fieldnames (each as a string)
- packed iterable of FieldObjects

as an excercise, I wrote a litlle wrapper, that takes anything,
very handy for people like me ;-)

    def Test_Select ( Select, Text ) :
      from   General_Globals import Iterable
      if not ( Select ) :
        print Text, A.select ()[:2]
        return
      if isinstance ( Select, basestring ) and ( ',' in Select ) :
        Select = Select.split(',')
      if Iterable ( Select ) :
        print Text, A.select ( *Select )[:2]
      else :
        print Text, A.select ( Select )[:2]

    Test_Select ( '', 'Empty string:')
    Test_Select ( None, 'None:')
    Test_Select ( 'Name,Module', 'Comma separated string:')
    Test_Select ( ['Name', 'Module'], 'Packed string iterable:')
    Test_Select ( [DB.VraagList.Name, DB.VraagList.Module], 'Packed iterable of 
FieldObjects')
    Test_Select ( [DB.VraagList['Name'], DB.VraagList['Module']], 'Packed 
iterable of FieldObjects')


On 30-11-2010 15:10, DenesL wrote:
> On Nov 30, 7:57 am, Stef Mientki <[email protected]> wrote:
>> hello,
>>
>> I'm trying to build some (not trivial) queries and encounter a number of 
>> problems
>> my first problem is that a space makes a huge difference,
>> is that to be expected ?
>>
>> this *works* as (I) expected
>>     Select = "Name, Test_Count"
>>     Rows = DB ( Query ).select ( Select )
> it is select(*fields, **attributes)
> where fields is a alist and attributes is a dictionary
> (according to python not web2py)
>
>>     print Rows[0]
>> <Row {'_extra': <Row {'Test_Count': 1000, 'Name': u'breath_algvm'}>}>
> here you are just lucky that the internal processing builds something
> similar to what is required for the real DB query, you can see it
> using:
> DB(Query)._select(Select) # note the underscore
>
>> but removing the space from the string, gives me an *unexpected result*
>>     Select = "Name,Test_Count"     ###  <-- space removed
>>     Rows = DB ( Query ).select ( Select )
>>     print Rows[0]
>> <Row {'_extra': <Row {'Name,Test_Count': u'breath_algvm'}>}>
>>
>> The second problem, has to do with what's the preferred substitution, string 
>> or objects ?
>>
>> this *works* :
>>     Select = DB.VraagList.Name
>>     Rows = DB ( Query ).select ( Select )
>>     print Rows[0]
>>
>> This *doesn't work*
>>     Select = DB.VraagList.Name, DB.VraagList.id
> python (not web2py) creates a tuple with both fields
> which is usable as explained below
>
>>     Rows = DB ( Query ).select ( Select )
> the correct call (python's rules) would be:
> Rows = DB  ( Query ).select( *Select )
>
>>     print Rows[0]
>>
>> for the record, this is the Query in all the above
>>     PID = 1018
>>     Query = ( DB.VraagList.id == DB.Opnamen.VLID ) & \
>>             ( DB.Opnamen.PID == PID )
>>
>> thanks,
>> Stef Mientki

Reply via email to