#๐Ÿ”’ Python datetime problem

77 messages ยท Page 1 of 1 (latest)

vague flumeBOT
#

@round wasp

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

round wasp
#

I want to add microsecond timings with the datetime module to the senaryoXX sections of the code I wrote, but no matter what I did, I couldn't succeed. Can you help me? Also, if there are any obvious errors in the code, I would be glad if you could help me, thank you.

uncut void
#

!e

from datetime import datetime
ct = datetime.now()
print([ct.replace(minute=m, second=s) for m, s in [(5, 12), (5, 54)]]
)
vague flumeBOT
uncut void
#

wheres the error this works as intended doesnt it?

#

also as there is a pattern in ur times u should think to reduce that hardcode part

round wasp
#

The code works without any problems.

uncut void
#

so in which line u try to add microseconds?

round wasp
#

I just want to add milliseconds or microseconds next to the minutes and seconds in the section you showed, but I couldn't do it.

#

for example

#

from datetime import datetime
ct = datetime.now()
print([ct.replace(minute=m, second=s, milisecond=ms) for m, s, ms in [(5, 12, 500000), (5, 543, 355555)]]
)

uncut void
#

!e

from datetime import datetime
ct = datetime.now()
print(ct.replace(microsecond=1))
vague flumeBOT
uncut void
#

?

round wasp
#

this works like this but when i write in this part the code isn't working

uncut void
#

also consider to combine/generalize ur senaryoXX functions and the list of tuple timings

round wasp
#

i changed part of my code like this but it isn't working

