DictForArgs

DictForArgs.py

See the doc string for the dictForArgs() function.

Also, there is a test suite in Tests/TestDictForArgs.py

MiscUtils.DictForArgs.DictForArgs(s)

Build dictionary from arguments.

Takes an input such as:

x=3
name="foo"
first='john' last='doe'
required border=3

And returns a dictionary representing the same. For keys that aren’t given an explicit value (such as ‘required’ above), the value is ‘1’.

All values are interpreted as strings. If you want ints and floats, you’ll have to convert them yourself.

This syntax is equivalent to what you find in HTML and close to other ML languages such as XML.

Returns {} for an empty string.

The informal grammar is:

(NAME [=NAME|STRING])*

Will raise DictForArgsError if the string is invalid.

See also: pyDictForArgs() and expandDictWithExtras() in this module.

exception MiscUtils.DictForArgs.DictForArgsError

Bases: Exception

Error when building dictionary from arguments.

__init__(*args, **kwargs)
args
with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

MiscUtils.DictForArgs.ExpandDictWithExtras(d, key='Extras', delKey=True, dictForArgs=<function dictForArgs>)

Return a dictionary with the ‘Extras’ column expanded by dictForArgs().

For example, given:

{'Name': 'foo', 'Extras': 'x=1 y=2'}

The return value is:

{'Name': 'foo', 'x': '1', 'y': '2'}

The key argument controls what key in the dictionary is used to hold the extra arguments. The delKey argument controls whether that key and its corresponding value are retained. The same dictionary may be returned if there is no extras key. The most typical use of this function is to pass a row from a DataTable that was initialized from a CSV file (e.g., a spreadsheet or tabular file). FormKit and MiddleKit both use CSV files and allow for an Extras column to specify attributes that occur infrequently.

MiscUtils.DictForArgs.PyDictForArgs(s)

Build dictionary from arguments.

Takes an input such as:

x=3
name="foo"
first='john'; last='doe'
list=[1, 2, 3]; name='foo'

And returns a dictionary representing the same.

All values are interpreted as Python expressions. Any error in these expressions will raise the appropriate Python exception. This syntax allows much more power than dictForArgs() since you can include lists, dictionaries, actual ints and floats, etc.

This could also open the door to hacking your software if the input comes from a tainted source such as an HTML form or an unprotected configuration file.

Returns {} for an empty string.

See also: dictForArgs() and expandDictWithExtras() in this module.

MiscUtils.DictForArgs.dictForArgs(s)

Build dictionary from arguments.

Takes an input such as:

x=3
name="foo"
first='john' last='doe'
required border=3

And returns a dictionary representing the same. For keys that aren’t given an explicit value (such as ‘required’ above), the value is ‘1’.

All values are interpreted as strings. If you want ints and floats, you’ll have to convert them yourself.

This syntax is equivalent to what you find in HTML and close to other ML languages such as XML.

Returns {} for an empty string.

The informal grammar is:

(NAME [=NAME|STRING])*

Will raise DictForArgsError if the string is invalid.

See also: pyDictForArgs() and expandDictWithExtras() in this module.

MiscUtils.DictForArgs.expandDictWithExtras(d, key='Extras', delKey=True, dictForArgs=<function dictForArgs>)

Return a dictionary with the ‘Extras’ column expanded by dictForArgs().

For example, given:

{'Name': 'foo', 'Extras': 'x=1 y=2'}

The return value is:

{'Name': 'foo', 'x': '1', 'y': '2'}

The key argument controls what key in the dictionary is used to hold the extra arguments. The delKey argument controls whether that key and its corresponding value are retained. The same dictionary may be returned if there is no extras key. The most typical use of this function is to pass a row from a DataTable that was initialized from a CSV file (e.g., a spreadsheet or tabular file). FormKit and MiddleKit both use CSV files and allow for an Extras column to specify attributes that occur infrequently.

MiscUtils.DictForArgs.pyDictForArgs(s)

Build dictionary from arguments.

Takes an input such as:

x=3
name="foo"
first='john'; last='doe'
list=[1, 2, 3]; name='foo'

And returns a dictionary representing the same.

All values are interpreted as Python expressions. Any error in these expressions will raise the appropriate Python exception. This syntax allows much more power than dictForArgs() since you can include lists, dictionaries, actual ints and floats, etc.

This could also open the door to hacking your software if the input comes from a tainted source such as an HTML form or an unprotected configuration file.

Returns {} for an empty string.

See also: dictForArgs() and expandDictWithExtras() in this module.