[973ab6]: / ReadersWriters / __pycache__ / ReadersWriters.cpython-35.pyc

Download this file

212 lines (211 with data), 19.2 kB



■ă§YPŃ@sdZddlmZmZmZmZmZddlZddlZddl	Z	ddl
mZddlm
Z
ddlmZddlmZddlmZdd	lmZed
âZdZdZdgZd
ZdZdZdZdZGddädâZdS)z; It is an interface for the developed readers and writers.
Ú)┌List┌TypeVar┌Dict┌Any┌CallableN)┌CsvFile)┌MysqlCommand)┌MysqlConnection)┌PickleSerialised)┌TextFile)┌MysqlQueries┌	DataFramezMohsen Mesgarpourz-Copyright 2016, https://github.com/mesgarpour┌GPLz1.1zmohsen.mesgarpour@gmail.com┌Releasec@s§eZdZeeeeedddÉäâZeeeedddÉäâZedeeeeddd	ÉäâZeeeeedd
dÉäâZ	eeeeedd
dÉäâZ
eeeeeedddÉäâZeeeeedddÉäâZ
eeeedddÉäâZeeeedddÉäâZedeeeedddÉäâZeeeeedddÉäâZeeeedddÉäâZeeeedd d!ÉäâZed"eeeegd"feeed#d$d%ÉäâZed&deeeeeegd"feeed'd(d)ÉäâZed&d*eeeeeegd"fd+d,d-ÉäâZeeeed.d/d0ÉäâZeeeed.d1d2ÉäâZed3eeed4d5d6ÉäâZed3d"d"d"eeeeeeeeegd"fd7d8d9ÉäâZed:d3deeeeeeeeegd"fd;d<d=ÉäâZ eeeeeed>d?d@ÉäâZ!eeeeedAdBdCÉäâZ"eeedDdEdFÉäâZ#edeeedGdHdIÉäâZ$ed*eeedGdJdKÉäâZ%ee&âe&âdLeeeeeeeedMdNdOÉ	äâZ'eeeeedPdQdRÉäâZ(eeedSdTdUÉäâZ)eeedVdWdXÉäâZ*eedYdZd[ÉäâZ+d"S)\┌ReadersWriters)┌path┌title┌ext┌returncCs&tâ}|j|||â|jâS)z°Check if the serialised object exists.
        :param path: the output directory path.
        :param title: the title of the output file.
        :param ext: the extension of the output file.
        :return: indicates if the file exists.
        )r
┌set┌exists)rrr┌readeręr˙UC:\Users\eagle\Documents\GitHub\Analytics_UoW\TCARER\ReadersWriters\ReadersWriters.py┌exists_serialised-s
	z ReadersWriters.exists_serialised)┌	db_schema┌db_tablercCsLtâ}|j|â}tj||ddâ}|dk	oKt|âdkS)z│Check if MySQL table exists.
        :param db_schema: the MySQL database schema.
        :param db_table: the MySQL table.
        :return: indicates if the file exists.
        ┌dataframingTNr)r┌exists_tabler┌load_mysql_query┌len)rr┌queries┌query┌outputrrr┌exists_mysql;s	zReadersWriters.exists_mysql┌csvcCs&tâ}|j|||â|jâS)aCheck if the CSV file exists.
        :param path: the directory path of the CSV file.
        :param title: the file name of the CSV file.
        :param ext: the extension of the CSV file (default: 'csv').
        :return: indicates if the file exists.
        )rrr)rrrrrrr┌
exists_csvHs
	zReadersWriters.exists_csvcCs&tâ}|j|||â|jâS)z¨Check if the text file exists.
        :param path: the directory path of the text file.
        :param title: the file name of the text file.
        :param ext: he extension of the text file.
        :return: indicates if the file exists.
        )rrr)rrrrrrr┌exists_textVs
	zReadersWriters.exists_text)rr┌columnrcCsOtâ}|j||â}tj||ddâ}|dk	oNt|âdkS)zţCheck if a column exists in a MySQL table.
        :param db_schema: the MySQL database schema.
        :param db_table: the MySQL table.
        :param column: name of the column.
        :return: indicates if the column exists.
        rTNr)r┌
