def test_advanced_slice(self): _slice = slice(1, 5, 2) sample = deque([ False, _slice, 123, UserDict({}), 5 + 5j, UserDict({ 'haha': 2 + 3j, 'toto': [False, { 'nini': 'xixi' }] }), True ]) output = sj.dumps(sample, cls=sj.AdvancedEncoder) print(output) self.assertEqual( '[false, {"start": 1, "stop": 5, "step": 2}, 123, {}, {"real": 5.0, "imag": 5.0}, {"haha": {"real": 2.0, "imag": 3.0}, "toto": [false, {"nini": "xixi"}]}, true]', output) output2 = sj.loads(output, cls=sj.AdvancedDecoder) print(output2) self.assertEqual([ False, _slice, 123, {}, (5 + 5j), { 'haha': (2 + 3j), 'toto': [False, { 'nini': 'xixi' }] }, True ], output2)
def test_crossfade(var, benchmark): w1, p1, e1 = var dict: UserDict = UserDict() dict["wave"], dict["partition"] = w1, p1 mt1 = MultiTrack(dict) w2 = Wave(value=np.arange(0, 50), fs=1) p2 = Partition( np.array([0, 15, 20, 50], dtype=np.int64), np.array(["start", "middle", "end"]), 1, ) dict = UserDict() dict["wave"], dict["partition"] = w2, p2 mt2 = MultiTrack(dict) mt3 = benchmark(mt1.crossfade, mt2, 5) assert mt3.duration == mt1.duration + mt2.duration - 5 assert mt3["wave"] == Wave( value=np.r_[np.arange(45), np.array([37, 31, 24, 18, 11]), np.arange(5, 50)], fs=1, ) assert mt3["partition"] == Partition( np.array([0, 15, 20, 48, 60, 65, 95], dtype=np.int64), np.array(["start", "middle", "end", "start", "middle", "end"]), 1, )
def test_list_of_userdicts(): "Input: a list of UserDicts." lod = [UserDict(foo=1, bar=2), UserDict(foo=3, bar=4)] expected1 = "\n".join(["- -", "1 2", "3 4", "- -"]) expected2 = "\n".join(["- -", "2 1", "4 3", "- -"]) result = tabulate(lod) assert_in(result, [expected1, expected2])
async def test_decorator_limiter_called_correctly(self, limit_class): app = Application() routes = RouteTableDef() async def test_view(request): return json_response("bad", status=400) async def test_view2(request): return json_response("bad", status=429) @routes.get("/") @aiopylimit("root_view", (60, 1), limit_reached_view=test_view) async def test(request): return json_response({"test": True}) app.add_routes(routes) request = UserDict() request.app = UserDict() request.app['limit_global_namespace_prefix'] = "aiohttp" request.app['limit_key_func'] = lambda x: "key" request.app['limit_reached_view'] = test_view2 ret = await test(request) limit_class.assert_called_once_with(60, 1) self.assertEqual(ret.status, 400)
def test_advanced_dumps(self): sample = UserList([ False, 123, UserDict({}), 5 + 5j, UserDict({ 'haha': 2 + 3j, 'toto': [False, { 'nini': 'xixi' }] }), True ]) self.assertEqual( '[\r\n false,\r\n 123,\r\n {},\r\n {\r\n "real": 5.0,\r\n ' ' "imag": 5.0\r\n },\r\n {\r\n "haha": {\r\n "real": 2.0,\r\n ' ' "imag": 3.0\r\n },\r\n "toto": [\r\n false,\r\n ' ' {\r\n "nini": "xixi"\r\n }\r\n ]\r\n },\r\n ' ' true\r\n]', sj.dumps(sample, cls=sj.AdvancedEncoder, indent=4)) sample = UserList([ False, datetime.now().date(), 123, UserDict({}), 5 + 5j, UserDict({ 'haha': 2 + 3j, 'toto': [False, { 'nini': 'xixi' }] }), True ]) haha = sj.dumps(sample, cls=sj.AdvancedEncoder, indent=4) print(haha) hhh = sj.loads(haha, cls=sj.AdvancedDecoder) print(hhh)
def test_blur_image(storage_client, image_mock, os_mock, capsys): filename = str(uuid.uuid4()) blur_bucket = 'blurred-bucket-' + str(uuid.uuid4()) os_mock.remove = MagicMock() os_mock.path = MagicMock() os_mock.path.basename = MagicMock(side_effect=(lambda x: x)) os_mock.getenv = MagicMock(return_value=blur_bucket) image_mock.return_value = image_mock image_mock.__enter__.return_value = image_mock blob = UserDict() blob.name = filename blob.bucket = UserDict() blob.download_to_filename = MagicMock() blob.upload_from_filename = MagicMock() image.__blur_image(blob) out, _ = capsys.readouterr() assert f'Image {filename} was downloaded to' in out assert f'Image {filename} was blurred.' in out assert f'Blurred image uploaded to: gs://{blur_bucket}/{filename}' in out assert os_mock.remove.called assert image_mock.resize.called
def test_advanced_complex(self): sample = UserList([ False, 123, UserDict({}), 5 + 5j, UserDict({ 'haha': 2 + 3j, 'toto': [False, { 'nini': 'xixi' }] }), True ]) output1 = sj.dumps(sample, cls=sj.AdvancedEncoder, indent=2) sample = deque([ False, 123, UserDict({}), 5 + 5j, UserDict({ 'haha': 2 + 3j, 'toto': [False, { 'nini': 'xixi' }] }), True ]) output2 = sj.dumps(sample, cls=sj.AdvancedEncoder, indent=2) self.assertEqual(output1, output2) output3 = sj.loads(output1, cls=sj.AdvancedDecoder) print(output3) self.assertEqual([ False, 123, {}, (5 + 5j), { 'haha': (2 + 3j), 'toto': [False, { 'nini': 'xixi' }] }, True ], output3) self.assertTrue(isinstance(output3[3], complex))
def test_advanced_uuid(self): _uuid = uuid.uuid1() sample = deque([ False, _uuid, 123, UserDict({}), 5 + 5j, UserDict({ 'haha': 2 + 3j, 'toto': [False, { 'nini': 'xixi' }] }), True ]) output = sj.dumps(sample, cls=sj.AdvancedEncoder) print(output) self.assertEqual( '[false, "%s", 123, {}, {"real": 5.0, "imag": 5.0}, {"haha": {"real": 2.0, "imag": 3.0}, "toto": [false, {"nini": "xixi"}]}, true]' % _uuid, output) output2 = sj.loads(output, cls=sj.AdvancedDecoder) print(output2) self.assertEqual([ False, _uuid, 123, {}, (5 + 5j), { 'haha': (2 + 3j), 'toto': [False, { 'nini': 'xixi' }] }, True ], output2)
def test_blur_image(image_mock, os_mock, capsys): filename = str(uuid.uuid4()) os_mock.remove = MagicMock() os_mock.path = MagicMock() os_mock.path.basename = MagicMock(side_effect=(lambda x: x)) image_mock.return_value = image_mock image_mock.__enter__.return_value = image_mock blob = UserDict() blob.name = filename blob.bucket = UserDict() blob.bucket.blob = MagicMock(return_value=blob) blob.download_to_filename = MagicMock() blob.upload_from_filename = MagicMock() main.__blur_image(blob) out, _ = capsys.readouterr() assert f'Image {filename} was downloaded to' in out assert f'Image {filename} was blurred.' in out assert f'Blurred image was uploaded to blurred-{filename}.' in out assert os_mock.remove.called assert image_mock.resize.called
def test_list_of_userdicts_keys(): "Input: a list of UserDicts." lod = [UserDict(foo=1, bar=2), UserDict(foo=3, bar=4)] expected1 = "\n".join( [" foo bar", "----- -----", " 1 2", " 3 4"]) expected2 = "\n".join( [" bar foo", "----- -----", " 2 1", " 4 3"]) result = tabulate(lod, headers="keys") assert_in(result, [expected1, expected2])
def empty_requests(http_equiv_file=None) -> dict: req = { 'hostname': 'http-observatory.security.mozilla.org', 'resources': { '__path__': None, '/': None, '/clientaccesspolicy.xml': None, '/contribute.json': None, '/crossdomain.xml': None, '/robots.txt': None, }, 'responses': { 'auto': UserDict(), 'cors': None, 'http': None, 'https': None, }, 'session': UserDict(), } # Parse the HTML file for its own headers, if requested if http_equiv_file: __dirname = os.path.abspath(os.path.dirname(__file__)) with open( os.path.join(__dirname, 'unittests', 'files', http_equiv_file), 'r') as f: html = f.read() # Load the HTML file into the object for content tests. req['resources']['__path__'] = html req['responses']['auto'].headers = { 'Content-Type': 'text/html', } req['responses']['auto'].history = [] req['responses']['auto'].request = UserDict() req['responses']['auto'].request.headers = UserDict() req['responses']['auto'].status_code = 200 req['responses'][ 'auto'].url = 'https://http-observatory.security.mozilla.org/' req['responses']['auto'].verified = True req['session'].cookies = RequestsCookieJar() req['responses']['cors'] = deepcopy(req['responses']['auto']) req['responses']['http'] = deepcopy(req['responses']['auto']) req['responses']['https'] = deepcopy(req['responses']['auto']) # Parse the HTML file for its own headers, if requested if http_equiv_file: req['responses']['auto'].http_equiv = parse_http_equiv_headers( req['resources']['__path__']) else: req['responses']['auto'].http_equiv = {} return req
def __init__(self, id, module, transform=None): self.id = id self.module = module # DM 2004-09-09: 'Transform' instances are stored as # part of a module level configuration structure # Therefore, they must not contain persistent objects self._config = UserDict() self._config.__allow_access_to_unprotected_subobjects__ = 1 self._config_metadata = UserDict() self._tr_init(1, transform)
def test_redirection_not_to_https(self): self.reqs['responses']['http'].url = 'http://http-observatory.services.mozilla.com/foo' history1 = UserDict() history1.request = UserDict() history1.request.url = 'http://http-observatory.services.mozilla.com/' self.reqs['responses']['http'].history.append(history1) result = redirection(self.reqs) self.assertEquals('redirection-not-to-https', result['result']) self.assertFalse(result['pass'])
def test_redirects_to_https(self): history1 = UserDict() history1.request = UserDict() history1.request.url = 'http://http-observatory.services.mozilla.com/' self.reqs['responses']['http'].history.append(history1) result = redirection(self.reqs) self.assertEquals('redirection-to-https', result['result']) self.assertEquals(['http://http-observatory.services.mozilla.com/', 'https://http-observatory.services.mozilla.com/'], result['route']) self.assertTrue(result['pass'])
def setUp(self): self.opts = UserDict() self.opts.filename = os.path.join( os.path.dirname(__file__), 'samples/ubuntu-11.10-x86-0xbaf.bin') self.opts.interface = 'file' self.opts.dry_run = True self.opts.size = None self.opts.address = None self.opts.verbose = None self.opts.list_targets = None self.tests = None self.module = UserDict() self.module.IS_INTRUSIVE = False
def test_redirects_invalid_cert(self): history1 = UserDict() history1.request = UserDict() history1.request.url = 'http://http-observatory.security.mozilla.org/' self.reqs['responses']['http'].history.append(history1) # Mark it as verification failed self.reqs['responses']['http'].verified = False result = redirection(self.reqs) self.assertEquals('redirection-invalid-cert', result['result']) self.assertFalse(result['pass'])
def test_first_redirection_off_host(self): self.reqs['responses']['http'].url = 'https://http-foo.services.mozilla.com/' history1 = UserDict() history1.status_code = 301 history1.request = UserDict() history1.request.url = 'http://http-observatory.services.mozilla.com/' self.reqs['responses']['http'].history.append(history1) result = redirection(self.reqs) self.assertEquals('redirection-off-host-from-http', result['result']) self.assertFalse(result['pass'])
def __init__(self, filename='synonyms.txt', file_sep=';'): pattern = re.compile(r'(?P<key>\w+)\s*[-|—](?P<vals>.+)') self.filename = filename self.file_sep = file_sep self.synonyms_dict = UserDict() with open(filename, 'r') as f: synonyms = f.read() result = Synonyms.PATTERN.findall(synonyms) # collapse a list of tuples name_set = set([i[0] for i in result]) tup_idx = [(i[0], idx) for idx, i in enumerate(result)] result_ = [] for i in name_set: res = list(filter(lambda item: item[0] == i, tup_idx)) ''' result[idx[1]] = ('word1', ' synonym11; synonym12; synonym13;') result[idx[1]][1] = ' synonym11; synonym12; synonym13;' ''' all_syn = [result[idx[1]][1] for idx in res] result_.append((i, ''.join(all_syn))) self.synonyms_dict = { item[0].lower(): self.make_values(item[1]) for item in result_ }
def read_all(path, nodes='nodes.csv', links='links.csv'): nodes_path = os.path.join(path, nodes) links_path = os.path.join(path, links) Graph = {} Names = {} for number, name in read_nodes(nodes_path): Node = UserDict({ 'number': number, 'name': name, 'neighbours': dict(), 'prohibited': set() }) Graph[number] = Node if name in Names: raise CorruptedData('{0} already exists {1}'.format(name, number))# собственное исключение + операция форматирования строки Names[name] = Node for n1, n2, penalty in read_links(links_path): Node1 = Graph[n1] Node2 = Graph[n2] Node1['neighbours'][n2] = (penalty, weakref.ref(Node2)) Node2['neighbours'][n1] = (penalty, weakref.ref(Node1)) return Graph, Names
def test_advanced_datetime(self): dt = datetime(year=2017, month=11, day=20, hour=10, minute=53, second=22, tzinfo=timezone(timedelta(-1, 68400))) b = UserDict({'haha': 2 + 3j, 'toto': [False, dt]}) c = sj.dumps(b, cls=sj.AdvancedEncoder) self.assertEqual( '{"haha": {"real": 2.0, "imag": 3.0}, "toto": [false, "2017-11-20T10:53:22-05:00"]}', c) print(c) d = sj.loads(c, cls=sj.AdvancedDecoder) self.assertEqual( { 'haha': (2 + 3j), 'toto': [ False, datetime(2017, 11, 20, 10, 53, 22, tzinfo=timezone(timedelta(-1, 68400))) ] }, d) print(d)
def dictize(obj): """A helper method, to make namedtuple-like object accessible as dict. If the object is namedtuple-like, its _asdict() form is returned, but in the returned object __getitem__ method is wrapped to dictize also any items returned. If the object does not have _asdict, it will be returned without any change. Integer keys still access the object as tuple. A more useful version would be to keep obj mostly as a namedtuple, just add getitem for string keys. Unfortunately, namedtuple inherits from tuple, including its read-only __getitem__ attribute, so we cannot monkey-patch it. TODO: Create a proxy for named tuple to allow that. :param obj: Arbitrary object to dictize. :type obj: object :returns: Dictized object. :rtype: same as obj type or collections.OrderedDict """ if not hasattr(obj, u"_asdict"): return obj overriden = UserDict(obj._asdict()) old_get = overriden.__getitem__ new_get = lambda self, key: dictize(old_get(self, key)) overriden.__getitem__ = new_get return overriden
def template_vars(template): """Return the namespace defined in the template object.""" try: return template._TemplateReference__context.vars except AttributeError: return UserDict(template)
def test_key_refcount(self): a = UserDict() code = """ py::object one = 1; py::object two = 2; py::tuple ref_counts(3); py::tuple obj_counts(3); py::tuple val_counts(3); py::tuple key_counts(3); obj_counts[0] = a.refcount(); key_counts[0] = one.refcount(); val_counts[0] = two.refcount(); a[1] = 2; obj_counts[1] = a.refcount(); key_counts[1] = one.refcount(); val_counts[1] = two.refcount(); a[1] = 2; obj_counts[2] = a.refcount(); key_counts[2] = one.refcount(); val_counts[2] = two.refcount(); ref_counts[0] = obj_counts; ref_counts[1] = key_counts; ref_counts[2] = val_counts; return_val = ref_counts; """ obj,key,val = inline_tools.inline(code,['a']) assert_equal(obj[0],obj[1]) assert_equal(obj[1],obj[2]) assert_equal(key[0] + 1, key[1]) assert_equal(key[1], key[2]) assert_equal(val[0] + 1, val[1]) assert_equal(val[1], val[2])
def test_setting_dict_to_invalid(self): self.cannot_set_attr(self.b, '__dict__', None, (AttributeError, TypeError)) from collections import UserDict d = UserDict({'known_attr': 7}) self.cannot_set_attr(self.fi.a.__func__, '__dict__', d, (AttributeError, TypeError))
def test_set_double_new(self): a = UserDict() key = 1.0 inline_tools.inline('a[key] = 123.0;',['a','key']) assert_equal(sys.getrefcount(key),4) # should be 3 assert_equal(sys.getrefcount(a[key]),2) assert_equal(a[key],123.0)
def test_set_complex(self): a = UserDict() key = 1+1j inline_tools.inline("a[key] = 1234;",['a','key']) assert_equal(sys.getrefcount(key),4) # should be 3 assert_equal(sys.getrefcount(a[key]),2) assert_equal(a[key],1234)
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'
def test_make_upper_case(firestore_mock, capsys): firestore_mock.collection = MagicMock(return_value=firestore_mock) firestore_mock.document = MagicMock(return_value=firestore_mock) firestore_mock.set = MagicMock(return_value=firestore_mock) user_id = str(uuid.uuid4()) date_string = datetime.now().isoformat() email_string = '%s@%s.com' % (uuid.uuid4(), uuid.uuid4()) data = { 'uid': user_id, 'metadata': {'createdAt': date_string}, 'email': email_string, 'value': { 'fields': { 'original': { 'stringValue': 'foobar' } } } } context = UserDict() context.resource = '/documents/some_collection/path/some/path' main.make_upper_case(data, context) out, _ = capsys.readouterr() assert 'Replacing value: foobar --> FOOBAR' in out firestore_mock.collection.assert_called_with('some_collection') firestore_mock.document.assert_called_with('path/some/path') firestore_mock.set.assert_called_with({'original': 'FOOBAR'})
def _ensure_tensor_on_device(self, inputs, device): if isinstance(inputs, ModelOutput): return ModelOutput({ name: self._ensure_tensor_on_device(tensor, device) for name, tensor in inputs.items() }) elif isinstance(inputs, dict): return { name: self._ensure_tensor_on_device(tensor, device) for name, tensor in inputs.items() } elif isinstance(inputs, UserDict): return UserDict({ name: self._ensure_tensor_on_device(tensor, device) for name, tensor in inputs.items() }) elif isinstance(inputs, list): return [ self._ensure_tensor_on_device(item, device) for item in inputs ] elif isinstance(inputs, tuple): return tuple([ self._ensure_tensor_on_device(item, device) for item in inputs ]) elif isinstance(inputs, torch.Tensor): return inputs.to(device) else: return inputs
def _ensure_tensor_on_device(self, inputs, device): if isinstance(inputs, ModelOutput): return ModelOutput({ name: self._ensure_tensor_on_device(tensor, device) for name, tensor in inputs.items() }) elif isinstance(inputs, dict): return { name: self._ensure_tensor_on_device(tensor, device) for name, tensor in inputs.items() } elif isinstance(inputs, UserDict): return UserDict({ name: self._ensure_tensor_on_device(tensor, device) for name, tensor in inputs.items() }) elif isinstance(inputs, list): return [ self._ensure_tensor_on_device(item, device) for item in inputs ] elif isinstance(inputs, tuple): return tuple([ self._ensure_tensor_on_device(item, device) for item in inputs ]) elif isinstance(inputs, torch.Tensor): if device == torch.device("cpu") and inputs.dtype in { torch.float16, torch.bfloat16 }: inputs = inputs.float() return inputs.to(device) else: return inputs