Skip to content

Commit 8a3fc22

Browse files
committed
test more text round trips
improve python/pandas rendering
1 parent c838a03 commit 8a3fc22

File tree

6 files changed

+68
-53
lines changed

6 files changed

+68
-53
lines changed

build/lib/data_algebra/expr_rep.py

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
21
from typing import Union
32
import collections
43

54
import data_algebra.util
65
import data_algebra.env
76

7+
88
# for some ideas in capturing expressions in Python see:
99
# scipy
1010
# pipe-like idea
@@ -18,7 +18,7 @@ class Term:
1818

1919
source_string: Union[str, None]
2020

21-
def __init__(self,):
21+
def __init__(self, ):
2222
self.source_string = None
2323

2424
# builders
@@ -687,7 +687,7 @@ def __init__(self, value):
687687
def replace_view(self, view):
688688
return self
689689

690-
def to_python(self, want_inline_parens=False):
690+
def to_python(self, *, want_inline_parens=False):
691691
return self.value.__repr__()
692692

693693

@@ -701,7 +701,7 @@ def __init__(self, value):
701701
def replace_view(self, view):
702702
return self
703703

704-
def to_python(self, want_inline_parens=False):
704+
def to_python(self, *, want_inline_parens=False):
705705
return self.value.__name__
706706

707707

@@ -713,10 +713,18 @@ def __init__(self, value):
713713
Term.__init__(self)
714714

715715
def replace_view(self, view):
716-
return self
716+
new_list = [ai.replace_view(view) for ai in self.value]
717+
return ListTerm(new_list)
717718

718-
def to_python(self, want_inline_parens=False):
719-
return self.value.__name__
719+
def to_python(self, *, want_inline_parens=False):
720+
return ('['
721+
+ ', '.join([ai.to_python(want_inline_parens=want_inline_parens) for ai in self.value])
722+
+ ']')
723+
724+
def to_pandas(self, *, want_inline_parens=False):
725+
return ('['
726+
+ ', '.join([ai.to_pandas(want_inline_parens=want_inline_parens) for ai in self.value])
727+
+ ']')
720728

721729
def get_column_names(self, columns_seen):
722730
for ti in self.value:
@@ -780,21 +788,21 @@ def connected_components(expr):
780788
"is_bad": lambda expr: "@is_bad(" + expr.args[0].to_pandas() + ")",
781789
"is_null": lambda expr: "@is_null(" + expr.args[0].to_pandas() + ")",
782790
"if_else": lambda expr: (
783-
"@if_else("
784-
+ expr.args[0].to_pandas()
785-
+ ", "
786-
+ expr.args[1].to_pandas()
787-
+ ", "
788-
+ expr.args[2].to_pandas()
789-
+ ")"
791+
"@if_else("
792+
+ expr.args[0].to_pandas()
793+
+ ", "
794+
+ expr.args[1].to_pandas()
795+
+ ", "
796+
+ expr.args[2].to_pandas()
797+
+ ")"
790798
),
791799
"neg": lambda expr: "-" + expr.args[0].to_pandas(want_inline_parens=True),
792800
"co_equalizer": lambda expr: (
793-
"@co_equalizer("
794-
+ expr.args[0].to_pandas()
795-
+ ", "
796-
+ expr.args[1].to_pandas()
797-
+ ")"
801+
"@co_equalizer("
802+
+ expr.args[0].to_pandas()
803+
+ ", "
804+
+ expr.args[1].to_pandas()
805+
+ ")"
798806
),
799807
"connected_components": connected_components,
800808
"partitioned_eval": lambda expr: (
@@ -811,7 +819,6 @@ def connected_components(expr):
811819
),
812820
}
813821

814-
815822
r_formatters = {"neg": lambda expr: "-" + expr.args[0].to_R(want_inline_parens=True)}
816823

817824