exists_columnrrr )rrr(r!r"r#rrr┌exists_mysql_columnds
	z"ReadersWriters.exists_mysql_column)rrr(rrcCs)tâ}|j|||â|j|âS)a4Check if the CSV file exists.
        :param path: the directory path of the CSV file.
        :param title: the file name of the CSV file.
        :param column: name of the column.
        :param ext: the extension of the CSV file (default: 'csv').
        :return: indicates if the column exists.
        )rrr))rrr(rrrrr┌exists_csv_columnss	z ReadersWriters.exists_csv_columncCs&tâ}|j|||â|jâS)aCheck the size of the saved file.
        :param path: the directory path of the serialised file.
        :param title: the title of the output file.
        :param ext: the extension of the output file.
        :return: showing stat information of the file.
        )r
r┌size)rrrrrrr┌size_serialisedâs
	zReadersWriters.size_serialisedcCs:tj||ddâ}t|ddâr6|dk	SdS)z┐Check number of records in a MySQL table.
        :param db_schema: the MySQL database schema.
        :param db_table: the MySQL table.
        :return: number of rows in the table.
        rTrN)r┌load_mysql_table┌int)rrr#rrr┌size_mysql_tableĹszReadersWriters.size_mysql_table)r"rrcCs:tj||ddâ}t|ddâr6|dk	SdS)zňCheck number of records in the output result of the MySQL query.
        :param query: the mysql query to execute.
        :param db_schema: the MySQL database schema.
        :return: number of rows in the output table.
        rTrN)rrr/)r"rr#rrr┌size_mysql_queryťszReadersWriters.size_mysql_querycCs&tâ}|j|||â|jâS)z■Check number of lines in the CSV file.
        :param path: the directory path of the CSV file.
        :param title: the title of the CSV file.
        :param ext: the extension of the CSV file.
        :return: number of lines in the CSV file.
        )rrr,)rrrrrrr┌size_csvžs
	zReadersWriters.size_csvcCs&tâ}|j|||â|jâS)aCheck number of lines in the text file.
        :param path: the directory path of the text file.
        :param title: the title of the text file.
        :param ext: the extension of the text file.
        :return: number of lines in the text file.
        )rrr,)rrrrrrr┌	size_textÁs
	zReadersWriters.size_text)rr┌objectscCs-tâ}|j||dâ|j|âdS)zŰSerialise the object (Pickle protocol=4), without compression.
        :param path: the directory path of the serialised file.
        :param title: the title of the output file.
        :param objects: the object to be saved.
        ┌pickleN)r
r┌save)rrr4┌writerrrr┌save_serialised├s		zReadersWriters.save_serialisedcCs-tâ}|j||dâ|j|âdS)z¸Serialise the object (Pickle protocol=4), then compress (BZ2 compression).
        :param path: the directory path of the serialised file.
        :param title: the title of the output file.
        :param objects: the object to be saved.
        ┌bz2N)r
r┌save_bz2)rrr4r7rrr┌save_serialised_compressedđs		z)ReadersWriters.save_serialised_compressedN)r"┌datarr┌batchc
Cstâ}|j|â|jâ}t||jâ}|dkrP|j|ân«|dkr{|j||||ddânâttj	t
|â|ââ}xatd|âD]P}	|j|||	||	d|ů||dt|	||	d|gââq¬W|j
âdS)aŹWrite several rows of data into a MySQL table, using dataframe or list.
        :param query: the mysql query to execute, if data type is list.
        :param data: the mysql query to execute (applicable for data type of list).
        :param db_schema: the MySQL database schema.
        :param db_table: the MySQL table.
        :param batch: indicates if data to be written in batches.
        N┌batch_title┌allrÚ)r	r┌openr┌db_session_vars┌write┌
write_manyr/┌math┌ceilr ┌range┌str┌close)
r"r<rrr=┌db┌engine┌dbcr,┌irrr┌
save_mysqlŢs
	
