Welcome to once_only’s documentation!
once_only
Run a python script or function only once in a given time frame.
If, for example, you have a script or service which might be called frequently, but you want to report errors only once daily to not annoy people too much, once_only is the library for you.
Quickstart
Suppose you want your script to complain, but not more than once a day.
once_only.daily
is the tool you need:
import once_only
@once_only.daily
def complain():
print("This is not right!")
Now you can run complain
as often as you want, from the same python
script or from others, it will only be run at
most once a day, so that at least 24 hours are between two complaints.
If you want to complain more or less often, there are other variants:
object |
time delta |
---|---|
|
1 week |
|
1 day |
|
1 hour |
|
1 minute |
|
custom |
Advanced Usage
Instead of using a once_only.Once
object as a decorator, you can also access
it directly via the check_ready()
and
check_ready_trigger()
functions:
import once_only
import datetime
once_every_two_hours = once_only.Once(datetime.timedelta(hours=2))
if once_every_two_hours.check_ready():
print("More than two hours have passed since last run!")
if not_a_dry_run and once_every_two_hours.check_ready_trigger():
print("Triggering timer and running!")
Note that all instances of once_only.Once
with the same time delta share the
same timer, but those with different time deltas don’t share the timer.
So, if you have never run anything before, this:
import once_only
import datetime
@once_only.minutely
def run_minutely():
print("minutely")
@once_only.hourly
def run_hourly():
print("hourly")
@once_only.Once(datetime.timedelta(minutes=60))
def run_every_60_minutes():
print("60 minutes")
run_minutely()
run_hourly()
run_every_60_minutes()
will print “minutely” and “hourly”, but not “60 minutes” because
60 minutes is the same as one hour, so the 60 minutes timer will
already be triggered by run_hourly
and run_every_60_minutes
will not be
run.
Further documentation can be found at: https://once_only.readthedocs.io.
License
Copyright 2022, Potsdam-Institut für Klimafolgenforschung e.V.
Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Installation
Stable release
To install once_only, run this command in your terminal:
$ pip install once_only
This is the preferred method to install once_only, as it will always install the most recent stable release.
If you don’t have pip installed, this Python installation guide can guide you through the process.
From sources
The sources for once_only can be downloaded from the Github repo.
You can either clone the public repository:
$ git clone git://github.com/mikapfl/once_only
Or download the tarball:
$ curl -OJL https://github.com/mikapfl/once_only/tarball/master
Once you have a copy of the source, you can install it with:
$ python setup.py install
API
Module contents
Run a python script or function only once in a given time frame.
- class once_only.Once(timedelta: timedelta, file: Optional[Path] = None)[source]
Bases:
object
Class for time keeping, enabling you to run functions only once in a given time span.
Contributing
Contributions are welcome, and they are greatly appreciated! Every little bit helps, and credit will always be given.
You can contribute in many ways:
Types of Contributions
Report Bugs
Report bugs at https://github.com/mikapfl/once_only/issues.
If you are reporting a bug, please include:
Your operating system name and version.
Any details about your local setup that might be helpful in troubleshooting.
Detailed steps to reproduce the bug.
Fix Bugs
Look through the GitHub issues for bugs. Anything tagged with “bug” and “help wanted” is open to whoever wants to implement it.
Write Documentation
once_only could always use more and better documentation!
Submit Feedback
The best way to send feedback is to file an issue at https://github.com/pflueger/once_only/issues.
If you are proposing a feature:
Explain in detail how it would work.
Keep the scope as narrow as possible, to make it easier to implement.
Remember that contributions are welcome :)
Get Started!
Ready to contribute? Here’s how to set up once_only for local development.
Fork the once_only repo on GitHub.
Clone your fork locally:
$ git clone git@github.com:your_name_here/once_only.git
Install your local copy into a virtualenv. Assuming you have virtualenvwrapper installed, this is how you set up your fork for local development:
$ cd once_only/ $ make virtual-environment $ make install-pre-commit
Create a branch for local development:
$ git checkout -b name-of-your-bugfix-or-feature
Now you can make your changes locally.
When you’re done making changes, check that your changes pass our tests and automatically format everything according to our rules:
$ make lint
Often, the linters can fix errors themselves, so if you get failures, run
make lint
again to see if any errors need human intervention.Commit your changes and push your branch to GitHub:
$ git add . $ git commit -m "Your detailed description of your changes." $ git push origin name-of-your-bugfix-or-feature
Submit a pull request through the GitHub website.
Pull Request Guidelines
Before you submit a pull request, check that it meets these guidelines:
The pull request should include tests.
If the pull request adds functionality, the docs should be updated. Put your new functionality into a function with a docstring and check the generated API documentation.
The pull request will be tested on python 3.7, 3.8, 3.9, and 3.10.
Deploying
A reminder for the maintainers on how to deploy.
Commit all your changes.
Run
tbump X.Y.Z
.Wait a bit that the release on github is created.
Upload the release to pyPI:
make release
Credits
Developers
Mika Pflüger <mika.pflueger@pik-potsdam.de>
Libraries
This package was originally created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.
Changelog
1.0.0 (2022-09-16)
Initial commit.
once_only.Once class implemented, with direct access API and decorator functionalities.
Add Quickstart documentation.