Python load+from+json

60 Python code examples are found related to " load+from+json". 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.
Example 1
Source File: creds.py    From pyfy with MIT License 6 votes vote down vote up
def load_from_json(self, path=None, name=None):
        """
        Loads credentials from JSON file

        Arguments:

            path (str): path of the directory the file is located in

            name (str): name of the file.
        """
        if path is None:
            path = os.path.dirname(os.path.abspath(__file__))
        if name is None:
            name = DEFAULT_FILENAME_BASE + self.__class__.__name__ + ".json"
        path = os.path.join(path, name)
        with open(path, "r") as infile:
            self.__dict__.update(json.load(infile)) 
Example 2
Source File: game.py    From myctf with MIT License 6 votes vote down vote up
def load_from_json(cls, fn):
        txt = open(fn, 'r').read()
        info = json.loads(txt)
        if 'score_board' in info:
            cls.score_board = info['score_board']
        if 'solved' in info:
            cls.solved = info['solved']
        if 'solved_all' in info:
            cls.solved_all = info['solved_all']
        if 'extra' in info:
            for k, v in info['extra'].items():
                # json 的 key 不能为数字,所以。。
                k = int(k)
                cls.extra[k] = v
        if 'last_submit' in info:
            cls.last_submit = info['last_submit']
        if 'is_end' in info:
            cls.deadline = info['is_end'] 
Example 3
Source File: add_update_labels.py    From redmine2github with MIT License 6 votes vote down vote up
def load_github_specs_from_json_file(self, github_specs_fname):
        """
        Create and load class attributes for each of the GITHUB_SPEC_ATTRIBUTE_NAMES

        e.g. self.REPO_NAME = 'something in JSON file'
        """
        assert isfile(github_specs_fname), "The github_specs file was not found: %s" % github_specs_filename

        try:
            d = json.loads(open(github_specs_fname, 'rU').read())
        except:
            msgx('Could not parse JSON in file: %s' % github_specs_fname)
        
        for attr in self.GITHUB_SPEC_ATTRIBUTE_NAMES:
            if not d.has_key(attr):
                msgx('Value not found for "%s".  The github specs file "%s" must have a "%s"' % (attr, github_specs_fname, attr ))
            #print ('loaded: %s->%s' % (attr, self.__dict__[attr]))
            self.__dict__[attr] = d[attr] 
Example 4
Source File: imageSaver.py    From Liked-Saved-Image-Downloader with MIT License 6 votes vote down vote up
def loadSubmissionsFromJson(filename):
    file = open(filename, 'r')
    # Ugh...
    lines = file.readlines()
    text = u''.join(lines)
    # Fix the formatting so the json module understands it
    text = "[{}]".format(text[1:-3])
        
    dictSubmissions = json.loads(text)
    submissions = []
    for dictSubmission in dictSubmissions:
        submission = Submissions.Submission()
        submission.initFromDict(dictSubmission)
        submissions.append(submission)

    return submissions 
Example 5
Source File: QgsJsonModel.py    From QGISFMV with GNU General Public License v3.0 6 votes vote down vote up
def loadJsonFromConsole(self, value):
        ''' Load Json from console output '''
        error = QJsonParseError()
        self.mDocument = QJsonDocument.fromJson(value, error)

        if self.mDocument is not None:
            self.beginResetModel()
            if self.mDocument.isArray():
                self.mRootItem.load(list(self.mDocument.array()))
            else:
                self.mRootItem = self.mRootItem.load(self.mDocument.object())
            self.endResetModel()

            return True

        qgsu.showUserAndLogMessage("", "QJsonModel: error loading Json", onlyLog=True)
        return False 
Example 6
Source File: core.py    From pyhttptest with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def load_content_from_json_file(file_path):
    """Loads content from the file.

    By passing ``file_path`` parameter, the file is opened
    and the content from the file is extracted.

    :param str file_path: Optional file path.

    :returns: A content in a list
    :rtype: `list`
    """
    with open(file_path, 'rb') as file:
        items_generator = ijson.items(file, '')
        list_of_items = [item for item in items_generator]

        return list_of_items 
Example 7
Source File: opviewer.py    From evmlab with GNU General Public License v3.0 6 votes vote down vote up
def load_contract_sources_from_combined_json(self, tx, combined_json, source_prefix=''):
        # TODO: untested, need reference file
        contracts = []
        sources = []

        # get sources (text) for all contracts
        for file in [os.path.join(source_prefix, s) for s in combined_json['sourceList']]:
            with open(file) as s:
                logger.debug(s.name)
                sources.append(s.read())

        # get contract
        for contract, val in combined_json['contracts'].items():
            contracts.append(Contract(sources, val, contract))

        self.contracts = contracts
        self.op_contracts = buildContexts(self.ops, self.api, contracts, tx)
        return self 
