I'm mystified by the behavior of dDataSet.filter() with PostgreSQL
databases:
The main window contains a dTextBox, a dButton and a dGrid. At the start
of the application the dGrid shows all records of a table (no great
number, round about fifty or so). If the user enters text into the
textbox and clicks the button, the table should be filtered. Expected
result in the grid: only the records containing the entered text in a
certain field.
Instead I get this, every time:
Traceback (most recent call last):
File "/home/sib/src/dabo/dabo/ui/uiwx/dControlMixin.py", line 27, in
_onWxHit
self.raiseEvent(dEvents.Hit, evt, *args, **kwargs)
File "/home/sib/src/dabo/dabo/ui/uiwx/dPemMixin.py", line 1084, in
raiseEvent
super(dPemMixin, self).raiseEvent(eventClass, nativeEvent, *args,
**kwargs)
File "/home/sib/src/dabo/dabo/lib/eventMixin.py", line 81, in raiseEvent
bindingFunction(event)
File "./umlaut_test_fb.py", line 58, in onFindContent
self.findContent()
File "./umlaut_test_fb.py", line 68, in findContent
biz.filter("st_name", stw, "contains")
File "/home/sib/src/dabo/dabo/biz/dBizobj.py", line 1389, in filter
self._CurrentCursor.filter(fld=fld, expr=expr, op=op)
File "/home/sib/src/dabo/dabo/db/dCursorMixin.py", line 1481, in filter
self._records = self._records.filter(fld=fld, expr=expr, op=op)
File "/home/sib/src/dabo/dabo/db/dDataSet.py", line 207, in filter
if expr in fldval:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 9:
ordinal not in range(128)
The encoding of the database is utf-8, many records contain non-ascii
characters in the searched fields.
I thought that the filter method of dDataSet might need "ustr" at the
right place or places. To find that place I replaced line 199 with this
code:
elif op == "contains":
### Start of test code
filtered = []
print (fld, type(fld))
print (expr, type(expr))
for rec in self:
fldval = rec[fld]
fldval = fldval or ""
print (fldval, type(fldval))
if expr in fldval:
filtered.append(rec)
print "Found"
### End of test code; original line:
###filtered = [rec for rec in self if expr in (rec[fld] or "")]
But I got unexpected results:
- Output from the Application:
Hier findContent
Gesucht: klein
- Output from dDataSet.filter:
('stname', <type 'str'>) - field name
(u'klein', <type 'unicode'>) - search expression
- And now the field content for the first records:
(u'Abgelaufene Medikamente ersetzen', <type 'unicode'>)
... several lines like this, with or without non-ascii characters
(u'Auto klein checken', <type 'unicode'>)
Found
(u'Auto klein reinigen', <type 'unicode'>)
Found
(u'Auto T\xdcV, ASU', <type 'unicode'>)
...
(u'E-Mail abholen D', <type 'unicode'>)
- And now, suddenly (not always at the same record!):
('Eingang Lohnsteuerkarte', <type 'str'>)
('Einkauf mit Vorbereitung', <type 'str'>)
('Einkommensteuererkl\xc3\xa4rung', <type 'str'>)
And here comes the same traceback as above.
More tests show that the change from type unicode to type str happens a
few records after the last record shown in the grid. But if I leave the
grid out and just let all the field values be printed in the console,
then they all have type unicode. So I don't understand what's happening
here!
Platform: GTK
Python Version: 2.7.6 on linux2
Dabo Version: 0.9.14
UI Version: 2.8.12.1 on wxGTK (gtk2)
Same thing on windows.
It seems that this should help:
filtered = [rec for rec in self if expr in ustr(rec[fld] or "")]
But I didn't yet really try it out. And I'd like to know most of all why
the field type in the bizobj dataset depends on the state of the UI.
Greetings,
Sibylle
--
Dr. Sibylle Koczian
Fasanenstrasse 12
D-82293 Mittelstetten
E-Postbrief: [email protected]
-----------------------------------------
_______________________________________________
Post Messages to: [email protected]
Subscription Maintenance: http://mail.leafe.com/mailman/listinfo/dabo-users
Searchable Archives: http://leafe.com/archives/search/dabo-users
This message: http://leafe.com/archives/byMID/[email protected]