0

I'm new to Python so the answer to this question is probably easy. I've done some coding in it before, but I forgot most of it.

I have an list of values, that I get from a file like this:

podela = fobj.read().split()

There are 10 values that as a whole represent some data. These 10 values repeat N times. So, Nx10 values all together. Only some values (4 out of 10) are used to create an instance of the following class.

I have a class that represents time:

class Vreme:
    day = 0
    hour = 0
    minute = 0
    second = 0

    def __init__(self, day, hour, minute, second):
        self.day = day
        self.hour = hour
        self.minute = minute
        self.second = second
    def __init__(self):
        self.day = 0
        self.hour = 0
        self.minute = 0
        self.second = 0

    def toString(self):
        s = repr(self.day)+'-'+repr(self.hour)+':'+repr(self.minute)+':'+repr(self.second)
        return s

    def print(self):
        s = self.toString()
        print(s)

How can I instantiate a list of instances of Vreme using the list podela?

EDIT1: podela holds the values, which are hex representations in a string format.

Part of the file example, 4xlines:

4a 02 f6 01 00 04 0e 08 03 00
4a 02 f6 01 00 04 0e 08 04 00
49 02 f6 01 00 04 0e 08 04 00
49 02 f5 01 00 04 0e 08 04 00
4a 02 f6 01 00 04 0e 08 05 00

podela after split, few last values:

'0e', '12', '1f', '00', '49', '02', 'f4', '01', '00', '04', '0e', '12', '20', '00', '49', '02', 'f5', '01', '00', '04', '0e', '12', '20', '00', '48', '02', 'f4', '01', '00', '04', '0e', '12', '20', '00', '4a', '02', 'f4', '01', '00', '04', '0e', '12', '21', '00', '49', '02', 'f5', '01', '00', '04', '0e', '12', '21', '00', '4a', '02', 'f5', '01', '00', '04', '0e', '12', '22', '00', '49', '02', 'f5', '01', '00', '04', '0e', '12', '22', '00', '48', '02', 'f4', '01', '00', '04', '0e', '12', '22', '00', '4a', '02', 'f6', '01', '00', '04', '0e', '12', '23', '00', '48', '02', 'f4', '01', '00', '04', '0e', '12', '23', '00']

Input file (that gets split for podela) is 56kB, but this is a test file. Real files will be 5MB+

EDIT2: Example of input and output Example is with a really small file, that holds 3x10 values.

The file holds these values:

00 00 00 00 00 00 00 00 00 00
01 02 03 04 05 06 07 08 09 10
11 11 11 11 11 11 11 11 11 11

When podela is made, each of these values is a member of an array

podela = [00 00 00 00 00 00 00 00 00 00 01 02 03 04 05 06 07 08 09 10 11 11 11 11 11 11 11 11 11 11]

Lets say that I use values at positions 0, 2, 4 and 8 for a constructor. vreme should be a list of 3 instances of Vreme:

vreme[0] = 00 00 00 00
vreme[1] = 01 03 05 07
vreme[2] = 11 11 11 11
4
  • 1
    What does podela look like? Post a sample please. Commented May 10, 2018 at 13:06
  • 1
    why do you have two __init__ methods? Commented May 10, 2018 at 13:07
  • so what part of your example represents days, minutes, etc.? Commented May 10, 2018 at 13:20
  • @IvanVinogradov I have two inits, cause I don't know python. I was trying different stuff, and that is from that time. As I see, i need only one constructor. Values are at positions 6, 7, 8 and 9 (day, hour, minute, second). For example, for first data set of 10 values, i need to extract 04 0e 08 03 Commented May 10, 2018 at 13:27

2 Answers 2

1

Maybe you can just append them in a for loop

vreme_instances = []
for i in podela:
    vreme_instances.append(Vreme(i[0], i[2], i[4], i[8]))  
    # change indexes at the line above as you need

assuming that podela is the list of tuples like this:

podela = [(4, 12, 32, 54), (16, 20, 12, 32)]
# [(day, hour, minute, second),]

Edit

To achieve the above result, first change this

podela = fobj.read().split()

to this

lines = fobj.read().splitlines()     # first: split input file into lines
podela = [x.split() for x in lines]  # second: split every line to values

That will give you a list of N sublists:

[
    ['4a', '02', 'f6', '01', '00', '04', '0e', '08', '03', '00'], 
    ['4a', '02', 'f6', '01', '00', '04', '0e', '08', '04', '00'], 
    ['49', '02', 'f6', '01', '00', '04', '0e', '08', '04', '00'], 
    ['49', '02', 'f5', '01', '00', '04', '0e', '08', '04', '00'],
]

Each sublist represents a Vreme instance.

Sign up to request clarification or add additional context in comments.

2 Comments

Hmmm i guess i can. I though there is a "one-line" solution. I know that I can do it with indexes, but that didn't really sound "python-like" to me. On the other hand, I have little to no clue how to work in Python, so I'm probably wrong
Thank you very much, the exact thing I needed
0

First, I recommend consolidating your two init functions into a single one like so:

class Vreme:
    day = 0
    hour = 0
    minute = 0
    second = 0

    def __init__(self, day=0, hour=0, minute=0, second=0):
        self.day = day
        self.hour = hour
        self.minute = minute
        self.second = second

    def toString(self):
        s = repr(self.day)+'-'+repr(self.hour)+':'+repr(self.minute)+':'+repr(self.second)
        return s

    def print(self):
        s = self.toString()
        print(s)

Then you can instantiate it as:

vreme = Vreme(*podela[:4])

assuming you want the first 4 elements of podela

Example:

podela = [1, 2, 3, 4, 5, 6]
vreme = Vreme(*podela[:4])

print(vreme.day)
# 1

print(vreme.hour)
# 2

print(vreme.minute)
# 3

print(vreme.second)
# 4

1 Comment

How can I get a list of vreme? I'll edit the question to include example of input/output

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.