Beispiel #1
0
class ThirdWindow(Screen):
    def __init__(self, **kwargs):
        super(ThirdWindow, self).__init__(**kwargs)
        Clock.schedule_once(self.yap, 0)

    def yap(self, za):
        box = self.ids.deneme
        graph_theme = {
            'label_options': {
                'color': rgb('444444'),  # color of tick labels and titles
                'bold': True
            },
            'background_color': rgb('2b2b2b'),  # canvas background color
            'tick_color': rgb('7ba21d'),  # ticks and grid
            'border_color': rgb('000000')
        }  # border drawn around each graph
        self.graph = Graph(
            xlabel='Time (s)',
            ylabel='Temparature (°F)',
            x_ticks_major=10,
            y_ticks_major=10,
            y_grid_label=True,
            x_grid_label=True,
            padding=5,
            #xmin=0,
            #ymin=0,
            **graph_theme)
        box.add_widget(self.graph)
        self.plot = LinePlot(line_width=2, color=[0.48, 0.63, 0.11, 1])
        self.graph.add_plot(self.plot)
        self.salise = 0
        self.saniye = 0
        self.dakika = 0
        self.values = []

    def start(self):
        self.tan = Clock.schedule_interval(self.arttirici, 0.5)
        self.zam = Clock.schedule_interval(self.zamanlayici, 0.1)

    def stop(self):
        if self.tan:
            Clock.unschedule(self.tan)
        self.ids.current_temp.text = ""
        self.ids.slider_temp.value = 0
        self.ids.slider_fan.value = 0
        self.salise = 0
        self.saniye = 0
        self.dakika = 0
        self.graph.remove_plot(self.plot)
        self.etiketteGoster()
        if self.zam:
            Clock.unschedule(self.zam)

    def zamanlayici(self, za):
        self.salise += 1
        if self.salise == 10:
            self.salise = 0
            self.saniye += 1
        if self.saniye == 60:
            self.saniye = 0
            self.dakika += 1
        self.etiketteGoster()

    def etiketteGoster(self):

        self.ids.etiket.text = "%s:%s:%s" % (self.dakika, str(
            self.saniye).zfill(2), str(self.salise).zfill(2))

    def arttirici(self, za):
        derece = self.ids.slider_temp.value
        fan_deger = self.ids.slider_fan.value
        self.son_deger = derece - fan_deger / 2
        if len(self.values) >= 100:
            self.values = []
        self.values.append(self.son_deger)
        self.ids.current_temp.text = "%.2f" % self.son_deger
        self.plot.points = [(i, j) for i, j in enumerate(self.values)]
