Skip to content

Commit 025ef72

Browse files
author
F Saad
committed
Migrate cgpm_analyze and cgpm_schema into their own subdirectories.
Adds __init__.py files in the two new packages. Updates setup.py to include the two packages, and grammar files. Updates cgpm_metamodel to correctly import cgpm_schema.parse Updates schema_schema.parse and cgpm_analyze.parse to reference grammar.py
1 parent f616973 commit 025ef72

File tree

7 files changed

+63
-71
lines changed

7 files changed

+63
-71
lines changed

setup.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,8 @@ def run_tests(self):
186186
lemonade = 'external/lemonade/dist'
187187
grammars = [
188188
'src/grammar.y',
189-
'src/metamodels/cgpm_grammar.y',
189+
'src/metamodels/cgpm_analyze/grammar.y',
190+
'src/metamodels/cgpm_schema/grammar.y',
190191
]
191192

192193
setup(
@@ -213,6 +214,8 @@ def run_tests(self):
213214
packages=[
214215
'bayeslite',
215216
'bayeslite.metamodels',
217+
'bayeslite.metamodels.cgpm_schema',
218+
'bayeslite.metamodels.cgpm_analyze',
216219
'bayeslite.plex',
217220
'bayeslite.shell',
218221
'bayeslite.weakprng',
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# Copyright (c) 2010-2016, MIT Probabilistic Computing Project
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.

src/metamodels/cgpm_analyze/parse.py

Lines changed: 10 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,29 @@
1919
from bayeslite.exception import BQLParseError
2020
from bayeslite.util import casefold
2121

22-
import analyze_grammar
22+
import grammar
2323

2424
'''
25-
grep -o 'K_[A-Z][A-Z0-9_]*' < analyze_grammar.y | sort -u | awk '
25+
grep -o 'K_[A-Z][A-Z0-9_]*' < grammar.y | sort -u | awk '
2626
{
2727
sub("^K_", "", $1);
28-
printf(" '\''%s'\'': analyze_grammar.K_%s,\n", tolower($1), $1);
28+
printf(" '\''%s'\'': grammar.K_%s,\n", tolower($1), $1);
2929
}'
3030
'''
3131

3232
KEYWORDS = {
33-
'skip': analyze_grammar.K_SKIP,
34-
'variables': analyze_grammar.K_VARIABLES,
33+
'skip': grammar.K_SKIP,
34+
'variables': grammar.K_VARIABLES,
3535
}
3636

3737
PUNCTUATION = {
38-
',': analyze_grammar.T_COMMA,
39-
';': analyze_grammar.T_SEMI,
38+
',': grammar.T_COMMA,
39+
';': grammar.T_SEMI,
4040
}
4141

4242
def parse(tokens):
4343
semantics = CGpmAnalyzeSemantics()
44-
parser = analyze_grammar.Parser(semantics)
44+
parser = grammar.Parser(semantics)
4545
for token in tokenize(tokens):
4646
semantics.context.append(token)
4747
if len(semantics.context) > 10:
@@ -61,9 +61,9 @@ def tokenize(tokens):
6161
elif token in PUNCTUATION:
6262
yield PUNCTUATION[token], token
6363
else: # XXX check for alphanumeric/_
64-
yield analyze_grammar.L_NAME, token
64+
yield grammar.L_NAME, token
6565
elif isinstance(token, (int, float)):
66-
yield analyze_grammar.L_NUMBER, token
66+
yield grammar.L_NUMBER, token
6767
else:
6868
raise IOError('Invalid token: %r' % (token,))
6969
yield 0, '' # EOF
@@ -108,44 +108,3 @@ def p_column_name_n(self, name): return name
108108
Skip = namedtuple('Skip', [
109109
'vars',
110110
])
111-
112-
if __name__ == '__main__':
113-
tokens = [
114-
'SKIPS', 'a', ',', 'b', ';', # XXX Why does this not raise?
115-
'VARIABLES', 'a', ',', 'b', ';',
116-
'SKIP', 'a', ';'
117-
]
118-
print parse(tokens)
119-
# [None, Variables(vars=['a', 'b']), Skip(vars=['a'])]
120-
121-
tokens = [
122-
'VARIABLES', 'a', ',', 'b', ';',
123-
'SKIP', 'a', ';',
124-
'SKIPS', 'a', ',', 'b', ';'
125-
]
126-
print parse(tokens)
127-
# [Variables(vars=['a', 'b']), Skip(vars=['a'])]
128-
129-
tokens = [
130-
'VARIABLES', 'a', ',', 'b', ';',
131-
'SKIP', 'a', ',', ';',
132-
'SKIPS', 'a', ',', 'b', ';'
133-
]
134-
print parse(tokens)
135-
# [Variables(vars=['a', 'b']), Skip(vars=['a', 'SKIPS'])]
136-
137-
tokens = [
138-
'VARIABLES', 'a', ',', 'b', ';',
139-
'SKIP', 'a', ',', ';',
140-
'SKIPS', ',', 'a', ',', 'b', ';'
141-
]
142-
print parse(tokens)
143-
# [Variables(vars=['a', 'b']), Skip(vars=['a', 'SKIPS', 'a', 'b'])]
144-
145-
tokens = [
146-
'VARIABLES', 'a', ',', 'b', ';',
147-
'SKIP', 'a', ',', ';',
148-
'SKIP', 'a', ',', 'b', ';'
149-
]
150-
print parse(tokens)
151-
# [Variables(vars=['a', 'b']), Skip(vars=['a', 'a', 'b'])]

src/metamodels/cgpm_metamodel.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
from bayeslite.stats import arithmetic_mean
6666
from bayeslite.util import casefold
6767

68-
import cgpm_parse
68+
import cgpm_schema.parse
6969

7070
CGPM_SCHEMA_1 = '''
7171
INSERT INTO bayesdb_metamodel (name, version) VALUES ('cgpm', 1);
@@ -119,7 +119,7 @@ def register(self, bdb):
119119
' with unknown schema version: %d' % (version,))
120120

121121
def create_generator(self, bdb, generator_id, schema_tokens):
122-
schema_ast = cgpm_parse.parse(schema_tokens)
122+
schema_ast = cgpm_schema.parse.parse(schema_tokens)
123123
schema = _create_schema(bdb, generator_id, schema_ast)
124124

125125
# Store the schema.
@@ -636,7 +636,7 @@ def _retrieve_stattype_dist_params(var):
636636
# Process each clause one by one.
637637
for clause in schema_ast:
638638

639-
if isinstance(clause, cgpm_parse.Basic):
639+
if isinstance(clause, cgpm_schema.parse.Basic):
640640
# Basic Crosscat component model: one variable to be put
641641
# into Crosscat views.
642642
var = clause.var
@@ -665,7 +665,7 @@ def _retrieve_stattype_dist_params(var):
665665
variables.append([var, stattype, dist, params])
666666
modelled.add(var)
667667

668-
elif isinstance(clause, cgpm_parse.Foreign):
668+
elif isinstance(clause, cgpm_schema.parse.Foreign):
669669
# Foreign model: some set of output variables is to be
670670
# modelled by foreign logic, possibly conditional on some
671671
# set of input variables.
@@ -717,7 +717,7 @@ def _retrieve_stattype_dist_params(var):
717717
'kwds': kwds,
718718
})
719719

720-
elif isinstance(clause, cgpm_parse.Subsample):
720+
elif isinstance(clause, cgpm_schema.parse.Subsample):
721721
if subsample is not None:
722722
raise BQLError(bdb, 'Duplicate subsample: %r' % (clause.n,))
723723
subsample = clause.n
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# Copyright (c) 2010-2016, MIT Probabilistic Computing Project
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
File renamed without changes.

src/metamodels/cgpm_parse.py renamed to src/metamodels/cgpm_schema/parse.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,33 +19,33 @@
1919
from bayeslite.exception import BQLParseError
2020
from bayeslite.util import casefold
2121

22-
import cgpm_grammar
22+
import grammar
2323

2424
'''
25-
grep -o 'K_[A-Z][A-Z0-9_]*' < cgpm_grammar.y | sort -u | awk '
25+
grep -o 'K_[A-Z][A-Z0-9_]*' < grammar.y | sort -u | awk '
2626
{
2727
sub("^K_", "", $1);
28-
printf(" '\''%s'\'': cgpm_grammar.K_%s,\n", tolower($1), $1);
28+
printf(" '\''%s'\'': grammar.K_%s,\n", tolower($1), $1);
2929
}'
3030
'''
3131

3232
KEYWORDS = {
33-
'given': cgpm_grammar.K_GIVEN,
34-
'model': cgpm_grammar.K_MODEL,
35-
'subsample': cgpm_grammar.K_SUBSAMPLE,
36-
'using': cgpm_grammar.K_USING,
33+
'given': grammar.K_GIVEN,
34+
'model': grammar.K_MODEL,
35+
'subsample': grammar.K_SUBSAMPLE,
36+
'using': grammar.K_USING,
3737
}
3838

3939
PUNCTUATION = {
40-
'(': cgpm_grammar.T_LROUND,
41-
')': cgpm_grammar.T_RROUND,
42-
',': cgpm_grammar.T_COMMA,
43-
'=': cgpm_grammar.T_EQ,
40+
'(': grammar.T_LROUND,
41+
')': grammar.T_RROUND,
42+
',': grammar.T_COMMA,
43+
'=': grammar.T_EQ,
4444
}
4545

4646
def parse(tokenses):
4747
semantics = CGPM_Semantics()
48-
parser = cgpm_grammar.Parser(semantics)
48+
parser = grammar.Parser(semantics)
4949
for token in tokenize(tokenses):
5050
semantics.context.append(token)
5151
if len(semantics.context) > 10:
@@ -64,9 +64,9 @@ def tokenize(tokenses):
6464
elif token in PUNCTUATION:
6565
yield PUNCTUATION[token], token
6666
else: # XXX check for alphanumeric/_
67-
yield cgpm_grammar.L_NAME, token
67+
yield grammar.L_NAME, token
6868
elif isinstance(token, (int, float)):
69-
yield cgpm_grammar.L_NUMBER, token
69+
yield grammar.L_NUMBER, token
7070
else:
7171
raise IOError('Invalid token: %r' % (token,))
7272
yield 0, '' # EOF

0 commit comments

Comments
 (0)