.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
searx.plugins.unit_converter Namespace Reference

Classes

class  SXNGPlugin

Functions

str|None _parse_text_and_convert (from_query, to_query)

Variables

str name = ""
 description = gettext("")
str plugin_id = ""
str preference_section = ""
list CONVERT_KEYWORDS = ["in", "to", "as"]
str RE_MEASURE

Detailed Description

A plugin for converting measured values from one unit to another unit (a
unit converter).

The plugin looks up the symbols (given in the query term) in a list of
converters, each converter is one item in the list (compare
:py:obj:`ADDITIONAL_UNITS`).  If the symbols are ambiguous, the matching units
of measurement are evaluated.  The weighting in the evaluation results from the
sorting of the :py:obj:`list of unit converters<symbol_to_si>`.

Function Documentation

◆ _parse_text_and_convert()

str | None searx.plugins.unit_converter._parse_text_and_convert ( from_query,
to_query )
protected

Definition at line 88 of file unit_converter.py.

88def _parse_text_and_convert(from_query, to_query) -> str | None:
89
90 # pylint: disable=too-many-branches, too-many-locals
91
92 if not (from_query and to_query):
93 return None
94
95 measured = re.match(RE_MEASURE, from_query, re.VERBOSE)
96 if not (measured and measured.group('number'), measured.group('unit')):
97 return None
98
99 # Symbols are not unique, if there are several hits for the from-unit, then
100 # the correct one must be determined by comparing it with the to-unit
101 # https://github.com/searxng/searxng/pull/3378#issuecomment-2080974863
102
103 # first: collecting possible units
104
105 source_list, target_list = [], []
106
107 for symbol, si_name, from_si, to_si, orig_symbol in symbol_to_si():
108
109 if symbol == measured.group('unit'):
110 source_list.append((si_name, to_si))
111 if symbol == to_query:
112 target_list.append((si_name, from_si, orig_symbol))
113
114 if not (source_list and target_list):
115 return None
116
117 source_to_si = target_from_si = target_symbol = None
118
119 # second: find the right unit by comparing list of from-units with list of to-units
120
121 for source in source_list:
122 for target in target_list:
123 if source[0] == target[0]: # compare si_name
124 source_to_si = source[1]
125 target_from_si = target[1]
126 target_symbol = target[2]
127
128 if not (source_to_si and target_from_si):
129 return None
130
131 _locale = get_locale() or 'en_US'
132
133 value = measured.group('sign') + measured.group('number') + (measured.group('E') or '')
134 value = babel.numbers.parse_decimal(value, locale=_locale)
135
136 # convert value to SI unit
137
138 if isinstance(source_to_si, (float, int)):
139 value = float(value) * source_to_si
140 else:
141 value = source_to_si(float(value))
142
143 # convert value from SI unit to target unit
144
145 if isinstance(target_from_si, (float, int)):
146 value = float(value) * target_from_si
147 else:
148 value = target_from_si(float(value))
149
150 if measured.group('E'):
151 # when incoming notation is scientific, outgoing notation is scientific
152 result = babel.numbers.format_scientific(value, locale=_locale)
153 else:
154 result = babel.numbers.format_decimal(value, locale=_locale, format='#,##0.##########;-#')
155
156 return f'{result} {target_symbol}'

Referenced by searx.plugins.unit_converter.SXNGPlugin.post_search().

Here is the caller graph for this function:

Variable Documentation

◆ CONVERT_KEYWORDS

list searx.plugins.unit_converter.CONVERT_KEYWORDS = ["in", "to", "as"]

Definition at line 33 of file unit_converter.py.

◆ description

searx.plugins.unit_converter.description = gettext("")

Definition at line 28 of file unit_converter.py.

◆ name

str searx.plugins.unit_converter.name = ""

Definition at line 27 of file unit_converter.py.

◆ plugin_id

str searx.plugins.unit_converter.plugin_id = ""

Definition at line 30 of file unit_converter.py.

◆ preference_section

str searx.plugins.unit_converter.preference_section = ""

Definition at line 31 of file unit_converter.py.

◆ RE_MEASURE

str searx.plugins.unit_converter.RE_MEASURE
Initial value:
1= r'''
2(?P<sign>[-+]?) # +/- or nothing for positive
3(\s*) # separator: white space or nothing
4(?P<number>[\d\.,]*) # number: 1,000.00 (en) or 1.000,00 (de)
5(?P<E>[eE][-+]?\d+)? # scientific notation: e(+/-)2 (*10^2)
6(\s*) # separator: white space or nothing
7(?P<unit>\S+) # unit of measure
8'''

Definition at line 78 of file unit_converter.py.