Beispiel #2
0
class StockDetailScreen(Screen):
    def __init__(self, **kwargs):
        super(StockDetailScreen, self).__init__(**kwargs)

        self.layout = CustomLayout()
        back_img = lm.createImage(source='images/back.png', rel_size=lm.rel_square(rel_width=.1))
        self.layout.add_item(CustomButton(back_img, on_release_func=lambda: back(self.manager), alignment='left'))

        self.stock_name = lm.createLabel(bold= False, rel_size= (1, .1), text_rel_size = (.95, .1), halign='center', valign='middle')
        self.layout.add_item(self.stock_name)

        self.layout.add_widget(Button(text="TRADE", bold=True, font_size=40,
                                        background_color=DARK_GREEN, on_release=self.trade),                                        
                                        rel_size=(.8, .1))

        self.current_price = lm.createLabel(font_size= 50, rel_size= (1, .1))

        self.layout.add_item(self.current_price)
        self.center_image = lm.createImage(source='images/up_arrow.png', rel_size=lm.rel_square(rel_width=.5))
        self.layout.add_item(self.center_image)

        self.stock_symbol = lm.createLabel(font_size= 50, rel_size= (1, .1))
        self.layout.add_item(self.stock_symbol)

        self.num_shares = lm.createLabel(font_size= 28, rel_size= (1, .1))
        self.layout.add_item(self.num_shares)

        self.layout.add_item(lm.createLabel(text = 'Past Week Performace', font_size= 28, rel_size= (1, .1)))

        # graph
        self.graph = Graph(x_ticks_major=1, tick_color = (0,0,0,.5), xlabel='Days',
              y_grid_label=True, x_grid_label=True, precision='%.2f', padding=5,
              x_grid=True, y_grid=True, xmin=-5, xmax=-1, ymin=0, ymax=100,
              border_color = (0,0,0,0), label_options = {'color': (0,0,0,1)})        

        self.plot = [(i-7, .3*i*100) for i in range(7)]

        self.layout.add_widget(self.graph, rel_size=(.90, .3))

        self.add_widget(self.layout.create())

    def on_pre_enter(self):
        position = Position(current_stock_symbol)
        position.update_price()
        self.stock_name.widget.text = symbol_data[position.tag]['NAME']
        self.current_price.widget.text = f'${position.current_price:,.2f}'
        self.stock_symbol.widget.text = position.tag
        self.center_image.widget.source = 'images/up_arrow.png' if position.day_change >= 0 else 'images/down_arrow.png'
        share_count = current_portfolio[position.tag].num_shares if current_portfolio[position.tag] else 0
        self.num_shares.widget.text = f'You own {share_count} share{"s" if share_count != 1 else ""}'
        self.plot_data(position.get_prev_week_data())

    def plot_data(self, data):
        """
        Change the data shown on the stock graph.
        data should be a list of tuples showing (x,y) pairs on the graph.
        """
        self.graph.ymax = max(price for _,price in data) * 1.01
        self.graph.ymin = min(price for _,price in data) * .99
        self.graph.y_ticks_major = (self.graph.ymax - self.graph.ymin) / 4
        self.graph.remove_plot(self.plot)
        self.plot = LinePlot(color=WHITE, line_width=3)
        self.plot.points = data
        self.graph.add_plot(self.plot)

    def trade(self, trade_button):
        """
        Enter the trade screen
        """
        global trade_mode
        trade_mode = 'BUY'
        screen_transition(self.manager, 'detail', 'trade', SLIDE_LEFT)
