Best Python code snippet using fMBT_python
ManageVars.py
Source:ManageVars.py  
1# -*- coding: utf-8 -*-2# Copyright 2015 Pietro Brunetti <pietro.brunetti@itb.cnr.it>3#4# Licensed under the Apache License, Version 2.0 (the "License");5# you may not use this file except in compliance with the License.6# You may obtain a copy of the License at7#8#    http://www.apache.org/licenses/LICENSE-2.0910# Unless required by applicable law or agreed to in writing, software11# distributed under the License is distributed on an "AS IS" BASIS,12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13# See the License for the specific language governing permissions and14# limitations under the License.1516""" Managing variable values """1718__authors__ = "Pietro Brunetti"1920import os2122import wx23import  wx.lib.filebrowsebutton as filebrowse24import wx.lib.agw.floatspin as FS2526from peptidome import fasta_indx2728import DialogCommons2930wildcard_f  = "Fasta File (*.fasta)|*.fasta|Aminoacids Fasta File(*.faa)|*.faa"3132class Dialog(wx.Dialog):33    """ Dialog for the variable managing"""34    def __init__(self, parent, ID, title, size=wx.DefaultSize,35                 pos=wx.DefaultPosition, style=wx.DEFAULT_DIALOG_STYLE):36        szr = DialogCommons.createMainSizer(self, parent, ID, title,37                                            pos, size, style)38        self._createMegabox(szr)39        DialogCommons.createBtnSizer(self, szr)40        self.SetSizer(szr)41        szr.Fit(self)424344    def _createMegabox(self, sizer):45        self.megabox = wx.BoxSizer(wx.VERTICAL)46        self._createFastaButton()47        self._createEnzymeChoice()48        self._createMissCutSpinCtrl()49        self._createDeltaPrecisionBox()50        self._setInitialState(self.megabox, sizer)5152    def _createFastaButton(self):53        fileDB = self.GetParent().fileDB54        if not fileDB.has_key("proteome"):55            self.fasta_path = os.path.expanduser("~")56        else:57            if not os.path.isfile(fileDB["proteome"]["fasta_path"]):58                self.fasta_path = os.path.expanduser("~")59            else:60                self.fasta_path = fileDB["proteome"]["fasta_path"]61        self.cF = filebrowse.FileBrowseButton(self,62                            labelText="Select a fasta file:",63                            fileMask=wildcard_f,64                            fileMode=wx.OPEN,65                            initialValue=self.fasta_path,66                            changeCallback = self._faCallback)6768        self.megabox.Add(self.cF, 0,69                         wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)7071    def _createEnzymeChoice(self):72        self.st2 = wx.StaticText(self, -1, "Enzyme", (45, 15))73        enzymeList = ["No enzyme",74                      "Trypsin (K or R)",75                      "Trypsin (complex rule)",76                      "Pepsin (A, V, L, I, G, F, Y or W)",77                      "Proteinase K (F, Y, W or L)"]78        self.chEnz = wx.Choice(self, -1, (100, 50), choices = enzymeList)79        self._setHorizontal([self.st2, self.chEnz], self.megabox)80        self.Bind(wx.EVT_CHOICE, self._enable_misscut, self.chEnz)8182    def _enable_misscut(self, event):83        if self.chEnz.GetSelection() != 0:84            self.chMC.Enable(True)85        else:86            self.chMC.Enable(False)878889    def _createMissCutSpinCtrl(self):90        self.st = wx.StaticText(self, -1, "Miss-cleavage value",91                                (45, 15))92        self.chMC = wx.SpinCtrl(self, -1, "", (30, 50))93        self.chMC.SetRange(0,5)94        self.chMC.SetValue(0)95        self._setHorizontal([self.st, self.chMC], self.megabox)96        self.chMC.Enable(False)9798    def _createDeltaPrecisionBox(self):99        #self._createDigitsControl()100        self._createPrecisionControl()101102    def _createDigitsControl(self):103        self.sc = wx.SpinCtrl(self, -1, "", (30, 50))104        self.sc.SetRange(1, 5)105        self.sc.SetValue(3)106        sd = wx.Button(self, -1, "Set Digits")107        self.Bind(wx.EVT_BUTTON, self.SetDigits, sd)108        self._setHorizontal([self.sc, sd], self.megabox)109110    def _createPrecisionControl(self):111        label = wx.StaticText(self, -1, "Delta mass [Da]")112        self.floatspin = FS.FloatSpin(self, -1, min_val=0, max_val=1,113                                       increment=0.001, value=0.008)114                                       #, extrastyle=FS.FS_LEFT)115        self.floatspin.SetFormat("%f")116        self.floatspin.SetDigits(3)117        self.floatspin.SetIncrement(10**(-3))118        self._setHorizontal([label, self.floatspin], self.megabox)119120    def _setHorizontal(self, ctrls, biggerBox):121        box = wx.BoxSizer(wx.HORIZONTAL)122        for each in ctrls:123            box.Add(each)124        biggerBox.Add(box, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)125126    def _setInitialState(self, megabox, sizer):127        sizer.Add(megabox, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)128129    def _faCallback(self, evt):130        self.fasta_path = evt.GetString()131132    def SetDigits(self, evt):133        """134        Set digits of precision135        """136        self.floatspin.SetValue(0.0)137        power = self.sc.GetValue()138        self.floatspin.SetDigits(power)139        self.floatspin.SetIncrement (10**-(power))140141142    def GetValue(self):143        #TO-DO: check if self.fasta_path is a valid file144        self.values = {}145        # the path fasta file is in Saf.name <-146        # but project does not report the name147        # use for the moment148        # >>> saf_obj = fileDB["proteome"]149        # >>> saf_obj.name = fileDB["fasta"]150        self.values["proteome"] = fasta_indx.Saf(self.fasta_path, window=True)151        self.values["delta"] = self.floatspin.GetValue()*(10**5)152153        self.values["enzyme"] = self.chEnz.GetSelection()154155        self.values["enzyme_exception"] = None156        self.values["miscut"] = self.chMC.GetValue()157        return self.values158159if __name__ == "__main__":160    app = wx.App()161    Dialog(None, -1, "Test")
...Day 17a.py
Source:Day 17a.py  
1puzzle_input = open("AdventOfCode2020/Day 17/puzzleinput.txt").read().splitlines()2test_input = open("AdventOfCode2020/Day 17/testinput.txt").read().splitlines()3from itertools import product4def FindAdjacent(coordinate,cubes):5    x,y,z = coordinate6    return [(x + dx,y + dy,z + dz) for dx,dy,dz in list(product([-1,0,1],repeat=3)) if (x + dx, y + dy, z + dz) in cubes and not (dx == dy == dz == 0)]7def CountAdjacent(coordinate,cubes,char):8    count = 09    for c in FindAdjacent(coordinate,cubes):10        count += (cubes[c] == char)11    return count12def Step(cubes, coordinates, xspan):13    newcubes = cubes.copy()14    biggerbox = set(product([x for x in range(xspan[0],xspan[1] + 1)],repeat=3))15    newcoordinates = biggerbox.difference(coordinates)16    for cd in newcoordinates:17        newcubes[cd] = "."18    coordinates = biggerbox.copy()19    if (3,3,-1) in newcoordinates:20        print("I'm new!")21        print(sorted(newcoordinates))22    for coordinate in coordinates:23        if coordinate in newcoordinates:24            if CountAdjacent(coordinate,cubes,"#") == 3:25                newcubes[coordinate] = "#"26        elif cubes[coordinate] == "#":27            if CountAdjacent(coordinate,cubes,"#") in [2,3]:28                newcubes[coordinate] = "#"29            else:30                newcubes[coordinate] = "."31        else:32            if CountAdjacent(coordinate,cubes,"#") == 3:33                newcubes[coordinate] = "#"34            else:35                newcubes[coordinate] = "."36    return newcubes, coordinates37def Display(cubes):38    for z in range(z_span[0],z_span[1]+1):39        for y in range(y_span[0],y_span[1]+1):40            for x in range(x_span[0],x_span[1]+1):41                print(cubes[(x,y,z)],end='')42            print()43        print()44states = dict()45x_span = (0,7)46y_span = (0,7)47z_span = (0,0)48coordinates = set(product([x for x in range(x_span[0],x_span[1] + 1)],repeat=3))49for coordinate in coordinates:50    states[coordinate] = '.'51for i,row in enumerate(puzzle_input):52    for j,state in enumerate(row):53        states[(j,i,0)] = state54Display(states)55for i in range(6):56    x_span = (x_span[0] - 1, x_span[1] + 1)57    y_span = (y_span[0] - 1, y_span[1] + 1)58    z_span = (z_span[0] - 1, z_span[1] + 1)59    states, coordinates = Step(states, coordinates, x_span)60    print("Step:",i)61    #Display(states)...Day 17b.py
Source:Day 17b.py  
1puzzle_input = open("AdventOfCode2020/Day 17/puzzleinput.txt").read().splitlines()2test_input = open("AdventOfCode2020/Day 17/testinput.txt").read().splitlines()3from itertools import product4def FindAdjacent(coordinate,cubes):5    x,y,z,w = coordinate6    return [(x + dx,y + dy,z + dz,w + dw) for dx,dy,dz,dw in list(product([-1,0,1],repeat=4)) if (x + dx, y + dy, z + dz, w + dw) in cubes and not (dx == dy == dz == dw == 0)]7def CountAdjacent(coordinate,cubes,char):8    count = 09    for c in FindAdjacent(coordinate,cubes):10        count += (cubes[c] == char)11    return count12def Step(cubes, coordinates, xspan):13    newcubes = cubes.copy()14    biggerbox = set(product([x for x in range(xspan[0],xspan[1] + 1)],repeat=4))15    newcoordinates = biggerbox.difference(coordinates)16    for cd in newcoordinates:17        newcubes[cd] = "."18    coordinates = biggerbox.copy()19    for coordinate in coordinates:20        if coordinate in newcoordinates:21            if CountAdjacent(coordinate,cubes,"#") == 3:22                newcubes[coordinate] = "#"23        elif cubes[coordinate] == "#":24            if CountAdjacent(coordinate,cubes,"#") in [2,3]:25                newcubes[coordinate] = "#"26            else:27                newcubes[coordinate] = "."28        else:29            if CountAdjacent(coordinate,cubes,"#") == 3:30                newcubes[coordinate] = "#"31            else:32                newcubes[coordinate] = "."33    return newcubes, coordinates34def Display(cubes):35    for z in range(z_span[0],z_span[1]+1):36        for y in range(y_span[0],y_span[1]+1):37            for x in range(x_span[0],x_span[1]+1):38                print(cubes[(x,y,z)],end='')39            print()40        print()41states = dict()42x_span = (0,7)43y_span = (0,7)44z_span = (0,0)45w_span = (0,0)46coordinates = set(product([x for x in range(x_span[0],x_span[1] + 1)],repeat=4))47for coordinate in coordinates:48    states[coordinate] = '.'49for i,row in enumerate(puzzle_input):50    for j,state in enumerate(row):51        states[(j,i,0,0)] = state52#Display(states)53for i in range(6):54    x_span = (x_span[0] - 1, x_span[1] + 1)55    y_span = (y_span[0] - 1, y_span[1] + 1)56    z_span = (z_span[0] - 1, z_span[1] + 1)57    w_span = (w_span[0] - 1, w_span[1] + 1)58    states, coordinates = Step(states, coordinates, x_span)59    print("Step:",i)60    #Display(states)...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!!
