Interface to a classification database using psycopg2

The classification_database_psycopg2 module defines interfaces to manipulate a PostgreSQL database of Cayley graph classifications.

AUTHORS:

  • Paul Leopardi (2017-10-28)

class boolean_cayley_graphs.classification_database_psycopg2.Psycopg2Default[source]

Bases: object

A default psycopg2 value.

NOTE:

From: Daniele Varrazzo
Date: `24 August 2014, 15:50:38`
Source: `https://postgrespro.com/list/thread-id/1544890`
See: `http://initd.org/psycopg/docs/advanced.html#adapting-new-python-types-to-sql-syntax`

TESTS:

sage: from boolean_cayley_graphs.classification_database_psycopg2 import *
sage: PSYCOPG2_DEFAULT = Psycopg2Default()
getquoted()[source]

See: http://initd.org/psycopg/docs/advanced.html

TESTS:

sage: from boolean_cayley_graphs.classification_database_psycopg2 import *
sage: PSYCOPG2_DEFAULT = Psycopg2Default()
sage: PSYCOPG2_DEFAULT.getquoted()
'DEFAULT'
boolean_cayley_graphs.classification_database_psycopg2.canonical_label_hash(canonical_label)[source]

Hash function for Graph canonical labels.

INPUT:

  • canonical_label – string. A graph6_string encoding a Graph canonical label.

OUTPUT: The sha256 hash of canonical_label as a psycopg2.Binary buffer.

TESTS:

sage: from boolean_cayley_graphs.classification_database_psycopg2 import *
sage: clh = canonical_label_hash("Arbitrary string")
sage: type(clh)
<class 'psycopg2.extensions.Binary'>
boolean_cayley_graphs.classification_database_psycopg2.connect_to_database(dbname, user=None, password=None, host=None)[source]

Connect to an existing database.

INPUT:

  • dbname – string. The name of the existing database.

  • user – string, optional. A Postgres user with appropriate permissions on the host.

  • password – string, optional. The Postgres password of user.

  • host – string, optional. The machine running the Postgres database management system that hosts the database.

OUTPUT: a database connection object.

EXAMPLE:

Create a database using a standardized name, connect to it, then drop the database.

sage: from boolean_cayley_graphs.classification_database_psycopg2 import *
sage: from psycopg2 import ProgrammingError
sage: dbname = 'doctest_connect_to_database_dbname'
sage: drop_database(dbname)
sage: conn = create_database(dbname)
sage: conn.close()
sage: con2 = connect_to_database(dbname)
sage: type(con2)
<class 'psycopg2.extensions.connection'>
sage: con2.close()
sage: drop_database(dbname)
boolean_cayley_graphs.classification_database_psycopg2.create_classification_tables(dbname, user=None, password=None, host=None)[source]

Create the tables used for a database of Cayley graph classifications.

INPUT:

  • dbname – string. The name of an existing database.

  • user – string, optional. A Postgres user with appropriate permissions on the host.

  • password – string, optional. The Postgres password of user.

  • host – string, optional. The machine running the Postgres database management system that hosts the database.

OUTPUT: a database connection object.

EXAMPLE:

Create a database, with tables, using a standardized name, list the table names, then drop the database.

sage: from boolean_cayley_graphs.classification_database_psycopg2 import *
sage: dbname = 'doctest_create_classification_tables_dbname'
sage: drop_database(dbname)
sage: conn = create_database(dbname)
sage: conn.close()
sage: conn = create_classification_tables(dbname)
sage: curs = conn.cursor()
sage: curs.execute(
....:     "SELECT table_name " +
....:     "FROM information_schema.tables " +
....:     "WHERE table_schema='public' AND table_type='BASE TABLE' " +
....:     "ORDER BY table_name")
sage: for row in curs:
....:     for x in row:
....:         print(x)
....:
bent_function
cayley_graph
graph
matrices
sage: conn.close()
sage: drop_database(dbname)
boolean_cayley_graphs.classification_database_psycopg2.create_database(dbname, user=None, password=None, host=None)[source]

Create a database.

INPUT:

  • dbname – string. The name of the database to be created.

  • user – string, optional. A Postgres user with appropriate permissions on the host.

  • password – string, optional. The Postgres password of user.

  • host – string, optional. The machine running the Postgres database management system that hosts the database.

OUTPUT: a database connection object.

EXAMPLE:

Create a database using a standardized name, then drop the database.

sage: from boolean_cayley_graphs.classification_database_psycopg2 import *
sage: from psycopg2 import ProgrammingError
sage: dbname = 'doctest_create_database_dbname'
sage: drop_database(dbname)
sage: conn = create_database(dbname)
sage: type(conn)
<class 'psycopg2.extensions.connection'>
sage: conn.close()
sage: drop_database(dbname)
boolean_cayley_graphs.classification_database_psycopg2.drop_database(dbname, user=None, password=None, host=None)[source]

Drop a database, if it exists.

