Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src/sage/databases/sql_db.py: replace tmp_dir() #36376

Merged
merged 1 commit into from
Oct 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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