shillelagh.adapters.api.gsheets.parsing package¶
Submodules¶
shillelagh.adapters.api.gsheets.parsing.base module¶
Token for parsing date and time.
- exception shillelagh.adapters.api.gsheets.parsing.base.InvalidPattern[source]¶
Bases:
ExceptionRaised when the pattern can’t be consumed.
- exception shillelagh.adapters.api.gsheets.parsing.base.InvalidValue[source]¶
Bases:
ExceptionRaised when the value can’t be consumed.
- class shillelagh.adapters.api.gsheets.parsing.base.LITERAL(token: str)[source]¶
Bases:
TokenA literal.
- class shillelagh.adapters.api.gsheets.parsing.base.Token(token: str)[source]¶
Bases:
Generic[Valid]A token.
- classmethod consume(pattern: str, history: list[Token]) tuple[Token, str][source]¶
Consume the pattern, returning the token and the remaining pattern.
- classmethod match(pattern: str, history: list[Token]) bool[source]¶
Check if token handles the beginning of the pattern.
shillelagh.adapters.api.gsheets.parsing.date module¶
Parse and format Google Sheet date/time patterns.
https://developers.google.com/sheets/api/guides/formats?hl=en#date_and_time_format_patterns
- class shillelagh.adapters.api.gsheets.parsing.date.AMPM(token: str)[source]¶
Bases:
APAs above, but displays “AM” or “PM” instead and is always capitalized.
- format(value: datetime | time, tokens: list[Token]) str[source]¶
Format the value using the pattern.
- class shillelagh.adapters.api.gsheets.parsing.date.AP(token: str)[source]¶
Bases:
TokenDisplays “a” for AM, and “p” for PM. Also changes hours to 12-hour format. If the token letter is capitalized, the output is as well.
- format(value: datetime | time, tokens: list[Token]) str[source]¶
Format the value using the pattern.
- class shillelagh.adapters.api.gsheets.parsing.date.D(token: str)[source]¶
Bases:
TokenDay of the month, no leading 0 for numbers less than 10.
- format(value: date | datetime, tokens: list[Token]) str[source]¶
Format the value using the pattern.
- class shillelagh.adapters.api.gsheets.parsing.date.DD(token: str)[source]¶
Bases:
DDay of the month, with a leading 0 for numbers less than 10.
- format(value: date | datetime, tokens: list[Token]) str[source]¶
Format the value using the pattern.
- class shillelagh.adapters.api.gsheets.parsing.date.DDD(token: str)[source]¶
Bases:
DDay of the week, three letter abbreviation (e.g., “Mon”).
- format(value: date | datetime, tokens: list[Token]) str[source]¶
Format the value using the pattern.
- class shillelagh.adapters.api.gsheets.parsing.date.DDDDPlus(token: str)[source]¶
Bases:
DDay of the week, full name.
- format(value: date | datetime, tokens: list[Token]) str[source]¶
Format the value using the pattern.
- class shillelagh.adapters.api.gsheets.parsing.date.DurationToken(token: str)[source]¶
Bases:
TokenA token for durations.
Durations are special because often only the first token is annotated. For example:
[h]:mm:ss
[ss].000
But apparently it is valid to annotate subsequent tokens:
[hh]:[mm]:[ss].000
Who knows?
Because of this, their regexes are dynamic, and depend on the token history.
- classmethod consume(pattern: str, history: list[Token]) tuple[Token, str][source]¶
Consume the pattern, returning the token and the remaining pattern.
- is_duration = True¶
- class shillelagh.adapters.api.gsheets.parsing.date.H(token: str)[source]¶
Bases:
TokenHour of the day.
Switches between 12 and 24 hour format depending on whether an am/pm indicator is present in the string.
- format(value: datetime | time, tokens: list[Token]) str[source]¶
Format the value using the pattern.
- class shillelagh.adapters.api.gsheets.parsing.date.HHPlus(token: str)[source]¶
Bases:
HSame as previous, but with a leading 0 for 1-9.
- format(value: datetime | time, tokens: list[Token]) str[source]¶
Format the value using the pattern.
- class shillelagh.adapters.api.gsheets.parsing.date.HPlusDuration(token: str)[source]¶
Bases:
DurationTokenNumber of elapsed hours in a time duration. Number of letters indicates minimum number of digits (adds leading 0s).
- class shillelagh.adapters.api.gsheets.parsing.date.M(token: str)[source]¶
Bases:
TokenIf the previous non-literal token was hours or the subsequent one is seconds, then it represents minutes in the hour (no leading 0). Otherwise, it represents the month of the year as a number (no leading 0).
- format(value: date | datetime | time, tokens: list[Token]) str[source]¶
Format the value using the pattern.
- class shillelagh.adapters.api.gsheets.parsing.date.MM(token: str)[source]¶
Bases:
MAs above, but with a leading 0 for both cases.
- format(value: date | datetime | time, tokens: list[Token]) str[source]¶
Format the value using the pattern.
- class shillelagh.adapters.api.gsheets.parsing.date.MMM(token: str)[source]¶
Bases:
TokenThree letter month abbreviation (e.g., “Feb”).
- format(value: date | datetime | time, tokens: list[Token]) str[source]¶
Format the value using the pattern.
- class shillelagh.adapters.api.gsheets.parsing.date.MMMM(token: str)[source]¶
Bases:
MMMFull month name. mmmmmm+ also matches this.
- format(value: date | datetime | time, tokens: list[Token]) str[source]¶
Format the value using the pattern.
- class shillelagh.adapters.api.gsheets.parsing.date.MMMMM(token: str)[source]¶
Bases:
MMMFirst letter of the month (e.g., “J” for June).
- format(value: date | datetime | time, tokens: list[Token]) str[source]¶
Format the value using the pattern.
- class shillelagh.adapters.api.gsheets.parsing.date.MPlusDuration(token: str)[source]¶
Bases:
DurationTokenNumber of elapsed minutes in a time duration. Number of letters indicates minimum number of digits (adds leading 0s).
- class shillelagh.adapters.api.gsheets.parsing.date.Meridiem(value)[source]¶
Bases:
EnumRepresent ante or post meridiem.
- AM = 'AM'¶
- PM = 'PM'¶
- class shillelagh.adapters.api.gsheets.parsing.date.S(token: str)[source]¶
Bases:
TokenSeconds in the minute without a leading 0.
- format(value: datetime | time, tokens: list[Token]) str[source]¶
Format the value using the pattern.
- class shillelagh.adapters.api.gsheets.parsing.date.SPlusDuration(token: str)[source]¶
Bases:
DurationTokenNumber of elapsed seconds in a time duration. Number of letters indicates minimum number of digits (adds leading 0s).
- class shillelagh.adapters.api.gsheets.parsing.date.SS(token: str)[source]¶
Bases:
SSeconds in the minute with a leading 0.
- format(value: datetime | time, tokens: list[Token]) str[source]¶
Format the value using the pattern.
- class shillelagh.adapters.api.gsheets.parsing.date.YY(token: str)[source]¶
Bases:
Token2-digit year.
- format(value: date | datetime, tokens: list[Token]) str[source]¶
Format the value using the pattern.
- class shillelagh.adapters.api.gsheets.parsing.date.YYYY(token: str)[source]¶
Bases:
Token4-digit year.
- format(value: date | datetime, tokens: list[Token]) str[source]¶
Format the value using the pattern.
- class shillelagh.adapters.api.gsheets.parsing.date.ZERO(token: str)[source]¶
Bases:
TokenTenths of seconds. You can increase the precision to 2 digits with 00 or 3 digits (milliseconds) with 000.
- format(value: datetime | time | timedelta, tokens: list[Token]) str[source]¶
Format the value using the pattern.
- shillelagh.adapters.api.gsheets.parsing.date.format_date_time_pattern(value: DateTime, pattern: str) str[source]¶
Format a date/time related object to a given pattern.
See https://developers.google.com/sheets/api/guides/formats?hl=en.
- shillelagh.adapters.api.gsheets.parsing.date.infer_column_type(pattern: str) str[source]¶
Infer the correct date-related type.
GSheets returns
datetimeas the type for timestamps, but also for time of day and durations. We need to parse the pattern to figure out the exact type.This also handles a case where a timestamp (
1/2/24 14:41) with a proper pattern (M/D/YY h:mm) was being returned as typetimeofday.
shillelagh.adapters.api.gsheets.parsing.number module¶
Parse and format Google Sheet number formats.
https://developers.google.com/sheets/api/guides/formats#number_format_tokens
- class shillelagh.adapters.api.gsheets.parsing.number.AT(token: str)[source]¶
Bases:
TokenInserts the raw text for the cell, if the cell has text input.
Not compatible with any of the other special characters and won’t display for numeric values (which are displayed as general format).
- format(value: int | float | str, tokens: list[Token]) str[source]¶
Format the value using the pattern.
- class shillelagh.adapters.api.gsheets.parsing.number.COLOR(token: str)[source]¶
Bases:
TokenCauses any value that is rendered by this sub-format to appear with the given text color.
Valid values for Color are Black, Blue, Cyan, Green, Magenta, Red, White or Yellow. Valid values for the “#” in Color# are 0 - 56 (this color palette shows a list of the colors that correspond to each number). Number format colors will override any user-entered colors on the cell, but will not override colors set by conditional formatting.
- class shillelagh.adapters.api.gsheets.parsing.number.COMMA(token: str)[source]¶
Bases:
TokenMultiplier.
When appearing after the digit characters the comma scales the digits by one thousand per comma.
- class shillelagh.adapters.api.gsheets.parsing.number.CONDITION(token: str)[source]¶
Bases:
TokenReplaces the default comparison section with another conditional expression.
Replaces the default positive, negative, or zero comparison section with another conditional expression. For example, [<100]”Low”;[>1000]”High”;000 will render the word “Low” for values below 100, “High” for values above 1000 and a three digit number (with leading 0s) for anything in between. Conditions can only be applied to the first two sub-formats and if a number matches more than one, it will use the first one it matches. If there is a third format, it will be used for “everything else”, otherwise if a number doesn’t match either format, it will be rendered as all “#”s filling up the cell width. The fourth format is always used for text, if it exists.
- class shillelagh.adapters.api.gsheets.parsing.number.DIGITS(token: str)[source]¶
Bases:
TokenOne or more digits in the number.
Each token renders digits differently:
0: if the digit is an insignificant 0, it is rendered as 0.
#: if the digit is an insignificant 0, it is not rendered.
?: if the digit is an insignificant 0, it is rendered as a space.
If there’s a comma between tokens it renders the entire number with grouping separators (grouping by the thousands)
- class shillelagh.adapters.api.gsheets.parsing.number.E(token: str)[source]¶
Bases:
TokenScientific format.
Renders the number in scientific format, with the formatting to the left of the E used for the non-exponent portion and the formatting to the right of the E used for the exponent portion. E+ will show a + sign for positive exponents. E- will only show a sign for negative exponents. If lowercase is used, the output e is lowercased as well.
- class shillelagh.adapters.api.gsheets.parsing.number.FRACTION(token: str)[source]¶
Bases:
TokenFractional format.
If it appears between two digit characters (0, # or ?), then it treats those digit groups as a fractional format. For example, the number format 0 #/# renders the number 23.25 as 23 1/4. The denominator can also be a literal integer, in which case it will enforce that integer as the denominator. The number format 0 #/8 displays the number 23.25 as 23 2/8. The fraction part is not rendered at all if the numerator would become 0. The number 23.1 with the number format 0 #/3 renders as just 23 (because the 0.1 rounded to 0/3). / is not compatible with scientific format or a format with a decimal point in it.
- class shillelagh.adapters.api.gsheets.parsing.number.PERCENT(token: str)[source]¶
Bases:
TokenLiteral percent sign.
Appears as a literal but also causes existing numbers to be multiplied by 100 before being rendered, in order to make percentages more readable.
- class shillelagh.adapters.api.gsheets.parsing.number.PERIOD(token: str)[source]¶
Bases:
TokenThe decimal point in the number.
The first period represents the decimal point in the number. Subsequent periods are rendered as literals. If you include a decimal point in the format, it will always be rendered, even for whole numbers.
- class shillelagh.adapters.api.gsheets.parsing.number.STAR(token: str)[source]¶
Bases:
TokenThis is included for compatibility with Excel number formats. It is currently ignored.
- class shillelagh.adapters.api.gsheets.parsing.number.UNDERSCORE(token: str)[source]¶
Bases:
TokenSkips the next character and renders a space.
This is used to line up number formats where the negative value is surrounded by parenthesis.
- shillelagh.adapters.api.gsheets.parsing.number.adjust_value(value: int | float, tokens: list[Token]) int | float[source]¶
Adjust value applying percent, scientific notation, and comma multiplier.
- shillelagh.adapters.api.gsheets.parsing.number.condition_matches(value: int | float, format_: str) bool[source]¶
Return true if the value matches the condition in the pattern.
- shillelagh.adapters.api.gsheets.parsing.number.fix_periods(tokens: Iterator[Token]) Iterator[Token][source]¶
Convert periods into literals after the first one.
- shillelagh.adapters.api.gsheets.parsing.number.format_number_pattern(value: int | float | str, pattern: str) str[source]¶
Format a number to a given pattern.
- shillelagh.adapters.api.gsheets.parsing.number.get_fraction(number: str) float[source]¶
Return the fraction associated with a fractional part.
>>> get_fraction("9") 0.9 >>> get_fraction("123") 0.123 >>> get_fraction("1") 0.1 >>> get_fraction("001") 0.001
- shillelagh.adapters.api.gsheets.parsing.number.has_condition(pattern: str) bool[source]¶
Return true if the pattern has condition instructions.
- shillelagh.adapters.api.gsheets.parsing.number.is_fractional(token: Token, tokens: list[Token]) bool[source]¶
Return true if the token is after the period.