Returns the very first item in the OrderedDict and throws an exception if it is empty. Returns the key description string the OrderedDict was constructed with. const std :: string & key_description ( ) const noexcept ¶ OrderedDict ( std :: initializer_list initializer_list ) ¶Ĭonstructs a new OrderedDict and pre-populates it with the given Items. OrderedDict & operator = ( OrderedDict & & other ) = default ¶ OrderedDict ( OrderedDict & & other ) = default ¶ OrderedDict & operator = ( const OrderedDict & other ) ¶Īssigns items from other to this OrderedDict. OrderedDict ( const OrderedDict & other ) ¶Ĭopy constructs this OrderedDict from other. This description is used in error messages thrown by the OrderedDict. I write about random things that come to mind, mostly for my future self and any visitor who might find useful.Public Functions explicit OrderedDict ( std :: string key_description = "Key" ) ¶Ĭonstructs the OrderedDict with a short description of the kinds of keys stored in the OrderedDict. « Convert Python objects to JSON Improved Priority Queue recipe in Python » About Me Posted by Cuong Dong-Si May 25 th, 2016 1:26 am python dump ( config, config_file, default = lambda o : vars ( o ), indent = 4 ) def main (): FILE_NAME = "hive_vertica_count.json" query_generator = generate_count_queries () create_config_file ( FILE_NAME, query_generator ) if _name_ = "_main_" : main () queries = query_generator with open ( filename, 'w' ) as config_file : json. url = "jdbc:vertica://192.168.5.174:5433/VMart" return conn def create_config_file ( filename, query_generator ): hive_source = get_hive_config () vertica_target = get_vertica_config () config = Config () config. url = "jdbc:hive2://192.168.5.184:10000/DWH" return conn def get_vertica_config (): """ Get pre-defined Vertica configuration. _dict_ = value pass def get_hive_config (): """ Get pre-defined Hive configuration. _dict_ def _setattr_ ( self, key, value ): self. OrderedDict () def _getattr_ ( self, item ): return self. Import collections import json class Config ( object ): ODICT = "odict" def _init_ ( self ): self. This option is just a quick and dirty workaround: our Config class should extend collections.OrderedDict class and, in the code, we refer to object instead of object.att. To have the keys appear in order as defined when converting to JSON, we have two options: Option 1: use OrderedDict as your base class Setting sort_keys option in json.dump is not applicable here since the keys will be sorted by their names, not their order of appearance like we do in the Java example. However, it sometimes makes sense to humans for the keys to be pretty-printed in order, especially when we need to look for two keys in JSON next to each other or one key before another.įor example, in the Config object in the last post, it is better to see source and target configurations side by side and, then, get to know what kind of tests from testName key before reading details of tests in queries key. In theory, it does not matter to machines when converting to/from JSON. In the JSON output shown in the last post, the keys are printed out of order since they are unordered in the internal dictionary _dict_.
0 Comments
Leave a Reply. |