Python builtins.next() Examples

The following are 30 code examples of builtins.next(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module builtins , or try the search function .
Example #1
Source File: service_accounts.py    From clusterfuzz with Apache License 2.0 6 votes vote down vote up
def _get_or_insert_iam_binding(policy, role):
  """Return the binding corresponding to the given role. Creates the binding if
  needed."""
  existing_binding = next(
      (binding for binding in policy['bindings'] if binding['role'] == role),
      None)
  if existing_binding:
    return existing_binding

  new_binding = {
      'role': role,
      'members': [],
  }

  policy['bindings'].append(new_binding)
  return new_binding 
Example #2
Source File: plugin.py    From misp42splunk with GNU Lesser General Public License v3.0 6 votes vote down vote up
def init_pipeline_plugins(plugin_dir):
    """
    Initialize the pipeline plugins which triggers the auto registering of user
    defined pipeline functions.
    1. Add the plugin_dir into sys.path.
    2. Import the file under plugin_dir that starts with "cce_plugin_" and ends
    with ".py"
    """
    if not op.isdir(plugin_dir):
        logger.warning("%s is not a directory! Pipeline plugin files won't be loaded.",
                       plugin_dir)
        return

    sys.path.append(plugin_dir)
    for file_name in next(walk(plugin_dir))[2]:
        if file_name == "__init__.py" or not file_name.startswith("cce_plugin_"):
            continue
        if file_name.endswith(".py"):
            import_plugin_file(file_name) 
Example #3
Source File: plugin.py    From misp42splunk with GNU Lesser General Public License v3.0 6 votes vote down vote up
def init_pipeline_plugins(plugin_dir):
    """
    Initialize the pipeline plugins which triggers the auto registering of user
    defined pipeline functions.
    1. Add the plugin_dir into sys.path.
    2. Import the file under plugin_dir that starts with "cce_plugin_" and ends
    with ".py"
    """
    if not op.isdir(plugin_dir):
        logger.warning("%s is not a directory! Pipeline plugin files won't be loaded.",
                       plugin_dir)
        return

    sys.path.append(plugin_dir)
    for file_name in next(walk(plugin_dir))[2]:
        if file_name == "__init__.py" or not file_name.startswith("cce_plugin_"):
            continue
        if file_name.endswith(".py"):
            import_plugin_file(file_name) 
Example #4
Source File: scoped_nodes.py    From clonedigger with GNU General Public License v3.0 6 votes vote down vote up
def is_abstract(self, pass_is_abstract=True):
        """return true if the method is abstract
        It's considered as abstract if the only statement is a raise of
        NotImplementError, or, if pass_is_abstract, a pass statement
        """
        for child_node in self.code.getChildNodes():
            if isinstance(child_node, Raise) and child_node.expr1:
                try:
                    name = next(child_node.expr1.nodes_of_class(Name))
                    if name.name == 'NotImplementedError':
                        return True
                except StopIteration:
                    pass
            if pass_is_abstract and isinstance(child_node, Pass):
                return True
            return False
        # empty function is the same as function with a single "pass" statement
        if pass_is_abstract:
            return True 
Example #5
Source File: scan.py    From rekall with GNU General Public License v2.0 6 votes vote down vote up
def skip(self, buffer_as, offset):
        """Skip uninteresting regions.

        Where should we go next? By default we go 1 byte ahead, but if some of
        the checkers have skippers, we may actually go much farther. Checkers
        with skippers basically tell us that there is no way they can match
        anything before the skipped result, so there is no point in trying them
        on all the data in between. This optimization is useful to really speed
        things up.
        """
        skip = 1
        for s in self.skippers:
            skip_value = s.skip(buffer_as, offset)
            skip = max(skip, skip_value)

        return skip 
Example #6
Source File: scan.py    From rekall with GNU General Public License v2.0 6 votes vote down vote up
def scan(self, offset=0, maxlen=None):
        available_length = maxlen or self.session.profile.get_constant(
            "MaxPointer")

        while available_length > 0:
            to_read = min(constants.SCAN_BLOCKSIZE + self.overlap,
                          available_length)

            # Now feed all the scanners from the same address space.
            for name, scanner in list(self.scanners.items()):
                for hit in scanner.scan(offset=offset, maxlen=to_read):
                    # Yield the result as well as cache it.
                    yield name, hit

            # Move to the next scan block.
            offset += constants.SCAN_BLOCKSIZE
            available_length -= constants.SCAN_BLOCKSIZE 
Example #7
Source File: index.py    From rekall with GNU General Public License v2.0 6 votes vote down vote up
def LookupIndex(self):
        """Loookup the profile from an index."""
        try:
            index = self.session.LoadProfile(
                "%s/index" % self.plugin_args.module)
        except ValueError:
            return

        cc = self.session.plugins.cc()
        for session in self.session.plugins.sessions().session_spaces():
            # Switch the process context to this session so the address
            # resolver can find the correctly mapped driver.
            with cc:
                cc.SwitchProcessContext(next(iter(session.processes())))

                # Get the image base of the win32k module.
                image_base = self.session.address_resolver.get_address_by_name(
                    self.plugin_args.module)

                for profile, _ in index.LookupIndex(
                        image_base, minimal_match=self.plugin_args.minimal_match):
                    yield self.session.GetParameter("process_context"), profile 
Example #8
Source File: config.py    From romcollectionbrowser with GNU General Public License v2.0 6 votes vote down vote up
def get_filetype_by_name(self, name, tree):
        fileTypeRows = tree.findall('FileTypes/FileType')

        fileTypeRow = next((element for element in fileTypeRows if element.attrib.get('name') == name), None)
        if fileTypeRow is None:
            Logutil.log('Configuration error. FileType %s does not exist in config.xml' % name, util.LOG_LEVEL_ERROR)
            return None, util.localize(32005)

        fileType = FileType()
        fileType.name = name

        try:
            fileType.id = fileTypeRow.attrib.get('id')
            fileType.type = fileTypeRow.find('type').text
            fileType.parent = fileTypeRow.find('parent').text
        except KeyError:
            Logutil.log('Configuration error. FileType %s must have an id' % name, util.LOG_LEVEL_ERROR)
            return None, util.localize(32005)
        except AttributeError:
            pass

        return fileType, '' 
Example #9
Source File: aff4.py    From pyaff4 with Apache License 2.0 6 votes vote down vote up
def extractFromVolume(container_urn, volume, imageURNs, destFolder):
    printVolumeInfo(container_urn.original_filename, volume)
    resolver = volume.resolver
    for imageUrn in imageURNs:
        imageUrn = utils.SmartUnicode(imageUrn)

        pathName = next(resolver.QuerySubjectPredicate(volume.urn, imageUrn, volume.lexicon.pathName))

        with resolver.AFF4FactoryOpen(imageUrn) as srcStream:
            if destFolder != "-":
                pathName = escaping.arnPathFragment_from_path(pathName.value)
                while pathName.startswith("/"):
                    pathName = pathName[1:]
                destFile = os.path.join(destFolder, pathName)
                if not os.path.exists(os.path.dirname(destFile)):
                    try:
                        os.makedirs(os.path.dirname(destFile))
                    except OSError as exc:  # Guard against race condition
                        if exc.errno != errno.EEXIST:
                            raise
                with open(destFile, "wb") as destStream:
                    shutil.copyfileobj(srcStream, destStream, length=32 * 2014)
                    print("\tExtracted %s to %s" % (pathName, destFile))
            else:
                shutil.copyfileobj(srcStream, sys.stdout) 
Example #10
Source File: inference.py    From clonedigger with GNU General Public License v3.0 6 votes vote down vote up
def infer_subscript(self, context=None):
    """infer simple subscription such as [1,2,3][0] or (1,2,3)[-1]
    """
    if len(self.subs) == 1:
        index = next(self.subs[0].infer(context))
        if index is YES:
            yield YES
            return
        try:
            # suppose it's a Tuple/List node (attribute error else)
            assigned = self.expr.getitem(index.value)
        except AttributeError:
            raise InferenceError()
        except IndexError:
            yield YES
            return
        for infered in assigned.infer(context):
            yield infered
    else:
        raise InferenceError() 
Example #11
Source File: compat.py    From clonedigger with GNU General Public License v3.0 5 votes vote down vote up
def imap(function, *iterables):
        iterables = list(map(iter, iterables))
        while True:
            args = [next(i) for i in iterables]
            if function is None:
                yield tuple(args)
            else:
                yield function(*args) 
Example #12
Source File: test_futurize.py    From kgsgo-dataset-preprocessor with Mozilla Public License 2.0 5 votes vote down vote up
def test_next_1(self):
        """
        Custom next methods should not be converted to __next__ in stage1, but
        any obj.next() calls should be converted to next(obj).
        """
        before = """
        class Upper:
            def __init__(self, iterable):
                self._iter = iter(iterable)
            def next(self):                 # note the Py2 interface
                return next(self._iter).upper()
            def __iter__(self):
                return self

        itr = Upper('hello')
        assert itr.next() == 'H'
        assert next(itr) == 'E'
        assert list(itr) == list('LLO')
        """

        after = """
        class Upper:
            def __init__(self, iterable):
                self._iter = iter(iterable)
            def next(self):                 # note the Py2 interface
                return next(self._iter).upper()
            def __iter__(self):
                return self

        itr = Upper('hello')
        assert next(itr) == 'H'
        assert next(itr) == 'E'
        assert list(itr) == list('LLO')
        """
        self.convert_check(before, after, stages=[1], run=PY2) 
Example #13
Source File: test_futurize.py    From kgsgo-dataset-preprocessor with Mozilla Public License 2.0 5 votes vote down vote up
def test_next_2(self):
        """
        This version of the above doesn't currently work: the self._iter.next() call in
        line 5 isn't converted to next(self._iter).
        """
        before = """
        class Upper:
            def __init__(self, iterable):
                self._iter = iter(iterable)
            def next(self):                 # note the Py2 interface
                return self._iter.next().upper()
            def __iter__(self):
                return self

        itr = Upper('hello')
        assert itr.next() == 'H'
        assert next(itr) == 'E'
        assert list(itr) == list('LLO')
        """

        after = """
        class Upper(object):
            def __init__(self, iterable):
                self._iter = iter(iterable)
            def next(self):                 # note the Py2 interface
                return next(self._iter).upper()
            def __iter__(self):
                return self

        itr = Upper('hello')
        assert next(itr) == 'H'
        assert next(itr) == 'E'
        assert list(itr) == list('LLO')
        """
        self.convert_check(before, after, stages=[1], run=PY2) 
Example #14
Source File: __init__.py    From riko with MIT License 5 votes vote down vote up
def get_assignment(result, skip=False, **kwargs):
    # print(result)
    result = iter(listize(result))

    if skip:
        return None, result

    try:
        first_result = next(result)
    except StopIteration:
        first_result = None

    try:
        second_result = next(result)
    except StopIteration:
        # pipe delivers one result, e.g., strconcat
        result = chain([first_result], result)
        multiple = False
    else:
        # pipe delivers multiple results, e.g., fetchpage/tokenizer
        result = chain([first_result], [second_result], result)
        multiple = True

    first = kwargs.get('count') == 'first'
    _all = kwargs.get('count') == 'all'
    one = first or not (multiple or _all)
    return one, iter([first_result]) if one else result 
Example #15
Source File: log_reg_classifier_utils.py    From snips-nlu with Apache License 2.0 5 votes vote down vote up
def get_noise_it(noise, mean_length, std_length, random_state):
    it = itertools.cycle(noise)
    while True:
        noise_length = int(random_state.normal(mean_length, std_length))
        # pylint: disable=stop-iteration-return
        yield " ".join(next(it) for _ in range(noise_length))
        # pylint: enable=stop-iteration-return 
Example #16
Source File: compat.py    From clonedigger with GNU General Public License v3.0 5 votes vote down vote up
def izip(*iterables):
        iterables = list(map(iter, iterables))
        while iterables:
            result = [next(i) for i in iterables]
            yield tuple(result) 
Example #17
Source File: autoconfig.py    From romcollectionbrowser with GNU General Public License v2.0 5 votes vote down vote up
def isInstalled(self, emulator):
        print ('EmulatorAutoconfig: isInstalled(). emulator = %s' % emulator.name)
        
        for detectionMethod in emulator.detectionMethods:
            print ('EmulatorAutoconfig: detectionMethod.name = ' + detectionMethod.name)
            if detectionMethod.name == 'packagename':
                try:
                    packages = os.popen(detectionMethod.command).readlines()
                except OSError as exc:
                    print ('EmulatorAutoconfig ERROR: error while reading list of packages: %s' % str(exc))
                    # Try with the next detectionMethod
                    continue

                print ('EmulatorAutoconfig: packages = ' + str(packages))
                for package in packages:
                    print ('EmulatorAutoconfig: package = ' + package)
                    print ('EmulatorAutoconfig: detectionMethod.packagename = ' + detectionMethod.packagename)
                    if package.strip() == detectionMethod.packagename.strip():
                        print ('EmulatorAutoconfig: emulator is installed!')
                        return True

            elif detectionMethod.name == 'registry':
                print ('Emulator installation status via registry has not been implemented')

            elif detectionMethod.name == 'commonFolders':
                print ('Emulator installation status via commonFolders has not been implemented')

            else:
                print ('Unhandled detection method: {0}'.format(detectionMethod.name))

        return False 
Example #18
Source File: data_augmentation.py    From snips-nlu with Apache License 2.0 5 votes vote down vote up
def generate_utterance(contexts_iterator, entities_iterators):
    context = deepcopy(next(contexts_iterator))
    context_data = []
    for chunk in context[DATA]:
        if ENTITY in chunk:
            chunk[TEXT] = deepcopy(
                next(entities_iterators[chunk[ENTITY]]))
        chunk[TEXT] = chunk[TEXT].strip() + " "
        context_data.append(chunk)
    context[DATA] = context_data
    return context 
Example #19
Source File: test_data_augmentation.py    From snips-nlu with Apache License 2.0 5 votes vote down vote up
def test_context_iterator(self, _):
        # Given
        dataset = {
            "intents": {
                "dummy": {"utterances": list(range(3))}
            }
        }
        random_state = np.random.RandomState(1)

        # When
        it = get_contexts_iterator(dataset, "dummy", random_state)
        context = [next(it) for _ in range(5)]

        # Then
        self.assertEqual(context, [0, 2, 1, 0, 2]) 
Example #20
Source File: evmasm.py    From pyevmasm with Apache License 2.0 5 votes vote down vote up
def parse_operand(self, buf):
        """ Parses an operand from buf

            :param buf: a buffer
            :type buf: iterator/generator/string
        """
        buf = iter(buf)
        try:
            operand = 0
            for _ in range(self.operand_size):
                operand <<= 8
                operand |= next(buf)
            self._operand = operand
        except StopIteration:
            raise ParseError("Not enough data for decoding") 
Example #21
Source File: _csv.py    From pydatalab with Apache License 2.0 5 votes vote down vote up
def browse(self, max_lines=None, headers=None):
    """Try reading specified number of lines from the CSV object.
    Args:
      max_lines: max number of lines to read. If None, the whole file is read
      headers: a list of strings as column names. If None, it will use "col0, col1..."
    Returns:
      A pandas DataFrame with the schema inferred from the data.
    Raises:
      Exception if the csv object cannot be read or not enough lines to read, or the
      headers size does not match columns size.
    """
    if self.path.startswith('gs://'):
      lines = Csv._read_gcs_lines(self.path, max_lines)
    else:
      lines = Csv._read_local_lines(self.path, max_lines)
    if len(lines) == 0:
      return pd.DataFrame(columns=headers)
    columns_size = len(next(csv.reader([lines[0]], delimiter=self._delimiter)))
    if headers is None:
      headers = ['col' + newstr(e) for e in range(columns_size)]
    if len(headers) != columns_size:
      raise Exception('Number of columns in CSV do not match number of headers')
    buf = StringIO()
    for line in lines:
      buf.write(line)
      buf.write('\n')
    buf.seek(0)
    df = pd.read_csv(buf, names=headers, delimiter=self._delimiter)
    for key, col in df.iteritems():
      if self._is_probably_categorical(col):
        df[key] = df[key].astype('category')
    return df 
Example #22
Source File: _csv_file.py    From pydatalab with Apache License 2.0 5 votes vote down vote up
def browse(self, max_lines=None, headers=None):
    """Try reading specified number of lines from the CSV object.
    Args:
      max_lines: max number of lines to read. If None, the whole file is read
      headers: a list of strings as column names. If None, it will use "col0, col1..."
    Returns:
      A pandas DataFrame with the schema inferred from the data.
    Raises:
      Exception if the csv object cannot be read or not enough lines to read, or the
      headers size does not match columns size.
    """
    if self.path.startswith('gs://'):
      lines = CsvFile._read_gcs_lines(self.path, max_lines)
    else:
      lines = CsvFile._read_local_lines(self.path, max_lines)
    if len(lines) == 0:
      return pd.DataFrame(columns=headers)
    columns_size = len(next(csv.reader([lines[0]], delimiter=self._delimiter)))
    if headers is None:
      headers = ['col' + newstr(e) for e in range(columns_size)]
    if len(headers) != columns_size:
      raise Exception('Number of columns in CSV do not match number of headers')
    buf = StringIO()
    for line in lines:
      buf.write(line)
      buf.write('\n')
    buf.seek(0)
    df = pd.read_csv(buf, names=headers, delimiter=self._delimiter)
    for key, col in df.iteritems():
      if self._is_probably_categorical(col):
        df[key] = df[key].astype('category')
    return df 
Example #23
Source File: config.py    From romcollectionbrowser with GNU General Public License v2.0 5 votes vote down vote up
def readImagePlacing(self, imagePlacingName, tree):

        #fileTypeForRow = None
        fileTypeForRows = tree.findall('ImagePlacing/fileTypeFor')

        fileTypeForRow = next(
            (element for element in fileTypeForRows if element.attrib.get('name') == imagePlacingName), None)
        if fileTypeForRow is None:
            Logutil.log(
                'Configuration error. ImagePlacing/fileTypeFor %s does not exist in config.xml' % str(imagePlacingName),
                util.LOG_LEVEL_ERROR)
            return None, util.localize(32005)

        imagePlacing = ImagePlacing()

        imagePlacing.name = imagePlacingName

        for attr in ['fileTypesForGameList', 'fileTypesForGameListSelected',
                     'fileTypesForMainView1', 'fileTypesForMainView2', 'fileTypesForMainView3',
                     'fileTypesForMainViewBackground', 'fileTypesForMainViewGameInfoBig',
                     'fileTypesForMainViewGameInfoUpperLeft', 'fileTypesForMainViewGameInfoUpperRight',
                     'fileTypesForMainViewGameInfoLowerLeft', 'fileTypesForMainViewGameInfoLowerRight',
                     'fileTypesForMainViewGameInfoLower', 'fileTypesForMainViewGameInfoUpper',
                     'fileTypesForMainViewGameInfoRight', 'fileTypesForMainViewGameInfoLeft',
                     'fileTypesForMainViewVideoWindowBig', 'fileTypesForMainViewVideoWindowSmall',
                     'fileTypesForMainViewVideoFullscreen']:
            # Hack - class attribute fileTypesForXXX doesn't match XML key fileTypeForXXX
            val = self.readFileTypeForElement(fileTypeForRow, attr.replace('fileTypesFor', 'fileTypeFor'), tree)
            log.debug("Reading imageplacing for {0}: {1}".format(attr, val))
            setattr(imagePlacing, attr, val)

        return imagePlacing, '' 
Example #24
Source File: tools.py    From RAFCON with Eclipse Public License 1.0 5 votes vote down vote up
def on_button_press(self, event):
        """Select items

        When the mouse button is pressed, the selection is updated.

        :param event: The button event
        """
        if event.get_button()[1] not in self._buttons:
            return False  # Only handle events for registered buttons (left mouse clicks)

        if event.get_state()[1] & constants.RUBBERBAND_MODIFIER:
            return False  # Mouse clicks with pressed shift key are handled in another tool

        # Special case: moving the NameView
        # This is only allowed, if the hovered item is a NameView and the Ctrl-key is pressed and the only selected
        # item is the parental StateView. In this case, the selection and _item will no longer be looked at,
        # but only _move_name_v
        self._item = self.get_item()
        if isinstance(self._item, NameView):
            selected_items = self.view.selected_items
            if event.get_state()[1] & Gdk.ModifierType.CONTROL_MASK and len(selected_items) == 1 and next(iter(selected_items)) is \
                    self._item.parent:
                self._move_name_v = True
            else:
                self._item = self._item.parent

        if not self._move_name_v:
            self._old_selection = self.view.selected_items
            if self._item not in self.view.selected_items:
                # When items are to be moved, a button-press should not cause any deselection.
                # However, the selection is stored, in case no move operation is performed.
                self.view.handle_new_selection(self._item)

        if not self.view.is_focus():
            self.view.grab_focus()

        return True 
Example #25
Source File: minijail.py    From clusterfuzz with Apache License 2.0 5 votes vote down vote up
def get_binding(self, src_path):
    """Returns binding for src_path.

    Args:
      src_path: The source directory path.

    Returns:
      A ChrootBinding with the same src_path.
    """
    return next((x for x in self._bindings
                 if os.path.abspath(x.src_path) == os.path.abspath(src_path)),
                None) 
Example #26
Source File: utils.py    From clusterfuzz with Apache License 2.0 5 votes vote down vote up
def step(self, reset=False):
    """Print one step of progress bar."""
    if reset:
      self.__init__(self.maxi, self.size, self.msg)
    if not self.header_printed:
      self.__print_header()
    next(self.p) 
Example #27
Source File: utils.py    From clusterfuzz with Apache License 2.0 5 votes vote down vote up
def find_input_index(index, input_ranges):
  """Find the input index given the index of training data."""
  return next(i for i, input in enumerate(input_ranges)
              if input['start'] <= index < input['end']) 
Example #28
Source File: performance_analyzer_test.py    From clusterfuzz with Apache License 2.0 5 votes vote down vote up
def assert_log_has_issue_matching(self,
                                    log_filename,
                                    expected_issue,
                                    stats_overrides=None):
    """Assert for testing log has a particular issue."""
    detected_issues = self.get_issues(log_filename, stats_overrides)
    actual_issue = next(
        (i for i in detected_issues if i['type'] == expected_issue['type']),
        None)

    self.assertIsNotNone(
        actual_issue,
        '"%s" issue is not found in the result' % expected_issue['type'])
    self.assertEqual(actual_issue['percent'], expected_issue['percent'])
    self.assertEqual(actual_issue['score'], expected_issue['score']) 
Example #29
Source File: storage.py    From clusterfuzz with Apache License 2.0 5 votes vote down vote up
def get_bucket_iam_binding(iam_policy, role):
  """Get the binding matching a role, or None."""
  return next((
      binding for binding in iam_policy['bindings'] if binding['role'] == role),
              None) 
Example #30
Source File: compute_engine_projects.py    From clusterfuzz with Apache License 2.0 5 votes vote down vote up
def get_cluster(self, name):
    """Get the cluster with the given name."""
    return next((cluster for cluster in self.clusters if cluster.name == name),
                None)