Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange python type "Text"?

Tags:

python

suds

I use library "suds" to communicate with SOAP server. After request was successfully done, I receive answer:

answer = client.invoke('RetrieveBLABLAObject', modelthings)

This answer is a structure of many different fields of requested object. Each field is structure that consist of "string" parameter "_type" and parameter "value", that may have different types.

answer[key][value] - returns me value of parameter "value". But in debug mode (I use Python 2.7.6 and PyCharm) it shows me that value has type "Text". Not "str", not "unicode", but "Text".

If I check it by

isinstance(obj[cur_key]['value'], unicode)

it shows me that it counts this text as "unicode". But if I compare it with unicode with same body, it returns false:

if obj[cur_key]['value'] != u'String that I know is there':
    print("true") #it is printing, but it shouldn't

Why this happens? How can I convert "Text" to unicode? I tried

obj[cur_key]['value'].decode('utf-8')

it falls with exception. How can I handle this "Text" type?

like image 510
Arkady Avatar asked Feb 05 '26 11:02

Arkady


1 Answers

You are looking at a subclass of unicode most likely. I think you have an instance of suds.sax.text.Text() here. Since this is a subclass, isinstance(obj, unicode) is True.

It's value is already a unicode string, so decoding will trigger an implicit encode first with ASCII, which will indeed fail.

You can convert the type to unicode by simply using:

unicode(obj[cur_key]['value'])

but take into account the value can still be XML escaped; the .unescape() method returns an unescaped version (it'll return self if the value wasn't escaped to begin with).

When comparing with another string, make sure they are exactly equal. Unicode values can contain many 'hidden' values, like zero-width characters or combining characters that can be represented in combined form as well. Use repr(value) to get an escaped representation that makes such codepoints more obvious.

The suds.sax.text.Text() class doesn't special-case equality testing, so your != test would work just the same as if the value was a unicode string instead of the subclass.

like image 186
Martijn Pieters Avatar answered Feb 07 '26 23:02

Martijn Pieters