|
4 | 4 | import getpass |
5 | 5 | import importlib |
6 | 6 | import os |
7 | | -import re |
8 | 7 | from typing import List, Optional, Tuple, Union |
9 | 8 |
|
10 | 9 | import pandas |
|
13 | 12 |
|
14 | 13 | from pysqa.utils.execute import execute_command |
15 | 14 | from pysqa.utils.queues import Queues |
| 15 | +from pysqa.utils.validate import value_error_if_none, value_in_range |
16 | 16 |
|
17 | 17 |
|
18 | 18 | class BasisQueueAdapter(object): |
@@ -372,15 +372,15 @@ def check_queue_parameters( |
372 | 372 | """ |
373 | 373 | if active_queue is None: |
374 | 374 | active_queue = self._config["queues"][queue] |
375 | | - cores = self._value_in_range( |
| 375 | + cores = value_in_range( |
376 | 376 | value=cores, |
377 | 377 | value_min=active_queue["cores_min"], |
378 | 378 | value_max=active_queue["cores_max"], |
379 | 379 | ) |
380 | | - run_time_max = self._value_in_range( |
| 380 | + run_time_max = value_in_range( |
381 | 381 | value=run_time_max, value_max=active_queue["run_time_max"] |
382 | 382 | ) |
383 | | - memory_max = self._value_in_range( |
| 383 | + memory_max = value_in_range( |
384 | 384 | value=memory_max, value_max=active_queue["memory_max"] |
385 | 385 | ) |
386 | 386 | return cores, run_time_max, memory_max |
@@ -465,7 +465,7 @@ def _job_submission_template( |
465 | 465 | """ |
466 | 466 | if queue is None: |
467 | 467 | queue = self._config["queue_primary"] |
468 | | - self._value_error_if_none(value=command) |
| 468 | + value_error_if_none(value=command) |
469 | 469 | if queue not in self.queue_list: |
470 | 470 | raise ValueError( |
471 | 471 | "The queue " |
@@ -567,113 +567,3 @@ def _load_templates(queue_lst_dict: dict, directory: str = ".") -> None: |
567 | 567 | + error.message, |
568 | 568 | lineno=error.lineno, |
569 | 569 | ) |
570 | | - |
571 | | - @staticmethod |
572 | | - def _value_error_if_none(value: str) -> None: |
573 | | - """ |
574 | | - Raise a ValueError if the value is None or not a string. |
575 | | -
|
576 | | - Args: |
577 | | - value (str/None): The value to check. |
578 | | -
|
579 | | - Raises: |
580 | | - ValueError: If the value is None. |
581 | | - TypeError: If the value is not a string. |
582 | | - """ |
583 | | - if value is None: |
584 | | - raise ValueError("Value cannot be None.") |
585 | | - if not isinstance(value, str): |
586 | | - raise TypeError() |
587 | | - |
588 | | - @classmethod |
589 | | - def _value_in_range( |
590 | | - cls, |
591 | | - value: Union[int, float, None], |
592 | | - value_min: Union[int, float, None] = None, |
593 | | - value_max: Union[int, float, None] = None, |
594 | | - ) -> Union[int, float, None]: |
595 | | - """ |
596 | | - Check if a value is within a specified range. |
597 | | -
|
598 | | - Args: |
599 | | - value (int/float/None): The value to check. |
600 | | - value_min (int/float/None): The minimum value. Defaults to None. |
601 | | - value_max (int/float/None): The maximum value. Defaults to None. |
602 | | -
|
603 | | - Returns: |
604 | | - int/float/None: The value if it is within the range, otherwise the minimum or maximum value. |
605 | | - """ |
606 | | - |
607 | | - if value is not None: |
608 | | - value_, value_min_, value_max_ = [ |
609 | | - ( |
610 | | - cls._memory_spec_string_to_value(v) |
611 | | - if v is not None and isinstance(v, str) |
612 | | - else v |
613 | | - ) |
614 | | - for v in (value, value_min, value_max) |
615 | | - ] |
616 | | - # ATTENTION: '60000' is interpreted as '60000M' since default magnitude is 'M' |
617 | | - # ATTENTION: int('60000') is interpreted as '60000B' since _memory_spec_string_to_value return the size in |
618 | | - # ATTENTION: bytes, as target_magnitude = 'b' |
619 | | - # We want to compare the the actual (k,m,g)byte value if there is any |
620 | | - if value_min_ is not None and value_ < value_min_: |
621 | | - return value_min |
622 | | - if value_max_ is not None and value_ > value_max_: |
623 | | - return value_max |
624 | | - return value |
625 | | - else: |
626 | | - if value_min is not None: |
627 | | - return value_min |
628 | | - if value_max is not None: |
629 | | - return value_max |
630 | | - return value |
631 | | - |
632 | | - @staticmethod |
633 | | - def _is_memory_string(value: str) -> bool: |
634 | | - """ |
635 | | - Check if a string specifies a certain amount of memory. |
636 | | -
|
637 | | - Args: |
638 | | - value (str): The string to check. |
639 | | -
|
640 | | - Returns: |
641 | | - bool: True if the string matches a memory specification, False otherwise. |
642 | | - """ |
643 | | - memory_spec_pattern = r"[0-9]+[bBkKmMgGtT]?" |
644 | | - return re.findall(memory_spec_pattern, value)[0] == value |
645 | | - |
646 | | - @classmethod |
647 | | - def _memory_spec_string_to_value( |
648 | | - cls, value: str, default_magnitude: str = "m", target_magnitude: str = "b" |
649 | | - ) -> Union[int, float]: |
650 | | - """ |
651 | | - Converts a valid memory string (tested by _is_memory_string) into an integer/float value of desired |
652 | | - magnitude `default_magnitude`. If it is a plain integer string (e.g.: '50000') it will be interpreted with |
653 | | - the magnitude passed in by the `default_magnitude`. The output will rescaled to `target_magnitude` |
654 | | -
|
655 | | - Args: |
656 | | - value (str): The string to convert. |
657 | | - default_magnitude (str): The magnitude for interpreting plain integer strings [b, B, k, K, m, M, g, G, t, T]. Defaults to "m". |
658 | | - target_magnitude (str): The magnitude to which the output value should be converted [b, B, k, K, m, M, g, G, t, T]. Defaults to "b". |
659 | | -
|
660 | | - Returns: |
661 | | - Union[int, float]: The value of the string in `target_magnitude` units. |
662 | | - """ |
663 | | - magnitude_mapping = {"b": 0, "k": 1, "m": 2, "g": 3, "t": 4} |
664 | | - if cls._is_memory_string(value): |
665 | | - integer_pattern = r"[0-9]+" |
666 | | - magnitude_pattern = r"[bBkKmMgGtT]+" |
667 | | - integer_value = int(re.findall(integer_pattern, value)[0]) |
668 | | - |
669 | | - magnitude = re.findall(magnitude_pattern, value) |
670 | | - if len(magnitude) > 0: |
671 | | - magnitude = magnitude[0].lower() |
672 | | - else: |
673 | | - magnitude = default_magnitude.lower() |
674 | | - # Convert it to default magnitude = megabytes |
675 | | - return (integer_value * 1024 ** magnitude_mapping[magnitude]) / ( |
676 | | - 1024 ** magnitude_mapping[target_magnitude] |
677 | | - ) |
678 | | - else: |
679 | | - return value |
0 commit comments