+#zReadersWriters.save_mysqlF)rrr<┌appendr┌kwargscKsFtâ}|j|||â|dkr2|jâ|j||ŹdS)aĚAppend to CSV file using dataframe, dictionary or list.
        :param path: the directory path of the CSV file.
        :param title: the file name of the CSV file.
        :param data: the data to write.
        :param append: indicates if data to be appended to an existing file.
        :param ext: the extension of the CSV file (default: 'csv').
        :param kwargs: any other arguments that the selected writer may accept.
        FN)rr┌resetrO)rrr<rOrrPr7rrr┌save_csv˙s
	
zReadersWriters.save_csv┌log)rrr<cCsCtâ}|j|||â|dkr2|jâ|j|âdS)aYAppend to text file using dataframe, dictionary or list.
        :param path: the directory path of the text file.
        :param title: the file name of the text file.
        :param data: the data to write.
        :param append: indicates if data to be appended to an existing file.
        :param ext: the extension of the CSV file.
        FN)rrrQrO)rrr<rOrr7rrr┌	save_texts

	
zReadersWriters.save_text)rrrcCs&tâ}|j||dâ|jâS)z┌Load a serialised object, that was not compressed.
        :param path: the directory path of the serialised file.
        :param title: the title of the output file.
        :return: the loaded python object.
        r5)r
r┌load)rrrrrr┌load_serialised"s	zReadersWriters.load_serialisedcCs&tâ}|j||dâ|jâS)zŔLoad a serialised object, that was compressed (BZ2 compression).
        :param path: the directory path of the serialised file.
        :param title: the title of the output file.
        :return: the loaded python object.
        r9)r
r┌load_bz2)rrrrrr┌load_serialised_compressed.s	z)ReadersWriters.load_serialised_compressedT)rrrcCsd|}tj|||âS)a7"Read a MySQL table and return it as a dataframe or a list.
        :param db_schema: the MySQL database schema.
        :param db_table: the MySQL table.
        :param dataframing: indicates if the return data will be dataframe or list.
        :return: the output of the executed query (read table).
        zSELECT * FROM )rr)rrrr"rrrr.:s

zReadersWriters.load_mysql_table)r"rrr=┌float_round_vars┌float_roundrc
Cs]tâ}|j|â|jâ}t||jâ}|j|||||â}	|jâ|	S)a*Execute a MySQL query and return the output result as dataframe or list.
        :param query: the mysql query to execute.
        :param db_schema: the MySQL database schema.
        :param dataframing: indicates if the return data will be dataframe or list.
        :param batch: indicates if data is loaded batch by batch.
        :param float_round_vars: list of float variables that needs to be rounded.
        :param float_round: the rounding precision for the 'float_round_vars' option.
        :return: the output of the executed query.
        )r	rrArrB┌readrI)
r"rrr=rYrZrJrKrLr#rrrrGs	

