Python json.get() Examples

The following are code examples for showing how to use json.get(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: alexa-apple-calendar   Author: zanderxyz   File: apple_calendar_api.py    MIT License 6 votes vote down vote up
def refresh_client(self, from_dt=None, to_dt=None):
        """
        Refreshes the CalendarService endpoint, ensuring that the
        event data is up-to-date. If no 'from_dt' or 'to_dt' datetimes
        have been given, the range becomes this month.
        """
        today = datetime.today()
        first_day, last_day = monthrange(today.year, today.month)
        if not from_dt:
            from_dt = datetime(today.year, today.month, first_day)
        if not to_dt:
            to_dt = datetime(today.year, today.month, last_day)
        params = dict(self.params)
        params.update({
            'lang': 'en-us',
            'usertz': get_localzone().zone,
            'startDate': from_dt.strftime('%Y-%m-%d'),
            'endDate': to_dt.strftime('%Y-%m-%d')
        })
        req = self.session.get(self._calendar_refresh_url, params=params)
        self.response = req.json() 
Example 2
Project: POI_Finder   Author: brandonxiang   File: coordTransform_utils.py    MIT License 6 votes vote down vote up
def geocode(address):
    """
    利用百度geocoding服务解析地址获取位置坐标
    :param address:需要解析的地址
    :return:
    """
    geocoding = {'s': 'rsv3',
                 'key': key,
                 'city': '全国',
                 'address': address}
    res = requests.get(
        "http://restapi.amap.com/v3/geocode/geo", params=geocoding)
    if res.status_code == 200:
        json = res.json()
        status = json.get('status')
        count = json.get('count')
        if status == '1' and int(count) >= 1:
            geocodes = json.get('geocodes')[0]
            lng = float(geocodes.get('location').split(',')[0])
            lat = float(geocodes.get('location').split(',')[1])
            return [lng, lat]
        else:
            return None
    else:
        return None 
Example 3
Project: retdec-regression-tests-framework   Author: avast   File: config_parser.py    MIT License 6 votes vote down vote up
def vtables(self):
        """Virtual tables (list of :class:`Vtable`).

        The returned list can be indexed by either positions (0, 1, ...) or
        vtable names. Example:

        .. code-block:: python

            module.vtables[0]       # Returns the first vtable.
            module.vtables['vt1']   # Returns the vtable named 'vt1'.

        """
        vtables = NamedObjectList()
        for vt in self.json.get('vtables', []):
            vtables.append(Vtable(vt))
        return vtables 
Example 4
Project: retdec-regression-tests-framework   Author: avast   File: config_parser.py    MIT License 6 votes vote down vote up
def classes(self):
        """C++ classes (list of :class:`Class`).

        The returned list can be indexed by either positions (0, 1, ...) or
        classes' names. Example:

        .. code-block:: python

            module.classes[0]        # Returns the first class.
            module.classes['class1'] # Returns the class named 'class1'.

        """
        classes = NamedObjectList()
        for c in self.json.get('classes', []):
            classes.append(Class(c))
        return classes 
Example 5
Project: luci-py   Author: luci   File: model.py    Apache License 2.0 6 votes vote down vote up
def decode_payload(cls, request):
    """Decode task payload.

    HugeTask controls its own payload entirely including urlencoding.
    It doesn't depend on any particular web framework.

    Args:
      request: a webapp Request instance.

    Returns:
      A dict of str to str. The same as the params argument to __init__.

    Raises:
      DeprecationWarning: When task payload constructed from an older
        incompatible version of mapreduce.
    """
    # TODO(user): Pass mr_id into headers. Otherwise when payload decoding
    # failed, we can't abort a mr.
    if request.headers.get(cls.PAYLOAD_VERSION_HEADER) != cls.PAYLOAD_VERSION:
      raise DeprecationWarning(
          "Task is generated by an older incompatible version of mapreduce. "
          "Please kill this job manually")
    return cls._decode_payload(request.body) 
Example 6
Project: luci-py   Author: luci   File: model.py    Apache License 2.0 6 votes vote down vote up
def _decode_payload(cls, body):
    compressed_payload_str = None
    if body.startswith(cls.PAYLOAD_KEY_PARAM):
      payload_key = body[len(cls.PAYLOAD_KEY_PARAM):]
      payload_entity = _HugeTaskPayload.get(payload_key)
      compressed_payload_str = payload_entity.payload
    elif body.startswith(cls.PAYLOAD_PARAM):
      compressed_payload_str = body[len(cls.PAYLOAD_PARAM):]

    if compressed_payload_str:
      payload_str = zlib.decompress(compressed_payload_str)
    else:
      payload_str = body

    result = {}
    for (name, value) in cgi.parse_qs(payload_str).items():
      if len(value) == 1:
        result[name] = value[0]
      else:
        result[name] = value
    return result 
Example 7
Project: luci-py   Author: luci   File: model.py    Apache License 2.0 6 votes vote down vote up
def find_all_by_mapreduce_state(cls, mapreduce_state):
    """Find all shard states for given mapreduce.

    Args:
      mapreduce_state: MapreduceState instance

    Yields:
      shard states sorted by shard id.
    """
    keys = cls.calculate_keys_by_mapreduce_state(mapreduce_state)
    i = 0
    while i < len(keys):
      @db.non_transactional
      def no_tx_get(i):
        return db.get(keys[i:i+cls._MAX_STATES_IN_MEMORY])
      # We need a separate function to so that we can mix non-transactional and
      # use be a generator
      states = no_tx_get(i)
      for s in states:
        i += 1
        if s is not None:
          yield s 
Example 8
Project: luci-py   Author: luci   File: model.py    Apache License 2.0 6 votes vote down vote up
def _decode_payload(cls, body):
    compressed_payload_str = None
    if body.startswith(cls.PAYLOAD_KEY_PARAM):
      payload_key = body[len(cls.PAYLOAD_KEY_PARAM):]
      payload_entity = _HugeTaskPayload.get(payload_key)
      compressed_payload_str = payload_entity.payload
    elif body.startswith(cls.PAYLOAD_PARAM):
      compressed_payload_str = body[len(cls.PAYLOAD_PARAM):]

    if compressed_payload_str:
      payload_str = zlib.decompress(compressed_payload_str)
    else:
      payload_str = body

    result = {}
    for (name, value) in cgi.parse_qs(payload_str).items():
      if len(value) == 1:
        result[name] = value[0]
      else:
        result[name] = value
    return result 
Example 9
Project: luci-py   Author: luci   File: model.py    Apache License 2.0 6 votes vote down vote up
def find_all_by_mapreduce_state(cls, mapreduce_state):
    """Find all shard states for given mapreduce.

    Args:
      mapreduce_state: MapreduceState instance

    Yields:
      shard states sorted by shard id.
    """
    keys = cls.calculate_keys_by_mapreduce_state(mapreduce_state)
    i = 0
    while i < len(keys):
      @db.non_transactional
      def no_tx_get(i):
        return db.get(keys[i:i+cls._MAX_STATES_IN_MEMORY])
      # We need a separate function to so that we can mix non-transactional and
      # use be a generator
      states = no_tx_get(i)
      for s in states:
        i += 1
        if s is not None:
          yield s 
Example 10
Project: trial2rev   Author: evidence-surveillance   File: views.py    MIT License 6 votes vote down vote up
def blank():
    """ load ftext review page """
    if not current_user.is_authenticated:
        return "Sorry! This action is only available to logged-in users", 401, {
            'ContentType': 'application/json'}

    review_id = request.args.get('id')

    if not review_id:
        review_id = crud.get_most_recent_ftext_review(current_user.db_id)
        return redirect('/blank?id=%s' % review_id)

    review = crud.get_ftext_review(review_id, current_user.db_id)
    if not review:
        return "That resource does not exist.", 404, {
            'ContentType': 'application/json'}

    if current_user.db_id != review['user_id']:
        return "Sorry! You do not have access to view this.", 403, {
            'ContentType': 'application/json'}
    return render_template('blank.html', review_id=review_id, abstract=review['abstract'], title=review['title']) 
Example 11
Project: trial2rev   Author: evidence-surveillance   File: views.py    MIT License 6 votes vote down vote up
def change_password():
    """
    change password of current user
    @return: refreshed page content indicating success or failure
    """
    changepw_form = ChangePasswordForm(request.form)
    user = User.get(current_user.id)
    if changepw_form.validate_on_submit() and user:
        if user.check_password(changepw_form.current_password.data):
            User.change_password(user, User.set_password(user, changepw_form.new_password.data))
            flash("Password Changed")
        else:
            flash("Current password invalid")
    else:
        flash("Email address invalid")
        return render_template('profile.html', changepw_form=changepw_form)
    return render_template('profile.html', changepw_form=changepw_form) 
Example 12
Project: trial2rev   Author: evidence-surveillance   File: views.py    MIT License 6 votes vote down vote up
def reset():
    """
    send reset password email to specified user email
    @return: refreshed page indicating success or failure
    """
    form = ForgotPasswordForm()
    if form.validate_on_submit():
        user = User.get(form.forgot_email.data)
        subject = "Password reset requested"
        token = ts.dumps(user.id, salt='recover-key')
        recover_url = url_for(
            'reset_with_token',
            token=token,
            _external=True)
        html = render_template(
            'recover.html',
            recover_url=recover_url)
        msg = Message(subject, sender=config.MAIL_USERNAME, recipients=[user.id])
        msg.body = html
        mail.send(msg)
        flash('Password reset email sent to ' + user.id)
        return redirect(url_for('login'))
    return render_template('login.html', loginform=EmailPasswordForm(), forgotpw=ForgotPasswordForm(),
                           accessform=RequestRegisterForm()) 
Example 13
Project: trial2rev   Author: evidence-surveillance   File: views.py    MIT License 6 votes vote down vote up
def reset_with_token(token):
    """
    reset password with email token
    @param token: unique token
    @type token: str
    @return: refreshed page indicating success or failure
    """
    try:
        email = ts.loads(token, salt="recover-key", max_age=86400)
    except:
        abort(404)
    form = PasswordForm()
    if form.validate_on_submit():
        user = User.get(email)
        password = form.password.data
        user.change_password(user.set_password(password))
        login_user(user)
        flash('Password changed successfully!')
        return redirect(url_for('main'))
    return render_template('reset_with_token.html', form=form, token=token) 
Example 14
Project: Blockchain   Author: Younes-Charfaoui   File: chacoin003.py    Apache License 2.0 5 votes vote down vote up
def get_previous_block(self):
        return self.chain[-1]
    
    # this method will get the nonce to make a valid blockchain. 
Example 15
Project: Blockchain   Author: Younes-Charfaoui   File: chacoin003.py    Apache License 2.0 5 votes vote down vote up
def add_node(self, address):
        # we get the address and parsed it.
        parsed_url = urlparse(address)
        
        # Then we take the netloc of the adress and add to the set.
        # example of netloc "https://127.0.0.1:5000/" netloc is "127.0.0.1:5000"
        self.nodes.add(parsed_url.netloc)
     
    # function that make concencus over the network of nodes. 
Example 16
Project: Blockchain   Author: Younes-Charfaoui   File: chacoin003.py    Apache License 2.0 5 votes vote down vote up
def replace_chain(self):
        # getting all the nodes
        network = self.nodes
        
        # creating variables to hold reference to the bigest chain and it's length
        longest_chain = None
        max_length = len(self.chain)
        for node in network:
            
            # requesting the chain in every node
            response = requests.get(f'http://{node}/get_chain')
            
            if response.status_code == 200:
                # getting the chain and the length of it in the current node
                length = response.json()['length']
                chain =response.json()['chain']
                
                # if the case that the current chain is bigger than the old one we update
                if length > max_length and self.is_chain_valid(chain):
                    max_length = length
                    longest_chain = chain
        # if the longest chain was updated then we update our chain.
        if longest_chain:
            self.chain = longest_chain
            # and we return true.
            return True
        return False

# Part 2 - Mining our Blockchain

# Creating web app 
Example 17
Project: Blockchain   Author: Younes-Charfaoui   File: chacoin003.py    Apache License 2.0 5 votes vote down vote up
def connect_node():
    json = request.get_json()
    nodes = json.get('nodes')
    if nodes is  None:
        return "No nodes",400
    for node in nodes:
        blockchain.add_node(node)
    response = {'message' : 'All the node are connected' , 
                'total_nodes' : list(blockchain.nodes)}
    return jsonify(response),200

# Replacing by the new longest chain. 
Example 18
Project: Blockchain   Author: Younes-Charfaoui   File: chacoin.py    Apache License 2.0 5 votes vote down vote up
def get_previous_block(self):
        return self.chain[-1]
    
    # this method will get the nonce to make a valid blockchain. 
Example 19
Project: Blockchain   Author: Younes-Charfaoui   File: chacoin.py    Apache License 2.0 5 votes vote down vote up
def replace_chain(self):
        # getting all the nodes
        network = self.nodes
        
        # creating variables to hold reference to the bigest chain and it's length
        longest_chain = None
        max_length = len(self.chain)
        for nodes in network:
            
            # requesting the chain in every node
            response = requests.get(f'http://{node}/get_chain')
            
            if response.status_code == 200:
                # getting the chain and the length of it in the current node
                length = response.json()['length']
                chain =response.json()['chain']
                
                # if the case that the current chain is bigger than the old one we update
                if length > max_length and self.is_chain_valid(chain):
                    max_length = length
                    longest_chain = chain
        # if the longest chain was updated then we update our chain.
        if longest_chain:
            self.chain = longest_chain
            # and we return true.
            return True
        return False

# Part 2 - Mining our Blockchain

# Creating web app 
Example 20
Project: Blockchain   Author: Younes-Charfaoui   File: chacoin.py    Apache License 2.0 5 votes vote down vote up
def connect_node():
    json = request.get_json()
    nodes = json.get('nodes')
    if nodes is  None:
        return "No nodes",400
    for node in nodes:
        blockchain.add_node(node)
    response = {'message' : 'All the node are connected' , 
                'total_nodes' : list(blockchain.nodes)}
    return jsonify(response),200

# Replacing by the new longest chain. 
Example 21
Project: Blockchain   Author: Younes-Charfaoui   File: chacoin002.py    Apache License 2.0 5 votes vote down vote up
def get_previous_block(self):
        return self.chain[-1]
    
    # this method will get the nonce to make a valid blockchain. 
Example 22
Project: Blockchain   Author: Younes-Charfaoui   File: chacoin002.py    Apache License 2.0 5 votes vote down vote up
def add_node(self, address):
        # we get the address and parsed it.
        parsed_url = urlparse(address)
        
        # Then we take the netloc of the adress and add to the set.
        # example of netloc "https://127.0.0.1:5000/" netloc is "127.0.0.1:5000"
        self.nodes.add(parsed_url.netloc)
     
    # function that make concencus over the network of nodes. 
Example 23
Project: Blockchain   Author: Younes-Charfaoui   File: chacoin002.py    Apache License 2.0 5 votes vote down vote up
def replace_chain(self):
        # getting all the nodes
        network = self.nodes
        
        # creating variables to hold reference to the bigest chain and it's length
        longest_chain = None
        max_length = len(self.chain)
        for node in network:
            
            # requesting the chain in every node
            response = requests.get(f'http://{node}/get_chain')
            
            if response.status_code == 200:
                # getting the chain and the length of it in the current node
                length = response.json()['length']
                chain =response.json()['chain']
                
                # if the case that the current chain is bigger than the old one we update
                if length > max_length and self.is_chain_valid(chain):
                    max_length = length
                    longest_chain = chain
        # if the longest chain was updated then we update our chain.
        if longest_chain:
            self.chain = longest_chain
            # and we return true.
            return True
        return False

# Part 2 - Mining our Blockchain

# Creating web app 
Example 24
Project: Blockchain   Author: Younes-Charfaoui   File: chacoin001.py    Apache License 2.0 5 votes vote down vote up
def get_previous_block(self):
        return self.chain[-1]
    
    # this method will get the nonce to make a valid blockchain. 
Example 25
Project: Blockchain   Author: Younes-Charfaoui   File: chacoin001.py    Apache License 2.0 5 votes vote down vote up
def add_node(self, address):
        # we get the address and parsed it.
        parsed_url = urlparse(address)
        
        # Then we take the netloc of the adress and add to the set.
        # example of netloc "https://127.0.0.1:5000/" netloc is "127.0.0.1:5000"
        self.nodes.add(parsed_url.netloc)
     
    # function that make concencus over the network of nodes. 
Example 26
Project: Blockchain   Author: Younes-Charfaoui   File: chacoin001.py    Apache License 2.0 5 votes vote down vote up
def replace_chain(self):
        # getting all the nodes
        network = self.nodes
        
        # creating variables to hold reference to the bigest chain and it's length
        longest_chain = None
        max_length = len(self.chain)
        for node in network:
            
            # requesting the chain in every node
            response = requests.get(f'http://{node}/get_chain')
            
            if response.status_code == 200:
                # getting the chain and the length of it in the current node
                length = response.json()['length']
                chain =response.json()['chain']
                
                # if the case that the current chain is bigger than the old one we update
                if length > max_length and self.is_chain_valid(chain):
                    max_length = length
                    longest_chain = chain
        # if the longest chain was updated then we update our chain.
        if longest_chain:
            self.chain = longest_chain
            # and we return true.
            return True
        return False

# Part 2 - Mining our Blockchain

# Creating web app 
Example 27
Project: Blockchain   Author: Younes-Charfaoui   File: chacoin001.py    Apache License 2.0 5 votes vote down vote up
def connect_node():
    json = request.get_json()
    nodes = json.get('nodes')
    if nodes is  None:
        return "No nodes",400
    for node in nodes:
        blockchain.add_node(node)
    response = {'message' : 'All the node are connected' , 
                'total_nodes' : list(blockchain.nodes)}
    return jsonify(response),200

# Replacing by the new longest chain. 
Example 28
Project: alexa-apple-calendar   Author: zanderxyz   File: apple_calendar_api.py    MIT License 5 votes vote down vote up
def request(self, *args, **kwargs):
        response = super(Session, self).request(*args, **kwargs)

        content_type = response.headers.get('Content-Type', '').split(';')[0]
        json_mimetypes = ['application/json', 'text/json']
        if content_type not in json_mimetypes:
            return response

        try:
            json = response.json()
        except:
            return response

        reason = json.get('errorMessage') or json.get('reason')
        if not reason and isinstance(json.get('error'), six.string_types):
            reason = json.get('error')
        if not reason and not response.ok:
            reason = response.reason
        if not reason and json.get('error'):
            reason = "Unknown reason"

        code = json.get('errorCode')

        if reason:
            raise Exception

        return response 
Example 29
Project: alexa-apple-calendar   Author: zanderxyz   File: apple_calendar_api.py    MIT License 5 votes vote down vote up
def _get_cookiejar_path(self):
        # Get path for cookiejar file
        return os.path.join(
            self._cookie_directory,
            ''.join([c for c in self.user.get('apple_id') if match(r'\w', c)])
        ) 
Example 30
Project: alexa-apple-calendar   Author: zanderxyz   File: apple_calendar_api.py    MIT License 5 votes vote down vote up
def __unicode__(self):
        return 'iCloud API: %s' % self.user.get('apple_id') 
Example 31
Project: alexa-apple-calendar   Author: zanderxyz   File: apple_calendar_api.py    MIT License 5 votes vote down vote up
def get_event_detail(self, pguid, guid):
        """
        Fetches a single event's details by specifying a pguid
        (a calendar) and a guid (an event's ID).
        """
        params = dict(self.params)
        params.update({'lang': 'en-us', 'usertz': get_localzone().zone})
        url = '%s/%s/%s' % (self._calendar_event_detail_url, pguid, guid)
        req = self.session.get(url, params=params)
        self.response = req.json()
        return self.response['Event'][0] 
Example 32
Project: RLBotTraining   Author: RLBot   File: bakkesmod_importer.py    MIT License 5 votes vote down vote up
def _parse_physics(self, rng, physics_json: Mapping[str, Any]) -> Physics:
        return Physics(
                location=self._parse_vec3(rng, physics_json.get('location', {})),
                velocity=self._parse_vec3(rng, physics_json.get('velocity', {})),
                rotation=self._parse_rotator(rng, physics_json.get('rotation', {})),
                angular_velocity=Vector3(0,0,0),
            ) 
Example 33
Project: RLBotTraining   Author: RLBot   File: bakkesmod_importer.py    MIT License 5 votes vote down vote up
def _parse_number(self, rng, json: Mapping[str, Any], attr_name:str, default_value=0):
        attr = json.get(attr_name, default_value)
        if self._is_number(attr):
            return attr
        assert isinstance(attr, str), f"I don't know how to decdoe this thing which should give a number: {repr(attr)}"
        number_or_tuple = eval(attr, {}, {})  # Note: dangerous
        if self._is_number(number_or_tuple):
            return number_or_tuple
        tup = number_or_tuple
        assert isinstance(tup, tuple), f"I don't know how to decode this thing: {repr(number_or_tuple)}"
        assert len(tup) == 2, f"I only know how to interpret number ranges made of a 2-tuple"
        # TODO: confirm with Bakkes that this is the right way of interpreting the tuple.
        return rng.uniform(tup[0], tup[1]) 
Example 34
Project: retdec-regression-tests-framework   Author: avast   File: config_parser.py    MIT License 5 votes vote down vote up
def is_statically_linked(self, func, address=None):
        """Checks that the given function is marked as statically linked.

        :param str func: Name of the function.
        :param int address: Optional address of the function.

        :returns: ``True`` if the function is marked as statically linked,
                  ``False`` otherwise.

        :raises AssertionError: If there is no such function in the config.

        When `address` is not ``None``, it is also checked that the function
        was detected on the given address.
        """
        # Find a function with the given name.
        for config_func in self.json.get('functions', []):
            if config_func['name'] == func:
                break
        else:  # nobreak
            raise AssertionError('no such function: {}'.format(func))

        if config_func['fncType'] != 'staticallyLinked':
            return False

        if address is not None and _string_to_int(config_func.get('startAddr')) != address:
            return False

        return True 
Example 35
Project: retdec-regression-tests-framework   Author: avast   File: config_parser.py    MIT License 5 votes vote down vote up
def __init__(self, json):
        self.json = json
        self.name = json.get('name')
        self.address = _string_to_int(json.get('address'))
        self.items = [VtableItem(d) for d in json.get('items', []) or []] 
Example 36
Project: retdec-regression-tests-framework   Author: avast   File: config_parser.py    MIT License 5 votes vote down vote up
def __init__(self, json):
        self.json = json
        self.address = _string_to_int(json.get('address'))
        self.target_address = _string_to_int(json.get('targetAddress'))
        self.target_name = json.get('targetName') 
Example 37
Project: MAX-Text-Summarizer   Author: IBM   File: test.py    Apache License 2.0 5 votes vote down vote up
def test_swagger():

    model_endpoint = 'http://localhost:5000/swagger.json'

    r = requests.get(url=model_endpoint)
    assert r.status_code == 200
    assert r.headers['Content-Type'] == 'application/json'

    json = r.json()
    assert 'swagger' in json
    assert json.get('info').get('title') == 'MAX Text Summarizer'
    assert json.get('info').get('description') == 'Generate a summarized description of a body of text.' 
Example 38
Project: MAX-Text-Summarizer   Author: IBM   File: test.py    Apache License 2.0 5 votes vote down vote up
def test_metadata():

    model_endpoint = 'http://localhost:5000/model/metadata'

    r = requests.get(url=model_endpoint)
    assert r.status_code == 200

    metadata = r.json()
    assert metadata['id'] == 'max-text-summarizer'
    assert metadata['name'] == 'MAX Text Summarizer'
    assert metadata['description'] == 'get_to_the_point TensorFlow model trained on CNN/Daily Mail Data'
    assert metadata['license'] == 'Apache V2' 
Example 39
Project: python-chrome-devtools-protocol   Author: HyperionGray   File: generate.py    MIT License 5 votes vote down vote up
def from_json(cls, type) -> 'CdpItems':
        ''' Generate code to instantiate an item from a JSON object. '''
        return cls(type.get('type'), type.get('$ref')) 
Example 40
Project: python-chrome-devtools-protocol   Author: HyperionGray   File: generate.py    MIT License 5 votes vote down vote up
def from_json(cls, property) -> 'CdpProperty':
        ''' Instantiate a CDP property from a JSON object. '''
        return cls(
            property['name'],
            property.get('description'),
            property.get('type'),
            property.get('$ref'),
            property.get('enum'),
            CdpItems.from_json(property['items']) if 'items' in property else None,
            property.get('optional', False),
        ) 
Example 41
Project: python-chrome-devtools-protocol   Author: HyperionGray   File: generate.py    MIT License 5 votes vote down vote up
def from_json(cls, type_) -> 'CdpType':
        ''' Instantiate a CDP type from a JSON object. '''
        return cls(
            type_['id'],
            type_.get('description'),
            type_['type'],
            CdpItems.from_json(type_['items']) if 'items' in type_ else None,
            type_.get('enum'),
            [CdpProperty.from_json(p) for p in type_.get('properties', list())],
        ) 
Example 42
Project: python-chrome-devtools-protocol   Author: HyperionGray   File: generate.py    MIT License 5 votes vote down vote up
def from_json(cls, command, domain) -> 'CdpCommand':
        ''' Instantiate a CDP command from a JSON object. '''
        parameters = command.get('parameters', list())
        returns = command.get('returns', list())

        return cls(
            command['name'],
            command.get('description'),
            command.get('experimental', False),
            [typing.cast(CdpParameter, CdpParameter.from_json(p)) for p in parameters],
            [typing.cast(CdpReturn, CdpReturn.from_json(r)) for r in returns],
            domain,
        ) 
Example 43
Project: python-chrome-devtools-protocol   Author: HyperionGray   File: generate.py    MIT License 5 votes vote down vote up
def from_json(cls, json: dict, domain: str):
        ''' Create a new CDP event instance from a JSON dict. '''
        return cls(
            json['name'],
            json.get('description'),
            [typing.cast(CdpParameter, CdpParameter.from_json(p))
                for p in json.get('parameters', list())],
            domain
        ) 
Example 44
Project: gym-http-api   Author: openai   File: gym_http_server.py    MIT License 5 votes vote down vote up
def get_required_param(json, param):
    if json is None:
        logger.info("Request is not a valid json")
        raise InvalidUsage("Request is not a valid json")
    value = json.get(param, None)
    if (value is None) or (value=='') or (value==[]):
        logger.info("A required request parameter '{}' had value {}".format(param, value))
        raise InvalidUsage("A required request parameter '{}' was not provided".format(param))
    return value 
Example 45
Project: gym-http-api   Author: openai   File: gym_http_server.py    MIT License 5 votes vote down vote up
def get_optional_param(json, param, default):
    if json is None:
        logger.info("Request is not a valid json")
        raise InvalidUsage("Request is not a valid json")
    value = json.get(param, None)
    if (value is None) or (value=='') or (value==[]):
        logger.info("An optional request parameter '{}' had value {} and was replaced with default value {}".format(param, value, default))
        value = default
    return value 
Example 46
Project: gym-http-api   Author: openai   File: gym_http_server.py    MIT License 5 votes vote down vote up
def shutdown():
    """ Request a server shutdown - currently used by the integration tests to repeatedly create and destroy fresh copies of the server running in a separate thread"""
    f = request.environ.get('werkzeug.server.shutdown')
    f()
    return 'Server shutting down' 
Example 47
Project: PyVMwareAirWatch   Author: jprichards   File: client.py    MIT License 5 votes vote down vote up
def __init__(self, json_response=None):
        if json_response is None:
            pass
        else:
            self.response = json_response
            self.error_code = json_response.get('errorCode')
            self.error_msg = str(json_response.get('message'))
            if self.error_code is None:
                self.error_code = 0
                self.error_msg = 'Unknown API error occurred' 
Example 48
Project: PyVMwareAirWatch   Author: jprichards   File: client.py    MIT License 5 votes vote down vote up
def get(self, module, path, version=None, params=None, header=None, timeout=30):
        """Sends a GET request to the API. Returns the response object."""
        if header is None:
            header = {}
        header.update(self._build_header(self.username, self.password, self.apikey))
        header.update({'Content-Type': 'application/json'})
        endpoint = self._build_endpoint(self.env, module, path, version)
        try:
            r = requests.get(endpoint, params=params, headers=header, timeout=timeout)
            r = self._check_for_error(r)
            return r
        except AirWatchAPIError as e:
            raise e 
Example 49
Project: PyVMwareAirWatch   Author: jprichards   File: client.py    MIT License 5 votes vote down vote up
def _check_for_error(response):
        """Checks the response for json data, then for an error, then for a status code"""
        if response.headers.get('Content-Type') in ('application/json', 'application/json; charset=utf-8'):
            json = response.json()
            if json.get('errorCode'):
                raise AirWatchAPIError(json_response=json)
            else:
                return json
        else:
            return response.status_code 
Example 50
Project: luci-py   Author: luci   File: model.py    Apache License 2.0 5 votes vote down vote up
def get(self, counter_name, default=0):
    """Get current counter value.

    Args:
      counter_name: counter name as string.
      default: default value if one doesn't exist.

    Returns:
      current counter value as int. 0 if counter was not set.
    """
    return self.counters.get(counter_name, default) 
Example 51
Project: luci-py   Author: luci   File: model.py    Apache License 2.0 5 votes vote down vote up
def increment(self, counter_name, delta):
    """Increment counter value.

    Args:
      counter_name: counter name as String.
      delta: increment delta as Integer.

    Returns:
      new counter value.
    """
    current_value = self.counters.get(counter_name, 0)
    new_value = current_value + delta
    self.counters[counter_name] = new_value
    return new_value 
Example 52
Project: luci-py   Author: luci   File: model.py    Apache License 2.0 5 votes vote down vote up
def from_json(cls, json):
    """Creates MapperSpec from a dict-like object."""
    return cls(json["mapper_handler_spec"],
               json["mapper_input_reader"],
               json["mapper_params"],
               json["mapper_shard_count"],
               json.get("mapper_output_writer")
              ) 
Example 53
Project: luci-py   Author: luci   File: model.py    Apache License 2.0 5 votes vote down vote up
def _get_mapreduce_spec(cls, mr_id):
    """Get Mapreduce spec from mr id."""
    key = 'GAE-MR-spec: %s' % mr_id
    spec_json = memcache.get(key)
    if spec_json:
      return cls.from_json(spec_json)
    state = MapreduceState.get_by_job_id(mr_id)
    spec = state.mapreduce_spec
    spec_json = spec.to_json()
    memcache.set(key, spec_json)
    return spec 
Example 54
Project: luci-py   Author: luci   File: model.py    Apache License 2.0 5 votes vote down vote up
def get_by_job_id(cls, mapreduce_id):
    """Retrieves the instance of state for a Job.

    Args:
      mapreduce_id: The mapreduce job to retrieve.

    Returns:
      instance of MapreduceState for passed id.
    """
    return db.get(cls.get_key_by_job_id(mapreduce_id)) 
Example 55
Project: luci-py   Author: luci   File: model.py    Apache License 2.0 5 votes vote down vote up
def get_processed(self):
    """Number of processed entities.

    Returns:
      The total number of processed entities as int.
    """
    return self.counters_map.get(context.COUNTER_MAPPER_CALLS) 
Example 56
Project: luci-py   Author: luci   File: model.py    Apache License 2.0 5 votes vote down vote up
def from_request(cls, request):
    """Create new TransientShardState from webapp request."""
    mapreduce_spec = MapreduceSpec.from_json_str(request.get("mapreduce_spec"))
    mapper_spec = mapreduce_spec.mapper
    input_reader_spec_dict = json.loads(request.get("input_reader_state"),
                                        cls=json_util.JsonDecoder)
    input_reader = mapper_spec.input_reader_class().from_json(
        input_reader_spec_dict)
    initial_input_reader_spec_dict = json.loads(
        request.get("initial_input_reader_state"), cls=json_util.JsonDecoder)
    initial_input_reader = mapper_spec.input_reader_class().from_json(
        initial_input_reader_spec_dict)

    output_writer = None
    if mapper_spec.output_writer_class():
      output_writer = mapper_spec.output_writer_class().from_json(
          json.loads(request.get("output_writer_state", "{}"),
                     cls=json_util.JsonDecoder))
      assert isinstance(output_writer, mapper_spec.output_writer_class()), (
          "%s.from_json returned an instance of wrong class: %s" % (
              mapper_spec.output_writer_class(),
              output_writer.__class__))

    handler = util.try_deserialize_handler(request.get("serialized_handler"))
    if not handler:
      handler = mapreduce_spec.mapper.handler

    return cls(mapreduce_spec.params["base_path"],
               mapreduce_spec,
               str(request.get("shard_id")),
               int(request.get("slice_id")),
               input_reader,
               initial_input_reader,
               output_writer=output_writer,
               retries=int(request.get("retries")),
               handler=handler) 
Example 57
Project: luci-py   Author: luci   File: model.py    Apache License 2.0 5 votes vote down vote up
def get(self, counter_name, default=0):
    """Get current counter value.

    Args:
      counter_name: counter name as string.
      default: default value if one doesn't exist.

    Returns:
      current counter value as int. 0 if counter was not set.
    """
    return self.counters.get(counter_name, default) 
Example 58
Project: luci-py   Author: luci   File: model.py    Apache License 2.0 5 votes vote down vote up
def increment(self, counter_name, delta):
    """Increment counter value.

    Args:
      counter_name: counter name as String.
      delta: increment delta as Integer.

    Returns:
      new counter value.
    """
    current_value = self.counters.get(counter_name, 0)
    new_value = current_value + delta
    self.counters[counter_name] = new_value
    return new_value 
Example 59
Project: luci-py   Author: luci   File: model.py    Apache License 2.0 5 votes vote down vote up
def from_json(cls, json):
    """Creates MapperSpec from a dict-like object."""
    return cls(json["mapper_handler_spec"],
               json["mapper_input_reader"],
               json["mapper_params"],
               json["mapper_shard_count"],
               json.get("mapper_output_writer")
              ) 
Example 60
Project: luci-py   Author: luci   File: model.py    Apache License 2.0 5 votes vote down vote up
def from_json(cls, json):
    """Create new MapreduceSpec from the json, encoded by to_json.

    Args:
      json: json representation of MapreduceSpec.

    Returns:
      an instance of MapreduceSpec with all data deserialized from json.
    """
    mapreduce_spec = cls(json["name"],
                         json["mapreduce_id"],
                         json["mapper_spec"],
                         json.get("params"),
                         json.get("hooks_class_name"))
    return mapreduce_spec 
Example 61
Project: luci-py   Author: luci   File: model.py    Apache License 2.0 5 votes vote down vote up
def get_by_job_id(cls, mapreduce_id):
    """Retrieves the instance of state for a Job.

    Args:
      mapreduce_id: The mapreduce job to retrieve.

    Returns:
      instance of MapreduceState for passed id.
    """
    return db.get(cls.get_key_by_job_id(mapreduce_id)) 
Example 62
Project: luci-py   Author: luci   File: model.py    Apache License 2.0 5 votes vote down vote up
def get_processed(self):
    """Number of processed entities.

    Returns:
      The total number of processed entities as int.
    """
    return self.counters_map.get(context.COUNTER_MAPPER_CALLS) 
Example 63
Project: luci-py   Author: luci   File: model.py    Apache License 2.0 5 votes vote down vote up
def from_request(cls, request):
    """Create new TransientShardState from webapp request."""
    mapreduce_spec = MapreduceSpec.from_json_str(request.get("mapreduce_spec"))
    mapper_spec = mapreduce_spec.mapper
    input_reader_spec_dict = json.loads(request.get("input_reader_state"),
                                        cls=json_util.JsonDecoder)
    input_reader = mapper_spec.input_reader_class().from_json(
        input_reader_spec_dict)
    initial_input_reader_spec_dict = json.loads(
        request.get("initial_input_reader_state"), cls=json_util.JsonDecoder)
    initial_input_reader = mapper_spec.input_reader_class().from_json(
        initial_input_reader_spec_dict)

    output_writer = None
    if mapper_spec.output_writer_class():
      output_writer = mapper_spec.output_writer_class().from_json(
          json.loads(request.get("output_writer_state", "{}"),
                     cls=json_util.JsonDecoder))
      assert isinstance(output_writer, mapper_spec.output_writer_class()), (
          "%s.from_json returned an instance of wrong class: %s" % (
              mapper_spec.output_writer_class(),
              output_writer.__class__))

    handler = util.try_deserialize_handler(request.get("serialized_handler"))
    if not handler:
      handler = mapreduce_spec.mapper.handler

    return cls(mapreduce_spec.params["base_path"],
               mapreduce_spec,
               str(request.get("shard_id")),
               int(request.get("slice_id")),
               input_reader,
               initial_input_reader,
               output_writer=output_writer,
               retries=int(request.get("retries")),
               handler=handler) 
Example 64
Project: trial2rev   Author: evidence-surveillance   File: views.py    MIT License 5 votes vote down vote up
def load_user(id):
    """
    retrieve a User instance with the given id
    @param id: id of desired User
    @return: User instance
    """
    obj = User.get(id)
    if obj is not None:
        return obj
    else:
        return None 
Example 65
Project: trial2rev   Author: evidence-surveillance   File: views.py    MIT License 5 votes vote down vote up
def unique_reviews_trials():
    """ get summary of data for front page """
    return json.dumps({'success': True, 'data': crud.data_summary()
                       }, default=str), 200, {
               'ContentType': 'application/json'} 
Example 66
Project: trial2rev   Author: evidence-surveillance   File: views.py    MIT License 5 votes vote down vote up
def condition_counts():
    """ get counts for each condition in the specified category """
    data = request.json
    try:
        only_verified = bool(int(data['onlyVerified']))
    except ValueError as e:
        return json.dumps({'success': False, 'msg': 'bad value for onlyVerified'
                           }), 400, {
                   'ContentType': 'application/json'}

    return json.dumps({'success': True, 'data': crud.condition_counts(data['category'], only_verified)
                       }), 200, {
               'ContentType': 'application/json'} 
Example 67
Project: trial2rev   Author: evidence-surveillance   File: views.py    MIT License 5 votes vote down vote up
def category_counts():
    """ get review counts for every category """
    try:
        only_verified = bool(int(request.args.get('onlyVerified')))
    except ValueError as e:
        return json.dumps({'success': False, 'msg': 'bad value for onlyVerified'
                           }), 400, {
                   'ContentType': 'application/json'}
    return json.dumps({'success': True, 'data': crud.category_counts(only_verified)
                       }), 200, {
               'ContentType': 'application/json'} 
Example 68
Project: trial2rev   Author: evidence-surveillance   File: views.py    MIT License 5 votes vote down vote up
def review_conditions(condition):
    """
    get a list of reviews for the specified condition
    @param condition: id of desired condition
    @return: rendered page content
    """
    return render_template('reviews_by_condition.html', reviews=crud.reviews_for_condition(condition)) 
Example 69
Project: trial2rev   Author: evidence-surveillance   File: views.py    MIT License 5 votes vote down vote up
def alter_users():
    """
    add new user or delete exiting user
    @return: refreshed page indicating success or failure
    """
    if request.form['submit'] == "Create User":
        form = NewUserForm(request.form)
        if form.validate_on_submit():
            if User.get(form.new_email.data) is None:
                User(form.new_email.data, form.nickname.data, form.password.data, form.type.data)

                msg = Message('ES3 Access Request', sender=config.MAIL_USERNAME,
                              recipients=[form.new_email.data])
                msg.html = render_template('registration_email.html', user=form.new_email.data, pw=form.password.data)
                mail.send(msg)

                flash('New user ' + form.new_email.data + ' created successfully')
            else:
                flash('User already exists')
            return redirect(url_for('admin_panel'))
        else:
            return render_template('adminpanel.html', new_user=form, users=User.get_all())
    elif request.form['submit'] == "Delete Selected":
        for x in request.form:
            if len(request.form) == 1:
                flash('Error - no user selected')
                break
            if x == current_user.id:
                flash('Error - cannot delete own account')
                break
            if request.form[x] == "on":
                User.delete(x)
                flash('User ' + x + ' deleted successfully')
            return redirect(url_for('admin_panel'))
    return render_template('adminpanel.html', new_user=NewUserForm(), users=User.get_all()) 
Example 70
Project: pxl   Author: svsticky   File: config.py    Mozilla Public License 2.0 5 votes vote down vote up
def from_json(cls, json: Dict[str, Any]) -> Config:
        return cls(
            s3_endpoint=json["s3_endpoint"],
            s3_region=json["s3_region"],
            s3_bucket=json["s3_bucket"],
            s3_key_id=json["s3_key_id"],
            s3_key_secret=json["s3_key_secret"],
            deploy_host=json["deploy_host"],
            deploy_user=json["deploy_user"],
            deploy_path=json["deploy_path"],
            public_image_url=json.get("public_image_url", ""),
        ) 
Example 71
Project: hail   Author: hail-is   File: build.py    MIT License 5 votes vote down vote up
def __init__(self, params):
        json = params.json

        self.name = json['name']
        if 'dependsOn' in json:
            self.deps = [params.name_step[d] for d in json['dependsOn'] if params.name_step[d]]
        else:
            self.deps = []
        self.scopes = json.get('scopes')
        self.run_if_requested = json.get('runIfRequested', False)

        self.token = generate_token() 
Example 72
Project: hail   Author: hail-is   File: build.py    MIT License 5 votes vote down vote up
def from_json(params):
        json = params.json
        return BuildImageStep(params,
                              json['dockerFile'],
                              json.get('contextPath'),
                              json.get('publishAs'),
                              json.get('inputs')) 
Example 73
Project: hail   Author: hail-is   File: build.py    MIT License 5 votes vote down vote up
def from_json(params):
        json = params.json
        return RunImageStep(params,
                            json['image'],
                            json['script'],
                            json.get('inputs'),
                            json.get('outputs'),
                            json.get('port'),
                            json.get('resources'),
                            json.get('serviceAccount'),
                            json.get('secrets'),
                            json.get('alwaysRun', False)) 
Example 74
Project: hail   Author: hail-is   File: build.py    MIT License 5 votes vote down vote up
def from_json(params):
        json = params.json
        return CreateNamespaceStep(params,
                                   json['namespaceName'],
                                   json.get('adminServiceAccount'),
                                   json.get('public', False),
                                   json.get('secrets')) 
Example 75
Project: hail   Author: hail-is   File: build.py    MIT License 5 votes vote down vote up
def from_json(params):
        json = params.json
        return DeployStep(params,
                          json['namespace'],
                          # FIXME config_file
                          json['config'],
                          json.get('link'),
                          json.get('wait')) 
Example 76
Project: codex-backend   Author: codexgigassys   File: Functions.py    MIT License 5 votes vote down vote up
def get_eset_sig_from_scan(scans):
    if scans is None:
        return None
    for scan in scans:
        if scan.get('name') in ['ESET-NOD32', 'NOD32', 'NOD32v2']:
            return scan.get('result')
    return '' 
Example 77
Project: codex-backend   Author: codexgigassys   File: Functions.py    MIT License 5 votes vote down vote up
def key_dict_clean(json):
    if json is None:
        return None
    array = []
    for key in json.keys():
        tmp_dict = json.get(key)
        tmp_dict["name"] = key
        array.append(tmp_dict)
    return array

# Replace dot with _
# in dictionaries keys
# in order to save them in mongo 
Example 78
Project: codex-backend   Author: codexgigassys   File: Functions.py    MIT License 5 votes vote down vote up
def key_list_clean(json):
    if json is None:
        return None
    array = []
    for key in json.keys():
        tmp_dict = {}
        tmp_dict["name"] = key
        tmp_dict["values"] = json.get(key)
        array.append(tmp_dict)
    return array 
Example 79
Project: codex-backend   Author: codexgigassys   File: Functions.py    MIT License 5 votes vote down vote up
def jsonize(data):
    return json.dumps(data, sort_keys=False, indent=4)


# Checks if the meta has a date. If it doesn't
# it updates it. If a date is found, the oldest
# date will get saved. 
Example 80
Project: codex-backend   Author: codexgigassys   File: Functions.py    MIT License 5 votes vote down vote up
def change_date_to_str(res):
    if res is None:
        return None
    for date_key in ["date", "upload_date", "date_start", "date_end", "date_enqueued"]:
        if res.get(date_key) is None:
            pass
        else:
            res[date_key] = str(res.get(date_key))
    return res