How to dump a dict to a JSON file?

import json with open(‘result.json’, ‘w’) as fp: json.dump(sample, fp) This is an easier way to do it. In the second line of code the file result.json gets created and opened as the variable fp. In the third line your dict sample gets written into the result.json!

Iterating over all the keys of a map

https://play.golang.org/p/JGZ7mN0-U- for k, v := range m { fmt.Printf(“key[%s] value[%s]\n”, k, v) } or for k := range m { fmt.Printf(“key[%s] value[%s]\n”, k, m[k]) } Go language specs for for statements specifies that the first value is the key, the second variable is the value, but doesn’t have to be present.

What is the difference between the HashMap and Map objects in Java?

There is no difference between the objects; you have a HashMap<String, Object> in both cases. There is a difference in the interface you have to the object. In the first case, the interface is HashMap<String, Object>, whereas in the second it’s Map<String, Object>. But the underlying object is the same. The advantage to using Map<String, … Read more

Convert Django Model object to dict with all of the fields intact

There are many ways to convert an instance to a dictionary, with varying degrees of corner case handling and closeness to the desired result. 1. instance.__dict__ instance.__dict__ which returns {‘_foreign_key_cache’: <OtherModel: OtherModel object>, ‘_state’: <django.db.models.base.ModelState at 0x7ff0993f6908>, ‘auto_now_add’: datetime.datetime(2018, 12, 20, 21, 34, 29, 494827, tzinfo=<UTC>), ‘foreign_key_id’: 2, ‘id’: 1, ‘normal_value’: 1, ‘readonly_value’: 2} This … Read more

How to create dictionary and add key value pairs dynamically in Javascript

var dict = []; // create an empty array dict.push({ key: “keyName”, value: “the value” }); // repeat this last part as needed to add more key/value pairs Basically, you’re creating an object literal with 2 properties (called key and value) and inserting it (using push()) into the array. Edit: So almost 5 years later, … Read more

How to use a dot “.” to access members of dictionary?

I’ve always kept this around in a util file. You can use it as a mixin on your own classes too. class dotdict(dict): “””dot.notation access to dictionary attributes””” __getattr__ = dict.get __setattr__ = dict.__setitem__ __delattr__ = dict.__delitem__ mydict = {‘val’:’it works’} nested_dict = {‘val’:’nested works too’} mydict = dotdict(mydict) mydict.val # ‘it works’ mydict.nested = … Read more

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