.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 89 of file unit_converter.py.

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

◆ description

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

Definition at line 29 of file unit_converter.py.

◆ name

str searx.plugins.unit_converter.name = ""

Definition at line 28 of file unit_converter.py.

◆ plugin_id

str searx.plugins.unit_converter.plugin_id = ""

Definition at line 31 of file unit_converter.py.

◆ preference_section

str searx.plugins.unit_converter.preference_section = ""

Definition at line 32 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 79 of file unit_converter.py.