本文整理汇总了Python中pyxform.builder.create_survey_from_xls函数的典型用法代码示例。如果您正苦于以下问题:Python create_survey_from_xls函数的具体用法?Python create_survey_from_xls怎么用?Python create_survey_from_xls使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了create_survey_from_xls函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: process_xlsform
def process_xlsform(filepath):
'''Genrates a pyxform survey object from a filepath or file object'''
survey = None
if isinstance(filepath, File):
survey = create_survey_from_xls(filepath)
else:
with open(filepath) as f:
survey = create_survey_from_xls(f)
return survey
示例2: save
def save(self, *args, **kwargs):
if self.xls:
survey = create_survey_from_xls(self.xls)
self.json = survey.to_json()
self.xml = survey.to_xml()
self._mark_start_time_boolean()
super(DataDictionary, self).save(*args, **kwargs)
示例3: test_child_record_parent_table_is_updated_when_sheet_is_renamed
def test_child_record_parent_table_is_updated_when_sheet_is_renamed(self):
survey = create_survey_from_xls(_logger_fixture_path(
'childrens_survey_with_a_very_long_name.xls'))
export_builder = ExportBuilder()
export_builder.set_survey(survey)
xls_file = NamedTemporaryFile(suffix='.xlsx')
filename = xls_file.name
export_builder.to_xls_export(filename, self.long_survey_data)
xls_file.seek(0)
wb = load_workbook(filename)
# get the children's sheet
ws1 = wb.get_sheet_by_name('childrens_survey_with_a_very_l1')
# parent_table is in cell K2
parent_table_name = ws1.cell('K2').value
expected_parent_table_name = 'childrens_survey_with_a_very_lo'
self.assertEqual(parent_table_name, expected_parent_table_name)
# get cartoons sheet
ws2 = wb.get_sheet_by_name('childrens_survey_with_a_very_l2')
parent_table_name = ws2.cell('G2').value
expected_parent_table_name = 'childrens_survey_with_a_very_l1'
self.assertEqual(parent_table_name, expected_parent_table_name)
xls_file.close()
示例4: test_for_loop
def test_for_loop(self):
path = utils.path_to_text_fixture('for_loop.xls')
survey = create_survey_from_xls(path)
self.maxDiff = None
try:
actual_xml = survey.to_xml()
except ValidationError:
self.fail("survey.to_xml() raised ValidationError.")
示例5: test_type_conversion
def test_type_conversion(self):
submission_1 = {
"_id": 579827,
"geolocation": "-1.2625482 36.7924794 0.0 21.0",
"_bamboo_dataset_id": "",
"meta/instanceID": "uuid:2a8129f5-3091-44e1-a579-bed2b07a12cf",
"name": "Smith",
"formhub/uuid": "633ec390e024411ba5ce634db7807e62",
"_submission_time": "2013-07-03T08:25:30",
"age": "107",
"_uuid": "2a8129f5-3091-44e1-a579-bed2b07a12cf",
"when": "2013-07-03",
"amount": "250.0",
"_geolocation": [
"-1.2625482",
"36.7924794"
],
"_xform_id_string": "test_data_types",
"_userform_id": "larryweya_test_data_types",
"_status": "submitted_via_web",
"precisely": "2013-07-03T15:24:00.000+03",
"really": "15:24:00.000+03"
}
submission_2 = {
"_id": 579828,
"_submission_time": "2013-07-03T08:26:10",
"_uuid": "5b4752eb-e13c-483e-87cb-e67ca6bb61e5",
"_bamboo_dataset_id": "",
"_xform_id_string": "test_data_types",
"_userform_id": "larryweya_test_data_types",
"_status": "submitted_via_web",
"meta/instanceID": "uuid:5b4752eb-e13c-483e-87cb-e67ca6bb61e5",
"formhub/uuid": "633ec390e024411ba5ce634db7807e62",
"amount": "",
}
survey = create_survey_from_xls(viewer_fixture_path(
'test_data_types/test_data_types.xls'))
export_builder = ExportBuilder()
export_builder.set_survey(survey)
# format submission 1 for export
survey_name = survey.name
indices = {survey_name: 0}
data = dict_to_joined_export(submission_1, 1, indices, survey_name)
new_row = export_builder.pre_process_row(data[survey_name],
export_builder.sections[0])
self.assertIsInstance(new_row['age'], int)
self.assertIsInstance(new_row['when'], datetime.date)
self.assertIsInstance(new_row['amount'], float)
# check missing values dont break and empty values return blank strings
indices = {survey_name: 0}
data = dict_to_joined_export(submission_2, 1, indices, survey_name)
new_row = export_builder.pre_process_row(data[survey_name],
export_builder.sections[0])
self.assertIsInstance(new_row['amount'], basestring)
self.assertEqual(new_row['amount'], '')
示例6: test_xls_convert_dates_before_1900
def test_xls_convert_dates_before_1900(self):
survey = create_survey_from_xls(viewer_fixture_path("test_data_types/test_data_types.xls"))
export_builder = ExportBuilder()
export_builder.set_survey(survey)
data = [{"name": "Abe", "when": "1899-07-03"}]
# create export file
temp_xls_file = NamedTemporaryFile(suffix=".xlsx")
export_builder.to_xls_export(temp_xls_file.name, data)
temp_xls_file.close()
示例7: save
def save(self, *args, **kwargs):
if self.xls:
# check if version is set
survey = self._check_version_set(create_survey_from_xls(self.xls))
self.json = survey.to_json()
self.xml = survey.to_xml()
self.version = survey.get('version')
self._mark_start_time_boolean()
set_uuid(self)
self._set_uuid_in_xml()
super(DataDictionary, self).save(*args, **kwargs)
示例8: handle
def handle(self, *args, **options):
try:
xls_filepath = options['xls_filepath']
except KeyError:
raise CommandError(_("You must provide the path to the xls file."))
# make sure path exists
if not os.path.exists(xls_filepath):
raise CommandError(
_("The xls file '%s' does not exist.") % xls_filepath)
try:
username = options['username']
except KeyError:
raise CommandError(
_("You must provide the username to publish the form to."))
# make sure user exists
try:
user = User.objects.get(username=username)
except User.DoesNotExist:
raise CommandError(_("The user '%s' does not exist.") % username)
# wasteful but we need to get the id_string beforehand
survey = create_survey_from_xls(xls_filepath)
# check if a form with this id_string exists for this user
form_already_exists = XForm.objects.filter(
user=user, id_string=survey.id_string).count() > 0
# id_string of form to replace, if any
id_string = None
if form_already_exists:
if 'replace' in options and options['replace']:
id_string = survey.id_string
self.stdout.write(_("Form already exist, replacing ..\n"))
else:
raise CommandError(
_("The form with id_string '%s' already exists, use the -r"
" option to replace it.") % survey.id_string)
else:
self.stdout.write(_("Form does NOT exist, publishing ..\n"))
try:
project_name = options['project_name']
project = Project.objects.get(name=project_name)
except (KeyError, Project.DoesNotExist):
project = get_user_default_project(user)
# publish
xls_file = django_file(xls_filepath, 'xls_file',
'application/vnd.ms-excel')
publish_xls_form(xls_file, user, project, id_string)
self.stdout.write(_("Done..\n"))
示例9: test_zipped_csv_export_works_with_unicode
def test_zipped_csv_export_works_with_unicode(self):
"""
cvs writer doesnt handle unicode we we have to encode to ascii
"""
survey = create_survey_from_xls(_logger_fixture_path(
'childrens_survey_unicode.xls'))
export_builder = ExportBuilder()
export_builder.set_survey(survey)
temp_zip_file = NamedTemporaryFile(suffix='.zip')
export_builder.to_zipped_csv(temp_zip_file.name, self.data_utf8)
temp_zip_file.seek(0)
temp_dir = tempfile.mkdtemp()
zip_file = zipfile.ZipFile(temp_zip_file.name, "r")
zip_file.extractall(temp_dir)
zip_file.close()
temp_zip_file.close()
# check that the children's file (which has the unicode header) exists
self.assertTrue(
os.path.exists(
os.path.join(temp_dir, "children.info.csv")))
# check file's contents
with open(os.path.join(temp_dir, "children.info.csv")) as csv_file:
reader = csv.reader(csv_file)
expected_headers = ['children.info/name.first',
'children.info/age',
'children.info/fav_colors',
u'children.info/fav_colors/red\u2019s',
u'children.info/fav_colors/blue\u2019s',
u'children.info/fav_colors/pink\u2019s',
'children.info/ice_creams',
'children.info/ice_creams/vanilla',
'children.info/ice_creams/strawberry',
'children.info/ice_creams/chocolate', '_id',
'_uuid', '_submission_time', '_index',
'_parent_table_name', '_parent_index',
u'_tags', '_notes', '_version',
'_duration', '_submitted_by']
rows = [row for row in reader]
actual_headers = [h.decode('utf-8') for h in rows[0]]
self.assertEqual(sorted(actual_headers), sorted(expected_headers))
data = dict(zip(rows[0], rows[1]))
self.assertEqual(
data[u'children.info/fav_colors/red\u2019s'.encode('utf-8')],
'True')
self.assertEqual(
data[u'children.info/fav_colors/blue\u2019s'.encode('utf-8')],
'True')
self.assertEqual(
data[u'children.info/fav_colors/pink\u2019s'.encode('utf-8')],
'False')
# check that red and blue are set to true
shutil.rmtree(temp_dir)
示例10: test_sav_special_char_columns
def test_sav_special_char_columns(self):
survey = create_survey_from_xls(
_logger_fixture_path('grains/grains.xls'))
export_builder = ExportBuilder()
export_builder.TRUNCATE_GROUP_TITLE = True
export_builder.set_survey(survey)
export_builder.INCLUDE_LABELS = True
export_builder.set_survey(survey)
for sec in export_builder.sections:
sav_options = export_builder._get_sav_options(sec['elements'])
sav_file = NamedTemporaryFile(suffix=".sav")
# No exception is raised
SavWriter(sav_file.name, **sav_options)
示例11: test_xls_convert_dates_before_1900
def test_xls_convert_dates_before_1900(self):
survey = create_survey_from_xls(viewer_fixture_path(
'test_data_types/test_data_types.xls'))
export_builder = ExportBuilder()
export_builder.set_survey(survey)
data = [
{
'name': 'Abe',
'when': '1899-07-03',
}
]
# create export file
temp_xls_file = NamedTemporaryFile(suffix='.xlsx')
export_builder.to_xls_export(temp_xls_file.name, data)
temp_xls_file.close()
示例12: test_xls_export_works_with_unicode
def test_xls_export_works_with_unicode(self):
survey = create_survey_from_xls(_logger_fixture_path("childrens_survey_unicode.xls"))
export_builder = ExportBuilder()
export_builder.set_survey(survey)
temp_xls_file = NamedTemporaryFile(suffix=".xlsx")
export_builder.to_xls_export(temp_xls_file.name, self.data_utf8)
temp_xls_file.seek(0)
# check that values for red\u2019s and blue\u2019s are set to true
wb = load_workbook(temp_xls_file.name)
children_sheet = wb.get_sheet_by_name("children.info")
data = dict([(r[0].value, r[1].value) for r in children_sheet.columns])
self.assertTrue(data[u"children.info/fav_colors/red\u2019s"])
self.assertTrue(data[u"children.info/fav_colors/blue\u2019s"])
self.assertFalse(data[u"children.info/fav_colors/pink\u2019s"])
temp_xls_file.close()
示例13: test_to_xls_export_generates_valid_sheet_names
def test_to_xls_export_generates_valid_sheet_names(self):
survey = create_survey_from_xls(_logger_fixture_path(
'childrens_survey_with_a_very_long_name.xls'))
export_builder = ExportBuilder()
export_builder.set_survey(survey)
xls_file = NamedTemporaryFile(suffix='.xls')
filename = xls_file.name
export_builder.to_xls_export(filename, self.data)
xls_file.seek(0)
wb = load_workbook(filename)
# check that we have childrens_survey, children, children_cartoons
# and children_cartoons_characters sheets
expected_sheet_names = ['childrens_survey_with_a_very_lo',
'childrens_survey_with_a_very_l1',
'childrens_survey_with_a_very_l2',
'childrens_survey_with_a_very_l3']
self.assertEqual(wb.get_sheet_names(), expected_sheet_names)
xls_file.close()
示例14: test_create_from_file_object
def test_create_from_file_object(self):
path = utils.path_to_text_fixture('yes_or_no_question.xls')
with open(path) as f:
create_survey_from_xls(f)
示例15: test_loop
def test_loop(self):
path = utils.path_to_text_fixture("another_loop.xls")
survey = create_survey_from_xls(path)
self.maxDiff = None
expected_dict = {
"name": "another_loop",
"id_string": "another_loop",
"sms_keyword": "another_loop",
"default_language": "default",
"title": "another_loop",
"type": "survey",
"children": [
{
"name": "loop_vehicle_types",
"type": "group",
"children": [
{
"label": {"English": "Car", "French": "Voiture"},
"name": "car",
"type": "group",
"children": [
{
"label": {
"English": "How many do you have?",
"French": "Combien avoir?",
},
"name": "total",
"type": "integer",
},
{
"bind": {"constraint": ". <= ../total"},
"label": {
"English": "How many are working?",
"French": "Combien marcher?",
},
"name": "working",
"type": "integer",
},
],
},
{
"label": {"English": "Motorcycle", "French": "Moto"},
"name": "motor_cycle",
"type": "group",
"children": [
{
"label": {
"English": "How many do you have?",
"French": "Combien avoir?",
},
"name": "total",
"type": "integer",
},
{
"bind": {"constraint": ". <= ../total"},
"label": {
"English": "How many are working?",
"French": "Combien marcher?",
},
"name": "working",
"type": "integer",
},
],
},
],
},
{
"children": [
{
"bind": {
"calculate": "concat('uuid:', uuid())",
"readonly": "true()",
},
"name": "instanceID",
"type": "calculate",
}
],
"control": {"bodyless": True},
"name": "meta",
"type": "group",
},
],
}
self.assertEquals(survey.to_json_dict(), expected_dict)