Why do I keep getting this type of error? - python-2.7

nodes = data['osm']['node']
nodes = list(filter(lambda node: 'tag' in node and \
len( list(filter(lambda tag: tag["_k"] == "name" \
and tag["_v"].startswith("node") , node['tag'])) ) > 0, nodes ) )
nodes
KeyError data['osm']
I am trying to filter the longitude and latitude from a location on osm.

Related

Django: How to take my query out of my for loop

I got the following code, that contains N queries:
for qty in total_qty_bought:
product_id = qty["product"]
quantity = int(qty["quantity__sum"])
try:
method_title = (
self.shipment_model.get(order_id=qty["order_id"])
.method_title.replace("Hent-selv", "")
.strip()
)
To solve the issue I tried to take the method_title query out of the for loop like this:
quantity = 0
for qty in total_qty_bought:
quantity = int(qty["quantity__sum"])
method_title = (
self.shipment_model.get(order_id=total_qty_bought[0]['order_id'])
.method_title.replace("Hent-selv", "")
.strip()
)
Note! There will be a full refrence further down, to understand the bigger picture
The issue in my solution is, that I am hard choosing which dict to enter , as I select [0] before order_id, and not in a for loop like before, would be selecting every individual item in the loop.
Is there a more sufficient way to do this? I do not see a solution without the for loop, but django debugger tool tells me it creates 2k+ queries.
CODE FOR REFRENCE
class InventoryStatusView(LoginRequiredMixin, View):
template_name = "lager-status.html"
cinnamon_form = CinnamonForm(prefix="cinnamon_form")
peber_form = PeberForm(prefix="peber_form")
pc_model = InventoryStatus
product_model = Product.objects.all()
order_item_model = WCOrderItem.objects.all()
shipment_model = WCOrderShipment.objects.all()
def get(self, request):
# Get all added objects that hasn't been deleted
objects = self.pc_model.objects.filter(is_deleted=False)
# Get all added objects that has been deleted
deleted_objects = self.pc_model.objects.filter(is_deleted=True)
# Sum all cinnamon that isn't deleted
total_cinnamon = (
self.pc_model.objects.filter(is_deleted=False)
.aggregate(Sum("cinnamon"))
.get("cinnamon__sum", 0.00)
)
# Sum all peber that isn't deleted
total_peber = (
self.pc_model.objects.filter(is_deleted=False)
.aggregate(Sum("peber"))
.get("peber__sum", 0.00)
)
# Get the amount of kilo attached to products
product_data = {}
queryset = ProductSpy.objects.select_related('product')
for productSpy in queryset:
product_data[productSpy.product.product_id] = productSpy.kilo
# Get quantity bought of each product
total_qty_bought = self.order_item_model.values(
"order_id", "product"
).annotate(Sum("quantity"))
# Get the cities from the inventory model
cities = dict(self.pc_model.CITIES)
# Set our total dict for later reference
our_total = {}
product = Product.objects.filter(
product_id__in={qty['product'] for qty in total_qty_bought}
).first()
# Check if we deal with kanel or peber as a product based on slug
index = 0
if product.slug.startswith("kanel-"):
index = 0
elif product.slug.startswith("peber-"):
index = 1
else:
pass
try:
# Sum the total quantity bought
quantity = 0
for qty in total_qty_bought:
quantity = int(qty["quantity__sum"])
# Get the inventory the order is picked from based on shipment method title
method_title = (
self.shipment_model.get(order_id=total_qty_bought[0]['order_id']) # The error
.method_title.replace("Hent-selv", "")
.strip()
)
# If the order isn't picked, but sent, use this inventory
if method_title not in cities.values():
method_title = "Hovedlager"
try:
# Get the total of kanel and peber bought
kilos = quantity * product_data[product.id]
# If there is no inventory, set it to 0 peber and 0 kanel
if method_title not in our_total:
our_total[method_title] = [0, 0]
# Combine place and kilos
our_total[method_title][index] += kilos
except KeyError as ex:
print(ex)
pass
except WCOrderShipment.DoesNotExist as ed:
print(ed)
pass
# Quantities BOUGHT! (in orders!)
print(our_total)
context = {
"cinnamon_form": self.cinnamon_form,
"peber_form": self.peber_form,
"objects": objects,
"deleted_objects": deleted_objects,
"total_cinnamon": total_cinnamon,
"total_peber": total_peber,
"our_total": our_total,
}
return render(request, self.template_name, context)
You can only do one query by using the __in operator:
shipments = self.shipment_model.get(order_id__in=list_containing_order_ids)
Then you can do a normal for loop in which you verify that condition.

Extracting edge data from a networkx graph

