Python munch.munchify() Examples

The following are 30 code examples of munch.munchify(). 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 munch , or try the search function .
Example #1
Source File: __init__.py    From restapi with GNU General Public License v2.0 6 votes vote down vote up
def createNewDateFieldDefinition(name, alias='', autoUpdate=False):
        """Creates a json definition for a new date field.

        Args
            name: Name of new date field.
            alias: Optional field name for alias.
            autoUpdate: Optional boolean to automatically populate the field 
                with the current date/time when a new record is added or updated 
                (like editor tracking). The default is False.
        """

        return munch.munchify({
            NAME: name,
            TYPE: DATE_FIELD,
            ALIAS: alias or name,
            SQL_TYPE: SQL_TYPE_OTHER,
            NULLABLE: FALSE,
            EDITABLE: TRUE,
            DOMAIN: NULL,
            DEFAULT_VALUE: SQL_AUTO_DATE_EXP if autoUpdate else NULL
        }) 
Example #2
Source File: common_types.py    From restapi with GNU General Public License v2.0 6 votes vote down vote up
def getFeatureExtent(in_features):
    """Gets the extent for a FeatureSet() or GeometryCollection(), must be convertible
    to a GeometryCollection().

    Arg:
        in_features: Input features (Feature|FeatureSet|GeometryCollection|json).
    
    Returns:
        An envelope json structure (extent).
    """

    if not isinstance(in_features, GeometryCollection):
        in_features = GeometryCollection(in_features)

    extents = [g.envelopeAsJSON() for g in iter(in_features)]
    full_extent = {SPATIAL_REFERENCE: extents[0].get(SPATIAL_REFERENCE)}
    for attr, op in {XMIN: min, YMIN: min, XMAX: max, YMAX: max}.iteritems():
        full_extent[attr] = op([e.get(attr) for e in extents])
    return munch.munchify(full_extent) 
Example #3
Source File: __init__.py    From restapi with GNU General Public License v2.0 6 votes vote down vote up
def disableEditorTracking(self):
        """Disables editor tracking."""
        capabilities = self.get(CAPABILITIES, '').split(',')
        editorInfo = self.get(EDITOR_TRACKING_INFO, {
            "enableEditorTracking": False,
            "enableOwnershipAccessControl": False,
            "allowOthersToUpdate": True,
            "allowOthersToDelete": True,
            "allowOthersToQuery": True,
            "allowAnonymousToUpdate": True,
            "allowAnonymousToDelete": True
          })
        editorInfo["enableEditorTracking"] = False

        # enable editor tracking at Feature Service level
        result = {}
        if CHANGE_TRACKING in capabilities:
            capabilities.remove(CHANGE_TRACKING)
            capabilities = ','.join(capabilities)
            result['disabled_at_feature_service'] = self.updateDefinition({CAPABILITIES: capabilities, HAS_STATIC_DATA: self.get(HAS_STATIC_DATA), EDITOR_TRACKING_INFO: editorInfo})
        else:
            result['disabled_at_feature_service'] = {'status': 'already disabled'}

        return munch.munchify(result) 
Example #4
Source File: common_types.py    From restapi with GNU General Public License v2.0 6 votes vote down vote up
def unqualify_fields(fs):
    """Removes fully qualified field names from a feature set.

    Arg:
        fs: restapi.FeatureSet() object or JSON.
    """

    if not isinstance(fs, FeatureSet):
        fs = FeatureSet(fs)

    clean_fields = {}
    for f in fs.fields:
        if f:
            clean = f.name.split('.')[-1]
            clean_fields[f.name] = clean
            f.name = clean

    for i,feature in enumerate(fs.features):
        feature_copy = {}
        for f, val in six.iteritems(feature.attributes):
            feature_copy[clean_fields.get(f, f)] = val
        fs.features[i].attributes = munch.munchify(feature_copy) 
Example #5
Source File: rest_utils.py    From restapi with GNU General Public License v2.0 6 votes vote down vote up
def _spatialReference(self):
        """Gets the spatial reference dict."""
        resp_d = {}
        if SPATIAL_REFERENCE in self.json:
            resp_d = self.json[SPATIAL_REFERENCE]
        elif self.json.get(EXTENT) and SPATIAL_REFERENCE in self.json[EXTENT]:
            resp_d = self.json[EXTENT][SPATIAL_REFERENCE]
        elif GEOMETRIES in self.json:
            try:
                first = self.json.get(GEOMETRIES, [])[0]
                resp_d = first.get(SPATIAL_REFERENCE) or {}
            except IndexError:
                pass
        elif CRS in self.json:
            resp_d = self.json.get(CRS, {})
        return munch.munchify(resp_d) 
