2016-06-12 106 views
0

我添加描述here我GDB通用GDB漂亮的打印機,下面一行添加類型的地圖使用的打印機:Python字典:unorderable類型的錯誤

pretty_printers_dict[re.compile ('.*Generic.*')] = GenericPrinter 

我需要去適應它我的類型:

pretty_printers_dict[re.compile ('MyNamespace1.*')] = GenericPrinter 
pretty_printers_dict[re.compile ('MyNamespace2.*')] = GenericPrinter 

基本上我喜歡用我的所有類型的打印機。

但我得到以下錯誤GDB內部:

Python Exception <class 'TypeError'> unorderable types: _sre.SRE_Pattern() < _sre.SRE_Pattern(): 
Python Exception <class 'TypeError'> unorderable types: _sre.SRE_Pattern() < _sre.SRE_Pattern(): 

如果我刪除任一條線,它工作正常。我很疑惑,

你看到它是什麼問題,以及如何解決它?

[UPDATE] 我通過將正則表達式擺脫它:

pretty_printers_dict[re.compile ('MyNamespace1.*|MyNamespace2.*')] = GenericPrinter 
+0

你能否提供更多代碼,因爲這沒有意義。這應該工作,並在我的機器上工作! –

+0

@my_question,如果你有一個工作解決方案,你應該添加它作爲答案並接受它。 –

回答

0

我遠離它通過將正則表達式:

pretty_printers_dict[re.compile ('MyNamespace1.*|MyNamespace2.*')] = GenericPrinter 
0

此問題僅出現在Python 3:

sorted功能需要比較兩個編譯正則表達式這隻能在Python 2中工作(我不知道爲什麼)。

但我不認爲sorted調用是必要的,這樣就這麼走了出來:

for function in pretty_printers_dict: 
    if function.match(typename): 
     return pretty_printers_dict[function](val)