Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ENH] Clean code #16

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 0 additions & 34 deletions src/clinicaio/hello.py

This file was deleted.

7 changes: 7 additions & 0 deletions src/clinicaio/models/bids/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from ._base import Index, Label, Suffix

__all__ = [
"Label",
"Index",
"Suffix",
]
54 changes: 54 additions & 0 deletions src/clinicaio/models/bids/_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from collections import UserString
from enum import Enum
from typing import Optional, Union

__all__ = [
"Label",
"Index",
"Suffix",
]


class Label(UserString):
"""Defines a label as defined in the BIDS specifications (i.e. an alphanumeric string)."""

def __init__(self, value: Union[str, Enum]):
super().__init__(self.validate(value))

@classmethod
def validate(cls, value: Union[str, Enum]) -> str:
if isinstance(value, Enum):
return value.value
elif isinstance(value, str) and value.isalnum():
return value
raise ValueError(
f"Label '{value}' is not a valid BIDS label: it must be string composed only by letters and/or numbers."
)


Suffix = Label


class Index(UserString):
"""Defines an index as defined in the BIDS specifications as a positive integer with padding logic."""

def __init__(self, value: int, length_as_string: Optional[int] = None):
super().__init__(self.validate(value, length_as_string))

@classmethod
def validate(cls, value: int, length_as_string: Optional[int] = None) -> str:
if not isinstance(value, int) or value < 0:
raise ValueError(
f"Index '{value}' is not a valid BIDS index: it must be a non-negative integer."
)
if length_as_string is None:
length_as_string = len(str(value))
if length_as_string <= 0:
raise ValueError(
f"The length of an Index should be at least 1, {length_as_string} was provided."
)
if len(str(value)) > length_as_string:
raise ValueError(
f"Cannot set index with value {value} with a length of {length_as_string}."
)
return str(value).zfill(length_as_string)
Loading
Loading