本文整理汇总了Python中safe.utilities.unicode.get_unicode函数的典型用法代码示例。如果您正苦于以下问题:Python get_unicode函数的具体用法?Python get_unicode怎么用?Python get_unicode使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了get_unicode函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: generate_insufficient_overlap_message
def generate_insufficient_overlap_message(
e,
exposure_geoextent,
exposure_layer,
hazard_geoextent,
hazard_layer,
viewport_geoextent):
"""Generate insufficient overlap message.
:param e: An exception.
:type e: Exception
:param exposure_geoextent: Extent of the exposure layer in the form
[xmin, ymin, xmax, ymax] in EPSG:4326.
:type exposure_geoextent: list
:param exposure_layer: Exposure layer.
:type exposure_layer: QgsMapLayer
:param hazard_geoextent: Extent of the hazard layer in the form
[xmin, ymin, xmax, ymax] in EPSG:4326.
:type hazard_geoextent: list
:param hazard_layer: Hazard layer instance.
:type hazard_layer: QgsMapLayer
:param viewport_geoextent: Viewport extents
as a list [xmin, ymin, xmax, ymax] in EPSG:4326.
:type viewport_geoextent: list
:return: An InaSAFE message object.
:rtype: safe.messaging.Message
"""
description = tr(
'There was insufficient overlap between the input layers and / or the '
'layers and the viewable area. Please select two overlapping layers '
'and zoom or pan to them or disable viewable area clipping in the '
'options dialog. Full details follow:')
message = m.Message(description)
text = m.Paragraph(tr('Failed to obtain the optimal extent given:'))
message.add(text)
analysis_inputs = m.BulletedList()
# We must use Qt string interpolators for tr to work properly
analysis_inputs.add(tr('Hazard: %s') % (hazard_layer.source()))
analysis_inputs.add(tr('Exposure: %s') % (exposure_layer.source()))
analysis_inputs.add(
tr('Viewable area Geo Extent: %s') % (
get_unicode(viewport_geoextent)))
analysis_inputs.add(
tr('Hazard Geo Extent: %s') % (
get_unicode(hazard_geoextent)))
analysis_inputs.add(
tr('Exposure Geo Extent: %s') % (
get_unicode(exposure_geoextent)))
analysis_inputs.add(
tr('Details: %s') % (
get_unicode(e)))
message.add(analysis_inputs)
return message
示例2: test_get_unicode
def test_get_unicode(self):
"""Test get_unicode function."""
text = 'Test á, é, í, ó, ú, ü, ñ, ¿'
unicode_repr = u'Test \xe1, \xe9, \xed, \xf3, \xfa, \xfc, \xf1, \xbf'
message = 'It should return %s, but it returned %s' % (
get_unicode(text), unicode_repr)
self.assertEqual(get_unicode(text), unicode_repr, message)
示例3: __init__
def __init__(self, message=None):
""""General constructor.
:param message: The optional error message.
:type message: str, unicode, MessageElement
"""""
if isinstance(message, unicode):
super(InaSAFEError, self).__init__(get_string(message))
self.message = message
elif isinstance(message, str):
super(InaSAFEError, self).__init__(message)
self.message = get_unicode(message)
elif isinstance(message, MessageElement):
super(InaSAFEError, self).__init__(message.to_text())
self.message = get_unicode(message.to_text())
elif message is None:
pass
elif isinstance(message, BaseException):
super(InaSAFEError, self).__init__(unicode(message))
self.message = unicode(message)
# This shouldn't happen...
else:
raise TypeError
示例4: test_update_keywords
def test_update_keywords(self):
"""Test append file keywords with update_keywords method."""
self.maxDiff = None
layer = clone_raster_layer(
name='tsunami_wgs84',
extension='.tif',
include_keywords=True,
source_directory=standard_data_path('hazard'))
new_keywords = {
'hazard_category': 'multiple_event'
}
self.keyword_io.update_keywords(layer, new_keywords)
keywords = self.keyword_io.read_keywords(layer)
expected_keywords = {
'hazard_category': 'multiple_event',
'title': 'Tsunami',
'hazard': 'tsunami',
'continuous_hazard_unit': 'metres',
'layer_geometry': 'raster',
'layer_purpose': 'hazard',
'layer_mode': 'continuous',
'keyword_version': inasafe_keyword_version
}
expected_keywords = {
k: get_unicode(v) for k, v in expected_keywords.iteritems()
}
self.assertDictEqual(keywords, expected_keywords)
示例5: html_to_file
def html_to_file(html, file_path=None, open_browser=False):
"""Save the html to an html file adapting the paths to the filesystem.
if a file_path is passed, it is used, if not a unique_filename is
generated.
:param html: the html for the output file.
:type html: str
:param file_path: the path for the html output file.
:type file_path: str
:param open_browser: if true open the generated html in an external browser
:type open_browser: bool
"""
if file_path is None:
file_path = unique_filename(suffix='.html')
# Ensure html is in unicode for codecs module
html = get_unicode(html)
with codecs.open(file_path, 'w', encoding='utf-8') as f:
f.write(html)
if open_browser:
open_in_browser(file_path)
示例6: test_str_unicode_str
def test_str_unicode_str(self):
"""Test if str(unicode(str)) works correctly."""
text = 'Test á, é, í, ó, ú, ü, ñ, ¿'.encode('utf-8')
unicode_repr = get_unicode(text)
str_repr = get_string(unicode_repr)
message = 'It should return %s, but it returned %s' % (text, str_repr)
self.assertEqual(text, str_repr, message)
示例7: _keywords_to_string
def _keywords_to_string(keywords, sublayer=None):
"""Create a string from a keywords dict.
Args:
* keywords: A required dictionary containing the keywords to stringify.
* sublayer: str optional group marker for a sub layer.
Returns:
str: a String containing the rendered keywords list
Raises:
Any exceptions are propogated.
.. note: Only simple keyword dicts should be passed here, not multilayer
dicts.
For example you pass a dict like this::
{'datatype': 'osm',
'category': 'exposure',
'title': 'buildings_osm_4326',
'subcategory': 'building',
'purpose': 'dki'}
and the following string would be returned:
datatype: osm
category: exposure
title: buildings_osm_4326
subcategory: building
purpose: dki
If sublayer is provided e.g. _keywords_to_string(keywords, sublayer='foo'),
the following:
[foo]
datatype: osm
category: exposure
title: buildings_osm_4326
subcategory: building
purpose: dki
"""
# Write
result = get_unicode("")
if sublayer is not None:
result = "[%s]\n" % sublayer
for k, value in keywords.items():
# Create key
msg = "Key in keywords dictionary must be a string. " "I got %s with type %s" % (k, str(type(k))[1:-1])
verify(isinstance(k, basestring), msg)
key = k
msg = 'Key in keywords dictionary must not contain the ":" ' 'character. I got "%s"' % key
verify(":" not in key, msg)
# Store
result += "%s: %s\n" % (key, value)
return result
示例8: toNewlineFreeString
def toNewlineFreeString(self):
"""Return a string representation of the table which contains no
newlines.
.. note:: any preformatted <pre> blocks will be adversely affected by
this.
"""
return get_unicode(self.__str__().replace('\n', ''))
示例9: add_list_entry
def add_list_entry(self, key, value):
"""Add an item to the keywords list given its key/value.
The key and value must both be valid, non empty strings
or an InvalidKVPError will be raised.
If an entry with the same key exists, it's value will be
replaced with value.
It will add the current key/value pair to the list if it is not
already present. The kvp will also be stored in the data of the
listwidgetitem as a simple string delimited with a bar ('|').
:param key: The key part of the key value pair (kvp).
:type key: str, unicode
:param value: Value part of the key value pair (kvp).
:type value: str, unicode
"""
if key is None or key == '':
return
# make sure that both key and value is unicode
key = get_unicode(key)
value = get_unicode(value)
message = ''
if ':' in key:
key = key.replace(':', '.')
message = self.tr('Colons are not allowed, replaced with "."')
if ':' in value:
value = value.replace(':', '.')
message = self.tr('Colons are not allowed, replaced with "."')
if message == '':
self.lblMessage.setText('')
self.lblMessage.hide()
else:
self.lblMessage.setText(message)
self.lblMessage.show()
item = QtGui.QListWidgetItem(key + ':' + value)
# We are going to replace, so remove it if it exists already
self.remove_item_by_key(key)
data = key + '|' + value
item.setData(QtCore.Qt.UserRole, data)
self.lstKeywords.insertItem(0, item)
示例10: set_widgets
def set_widgets(self):
"""Set widgets on the Source tab."""
# Just set values based on existing keywords
source = self.parent.get_existing_keyword('source')
if source or source == 0:
self.leSource.setText(get_unicode(source))
else:
self.leSource.clear()
source_scale = self.parent.get_existing_keyword('scale')
if source_scale or source_scale == 0:
self.leSource_scale.setText(get_unicode(source_scale))
else:
self.leSource_scale.clear()
source_date = self.parent.get_existing_keyword('date')
if source_date:
self.ckbSource_date.setChecked(True)
self.dtSource_date.setDateTime(
QDateTime.fromString(get_unicode(source_date),
'yyyy-MM-dd HH:mm:ss'))
else:
self.ckbSource_date.setChecked(False)
self.dtSource_date.clear()
source_url = self.parent.get_existing_keyword('url')
try:
source_url = source_url.toString()
except AttributeError:
pass
if source_url or source_url == 0:
self.leSource_url.setText(get_unicode(source_url))
else:
self.leSource_url.clear()
source_license = self.parent.get_existing_keyword('license')
if source_license or source_license == 0:
self.leSource_license.setText(get_unicode(source_license))
else:
self.leSource_license.clear()
示例11: __init__
def __init__(self, text, **kwargs):
"""Creates a strong Text object
:param text: Text to add to the message,
:type text: str
We pass the kwargs on to the base class so an exception is raised
if invalid keywords were passed. See:
http://stackoverflow.com/questions/13124961/
how-to-pass-arguments-efficiently-kwargs-in-python
"""
super(PlainText, self).__init__(**kwargs)
self.text = get_unicode(text)
示例12: tr
def tr(text):
"""We define a tr() alias here since the utilities implementation below
is not a class and does not inherit from QObject.
.. note:: see http://tinyurl.com/pyqt-differences
:param text: String to be translated
:type text: str, unicode
:returns: Translated version of the given string if available, otherwise
the original string.
:rtype: str, unicode
"""
# Ensure it's in unicode
text = get_unicode(text)
# noinspection PyCallByClass,PyTypeChecker,PyArgumentList
return QCoreApplication.translate('@default', text)
示例13: tr
def tr(text, context='@default'):
"""We define a tr() alias here since the utilities implementation below
is not a class and does not inherit from QObject.
.. note:: see http://tinyurl.com/pyqt-differences
:param text: String to be translated
:type text: str, unicode
:param context: A context for the translation. Since a same can be
translated to different text depends on the context.
:type context: str
:returns: Translated version of the given string if available, otherwise
the original string.
:rtype: str, unicode
"""
# Ensure it's in unicode
text = get_unicode(text)
# noinspection PyCallByClass,PyTypeChecker,PyArgumentList
translated_text = QCoreApplication.translate(context, text)
# Check if there is missing container. If so, return the original text.
# See #3164
if text.count('%') == translated_text.count('%'):
return translated_text
else:
content = (
'There is a problem in the translation text.\n'
'The original text: "%s".\n'
'The translation: "%s".\n'
'The number of %% character does not match (%s and %s).'
'Please check the translation in transifex for %s.' % (
text,
translated_text,
text.count('%'),
translated_text.count('%s'),
locale()
))
LOGGER.warning(content)
return text
示例14: get_keywords
def get_keywords(self):
"""Obtain the state of the dialog as a keywords dict.
:returns: Keywords reflecting the state of the dialog.
:rtype: dict
"""
keywords = {}
inasafe_fields = {}
keywords['layer_geometry'] = self.get_layer_geometry_key()
if self.step_kw_purpose.selected_purpose():
keywords['layer_purpose'] = self.step_kw_purpose.\
selected_purpose()['key']
if self.step_kw_subcategory.selected_subcategory():
key = self.step_kw_purpose.selected_purpose()['key']
keywords[key] = self.step_kw_subcategory.\
selected_subcategory()['key']
if self.get_layer_geometry_key() == layer_geometry_raster['key']:
if self.step_kw_band_selector.selected_band():
keywords['active_band'] = self.step_kw_band_selector.\
selected_band()
if keywords['layer_purpose'] == layer_purpose_hazard['key']:
if self.step_kw_hazard_category.selected_hazard_category():
keywords['hazard_category'] \
= self.step_kw_hazard_category.\
selected_hazard_category()['key']
if self.step_kw_layermode.selected_layermode():
keywords['layer_mode'] = self.step_kw_layermode.\
selected_layermode()['key']
if self.step_kw_unit.selected_unit():
if self.step_kw_purpose.selected_purpose() == layer_purpose_hazard:
key = continuous_hazard_unit['key']
else:
key = exposure_unit['key']
keywords[key] = self.step_kw_unit.selected_unit()['key']
if self.step_kw_field.selected_fields():
field_key = self.field_keyword_for_the_layer()
inasafe_fields[field_key] = self.step_kw_field.selected_fields()
if self.step_kw_classification.selected_classification():
keywords['classification'] = self.step_kw_classification.\
selected_classification()['key']
if keywords['layer_purpose'] == layer_purpose_hazard['key']:
multi_classifications = self.step_kw_multi_classifications.\
get_current_state()
value_maps = multi_classifications.get('value_maps')
if value_maps is not None:
keywords['value_maps'] = value_maps
thresholds = multi_classifications.get('thresholds')
if thresholds is not None:
keywords['thresholds'] = thresholds
else:
if self.step_kw_layermode.selected_layermode():
layer_mode = self.step_kw_layermode.selected_layermode()
if layer_mode == layer_mode_continuous:
thresholds = self.step_kw_threshold.get_threshold()
if thresholds:
keywords['thresholds'] = thresholds
elif layer_mode == layer_mode_classified:
value_map = self.step_kw_classify.selected_mapping()
if value_map:
keywords['value_map'] = value_map
if self.step_kw_source.leSource.text():
keywords['source'] = get_unicode(
self.step_kw_source.leSource.text())
if self.step_kw_source.leSource_url.text():
keywords['url'] = get_unicode(
self.step_kw_source.leSource_url.text())
if self.step_kw_source.leSource_scale.text():
keywords['scale'] = get_unicode(
self.step_kw_source.leSource_scale.text())
if self.step_kw_source.ckbSource_date.isChecked():
keywords['date'] = self.step_kw_source.dtSource_date.dateTime()
if self.step_kw_source.leSource_license.text():
keywords['license'] = get_unicode(
self.step_kw_source.leSource_license.text())
if self.step_kw_title.leTitle.text():
keywords['title'] = get_unicode(self.step_kw_title.leTitle.text())
inasafe_fields.update(self.step_kw_inasafe_fields.get_inasafe_fields())
inasafe_fields.update(
self.step_kw_default_inasafe_fields.get_inasafe_fields())
inasafe_fields.update(
self.step_kw_fields_mapping.get_field_mapping()['fields'])
if inasafe_fields:
keywords['inasafe_fields'] = inasafe_fields
inasafe_default_values = {}
if keywords['layer_geometry'] == layer_geometry_raster['key']:
pass
# Notes(IS): Skipped assigning raster inasafe default value for
# now.
# inasafe_default_values = self.\
# step_kw_inasafe_raster_default_values.\
# get_inasafe_default_values()
else:
inasafe_default_values.update(
self.step_kw_default_inasafe_fields.get_inasafe_default_values(
))
#.........这里部分代码省略.........
示例15: run
def run(self, layers=None):
"""Risk plugin for flood population evacuation.
:param layers: List of layers expected to contain
* hazard_layer : Vector polygon layer of flood depth
* exposure_layer : Raster layer of population data on the same grid
as hazard_layer
Counts number of people exposed to areas identified as flood prone
:returns: Map of population exposed to flooding Table with number of
people evacuated and supplies required.
:rtype: tuple
"""
self.validate()
self.prepare(layers)
# Get the IF parameters
affected_field = self.parameters['affected_field']
affected_value = self.parameters['affected_value']
evacuation_percentage = self.parameters['evacuation_percentage']
# Identify hazard and exposure layers
hazard_layer = self.hazard
exposure_layer = self.exposure
# Check that hazard is polygon type
if not hazard_layer.is_polygon_data:
message = (
'Input hazard must be a polygon layer. I got %s with layer '
'type %s' % (
hazard_layer.get_name(),
hazard_layer.get_geometry_name()))
raise Exception(message)
nan_warning = False
if has_no_data(exposure_layer.get_data(nan=True)):
nan_warning = True
# Check that affected field exists in hazard layer
if affected_field in hazard_layer.get_attribute_names():
self.use_affected_field = True
# Run interpolation function for polygon2raster
interpolated_layer, covered_exposure = \
assign_hazard_values_to_exposure_data(
hazard_layer,
exposure_layer,
attribute_name=self.target_field)
# Data for manipulating the covered_exposure layer
new_covered_exposure_data = covered_exposure.get_data()
covered_exposure_top_left = numpy.array([
covered_exposure.get_geotransform()[0],
covered_exposure.get_geotransform()[3]])
covered_exposure_dimension = numpy.array([
covered_exposure.get_geotransform()[1],
covered_exposure.get_geotransform()[5]])
# Count affected population per polygon, per category and total
total_affected_population = 0
for attr in interpolated_layer.get_data():
affected = False
if self.use_affected_field:
row_affected_value = attr[affected_field]
if row_affected_value is not None:
if isinstance(row_affected_value, Number):
type_func = type(row_affected_value)
affected = row_affected_value == type_func(
affected_value)
else:
affected =\
get_unicode(affected_value).lower() == \
get_unicode(row_affected_value).lower()
else:
# assume that every polygon is affected (see #816)
affected = True
if affected:
# Get population at this location
population = attr[self.target_field]
if not numpy.isnan(population):
population = float(population)
total_affected_population += population
else:
# If it's not affected, set the value of the impact layer to 0
grid_point = attr['grid_point']
index = numpy.floor(
(grid_point - covered_exposure_top_left) / (
covered_exposure_dimension)).astype(int)
new_covered_exposure_data[index[1]][index[0]] = 0
# Estimate number of people in need of evacuation
evacuated = (
total_affected_population * evacuation_percentage / 100.0)
total_population = int(
numpy.nansum(exposure_layer.get_data(scaling=False)))
#.........这里部分代码省略.........