26
26
27
27
28
28
import os
29
- import csv
30
29
from enum import Enum
31
30
32
31
from sage .structure .sage_object import SageObject
@@ -227,6 +226,20 @@ def csv(self):
227
226
"""
228
227
return '%s.csv' % (self .value [1 ])
229
228
229
+ def num_knots (self ):
230
+ r"""
231
+ Return the file name under which the number of knots is stored
232
+ in an sobj-file.
233
+
234
+ Examples::
235
+
236
+ sage: from sage.databases.knotinfo_db import KnotInfoDataBase
237
+ sage: ki_db = KnotInfoDataBase()
238
+ sage: ki_db.filename.knots.num_knots()
239
+ 'num_knots.sobj'
240
+ """
241
+ return 'num_knots.sobj'
242
+
230
243
def sobj_row (self ):
231
244
r"""
232
245
Return the file name under which the row-data of the csv-File
@@ -347,10 +360,9 @@ def __init__(self, install=False):
347
360
'linkinfo_data_complete']>
348
361
"""
349
362
# some constants
350
- self ._delimiter = '|'
351
- self ._names_column = 'name'
363
+ self ._names_column = 'name'
352
364
self ._components_column = 'components'
353
- self ._knot_prefix = 'K'
365
+ self ._knot_prefix = 'K'
354
366
355
367
self ._knot_list = None
356
368
self ._link_list = None
@@ -359,16 +371,34 @@ def __init__(self, install=False):
359
371
360
372
from sage .features .databases import DatabaseKnotInfo
361
373
self ._feature = DatabaseKnotInfo ()
362
- self ._sobj_path = self ._feature .search_path [0 ]
374
+ self ._sobj_path = self ._feature ._sobj_path
375
+
376
+ def reset_filecache (self ):
377
+ r"""
378
+ Reset the internal files containing the database.
379
+
380
+ EXAMPLES::
381
+
382
+ sage: from sage.databases.knotinfo_db import KnotInfoDataBase
383
+ sage: ki_db = KnotInfoDataBase()
384
+ sage: ki_db.reset_filecache() # optional - database_knotinfo
385
+ """
386
+ if not self ._feature .is_present ():
387
+ return
388
+ sobj_path = self ._sobj_path
389
+ os .system ('rm -rf %s' % sobj_path )
390
+ from sage .misc .misc import sage_makedirs
391
+ sage_makedirs (sobj_path )
392
+
393
+ num_knots_file = os .path .join (sobj_path , self .filename .knots .num_knots ())
394
+ knot_list = self .knot_list ()
395
+ num_knots = len (knot_list ) - 1
396
+ save (num_knots , num_knots_file )
397
+ self ._num_knots = num_knots
398
+ self ._create_col_dict_sobj ()
399
+ self ._create_data_sobj ()
400
+ return
363
401
364
- if install :
365
- knot_list = self .knot_list ()
366
- num_knots = len (knot_list ) - 1
367
- print ('Setting the number of Knots: %s!' % num_knots )
368
- save (num_knots , '%s/%s' % (self ._sobj_path , self ._feature .filename ))
369
- self ._feature ._cache_is_present = None # must be reset for package installation
370
- self ._create_col_dict_sobj ()
371
- self ._create_data_sobj ()
372
402
373
403
def demo_version (self ):
374
404
r"""
@@ -382,9 +412,14 @@ def demo_version(self):
382
412
sage: ki_db.demo_version() # optional - database_knotinfo
383
413
False
384
414
"""
385
- if not self ._demo :
415
+ if self ._demo is None :
386
416
if self ._feature .is_present ():
387
- self ._num_knots = load (self ._feature .absolute_path ())
417
+ num_knots_file = os .path .join (self ._sobj_path , self .filename .knots .num_knots ())
418
+ from builtins import FileNotFoundError
419
+ try :
420
+ self ._num_knots = load (num_knots_file )
421
+ except FileNotFoundError :
422
+ self .reset_filecache ()
388
423
self ._demo = False
389
424
else :
390
425
self ._demo = True
@@ -404,12 +439,8 @@ def knot_list(self):
404
439
if self ._knot_list :
405
440
return self ._knot_list
406
441
407
- print ('Importing KnotInfo database from SPKG!' )
408
- os .system ('pwd' )
409
- knot_csv = open ('src/%s' % self .filename .knots .csv ())
410
- knot_dict = csv .DictReader (knot_csv , delimiter = self ._delimiter )
411
- self ._knot_list = list (knot_dict )
412
- knot_csv .close ()
442
+ from database_knotinfo import link_list
443
+ self ._knot_list = link_list ()
413
444
return self ._knot_list
414
445
415
446
@@ -426,11 +457,8 @@ def link_list(self):
426
457
if self ._link_list :
427
458
return self ._link_list
428
459
429
- print ('Importing LinkInfo database from SPKG!' )
430
- link_csv = open ('src/%s' % self .filename .links .csv ())
431
- link_dict = csv .DictReader (link_csv , delimiter = self ._delimiter )
432
- self ._link_list = list (link_dict )
433
- link_csv .close ()
460
+ from database_knotinfo import link_list
461
+ self ._link_list = link_list (proper_links = True )
434
462
return self ._link_list
435
463
436
464
def _create_col_dict_sobj (self ):
@@ -786,6 +814,7 @@ def _test_database(self, **options):
786
814
db = KnotInfoDataBase ()
787
815
dc = db .columns ()
788
816
817
+
789
818
data_demo_sample = {
790
819
dc .name : ['0_1' , '3_1' , '4_1' , '5_1' , '5_2' , '6_1' , '6_2' , '6_3' , '7_1' , '7_2' ,
791
820
'L2a1{0}' , 'L2a1{1}' , 'L4a1{0}' , 'L4a1{1}' , 'L5a1{0}' , 'L5a1{1}' ,
0 commit comments