testing for data type
Hi Listers, I have a requirement to test for a data type could someone tell me if this is possible in python? Basically I have a ZPT in Zope that users can select checkboxes in a form which pass arguments for a python function, however if there is only one checkbox selected it is passed as a string whereas more than one checkbox is passed as a list. Therefore if my function is required to perform an action based on each argument passed in the list the function works correctly but if it is passed as a string nothing happens. This is my function: selecteddeptcodes = context.REQUEST.DEPTCODE currentstatus = context.REQUEST.STATUS if currentstatus == 'pending': for dptcd in selecteddeptcodes: context.changetolive(DEPTCODE=dptcd) if currentstatus == 'old': for dptcd in selecteddeptcodes: context.changetopending(DEPTCODE=dptcd) return context.pub_dept_form(context, context.REQUEST, message='Updated Status') The argument in question is selecteddeptcodes. I tried to make my function conditional based on the length of the argument passed but if it’s just one checkbox value passed the length of the argument is 2 (which is the number of chars passed in the string) and if there are two checkboxes the length of the argument (which is the number of items in the list) is also 2. So that doesn’t help. Any assistance would be appreciated. Jon -- http://mail.python.org/mailman/listinfo/python-list
Newbie help - test for data type
Hi Listers, I have a requirement to test for a data type could someone tell me if this is possible in python? Basically I have a ZPT in Zope that users can select checkboxes in a form which pass arguments for a python function, however if there is only one checkbox selected it is passed as a string whereas more than one checkbox is passed as a list. Therefore if my function is required to perform an action based on each argument passed in the list the function works correctly but if it is passed as a string nothing happens. This is my function: selecteddeptcodes = context.REQUEST.DEPTCODE currentstatus = context.REQUEST.STATUS if currentstatus == 'pending': for dptcd in selecteddeptcodes: context.changetolive(DEPTCODE=dptcd) if currentstatus == 'old': for dptcd in selecteddeptcodes: context.changetopending(DEPTCODE=dptcd) return context.pub_dept_form(context, context.REQUEST, message='Updated Status') The argument in question is selecteddeptcodes. I tried to make my function conditional based on the length of the argument passed but if its just one checkbox value passed the length of the argument is 2 (which is the number of chars passed in the string) and if there are two checkboxes the length of the argument (which is the number of items in the list) is also 2. So that doesnt help. Any assistance would be appreciated. Jon -- http://mail.python.org/mailman/listinfo/python-list
Re: Newbie help - test for data type
Thanks for everyone’s help, much appreciated, I’ll check out the isinstance function. Jon -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Dennis Benzinger Sent: 31 July 2006 10:20 To: python-list@python.org Subject: Re: Newbie help - test for data type Jonathan Bowlas wrote: > Hi Listers, > > I have a requirement to test for a data type could someone tell me if this > is possible in python? > > Basically I have a ZPT in Zope that users can select checkboxes in a form > which pass arguments for a python function, however if there is only one > checkbox selected it is passed as a string whereas more than one checkbox is > passed as a list. Therefore if my function is required to perform an action > based on each argument passed in the list the function works correctly but > if it is passed as a string nothing happens. You need the isinstance() function. For example you can use isinstance(selecteddeptcodes, list) to test if your variable is a list. If you want to test the other way round use isinstance(selecteddeptcodes, str) if your variable is a string, isinstance(selecteddeptcodes, unicode) if it's a unicode string or isinstance(selecteddeptcodes, basestring) to test if it's a string or unicode string. Read more about this function and the types to test for in http://docs.python.org/lib/built-in-funcs.html. If you already have the code for a list argument I'd check if it's not a list and then turn it into a list: if not isintance(selecteddeptcodes, list): selecteddeptcodes = [selecteddeptcodes] Bye, Dennis -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re:Newbie help - test for data type
Thanks Bruno, I’ll use this. Much appreciated. Jon -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Bruno Desthuilliers Sent: 31 July 2006 10:37 To: python-list@python.org Subject: Re: [OT]Newbie help - test for data type Jonathan Bowlas wrote: > Hi Listers, > > I have a requirement to test for a data type could someone tell me if this > is possible in python? > > Basically I have a ZPT in Zope that users can select checkboxes in a form > which pass arguments for a python function, however if there is only one > checkbox selected it is passed as a string whereas more than one checkbox is > passed as a list. You should have posted this on Zope's mailing-list. There's a mechanism in Zope to handle this case, cf section "Passing Parameters to Scripts" in: http://www.zope.org/Documentation/Books/ZopeBook/2_6Edition/DTML.stx/ScriptingZope.stx The mechanism is based on 'decorating' forms fields names with type annotation. In you case, this would look like: tal:attribute="value code" /> HTH -- bruno desthuilliers python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for p in '[EMAIL PROTECTED]'.split('@')])" -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Convert StringIO to string
Hi listers, I've written this little script to generate some html but I cannot get it to convert to a string so I can perform a replace() on the >, < characters that get returned. from StringIO import StringIO def generator_file(rsspath,titleintro,tickeropt): scripter=StringIO() scripter.write('\n') scripter.write('new rss_ticker(%s, %s, %s)\n' % (rsspath, titleintro, tickeropt)) scripter.write('\n') return scripter.getvalue() I tried adding this: scripter = scripter.replace("<", "<") scripter = scripter.replace(">", ">") But obviously replace() isn't an attribute of StringIO so I guess I need to convert it to a string first, can someone please advise how I can do this? Cheers Jon -- http://mail.python.org/mailman/listinfo/python-list
RE: Convert StringIO to string
Ahh thanks, I'll give that a try. -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of [EMAIL PROTECTED] Sent: 16 October 2006 14:00 To: python-list@python.org Subject: Re: Convert StringIO to string Jonathan Bowlas wrote: > But obviously replace() isn't an attribute of StringIO so I guess I need to > convert it to a string first, can someone please advise how I can do this? StringIO objects are file-like objects, so you need to use read or readlines to get the string data out of it (just like a regular file). Before reading remember to seek back to the beginning to get all of the data ("Be kind, rewind!"): >>> import StringIO >>> s = StringIO.StringIO() >>> s.write("hello world\n") >>> s.seek(0) >>> s.read() 'hello world\n' >>> s = StringIO.StringIO() >>> s.write("hello world\n") >>> s.read() '' -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
RE: Convert StringIO to string
Ok, I think I'm explaining this badly because I've used getvalue() in the script I originally submitted to the list, see below: from StringIO import StringIO def generator_file(rsspath,titleintro,tickeropt): scripter=StringIO() scripter.write('\n')scripter.write('new rss_ticker(%s, %s, %s)\n' % (rsspath, titleintro, tickeropt))
scripter.write('\n') return scripter.getvalue() The problem is this script returns the following: And I want to replace the < and > characters for < or > so it actually renders correctly in a browser so I'm returned this:
new rss_ticker(www.bbc.co.uk, True, True) So what am I doing wrong? J -- http://mail.python.org/mailman/listinfo/python-list
RE: Convert StringIO to string
Your suggestion didn't seem to make any difference at all, it still returns <script type="text/javascript"> new rss_ticker(gdfgdfg, True, True) </script> Any other ideas? Jon -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Rob Williscroft Sent: 16 October 2006 14:37 To: python-list@python.org Subject: Re: Convert StringIO to string Jonathan Bowlas wrote in news:[EMAIL PROTECTED] in comp.lang.python: > Hi listers, > > I've written this little script to generate some html but I cannot get > it to convert to a string so I can perform a replace() on the >, > < characters that get returned. > > from StringIO import StringIO > > def generator_file(rsspath,titleintro,tickeropt): > scripter=StringIO() > scripter.write('\n') > scripter.write('new rss_ticker(%s, %s, %s)\n' % (rsspath, > titleintro, tickeropt)) > scripter.write('\n') > return scripter.getvalue() > > I tried adding this: > > scripter = scripter.replace("<", "<") scripter = > scripter.replace(">", ">") > > But obviously replace() isn't an attribute of StringIO so I guess I > need to convert it to a string first, can someone please advise how I > can do this? > How strange, you are already "converting" to a string in the return line (the call to the getvalue() method), so: scripter = scripter.getvalue().replace("<", "<") scripter = scripter.replace(">", ">") return scripter should do what you want. Rob. -- http://www.victim-prime.dsl.pipex.com/ -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list