Skip to content

Commit 37fba4f

Browse files
author
Kareem Zidane
committed
remove underscore from util functions
1 parent c0534e2 commit 37fba4f

File tree

4 files changed

+46
-46
lines changed

4 files changed

+46
-46
lines changed

src/cs50/_session.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@
44
import sqlalchemy.orm
55

66
from ._session_util import (
7-
_is_sqlite_url,
8-
_assert_sqlite_file_exists,
9-
_create_session,
7+
is_sqlite_url,
8+
assert_sqlite_file_exists,
9+
create_session,
1010
)
1111

1212

1313
class Session:
1414
"""Wraps a SQLAlchemy scoped session"""
1515

1616
def __init__(self, url, **engine_kwargs):
17-
if _is_sqlite_url(url):
18-
_assert_sqlite_file_exists(url)
17+
if is_sqlite_url(url):
18+
assert_sqlite_file_exists(url)
1919

20-
self._session = _create_session(url, **engine_kwargs)
20+
self._session = create_session(url, **engine_kwargs)
2121

2222
def execute(self, statement):
2323
"""Converts statement to str and executes it"""

src/cs50/_session_util.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@
55

66
import sqlalchemy
77

8-
def _is_sqlite_url(url):
8+
def is_sqlite_url(url):
99
return url.startswith("sqlite:///")
1010

1111

12-
def _assert_sqlite_file_exists(url):
12+
def assert_sqlite_file_exists(url):
1313
path = url[len("sqlite:///"):]
1414
if not os.path.exists(path):
1515
raise RuntimeError(f"does not exist: {path}")
1616
if not os.path.isfile(path):
1717
raise RuntimeError(f"not a file: {path}")
1818

1919

20-
def _create_session(url, **engine_kwargs):
20+
def create_session(url, **engine_kwargs):
2121
engine = _create_engine(url, **engine_kwargs)
2222
_setup_on_connect(engine)
2323
return _create_scoped_session(engine)

src/cs50/_statement.py

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44

55
from ._sql_sanitizer import SQLSanitizer, escape_verbatim_colon
66
from ._statement_util import (
7-
_format_and_parse,
8-
_get_human_readable_list,
9-
_is_identifier,
10-
_is_operation_token,
11-
_is_placeholder,
12-
_is_string_literal,
13-
_Paramstyle,
14-
_parse_placeholder,
7+
format_and_parse,
8+
get_human_readable_list,
9+
is_identifier,
10+
is_operation_token,
11+
is_placeholder,
12+
is_string_literal,
13+
Paramstyle,
14+
parse_placeholder,
1515
)
1616

1717

@@ -27,7 +27,7 @@ def __init__(self, dialect, sql, *args, **kwargs):
2727
self._args = self._get_escaped_args(args)
2828
self._kwargs = self._get_escaped_kwargs(kwargs)
2929

30-
self._statement = _format_and_parse(sql)
30+
self._statement = format_and_parse(sql)
3131
self._tokens = self._tokenize()
3232

3333
self._operation_keyword = self._get_operation_keyword()
@@ -48,7 +48,7 @@ def _tokenize(self):
4848

4949
def _get_operation_keyword(self):
5050
for token in self._statement:
51-
if _is_operation_token(token.ttype):
51+
if is_operation_token(token.ttype):
5252
token_value = token.value.upper()
5353
if token_value in {"BEGIN", "DELETE", "INSERT", "SELECT", "START", "UPDATE"}:
5454
operation_keyword = token_value
@@ -61,8 +61,8 @@ def _get_operation_keyword(self):
6161
def _get_paramstyle(self):
6262
paramstyle = None
6363
for token in self._tokens:
64-
if _is_placeholder(token.ttype):
65-
paramstyle, _ = _parse_placeholder(token.value)
64+
if is_placeholder(token.ttype):
65+
paramstyle, _ = parse_placeholder(token.value)
6666
break
6767
else:
6868
paramstyle = self._default_paramstyle()
@@ -72,17 +72,17 @@ def _get_paramstyle(self):
7272
def _default_paramstyle(self):
7373
paramstyle = None
7474
if self._args:
75-
paramstyle = _Paramstyle.QMARK
75+
paramstyle = Paramstyle.QMARK
7676
elif self._kwargs:
77-
paramstyle = _Paramstyle.NAMED
77+
paramstyle = Paramstyle.NAMED
7878

7979
return paramstyle
8080

8181
def _get_placeholders(self):
8282
placeholders = collections.OrderedDict()
8383
for index, token in enumerate(self._tokens):
84-
if _is_placeholder(token.ttype):
85-
paramstyle, name = _parse_placeholder(token.value)
84+
if is_placeholder(token.ttype):
85+
paramstyle, name = parse_placeholder(token.value)
8686
if paramstyle != self._paramstyle:
8787
raise RuntimeError("inconsistent paramstyle")
8888

@@ -91,11 +91,11 @@ def _get_placeholders(self):
9191
return placeholders
9292

