The BSD or Berkely Packet Filter is a register-based filter evaluator and network tap invented 1990 by Steven McCanne and Van Jacobson to replace the CMU/Stanford Packet Filter (CSPF) and Sun NIT filter technology with a faster alternative1).
While bpf consists of two components, the filter evaluator and the network tap, we'll ignore the network tap and focus on the filter evaluator instead.
Having a xml field in your postgres database table, you may want to match something with xpath.
Unfortunately the result of the match is an ARRAY, and the django.db.backends.postgresql_psycopg2 engine does not convert it properly to a list of ElementTree's.
But, this can be changed:
import xml.etree.ElementTree as ET
from psycopg2.extensions import new_type, register_type
from psycopg2.extensions import STRINGARRAY
def cast_xml_array(value, cur):
if value is None:
elements = STRINGARRAY(value,cur)
return [ET.fromstring(i) for i in elements]
XML = new_type((143,), "XML", cast_xml_array)
This snippet registers a new type for the OID 143, which is xml for me.
The result is converted to a list of strings using psycopg2's internal STRINGARRAY type, and this list of strings is converted to a list of ElementTrees.
Fans of Python use the phrase “batteries included” to describe the standard library, which covers everything from asynchronous processing to zip files.