File: util.py

package info (click to toggle)
python-libais 0.17%2Bgit.20190917.master.e464cf8-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 5,768 kB
  • sloc: cpp: 56,058; python: 11,974; makefile: 535; sh: 453
file content (19 lines) | stat: -rw-r--r-- 359 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
"""Helpers for libais."""


def MaybeToNumber(instance):
  """Convert to an int or float if possible."""
  if isinstance(instance, (float, int)) or instance is None:
    return instance

  try:
    return int(instance)
  except (TypeError, ValueError):
    pass

  try:
    return float(instance)
  except (TypeError, ValueError):
    pass

  return instance