Best Python code snippet using pandera_python
torch.py
Source:torch.py  
1from __future__ import absolute_import, division, print_function2import torch3from pyro.distributions.torch_distribution import TorchDistributionMixin4class Bernoulli(torch.distributions.Bernoulli, TorchDistributionMixin):5    def expand(self, batch_shape):6        validate_args = self.__dict__.get('validate_args')7        if 'probs' in self.__dict__:8            probs = self.probs.expand(batch_shape)9            return Bernoulli(probs=probs, validate_args=validate_args)10        else:11            logits = self.logits.expand(batch_shape)12            return Bernoulli(logits=logits, validate_args=validate_args)13class Beta(torch.distributions.Beta, TorchDistributionMixin):14    def expand(self, batch_shape):15        validate_args = self.__dict__.get('validate_args')16        concentration1 = self.concentration1.expand(batch_shape)17        concentration0 = self.concentration0.expand(batch_shape)18        return Beta(concentration1, concentration0, validate_args=validate_args)19class Categorical(torch.distributions.Categorical, TorchDistributionMixin):20    def expand(self, batch_shape):21        batch_shape = torch.Size(batch_shape)22        validate_args = self.__dict__.get('validate_args')23        if 'probs' in self.__dict__:24            probs = self.probs.expand(batch_shape + self.probs.shape[-1:])25            return Categorical(probs=probs, validate_args=validate_args)26        else:27            logits = self.logits.expand(batch_shape + self.logits.shape[-1:])28            return Categorical(logits=logits, validate_args=validate_args)29class Cauchy(torch.distributions.Cauchy, TorchDistributionMixin):30    def expand(self, batch_shape):31        validate_args = self.__dict__.get('validate_args')32        loc = self.loc.expand(batch_shape)33        scale = self.scale.expand(batch_shape)34        return Cauchy(loc, scale, validate_args=validate_args)35class Chi2(torch.distributions.Chi2, TorchDistributionMixin):36    def expand(self, batch_shape):37        validate_args = self.__dict__.get('validate_args')38        df = self.df.expand(batch_shape)39        return Chi2(df, validate_args=validate_args)40class Dirichlet(torch.distributions.Dirichlet, TorchDistributionMixin):41    def expand(self, batch_shape):42        batch_shape = torch.Size(batch_shape)43        validate_args = self.__dict__.get('validate_args')44        concentration = self.concentration.expand(batch_shape + self.event_shape)45        return Dirichlet(concentration, validate_args=validate_args)46class Exponential(torch.distributions.Exponential, TorchDistributionMixin):47    def expand(self, batch_shape):48        validate_args = self.__dict__.get('validate_args')49        rate = self.rate.expand(batch_shape)50        return Exponential(rate, validate_args=validate_args)51class Gamma(torch.distributions.Gamma, TorchDistributionMixin):52    def expand(self, batch_shape):53        validate_args = self.__dict__.get('validate_args')54        concentration = self.concentration.expand(batch_shape)55        rate = self.rate.expand(batch_shape)56        return Gamma(concentration, rate, validate_args=validate_args)57class Geometric(torch.distributions.Geometric, TorchDistributionMixin):58    def expand(self, batch_shape):59        validate_args = self.__dict__.get('validate_args')60        if 'probs' in self.__dict__:61            probs = self.probs.expand(batch_shape)62            return Geometric(probs=probs, validate_args=validate_args)63        else:64            logits = self.logits.expand(batch_shape)65            return Geometric(logits=logits, validate_args=validate_args)66class Gumbel(torch.distributions.Gumbel, TorchDistributionMixin):67    def expand(self, batch_shape):68        validate_args = self.__dict__.get('validate_args')69        loc = self.loc.expand(batch_shape)70        scale = self.scale.expand(batch_shape)71        return Gumbel(loc, scale, validate_args=validate_args)72class Independent(torch.distributions.Independent, TorchDistributionMixin):73    def expand(self, batch_shape):74        batch_shape = torch.Size(batch_shape)75        validate_args = self.__dict__.get('validate_args')76        extra_shape = self.base_dist.event_shape[:self.reinterpreted_batch_ndims]77        base_dist = self.base_dist.expand(batch_shape + extra_shape)78        return Independent(base_dist, self.reinterpreted_batch_ndims, validate_args=validate_args)79class Laplace(torch.distributions.Laplace, TorchDistributionMixin):80    def expand(self, batch_shape):81        validate_args = self.__dict__.get('validate_args')82        loc = self.loc.expand(batch_shape)83        scale = self.scale.expand(batch_shape)84        return Laplace(loc, scale, validate_args=validate_args)85class LogNormal(torch.distributions.LogNormal, TorchDistributionMixin):86    def expand(self, batch_shape):87        validate_args = self.__dict__.get('validate_args')88        loc = self.loc.expand(batch_shape)89        scale = self.scale.expand(batch_shape)90        return LogNormal(loc, scale, validate_args=validate_args)91class Multinomial(torch.distributions.Multinomial, TorchDistributionMixin):92    def expand(self, batch_shape):93        batch_shape = torch.Size(batch_shape)94        validate_args = self.__dict__.get('validate_args')95        if 'probs' in self.__dict__:96            probs = self.probs.expand(batch_shape + self.event_shape)97            return Multinomial(self.total_count, probs=probs, validate_args=validate_args)98        else:99            logits = self.logits.expand(batch_shape + self.event_shape)100            return Multinomial(self.total_count, logits=logits, validate_args=validate_args)101class MultivariateNormal(torch.distributions.MultivariateNormal, TorchDistributionMixin):102    def expand(self, batch_shape):103        batch_shape = torch.Size(batch_shape)104        validate_args = self.__dict__.get('validate_args')105        loc = self.loc.expand(batch_shape + self.event_shape)106        if 'scale_tril' in self.__dict__:107            scale_tril = self.scale_tril.expand(batch_shape + self.event_shape + self.event_shape)108            return MultivariateNormal(loc, scale_tril=scale_tril, validate_args=validate_args)109        elif 'covariance_matrix' in self.__dict__:110            covariance_matrix = self.covariance_matrix.expand(batch_shape + self.event_shape + self.event_shape)111            return MultivariateNormal(loc, covariance_matrix=covariance_matrix, validate_args=validate_args)112        else:113            precision_matrix = self.precision_matrix.expand(batch_shape + self.event_shape + self.event_shape)114            return MultivariateNormal(loc, precision_matrix=precision_matrix, validate_args=validate_args)115class Normal(torch.distributions.Normal, TorchDistributionMixin):116    def expand(self, batch_shape):117        validate_args = self.__dict__.get('validate_args')118        loc = self.loc.expand(batch_shape)119        scale = self.scale.expand(batch_shape)120        return Normal(loc, scale, validate_args=validate_args)121class OneHotCategorical(torch.distributions.OneHotCategorical, TorchDistributionMixin):122    def expand(self, batch_shape):123        batch_shape = torch.Size(batch_shape)124        validate_args = self.__dict__.get('validate_args')125        if 'probs' in self.__dict__:126            probs = self.probs.expand(batch_shape + self.event_shape)127            return OneHotCategorical(probs=probs, validate_args=validate_args)128        else:129            logits = self.logits.expand(batch_shape + self.event_shape)130            return OneHotCategorical(logits=logits, validate_args=validate_args)131class Poisson(torch.distributions.Poisson, TorchDistributionMixin):132    def expand(self, batch_shape):133        validate_args = self.__dict__.get('validate_args')134        rate = self.rate.expand(batch_shape)135        return Poisson(rate, validate_args=validate_args)136class StudentT(torch.distributions.StudentT, TorchDistributionMixin):137    def expand(self, batch_shape):138        validate_args = self.__dict__.get('validate_args')139        df = self.df.expand(batch_shape)140        loc = self.loc.expand(batch_shape)141        scale = self.scale.expand(batch_shape)142        return StudentT(df, loc, scale, validate_args=validate_args)143class TransformedDistribution(torch.distributions.TransformedDistribution, TorchDistributionMixin):144    def expand(self, batch_shape):145        base_dist = self.base_dist.expand(batch_shape)146        return TransformedDistribution(base_dist, self.transforms)147class Uniform(torch.distributions.Uniform, TorchDistributionMixin):148    def expand(self, batch_shape):149        validate_args = self.__dict__.get('validate_args')150        low = self.low.expand(batch_shape)151        high = self.high.expand(batch_shape)152        return Uniform(low, high, validate_args=validate_args)153# Programmatically load all distributions from PyTorch.154__all__ = []155for _name, _Dist in torch.distributions.__dict__.items():156    if _name == 'Binomial':157        continue158    if not isinstance(_Dist, type):159        continue160    if not issubclass(_Dist, torch.distributions.Distribution):161        continue162    if _Dist is torch.distributions.Distribution:163        continue164    try:165        _PyroDist = locals()[_name]166    except KeyError:167        class _PyroDist(_Dist, TorchDistributionMixin):168            pass169        _PyroDist.__name__ = _name170        locals()[_name] = _PyroDist171    _PyroDist.__doc__ = '''172    Wraps :class:`{}.{}` with173    :class:`~pyro.distributions.torch_distribution.TorchDistributionMixin`.174    '''.format(_Dist.__module__, _Dist.__name__)175    __all__.append(_name)176# Create sphinx documentation.177__doc__ = '\n\n'.join([178    '''179    {0}180    ----------------------------------------------------------------181    .. autoclass:: pyro.distributions.{0}182    '''.format(_name)183    for _name in sorted(__all__)...testSecurityManager.py
Source:testSecurityManager.py  
1##############################################################################2#3# Copyright (c) 2005 Zope Foundation and Contributors.4# All Rights Reserved.5#6# This software is subject to the provisions of the Zope Public License,7# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.8# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED9# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED10# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS11# FOR A PARTICULAR PURPOSE.12#13##############################################################################14"""Tests for the SecurityManager implementations15"""16import unittest17_THREAD_ID = 12318class DummyContext:19    def __init__(self):20        self.user = object()21        self.stack = []22class DummyPolicy:23    CHECK_PERMISSION_ARGS = None24    CHECK_PERMISSION_RESULT = object()25    VALIDATE_ARGS = None26    def checkPermission(self, *args):27        self.CHECK_PERMISSION_ARGS = args28        return self.CHECK_PERMISSION_RESULT29    def validate(self, *args):30        self.VALIDATE_ARGS = args31        return True32class ExecutableObject:33    def __init__(self, new_policy):34        self._new_policy = new_policy35    def _customSecurityPolicy(self):36        return self._new_policy37class ISecurityManagerConformance:38    def test_conforms_to_ISecurityManager(self):39        from AccessControl.interfaces import ISecurityManager40        from zope.interface.verify import verifyClass41        verifyClass(ISecurityManager, self._getTargetClass())42class SecurityManagerTestBase(unittest.TestCase):43    def _makeOne(self, thread_id, context):44        return self._getTargetClass()(thread_id, context)45    def test_getUser(self):46        context = DummyContext()47        mgr = self._makeOne(_THREAD_ID, context)48        self.failUnless(mgr.getUser() is context.user)49    def test_calledByExecutable_no_stack(self):50        context = DummyContext()51        mgr = self._makeOne(_THREAD_ID, context)52        self.failIf(mgr.calledByExecutable())53    def test_calledByExecutable_with_stack(self):54        context = DummyContext()55        mgr = self._makeOne(_THREAD_ID, context)56        executableObject = object()57        mgr.addContext(executableObject)58        self.failUnless(mgr.calledByExecutable())59    def test_addContext_no_custom_policy(self):60        context = DummyContext()61        mgr = self._makeOne(_THREAD_ID, context)62        original_policy = mgr._policy63        executableObject = object()64        mgr.addContext(executableObject)65        self.failUnless(mgr._policy is original_policy)66    def test_addContext_with_custom_policy(self):67        context = DummyContext()68        mgr = self._makeOne(_THREAD_ID, context)69        new_policy = DummyPolicy()70        executableObject = ExecutableObject(new_policy)71        mgr.addContext(executableObject)72        self.failUnless(mgr._policy is new_policy)73    def test_addContext_with_custom_policy_then_none(self):74        context = DummyContext()75        mgr = self._makeOne(_THREAD_ID, context)76        original_policy = mgr._policy77        new_policy = DummyPolicy()78        executableObject = ExecutableObject(new_policy)79        mgr.addContext(executableObject)80        mgr.addContext(object())81        self.failUnless(mgr._policy is original_policy)82    def test_removeContext_pops_items_above_EO(self):83        context = DummyContext()84        ALPHA, BETA, GAMMA, DELTA = object(), object(), object(), object()85        context.stack.append(ALPHA)86        context.stack.append(BETA)87        context.stack.append(GAMMA)88        context.stack.append(DELTA)89        mgr = self._makeOne(_THREAD_ID, context)90        mgr.removeContext(GAMMA)91        self.assertEqual(len(context.stack), 2)92        self.failUnless(context.stack[0] is ALPHA)93        self.failUnless(context.stack[1] is BETA)94    def test_removeContext_last_EO_restores_default_policy(self):95        context = DummyContext()96        mgr = self._makeOne(_THREAD_ID, context)97        original_policy = mgr._policy98        new_policy = mgr._policy = DummyPolicy()99        top = object()100        context.stack.append(top)101        mgr.removeContext(top)102        self.failUnless(mgr._policy is original_policy)103    def test_removeContext_with_top_having_custom_policy(self):104        context = DummyContext()105        mgr = self._makeOne(_THREAD_ID, context)106        new_policy = DummyPolicy()107        context.stack.append(ExecutableObject(new_policy))108        top = object()109        context.stack.append(top)110        mgr.removeContext(top)111        self.failUnless(mgr._policy is new_policy)112    def test_removeContext_with_top_having_no_custom_policy(self):113        context = DummyContext()114        mgr = self._makeOne(_THREAD_ID, context)115        original_policy = mgr._policy116        new_policy = DummyPolicy()117        executableObject = ExecutableObject(new_policy)118        context.stack.append(executableObject)119        top = object()120        context.stack.append(top)121        mgr.removeContext(executableObject)122        self.failUnless(mgr._policy is original_policy)123    def test_checkPermission_delegates_to_policy(self):124        context = DummyContext()125        PERMISSION = 'PERMISSION'126        TARGET = object()127        mgr = self._makeOne(_THREAD_ID, context)128        new_policy = mgr._policy = DummyPolicy()129        result = mgr.checkPermission(PERMISSION, TARGET)130        self.failUnless(result is DummyPolicy.CHECK_PERMISSION_RESULT)131        self.failUnless(new_policy.CHECK_PERMISSION_ARGS[0] is PERMISSION)132        self.failUnless(new_policy.CHECK_PERMISSION_ARGS[1] is TARGET)133        self.failUnless(new_policy.CHECK_PERMISSION_ARGS[2] is context)134    def test_validate_without_roles_delegates_to_policy(self):135        from AccessControl.SimpleObjectPolicies import _noroles136        context = DummyContext()137        ACCESSED = object()138        CONTAINER = object()139        NAME = 'NAME'140        VALUE = object()141        mgr = self._makeOne(_THREAD_ID, context)142        new_policy = mgr._policy = DummyPolicy()143        result = mgr.validate(ACCESSED,144                              CONTAINER,145                              NAME,146                              VALUE,147                             )148        self.failUnless(result)149        self.assertEqual(len(new_policy.VALIDATE_ARGS), 5)150        self.failUnless(new_policy.VALIDATE_ARGS[0] is ACCESSED)151        self.failUnless(new_policy.VALIDATE_ARGS[1] is CONTAINER)152        self.assertEqual(new_policy.VALIDATE_ARGS[2], NAME)153        self.failUnless(new_policy.VALIDATE_ARGS[3] is VALUE)154        self.failUnless(new_policy.VALIDATE_ARGS[4] is context)155    def test_validate_with_roles_delegates_to_policy(self):156        from AccessControl.SimpleObjectPolicies import _noroles157        context = DummyContext()158        ACCESSED = object()159        CONTAINER = object()160        NAME = 'NAME'161        VALUE = object()162        ROLES = ('Hamlet', 'Othello')163        mgr = self._makeOne(_THREAD_ID, context)164        new_policy = mgr._policy = DummyPolicy()165        result = mgr.validate(ACCESSED,166                              CONTAINER,167                              NAME,168                              VALUE,169                              ROLES,170                             )171        self.failUnless(result)172        self.assertEqual(len(new_policy.VALIDATE_ARGS), 6)173        self.failUnless(new_policy.VALIDATE_ARGS[0] is ACCESSED)174        self.failUnless(new_policy.VALIDATE_ARGS[1] is CONTAINER)175        self.assertEqual(new_policy.VALIDATE_ARGS[2], NAME)176        self.failUnless(new_policy.VALIDATE_ARGS[3] is VALUE)177        self.failUnless(new_policy.VALIDATE_ARGS[4] is context)178        self.assertEqual(new_policy.VALIDATE_ARGS[5], ROLES)179    def test_DTMLValidate_delegates_to_policy_validate(self):180        from AccessControl.SimpleObjectPolicies import _noroles181        context = DummyContext()182        ACCESSED = object()183        CONTAINER = object()184        NAME = 'NAME'185        VALUE = object()186        MD = {}187        mgr = self._makeOne(_THREAD_ID, context)188        new_policy = mgr._policy = DummyPolicy()189        result = mgr.DTMLValidate(ACCESSED,190                                  CONTAINER,191                                  NAME,192                                  VALUE,193                                  MD,194                                 )195        self.failUnless(result)196        self.assertEqual(len(new_policy.VALIDATE_ARGS), 5)197        self.failUnless(new_policy.VALIDATE_ARGS[0] is ACCESSED)198        self.failUnless(new_policy.VALIDATE_ARGS[1] is CONTAINER)199        self.assertEqual(new_policy.VALIDATE_ARGS[2], NAME)200        self.failUnless(new_policy.VALIDATE_ARGS[3] is VALUE)201        self.failUnless(new_policy.VALIDATE_ARGS[4] is context)202class PythonSecurityManagerTests(SecurityManagerTestBase,203                                 ISecurityManagerConformance,204                                ):205    def _getTargetClass(self):206        from AccessControl.ImplPython import SecurityManager207        return SecurityManager208# N.B.:  The C version mixes in the Python version, which is why we209#        can test for conformance to ISecurityManager.210class C_SecurityManagerTests(SecurityManagerTestBase,211                             ISecurityManagerConformance,212                            ):213    def _getTargetClass(self):214        from AccessControl.ImplC import SecurityManager215        return SecurityManager216def test_suite():217    suite = unittest.TestSuite()218    suite.addTest(unittest.makeSuite(PythonSecurityManagerTests))219    suite.addTest(unittest.makeSuite(C_SecurityManagerTests))...test_commandline.py
Source:test_commandline.py  
...8    # These tests check validate_args method of the commandline class9    def test_validate_noport_nopass(self):10        input = []11        with pytest.raises(VaultError):12            commandline.validate_args(input)13    def test_validate_lowport_nopass(self):14        input = [10]15        with pytest.raises(VaultError):16            commandline.validate_args(input)17    def test_validate_highport_nopass(self):18        input = [100000]19        with pytest.raises(VaultError):20            commandline.validate_args(input)21    def test_validate_octalport_nopass(self):22        input = [oct(64)]23        with pytest.raises(VaultError):24            commandline.validate_args(input)25    def test_validate_hexport_nopass(self):26        input = ['0x400']27        with pytest.raises(VaultError):28            commandline.validate_args(input)29    def test_validate_noport_withpass(self):30        input = ['admin']31        with pytest.raises(VaultError):32            commandline.validate_args(input)33    def test_validate_port_nopass(self):34        input = [1024]35        assert commandline.validate_args(input) == [1024, 'admin']36    def test_validate_port_goodpass(self):37        input = [1024, 'password']38        assert commandline.validate_args(input) == [1024, 'password']39    def test_validate_port_invalidpass(self):40        input = [1024, '~%password']41        with pytest.raises(VaultError):42            commandline.validate_args(input)43    def test_validate_port_invalidpass_longer(self):44        input = [1024, 'password~!@#$%^&*()_+=']45        with pytest.raises(VaultError):46            commandline.validate_args(input)47    def test_validate_port_longpass(self):48        password = ''.join(random.choice(string.ascii_letters + string.digits) for x in range(65535 + 1))49        input = [1024, password]50        with pytest.raises(VaultError):51            commandline.validate_args(input)52    # TestArg153    def test_validate_port_testarg1(self):54        input = ['06300']55        with pytest.raises(VaultError):56            commandline.validate_args(input)57    # TestArg258    def test_validate_port_testarg2(self):59        input = ['014234']60        with pytest.raises(VaultError):61            commandline.validate_args(input)62    # TestArg363    def test_validate_port_testarg3(self):64        input = ['0x189C']65        with pytest.raises(VaultError):66            commandline.validate_args(input)67    # TestArg468    def test_validate_port_testarg4(self):69        input = [' 6300']70        with pytest.raises(VaultError):71            commandline.validate_args(input)72    # TestArg573    def test_validate_port_testarg5(self):74        input = ['6300 ']75        with pytest.raises(VaultError):76            commandline.validate_args(input)77    # TestArg678    def test_validate_port_testarg6(self):79        input = ['1023']80        with pytest.raises(VaultError):81            commandline.validate_args(input)82    # TestArg783    def test_validate_port_testarg7(self):84        input = ['65536']85        with pytest.raises(VaultError):...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!!
