Liondancer
Liondancer

Reputation: 16469

type unicode and type str

Program written in python 2.6.7!

if type(value) == str and 'count(*)' in value:
   testcase['isCountQuery'] = 'true'
   break

My test case wont pass because value is type unicode

(Pdb) type(value) == str
False
(Pdb) value
u'select count(*) from uc_etl.agency_1'
(Pdb) type(value)
<type 'unicode'>
(Pdb) value
u'select count(*) from uc_etl.agency_1'

I tried changing my if statement to:

if type(value) == unicode and 'count(*)' in value:
    testcase['isCountQuery'] = 'true'
    break

type == unicode does not exists.

I could wrap str(value) but I was wondering if there was another fix for this

How can I fix this?

Upvotes: 0

Views: 548

Answers (2)

CristiFati
CristiFati

Reputation: 41137

[Python 2.Docs]: Built-in Functions - isinstance(object, classinfo) is the preferred way:

from types import StringType, UnicodeType
if value and isinstance(value, (StringType, UnicodeType)) and "count(*)" in value:
    #the rest of the code.

Not sure why unicode is not defined.

Upvotes: 3

running.t
running.t

Reputation: 5709

At least in python 2 you could check if value is basestring (base class for str and unicode).

if isinstance(value, basestring)  and 'count(*)' in value:
   testcase['isCountQuery'] = 'true'
   break

Upvotes: 3

Related Questions