Skip to main content

(Thread-safe) list(s) with extended / enhanced in-place capabilities (e.g. conditional element selection)

Project description

The 'EnhaaancedLists' library contains list classes with extended / enhanced in-place capabilities.

Together with the 'elem' term / alias (comprised too), some of their methods (also) allow using a new operator notation for selecting list elements - closely resembling mathematical conditions.

EnhList

Examples for additional capabilities (the standard list operations work too) are:

( Note that the '&' resp. the '|' are 'abused' as 'logical and resp. logical or' in this context (and not 'bitwise'!) ).

    #convert a parameter list into an enhanced list 
    eL = EnhList(1,3,5,7)                                       #eL: [1,3,5,7]

    #push single as well as multiple elements into the list
    eL.push(9)                                  ==> None        #eL: [1,3,5,7,9]
    eL.push(11,13,15)                           ==> None        #eL: [1,3,5,7,9,11,13,15]

    #pop single as well as multiple elements from the list - 
    #note that push/pop implements a FIFO - in contrast to the standard list (LIFO)
    eL.pop()                                    ==> 1           #eL: [3,5,7,9,11,13,15]
    eL.pop( (elem > 3) & (elem < 11), single )  ==> 5           #eL: [3,7,9,11,13,15]
    eL.pop( (elem > 3) & (elem < 11)         )  ==> [7,9]       #eL: [3,11,13,15]      

    #get items from list
    eL[ elem >= 10         ]                    ==> [11,13,15]  #eL: unchanged
    eL[ elem >= 10, single ]                    ==> 11          #eL: unchanged
    eL[ elem <  3,  single ]                    ==> None        #eL: unchanged

    #check whether list contains items
    ( elem <  3 ) in eL                         ==> False       #eL: unchanged
    ( elem >= 3 ) in eL                         ==> True        #eL: unchanged

    #delete items from list
    del eL[ elem < 12, single ]                 ==> ---         #eL: [11,13,15]
    del eL[ elem > 12         ]                 ==> ---         #eL: [11]

    eL = EnhList(1,3,5,7)                                       #eL: [1,3,5,7]
    #check whether all element meet a condition
    eL.areAll( elem % 2 == 1 )                  ==> True        #eL: unchanged
    eL.areAll( elem     >= 3 )                  ==> False       #eL: unchanged

    #map function on elements / work with items of elements
    eL.mapIf( lambda x: dict(a=x) )                          
                        ==> None        #eL: [{'a':1},{'a':3},{'a':5},{'a':7}]
    eL.mapIf( lambda x: x['a'] + 1, elem['a'] > 3)           
                        ==> None        #eL: [{'a':1},{'a':3},6,8]

    #work with attributes of elements
    class Attr(object):
        def __init__(self, value):
            self.a = value
        def __repr__(self):
            return ".a=%s" % self.a
    eL.mapIf( lambda x: Attr(x), lambda x: type(x) ==  int ) 
                        ==> None        #eL: [{'a':1},{'a':3},.a=6,.a=8]

SecList

The 'SecList' class is a secured version of the enhanced list class 'EnhList'.

Access to its elements has been made 'thread-safe' by wrapping the belonging methods in a 'with' context automatically 'acquiring' / 'releasing' an internal 'SemiBlockingMutex' (a special multithreading / multiprocessing lock).

Example:

   #convert a parameter list into a secured list 
   sL = SecList(1,3,5,7,9,11,13)                                #sL: [1,3,5,7,9,11,13]
   
   #if then a first thread e.g. would run the following statement:
   poppedLtL = sL.pop( (elem < 9) )
   
   #and a second thread in parallel (!!) e.g. would run the following statement:
   poppedGtL = sL.pop( (elem > 7) )
   
   #there would be no error and the result would be:
   #poppedLtL <==> [1,3,5,7]
   #poppedGtL <==> [9,11,13]
   #sL        <==> [] 

Further Informations

Detailed descriptions can be found in the doc/help-texts of said 'EnhList' and 'SecList' classes and their methods. E.g. try:

    python -i      #and then:
    help(EnhList)
    help(SecList)  

    #and/or
    python enhaaancedLists.py --intro
    python enhaaancedLists.py --test

More examples can be found in the source code of the selftest() function of the "enhaaancedList.py" library module and the methods called from there.

Also note, that 'elem' just is an alias defined as follows:

elem = ConditionFunction(lambda x: x)

That means, that more informations about 'elem' also can be found in the doc/help-text belonging to the class 'ConditionFunction', which, by the way, is inherited from functools.partial.

Further infomations and links can be found on my homepage https://www.blackward.de

Have Fun!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

enhaaancedLists-0.75-py2.py3-none-any.whl (23.5 kB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page