-2

Currently I am porting a pascal program to python, and there I have the following code:

ScanList  = Record
                            name    : string;
                            I       : Integer;
                            Lower,
                            Upper   : Array [1..20] of real;
                            step    : Array [1..20] of real;
                            counts  : Array [1..20] of Integer;
                           Pol     : Array [1..20] of Integer;
                            AverageNo: Array[1..20] of Integer;
                            Average : Array [1..20] of AveMode;

                            selected: Array [1..20] of Boolean;
                            saved,
                            loaded,

                            altered : Boolean;

                        end;

I already rewrote most of it into python, thus it looks now like this:

ScanList  = Record
        name = "Hello"
        I = 0
        Lower = [0 for i in range(20)]
        Upper = [0 for i in range(20)]
        step  = [0 for i in range(20)]
        counts = [0 for i in range(20)]
        Pol   = [0 for i in range(20)]
        AverageNo = [0 for i in range(20)]
        Average  = [0 for i in range(20)]

        selected = [0 for i in range(20)]
        saved = True
        loaded = True

        altered = True
    end

But now my problem is, that I want to have it as a struct (as in the old pascal code). I already have seen this question: C-like structures in Python, but in this question I do not have arrays, but in mine I have. How can I port the code above into a struct?
Thank you!

arc_lupus
  • 3,942
  • 5
  • 45
  • 81
  • read this you can https://docs.python.org/2/library/struct.html#format-characters – sundar nataraj Sep 10 '14 at 12:12
  • 3
    I don't really understand how your rewritten code is supposed to be Python. – Marius Sep 10 '14 at 12:15
  • @Marius: Where are the problems? My python is already a bit rusty, and as I have already stated it is not completely ported... – arc_lupus Sep 10 '14 at 12:18
  • @arc_lupus: Even if you can hack together something that superficially looks like a Pascal struct, there's absolutely no guarantee you'll be able to interact with it in the same way as the Pascal code you have does. Think about what your data means and how best to represent that with a Python data structure. – Marius Sep 10 '14 at 12:28
  • The code shown does not specify packing, so it is not a complete description of the binary representation of the record. Also the "string" size, and the size of enums is compiler (and -setting) dependent. – Marco van de Voort Sep 10 '14 at 13:01
  • @Marius: I do not want to have something which looks like a pascal struct, it should work like one. And after I already have stated above, I first wanted to convert the inner part of the struct (because I know how, and it is already done) and then pack everything into a struct (what has not happened till now). – arc_lupus Sep 10 '14 at 13:47
  • 1
    @arc_lupus I'd appreciate it if you could tell me why the code below doesn't satisfy you, so perhaps I could help. – Korem Sep 10 '14 at 13:50
  • @Korem: I had no possibility to test it till now, but I did't downvote you (and as soon as I have tested it I will tell you). – arc_lupus Sep 10 '14 at 13:53
  • @arc_lupus I don't care about the downvote, wasn't asking because of that. – Korem Sep 10 '14 at 14:08
  • Does this answer your question? [C-like structures in Python](https://stackoverflow.com/questions/35988/c-like-structures-in-python) – questionto42 Nov 28 '21 at 13:44

2 Answers2

1

This is a script to convert a dictionary to a structure:

class dict2struct:
    """
    Creates a structure from a dictionary.
    """
    def __init__(self, dict):
        for key in dict:
            val = dict[key]
            exec('self.' + key + '=val')
itsikw
  • 11
  • 3
0

Similar to another answer in the question you've posted

class Record:
    def __init__(self, **kwargs):
        self.__dict__.update(kwargs)

scanlist = Record(
        name = "Hello",
        I = 0,
        Lower = [0 for i in range(20)],
        ...,
        loaded = true,
        altered = true)

Please note some differences from your code - there's no end in python; arguments passed to the "struct" are separated by a comma; it's not customary to have capitalized var names in python.

Community
  • 1
  • 1
Korem
  • 11,383
  • 7
  • 55
  • 72
  • Works, it was probably downvoted because it is just the answer of [C-like structures in Python](https://stackoverflow.com/a/35993/11154841) again, and although the OP mentions this link, it can be doubted that this Q/A here is not a full duplicate of the Q/A there. Also from that link: check the new solution with `from dataclasses import dataclass` instead. – questionto42 Nov 28 '21 at 13:40