Reputation: 838
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
Upvotes: 0
Views: 146
Reputation: 4473
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),]
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.
Upvotes: 1
Reputation: 3399
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
Upvotes: 0