Example #6
Source File: rest_utils.py    From restapi with GNU General Public License v2.0 6 votes vote down vote up
def __init__(self, in_json):
        """Inits class with JSON as geo feature set.

        Arg:
            in_json: Input json response from request.
        """

        if isinstance(in_json, six.string_types):
            if not in_json.startswith('{') and os.path.isfile(in_json):
                with open(in_json, 'r') as f:
                    in_json = json.load(f)
            else:
                in_json = json.loads(in_json)
        if isinstance(in_json, self.__class__):
            self.json = in_json.json
        elif isinstance(in_json, dict):
            self.json = munch.munchify(in_json) 
Example #7
Source File: config.py    From lsq-net with MIT License 6 votes vote down vote up
def get_config(default_file):
    p = argparse.ArgumentParser(description='Learned Step Size Quantization')
    p.add_argument('config_file', metavar='PATH', nargs='+',
                   help='path to a configuration file')
    arg = p.parse_args()

    with open(default_file) as yaml_file:
        cfg = yaml.safe_load(yaml_file)

    for f in arg.config_file:
        if not os.path.isfile(f):
            raise FileNotFoundError('Cannot find a configuration file at', f)
        with open(f) as yaml_file:
            c = yaml.safe_load(yaml_file)
            cfg = merge_nested_dict(cfg, c)

    return munch.munchify(cfg) 
Example #8
Source File: rest_utils.py    From restapi with GNU General Public License v2.0 6 votes vote down vote up
def __init__(self, response):
        """Handler for GP Task Response.

        response: JSON response from GP Task execution.
        """
        self._values = {}
        self.json = munch.munchify(response)

        # get values cache
        if isinstance(self.results, dict):
            for key in self.results.keys():
                self.getValue(key)

        elif isinstance(self.results, list):
            for res in self.results:
                self.getValue(res.get(PARAM_NAME)) 
Example #9
Source File: test_sync_service.py    From basecrm-python with Apache License 2.0 6 votes vote down vote up
def test_fetch_got_data(self):
        queue_items = munchify({
            'items': [{
                'data': {
                    'id': 1
                },
                'meta': {
                    'type': 'user',
                    'sync': {
                        'event_type': 'created',
                        'ack_key': 'User-1234-1',
                        'revision': 1
                    }
                }
            }]
        })
        http_client = mock.create_autospec(basecrm.HttpClient)
        http_client.get.return_value = (200, {}, queue_items)

        sync = basecrm.SyncService(http_client)
        self.assertEquals(sync.fetch(self.device_uuid, self.session_id), queue_items['items'])
        http_client.get.assert_called_once_with("/sync/{session_id}/queues/main".format(session_id=self.session_id),
                                                params=None,
                                                headers={'X-Basecrm-Device-UUID': self.device_uuid},
                                                raw=True) 
Example #10
Source File: rest_utils.py    From restapi with GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, jobInfo):
        self.json = munch.munchify(jobInfo) 
Example #11
Source File: common_types.py    From restapi with GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, in_json):
        """Creates a JSON form input JSON."""
        self.json = munch.munchify(in_json)
        super(self.__class__, self).__init__() 
Example #12
Source File: rest_utils.py    From restapi with GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, feature):
        """Inits the class with a feature.

        Arg:
            feature: Input json for feature.
        """
        
        self.json = munch.munchify(feature)
        self._propsGetter = ATTRIBUTES if ATTRIBUTES in self.json else PROPERTIES
        self._type = GEOJSON if self._propsGetter == PROPERTIES else ESRI_JSON_FORMAT 
Example #13
Source File: rest_utils.py    From restapi with GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, in_json):
        """Inits class with json for query related records.

        Arg:
            in_json: json response for query related records operation.
        """
        
        self.json = munch.munchify(in_json)
        self.geometryType = self.json.get(enums.geometry.type)
        self.spatialReference = self.json.get(SPATIAL_REFERENCE) 
Example #14
Source File: rest_utils.py    From restapi with GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, response):
        """Response JSON object from generate_token."""
        self.json = munch.munchify(response)
        super(JsonGetter, self).__init__()
        self._cookie = {AGS_TOKEN: self.token}
        self._portal = self.json.get('_{}'.format(PORTAL_INFO))
        if '_portalInfo' in self.json:
            del self.json._portalInfo
