Boolean-Cayley-graphs / boolean_cayley_graphs / __pycache__ / classification_database_duckdb.cpython-39-pytest-6.2.5.pyc
22144 viewsa ���`)P � @ s� d Z ddlZddlm mZ ddlZddlZddl Z ddl mZ ddlm Z ddlmZmZ ddlmZ dd� Zd d � Zdd� Zd d� Zdd� Zddd�Zdd� Zefdd�Zdd� ZdS )a Interface to a classification database using duckdb =================================================== The ``classification_database_duckdb`` module defines interfaces to manipulate an duckdb 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 )aK 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_duckdb import * sage: conn = create_database(db_name) sage: type(conn) <type 'duckdb.Connection'> sage: drop_database(db_name) )�duckdb�connect�Row�row_factory��db_name�conn� r �X/home/user/Boolean-Cayley-graphs/boolean_cayley_graphs/classification_database_duckdb.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_duckdb import * sage: conn = create_database(db_name) sage: con2 = connect_to_database(db_name) sage: type(con2) <type 'duckdb.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 s2 t j�| �r.zt �| � W n ty, Y n0 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_duckdb 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_duckdb 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, graph_id INTEGER, FOREIGN KEY(nvariables, bent_function) REFERENCES bent_function(nvariables, bent_function), FOREIGN KEY(graph_id) REFERENCES graph(graph_id), PRIMARY KEY(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(bent_function, bent_cayley_graph_index) REFERENCES cayley_graph(bent_function, cayley_graph_index), FOREIGN KEY(bent_function, dual_cayley_graph_index) REFERENCES cayley_graph(bent_function, cayley_graph_index), PRIMARY KEY(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 )NzUTF-8)�bytes�hashlib�sha256�digest)�g�encodingZbytes_gr r r �canonical_label_hash� s r&