本文整理汇总了Python中collections.UserDict方法的典型用法代码示例。如果您正苦于以下问题:Python collections.UserDict方法的具体用法?Python collections.UserDict怎么用?Python collections.UserDict使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类collections
的用法示例。
在下文中一共展示了collections.UserDict方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_extract_auth_params
# 需要导入模块: import collections [as 别名]
# 或者: from collections import UserDict [as 别名]
def test_extract_auth_params():
request = UserDict()
request.headers = {}
assert _extract_auth_params(request) is None
request.headers = {'Authorization': 'no-space'}
with pytest.raises(InvalidAuthParameters):
_extract_auth_params(request)
request.headers = {'Authorization': ('BadAuthType signMethod=HMAC-SHA256,'
'credential=fake-ak:fake-sig')}
with pytest.raises(InvalidAuthParameters):
_extract_auth_params(request)
request.headers = {'Authorization': ('BackendAI signMethod=HMAC-SHA256,'
'credential=fake-ak:fake-sig')}
ret = _extract_auth_params(request)
assert ret[0] == 'HMAC-SHA256'
assert ret[1] == 'fake-ak'
assert ret[2] == 'fake-sig'
示例2: test_custom_asserts
# 需要导入模块: import collections [as 别名]
# 或者: from collections import UserDict [as 别名]
def test_custom_asserts(self):
# This would always trigger the KeyError from trying to put
# an array of equal-length UserDicts inside an ndarray.
data = JSONArray([collections.UserDict({'a': 1}),
collections.UserDict({'b': 2}),
collections.UserDict({'c': 3})])
a = pd.Series(data)
self.assert_series_equal(a, a)
self.assert_frame_equal(a.to_frame(), a.to_frame())
b = pd.Series(data.take([0, 0, 1]))
with pytest.raises(AssertionError):
self.assert_series_equal(a, b)
with pytest.raises(AssertionError):
self.assert_frame_equal(a.to_frame(), b.to_frame())
示例3: test_MutableMapping_subclass
# 需要导入模块: import collections [as 别名]
# 或者: from collections import UserDict [as 别名]
def test_MutableMapping_subclass(self):
# Test issue 9214
mymap = UserDict()
mymap['red'] = 5
self.assertIsInstance(mymap.keys(), Set)
self.assertIsInstance(mymap.keys(), KeysView)
self.assertIsInstance(mymap.items(), Set)
self.assertIsInstance(mymap.items(), ItemsView)
mymap = UserDict()
mymap['red'] = 5
z = mymap.keys() | {'orange'}
self.assertIsInstance(z, set)
list(z)
mymap['blue'] = 7 # Shouldn't affect 'z'
self.assertEqual(sorted(z), ['orange', 'red'])
mymap = UserDict()
mymap['red'] = 5
z = mymap.items() | {('orange', 3)}
self.assertIsInstance(z, set)
list(z)
mymap['blue'] = 7 # Shouldn't affect 'z'
self.assertEqual(sorted(z), [('orange', 3), ('red', 5)])
示例4: test_compat_pickle
# 需要导入模块: import collections [as 别名]
# 或者: from collections import UserDict [as 别名]
def test_compat_pickle(self):
tests = [
(range(1, 7), '__builtin__', 'xrange'),
(map(int, '123'), 'itertools', 'imap'),
(functools.reduce, '__builtin__', 'reduce'),
(dbm.whichdb, 'whichdb', 'whichdb'),
(Exception(), 'exceptions', 'Exception'),
(collections.UserDict(), 'UserDict', 'IterableUserDict'),
(collections.UserList(), 'UserList', 'UserList'),
(collections.defaultdict(), 'collections', 'defaultdict'),
]
for val, mod, name in tests:
for proto in range(3):
with self.subTest(type=type(val), proto=proto):
pickled = self.dumps(val, proto)
self.assertIn(('c%s\n%s' % (mod, name)).encode(), pickled)
self.assertIs(type(self.loads(pickled)), type(val))
示例5: test_plain
# 需要导入模块: import collections [as 别名]
# 或者: from collections import UserDict [as 别名]
def test_plain(self):
f = self.makeCallable('a, b=1')
self.assertEqualCallArgs(f, '2')
self.assertEqualCallArgs(f, '2, 3')
self.assertEqualCallArgs(f, 'a=2')
self.assertEqualCallArgs(f, 'b=3, a=2')
self.assertEqualCallArgs(f, '2, b=3')
# expand *iterable / **mapping
self.assertEqualCallArgs(f, '*(2,)')
self.assertEqualCallArgs(f, '*[2]')
self.assertEqualCallArgs(f, '*(2, 3)')
self.assertEqualCallArgs(f, '*[2, 3]')
self.assertEqualCallArgs(f, '**{"a":2}')
self.assertEqualCallArgs(f, 'b=3, **{"a":2}')
self.assertEqualCallArgs(f, '2, **{"b":3}')
self.assertEqualCallArgs(f, '**{"b":3, "a":2}')
# expand UserList / UserDict
self.assertEqualCallArgs(f, '*collections.UserList([2])')
self.assertEqualCallArgs(f, '*collections.UserList([2, 3])')
self.assertEqualCallArgs(f, '**collections.UserDict(a=2)')
self.assertEqualCallArgs(f, '2, **collections.UserDict(b=3)')
self.assertEqualCallArgs(f, 'b=2, **collections.UserDict(a=3)')
示例6: test_multiple_features
# 需要导入模块: import collections [as 别名]
# 或者: from collections import UserDict [as 别名]
def test_multiple_features(self):
f = self.makeCallable('a, b=2, *f, **g')
self.assertEqualCallArgs(f, '2, 3, 7')
self.assertEqualCallArgs(f, '2, 3, x=8')
self.assertEqualCallArgs(f, '2, 3, x=8, *[(4,[5,6]), 7]')
self.assertEqualCallArgs(f, '2, x=8, *[3, (4,[5,6]), 7], y=9')
self.assertEqualCallArgs(f, 'x=8, *[2, 3, (4,[5,6])], y=9')
self.assertEqualCallArgs(f, 'x=8, *collections.UserList('
'[2, 3, (4,[5,6])]), **{"y":9, "z":10}')
self.assertEqualCallArgs(f, '2, x=8, *collections.UserList([3, '
'(4,[5,6])]), **collections.UserDict('
'y=9, z=10)')
f = self.makeCallable('a, b=2, *f, x, y=99, **g')
self.assertEqualCallArgs(f, '2, 3, x=8')
self.assertEqualCallArgs(f, '2, 3, x=8, *[(4,[5,6]), 7]')
self.assertEqualCallArgs(f, '2, x=8, *[3, (4,[5,6]), 7], y=9, z=10')
self.assertEqualCallArgs(f, 'x=8, *[2, 3, (4,[5,6])], y=9, z=10')
self.assertEqualCallArgs(f, 'x=8, *collections.UserList('
'[2, 3, (4,[5,6])]), q=0, **{"y":9, "z":10}')
self.assertEqualCallArgs(f, '2, x=8, *collections.UserList([3, '
'(4,[5,6])]), q=0, **collections.UserDict('
'y=9, z=10)')
示例7: test_avoid_infinite_retries
# 需要导入模块: import collections [as 别名]
# 或者: from collections import UserDict [as 别名]
def test_avoid_infinite_retries(capsys):
now = datetime.now(timezone.utc)
with patch('main.datetime', wraps=datetime) as datetime_mock:
datetime_mock.now = Mock(return_value=now)
old_context = UserDict()
old_context.timestamp = (now - timedelta(seconds=15)).isoformat()
old_context.event_id = 'old_event_id'
young_context = UserDict()
young_context.timestamp = (now - timedelta(seconds=5)).isoformat()
young_context.event_id = 'young_event_id'
main.avoid_infinite_retries(None, old_context)
out, _ = capsys.readouterr()
assert f"Dropped {old_context.event_id} (age 15000.0ms)" in out
main.avoid_infinite_retries(None, young_context)
out, _ = capsys.readouterr()
assert f"Processed {young_context.event_id} (age 5000.0ms)" in out
示例8: test_process_offensive_image
# 需要导入模块: import collections [as 别名]
# 或者: from collections import UserDict [as 别名]
def test_process_offensive_image(
storage_client,
vision_client,
__blur_image,
capsys):
result = UserDict()
result.safe_search_annotation = UserDict()
result.safe_search_annotation.adult = 5
result.safe_search_annotation.violence = 5
vision_client.safe_search_detection = MagicMock(return_value=result)
filename = str(uuid.uuid4())
data = {
'bucket': 'my-bucket',
'name': filename
}
main.blur_offensive_images(data, None)
out, _ = capsys.readouterr()
assert 'Analyzing %s.' % filename in out
assert 'The image %s was detected as inappropriate.' % filename in out
assert main.__blur_image.called
示例9: test_process_safe_image
# 需要导入模块: import collections [as 别名]
# 或者: from collections import UserDict [as 别名]
def test_process_safe_image(
storage_client,
vision_client,
__blur_image,
capsys):
result = UserDict()
result.safe_search_annotation = UserDict()
result.safe_search_annotation.adult = 1
result.safe_search_annotation.violence = 1
vision_client.safe_search_detection = MagicMock(return_value=result)
filename = str(uuid.uuid4())
data = {
'bucket': 'my-bucket',
'name': filename
}
main.blur_offensive_images(data, None)
out, _ = capsys.readouterr()
assert 'Analyzing %s.' % filename in out
assert 'The image %s was detected as OK.' % filename in out
assert __blur_image.called is False
示例10: test_process_safe_image
# 需要导入模块: import collections [as 别名]
# 或者: from collections import UserDict [as 别名]
def test_process_safe_image(
storage_client,
vision_client,
__blur_image,
capsys):
result = UserDict()
result.safe_search_annotation = UserDict()
result.safe_search_annotation.adult = 1
result.safe_search_annotation.violence = 1
vision_client.safe_search_detection = MagicMock(return_value=result)
filename = str(uuid.uuid4())
data = {
'bucket': 'my-bucket',
'name': filename
}
image.blur_offensive_images(data)
out, _ = capsys.readouterr()
assert 'Analyzing %s.' % filename in out
assert 'The image %s was detected as OK.' % filename in out
assert __blur_image.called is False
示例11: __init__
# 需要导入模块: import collections [as 别名]
# 或者: from collections import UserDict [as 别名]
def __init__(*args, **kwargs):
if not args:
raise TypeError("descriptor '__init__' of 'UserDict' object "
"needs an argument")
self, args = args[0], args[1:]
if len(args) > 1:
raise TypeError('expected at most 1 arguments, got %d' % len(args))
if args:
dict = args[0]
elif 'dict' in kwargs:
dict = kwargs.pop('dict')
import warnings
warnings.warn("Passing 'dict' as keyword argument is deprecated",
DeprecationWarning, stacklevel=2)
else:
dict = None
self.data = {}
if dict is not None:
self.update(dict)
if len(kwargs):
self.update(kwargs)
示例12: test_redirects_to_https_with_port_number
# 需要导入模块: import collections [as 别名]
# 或者: from collections import UserDict [as 别名]
def test_redirects_to_https_with_port_number(self):
# same thing, but with :443 on the URL, see issue #180
self.reqs['responses']['http'].url = 'https://http-observatory.security.mozilla.org:443/'
history1 = UserDict()
history1.request = UserDict()
history1.request.url = 'http://http-observatory.security.mozilla.org/'
self.reqs['responses']['http'].history.append(history1)
result = redirection(self.reqs)
self.assertEquals('redirection-to-https', result['result'])
self.assertEquals(['http://http-observatory.security.mozilla.org/',
'https://http-observatory.security.mozilla.org:443/'], result['route'])
self.assertTrue(result['pass'])
示例13: test_first_redirection_still_http
# 需要导入模块: import collections [as 别名]
# 或者: from collections import UserDict [as 别名]
def test_first_redirection_still_http(self):
self.reqs['responses']['http'].url = 'https://http-observatory.security.mozilla.org/foo'
history1 = UserDict()
history1.request = UserDict()
history1.request.url = 'http://http-observatory.security.mozilla.org/'
history2 = UserDict()
history2.request = UserDict()
history2.request.url = 'http://http-observatory.security.mozilla.org/foo'
self.reqs['responses']['http'].history.append(history1)
self.reqs['responses']['http'].history.append(history2)
result = redirection(self.reqs)
self.assertEquals('redirection-not-to-https-on-initial-redirection', result['result'])
self.assertFalse(result['pass'])
示例14: test_all_redirections_preloaded
# 需要导入模块: import collections [as 别名]
# 或者: from collections import UserDict [as 别名]
def test_all_redirections_preloaded(self):
self.reqs['responses']['http'].url = 'https://www.pokeinthe.io/foo/bar'
for url in ('http://pokeinthe.io/',
'https://pokeinthe.io/',
'https://www.pokeinthe.io/',
'https://baz.pokeinthe.io/foo'):
history = UserDict()
history.request = UserDict()
history.request.url = url
self.reqs['responses']['http'].history.append(history)
result = redirection(self.reqs)
self.assertEquals('redirection-all-redirects-preloaded', result['result'])
self.assertTrue(result['pass'])
示例15: _from_factorized
# 需要导入模块: import collections [as 别名]
# 或者: from collections import UserDict [as 别名]
def _from_factorized(cls, values, original):
return cls([collections.UserDict(x) for x in values if x != ()])