Python typing.SupportsInt() Examples
The following are 13
code examples of typing.SupportsInt().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
typing
, or try the search function
.
Example #1
Source File: filesize.py From pyfilesystem2 with MIT License | 6 votes |
def _to_str(size, suffixes, base): # type: (SupportsInt, Iterable[Text], int) -> Text try: size = int(size) except ValueError: raise TypeError("filesize requires a numeric value, not {!r}".format(size)) if size == 1: return "1 byte" elif size < base: return "{:,} bytes".format(size) # TODO (dargueta): Don't rely on unit or suffix being defined in the loop. for i, suffix in enumerate(suffixes, 2): # noqa: B007 unit = base ** i if size < unit: break return "{:,.1f} {}".format((base * size / unit), suffix)
Example #2
Source File: filesize.py From pyfilesystem2 with MIT License | 6 votes |
def traditional(size): # type: (SupportsInt) -> Text """Convert a filesize in to a string (powers of 1024, JDEC prefixes). In this convention, ``1024 B = 1 KB``. This is the format that was used to display the size of DVDs (*700 MB* meaning actually about *734 003 200 bytes*) before standardisation of IEC units among manufacturers, and still used by **Windows** to report the storage capacity of hard drives (*279.4 GB* meaning *279.4 × 1024³ bytes*). Arguments: size (int): A file size. Returns: `str`: A string containing an abbreviated file size and units. Example: >>> filesize.traditional(30000) '29.3 KB' """ return _to_str(size, ("KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"), 1024)
Example #3
Source File: filesize.py From pyfilesystem2 with MIT License | 6 votes |
def binary(size): # type: (SupportsInt) -> Text """Convert a filesize in to a string (powers of 1024, IEC prefixes). In this convention, ``1024 B = 1 KiB``. This is the format that has gained adoption among manufacturers to avoid ambiguity regarding size units, since it explicitly states using a binary base (*KiB = kibi bytes = kilo binary bytes*). This format is notably being used by the **Linux** kernel (see ``man 7 units``). Arguments: int (size): A file size. Returns: `str`: A string containing a abbreviated file size and units. Example: >>> filesize.binary(30000) '29.3 KiB' """ return _to_str(size, ("KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"), 1024)
Example #4
Source File: filesize.py From pyfilesystem2 with MIT License | 6 votes |
def decimal(size): # type: (SupportsInt) -> Text """Convert a filesize in to a string (powers of 1000, SI prefixes). In this convention, ``1000 B = 1 kB``. This is typically the format used to advertise the storage capacity of USB flash drives and the like (*256 MB* meaning actually a storage capacity of more than *256 000 000 B*), or used by **Mac OS X** since v10.6 to report file sizes. Arguments: int (size): A file size. Returns: `str`: A string containing a abbreviated file size and units. Example: >>> filesize.decimal(30000) '30.0 kB' """ return _to_str(size, ("kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"), 1000)
Example #5
Source File: version.py From pipenv with MIT License | 5 votes |
def _parse_letter_version( letter, # type: str number, # type: Union[str, bytes, SupportsInt] ): # type: (...) -> Optional[Tuple[str, int]] if letter: # We consider there to be an implicit 0 in a pre-release if there is # not a numeral associated with it. if number is None: number = 0 # We normalize any letters to their lower case form letter = letter.lower() # We consider some words to be alternate spellings of other words and # in those cases we want to normalize the spellings to our preferred # spelling. if letter == "alpha": letter = "a" elif letter == "beta": letter = "b" elif letter in ["c", "pre", "preview"]: letter = "rc" elif letter in ["rev", "r"]: letter = "post" return letter, int(number) if not letter and number: # We assume if we are given a number, but we are not given a letter # then this is using the implicit post release syntax (e.g. 1.0-1) letter = "post" return letter, int(number) return None
Example #6
Source File: version.py From pipenv with MIT License | 5 votes |
def _parse_letter_version( letter, # type: str number, # type: Union[str, bytes, SupportsInt] ): # type: (...) -> Optional[Tuple[str, int]] if letter: # We consider there to be an implicit 0 in a pre-release if there is # not a numeral associated with it. if number is None: number = 0 # We normalize any letters to their lower case form letter = letter.lower() # We consider some words to be alternate spellings of other words and # in those cases we want to normalize the spellings to our preferred # spelling. if letter == "alpha": letter = "a" elif letter == "beta": letter = "b" elif letter in ["c", "pre", "preview"]: letter = "rc" elif letter in ["rev", "r"]: letter = "post" return letter, int(number) if not letter and number: # We assume if we are given a number, but we are not given a letter # then this is using the implicit post release syntax (e.g. 1.0-1) letter = "post" return letter, int(number) return None
Example #7
Source File: test_typing.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_supports_int(self): assert issubclass(int, typing.SupportsInt) assert not issubclass(str, typing.SupportsInt)
Example #8
Source File: version.py From pip-api with Apache License 2.0 | 5 votes |
def _parse_letter_version( letter, # type: str number, # type: Union[str, bytes, SupportsInt] ): # type: (...) -> Optional[Tuple[str, int]] if letter: # We consider there to be an implicit 0 in a pre-release if there is # not a numeral associated with it. if number is None: number = 0 # We normalize any letters to their lower case form letter = letter.lower() # We consider some words to be alternate spellings of other words and # in those cases we want to normalize the spellings to our preferred # spelling. if letter == "alpha": letter = "a" elif letter == "beta": letter = "b" elif letter in ["c", "pre", "preview"]: letter = "rc" elif letter in ["rev", "r"]: letter = "post" return letter, int(number) if not letter and number: # We assume if we are given a number, but we are not given a letter # then this is using the implicit post release syntax (e.g. 1.0-1) letter = "post" return letter, int(number) return None
Example #9
Source File: _version.py From hangar-py with Apache License 2.0 | 5 votes |
def _parse_letter_version( letter: str, number: Union[str, bytes, SupportsInt], ) -> Optional[Tuple[str, int]]: if letter: # We consider there to be an implicit 0 in a pre-release if there is # not a numeral associated with it. if number is None: number = 0 # We normalize any letters to their lower case form letter = letter.lower() # We consider some words to be alternate spellings of other words and # in those cases we want to normalize the spellings to our preferred # spelling. if letter == "alpha": letter = "a" elif letter == "beta": letter = "b" elif letter in ["c", "pre", "preview"]: letter = "rc" elif letter in ["rev", "r"]: letter = "post" return letter, int(number) if not letter and number: # We assume if we are given a number, but we are not given a letter # then this is using the implicit post release syntax (e.g. 1.0-1) letter = "post" return letter, int(number) return None
Example #10
Source File: version.py From rules_pip with MIT License | 5 votes |
def _parse_letter_version( letter, # type: str number, # type: Union[str, bytes, SupportsInt] ): # type: (...) -> Optional[Tuple[str, int]] if letter: # We consider there to be an implicit 0 in a pre-release if there is # not a numeral associated with it. if number is None: number = 0 # We normalize any letters to their lower case form letter = letter.lower() # We consider some words to be alternate spellings of other words and # in those cases we want to normalize the spellings to our preferred # spelling. if letter == "alpha": letter = "a" elif letter == "beta": letter = "b" elif letter in ["c", "pre", "preview"]: letter = "rc" elif letter in ["rev", "r"]: letter = "post" return letter, int(number) if not letter and number: # We assume if we are given a number, but we are not given a letter # then this is using the implicit post release syntax (e.g. 1.0-1) letter = "post" return letter, int(number) return None
Example #11
Source File: test_typing.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_supports_int(self): self.assertIsSubclass(int, typing.SupportsInt) self.assertNotIsSubclass(str, typing.SupportsInt)
Example #12
Source File: test_typing.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_protocol_instance_type_error(self): with self.assertRaises(TypeError): isinstance(0, typing.SupportsAbs) class C1(typing.SupportsInt): def __int__(self) -> int: return 42 class C2(C1): pass c = C2() self.assertIsInstance(c, C1)
Example #13
Source File: superset_test_custom_template_processors.py From incubator-superset with Apache License 2.0 | 5 votes |
def DATE( ts: datetime, day_offset: SupportsInt = 0, hour_offset: SupportsInt = 0 ) -> str: """Current day as a string""" day_offset, hour_offset = int(day_offset), int(hour_offset) offset_day = (ts + timedelta(days=day_offset, hours=hour_offset)).date() return str(offset_day)