Example 8
Source File: namespace.py    From claf with MIT License 6 votes vote down vote up
def load_from_json(self, dict_data):

        name_value_pairs = []

        def make_key_value_pairs(d, prefix=""):
            for k, v in d.items():
                if type(v) == dict:
                    next_prefix = k
                    if prefix != "":
                        next_prefix = f"{prefix}.{k}"
                    make_key_value_pairs(v, prefix=next_prefix)
                else:
                    key_with_prefix = k
                    if prefix != "":
                        key_with_prefix = f"{prefix}.{k}"
                    name_value_pairs.append((key_with_prefix, v))

        make_key_value_pairs(dict_data)
        for (name, value) in name_value_pairs:
            self.__setattr__(name, value) 
Example 9
Source File: inv3d.py    From geoist with MIT License 6 votes vote down vote up
def load_from_json(self, filename):
        """load model parameters and initialize it from JSON file
        TO_DO LIST 1:BEI
        """
        data_dict = {}
        try:
            f = open(filename, mode = 'r')
            data_dict = json.load(f)
            f.close
        except IOError:
            print('No file : %s' %(filename))            
        except ValueError:
            print('check raw data file')
        except IndexError:
            print('check raw data file: possibly last line?')           
        return data_dict 
Example 10
Source File: utils.py    From ReGraph with MIT License 6 votes vote down vote up
def load_edges_from_json(j_data):
    """Load edges from json-like dict."""
    loaded_edges = []
    if "edges" in j_data.keys():
        j_edges = j_data["edges"]
        for edge in j_edges:
            if "from" in edge.keys():
                s_node = edge["from"]
            else:
                raise ReGraphError(
                    "Error loading graph: edge source is not specified!")
            if "to" in edge.keys():
                t_node = edge["to"]
            else:
                raise ReGraphError(
                    "Error loading graph: edge target is not specified!")
            if "attrs" in edge.keys():
                attrs = json_dict_to_attrs(edge["attrs"])
                loaded_edges.append((s_node, t_node, attrs))
            else:
                loaded_edges.append((s_node, t_node))
    return loaded_edges 
Example 11
Source File: utils.py    From ReGraph with MIT License 6 votes vote down vote up
def load_nodes_from_json(j_data):
    """Load nodes from json-like dict."""
    loaded_nodes = []
    if "nodes" in j_data.keys():
        j_nodes = j_data["nodes"]
        for node in j_nodes:
            if "id" in node.keys():
                node_id = node["id"]
            else:
                raise ReGraphError(
                    "Error loading graph: node id is not specified!")
            attrs = None
            if "attrs" in node.keys():
                attrs = json_dict_to_attrs(node["attrs"])
            loaded_nodes.append((node_id, attrs))
    else:
        raise ReGraphError(
            "Error loading graph: no nodes specified!")
    return loaded_nodes 
Example 12
Source File: themes.py    From python-inquirer with MIT License 6 votes vote down vote up
def load_theme_from_json(json_theme):
    """
    Load a theme from a json.
    Expected format:
    {
        "Question": {
            "mark_color": "yellow",
            "brackets_color": "normal",
            ...
        },
        "List": {
            "selection_color": "bold_blue",
            "selection_cursor": "->"
        }
    }

    Color values should be string representing valid blessings.Terminal colors.
    """
    return load_theme_from_dict(json.loads(json_theme)) 
Example 13
Source File: json.py    From pyABC with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def load_dict_from_json(file_: str, key_type: type = int):
    """
    Read in json file. Convert keys to `key_type'.
    Inverse to `save_dict_to_json`.

    Parameters
    ----------
    file_:
        Name of the file to read in.
    key_type:
        Type to convert the keys into.

    Returns
    -------
    dct: The json file contents.
    """
    with open(file_, 'r') as f:
        _dct = json.load(f)
    dct = {}
    for key, val in _dct.items():
        dct[int(key)] = val
    return dct 
Example 14
Source File: demo_adblock_analysis.py    From info-flow-experiments with GNU General Public License v3.0 6 votes vote down vote up
def load_ads_from_json(log_name,session):
    # The save file name format is "adb_logfile.session.json
    dirname = os.path.dirname(log_name)
    base=os.path.splitext(os.path.basename(log_name))[0][4:]
    json_name  = base+"."+session+".json" 
    json_file = os.path.join(dirname,json_name)
    with open(json_file, 'r') as infile:
        raw_ad_lines = json.load(infile)
    
    print("loaded {} lines from session: {}".format(len(raw_ad_lines),session))

    ad_lines =[]
    for line in raw_ad_lines:
        # parse line back into a named tuple, properly encoding to utf-8
        utf8_line  = map(lambda s: s.encode('utf-8') if not isinstance(s, dict) and not isinstance(s,int) else s, line)
        ad_lines.append(Ad(*utf8_line))

    return ad_lines 
Example 15
Source File: baseextractor.py    From spikeextractors with MIT License 6 votes vote down vote up
def load_extractor_from_json(json_file):
        '''
        Instantiates extractor from json file

        Parameters
        ----------
        json_file: str or Path
            Path to json file

        Returns
        -------
        extractor: RecordingExtractor or SortingExtractor
            The loaded extractor object
        '''
        json_file = Path(json_file)
        with open(str(json_file), 'r') as f:
            d = json.load(f)
            extractor = _load_extractor_from_dict(d)
        return extractor 
