Best Python code snippet using molecule_python
_control.py
Source:_control.py  
1"""Control objects for surface calculations."""2from fsc.export import export3from ..line._control import _create_line_controls4from .._control import (5    ControlContainer, DataControl, ConvergenceControl, StatefulControl,6    IterationControl, SurfaceControl, LineControl7)8from .._utils import _get_max_move9@export10class SurfaceControlContainer(ControlContainer):11    """12    Container for controls used in the surface run.13    """14    def __init__(self, controls):15        super().__init__(16            controls=controls,17            categories={18                'line': [LineControl],19                'stateful': [StatefulControl, SurfaceControl],20                'data': [DataControl, SurfaceControl],21                'convergence': [ConvergenceControl, SurfaceControl],22                'iteration': [IterationControl, SurfaceControl],23            },24            valid_type=(SurfaceControl, LineControl),25        )26@export27class MoveCheck(DataControl, ConvergenceControl, SurfaceControl):28    """29    Performs the check whether the WCC in neighbouring lines have moved too much.30    """31    def __init__(self, *, move_tol):32        self.move_tol = move_tol33        self._converged = None34    @property35    def converged(self):36        return self._converged37    def update(self, data):38        self._converged = [39            _get_max_move(l1.wcc, l2.wcc) <40            self.move_tol * min(l1.gap_size, l2.gap_size)41            for l1, l2 in zip(data.lines[:-1], data.lines[1:])42        ]43@export44class GapCheck(DataControl, ConvergenceControl, SurfaceControl):45    """46    Performs the check whether the largest gap is too close to WCC in neighbouring lines.47    """48    def __init__(self, *, gap_tol):49        self.gap_tol = gap_tol50        self._converged = None51    @property52    def converged(self):53        return self._converged54    def update(self, data):55        self._converged = [56            all(57                abs(w2 - l1.gap_pos) > self.gap_tol * l1.gap_size58                for w2 in l2.wcc59            ) and all(60                abs(w1 - l2.gap_pos) > self.gap_tol * l2.gap_size61                for w1 in l1.wcc62            ) for l1, l2 in zip(data.lines, data.lines[1:])63        ]64def _create_surface_controls(*, pos_tol, iterator, move_tol, gap_tol):65    """66    Helper function to create the control objects needed by a surface calculation.67    """68    controls = _create_line_controls(pos_tol=pos_tol, iterator=iterator)69    if move_tol is not None:70        controls.append(MoveCheck(move_tol=move_tol))71    if gap_tol is not None:72        controls.append(GapCheck(gap_tol=gap_tol))...main.py
Source:main.py  
1#!/usr/bin/env python32from __future__ import annotations3from typing import Dict, Iterable, List4class Node:5    def __init__(self, name: int):6        self.name = name7        self.score = 1.08        self._temp_score = 1.09        self._parents: List[Node] = []10        self._children: List[Node] = []11    def update(self, new_score: float, epsilon: float) -> bool:12        self._temp_score = new_score13        return abs(new_score - self.score) < epsilon14    def release(self):15        self.score = self._temp_score16    def connect(self, other: Node) -> None:17        if other not in self._children:18            self._children.append(other)19        other.add_parent(self)20    def add_parent(self, parent: Node) -> None:21        if parent not in self._parents:22            self._parents.append(parent)23    def parents(self) -> Iterable[Node]:24        return self._parents25    @property26    def outgoing(self) -> int:27        return len(self._children)28class PageRank:29    def __init__(self, damping: int = 0.7, epsilon: float = 0.001):30        self.epsilon = epsilon31        self.damping = damping32        self._jump_factor = damping33        self._converged: Dict[Node, bool] = {}34    def add(self, new_node: Node) -> None:35        self._converged[new_node] = False36        self._jump_factor = (1-self.damping)/len(self._converged)37    def print_scores(self):38        print("Scores:")39        for node in self._converged.keys():40            print(f"{node.name}: score:{node.score:.2f}")41    def run(self, max_iterations: int = 100):42        count = 043        while(count < max_iterations and not all(self._converged.values())):44            for node in self._converged.keys():45                if self._converged[node]:46                    continue47                new_score = (48                    self._jump_factor49                    + self.damping50                    * sum([n.score / n.outgoing for n in node.parents()])51                )52                stop = node.update(new_score, self.epsilon)53                self._converged[node] = stop54            for node in self._converged.keys():55                node.release()56            count += 157        self.print_scores()58        print(f"\nIterations:{count}")59def main():60    a = Node(1)61    b = Node(2)62    c = Node(3)63    d = Node(4)64    e = Node(5)65    a.connect(b)66    b.connect(c); b.connect(e)67    c.connect(a)68    d.connect(e)69    e.connect(e)70    ranker = PageRank()71    ranker.add(a); ranker.add(b); ranker.add(c); ranker.add(d); ranker.add(e)72    ranker.run()73if __name__ == "__main__":...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