@@ -865,7 +872,7 @@ def to_pandas(self, *, want_inline_parens=False):
865872
return "_" + self.op + "()"
866873
if len(self.args) == 1:
867874
return (
868-
self.op + "(" + self.args[0].to_pandas(want_inline_parens=False) + ")"
875+
self.op + "(" + self.args[0].to_pandas(want_inline_parens=False) + ")"
869876
)
870877
subs = [ai.to_pandas(want_inline_parens=True) for ai in self.args]
871878
if len(subs) == 2 and self.inline:
@@ -882,7 +889,7 @@ def to_R(self, *, want_inline_parens=False):
882889
return self.op + "()"
883890
if len(self.args) == 1:
884891
return (
885-
self.op + "(" + self.args[0].to_pandas(want_inline_parens=False) + ")"
892+
self.op + "(" + self.args[0].to_pandas(want_inline_parens=False) + ")"
886893
)
887894
subs = [ai.to_pandas(want_inline_parens=True) for ai in self.args]
888895
if len(subs) == 2 and self.inline:

coverage.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,14 @@ data_algebra/db_model.py 359 80 78%
6161
data_algebra/diagram.py 56 44 21%
6262
data_algebra/env.py 52 7 87%
6363
data_algebra/expr.py 20 4 80%
64-
data_algebra/expr_rep.py 614 206 66%
64+
data_algebra/expr_rep.py 617 206 67%
6565
data_algebra/flow_text.py 17 0 100%
6666
data_algebra/pandas_model.py 165 25 85%
6767
data_algebra/test_util.py 6 0 100%
6868
data_algebra/util.py 96 11 89%
6969
data_algebra/yaml.py 121 15 88%
7070
----------------------------------------------------------
71-
TOTAL 3549 946 73%
71+
TOTAL 3552 946 73%
7272

7373

74-
========================== 51 passed in 9.17 seconds ===========================
74+
========================== 51 passed in 9.26 seconds ===========================

data_algebra/expr_rep.py

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
21
from typing import Union
32
import collections
43

54
import data_algebra.util
65
import data_algebra.env
76

7+
88
# for some ideas in capturing expressions in Python see:
99
# scipy
1010
# pipe-like idea
@@ -18,7 +18,7 @@ class Term:
1818

1919
source_string: Union[str, None]
2020

21-
def __init__(self,):
21+
def __init__(self, ):
2222
self.source_string = None
2323

2424
# builders
@@ -687,7 +687,7 @@ def __init__(self, value):
687687
def replace_view(self, view):
688688
return self
689689

690-
def to_python(self, want_inline_parens=False):
690+
def to_python(self, *, want_inline_parens=False):
691691
return self.value.__repr__()
692692

693693

@@ -701,7 +701,7 @@ def __init__(self, value):
701701
def replace_view(self, view):
702702
return self
703703

704-
def to_python(self, want_inline_parens=False):
704+
def to_python(self, *, want_inline_parens=False):
705705
return self.value.__name__
706706

707707

@@ -713,10 +713,18 @@ def __init__(self, value):
713713
Term.__init__(self)
714714

715715
def replace_view(self, view):
716-
return self
716+
new_list = [ai.replace_view(view) for ai in self.value]
717+
return ListTerm(new_list)
717718

718-
def to_python(self, want_inline_parens=False):
719-
return self.value.__name__
719+
def to_python(self, *, want_inline_parens=False):
720+
return ('['
721+
+ ', '.join([ai.to_python(want_inline_parens=want_inline_parens) for ai in self.value])
722+
+ ']')
723+
724+
def to_pandas(self, *, want_inline_parens=False):
725+
return ('['
726+
+ ', '.join([ai.to_pandas(want_inline_parens=want_inline_parens) for ai in self.value])
727+
+ ']')
720728

721729
def get_column_names(self, columns_seen):
722730
for ti in self.value:
@@ -780,21 +788,21 @@ def connected_components(expr):
780788
"is_bad": lambda expr: "@is_bad(" + expr.args[0].to_pandas() + ")",
781789
"is_null": lambda expr: "@is_null(" + expr.args[0].to_pandas() + ")",
782790
"if_else": lambda expr: (
783-
"@if_else("
784-
+ expr.args[0].to_pandas()
785-
+ ", "
786-
+ expr.args[1].to_pandas()
787-
+ ", "
788-
+ expr.args[2].to_pandas()
789-
+ ")"
791+
"@if_else("
792+
+ expr.args[0].to_pandas()
793+
+ ", "
794+
+ expr.args[1].to_pandas()
795+
+ ", "
796+
+ expr.args[2].to_pandas()
797+
+ ")"
790798
),
791799
"neg": lambda expr: "-" + expr.args[0].to_pandas(want_inline_parens=True),
792800
"co_equalizer": lambda expr: (
793-
"@co_equalizer("
794-
+ expr.args[0].to_pandas()
795-
+ ", "
796-
+ expr.args[1].to_pandas()
797-
+ ")"
801+
"@co_equalizer("
802+
+ expr.args[0].to_pandas()
803+
+ ", "
804+
+ expr.args[1].to_pandas()
805+
+ ")"
798806
),
799807
"connected_components": connected_components,
800808
"partitioned_eval": lambda expr: (
@@ -811,7 +819,6 @@ def connected_components(expr):
811819
),
812820
}
813821

