HI Anthony,

Thanks for the follow up and comments. I found a simple solution,
described just below. I consider my problem solved. However, you asked
for more information which is also below. If you really wish to take
this on to investigate further for a possible bug report - let me know
and I'll send you a stripped down app to experiment with.

 The short summary, my solution uses the dictionary method
iteritems(), maybe not so nice to look at but it works. I think the
problems with the more direct solutions are a result of that among
python254, pythno271,  active python261, and Web2Py there are
differences in what python returns in some dictionary methods, and the
operators [c] and [x][c]. Most of the time when Web2py performed some
conversion from dictionary to list, it was not possible to access the
results as a list, using [0], or access it as a dictionary with a key,
or access it as a string. I did not notice this happening when working
similar test examples with similar python code just running in a
console.

thanks, james c.


================
Late Friday, while grasping at things like string output conversions
as possible solutions, I found  alternate dictionary iterative methods
- after which I had a solution working in about two minutes. It just
worked. here it is:

cpp=db(db.customer_names.id==name_index).select().as_dict()
for kk, vv in cpp.iteritems():
      cp1 = kk
      cpp = vv['name']

I consider this solving my problem. However, I did continue trying to
get something, similar to what you suggest working.

db(db.customer_names.id==names_index).select('name').first().name

I could get something similar to this running in the console. My
web2py is using the default Python254. With Python254, adding first()
or .name, results in a compile or syntax error. The least common parts
of this code that I got to work is:

pcc=db(db.customer_names.id==names_index).select('name')

this is returned through to the view as  return
dict(display_name=pcc...)  and in the view {{=display_name}}. In this
case I believe the functions are returning html because the resulting
web page output is formated such as:

    NAME
--------------
 The Big

Then I tried:
===
cname=db(db.customer_names.id==names_index).select('name').first()
crashes with

ERROR SNAPSHOT
<type 'exceptions.KeyError'>('xml')

===

cname=db(db.customer_names.id==names_index).select().first()

Type 'exceptions.KeyError'>('xml')

===

cname=db(db.customer_names.id==name_index).select('name').first()

<type 'exceptions.KeyError'>('xml')

===

I then tried a lot of variations on

cname=(db.(db.customer_names.id==name_index).select()).as_dict()
['name_index']
if the above worked, it would not be the answer - it would only be the
second dict()

also using  .views()  and .items()   views()  transforms the
dictionary to a nice format:
 [{'id': 12, 'name': 'WATER COM'}]  which in the console a solution
quickly followed.
However, Web2py it was not possible to address this either as a list,
dictionary or string.

 I believe these problems are related to differences among Python
2.5.4, Python 2.7.1, Active Python 2.6.x, and Web2Py in how they
handle Python dictionary methods such as values() and itmes(), as well
as the operators [] and [][].

Both Python 2.5.4 and 2.7.1's implementation of .values() does not
match the documentation. a.values() is supposed to return only just
the values in the dictionary as a list. The Python 25 and Python27
implementation appear to just embed the dictionary between to square
brackets "[']" and change the "type" to list. Active state implements
exactly what is described, it returns just a list of the values. I
could get code working (in a manner similar to what you suggest) with
the three versions of python in console to retrieve specific
dictionary items, but in Web2py the system seems to not be recognizing
these returned items as lists or as dictionaries.

db.define_table('customer_names',


                    Field('name'))



db.define_table('customer_accounts',


                    Field('possible'))





db.define_table('customer',


                    Field('symbol', requires=IS_NOT_EMPTY()),


                    Field('name', db.customer_names),


                    Field('account', db.customer_accounts))


