Skip to main content

Helpful functions for python.

Project description

Collection of useful python functions/features.

Prepared by: Daniel A Hagen
Build Status PEP8 Coverage Status

Installation

pip install danpy

Installation from GitHub

git clone https://github.com/danhagen/danpy.git && cd danpy
pip install -r requirements.txt
pip install .

Statusbar for Python for/while loops with danpy.sb

This helpful statusbar can be used with for/while loops to keep track of how much time has elapsed as well as how much time remains. Simply place inside the loop (after initializing the statusbar -- dsb -- with number_of_loops) and update with the current timestep (i). A title can be added to the statusbar to keep track of individual function/loops and it is recommended that any function that runs a loop uses arbitrary_function_name.__name__ to automatically assign an appropriate title. The starting_value can also be initialized or left at default (0).

Initialize statusbar before running a for/while loop.

from danpy.sb import *
from time import sleep

number_of_loops = 10
statusbar = dsb(number_of_loops,title='a Loop')
for i in range(number_of_loops):
  sleep(0.5)
  statusbar.update(i)

It is useful to either reset the statusbar instance. However, loops run in succession will automatically reset if the loops are of the same size.

from danpy.sb import *
from time import sleep

number_of_inside_loops = 10
number_of_outside_loops = 3
statusbar = dsb(number_of_inside_loops,title="Loop-D-Loops")
for j in range(number_of_outside_loops):
  for i in range(number_of_inside_loops):
    sleep(0.5)
    statusbar.update(i)

It is also possible to rename loops when they are run in succession by using the updating title.

from danpy.sb import *
from time import sleep

number_of_loops = 10
statusbar = dsb(number_of_loops,title="a Loop")
for i in range(number_of_loops):
  sleep(0.5)
  statusbar.update(i)
  
for i in range(number_of_loops):
  sleep(0.5)
  statusbar.update(i,title="a Different Loop")

However, these automatic reset features will only work if the length of each loop is the same and they have the same starting value. If you wish to run consecutive loops with different starting values or loop lengths, then you can reset the statusbar. It should be noted that the automatic reset, although convenient, will initialize the start_time after the first iteration of the loop. Therefore it is not the most accurate representation of runtime. We recommend a hard reset between trials or a redefinition of the statusbar before each loop.

Resetting Statusbar

A statusbar can be reset by calling the builtin function reset one of two way; reset() will return a statusbar with the previously used starting_value, number_of_loops, and title, or reset(**kwargs) can manually reset any of those values (while the others are kept as previously define).

from danpy.sb import *
from time import sleep

number_of_loops = 10
statusbar = dsb(number_of_loops,title='One Loop')
for i in range(number_of_loops):
  sleep(0.5)
  statusbar.update(i)

a_different_number_of_loops = 20
a_different_starting_value = 1
statusbar.reset(
  number_of_loops=a_different_number_of_loops,
  starting_value=a_different_starting_value,
  title="Another Loop")
for i in range(1,a_different_number_of_loops+1):
  sleep(0.5)
  statusbar.update(i)

Using while Loops

If using a while loop, the statusbar will still update, but depending on the nature of the code in the loop, the extrapolation to determine time remaining may be off.

from danpy.sb import *
from time import sleep

count = 0
number_of_loops = 10
statusbar = dsb(number_of_loops,title="a while Loop")
while count<number_of_loops:
  sleep(0.5)
  statusbar.update(count)
  count+=1

Only compatible with while loops that utilize a count metric where the loop continues while count<number_of_loops. The "<" ensures that the statusbar terminates at 100%. If you use "<=" then the input to the statusbar will be statusbar.update(i,number_of_loops+1,**kwargs).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

danpy-0.1.6.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

danpy-0.1.6-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file danpy-0.1.6.tar.gz.

File metadata

  • Download URL: danpy-0.1.6.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for danpy-0.1.6.tar.gz
Algorithm Hash digest
SHA256 41bfac02ce9b4bc09cc8eb9e382447af85be4e4ff5997a0775b266db00601800
MD5 3e06ea0e8aed08739502425c6da138a1
BLAKE2b-256 eee0bb20c03ff03ec0b8e97891a6e9cc72093b5bbba337b35a57a18a88d136a2

See more details on using hashes here.

File details

Details for the file danpy-0.1.6-py3-none-any.whl.

File metadata

File hashes

Hashes for danpy-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 3956af3c9faf70f255e7305a3ed79ab54b4b6658f6ecc012b9dad47762a0945a
MD5 3d9bcef83107d6cd98622ce9b7d441bb
BLAKE2b-256 c55f6047fd60b24f0ec3985d72ad3d73c2ccbbfc88781ad1b8261cab1f3e11b0

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page