Boolean-Cayley-graphs / boolean_cayley_graphs / __pycache__ / classification_database_sqlite3.cpython-38.pyc
22144 viewsU '@�bSS � @ s� d Z ddlZddlZddlZddlmZ ddlmZ ddlm Z m Z ddlmZ dd� Z d d � Zdd� Zd d� Zdd� Zdd� Zddd�Zdd� Ze fdd�Zdd� ZdS )a Interface to a classification database using sqlite3 ==================================================== The ``classification_databasepsqlite3`` module defines interfaces to manipulate an SQLite3 database of Cayley graph classifications. AUTHORS: - Paul Leopardi (2017-10-28) � N)�matrix)�BentFunction)�%BentFunctionCayleyGraphClassification�default_algorithm)�weight_classc C s t �| �}t j|_|S )aN Create a database. INPUT: - ``db_name`` -- string. The name of the database to be created. OUTPUT: a database connection object. EXAMPLE: Create a database using a temporary filename, then drop the database. :: sage: from sage.misc.temporary_file import tmp_filename sage: db_name = tmp_filename(ext='.db') sage: from boolean_cayley_graphs.classification_database_sqlite3 import * sage: conn = create_database(db_name) sage: type(conn) <class 'sqlite3.Connection'> sage: drop_database(db_name) )�sqlite3�connect�Row�row_factory��db_name�conn� r �Y/home/user/Boolean-Cayley-graphs/boolean_cayley_graphs/classification_database_sqlite3.py�create_database# s r c C s4 t j�| �r"t�| �}tj|_|S td�| ���dS )a� Connect to an existing database. INPUT: - ``db_name`` -- string. The name of the existing database. OUTPUT: a database connection object. EXAMPLE: Create a database using a temporary filename, connect to it, then drop the database. :: sage: from sage.misc.temporary_file import tmp_filename sage: db_name = tmp_filename(ext='.db') sage: from boolean_cayley_graphs.classification_database_sqlite3 import * sage: conn = create_database(db_name) sage: con2 = connect_to_database(db_name) sage: type(con2) <class 'sqlite3.Connection'> sage: drop_database(db_name) zFile not found: {}N) �os�path�isfiler r r r �IOError�formatr r r r �connect_to_database@ s r c C s4 t j�| �r0zt �| � W n tk r. Y nX dS )a� Drop a database, if it exists. INPUT: - ``db_name`` -- string. The name of the existing database. OUTPUT: None. EXAMPLE: Create a database using a temporary filename, then drop the database. :: sage: from boolean_cayley_graphs.classification_database_sqlite3 import * sage: import os sage: db_name = tmp_filename(ext='.db') sage: conn = create_database(db_name) sage: os.path.exists(db_name) True sage: drop_database(db_name) sage: os.path.exists(db_name) False sage: drop_database(db_name) sage: os.path.exists(db_name) False N)r r �exists�remove�OSError)r r r r � drop_databasea s r c C sD t | �}|�� }|�d� |�d� |�d� |�d� |�� |S )a Create the tables used for a database of Cayley graph classifications. INPUT: - ``db_name`` -- string. The name of an existing database. OUTPUT: a database connection object. EXAMPLE: Create a database, with tables, using a temporary filename, list the table names, then drop the database. :: sage: from boolean_cayley_graphs.classification_database_sqlite3 import * sage: import os sage: db_name = tmp_filename(ext='.db') sage: conn = create_database(db_name) sage: conn.close() sage: conn = create_classification_tables(db_name) sage: os.path.exists(db_name) True sage: curs = conn.cursor() sage: result = curs.execute("SELECT name FROM sqlite_master WHERE type='table'") sage: for row in curs: ....: for x in row: ....: print(x) ....: bent_function graph cayley_graph matrices sage: conn.close() sage: drop_database(db_name) z� CREATE TABLE bent_function( nvariables INTEGER, bent_function BLOB, name TEXT UNIQUE, PRIMARY KEY(nvariables, bent_function))z� CREATE TABLE graph( graph_id INTEGER, canonical_label_hash BLOB UNIQUE, canonical_label TEXT, PRIMARY KEY(graph_id))a� CREATE TABLE cayley_graph( nvariables INTEGER, bent_function BLOB, cayley_graph_index INTEGER, canonical_label_hash BLOB, FOREIGN KEY(nvariables, bent_function) REFERENCES bent_function(nvariables, bent_function), FOREIGN KEY(canonical_label_hash) REFERENCES graph(canonical_label_hash), PRIMARY KEY(nvariables, bent_function, cayley_graph_index))a� CREATE TABLE matrices( nvariables INTEGER, bent_function BLOB, b INTEGER, c INTEGER, bent_cayley_graph_index INTEGER, dual_cayley_graph_index INTEGER, weight_class INTEGER, FOREIGN KEY(nvariables, bent_function) REFERENCES bent_function(nvariables, bent_function), FOREIGN KEY(nvariables, bent_function, bent_cayley_graph_index) REFERENCES cayley_graph(nvariables, bent_function, cayley_graph_index), FOREIGN KEY(nvariables, bent_function, dual_cayley_graph_index) REFERENCES cayley_graph(nvariables, bent_function, cayley_graph_index), PRIMARY KEY(nvariables, bent_function, b, c)))r �cursor�execute�commit)r r �cursr r r �create_classification_tables� s & r c C s d}t | |�}t�|��� S )a� Hash a graph canonical label. INPUT: - ``g`` -- a graph canonical label. OUTPUT: A hash digest as a bytes object. EXAMPLE: :: sage: from boolean_cayley_graphs.classification_database_sqlite3 import * sage: from boolean_cayley_graphs.bent_function import BentFunction sage: bentf = BentFunction([0,0,0,1]) sage: cayley_graph = bentf.extended_cayley_graph() sage: cgcl = cayley_graph.canonical_label().graph6_string() sage: cgcl_hash = canonical_label_hash(cgcl) sage: print(type(cgcl_hash)) <class 'bytes'> sage: print(len(cgcl_hash)) 32 zUTF-8)�bytes�hashlib�sha256�digest)�g�encodingZbytes_gr r r �canonical_label_hash� s r&