Example 16
Source File: config.py    From tf-crnn with GNU General Public License v3.0 6 votes vote down vote up
def load_lookup_from_json(cls, json_filenames: Union[List[str], str]) -> dict:
        """
        Load a lookup table from a json file to a dictionnary
        :param json_filenames: either a filename or a list of filenames
        :return:
        """

        lookup = dict()
        if isinstance(json_filenames, list):
            for file in json_filenames:
                with open(file, 'r', encoding='utf8') as f:
                    data_dict = json.load(f)
                lookup.update(data_dict)

        elif isinstance(json_filenames, str):
            with open(json_filenames, 'r', encoding='utf8') as f:
                lookup = json.load(f)

        return cls.map_lookup(lookup) 
Example 17
Source File: sticker_message.py    From bale-bot-python with Apache License 2.0 6 votes vote down vote up
def load_from_json(cls, json):
        if isinstance(json, dict):
            json_dict = json
        elif isinstance(json, str):
            json_dict = json_handler.loads(json)
        else:
            raise ValueError(Error.unacceptable_json)

        sticker_id = json_dict.get("stickerId", None)
        sticker_collection_id = json_dict.get("stickerCollectionId", None)
        sticker_collection_access_hash = json_dict.get("stickerCollectionAccessHash", None)
        fast_preview = json_dict.get("fastPreview ", None)
        image512 = ImageLocation.load_from_json(json_dict.get("image512", None)) if json_dict.get("image512",
                                                                                                  None) else None
        image256 = ImageLocation.load_from_json(json_dict.get("image256", None)) if json_dict.get("image256",
                                                                                                  None) else None

        return cls(sticker_id=sticker_id, sticker_collection_id=sticker_collection_id,
                   sticker_collection_access_hash=sticker_collection_access_hash, fast_preview=fast_preview,
                   image512=image512, image256=image256) 
Example 18
Source File: load_data.py    From calculator_of_Onmyoji with GNU General Public License v2.0 6 votes vote down vote up
def load_json_from_editor(data, ignore_serial):
    '''从网页版御魂编辑器读取数据'''
    def mitama_json_to_dict(json_obj):
        MITAMA_COL_MAP = {'御魂序号': 'id', '御魂类型': 'name',
                          '位置': 'pos'}
        serial = json_obj['id']
        if skip_serial(serial, ignore_serial):
            return None
        mitama = {}
        for col_name in data_format.MITAMA_COL_NAME_ZH[1:]:
            if col_name in MITAMA_COL_MAP:
                mitama[col_name] = json_obj[MITAMA_COL_MAP[col_name]]
            else:
                mitama[col_name] = 0

        for props in [json_obj['mainAttr'],
                      json_obj['addonAttr']] + json_obj['addiAttr']:
            if props['attrName'] in data_format.MITAMA_PROPS:
                mitama[props['attrName']] += float(props['attrVal'])

        return (serial, mitama)

    mitama_list = map(mitama_json_to_dict, data['data'])
    return dict([x for x in mitama_list if x]) 
Example 19
Source File: passer_lib.py    From passer with GNU General Public License v3.0 5 votes vote down vote up
def load_json_from_file(json_filename):
	"""Bring in json content from a file and return it as a python data structure (or None if not successful for any reason)."""

	ljff_return = None

	if os.path.exists(json_filename) and os.access(json_filename, os.R_OK):
		try:
			with open(json_filename) as json_h:
				ljff_return = json.loads(json_h.read())
		except:
			pass

	return ljff_return 
Example 20
Source File: keypoints_to_graph.py    From cvToolkit with MIT License 5 votes vote down vote up
def load_graph_pairs_from_json(json_file_path):
    python_data = read_json_from_file(json_file_path)
    num_imgs = len(python_data)

    track_id_dict = {}
    for track_id in range(100):
        track_id_dict[track_id] = []

    max_track_id = -1
    for img_id in range(num_imgs):
        image_id = python_data[img_id]["image"]["id"]
        candidates = python_data[img_id]["candidates"]

        num_candidates = len(candidates)
        for candidate_id in range(num_candidates):
            candidate = candidates[candidate_id]
            track_id = candidate["track_id"]
            keypoints = candidate["pose_keypoints_2d"]
            bbox = candidate["det_bbox"]

            if track_id > max_track_id:
                max_track_id = track_id

            candidate_dict = {"track_id": track_id,
                              "img_id": image_id,
                              "bbox": bbox,
                              "keypoints":keypoints}
            track_id_dict[track_id].append(candidate_dict)

    graph_pair_list_all = []
    for track_id in range(max_track_id):
        candidate_dict_list = track_id_dict[track_id]
        candidate_dict_list_sorted = sorted(candidate_dict_list, key=lambda k:k['img_id'])

        graph_pair_list = get_graph_pairs(candidate_dict_list_sorted)
        graph_pair_list_all.extend(graph_pair_list)
    return  graph_pair_list_all 
