@@ -1026,16 +1026,19 @@ def __init__(self, filename=None, read_only=None, skeleton=None):
1026
1026
NOTE: The values of ``display_cols`` are always concatenated in
1027
1027
intersections and unions.
1028
1028
1029
- Of course, we can save the database to file::
1029
+ Of course, we can save the database to file. Here we use a
1030
+ temporary directory that we clean up afterwards::
1030
1031
1031
- sage: replace_with_your_own_filepath = tmp_dir()
1032
- sage: D.save(replace_with_your_own_filepath + 'simon.db')
1032
+ sage: import tempfile
1033
+ sage: d = tempfile.TemporaryDirectory()
1034
+ sage: dbpath = os.path.join(d.name, 'simon.db')
1035
+ sage: D.save(dbpath)
1033
1036
1034
1037
Now the database's hard link is to this file, and not the temporary db
1035
1038
file. For example, let's say we open the same file with another class
1036
1039
instance. We can load the file as an immutable database::
1037
1040
1038
- sage: E = SQLDatabase(replace_with_your_own_filepath + 'simon.db' )
1041
+ sage: E = SQLDatabase(dbpath )
1039
1042
sage: E.show('simon')
1040
1043
graph6 vertices edges
1041
1044
------------------------------------------------------------
@@ -1062,6 +1065,11 @@ def __init__(self, filename=None, read_only=None, skeleton=None):
1062
1065
Traceback (most recent call last):
1063
1066
...
1064
1067
RuntimeError: Cannot drop tables from a read only database.
1068
+
1069
+ Call ``cleanup()`` on the temporary directory to, well, clean it up::
1070
+
1071
+ sage: d.cleanup()
1072
+
1065
1073
"""
1066
1074
if filename is None :
1067
1075
if read_only is None :
@@ -1111,10 +1119,12 @@ def __repr__(self):
1111
1119
1112
1120
EXAMPLES::
1113
1121
1114
- sage: replace_with_filepath = tmp_dir() + 'test.db'
1115
- sage: SD = SQLDatabase(replace_with_filepath, False)
1116
- sage: SD.create_table('simon', {'n':{'sql':'INTEGER', 'index':True}})
1117
- sage: print(SD)
1122
+ sage: import tempfile
1123
+ sage: with tempfile.TemporaryDirectory() as d:
1124
+ ....: dbpath = os.path.join(d, "test.db")
1125
+ ....: SD = SQLDatabase(dbpath, False)
1126
+ ....: SD.create_table('simon', {'n':{'sql':'INTEGER', 'index':True}})
1127
+ ....: print(SD)
1118
1128
table simon:
1119
1129
column n: index: True; primary_key: False; sql: INTEGER;
1120
1130
unique: False;
@@ -1182,10 +1192,12 @@ def save(self, filename):
1182
1192
sage: MonicPolys = SQLDatabase()
1183
1193
sage: MonicPolys.create_table('simon', {'n':{'sql':'INTEGER', 'index':True}})
1184
1194
sage: for n in range(20): MonicPolys.add_row('simon', (n,))
1185
- sage: tmp = tmp_dir() # replace with your own file path
1186
- sage: MonicPolys.save(tmp+'sage.db')
1187
- sage: N = SQLDatabase(tmp+'sage.db')
1188
- sage: N.show('simon')
1195
+ sage: import tempfile
1196
+ sage: with tempfile.TemporaryDirectory() as d:
1197
+ ....: dbpath = os.path.join(d, "sage.db")
1198
+ ....: MonicPolys.save(dbpath)
1199
+ ....: N = SQLDatabase(dbpath)
1200
+ ....: N.show('simon')
1189
1201
n
1190
1202
--------------------
1191
1203
0
0 commit comments