I need to extract out the edge information for each graph displayed. The data is in utf-8 form.
The graphs are displayed for each sentence in the document. So now the information from the graph has to be extracted out. The sentences to be extracted out won't be the same as some sentences are merged in the graph. The image explains the output which has some graphs (each graph has a maximum of three nodes)
for s in subject_list:
if s is not "":
graph.add_node(s)
labels[s] = s
for o in object_list:
if o is not "":
graph.add_node(o)
labels[b] = b
for v in verb_list:
if v is not "":
graph.add_node(v)
labels[v] = v
for (s, o, v) in zip(subject_list, object_list, verb_list):
if s and o is not "":
graph.add_edge(s, o)
if o and v is not "":
graph.add_edge(o, v)
pos=nx.spring_layout(graph)
nx.draw(graph, with_labels = True, font_family = "Nirmala UI", node_size = 40, font_size = 9 ,node_color = "darkblue")
pl.show()
g=[]
for component in nx.connected_components(graph):
# Each component is the set of nodes
#print(component)
# Filter all edges in graph: we keep only that are in the component
g=(list(
filter(
lambda x: x[0] in component and x[1] in component,
graph.edges
)
))
print g
ls=[]
for x in g[0]:
ls.append(x)
print (x)
![connected components output][1]
[1]: https://i.stack.imgur.com/iynw1.png
You need the connected_components function:
for component in nx.connected_components(graph):
# Each component is the set of nodes
print(component)
# Filter all edges in graph: we keep only that are in the component
print(list(
filter(
lambda x: x[0] in component and x[1] in component,
graph.edges
)
))

#SQLite This query statement does not work as I thought. What should I do

const static char* QUERY_UPDATE_ROUTE_ADDRESS = "UPDATE [PRouteAddrTable] SET [addressLanguage] = ?, [iso] = ?, " \
"[country] = ? , [state] = ?, [city] = ? , [cityCenter] = ?, " \
"[street] = ? , [street2] = ?, [houseNo] = ? , [zip] = ? " \
"WHERE RouteID = ? AND WptUseType = ? AND Lon = ? AND Lat = ?";
When this query is executed, the table is empty. So I think the table should be empty even after executing the query statement. But there is one record. I seem to be thinking wrong.
Please Help me

Index out of range, only inside function python

I really don't understand what is happening here. I have a function:
def get_appliance_status(appliance):
appliance_data = None
status_color = SENSOR_STATUS_COLOR_UNKNOWN
status_message = 'No Appliance Data Available'
try:
appliance_data = ApplianceData.objects.filter(appliance=appliance).latest('timestamp')
except Exception as ex:
print ex
else:
if appliance_data:
status_color = SENSOR_STATUS_COLOR_OK
status_message = 'Appliance Temperature and Pressure values are within designed ranges.'
temperature_status = ''
pressure_status = ''
# status of inside box temp and food temps 1-4
if appliance_data.temperature_4_f > appliance.max_temperature or \
appliance_data.temperature_5_f > appliance.max_temperature or \
appliance_data.temperature_6_f > appliance.max_temperature or \
appliance_data.temperature_7_f > appliance.max_temperature or \
appliance_data.temperature_8_f > appliance.max_temperature:
status_color = SENSOR_STATUS_COLOR_WARN
temperature_status = 'Appliance Temperature is outside designed ranges. '
status_message = temperature_status
if est_obj > timestamp + timedelta(minutes=15) or \
est_obj > timestamp + timedelta(minutes=15) and \
appliance_data.temperature_4_f > appliance.max_temperature:
status_color = SENSOR_STATUS_COLOR_CRITICAL
status_message = "Polar Board Has Lost Connection/Power Failure, Critical"
#textclient.text(textclient.Brian, "Polar Board has lost connection" + " " + appliance.name)
if appliance_data.power_1_amps > 0 and \
appliance_data.power_2_amps == 0:
status_color = SENSOR_STATUS_COLOR_EMERGENCY
status_message = "Condenser Motor Fail-Emergency"
if appliance_data.power_1_amps == 0 and \
appliance_data.power_2_amps > 0:
status_color = SENSOR_STATUS_COLOR_EMERGENCY
status_message = "Compressor Motor Fail-Emergency"
if appliance_data.power_1_amps == 0 and \
appliance_data.power_2_amps == 0 and \
appliance_data.power_3_amps == 0 and \
appliance_data.power_4_amps == 0:
status_color = SENSOR_STATUS_COLOR_EMERGENCY
status_message = "Power failure- Emergency"
if appliance_data.pressure_1_psi < 8 and \
appliance_data.pressure_2_psi < 7 and \
appliance_data.power_1_amps == 0 and \
appliance_data.power_2_amps == 0 and \
appliance_data.power_3_amps > 0 and \
appliance_data.power_4_amps > 0:
status_color = SENSOR_STATUS_COLOR_EMERGENCY
status_message = "No Pressure, Condenser/Compressor failure, Emergency"
if appliance_data.pressure_1_psi > appliance.max_pressure_1 or \
appliance_data.pressure_1_psi < appliance.min_pressure_1 or \
appliance_data.pressure_2_psi > appliance.max_pressure_2 or \
appliance_data.pressure_2_psi < appliance.min_pressure_2:
status_color = SENSOR_STATUS_COLOR_WARN
status_message = ''
pressure_status = 'Appliance Pressure is outside designed ranges.'
status_message = ' '.join((temperature_status, pressure_status))
if appliance_data.power_3_amps == 0 and \
appliance_data.power_4_amps > 0:
status_color = SENSOR_STATUS_COLOR_CRITICAL
status_message = "Evaporator Fan 1 Failure, Critical"
if appliance_data.power_3_amps > 0 and \
appliance_data.power_4_amps == 0:
status_color = SENSOR_STATUS_COLOR_CRITICAL
status_message = "Evaporator Fan 2 Failure, Critical "
for item in my_appliance:
if item.temperature_4_f > appliance.max_temperature:
above_temp.append(item.timestamp.astimezone(local_tz).strftime('%H:%M'))
else:
break
print(above_temp)
return '<span class="status" style="background-color:' + status_color +';" title="' + status_message + '"></span>'
The problem lies here:
above_temp=[]
for item in my_appliance:
if item.temperature_4_f > appliance.max_temperature:
above_temp.append(item.timestamp.astimezone(local_tz)
.strftime('%H:%M'))
else:
break
print(above_temp)
If I try print above_temp[0], I get an out of range error. I can access the first element with above_temp[0:1]. However, this returns a list of this element, this is not what I want. When I print(above_temp) I get an [], then after that I get the list that I want. I really want the first and last element of this list. The last element with change in index, based on len of list. If I run this same code outside of the function, it works as expected. I need it to run inside this function though. Why does it initially return an empty list and why can't I just use indexing to access elements inside this function?
Here is what I get when I run this:
[]
['11:17', '11:14', '11:12', '11:10', '11:08', '11:06', '11:04', '11:02',
'11:00', '10:58', '10:56', '10:53', '10:51', '10:49', '10:47', '10:45',
'10:43', '10:41', '10:39', '10:37', '10:35', '10:32', '10:30', '10:28',
'10:26', '10:24', '10:22', '10:20', '10:18', '10:16', '10:14', '10:11',
'10:09', '10:07', '10:05', '10:03', '10:01', '09:59', '09:57', '09:55',
'09:53', '09:50', '09:48', '09:46', '09:44', '09:42', '09:40', '09:38',
'09:36', '09:34', '09:32', '09:29']
List slicing is more lenient - it even works if the list does not entail the elements you are asking from it, it will return an empty list [] in that case.
Indexing into a list at an non-present location will return an Error.
Reproducable by:
tata = []
print tata[0:200]
try:
print tata[0]
except IndexError as idxerr:
print idxerr
Output:
[]
list index out of range
See Introduction in the Section about Strings:
word = 'Python'
Attempting to use an index that is too large will result in an error:
word[42] # the word only has 6 characters Traceback (most recent call last):
File "<stdin>", line 1, in <module> IndexError: string
index out of range
However, out of range slice indexes are handled
gracefully when used for slicing:
word[4:42] 'on'
word[42:] ''
If you need to acces the first and last element of a list you might consider doing this:
firstVal, lastVal = above_temp[0:1],above_temp[-1:] # both wont throw error, but
# might be [] afterwards
Demo:
tata = [2,3]
firstVal, lastVal = tata[0:1],tata[-1:]
print firstVal , lastVal
tata = []
firstVal, lastVal = tata[0:1],tata[-1:]
print firstVal , lastVal
Output:
[2] [3]
[] []

Django conditional annotation: Queryset return empty if `When` condition return empty

I have three model Flat, FlatDebit and FlatDebitType
class Flat(models.Model):
number = models.IntegerField()
class FlatDebit(models.Model):
flat = models.ForeingKey('foo.Flat', related_name='debits')
debit_type = models.ForeingKey('foo.FlatDebitType')
unpaid_amount = models.DecimalField()
class FlatDebitType(models.Model):
TYPE1 = 1
TYPE2 = 2
TYPE_CHOICES = ((TYPE1, 'Type 1'), (TYPE2, 'Type 2'),)
type = models.PositiveSmallIntegerField(default=TYPE1, choices=TYPE_CHOICES)
I want to get sum of unpaid_amount of some debits of each flat by debit_type.
Following annotation works as expected if there is debits which has FlatDebitType which has TYPE2.
flats = Flat.objects.all()
# <QuerySet [<Flat: 1>,..]
flats = flats.annotate(
# type1_unpaid_amount=Sum(....),
type2_unpaid_amount=Sum(Case(
# If there is no debit which has debit_type=TYPE2
# `flats` queryset return empty
When(debits__debit_type__type=FlatDebitType.TYPE2,
then='debits__unpaid_amount'
),
output_field=models.DecimalField(),
# I specified default value for getting 0 as value
# if debits with TYPE1 but not affecting
default=Decimal('0.00')
)),
)
But if there is no debits which specified debit_type, queryset returns empty.
print(flats)
#<QuerySet []>
I'm sorry for my bad English.