db.customer.name.requires = IS_IN_DB(db,db.customer_names.id, '%
(name)s')


db.customer.account.requires = IS_IN_DB(db,db.customer_accounts.id, '%
(possible)s')


On Apr 1, 9:09 pm, Anthony <abasta...@gmail.com> wrote:
> Are you saying that 
> db(db.customer_names.id==names.index).select('name').first().name does
> not return just the name? Can you show your model code?
>
> Also, if you use as_list, you get a list of dictionaries (one dictionary per
> Row in the Rows object). If there is only one Row, then there is only one
> dictionary in the list, and therefore the maximum index of the list will be
> 0. If you try current_customer_name[2], it's looking for the third
> dictionary in the list (i.e., the third Row), which of course does not
> exist. The way you have defined current_customer_name, I think the actual
> name should be current_customer_name[0]['name'] (the [0] gets you the first
> dictionary in the list, and the ['name'] gets you the value associated with
> the 'name' key in that dictionary). But again, you shouldn't have to do it
> that way -- the query above should return the name directly without using
> as_list.
>
> Anthony
>
>
>
>
>
>
>
> On Friday, April 1, 2011 9:20:26 PM UTC-4, james c. wrote:
> > Thanks Denes,
>
> > Thank you Denes for your recommendations.
>
> > I've seen documentation leading me to believe that most of this here
> > should work. But it doesn't. 'Which reminds me that I have Python 2.6x
> > installed and when I downloaded my current version, Version 1.91.6
> > (2011-01-03 17:55:14), there was a red 5 in the requirements Python
> > 2.5x. It appears that many folks are now using 2.6x. I am going to
> > upgrade and hope that addresses my issues. If anyone sees anything
> > here or has more ideas I would appreciate the help or pointers to
> > documentation. Thanks, James C.
>
> > This is a very simple little test app for which I am implementing this
> > very limited function, described here, working. I think the
> > recommendation below should work. I also think that the use
> > of .as_list() approach should work (however, there exists one
> > additional character between the 'xoxoxo',  characters which define
> > the list - this may be confusing this approach.)
>
> > I Tried both recommendations without any good results. The first
> > recommendation is close to what I found in the documentation - neither
> > would run. In either case the changes would not get beyond the editor.
> > I could not find any documentation close to the second recommendation.
> > The syntax might be wrong but could not find any further information
> > to support that conclusion. After much lack of progress I tried a more
> > direct approach using .as_list() The query I'm using and described
> > earlier returns only one field, and that is customer name along with a
> > database descriptive header.  I tried the following in the controller:
>
> >  db(db.customer_names.id==names.index).select('name')
> > current_c_name = db(db.customer_names.id==name_index).select('name')
> > current_customer_name = current_c_name.as_list()
> > display_name = current_customer_name[2]
> > return dict(display_name,...
>
> > in view:
>
> > {{=display_name}} is also defined earlier with a default value. If
> > just the entire 3 element "list" is displayed through
> > {{=display_name}} it looks like:
>
> >  [{'_extra': {'name': u'The Big Company'}}]
>
> > This goes wrong when trying to index the list
> > current_customer_name[2]. What ever index is specified an index out of
> > range error is returned.
>
> > On Apr 1, 1:11 pm, DenesL <dene...@yahoo.ca> wrote:
> > > On Apr 1, 3:24 pm, "james c." <james....@gmail.com> wrote:
>
> > > > This is probably simple but I can't find in the documentation or
> > > > recognize anything posted on this. I have a primary database including
> > > > fields which entries must be members of other databases - this is
> > > > enforced through the primary database definition and select lists
> > > > entry for the enforced membership fields: as recommended by the
> > > > current manual. I must display the information entered on existing and
> > > > later pages. Within the primary database the items entered through
> > > > selected drop down lists appear as index which mean nothing to an end
> > > > user. I can retrieve the  indexed items by using something like in the
> > > > controller/function:
>
> > > > current_customer_name =
> > > > db(db.customer_names.id==names.index).select('name')
>
> > > The select returns a Rows object, not just one row and not a field of
> > > that row
> > > so to get to the name field value you can do
>
> > > db(db.customer_names.id==names.index).select('name').first().name
>
> > > or
> > > db(db.customer_names.id==names.index).select('name')[0].name
>
> > > and then your variable will contain only the customer's name.
>
> > > > and is displayed in the view html with:
>
> > > > {{=current_customer_name))
>
> > > > I need the results to be displayed in this form (which it does not):
> > > > The Big Company
>
> > > > It displays this (which won't work for
> > > > me):                                                 NAME
>
> > > > ------------------------------
>
> > > > The Big Company
>
> > > > I would appreciate any advice or pointers on where to look further.
> > > > thanks in advance.
>
> > > > James C.

Reply via email to