Best Python code snippet using molecule_python
spider_meituan.py
Source:spider_meituan.py
...8class StateInitial(StateBase):9 def start_div(self, attrs):10 c=get_attr(attrs, 'id')11 if c == 'index-deals':12 self.change_state(self.context.state_div_goods)13 elif c == 'deal-default':14 self.change_state(self.context.state_default_goods)15class StateDivGoods(StateBase):16 def start_div(self, attrs):17 c=get_attr(attrs, 'class')18 if c == 'sidebar':19 self.change_state(self.context.state_end)20 elif c == 'item' or c == 'item odd':21 self.change_state(self.context.state_a_title)22 elif c == 'primary cf':23 self.change_state(self.context.state_primary_a_title)24class StatePrimaryH1Title(StateBase):25 def start_h1(self, attrs):26 self.change_state(self.context.state_primary_a_title)27 28class StatePrimaryATitle(StateBase):29 def start_a(self, attrs):30 href=get_attr(attrs, 'href')31 self.context.add_url(href)32 self.change_state(self.context.state_primary_data_title)33class StatePrimaryDataTitle(StateBase):34 def handle_data(self, data):35 title=data.strip()36 self.context.add_title(title)37 self.change_state(self.context.state_primary_p_price)38class StatePrimaryPPrice(StateBase):39 def start_p(self, attrs):40 c=get_attr(attrs, 'class')41 if c=='deal-price' :42 self.change_state(self.context.state_primary_strong_price)43class StatePrimaryStrongPrice(StateBase):44 def start_strong(self, attrs):45 self.change_state(self.context.state_primary_price)46class StatePrimaryPrice(StateBase):47 def handle_data(self, data):48 price=parse_first_float(data.strip())49 self.context.logger.debug('got price ' + price + ' from data ' + data.strip())50 self.context.add_price(price)51 self.change_state(self.context.state_primary_table_value)52class StatePrimaryTableValue(StateBase):53 def start_table(self, attrs):54 c=get_attr(attrs, 'class')55 if c == 'discount':56 self.change_state(self.context.state_primary_tr_value)57class StatePrimaryTrValue(StateBase):58 def start_tr(self, attrs):59 c=get_attr(attrs, 'class')60 if c=='number':61 self.change_state(self.context.state_primary_del_value)62class StatePrimaryDelValue(StateBase):63 def start_del(self, attrs):64 self.change_state(self.context.state_primary_value)65class StatePrimaryValue(StateBase):66 def handle_data(self, data):67 value=parse_first_float(data.strip())68 self.context.logger.debug('got value ' + value + ' from data ' + data)69 self.context.add_value(value)70 self.change_state(self.context.state_primary_p_bought)71class StatePrimaryPBought(StateBase):72 def start_p(self, attrs):73 c=get_attr(attrs, 'class')74 if c == 'deal-buy-tip-top':75 self.change_state(self.context.state_primary_strong_bought)76class StatePrimaryStrongBought(StateBase):77 def start_strong(self, attrs):78 self.change_state(self.context.state_primary_bought)79class StatePrimaryBought(StateBase):80 def handle_data(self, data):81 bought=parse_first_integer(data.strip())82 self.context.add_bought(bought)83 self.change_state(self.context.state_primary_div_time_left)84class StatePrimaryDivTimeLeft(StateBase):85 def start_div(self, attrs):86 c=get_attr(attrs, 'class').split(' ')87 if 'deal-timeleft' in c:88 timeleft=get_attr(attrs, 'diff')89 self.context.add_time_end_by_timeleft(timeleft)90 self.change_state(self.context.state_primary_div_image)91class StatePrimaryDivImage(StateBase):92 def start_div(self, attrs):93 c=get_attr(attrs, 'class')94 if c == 'cover' :95 self.change_state(self.context.state_primary_image)96 97class StatePrimaryImage(StateBase):98 def start_img(self, attrs):99 img=get_attr(attrs, 'src')100 self.context.add_image(img)101 self.change_state(self.context.state_div_goods)102 103class StateH1Title(StateBase):104 def start_h1(self, attrs):105 self.change_state(self.context.state_a_title)106 107class StateATitle(StateBase):108 def start_a(self, attrs):109 href=get_attr(attrs, 'href')110 self.context.add_url(href)111 self.change_state(self.context.state_data_title)112class StateDataTitle(StateBase):113 def handle_data(self, data):114 title=data.strip()115 self.context.add_title(title)116 self.change_state(self.context.state_div_image)117class StateDivImage(StateBase):118 def start_div(self, attrs):119 c=get_attr(attrs, 'class')120 if c == 'cover' :121 self.change_state(self.context.state_image)122 123class StateImage(StateBase):124 def start_img(self, attrs):125 img=get_attr(attrs, 'src')126 self.context.add_image(img)127 self.change_state(self.context.state_p_price)128class StatePPrice(StateBase):129 def start_p(self, attrs):130 c=get_attr(attrs, 'class')131 if c=='deal-price' :132 self.change_state(self.context.state_strong_price)133class StateStrongPrice(StateBase):134 def start_strong(self, attrs):135 self.change_state(self.context.state_price)136 def unknown_starttag(self, tag, attrs):137 self.context.logging.debug('enter strong')138 if tag == 'strong':139 self.start_strong(attrs)140class StatePrice(StateBase):141 def handle_data(self, data):142 price=parse_first_float(data.strip())143 self.context.add_price(price)144 self.change_state(self.context.state_del_value)145class StateTableValue(StateBase):146 def start_table(self, attrs):147 c=get_attr(attrs, 'class')148 if c == 'discount':149 self.change_state(self.context.state_del_value)150class StateDelValue(StateBase):151 def start_del(self, attrs):152 self.change_state(self.context.state_value)153class StateValue(StateBase):154 def handle_data(self, data):155 value=parse_first_float(data.strip())156 self.context.add_value(value)157 self.change_state(self.context.state_div_time_left)158class StateDivTimeLeft(StateBase):159 def start_div(self, attrs):160 c=get_attr(attrs, 'class').split(' ')161 if 'deal-timeleft' in c:162 timeleft=get_attr(attrs, 'diff')163 self.context.logger.debug('got timeleft ' + timeleft)164 self.context.add_time_end_by_timeleft(timeleft)165 self.change_state(self.context.state_p_bought)166class StatePBought(StateBase):167 def start_p(self, attrs):168 c=get_attr(attrs, 'class')169 if c == 'deal-buy-tip-top':170 self.change_state(self.context.state_strong_bought)171class StateStrongBought(StateBase):172 def start_strong(self, attrs):173 self.change_state(self.context.state_bought)174class StateBought(StateBase):175 def handle_data(self, data):176 bought=parse_first_integer(data.strip())177 self.context.add_bought(bought)178 self.change_state(self.context.state_div_goods)179class StateEnd(StateBase):180 pass181class StateDefaultGoods(StateBase):182 def start_div(self, attrs):183 c=get_attr(attrs, 'class')184 cid=get_attr(attrs, 'id')185 if c == 'cf' and cid == 'deal-intro':186 self.change_state(self.context.state_default_a_title)187class StateDefaultH1Title(StateBase):188 def start_h1(self, attrs):189 self.change_state(self.context.state_default_a_title)190 191class StateDefaultATitle(StateBase):192 def start_a(self, attrs):193 href=get_attr(attrs, 'href')194 self.context.add_url(href)195 self.change_state(self.context.state_default_data_title_prefix)196class StateDefaultDataTitlePrefix(StateBase):197 def handle_data(self, data):198 self.change_state(self.context.state_default_data_title)199class StateDefaultDataTitle(StateBase):200 def handle_data(self, data):201 title=data.strip()202 self.context.add_title(title)203 self.change_state(self.context.state_default_p_price)204class StateDefaultPPrice(StateBase):205 def start_p(self, attrs):206 c=get_attr(attrs, 'class')207 if c=='deal-price' :208 self.change_state(self.context.state_default_strong_price)209class StateDefaultStrongPrice(StateBase):210 def start_strong(self, attrs):211 self.change_state(self.context.state_default_price)212class StateDefaultPrice(StateBase):213 def handle_data(self, data):214 price=parse_first_float(data.strip())215 self.context.add_price(price)216 self.change_state(self.context.state_default_table_value)217class StateDefaultTableValue(StateBase):218 def start_table(self, attrs):219 c=get_attr(attrs, 'class')220 if c == 'deal-discount':221 self.change_state(self.context.state_default_td_value)222class StateDefaultTdValue(StateBase):223 def start_td(self, attrs):224 self.change_state(self.context.state_default_del_value)225class StateDefaultDelValue(StateBase):226 def start_del(self, attrs):227 self.change_state(self.context.state_default_value)228class StateDefaultValue(StateBase):229 def handle_data(self, data):230 value=parse_first_float(data.strip())231 self.context.add_value(value)232 self.change_state(self.context.state_default_div_time_left)233class StateDefaultDivTimeLeft(StateBase):234 def start_div(self, attrs):235 c=get_attr(attrs, 'class').split(' ')236 if 'deal-timeleft' in c:237 timeleft=get_attr(attrs, 'diff')238 self.context.add_time_end_by_timeleft(timeleft)239 self.change_state(self.context.state_default_p_bought)240class StateDefaultPBought(StateBase):241 def start_p(self, attrs):242 c=get_attr(attrs, 'class')243 if c == 'deal-buy-tip-top':244 self.change_state(self.context.state_default_strong_bought)245class StateDefaultStrongBought(StateBase):246 def start_strong(self, attrs):247 self.change_state(self.context.state_default_bought)248class StateDefaultBought(StateBase):249 def handle_data(self, data):250 bought=parse_first_integer(data.strip())251 self.context.add_bought(bought)252 self.change_state(self.context.state_default_div_image)253class StateDefaultDivImage(StateBase):254 def start_div(self, attrs):255 c=get_attr(attrs, 'class')256 if c == 'deal-buy-cover-img' :257 self.change_state(self.context.state_default_image)258 259class StateDefaultImage(StateBase):260 def start_img(self, attrs):261 img=get_attr(attrs, 'src')262 self.context.add_image(img)263 self.change_state(self.context.state_end)264class SpiderMeituan(SpiderBase):265 logger = logging.getLogger('tuan.spider.SpiderMeituan')266 def __init__(self):267 SpiderBase.__init__(self)268 self.state_initial=StateInitial(self)269 self.state_div_goods=StateDivGoods(self)270 self.state_h1_title=StateH1Title(self)271 self.state_a_title=StateATitle(self)272 self.state_data_title=StateDataTitle(self)273 self.state_div_image=StateDivImage(self)274 self.state_image=StateImage(self)275 self.state_p_price=StatePPrice(self)276 self.state_strong_price=StateStrongPrice(self)277 self.state_price=StatePrice(self)...
utils.py
Source:utils.py
1import numpy as np234'''纯å¤çç¥'''5def forward_chunduo(hs300, spearman, threshold):6 state = -17 change_state = 08 weeknum = 09 idx0 = 010 idx1 = 011 money = 100000012 idxs = []13 add_times = 014 sub_times = 015 num = 016 record_money = []17 record = [money]18 for i in range(0,len(spearman)):19 record_money.append(money)20 if state == -1:21 if spearman[i] >= threshold:22 idx0 = i #è®°å½ä¹°ç¹23 change_state=1 #æ¹åä»ä½ä¿¡å·24 num = money//(100*hs300[idx0])*100 #è®°å½ä¹°åè¡æ°25 cost = hs300[idx0]*num #è®°å½ä¹°å
¥éé¢26 if state == 1:27 if i == len(spearman) - 1:28 idx1 = i29 profit = hs300[i]*num #è®°å½ååºéé¢30 money = money-cost+profit #è®°å½å½åéé¢31 money_last = record[-1]32 record.append(money)33 idxs.append([[idx0,i], [hs300[idx0],hs300[i]],[num],[money],[(money/money_last)- 1]]) #è®°å½äº¤æä¿¡æ¯ï¼ä¹°åæ¶é´ï¼ä¹°å
¥ä»·æ ¼ï¼ååºä»·æ ¼,交æäºå¤å°è¡ï¼34 if hs300[i] > hs300[idx0]:35 add_times = add_times + 136 else:37 sub_times = sub_times + 138 change_state=139 if spearman[i] <= -threshold:40 idx1 = i41 profit = hs300[i]*num #è®°å½ååºéé¢42 money = money-cost+profit #è®°å½å½åéé¢43 money_last = record[-1]44 record.append(money)45 idxs.append([[idx0,i], [hs300[idx0],hs300[i]],[num],[money],[(money/money_last)- 1]]) #è®°å½äº¤æä¿¡æ¯ï¼ä¹°åæ¶é´ï¼ä¹°å
¥ä»·æ ¼ï¼ååºä»·æ ¼,交æäºå¤å°è¡ï¼4647 if hs300[i] > hs300[idx0]:48 add_times = add_times + 149 else:50 sub_times = sub_times + 151 change_state=152 weeknum = weeknum + 153 if change_state:54 change_state=055 state = -state5657 res = {}5859 # print('æç»éé¢', money)60 # print('å¹´åæ¶çç',(money/1000000)**(1/13)-1)61 # print('交æ次æ°', len(idxs))62 # print('æ»äº¤æ次æ°/çå©/äºæ', add_times+sub_times, add_times, sub_times)6364 money_finale = money # æç»éé¢65 res['money_finale'] = money_finale6667 Rp = (money_finale / 1000000) ** (1 / (len(spearman)/52)) - 168 res['Rp'] = Rp # å¹´åæ¶çç6970 k = 471 ratio = []72 for i in range(len(record_money) // k - 1):73 ratio.append(record_money[i*k+k] / record_money[i*k])74 flow = np.std(ratio)*(12**0.5) # å¹´åæ³¢å¨ç75 res['flow'] = flow7677 sharp = Rp / flow # å¤æ®æ¯ç78 res['sharp'] = sharp7980 # res['record'] = record81 res['record_money'] = record_money8283 res['add_times'] = add_times8485 res['sub_times'] = sub_times8687 res['record_idxs'] = idxs88 return res 899091'''å¤ç©ºçç¥_hs300'''9293def forward_duokong(hs300, spearman, threshold):9495 # 模æè´ä¹°96 state = 0 # 0代表空ä»ï¼1代表æå¤åæªå¹³ï¼-1代表æ空åæªå¹³97 change_state = 09899 idx0 = 0100 idx1 = 0101 money = 1000000102103 add_times = 0104 sub_times = 0105106 record = []107 record_money = []108 num_duo = 0109 num_kong = 0110111 for i in range(0, len(spearman)):112 record_money.append(money)113 if spearman[i] >= threshold: # çå¤ä¿¡å·114 if state == -1 or state == 0: # å¦æ æ空åæªå¹³æè
空ä»115 idx0 = i # è®°å½ä¹°ç¹116 change_state = 1 # æ¹åä»ä½ä¿¡å·117 if num_kong == 0:118 num_duo = money // (100 * hs300[idx0]) * 100 # è®°å½ä¹°åè¡æ°119 else:120 money = money - num_kong * hs300[idx0] + num_kong * hs300[idx1] # 平空ååçéé¢121 if hs300[idx0] < hs300[idx1]:122 record.append([[money], [idx1, idx0], [hs300[idx1], hs300[idx0]], ['平空èï¼åå¤']])123 add_times = add_times + 1124 else:125 record.append([[money], [idx1, idx0], [hs300[idx1], hs300[idx0]], ['平空è´ï¼åå¤']])126 sub_times = sub_times + 1127 num_kong = 0128 num_duo = money // (100 * hs300[idx0]) * 100 # çå¤ä¹°å
¥è¡æ°129130 if change_state == 1 and state == 0: # æ¹åä¿¡å· ä¸ç©ºä» æ¹ä¸ºå¤åä¿¡å·131 change_state = 0132 state = 1133 elif change_state == -1 and state == 0: # æ¹åä¿¡å· ä¸ç©ºä» æ¹ä¸ºç©ºåä¿¡å·134 change_state = 0135 state = -1136 else:137 change_state = 0 # æ¹åä¿¡å· å¤å空 空åå¤138 state = -state139140 if spearman[i] <= -threshold:141 if state == 1 or state == 0: # å¦ææå¤åæªå¹³æ空ä»142 idx1 = i143 change_state = -1 # æ¹åä»ä½ä¿¡å·144 if num_duo == 0:145 num_kong = money // (100 * hs300[idx1]) * 100 # è®°å½ä¹°åè¡æ°146147 else:148 money = money + num_duo * hs300[idx1] - num_duo * hs300[idx0] # å¹³å¤ååçéé¢149 if hs300[idx0] < hs300[idx1]:150 record.append([[money], [idx0, idx1], [hs300[idx0], hs300[idx1]], ['å¹³å¤èï¼å空']])151 add_times = add_times + 1152 else:153 record.append([[money], [idx0, idx1], [hs300[idx0], hs300[idx1]], ['å¹³å¤è´ï¼å空']])154 sub_times = sub_times + 1155 num_duo = 0156 num_kong = money // (100 * hs300[idx0]) * 100 # ç空åå
¥è¡æ°157158 if change_state == 1 and state == 0: # æ¹åä¿¡å· ä¸ç©ºä» æ¹ä¸ºå¤åä¿¡å·159 change_state = 0160 state = 1161 elif change_state == -1 and state == 0: # æ¹åä¿¡å· ä¸ç©ºä» æ¹ä¸ºç©ºåä¿¡å·162 change_state = 0163 state = -1164 else:165 change_state = 0 # æ¹åä¿¡å· å¤å空 空åå¤166 state = -state167 if i == len(spearman) - 1:168 if state == 1:169 money = money + hs300[i] * num_duo170 else:171 money = money + hs300[i] * num_kong172173 res = {}174 175 money_finale = record[-1][0][0] # æç»éé¢176 res['money_finale'] = money_finale177178 Rp = (record[-1][0][0] / 1000000) ** (1 / (len(spearman)/52)) - 1179 res['Rp'] = Rp # å¹´åæ¶çç180181 k = 4182 ratio = []183 for i in range(len(record_money) // k - 1):184 ratio.append(record_money[i*k+k] / record_money[i*k])185 flow = np.std(ratio)*(12**0.5) # å¹´åæ³¢å¨ç186 res['flow'] = flow187188 sharp = Rp / flow # å¤æ®æ¯ç189 res['sharp'] = sharp190191 res['record'] = record192 res['record_money'] = record_money193 res['add_times'] = add_times194195 res['sub_times'] = sub_times196
...
bot_test.py
Source:bot_test.py
...6 eq_(True, bot.in_final_state())7@istest8def bot_should_be_in_accept_in_action_transition():9 bot = Bot(1)10 bot.change_state('010') #de reposo a en acción11 bot.change_state('001') #de en acción a en reposo12 eq_(True, bot.in_final_state())13 eq_(0, bot.get_state())14@istest15def bot_should_be_in_final_state():16 bot = Bot(1)17 bot.change_state('011') #de reposo a calculando18 bot.change_state('100') #de calculando a en movimiento19 bot.change_state('101') #de en movimiento a interactuando20 bot.change_state('110') #de interactuando a regresando21 bot.change_state('001') #de regresando a en reposo22 eq_(True, bot.in_final_state())23 eq_(0, bot.get_state())24@istest25def bot_sould_not_accept_state():26 bot = Bot(1)27 bot.change_state('010')28 bot.change_state('101')...
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!!