Skip to content

bgc_data_processing.utils.patterns

Filenames Patterns building.

FileNamePattern(str_pattern, year_field='years', month_field='months', day_field='days')

Create the pattern to use to select filenames.

Parameters:

Name Type Description Default
str_pattern str

Raw string pattern, with field to infer written in brackets ('{'&'}').

required
year_field str

Name of the field supposed to contain the year informations. , by default "years"

'years'
month_field str

Name of the field supposed to contain the month informations. , by default "months"

'months'
day_field str

Name of the field supposed to contain the day informations. , by default "days"

'days'
Source code in src/bgc_data_processing/utils/patterns.py
30
31
32
33
34
35
36
37
38
39
40
def __init__(
    self,
    str_pattern: str,
    year_field: str = "years",
    month_field: str = "months",
    day_field: str = "days",
) -> None:
    self._base = str_pattern
    self._year = year_field
    self._month = month_field
    self._day = day_field

build_from_constraint(date_constraint)

Build the pattern from date constraints.

Parameters:

Name Type Description Default
date_constraint dict

Date constraint.

required

Returns:

Type Description
str

Final pattern.

Source code in src/bgc_data_processing/utils/patterns.py
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
def build_from_constraint(self, date_constraint: dict) -> "PatternMatcher":
    """Build the pattern from date constraints.

    Parameters
    ----------
    date_constraint : dict
        Date constraint.

    Returns
    -------
    str
        Final pattern.
    """
    date_min, date_max = self._parse_dates_from_constraints(date_constraint)
    return self.build(date_min=date_min, date_max=date_max)

build(date_min, date_max)

Build the pattern from the starting and ending dates.

Parameters:

Name Type Description Default
date_min date | None

Starting date.

required
date_max date | None

Ending date.

required

Returns:

Type Description
str

Final pattern.

Source code in src/bgc_data_processing/utils/patterns.py
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
def build(
    self,
    date_min: dt.date | None,
    date_max: dt.date | None,
) -> "PatternMatcher":
    """Build the pattern from the starting and ending dates.

    Parameters
    ----------
    date_min : dt.date | None
        Starting date.
    date_max : dt.date | None
        Ending date.

    Returns
    -------
    str
        Final pattern.
    """
    interval_patterns = self._slice(date_min=date_min, date_max=date_max)
    pattern = "|".join(map(self._create_pattern, interval_patterns))
    return PatternMatcher(pattern=pattern)

DateIntervalPattern(date_min, date_max, precision='day')

Create the patterns values for a given date interval.

Parameters:

Name Type Description Default
date_min date | None

Starting date of the interval.

required
date_max date | None

Ending date of the interval.

required
precision str

Precision of the interval. A "day" precision means that year and month are constant, therefore the required days are contained between the minimum day number and the maximum day number. A "month" precision means that year is constant but months are different but totally included (from first day to last day.) in the interval., therefore the required months are contained between the minimum months number and the maximum months number. A "year" precision means the years to include are different but totally included (from first day to last day.) in the interval, therefore the required years are contained between the minimum year number and the maximum year number., by default "day"

'day'
Source code in src/bgc_data_processing/utils/patterns.py
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
def __init__(
    self,
    date_min: dt.date | None,
    date_max: dt.date | None,
    precision: str = "day",
) -> None:
    if not (date_min is None and date_max is None):
        self._validate_inputs(
            date_min=date_min,
            date_max=date_max,
            precision=precision,
        )

    self._precision = precision
    self._min = date_min
    self._max = date_max

years: str property

'year' part of the pattern.

months: str property

'month' part of the pattern.

days: str property

'day' part of the pattern.

__repr__()

Object representation string.

Returns:

Type Description
str

Starting date - Ending date ; precision: interval precision.

Source code in src/bgc_data_processing/utils/patterns.py
409
410
411
412
413
414
415
416
417
def __repr__(self) -> str:
    """Object representation string.

    Returns
    -------
    str
        Starting date - Ending date ; precision: interval precision.
    """
    return f"{self._min} - {self._max} ; precision: {self._precision}"