INPUT:

  • dbname – string. The name of the existing database.

  • user – string, optional. A Postgres user with appropriate permissions on the host.

  • password – string, optional. The Postgres password of user.

  • host – string, optional. The machine running the Postgres database management system that hosts the database.

OUTPUT: None.

EXAMPLE:

Create a database using a standardized name, then drop the database.

sage: from boolean_cayley_graphs.classification_database_psycopg2 import *
sage: dbname = 'doctest_drop_database_dbname'
sage: drop_database(dbname)
sage: conn = create_database(dbname)
sage: type(conn)
<class 'psycopg2.extensions.connection'>
sage: conn.close()
sage: drop_database(dbname)
sage: drop_database(dbname)
boolean_cayley_graphs.classification_database_psycopg2.insert_classification(conn, bfcgc, name=None)[source]

Insert a Cayley graph classification into a database.

INPUT:

  • conn – a connection object for the database.

  • bfcgc – a Cayley graph classification.

  • name – string (default: None). The name of the bent function.

OUTPUT: None.

A cursor object corresponding to state of the database after the classification is inserted.

EXAMPLE:

Create a database, with tables, using a standardized name, insert a classification, retrieve it by bent function, then drop the database.

sage: from boolean_cayley_graphs.classification_database_psycopg2 import *
sage: from boolean_cayley_graphs.bent_function import BentFunction
sage: from boolean_cayley_graphs.bent_function_cayley_graph_classification import BentFunctionCayleyGraphClassification
sage: bentf = BentFunction([0,0,0,1])
sage: bfcgc = BentFunctionCayleyGraphClassification.from_function(bentf)
sage: bfcgc.algebraic_normal_form
x0*x1
sage: dbname = 'doctest_insert_classification_dbname'
sage: drop_database(dbname)
sage: conn = create_database(dbname)
sage: conn.close()
sage: conn = create_classification_tables(dbname)
sage: insert_classification(conn, bfcgc, 'bentf')
sage: result = select_classification_where_bent_function(conn, bentf)
sage: result.algebraic_normal_form
x0*x1
sage: conn.close()
sage: drop_database(dbname)
boolean_cayley_graphs.classification_database_psycopg2.select_classification_where_bent_function(conn, bentf)[source]

Retrieve a Cayley graph classification for a given bent function from a database.

INPUT:

  • conn – a connection object for the database.

  • bentf – class BentFunction. A bent function.

OUTPUT:

class BentFunctionCayleyGraphClassification. The corresponding a Cayley graph classification.

EXAMPLE:

Create a database, with tables, using a standardized name, insert a classification, retrieve it by bent function, then drop the database.

sage: from boolean_cayley_graphs.classification_database_psycopg2 import *
sage: from boolean_cayley_graphs.bent_function import BentFunction
sage: from boolean_cayley_graphs.bent_function_cayley_graph_classification import BentFunctionCayleyGraphClassification
sage: bentf = BentFunction([0,0,0,1])
sage: bfcgc = BentFunctionCayleyGraphClassification.from_function(bentf)
sage: bfcgc.algebraic_normal_form
x0*x1
sage: dbname = 'doctest_select_classification_where_bent_function_dbname'
sage: drop_database(dbname)
sage: conn = create_database(dbname)
sage: conn.close()
sage: conn = create_classification_tables(dbname)
sage: insert_classification(conn, bfcgc, 'bentf')
sage: result = select_classification_where_bent_function(conn, bentf)
sage: result.algebraic_normal_form
x0*x1
sage: type(result)
<class 'boolean_cayley_graphs.bent_function_cayley_graph_classification.BentFunctionCayleyGraphClassification'>
sage: result.report(report_on_matrix_details=True)
Algebraic normal form of Boolean function: x0*x1
Function is bent.
<BLANKLINE>
Weight class matrix:
[0 0 0 1]
[0 1 0 0]
[0 0 1 0]
[1 0 0 0]
<BLANKLINE>
SDP design incidence structure t-design parameters: (True, (1, 4, 1, 1))
<BLANKLINE>
Classification of Cayley graphs and classification of Cayley graphs of duals are the same:
<BLANKLINE>
There are 2 extended Cayley classes in the extended translation class.
<BLANKLINE>
Matrix of indices of Cayley graphs:
[0 0 0 1]
[0 1 0 0]
[0 0 1 0]
[1 0 0 0]
sage: conn.close()
sage: drop_database(dbname)
boolean_cayley_graphs.classification_database_psycopg2.select_classification_where_bent_function_cayley_graph(conn, bentf, algorithm='sage')[source]

Given a bent function bentf, retrieve all classifications that contain a Cayley graph isomorphic to the Cayley graph of bentf.

INPUT:

  • conn – a connection object for the database.

  • bentf – class BentFunction. A bent function.

  • algorithm – string (default: BentFunctionCayleyGraphClassification.default_algorithm). Algorithm used for canonical labelling.

OUTPUT:

A list where each entry has class BentFunctionCayleyGraphClassification. The corresponding list of Cayley graph classifications.

NOTE:

The list is not sorted in any way.

EXAMPLE:

