8000 Update Replicate Workbook example to using DictReader which makes the… · chid/document-api-python@a36337f · GitHub
[go: up one dir, main page]

Skip to content

Commit a36337f

Browse files
committed
Update Replicate Workbook example to using DictReader which makes the code more readable. Also remove the unnecessary copy.copy call and import. Small refactor of parameter name for Workbook.save_as method to be more descriptive than 'value'. Manual testing on python2 and pyhon3 for example. Verified still works
1 parent c5e53ec commit a36337f

File tree

2 files changed

+10
-13
lines changed

2 files changed

+10
-13
lines changed
Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import csv # so we can work with our database list (in a CSV file)
2-
import copy # to make copies
32

43
############################################################
54
# Step 1) Use Workbook object from the Document API
@@ -16,13 +15,11 @@
1615
# create new .twb's with their settings
1716
############################################################
1817
with open('databases.csv') as csvfile:
19-
next(csvfile) # Skip the first line which is our CSV header row
20-
databases = csv.reader(csvfile, delimiter=',', quotechar='"')
18+
databases = csv.DictReader(csvfile, delimiter=' 8000 ,', quotechar='"')
2119
for row in databases:
22-
newWB = copy.copy(sourceWB)
23-
2420
# Set our unique values for this database
25-
newWB.datasources[0].connection.server = row[1] # Server
26-
newWB.datasources[0].connection.dbname = row[2] # Database
27-
newWB.datasources[0].connection.username = row[3] # User
28-
newWB.save_as(row[0] + ' - Superstore' + '.twb') # Save our newly created .twb with the new file name
21+
sourceWB.datasources[0].connection.server = row['Server']
22+
sourceWB.datasources[0].connection.dbname = row['Database']
23+
sourceWB.datasources[0].connection.username = row['User']
24+
# Save our newly created .twb with the new file name
25+
sourceWB.save_as(row['DBFriendlyName'] + ' - Superstore' + '.twb')

Document API/tableaudocumentapi/workbook.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,22 +78,22 @@ def save(self):
7878
# save the file
7979
self._workbookTree.write(self._filename)
8080

81-
def save_as(self, value):
81+
def save_as(self, new_filename):
8282
"""
8383
Save our file with the name provided.
8484
8585
Args:
86-
value: New name for the workbook file. String.
86+
new_filename: New name for the workbook file. String.
8787
8888
Returns:
8989
Nothing.
9090
9191
"""
9292

9393
# We have a valid type of input file
94-
if self._is_valid_file(value):
94+
if self._is_valid_file(new_filename):
9595
# save the file
96-
self._workbookTree.write(value)
96+
self._workbookTree.write(new_filename)
9797
else:
9898
print('Invalid file type. Must be .twb or .tds.')
9999
raise Exception()

0 commit comments

Comments
 (0)
0