Пример #1
0
def get_event_code_from_name(name, evt_handler_type):
    '''
    Get an event code given a `name` and an `evt_handler_type`.
    
    For example, given a `name` of `left_down` this function will return the
    event code `wx.EVT_LEFT_DOWN`.
    
    If `evt_handler_type` has an `.event_modules` attribute, these modules will
    be searched for event codes in precedence to `wx` and the event handler
    type's own module.
    '''
    processed_name = 'EVT_%s' % string_tools.case_conversions. \
                                camel_case_to_lower_case(name).upper()
    raw_event_modules = \
        (evt_handler_type.event_modules if
         sequence_tools.is_sequence(evt_handler_type.event_modules) else
         [evt_handler_type.event_modules])
    event_modules = raw_event_modules + [
        address_tools.resolve(evt_handler_type.__module__), wx
    ]
    for event_module in event_modules:
        try:
            return getattr(event_module, processed_name)
        except AttributeError:
            pass
    else:
        raise LookupError("Couldn't find event by the name of '%s'." %
                          processed_name)
Пример #2
0
def get_event_code_from_name(name, evt_handler_type):
    '''
    Get an event code given a `name` and an `evt_handler_type`.
    
    For example, given a `name` of `left_down` this function will return the
    event code `wx.EVT_LEFT_DOWN`.
    
    If `evt_handler_type` has an `.event_modules` attribute, these modules will
    be searched for event codes in precedence to `wx` and the event handler
    type's own module.
    '''
    processed_name = 'EVT_%s' % string_tools.case_conversions. \
                                camel_case_to_lower_case(name).upper()
    raw_event_modules = \
        (evt_handler_type.event_modules if
         sequence_tools.is_sequence(evt_handler_type.event_modules) else 
         [evt_handler_type.event_modules])
    event_modules = raw_event_modules + [
        address_tools.resolve(evt_handler_type.__module__),
        wx
    ]
    for event_module in event_modules:
        try:
            return getattr(event_module, processed_name)
        except AttributeError:
            pass
    else:
        raise LookupError("Couldn't find event by the name of '%s'." %
                          processed_name)
def _key_dict_to_accelerators(key_dict):
    '''
    Convert a dict mapping keys to ids to a list of accelerators.
    
    The values of `key_dict` are wxPython IDs. The keys may be either:
    
      - `Key` instances.
      - Key-codes given as `int`s.
      - Tuples of `Key` instances and/or key-codes given as `int`s.

    Example:
    
        _key_dict_to_accelerators(
            {Key(ord('Q')): quit_id,
             (Key(ord('R'), cmd=True),
              Key(wx.WXK_F5)): refresh_id,
             wx.WXK_F1: help_id}
        ) == [
            (wx.ACCEL_NORMAL, ord('Q'), quit_id),
            (wx.ACCEL_CMD, ord('R'), refresh_id),
            (wx.ACCEL_NORMAL, ord('Q'), refresh_id),
            (wx.ACCEL_NORMAL, wx.WXK_F1, help_id),
        ]
    
    '''
    
    accelerators = []
    
    original_key_dict = key_dict
    key_dict = {}
    
    ### Breaking down key tuples to individual entries: #######################
    #                                                                         #
    for key, id in original_key_dict.items():
        if sequence_tools.is_sequence(key):
            key_sequence = key
            for actual_key in key_sequence:
                key_dict[actual_key] = id
        else:
            key_dict[key] = id
    #                                                                         #
    ### Finished breaking down key tuples to individual entries. ##############
    
    for key, id in key_dict.items():
        if isinstance(key, int):
            key = wx_tools.keyboard.Key(key)
        assert isinstance(key, wx_tools.keyboard.Key)
        (modifiers, key_code) = key.to_accelerator_pair()
        accelerator = (modifiers, key_code, id)
        accelerators.append(accelerator)
    return accelerators
