Date Selection directly in the code?

This is the place for queries that don't fit in any of the other categories.

Date Selection directly in the code?

Postby Vihral » Tue Apr 01, 2014 9:39 am

Hi

Im brand new to python and im working on a script to run to automatically download some files from a set of URL's. In these URL's i have put brackets that I have defined since these 3 lines in the url needs to changed (1 unix timestamp, start date and end date). This is all fixed.

Now ive gotten it to work with the following:

Code: Select all
def get_file(url, filename):


   end_date = datetime.now().strftime("%Y-%m-%d")
   start_date = datetime.now()-timedelta(days=7)
   start_date = start_date.strftime("%Y-%m-%d")
   current_timestamp = int(time())
   formatted_url = url.format(current_timestamp, start_date, end_date)
   print(start_date)
   print(end_date)
   print(formatted_url)

   data = requests.get(formatted_url)
   data.raise_for_status()

   with open(filename, "wb") as f:
      f.write(data.content)


This takes todays date and 7 days in the past.

Now what i would like to do is change this to be changeable? So I can set my own dates instead.

Eventually I want to make a GUI on this (I have no idea where to start with the GUI but ill get there eventually!).

Any ideas on how i cancode this so that I can choose dates?

It would be prefered that in the code I just open it and then have a certain string i manually set the date and then run the script.

I may be rambling but hopefully someone knows what to do.
Last edited by stranac on Tue Apr 01, 2014 9:45 am, edited 1 time in total.
Reason: First post lock.
Vihral
 
Posts: 2
Joined: Tue Apr 01, 2014 9:36 am

Re: Date Selection directly in the code?

Postby stranac » Tue Apr 01, 2014 9:50 am

Without a GUI, raw_input() and datetime.datetime.strptime().
GUI toolkits usually have stuff for selecting dates, so you shouldn't have a problem when you decide to use a GUI.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1093
Joined: Thu Feb 07, 2013 3:42 pm

Re: Date Selection directly in the code?

Postby Vihral » Tue Apr 01, 2014 10:06 am

stranac wrote:Without a GUI, raw_input() and datetime.datetime.strptime().
GUI toolkits usually have stuff for selecting dates, so you shouldn't have a problem when you decide to use a GUI.



thanks, if I want to go over to a GUI sooner rather then later, what would I need to learn? What I want is a simple GUI with 2 boxes for STart date and end-date and then a button to start the script (which then changes the value in the URL to the dates selected in the boxes).

I looked around a bit and found something called tkinter, what is this exactly? I tried following a guide on youtube (running 2.7, i am running 3.4, windows 7 32-bit) and got it to open an empty window but when i try to add stuff like
Code: Select all
app,grid
or similar things (tried label aswell just to get some text in it and button) it wont start up anymore. Cant see whats wrong since the command prompt dissapears so quickly.

If I have further questions regarding the GUI ill create a topic on that in the GUI section to keep the forums clean as the rules state! :)

Thanks in advance!
Vihral
 
Posts: 2
Joined: Tue Apr 01, 2014 9:36 am

Re: Date Selection directly in the code?

Postby stranac » Tue Apr 01, 2014 11:40 am

Tkinter is one of the many GUI libraries available.
It's pretty simple, but very basic(I don't think it has a date picker widget like many others do).
Vihral wrote:Cant see whats wrong since the command prompt dissapears so quickly.

Sounds like you're not executing python properly.
You should check out this tutorial: Executing Python
Scroll down to the Command Line / Terminal section, and read it.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1093
Joined: Thu Feb 07, 2013 3:42 pm

Re: Date Selection directly in the code?

Postby metulburr » Tue Apr 01, 2014 12:13 pm

here is a raw calander class, but it needs tweaking.

Code: Select all

import calendar
import datetime
import sys
if sys.version[0] == '2':
    import Tkinter as tk
else:
    import tkinter as tk

class Calendar:
    def __init__(self, parent, values):
        self.values = values
        self.parent = parent
        self.cal = calendar.TextCalendar(calendar.SUNDAY)
        self.year = datetime.date.today().year
        self.month = datetime.date.today().month
        self.wid = []
        self.day_selected = 1
        self.month_selected = self.month
        self.year_selected = self.year
        self.day_name = ''
       
        self.setup(self.year, self.month)
       
    def clear(self):
        for w in self.wid[:]:
            w.grid_forget()
            #w.destroy()
            self.wid.remove(w)
   
    def go_prev(self):
        if self.month > 1:
            self.month -= 1
        else:
            self.month = 12
            self.year -= 1
        #self.selected = (self.month, self.year)
        self.clear()
        self.setup(self.year, self.month)

    def go_next(self):
        if self.month < 12:
            self.month += 1
        else:
            self.month = 1
            self.year += 1
       
        #self.selected = (self.month, self.year)
        self.clear()
        self.setup(self.year, self.month)
       
    def selection(self, day, name):
        self.day_selected = day
        self.month_selected = self.month
        self.year_selected = self.year
        self.day_name = name
       
        #data
        self.values['day_selected'] = day
        self.values['month_selected'] = self.month
        self.values['year_selected'] = self.year
        self.values['day_name'] = name
        self.values['month_name'] = calendar.month_name[self.month_selected]
       
        self.clear()
        self.setup(self.year, self.month)
       
    def setup(self, y, m):
        left = tk.Button(self.parent, text='<', command=self.go_prev)
        self.wid.append(left)
        left.grid(row=0, column=1)
       
        header = tk.Label(self.parent, height=2, text='{}   {}'.format(calendar.month_abbr[m], str(y)))
        self.wid.append(header)
        header.grid(row=0, column=2, columnspan=3)
       
        right = tk.Button(self.parent, text='>', command=self.go_next)
        self.wid.append(right)
        right.grid(row=0, column=5)
       
        days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
        for num, name in enumerate(days):
            t = tk.Label(self.parent, text=name[:3])
            self.wid.append(t)
            t.grid(row=1, column=num)
       
        for w, week in enumerate(self.cal.monthdayscalendar(y, m), 2):
            for d, day in enumerate(week):
                if day:
                    #print(calendar.day_name[day])
                    b = tk.Button(self.parent, width=1, text=day, command=lambda day=day:self.selection(day, calendar.day_name[(day-1) % 7]))
                    self.wid.append(b)
                    b.grid(row=w, column=d)
                   
        sel = tk.Label(self.parent, height=2, text='{} {} {} {}'.format(
            self.day_name, calendar.month_name[self.month_selected], self.day_selected, self.year_selected))
        self.wid.append(sel)
        sel.grid(row=8, column=0, columnspan=7)
       
        ok = tk.Button(self.parent, width=5, text='OK', command=self.kill_and_save)
        self.wid.append(ok)
        ok.grid(row=9, column=2, columnspan=3, pady=10)
       
    def kill_and_save(self):
        self.parent.destroy()


if __name__ == '__main__':

    class Control:
        def __init__(self, parent):
            self.parent = parent
            self.choose_btn = tk.Button(self.parent, text='Choose',command=self.popup)
            self.show_btn = tk.Button(self.parent, text='Show Selected',command=self.print_selected_date)
            self.choose_btn.grid()
            self.show_btn.grid()
            self.data = {}
           
        def popup(self):
            child = tk.Toplevel()
            cal = Calendar(child, self.data)
           
        def print_selected_date(self):
            print(self.data)
           

    root = tk.Tk()
    app = Control(root)
    root.mainloop()

       
 

New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1319
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY


Return to General Coding Help

Who is online

Users browsing this forum: Crimson King, Mekire and 3 guests