Python json.dump() Examples

The following are 30 code examples of json.dump(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module json , or try the search function .
Example #1
Source File: workflow.py    From wechat-alfred-workflow with MIT License 6 votes vote down vote up
def register(self, name, serializer):
        """Register ``serializer`` object under ``name``.

        Raises :class:`AttributeError` if ``serializer`` in invalid.

        .. note::

            ``name`` will be used as the file extension of the saved files.

        :param name: Name to register ``serializer`` under
        :type name: ``unicode`` or ``str``
        :param serializer: object with ``load()`` and ``dump()``
            methods

        """
        # Basic validation
        getattr(serializer, 'load')
        getattr(serializer, 'dump')

        self._serializers[name] = serializer 
Example #2
Source File: workflow.py    From wechat-alfred-workflow with MIT License 6 votes vote down vote up
def cache_data(self, name, data):
        """Save ``data`` to cache under ``name``.

        If ``data`` is ``None``, the corresponding cache file will be
        deleted.

        :param name: name of datastore
        :param data: data to store. This may be any object supported by
                the cache serializer

        """
        serializer = manager.serializer(self.cache_serializer)

        cache_path = self.cachefile('%s.%s' % (name, self.cache_serializer))

        if data is None:
            if os.path.exists(cache_path):
                os.unlink(cache_path)
                self.logger.debug('deleted cache file: %s', cache_path)
            return

        with atomic_writer(cache_path, 'wb') as file_obj:
            serializer.dump(data, file_obj)

        self.logger.debug('cached data: %s', cache_path) 
Example #3
Source File: coco.py    From Collaborative-Learning-for-Weakly-Supervised-Object-Detection with MIT License 6 votes vote down vote up
def gt_roidb(self):
    """
    Return the database of ground-truth regions of interest.
    This function loads/saves from/to a cache file to speed up future calls.
    """
    cache_file = osp.join(self.cache_path, self.name + '_gt_roidb.pkl')
    if osp.exists(cache_file):
      with open(cache_file, 'rb') as fid:
        roidb = pickle.load(fid)
      print('{} gt roidb loaded from {}'.format(self.name, cache_file))
      return roidb

    gt_roidb = [self._load_coco_annotation(index)
                for index in self._image_index]

    with open(cache_file, 'wb') as fid:
      pickle.dump(gt_roidb, fid, pickle.HIGHEST_PROTOCOL)
    print('wrote gt roidb to {}'.format(cache_file))
    return gt_roidb 
Example #4
Source File: coco.py    From Collaborative-Learning-for-Weakly-Supervised-Object-Detection with MIT License 6 votes vote down vote up
def _write_coco_results_file(self, all_boxes, res_file):
    # [{"image_id": 42,
    #   "category_id": 18,
    #   "bbox": [258.15,41.29,348.26,243.78],
    #   "score": 0.236}, ...]
    results = []
    for cls_ind, cls in enumerate(self.classes):
      if cls == '__background__':
        continue
      print('Collecting {} results ({:d}/{:d})'.format(cls, cls_ind,
                                                       self.num_classes - 1))
      coco_cat_id = self._class_to_coco_cat_id[cls]
      results.extend(self._coco_results_one_category(all_boxes[cls_ind],
                                                     coco_cat_id))
    print('Writing results json to {}'.format(res_file))
    with open(res_file, 'w') as fid:
      json.dump(results, fid) 
Example #5
Source File: data.py    From comet-commonsense with Apache License 2.0 6 votes vote down vote up
def save_eval_file(opt, stats, eval_type="losses", split="dev", ext="pickle"):
    if cfg.test_save:
        name = "{}/{}.{}".format(utils.make_name(
            opt, prefix="garbage/{}/".format(eval_type),
            is_dir=True, eval_=True), split, ext)
    else:
        name = "{}/{}.{}".format(utils.make_name(
            opt, prefix="results/{}/".format(eval_type),
            is_dir=True, eval_=True), split, ext)
    print("Saving {} {} to {}".format(split, eval_type, name))

    if ext == "pickle":
        with open(name, "wb") as f:
            pickle.dump(stats, f)
    elif ext == "txt":
        with open(name, "w") as f:
            f.write(stats)
    elif ext == "json":
        with open(name, "w") as f:
            json.dump(stats, f)
    else:
        raise 
Example #6
Source File: build.py    From Traffic_sign_detection_YOLO with MIT License 6 votes vote down vote up
def savepb(self):
		"""
		Create a standalone const graph def that 
		C++	can load and run.
		"""
		darknet_pb = self.to_darknet()
		flags_pb = self.FLAGS
		flags_pb.verbalise = False
		
		flags_pb.train = False
		# rebuild another tfnet. all const.
		tfnet_pb = TFNet(flags_pb, darknet_pb)		
		tfnet_pb.sess = tf.Session(graph = tfnet_pb.graph)
		# tfnet_pb.predict() # uncomment for unit testing
		name = 'built_graph/{}.pb'.format(self.meta['name'])
		os.makedirs(os.path.dirname(name), exist_ok=True)
		#Save dump of everything in meta
		with open('built_graph/{}.meta'.format(self.meta['name']), 'w') as fp:
			json.dump(self.meta, fp)
		self.say('Saving const graph def to {}'.format(name))
		graph_def = tfnet_pb.sess.graph_def
		tf.train.write_graph(graph_def,'./', name, False) 
Example #7
Source File: prepare_model_yaml.py    From models with MIT License 6 votes vote down vote up
def make_model_yaml(template_yaml, model_json, output_yaml_path):
    #
    with open(template_yaml, 'r') as f:
        model_yaml = yaml.load(f)
    #
    # get the model config:
    json_file = open(model_json, 'r')
    loaded_model_json = json_file.read()
    json_file.close()
    loaded_model = keras.models.model_from_json(loaded_model_json)
    #
    model_yaml["schema"]["targets"] = []
    for oname, oshape in zip(loaded_model.output_names, loaded_model.output_shape):
        append_el ={"name":oname , "shape":str(oshape)#replace("None,", "")
        , "doc":"Methylation probability for %s"%oname}
        model_yaml["schema"]["targets"].append(append_el)
    #
    with open(output_yaml_path, 'w') as f:
        yaml.dump(model_yaml, f, default_flow_style=False) 
Example #8
Source File: prepare_model_yaml.py    From models with MIT License 6 votes vote down vote up
def make_secondary_dl_yaml(template_yaml, model_json, output_yaml_path):
    with open(template_yaml, 'r') as f:
        model_yaml = yaml.load(f)
    #
    # get the model config:
    json_file = open(model_json, 'r')
    loaded_model_json = json_file.read()
    json_file.close()
    loaded_model = keras.models.model_from_json(loaded_model_json)
    #
    model_yaml["output_schema"]["targets"] = []
    for oname, oshape in zip(loaded_model.output_names, loaded_model.output_shape):
        append_el ={"name":oname , "shape":str(oshape)#replace("None,", "")
        , "doc":"Methylation probability for %s"%oname}
        model_yaml["output_schema"]["targets"].append(append_el)
    #
    with open(output_yaml_path, 'w') as f:
        yaml.dump(model_yaml, f, default_flow_style=False) 
Example #9
Source File: conf.py    From neuropythy with GNU Affero General Public License v3.0 6 votes vote down vote up
def saverc(filename, dat, overwrite=False):
    '''
    saverc(filename, d) saves the given configuration dictionary d to the given filename in JSON
      format. If d is not a dictionary or if filename already exists or cannot be created, an error
      is raised. This funciton does not create directories.

    The optional argument overwrite (default: False) may be passed as True to overwrite files that
    already exist.
    '''
    filename = os.path.expanduser(os.path.expandvars(filename))
    if not overwrite and os.path.isfile(filename):
        raise ValueError('Given filename %s already exists' % filename)
    if not pimms.is_map(dat):
        try: dat = dict(dat)
        except Exception: raise ValueError('Given config data must be a dictionary')
    with open(filename, 'w') as fl:
        json.dump(dat, fl, sort_keys=True)
    return filename

# the private class that handles all the details... 
Example #10
Source File: core.py    From neuropythy with GNU Affero General Public License v3.0 6 votes vote down vote up
def save_json(filename, obj, normalize=True):
    '''
    save_json(filename, obj) writes the given object to the given filename (or stream) in a
      normalized JSON format.

    The optional argument normalize (default True) may be set to False to prevent the object from
    being run through neuropythy's normalize system.
    '''
    from neuropythy.util import normalize as norm
    dat = norm(obj) if normalize else obj
    if pimms.is_str(filename):
        jsonstr = json.dumps(dat)
        if any(filename.endswith(s) for s in ('.gz', '.bz2', '.lzma')):
            with gzip.open(filename, 'wt') as fl: fl.write(jsonstr)
        else:
            with open(filename, 'wt') as fl: fl.write(jsonstr)
    else: json.dump(dat, filename)
    return filename 
Example #11
Source File: coco.py    From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 6 votes vote down vote up
def _write_coco_results(self, _coco, detections):
        """ example results
        [{"image_id": 42,
          "category_id": 18,
          "bbox": [258.15,41.29,348.26,243.78],
          "score": 0.236}, ...]
        """
        cats = [cat['name'] for cat in _coco.loadCats(_coco.getCatIds())]
        class_to_coco_ind = dict(zip(cats, _coco.getCatIds()))
        results = []
        for cls_ind, cls in enumerate(self.classes):
            if cls == '__background__':
                continue
            logger.info('collecting %s results (%d/%d)' % (cls, cls_ind, self.num_classes - 1))
            coco_cat_id = class_to_coco_ind[cls]
            results.extend(self._coco_results_one_category(detections[cls_ind], coco_cat_id))
        logger.info('writing results json to %s' % self._result_file)
        with open(self._result_file, 'w') as f:
            json.dump(results, f, sort_keys=True, indent=4) 
Example #12
Source File: proxyLoader.py    From premeStock with MIT License 6 votes vote down vote up
def loadProxies():
	proxiesList = []
	cprint("Loading proxies...","green")

	site2(proxiesList) # load proxies

	# proxiesList = ["13.85.80.251:443"]
	# proxiesList = ["13.85.80.251:443"]
	# proxiesList = ["144.217.16.78:3128"]
	proxiesList = proxiesList[::-1]
	proxiesList = proxiesList[:10]
	proxiesList = filterConnections(proxiesList) # filter for working connections

	# Write to file
	with open("proxies.txt", 'w') as outfile:
		json.dump(proxiesList, outfile)
	cprint("Proxies saved to proxies.txt!","magenta","on_grey", attrs=['bold']) 
Example #13
Source File: log-parser.py    From aws-waf-security-automations with Apache License 2.0 6 votes vote down vote up
def write_output(bucket_name, key_name, output_key_name, outstanding_requesters):
    logging.getLogger().debug('[write_output] Start')

    try:
        current_data = '/tmp/' + key_name.split('/')[-1] + '_LOCAL.json'
        with open(current_data, 'w') as outfile:
            json.dump(outstanding_requesters, outfile)

        s3 = boto3.client('s3')
        s3.upload_file(current_data, bucket_name, output_key_name, ExtraArgs={'ContentType': "application/json"})
        remove(current_data)

    except Exception as e:
        logging.getLogger().error("[write_output] \tError to write output file")
        logging.getLogger().error(e)

    logging.getLogger().debug('[write_output] End') 
Example #14
Source File: main.py    From cs294-112_hws with MIT License 6 votes vote down vote up
def train(session, model, curr_dir, data_train, data_val):
    curr_dir = os.path.join(curr_dir, model.algorithm)
    bestmodel_dir = os.path.join(curr_dir, 'best_checkpoint')
    
    if not os.path.exists(curr_dir):
        os.makedirs(curr_dir)
    
    file_handler = logging.FileHandler(os.path.join(curr_dir, 'log.txt'))
    logging.getLogger().addHandler(file_handler)
    
    with open(os.path.join(curr_dir, FLAGS['save_name'] + '.json'), 'w') as f:
        json.dump(FLAGS, f)
    
    if not os.path.exists(bestmodel_dir):
        os.makedirs(bestmodel_dir)
    
    initialize_model(session, model, curr_dir, expect_exists=False)
    model.train(session, curr_dir, bestmodel_dir, data_train, data_val) 
Example #15
Source File: agent_pop.py    From indras_net with GNU General Public License v3.0 5 votes vote down vote up
def record_results(self, file_nm):
        json.dump(self, open(file_nm, 'w'), indent=4,
                  default=self.jsondump) 
Example #16
Source File: prop_args2.py    From indras_net with GNU General Public License v3.0 5 votes vote down vote up
def write(self, file_nm):
        """
        Write properties to json file.
        Useful for storing interesting parameter sets.
        """
        dict_for_json = {}
        for prop_name in self.props:
            dict_for_json[prop_name] = self.props[prop_name].to_json()
        f = open(file_nm, 'w')
        json.dump(dict_for_json, f, indent=4)
        f.close() 
Example #17
Source File: prop_args.py    From indras_net with GNU General Public License v3.0 5 votes vote down vote up
def write(self, file_nm):
        """
        Write properties to json file.
        Useful for storing interesting parameter sets.
        """
        json.dump(self.props, open(file_nm, 'w'), indent=4) 
Example #18
Source File: results.py    From vergeml with MIT License 5 votes vote down vote up
def _sync(self, force=False):
        now = datetime.datetime.now()
        if force or (now - self.last_sync).total_seconds() > _SYNC_INTV:
            self.last_sync = now
            with open(self.path, "w") as f:
                json.dump(self.data, f) 
Example #19
Source File: session.py    From mlbv with GNU General Public License v3.0 5 votes vote down vote up
def save(self):
        with open(SESSION_FILE, 'w') as outfile:
            json.dump(self._state, outfile)
        self.session.cookies.save(COOKIE_FILE) 
Example #20
Source File: constants.py    From aegea with Apache License 2.0 5 votes vote down vote up
def write():
    from . import aws
    constants = {"instance_types": {}}
    instance_attrs = ["vcpu", "memory", "storage", "gpu", "clockSpeed", "networkPerformance"]
    spot_instance_families = {"m3", "m4", "c3", "c4", "r3", "r4", "i2", "i3", "d2", "g2"}
    for product in aws.get_ec2_products():
        if not any(product["attributes"]["instanceType"].startswith(fam + ".") for fam in spot_instance_families):
            continue
        traits = {field: product["attributes"].get(field) for field in instance_attrs}
        constants["instance_types"][product["attributes"]["instanceType"]] = traits
    with open(_constants_filename, "w") as fh:
        json.dump(constants, fh) 
Example #21
Source File: vcc_utils.py    From VEX_Syntax with MIT License 5 votes vote down vote up
def generate_simple_completions(sublime_completion_path=COMP_PATH):
    """Converts the function signitures generated by vcc into SublimeText compatable completion
    JSON files."""
    completions = []
    for name in all_functions():
        completions.append({'trigger' : ('%s\tfunction' % name),
                            'contents': ('%s(${1})' % name)})

    data = {'scope': 'source.vex', 'completions': completions}

    with open(sublime_completion_path, 'w') as f:
        json.dump(data, f, sort_keys=True, indent=4, separators=(',', ': ')) 
Example #22
Source File: vcc_utils.py    From VEX_Syntax with MIT License 5 votes vote down vote up
def generate_completions(sublime_completion_path=COMP_PATH):
    """Converts the function signitures generated by vcc into SublimeText compatable completion
    JSON files."""
    completions = []
    for sig in all_function_signatures():

        if len(sig['args']) == 1 and sig['args'][0] == 'void':
            comp_arg_fmt = ''
        else:
            comp_arg_fmt = ''
            comp_arg_fmt_no_varadic = ''
            c = 1

            for arg_type in sig['args']:
                comp_arg_fmt += ('${%d:%s}, ' % (c, arg_type))
                c += 1
                if arg_type != '...':
                    comp_arg_fmt_no_varadic = comp_arg_fmt

            comp_arg_fmt = comp_arg_fmt[:-2]  # stripping ', ' before closing parenthesis
            comp_arg_fmt_no_varadic = comp_arg_fmt_no_varadic[:-2]

        # in the varadic case, we'll generate two completions - one with and one without the
        # varadic argument elipsis
        if sig['args'][-1] == '...':
            new_hint = sig['hint'][:-4]
            new_hint = new_hint.rstrip().rstrip(';')
            new_hint += ')'
            completions.append({'trigger' : ('%s\t%s' % (sig['name'], new_hint)),
                                'contents': ('%s(%s)' % (sig['name'], comp_arg_fmt_no_varadic))})

        completions.append({'trigger' : ('%s\t%s' % (sig['name'], sig['hint'])),
                            'contents': ('%s(%s)' % (sig['name'], comp_arg_fmt))})

    data = {'scope': 'source.vex', 'completions': completions}

    with open(sublime_completion_path, 'w') as f:
        json.dump(data, f, sort_keys=True, indent=4, separators=(',', ': ')) 
Example #23
Source File: workflow3.py    From wechat-alfred-workflow with MIT License 5 votes vote down vote up
def send_feedback(self):
        """Print stored items to console/Alfred as JSON."""
        json.dump(self.obj, sys.stdout)
        sys.stdout.flush() 
Example #24
Source File: workflow.py    From wechat-alfred-workflow with MIT License 5 votes vote down vote up
def dump(cls, obj, file_obj):
        """Serialize object ``obj`` to open JSON file.

        .. versionadded:: 1.8

        :param obj: Python object to serialize
        :type obj: JSON-serializable data structure
        :param file_obj: file handle
        :type file_obj: ``file`` object

        """
        return json.dump(obj, file_obj, indent=2, encoding='utf-8') 
Example #25
Source File: workflow.py    From wechat-alfred-workflow with MIT License 5 votes vote down vote up
def dump(cls, obj, file_obj):
        """Serialize object ``obj`` to open pickle file.

        .. versionadded:: 1.8

        :param obj: Python object to serialize
        :type obj: Python object
        :param file_obj: file handle
        :type file_obj: ``file`` object

        """
        return cPickle.dump(obj, file_obj, protocol=-1) 
Example #26
Source File: workflow.py    From wechat-alfred-workflow with MIT License 5 votes vote down vote up
def dump(cls, obj, file_obj):
        """Serialize object ``obj`` to open pickle file.

        .. versionadded:: 1.8

        :param obj: Python object to serialize
        :type obj: Python object
        :param file_obj: file handle
        :type file_obj: ``file`` object

        """
        return pickle.dump(obj, file_obj, protocol=-1)


# Set up default manager and register built-in serializers 
Example #27
Source File: coco.py    From Collaborative-Learning-for-Weakly-Supervised-Object-Detection with MIT License 5 votes vote down vote up
def _do_detection_eval(self, res_file, output_dir):
    ann_type = 'bbox'
    coco_dt = self._COCO.loadRes(res_file)
    coco_eval = COCOeval(self._COCO, coco_dt)
    coco_eval.params.useSegm = (ann_type == 'segm')
    coco_eval.evaluate()
    coco_eval.accumulate()
    self._print_detection_eval_metrics(coco_eval)
    eval_file = osp.join(output_dir, 'detection_results.pkl')
    with open(eval_file, 'wb') as fid:
      pickle.dump(coco_eval, fid, pickle.HIGHEST_PROTOCOL)
    print('Wrote COCO eval results to: {}'.format(eval_file)) 
Example #28
Source File: utils.py    From comet-commonsense with Apache License 2.0 5 votes vote down vote up
def generate_config_files(type_, key, name="base", eval_mode=False):
    with open("config/default.json".format(type_), "r") as f:
        base_config = json.load(f)
    with open("config/{}/default.json".format(type_), "r") as f:
        base_config_2 = json.load(f)
    if eval_mode:
        with open("config/{}/eval_changes.json".format(type_), "r") as f:
            changes_by_machine = json.load(f)
    else:
        with open("config/{}/changes.json".format(type_), "r") as f:
            changes_by_machine = json.load(f)

    base_config.update(base_config_2)

    if name in changes_by_machine:
        changes = changes_by_machine[name]
    else:
        changes = changes_by_machine["base"]

    # for param in changes[key]:
    #     base_config[param] = changes[key][param]

    replace_params(base_config, changes[key])

    mkpath("config/{}".format(type_))

    with open("config/{}/config_{}.json".format(type_, key), "w") as f:
        json.dump(base_config, f, indent=4) 
Example #29
Source File: pkconfig.py    From pkmeter with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def save(self):
        log.info('Saving config: %s' % CONFIGPATH)
        os.makedirs(os.path.dirname(CONFIGPATH), exist_ok=True)
        self.set('pkmeter', 'positions', [w.position() for w in self.pkmeter.widgets])
        with open(CONFIGPATH, 'w') as handle:
            json.dump(self.values, handle, indent=2, sort_keys=True) 
Example #30
Source File: twitter-export-image-fill.py    From twitter-export-image-fill with The Unlicense 5 votes vote down vote up
def resave_data(data, data_filename, first_data_line, year_str, month_str):
  # Writing to a separate file so that we can only copy over the
  # main file when done
  data_filename_temp = 'data/js/tweets/%s_%s.js.tmp' % (year_str, month_str)
  with open(data_filename_temp, 'w') as f:
    f.write(first_data_line)
    json.dump(data, f, indent=2)
  os.remove(data_filename)
  os.rename(data_filename_temp, data_filename)


# Download a given image directly from the URL