Skip to main content

Parser for text files with decorators into Python dictionaries

Project description

Decorators Parser

License

© 2023 Smartschool Inc. All rights reserved.

Installation guide

  1. Install Python 3.7 or newer from https://www.python.org/downloads/
  2. Install PIP using this guide
  3. Run pip install decorator-parser in your command line

API

parse

def parse(self, path=None, data=None)

Parses given string or file and returns a list of dictionaries. If both path and data are provided, data will be ignored.

Decorators format

Decorator name can be any string that does not contain '@' character. If decorator does not satisfy this requirement InvalidDecoratorException will be thrown.

Example:

@decorator(some nice value)

Standard decorators

Decorators can be used in one of three ways:

@decorator(value)

or

@decorator()
Some very long and complicated value that would be hard to read if it were in parenthesis like the value above.

or

@decorator
Some very long and complicated value that would be hard to read if it were in parenthesis like the value above.

All of these create Python dictionary like this:

{
    'decorator': 'value'
}

@new decorator

Using @new decorator starts a new dictionary and adds it to the current list of dictionaries. In a single piece of text between two @new decorators (which correspond to a single Python dictionary) there can not be two decorators with the same name. Using a same name without an appropriate @new decorator will result in DuplicateDecoratorException being thrown.

Example:

task.txt:

@question()
Who is Lincoln?

@new
@question()
What is the purpose of the Bill of Rights?

run.py:

from decorator_parser.parse import Parser
task_parser = Parser()
print(task_parser.parse_file('task.txt'))

result:

[
    {
        'question': 'Who is Lincoln?'
    },
    {
        'question': 'What is the purpose of the Bill of Rights?'
    }
]

Global decorators

In addition to standard decorators, decorators which name starts with global- are added to each dictionary while parsing a file. Dictionary key is the decorator's suffix after global-

Example:

task.txt:

@global-topic(History)
@question()
Who is Lincoln?

@new
@question()
What is the purpose of the Bill of Rights?

run.py:

from decorator_parser.parse import Parser
task_parser = Parser()
print(task_parser.parse_file('task.txt'))

result:

[
    {
        'topic': 'History',
        'question': 'Who is Lincoln?'
    },
    {
        'topic': 'History',
        'question': 'What is the purpose of the Bill of Rights?'
    }
]

Constraints

Decorators can use constraints on their values. If a decorator has value that does not match regular expression provided, Parser will throw InvalidValueException. Parser class takes optional constraints argument in its constructor which is a Python dictionary in a format shown below (if format of the given dictionary is invalid, InvalidConstraintException will be thrown):

example = {
    'question': 
    {
        'regex': '([^@]+)',
        'description': 'any non-empty string without @'
    },
    'correct':
    {
        'regex': '([1-4])',
        'description': 'any number from 1 to 4'
    }
}

Example 1

task.txt:

@correct(11)

run.py:

from decorator_parser.parse import Parser
task_parser = Parser(example)
print(task_parser.parse_file('task.txt'))

Will result in the following output:

errors.InvalidValueException: Line 1: 'correct' should be any number from 1 to 4 but is 11

Example 2

If we take Example 1 but change task.txt file to:

correct(1)

The output will be:

[
    {
        'correct': '1'
    }
]

Ignored decorators

If you need to use @ symbol for something else than decorator, you can specify names to exclude from the search

Example:

task.txt:

@question
Some long question with @ref in it

run.py:

from decorator_parser.parse import Parser
task_parser = Parser(ignored=['ref'])
print(task_parser.parse_file('task.txt'))

Will result in the following output:

[
    {
        'question': 'Some long question with @ref in it'
    }
]

Project details


Download files

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

Source Distribution

decorator_parser-1.2.0.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

decorator_parser-1.2.0-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file decorator_parser-1.2.0.tar.gz.

File metadata

  • Download URL: decorator_parser-1.2.0.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.10

File hashes

Hashes for decorator_parser-1.2.0.tar.gz
Algorithm Hash digest
SHA256 1d97a1a25361f53fda9158385913d46b4e7d96b9d220563788d45852336cbd1e
MD5 6f832ce47a9e2a6574b685897fd610f2
BLAKE2b-256 f7f729803f1c6c79c177eb892785203feedf666bb60cbdea95bacf0c11b5418f

See more details on using hashes here.

File details

Details for the file decorator_parser-1.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for decorator_parser-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 08bc5367bf55e7150ac2e6021664b727ccef9944494fda07031488a0beece933
MD5 623417db92000636c4eb3fad3edff7c3
BLAKE2b-256 75687ba0e87d099ec84f916359a9320dd4a79371f84709d7eb22a07f1cfc8771

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page