Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
A timer context manager measuring the clock wall time of the code block it contains.
contexttimer
provides you with a couple of utilities to quickly measure the execution time of a code block or a function.
contexttimer.Timer
is a context manager measuring the execution time of the code block it contains.
The elapsed time is accessible through the elapsed
property.
with Timer() as t: ... # some code here print t.elapsed
contexttimer.Timer
context managercontexttimer.Timer
is a context manager <http://docs.python.org/reference/datamodel.html#context-managers>
_ with 2 parameters and a public property:
default_timer
: a platform specific timer function (time.time
for Unix platforms and time.clock
for Windows platforms). You can instanciate a Timer
object with your own timer, by passing it to the constructor.factor
: a multiplying factor applied to the elapsed
property. For example, a factor or 1000 will lead to elapsed
being expressed in milliseconds instead of seconds. Default value of 1.elapsed
: (read only property) the wall clock timing of the execution of the code block, in seconds. By default, expressed in seconds.Example """""""
from contexttimer import Timer with Timer(factor=1000) as t: ... for i in xrange(10000000): ... pass ... print(t.elapsed) 73.6618041992 # in miliseconds
Note that elapsed
is calculated on demand, so it is possible to time sub-parts of your code block:
with Timer(factor=1000) as t: ... # do some things ... print t.elapsed ... # do other tings ... print t.elapsed ... 10.122 # in ms 20.567
contexttimer.timer
function decoratorYou can use the @timer
function decorator to measure the time execution of an entire function.
When the function returns its value, its execution time will be printed to the stdout (default), or to the argument logger.
Examples """"""""
@timer ... def sleep_for_2s(): ... time.sleep(2)
sleep_for_2s() function sleep_for_2s execution time: 2.002
logging.basicConfig() @timer(logger=logging.getLogger()) ... def sleep_for_2s(): ... time.sleep(2)
sleep_for_2s() DEBUG:root:function blah execution time: 2.002
As it makes use of the Timer
context manager inside, all arguments passed to the @timer
decorator will be used a Timer
init arguments.
Example: """"""""
@timer(factor=1000) ... def sleepawhile(n): ... time.sleep(n) ... sleepawhile(2) function sleepawhile execution time: 2000.089
contexttimer.timeout.timeout
decoratorYou can use the @timeout
function decorator to stop a function/method call and call a handler if the call exceeds a fixed amount of time.
Example: """"""""
def timeout_handler(limit, f, *args, **kwargs): ... print "{func} call timed out after {lim}s.".format( ... func=f.name, lim=limit) ... @timeout(limit=5, handler=timeout_handler) ... def work(foo, bar, baz="spam") ... time.sleep(10) work("foo", "bar", "baz")
work call timed out after 5s.
Thanks to halloi, wolanko and Jon Blackburn for their helpful insights and contributions.
contexttimer
is released under the GPLv3 license.
FAQs
A timer context manager measuring the clock wall time of the code block it contains.
We found that contexttimer demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.