Reputation: 23
I'm trying to create a virtual model of a production plant. For that I created the following structure:
My problem is that the set_part method I created to fill each workshop with the corresponding parts doesn't seem to work.
I have placed some print statements here and there to check what was going on, and it seems that the objects are created correctly inside the loader class, but they don't get out of there.
This is the code:
class Phase:
def __init__(self, seq, workshop, placings):
self.seq = seq
self.workshop = workshop
self.placings = placings
class Part:
def __init__(self, name, phases, workshop):
self.name = name
self.phases = phases
self.workshop = workshop
class Workshop:
def __init__(self, name, shifts, shift_dur):
self.name = name
self.shifts = shifts
self.shift_dur = shift_dur
self.parts = []
self.automation = []
print('parts inside workshop ', self.name, ':', self.parts)
#this should assign the level of automation to the workshop
automation = []
for part in self.parts:
for phase in part.phases:
if phase.placings > 0:
automation.append('auto')
else:
automation.append('manual')
self.automation = automation
def set_part(self, part):
self.parts.append(part)
class Loader:
def __init__(self, cycles_file, workshops_file):
self.cycles_file = cycles_file
self.workshops_file = workshops_file
self.workshops = []
# those are some nested cycles that I use to create part objects with the same workshops
parts = []
for sheet in self.cycles_file:
name = sheet
phases = []
workshops_set = set()
for phase in self.cycles_file[sheet]:
seq = phase['seq']
workshop = phase['workshop']
placings = phase['placings']
workshops_set.add(workshop)
phase = Phase(seq, workshop, placings)
phases.append(phase)
for workshop in workshops_set:
same_workshops_phases = []
for phase in phases:
if workshop == phase.workshop:
same_workshops_phases.append(phase)
part = Part(name, same_workshops_phases, workshop)
parts.append(part)
# creates workshop obj
for workshop in self.workshops_file:
name = workshop
shifts = self.workshops_file[workshop]['Shifts']
shift_dur = self.workshops_file[workshop]['Shift duration']
workshop = Workshop(name, shifts, shift_dur)
self.workshops.append(workshop)
# setting parts for workshops
for part in parts:
for workshop in self.workshops:
if workshop.name == part.workshop:
workshop.set_part(part)
for workshop in self.workshops:
print('workshop: ', workshop.name)
print('level of automation: ', workshop.automation)
print('parts processed by workshop:')
for part in workshop.parts:
print(part.name)
print('with phases:')
for phase in part.phases:
print(phase.seq)
There is an indentation error in the first line of class Loader, but it's just a copy/paste issue. Then I create the objects:
cycles_file = {'p1': [{'seq': 10, 'workshop': 'ws1', 'placings': 2},
{'seq': 20, 'workshop': 'ws2', 'placings': 0}],
'p2': [{'seq': 10, 'workshop': 'ws3', 'placings': 0}],
'p3': [{'seq': 10, 'workshop': 'ws2', 'placings': 0},
{'seq': 20, 'workshop': 'ws1', 'placings': 3},
{'seq': 30, 'workshop': 'ws3', 'placings': 0}]}
workshops_file = {'ws1': {'Shifts': 3, 'Shift duration': 8},
'ws2': {'Shifts': 2, 'Shift duration': 7.5},
'ws3': {'Shifts': 2, 'Shift duration': 7.5}}
loader = Loader(cycles_file, workshops_file)
this is the result I get:
parts inside workshop ws1 : []
parts inside workshop ws2 : []
parts inside workshop ws3 : []
workshop: ws1
level of automation: []
parts processed by workshop:
p1 with phases: 10
p3 with phases: 20
workshop: ws2
level of automation: []
parts processed by workshop:
p1 with phases: 20
p3 with phases: 10
workshop: ws3
level of automation: []
parts processed by workshop:
p2 with phases: 10
p3 with phases: 30
As you see, the parts inside workshops are empty and so the automation level can't be set...
Can somebody help me? I'm a self learner and I feel really lost!
Upvotes: 0
Views: 67
Reputation: 917
Your code is fine, print is misleading you.
In class Workshop, pay attention here:
class Workshop:
def __init__(self, name, shifts, shift_dur):
self.parts = []
self.automation = []
print('parts inside workshop ', self.name, ':', self.parts)
You are printing parts inside workshops before set_part
method runs.
As I mentioned before your code is fine, the parts inside workshops are NOT empty, just test it with:
for workshop in loader.workshops:
print('parts inside workshop ', workshop.name, ':', workshop.parts)
Upvotes: 1