File: _function.py

package info (click to toggle)
python-typepy 1.3.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 624 kB
  • sloc: python: 2,886; makefile: 78; sh: 7
file content (52 lines) | stat: -rw-r--r-- 1,096 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
"""
.. codeauthor:: Tsuyoshi Hombashi <tsuyoshi.hombashi@gmail.com>
"""

import re
from typing import Any, Optional, Type

from ._const import StrictLevel
from .type import AbstractType, Integer, NullString, RealNumber, String


def is_hex(value: Any) -> bool:
    try:
        int(value, 16)
    except (TypeError, ValueError):
        return False

    return True


def is_null_string(value: Any) -> bool:
    return NullString(value, strict_level=StrictLevel.MIN).is_type()


def is_not_null_string(value: Any) -> bool:
    return String(value, strict_level=StrictLevel.MAX).is_type()


def is_empty_sequence(value: Any) -> bool:
    try:
        return value is None or len(value) == 0
    except TypeError:
        return False


def is_not_empty_sequence(value: Any) -> bool:
    try:
        return len(value) > 0
    except TypeError:
        return False


def extract_typepy_from_dtype(dtype) -> Optional[Type[AbstractType]]:
    dtype = str(dtype)

    if re.search("^float", dtype):
        return RealNumber

    if re.search("^int", dtype):
        return Integer

    return None