Package: thecollector
The CollectorThis is still under heavy developmentThe idea behind this a container that can store data and time tag the
data when it is captured. The main structure is a dict which has keys
for each data series stored.This was written for a class I taught on robotics. It is meant to be simple and
teach the students some things. There are probably better solutions out there,
but I like this. :smirk:Additionally, there is nothing magically about what this does:It provides a generic interface to usingpickle,json, ormsgpackas
the protocol for saving data to diskIt also allows you to convert between them if neededBag files can be read using the original protocol, thus data is never lost
if this library goes awayDesigned to be simple and straight forwardSetupInstallThe suggested way to install this is via thepipcommand as follows:pip install the_collector
pip install the_collector[numpy]If you installnumpy, then you get access to working with numpy arrays
using the functions:array_pack()andarray_pack(). These really don't
save you much.UsageBagItBag stores data in memory until the buffer size limit is reached then it dumps
the data to a file.#!/usr/bin/env python3from__future__importprint_functionfromthe_collectorimportBagItfromthe_collectorimportJson,MsgPack,Pickleimportjsond={'a':1,'b':2}bag=BagIt(Json)# ascii, cross platform# bag = BagIt(Pickle)  # binary, python only but handles python objects well# bag = BagIt(MsgPack) # binary, may have to write special packer for classesforiinrange(10):bag.push('test',d)# push value d to key 'test'bag.push('bob',d)bag.push('tom',('a',i,))# timestamp adds a timestamp automatically to the bag file. Thus, you won't# over write bob.json.bag each time you run this program because the filename# is bob-2019-04-20-15:35:25.6543.json.bagfname=bag.write('bob',timestamp=False)print(">> created:",fname)data=bag.read(fname)# data is just a dictionaryprint(data)Now, since there is nothing super specialthe_collectordoes with packing
data, you can always read the bag files using the original libraries:withopen(fname,'rb')asfd:data=json.load(fd)forkey,valindata.items():print("{}[{}]".format(key,len(val)))forvinval:print("{}".format(v),end=' ')print(' ')Circular BufferThis is perhaps a little out of place, but I use circular buffers with handling data
so I stuck it in this library.fromthe_collectorimportCircularBuffercb=CircularBuffer(60)# can only hold 60 items before it copies over data# Let's push way more than 60 thingsforiinrange(200):cb.push(i)print(cb.get_all())# print everythingprint('get cb[7]',cb[7])print('get cb[0]',cb[0])print('get last',cb.get_last())Data TupleUse a genericnamedtuplefor data storage. It will automatically insert a
timestamp when created. This is useful for tagging data with a timestamp and
not having to remember to do it yourself.WARNING:jsonandmsgpackhave issues withtuples, so this is best
used withpickleor you have to acceptjsonwill turn it into a list andmsgpackwill turn it into a regulartuple. Either way, you still keep the
timestamp.fromthe_collectorimportDatad=Data((1,2,3,))# timestamp generate when madeprint("Data[{}]:{}".format(d.timestamp,d.data))print("Namedtuple output:",d)Todoadd github actionslook at enablingBytesIOfor testing/working so you don't litter filing system
with test bag filessupport PEP517 and replacesetup.pywithpyprogram.tomlmove tests fromnosetopytestsee if there is value inmatplotlibintegration or just add an iterator capabilityExample:plt.plot(mydata['bob'])and will plot 2D or 3D dataChange LogDateVersionNotes2019-07-060.8.2add generic data container2019-04-280.8.0can store data usingjson,pickle, ormsgpack2018-07-250.7.0addedmsgpackmessages and a way to do custom messages2018-07-140.6.0changed interface to support buffered writing to disk2018-07-090.5.0moved away fromjsonand now usingmsgpack2017-11-230.4.0fixes, documentation, unit tests2017-10-040.0.1initThe MIT License (MIT)Copyright (c) 2017 Kevin J. WalchkoPermission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.