9393
def _plugin_escaped_params(self):
94-
if self._paramstyle in {_Paramstyle.FORMAT, _Paramstyle.QMARK}:
94+
if self._paramstyle in {Paramstyle.FORMAT, Paramstyle.QMARK}:
9595
self._plugin_format_or_qmark_params()
96-
elif self._paramstyle == _Paramstyle.NUMERIC:
96+
elif self._paramstyle == Paramstyle.NUMERIC:
9797
self._plugin_numeric_params()
98-
if self._paramstyle in {_Paramstyle.NAMED, _Paramstyle.PYFORMAT}:
98+
if self._paramstyle in {Paramstyle.NAMED, Paramstyle.PYFORMAT}:
9999
self._plugin_named_or_pyformat_params()
100100

101101
def _plugin_format_or_qmark_params(self):
@@ -105,8 +105,8 @@ def _plugin_format_or_qmark_params(self):
105105

106106
def _assert_valid_arg_count(self):
107107
if len(self._placeholders) != len(self._args):
108-
placeholders = _get_human_readable_list(self._placeholders.values())
109-
args = _get_human_readable_list(self._args)
108+
placeholders = get_human_readable_list(self._placeholders.values())
109+
args = get_human_readable_list(self._args)
110110
if len(self._placeholders) < len(self._args):
111111
raise RuntimeError(f"fewer placeholders ({placeholders}) than values ({args})")
112112

@@ -122,7 +122,7 @@ def _plugin_numeric_params(self):
122122
unused_arg_indices.remove(num)
123123

124124
if len(unused_arg_indices) > 0:
125-
unused_args = _get_human_readable_list(
125+
unused_args = get_human_readable_list(
126126
[self._args[i] for i in sorted(unused_arg_indices)])
127127
raise RuntimeError(
128128
f"unused value{'' if len(unused_args) == 1 else 's'} ({unused_args})")
@@ -137,13 +137,13 @@ def _plugin_named_or_pyformat_params(self):
137137
unused_params.remove(param_name)
138138

139139
if len(unused_params) > 0:
140-
joined_unused_params = _get_human_readable_list(sorted(unused_params))
140+
joined_unused_params = get_human_readable_list(sorted(unused_params))
141141
raise RuntimeError(
142142
f"unused value{'' if len(unused_params) == 1 else 's'} ({joined_unused_params})")
143143

144144
def _escape_verbatim_colons(self):
145145
for token in self._tokens:
146-
if _is_string_literal(token.ttype) or _is_identifier(token.ttype):
146+
if is_string_literal(token.ttype) or is_identifier(token.ttype):
147147
token.value = escape_verbatim_colon(token.value)
148148

149149
def get_operation_keyword(self):

src/cs50/_statement_util.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
import sqlparse
77

88

9-
class _Paramstyle(enum.Enum):
9+
class Paramstyle(enum.Enum):
1010
FORMAT = enum.auto()
1111
NAMED = enum.auto()
1212
NUMERIC = enum.auto()
1313
PYFORMAT = enum.auto()
1414
QMARK = enum.auto()
1515

1616

17-
def _format_and_parse(sql):
17+
def format_and_parse(sql):
1818
formatted_statements = sqlparse.format(sql, strip_comments=True).strip()
1919
parsed_statements = sqlparse.parse(formatted_statements)
2020
statement_count = len(parsed_statements)
@@ -26,47 +26,47 @@ def _format_and_parse(sql):
2626
return parsed_statements[0]
2727

2828

29-
def _is_placeholder(ttype):
29+
def is_placeholder(ttype):
3030
return ttype == sqlparse.tokens.Name.Placeholder
3131

3232

33-
def _parse_placeholder(value):
33+
def parse_placeholder(value):
3434
if value == "?":
35-
return _Paramstyle.QMARK, None
35+
return Paramstyle.QMARK, None
3636

3737
# E.g., :1
3838
matches = re.search(r"^:([1-9]\d*)$", value)
3939
if matches:
40-
return _Paramstyle.NUMERIC, int(matches.group(1)) - 1
40+
return Paramstyle.NUMERIC, int(matches.group(1)) - 1
4141

4242
# E.g., :foo
4343
matches = re.search(r"^:([a-zA-Z]\w*)$", value)
4444
if matches:
45-
return _Paramstyle.NAMED, matches.group(1)
45+
return Paramstyle.NAMED, matches.group(1)
4646

4747
if value == "%s":
48-
return _Paramstyle.FORMAT, None
48+
return Paramstyle.FORMAT, None
4949

5050
# E.g., %(foo)s
5151
matches = re.search(r"%\((\w+)\)s$", value)
5252
if matches:
53-
return _Paramstyle.PYFORMAT, matches.group(1)
53+
return Paramstyle.PYFORMAT, matches.group(1)
5454

5555
raise RuntimeError(f"{value}: invalid placeholder")
5656

5757

58-
def _is_operation_token(ttype):
58+
def is_operation_token(ttype):
5959
return ttype in {
6060
sqlparse.tokens.Keyword, sqlparse.tokens.Keyword.DDL, sqlparse.tokens.Keyword.DML}
6161

6262

63-
def _is_string_literal(ttype):
63+
def is_string_literal(ttype):
6464
return ttype in [sqlparse.tokens.Literal.String, sqlparse.tokens.Literal.String.Single]
6565

6666

67-
def _is_identifier(ttype):
67+
def is_identifier(ttype):
6868
return ttype == sqlparse.tokens.Literal.String.Symbol
6969

7070

71-
def _get_human_readable_list(iterable):
71+
def get_human_readable_list(iterable):
7272
return ", ".join(str(v) for v in iterable)

0 commit comments

Comments
 (0)