Working on lists in python
'is trying to do a small modification to the django lfs project that will allow me to deactivate products without stock. Unfortunately, I'm just starting to learn python, so I'm having big problems with its syntax. This is what I am trying to do. I am using is_variant method which returns true if my product is a subtype. If it's a variant, I go to the parent product, get its active variants, and check their inventory. If the stock value is greater than 0, the active variable is 0, otherwise it is 1. If after the "active" loop of the options it is still 1, I set the parent product to false.
I somehow can't get it to work properly. Using:
def deactivate(self):
if self.is_variant():
prod = self.parent
prod.active = all(var.get_stock_amount() != 0 for var in prod.variants.filter(active=True))
else:
prod.active = self.get_stock_amount() != 0
self.parent.save()
It deactivates my product regardless of whether it has stock options or not. And when using:
inactive = 0
if self.is_variant():
prod = self.parent
for s in prod.variants.filter(active=True):
if s.get_stock_amount() == 0:
inactive = 1
else:
inactive = 0
if inactive == 1:
prod.active = 0
prod.save()
else:
if self.get_stock_amount() == 0:
self.active = 0
self.save()
The same thing happens, so my product gets deactivated every time.
I have checked the return types in the wrapper and myself is the option and it is active.
source to share
The right decision. Guess there is still a lot of room to optimize it, but first I need to find out how :):
def deactivate(self):
"""If there are no stocks, deactivate the product. Used in last step of checkout.
"""
inactive = False
if self.is_variant():
prod = self.parent
inactive = all(var.get_stock_amount() == 0 for var in prod.variants.filter(active=True))
if inactive:
prod.active = 0
prod.save()
else:
if self.get_stock_amount() == 0:
self.active = 0
self.save()
source to share
Firstly, I would not name a list set
, because it is a built-in Python method (see set
), Use append
in a list (your syntax is just incorrect and an error that you explicitly state); you need to initialize the list before:
def deactivate(self):
"""If there are no stocks, deactivate the product. Used in last step of checkout.
"""
if self.has_variants():
sets = []
for s in self.variants.filter(active=True):
sets.append(s)
for var in sets:
...
But why create a list in advance if the only goal is to repeat it again? You can simply:
def deactivate(self):
"""If there are no stocks, deactivate the product. Used in last step of checkout.
"""
if self.has_variants():
for s in self.variants.filter(active=True):
if s.get_stock_amount() == 0:
inactive = True
else:
inactive = False
else:
...
Learn more about lists .
source to share
This code is not so different.
- Create a named list
set
(as above) - Setting a variable multiple times in a loop without reading
- Raw return value (if there is only one exit point, how can this be useful?)
I think this will work just as well:
def deactivate(self):
"""If there are no stocks, deactivate the product. Used in last step of checkout.
"""
if self.has_variants():
inactive = any(var.get_stock_amount() == 0 for var in self.variants.filter(active=True))
else:
inactive = self.get_stock_amount() == 0
self.active = not inactive
or perhaps:
def deactivate(self):
"""If there are no stocks, deactivate the product. Used in last step of checkout.
"""
if self.has_variants():
self.active = all(var.get_stock_amount() != 0 for var in self.variants.filter(active=True))
else:
self.active = self.get_stock_amount() != 0
source to share