def test_length(self): d1 = datastructures.MultiDict([("foo", "1")]) d2 = datastructures.MultiDict([("bar", "2")]) assert len(d1) == len(d2) == 1 d = self.storage_class([d1, d2]) assert len(d) == 2 d1.clear() assert len(d1) == 0 assert len(d) == 1
def test_length(self): d1 = datastructures.MultiDict([('foo', '1')]) d2 = datastructures.MultiDict([('bar', '2')]) assert len(d1) == len(d2) == 1 d = self.storage_class([d1, d2]) assert len(d) == 2 d1.clear() assert len(d1) == 0 assert len(d) == 1
def test_iterables(self): a = ds.MultiDict((("key_a", "value_a"),)) b = ds.MultiDict((("key_b", "value_b"),)) ab = ds.CombinedMultiDict((a, b)) assert sorted(ab.lists()) == [("key_a", ["value_a"]), ("key_b", ["value_b"])] assert sorted(ab.listvalues()) == [["value_a"], ["value_b"]] assert sorted(ab.keys()) == ["key_a", "key_b"] assert sorted(ab.lists()) == [("key_a", ["value_a"]), ("key_b", ["value_b"])] assert sorted(ab.listvalues()) == [["value_a"], ["value_b"]] assert sorted(ab.keys()) == ["key_a", "key_b"]
def test_iterables(self): a = datastructures.MultiDict((("key_a", "value_a"),)) b = datastructures.MultiDict((("key_b", "value_b"),)) ab = datastructures.CombinedMultiDict((a, b)) assert sorted(ab.lists()) == [('key_a', ['value_a']), ('key_b', ['value_b'])] assert sorted(ab.listvalues()) == [['value_a'], ['value_b']] assert sorted(ab.keys()) == ["key_a", "key_b"] assert sorted(iterlists(ab)) == [('key_a', ['value_a']), ('key_b', ['value_b'])] assert sorted(iterlistvalues(ab)) == [['value_a'], ['value_b']] assert sorted(iterkeys(ab)) == ["key_a", "key_b"]
def test_geographic_search(monkeypatch, endpoint_function, endpoint_kwargs, system_id): monkeypatch.setattr( flask, "request", mock.MagicMock( headers={}, args=datastructures.MultiDict([("latitude", 10), ("longitude", 20)]), ), ) service_layer_response = "TEST" service_layer_function = mock.MagicMock() service_layer_function.return_value = service_layer_response monkeypatch.setattr(stopservice, "geographical_search", service_layer_function) expected_content = None if expected_content is None: expected_content = json.dumps(service_layer_response) response = endpoint_function(**endpoint_kwargs) assert expected_content == response.get_data(as_text=True) assert HttpStatus.OK == response.status_code service_layer_function.assert_called_once_with( system_id=system_id, latitude=10, longitude=20, distance=1000, return_service_maps=True, )
def endpoints_test_helper( monkeypatch, endpoints_module, service_module, function_name, function_args, function_kwargs=None, expected_http_status=HttpStatus.OK, expected_content=None, expected_content_type="application/json", ): if function_kwargs is None: function_kwargs = {} monkeypatch.setattr( flask, "request", mock.MagicMock(headers={}, args=datastructures.MultiDict())) service_layer_response = "TEST" service_layer_function = mock.MagicMock() service_layer_function.return_value = service_layer_response monkeypatch.setattr(service_module, function_name, service_layer_function) if expected_content is None: expected_content = json.dumps(service_layer_response) response = getattr(endpoints_module, function_name)(*function_args) assert expected_content == response.get_data(as_text=True) assert expected_http_status == response.status_code assert expected_content_type == response.content_type service_layer_function.assert_called_once_with(*function_args, **function_kwargs)
def test_basic_interface(self): d1 = ds.MultiDict([("foo", "1")]) d2 = ds.MultiDict([("bar", "2"), ("bar", "3")]) d = self.storage_class([d1, d2]) # lookup assert d["foo"] == "1" assert d["bar"] == "2" assert d.getlist("bar") == ["2", "3"] assert sorted(d.items()) == [("bar", "2"), ("foo", "1")] assert sorted(d.items(multi=True)) == [("bar", "2"), ("bar", "3"), ("foo", "1")] assert "missingkey" not in d assert "foo" in d # type lookup assert d.get("foo", type=int) == 1 assert d.getlist("bar", type=int) == [2, 3] # get key errors for missing stuff with pytest.raises(KeyError): d["missing"] # make sure that they are immutable with pytest.raises(TypeError): d["foo"] = "blub" # copies are mutable d = d.copy() d["foo"] = "blub" # make sure lists merges md1 = ds.MultiDict((("foo", "bar"), ("foo", "baz"))) md2 = ds.MultiDict((("foo", "blafasel"), )) x = self.storage_class((md1, md2)) assert list(x.lists()) == [("foo", ["bar", "baz", "blafasel"])] # make sure dicts are created properly assert x.to_dict() == {"foo": "bar"} assert x.to_dict(flat=False) == {"foo": ["bar", "baz", "blafasel"]}
def test_basic_interface(self): d1 = datastructures.MultiDict([('foo', '1')]) d2 = datastructures.MultiDict([('bar', '2'), ('bar', '3')]) d = self.storage_class([d1, d2]) # lookup self.assert_equal(d['foo'], '1') self.assert_equal(d['bar'], '2') self.assert_equal(d.getlist('bar'), ['2', '3']) self.assert_equal(sorted(d.items()), [('bar', '2'), ('foo', '1')]) self.assert_equal(sorted(d.items(multi=True)), [('bar', '2'), ('bar', '3'), ('foo', '1')]) assert 'missingkey' not in d assert 'foo' in d # type lookup self.assert_equal(d.get('foo', type=int), 1) self.assert_equal(d.getlist('bar', type=int), [2, 3]) # get key errors for missing stuff with self.assert_raises(KeyError): d['missing'] # make sure that they are immutable with self.assert_raises(TypeError): d['foo'] = 'blub' # copies are immutable d = d.copy() with self.assert_raises(TypeError): d['foo'] = 'blub' # make sure lists merges md1 = datastructures.MultiDict((("foo", "bar"), )) md2 = datastructures.MultiDict((("foo", "blafasel"), )) x = self.storage_class((md1, md2)) self.assert_equal(list(iterlists(x)), [('foo', ['bar', 'blafasel'])])
def test_ordered_interface(self): cls = self.storage_class d = cls() assert not d d.add("foo", "bar") assert len(d) == 1 d.add("foo", "baz") assert len(d) == 1 assert list(iteritems(d)) == [("foo", "bar")] assert list(d) == ["foo"] assert list(iteritems(d, multi=True)) == [("foo", "bar"), ("foo", "baz")] del d["foo"] assert not d assert len(d) == 0 assert list(d) == [] d.update([("foo", 1), ("foo", 2), ("bar", 42)]) d.add("foo", 3) assert d.getlist("foo") == [1, 2, 3] assert d.getlist("bar") == [42] assert list(iteritems(d)) == [("foo", 1), ("bar", 42)] expected = ["foo", "bar"] assert list(d.keys()) == expected assert list(d) == expected assert list(iterkeys(d)) == expected assert list(iteritems(d, multi=True)) == [ ("foo", 1), ("foo", 2), ("bar", 42), ("foo", 3), ] assert len(d) == 2 assert d.pop("foo") == 1 assert d.pop("blafasel", None) is None assert d.pop("blafasel", 42) == 42 assert len(d) == 1 assert d.poplist("bar") == [42] assert not d d.get("missingkey") is None d.add("foo", 42) d.add("foo", 23) d.add("bar", 2) d.add("foo", 42) assert d == datastructures.MultiDict(d) id = self.storage_class(d) assert d == id d.add("foo", 2) assert d != id d.update({"blah": [1, 2, 3]}) assert d["blah"] == 1 assert d.getlist("blah") == [1, 2, 3] # setlist works d = self.storage_class() d["foo"] = 42 d.setlist("foo", [1, 2]) assert d.getlist("foo") == [1, 2] with pytest.raises(BadRequestKeyError): d.pop("missing") with pytest.raises(BadRequestKeyError): d["missing"] # popping d = self.storage_class() d.add("foo", 23) d.add("foo", 42) d.add("foo", 1) assert d.popitem() == ("foo", 23) with pytest.raises(BadRequestKeyError): d.popitem() assert not d d.add("foo", 23) d.add("foo", 42) d.add("foo", 1) assert d.popitemlist() == ("foo", [23, 42, 1]) with pytest.raises(BadRequestKeyError): d.popitemlist() # Unhashable d = self.storage_class() d.add("foo", 23) pytest.raises(TypeError, hash, d)
def test_ordered_interface(self): cls = self.storage_class d = cls() assert not d d.add('foo', 'bar') assert len(d) == 1 d.add('foo', 'baz') assert len(d) == 1 assert list(iteritems(d)) == [('foo', 'bar')] assert list(d) == ['foo'] assert list(iteritems(d, multi=True)) == \ [('foo', 'bar'), ('foo', 'baz')] del d['foo'] assert not d assert len(d) == 0 assert list(d) == [] d.update([('foo', 1), ('foo', 2), ('bar', 42)]) d.add('foo', 3) assert d.getlist('foo') == [1, 2, 3] assert d.getlist('bar') == [42] assert list(iteritems(d)) == [('foo', 1), ('bar', 42)] expected = ['foo', 'bar'] assert list(d.keys()) == expected assert list(d) == expected assert list(iterkeys(d)) == expected assert list(iteritems(d, multi=True)) == \ [('foo', 1), ('foo', 2), ('bar', 42), ('foo', 3)] assert len(d) == 2 assert d.pop('foo') == 1 assert d.pop('blafasel', None) is None assert d.pop('blafasel', 42) == 42 assert len(d) == 1 assert d.poplist('bar') == [42] assert not d d.get('missingkey') is None d.add('foo', 42) d.add('foo', 23) d.add('bar', 2) d.add('foo', 42) assert d == datastructures.MultiDict(d) id = self.storage_class(d) assert d == id d.add('foo', 2) assert d != id d.update({'blah': [1, 2, 3]}) assert d['blah'] == 1 assert d.getlist('blah') == [1, 2, 3] # setlist works d = self.storage_class() d['foo'] = 42 d.setlist('foo', [1, 2]) assert d.getlist('foo') == [1, 2] with pytest.raises(BadRequestKeyError): d.pop('missing') with pytest.raises(BadRequestKeyError): d['missing'] # popping d = self.storage_class() d.add('foo', 23) d.add('foo', 42) d.add('foo', 1) assert d.popitem() == ('foo', 23) with pytest.raises(BadRequestKeyError): d.popitem() assert not d d.add('foo', 23) d.add('foo', 42) d.add('foo', 1) assert d.popitemlist() == ('foo', [23, 42, 1]) with pytest.raises(BadRequestKeyError): d.popitemlist()
def test_ordered_interface(self): cls = self.storage_class d = cls() assert not d d.add('foo', 'bar') self.assert_equal(len(d), 1) d.add('foo', 'baz') self.assert_equal(len(d), 1) self.assert_equal(list(iteritems(d)), [('foo', 'bar')]) self.assert_equal(list(d), ['foo']) self.assert_equal(list(iteritems(d, multi=True)), [('foo', 'bar'), ('foo', 'baz')]) del d['foo'] assert not d self.assert_equal(len(d), 0) self.assert_equal(list(d), []) d.update([('foo', 1), ('foo', 2), ('bar', 42)]) d.add('foo', 3) self.assert_equal(d.getlist('foo'), [1, 2, 3]) self.assert_equal(d.getlist('bar'), [42]) self.assert_equal(list(iteritems(d)), [('foo', 1), ('bar', 42)]) expected = ['foo', 'bar'] self.assert_sequence_equal(list(d.keys()), expected) self.assert_sequence_equal(list(d), expected) self.assert_sequence_equal(list(iterkeys(d)), expected) self.assert_equal(list(iteritems(d, multi=True)), [('foo', 1), ('foo', 2), ('bar', 42), ('foo', 3)]) self.assert_equal(len(d), 2) self.assert_equal(d.pop('foo'), 1) assert d.pop('blafasel', None) is None self.assert_equal(d.pop('blafasel', 42), 42) self.assert_equal(len(d), 1) self.assert_equal(d.poplist('bar'), [42]) assert not d d.get('missingkey') is None d.add('foo', 42) d.add('foo', 23) d.add('bar', 2) d.add('foo', 42) self.assert_equal(d, datastructures.MultiDict(d)) id = self.storage_class(d) self.assert_equal(d, id) d.add('foo', 2) assert d != id d.update({'blah': [1, 2, 3]}) self.assert_equal(d['blah'], 1) self.assert_equal(d.getlist('blah'), [1, 2, 3]) # setlist works d = self.storage_class() d['foo'] = 42 d.setlist('foo', [1, 2]) self.assert_equal(d.getlist('foo'), [1, 2]) with self.assert_raises(BadRequestKeyError): d.pop('missing') with self.assert_raises(BadRequestKeyError): d['missing'] # popping d = self.storage_class() d.add('foo', 23) d.add('foo', 42) d.add('foo', 1) self.assert_equal(d.popitem(), ('foo', 23)) with self.assert_raises(BadRequestKeyError): d.popitem() assert not d d.add('foo', 23) d.add('foo', 42) d.add('foo', 1) self.assert_equal(d.popitemlist(), ('foo', [23, 42, 1])) with self.assert_raises(BadRequestKeyError): d.popitemlist()
cursor = db.cursor() tables = cursor.execute('SELECT name FROM sqlite_master').fetchall() for table in tables: table_name = table[0] print '[*] fetching data from ' + table_name price = price + cursor.execute('SELECT price FROM ' + table_name + ' WHERE price < 12000000').fetchall() lat = lat + cursor.execute('SELECT lat FROM ' + table_name + ' WHERE price < 12000000').fetchall() lng = lng + cursor.execute('SELECT lng FROM ' + table_name + ' WHERE price < 12000000').fetchall() print '[*] finished db reading\n' print '[*] starting data processing: averaging price by same addresses' md = datastructures.MultiDict( map(lambda i: ((lng[i], lat[i]), price[i]), range(len(lat)))) tdic = dict((key, sum([x[0] for x in md.getlist(key)]) / len(md.getlist(key))) for key in md.keys()) if action == 'interpolation': interpolation(tdic) else: fieldinterpolation(tdic) ya_map_string = 'http://static-maps.yandex.ru/1.x/?ll=37.5946002,55.7622764&spn=0.25,0.25&size=400,400&l=map' """ ya_map_string = 'http://static-maps.yandex.ru/1.x/?ll='+str(center_lng)+','+str(center_lat)+'&spn='+str(mx*diameterx)+','+str(my*diametery)+'&size=400,400&l=map' if False: ya_map_string = ya_map_string + '&pt=' for i in filtered_dic.keys(): print "Filtered coordinates (lng, lat): "+str(i)
def request_filters(self) -> datastructures.MultiDict: return datastructures.MultiDict([ (key, value) for key, value in flask.request.args.items(multi=True) if key.startswith('filter') ])
def json2formdata(json): return datastructures.MultiDict(json)