Example 21
Source File: user.py    From GadioVideo with MIT License 5 votes vote down vote up
def load_from_json(cls, parsed_json: str):
        """[summary]
        
        Arguments:
            parsed_json {str} -- json for user
        
        Raises:
            LookupError: Raised when json object is incorrect
            LookupError: Raised when json object is not for initializing user
        
        Returns:
            User -- a instance initialized with json attributes
        """
        try:
            json_type = parsed_json['type']
        except:
            raise LookupError('Incorrect json passed to user')

        if (parsed_json['type'] != "users"):
            raise AttributeError('Json passed to user is not for user')

        try:
            instance = cls(user_id=parsed_json['id'],
                        nickname=parsed_json['attributes']['nickname'],
                        image_id=parsed_json['attributes']['thumb'])
            return instance
        except:
            raise KeyError('Json does not include necessary user attributes') 
Example 22
Source File: abstractive_summarization_seq2seq.py    From nlp-recipes with MIT License 5 votes vote down vote up
def load_from_json(cls, json_file):
        config = cls()
        with open(json_file, "r") as f:
            config.__dict__ = json.load(f)
        return config 
Example 23
Source File: dialog.py    From cakechat with Apache License 2.0 5 votes vote down vote up
def load_processed_dialogs_from_json(lines, text_field_name, condition_field_name):
    for line_json in JsonTextLinesIterator(lines):
        yield [{
            text_field_name: entry[DIALOG_TEXT_FIELD],
            condition_field_name: entry[DIALOG_CONDITION_FIELD]
        } for entry in line_json] 
Example 24
Source File: io.py    From convis with GNU General Public License v3.0 5 votes vote down vote up
def load_dict_from_json(filename):
    """
        Loads a (flat) dictionary from a json file and converts
        lists back into numpy arrays.
    """
    with open(filename,'r') as fp:
        dat = json.load(fp)
        assert(type(dat) == dict)
        dat = dict([(p,_json_safe_to_value(param)) for (p,param) in _iteritems(dat)])
    return dat 
Example 25
Source File: file_util.py    From scfcli with Apache License 2.0 5 votes vote down vote up
def load_json_from_file(p):
        if p is None:
            return {}
        try:
            with io.open(p, mode='r', encoding='utf-8') as fp:
                return json.load(fp)
        except Exception as e:
            Operation(e, err_msg=traceback.format_exc(), level="ERROR").no_output()
            raise InvalidEnvVarsException('read environment from file {} failed: {}'.format(p, str(e))) 
Example 26
Source File: file_ops.py    From raiden-contracts with MIT License 5 votes vote down vote up
def load_json_from_path(f: Path) -> Optional[Dict[str, Any]]:
    try:
        with f.open() as deployment_file:
            return json.load(deployment_file)
    except FileNotFoundError:
        return None
    except (JSONDecodeError, UnicodeDecodeError) as ex:
        raise ValueError(f"Deployment data file is corrupted: {ex}") from ex 
Example 27
Source File: restore.py    From rasa-for-botfront with Apache License 2.0 5 votes vote down vote up
def load_tracker_from_json(tracker_dump: Text, domain: Domain) -> DialogueStateTracker:
    """Read the json dump from the file and instantiate a tracker it."""

    tracker_json = json.loads(rasa.utils.io.read_file(tracker_dump))
    sender_id = tracker_json.get("sender_id", UserMessage.DEFAULT_SENDER_ID)
    return DialogueStateTracker.from_dict(
        sender_id, tracker_json.get("events", []), domain.slots
    ) 
Example 28
Source File: _entity.py    From ballistica with MIT License 5 votes vote down vote up
def load_from_json_str(self, s: str, error: bool = True) -> None:
        """Set the entity's data in-place from a json string.

        The 'error' argument determines whether Exceptions will be raised
        for invalid data values. Values will be reset/conformed to valid ones
        if error is False. Note that Exceptions will always be raised
        in the case of invalid formatted json.
        """
        data = self.json_loads(s)
        self.set_data(data, error=error) 
Example 29
Source File: commerce_setup_helper.py    From atg-commerce-iaas with MIT License 5 votes vote down vote up
def load_json_from_url(jsonUrl, key):
    """
    Read json data from a URL
    """      
    try:
        data = urllib2.urlopen(jsonUrl)
    except urllib2.URLError, e:
        logger.error("url error") 
Example 30
Source File: commerce_setup_helper.py    From atg-commerce-iaas with MIT License 5 votes vote down vote up
def load_json_from_file(jsonFile, key):
    """
    Read json data from a file on the filesystem
    """        
    with open(jsonFile) as data_file:    
        data = json.load(data_file)
        if key not in data:
            raise ValueError ("Root " + key + " item missing")   
        return data[key] 
Example 31
Source File: OnlineNeuralNetwork.py    From onn with Apache License 2.0 5 votes vote down vote up
def load_params_from_json(self, json_data):
        params = json.loads(json_data)
        o_dict = collections.OrderedDict()
        for key, tensor in params.items():
            o_dict[key] = torch.tensor(tensor).to(self.device)
        self.load_state_dict(o_dict) 
