Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Release Manager committed Oct 4, 2023
2 parents aadad8b + 17285d9 commit d2fa61b
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions src/sage/databases/sql_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -1026,16 +1026,19 @@ def __init__(self, filename=None, read_only=None, skeleton=None):
NOTE: The values of ``display_cols`` are always concatenated in
intersections and unions.
Of course, we can save the database to file::
Of course, we can save the database to file. Here we use a
temporary directory that we clean up afterwards::
sage: replace_with_your_own_filepath = tmp_dir()
sage: D.save(replace_with_your_own_filepath + 'simon.db')
sage: import tempfile
sage: d = tempfile.TemporaryDirectory()
sage: dbpath = os.path.join(d.name, 'simon.db')
sage: D.save(dbpath)
Now the database's hard link is to this file, and not the temporary db
file. For example, let's say we open the same file with another class
instance. We can load the file as an immutable database::
sage: E = SQLDatabase(replace_with_your_own_filepath + 'simon.db')
sage: E = SQLDatabase(dbpath)
sage: E.show('simon')
graph6 vertices edges
------------------------------------------------------------
Expand All @@ -1062,6 +1065,11 @@ def __init__(self, filename=None, read_only=None, skeleton=None):
Traceback (most recent call last):
...
RuntimeError: Cannot drop tables from a read only database.
Call ``cleanup()`` on the temporary directory to, well, clean it up::
sage: d.cleanup()
"""
if filename is None:
if read_only is None:
Expand Down Expand Up @@ -1111,10 +1119,12 @@ def __repr__(self):
EXAMPLES::
sage: replace_with_filepath = tmp_dir() + 'test.db'
sage: SD = SQLDatabase(replace_with_filepath, False)
sage: SD.create_table('simon', {'n':{'sql':'INTEGER', 'index':True}})
sage: print(SD)
sage: import tempfile
sage: with tempfile.TemporaryDirectory() as d:
....: dbpath = os.path.join(d, "test.db")
....: SD = SQLDatabase(dbpath, False)
....: SD.create_table('simon', {'n':{'sql':'INTEGER', 'index':True}})
....: print(SD)
table simon:
column n: index: True; primary_key: False; sql: INTEGER;
unique: False;
Expand Down Expand Up @@ -1182,10 +1192,12 @@ def save(self, filename):
sage: MonicPolys = SQLDatabase()
sage: MonicPolys.create_table('simon', {'n':{'sql':'INTEGER', 'index':True}})
sage: for n in range(20): MonicPolys.add_row('simon', (n,))
sage: tmp = tmp_dir() # replace with your own file path
sage: MonicPolys.save(tmp+'sage.db')
sage: N = SQLDatabase(tmp+'sage.db')
sage: N.show('simon')
sage: import tempfile
sage: with tempfile.TemporaryDirectory() as d:
....: dbpath = os.path.join(d, "sage.db")
....: MonicPolys.save(dbpath)
....: N = SQLDatabase(dbpath)
....: N.show('simon')
n
--------------------
0
Expand Down

0 comments on commit d2fa61b

Please sign in to comment.