Skip to content

Commit d2fa61b

Browse files
author
Release Manager
committed
gh-36376: src/sage/databases/sql_db.py: replace tmp_dir() Standard `tempfile.TemporaryDirectory()` replacement. URL: #36376 Reported by: Michael Orlitzky Reviewer(s): David Coudert
2 parents aadad8b + 17285d9 commit d2fa61b

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

src/sage/databases/sql_db.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,16 +1026,19 @@ def __init__(self, filename=None, read_only=None, skeleton=None):
10261026
NOTE: The values of ``display_cols`` are always concatenated in
10271027
intersections and unions.
10281028
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::
10301031
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)
10331036
10341037
Now the database's hard link is to this file, and not the temporary db
10351038
file. For example, let's say we open the same file with another class
10361039
instance. We can load the file as an immutable database::
10371040
1038-
sage: E = SQLDatabase(replace_with_your_own_filepath + 'simon.db')
1041+
sage: E = SQLDatabase(dbpath)
10391042
sage: E.show('simon')
10401043
graph6 vertices edges
10411044
------------------------------------------------------------
@@ -1062,6 +1065,11 @@ def __init__(self, filename=None, read_only=None, skeleton=None):
10621065
Traceback (most recent call last):
10631066
...
10641067
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+
10651073
"""
10661074
if filename is None:
10671075
if read_only is None:
@@ -1111,10 +1119,12 @@ def __repr__(self):
11111119
11121120
EXAMPLES::
11131121
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)
11181128
table simon:
11191129
column n: index: True; primary_key: False; sql: INTEGER;
11201130
unique: False;
@@ -1182,10 +1192,12 @@ def save(self, filename):
11821192
sage: MonicPolys = SQLDatabase()
11831193
sage: MonicPolys.create_table('simon', {'n':{'sql':'INTEGER', 'index':True}})
11841194
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')
11891201
n
11901202
--------------------
11911203
0

0 commit comments

Comments
 (0)