Python源码示例:bisect.bisect()
示例1
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
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
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
示例4
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)
示例5
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
示例6
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
示例7
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
示例8
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
示例9
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
示例10
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
示例11
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
示例12
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)
示例13
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]
示例14
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
示例15
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)
示例16
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
示例17
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]
示例18
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()
示例19
def locate(x, y, position):
i, j = position
return bisect(x, i)-1, bisect(y, j)-1
示例20
def locate(x, y, position):
i, j = position
return bisect(x, i)-1, bisect(y, j)-1
示例21
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
示例22
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
示例23
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
示例24
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
示例25
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]
示例26
def cmp_bisect_right(ccmp, a, x, lo=0, hi=None):
"""
same as bisect.bisect but uses custom cmp function
"""
if lo < 0:
raise ValueError('lo must be non-negative')
if hi is None:
hi = len(a)
while lo < hi:
mid = (lo+hi)>>1
if ccmp(a[mid],x)>0: hi = mid # ie. if x < a[mid]
else: lo = mid+1
return lo
示例27
def cmp_bisect_left(ccmp, a, x, lo=0, hi=None):
"""
same as bisect.bisect_left but uses custom cmp function
"""
if lo < 0:
raise ValueError('lo must be non-negative')
if hi is None:
hi = len(a)
while lo < hi:
mid = (lo+hi)>>1
if ccmp(x, a[mid])>0: lo = mid+1 # ie. if a[mid] < x
else: hi = mid
return lo
示例28
def PA2VA_for_DTB(self, physical_address, dtb, userspace=None):
if dtb == None:
return None, None
# Choose the userspace mode automatically.
if userspace is None:
userspace = dtb != self.session.kernel_address_space.dtb
# Build a map for this dtb.
lookup_map = self.dtb2maps.get(dtb)
# If we want the full resolution and the previous cached version was for
# userspace only, discard this lookup map and rebuild it.
if not userspace and self.dtb2userspace.get(dtb):
lookup_map = None
if lookup_map is None:
lookup_map = self.dtb2maps[dtb] = self.build_address_map(
dtb, userspace=userspace)
self.dtb2userspace[dtb] = userspace
if lookup_map:
if physical_address > lookup_map[0][0]:
# This efficiently finds the entry in the map just below the
# physical_address.
lookup_pa, length, lookup_va = lookup_map[
bisect.bisect(
lookup_map, (physical_address, 2**64, 0, 0))-1]
if (lookup_pa <= physical_address and
lookup_pa + length > physical_address):
# Yield the pid and the virtual offset
task = self.dtb2task.get(dtb)
if task is not None:
task = self.GetTaskStruct(task)
else:
task = "Kernel"
return lookup_va + physical_address - lookup_pa, task
return None, None
示例29
def build_address_map(self, dtb, userspace=True):
"""Given the virtual_address_space, build the address map."""
# This lookup map is sorted by the physical address. We then use
# bisect to efficiently look up the physical page.
tmp_lookup_map = []
self.dirty = True
if dtb != None:
address_space = self.session.kernel_address_space.__class__(
base=self.session.physical_address_space,
session=self.session,
dtb=dtb)
highest_virtual_address = self.session.GetParameter(
"highest_usermode_address")
for run in address_space.get_mappings():
# Only consider userspace addresses for processes.
if userspace and run.start > highest_virtual_address:
break
tmp_lookup_map.append((run.file_offset, run.length, run.start))
self.session.report_progress(
"Enumerating memory for dtb %#x (%#x)", dtb, run.start)
# Now sort the map and return it.
tmp_lookup_map.sort()
return tmp_lookup_map
示例30
def prune_queue(self, ratio = None, margin = None, top_n = None, max_length_diff = None):
if len(self.queue) == 0:
return
if max_length_diff is not None:
max_length = max(len(pitem.item.current_translation) for pitem in self.queue)
self.queue = [pitem for pitem in self.queue if len(pitem.item.current_translation) >= max_length - max_length_diff]
initial_length = len(self.queue)
if self.dirty:
self.sort()
if top_n is not None:
self.queue = self.queue[:top_n]
max_priority = float("-inf")
min_priority = float("-inf")
threshold1 = float("-inf")
threshold2 = float("-inf")
threshold = float("-inf")
threshold_index = float("-inf")
if ratio is not None or margin is not None:
max_priority = self.queue[0].priority
min_priority = self.queue[-1].priority
if ratio is not None:
threshold1 = max_priority + max_priority*ratio
if margin is not None:
threshold2 = max_priority - margin
if threshold1 is not None and threshold2 is not None:
threshold = max(threshold1, threshold2)
elif threshold1 is not None:
threshold = threshold1
elif threshold2 is not None:
threshold = threshold2
if threshold > min_priority:
threshold_index = bisect.bisect([-item.priority for item in self.queue], -threshold)
self.queue = self.queue[:threshold_index]
final_length = len(self.queue)
#print(f"pruned {initial_length} -> {final_length} maxp:{max_priority:2f} minp:{min_priority:2f} th1:{threshold1:2f} th2:{threshold2:2f} th:{threshold:2f} thi:{threshold_index}")