How to use _getattr method in locust

Best Python code snippet using locust

field.py

Source:field.py Github

copy

Full Screen

...44 self.mimeTypes = mimeTypes45 except:46 raise ValueError('Mime type format error.', mimeTypes)47 def get(self, object, _getattr=getattr, _setattr=setattr):48 value = _getattr(object, self.__name__, None)49 if IFile.providedBy(value):50 return value51 elif value is None:52 value = u''53 self.set(object, value, _getattr, _setattr)54 return self.get(object, _getattr, _setattr)55 raise AttributeError(self.__name__)56 def set(self, object, value, _getattr=getattr, _setattr=setattr):57 if self.readonly:58 raise TypeError("Can't set values on read-only fields "59 "(name=%s, class=%s.%s)"60 % (self.__name__,61 object.__class__.__module__,62 object.__class__.__name__))63 if IFile.providedBy(value):64 _setattr(object, self.__name__, value)65 elif IFileData.providedBy(value):66 data = _getattr(object, self.__name__, None)67 if not IFile.providedBy(data):68 data = File()69 data.data = value.data70 data.mimeType = value.mimeType71 data.filename = value.filename72 data.disablePreview = value.disablePreview73 data.disablePrint = value.disablePrint74 _setattr(object, self.__name__, data)75 else:76 data = removeSecurityProxy(data)77 data.data = value.data78 data.mimeType = value.mimeType79 data.filename = value.filename80 data.disablePreview = value.disablePreview81 data.disablePrint = value.disablePrint82 _setattr(object, self.__name__, data)83 notify(ObjectModifiedEvent(data))84 elif IFileDataClear.providedBy(value):85 data = _getattr(object, self.__name__, None)86 if IFile.providedBy(data):87 data.clear()88 else:89 _setattr(object, self.__name__, File())90 elif IFileDataNoValue.providedBy(value):91 pass92 else:93 self.set(object, FileData(value), _getattr, _setattr)94 def _validate(self, value):95 if (IFileDataNoValue.providedBy(value) or not value) and self.required:96 raise RequiredMissing()97 if IFileDataClear.providedBy(value) or \98 IFileDataNoValue.providedBy(value):99 return100 super(FileField, self)._validate(value)101 if not (IFile.providedBy(value) or IFileData.providedBy(value)):102 raise WrongType(value, (IFile, IFileData))103 if self.mimeTypes:104 mt1, mt2 = value.mimeType.split('/')105 sec = self.mts.get(mt1)106 if sec:107 if '*' in sec:108 return109 elif mt2 in sec:110 return111 raise NotAllowedFileType(value.mimeType, self.mimeTypes)112class ImageField(schema.MinMaxLen, schema.Field):113 interface.implements(IBlobDataField, IImageField)114 mimeTypes = ('image/jpeg', 'image/gif', 'image/png')115 def __init__(self, scale=False, maxWidth=0, maxHeight=0, **kw):116 super(ImageField, self).__init__(**kw)117 self.scale = scale118 self.maxWidth = maxWidth119 self.maxHeight = maxHeight120 def get(self, object, _getattr=getattr, _setattr=setattr):121 value = _getattr(object, self.__name__, None)122 if IImage.providedBy(value):123 return value124 elif IFile.providedBy(value):125 self.set(object, value.data, _getattr, _setattr)126 return self.get(object, _getattr, _setattr)127 elif value is None:128 value = u''129 self.set(object, value, _getattr, _setattr)130 return self.get(object, _getattr, _setattr)131 def set(self, object, value, _getattr=getattr, _setattr=setattr):132 if self.readonly:133 raise TypeError("Can't set values on read-only fields "134 "(name=%s, class=%s.%s)"135 % (self.__name__,136 object.__class__.__module__,137 object.__class__.__name__))138 if IImage.providedBy(value):139 _setattr(object, self.__name__, value)140 elif IFileData.providedBy(value):141 data = _getattr(object, self.__name__, None)142 if not IImage.providedBy(data):143 data = Image()144 data.data = value.data145 data.mimeType = value.mimeType146 data.filename = value.filename147 _setattr(object, self.__name__, data)148 else:149 data = removeSecurityProxy(data)150 data.data = value.data151 data.mimeType = value.mimeType152 data.filename = value.filename153 _setattr(object, self.__name__, data)154 if self.scale and (self.maxWidth < data.width or155 self.maxHeight < data.height):156 data.scale(self.maxWidth, self.maxHeight)157 elif IFileDataClear.providedBy(value):158 data = _getattr(object, self.__name__, None)159 if IImage.providedBy(data):160 data.clear()161 else:162 _setattr(object, self.__name__, Image())163 elif IFileDataNoValue.providedBy(value):164 pass165 else:166 self.set(object, FileData(value), _getattr, _setattr)167 def _validate(self, value):168 if (IFileDataNoValue.providedBy(value) or not value) and self.required:169 raise RequiredMissing()170 if IFileDataClear.providedBy(value) or \171 IFileDataNoValue.providedBy(value):172 return173 super(ImageField, self)._validate(value)174 if not (IImage.providedBy(value) or IFileData.providedBy(value)):175 raise WrongType(value, (IImage, IFileData))176 if self.mimeTypes:177 if value.mimeType not in self.mimeTypes:178 raise NotAllowedFileType(value.mimeType, self.mimeTypes)179 if not self.scale and (self.maxWidth > 0 or self.maxHeight > 0):180 if IImage.providedBy(value):181 width, height = value.width, value.height182 else:183 width, height = getImageSize(value.data)184 if self.maxWidth > 0 and width > self.maxWidth:185 raise ImageDimensionExceeded(width, ('width', self.maxWidth))186 if self.maxHeight > 0 and height > self.maxHeight:187 raise ImageDimensionExceeded(188 height, ('height', self.maxHeight))189class FileFieldProperty(object):190 def __init__(self, field, name=None):191 if name is None:192 name = field.__name__193 self.__field = field194 self.__name = name195 def __get__(self, inst, klass):196 if inst is None:197 return self198 try:199 value = self.__field.get(inst, self.__getattr, self.__setattr)200 except AttributeError:201 value = _marker202 if value is _marker:203 field = self.__field.bind(inst)204 value = getattr(field, 'default', _marker)205 if value is _marker:206 raise AttributeError(self.__name)207 return value208 def __set__(self, inst, value):209 field = self.__field.bind(inst)210 field.validate(value)211 if field.readonly and inst.__dict__.has_key(self.__name):212 raise ValueError(self.__name, 'field is readonly')213 self.__field.set(inst, value, self.__getattr, self.__setattr)214 def __delete__(self, inst):215 if self.__name in inst.__dict__:216 del inst.__dict__[self.__name]217 def __getattr(self, object, name, default=None):218 return removeSecurityProxy(object).__dict__.get(name, default)219 def __setattr(self, object, name, value):220 removeSecurityProxy(object).__dict__[name] = value221 if IPersistent.providedBy(object):...