Example 32
Source File: common_utils.py    From NeuronBlocks with MIT License 5 votes vote down vote up
def load_from_json(json_path, debug=True):
    data = None
    with open(json_path, 'r', encoding='utf-8') as f:
        try:
            data = json.loads(f.read())
        except Exception as e:
            raise ConfigurationError("%s is not a legal JSON file, please check your JSON format!" % json_path)
    if debug:
        logging.debug("%s loaded!" % json_path)
    return data 
Example 33
Source File: basic_worm.py    From tierpsy-tracker with MIT License 5 votes vote down vote up
def load_from_JSON(self, JSON_path):
        with open(JSON_path, 'r') as infile:
            serialized_data = infile.read()

        member_list = json_to_data(serialized_data)

        for member in member_list:
            setattr(self, member[0], member[1])

#%% 
Example 34
Source File: keypoints_to_graph_triplet.py    From cvToolkit with MIT License 5 votes vote down vote up
def load_graph_triplets_from_json(json_file_path):
    python_data = read_json_from_file(json_file_path)
    num_imgs = len(python_data)

    track_id_dict = {}
    for track_id in range(100):
        track_id_dict[track_id] = []

    img_id_dict = {}
    for img_id in range(1000):
        img_id_dict[img_id] = []

    max_track_id = -1
    for img_id in range(num_imgs):
        image_id = python_data[img_id]["image"]["id"]
        candidates = python_data[img_id]["candidates"]

        num_candidates = len(candidates)
        for candidate_id in range(num_candidates):
            candidate = candidates[candidate_id]
            track_id = candidate["track_id"]
            keypoints = candidate["pose_keypoints_2d"]
            bbox = candidate["det_bbox"]

            if track_id > max_track_id:
                max_track_id = track_id

            candidate_dict = {"track_id": track_id,
                              "img_id": image_id,
                              "bbox": bbox,
                              "keypoints":keypoints}

            track_id_dict[track_id].append(candidate_dict)
            img_id_dict[img_id].append(candidate_dict)

    graph_triplet_list_all = get_graph_triplet(track_id_dict, img_id_dict, max_track_id)
    return  graph_triplet_list_all 
Example 35
Source File: utils.py    From golem with MIT License 5 votes vote down vote up
def load_json_from_file(filepath, ignore_failure=False, default=None):
    json_data = default
    with open(filepath, encoding='utf-8') as json_file:
        try:
            contents = json_file.read()
            if len(contents.strip()):
                json_data = json.loads(contents)
        except Exception as e:
            msg = 'There was an error parsing file {}'.format(filepath)
            print(msg)
            print(traceback.format_exc())
            if not ignore_failure:
                raise Exception(msg).with_traceback(e.__traceback__)
    return json_data 
Example 36
Source File: load_json.py    From how-to with Creative Commons Zero v1.0 Universal 5 votes vote down vote up
def load_json_from_file(filename):
    """
    Load JSON from a file.

    @input  filename  Name of the file to be read.
    @returns Output SFrame

    """
    # Read the entire file into a SFrame with one row
    sf = gl.SFrame.read_csv(filename, delimiter='\n', header=False)
    #  +--------------------------------+
    #  |               X1               |
    #  +--------------------------------+
    #  | [{'Phone': '0845 46 45', ' ... |
    #  +--------------------------------+
    
    # Stack the single large row into many rows. After stacking, each row is a
    # dictionary that contains the data. 
    sf = sf.stack('X1')
    
    #+--------------------------------+
    #|               X1               |
    #+--------------------------------+
    #| {'Phone': '0845 46 45', 'N ... |
    #| {'Phone': '0800 744 7888', ... |
    #| {'Phone': '(01303) 294965' ... |
    #|              ...               |
    #+--------------------------------+
    
    # The dictionary can be unpacked to generate the individual columns.
    sf = sf.unpack('X1', column_name_prefix='')
    return sf


# User the function on a toy example 
Example 37
Source File: click_models.py    From Unbiased-Learning-to-Rank-with-Unbiased-Propensity-Estimation with Apache License 2.0 5 votes vote down vote up
def loadModelFromJson(model_desc):
	click_model = PositionBiasedModel()
	if model_desc['model_name'] == 'user_browsing_model':
		click_model = UserBrowsingModel()
	click_model.eta = model_desc['eta']
	click_model.click_prob = model_desc['click_prob']
	click_model.exam_prob = model_desc['exam_prob']
	return click_model 
