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.


    Extracting the Date from the UI date and time spinner

    Pythonista
    6
    39
    23311
    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.
    • ccc
      ccc last edited by ccc

      This post is deleted!
      1 Reply Last reply Reply Quote 0
      • JonB
        JonB @abushama last edited by JonB

        @abushama You seem to be calling calndr() from within calndr(). calndr() does not return a date.
        Reread each line in your code and remind yourself what you are trying to achieve with each line...

        abushama 1 Reply Last reply Reply Quote 2
        • abushama
          abushama @Phuket2 last edited by

          @Phuket2
          i think u got my point
          but when i ran the code
          this kide or error pop up

          AttributeError: '_ui.DatePicker' object has no attribute 'year'
          which is:-
          year=date.year

          Phuket2 1 Reply Last reply Reply Quote 0
          • abushama
            abushama @JonB last edited by

            @JonB
            i did but am still straggling
            how to extract time from calnder celnder and write it in csv file
            that all what am trying😔

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

              @abushama. One proble was you were setting the date to be the ui.DatePicker, not the attr date. You can see I changed it in your function (I missed that originally, I didn't run the code because you didn't post it all, so it was easy for me to miss that). Look, I just got it working in the way i thought you had tried to get it working.
              Your calndr function looks likes it would make more sense for you to put it into the class as a method.
              I am often torn about how to help. If I make too many changes it can be frustrating to the person and confuse them more. @JonB point about going over your code is very valid. The way you where calling your function again inside trying to write to the csv file was way off. So I would suggest to take what i have done and try to move your calndr function into the class. Then really look closely about what's happening when you are writing out your csv file.
              You are not that far away. Just take your time and really look through it. Still here if you need more help.
              EDIT please see the notes I added to the bottom of the post.

              import ui
              import csv
              
              def calndr(sender):
              	date = sender.superview['calender'].date
              	# assuming that is the name of your DatePicker object
              	# this returns a datetime.datetime object, see module datetime
              	
              	year = date.year
              	month = date.month
              	
              	with open('newfile.csv', 'a') as csvfile:
              		fieldnames = ['Year', 'Month']
              		writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
              		writer.writerow({'Year': year, 'Month': month})
              		
              class MyClass(ui.View):
              	def __init__(self, *args, **kwargs):
              		super().__init__(*args, **kwargs)
              		self.make_view()
              		
              	def make_view(self):
              		dtp = ui.DatePicker(name='calender')
              		
              		btn = ui.Button(width=80,
              						height=32,
              						border_width=.5,
              						bg_color='white',
              						corner_radius=.5,
              						action=calndr)
              		btn.title='Save'
              		# just position the button
              		btn.center = self.center
              		btn.y = self.height - (btn.height * 2)
              		
              		# add the date picker & btn to the view
              		self.add_subview(dtp)
              		self.add_subview(btn)
              		
              if __name__ == '__main__':
              	f = (0, 0, 300, 400)
              	v = MyClass(frame = f)
              	v.present(style='sheet', animated=False)
              

              Btw, the csv writer has other methods, for example to write out the headers to you file. Of course you would only want to write the headers once. Oh, I should have mentioned, you should not really move your calndr function into the class the way it is. It really should be a method that just is passed you year and month and writes out the csv file. You really don't want the other objects around. If you make the method just to handle the csv file, you could for example reuse that code in other objects.

              abushama 2 Replies Last reply Reply Quote 0
              • abushama
                abushama @Phuket2 last edited by ccc

                @Phuket2
                thank u very much
                i got this one too i would like to share
                and advice my
                i kw this not like ur prof one u did but this what i came with

                import ui
                import datetime
                import csv
                
                
                def calc(sender):
                		v = sender.superview
                		GontarHighPrice = v['txt1'].text
                		GontarLowPrice = v['txt2'].text
                		DollerRate =v ['txt3'].text
                		
                		Avg = (float(GontarHighPrice)+float(GontarLowPrice))/2
                		TonInPounds=22.25*Avg
                		SPound="{0:,.2f}".format(TonInPounds)
                		v['txt4'].text=str(SPound)
                		
                		TonInDollars=TonInPounds/float(DollerRate)
                		v['txt5'].text=str("{0:,.2f}".format(TonInDollars))
                		#TID=v['txt5'].text
                		
                		ProtSudan=TonInDollars+150
                		v['txt6'].text="{0:,.2f}".format(ProtSudan)
                		#FOB=v['txt6'].text
                		
                		C_F= ProtSudan+50
                		v['txt7'].text="{0:,.2f}".format(C_F)
                		#CF=v['txt7'].text
                		
                		Amman= C_F+120
                		v['txt8'].text="{0:,.2f}".format(Amman)
                		Amm=v['txt8'].text
                		
                		with open('newfile1.csv', 'a') as csvfile1:
                					fieldnames = ['Amm']
                					writer = csv.DictWriter(csvfile1, fieldnames=fieldnames)
                					writer.writerow({'Amm': Amm})
                					csvfile1.close()
                		
                def calndr(sender):
                		Mydate=sender.date
                		day = Mydate.day
                		month = Mydate.month
                		year = Mydate.year
                	
                		with open('newfile1.csv', 'a') as csvfile1:
                				fieldnames = ['Day', 'Month','Year']
                				writer = csv.DictWriter(csvfile1, fieldnames=fieldnames)
                				writer.writerow({'Day': day, 'Month': month,'Year': year})
                				csvfile1.close()
                
                		v = ui.load_view()
                		v.present('sheet')
                
                1 Reply Last reply Reply Quote 0
                • abushama
                  abushama @Phuket2 last edited by

                  @Phuket2
                  As u notice that i got 2 open files to append
                  the data of
                  Amm and the date which i got from calender celnder

                  all i need is to make it one open file
                  which i can append in one csv file to covert to excel sheet

                  ?????

                  Phuket2 2 Replies Last reply Reply Quote 0
                  • Phuket2
                    Phuket2 @abushama last edited by

                    @abushama, I am not really sure what your final result is suppose to look like. But if you look below there is a function that will take a dict and write it out to the csv file. So that function is only concerned with your csv file, not how to get the data. If you call the function over and over it will be ok, because we only write the headers out once.

                    import csv
                    import os
                    
                    def write_to_csv(filename, data_dict):
                    	
                    	fexists = os.path.exists(filename) # We set a var to see if the file exists 
                    	
                    	fieldnames = list(my_data_dict.keys()) # get a list of the keys to use as the header
                    	with open(filename, 'a') as csvfile:
                    			#fieldnames = ['Day', 'Month','Year']
                    			writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
                    			
                    			# only write the header one time to the file
                    			if not fexists:
                    				writer.writeheader()
                    				
                    			writer.writerow(data_dict)
                    			#svfile1.close()  # you dont need this.  Look up Context Managers
                    
                    if __name__ == '__main__':
                    	filename = 'my_csv.csv'
                    	
                    	# you would collect your data and put it in a dict.
                    	my_data_dict = dict(
                    		Day = 1,
                    		Month = 12,
                    		Year = 2017,
                    		Amm = 2.5	
                    	)
                    	
                    	write_to_csv(filename , my_data_dict)
                    	
                    	# just print the file to the console, make sure its what we wanted
                    	with open(filename) as f:
                    		for line in f:
                    			print(line, end='')	
                    
                    abushama 1 Reply Last reply Reply Quote 0
                    • abushama
                      abushama @Phuket2 last edited by

                      @Phuket2
                      am trying to post a screenshot of my UI but i dont know how??
                      may u send my ur email plz?

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

                        Posting images to this forum works like this: https://guides.github.com/features/mastering-markdown/#examples

                        1 Reply Last reply Reply Quote 1
                        • Phuket2
                          Phuket2 @abushama last edited by

                          @abushama, I had already done this before I seen @ccc post. But this also should help

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

                            Perhaps the missing magic in this thread is seek() which lets you open a file in 'a' mode and then seek to the beginning of that file (append_file.seek(0, 0)) so that you can read() its contents and then seek to the end of the file (append_file.seek(0, 2)) and then write() new content to the end of the file.

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

                              @ccc, I am a little baffled. I was going to suggest he could call the function twice with different files names then have another small function to combine the files or create a 3rd file. Whatever is needed.
                              Maybe should have done this without using csv to help for clarity the fact its only a comma separated file with some headings if required. The csv writer in this case would only help with imbedded quotes in strings etc...In this simple case.
                              The dict writer is nice though.
                              We will see what @abushama comes back with.

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

                                It seems to me, he wants a function which gathers the entire UI state, not just the sender, and write that to csv.

                                i.e there should be a utility function which gets the date, as well as everything else he wants in the file, and writes the csv. Then both callbacks call the exact same function.

                                abushama 1 Reply Last reply Reply Quote 1
                                • Phuket2
                                  Phuket2 @abushama last edited by

                                  @abushama, how are you going? Did you get a solution? I think i read too quickly, I see what @JonB is saying now.
                                  The functions that have been written should show you the way how to achieve what you want. Let us know if you got what you wanted working or not

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


                                    this is my UI simple project
                                    i hope this give u a quite view

                                    1 Reply Last reply Reply Quote 0
                                    • abushama
                                      abushama @JonB last edited by

                                      @JonB
                                      exatly..i input
                                      the first three cells
                                      GontarHighPrice
                                      GontarLowPrice
                                      and the doller rate
                                      then i tunning the calnder celnder to the certin date and press calculate botton
                                      this action will calculte other cells in the UI and in the same time will save this all this data + the date in one csv

                                      what i got so far in the code above all the data and the date
                                      but not in one order

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

                                        @abushama , as far as I can see the below should do want you want, if not pretty close to it. I am not saying its the prettiest way to do it. But its very procedural. Hopefully its easy to follow. There are always many solutions to a given problem. I have tried to do it as I imagine you are doing it. I mean by loading the form. I assume in the designer for the button you are setting the action to call.
                                        For the below to work you need to set your calc button's action to calc_button_action and you have to set the my_screen_fn to the name of your view you made in the Designer.

                                        import ui
                                        from os.path import exists 
                                        import csv
                                        
                                        _csv_filename = 'myoutput.csv'
                                        		
                                        def calc_button_action(sender):
                                        	'''
                                        		Here your calc button will call 3 functions.
                                        		1. do_calculations, so you calculate and put the values in your fields
                                        		2. collect_data, will collect all the data from your view and return it as a dict
                                        		3. write_to_csv, you pass your dict you collected the data into and it will be written
                                        		to the csv file.  The file name is at the top of the file.  You could ask for the name
                                        		of the file for example.
                                        	'''
                                        	
                                        	# v is set to your view, so you can access the other objects on you view now and
                                        	# pass your view to other functions!
                                        	v = sender.superview
                                        	
                                        	do_calculations(v)
                                        	data_dict = collect_data(v)
                                        	write_to_csv(_csv_filename, data_dict)
                                        	
                                        def do_calculations(v):
                                        	'''
                                        		in here, just do your calculations and update
                                        		your fields with the calculated data
                                        	'''
                                        	# so to access the date in the ui.DatePicker, lets say its name is cal
                                        	the_date = v['cal'].date
                                        	
                                        	# you can access all your objects as above.
                                        	
                                        	v['txt9'].text = str(10 * 2) # whatever you calculate
                                        	
                                        	
                                        def collect_data(v):
                                        	'''
                                        		in here you are only intrested in collecting your data from the view
                                        		again, you have the view so you can access your fields.
                                        		I have used a dict here to collect the information.
                                        		I think if you are using py 3.6, your dict will keep its order as you add your items
                                        	'''
                                        	
                                        	# I have only filled in a few fields here. But you would add everything from your view
                                        	# you wanted written out to the csv.  Add the items in the order you want them written 
                                        	# to the csv file.
                                        	d = dict(
                                        			Year = v['cal'].date.year,
                                        			Month = v['cal'].date.month,
                                        			Day = v['cal'].date.day,
                                        			HighPrice = v['txt9'].text,
                                        			)
                                        	return d
                                        	
                                        def write_to_csv(filename, data_dict):
                                        	'''
                                        		This function is only concerned with writing your dict to the csv file.  
                                        	'''
                                        	fexists=exists(filename) # We set a var to see if the file exists
                                        	
                                        	fieldnames = list(data_dict.keys()) # get a list of the keys to use as the header
                                        	with open(filename, 'a') as csvfile:
                                        		writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
                                        		# only write the header one time to the file
                                        		if not fexists:
                                        			writer.writeheader()
                                        			
                                        		writer.writerow(data_dict)
                                        		
                                        		
                                        if __name__ == '__main__':
                                        	my_screen_fn = 'someview.pyui'
                                        	v = ui.load_view(my_screen_fn)
                                        	v.present(style='sheet', animated=False)
                                        
                                        abushama 1 Reply Last reply Reply Quote 0
                                        • abushama
                                          abushama @Phuket2 last edited by

                                          @Phuket2
                                          thank you very much
                                          frist thing u taught how to
                                          combin funtions in my UI which i do not know before.
                                          2)ur code is so clear to get the idea simply

                                          as u side am not using 3.6 python so the output of dic are not orginize as i put in my code
                                          so any idea..?

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

                                            @abushama , ok here is a version that is using lists. I thought it should be a simple change to use OrderDict, but I had a problem to get it to work. So i used a list instead :(

                                            import ui
                                            from os.path import exists 
                                            import csv
                                            
                                            _csv_filename = 'myoutputV2.csv'
                                            		
                                            def calc_button_action(sender):
                                            	'''
                                            		Here your calc button will call 3 functions.
                                            		1. do_calculations, so you calculate and put the values in your fields
                                            		2. collect_data, will collect all the data from your view and return it as a list
                                            		3. write_to_csv, you pass your list you collected the data into and it will be written
                                            		to the csv file.  The file name is at the top of the file.  You could ask for the name
                                            		of the file for example.
                                            	'''
                                            	
                                            	# v is set to your view, so you can access the other objects on you view now and
                                            	# pass your view to other functions!
                                            	v = sender.superview
                                            	
                                            	do_calculations(v)
                                            	data_list = collect_data(v)
                                            	write_to_csv(_csv_filename, data_list , ['Year', 'Month', 'Day', 'HighPrice'])
                                            	
                                            def do_calculations(v):
                                            	'''
                                            		in here, just do your calculations and update
                                            		your fields with the calculated data
                                            	'''
                                            	# so to access the date in the ui.DatePicker, lets say its name is cal
                                            	the_date = v['cal'].date
                                            	
                                            	# you can access all your objects as above.
                                            	
                                            	v['txt9'].text = str(10 * 2) # whatever you calculate
                                            	
                                            	
                                            def collect_data(v):
                                            	'''
                                            		in here you are only intrested in collecting your data from the view
                                            		again, you have the view so you can access your fields.
                                            		Using a list now! not a dict
                                            	'''
                                            	lst = []
                                            	# I have only filled in a few fields here. But you would add everything from your view
                                            	# you wanted written out to the csv.  Add the items in the order you want them written 
                                            	# to the csv file.
                                            	lst.append(v['cal'].date.year)
                                            	lst.append(v['cal'].date.month)
                                            	lst.append(v['cal'].date.day)
                                            	lst.append(v['txt9'].text)
                                            	
                                            	return lst
                                            	
                                            def write_to_csv(filename, data_list, field_name_list=None):
                                            	'''
                                            		This function is only concerned with writing your list to the csv file.  
                                            	'''
                                            	fexists=exists(filename) # We set a var to see if the file exists
                                            	
                                            	
                                            	with open(filename, 'a') as csvfile:
                                            		csvwriter = csv.writer(csvfile, delimiter=',',
                                            							quotechar='|', quoting=csv.QUOTE_MINIMAL)
                                            		
                                            		if not fexists and field_name_list != None:
                                            			csvwriter.writerow(field_name_list)
                                            			
                                            		csvwriter.writerow(data_list)	
                                            		
                                            if __name__ == '__main__':
                                            	my_screen_fn = 'someview.pyui'
                                            	v = ui.load_view(my_screen_fn)
                                            	v.present(style='sheet', animated=False)
                                            
                                            abushama 2 Replies Last reply Reply Quote 0
                                            • First post
                                              Last post
                                            Powered by NodeBB Forums | Contributors