def ip_set(self, ip, prefix='http'): print(threading.current_thread().getName(), '开始') prefix = prefix.lower() url = '{}://whois.pconline.com.cn/ipJson.jsp?json=true'.format(prefix) retries = 0 max_retries = 3 self.lock.acquire() while retries < max_retries: try: requests.Session().keep_alive = False response = requests.get(url, headers=self.random_headers, proxies={prefix: ip}, verify=False, timeout=60) if response.status_code == 200 and json.loads( response.text)['regionCode'] == '0': key = "ip:{}:{}".format( prefix, hashlib.md5(ip.encode(encoding='UTF-8')).hexdigest()) r().set(key, ip) self.proxy.append(ip) break except Exception as e: retries += 1 self.sleep() if retries == max_retries: print("ip查询请求{}次错误,{}".format(retries, e)) self.lock.release()
def market(self): res = requests.get(self.marketUrl.format(self.timestamps)) res = json.loads(re.match(".*?({.*}).*", res.text, re.S).group(1)) for item in res.values(): data = { 'name': item['name'], 'time': item['time'], 'percent': item['percent'], 'price': item['price'] } r().hmset(item['code'], data, 'market')
def get(self, fundId): headers = {'User-Agent': self.headers} actualNetWorth = '' actualNetWorthRatio = '' res = r().hmget('fund', fundId) if self.tm_hour <= 15 or not res: api = requests.get(self.url.format(fundId, self.timestamps), headers=headers) res = json.loads(re.match(".*?({.*}).*", api.text, re.S).group(1)) else: res = { 'fundcode': res['fund_id'], 'name': res['fund_name'], 'dwjz': res['net_worth'], 'gsz': res['estimated_net_worth'], 'gszzl': res['estimated_net_worth_ratio'], 'gztime': res['update_at'], 'actual_net_worth': res['actual_net_worth'], 'actual_net_worth_ratio': res['actual_net_worth_ratio'] } if res['gztime'][:10] == time.strftime('%Y-%m-%d', time.localtime()): actualNetWorth = res['actual_net_worth'] actualNetWorthRatio = res['actual_net_worth_ratio'] if self.tm_hour >= 20 and (not actualNetWorth or not actualNetWorthRatio): html = requests.get(self.htmlUrl.format(fundId), headers=headers) soup = BeautifulSoup(html.text, 'lxml') dataItem = soup.find('dl', class_='dataItem02') if dataItem != None: dataNums = dataItem.find('dd', class_='dataNums').find_all('span') date = dataItem.dt.p.text[-11:-1] if date == time.strftime('%Y-%m-%d', time.localtime()): actualNetWorth = dataNums[0].text actualNetWorthRatio = dataNums[1].text data = { 'fund_id': res['fundcode'], 'fund_name': res['name'], 'net_worth': res['dwjz'], 'estimated_net_worth': res['gsz'], 'estimated_net_worth_ratio': res['gszzl'], 'actual_net_worth': actualNetWorth, 'actual_net_worth_ratio': actualNetWorthRatio, 'update_at': time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) } r().hmset(res['fundcode'], data, 'fund')
def test_r(self): service = Service([ ]) input_data = [{'Name': 'Robert'}] _, rows = r(service, input_data, 'output = input') self.assertEqual(len(rows), 1) self.assertEqual(rows[0]['Name'], 'Robert')
def test_r_package_usage_from_custom_script(self): import r_index_logging r_index_logging.clear_log_entries() service = Service([ Stanza('package://race', {}), Stanza('package://boot', {}) ]) scriptlib.add(service, 'test', """ library(race) add <- function(a,b) { c = a+b return(c) } result = add(4,10) output = data.frame(Result=c(result)) """) _, rows = r(service, None, 'test.r') indexed_events = r_index_logging.get_log_entries() def is_indexed(text): for e in indexed_events: if text in e: return True return False self.assertTrue(is_indexed('action=\"package_usage\"')) self.assertTrue(is_indexed('package_name=\"race\"'))
def test_r_package_usage(self): import r_index_logging r_index_logging.clear_log_entries() service = Service([ Stanza('package://race', {}), Stanza('package://boot', {}) ]) _, rows = r(service, None, ''' library(race) library(boot) library(race) output = data.frame(h1=c('v1')) ''') indexed_events = r_index_logging.get_log_entries() def is_indexed(text): for e in indexed_events: if text in e: return True return False self.assertTrue(is_indexed('action=\"package_usage\"')) self.assertTrue(is_indexed('package_name=\"race\"')) self.assertTrue(is_indexed('action=\"package_usage\"')) self.assertTrue(is_indexed('package_name=\"boot\"'))
def test_r_package_installation(self): service = Service([ Stanza('package://race', {}) ]) input_data = [{'Name': 'Robert'}] _, rows = r(service, input_data, ''' library(race) output = input ''') self.assertEqual(len(rows), 1) self.assertEqual(rows[0]['Name'], 'Robert')
def f(N,widths): ''' Returns the loss function for an array of indices N and widths. ''' loss = 0. for i,f in enumerate(bands): band = np.arange(f*(1.-fbw[i]/2.),f*(1.+fbw[i]/2.),2.) kband = 2.*np.pi*band*(1.e9)/299792458. x = np.mean(np.array([r.r(angle_of_incidence*np.pi/180.,k,N,widths)**2. for k in kband])) loss += x return loss
def test_r_package_usage(self): import index_logging index_logging.clear_log_entries() service = Service([ Stanza('package://race', {}), Stanza('package://boot', {}), ]) _, rows = r(service, None, ''' library(race) library(boot) library(race) output = data.frame(h1=c('v1')) ''') indexed_events = index_logging.get_log_entries() self.assertEqual(len(indexed_events), 4) self.assertTrue('action=\"package_usage\"' in indexed_events[1]) self.assertTrue('package_name=\"race\"' in indexed_events[1]) self.assertTrue('action=\"package_usage\"' in indexed_events[2]) self.assertTrue('package_name=\"boot\"' in indexed_events[2])
def test_r_package_usage(self): import index_logging index_logging.clear_log_entries() service = Service([ Stanza('package://race', {}), Stanza('package://boot', {}), ]) _, rows = r( service, None, ''' library(race) library(boot) library(race) output = data.frame(h1=c('v1')) ''') indexed_events = index_logging.get_log_entries() self.assertEqual(len(indexed_events), 4) self.assertTrue('action=\"package_usage\"' in indexed_events[1]) self.assertTrue('package_name=\"race\"' in indexed_events[1]) self.assertTrue('action=\"package_usage\"' in indexed_events[2]) self.assertTrue('package_name=\"boot\"' in indexed_events[2])
def test_r_package_usage_from_custom_script(self): import index_logging index_logging.clear_log_entries() service = Service([ Stanza('package://race', {}), Stanza('package://boot', {}), ]) scriptlib.add(service, 'test', """ library(race) add <- function(a,b) { c = a+b return(c) } result = add(4,10) output = data.frame(Result=c(result)) """) _, rows = r(service, None, 'test.r') indexed_events = index_logging.get_log_entries() self.assertEqual(len(indexed_events), 3) self.assertTrue('action=\"package_usage\"' in indexed_events[1]) self.assertTrue('package_name=\"race\"' in indexed_events[1])
def test_r_package_usage_from_custom_script(self): import index_logging index_logging.clear_log_entries() service = Service([ Stanza('package://race', {}), Stanza('package://boot', {}), ]) scriptlib.add( service, 'test', """ library(race) add <- function(a,b) { c = a+b return(c) } result = add(4,10) output = data.frame(Result=c(result)) """) _, rows = r(service, None, 'test.r') indexed_events = index_logging.get_log_entries() self.assertEqual(len(indexed_events), 3) self.assertTrue('action=\"package_usage\"' in indexed_events[1]) self.assertTrue('package_name=\"race\"' in indexed_events[1])
def plot_reflection(N,widths): A = [] for k in ks: A.append(10.*np.log10(r.r(angle_of_incidence*np.pi/180.,k,N,widths)**2.)) return A
def lmReturn(alpha, beta): R = [] for i in period: R.append(r.r(alpha, beta, i)) return np.mean(R)
import numpy as np import time from a import a from r import r from t import t_ciz as t from u import u from k import k a = a() r = r() t = t() u = u() k = k() iki_br = np.zeros((20, 3)) ataturk = np.column_stack((a, t, a, t, u, iki_br, r, k)) for i in range(20): for j in range(142): if ataturk[i][j] == 1: print('*', end='') else: print(' ', end='') print('') time.sleep(.100)
def __init__(self): if self._first == False: self.proxy = r().get("ip*") self.ua = UserAgent(verify_ssl=False) self._first = True self.lock = threading.Lock()