##        self.isAGOL = self.json.get(IS_AGOL, False)
##        self.isAdmin = self.json.get(IS_ADMIN, False) 
Example #15
Source File: __init__.py    From restapi with GNU General Public License v2.0 5 votes vote down vote up
def createNewGlobalIdFieldDefinition():
        """Adds a new global id field json defition."""
        return munch.munchify({
            NAME: 'GlobalID',
            TYPE: GLOBALID,
            ALIAS: 'GlobalID',
            SQL_TYPE: SQL_TYPE_OTHER,
            NULLABLE: FALSE,
            EDITABLE: FALSE,
            DOMAIN: NULL,
            DEFAULT_VALUE: SQL_GLOBAL_ID_EXP
        }) 
Example #16
Source File: rest_utils.py    From restapi with GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, error):
        self.json = munch.munchify(error)
        self.showWarning() 
Example #17
Source File: rest_utils.py    From restapi with GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, res_dict, feature_id=None):
        """Inits class with response
        
        Args:
            res_dict: Dictionary of response. 
        """
        RequestError(res_dict)
        self.json = munch.munchify(res_dict) 
Example #18
Source File: utils.py    From models-comparison.pytorch with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __init__(self, opts, scale=0.875, random_crop=False, random_hflip=False, random_vflip=False):
        if type(opts) == dict:
            opts = munchify(opts)
        self.input_size = opts.input_size
        self.input_space = opts.input_space
        self.input_range = opts.input_range
        self.mean = opts.mean
        self.std = opts.std

        # https://github.com/tensorflow/models/blob/master/research/inception/inception/image_processing.py#L294
        self.scale = scale
        self.random_crop = random_crop
        self.random_hflip = random_hflip
        self.random_vflip = random_vflip

        tfs = []
        tfs.append(transforms.Resize(int(math.floor(max(self.input_size)/self.scale))))

        if random_crop:
            tfs.append(transforms.RandomCrop(max(self.input_size)))
        else:
            tfs.append(transforms.CenterCrop(max(self.input_size)))

        if random_hflip:
            tfs.append(transforms.RandomHorizontalFlip())

        if random_vflip:
            tfs.append(transforms.RandomVerticalFlip())

        tfs.append(transforms.ToTensor())
        tfs.append(ToSpaceBGR(self.input_space=='BGR'))
        tfs.append(ToRange255(max(self.input_range)==255))
        tfs.append(transforms.Normalize(mean=self.mean, std=self.std))

        self.tf = transforms.Compose(tfs) 
Example #19
Source File: loader.py    From SA-ctf_scoreboard with Creative Commons Zero v1.0 Universal 5 votes vote down vote up
def _load_global_setting(self, candidate, variables):
        """
        Load and render global setting with variables.
        :param candidate: Global setting as a `dict`
        :param variables: variables from context to render setting
        :return: A `Munch` object
        """
        candidate = candidate or {}
        proxy_setting = self._load_proxy(candidate.get('proxy'), variables)
        log_setting = self._load_logging(candidate.get('logging'), variables)

        return munchify({'proxy': proxy_setting, 'logging': log_setting}) 
Example #20
Source File: loader.py    From SA-ctf_scoreboard with Creative Commons Zero v1.0 Universal 5 votes vote down vote up
def _load_request(self, request):
        options = self._load_options(request['request'])

        pre_process = self._load_processor(request.get('pre_process', {}))
        post_process = self._load_processor(request['post_process'])
        checkpoint = self._load_checkpoint(request.get('checkpoint'))
        iteration_mode = self._load_iteration_mode(request['iteration_mode'])

        return munchify({
            'request': options,
            'pre_process': pre_process,
            'post_process': post_process,
            'checkpoint': checkpoint,
            'iteration_mode': iteration_mode,
        }) 
Example #21
Source File: loader.py    From SA-ctf_scoreboard with Creative Commons Zero v1.0 Universal 5 votes vote down vote up
def load(self, definition, schema_file, context):
        """Load cloud connect configuration from a `dict` and validate
        it with schema and global settings will be rendered.
        :param schema_file: Schema file location used to validate config.
        :param definition: A dictionary contains raw configs.
        :param context: variables to render template in global setting.
        :return: A `Munch` object.
        """
        try:
            validate(definition, self._get_schema_from_file(schema_file))
        except ValidationError:
            raise ConfigException(
                'Failed to validate interface with schema: {}'.format(
                    traceback.format_exc()))

        try:
            global_settings = self._load_global_setting(
                definition.get('global_settings'), context
            )

            requests = [self._load_request(item) for item in definition['requests']]

            return munchify({
                'meta': munchify(definition['meta']),
                'tokens': definition['tokens'],
                'global_settings': global_settings,
                'requests': requests,
            })
        except Exception as ex:
            error = 'Unable to load configuration: %s' % str(ex)
            _logger.exception(error)
            raise ConfigException(error) 