Example 38
Source File: utils.py    From ASER with MIT License 5 votes vote down vote up
def load_json_lines_from_file_multicore(fname, n_workers=0):
    with open(fname) as f:
        raw_records = f.readlines()
    if not n_workers:
        return [json.loads(record) for record in raw_records]
    raw_record_chunks = list(chunks(raw_records, len(raw_records) // (n_workers - 1)))
    res_list = []
    pool = Pool(n_workers)
    for chunk in raw_record_chunks:
        res = pool.apply_async(load_json_lines, args=(chunk, ))
        res_list.append(res)
    pool.close()
    pool.join()
    results = [t for item in res_list for t in item.get()]
    return results 
Example 39
Source File: gen.py    From cwg with GNU General Public License v3.0 5 votes vote down vote up
def load_data_from_json_file(working_dir, filename, parse_function):
    data = [];
    with open(os.path.join(working_dir, filename), 'r') as f:
        while 1:
            line = f.readline();
            if line == '':
                break;
            j = json.loads(line);
            data.append(parse_function(j));
    return data; 
Example 40
Source File: as_events.py    From altanalyze with Apache License 2.0 5 votes vote down vote up
def load_from_json_file(self, json_filename):
	# clear currently loaded events, if any
	self.clear_events()
	# Modify events directly
	t1 = time.time()
	self.events = json_utils.json_load_file(json_filename)
	t2 = time.time()
	print "Loading from JSON file took %.2f seconds." %(float(t2 - t1))
	self.num_events = len(self.events) 
Example 41
Source File: schema_loader.py    From faker-schema with MIT License 5 votes vote down vote up
def load_json_from_file(file_path):
    """Load schema from a JSON file"""
    try:
        with open(file_path) as f:
            json_data = json.load(f)
    except ValueError as e:
        raise ValueError('Given file {} is not a valid JSON file: {}'.format(file_path, e))
    else:
        return json_data 
Example 42
Source File: util.py    From AntNRE with Apache License 2.0 5 votes vote down vote up
def load_corpus_from_json_file(json_file: str,
                               save_dir: str,
                               entity_schema: str) -> List[Dict]:
    basename = os.path.basename(json_file)
    save_file = os.path.join(save_dir, basename.split('.')[0] + ".format")

    format_json_file(json_file, save_file, entity_schema)
    
    with open(save_file, "r", encoding="utf8") as f:
        instances = []
        instance = defaultdict(list)
        for line in f:
            line = line.rstrip()
            if line:
                line = line.split('\t')
                if len(line) == 3:
                    instance['tokens'].append(line[0])
                    instance['ent_span_labels'].append(line[1])
                    instance['ent_labels'].append(line[2])
                elif len(line) == 2:
                    instance['ent_ids'].append(eval(line[0]))
                    instance['ent_ids_labels'].append(line[1])
                elif len(line) == 7:
                    candi_rel = (eval(line[0]), eval(line[3]))
                    instance['candi_rels'].append(candi_rel)
                    instance['rel_labels'].append(line[-1])
            else:
                if instance:
                    assert len(instance['tokens']) == len(instance['ent_labels'])
                    assert len(instance['tokens']) == len(instance['ent_span_labels'])
                    assert len(instance['candi_rels']) == len(instance['rel_labels'])
                    assert len(instance['ent_ids']) == len(instance['ent_ids_labels'])
                    instances.append(instance)
                instance = defaultdict(list)
        if instance:
            assert len(instance['tokens']) == len(instance['ent_labels'])
            assert len(instance['tokens']) == len(instance['ent_span_labels'])
            assert len(instance['candi_rels']) == len(instance['rel_labels'])
            assert len(instance['ent_ids']) == len(instance['ent_ids_labels'])
            instances.append(instance)
    return instances 
Example 43
Source File: schema_loader.py    From faker-schema with MIT License 5 votes vote down vote up
def load_json_from_string(string):
    """Load schema from JSON string"""
    try:
        json_data = json.loads(string)
    except ValueError as e:
        raise ValueError('Given string is not valid JSON: {}'.format(e))
    else:
        return json_data 
Example 44
Source File: keypoints_to_graph_triplet.py    From video-to-pose3D with MIT License 5 votes vote down vote up
def load_graph_triplets_from_json(json_file_path):
    python_data = read_json_from_file(json_file_path)
    num_imgs = len(python_data)

    track_id_dict = {}
    for track_id in range(100):
        track_id_dict[track_id] = []

    img_id_dict = {}
    for img_id in range(1000):
        img_id_dict[img_id] = []

    max_track_id = -1
    for img_id in range(num_imgs):
        image_id = python_data[img_id]["image"]["id"]
        candidates = python_data[img_id]["candidates"]

        num_candidates = len(candidates)
        for candidate_id in range(num_candidates):
            candidate = candidates[candidate_id]
            track_id = candidate["track_id"]
            keypoints = candidate["pose_keypoints_2d"]
            bbox = candidate["det_bbox"]

            if track_id > max_track_id:
                max_track_id = track_id

            candidate_dict = {"track_id": track_id,
                              "img_id": image_id,
                              "bbox": bbox,
                              "keypoints": keypoints}

            track_id_dict[track_id].append(candidate_dict)
            img_id_dict[img_id].append(candidate_dict)

    graph_triplet_list_all = get_graph_triplet(track_id_dict, img_id_dict, max_track_id)
    return graph_triplet_list_all 
Example 45
Source File: questions.py    From python-inquirer with MIT License 5 votes vote down vote up
def load_from_json(question_json):
    """
    Load Questions from a JSON string.
    :return: A list of Question objects with associated data if the JSON
             contains a list or a Question if the JSON contains a dict.
    :return type: List or Dict
    """
    data = json.loads(question_json)
    if isinstance(data, list):
        return load_from_list(data)
    if isinstance(data, dict):
        return load_from_dict(data)
    raise TypeError(
        'Json contained a %s variable when a dict or list was expected',
        type(data)) 
Example 46
Source File: utils.py    From micromasters with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def load_json_from_file(project_rel_filepath):
    """
    Loads JSON data from a file
    """
    path = '{}/{}'.format(settings.BASE_DIR, project_rel_filepath)
    with open(path, 'r') as f:
        return json.load(f) 
Example 47
Source File: esa_cci_ftp.py    From cate with MIT License 5 votes vote down vote up
def load_from_json(self, json_fp_or_str: Union[str, IOBase]):
        if isinstance(json_fp_or_str, str):
            fp = StringIO(json_fp_or_str)
        else:
            fp = json_fp_or_str
        data_store_dict = json.load(fp)
        remote_url = data_store_dict.get('remote_url', self._remote_url)
        data_sources_json = data_store_dict.get('data_sources', [])
        data_sources = []
        for data in data_sources_json:
            file_set_info = None
            if 'start_date' in data and 'end_date' in data and 'num_files' in data and 'size_mb' in data:
                # TODO (mzuehlke, 20160603): used named parameters
                file_set_info = FileSetInfo('2016-05-26 15:32:52',
                                            # TODO (mzuehlke, 20160603): include scan time in JSON
                                            data['start_date'],
                                            data['end_date'],
                                            data['num_files'],
                                            data['size_mb'])

            # TODO (mzuehlke, 20160603): used named parameters
            file_set_data_source = FileSetDataSource(self,
                                                     # TODO (mzuehlke, 20160603): change this in the JSON file
                                                     data['name'].replace('/', '_').upper(),
                                                     data['base_dir'],
                                                     data['file_pattern'],
                                                     fileset_info=file_set_info)
            data_sources.append(file_set_data_source)

        self._remote_url = remote_url
        self._data_sources.extend(data_sources) 
Example 48
Source File: keypoints_to_graph.py    From video-to-pose3D with MIT License 5 votes vote down vote up
def load_graph_pairs_from_json(json_file_path):
    python_data = read_json_from_file(json_file_path)
    num_imgs = len(python_data)

    track_id_dict = {}
    for track_id in range(100):
        track_id_dict[track_id] = []

    max_track_id = -1
    for img_id in range(num_imgs):
        image_id = python_data[img_id]["image"]["id"]
        candidates = python_data[img_id]["candidates"]

        num_candidates = len(candidates)
        for candidate_id in range(num_candidates):
            candidate = candidates[candidate_id]
            track_id = candidate["track_id"]
            keypoints = candidate["pose_keypoints_2d"]
            bbox = candidate["det_bbox"]

            if track_id > max_track_id:
                max_track_id = track_id

            candidate_dict = {"track_id": track_id,
                              "img_id": image_id,
                              "bbox": bbox,
                              "keypoints": keypoints}
            track_id_dict[track_id].append(candidate_dict)

    graph_pair_list_all = []
    for track_id in range(max_track_id):
        candidate_dict_list = track_id_dict[track_id]
        candidate_dict_list_sorted = sorted(candidate_dict_list, key=lambda k: k['img_id'])

        graph_pair_list = get_graph_pairs(candidate_dict_list_sorted)
        graph_pair_list_all.extend(graph_pair_list)
    return graph_pair_list_all 
Example 49
Source File: transformer.py    From livebot with MIT License 5 votes vote down vote up
def load_from_json(fin):
    datas = []
    for line in fin:
        data = json.loads(line)
        datas.append(data)
    return datas 
Example 50
Source File: task.py    From grimoirelab-sirmordred with GNU General Public License v3.0 5 votes vote down vote up
def load_aliases_from_json(aliases_json):
        with open(aliases_json, 'r') as f:
            try:
                aliases = json.load(f)
            except Exception as ex:
                logger.error(ex)
                raise

        return aliases 
Example 51
Source File: restore.py    From rasa_core with Apache License 2.0 5 votes vote down vote up
def load_tracker_from_json(tracker_dump: Text,
                           domain: Domain) -> DialogueStateTracker:
    """Read the json dump from the file and instantiate a tracker it."""

    tracker_json = json.loads(utils.read_file(tracker_dump))
    sender_id = tracker_json.get("sender_id", UserMessage.DEFAULT_SENDER_ID)
    return DialogueStateTracker.from_dict(sender_id,
                                          tracker_json.get("events", []),
                                          domain.slots) 
Example 52
Source File: keract.py    From keract with MIT License 5 votes vote down vote up
def load_activations_from_json_file(filename):
    """
    Read the activations from the disk
    :param filename: filename to read the activations from (JSON format)
    :return: activations (dict mapping layers)
    """
    import numpy as np
    with open(filename, 'r') as r:
        d = json.load(r, object_pairs_hook=OrderedDict)
        activations = OrderedDict({k: np.array(v) for k, v in d.items()})
        return activations 
Example 53
Source File: BaseSpider.py    From QQZoneMood with MIT License 5 votes vote down vote up
def load_data_from_json(self, file_name):
        try:
            with open(file_name, encoding='utf-8') as content:
                data = json.load(content)
            return data
        except BaseException as e:
            self.format_error(e, 'Failed to load data ' + file_name) 
Example 54
Source File: BaseSpider.py    From QQZoneMood with MIT License 5 votes vote down vote up
def load_all_data_from_json(self):
        self.content = self.load_data_from_json(self.CONTENT_FILE_NAME)
        self.like_list_names = self.load_data_from_json(self.LIKE_LIST_NAME_FILE_NAME)
        self.mood_details = self.load_data_from_json(self.MOOD_DETAIL_FILE_NAME)
        self.like_detail = self.load_data_from_json(self.LIKE_DETAIL_FILE_NAME)
        print("Success to Load Data From Json") 
Example 55
Source File: web_crawler.py    From images-web-crawler with GNU General Public License v3.0 5 votes vote down vote up
def load_urls_from_json(self, filename):
        """ Load links from a json file"""
        if not os.path.isfile(filename):
            self.error("Failed to load URLs, file '" + filename + "' does not exist")
        with open(filename) as links_file:
            self.images_links = json.load(links_file)
        print("\nLinks loaded from ", filename) 
Example 56
Source File: generic.py    From ReGraph with MIT License 5 votes vote down vote up
def load_graph_from_json_apoc(tx, json_data, node_label, edge_label,
                              tmp_dir=None):
    # store json-file somewhere, generate attr repr.
    if tmp_dir is None:
        tmp_dir = "/var/lib/neo4j/import/"
    path = tmp_dir + "kami_tmp.json"
    # fd, path = tempfile.mkstemp(prefix=tmp_dir)
    # try:
    with open(path, 'w+') as tmp:
        for n in json_data["nodes"]:
            n["attrs"] = generate_attributes_json(
                attrs_from_json(n["attrs"]))
            n["attrs"]["id"] = n["id"]
        for e in json_data["edges"]:
            e["attrs"] = generate_attributes_json(
                attrs_from_json(e["attrs"]))
        json.dump(json_data, tmp)

        # load nodes
        node_query = (
            "WITH 'file:///{}' AS url\n".format(path) +
            "CALL apoc.load.json(url) YIELD value\n" +
            "UNWIND value.nodes AS node\n" +
            "MERGE (n:{} {{ id: node.id }}) ON CREATE\n".format(node_label) +
            "\tSET n = node.attrs\n"
        )
        tx.run(node_query)

        # load edges
        edge_query = (
            "WITH 'file:///{}' AS url\n".format(path) +
            "CALL apoc.load.json(url) YIELD value\n" +
            "UNWIND value.edges AS edge\n" +
            "MATCH (u:{} {{ id: edge.from }}), (v:{} {{ id: edge.to }}) \n".format(
                node_label, node_label) +
            "MERGE (u)-[rel:{}]->(v)\n ON CREATE\n".format(edge_label) +
            "\tSET rel = edge.attrs\n"
        )
        tx.run(edge_query)
    # finally:
    #     os.remove(path) 
Example 57
Source File: common.py    From OpenBookQA with Apache License 2.0 5 votes vote down vote up
def load_json_from_file(file_name):
    """
    Loads items from a jsonl  file. Each line is expected to be a valid json.
    :param file_name: Jsonl file with single json object per line
    :return: List of serialized objects
    """
    items = []
    for line in open(file_name, mode="r"):
        item = json.loads(line.strip())
        items.append(item)

    return items 
Example 58
Source File: decompiled_cache.py    From GhIDA with Apache License 2.0 5 votes vote down vote up
def load_cache_from_json(self):
        try:
            print("GhIDA:: [DEBUG] loading decomp cache from json")
            with open(self.__cache_path) as f_in:
                self.__decompiled_cache = json.load(f_in)
                print("GhIDA:: [DEBUG] loaded %d items from cache" % len(
                    self.__decompiled_cache))
        except Exception:
            print("GhIDA:: [!] error while loading code from %s" %
                  self.__cache_path)
            self.__decompiled_cache = dict() 
Example 59
Source File: storage_utils.py    From RAFCON with Eclipse Public License 1.0 5 votes vote down vote up
def load_objects_from_json(path, as_dict=False):
    """Loads a dictionary from a json file.

    :param path: The relative path of the json file.
    :return: The dictionary specified in the json file
    """
    f = open(path, 'r')
    if as_dict:
        result = json.load(f)
    else:
        result = json.load(f, cls=JSONObjectDecoder, substitute_modules=substitute_modules)
    f.close()
    return result 
Example 60
Source File: recorder.py    From zero with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def load_from_json(self, file_name):
        tf.logging.info("Loading recoder file from {}".format(file_name))
        record = json.load(open(file_name, 'rb'))
        record = dict((key.encode("UTF-8"), value) for (key, value) in record.items())
        self.__dict__.update(record)