Einzelnen Beitrag anzeigen

bernhard_LA

Registriert seit: 8. Jun 2009
Ort: Bayern
1.123 Beiträge
 
Delphi 11 Alexandria
 
#5

AW: Portierung Klassen Python nach Delphi, GDS2 parser

  Alt 5. Okt 2017, 22:27
in der python version werden im source code records definiert. (siehe code ausschnitt)
der Zugriff auf ein record sollte über ein Attribut und dann auf die richtige Datenauswerte Logik erfolgen.

Info : Primar liegen alle Daten im Hex Format in einer GDS Datei vor, über ein Flag kann ich dann die richtige Interpretation der Daten vornehmen.


Wie würde man die Python Lösung in Delphi realisieren ?




Delphi-Quellcode:
class AbstractRecord(object):
    def __init__(self, variable):
        self.variable = variable

    def read(self, instance, gen):
        raise NotImplementedError

    def save(self, instance, stream):
        raise NotImplementedError

    def __repr__(self):
        return '<property: %s>'%self.variable

class SecondVar(object):
    """Class that simplifies second property support."""
    def __init__(self, variable2):
        self.variable2 = variable2

class SimpleRecord(AbstractRecord):
    def __init__(self, variable, gds_record):
        AbstractRecord.__init__(self, variable)
        self.gds_record = gds_record

    def read(self, instance, gen):
        rec = gen.current
        rec.check_tag(self.gds_record)
        rec.check_size(1)
        setattr(instance, self.variable, rec.data[0])
        gen.read_next()

    def save(self, instance, stream):
        record.Record(self.gds_record, (getattr(instance, self.variable),)).save(stream)

class SimpleOptionalRecord(SimpleRecord):
    def optional_read(self, instance, unused_gen, rec):
        """
        Called when optional tag is found. `rec` contains that tag.
        `gen` is advanced to next record befor calling this function.
        """
        rec.check_size(1)
        setattr(instance, self.variable, rec.data[0])

    def read(self, instance, gen):
        rec = gen.current
        if rec.tag == self.gds_record:
            gen.read_next()
            self.optional_read(instance, gen, rec)

    def save(self, instance, stream):
        data = getattr(instance, self.variable, None)
        if data is not None:
            record.Record(self.gds_record, (data,)).save(stream)

class OptionalWholeRecord(SimpleOptionalRecord):
    """Class for records that need to store all data (not data[0])."""
    def optional_read(self, instance, unused_gen, rec):
        setattr(instance, self.variable, rec.data)

    def save(self, instance, stream):
        data = getattr(instance, self.variable, None)
        if data is not None:
            record.Record(self.gds_record, data).save(stream)

class PropertiesRecord(AbstractRecord):
    def read(self, instance, gen):
        rec = gen.current
        props = []
        while rec.tag == tags.PROPATTR:
            rec.check_size(1)
            propattr = rec.data[0]
            rec = gen.read_next()
            rec.check_tag(tags.PROPVALUE)
            props.append((propattr, rec.data))
            rec = gen.read_next()
        setattr(instance, self.variable, props)

    def save(self, instance, stream):
        props = getattr(instance, self.variable)
        if props:
            for (propattr, propvalue) in props:
                record.Record(tags.PROPATTR, (propattr,)).save(stream)
                record.Record(tags.PROPVALUE, propvalue).save(stream)
.....
....

PS: der Link zur Erklärung GDS Format

http://boolean.klaasholwerda.nl/inte...gdsformat.html


GDS Dateien mit Delphi lesen : https://github.com/delphiEDA/gdsparser/

Geändert von bernhard_LA ( 5. Okt 2017 um 22:30 Uhr)
  Mit Zitat antworten Zitat