with_day_precision(date_min, date_max) classmethod

Create a DateIntervalPattern with day precision.

Parameters:

Name Type Description Default
date_min date

Starting date.

required
date_max date

Ending date.

required

Returns:

Type Description
DateIntervalPattern

Date interval pattern.

Source code in src/bgc_data_processing/utils/patterns.py
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
@classmethod
def with_day_precision(
    cls,
    date_min: dt.date,
    date_max: dt.date,
) -> "DateIntervalPattern":
    """Create a DateIntervalPattern with day precision.

    Parameters
    ----------
    date_min : dt.date
        Starting date.
    date_max : dt.date
        Ending date.

    Returns
    -------
    DateIntervalPattern
        Date interval pattern.
    """
    return cls(
        date_min=date_min,
        date_max=date_max,
        precision=cls._day_precision_label,
    )

with_month_precision(date_min, date_max) classmethod

Create a DateIntervalPattern with month precision.

Parameters:

Name Type Description Default
date_min date

Starting date.

required
date_max date

Ending date.

required

Returns:

Type Description
DateIntervalPattern

Date interval pattern.

Source code in src/bgc_data_processing/utils/patterns.py
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
@classmethod
def with_month_precision(
    cls,
    date_min: dt.date,
    date_max: dt.date,
) -> "DateIntervalPattern":
    """Create a DateIntervalPattern with month precision.

    Parameters
    ----------
    date_min : dt.date
        Starting date.
    date_max : dt.date
        Ending date.

    Returns
    -------
    DateIntervalPattern
        Date interval pattern.
    """
    return cls(
        date_min=date_min,
        date_max=date_max,
        precision=cls._month_precision_label,
    )

with_year_precision(date_min, date_max) classmethod

Create a DateIntervalPattern with year precision.

Parameters:

Name Type Description Default
date_min date

Starting date.

required
date_max date

Ending date.

required

Returns:

Type Description
DateIntervalPattern

Date interval pattern.

Source code in src/bgc_data_processing/utils/patterns.py
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
@classmethod
def with_year_precision(
    cls,
    date_min: dt.date,
    date_max: dt.date,
) -> "DateIntervalPattern":
    """Create a DateIntervalPattern with year precision.

    Parameters
    ----------
    date_min : dt.date
        Starting date.
    date_max : dt.date
        Ending date.

    Returns
    -------
    DateIntervalPattern
        Date interval pattern.
    """
    return cls(
        date_min=date_min,
        date_max=date_max,
        precision=cls._year_precision_label,
    )

PatternMatcher(pattern, validation_function=lambda : True)

Matcher to find files which match agiven pattern.

Parameters:

Name Type Description Default
pattern str

Pattern to match.

required
validation_function Callable

Function to use to validate that files are to be loaded., by default lambdafilepath

lambda : True
Source code in src/bgc_data_processing/utils/patterns.py
568
569
570
571
572
573
574
def __init__(
    self,
    pattern: str,
    validation_function: Callable = lambda filepath: True,  # noqa: ARG005
) -> None:
    self._pattern = pattern
    self.validate = validation_function

validate: Callable property writable

Validation function.

select_matching_filepath(research_directory)

Select the filepaths matching the pattern.

Parameters:

Name Type Description Default
research_directory Path | str

Directory to serach for files.

required

Returns:

Type Description
list[Path]

List of correct paths.

Source code in src/bgc_data_processing/utils/patterns.py
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
def select_matching_filepath(
    self,
    research_directory: Path | str,
) -> list[Path]:
    """Select the filepaths matching the pattern.

    Parameters
    ----------
    research_directory : Path | str
        Directory to serach for files.

    Returns
    -------
    list[Path]
        List of correct paths.
    """
    return self._recursive_match(
        research_dir=Path(research_directory),
        pattern=self._pattern,
    )