List and dictionaries - python-2.7

How do I assign values to items in a list in a dictionary method
menu_list = ['bread','butter','rice','sugar']
print(menu_list)
stock_dict = {'menu_list[0] == 2',
'menu_list[1] == 5',
'menu_list[2] == 0',
'menu_list[3] == 6',
}
print(stock_dict)
I was hoping that the items in my list would then have the stock value of the items in the dictionary, on top of this I also need to add a dictionary that will assign prices of items and then calculate the total

Related

Assign database value to Django variable

Looking to do something like this is Django:
$price = mysql_query("SELECT price FROM products WHERE product = '$product'");
$result = mysql_fetch_array($price);
If $result['price'] == 1:
do something
My thought was something like below but seems to be way off:
item = Order.objects.get(quote_number = quotex)
price = item.quote_status
headers = {
# Already added when you pass json= but not when you pass data=
# 'Content-Type': 'application/json',
'X-API-KEY': '',
}
messages.success(request, price)
if price == 2:
Price == 2 does not see the number 2 where messages 2 on price var
please note this is not being used to output to a template, as I already know how to do that.

Groovy how to find a value from an ArrayList in a Map

I have an ArrayList and a list of Maps.
def paint= ["blue", "green", "red"]
def cars = [[color:"black", year:2018, id: 1345], [color:"blue", year:2009, id: 1241], [color:"green", year:2009, id: 1525], [color:"yellow", year:2009, id: 1891]]
I want to search the List Map and find id's of the cars with the colors mentioned in the paint ArrayList and return an ArrayList like shown below
result [1241, 1525]
I have the below mentioned code however it only shows the result with the first mentioned color in the ArrayList.
def result = cars.findAll{it.color in paint}
def resultID = result.id.join(",")
This returns only
resultID [1241]

Using Animated Switcher, which are stored in a List

I want to have a Scaffold, which contains a variable amount of Elements in a ListView. The Elements are all equal, just a few values are different. So I made a Scaffold for those Elements and want to store them in a growable List. My Problem is, that the AnimatedSwitcher doesn't work anymore when I retrieve it from the List.
bool Boolean = false; <br/>
List<Widget> widgets = [ AnimatedSwitcher(<br/>duration: Duration(seconds: 2), <br/>child: Boolean ? childrenOne : childrenTwo) ]
body: ListView( <br/>
children: widgets
),

Return value in sales.order.line from wizard in Odoov10

I have a Many2many type field in wizard('pack_ids') and also a Many2many('pack_id') type field in sale.order.line. And i want that the value of Many2many type field of wizard('pack_ids') return in sale.order.line field('pack_id').
For this my code is here:
class SalePackWizard(models.TransientModel):
_name = "sale.pack.wizard"
_description = "Sale Pack Wizard"
#api.onchange('product_id')
def _onchange_product_pack_name(self):
print"A:", self.product_id.product_pack
res = self.product_id.product_pack
a = {}
print "res:", res
if res:
domain = {'pack_ids': [('id', 'in', [v.id for v in res])]}
a= res
print "a:", a
return {'domain': domain}
product_id = fields.Many2one('product.product', string="Product Pack", required=True, domain="[('is_pack','=',True)]")
qty = fields.Float(string='Quantity', digits=dp.get_precision('Product Unit of Measure'), required=True, default=1.0)
pack_ids = fields.Many2many('product.pack', string='Pack Products', change_default=True,
default=_onchange_product_pack_name)
#api.multi
def action_salepack_add(self):
rec = self._context.get('active_ids', [])
print "REC", rec, self.product_id.categ_id #product_uom
if rec:
line_values = {'product_id': self.product_id.id,
#'design_id':self.design_id.id,
'pack_id': self.product_id.product_pack,
'category_id':self.product_id.categ_id.id,
'order_id':rec[0],
'product_uom_qty':self.qty,
}
sale_order_line = self.env['sale.order.line'].create(line_values)
you can update your code by that:
#api.multi
def action_salepack_add(self):
order_id = self._context.get('active_id',False)
if order_id:
line_values = {'product_id': self.product_id.id,
'pack_id': [ ( 6, 0, [self.product_id.product_pack.id] ) ],
'category_id':self.product_id.categ_id.id,
'order_id':order_id,
'product_uom_qty':self.qty,
}
sale_order_line = self.env['sale.order.line'].create(line_values)
You cant create values in a many2many field just giving it the id (this is only for many2one). If the field is a one2many or many2many:
(0, 0, { values }) link to a new record that needs to be created with the given values dictionary
(1, ID, { values }) update the linked record with id = ID (write values on it)
(2, ID) remove and delete the linked record with id = ID (calls unlink on ID, that will delete the object completely, and the link to it as well)
(3, ID) cut the link to the linked record with id = ID (delete the relationship between the two objects but does not delete the target object itself)
(4, ID) link to existing record with id = ID (adds a relationship)
(5) unlink all (like using (3,ID) for all linked records)
(6, 0, [IDs]) replace the list of linked IDs (like using (5) then (4,ID) for each ID in the list of IDs)
Error Fixed. Here is the solution for error:
#api.multi
def action_salepack_add(self):
rec = self._context.get('active_ids', [])
print "REC", rec, self.product_id.categ_id #product_uom
if rec:
line_values = {'product_id': self.product_id.id,
#'design_id':self.design_id.id,
'pack_id': [(6, 0, [v.id for v in self.product_id.product_pack])],
'category_id':self.product_id.categ_id.id,
'order_id':rec[0],
'product_uom_qty':self.qty,
}
sale_order_line = self.env['sale.order.line'].create(line_values)
Thanks,

DRYing up template code in Meteor?

I'm noticing significant code duplication while building my first Meteor project, and I'm wondering if there's a way to DRY it up.
My database model has stores, each of which have a number of products, and a field with the amount currently in inventory.
var store_id = Store.insert({name: 'Store 1', max_items: 50});
var p1 = Product.insert({name: 'General', store_id: store_id, item_count: 20});
var p2 = Product.insert({name: 'Special', store_id: store_id, item_count: 10});
I have a template to display a store, and statistics on how many products and items it has.
<template name="store">
<div class="store">
<b>{{name}}</b>
<p>Current items: {{current_items}}</p>
<p>Maximum # of items allowed in inventory: {{max_items}}</p>
<p>% Full: {{percent_full}}%</p>
</div>
</template>
Calculating the number of current items seems fairly straightforward, I pull all the products, sum the item counts (using d3), return the result.
Template.store.current_items = function () {
var store_id = this._id;
var items = Product.find({store_id: store_id}).fetch();
if ( items.length > 0 ) {
var item_cnt = d3.sum(_.pluck(items, 'item_count'));
return item_cnt;
}
else {
return 'N/A';
}
};
To calculate a percentage comparing the total # of allowed items, and the current items, it seems like I have to reduplicate everything. Is there a better way to do this?
Template.store.percent_full = function () {
var store_id = this._id;
var items = Product.find({store_id: store_id}).fetch();
if ( items.length > 0 ) {
var item_cnt = d3.sum(_.pluck(items, 'item_count'));
return item_cnt / max_items * 100;
}
else {
return 'N/A';
}
};
Extract the duplicated logic to a separate function and just call it appropriately from different helpers. This is not really different from DRYing any other JS code.