Beispiel #3
0
class mainApp(App):
    def build(self):
        self.icon = 'diceSmall.png'
        mainPanel = TabbedPanel()
        mainPanel.default_tab_text = 'Dice Roll'
        
        mainPanel.add_widget(self.tabProbabilityTesting())
        mainPanel.default_tab_content = self.tabDiceRoll()
        return mainPanel
    
    def addButtons(self,buttonList,buttonFunctionList,layout):
        for rowIndex, rowButtons in enumerate(buttonList):
            rowLayout = BoxLayout(
                size_hint = (1,1),
                padding = [0,0.5,0,0.5],
                )
            for buttonIndex, buttonLabel in enumerate(rowButtons):
                button = Button(
                    text = buttonLabel,
                    pos_hint={"center_x": 0.5, "center_y": 0.5},
                    size_hint = (1,1),
                    )
                button.bind(on_press=buttonFunctionList[rowIndex][buttonIndex])
                rowLayout.add_widget(button)
            layout.add_widget(rowLayout)
        
    def tabDiceRoll(self):
        mainLayout = BoxLayout(orientation = 'vertical')
        fontSize = 35
        labelList = ['d4','d6','d8','d10','d12','d20','d100','custom']
        self.diceDict = {}
        buttons = [
            ['Roll Inputs','Clear Inputs'],
            ['D20','2d6'],
            ['Roll Stats', '(dis)Advantage'],
            ]
        buttonFunctionList = [
            [self.fireRollButton, self.fireClearButton],
            [self.fireD20Button,self.fireMonopolyButton],
            [self.fireStatsButton, self.fireAdvantageButton], 
            ]
        
        for label in labelList:
            rowLayout = BoxLayout()
            self.kivyLabel = TextInput(
                text = label if label != 'custom' else '',
                hint_text = 'Custom dice' if label == 'custom' else '',
                background_color = [0.75,0.75,0.75,1],
                font_size = '{size}sp'.format(size = int(fontSize/2)),
                readonly = True if label != 'custom' else False,
                input_filter = 'int',
                size_hint = (0.3,1))
            self.diceDict[label] = numberInput(
                multiline = False, 
                font_size = '{size}sp'.format(size = int(fontSize/2)), 
                hint_text = 'Enter number of dice here',
                input_filter = 'int',
                size_hint = (0.7,1))
            rowLayout.add_widget(self.kivyLabel)
            rowLayout.add_widget(self.diceDict[label])
            mainLayout.add_widget(rowLayout)
            
        self.addButtons(buttons,buttonFunctionList,mainLayout)
            
        self.resultTextBox = TextInput(
            text = '\n\n\n\n\n',
            multiline = True, 
            readonly = True, 
            font_size = '{size}sp'.format(size = int(fontSize/2)),
            halign = 'left',
            background_color = [109/255,162/255,1,1],
            background_active = 'd20Skeleton.png',
            background_normal = 'd20Skeleton.png',
            size_hint = (0.9,8),
            pos_hint = {'center_x': .5, 'center_y': .5}
            )
        mainLayout.add_widget(self.resultTextBox)
        
        return mainLayout
    
    def tabProbabilityTesting(self):
        '''
        tab containing plot of probability distributions from various user-specificed pools of dice
        '''
        self.plotColorMap = [
            [1,0,0,1],
            [0,1,0,1],
            [0,0,1,1],
            [1,1,0,1],
            [1,0,1,1],
            [0,1,1,1],
            [1,1,1,1],
            [0.75,0.75,0.75,1]]
        tabProb = TabbedPanelItem(text = 'Prob. Plots')
        self.statsMainLayout = BoxLayout(orientation = 'vertical')
        self.entryLayout =  BoxLayout(orientation = 'vertical',size_hint = (1,0.45))
        buttonLayout = BoxLayout(size_hint = (1,0.05))
        self.graphLayout = BoxLayout(size_hint = (1,0.5))
        self.graph = Graph(xlabel='Value', ylabel='Counts', 
            x_ticks_minor=1,x_ticks_major=2, y_ticks_minor = 100, y_ticks_major=500,
            y_grid_label=True, x_grid_label=True, padding=5,
            x_grid=True, y_grid=True, xmin=-0, xmax=15, ymin=0,ymax = 5000)
        self.graphLayout.add_widget(self.graph)
        self.plotList = []
        self.statsMainLayout.add_widget(self.entryLayout)
        self.statsMainLayout.add_widget(buttonLayout)
        self.statsMainLayout.add_widget(self.graphLayout)
        self.testList = []
        self.appendNewTest(self.entryLayout,readOnly = True) 
        self.testList.append(self.appendNewTest(self.entryLayout)) 
        buttonList = [['Add New Test','Plot Results','Reset']]
        buttonFunctionList = [[self.fireNewTestButton,self.firePlotButton,self.fireResetTestsButton]]
        self.addButtons(buttonList,buttonFunctionList,buttonLayout)
        tabProb.add_widget(self.statsMainLayout)
        return tabProb    
    
    def appendNewTest(self,layout,readOnly = False):
        '''
        Add new test to probability distribution tab
        readOnly should be true for the first line to give a header column, and false thereafter
        '''
        rowLayout = BoxLayout(orientation = 'horizontal',)
        testIndex = len(self.testList) if not readOnly else -1
        testDice = TextInput(
            hint_text = 'Dice', 
            text = 'Enter dice list' if readOnly else '',
            readonly = readOnly,
            background_color = self.plotColorMap[testIndex])
        testBonus = numberInput(
            hint_text = 'Bonus', 
            text = 'Enter fixed bonus' if readOnly else '',
            readonly = readOnly,
            background_color = self.plotColorMap[testIndex])
        testMode = numberInput(
            hint_text = 'Expected Value (output)', 
            text = 'Expected Value' if readOnly else '',
            readonly = True,
            background_color = self.plotColorMap[testIndex])
        rowLayout.add_widget(testDice)
        rowLayout.add_widget(testBonus)
        rowLayout.add_widget(testMode)
        layout.add_widget(rowLayout)
        return(testDice,testBonus,testMode,rowLayout)
    
        
    def createPlot(self):
        '''
        Clear existing plots, and add a plot for each specificed test to the graph.
        Each will be colour coded to match the background of the test rowButtons
        Could (should!) do actual stats stuff to get expected values of each dice pool, but 10k tests takes negligible time even on a phone
        '''
        for plot in self.plotList:
            self.graph.remove_plot(plot)
        self.plotList = []
        maxCount = [500]
        maxValue = [6]
        numberOfTests = 10000
        for index, test in enumerate(self.testList):
            outcomes = {}
            diceList = re.findall(r"[\w\d']+", test[0].text)
            if len(diceList) > 0:
                bonus = 0 if len(test[1].text) == 0 else int(test[1].text)
                for i in range(0,numberOfTests):
                    diceResult = diceRoll.rollDicePool(diceList = diceList, bonus = bonus)[0]
                    if diceResult in outcomes:
                        outcomes[diceResult]+=1
                    else:
                        outcomes[diceResult] = 1
                plot = MeshLinePlot(color=self.plotColorMap[index],mode = 'points')
                xList = [key for key in outcomes]
                xList.sort()
                plot.points = [(key, outcomes[key]) for key in xList]
                maxCount.append(max(outcomes[key] for key in outcomes))
                maxValue.append(max(key for key in outcomes))
                expectedValue = round(sum([key * outcomes[key] for key in outcomes])/numberOfTests,1)
                test[2].text = str(expectedValue) if expectedValue != 0 else 'Error with {diceList}'.format(diceList = diceList)
                self.results=outcomes
                self.graph.add_plot(plot)
                self.plotList.append(plot)
        #Pad graph axes to make maximum values clear and prevent them being lost over the edge of the visible space
        self.graph.xmax = max(maxValue) + 2
        self.graph.ymax = max(maxCount) + 500
    
    
    def diceRollOutput(self,diceList):
        dicePool =  diceRoll.dicePool(diceList = diceList)
        dicePool.roll()
        message = dicePool.diceString+ ' \n'
        for diceType in dicePool.resultDict:
            message += 'd{diceType} : {diceResults}\n'.format(
                diceType = diceType,
                diceResults = ', '.join(map(str,dicePool.resultDict[diceType])))
        message += 'Result: ' + str(dicePool.rollSum)
        return message
    
    def fireRollButton(self,instance):
        diceList = []
        for dice in self.diceDict:
            no_of_dice = self.diceDict[dice].text
            if dice == 'custom':
                dice = 'd' + self.kivyLabel.text.lower()
            if no_of_dice != '' and no_of_dice != 0:
                diceList.append(no_of_dice + dice)
        if len(diceList) > 0:
            message = self.diceRollOutput(diceList)
            self.resultTextBox.text = '\n' + message + '\n'
        
    def fireD20Button(self,instance):
        result = diceRoll.rollDice(20)
        self.resultTextBox.text = '\n\nRolled 1d20\nResult: ' + str(result) + '\n'
         
    def fireAdvantageButton(self,instance,sides=20):
        result1 = diceRoll.rollDice(sides)
        result2 = diceRoll.rollDice(sides)
        message = 'Rolled 2d{sides}\nResult: {r1} , {r2}'.format(sides = sides, r1 = result1, r2 = result2)
        self.resultTextBox.text = '\n\n' + message + '\n'
    
    def fireMonopolyButton(self,instance):
        message = self.diceRollOutput(['2d6'])
        self.resultTextBox.text = '\n' + message + '\n'
       
    def fireStatsButton(self,instance):
        stats = diceRoll.rollStats(6)
        outString = 'Stats rolled : \n' + str(stats[0]) + ' ' + str(stats[1]) + ' ' + str(stats[2]) +'\n' + str(stats[3]) + ' ' + str(stats[4]) + ' ' + str(stats[5])
        outString += '\nAverage of ' + str(np.round( sum(stats) / len(stats) , 2))
        self.resultTextBox.text = '\n' + outString + '\n'
        
    def fireClearButton(self,instance):
        for label in self.diceDict:
            self.diceDict[label].text = ''
        self.kivyLabel.text = ''
        
    def fireNewTestButton(self,instance):
        if len(self.testList) <7:
            self.testList.append(self.appendNewTest(self.entryLayout)) 
            
    def fireResetTestsButton(self,instance):
        while len(self.testList) >1:
            self.entryLayout.remove_widget(self.testList.pop()[-1])
        for textBox in self.testList[0]:
            textBox.text = ''
        for plot in self.plotList:
            self.graph.remove_plot(plot)
    
    def firePlotButton(self,instance):
        self.createPlot()