zReadersWriters.load_mysql_queryr)rr┌skiprrrPrcKs/tâ}|j|||â|j|||ŹS)aţRead the CSV file into dataframe or list.
        :param path: the directory path of the CSV file.
        :param title: the file name of the CSV file.
        :param skip: lines to skip before reading or writing.
        :param dataframing: indicates if the outputs must be saved into dataframe.
        :param ext: the extension of the CSV file (default: 'csv').
        :param kwargs: any other arguments that the selected reader may accept.
        :return: the read file contents.
        )rrr[)rrr\rrrPrrrr┌load_csv_s	zReadersWriters.load_csv)rrrr\rcCs)tâ}|j|||â|j|âS)aRead the text file into dataframe or list.
        :param path: the directory path of the text file.
        :param title: the file name of the text file.
        :param ext: the extension of the text file.
        :param skip: lines to skip before reading or writing.
        )rrr[)rrrr\rrrr┌	load_textss	zReadersWriters.load_text)┌db_procedure┌argsrrcCsTtâ}|j|â|jâ}t||jâ}|j||â}|jâ|S)a7Execute a MySQL procedure, and return the raw output results (if applicable).
        :param db_procedure: the mysql procedure to execute.
        :param args: the procedure's input arguments.
        :param db_schema: the MySQL database schema.
        :return: the raw output results (if applicable).
        )r	rrArrB┌	call_procrI)r_r`rrJrKrLr#rrr┌load_mysql_procedureés
	

z#ReadersWriters.load_mysql_procedure)rrcCsd|}tj||âdS)züTruncate the MySQL table.
        :param db_schema: the MySQL database schema.
        :param db_table: the MySQL table.
        zTRUNCATE TABLE N)rr)rrr"rrr┌reset_mysql_tableös
z ReadersWriters.reset_mysql_table)rrrcCs*tâ}|j|||â|jâdS)z▄Reset the CSV file reader/writer.
        :param path: the directory path of the CSV file.
        :param title: the file name of the CSV file.
        :param ext: the extension of the CSV file (default: 'csv').
        N)rrrQ)rrrrrrr┌	reset_csv×s		zReadersWriters.reset_csvcCs*tâ}|j|||â|jâdS)z¤Reset the text file reader/writer.
        :param path: the directory path of the text file.
        :param title: the file name of the text file.
        :param ext: the extension of the text file.
        N)rrrQ)rrrrrrr┌
reset_textźs		zReadersWriters.reset_textz$ENGINE=InnoDB DEFAULT CHARSET=latin1)┌table┌columns┌	col_types┌defaults┌primary_keys┌unique_keys┌metarcCs(tâ}|j|||||||âS)aňGenerate a bespoke create query.
        :param table: the MySQL table.
        :param columns: the list of columns.
        :param col_types: the list of column types for columns.
        :param defaults: the list of default values for columns.
        :param primary_keys: the list of primary keys for the table.
        :param unique_keys: the list of unique keys for the table.
        :param meta: the table metadata (Engine and charset).
        :return: the MySQL query
        )r┌create)rfrgrhrirjrkrlr!rrr┌mysql_query_createŞs	z!ReadersWriters.mysql_query_create)rfrgrhrcCstâ}|j|||âS)zŮGenerate a bespoke insert query.
        :param table: the MySQL table.
        :param columns: the list of columns.
        :param col_types: the list of column types for columns.
        :return: the MySQL query
        )r┌insert)rfrgrhr!rrr┌mysql_query_insert═s
	z!ReadersWriters.mysql_query_insert)rfrcCstâ}|j|âS)zoGenerate a bespoke drop query.
        :param table: the MySQL table.
        :return: the MySQL query
        )r┌drop)rfr!rrr┌mysql_query_drop┌s	zReadersWriters.mysql_query_drop)┌namercCscx\td|dâjâ}|dkr7tdâdS|dkrQtdâdStd	âqWd
S)z▒Commandline question, which aims to get confirmation of the user.
        :param name: the subject name, which needs confirmation.
        :return: the user's response.
        zConfirm or reject z,
 >> Print 'y' to accept or 'n' to decline: ┌y┌ApprovedT┌n┌DeclinedFz!ERROR: Invalid command 'y' or 'n'N)┌input┌lower┌print)rs┌responserrr┌question_overwriteŃs

z!ReadersWriters.question_overwrite)rcCsćtjj|âséytj|ddâWnVtk
rü}z6tjtdj|j	dt
|j	dâââWYdd}~XnXdS)zuCreate directories in the specified path if do not exist.
        :param path: the directory path to a file.
        ┌exist_okTz* - Directory creation error: {0:d}:
 {1:s}rr@N)┌osrr┌makedirs┌OSError┌sys┌exit┌__name__┌formatr`rH)r┌errr┌create_directories˘s
z!ReadersWriters.create_directories),râ┌
__module__┌__qualname__┌staticmethodrH┌boolrr$r&r'r*r+r/r-r0r1r2r3rr8r;rr┌PandasDataFramerNrrRrTrVrXr.rr]r^rbrcrdre┌listrnrprrr|rćrrrrr,s╬$		$

7%4

!
=+!
!
0
r) ┌__doc__┌typingrrrrrrEr~rü┌ReadersWriters._CsvFiler┌ReadersWriters._MysqlCommandr┌ReadersWriters._MysqlConnectionr	┌ ReadersWriters._PickleSerialisedr
┌ReadersWriters._TextFiler┌ReadersWriters._MysqlQueriesrrő┌
__author__┌
__copyright__┌__credits__┌__license__┌__version__┌__maintainer__┌	__email__┌
__status__rrrrr┌<module>s((