Skip to main content

Get Variable History of specific variables

Project description

Varhist Python Package Desicription

Overview

varhist is a python package that allows the user to easily view a variable's history through their code. This makes debugging easier. Standart debuggers do not have this capability. Once initialized, varhist will quickly and quietly track a variable's historic values and where/when the values were changed.

Installation


Type pip install varhist in your command line or terminal


IMPORTANT

For the varhist library to work, you have to type this at the start of your python code.

import sys, varhist
sys.settrace(varhist.trace)

This is equivalent to initializing the module. Without this, the module will not function. You also must put your code in a main function. This is easily done by highlighting all of your code (potentially omitting imports and functions/classes), pressing tab, and then adding a line saying

def main():

to the top (the function name is arbritary). Then, call the function at the end of the code. Simple!

E.g.

import sys, varhist
sys.settrace(varhist.trace)
print("Hello World")

to


import sys, varhist
sys.settrace(varhist.trace.)
def main():
    print("Hello World") # Inside a function now!

main() # Call the function!

Examples

Basic Usage


.track()

To start tracking a variable, use the .track method. This takes as many arguments as you want and will start tracking all of those variables. Note: If the variable is created later on in the code (after calling this function), the package will start tracking the variable when it is created. If the variable was created before this was called, only the changes after this call will be tracked.

.history()

When you want to access the history, you can use the varhist.history() function. This neatly prints out the history of all the variable put in as arguments.

.HIST

To directly see the history to do something with it, you can use the varhist.HIST variable. This is a dictionary, so to access the history of a variable such as c, use varhist.HIST['c']. This will be a list of lists. Every sub-list is a line of history. The first value in the sub-list is the new value of the variable. The second is the line number and the third is the function in which the variable was changed.

NOTE: If you are tracking a variable that has not been created yet, accessing that variables history through .HIST will result in an error as that variable has not been created yet.

Example
import sys
from varhist import varhist

sys.settrace(varhist.trace)

a = 4 # This is not included in the history of a as we start tracking it later

varhist.track('a', 'b', 'c') # Start tracking a, b, c

def main():
	a = 3
	b = a + 2 # 5
	for i in range(2):
		c += 1

main()

varhist.history('a', 'b', 'c')
print(varhist.HIST['c']) # Print history for c. You can also do this for the other variables that are being tracked.
Output
----------Start of History for 'a'----------
Variable 'a' changed to '3' on line '73' in function 'main'
----------End of History for 'a'----------


----------Start of History for 'b'----------
Variable 'b' changed to '5' on line '74' in function 'main'
----------End of History for 'b'----------


----------Start of History for 'c'----------
Variable 'c' changed to '0' on line '75' in function 'main'
Variable 'c' changed to '1' on line '77' in function 'main'
Variable 'c' changed to '2' on line '77' in function 'main'
----------End of History for 'c'----------
[[0, 75, 'main'], [1, 77, 'main'], [2, 77, 'main']] <--- History for 'c'. The first sub-list
says the variable c changed to 0 on line 75 in function main.

Advance Usage

Tracking Attributes of Objects

This module can also track Attributes of Objects. For example, if you want to track the attribute name of a object of type Person (this is arbritary) named Me. You would seperate the attribute from the object with a .. Use varhist.track('Me.name'). You can also extend infinitely. You could, for example, track varhist.track('Me.name.spelling') if the name was an object with a spelling attribute, etc.

Example
import sys
from varhist import varhist

sys.settrace(varhist.trace)
varhist.track('somebody.name')

class Person:
	def __init__(self, name):
		self.name = name

def main():
    varhist.track('x', 'y')
    somebody = Person("Rithwik")
    # Modify the name
    somebody.name = "John"
    somebody.name += ' Green'

main()
varhist.history('somebody.name')
Output
----------Start of History for 'somebody.name'----------
Variable 'somebody.name' changed to 'Rithwik' on line '71' in function 'main' # Changed in the __init__ function of the Person class. But, the function is still 'main' as the variable's scope is the main function.
Variable 'somebody.name' changed to 'John' on line '77' in function 'main'
Variable 'somebody.name' changed to 'John Green' on line '78' in function 'main'
----------End of History for 'somebody.name'----------

I hope that this package comes in handy!

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

varhist-0.0.4.tar.gz (18.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

varhist-0.0.4-py2.py3-none-any.whl (16.2 kB view details)

Uploaded Python 2Python 3

File details

Details for the file varhist-0.0.4.tar.gz.

File metadata

  • Download URL: varhist-0.0.4.tar.gz
  • Upload date:
  • Size: 18.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.1

File hashes

Hashes for varhist-0.0.4.tar.gz
Algorithm Hash digest
SHA256 824c837b54743074970ac6346b08c18250fba71a1c57e2d9afc84ab89b120fd6
MD5 0dcc3fc246c1e353cd3af2e1ff36c9e8
BLAKE2b-256 ccd197111b42bf002398718826766681cc725199adf7ccc5f873cdbd5880c9ff

See more details on using hashes here.

File details

Details for the file varhist-0.0.4-py2.py3-none-any.whl.

File metadata

  • Download URL: varhist-0.0.4-py2.py3-none-any.whl
  • Upload date:
  • Size: 16.2 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.1

File hashes

Hashes for varhist-0.0.4-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 a241acd6d6e3e9c9c6c18dd745201e9069e9e7ee92ee2421bea6938a4c5a966d
MD5 5826c3e0a1d81bf134a483db56d5343d
BLAKE2b-256 932c9371c89daaa7b2ae60b02cf7f8cc8f410d60abf0799c69304a933d2a615e

See more details on using hashes here.

Supported by

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