Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Random Date and Date Range do the same thing #2

Open
TokynBlast opened this issue Jun 8, 2023 · 0 comments
Open

Random Date and Date Range do the same thing #2

TokynBlast opened this issue Jun 8, 2023 · 0 comments

Comments

@TokynBlast
Copy link
Owner

TokynBlast commented Jun 8, 2023

All though, it may not look like it, they do the exact same thing, and I don't know which one is more reliable (Error handling and simplicity):

    def gen_date(self):
        start_date = simpledialog.askstring("Set Date Range", "Enter start date (MM/DD/YYYY):")
        end_date = simpledialog.askstring("Set Date Range", "Enter end date (MM/DD/YYYY):")
        if start_date and end_date:
            start_date = datetime.strptime(start_date, "%m/%d/%Y")
            end_date = datetime.strptime(end_date, "%m/%d/%Y")
            delta = (end_date - start_date).days
            random_days = random.randint(0, delta)
            random_date = start_date + timedelta(days=random_days)
            random_date = max(random_date, start_date)
            random_date = min(random_date, end_date)

Or

    def date_range(self):
        start = simpledialog.askstring("Set Date Range", "Enter start date (MM/DD/YYYY):")
        if start:
            try:
                start = datetime.strptime(start, "%m/%d/%Y")
                end = simpledialog.askstring("Set Date Range", "Enter end date (MM/DD/YYYY):")
                if end:
                    end = datetime.strptime(end, "%m/%d/%Y")
                    if start > end:
                        raise ValueError("Start date must be earlier than end date")
                    delta = (end - start).days
                    random_days = random.randint(0, delta)
                    gen_date = end - timedelta(days=random_days)
                    self.text.insert("insert", gen_date.strftime("%B %d, %Y"))
                else:
                    messagebox.showerror("Error", "End date is required")
            except ValueError as e:
                messagebox.showerror("Error", str(e))

The first one is shorter, but it looks too good to be true. If they both have good error handling, then it should be the one that is shorter.
the first one is 932 characters, the second one is 632.
Character amount matters to me, because I want it to be as little characters as possible, taking up less storage, meaning a larger audience can reach it.

I have tried to remove the question from 1, because I know that 2 will definitely NOT work if just remove the question.
The error I get is:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\2558735\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 1948, in __call__
    return self.func(*args)
           ^^^^^^^^^^^^^^^^
  File "C:\Users\2558735\Downloads\CandelX\CandelX.py", line 203, in gen_date
    start_date = datetime.strptime(start_date, "%m/%d/%Y")
                                   ^^^^^^^^^^
UnboundLocalError: cannot access local variable 'start_date' where it is not associated with a value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant