Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am new to Python so be gentle.
I have been trying to write a program that counts or measures events in real time. At the moment I am using the sleep command but this pause event doesn't take into account the time the program takes to run. I have read up on the datetime module and can sort of see how this could be used, but I am a bit stuck in implementing this.
In short, I want a program that counts from 0 to 100 in real time seconds and milliseconds.
Your best bet (besides Googling for help before posting a question on SO) might be to do something like this:
Note the time when the program starts. start = datetime.datetime.now()
Do your calculations
sleep until 100 seconds after start. (start + datetime.timedelta(seconds=100))
Note that this won't be perfect, since there is a little overhead involved with the steps between accessing the "current time" and going to "sleep" (e.g. subtracting "current time" from "wake-up time"). However, if your sleep precision only needs to be in seconds, you should be okay.
Repeat as needed.
If, after trying it out, you need additional help with the actual implementation of these steps, feel free to come back and post another question on that topic.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 days ago.
Improve this question
Good morning all, I am struggling with TD ameritrade’s authentication process, specifically the 30 min life span of the bearer key.
I have written a Python script to run the authentication process (https://developer.tdameritrade.com/content/simple-auth-local-apps). It’s partially automated, except for the pause to deal with the two step verification (which I can’t work around).
The trouble I have is that if the bearer key needs to be updated every 30 minutes, and the process script isn’t fully automated, it defeats the purpose of having a bot that can run and execute trades.
I see references to a refresh key, but not sure how to implement.
so far I have tried forums, YouTube , and reaching out to others within my network.
Any help is appreciated
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
The community reviewed whether to reopen this question last month and left it closed:
Duplicate This question has been answered, is not unique, and doesn’t differentiate itself from another question.
Improve this question
I want to run a script, or at least the block of code in it every 12 hours. How bad/ how much resources will I be wasting by using:
while True:
My Code
time.sleep(43200)
Is there an easier and more efficient way to accomplish this?
I'd recommend using apscheduler if you need to run the code once in an hour (or more):
from apscheduler.schedulers.blocking import BlockingScheduler
def main():
Do something
scheduler = BlockingScheduler()
scheduler.add_job(main, "interval", hours=12)
scheduler.start()
apscheduler provides more controlled and consistent timing of when the operation will be executed. For example, if you want to run something every 12 hours but the processing takes 11 hours, then a sleep based approach would end up executing every 23 hours (11 hours running + 12 hours sleeping).
this timing is not accurate as it only count time when cpu is sheduled on this process
at least you can check target time is arrived every several second
and this is not a good solution as your process is less reliable than system cron. your process may hang due to unknown bugs and on system high cpu/mem utilization
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am new to learning python, I see it's a simple language but my teacher thinks it's a good thing to give me very advanced tasks to complete, I have it due tomorrow so if anyone could manage to code this in, I will be greatly appreciated. Here is what he wants:
Detect user input for the words: "add" and "sub" aka subtraction. if the wrong answer is given, it should re-ask the question.
If the user chooses either, it should generate two numbers from 1-9, number 1 >= number 2, and the user should be asked the answer.
If the answer is correct, it should move to the next round (our teacher said there must be 5 rounds, at each end of the round the code should ask the user to type "add" or "sub" again.
if the user gives the wrong answer, it should re-ask the question.
The code ends when all 5 rounds are done, or if the user types "end" at any time with a message saying "Goodbye".
Greatly appreciated.
It is generally uncalled for people to post their homework here and have it solved, as StackOverflow is meant as a place that every question can help out a lot of people interested in your matter.
While I will not do your homework, here's what you can do:
Use "while" statements for the input check and validation, which means it will keep asking if the input is different from the value you set as required for the loop to break.
Use "If" statements for general validation checks.
What is most important though is that you take the time and read/watch tutorials that can help you get a better understanding of the language, as Stackoverflow is a place for questions of a specific nature and not a homework-solving/debugging service. Hope I could be of help!
I personally recommend the following resources for learning Python:
https://www.mikedane.com/
https://www.w3schools.com/
https://www.sololearn.com/
Personal Playlist for learning Python
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have two Flask apps in OpenShift. Each one in a free small gear. Each one uses an own PostgreSQL cartridge. The code is quite simple: REST apps that do a query in the DB and return a JSON. The DB is almost empty and, in this test phase, they are accessed only by me and a response time recorder.
Most of the time, for both apps, the response time is ~300ms, what is great.
But sometimes the response takes much longer: 4s, 7s, 12s or even 15s. The same request taking 50X more time to return.
If these slow response requests were spaced 24h/48h I would say it's some kind of OpenShift idle policy. But they aren't. It's more like any request has a 5% chance of being a slow one, even if they are spaced only a few seconds. And if the requests are spaced a few minutes the "chance of being slow" raises for more than 50%.
Any ideas?
Thanks for the attention!
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to write equivalent Python code of following Objective-C code using PyObjC. Not sure how to do? Any help would be highly appreciated on ow to bridge Objective-C code into Python.
#import <IOKit/pwr_mgt/IOPMLib.h>
...
// kIOPMAssertionTypeNoDisplaySleep prevents display sleep,
// kIOPMAssertionTypeNoIdleSleep prevents idle sleep
//reasonForActivity is a descriptive string used by the system whenever it needs
// to tell the user why the system is not sleeping. For example,
// "Mail Compacting Mailboxes" would be a useful string.
// NOTE: IOPMAssertionCreateWithName limits the string to 128 characters.
CFStringRef* reasonForActivity= CFSTR("Describe Activity Type");
IOPMAssertionID assertionID;
IOReturn success = IOPMAssertionCreateWithName(kIOPMAssertionTypeNoDisplaySleep,
kIOPMAssertionLevelOn, reasonForActivity, &assertionID);
if (success == kIOReturnSuccess)
{
//Add the work you need to do without
// the system sleeping here.
success = IOPMAssertionRelease(assertionID);
//The system will be able to sleep again.
}
...
you need to generate a bridge file for the IOKit.framework first using the gen_bridge_metadata command.
you can hard code the contents of the file into a Python variable if you want.
then load the bridge into into PyObjC using objc.parseBridgeSupport()
objc.parseBridgeSupport(BRIDGE_FILE_STRING, globals(), objc.pathForFramework("/System/Library/Frameworks/IOKit.framework"))`
examples here and here
Here is an example that does almost exactly what you are asking.