Example #22
Source File: settings.py    From qstrader with MIT License 5 votes vote down vote up
def from_file(fname=DEFAULT_CONFIG_FILENAME, testing=False):
    if testing:
        return TEST
    try:
        with open(os.path.expanduser(fname)) as fd:
            conf = yaml.load(fd)
        conf = munchify(conf)
        return conf
    except IOError:
        print("A configuration file named '%s' is missing" % fname)
        s_conf = yaml.dump(unmunchify(DEFAULT), explicit_start=True, indent=True, default_flow_style=False)
        print("""
Creating this file

%s

You still have to create directories with data and put your data in!
""" % s_conf)
        time.sleep(3)
        try:
            with open(os.path.expanduser(fname), "w") as fd:
                fd.write(s_conf)
        except IOError:
            print("Can create '%s'" % fname)
    print("Trying anyway with default configuration")
    return DEFAULT 
Example #23
Source File: main.py    From DeepBSDE with MIT License 5 votes vote down vote up
def main(argv):
    del argv
    with open(FLAGS.config_path) as json_data_file:
        config = json.load(json_data_file)
    config = munch.munchify(config)
    bsde = getattr(eqn, config.eqn_config.eqn_name)(config.eqn_config)
    tf.keras.backend.set_floatx(config.net_config.dtype)

    if not os.path.exists(FLAGS.log_dir):
        os.mkdir(FLAGS.log_dir)
    path_prefix = os.path.join(FLAGS.log_dir, FLAGS.exp_name)
    with open('{}_config.json'.format(path_prefix), 'w') as outfile:
        json.dump(dict((name, getattr(config, name))
                       for name in dir(config) if not name.startswith('__')),
                  outfile, indent=2)

    absl_logging.get_absl_handler().setFormatter(logging.Formatter('%(levelname)-6s %(message)s'))
    absl_logging.set_verbosity('info')

    logging.info('Begin to solve %s ' % config.eqn_config.eqn_name)
    bsde_solver = BSDESolver(config, bsde)
    training_history = bsde_solver.train()
    if bsde.y_init:
        logging.info('Y0_true: %.4e' % bsde.y_init)
        logging.info('relative error of Y0: %s',
                     '{:.2%}'.format(abs(bsde.y_init - training_history[-1, 2])/bsde.y_init))
    np.savetxt('{}_training_history.csv'.format(path_prefix),
               training_history,
               fmt=['%d', '%.5e', '%.5e', '%d'],
               delimiter=",",
               header='step,loss_function,target_value,elapsed_time',
               comments='') 
Example #24
Source File: errors.py    From basecrm-python with Apache License 2.0 5 votes vote down vote up
def __init__(self, http_status, errors_payload):
        """
        :param int http_status: Http status code.
        :param dict errors_payload: Json decoded payload from the errors response.
        """
        self.http_status = http_status
        self.errors = [munchify(error_envelope['error'])
                       for error_envelope in errors_payload['errors']]
        self.logref = errors_payload['meta']['logref']

        message = "\n".join([str(error) for error in self.errors])
        super(BaseError, self).__init__(message) 
Example #25
Source File: loader.py    From misp42splunk with GNU Lesser General Public License v3.0 5 votes vote down vote up
def _load_request(self, request):
        options = self._load_options(request['request'])

        pre_process = self._load_processor(request.get('pre_process', {}))
        post_process = self._load_processor(request['post_process'])
        checkpoint = self._load_checkpoint(request.get('checkpoint'))
        iteration_mode = self._load_iteration_mode(request['iteration_mode'])

        return munchify({
            'request': options,
            'pre_process': pre_process,
            'post_process': post_process,
            'checkpoint': checkpoint,
            'iteration_mode': iteration_mode,
        }) 
Example #26
Source File: loader.py    From misp42splunk with GNU Lesser General Public License v3.0 5 votes vote down vote up
def load(self, definition, schema_file, context):
        """Load cloud connect configuration from a `dict` and validate
        it with schema and global settings will be rendered.
        :param schema_file: Schema file location used to validate config.
        :param definition: A dictionary contains raw configs.
        :param context: variables to render template in global setting.
        :return: A `Munch` object.
        """
        try:
            validate(definition, self._get_schema_from_file(schema_file))
        except ValidationError:
            raise ConfigException(
                'Failed to validate interface with schema: {}'.format(
                    traceback.format_exc()))

        try:
            global_settings = self._load_global_setting(
                definition.get('global_settings'), context
            )

            requests = [self._load_request(item) for item in definition['requests']]

            return munchify({
                'meta': munchify(definition['meta']),
                'tokens': definition['tokens'],
                'global_settings': global_settings,
                'requests': requests,
            })
        except Exception as ex:
            error = 'Unable to load configuration: %s' % str(ex)
            _logger.exception(error)
            raise ConfigException(error) 
