Ticket #8156: trac_8156a.patch

File trac_8156a.patch, 2.2 KB (added by zimmerma, 12 years ago)
  • sage/interfaces/all.py

    # HG changeset patch
    # User Paul Zimmermann <zimmerma@loria.fr>
    # Date 1271694750 -7200
    # Node ID 8eb2c2d369aadf96e8424a625073718370f1cb05
    # Parent  ea02bc44fa947f97673768aede1126e6bc7b2c79
    provides new functions read_data (cf #8156)
    
    diff -r ea02bc44fa94 -r 8eb2c2d369aa sage/interfaces/all.py
    a b  
    3333from povray import povray
    3434from lie import lie, lie_console, LiE
    3535from r import r, r_console, R, r_version, is_RElement
     36from read_data import read_data
    3637
    3738# signal handling
    3839from get_sigs import *
  • new file sage/interfaces/read_data.py

    diff -r ea02bc44fa94 -r 8eb2c2d369aa sage/interfaces/read_data.py
    - +  
     1"""
     2An interface to read data files
     3"""
     4
     5###############################################################################
     6#   Sage: System for Algebra and Geometry Experimentation   
     7#       Copyright (C) 2010 Paul Zimmermann
     8#  Distributed under the terms of the GNU General Public License (GPL),
     9#  version 2 or later. The full text of the GPL is available at:
     10#                  http://www.gnu.org/licenses/
     11###############################################################################
     12
     13def read_data(f,t):
     14   """
     15   Read data from file 'f' and class 't' (one element per line),
     16   and returns a list of elements.
     17
     18   INPUT:
     19
     20       - 'f' - a file name
     21       - 't' - a class (objects will be coerced to that class)
     22
     23   OUTPUT:
     24
     25       a list of elements of class 't'.
     26
     27   EXAMPLES::
     28
     29       sage: indata = os.path.join(SAGE_TMP, 'in.data')       
     30       sage: f = open(indata, "w")
     31       sage: f.write("17\n42\n")
     32       sage: f.close()
     33       sage: l = read_data(indata, ZZ); l
     34       [17, 42]
     35       sage: f = open(indata, "w")
     36       sage: f.write("1.234\n5.678\n")
     37       sage: f.close()
     38       sage: l = read_data(indata, RealField(17)); l
     39       [1.234, 5.678]
     40   """
     41   fp = open(f,"r")
     42   l = []
     43   while True:
     44      s = fp.readline()
     45      s = s.rstrip()
     46      if s == '':
     47         break
     48      l.append(t(s))
     49   fp.close()
     50   return l