
    'j                    d    d dl mZ d dlmZmZmZmZ d dlmZm	Z	 erd dl
mZ d dlmZ 	 	 dddZdS )    )annotations)IterableHashableTYPE_CHECKINGOptional)DXFValueErrorDXFAttributeError)	DXFEntity)KeyFunc NentitiesIterable[DXFEntity]	dxfattribstrkeyOptional[KeyFunc]returndict[Hashable, list[DXFEntity]]c                J   t          |f          rt          d          dk    rfd}|t          d          t                      }| D ]T}|j        s
	  ||          }n# t          $ r Y #w xY w|+|                    |g           }|                    |           U|S )ay  
    Groups a sequence of DXF entities by a DXF attribute like ``'layer'``,
    returns a dict with `dxfattrib` values as key and a list of entities
    matching this `dxfattrib`.
    A `key` function can be used to combine some DXF attributes (e.g. layer and
    color) and should return a hashable data type like a tuple of strings,
    integers or floats, `key` function example::

        def group_key(entity: DXFEntity):
            return entity.dxf.layer, entity.dxf.color

    For not suitable DXF entities return ``None`` to exclude this entity, in
    this case it's not required, because :func:`groupby` catches
    :class:`DXFAttributeError` exceptions to exclude entities, which do not
    provide layer and/or color attributes, automatically.

    Result dict for `dxfattrib` = ``'layer'`` may look like this::

        {
            '0': [ ... list of entities ],
            'ExampleLayer1': [ ... ],
            'ExampleLayer2': [ ... ],
            ...
        }

    Result dict for `key` = `group_key`, which returns a ``(layer, color)``
    tuple, may look like this::

        {
            ('0', 1): [ ... list of entities ],
            ('0', 3): [ ... ],
            ('0', 7): [ ... ],
            ('ExampleLayer1', 1): [ ... ],
            ('ExampleLayer1', 2): [ ... ],
            ('ExampleLayer1', 5): [ ... ],
            ('ExampleLayer2', 7): [ ... ],
            ...
        }

    All entity containers (modelspace, paperspace layouts and blocks) and the
    :class:`~ezdxf.query.EntityQuery` object have a dedicated :meth:`groupby`
    method.

    Args:
        entities: sequence of DXF entities to group by a DXF attribute or a
            `key` function
        dxfattrib: grouping DXF attribute like ``'layer'``
        key: key function, which accepts a :class:`DXFEntity` as argument and
            returns a hashable grouping key or ``None`` to ignore this entity

    z4Specify a dxfattrib or a key function, but not both.r   c                8    | j                                       S )N)dxfget_default)entityr   s    G/DATA/AppData/hermes/venv/lib/python3.11/site-packages/ezdxf/groupby.py<lambda>zgroupby.<locals>.<lambda>K   s    VZ33I>>     NzMno valid argument found, specify a dxfattrib or a key function, but not both.)allr   dictis_aliver	   
setdefaultappend)r   r   r   result
dxf_entity	group_keygroups    `     r   groupbyr&      s    p Is 
B
 
 	
 B>>>>
{
 
 	

 /3ffF 
% 
%
" 		JII  	 	 	H	  %%i44ELL$$$Ms   A%%
A21A2)r   N)r   r   r   r   r   r   r   r   )
__future__r   typingr   r   r   r   ezdxf.lldxf.constr   r	   ezdxf.entitiesr
   ezdxf.eztypesr   r&    r   r   <module>r-      s    # " " " " " > > > > > > > > > > > > > > > > > > > > &((((((%%%%%%
 !P P P P P P Pr   