Create a database, with tables, using a standardized name, insert a classification, retrieve all related classifications by bent function Cayley graph, then drop the database.

sage: from boolean_cayley_graphs.classification_database_psycopg2 import *
sage: from boolean_cayley_graphs.bent_function import BentFunction
sage: from boolean_cayley_graphs.bent_function_cayley_graph_classification import BentFunctionCayleyGraphClassification
sage: dbname = 'doctest_select_classification_where_bent_function_dbname'
sage: drop_database(dbname)
sage: conn = create_database(dbname)
sage: conn.close()
sage: conn = create_classification_tables(dbname)
sage: bentf0 = BentFunction([0,0,0,1])
sage: bfcgc0 = BentFunctionCayleyGraphClassification.from_function(bentf0)
sage: bfcgc0.algebraic_normal_form
x0*x1
sage: insert_classification(conn, bfcgc0, 'bentf0')
sage: bentf1 = BentFunction([1,0,0,0])
sage: bfcgc1 = BentFunctionCayleyGraphClassification.from_function(bentf1)
sage: bfcgc1.algebraic_normal_form
x0*x1 + x0 + x1 + 1
sage: insert_classification(conn, bfcgc1, 'bentf1')
sage: result = select_classification_where_bent_function_cayley_graph(conn, bentf1)
sage: type(result)
<class 'list'>
sage: len(result)
2
sage: sorted_result = sorted(result, key=lambda c: str(c.algebraic_normal_form))
sage: for c in sorted_result:
....:     type(c)
....:     c.algebraic_normal_form
....:     c.report()
<class 'boolean_cayley_graphs.bent_function_cayley_graph_classification.BentFunctionCayleyGraphClassification'>
x0*x1
Algebraic normal form of Boolean function: x0*x1
Function is bent.
<BLANKLINE>
<BLANKLINE>
SDP design incidence structure t-design parameters: (True, (1, 4, 1, 1))
<BLANKLINE>
Classification of Cayley graphs and classification of Cayley graphs of duals are the same:
<BLANKLINE>
There are 2 extended Cayley classes in the extended translation class.
<class 'boolean_cayley_graphs.bent_function_cayley_graph_classification.BentFunctionCayleyGraphClassification'>
x0*x1 + x0 + x1 + 1
Algebraic normal form of Boolean function: x0*x1 + x0 + x1 + 1
Function is bent.
<BLANKLINE>
<BLANKLINE>
SDP design incidence structure t-design parameters: (True, (1, 4, 1, 1))
<BLANKLINE>
Classification of Cayley graphs and classification of Cayley graphs of duals are the same:
<BLANKLINE>
There are 2 extended Cayley classes in the extended translation class.
sage: conn.close()
sage: drop_database(dbname)
boolean_cayley_graphs.classification_database_psycopg2.select_classification_where_name(conn, name)[source]

Retrieve a Cayley graph classification for a bent function with a given name from a database.

INPUT:

  • conn – a connection object for the database.

  • name – string. The name of the bent function.

OUTPUT: class BentFunctionCayleyGraphClassification. The corresponding a Cayley graph classification.

EXAMPLE:

Create a database, with tables, using a standardized name, insert a classification, retrieve it by bent function, then drop the database.

sage: from boolean_cayley_graphs.classification_database_psycopg2 import *
sage: from boolean_cayley_graphs.bent_function import BentFunction
sage: from boolean_cayley_graphs.bent_function_cayley_graph_classification import BentFunctionCayleyGraphClassification
sage: bentf = BentFunction([0,0,0,1])
sage: bfcgc = BentFunctionCayleyGraphClassification.from_function(bentf)
sage: bfcgc.algebraic_normal_form
x0*x1
sage: dbname = 'doctest_select_classification_where_bent_function_dbname'
sage: drop_database(dbname)
sage: conn = create_database(dbname)
sage: conn.close()
sage: conn = create_classification_tables(dbname)
sage: insert_classification(conn, bfcgc, 'bentf')
sage: result = select_classification_where_name(conn, 'bentf')
sage: result.algebraic_normal_form
x0*x1
sage: type(result)
<class 'boolean_cayley_graphs.bent_function_cayley_graph_classification.BentFunctionCayleyGraphClassification'>
sage: result.report(report_on_matrix_details=True)
Algebraic normal form of Boolean function: x0*x1
Function is bent.
<BLANKLINE>
Weight class matrix:
[0 0 0 1]
[0 1 0 0]
[0 0 1 0]
[1 0 0 0]
<BLANKLINE>
SDP design incidence structure t-design parameters: (True, (1, 4, 1, 1))
<BLANKLINE>
Classification of Cayley graphs and classification of Cayley graphs of duals are the same:
<BLANKLINE>
There are 2 extended Cayley classes in the extended translation class.
<BLANKLINE>
Matrix of indices of Cayley graphs:
[0 0 0 1]
[0 1 0 0]
[0 0 1 0]
[1 0 0 0]
sage: conn.close()
sage: drop_database(dbname)