Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7537359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:49:53+00:00 2026-05-30T06:49:53+00:00

I am writing a Python script that uses data of dubious quality. The data

  • 0

I am writing a Python script that uses data of dubious quality. The data is being stored in an SQLite database.

I would like a compact way to specify constraints on the data. The constraints are of two types:

  1. Data errors – an error message will be issued.
    • “Column A must be an integer in the range 0-10”
    • “Column B must be a non-blank string”, and so forth.
  2. Data quality warnings – “are you sure this is right?” A warning message will be issued. The constraints would be things like
    • “warn if Column C has a default value of 0” – are you sure the typist didn’t miss an entry?
    • “warn if the number in Column D is unusually large (> 1000)”.

Ideally, I would like to express my constraints in a human-readable format like:

'kV' MUST BE float IN RANGE 0-10
'Rating' SHOULD NOT BE DEFAULT 1.0
'Description' SHOULD NOT BE DEFAULT ""

… but I’ll take any improvement on my current approach (below). I would be happy to accept a solution that involves enforcing the constraints in either Python or a SQLite schema.

Here’s what I’m using at the moment:

 def is_number_in_range(number, expected_type, lower, upper):
    if type(number) != expected_type:
        return "not an %s" % expected_type
    elif ((number < lower) or (number > upper)):
        return "%s out of range [%i-%i]." % (expected_type, upper, lower)
    else:
        return "OK"

def not_default (value, expected_type, default_value):
    if type(value) != expected_type:
        return "not an %s" % expected_type
    elif value == default_value:
        return "default value of %s - make sure this is what you want." % default_value
    else:
        return "OK"

def Check_Cable_Lib(db_conn):
    res = db_conn.execute("SELECT * FROM Lib_Cable LIMIT 1")

    constraints = (
        ('kV', lambda x: is_number_in_range(x, float, 0, 1000) ),
        ('kA1', lambda x: is_number_in_range (x, float, 0, 10) ),
        ('kA1', lambda x: not_default(x, float, 1.0))
    )

    for cable_type in res:
        for constraint in constraints:
            constraint_variable = constraint[0]
            constraint_data = cable_type[constraint_variable]
            constraint_function = constraint[1]

            validation_message = constraint_function(constraint_data)
            print ("%(constraint_variable)s = %(constraint_data)s : %(validation_message)s" % locals())

stage1_db_path = "stage1.sqlite3";
db_conn = sqlite3.connect(stage1_db_path)
db_conn.row_factory = sqlite3.Row
Check_Cable_Lib(db_conn)

Example output:

kV = 11.0 : OK
kA1 = 1.0 : OK
kA1 = 1.0 : default value of 1.0 - make sure this is what you want.

EDIT: I’m aware it’s impolite to explicitly check types in Python. However for the sake of the code that uses the data, I need to check that SQLite hasn’t stored unexpected things in the columns (“hello world” in an INT column, etc.) Remember the data is of dubious quality and SQLite will happily put any type of data in any column. Catching these types of data entry errors is one of the objectives of this code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T06:49:54+00:00Added an answer on May 30, 2026 at 6:49 am

    Combining @onedaywhen’s idea to use SQL to check the constraints, and @ABS’s idea to define the constraints in a more readable way, here’s what I’ve come up with.

    Wrapping it up in a class probably isn’t particularly useful (as used in the example it’s a glorified wrapper around the check() function), but it means I can bake some slightly nicer output formatting into it later.

    class Constraint:
        def __init__(self, table, column, constraint, error_or_warning):
            """
            examples:
                Constraint('Lib_Cable','kV', '> 0', 'error')
                Constraint('Lib_Cable','Insulation', '!= 0', 'warning')
    
            """
            self.table, self.column, self.constraint, self.error_or_warning = \
            table, column, constraint, error_or_warning
    
        def check (self, db_conn):
            c = db_conn.cursor()
            c.row_factory = sqlite3.Row
    
            query = "SELECT * FROM %(table)s WHERE NOT (%(column)s %(constraint)s)" % \
                    {'table'      : self.table,
                     'column'     : self.column,
                     'constraint' : self.constraint }
            # [FIXME] start a transaction here - guard against novice SQL injections?
            res = c.execute(query)
    
            for row in res:
                print ( "%(error_or_warning)s: Row with key %(key)s in table %(table)s violates constraint %(column)s %(constraint)s)." % \
                    {'key'        : row['KeyName'],
                     'table'      : self.table,
                     'column'     : self.column,
                     'constraint' : self.constraint,
                     'error_or_warning' : self.error_or_warning} )
    
            # [FIXME] discard transaction
    
    Constraint_1 = Constraint('Lib_Cable', 'Insulation', '!= 0', 'error')        
    Constraint_1.check(db_conn)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a python script that uses subprocess.Popen to execute two programs (from compiled
I'm writing a python script that scrapes a website, where the website uses OpenID
I'm writing a python script that uses this awkward glob syntax. import glob F
I would like to write a python script that would upload any file I
I have a python script that is constantly grabbing data from Twitter and writing
im writing a python script that is being executed in cygwin. one of the
I'm writing a python script that uses os.walk() to walk a directory tree. I'd
I've been writing python scripts that run locally. I would now like to offer
I'm writing a python script that executes a csh script in Solaris 10. The
I amm writing a little python script that will grab information from VMs of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.