uncut void
#
            if (current_time.hour == target_time.hour and
                    current_time.minute == target_time.minute and
                    current_time.second == target_time.second and
                    current_time.microsecond // 1000 == target_time.microsecond // 1000 and

why u dont compare dt.obj with dt.obj?

round wasp
#

For example, the code snippet required for hour (5, 12, 500) is not executed.

#

what do you want to say

#

i dont understand

#

can you explain more explanatory

uncut void
#

u comapre all values expect for date (hour, min, sec, msec) why not compare the dt obj themself?

short ocean
#

the current time might not be exact, as it might be some microseconds off from the target time, I guess.

uncut void
#

mh i see ok

short ocean
#

though you could probably just round and replace it as needed, and then just compare them

#

the date should be the same anyway

uncut void
#

my thoughts as it looks ugly and unnecessarily complicated

uncut void
#

!e

from datetime import datetime
ct = datetime.now()
new_time_tuple = (0, 1, 500*1000)
print(ct.replace(minute=new_time_tuple[0], second=new_time_tuple[1], microsecond=new_time_tuple[-1]))
vague flumeBOT
round wasp
#

I'm making a mistake in some part but I don't know where.

short ocean
#

what exactly is that code even supposed to do?

round wasp
#

makes the necessary clicks and keystrokes at the specified local times

#

it works local minutes and seconds

uncut void
#

for what purpose?

round wasp
#

to automate a task that requires manual effort in a game

uncut void
#

so i smell ToS

round wasp
#

the problem is not this

uncut void
#

!rule 5

#

!rule 5

vague flumeBOT
#

5. Do not provide or request help on projects that may violate terms of service, or that may be deemed inappropriate, malicious, or illegal.

round wasp
#

this is just a game

short ocean
#

I'd assume your time.sleep can cause isses

#

you jump 500 ms with that, so probably miss a lot of exact ms matches

uncut void
#

@short ocean so its cool to help out even tho might violate ToS?

round wasp
#

iI tried removing the timesleeps at the end of the senaryo functions but it didn't work

short ocean
uncut void
#

okay

short ocean
#

(except of course that nearly all your values stop at around 24 minute, and it's 20:36 for me, so I needed to add some test values)

uncut void
#

in terms of performance bottlenecks i would suggest to try numpy instead of datetime for comparisons

round wasp
#

thank you for efforts by the way i am grateful

uncut void
#

so ur whileloop will be x4 times faster, even increasing with size (exponential scaling)

round wasp
short ocean
#

I'd at least try to add a print into your fi bodies to see if any are true.

round wasp
#

It is very interesting that it works for minutes and seconds but does not work when microseconds are involved.

uncut void
#

create a elif which excludes the ms match requiered and check the returns

uncut void
#

to not let this die as im kinda curious now i think python isnt the best lang to write such code maybe C would be more appropriate

#

!e

from datetime import datetime
import numpy as np

executed_times = {}
ct = datetime.now()
tt = [ct.replace(minute=m, second=s, microsecond=ms) for m, s, ms in [
        (5, 12, 500), (5, 54, 200), (6, 0, 0), (6, 12, 300), (6, 54, 600), (7, 0, 100),
        (7, 12, 800), (7, 24, 400), (8, 24, 200), (8, 30, 500), (9, 0, 0), (9, 12, 700),
        (9, 42, 500), (9, 54, 300), (10, 0, 0), (10, 42, 800), (10, 54, 100), (11, 30, 600),
        (11, 54, 200), (12, 24, 500), (12, 30, 300), (13, 0, 0), (13, 12, 600), (13, 24, 700),
        (13, 30, 100), (13, 42, 400), (14, 24, 300), (14, 30, 600), (14, 42, 100),
        (15, 54, 500), (16, 0, 0), (16, 12, 200), (17, 0, 700), (17, 24, 500),
        (18, 42, 300), (18, 54, 400), (19, 0, 0), (19, 12, 600), (19, 54, 700),
        (20, 0, 100), (20, 12, 800), (21, 12, 200), (21, 24, 400), (21, 42, 600),
        (22, 24, 300), (22, 30, 500), (22, 42, 700), (23, 42, 800), (23, 54, 200),
        (27, 24, 100), (27, 30, 300), (27, 42, 600)]]
np_tt = np.array(tt).astype(np.datetime64)

while len(executed_times.keys()) <= 0:
    ct = datetime.now()
    ct = ct.replace(microsecond=ct.microsecond // 1000)
    # if ct.microsecond % 100 == 0: # maybe check if u can pre-sample to reduce resources
    np_ct = np.array(ct).astype(np.datetime64)
    if np.array(np_ct).astype(np.datetime64) in np_tt:
        executed_times[int(np.where([np_ct == np_tt])[-1][0])] = True
#

@round wasp think this improves the needed resources let me know how much RAM u used before and now ๐Ÿ˜‰

round wasp
#

i'll change my code like this but I still can't figure out the millisecond problem.

uncut void
#

the problem was that ur convertion was faulty

#

atleast i think that was the problem

#

also see what i marked out could help to pre-sample

#

note these now arent 1/10 sec they are indeed microsecs, so this runs super fast and efficient while finding matches

round wasp
#

iI understand you now, I'll try this in a moment.

uncut void
#

from datetime import datetime
import numpy as np

def generate_times(current_time, second_increments, microsecond_increments):
    times = []
    
    for second_increment in second_increments:
        for microsecond_increment in microsecond_increments:
            new_time = current_time.replace(second=second_increment % 60, microsecond=microsecond_increment)
            times.append(new_time)
    
    return times

executed_times = {}
ct = datetime.now()
tt = generate_times(ct, [0, 10, 20, 30, 40, 50], [0, 500])
np_tt = np.array(tt).astype(np.datetime64)
print(len(np_tt))
mask = np_tt > np.datetime64(ct)
np_tt = np_tt[mask]
print(len(np_tt))

while len(executed_times.keys()) != len(np_tt):
    ct = datetime.now()
    ct = ct.replace(microsecond=ct.microsecond // 1000)
    # if ct.microsecond % 100 == 0: # maybe check if u can pre-sample to reduce resources
    np_ct = np.array(ct).astype(np.datetime64)
    if np.array(np_ct).astype(np.datetime64) in np_tt:
        idx = int(np.where([np_ct == np_tt])[-1][0])
        if idx in executed_times.keys():
            continue
        else:
            executed_times[idx] = True


ontop of checking if matches are even possible u should adjust so the code resets after each minute or so, if the script misses one value ull run eternally

#

@round wasp still waiting on ur resource reply ๐Ÿ˜„

round wasp
#

im not home now im so sorry about this

#

i'll check fast

vague flumeBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.