Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'oct': 0.02; 'example:': 0.03; 'string.': 0.04; '(except': 0.05; 'json': 0.07; 'space.': 0.07; 'derived': 0.09; 'dict': 0.09; 'format?': 0.09; 'forward.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'storing': 0.09; 'tuple': 0.09; 'tuple.': 0.09; 'am,': 0.12; 'binary': 0.13; 'float': 0.13; 'library': 0.14; 'converting': 0.15; 'fixed.': 0.15; "'',": 0.16; '(double)': 0.16; '(long': 0.16; '42,': 0.16; 'block,': 0.16; 'compression': 0.16; 'gzip': 0.16; 'pack.': 0.16; 'sequential': 0.16; 'simplest': 0.16; 'subject:memory': 0.16; 'subject:print': 0.16; 'tuple)': 0.16; 'wrote:': 0.18; 'int': 0.18; 'convert': 0.19; '(which': 0.19; 'memory': 0.20; 'later': 0.21; 'trying': 0.21; 'subject:data': 0.21; "doesn't": 0.23; 'smallest': 0.23; 'unlikely': 0.23; 'header:In-Reply-To:1': 0.23; 'string': 0.23; 'variable': 0.24; 'consist': 0.24; "i'm": 0.26; '(and': 0.28; 'network,': 0.28; 'bit': 0.28; 'tried': 0.28; 'fixed': 0.28; '+0200,': 0.30; 'characters,': 0.30; 'efficiently': 0.30; 'strings,': 0.30; 'tuples': 0.30; 'yes.': 0.30; '(the': 0.30; "skip:' 10": 0.30; 'hi,': 0.31; 'fastest': 0.32; 'actual': 0.32; "isn't": 0.32; 'quite': 0.32; 'to:addr:python-list': 0.32; 'actually': 0.33; 'instead': 0.33; 'there': 0.33; 'header:User- Agent:1': 0.33; 'header:X-Complaints-To:1': 0.33; '(as': 0.34; 'file.': 0.34; 'probably': 0.34; 'integer': 0.34; 'keys': 0.34; 'root': 0.34; 'typical': 0.34; 'data,': 0.35; 'sets': 0.35; 'certain': 0.35; 'rather': 0.35; 'file': 0.36; 'fri,': 0.36; 'optimization': 0.36; 'drives': 0.36; 'element': 0.36; 'formats': 0.36; 'minutes.': 0.36; 'entry': 0.37; 'but': 0.37; 'using': 0.37; 'received:org': 0.37; 'could': 0.38; 'steven': 0.38; 'several': 0.38; 'some': 0.38; 'expensive': 0.38; 'enough': 0.38; 'should': 0.39; 'subject: (': 0.39; 'subject:: ': 0.39; 'might': 0.39; 'to:addr:python.org': 0.39; 'data': 0.40; 'files': 0.40; 'store': 0.40; 'more': 0.60; 'within': 0.60; 'per': 0.61; 'your': 0.61; 'efficient': 0.61; '2011': 0.62; 'order': 0.62; 'exceed': 0.64; 'zip': 0.64; 'manner': 0.65; 'demand': 0.65; 'plus': 0.66; 'monthly': 0.68; 'become': 0.69; 'transfer': 0.72; 'months.': 0.73; 'thousand': 0.73; 'dict,': 0.84; 'known)': 0.84; 'payload': 0.84; 'premature': 0.84; 'subject:foot': 0.84; 'subject:types': 0.84; 'required)': 0.91; 'transferred': 0.96 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Gelonida N Subject: Re: save tuple of simple data types to disk (low memory foot print) Date: Sat, 29 Oct 2011 18:44:14 +0200 References: <4eab5021$0$29968$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: unicorn.dungeon.de User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.23) Gecko/20110921 Lightning/1.0b2 "" In-Reply-To: <4eab5021$0$29968$c3e8da3$5496439d@news.astraweb.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 118 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1319906669 news.xs4all.nl 6977 [2001:888:2000:d::a6]:37705 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:15136 On 10/29/2011 03:00 AM, Steven D'Aprano wrote: > On Fri, 28 Oct 2011 22:47:42 +0200, Gelonida N wrote: > >> Hi, >> >> I would like to save many dicts with a fixed amount of keys tuples to a >> file in a memory efficient manner (no random, but only sequential >> access is required) > > What do you mean "keys tuples"? Corrected phrase: I would like to save many dicts with a fixed (and known) amount of keys in a memory efficient manner (no random, but only sequential access is required) to a file (which can later be sent over a slow expensive network to other machines) Example: Every dict will have the keys 'timestamp', 'floatvalue', 'intvalue', 'message1', 'message2' 'timestamp' is an integer 'floatvalue' is a float 'intvalue' an int 'message1' is a string with a length of max 2000 characters, but can often be very short 'message2' the same as message1 so a typical dict will look like { 'timetamp' : 12, 'floatvalue': 3.14159, 'intvalue': 42, 'message1' : '', 'message2' : '=' * 1999 } > > What do you call "many"? Fifty? A thousand? A thousand million? How many > items in each dict? Ten? A million? File size can be between 100kb and over 100Mb per file. Files will be accumulated over months. I just want to use the smallest possible space, as the data is collected over a certain time (days / months) and will be transferred via UMTS / EDGE / GSM network, where the transfer takes already for quite small data sets several minutes. I want to reduce the transfer time, when requesting files on demand (and the amount of data in order to not exceed the monthly quota) >> As the keys are the same for each entry I considered converting them to >> tuples. > > I don't even understand what that means. You're going to convert the keys > to tuples? What will that accomplish? >> As the keys are the same for each entry I considered converting them (the before mentioned dicts) to tuples. so the dict { 'timetamp' : 12, 'floatvalue': 3.14159, 'intvalue': 42, 'message1' : '', 'message2' : '=' * 1999 } would become [ 12, 3.14159, 42, '', ''=' * 1999 ] > > >> The tuples contain only strings, ints (long ints) and floats (double) >> and the data types for each position within the tuple are fixed. >> >> The fastest and simplest way is to pickle the data or to use json. Both >> formats however are not that optimal. > > How big are your JSON files? 10KB? 10MB? 10GB? > > Have you tried using pickle's space-efficient binary format instead of > text format? Try using protocol=2 when you call pickle.Pickler. No. This is probably already a big step forward. As I know the data types if each element in the tuple I would however prefer a representation, which is not storing the data types for each typle over and over again (as they are the same for each dict / tuple) > > Or have you considered simply compressing the files? Compression makes sense but the inital file format should be already rather 'compact' > >> I could store ints and floats with pack. As strings have variable length >> I'm not sure how to save them efficiently (except adding a length first >> and then the string. > > This isn't 1980 and you're very unlikely to be using 720KB floppies. > Premature optimization is the root of all evil. Keep in mind that when > you save a file to disk, even if it contains only a single bit of data, > the actual space used will be an entire block, which on modern hard > drives is very likely to be 4KB. Trying to compress files smaller than a > single block doesn't actually save you any space. > > >> Is there already some 'standard' way or standard library to store such >> data efficiently? > > Yes. Pickle and JSON plus zip or gzip. > pickle protocol-2 + gzip of the tuple derived from the dict, might be good enough for the start. I have to create a little more typical data in order to see how many percent of my payload would consist of repeating the data types for each tuple.