Skip to content

Examples

Flav20 edited this page Nov 8, 2024 · 6 revisions

Home [Previous page](Arguments explaination)

Table of contents

Example-file

We will use this example file test.txt

With cat -e test.txt:

[Warning]:Entity not found$
[Error]:Unable to recover data$
[Info]:Segfault$
[Warning]:Indentation$
[Error]:Memory leaks$
[Info]:Entity not found$
[Warning]:Unable to recover data$
  $
[Error]:Segfault$
[Info]:Indentation$
[Warning]:Memory leaks$

Example-simple-head

1\ Simple head (can be change to tail) Code:

import file_utils_operations_lib

path: str = "my_path_to_file"
n: int = 2 # Number of lines to read

try:
    head: list = file_utils_operations_lib.WithEOL.head(path=path, n=n)
    print(head)
except:
    print("Unable to open/read the file")

Stdout:

['[Warning]:Entity not found', '[Error]:Unable to recover data']

Example-simple-tail

Code:

import file_utils_operations_lib

path: str = "my_path_to_file"
n: int = 2 # Number of lines to read

try:
    tail: list = file_utils_operations_lib.WithEOL.tail(path=path, n=n)
    print(tail)
except:
    print("Unable to open/read the file")

Stdout:

['[Info]:Indentation', '[Warning]:Memory leaks']

Example-simple-between

Code:

import file_utils_operations_lib

path: str = "my_path_to_file"
n1: int = 2 # First line to read
n2: int = 4 # Last line to read

try:
    between: list = file_utils_operations_lib.WithEOL.between(path=path, n1=n1, n2=n2)
    print(between)
except:
    print("Unable to open/read the file")

Stdout:

['[Error]:Unable to recover data', '[Info]:Segfault', '[Warning]:Indentation']

Example-simple-parse

Code:

import file_utils_operations_lib

path: str = "my_path_to_file"

try:
    between: list = file_utils_operations_lib.WithEOL.parse(path=path)
    print(between)
except:
    print("Unable to open/read the file")

Stdout:

['[Warning]:Entity not found', '[Error]:Unable to recover data', '[Info]:Segfault', '[Warning]:Indentation', '[Error]:Memory leaks', '[Info]:Entity not found', '[Warning]:Unable to recover data', '  ', '[Error]:Segfault', '[Info]:Indentation', '[Warning]:Memory leaks']

Example-simple-count_lines

Code:

import file_utils_operations_lib

path: str = "my_path_to_file"

try:
    between: list = file_utils_operations_lib.WithEOL.count_lines(path=path)
    print(between)
except:
    print("Unable to open/read the file")

Stdout:

11

Example-remove_empty_string

With remove_empty_string enable:
Code:

import file_utils_operations_lib

path: str = "my_path_to_file"
n: int = 4 # First line to read

try:
    tail: list = file_utils_operations_lib.WithEOL.tail(path=path, n=n, remove_empty_string=True)
    print(tail)
except:
    print("Unable to open/read the file")

Stdout:

['[Warning]:Unable to recover data', '[Error]:Segfault', '[Info]:Indentation', '[Warning]:Memory leaks']

With remove_empty_string disable (default option):
Code:

import file_utils_operations_lib

path: str = "my_path_to_file"
n: int = 4 # First line to read

try:
    tail: list = file_utils_operations_lib.WithEOL.tail(path=path, n=n, remove_empty_string=False)
    print(tail)
except:
    print("Unable to open/read the file")

Stdout:

['  ', '[Error]:Segfault', '[Info]:Indentation', '[Warning]:Memory leaks']

Example-regex_keep

Code:

import file_utils_operations_lib

path: str = "my_path_to_file"
n: int = 4 # First line to read

try:
    head: list = file_utils_operations_lib.WithEOL.head(path=path, n=n, remove_empty_string=False, regex_keep=["\[Warning\]:*", "\[Error\]:*"])
    print(head)
except:
    print("Unable to open/read the file")

Stdout:

['[Warning]:Entity not found', '[Error]:Unable to recover data', '[Warning]:Indentation']

Why tere is just 3 elements instead of 4? You should look at the restrict option

Example-regex_pass

Code:

import file_utils_operations_lib

path: str = "my_path_to_file"
n: int = 4 # First line to read

try:
    head: list = file_utils_operations_lib.WithEOL.head(path=path, n=n, remove_empty_string=False, regex_pass=["\[Warning\]:*", "\[Error\]:*"])
    print(head)
except:
    print("Unable to open/read the file")

Stdout:

['[Info]:Segfault']

Why tere is just 3 elements instead of 4? You should look at the restrict option

Example-restrict

With restrict disable:
Code:

import file_utils_operations_lib

path: str = "my_path_to_file"
n: int = 4 # First line to read

try:
    head: list = file_utils_operations_lib.WithEOL.head(path=path, n=4, remove_empty_string=False, regex_keep=["\[Warning\]:*", "\[Error\]:*"], restrict=False)
    print(head)
except:
    print("Unable to open/read the file")

Stdout:

['[Warning]:Entity not found', '[Error]:Unable to recover data', '[Warning]:Indentation', '[Error]:Memory leaks']

With restrict enbale(default):
Code:

import file_utils_operations_lib

path: str = "my_path_to_file"
n: int = 4 # First line to read

try:
    head: list = file_utils_operations_lib.WithEOL.head(path=path, n=4, remove_empty_string=False, regex_keep=["\[Warning\]:*", "\[Error\]:*"], restrict=True)
    print(head)
except:
    print("Unable to open/read the file")

Stdout:

['[Warning]:Entity not found', '[Error]:Unable to recover data', '[Warning]:Indentation']

Home

Clone this wiki locally