data_store.py :  » Math » SciPy » scipy » scipy » io » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Math » SciPy 
SciPy » scipy » scipy » io » data_store.py
""" Load or save values to a file.

    Shelves work well for storing data, but they are slow to access
    repeatedly - especially for large data sets.  This module allows
    you to store data to a file and then load it back into the workspace.
    When the data is stored, a python module is also created as the
    "namespace for the data"
    >>> import scipy.io
    >>> import os
    >>> a = 1
    >>> scipy.io.save_as_module('c:/temp/junker',{'a':a})
    >>> os.chdir('c:/temp')
    >>> import junker
    >>> junker.a
    1
"""

__all__ = ['save_as_module',
           # The rest of these are all deprecated
           'save', 'create_module',
           'create_shelf', 'load']

import dumb_shelve
import os

from numpy import deprecate

def _load(module):
    """ Load data into module from a shelf with
        the same name as the module.
    """
    dir,filename = os.path.split(module.__file__)
    filebase = filename.split('.')[0]
    fn = os.path.join(dir, filebase)
    f = dumb_shelve.open(fn, "r")
    #exec( 'import ' + module.__name__)
    for i in f.keys():
        exec( 'import ' + module.__name__+ ';' +
              module.__name__+'.'+i + '=' + 'f["' + i + '"]')
#       print i, 'loaded...'
#   print 'done'

load = deprecate(_load, message="""
This is an internal function used with scipy.io.save_as_module

If you are saving arrays into a module, you should think about using
HDF5 or .npz files instead.
""")


def _create_module(file_name):
    """ Create the module file.
    """
    if not os.path.exists(file_name+'.py'): # don't clobber existing files
        module_name = os.path.split(file_name)[-1]
        f = open(file_name+'.py','w')
        f.write('import scipy.io.data_store as data_store\n')
        f.write('import %s\n' % module_name)
        f.write('data_store._load(%s)' % module_name)
        f.close()

create_module = deprecate(_create_module, message="""
This is an internal function used with scipy.io.save_as_module

If you are saving arrays into a module, you should think about
using HDF5 or .npz files instead.
""")

def _create_shelf(file_name,data):
    """Use this to write the data to a new file
    """
    shelf_name = file_name.split('.')[0]
    f = dumb_shelve.open(shelf_name,'w')
    for i in data.keys():
#       print 'saving...',i
        f[i] = data[i]
#   print 'done'
    f.close()

create_shelf = deprecate(_create_shelf, message="""
This is an internal function used with scipy.io.save_as_module

If you are saving arrays into a module, you should think about using
HDF5 or .npz files instead.
""")


def save_as_module(file_name=None,data=None):
    """
    Save the dictionary "data" into a module and shelf named save.

    Parameters
    ----------
    file_name : str, optional
        File name of the module to save.
    data : dict, optional
        The dictionary to store in the module.

    """
    _create_module(file_name)
    _create_shelf(file_name,data)

save = deprecate(save_as_module, old_name='save', new_name='save_as_module')
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.