Best Python code snippet using pandera_python
check.py
Source:check.py  
1#2# Copyright (C) 2016 Red Hat, Inc.3#4# This copyrighted material is made available to anyone wishing to use,5# modify, copy, or redistribute it subject to the terms and conditions of6# the GNU General Public License v.2, or (at your option) any later version.7# This program is distributed in the hope that it will be useful, but WITHOUT8# ANY WARRANTY expressed or implied, including the implied warranties of9# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General10# Public License for more details.  You should have received a copy of the11# GNU General Public License along with this program; if not, write to the12# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA13# 02110-1301, USA.  Any Red Hat trademarks that are incorporated in the14# source code or documentation are not subject to the GNU General Public15# License and may only be used or replicated with the express permission of16# Red Hat, Inc.17#18from __future__ import absolute_import19from __future__ import unicode_literals20from dnf.i18n import _21from dnf.cli import commands22import argparse23import dnf.exceptions24class CheckCommand(commands.Command):25    """A class containing methods needed by the cli to execute the check26    command.27    """28    aliases = ('check',)29    summary = _('check for problems in the packagedb')30    @staticmethod31    def set_argparser(parser):32        parser.add_argument('--all', dest='check_types',33                            action='append_const', const='all',34                            help=_('show all problems; default'))35        parser.add_argument('--dependencies', dest='check_types',36                            action='append_const', const='dependencies',37                            help=_('show dependency problems'))38        parser.add_argument('--duplicates', dest='check_types',39                            action='append_const', const='duplicates',40                            help=_('show duplicate problems'))41        parser.add_argument('--obsoleted', dest='check_types',42                            action='append_const', const='obsoleted',43                            help=_('show obsoleted packages'))44        parser.add_argument('--provides', dest='check_types',45                            action='append_const', const='provides',46                            help=_('show problems with provides'))47        # Add compatibility with yum but invisible in help48        # In choices [] allows to return empty list if no argument otherwise it fails49        parser.add_argument('check_yum_types', nargs='*', choices=[50            'all', 'dependencies', 'duplicates', 'obsoleted', 'provides', []],51                            help=argparse.SUPPRESS)52    def configure(self):53        self.cli.demands.sack_activation = True54        if self.opts.check_yum_types:55            if self.opts.check_types:56                self.opts.check_types = self.opts.check_types + \57                                        self.opts.check_yum_types58            else:59                self.opts.check_types = self.opts.check_yum_types60        if not self.opts.check_types:61            self.opts.check_types = {'all'}62        else:63            self.opts.check_types = set(self.opts.check_types)64        self.base.conf.disable_excludes.append("all")65    def run(self):66        output_set = set()67        q = self.base.sack.query().installed()68        if self.opts.check_types.intersection({'all', 'dependencies'}):69            for pkg in q:70                for require in pkg.requires:71                    if str(require).startswith('rpmlib'):72                        continue73                    if not len(q.filter(provides=[require])):74                        msg = _("{} has missing requires of {}")75                        output_set.add(msg.format(76                            self.base.output.term.bold(pkg),77                            self.base.output.term.bold(require)))78                for conflict in pkg.conflicts:79                    conflicted = q.filter(provides=[conflict],80                                          name=str(conflict).split()[0])81                    for conflict_pkg in conflicted:82                        msg = '{} has installed conflict "{}": {}'83                        output_set.add(msg.format(84                            self.base.output.term.bold(pkg),85                            self.base.output.term.bold(conflict),86                            self.base.output.term.bold(conflict_pkg)))87        if self.opts.check_types.intersection({'all', 'duplicates'}):88            installonly = self.base._get_installonly_query(q)89            dups = q.duplicated().difference(installonly)._name_dict()90            for name, pkgs in dups.items():91                pkgs.sort()92                for dup in pkgs[1:]:93                    msg = _("{} is a duplicate with {}").format(94                        self.base.output.term.bold(pkgs[0]),95                        self.base.output.term.bold(dup))96                    output_set.add(msg)97        if self.opts.check_types.intersection({'all', 'obsoleted'}):98            for pkg in q:99                for obsolete in pkg.obsoletes:100                    obsoleted = q.filter(provides=[obsolete],101                                         name=str(obsolete).split()[0])102                    if len(obsoleted):103                        msg = _("{} is obsoleted by {}").format(104                            self.base.output.term.bold(obsoleted[0]),105                            self.base.output.term.bold(pkg))106                        output_set.add(msg)107        if self.opts.check_types.intersection({'all', 'provides'}):108            for pkg in q:109                for provide in pkg.provides:110                    if pkg not in q.filter(provides=[provide]):111                        msg = _("{} provides {} but it cannot be found")112                        output_set.add(msg.format(113                            self.base.output.term.bold(pkg),114                            self.base.output.term.bold(provide)))115        for msg in sorted(output_set):116            print(msg)117        if output_set:118            raise dnf.exceptions.Error(...calc.py
Source:calc.py  
...3class InvalidPermissions(Exception):4    pass5class Calculator:6    def add(self, x, y):7        self.check_types(x, y)8        return x + y9    def substract(self, x, y):10        self.check_types(x, y)11        return x - y12    def multiply(self, x, y):13        if not app.util.validate_permissions(f"{x} * {y}", "user1"):14            raise InvalidPermissions('User has no permissions')15        self.check_types(x, y)16        return x * y17    def divide(self, x, y):18        self.check_types(x, y)19        if y == 0:20            raise TypeError("Division by zero is not possible")21        return x / y22    def power(self, x, y):23        self.check_types(x, y)24        return x ** y25    def sqrt(self,x):26        self.check_types(x,1)27        if x < 0:28          raise TypeError("Nùmero negativo")29        return math.sqrt(x)30    def log10(self,x):31        self.check_types(x,1)32        if x == 0:33          raise TypeError("Error calculo logaritmo")34        return math.log10(x)35    def check_types(self, x, y):36        if not isinstance(x, (int, float)) or not isinstance(y, (int, float)):37            raise TypeError("Parameters must be numbers")38if __name__ == "__main__":  # pragma: no cover39    calc = Calculator()40    result = calc.add(2, 2)...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!!
