structa.conversions
- structa.conversions.try_conversion(sample, conversion, threshold=0)[source]
Given a
Countersample of strings, call the specified conversion on each string returning the set of converted values.conversion must be a callable that accepts a single string parameter and returns the converted value. If the conversion fails it must raise a
ValueErrorexception.If threshold is specified (defaults to 0), it defines the number of “bad” conversions (which result in
ValueErrorbeing raised) that will be ignored. If threshold is exceeded, thenValueErrorwill be raised (or rather passed through from the underlying conversion). Likewise, if threshold is not exceeded, but zero conversions are successful thenValueErrorwill also be raised.
- structa.conversions.parse_bool(s, false='0', true='1')[source]
Convert the string s (stripped and lower-cased) to a bool, if it matches either the false string (defaults to ‘0’) or true (defaults to ‘1’). If it matches neither, raises a
ValueError.
- structa.conversions.parse_duration(s)[source]
Convert the string s to a
relativedelta. The string must consist of white-space and/or comma separated values which are a number followed by a suffix indicating duration. For example:>>> parse_duration('1s') relativedelta(seconds=+1) >>> parse_duration('5 minutes, 30 seconds') relativedelta(minutes=+5, seconds=+30) >>> parse_duration('1 year') relativedelta(years=+1)
Note that some suffixes like “m” can be ambiguous; using common abbreviations should avoid ambiguity:
>>> parse_duration('1 m') relativedelta(months=+1) >>> parse_duration('1 min') relativedelta(minutes=+1) >>> parse_duration('1 mon') relativedelta(months=+1)
The set of possible durations, and their recognized suffixes is as follows:
Microseconds: microseconds, microsecond, microsec, micros, micro, mseconds, msecond, msecs, msec, ms
Seconds: seconds, second, secs, sec, s
Minutes: minutes, minute, mins, min, mi
Hours: hours, hour, hrs, hr, h
Days: days, day, d
Weeks: weeks, week, wks, wk, w
Months: months, month, mons, mon, mths, mth, m
Years: years, year, yrs, yr, y
If conversion fails,
ValueErroris raised.
- structa.conversions.parse_duration_or_timestamp(s)[source]
Convert the string s to a
datetimeor arelativedelta. Duration conversion is attempted to and, if this fails, date-time conversion is attempted. AValueErroris raised if both conversions fail.