Example #27
Source File: loader.py    From misp42splunk with GNU Lesser General Public License v3.0 5 votes vote down vote up
def _load_global_setting(self, candidate, variables):
        """
        Load and render global setting with variables.
        :param candidate: Global setting as a `dict`
        :param variables: variables from context to render setting
        :return: A `Munch` object
        """
        candidate = candidate or {}
        proxy_setting = self._load_proxy(candidate.get('proxy'), variables)
        log_setting = self._load_logging(candidate.get('logging'), variables)

        return munchify({'proxy': proxy_setting, 'logging': log_setting}) 
Example #28
Source File: loader.py    From misp42splunk with GNU Lesser General Public License v3.0 5 votes vote down vote up
def _load_request(self, request):
        options = self._load_options(request['request'])

        pre_process = self._load_processor(request.get('pre_process', {}))
        post_process = self._load_processor(request['post_process'])
        checkpoint = self._load_checkpoint(request.get('checkpoint'))
        iteration_mode = self._load_iteration_mode(request['iteration_mode'])

        return munchify({
            'request': options,
            'pre_process': pre_process,
            'post_process': post_process,
            'checkpoint': checkpoint,
            'iteration_mode': iteration_mode,
        }) 
Example #29
Source File: utils.py    From cloud-inquisitor with Apache License 2.0 5 votes vote down vote up
def read_config():
    """Attempts to read the application configuration file and will raise a `FileNotFoundError` if the
    configuration file is not found. Returns the folder where the configuration file was loaded from, and
    a `Munch` (dict-like object) containing the configuration

    Configuration file paths searched, in order:
        * ~/.cinq/config.json'
        * ./config.json
        * /usr/local/etc/cloud-inquisitor/config.json

    Returns:
        `str`, `dict`
    """
    def __recursive_update(old, new):
        out = deepcopy(old)
        for k, v in new.items():
            if issubclass(type(v), dict):
                if k in old:
                    out[k] = __recursive_update(old[k], v)
                else:
                    out[k] = v
            else:
                out[k] = v

        return out

    for fpath in CONFIG_FILE_PATHS:
        if os.path.exists(fpath):
            data = munch.munchify(json.load(open(fpath, 'r')))

            # Our code expects a munch, so ensure that any regular dicts are converted
            return os.path.dirname(fpath), munch.munchify(__recursive_update(DEFAULT_CONFIG, data))

    raise FileNotFoundError('Configuration file not found') 
Example #30
Source File: cbsbuild.py    From rdopkg with Apache License 2.0 5 votes vote down vote up
def new_build(profile='cbs', scratch=True):
    # Very ugly: some utilities are only available in koji CLI
    # and not in the koji module
    if not KOJI_AVAILABLE:
        raise ModuleNotAvailable(module='koji')
    if not RPM_AVAILABLE:
        raise RpmModuleNotAvailable()
    import imp
    kojibin = find_executable('koji')
    kojicli = imp.load_source('kojicli', kojibin)
    # Koji 1.13 moves client internal API into another path
    try:
        from koji_cli.lib import _unique_path, _progress_callback, watch_tasks
    except ImportError:
        from kojicli import _unique_path, _progress_callback, watch_tasks

    kojiclient = setup_kojiclient(profile)
    # Note: required to make watch_tasks work
    kojicli.options = opts = munchify(kojiclient.opts)
    build_target = guess_build()
    if not build_target:
        log.warn("failed to identify build tag from branch")
        return

    retrieve_sources()
    srpm = create_srpm()
    if not srpm:
        log.warn('No srpm available')
        return
    serverdir = _unique_path('cli-build')
    kojiclient.uploadWrapper(srpm, serverdir, callback=_progress_callback)
    source = "%s/%s" % (serverdir, os.path.basename(srpm))
    task_id = kojiclient.build(source, build_target, {'scratch': scratch})
    print("Created task:", task_id)

    print("Task info: {}/taskinfo?taskID={}".format(opts['weburl'], task_id))
    kojiclient.logout()
    watch_tasks(kojiclient, [task_id])