8000 #72 Add description to the field object by graysonarts · Pull Request #73 · tableau/document-api-python · GitHub
[go: up one dir, main page]

Skip to content
< 8000 div class="gh-header-show ">

#72 Add description to the field object #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Examples/GetFields/show_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
if field.default_aggregation:
print(' the default aggregation is {}'.format(field.default_aggregation))
blank_line = True
if field.description:
print(' the description is {}'.format(field.description))

if blank_line:
print('')
Expand Down
16 changes: 16 additions & 0 deletions tableaudocumentapi/field.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import functools
import xml.etree.ElementTree as ET


_ATTRIBUTES = [
'id', # Name of the field as specified in the file, usually surrounded by [ ]
Expand All @@ -8,6 +10,7 @@
'type', # three possible values: quantitative, ordinal, or nominal
'alias', # Name of the field as displayed in Tableau if the default name isn't wanted
'calculation', # If this field is a calculated field, this will be the formula
'description', # If this field has a description, this will be the description (including formatting tags)
]

_METADATA_ATTRIBUTES = [
Expand Down Expand Up @@ -164,6 +167,11 @@ def default_aggregation(self):
""" The default type of aggregation on the field (e.g Sum, Avg)"""
return self._aggregation

@property
def description(self):
""" The contents of the <desc> tag on a field """
return self._description

@property
def worksheets(self):
return list(self._worksheets)
Expand All @@ -184,3 +192,11 @@ def _read_calculation(xmldata):
return None

return calc.attrib.get('formula', None)

@staticmethod
def _read_description(xmldata):
description = xmldata.find('.//desc')
if description is None:
return None

return u'{}'.format(ET.tostring(description, encoding='utf-8')) # This is necessary for py3 support
9 changes: 8 additions & 1 deletion test/assets/datasource_test.tds
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,14 @@
<column datatype='integer' name='[Number of Records]' role='measure' type='quantitative' user:auto-column='numrec'>
<calculation class='tableau' formula='1' />
</column>
<column caption='A' datatype='string' name='[a]' role='dimension' type='nominal' />
<column caption='A' datatype='string' name='[a]' role='dimension' type='nominal'>
<desc>
<formatted-text>
<run bold='true' fontsize='96'>A thing</run>
<run fontcolor='#686868'>&#10;Something will go here too, in a muted gray</run>
</formatted-text>
</desc>
</column>
<column caption='Today&apos;s Date' datatype='string' name='[Today&apos;s Date]' role='dimension' type='nominal' />
<column caption='X' datatype='integer' name='[x]' role='measure' type='ordinal' />
<column caption='Y' datatype='integer' name='[y]' role='measure' type='quantitative' />
Expand Down
5 changes: 5 additions & 0 deletions test/test_datasource.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ def test_datasource_field_datatype(self):
def test_datasource_field_role(self):
self.assertEqual(self.ds.fields['[x]'].role, 'measure')

def test_datasource_field_description(self):
actual = self.ds.fields['[a]'].description
self.assertIsNotNone(actual)
self.assertTrue(u'muted gray' in actual)


class DataSourceFieldsTWB(unittest.TestCase):

Expand Down
0