Skip to content

Retrieve all table names from existing SQLite file. - #72

Merged
menshikh-iv merged 14 commits into
piskvorky:masterfrom
transfluxus:tablenames
Jan 26, 2018
Merged

Retrieve all table names from existing SQLite file.#72
menshikh-iv merged 14 commits into
piskvorky:masterfrom
transfluxus:tablenames

Conversation

@transfluxus

@transfluxus transfluxus commented Jan 18, 2018

Copy link
Copy Markdown
Contributor

Add get_tablenames utility method that retrieves all table names from the file with SQLite database.

FIxes #71.

Comment thread sqlitedict.py Outdated
def print_tablenames(filename):
"""print tablenames and return them as list. Returns empty list if file does not exist (instead of creating that file)"""
if not os.path.isfile(filename):
logger.warning('file %s does not exist' %filename)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explicit fail (exception) better?

Also, spaces around %.

Comment thread sqlitedict.py Outdated
if not os.path.isfile(filename):
logger.warning('file %s does not exist' %filename)
return
raise IOError('file %s does not exist' % (filename))

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does sqlite3 do when asked to connect to a file that doesn't exist? Isn't that "default" error better?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's weird. It creates a database of that name and returns an empty list

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. That's really bad behaviour from sqlite3 -- I agree raising an exception is much better.

@ownport

ownport commented Jan 18, 2018

Copy link
Copy Markdown
Contributor

As proposal, instead of printing tables maybe it will be better to return the list of them and user will decide how to use this list?

@menshikh-iv menshikh-iv left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good start, don't forget about tests (need to add tests for new functionality)

Comment thread sqlitedict.py Outdated
conn = sqlite3.connect(filename)
res = conn.execute("SELECT name FROM sqlite_master WHERE type='table';")
tablenames = [name[0] for name in res]
print(tablenames)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return tablenames enough (no need to print it)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

k

Comment thread sqlitedict.py Outdated
return loads(bytes(obj))


def print_tablenames(filename):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO better to move it to SqliteDict class and use as method, wdyt @transfluxus ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

possible. but that would create already at least one table (unnamed). could be a static method. The way use it is to cal it before creating the SqliteDict obj.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But what for? I don't see a problem here.

@transfluxus

Copy link
Copy Markdown
Contributor Author

instead of this:

db = SqliteDict('testDB.sqlite')
db = SqliteDict('testDB.sqlite',tablename='vocab')
db.get_tablenames()

I would rather have something like this:

dbs = {tablename : SqliteDict('testDB.sqlite',tablename) for tablename in get_tablenames('testDB.sqlite')}
@transfluxus

Copy link
Copy Markdown
Contributor Author

but just let me know, what you prefer. it includes both versions now

Comment thread sqlitedict.py Outdated

def get_tablenames(self):
"""print tablenames and return them as list"""
res = self.conn.select("SELECT name FROM sqlite_master WHERE type='table';")

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-1 on code duplication, it makes maintenance a headache. Keep it DRY.

Can you refactor the core functionality to live in only one place? Possibly being used/called from multiple places, that's fine.

@transfluxus

Copy link
Copy Markdown
Contributor Author

Of course. That's what I came up with in the end, since the db connection/cursor are in the run of the multithread obj.

I'm not used to write test :), but I added some. Hope they make sense

@@ -0,0 +1,6 @@
{

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated file, please remove

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aii. sneaked in...

Comment thread tests/test_core.py Outdated
self.assertEqual(db1.get_tablenames(), ['table1'])
db2 = sqlitedict.SqliteDict(fname,tablename='table2')
self.assertEqual(db1.get_tablenames(), ['table1','table2'])
db1.close()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with SliteDict(...): 
    self.assertEqual 

better than db = ; db.close()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will do that

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a reason you're not having
from sqlitedict import SliteDict ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, I did, makes things look cleaner.
having with makes things look better indeed. However my test is now the only one doing that. looks a bit inconsistent...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@transfluxus I think we'll cleanup codebase later, don't worry :)

Comment thread tests/test_core.py Outdated

def test_tablenames(self):
fname = norm_file('tests/db/tablenames-test-1.sqlite')
db = sqlitedict.SqliteDict(fname)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's about an empty file?

@transfluxus transfluxus Jan 22, 2018

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure what you mean. in any case table unnamed will be created

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@transfluxus aha, OK!

Comment thread sqlitedict.py Outdated
return loads(bytes(obj))


def get_tablenames(filename):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably better to remove this function & move all code to method, wdyt?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you mean just a class method?
I can do that. just makes use case look unpractical. see above

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@transfluxus what's about static method?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

Ramin added 2 commits January 22, 2018 19:50
Comment thread sqlitedict.py Outdated
yield rec

def select_one(self, req, arg=None):
def select_one(self, req, arg=None):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

leading space (PEP8)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is out

Comment thread tests/test_temp_db.py
from sys import version_info
major_version = version_info[0]

from accessories import norm_file

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused import

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is gone

Comment thread sqlitedict.py Outdated
"""get the names of the tables in an sqlite db as a list"""
if not os.path.isfile(filename):
raise IOError('file %s does not exist' % (filename))
conn = sqlite3.connect(filename)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's about with here too?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gotit

change test imports
@menshikh-iv menshikh-iv changed the title add print_tablenames method Jan 26, 2018
@menshikh-iv

Copy link
Copy Markdown
Contributor

Thank you @transfluxus! Congratz with first contribution 🥇

@menshikh-iv
menshikh-iv merged commit 289c022 into piskvorky:master Jan 26, 2018
@piskvorky piskvorky changed the title Add get_tablenames method. Fix #71 Sep 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

4 participants