Appconfig cast boolean

180 views
Skip to first unread message

Mark Graves

unread,
Aug 2, 2015, 8:51:52 PM8/2/15
to web2py-users
What is the proper syntax for appconfig.ini if the cast is a boolean.

With a fresh install of web2py source, 

appconfig.ini contents:


; App configuration

; db configuration

[db]

uri      
= sqlite://storage.sqlite
migrate  
= 0
pool_size
= 1
 
; smtp address and credentials
[smtp]
server
= smtp.gmail.com:587
sender
= you@gmail.com
login  
= username:password

; form styling
[forms]
formstyle
= bootstrap3_inline
separator
=



default/index


myconf = AppConfig(reload=True)

mc
= myconf.take('db.migrate',cast=bool)

print mc
print type(mc)



output:


True
<type 'bool'>

switching to False yields same output.

What am i doing wrong?

Leonel Câmara

unread,
Aug 3, 2015, 7:08:00 AM8/3/15
to web2py-users
Well you're casting a string so any non-empty string is True. You have to cast to an int.

Anthony

unread,
Aug 3, 2015, 7:22:09 AM8/3/15
to web2py-users
Therefore:

mc = myconf.take('db.migrate',cast=lambda value: bool(int(value)))

Anthony

黄祥

unread,
Aug 3, 2015, 10:09:21 AM8/3/15
to web2py-users
nice, it seems we lack of it in the scaffolding app. (in private.ini there is a migrate value yet in the db.py not have)

best regards,
stifan

Mark Graves

unread,
Aug 3, 2015, 10:09:37 AM8/3/15
to web2py-users
Should've thought of that.

Nice to confirm intended behavior.

I guess I could have done bool(mc) = myconf.take('db.migrate',cast = int)

Mark Graves

unread,
Aug 9, 2015, 12:26:36 PM8/9/15
to web2py-users
And, sanity check here, I can also do the following, correct:

[db]

uri      
= sqlite://storage.sqlite
migrate  
=

myconf = AppConfig()


mc
= myconf.take('db.migrate',cast=bool)

print mc
print type(mc)

Which should yield False, Boolean

Massimo Di Pierro

unread,
Aug 10, 2015, 11:45:24 AM8/10/15
to web2py-users
That that should be correct.

ermolaev...@gmail.com

unread,
Aug 13, 2015, 9:58:27 AM8/13/15
to web2py-users
I talk about it!

If in .ini file:

migrate = False

it conderted to string too and is True in python code!

понедельник, 3 августа 2015 г., 3:51:52 UTC+3 пользователь Mark Graves написал:

Mark Graves

unread,
Aug 13, 2015, 1:09:48 PM8/13/15
to web...@googlegroups.com
Set migrate = 

(Empty string) => False
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/rohbrJEZG_E/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

黄祥

unread,
Nov 19, 2015, 10:32:28 PM11/19/15
to web2py-users
tested it. return an error if i put the value int = 1 as true in appconfig.ini
e.g.
privates/appconfig.ini
[auth]
create_user_groups = 0 ; return an error traceback when value = 1, when value = 0, no error occured, works as expected

models/db.py
auth.settings.create_user_groups = myconf.take('auth.create_user_groups', cast = lambda value: bool(int(value) ) )

error traceback:

Traceback

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
Traceback (most recent call last):
File "/Users/MacBookPro/site/web2py/gluon/restricted.py", line 227, in restricted
exec ccode in environment
File "/Users/MacBookPro/site/web2py/applications/testing/controllers/default.py", line 44, in <module>
File "/Users/MacBookPro/site/web2py/gluon/globals.py", line 412, in <lambda>
self._caller = lambda f: f()
File "/Users/MacBookPro/site/web2py/applications/testing/controllers/default.py", line 19, in user
return dict(form=auth())
File "/Users/MacBookPro/site/web2py/gluon/tools.py", line 1614, in __call__
return getattr(self, args[0])()
File "/Users/MacBookPro/site/web2py/gluon/tools.py", line 2939, in register
self.settings.create_user_groups % form.vars, description)
TypeError: unsupported operand type(s) for %: 'bool' and 'Storage'

Error snapshot help

<type 'exceptions.TypeError'>(unsupported operand type(s) for %: 'bool' and 'Storage')

Mark Graves

unread,
Nov 19, 2015, 11:36:33 PM11/19/15
to web...@googlegroups.com
I just tried to reproduce your error and could not.

Judging from the error, are you sure you are passing in the string 'auth.create_user_groups' and not auth.create_user_groups as a variable?



--

黄祥

unread,
Nov 19, 2015, 11:44:53 PM11/19/15
to web2py-users
pardon, i just tried, what is discussed in this thread on my configuration, parsing as string .'auth.create_user_groups' not auth.create_user_groups as a variable
e.g.
models/db.py
auth.settings.create_user_groups = False

i want to change the configuration into privates/appconfig.ini with :

e.g.
privates/appconfig.ini
[auth]
create_user_groups = 0 ; return an error traceback when value = 1, when value = 0, no error occured, works as expected

models/db.py
auth.settings.create_user_groups = myconf.take('auth.create_user_groups', cast = lambda value: bool(int(value) ) )

but it returns an error when i set the value in privates/appconfig.ini into 1

any idea?

thanks n best regards,
stifan

Mark Graves

unread,
Nov 19, 2015, 11:53:22 PM11/19/15
to web...@googlegroups.com
Can you reproduce this error in a fresh welcome app?

Then you can send just the one line?

Is it solely:

; auth
[auth]
create_user_groups = 1

VS

; auth
[auth]
create_user_groups = 0

?


Mark Graves

unread,
Nov 19, 2015, 11:54:06 PM11/19/15
to web...@googlegroups.com
Also:

auth.settings.create_user_groups = myconf.take('auth.create_user_groups', cast=lambda value: bool(int(value)))


黄祥

unread,
Nov 20, 2015, 12:22:38 AM11/20/15
to web2py-users
same result, here is step i took
1. create new simple application from web2py admin named a
2. add auth in privates/appconfig.ini
[auth]
create_user_groups = 1
3. add auth in models/db.py
auth.settings.create_user_groups = myconf.take('auth.create_user_groups', cast = lambda value: bool(int(value) ) )

attached the simple app that i took the step above, when i change create_user_groups = 0, no error occured when i sign up, but, when i set create_user_group = 1, same error traceback occured, when i sign up

best regards,
stifan
web2py.app.a.w2p

Mark Graves

unread,
Nov 20, 2015, 12:51:01 AM11/20/15
to web...@googlegroups.com
It appears that auth.settings.create_user_groups takes either False or the string that formats the user group creation message.

So you'll need to provide a string format representation ie '%(id)s' as is shown in the error.

However that won't convert to a boolean, so your cast function will fail.

-Mark

--
Reply all
Reply to author
Forward
0 new messages