Value Converters
convert_to_bool_string
Turns a value into a bool string, suitable for an XML. It is case-insensitive, meaning that the words can also be capitalised.
Accepted values
false
,0
,0.0
,no
,non
,nein
->False
true
,1
,1.0
,yes
,oui
,ja
,sì
->True
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
Any
|
value to transform |
required |
Returns:
Type | Description |
---|---|
bool
|
|
Examples:
result = xmllib.convert_to_bool_string(1)
# result == True
result = xmllib.convert_to_bool_string("nein")
# result == False
result = xmllib.convert_to_bool_string(None)
# raises XmllibInputError
Source code in dsp/dsp-tools/src/dsp_tools/xmllib/value_converters.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
|
replace_newlines_with_tags
Converts the newlines in a string to XML tags.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text
|
str
|
string to convert |
required |
converter_option
|
NewlineReplacement
|
specifies what tag to use instead of the newline |
required |
Returns:
Type | Description |
---|---|
str
|
String with replaced values |
Examples:
result = xmllib.replace_newlines_with_tags(
"Start\nEnd", xmllib.NewlineReplacement.NONE
)
# result == "Start\nEnd"
result = xmllib.replace_newlines_with_tags(
"Start\nEnd", xmllib.NewlineReplacement.LINEBREAK
)
# result == "Start<br/>End"
result = xmllib.replace_newlines_with_tags(
"Start\n\nEnd", xmllib.NewlineReplacement.PARAGRAPH
)
# result == "<p>Start</p><p>End</p>"
Source code in dsp/dsp-tools/src/dsp_tools/xmllib/value_converters.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
|
replace_newlines_with_paragraph_tags
Replace Start\nEnd
with <p>Start</p><p>End</p>
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text
|
str
|
string to be formatted |
required |
Returns:
Type | Description |
---|---|
str
|
Formatted string with paragraph tags |
Examples:
result = xmllib.replace_newlines_with_paragraph_tags("Start\nEnd")
# result == "<p>Start</p><p>End</p>"
# multiple consecutive newlines will be treated as one newline
result = xmllib.replace_newlines_with_paragraph_tags("Start\n\nEnd")
# result == "<p>Start</p><p>End</p>"
Source code in dsp/dsp-tools/src/dsp_tools/xmllib/value_converters.py
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
|
replace_newlines_with_br_tags
Replaces \n
with <br/>
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text
|
str
|
string to be formatted |
required |
Returns:
Type | Description |
---|---|
str
|
Formatted string with break-line tags |
Examples:
result = xmllib.replace_newlines_with_br_tags("Start\nEnd")
# result == "Start<br/>End"
# multiple consecutive newlines will be converted into multiple break-lines
result = xmllib.replace_newlines_with_br_tags("Start\n\nEnd")
# result == "Start<br/><br/>End"
Source code in dsp/dsp-tools/src/dsp_tools/xmllib/value_converters.py
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
|
reformat_date
Reformats a date string into the DSP format.
- If the input cannot be reformatted according to the configuration, or if the result is not a valid DSP date, a warning is emitted and the original input is returned.
- If the input is empty, a warning is emitted and an empty string is returned.
- If the input is already a correctly formatted DSP-date, the original input is returned.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
date
|
str | int
|
date string to be reformatted |
required |
date_precision_separator
|
str | None
|
the separation between the day, month and year |
required |
date_range_separator
|
str | None
|
the separation between two dates |
required |
date_format
|
DateFormat
|
the format of the date, see |
required |
calendar
|
Calendar
|
the calendar of the date, see |
GREGORIAN
|
era
|
Era | None
|
the era of the date, see |
CE
|
resource_id
|
str | None
|
the ID of the associated resource, this is to improve the error message |
None
|
Returns:
Type | Description |
---|---|
str
|
A reformatted date or the original input if the reformatted result is not a valid DSP date |
Examples:
# default configuration, starting with the day
result = xmllib.reformat_date(
date="1.11.2000",
date_precision_separator=".",
date_range_separator=None,
date_format=xmllib.DateFormat.DD_MM_YYYY
)
# result == "GREGORIAN:CE:2000-11-1:CE:2000-11-1"
# default configuration, but starting with the year
result = xmllib.reformat_date(
date="2000.11.1",
date_precision_separator=".",
date_range_separator=None,
date_format=xmllib.DateFormat.YYYY_MM_DD,
)
# result == "GREGORIAN:CE:2000-11-1:CE:2000-11-1"
# with a date range
result = xmllib.reformat_date(
date="1.11.2000-2001",
date_precision_separator=".",
date_range_separator="-",
date_format=xmllib.DateFormat.DD_MM_YYYY,
)
# result == "GREGORIAN:CE:2000-11-1:CE:2001"
# islamic calendar, where eras are not allowed
result = xmllib.reformat_date(
date="1.11.2000",
date_precision_separator=".",
date_range_separator=None,
date_format=xmllib.DateFormat.DD_MM_YYYY,
calendar=xmllib.Calendar.ISLAMIC,
era=None
)
# result == "ISLAMIC:2000-11-1:2000-11-1"
# with a different era
result = xmllib.reformat_date(
date="1.11.2000",
date_precision_separator=".",
date_range_separator="-",
date_format=xmllib.DateFormat.DD_MM_YYYY,
era=xmllib.Era.AD
)
# result == "GREGORIAN:AD:2000-11-1:AD:2000-11-1"
# reformatted date, no precision in the date string is required
result = xmllib.reformat_date(
date="2000",
date_precision_separator=".",
date_range_separator="-",
date_format=xmllib.DateFormat.DD_MM_YYYY,
)
# result == "GREGORIAN:CE:2000:CE:2000"
# already correctly formatted date
result = xmllib.reformat_date(
date="GREGORIAN:CE:2000:CE:2000",
date_precision_separator=".",
date_range_separator="-",
date_format=xmllib.DateFormat.DD_MM_YYYY,
)
# result == "GREGORIAN:CE:2000:CE:2000"
# invalid input: a warning is emitted and the original input is returned
result = xmllib.reformat_date(
date="not-a-date",
date_precision_separator=".",
date_range_separator="-",
date_format=xmllib.DateFormat.DD_MM_YYYY,
)
# WARNING is emitted
# result == "not-a-date"
Source code in dsp/dsp-tools/src/dsp_tools/xmllib/value_converters.py
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
|
find_dates_in_string
Checks if a string contains date values (single dates, or date ranges), and return all found dates as set of DSP-formatted strings. Returns an empty set if no date was found. See XML documentation for details.
Notes
- If no era or calendar is given, dates are interpreted in the Common Era and the Gregorian calendar.
- Standalone numbers from 000-2999, in 3/4-digit form, are interpreted as years CE.
- If a number (with any number of digits) is followed by CE, C.E., AD, A.D., it is interpreted as years CE.
- If a number (with any number of digits) is followed by BCE, BC, B.C., B.C.E., av. J.-C., it is interpreted as years BCE.
- Dates written with slashes are always interpreted in a European manner: 5/11/2021 is the 5th of November.
- In the European notation, 2-digit years are expanded to 4 digits, with the current year as watershed:
- 30.4.24 -> 30.04.2024
- 30.4.50 -> 30.04.1950
Currently supported date formats
- 0476-09-04 -> GREGORIAN:CE:0476-09-04:CE:0476-09-04
- 0476_09_04 -> GREGORIAN:CE:0476-09-04:CE:0476-09-04
- 30.4.2021 -> GREGORIAN:CE:2021-04-30:CE:2021-04-30
- 30.4.21 -> GREGORIAN:CE:2021-04-30:CE:2021-04-30
- 5/11/2021 -> GREGORIAN:CE:2021-11-05:CE:2021-11-05
- Jan 26, 1993 -> GREGORIAN:CE:1993-01-26:CE:1993-01-26
- 26 Jan 1993 -> GREGORIAN:CE:1993-01-26:CE:1993-01-26
- 26 January 1993 -> GREGORIAN:CE:1993-01-26:CE:1993-01-26
-
- Jan. 1993 -> GREGORIAN:CE:1993-01-26:CE:1993-01-26
-
- Januar 1993 -> GREGORIAN:CE:1993-01-26:CE:1993-01-26
- 28.2.-1.12.1515 -> GREGORIAN:CE:1515-02-28:CE:1515-12-01
- 25.-26.2.0800 -> GREGORIAN:CE:0800-02-25:CE:0800-02-26
- 1.9.2022-3.1.2024 -> GREGORIAN:CE:2022-09-01:CE:2024-01-03
- 1848 -> GREGORIAN:CE:1848:CE:1848
- 1849/1850 -> GREGORIAN:CE:1849:CE:1850
- 1849/50 -> GREGORIAN:CE:1849:CE:1850
- 1845-50 -> GREGORIAN:CE:1845:CE:1850
- 840-50 -> GREGORIAN:CE:840:CE:850
- 840-1 -> GREGORIAN:CE:840:CE:841
- 9 BC / 9 B.C. / 9 B.C.E. / 9 BCE -> GREGORIAN:BC:9:BC:9
- 20 BCE - 50 CE -> GREGORIAN:BC:20:CE:50
- 1000-900 av. J.-C. -> GREGORIAN:BC:1000:BC:900
- 45 av. J.-C. -> GREGORIAN:BC:45:BC:45
Parameters:
Name | Type | Description | Default |
---|---|---|---|
string
|
str
|
string to check |
required |
Returns:
Type | Description |
---|---|
set[str]
|
(possibly empty) set of DSP-formatted date strings |
Examples:
result = xmllib.find_dates_in_string("1849/1850")
# result == {"GREGORIAN:CE:1849:CE:1850"}
result = xmllib.find_dates_in_string("not a valid date")
# result == {}
result = xmllib.find_dates_in_string("first date: 2024. Second: 2025.")
# result == {"GREGORIAN:CE:2024:CE:2024", "GREGORIAN:CE:2025:CE:2025"}
Source code in dsp/dsp-tools/src/dsp_tools/xmllib/value_converters.py
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 |
|