text
stringlengths
0
828
# RECURSIVELY CREATE MISSING DIRECTORIES
if not os.path.exists(os.path.dirname(filepath)):
os.makedirs(os.path.dirname(filepath))
stream = file(filepath, 'w')
yaml.dump(dataCopy, stream, default_flow_style=False)
stream.close()
self.log.debug('completed the ``yaml`` method')
return renderedData"
4783,"def mysql(
self,
tableName,
filepath=None,
createStatement=None
):
""""""*Render the dataset as a series of mysql insert statements*
**Key Arguments:**
- ``tableName`` -- the name of the mysql db table to assign the insert statements to.
- ``filepath`` -- path to the file to write the mysql inserts content to. Default *None*
createStatement
**Return:**
- ``renderedData`` -- the data rendered mysql insert statements (string format)
**Usage:**
.. code-block:: python
print dataSet.mysql(""testing_table"")
this output the following:
.. code-block:: plain
INSERT INTO `testing_table` (address,dateCreated,owner,pet) VALUES (""belfast, uk"" ,""2016-09-14T16:21:36"" ,""daisy"" ,""dog"") ON DUPLICATE KEY UPDATE address=""belfast, uk"", dateCreated=""2016-09-14T16:21:36"", owner=""daisy"", pet=""dog"" ;
INSERT INTO `testing_table` (address,dateCreated,owner,pet) VALUES (""the moon"" ,""2016-09-14T16:21:36"" ,""john"" ,""snake"") ON DUPLICATE KEY UPDATE address=""the moon"", dateCreated=""2016-09-14T16:21:36"", owner=""john"", pet=""snake"" ;
INSERT INTO `testing_table` (address,dateCreated,owner,pet) VALUES (""larne"" ,""2016-09-14T16:21:36"" ,""susan"" ,""crocodile"") ON DUPLICATE KEY UPDATE address=""larne"", dateCreated=""2016-09-14T16:21:36"", owner=""susan"", pet=""crocodile"" ;
To save this rendering to file use:
.. code-block:: python
dataSet.mysql(""testing_table"", ""/path/to/myfile.sql"")
""""""
self.log.debug('starting the ``csv`` method')
import re
if createStatement and ""create table if not exists"" not in createStatement.lower():
regex = re.compile(r'^\s*CREATE TABLE ', re.I | re.S)
createStatement = regex.sub(
""CREATE TABLE IF NOT EXISTS "", createStatement)
renderedData = self._list_of_dictionaries_to_mysql_inserts(
tableName=tableName,
createStatement=createStatement
)
if filepath and len(self.listOfDictionaries):
# RECURSIVELY CREATE MISSING DIRECTORIES
if not os.path.exists(os.path.dirname(filepath)):
os.makedirs(os.path.dirname(filepath))
writeFile = codecs.open(filepath, encoding='utf-8', mode='w')
writeFile.write(renderedData)
writeFile.close()
self.log.debug('completed the ``csv`` method')
return renderedData"
4784,"def _list_of_dictionaries_to_csv(
self,
csvType=""human""):
""""""Convert a python list of dictionaries to pretty csv output
**Key Arguments:**
- ``csvType`` -- human, machine or reST
**Return:**
- ``output`` -- the contents of a CSV file
""""""
self.log.debug(
'starting the ``_list_of_dictionaries_to_csv`` function')
if not len(self.listOfDictionaries):
return ""NO MATCH""
dataCopy = copy.deepcopy(self.listOfDictionaries)
tableColumnNames = dataCopy[0].keys()
columnWidths = []
columnWidths[:] = [len(tableColumnNames[i])
for i in range(len(tableColumnNames))]
output = io.BytesIO()
# setup csv styles
if csvType == ""machine"":
delimiter = "",""