kkpyutil 1.37.0__tar.gz → 1.38.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: kkpyutil
3
- Version: 1.37.0
3
+ Version: 1.38.0
4
4
  Summary: Building blocks for sysadmin and DevOps
5
5
  Home-page: https://github.com/kakyoism/kkpyutil/
6
6
  License: MIT
@@ -1698,6 +1698,48 @@ def is_number_text(text):
1698
1698
  return False
1699
1699
 
1700
1700
 
1701
+ def is_bool_text(text):
1702
+ return text.lower() in ('true', 'false')
1703
+
1704
+
1705
+ def create_parameter(name, default: str, val_range=None, step=0.1, precision: int = 2, delim=' '):
1706
+ """
1707
+ - a single user text input may carry polymorphic primitive data types
1708
+ - so we need to convert it to a good enough data record that a frontend can understand
1709
+ - step is for numbers only, and precision is for floats only
1710
+ - some frontend may offer fine-tuning, a good practice is to use step/10 for that, but this low-level API does not offer fine-tuning for minimalism
1711
+ - for numbers, null xrange or null component means no range limit
1712
+ - for options, range is a tuple of options; single-selection uses a literal str as default; multi-selection uses a space-separated str 'opt1 opt2'
1713
+ """
1714
+ if options := isinstance(val_range, tuple):
1715
+ assert len(val_range)
1716
+ try:
1717
+ i_default = val_range.index(default)
1718
+ # single-select
1719
+ default_opts = default
1720
+ except ValueError:
1721
+ # multi-select
1722
+ default_opts = tuple([opt.strip() for opt in default.split(delim)])
1723
+ return {'name': name, 'type': 'option', 'default': default_opts, 'range': val_range}
1724
+ if is_bool_text(default):
1725
+ return {'name': name, 'type': 'bool', 'default': default.lower() == 'true'}
1726
+ if is_number_text(default):
1727
+ # edge cases: None, (None, 1.0), (1.0, None), (None, None)
1728
+ if val_range is None:
1729
+ val_range = [float('-inf'), float('inf')]
1730
+ else:
1731
+ assert len(val_range) == 2
1732
+ if val_range[0] is None:
1733
+ val_range[0] = '-inf'
1734
+ if val_range[1] is None:
1735
+ val_range[1] = 'inf'
1736
+ val_range = [float(val_range[0]), float(val_range[1])]
1737
+ if is_float_text(default):
1738
+ return {'name': name, 'type': 'float', 'default': float(default), 'range': val_range, 'step': step, 'precision': precision}
1739
+ return {'name': name, 'type': 'int', 'default': int(default), 'range': val_range, 'step': max(int(step), 1)}
1740
+ return {'name': name, 'type': 'str', 'default': default}
1741
+
1742
+
1701
1743
  def compare_dsv_lines(line1, line2, delim=' ', float_rel_tol=1e-6, float_abs_tol=1e-6, striptext=True, randomidok=False, logger=None):
1702
1744
  """
1703
1745
  - compare two lines of delimiter-separated values, with numerical and uuid comparison in mind
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "kkpyutil"
3
- version = "1.37.0"
3
+ version = "1.38.0"
4
4
  description = "Building blocks for sysadmin and DevOps"
5
5
  authors = ["Beinan Li <li.beinan@gmail.com>"]
6
6
  maintainers = ["Beinan Li <li.beinan@gmail.com>"]
File without changes
File without changes