Repository: ray-project/ray
File: python/ray/data/_internal/util.py (Python)

Code before:
    if num > 0 and num < len(paths):
        raise ValueError(
            "The paths must all be local-scheme or not local-scheme, "
            f"but found mixed {paths}"
        )
    return num == len(paths)


def _validate_head_node_resources_for_local_scheduling(
    ray_remote_args: Dict[str, Any],
    *,
    op_name: str,
) -> None:
    """Ensure the head node has enough resources before pinning work there.

    Local paths (``local://``) and other driver-local I/O force tasks onto the
    head node via ``NodeAffinitySchedulingStrategy``. If the head node was
    intentionally started with zero logical resources (a common practice to
    avoid OOMs), those tasks become unschedulable. Detect this upfront and
    raise a clear error with remediation steps.
    """

    # Ray defaults to reserving 1 CPU per task when num_cpus isn't provided.
    num_cpus = ray_remote_args.get("num_cpus")
    if num_cpus is None:
        num_cpus = 1
    num_gpus = ray_remote_args.get("num_gpus")
    if num_gpus is None:
        num_gpus = 0
    memory = ray_remote_args.get("memory")
    if memory is None:
        memory = 0

    required_resources: Dict[str, float] = {}
    if num_cpus > 0:
        required_resources["CPU"] = float(num_cpus)
    if num_gpus > 0:
        required_resources["GPU"] = float(num_gpus)
    if memory > 0:
        required_resources["memory"] = float(memory)

    # Include any additional custom resources requested.
    custom_resources = ray_remote_args.get("resources")
    if custom_resources is None:
        custom_resources = {}
    for name, amount in custom_resources.items():
        if amount is None:
            continue
        if amount > 0:
            required_resources[name] = float(amount)


Review comment (suggestion):
here and below, you can do 
```python
num_cpus = ray_remote_args.get("num_cpus", 1) # default is 2nd param
```

Code after:
    if num > 0 and num < len(paths):
        raise ValueError(
            "The paths must all be local-scheme or not local-scheme, "
            f"but found mixed {paths}"
        )
    return num == len(paths)


def _truncated_repr(obj: Any) -> str:
    """Utility to return a truncated object representation for error messages."""
    msg = str(obj)
    if len(msg) > 200:
        msg = msg[:200] + "..."
    return msg


def _insert_doc_at_pattern(
    obj,
    *,
    message: str,
    pattern: str,
    insert_after: bool = True,
    directive: Optional[str] = None,
    skip_matches: int = 0,
) -> str:
    if "\n" in message:
        raise ValueError(
            "message shouldn't contain any newlines, since this function will insert "
            f"its own linebreaks when text wrapping: {message}"
        )

    doc = obj.__doc__.strip()
    if not doc:
        doc = ""

    if pattern == "" and insert_after:
        # Empty pattern + insert_after means that we want to append the message to the
        # end of the docstring.
        head = doc
        tail = ""
    else:
        tail = doc
        i = tail.find(pattern)
        skip_matches_left = skip_matches
        while i != -1:
            if insert_after:
                # Set offset to the first character after the pattern.
                offset = i + len(pattern)
            else:
                # Set offset to the first character in the matched line.
                offset = tail[:i].rfind("\n") + 1