def realvalidator(text, separator = '.'):
if separator != '.':
if string.find(text, '.') >= 0:
return ERROR
index = string.find(text, separator)
if index >= 0:
text = text[:index] + '.' + text[index + 1:]
try:
string.atof(text)
return OK
except ValueError:
# Check if the string could be made valid by appending a digit
# eg ('-', '+', '.', '-.', '+.', '1.23e', '1E-').
if len(text) == 0:
return PARTIAL
if text[-1] in string.digits:
return ERROR
try:
string.atof(text + '0')
return PARTIAL
except ValueError:
return ERROR
python类atof()的实例源码
def xview(self, mode = None, value = None, units = None):
if type(value) == types.StringType:
value = string.atof(value)
if mode is None:
return self._horizScrollbar.get()
elif mode == 'moveto':
frameWidth = self._frame.winfo_reqwidth()
self.startX = value * float(frameWidth)
else: # mode == 'scroll'
clipperWidth = self._clipper.winfo_width()
if units == 'units':
jump = int(clipperWidth * self['horizfraction'])
else:
jump = clipperWidth
self.startX = self.startX + value * jump
self.reposition()
# Called when the user clicks in the vertical scrollbar.
# Calculates new position of frame then calls reposition() to
# update the frame and the scrollbar.
def yview(self, mode = None, value = None, units = None):
if type(value) == types.StringType:
value = string.atof(value)
if mode is None:
return self._vertScrollbar.get()
elif mode == 'moveto':
frameHeight = self._frame.winfo_reqheight()
self.startY = value * float(frameHeight)
else: # mode == 'scroll'
clipperHeight = self._clipper.winfo_height()
if units == 'units':
jump = int(clipperHeight * self['vertfraction'])
else:
jump = clipperHeight
self.startY = self.startY + value * jump
self.reposition()
# ======================================================================
# Configuration methods.
def main():
if len(sys.argv) == 2: # slob city
stringlength = string.atoi(sys.argv[1])
if stringlength < 2 or stringlength % 2 != 0:
raise RuntimeError('String length requested must be even '
'integer > 0.')
else:
stringlength = 6
while 1:
line = sys.stdin.readline()
if not line:
break
latlon = re.findall(r'([-0-9.]+)\s+([-0-9.]+)', line)
if latlon:
for leftval, rightval in latlon:
lat = string.atof(leftval)
lon = string.atof(rightval)
else:
raise RuntimeError('Cannot even get the basic items.')
astring = maidenhead.mh1(lat, lon, stringlength)
print('{0}'.format(astring))
def main():
if len(sys.argv) == 2: # slob city
stringlength = string.atoi(sys.argv[1])
if stringlength < 2 or stringlength % 2 != 0:
raise RuntimeError('String length requested must be even '
'integer > 0.')
else:
stringlength = 6
while 1:
line = sys.stdin.readline()
if not line:
break
latlon = re.findall(r'([-0-9.]+)\s+([-0-9.]+)', line)
if latlon:
for leftval, rightval in latlon:
lat = string.atof(leftval)
lon = string.atof(rightval)
else:
raise RuntimeError('Cannot even get the basic items.')
astring = maidenhead.mh2(lat, lon, stringlength)
print('{0}'.format(astring))
def add_solver(self, fname):
"""Collect metrics for each solver."""
comment = re.compile(r'^[\s]*[%#]')
column = re.compile(self.options['sep'])
# Grab the column from the file.
metrics = []
with open(fname, 'r') as fp:
for line in fp:
if not comment.match(line):
line = line.strip()
cols = column.split(line)
data = atof(cols[self.options['datacol'] - 1])
metrics.append(data)
self.metrics.append(metrics)
if len(metrics) != len(self.metrics[0]):
raise ValueError('All solvers must have same number of problems.')
def parse_detail_page(self, response):
item = response.meta["item"]
score = response.xpath('//div[@class="statics clearfix"]/div[@class="static-item l score-btn"]/span[@class="meta-value"]/text()').extract_first().encode('utf-8').strip()
item['score'] = string.atof(score)
review_num = response.xpath('//div[@class="score-box"]/a[@class="person-num"]/span/text()').extract_first().encode('utf-8').strip()
review_num = re.match(r'\d+', review_num).group(0).strip()
item['review_num'] = string.atoi(review_num)
item['intro_detail'] = response.xpath('//div[@class="content"]/div[@class="course-brief"]/p/text()').extract_first().encode('utf-8').strip()
intro = item['name'] + item['intro'] + item['intro_detail']
item['keywords'] = self.extractKeywords(intro)
print item['keywords']
yield item
def stringtoreal(text, separator = '.'):
if separator != '.':
if string.find(text, '.') >= 0:
raise ValueError, 'invalid value: ' + text
index = string.find(text, separator)
if index >= 0:
text = text[:index] + '.' + text[index + 1:]
return string.atof(text)
######################################################################
### File: PmwBalloon.py
def test_atof(self):
self.assertAlmostEqual(string.atof(" 1 "), 1.0)
self.assertRaises(ValueError, string.atof, " 1x ")
self.assertRaises(ValueError, string.atof, " x1 ")
def test_atof(self):
self.assertAlmostEqual(string.atof(" 1 "), 1.0)
self.assertRaises(ValueError, string.atof, " 1x ")
self.assertRaises(ValueError, string.atof, " x1 ")
def extract_data(path):
""" extract all data for feeding placeholder.
Args:
path -- the txt file each line with format
image_dir bounding box labeled points
Return:
images -- image cropped around face range and resized to 39*39
points -- points extracted from txt
factors -- scale factor
crds -- crop box
widths -- bounding box width
"""
images = []; points = []; factors = []; crds = []; widths = []
with open(path, "r") as f:
lines = f.readlines()
for line in lines:
a = line.split(); impath = a[0]; a = a[1:]
aa = []
for i in range(len(a)): aa.append(string.atof(a[i]))
bbox_width = aa[1] - aa[0]; bbox_height = aa[3] - aa[2]
widths.append(bbox_width)
left = int(bbox_width * ranges[0] + aa[0])
top = int(bbox_height * ranges[2] + aa[2])
if bbox_height >= bbox_width:
bottom = int(bbox_height * ranges[3] + aa[2])
height = bottom - top; diff = bbox_height - bbox_width
left = int(left - 0.5 * diff); right = left + height
factor = 39 / height
else:
right = int(bbox_width * ranges[1] + aa[0])
width = right - left; diff = bbox_width - bbox_height
top = int(top - 0.5*diff); bottom = top + width
factor = 39 / width
factors.append([factor])
box = (left, right, top, bottom); crds.append([left, top])
im = Image.open(impath); image = im.crop(box)
images.append(np.array(image.resize((39, 39))) / 255)
point_raw = aa[4:]; points.append(point_raw)
print(points[0])
return images, points, factors, crds, widths
def Sum_of_weight(G):
#nx.number_of_edges(nx.ego_graph(Hub_ego,n,1))
EdgeList = G.edges(data=True) #[(0, 1, {}), (1, 2, {}), (2, 3, {})]
#print EdgeList
Sum_of_weight = 0.0
for edge in EdgeList:
Sum_of_weight = Sum_of_weight + edge[2]['weight'] #weight=string.atof(line[3]),timestamp=string.atof(line[5]
#end for
return Sum_of_weight
def get_ht_thread():
global h
global t
while True:
gpio_lock.acquire()
ht = dht.read_retry(dht.DHT22, config.DHT22_GPIO_NUM)
gpio_lock.release()
h = '{0:0.1f}'.format(ht[0])
t = '{0:0.1f}'.format(ht[1])
h = string.atof(h)
t = string.atof(t)
time.sleep(2)
def get_stock_now_price(self, stock_code):
"""
?????????
:param stock_id: ??id
:return:
"""
code = self._code_to_symbol(stock_code)
data = urllib.urlopen("http://hq.sinajs.cn/list=" + code).read().decode('gb2312')
stockInfo = data.split(',')
currentPrice = string.atof(stockInfo[3])
return float(currentPrice)
def get_yesterday_price(self, stock_code):
"""
??????????
:param stock_code:
:return:
"""
code = self._code_to_symbol(stock_code)
data = urllib.urlopen("http://hq.sinajs.cn/list=" + code).read().decode('gb2312')
stockInfo = data.split(',')
currentPrice = string.atof(stockInfo[2])
return float(currentPrice)
def HexamerFeatures(seq,hash_matrix):
if len(seq) < 6:
return(0,0)
frame_sequence = list(seq)
frame_seq_length = len(frame_sequence)
CDS_array = []
for o in range(0,3):
frame_TempStr = ''
frame_TempStr = InitCodonSeq(o,frame_seq_length-2,3,frame_sequence)
frame_array = frame_TempStr.split(' ') ## codon array
frame_array.pop()
other_num = 0
frame_array_Len = len(frame_array) - 1
for j in range(frame_array_Len):
temp2 = frame_array[j]+frame_array[j+1]
temple4 = re.compile('[atcg]{6}')
if temple4.match(temp2):
other_num = string.atof(other_num) + string.atof(hash_matrix[temp2])
frame_array_Len = frame_array_Len + 2
other_num = other_num / frame_array_Len
CDS_array.append(other_num)
Mscore = max(CDS_array)
score_distance = 0
for m in range(0,3): #problem location
score_distance += Mscore - CDS_array[m]
score_distance = score_distance/float(2)
return(Mscore,score_distance)
#==============================================================================
# The main process for the feature calculation
#==============================================================================
def HexamerFeatures(seq,hash_matrix):
if len(seq) < 6:
return(0,0)
frame_sequence = list(seq)
frame_seq_length = len(frame_sequence)
CDS_array = []
for o in range(0,3):
frame_TempStr = ''
frame_TempStr = InitCodonSeq(o,frame_seq_length-2,3,frame_sequence)
frame_array = frame_TempStr.split(' ') ## codon array
frame_array.pop()
other_num = 0
frame_array_Len = len(frame_array) - 1
for j in range(frame_array_Len):
temp2 = frame_array[j]+frame_array[j+1]
temple4 = re.compile('[atcg]{6}')
if temple4.match(temp2):
other_num = string.atof(other_num) + string.atof(hash_matrix[temp2])
frame_array_Len = frame_array_Len + 2
other_num = other_num / frame_array_Len
CDS_array.append(other_num)
Mscore = max(CDS_array)
score_distance = 0
for m in range(0,3): #problem location
score_distance += Mscore - CDS_array[m]
score_distance = score_distance/float(2)
return(Mscore,score_distance)
#==============================================================================
# Print the final result
#==============================================================================
def test_atof(self):
self.assertAlmostEqual(string.atof(" 1 "), 1.0)
self.assertRaises(ValueError, string.atof, " 1x ")
self.assertRaises(ValueError, string.atof, " x1 ")
def test_atof(self):
self.assertAlmostEqual(string.atof(" 1 "), 1.0)
self.assertRaises(ValueError, string.atof, " 1x ")
self.assertRaises(ValueError, string.atof, " x1 ")
def calculate_date(self):
point = 0.0
weight = 0.0
for i in range(len(self.points)):
if(self.points[i].isdigit()):
point += string.atof(self.points[i])*string.atof(self.weights[i])
weight += string.atof(self.weights[i])
print point/weight
#??
def read_float(self, input_file):
s = string.strip(input_file.readline())
print s
return string.atof(s)
def load_prior_prob(file_name):
f_input = open(file_name,"r")
is_first_line = 0
try:
while True:
line = f_input.readline()
line = line.strip()
if(len(line) == 0):
break
items = line.split("\t")
if len(items) != 3:
print "error in the prob file!",len(items),line
sys.exit()
if is_first_line == 0:
sign_id_map[items[1]] = 0
sign_id_map[items[2]] = 1
is_first_line = 1
else:
word_list[items[0]] = len(prob_matrix)
prob_matrix.append((string.atof(items[1]),string.atof(items[2]),items[0]))
#print line
finally:
for i in range(len(prob_matrix)):
if word_list[prob_matrix[i][2]] != i:
print "load prob error !"
sys.exit()
f_input.close()
def load_prior_prob(file_name):
f_input = open(file_name,"r")
is_first_line = 0
try:
while True:
line = f_input.readline()
line = line.strip()
if(len(line) == 0):
break
items = line.split("\t")
if len(items) != 3:
print "error in the prob file!",len(items),line
sys.exit()
if is_first_line == 0:
sign_id_map[items[1]] = 0
sign_id_map[items[2]] = 1
is_first_line = 1
else:
word_list[items[0]] = len(prob_matrix)
prob_matrix.append((string.atof(items[1]),string.atof(items[2]),items[0]))
#print line
finally:
for i in range(len(prob_matrix)):
if word_list[prob_matrix[i][2]] != i:
print "load prob error !"
sys.exit()
f_input.close()
def load_prior_prob(file_name):
f_input = open(file_name,"r")
is_first_line = 0
try:
while True:
line = f_input.readline()
line = line.strip()
if(len(line) == 0):
break
items = line.split("\t")
if len(items) != 3:
print "error in the prob file!",len(items),line
sys.exit()
if is_first_line == 0:
sign_id_map[items[1]] = 0
sign_id_map[items[2]] = 1
is_first_line = 1
else:
word_list[items[0]] = len(prob_matrix)
prob_matrix.append((string.atof(items[1]),string.atof(items[2]),items[0]))
#print line
finally:
for i in range(len(prob_matrix)):
if word_list[prob_matrix[i][2]] != i:
print "load prob error !"
sys.exit()
f_input.close()
def load_prior_prob(file_name):
f_input = open(file_name,"r")
is_first_line = 0
try:
while True:
line = f_input.readline()
line = line.strip()
if(len(line) == 0):
break
items = line.split("\t")
if len(items) != 3:
print "error in the prob file!",len(items),line
sys.exit()
if is_first_line == 0:
sign_id_map[items[1]] = 0
sign_id_map[items[2]] = 1
is_first_line = 1
else:
word_list[items[0]] = len(prob_matrix)
prob_matrix.append((string.atof(items[1]),string.atof(items[2]),items[0]))
#print line
finally:
for i in range(len(prob_matrix)):
if word_list[prob_matrix[i][2]] != i:
print "load prob error !"
sys.exit()
f_input.close()
def readFloatArrayWithSkip(filename, col):
"Return a floating-point array containing the data from file |filename|."
if not type(col) == list: col = [col]
data = []
from operator import getitem
for line in TextFile(filename):
if line[0] != '#':
fields = string.split(line)
data.append(map(string.atof, [fields[x] for x in col]))
a = Numeric.array(data)
if a.shape[0] == 1 or a.shape[1] == 1:
a = Numeric.ravel(a)
return a
def readFloatArray(filename):
"Return a floating-point array containing the data from file |filename|."
data = []
for line in TextFile(filename):
if line[0] != '#':
data.append(map(string.atof, string.split(line)))
a = Numeric.array(data)
if a.shape[0] == 1 or a.shape[1] == 1:
a = Numeric.ravel(a)
return a
def parse(self, response):
# Get help from: http://stackoverflow.com/questions/38574869/how-can-i-jump-to-next-page-in-scrapy
if response.meta.get('is_json', False):
page = Selector(text=json.loads(response.body)['table'])
else:
page = Selector(response)
if self.flag:
self.total_item_num = int(page.xpath('//div[@id="show-more-courses"]/text()').re(r'courses of (.*)')[0]) + 50
print "Total courses: ", self.total_item_num
self.steps = self.total_item_num / 50 + 1
self.flag = False
base_urls = "https://www.class-central.com/courses/past"
#base_urls = "https://www.class-central.com/courses/recentlyAdded"
my_header = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36'}
divs = page.xpath('//tr[@itemtype="http://schema.org/Event"]')
#print "print content", len(divs)
print "Process: ", self.cnt, '/', self.steps
for div in divs:
item = MoocCrawlerItem()
item = {k:"" for k in item.keys()}
parse_name = div.xpath('./td/a/span[@class="course-name-text"]/text()').extract_first().strip()
item['name'] = parse_name
parse_score = div.xpath('./td/div[@class="course-rating-value"]/text()').extract_first().strip()
if len(parse_score) > 3:
parse_score = parse_score[:3]
item['score'] = string.atof(parse_score) * 2
parse_platform = div.xpath('./td/div[@class="course-provider"]/text()').extract_first().strip()
item['platform'] = parse_platform
parse_url = div.xpath('./td/a/@href').extract_first().decode().encode('utf-8').strip()
item['url'] = "https://www.class-central.com" + parse_url
parse_cid = re.findall(r'/mooc/(.*)/', parse_url)[0]
item['cid'] = "cc" + parse_cid
req = scrapy.Request(item['url'], headers=my_header, callback=self.parse_detail_page)
req.meta['item'] = item
yield req
#next_page_el = respones.xpath("//div[@id='show-more-courses']")
if self.cnt < self.steps:
#if next_page_el:
next_page_url = "https://www.class-central.com/maestro/courses/past?page=1&_=1471346096733"
#next_page_url = "https://www.class-central.com/maestro/courses/recentlyAdded?page=1"
next_page = response.meta.get('page', 1) + 1
next_page_url = add_or_replace_parameter(next_page_url, 'page', next_page)
r = scrapy.Request(next_page_url, headers=my_header, callback=self.parse, meta={'page': next_page, 'is_json': True})
self.cnt += 1
yield r
def Create_Graph(fname = None):
'''
G = nx.Graph()
G.add_edge(0, 1, weight = 2.0,timestamp = 1.0)
G.add_edge(0, 2, weight = 2.0,timestamp = 1.0)
G.add_edge(0, 3, weight = 2.0,timestamp = 1.0)
G.add_edge(0, 4, weight = 2.0,timestamp = 1.0)
G.add_edge(0, 5, weight = 2.0,timestamp = 1.0)
G.add_edge(4, 6, weight = 2.0,timestamp = 1.0)
G.add_edge(4, 7, weight = 2.0,timestamp = 1.0)
G.add_edge(4, 8, weight = 2.0,timestamp = 1.0)
G.add_edge(7, 8, weight = 2.0,timestamp = 1.0)
G.add_edge(5, 9, weight = 2.0,timestamp = 1.0)
G.add_edge(2, 3, weight = 2.0,timestamp = 1.0)
G.add_edge(2, 13, weight = 2.0,timestamp = 1.0)
G.add_edge(2, 11, weight = 2.0,timestamp = 1.0)
G.add_edge(2, 12, weight = 2.0,timestamp = 1.0)
G.add_edge(11, 12, weight = 2.0,timestamp = 1.0)
G.add_edge(3, 11, weight = 2.0,timestamp = 1.0)
G.add_edge(3, 10, weight = 2.0,timestamp = 1.0)
'''
#fname = 'F:/Link_Prediction_Code/Dataset/6-Wireless_contact_Train_Regular.txt'
#Get edge from txt type data
try:
fdobj = open(fname,'r')
except IOError as e:
print "***file open error:",e
else:
G = nx.Graph()
eline = fdobj.readline()
eline = fdobj.readline()
eline = fdobj.readline()
eline = fdobj.readline()
eline = fdobj.readline()
eline = fdobj.readline()
eline = fdobj.readline()
eline = fdobj.readline()
eline = fdobj.readline()
while eline:
line = eline.strip('\n').split()
G.add_edge(string.atoi(line[0]),string.atoi(line[1]),weight=string.atof(line[3]),timestamp=string.atof(line[4]))#weight=string.atof(line[3])
eline = fdobj.readline()
#end while
#Data_Prepare.Drawcomgraph(G)
return G
##==========================================================================================
def get (namepatterns,verbose=1):
"""
Loads a list of lists from text files (specified by a UNIX-style
wildcard filename pattern) and converts all numeric values to floats.
Uses the glob module for filename pattern conversion. Loaded filename
is printed if verbose=1.
Usage: get (namepatterns,verbose=1)
Returns: a 1D or 2D list of lists from whitespace delimited text files
specified by namepatterns; numbers that can be converted to floats
are so converted
"""
fnames = []
if type(namepatterns) in [ListType,TupleType]:
for item in namepatterns:
fnames = fnames + glob.glob(item)
else:
fnames = glob.glob(namepatterns)
if len(fnames) == 0:
if verbose:
print 'NO FILENAMES MATCH ('+namepatterns+') !!'
return None
if verbose:
print fnames # so user knows what has been loaded
elements = []
for i in range(len(fnames)):
file = open(fnames[i])
newelements = map(string.split,file.readlines())
for i in range(len(newelements)):
for j in range(len(newelements[i])):
try:
newelements[i][j] = string.atoi(newelements[i][j])
except ValueError:
try:
newelements[i][j] = string.atof(newelements[i][j])
except:
pass
elements = elements + newelements
if len(elements)==1: elements = elements[0]
return elements