Python Forum
parse/read from file seperated by dots
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
parse/read from file seperated by dots
#6
Here is a different approach.
The structural pattern matching requires Python 3.10.
The dataclass is a convenient way to create classes which contains data.


from __future__ import annotations

from collections.abc import Generator
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any


@dataclass(order=True)
class Step:
    ident: str = field(compare=False)
    number: int = field(compare=True)
    name: str = field(compare=False)
    result: str = field(compare=False)
    progress: str = field(compare=False)


def parse(file: str | Path | None = None, text: str | None = None) -> Generator[Step, None, None]:
    data : dict[str, Any] = {}

    if file and text:
        raise TypeError("text and file are mutually exclusive")

    if not (file or text):
        raise TypeError("text or file must be given")

    if file:
        text = Path(file).read_text()

    if not text:
        return

    for line in text.splitlines():
        try:
            step, assignment = map(str.strip, line.split("=", maxsplit=1))
        except ValueError:
            continue

        elements = step.split(".", maxsplit=3)

        match len(elements):
            case 2:
                if data:
                    try:
                        yield Step(**data)
                    except TypeError:
                        pass
                    data.clear()
                data["number"] = int(elements[1])
                data["ident"] = assignment
            case 3:
                data[elements[2].lower()] = assignment

    if data:
        try:
            yield Step(**data)
        except TypeError:
            pass


text = """
garbage90832.rtzwrtzwrt.ztzwrtz = gg

Step.0 = Foo

!!!!ยง$252346"$%/
Step.1 = Task1
Step.1.Name = My first Task
Step.1.Result = good
Step.1.Progress = finished
Step.2 = Task2
Step.2.Name = My second Task
Step.2.Result = good
Step.2.Progress = finished
Step.3 = FOO
Step.3.Name = My thrid Task
Step.3.Result = xx
Step.3.Progress = waiting
garbage $%&)(/=
"""

result = sorted(parse(text=text))

for step in result:
    print(step)
Result from for-loop:
Quote:Step(ident='Task1', number=1, name='My first Task', result='good', progress='finished')
Step(ident='Task2', number=2, name='My second Task', result='good', progress='finished')
Step(ident='FOO', number=3, name='My thrid Task', result='xx', progress='waiting')

TypeHints are optional. Nothing is checked during runtime.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
RE: parse/read from file seperated by dots - by DeaD_EyE - Jun-26-2023, 12:26 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Connecting to Remote Server to read contents of a file ChaitanyaSharma 1 360 May-03-2024, 07:23 AM
Last Post: Pedroski55
  Recommended way to read/create PDF file? Winfried 3 3,045 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  parse json field from csv file lebossejames 4 875 Nov-14-2023, 11:34 PM
Last Post: snippsat
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,736 Nov-09-2023, 10:56 AM
Last Post: mg24
  read file txt on my pc to telegram bot api Tupa 0 1,227 Jul-06-2023, 01:52 AM
Last Post: Tupa
  Formatting a date time string read from a csv file DosAtPython 5 1,544 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  How do I read and write a binary file in Python? blackears 6 7,766 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Read csv file with inconsistent delimiter gracenz 2 1,308 Mar-27-2023, 08:59 PM
Last Post: deanhystad
  Read text file, modify it then write back Pavel_47 5 1,794 Feb-18-2023, 02:49 PM
Last Post: deanhystad
  Correctly read a malformed CSV file data klllmmm 2 2,179 Jan-25-2023, 04:12 PM
Last Post: klllmmm

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020