814-
815822
r_formatters = {"neg": lambda expr: "-" + expr.args[0].to_R(want_inline_parens=True)}
816823

817824

@@ -865,7 +872,7 @@ def to_pandas(self, *, want_inline_parens=False):
865872
return "_" + self.op + "()"
866873
if len(self.args) == 1:
867874
return (
868-
self.op + "(" + self.args[0].to_pandas(want_inline_parens=False) + ")"
875+
self.op + "(" + self.args[0].to_pandas(want_inline_parens=False) + ")"
869876
)
870877
subs = [ai.to_pandas(want_inline_parens=True) for ai in self.args]
871878
if len(subs) == 2 and self.inline:
@@ -882,7 +889,7 @@ def to_R(self, *, want_inline_parens=False):
882889
return self.op + "()"
883890
if len(self.args) == 1:
884891
return (
885-
self.op + "(" + self.args[0].to_pandas(want_inline_parens=False) + ")"
892+
self.op + "(" + self.args[0].to_pandas(want_inline_parens=False) + ")"
886893
)
887894
subs = [ai.to_pandas(want_inline_parens=True) for ai in self.args]
888895
if len(subs) == 2 and self.inline:
38 Bytes
Binary file not shown.

dist/data_algebra-0.3.1.tar.gz

41 Bytes
Binary file not shown.

tests/test_cc.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import data_algebra.util
55
from data_algebra.data_ops import *
66
from data_algebra.connected_components import connected_components, partitioned_eval
7+
from data_algebra.test_util import formats_to_self
78

89

910
def test_cc():
@@ -22,14 +23,14 @@ def test_cc_ops():
2223

2324
ops = describe_table(d). \
2425
extend({'c': 'f.co_equalizer(g)'})
25-
res = ops.transform(d)
26+
assert formats_to_self(ops)
2627

28+
res = ops.transform(d)
2729
expect = pandas.DataFrame({
2830
'f': [1, 4, 6, 2, 1],
2931
'g': [2, 5, 7, 3, 7],
3032
'c': [1, 4, 1, 1, 1],
3133
})
32-
3334
assert data_algebra.util.equivalent_frames(res, expect)
3435

3536

@@ -41,14 +42,14 @@ def test_cc_ops_f():
4142

4243
ops = describe_table(d). \
4344
extend({'c': 'connected_components(f, g)'})
44-
res = ops.transform(d)
45+
assert formats_to_self(ops)
4546

47+
res = ops.transform(d)
4648
expect = pandas.DataFrame({
4749
'f': [1, 4, 6, 2, 1],
4850
'g': [2, 5, 7, 3, 7],
4951
'c': [1, 4, 1, 1, 1],
5052
})
51-
5253
assert data_algebra.util.equivalent_frames(res, expect)
5354

5455

@@ -70,13 +71,13 @@ def test_cc_partitioned_ops():
7071

7172
ops = describe_table(d). \
7273
extend({'c': 'partitioned_eval(connected_components, [f, g], [p])'})
73-
res = ops.transform(d)
74+
assert formats_to_self(ops)
7475

76+
res = ops.transform(d)
7577
expect = pandas.DataFrame({
7678
'f': [1, 4, 6, 2, 1],
7779
'g': [2, 5, 7, 3, 7],
7880
'p': [1, 2, 1, 2, 1],
7981
'c': [1, 4, 1, 2, 1],
8082
})
81-
8283
assert data_algebra.util.equivalent_frames(res, expect)

0 commit comments

Comments
 (0)