Parsing Textfields
Project description
textfield_parser
Getting a string from a textfield and returns the parsed content.
read_value
Reads a string and strips spaces and quotes.
def read_value(text)
Arguments
- text -string- from textfield that will be parsed
Return
- stripped string
read_list
Reads a comma separated list and returns either the items or the remainder of the additional list when a text contains the modifier 'Not'.
def read_list(text,value_list=None,sep = ',', modifier_list = None)`
Arguments
- text -string- from textfield that will be parsed
- value_list -list- list from which the parsed text elements will be removed when there is a modifier 'NOT'.
- sep -char- element separator in string
- modifier_list -list- List of modifier for excluding the listed items from value_list Default: ['!', '~', 'not', 'Not', 'NOT']
Return
- List of parsed parameters
Examples
**Simple list: **
- text = "'Hello', 'a list', separated by , me "
-> ['Hello', 'a list', 'separated by', 'me']
Not list
- text = "Not Mercedes, Renault, Citroen, Peugeaut, 'Rolls Royce'"
- value_list = ['Mercedes', 'Audi', 'VW', 'Skoda', 'Renault', 'Citroen', 'Peugeot', 'Rolls Royce']
-> ['Audi', 'VW', 'Skoda', 'Peugeot']
read_value_lists
Reads a 'outer_sep'-separated list of a mapping with 'inner-sep' separated list and returns a dictionary with the map as key and a list as value.
def read_value_lists(text,value_list=None,inner_sep = ',',outer_sep = ';',map_sep=':')`
Arguments
- text -string- from textfield that will be parsed
- inner_sep -char- element separator of value list
- outer_sep -char- list separator for the lists
- map_sep -char- separates map from list
Return
- Dictionary of the keys and lists.
Example
text = "'Mercedes':expensive, German, respectable; Audi:'sportive, German, technology-advanced';
VW : 'people', 'solid', No1; Citroen:cool, Fantomas, CV2, elastic ; 'Rolls Rocye': royal,
British, 'very expensive', black"
-> {'Mercedes': ['expensive', 'German', 'respectable'], 'Audi': ['sportive', 'German', 'technology-advanced'], 'VW': ['people', 'solid', 'No1'], 'Citroen': ['cool', 'Fantomas', 'CV2', 'elastic'], 'Rolls Rocye': ['royal', 'British', 'very expensive', 'black']} Map :{'Mercedes': 'expensive', 'Audi': 'sportive', 'VW': 'people', 'Citroen': 'cool', 'Rolls Rocye': 'royal'}
read_map
Reads a comma separated list of mappings and returns a dictionary.
def read_map(text, sep=',')
Arguments
- text -string- from textfield that will be parsed
- sep -char- element separator in string
Return
- dictionary of parsed parameters
Example
text = "'Mercedes':expensive, Audi:'sportiv', VW : 'people', Citroen:cool, 'Rolls Rocye': royal"
-> {'Mercedes': 'expensive', 'Audi': 'sportiv', 'VW': 'people', 'Citroen': 'cool', 'Rolls Rocye': 'royal'}`
read_json
Reads a json formatted string and return a dictionary
Arguments
- text -string- json-string
Return
- dictionary of json
Example
json_text = "{"Luxury Class": {"Mercedes":"expensive","Rolls Rocye": "royal"}, "High Middle Class":{"Audi":"sportiv"}, "Middle Class" : {"Citroen":"cool","VW" : "people" }}"
-> `{'Luxury Class': {'Mercedes': 'expensive', 'Rolls Rocye': 'royal'}, 'High Middle Class': {'Audi': 'sportiv'}, 'Middle Class': {'Citroen': 'cool', 'VW': 'people'}}
read_comparisons
Parses a list of comparisons and returns a list of lists with 3 items: (left, comparison-operator, right). There is an internal mapping of comparison characters: {'!=':'!','==':'=','>=':'≥','=>':'≥','<=':'≤','=<':'≤'}
def read_comparisons(text,sep = ',',formula_map = None)
Arguments
- text -string- Textfield string
- sep -char- element separator in string
- modifier_map -dictionary- mapping of comparison strings to 1-char comparison. Default: {'!=': '!', '==': '=', '>=': '≥', '=>': '≥', '<=': '≤', '=<': '≤'} `
Return
- List of 3 element lists
Examples
' anna > 1.70, norbert != 900, cindy <= 1.65'
-> [['anna', '>', 1.7], ['norbert', '!', 900.0], ['cindy', '≤', 1.65]]`
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for textfield_parser-0.0.12-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 62955e9dae4960eb5026f598529c477abc001f48a5404f1c4695f550b9d7fc1c |
|
MD5 | 555b8b55feac94cb5822c392ee476a31 |
|
BLAKE2b-256 | 47046642f85d552817b4dc5eed6d8657057623148e68fbe05c2e163372b264ad |