Full Screen

Full Screen

_Base.py.svn-base

Source:_Base.py.svn-base Github

copy

Full Screen

1#!/usr/bin/env python3.22######################################################################3# Copyright (c)2011-2012, David L. Armstrong.4#5# test/P4OO._Base.py6#7# See COPYRIGHT AND LICENSE section below for usage8# and distribution rights.9#10######################################################################11#NAME / DESCRIPTION12'''13Perforce _Base unittest Class14'''15######################################################################16# Include Paths17#18import sys19sys.path.append('../lib')20# For P4Python21sys.path.append('/Users/armstd/p4/Davids-MacBook-Air/projects/infra/main/site-python/Darwin-11.0.1-x86_64/python3.2')22######################################################################23# Includes24#25# P4OO._Base brings in our Exception hierarchy26import P4OO._Base27import unittest28######################################################################29# P4Python Class Initialization30#31#class TestSequenceFunctions(unittest.TestCase):32class TestP4OO_Base(unittest.TestCase):33 def setUp(self):34# self.seq = range(10)35 pass36 def tearDown(self):37# self.widget.dispose()38# self.widget = None39 pass40 # Test an object instantiated with no attributes41 def test_initEmpty(self):42 testObj1 = P4OO._Base._P4OOBase()43 self.assertTrue(isinstance(testObj1, P4OO._Base._P4OOBase))44 self.assertEqual(testObj1._getAttr("foo"), None, "_getAttr for non-existing attribute returns None")45 self.assertEqual(testObj1._getAttr("foo"), None, "subsequent _getAttr for non-existing attribute also returns None")46 self.assertEqual(testObj1._setAttr("foo", "bar"), "bar", "_setAttr for new attribute returns value")47 self.assertEqual(testObj1._getAttr("foo"), "bar", "_getAttr for existing attribute returns value")48 self.assertEqual(testObj1._setAttr("foo", "baz"), "baz", "_setAttr for existing attribute returns value")49 self.assertEqual(testObj1._getAttr("foo"), "baz", "_getAttr for changed attribute returns new value")50 self.assertEqual(testObj1._delAttr("foo"), "baz", "_delAttr returns value of attribute")51 self.assertEqual(testObj1._getAttr("foo"), None, "_getAttr for non-existing attribute returns None")52 self.assertEqual(testObj1._delAttr("foo"), None, "_delAttr returns nothing for non-existant attribute")53 def test_init1Attr(self):54 testObj1 = P4OO._Base._P4OOBase(foo="bar")55 self.assertTrue(isinstance(testObj1, P4OO._Base._P4OOBase))56 self.assertEqual(testObj1._getAttr("foo"), "bar", "_getAttr for existing attribute returns value")57 self.assertEqual(testObj1._setAttr("foo", "baz"), "baz", "_setAttr for existing attribute returns value")58 self.assertEqual(testObj1._getAttr("foo"), "baz", "_getAttr for changed attribute returns new value")59 def test_basicMethods(self):60 testObj1 = P4OO._Base._P4OOBase()61 self.assertEqual(testObj1._uniqueID(), id(testObj1), "default _uniqueID returns id() value")62 self.assertEqual(testObj1._initialize(), 1, "default _initialize returns 1")63# def test_Exceptions(self):64# pass65# def test_P4Connection(self):66# pass67if __name__ == '__main__':68 unittest.main()69######################################################################70# Standard authorship and copyright for documentation71#72# AUTHOR73#74# David L. Armstrong <armstd@cpan.org>75#76# COPYRIGHT AND LICENSE77#78# Copyright (c)2011-2012, David L. Armstrong.79#80# This module is distributed under the terms of the Artistic License81# 2.0. For more details, see the full text of the license in the file82# LICENSE.83#84# SUPPORT AND WARRANTY85#86# This program is distributed in the hope that it will be87# useful, but it is provided "as is" and without any expressed88# or implied warranties....

Full Screen

Full Screen

security_in_syntax.py

Source:security_in_syntax.py Github

copy

Full Screen

1# These are all supposed to raise a SyntaxError when using2# compile_restricted() but not when using compile().3# Each function in this module is compiled using compile_restricted().4def overrideGuardWithFunction():5 def _getattr(o): return o6def overrideGuardWithLambda():7 lambda o, _getattr=None: o8def overrideGuardWithClass():9 class _getattr:10 pass11def overrideGuardWithName():12 _getattr = None13def overrideGuardWithArgument():14 def f(_getattr=None):15 pass16def reserved_names():17 printed = ''18def bad_name():19 __ = 12...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run locust automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful