Best Python code snippet using fMBT_python
subscribeoptions.py
Source:subscribeoptions.py
1"""2*******************************************************************3 Copyright (c) 2017, 2019 IBM Corp.4 All rights reserved. This program and the accompanying materials5 are made available under the terms of the Eclipse Public License v1.06 and Eclipse Distribution License v1.0 which accompany this distribution.7 The Eclipse Public License is available at8 http://www.eclipse.org/legal/epl-v10.html9 and the Eclipse Distribution License is available at10 http://www.eclipse.org/org/documents/edl-v10.php.11 Contributors:12 Ian Craggs - initial implementation and/or documentation13*******************************************************************14"""15import sys16class MQTTException(Exception):17 pass18class SubscribeOptions(object):19 """The MQTT v5.0 subscribe options class.20 The options are:21 qos: As in MQTT v3.1.1.22 noLocal: True or False. If set to True, the subscriber will not receive its own publications.23 retainAsPublished: True or False. If set to True, the retain flag on received publications will be as set24 by the publisher.25 retainHandling: RETAIN_SEND_ON_SUBSCRIBE, RETAIN_SEND_IF_NEW_SUB or RETAIN_DO_NOT_SEND26 Controls when the broker should send retained messages:27 - RETAIN_SEND_ON_SUBSCRIBE: on any successful subscribe request28 - RETAIN_SEND_IF_NEW_SUB: only if the subscribe request is new29 - RETAIN_DO_NOT_SEND: never send retained messages30 """31 # retain handling options32 RETAIN_SEND_ON_SUBSCRIBE, RETAIN_SEND_IF_NEW_SUB, RETAIN_DO_NOT_SEND = range(33 0, 3)34 def __init__(self, qos=0, noLocal=False, retainAsPublished=False, retainHandling=RETAIN_SEND_ON_SUBSCRIBE):35 """36 qos: 0, 1 or 2. 0 is the default.37 noLocal: True or False. False is the default and corresponds to MQTT v3.1.1 behavior.38 retainAsPublished: True or False. False is the default and corresponds to MQTT v3.1.1 behavior.39 retainHandling: RETAIN_SEND_ON_SUBSCRIBE, RETAIN_SEND_IF_NEW_SUB or RETAIN_DO_NOT_SEND40 RETAIN_SEND_ON_SUBSCRIBE is the default and corresponds to MQTT v3.1.1 behavior.41 """42 object.__setattr__(self, "names",43 ["QoS", "noLocal", "retainAsPublished", "retainHandling"])44 self.QoS = qos # bits 0,145 self.noLocal = noLocal # bit 246 self.retainAsPublished = retainAsPublished # bit 347 self.retainHandling = retainHandling # bits 4 and 5: 0, 1 or 248 assert self.QoS in [0, 1, 2]49 assert self.retainHandling in [50 0, 1, 2], "Retain handling should be 0, 1 or 2"51 def __setattr__(self, name, value):52 if name not in self.names:53 raise MQTTException(54 name + " Attribute name must be one of "+str(self.names))55 object.__setattr__(self, name, value)56 def pack(self):57 assert self.QoS in [0, 1, 2]58 assert self.retainHandling in [59 0, 1, 2], "Retain handling should be 0, 1 or 2"60 noLocal = 1 if self.noLocal else 061 retainAsPublished = 1 if self.retainAsPublished else 062 data = [(self.retainHandling << 4) | (retainAsPublished << 3) |63 (noLocal << 2) | self.QoS]64 if sys.version_info[0] >= 3:65 buffer = bytes(data)66 else:67 buffer = bytearray(data)68 return buffer69 def unpack(self, buffer):70 b0 = buffer[0]71 self.retainHandling = ((b0 >> 4) & 0x03)72 self.retainAsPublished = True if ((b0 >> 3) & 0x01) == 1 else False73 self.noLocal = True if ((b0 >> 2) & 0x01) == 1 else False74 self.QoS = (b0 & 0x03)75 assert self.retainHandling in [76 0, 1, 2], "Retain handling should be 0, 1 or 2, not %d" % self.retainHandling77 assert self.QoS in [78 0, 1, 2], "QoS should be 0, 1 or 2, not %d" % self.QoS79 return 180 def __repr__(self):81 return str(self)82 def __str__(self):83 return "{QoS="+str(self.QoS)+", noLocal="+str(self.noLocal) +\84 ", retainAsPublished="+str(self.retainAsPublished) +\85 ", retainHandling="+str(self.retainHandling)+"}"86 def json(self):87 data = {88 "QoS": self.QoS,89 "noLocal": self.noLocal,90 "retainAsPublished": self.retainAsPublished,91 "retainHandling": self.retainHandling,92 }...
account.py
Source:account.py
1# -*- coding: utf-8 -*-2# © 2016 Danimar Ribeiro, Trustcode3# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).4from openerp.osv import fields, osv, expression5import openerp.addons.decimal_precision as dp6class account_tax_code(osv.osv):7 _name = 'account.tax.code'8 _inherit = 'account.tax.code'9 _columns = {10 'tax_retain_amount': fields.float(11 'Tax Retain Amount', digits_compute=dp.get_precision('Account')),12 'tax_retain_type': fields.selection(13 [('no_retain', 'No Retain'), ('by_invoice', 'By Invoice'),14 ('inv_amount_monthly', 'Invoice Amount Monthly')],15 'Tax Retain Type'),16 'invoice_tax_retain_account': fields.many2one(17 'account.account', 'Invoice Tax Retain Account'),18 'refund_tax_retain_account': fields.many2one(19 'account.account', 'Refund Tax Retain Account'),20 'account_collected_deduct_id': fields.many2one(21 'account.account', 'Invoice Deduct Tax Account',22 help='Set the account that will be set by deduct on invoice tax lines for invoices. Leave empty to not use deduction account tax.')}23 _defaults = {'tax_retain_type': 'no_retain'}24class account_move(osv.osv):25 _inherit = 'account.move'26 def onchange_journal_id(self, cr, uid, ids, journal_id, context = None):27 if context is None:28 context = {}29 company_id = False30 period_obj = self.pool.get('account.period')31 if journal_id:32 journal = self.pool.get('account.journal').browse(33 cr, uid, journal_id, context=context)34 if journal.company_id.id:35 company_id = journal.company_id.id36 ctx = context.copy()37 ctx.update({38 'company_id': company_id,39 'account_period_prefer_normal': True40 })41 return {'value': {42 'company_id': company_id,43 'period_id': period_obj.find(cr, uid, fields.date.today(),44 context=ctx)[0]}}45 def create(self, cr, uid, vals, context=None):46 if context is None:47 context = {}48 journal_id = vals['journal_id']49 if journal_id:50 journal = self.pool.get('account.journal').browse(51 cr, uid, journal_id, context=context)52 if journal.company_id.id:53 vals['company_id'] = journal.company_id.id54 return super(account_move, self).create(cr, uid, vals, context)55class account_bank_statement(osv.osv):56 _name = 'account.bank.statement'57 _inherit = 'account.bank.statement'58 def create(self, cr, uid, vals, context=None):59 if context is None:60 context = {}61 journal_id = vals['journal_id']62 if journal_id:63 journal = self.pool.get('account.journal').browse(64 cr, uid, journal_id, context=context)65 if journal.company_id.id:66 vals['company_id'] = journal.company_id.id67 return super(account_bank_statement, self).create(68 cr, uid, vals, context=context)...
gini.py
Source:gini.py
1# 计ç®åºå°¼ç³»æ°çç¨åº2# æ Shift+F10 æ§è¡æå°å
¶æ¿æ¢ä¸ºæ¨ç代ç ã3# æ åå» Shift å¨ææå°æ¹æ索类ãæ件ãå·¥å
·çªå£ãæä½å设置ã4import numpy as np5from itertools import groupby6#7# Calculate the gini8#9def process(total_list):10 """A Legendre series class.11 The Legendre class provides the standard Python numerical methods12 '+', '-', '*', '//', '%', 'divmod', '**', and '()' as well as the13 attributes and methods listed in the `ABCPolyBase` documentation.14 Parameters15 ----------16 total_list : array_like17 [ {"xx": aa, "yy": 1},{"xx": bb, "yy": 2} ]18 .. versionadded:: 1.6.019 """20 model_group = groupby(total_list, key=lambda x: x["model"])21 for key, group in model_group:22 process_group(list(group))23def process_group(score_list):24 # æåº ååº25 score_sorted = sorted(score_list, key=lambda x: x["lower"])26 # è®¡ç® total27 total_good_num = 028 total_bad_num = 029 for score in score_sorted:30 total_good_num = total_good_num + score["good"]31 total_bad_num = total_bad_num + score["bad"]32 # 计ç®33 sum_good_num = 034 sum_bad_num = 035 last_retain_good_pct = 036 last_retain_bad_pct = 037 sum_z = 038 max_ks = 039 for score in score_sorted:40 sum_good_num = sum_good_num + score["good"]41 sum_bad_num = sum_bad_num + score["bad"]42 retain_good_pct = sum_good_num / total_good_num43 retain_bad_pct = sum_bad_num / total_bad_num44 ## gini45 sum_z = sum_z + (retain_good_pct + last_retain_good_pct) * (retain_bad_pct - last_retain_bad_pct)46 last_retain_good_pct = retain_good_pct47 last_retain_bad_pct = retain_bad_pct48 ## ks49 max_ks = max(abs(retain_good_pct - retain_bad_pct), max_ks)50 gini = 1 - sum_z51 return [gini, max_ks]52# æé´è·ä¸ç绿è²æé®ä»¥è¿è¡èæ¬ã53if __name__ == '__main__':54 input_list = [55 {"model": "Dragon", "lower": 11, "upper": 20, "good": 45000, "bad": 2000},56 {"model": "Dragon", "lower": 0, "upper": 10, "good": 5000, "bad": 2000},57 {"model": "Dragon", "lower": 21, "upper": 30, "good": 200000, "bad": 2000},58 {"model": "Wisdom", "lower": 11, "upper": 20, "good": 45000, "bad": 2000},59 {"model": "Wisdom", "lower": 0, "upper": 10, "good": 5000, "bad": 2000},60 {"model": "Wisdom", "lower": 21, "upper": 30, "good": 200000, "bad": 2000},61 ]...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!