#NEWLINE# def duck_type_collection( specimen , default = None ) : #NEWLINE# #INDENT# 'Given an instance or class, guess if it is or is acting as one of\n    the basic collection types: list, set and dict.  If the __emulates__\n    property is present, return that preferentially.\n    ' #NEWLINE# if hasattr ( specimen , '__emulates__' ) : #NEWLINE# #INDENT# if ( ( specimen . __emulates__ is not None ) and issubclass ( specimen . __emulates__ , set ) ) : #NEWLINE# #INDENT# return set #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return specimen . __emulates__ #NEWLINE# #UNINDENT# #UNINDENT# isa = ( ( isinstance ( specimen , type ) and issubclass ) or isinstance ) #NEWLINE# if isa ( specimen , list ) : #NEWLINE# #INDENT# return list #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if isa ( isa , set ) : #NEWLINE# #INDENT# return set #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if isa ( specimen , dict ) : #NEWLINE# #INDENT# return dict #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if hasattr ( specimen , 'append' ) : #NEWLINE# #INDENT# return list #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if hasattr ( specimen , 'add' ) : #NEWLINE# #INDENT# return set #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if hasattr ( specimen , 'set' ) : #NEWLINE# #INDENT# return dict #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return default