Package: rangetree
rangetreeis an Apache2 licensed library, written in Python 3, for easy and fast
lookups of numeric ranges.Given three integer ranges, 0 - 9, 10 - 99, and 100 - 999,rangetreemakes
it trivial to look up exactly which range any integer falls in. (Note Python
slices and ranges include the first index, and exclude the second.)>>>fromrangetreeimportRangeTree>>>>>>r=RangeTree()>>>r[0:10]='single digits'>>>r[10:100]='double digits'>>>r[100:1000]='triple digits'>>>>>>r[4]'single digits'RangeTrees are optimized for lookups, and make use of the excellent
bintrees library.Featuressupports open and closed rangessupports integer keysoptimized for lookups (not insertions)InstallationTo installrangetree, simply:$pipinstallrangetreeUsageInsertion is done using Python’s slice notation, or using range objects.>>>r=RangeTree()>>>r[0:10]='single digits'>>>r[range(10,100)]='double digits'Negative integers are supported.>>>r[-10:0]='negative singles'Missing a range will result in aKeyError. UseRangetree.get()or
theinoperator.>>>1000inrFalse>>>r[1000]Traceback(mostrecentcalllast):File"<stdin>",line1,in<module>File"rangetree.py",line93,in__getitem__raiseKeyError(key)KeyError:1000>>>r.get(1000,'no value')'no value'Open ranges (that go to or from infinity) are supported. Setting open ranges is only
possible using the slice notation.>>>r[1000:]='quadruple digits or more'>>>r[999999999]'quadruple digits or more'Overlapping ranges will result in aKeyError.>>>r=RangeTree()>>>r[1000:]='quadruple digits or more'>>>r[10000:]='ten thousand'Traceback(mostrecentcalllast):File"<stdin>",line1,in<module>File"rangetree.py",line58,in__setitem__raiseKeyError('Overlapping intervals.')KeyError:'Overlapping intervals.'rangetreeis fast. Usingperf, given 2000 intervals:$pyperftimeit--rigorous-g--duplicate5-s"from rangetree import RangeTree; r = RangeTree()"-s"for i in range(2000):"-s" r[i*10:i*10+10] = i""r[500]".........................................3.75us:1#######3.77us:2#############3.80us:9###########################################################3.82us:5#################################3.84us:8#####################################################3.86us:9###########################################################3.89us:7##############################################3.91us:8#####################################################3.93us:8#####################################################3.95us:6########################################3.98us:10##################################################################4.00us:12###############################################################################4.02us:5#################################4.05us:9###########################################################4.07us:5#################################4.09us:6########################################4.11us:3####################4.14us:4##########################4.16us:2#############4.18us:0|4.20us:1#######Median+-stddev:3.97us+-0.11usThe ballpark figure for lookups is in the single digit microseconds.Changelog1.0 (2016-10-20)Initial public release.ContributingContributions are very welcome. Tests can be run withtox, please ensure
the coverage at least stays the same before you submit a pull request.CreditsThe development ofrangetreeis sponsored byNanobit.rangetreeis tested withHypothesis, by David R. MacIver.rangetreeis benchmarked usingperf, by Victor Stinner.