omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    A class just to draw a grid of buttons. Has some interesting options

    Pythonista
    2
    11
    4837
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Phuket2
      Phuket2 last edited by

      Just a class that draws a grid. _COLS & _ROWS determine the grid size. Also in the init function where the buttons are created have a var __USE_COPY. if True is consistently , 80, 90% faster on my iPad Air 2.

      
      import ui
      import copy
      import time
      
      _ROWS = 16
      _COLS = 16
      
      # class, just to draw a grid of button, also
      # should rotate.
       
      class grid(ui.View):
      	def __init__(self):
      		self.btns = []
      		
      		# just create the buttons rows * columns
      		__USE_COPY = False        
      		if not __USE_COPY:
      			start = time.time()
      			for i in range((_COLS * _ROWS)  ):
      				btn = ui.Button(title = str(i))
      				btn.action = self.hit_test
      				self.btns.append(btn)
      				self.add_subview(btn)
      			finish = time.time()
      		else:
      			start = time.time()
      			btn = ui.Button()
      			for i in range((_COLS * _ROWS)  ):
      				new_btn = copy.copy(btn)
      				new_btn.title = str(i)
      				new_btn.action = self.hit_test
      				self.btns.append(new_btn)
      				self.add_subview(new_btn)
      			finish = time.time()
      		print finish - start 
      			
      		self.style()
      			
      	def style(self):
      		self.background_color = 'white'
      		
      		for btn in self.btns:
      			btn.background_color = 'red'
      			btn.tint_color = 'white'
      			btn.border_width = .5
      		
      	
      		
      	def layout(self):
      		if self.superview:
      			self.frame = superview.bounds
      		
      		w,h = self.width / _COLS, self.height / _ROWS
      		x = y = 0
      		for btn in self.btns:
      			btn.width, btn.height = w,h
      			btn.x, btn.y = x * w, y * h
      			x += 1
      			if not x % _COLS :
      				x = 0
      				y += 1
      	
      	
      	def hit_test(self, sender):
      		print 'hit - ', sender.title
      		
      if __name__ == '__main__':
      	x = grid().present('')
      
      1 Reply Last reply Reply Quote 0
      • ccc
        ccc last edited by

        When timing things, I like to use elapsed_time.py which allows you to write:

        with timer("download Pythonista Forums page"):
            html = requests.get('https://omz-forums.appspot.com').text
        

        and it will print Elapsed time (download Pythonista Forums page): 0:00:00.513742

        1 Reply Last reply Reply Quote 0
        • ccc
          ccc last edited by

          I ran some timings and did not see much difference between the two approaches:

          Elapsed time (True):  0:00:00.285801
          Elapsed time (True):  0:00:00.258893
          Elapsed time (True):  0:00:00.270147
          Elapsed time (True):  0:00:00.289374
          Elapsed time (True):  0:00:00.283184 --> avg. 0.2774798 --> 8.56% faster
          
          Elapsed time (False): 0:00:00.369390
          Elapsed time (False): 0:00:00.278358
          Elapsed time (False): 0:00:00.288801
          Elapsed time (False): 0:00:00.280331
          Elapsed time (False): 0:00:00.289258 --> avg. 0.3012276
          

          In both cases it almost always takes less than a third of a second to build all the cells.

          1 Reply Last reply Reply Quote 0
          • Phuket2
            Phuket2 last edited by

            @ccc, I will run some timings later. I am sure I seen 80 to 90% increase. I did run it multiple times to make sure it wasn't a caching issue. Oh, well, I will just split them out 2 Functions and run the timings as you have with your timer function to see.

            I ran it on an iPad 2 air, what did you run it on? I will also try my iphone 6 later, will be interesting to compare.

            Thanks again

            1 Reply Last reply Reply Quote 0
            • ccc
              ccc last edited by

              import platform ; platform.platform() # --> 'Darwin-14.0.0-iPad3,4-32bit'

              I am running iOS 8.3 so why is it still 32-bit??

              1 Reply Last reply Reply Quote 0
              • Phuket2
                Phuket2 last edited by

                Elapsed time (False 16x16): 0:00:00.199482
                Elapsed time (False 16x16): 0:00:00.221685
                Elapsed time (False 16x16): 0:00:00.176133
                Elapsed time (False 16x16): 0:00:00.166889
                Elapsed time (False 16x16): 0:00:00.192668
                avg 191371

                Elapsed time (True 16x16): 0:00:00.106144
                Elapsed time (True 16x16): 0:00:00.103462
                Elapsed time (True 16x16): 0:00:00.121247
                Elapsed time (True 16x16): 0:00:00.109012
                Elapsed time (True 16x16): 0:00:00.104777
                avg 108,928
                approx 43% speed increase

                Darwin-14.0.0-iPad5,4-32bit
                not sure why 32bit, this is what i get

                1 Reply Last reply Reply Quote 0
                • Phuket2
                  Phuket2 last edited by

                  Sorry, tried to fix the formatting 3 times without luck

                  1 Reply Last reply Reply Quote 0
                  • Phuket2
                    Phuket2 last edited by

                    @ccc, I don't understand the contextlib at all. I sort of think I know what your timer code is doing. I run the below code and it seems ok. Wanted to ask you if the below code is ok, or are there side effects I don't understand?
                    the code below can not run, it's just the concept

                    if __name__ == '__main__':
                    	def month_changing(sender, d)	:
                    		if _DEBUG :print 'holy crap, month will change', sender, d
                    	
                    	
                    	with timer('Create Calender'):
                    		d = datetime.datetime(2015,11, 5)
                    		x = cal_view(200, date = d )
                    		with timer('set an attribute'):
                    			x.day_widgets[15].day_ind_visible(True)
                    		x.month_will_change = month_changing
                    		x.present('sheet')
                    
                    1 Reply Last reply Reply Quote 0
                    • Phuket2
                      Phuket2 last edited by

                      To mix it up a little further.... I assume it all works. Just want to double check.

                      
                      if __name__ == '__main__':
                      	def month_changing(sender, d)	:
                      		with timer('Month Changing'):
                      			if _DEBUG :print 'holy crap, month will change', sender, d
                      			pass
                      	
                      	
                      	with timer('Create Calender'):
                      		d = datetime.datetime(2015,11, 5)
                      		x = cal_view(200, date = d )
                      		with timer('set an attribute'):
                      			x.day_widgets[15].day_ind_visible(True)
                      		x.month_will_change = month_changing
                      		x.present('sheet')
                      
                      1 Reply Last reply Reply Quote 0
                      • ccc
                        ccc last edited by

                        If the code above could run then it would run just as you expect it to run. :-)

                        The contextlib stuff is supercool in a mind bending way because it allows you to build your own with xxx: context managers thru the magic of the builtin yield command which is the like the quantum leap of Python. Yield and yield from are not easy to get fixed in you mind but they can be quite powerful once mastered.

                        1 Reply Last reply Reply Quote 0
                        • Phuket2
                          Phuket2 last edited by

                          @ccc, ok thanks. Still learning the simple stuff at the moment. But will put it on my to learn list, it's a big list :) I am sure you got my meaning. The code runs, just too much to include here, wanted to show a real example rather than some test code.
                          Thanks...

                          1 Reply Last reply Reply Quote 0
                          • First post
                            Last post
                          Powered by NodeBB Forums | Contributors