Drawing a path with a line in OpenLayers using JavaScript

You would need to make use of the LineString object Here is an example: var lineLayer = new OpenLayers.Layer.Vector(“Line Layer”); map.addLayer(lineLayer); map.addControl(new OpenLayers.Control.DrawFeature(lineLayer, OpenLayers.Handler.Path)); var points = new Array( new OpenLayers.Geometry.Point(lon1, lat1), new OpenLayers.Geometry.Point(lon2, lat2) ); var line = new OpenLayers.Geometry.LineString(points); var style = { strokeColor: ‘#0000ff’, strokeOpacity: 0.5, strokeWidth: 5 }; var lineFeature = … Read more

Recursively access dict via attributes as well as index access?

Here’s one way to create that kind of experience: class DotDictify(dict): MARKER = object() def __init__(self, value=None): if value is None: pass elif isinstance(value, dict): for key in value: self.__setitem__(key, value[key]) else: raise TypeError(‘expected dict’) def __setitem__(self, key, value): if isinstance(value, dict) and not isinstance(value, DotDictify): value = DotDictify(value) super(DotDictify, self).__setitem__(key, value) def __getitem__(self, key): … Read more

Elegant way of counting occurrences in a java collection

Now let’s try some Java 8 code: static public Map<String, Integer> toMap(List<String> lst) { return lst.stream() .collect(HashMap<String, Integer>::new, (map, str) -> { if (!map.containsKey(str)) { map.put(str, 1); } else { map.put(str, map.get(str) + 1); } }, HashMap<String, Integer>::putAll); } static public Map<String, Integer> toMap(List<String> lst) { return lst.stream().collect(Collectors.groupingBy(s -> s, Collectors.counting())); } I think this … Read more

generalised insert into sqlalchemy using dictionary

The idiomatic way to unpack a dictionary is to use the double star operator **. To use it with flask-sqlalchemy: class Employee(db.Model) id = db.Column(…) firstname = db.Column(…) lastname = db.Column(…) employee_data = {‘firstname’:’John’,’lastname’:’Smith’} employee = Employee(**employee_data) db.session.add(employee) db.session.commit() Be aware that the keys in the dictionary have to match the attribute names of the … Read more

how do you make a heterogeneous boost::map?

#include <map> #include <string> #include <iostream> #include <boost/any.hpp> int main() { try { std::map<std::string, boost::any> m; m[“a”] = 2; m[“b”] = static_cast<char const *>(“black sheep”); int i = boost::any_cast<int>(m[“a”]); std::cout << “I(” << i << “)\n”; int j = boost::any_cast<int>(m[“b”]); // throws exception std::cout << “J(” << j << “)\n”; } catch(…) { std::cout << … Read more

How to set initial size for a dictionary in Python?

With performance issues it’s always best to measure. Here are some timings: d = {} for i in xrange(4000000): d[i] = None # 722ms d = dict(itertools.izip(xrange(4000000), itertools.repeat(None))) # 634ms dict.fromkeys(xrange(4000000)) # 558ms s = set(xrange(4000000)) dict.fromkeys(s) # Not including set construction 353ms The last option doesn’t do any resizing, it just copies the hashes … Read more

Flask request and application/json content type

Use request.get_json() and set force to True: @menus.route(“https://stackoverflow.com/”, methods=[“PUT”, “POST”]) def new(): return jsonify(request.get_json(force=True)) From the documentation: By default this function will only load the json data if the mimetype is application/json but this can be overridden by the force parameter. Parameters: force – if set to True the mimetype is ignored. For older Flask … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)