Best Python code snippet using tappy_python
tree.py
Source:tree.py  
1import six2import random as rnd3from .bpe import BpePair, PAR_CHILD_PAIR, ORD_PAIR, UNORD_PAIR4TOK_WORD = '<?>'5SKP_WORD = '<sk>'6RIG_WORD = '<]>'7SKIP_OP_LIST = ['lambda', 'exists', 'argmin', 'argmax',8                'min', 'max', 'count', 'sum', 'the']9class STree(object):10    def __init__(self, init):11        self.parent = None12        self.children = []13        self.surr_paren = False14        if init is not None:15            if isinstance(init, list):16                self.set_by_token_list(init)17            elif isinstance(init, six.string_types):18                self.set_by_str(init)19            else:20                raise NotImplementedError21    def add_child(self, c):22        if isinstance(c, type(self)):23            c.parent = self24        self.children.append(c)25    def set_by_str(self, s):26        _tk_list = s.replace('(', '( ').replace('  ', ' ').strip().split(' ')27        self._set_by_token_list(_tk_list)28    def set_by_token_list(self, _tk_list):29        self.set_by_str(' '.join(_tk_list))30    # well-tokenized token list31    def _set_by_token_list(self, _tk_list):32        c_left = _tk_list.count('(')33        c_right = _tk_list.count(')')34        if (c_right > c_left) and (c_right - c_left < len(_tk_list)):35            _tk_list = _tk_list[:c_left - c_right]36        if _tk_list[0] == '(' and _tk_list[-1] == ')':37            tk_list = _tk_list[1:-1]38            self.surr_paren = True39        else:40            tk_list = _tk_list41        i = 042        while i < len(tk_list):43            if tk_list[i] == '(':44                # find the matching ')'45                depth = 046                right = len(tk_list)47                for j in range(i + 1, len(tk_list)):48                    if tk_list[j] == ')':49                        if depth == 0:50                            right = j51                            break52                        else:53                            depth -= 154                    elif tk_list[j] == '(':55                        depth += 156                c = type(self)(tk_list[i + 1:right])57                c.surr_paren = True58                self.add_child(c)59                i = right + 160            else:61                # skip ``e'' in ``lambda $0 e''62                if (i < 2) or not ((tk_list[i - 2] == 'lambda') and tk_list[i - 1].startswith('$') and len(tk_list[i]) == 1):63                    self.add_child(tk_list[i])64                i += 165    def to_list(self, shorten=True):66        s_list = []67        for c in self.children:68            if isinstance(c, type(self)):69                s_list.extend(c.to_list(shorten=shorten))70            else:71                s_list.append(c)72        if self.surr_paren:73            assert len(s_list) > 174            if shorten:75                s_list = ['(' + s_list[0]] + s_list[1:] + [')']76            else:77                s_list = ['('] + s_list + [')']78        return s_list79    def __str__(self):80        return ' '.join(self.to_list(shorten=True))81    def layout(self, dataset, add_skip=False):82        if dataset == 'atis':83            return self.atis_layout(add_skip=add_skip)84        elif dataset == 'geoqueries':85            return self.atis_layout(add_skip=add_skip)86        elif dataset == 'jobs':87            return self.jobs_layout(add_skip=add_skip)88        else:89            raise NotImplementedError90    def atis_layout(self, add_skip=False):91        op = self.children[0]92        is_leaf = not any((isinstance(c, type(self)) for c in self.children))93        if is_leaf:94            if self.surr_paren:95                op += '@{}'.format(len(self.children) - 1)96                if add_skip:97                    return [op] + [SKP_WORD for __ in range(len(self.children) - 1)] + [RIG_WORD]98                else:99                    return [op]100            else:101                assert len(self.children) == 1102                return [TOK_WORD]103        else:104            skip = 1105            if op in SKIP_OP_LIST:106                # skip one variable ($0, ...)107                skip = 2108            elif op in ('and', 'or', 'not', '=', '>', '<'):109                pass110            else:111                # atis: op in ('airline:e', 'departure_time', 'aircraft', 'fare', 'stop', 'the', 'stops', 'has_meal', 'restriction_code', 'airline_name', 'flight_number', 'capacity')112                op += '@{}'.format(len(self.children) - 1)113            lay_list = [op]114            if add_skip:115                for __ in range(skip - 1):116                    lay_list.append(SKP_WORD)117            for i in range(skip, len(self.children)):118                c = self.children[i]119                if isinstance(c, type(self)):120                    lay_list.extend(c.atis_layout(add_skip=add_skip))121                else:122                    lay_list.append(TOK_WORD)123            if self.surr_paren:124                lay_list = ['(' + lay_list[0]] + lay_list[1:] + [')']125            else:126                raise NotImplementedError127            return lay_list128    def geo_layout(self, add_skip=False):129        pass130    def jobs_layout(self, add_skip=False):131        pass132    def norm(self, not_layout=False):133        if len(self.children) > 1:134            for c in self.children:135                if isinstance(c, type(self)):136                    c.norm(not_layout=not_layout)137            # sort child nodes for ``and/or/=''138            op = self.children[0]139            st_sort = None140            if op in ('and', 'or', '=', 'next_to:t',):141                st_sort = 1142            elif op in ('exists', 'min', 'max', 'count', 'sum'):143                if not_layout:144                    # skip $variable145                    st_sort = 2146                else:147                    st_sort = 1148            if (st_sort is not None) and (len(self.children) > st_sort + 1):149                arg_list = list(sorted(self.children[st_sort:], key=str))150                self.children = self.children[:st_sort] + arg_list151            if not_layout:152                # remove duplicate child nodes for ``and/or''153                if op in ('and', 'or'):154                    deduplicate_list = []155                    has_set = set()156                    for c in self.children:157                        if str(c) not in has_set:158                            has_set.add(str(c))159                            deduplicate_list.append(c)160                    self.children = deduplicate_list161        return self162    def permute(self, not_layout=False):163        if len(self.children) > 1:164            for c in self.children:165                if isinstance(c, type(self)):166                    c.permute(not_layout=not_layout)167            # sort child nodes for ``and/or/=''168            op = self.children[0]169            st_sort = None170            if op in ('and', 'or', '=', 'next_to:t',):171                st_sort = 1172            elif op in ('exists', 'min', 'max', 'count', 'sum'):173                if not_layout:174                    # skip $variable175                    st_sort = 2176                else:177                    st_sort = 1178            if (st_sort is not None) and (len(self.children) > st_sort + 1):179                arg_list = list(self.children[st_sort:])180                rnd.shuffle(arg_list)181                self.children = self.children[:st_sort] + arg_list182        return self183    def is_ordered(self):184        op = self.children[0]185        if op in ('and', 'or', '=', 'next_to:t',) or op in ('exists', 'min', 'max', 'count', 'equals', 'sum'):186            return False187        else:188            return True189    def all_bpe_pairs(self):190        pair_list = []191        if len(self.children) <= 1:192            pass193        elif len(self.children) == 2 and isinstance(self.children[1], six.string_types):194            pair_list.append(195                BpePair((self.children[0], self.children[1], PAR_CHILD_PAIR)))196        else:197            c_list = self.children[1:]198            if len(c_list) >= 2:199                if self.is_ordered():200                    for i in range(len(c_list) - 1):201                        if isinstance(c_list[i], six.string_types) and isinstance(c_list[i + 1], six.string_types):202                            pair_list.append(203                                BpePair((c_list[i], c_list[i + 1], ORD_PAIR)))204                else:205                    s_list = [c for c in c_list if isinstance(206                        c, six.string_types)]207                    s_list.sort()208                    for i in range(len(s_list) - 1):209                        for j in range(i + 1, len(s_list)):210                            pair_list.append(211                                BpePair((s_list[i], s_list[j], UNORD_PAIR)))212            for c in self.children:213                if isinstance(c, type(self)):214                    pair_list.extend(c.all_bpe_pairs())215        return pair_list216    def apply_bpe(self, p):217        is_ordered = self.is_ordered()218        if len(self.children) <= 1:219            pass220        elif p.type_pair == PAR_CHILD_PAIR:221            new_list = []222            for c in self.children:223                if isinstance(c, type(self)) and len(c.children) == 2 and isinstance(c.children[1], six.string_types) and p.is_match(c.children[0], c.children[1]):224                    new_list.append(str(p))225                else:226                    new_list.append(c)227            self.children = new_list228            # check self229            c = self230            if isinstance(c, type(self)) and len(c.children) == 2 and isinstance(c.children[1], six.string_types) and p.is_match(c.children[0], c.children[1]):231                self.children = [str(p)]232                self.surr_paren = False233        elif p.type_pair == ORD_PAIR:234            c_list = self.children[1:]235            if len(c_list) >= 2 and is_ordered:236                new_list = [self.children[0]]237                i = 0238                while i < len(c_list):239                    if (i + 1 < len(c_list)) and isinstance(c_list[i], six.string_types) and isinstance(c_list[i + 1], six.string_types) and p.is_match(c_list[i], c_list[i + 1]):240                        new_list.append(str(p))241                        i += 2242                    else:243                        new_list.append(c_list[i])244                        i += 1245                self.children = new_list246        elif p.type_pair == UNORD_PAIR:247            if not is_ordered:248                while True:249                    c_list = self.children[1:]250                    found = None251                    if len(c_list) >= 2:252                        for i in range(len(c_list) - 1):253                            for j in range(i + 1, len(c_list)):254                                if isinstance(c_list[i], six.string_types) and isinstance(c_list[j], six.string_types) and p.is_match(c_list[i], c_list[j]):255                                    found = (i, j)256                                    break257                            if found is not None:258                                break259                    if found is None:260                        break261                    else:262                        # replace263                        new_list = [self.children[0]] + [c_list[i]264                                                         for i in range(found[0])]265                        new_list.append(str(p))266                        new_list.extend([c_list[i] for i in range(267                            found[0] + 1, len(c_list)) if i != found[1]])268                        self.children = new_list269        for c in self.children:270            if isinstance(c, type(self)):271                c.apply_bpe(p)272def norm_tree_var(t):273    tk_list = t.to_list(shorten=False)274    v_list = []275    for tk in tk_list:276        if tk.startswith('$') and (tk not in v_list):277            v_list.append(tk)278    v_dict = {}279    for i, v in enumerate(v_list):280        v_map = '$' + str(i)281        if v != v_map:282            v_dict[v] = v_map283    return STree([v_dict.get(tk, tk) for tk in tk_list])284def is_tree_eq(t1, t2, not_layout=False):285    try:286        if isinstance(t1, STree):287            t1 = STree(str(t1))288        else:289            t1 = STree(t1)290        if isinstance(t2, STree):291            t2 = STree(str(t2))292        else:293            t2 = STree(t2)294        return str(norm_tree_var(t1.norm(not_layout=not_layout))).lower() == str(norm_tree_var(t2.norm(not_layout=not_layout))).lower()295    except Exception:296        return False297if __name__ == '__main__':298    for s in ("(capacity boeing:mf )",299              "( lambda $0 e ( exists $1 ( and ( airline $1 al0 ) ( flight_number $1 fn0 ) ( = ( aircraft_code $1 ) $0 ) ) ) )",300              "(lambda $0 e ( and ( flight $0 ) ( to $0 ap0 ) ( exists $1 ( and ( city $1 ) ( from $0 $1 ) ) ) ) )".split()):301        t = STree(s)302        print(1, t)303        print(2, t.to_list())304        print(3, t.to_list(shorten=False))305        print(4, ' '.join(t.layout('atis', add_skip=False)))306        print(5, ' '.join(t.layout('atis', add_skip=True)))307        assert len(str(t).split(' ')) == len(t.layout('atis', add_skip=True))308        assert str(t) == str(STree(str(t)))309        t_lay = STree(' '.join(t.layout('atis', add_skip=False)))310        assert str(t_lay) == str(STree(str(t_lay)))311        print('')312    s1 = "(lambda $0 e ( and ( flight $0 ) ( to $0 ap0 ) ( exists $1 ( and ( from $0 $1 ) ( city $1 ) ) ) ) )"313    s2 = "(lambda $a e ( and ( to $a ap0 ) ( flight $a ) ( exists $0 ( and ( city $0 ) ( from $a $0 ) ) ) ) )"...test_status_summary.py
Source:test_status_summary.py  
...49        """Test default increment of skips50        """51        status = StatusSummary()52        expected = 253        status.add_skip()54        status.add_skip()55        received = status.skips()56        self.assertEqual(expected, received)57    def test_status_add_skip_specified(self):58        """Test specified increment of skips59        """60        status = StatusSummary()61        expected = 262        status.add_skip(2)63        received = status.skips()64        self.assertEqual(expected, received)65        expected = 566        status.add_skip(3)67        received = status.skips()68        self.assertEqual(expected, received)69    def test_status_add_fail_default(self):70        """Test default increment of failures71        """72        status = StatusSummary()73        expected = 274        status.add_failure()75        status.add_failure()76        received = status.failures()77        self.assertEqual(expected, received)78    def test_status_add_failures_specified(self):79        """Test default increment of failures80        """81        status = StatusSummary()82        expected = 283        status.add_failure(2)84        received = status.failures()85        self.assertEqual(expected, received)86        expected = 587        status.add_failure(3)88        received = status.failures()89        self.assertEqual(expected, received)90    def test_status_add_pass_default(self):91        """Test default increment of passes92        """93        status = StatusSummary()94        expected = 395        status.add_pass()96        status.add_pass()97        status.add_pass()98        received = status.passes()99        self.assertEqual(expected, received)100    def test_status_add_pass_specified(self):101        """Test default increment of passs102        """103        status = StatusSummary()104        expected = 2105        status.add_pass(2)106        received = status.passes()107        self.assertEqual(expected, received)108        expected = 6109        status.add_pass(4)110        received = status.passes()111        self.assertEqual(expected, received)112    def test_status_add_total_default(self):113        """Test default increment of total when passes, failures and skips are114added.115        """116        status = StatusSummary()117        expected = 3118        status.add_pass()119        status.add_failure()120        status.add_skip()121        received = status.total()122        self.assertEqual(expected, received)123    def test_status_add_total_specified(self):124        """Test increment of total when passs, failures and skips are added.125        """126        status = StatusSummary()127        expected = 2128        status.add_failure(2)129        received = status.total()130        self.assertEqual(expected, received)131        expected = 6132        status.add_pass(4)133        received = status.total()134        self.assertEqual(expected, received)135        expected = 9136        status.add_skip(3)137        received = status.total()138        self.assertEqual(expected, received)139    def test_status_addition_operator(self):140        """Test addition operator for adding status objects141        """142        status_1 = StatusSummary()143        expected_total_1 = 3144        status_1.add_pass()145        status_1.add_failure()146        status_1.add_skip()147        received = status_1.total()148        self.assertEqual(expected_total_1, received)149        status_2 = StatusSummary()150        status_2.add_failure(2)151        status_2.add_pass(4)152        status_2.add_skip(3)153        expected_total_2 = 9154        received = status_2.total()155        self.assertEqual(expected_total_2, received)156        status_3 = status_1 + status_2157        expected_total_3 = expected_total_1 + expected_total_2158        received = status_3.total()159        self.assertEqual(expected_total_3, received)160        expected_skip_3 = status_1.skips() + status_2.skips()161        received = status_3.skips()162        self.assertEqual(expected_skip_3, received)163    def test_status_addition_increment(self):164        """Test addition increment, +=, operator for adding status objects165        """166        status_1 = StatusSummary()167        expected_total_1 = 3168        status_1.add_pass()169        status_1.add_failure()170        status_1.add_skip()171        received = status_1.total()172        self.assertEqual(expected_total_1, received)173        status_2 = StatusSummary()174        status_2.add_failure(2)175        status_2.add_pass(4)176        status_2.add_skip(3)177        expected_total_2 = 9178        received = status_2.total()179        self.assertEqual(expected_total_2, received)180        expected_total_3 = expected_total_1 + expected_total_2181        expected_skip_3 = status_1.skips() + status_2.skips()182        status_1 += status_2183        received_skip_3 = status_1.skips()184        self.assertEqual(expected_skip_3, received_skip_3)185        received_total_3 = status_1.total()186        self.assertEqual(expected_total_3, received_total_3)187# -----------------------------------------------------------------------------188if __name__ == '__main__':189    # unittest.main(buffer=True)190    unittest.main()  # pragma: no coveragedecoder.py
Source:decoder.py  
...52        os = self.os53        y = self.dec5[0](x)54        for i in range(3):55            y = self.dec5[1+i](y)56        y, skips, os = self.add_skip(x, y, skips, os)57        x = y58        for i in range(4):59            y = self.dec4[i](y)60        y, skips, os = self.add_skip(x, y, skips, os)61        62        x = y63        for i in range(4):64            y = self.dec3[i](y)65        y, skips, os = self.add_skip(x, y, skips, os)66        67        x = y68        for i in range(4):69            y = self.dec2[i](y)70        y, skips, os = self.add_skip(x, y, skips, os)71        72        x = y73        for i in range(4):74            y = self.dec1[i](y)75        y, skips, os = self.add_skip(x, y, skips, os)76        y = self.dropout(y)77        return y78    79    def add_skip(self, x, y, skips, os):80        if y.shape[2] > x.shape[2]:81            os //= 282            y = y + tf.stop_gradient(skips[os])83        return y, skips, os84    def set_skips(self, skips, os):85        self.skips = skips86        self.os = os87    def make_decoder_layer(self, planes, bn_d=0.01, stride= 2):88        layers = []89        if stride == 2:90            if self.pixel_shuffle_flag:91                layers.append(PixelShuffle())92                layers.append(Conv2D(planes[1], kernel_size = 3, padding='same', data_format=self.data_format))93            else:...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
