Skip to main content

The parser which can get dict with strings and convert them to Python types like int, float, date, list etc. It can be useful for parsing request params via API.

Project description

parser-string-python

The parser which can get dict with strings and convert them to Python types like int, float, date, list etc. It can be useful for parsing request params via API.

Table of contents

How to create fields

There is 99% that you no need more than default fields. And that section is just for knowing how it works. Parser gets fields as required arguments. A field is a pair of key and value. This way any dict is many fields. Every filed inherits from base class Field. The Field has two important methods _is_valid_value() and _resolve(). Those methods should me override. The _resolve() converts a value to the necessary type. It's the main class method.

Also, a field can have property param_start_with. It's necessary when you want to set validation via param key. For example, there are fields list_ids and ids. You set param_start_with as list. It means only list_ids will be valid.

from parser_json_python.fields import Field

class FieldDate(Field):
    param_start_with = 'date'
    date_template = '%Y-%m-%d %H:%M'

    def _is_valid_value(self, value):
        try:
            self._resolve(value)
            return True
        except ValueError:
            return False

    def _resolve(self, value):
        return datetime.strptime(value, self.date_template)

How to create a parser

You can import DefaultParser with all default fields. If you want to create own parser you need to set fields as a list of Field object.

from parser_json_python.fields import *

DefaultParser = Parser(
    fields=(FieldInt(), FieldFloat(), FieldList(), FieldDate(), FieldBoolean())
)

How to use a parser

A Parser has two methods. The first parse_field() is for convert one param and the second parse() for convert dict with params.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for parser-json-python, version 1.0.1
Filename, size File type Python version Upload date Hashes
Filename, size parser_json_python-1.0.1-py3-none-any.whl (5.6 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size parser-json-python-1.0.1.tar.gz (3.5 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page