Ticket #8156: trac_8156.patch

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

    # HG changeset patch
    # User Paul Zimmermann <zimmerma@loria.fr>
    # Date 1266917687 -3600
    # Node ID ff241da7b3c8e0869cfdd986c3b4afa034bcbc8c
    # Parent  46b1ee5997ac7e7faafdd86751fe7d121b6c1385
    added new function read_data
    
    diff -r 46b1ee5997ac -r ff241da7b3c8 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 46b1ee5997ac -r ff241da7b3c8 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 type 't' (one element per line),
     16   and returns a list of elements.
     17
     18   OUTPUT:
     19
     20       a list of elements of type 't'.
     21
     22   EXAMPLES::
     23       
     24       sage: !echo "17" > in.data; echo "42" >> in.data
     25       sage: cat in.data
     26       17
     27       42
     28       sage: l = read_data("in.data", ZZ); l
     29       [17. 42]
     30       sage: !echo "1.234" > in.data; echo "5.678" >> in.data
     31       sage: l = read_data("in.data", RealField(17)); l
     32       [1.234, 5.678]
     33   """
     34   fp = open(f,"r")
     35   l = []
     36   while True:
     37      s = fp.readline()
     38      s = s.rstrip()
     39      if s == '':
     40         break
     41      l.append(t(s))
     42   fp.close()
     43   return l