本文整理汇总了Python中bisect.bisect方法的典型用法代码示例。如果您正苦于以下问题:Python bisect.bisect方法的具体用法?Python bisect.bisect怎么用?Python bisect.bisect使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bisect
的用法示例。
在下文中一共展示了bisect.bisect方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_ip_geo_localization
# 需要导入模块: import bisect [as 别名]
# 或者: from bisect import bisect [as 别名]
def get_ip_geo_localization(self, ip):
self._logger.debug("Getting {0} geo localization ".format(ip))
if ip.strip() != "" and ip is not None:
result = linecache.getline(self._ip_localization_file, bisect.bisect(self._ip_localization_ranges, Util.ip_to_int(ip)))
result.strip('\n')
reader = csv.reader([result])
row = reader.next()
geo_loc = ";".join(row[4:6]) + " " + ";".join(row[8:9])
domain = row[9:10][0]
result = {"geo_loc": geo_loc, "domain": domain}
return result
示例2: _find
# 需要导入模块: import bisect [as 别名]
# 或者: from bisect import bisect [as 别名]
def _find(self, path):
path = path[self.prefix_len:]
if path in self._files:
result = True
else:
if path and path[-1] != os.sep:
path = path + os.sep
i = bisect.bisect(self.index, path)
try:
result = self.index[i].startswith(path)
except IndexError:
result = False
if not result:
logger.debug('_find failed: %r %r', path, self.loader.prefix)
else:
logger.debug('_find worked: %r %r', path, self.loader.prefix)
return result
示例3: add_hooks
# 需要导入模块: import bisect [as 别名]
# 或者: from bisect import bisect [as 别名]
def add_hooks(self):
libraries = self.Engine.GetLoadedModules()
for library_path in libraries:
# Check to see if this library should be looked at
library_name = library_path.split("\\")[-1].split("/")[-1].lower()
# Add it to the clean library list maybe
if library_name in self.ProcessBase.clean_modules_names:
# Load the header
header = self.Engine.GetLibraryHeader(library_name)
library_base = self.Engine.GetModuleBase(library_name)
library_size = header.optHeader.SizeOfImage
insert_point = bisect (self.ProcessBase.clean_module_bases, library_base)
self.ProcessBase.clean_module_bases.insert(insert_point, library_base)
self.ProcessBase.clean_module_sizes.insert(insert_point, library_size)
示例4: get_index
# 需要导入模块: import bisect [as 别名]
# 或者: from bisect import bisect [as 别名]
def get_index(self, value, key="name"):
value = value.lower().replace(" ", "")
if key == "name":
index = bisect.bisect(self.db_names, value) - 1
if self.db_names[index] == value:
return index
else:
return -1
try:
index = self.db_abbrevs.index(value)
except ValueError:
index = -1
return index
示例5: add_to_surname_list
# 需要导入模块: import bisect [as 别名]
# 或者: from bisect import bisect [as 别名]
def add_to_surname_list(self, person, batch_transaction):
"""
Add surname to surname list
"""
if batch_transaction:
return
name = None
primary_name = person.get_primary_name()
if primary_name:
surname_list = primary_name.get_surname_list()
if len(surname_list) > 0:
name = surname_list[0].surname
if name is None:
return
i = bisect.bisect(self.surname_list, name)
if 0 < i <= len(self.surname_list):
if self.surname_list[i-1] != name:
self.surname_list.insert(i, name)
else:
self.surname_list.insert(i, name)
示例6: interpolate_1d
# 需要导入模块: import bisect [as 别名]
# 或者: from bisect import bisect [as 别名]
def interpolate_1d(table, value):
"""Return the interpolated result using the table for the passed value.
:type table: list[(int, float)]
:param table: a list where keys are numbers
:type value: float | int
:param value: the value we want to compute the result from.
"""
table.sort(key=lambda fn: fn[0])
keys = [fv[0] for fv in table]
index = bisect.bisect(keys, value)
if index == 0:
return table[index][1]
elif index == len(table):
return table[index - 1][1]
else:
lo = table[index - 1]
hi = table[index]
return (hi[1] - lo[1]) * (value - lo[0]) / (hi[0] - lo[0]) + lo[1]
示例7: add_geospatial_info
# 需要导入模块: import bisect [as 别名]
# 或者: from bisect import bisect [as 别名]
def add_geospatial_info(iploc,inbound,outbound,twoway):
iplist = ''
if os.path.isfile(iploc):
iplist = np.loadtxt(iploc,dtype=np.uint32,delimiter=',',usecols={0},\
converters={0: lambda s: np.uint32(s.replace('"',''))})
else:
print "No iploc.csv file was found, Map View map won't be created"
# get geospatial info, only when iplocation file is available
if iplist != '':
for srcip in outbound:
reader = csv.reader([linecache.getline(\
iploc, bisect.bisect(iplist,outbound[srcip]['ip_int'])).replace('\n','')])
outbound[srcip]['geo'] = reader.next()
reader = csv.reader([linecache.getline(\
iploc, bisect.bisect(iplist,outbound[srcip]['dst_ip_int'])).replace('\n','')])
outbound[srcip]['geo_dst'] = reader.next()
for dstip in twoway:
reader = csv.reader([linecache.getline(\
iploc,bisect.bisect(iplist,twoway[dstip]['ip_int'])).replace('\n','')])
twoway[dstip]['geo'] = reader.next()
for srcip in inbound:
reader = csv.reader([linecache.getline(\
iploc, bisect.bisect(iplist,inbound[srcip]['ip_int'])).replace('\n','')])
inbound[srcip]['geo'] = reader.next()
reader = csv.reader([linecache.getline(\
iploc, bisect.bisect(iplist,inbound[srcip]['src_ip_int'])).replace('\n','')])
inbound[srcip]['geo_src'] = reader.next()
return inbound,outbound,twoway
示例8: put
# 需要导入模块: import bisect [as 别名]
# 或者: from bisect import bisect [as 别名]
def put(self, rank, val):
"""
Args:
rank(int): rank of th element. All elements must have different ranks.
val: an object
"""
idx = bisect.bisect(self.ranks, rank)
self.ranks.insert(idx, rank)
self.data.insert(idx, val)
示例9: _estimate_cardinality_per_partition
# 需要导入模块: import bisect [as 别名]
# 或者: from bisect import bisect [as 别名]
def _estimate_cardinality_per_partition(self, partition, columns, operators,
vals):
distinct_val_covered = 1
observed_cid = []
for c, o, v in zip(columns, operators, vals):
if not c.data.dtype == 'int64':
v = c.ValToBin(v)
cid = self.table.ColumnIndex(c.name)
observed_cid.append(cid)
spread = partition.uniform_spreads[cid]
if o in ['<', '<=']:
if o == '<':
distinct_val_covered = distinct_val_covered * bisect.bisect_left(
spread, v)
else:
distinct_val_covered = distinct_val_covered * bisect.bisect(
spread, v)
elif o in ['>', '>=']:
if o == '>':
distinct_val_covered = distinct_val_covered * (
len(spread) - bisect.bisect(spread, v))
else:
distinct_val_covered = distinct_val_covered * (
len(spread) - bisect.bisect_left(spread, v))
else:
assert o == '=', o
if not v in spread:
distinct_val_covered = 0
for cid in range(len(partition.uniform_spreads)):
if not cid in observed_cid:
distinct_val_covered = distinct_val_covered * len(
partition.uniform_spreads[cid])
return distinct_val_covered * partition.density
示例10: _remove
# 需要导入模块: import bisect [as 别名]
# 或者: from bisect import bisect [as 别名]
def _remove(self, gstr_indices):
""" Removes the data in indices given by gstr_indices """
if self.bStatic: raise ValueError("Cannot _remove on a static DataSet object")
#Removing elements from oliData, timeData, and repData is easy since
# these are just lists. Hard part is adjusting cirIndex values: we
# need to subtract k from index n, where k is the number of indices
# in `gstr_indices` less than n.
inds = sorted(list(gstr_indices))
#remove indices from lists (high->low)
for i in reversed(inds):
del self.oliData[i]
del self.timeData[i]
if self.repData:
del self.repData[i]
#remove elements of cirIndex assoc. w/deleted indices
keys_to_delete = []; inds_set = set(inds)
for k, v in self.cirIndex.items():
if v in inds_set:
keys_to_delete.append(k)
for k in keys_to_delete:
del self.cirIndex[k]
#adjust remaining indices in cirIndex
inds_ar = _np.array(inds, _np.int64)
for k in self.cirIndex.keys():
cnt = _bisect.bisect(inds_ar, self.cirIndex[k]) # cnt == number of removed
self.cirIndex[k] -= cnt # indices < self.cirIndex[k]
示例11: seek
# 需要导入模块: import bisect [as 别名]
# 或者: from bisect import bisect [as 别名]
def seek(self, offset, whence=0):
if self.closed:
raise ValueError('seek of closed file.')
if whence == 1:
offset += self.tell()
elif whence == 2:
offset += self.size
elif whence != 0:
raise ValueError("invalid 'whence'; should be 0, 1, or 2.")
if offset < 0:
raise OSError('invalid offset')
# If the offset is not in the current file, find right one.
while not (0 <= offset - self._file_offsets[self.file_nr]
< self._file_sizes[self.file_nr]):
# Note that not all files may have been opened at this point.
# In that case, bisecting would find we're out of the current files
# and one would open a new one. The while loop ensures we keep
# trying until we've got there or reached the end of the files.
file_nr = bisect(self._file_offsets, offset) - 1
try:
self._open(file_nr)
except OSError:
# If no files left, put pointer beyond end of last file.
if file_nr != len(self._file_sizes): # pragma: no cover
raise
self._open(file_nr - 1)
break
self.fh.seek(offset - self._file_offsets[self.file_nr])
return self.tell()
示例12: locate
# 需要导入模块: import bisect [as 别名]
# 或者: from bisect import bisect [as 别名]
def locate(x, y, position):
i, j = position
return bisect(x, i)-1, bisect(y, j)-1
示例13: get_resources
# 需要导入模块: import bisect [as 别名]
# 或者: from bisect import bisect [as 别名]
def get_resources(self, resource):
path = resource.path[self.prefix_len:]
if path and path[-1] != os.sep:
path += os.sep
plen = len(path)
result = set()
i = bisect.bisect(self.index, path)
while i < len(self.index):
if not self.index[i].startswith(path):
break
s = self.index[i][plen:]
result.add(s.split(os.sep, 1)[0]) # only immediate children
i += 1
return result
示例14: _is_directory
# 需要导入模块: import bisect [as 别名]
# 或者: from bisect import bisect [as 别名]
def _is_directory(self, path):
path = path[self.prefix_len:]
if path and path[-1] != os.sep:
path += os.sep
i = bisect.bisect(self.index, path)
try:
result = self.index[i].startswith(path)
except IndexError:
result = False
return result
示例15: get_qindex
# 需要导入模块: import bisect [as 别名]
# 或者: from bisect import bisect [as 别名]
def get_qindex(self, flat_index):
"""Find qindex containing a flat index.
Given a flat index, to find the corresponding entry in an Array, we need to determine the
block it is saved in. For example, if ``slices = [[0, 3], [3, 7], [7, 12]]``,
the flat index ``5`` corresponds to the second entry, ``qindex = 1`` (since 5 is in [3:7]),
and the index within the block would be ``2 = 5 - 3``.
Parameters
----------
flat_index : int
A flat index of the leg. Negative index counts from behind.
Returns
-------
qindex : int
The qindex, i.e. the index of the block containing `flat_index`.
index_within_block : int
The index of `flat_index` within the block given by `qindex`.
"""
if flat_index < 0:
flat_index += self.ind_len
if flat_index < 0:
raise IndexError("flat index {0:d} too negative for leg with ind_len {1:d}".format(
flat_index - self.ind_len, self.ind_len))
elif flat_index > self.ind_len:
raise IndexError("flat index {0:d} too large for leg with ind_len {1:d}".format(
flat_index, self.ind_len))
qind = bisect.bisect(self.slices, flat_index) - 1
return qind, flat_index - self.slices[qind]