Skip to content

Commit 1f839bd

Browse files
committed
Improved docstrings and reordered method declarations
1 parent bc05495 commit 1f839bd

File tree

3 files changed

+56
-56
lines changed

3 files changed

+56
-56
lines changed

mysql/toolkit/components/advanced.py

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,40 @@ class Advanced:
77
def __init__(self):
88
pass
99

10-
def truncate_database(self):
11-
"""Drop all tables in a database."""
12-
# Get list of tables
13-
tables = self.tables if isinstance(self.tables, list) else [self.tables]
14-
if len(tables) > 0:
15-
# Join list of tables into comma separated string
16-
tables_str = ', '.join([wrap(table) for table in tables])
17-
self.execute('DROP TABLE ' + tables_str)
18-
self._printer('\t' + str(len(tables)), 'tables truncated')
19-
return tables
10+
# def create_table(self, table, data, headers=None):
11+
# """Generate and execute a create table query by parsing a 2D dataset"""
12+
# # TODO: Fix
13+
# # Set headers list
14+
# if not headers:
15+
# headers = data[0]
16+
#
17+
# # Create dictionary columns and data types from headers list
18+
# data_types = {header: None for header in headers}
19+
#
20+
# # Confirm that each row of the dataset is the same length
21+
# for row in data:
22+
# assert len(row) == len(headers)
23+
#
24+
# # Create list of columns
25+
# columns = [header + ' ' + data_type for header, data_type in data_types]
26+
# self._printer(columns)
27+
# statement = "create table " + table + " ("
28+
# self._printer(statement)
29+
30+
def drop_empty_tables(self):
31+
"""Drop all empty tables in a database."""
32+
# Count number of rows in each table
33+
counts = self.count_rows_all()
34+
drops = []
35+
36+
# Loop through each table key and validate that rows count is not 0
37+
for table, count in counts.items():
38+
if count < 1:
39+
# Drop table if it contains no rows
40+
self.drop(table)
41+
self._printer('Dropped table', table)
42+
drops.append(table)
43+
return drops
2044

2145
def insert_uniques(self, table, columns, values):
2246
"""
@@ -62,40 +86,16 @@ def update_many(self, table, columns, values, where_col, where_index):
6286
for row in values:
6387
self.update(table, columns, row, (where_col, row[where_index]))
6488

65-
# def create_table(self, table, data, headers=None):
66-
# """Generate and execute a create table query by parsing a 2D dataset"""
67-
# # TODO: Fix
68-
# # Set headers list
69-
# if not headers:
70-
# headers = data[0]
71-
#
72-
# # Create dictionary columns and data types from headers list
73-
# data_types = {header: None for header in headers}
74-
#
75-
# # Confirm that each row of the dataset is the same length
76-
# for row in data:
77-
# assert len(row) == len(headers)
78-
#
79-
# # Create list of columns
80-
# columns = [header + ' ' + data_type for header, data_type in data_types]
81-
# self._printer(columns)
82-
# statement = "create table " + table + " ("
83-
# self._printer(statement)
84-
85-
def drop_empty_tables(self):
86-
"""Drop all empty tables in a database."""
87-
# Count number of rows in each table
88-
counts = self.count_rows_all()
89-
drops = []
90-
91-
# Loop through each table key and validate that rows count is not 0
92-
for table, count in counts.items():
93-
if count < 1:
94-
# Drop table if it contains no rows
95-
self.drop(table)
96-
self._printer('Dropped table', table)
97-
drops.append(table)
98-
return drops
89+
def truncate_database(self):
90+
"""Drop all tables in a database."""
91+
# Get list of tables
92+
tables = self.tables if isinstance(self.tables, list) else [self.tables]
93+
if len(tables) > 0:
94+
# Join list of tables into comma separated string
95+
tables_str = ', '.join([wrap(table) for table in tables])
96+
self.execute('DROP TABLE ' + tables_str)
97+
self._printer('\t' + str(len(tables)), 'tables truncated')
98+
return tables
9999

100100
def execute_script(self, sql_script, commands=None):
101101
"""Wrapper method for ExecuteScript class."""

mysql/toolkit/components/core.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def __init__(self):
66
pass
77

88
def select(self, table, cols, _print=True):
9-
"""Query only certain columns from a table and every row."""
9+
"""Query every row and only certain columns from a table."""
1010
# Concatenate statement
1111
cols_str = join_cols(cols)
1212
statement = "SELECT " + cols_str + " FROM " + wrap(table)
@@ -38,7 +38,7 @@ def select_where(self, table, cols, where):
3838
self._fetch(statement)
3939

4040
def insert(self, table, columns, values):
41-
"""Insert a singular row into a table"""
41+
"""Insert a single row into a table."""
4242
# Concatenate statement
4343
cols, vals = get_col_val_str(columns)
4444
statement = "INSERT INTO " + wrap(table) + "(" + cols + ") " + "VALUES (" + vals + ")"
@@ -93,7 +93,7 @@ def truncate(self, table):
9393
self.execute(statement)
9494
self._printer('\tMySQL table ' + str(table) + ' successfully truncated')
9595

96-
def drop_table(self, table):
96+
def drop(self, table):
9797
"""Drop a table from a database."""
9898
self.execute('DROP TABLE ' + wrap(table))
9999
return table

mysql/toolkit/components/results.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,10 @@ def get_primary_key(self, table):
2929
if 'pri' in column[3].lower():
3030
return column[0]
3131

32-
def get_primary_key_values(self, table):
32+
def get_primary_key_vals(self, table):
3333
"""Retrieve a list of primary key values in a table"""
3434
return self.select(table, self.get_primary_key(table), _print=False)
3535

36-
def count_rows(self, table):
37-
"""Get the number of rows in a particular table"""
38-
return self.select(table, 'COUNT(*)', False)
39-
40-
def count_rows_all(self):
41-
"""Get the number of rows for every table in the database."""
42-
return {table: self.count_rows(table) for table in self.tables}
43-
4436
def get_schema(self, table, with_headers=False):
4537
"""Retrieve the database schema for a particular table."""
4638
f = self._fetch('desc ' + wrap(table))
@@ -49,3 +41,11 @@ def get_schema(self, table, with_headers=False):
4941
if with_headers:
5042
f.insert(0, ['Column', 'Type', 'Null', 'Key', 'Default', 'Extra'])
5143
return f
44+
45+
def count_rows(self, table):
46+
"""Get the number of rows in a particular table"""
47+
return self.select(table, 'COUNT(*)', False)
48+
49+
def count_rows_all(self):
50+
"""Get the number of rows for every table in the database."""
51+
return {table: self.count_rows(table) for table in self.tables}

0 commit comments

Comments
 (0)