Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #15138

Re: save tuple of simple data types to disk (low memory foot print)

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python.list@tim.thechases.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'csv': 0.09; 'data:': 0.09; 'dict': 0.09; 'finally:': 0.09; 'output': 0.10; 'float': 0.13; 'cc:addr:python-list': 0.15; "'',": 0.16; "'hello": 0.16; '(name,': 0.16; '(str,': 0.16; '-tkc': 0.16; '42,': 0.16; 'compression': 0.16; 'f.close()': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'gzip': 0.16; 'message- id:@tim.thechases.com': 0.16; 'received:70.251': 0.16; 'received:dsl.rcsntx.swbell.net': 0.16; 'received:rcsntx.swbell.net': 0.16; 'received:swbell.net': 0.16; 'row': 0.16; 'sequential': 0.16; 'subject:memory': 0.16; 'subject:print': 0.16; 'wrote:': 0.18; '1999,': 0.18; 'int': 0.18; '(which': 0.19; 'appropriate': 0.19; 'memory': 0.20; 'later': 0.21; 'cc:no real name:2**0': 0.21; 'subject:data': 0.21; "doesn't": 0.23; 'header:In-Reply-To:1': 0.23; 'string': 0.23; 'cc:2**0': 0.25; 'code': 0.25; '(and': 0.28; 'import': 0.28; 'fixed': 0.28; 'cc:addr:python.org': 0.29; 'characters,': 0.30; "skip:' 10": 0.30; 'pretty': 0.32; 'quite': 0.32; 'it.': 0.33; 'header:User-Agent:1': 0.33; 'file.': 0.34; 'duplicate': 0.34; 'integer': 0.34; 'keys': 0.34; 'try:': 0.34; 'typical': 0.34; 'rather': 0.35; 'file': 0.36; 'something': 0.36; 'but': 0.37; 'some': 0.38; 'expensive': 0.38; 'should': 0.39; 'subject: (': 0.39; 'subject:: ': 0.39; 'data': 0.40; 'files': 0.40; 'per': 0.61; 'your': 0.61; 'efficient': 0.61; 'here': 0.65; 'manner': 0.65; 'below.': 0.65; 'months.': 0.73; 'thousand': 0.73; '(int,': 0.84; 'known)': 0.84; 'subject:foot': 0.84; 'subject:types': 0.84; '10000': 0.91; 'need,': 0.91; 'required)': 0.91; 'writer,': 0.91
Date Sat, 29 Oct 2011 12:47:42 -0500
From Tim Chase <python.list@tim.thechases.com>
User-Agent Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.20) Gecko/20110826 Icedove/3.1.12
MIME-Version 1.0
To Gelonida N <gelonida@gmail.com>
Subject Re: save tuple of simple data types to disk (low memory foot print)
References <mailman.2293.1319834877.27778.python-list@python.org> <4eab5021$0$29968$c3e8da3$5496439d@news.astraweb.com> <j8hagu$1o3$1@dough.gmane.org>
In-Reply-To <j8hagu$1o3$1@dough.gmane.org>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-AntiAbuse This header was added to track abuse, please include it with any abuse report
X-AntiAbuse Primary Hostname - boston.accountservergroup.com
X-AntiAbuse Original Domain - python.org
X-AntiAbuse Originator/Caller UID/GID - [47 12] / [47 12]
X-AntiAbuse Sender Address Domain - tim.thechases.com
X-Source
X-Source-Args
X-Source-Dir
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2310.1319912594.27778.python-list@python.org> (permalink)
Lines 104
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1319912594 news.xs4all.nl 6957 [2001:888:2000:d::a6]:38988
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:15138

Show key headers only | View raw


On 10/29/11 11:44, Gelonida N wrote:
> 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.

If Steven's pickle-protocol2 solution doesn't quite do what you 
need, you can do something like the code below.  Gzip is pretty 
good at addressing...

>> Or have you considered simply compressing the files?
> Compression makes sense but the inital file format should be
> already rather 'compact'

...by compressing out a lot of the duplicate aspects.  Which also 
mitigates some of the verbosity of CSV.

It serializes the data to a gzipped CSV file then unserializes 
it.  Just point it at the appropriate data-source, adjust the 
column-names and data-types

-tkc

from gzip import GzipFile
from csv import writer, reader

data = [ # use your real data here
     {
     'timestamp': 12,
     'floatvalue': 3.14159,
     'intvalue': 42,
     'message1': 'hello world',
     'message2': '=' * 1999,
     },
     ] * 10000


f = GzipFile('data.gz', 'wb')
try:
     w = writer(f)
     for row in data:
         w.writerow([
             row[name] for name in (
             # use your real col-names here
             'timestamp',
             'floatvalue',
             'intvalue',
             'message1',
             'message2',
             )])
finally:
     f.close()

output = []
for row in reader(GzipFile('data.gz')):
     d = dict((
         (name, f(row[i]))
         for i, (f,name) in enumerate((
             # adjust for your column-names/data-types
             (int, 'timestamp'),
             (float, 'floatvalue'),
             (int, 'intvalue'),
             (str, 'message1'),
             (str, 'message2'),
             ))))
     output.append(d)

# or

output = [
     dict((
         (name, f(row[i]))
         for i, (f,name) in enumerate((
             # adjust for your column-names/data-types
             (int, 'timestamp'),
             (float, 'floatvalue'),
             (int, 'intvalue'),
             (str, 'message1'),
             (str, 'message2'),
             ))))
     for row in reader(GzipFile('data.gz'))
     ]

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

save tuple of simple data types to disk (low memory foot print) Gelonida N <gelonida@gmail.com> - 2011-10-28 22:47 +0200
  Re: save tuple of simple data types to disk (low memory foot print) Roy Smith <roy@panix.com> - 2011-10-28 19:08 -0400
    Re: save tuple of simple data types to disk (low memory foot print) Gelonida N <gelonida@gmail.com> - 2011-10-29 18:47 +0200
  Re: save tuple of simple data types to disk (low memory foot print) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-10-29 01:00 +0000
    Re: save tuple of simple data types to disk (low memory foot print) Gelonida N <gelonida@gmail.com> - 2011-10-29 18:44 +0200
    Re: save tuple of simple data types to disk (low memory foot print) Tim Chase <python.list@tim.thechases.com> - 2011-10-29 12:47 -0500

csiph-web