@@ -1757,25 +1757,7 @@ def do_show_index(self, arg):
1757
1757
def complete_show_index (self , text , line , begidx , lastidx ):
1758
1758
return self ._complete_tables (text )
1759
1759
1760
- def get_filename (self , line ):
1761
- # Good to move some of this into eautils.fileio
1762
- line = line .replace (';' , '' )
1763
- if line == "" :
1764
- print ('\n Must include table filename!\n ' )
1765
- return
1766
- if line .find ('.' ) == - 1 :
1767
- print (colored ('\n Error in filename\n ' , "red" ))
1768
- return
1769
-
1770
- filename = "" .join (line .split ())
1771
- basename = os .path .basename (filename )
1772
- alls = basename .split ('.' )
1773
- if len (alls ) > 2 :
1774
- # Oracle tables cannot contain a '.'
1775
- print ("\n Do not use extra '.' in filename\n " )
1776
- return
1777
1760
1778
- return filename
1779
1761
1780
1762
def check_table_exists (self , table ):
1781
1763
# check table first
@@ -1915,11 +1897,11 @@ def insert_data(self, table, columns, values, dtypes=None, niter = 0):
1915
1897
niter + 1 , len (values ), len (columns ), table .upper (), t2 - t1 ), "green" ))
1916
1898
1917
1899
1918
- def do_load_table (self , line , name = None , chunksize = None ):
1900
+ def do_load_table (self , line , name = None , chunksize = None , memsize = None ):
1919
1901
"""
1920
1902
DB:Loads a table from a file (csv or fits) taking name from filename and columns from header
1921
1903
1922
- Usage: load_table <filename> [--tablename NAME] [--chunksize CHUNK]
1904
+ Usage: load_table <filename> [--tablename NAME] [--chunksize CHUNK] [--memsize MEMCHUNK]
1923
1905
Ex: example.csv has the following content
1924
1906
RA,DEC,MAG
1925
1907
1.23,0.13,23
@@ -1932,6 +1914,8 @@ def do_load_table(self, line, name=None, chunksize=None):
1932
1914
--tablename NAME given name for the table, default is taken from filename
1933
1915
--chunksize CHUNK Number of rows to be inserted at a time. Useful for large files
1934
1916
that do not fit in memory
1917
+ --memsize MEMCHUNK The size in Mb to be read in chunks. If both specified, the lower
1918
+ number of rows is selected (the lower memory limitations)
1935
1919
1936
1920
Note: - For csv or tab files, first line must have the column names (without # or any other comment) and same format
1937
1921
as data (using ',' or space)
@@ -1943,7 +1927,9 @@ def do_load_table(self, line, name=None, chunksize=None):
1943
1927
load_parser .add_argument ('filename' , help = 'name for the file' , action = 'store' , default = None )
1944
1928
load_parser .add_argument ('--tablename' , help = 'name for the table' , action = 'store' , default = None )
1945
1929
load_parser .add_argument ('--chunksize' , help = 'number of rows to read in blocks to avoid memory '
1946
- 'issues' , action = 'store' , type = int , default = None )
1930
+ 'issues' , action = 'store' , type = int , default = None )
1931
+ load_parser .add_argument ('--memsize' , help = 'size of the chunks to be read in Mb ' ,
1932
+ action = 'store' , type = int , default = None )
1947
1933
load_parser .add_argument ('-h' , '--help' , help = 'print help' , action = 'store_true' )
1948
1934
try :
1949
1935
load_args = load_parser .parse_args (line .split ())
@@ -1953,11 +1939,20 @@ def do_load_table(self, line, name=None, chunksize=None):
1953
1939
if load_args .help :
1954
1940
self .do_help ('load_table' )
1955
1941
return
1956
- filename = self .get_filename (load_args .filename )
1942
+ filename = eafile .get_filename (load_args .filename )
1957
1943
table = load_args .tablename
1958
1944
chunk = load_args .chunksize
1945
+ memchunk = load_args .memsize
1959
1946
if chunksize is not None :
1960
1947
chunk = chunksize
1948
+ if memsize is not None :
1949
+ memchunk = memsize
1950
+ if memchunk is not None :
1951
+ memchunk_rows = eafile .get_chunksize (filename , memory = memchunk )
1952
+ if chunk is not None :
1953
+ chunk = min (chunk , memchunk_rows )
1954
+ else :
1955
+ chunk = memchunk_rows
1961
1956
if filename is None : return
1962
1957
base , ext = os .path .splitext (os .path .basename (filename ))
1963
1958
@@ -1978,7 +1973,7 @@ def do_load_table(self, line, name=None, chunksize=None):
1978
1973
return
1979
1974
1980
1975
try :
1981
- data , iterator = self . load_data (filename )
1976
+ data , iterator = eafile . read_file (filename )
1982
1977
except :
1983
1978
print_exception ()
1984
1979
return
@@ -2070,11 +2065,11 @@ def complete_load_table(self, text, line, start_idx, end_idx):
2070
2065
return _complete_path (line )
2071
2066
2072
2067
2073
- def do_append_table (self , line , name = None , chunksize = None ):
2068
+ def do_append_table (self , line , name = None , chunksize = None , memsize = None ):
2074
2069
"""
2075
2070
DB:Appends a table from a file (csv or fits) taking name from filename and columns from header.
2076
2071
2077
- Usage: append_table <filename> [--tablename NAME] [--chunksize CHUNK]
2072
+ Usage: append_table <filename> [--tablename NAME] [--chunksize CHUNK] [--memsize MEMCHUNK]
2078
2073
Ex: example.csv has the following content
2079
2074
RA,DEC,MAG
2080
2075
1.23,0.13,23
@@ -2087,7 +2082,9 @@ def do_append_table(self, line, name=None, chunksize=None):
2087
2082
2088
2083
--tablename NAME given name for the table, default is taken from filename
2089
2084
--chunksize CHUNK Number of rows to be inserted at a time. Useful for large files
2090
- that do not fit in memory
2085
+ that do not fit in memory
2086
+ --memsize MEMCHUNK The size in Mb to be read in chunks. If both specified, the lower
2087
+ number of rows is selected (the lower memory limitations)
2091
2088
2092
2089
Note: - For csv or tab files, first line must have the column names (without # or any other comment) and same format
2093
2090
as data (using ',' or space)
@@ -2100,6 +2097,8 @@ def do_append_table(self, line, name=None, chunksize=None):
2100
2097
append_parser .add_argument ('--tablename' , help = 'name for the table to append to' , action = 'store' , default = None )
2101
2098
append_parser .add_argument ('--chunksize' , help = 'number of rows to read in blocks to avoid memory '
2102
2099
'issues' , action = 'store' , default = None , type = int )
2100
+ append_parser .add_argument ('--memsize' , help = 'size of the chunks to be read in Mb ' , action = 'store' ,
2101
+ type = int , default = None )
2103
2102
append_parser .add_argument ('-h' , '--help' , help = 'print help' , action = 'store_true' )
2104
2103
try :
2105
2104
append_args = append_parser .parse_args (line .split ())
@@ -2109,11 +2108,21 @@ def do_append_table(self, line, name=None, chunksize=None):
2109
2108
if append_args .help :
2110
2109
self .do_help ('append_table' )
2111
2110
return
2112
- filename = self .get_filename (append_args .filename )
2111
+ filename = eafile .get_filename (append_args .filename )
2113
2112
table = append_args .tablename
2114
2113
chunk = append_args .chunksize
2114
+ memchunk = append_args .memsize
2115
2115
if chunksize is not None :
2116
2116
chunk = chunksize
2117
+ if memsize is not None :
2118
+ memchunk = memsize
2119
+ if memchunk is not None :
2120
+ memchunk_rows = eafile .get_chunksize (filename , memory = memchunk )
2121
+ if chunk is not None :
2122
+ chunk = min (chunk , memchunk_rows )
2123
+ else :
2124
+ chunk = memchunk_rows
2125
+
2117
2126
if filename is None : return
2118
2127
base , ext = os .path .splitext (os .path .basename (filename ))
2119
2128
@@ -2134,7 +2143,7 @@ def do_append_table(self, line, name=None, chunksize=None):
2134
2143
'\n DESDB ~> CREATE TABLE %s (COL1 TYPE1(SIZE), ..., COLN TYPEN(SIZE));\n ' % table .upper ())
2135
2144
return
2136
2145
try :
2137
- data , iterator = self . load_data (filename )
2146
+ data , iterator = eafile . read_file (filename )
2138
2147
except :
2139
2148
print_exception ()
2140
2149
return
@@ -2508,7 +2517,7 @@ def myquota(self):
2508
2517
"""
2509
2518
self .do_myquota ('' )
2510
2519
2511
- def load_table (self , table_file , name = None , chunksize = None ):
2520
+ def load_table (self , table_file , name = None , chunksize = None , memsize = None ):
2512
2521
"""
2513
2522
Loads and create a table in the DB. If name is not passed, is taken from
2514
2523
the filename. Formats supported are 'fits', 'csv' and 'tab' files
@@ -2518,21 +2527,22 @@ def load_table(self, table_file, name=None, chunksize=None):
2518
2527
table_file : Filename to be uploaded as table (.csv, .fits, .tab)
2519
2528
name : Name of the table to be created
2520
2529
chunksize : Number of rows to upload at a time to avoid memory issues
2530
+ memsize : Size of chunk to be read. In Mb. If both specified, the lower number of rows is selected
2521
2531
2522
2532
Returns:
2523
2533
--------
2524
2534
True if success otherwise False
2525
2535
2526
2536
"""
2527
2537
try :
2528
- self .do_load_table (table_file , name = name , chunksize = chunksize )
2538
+ self .do_load_table (table_file , name = name , chunksize = chunksize , memsize = memsize )
2529
2539
return True
2530
2540
except :
2531
2541
# exception
2532
2542
return False
2533
2543
2534
2544
2535
- def append_table (self , table_file , name = None , chunksize = None ):
2545
+ def append_table (self , table_file , name = None , chunksize = None , memsize = None ):
2536
2546
"""
2537
2547
Appends data to a table in the DB. If name is not passed, is taken from
2538
2548
the filename. Formats supported are 'fits', 'csv' and 'tab' files
@@ -2542,13 +2552,14 @@ def append_table(self, table_file, name=None, chunksize=None):
2542
2552
table_file : Filename to be uploaded as table (.csv, .fits, .tab)
2543
2553
name : Name of the table to be created
2544
2554
chunksize : Number of rows to upload at a time to avoid memory issues
2555
+ memsize : Size of chunk to be read. In Mb. If both specified, the lower number of rows is selected
2545
2556
2546
2557
Returns:
2547
2558
--------
2548
2559
True if success otherwise False
2549
2560
"""
2550
2561
try :
2551
- self .do_append_table (table_file , name = name , chunksize = chunksize )
2562
+ self .do_append_table (table_file , name = name , chunksize = chunksize , memsize = memsize )
2552
2563
return True
2553
2564
except :
2554
2565
return False
@@ -2664,7 +2675,10 @@ def initial_message(quiet=False, clear=True):
2664
2675
or --append_table" )
2665
2676
parser .add_argument ("--chunksize" , dest = 'chunksize' , type = int , default = None ,
2666
2677
help = "Number of rows to be inserted at a time. Useful for large files \
2667
- that do not fit in memory. Use with --load_table" )
2678
+ that do not fit in memory. Use with --load_table or --append_table" )
2679
+ parser .add_argument ("--memsize" , dest = 'memsize' , type = int , default = None ,
2680
+ help = " Size of chunk to be read at a time in Mb. Use with --load_table or "
2681
+ "--append_table" )
2668
2682
parser .add_argument ("-s" , "--db" ,dest = 'db' , #choices=[...]?
2669
2683
help = "Override database name [dessci,desoper,destest]" )
2670
2684
parser .add_argument ("-q" , "--quiet" , action = "store_true" , dest = 'quiet' ,
@@ -2785,6 +2799,8 @@ def colored(line, color): return line
2785
2799
linein += ' --tablename ' + args .tablename
2786
2800
if args .chunksize is not None :
2787
2801
linein += ' --chunksize ' + str (args .chunksize )
2802
+ if args .memsize is not None :
2803
+ linein += ' --memsize ' + str (args .memsize )
2788
2804
cmdinterp .onecmd (linein )
2789
2805
os ._exit (0 )
2790
2806
elif args .appendtable is not None :
@@ -2795,6 +2811,8 @@ def colored(line, color): return line
2795
2811
linein += ' --tablename ' + args .tablename
2796
2812
if args .chunksize is not None :
2797
2813
linein += ' --chunksize ' + str (args .chunksize )
2814
+ if args .memsize is not None :
2815
+ linein += ' --memsize ' + str (args .memsize )
2798
2816
cmdinterp .onecmd (linein )
2799
2817
os ._exit (0 )
2800
2818
else :
0 commit comments