python - How can I define an application's Validation Rules in an XML file -


i have application validates csv file against set rules. application checks if "columns/fields" in cvs marked mandatory, others checks if mandatory status based upon field. e.g. column 2 has conditional check against column 5 such if column 5 has value, column 2 must have value.

i have implemented using vb , python. problem logic hard coded in application. want move rules xml application read xml , process file. if rules processing change -and change often- application remains same , xml changes.

here 2 sample rules in python:

sample one

current_column_data = 5 #data current position in csv if validate_data_type(current_column_data, expected_data_type) == false:     return error_message index_to_check_against = 10 #column against there "logical" test text_to_check = get_text(index_to_check_against) if validate_data_type(text_to_check, expected_data_type) == false:     return error_message if current_column_data > 10:    #this test checking string vs string have keep in mind avoid errors since current column data string value     if text_to_check <= 0:         return "text check should greater 0 if current column data greater 10 " 

sample two

current_column_data = "self employed" #data current position in csv if validate_data_type(current_column_data, expected_data_type) == false:     return error_message index_to_check_against = 10 #column against there "logical" test text_to_check = get_text(index_to_check_against) if validate_data_type(text_to_check, expected_data_type) == false:     return error_message if text_to_check == "a":    #here expect if provided in index check, current column should have value hence raise error message     if len(current_column_data) = 0:         return "current column mandatory given "a" provided in column_to_check"" 

note: each column in csv, know data type expect, expected length of field, whether mandatory, optional or conditional , if conditional other column condition based on

now need guidance on how can possibly in xml , application reads xml , knows each column.

someone suggested following sample elsewhere still can't wrap head around concept.:

<check left="" right="9" operation="gte" value="3" error_message="logical failure something" />  #meaning: column 9 should "gte" i.e. greater or equal 2 value 3" 

is there different way go achieving kind of logic or way improve have here?

suggestions , pointers welcome

this concept called domain specific language (dsl) - creating mini-programming language validating csv files. dsl allows express succinctly rules valid csv file.

this dsl expressed using xml, or alternative approach develop library of functions in python instead. dsl expressed mini-python program sequence of these functions. approach called in-language or "internal" dsl - , has benefit have full power of python @ disposal within language.

looking @ samples - you're close already. when read them, they're english description of csv validation rules.

don't feel have go down xml route - there's nothing wrong keeping in python

  • you can split code, have python file "csv validation rules" expressed in dsl, need update/redistribute frequently, , separate files define dsl functions, change less frequently
  • in cases it's possible develop dsl point non-programmers can update/maintain "programs" written in it

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -