Data structure for maintaining tabular data in memory?

前端 未结 6 1995
孤城傲影
孤城傲影 2020-12-04 05:41

My scenario is as follows: I have a table of data (handful of fields, less than a hundred rows) that I use extensively in my program. I also need this data to be persistent,

6条回答
  •  时光取名叫无心
    2020-12-04 06:28

    I personally wrote a lib for pretty much that quite recently, it is called BD_XML

    as its most fundamental reason of existence is to serve as a way to send data back and forth between XML files and SQL databases.

    It is written in Spanish (if that matters in a programming language) but it is very simple.

    from BD_XML import Tabla
    

    It defines an object called Tabla (Table), it can be created with a name for identification an a pre-created connection object of a pep-246 compatible database interface.

    Table = Tabla('Animals') 
    

    Then you need to add columns with the agregar_columna (add_column) method, with can take various key word arguments:

    • campo (field): the name of the field

    • tipo (type): the type of data stored, can be a things like 'varchar' and 'double' or name of python objects if you aren't interested in exporting to a data base latter.

    • defecto (default): set a default value for the column if there is none when you add a row

    • there are other 3 but are only there for database tings and not actually functional

    like:

    Table.agregar_columna(campo='Name', tipo='str')
    Table.agregar_columna(campo='Year', tipo='date')
    #declaring it date, time, datetime or timestamp is important for being able to store it as a time object and not only as a number, But you can always put it as a int if you don't care for dates
    Table.agregar_columna(campo='Priority', tipo='int')
    

    Then you add the rows with the += operator (or + if you want to create a copy with an extra row)

    Table += ('Cat', date(1998,1,1), 1)
    Table += {'Year':date(1998,1,1), 'Priority':2, Name:'Fish'}
    #…
    #The condition for adding is that is a container accessible with either the column name or the position of the column in the table
    

    Then you can generate XML and write it to a file with exportar_XML (export_XML) and escribir_XML (write_XML):

    file = os.path.abspath(os.path.join(os.path.dirname(__file__), 'Animals.xml'))
    Table.exportar_xml()
    Table.escribir_xml(file)
    

    And then import it back with importar_XML (import_XML) with the file name and indication that you are using a file and not an string literal:

    Table.importar_xml(file, tipo='archivo')
    #archivo means file
    

    Advanced

    This are ways you can use a Tabla object in a SQL manner.

    #UPDATE  SET Name = CONCAT(Name,' ',Priority), Priority = NULL WHERE id = 2
    for row in Table:
        if row['id'] == 2:
            row['Name'] += ' ' + row['Priority']
            row['Priority'] = None
    print(Table)
    
    #DELETE FROM 
    WHERE MOD(id,2) = 0 LIMIT 1 n = 0 nmax = 1 for row in Table: if row['id'] % 2 == 0: del Table[row] n += 1 if n >= nmax: break print(Table)

    this examples assume a column named 'id' but can be replaced width row.pos for your example.

    if row.pos == 2:
    

    The file can be download from:

    https://bitbucket.org/WolfangT/librerias

    提交回复
    热议问题