def _key_dict_to_accelerators(key_dict):
    '''
    Convert a dict mapping keys to ids to a list of accelerators.
    
    The values of `key_dict` are wxPython IDs. The keys may be either:
    
      - `Key` instances.
      - Key-codes given as `int`s.
      - Tuples of `Key` instances and/or key-codes given as `int`s.

    Example:
    
        _key_dict_to_accelerators(
            {Key(ord('Q')): quit_id,
             (Key(ord('R'), cmd=True),
              Key(wx.WXK_F5)): refresh_id,
             wx.WXK_F1: help_id}
        ) == [
            (wx.ACCEL_NORMAL, ord('Q'), quit_id),
            (wx.ACCEL_CMD, ord('R'), refresh_id),
            (wx.ACCEL_NORMAL, ord('Q'), refresh_id),
            (wx.ACCEL_NORMAL, wx.WXK_F1, help_id),
        ]
    
    '''

    accelerators = []

    original_key_dict = key_dict
    key_dict = {}

    ### Breaking down key tuples to individual entries: #######################
    #                                                                         #
    for key, id in original_key_dict.items():
        if sequence_tools.is_sequence(key):
            key_sequence = key
            for actual_key in key_sequence:
                key_dict[actual_key] = id
        else:
            key_dict[key] = id
    #                                                                         #
    ### Finished breaking down key tuples to individual entries. ##############

    for key, id in key_dict.items():
        if isinstance(key, int):
            key = wx_tools.keyboard.Key(key)
        assert isinstance(key, wx_tools.keyboard.Key)
        (modifiers, key_code) = key.to_accelerator_pair()
        accelerator = (modifiers, key_code, id)
        accelerators.append(accelerator)
    return accelerators
Пример #5
0
 def __init__(self, iterable):
     was_given_a_sequence = sequence_tools.is_sequence(iterable) and \
                            not isinstance(iterable, LazyTuple)
     
     self.exhausted = True if was_given_a_sequence else False
     '''Flag saying whether the internal iterator is totally exhausted.'''
     
     self.collected_data = list(iterable) if was_given_a_sequence else []
     '''All the items that were collected from the iterable.'''
     
     self._iterator = None if was_given_a_sequence else iter(iterable)
     '''The internal iterator from which we get data.'''
     
     self.lock = threading.Lock()
     '''Lock used while exhausting to make `LazyTuple` thread-safe.'''
Пример #6
0
    def __init__(self, iterable):
        was_given_a_sequence = sequence_tools.is_sequence(iterable) and \
                               not isinstance(iterable, LazyTuple)

        self.exhausted = True if was_given_a_sequence else False
        '''Flag saying whether the internal iterator is totally exhausted.'''

        self.collected_data = list(iterable) if was_given_a_sequence else []
        '''All the items that were collected from the iterable.'''

        self._iterator = None if was_given_a_sequence else iter(iterable)
        '''The internal iterator from which we get data.'''

        self.lock = threading.Lock()
        '''Lock used while exhausting to make `LazyTuple` thread-safe.'''
def test_length_2():

    # `iterate_overlapping_subsequences` returns an iterator, not a sequence:
    assert not sequence_tools.is_sequence(
        iterate_overlapping_subsequences(list(range(4))))

    assert tuple(iterate_overlapping_subsequences(list(range(4)))) == \
           tuple(iterate_overlapping_subsequences(range(4))) == \
           ((0, 1), (1, 2), (2, 3))

    assert tuple(iterate_overlapping_subsequences(list(range(4)),
                                                  wrap_around=True)) == \
           tuple(iterate_overlapping_subsequences(range(4),
                                                  wrap_around=True)) ==\
           ((0, 1), (1, 2), (2, 3), (3, 0))

    assert tuple(iterate_overlapping_subsequences('meow')) == \
           (('m', 'e'), ('e', 'o'), ('o', 'w'))
def test_length_2():
    
    # `iterate_overlapping_subsequences` returns an iterator, not a sequence:
    assert not sequence_tools.is_sequence(
        iterate_overlapping_subsequences(range(4))
    )
                                          
    assert tuple(iterate_overlapping_subsequences(range(4))) == \
           tuple(iterate_overlapping_subsequences(xrange(4))) == \
           ((0, 1), (1, 2), (2, 3))
                                          
    assert tuple(iterate_overlapping_subsequences(range(4),
                                                  wrap_around=True)) == \
           tuple(iterate_overlapping_subsequences(xrange(4),
                                                  wrap_around=True)) ==\
           ((0, 1), (1, 2), (2, 3), (3, 0))
                                          
    assert tuple(iterate_overlapping_subsequences('meow')) == \
           (('m', 'e'), ('e', 'o'), ('o', 'w'))