Package: oven
OvenAn util package that includes algorithms, functions and IO similar to that of other languages' standard library.InstallationInstallation with pip is recommended.pipinstallovenTable of ContentsStandard I/OCreating SessionCalling cin/coutPointerInstantiating PointersCalling PointersStructsStackQueueTreeBinary TreeStandard I/OThestdiopackage includes convient functions to redefine basic I/O.fromoven.stdioimport*Creating SessionThestdiosession is required in order to call any I/O functions. A session is created as such:set_io(IO_STREAM,'path/to/file',METHOD)TheIO_STREAMcan be any of:stdio.FILE: Redirect the IO stream to a filestdio.CONSOLE: Redirect the IO stream to the consoleTheMETHODcan be any of:r: Read from a filew: Write to a file, create/override if nessesarya: Append to a file, create if nessesaryMethodwoverrides the file and dump all output from the session, whileaappend all output from the session to the end of the file.If theIO_STREAMis set toCONSOLE, then thepathargument will be ignored.Note that thestdiosession is created on the global scope, therefore one session will always suffice for one method of IO action.An example is given below. This code reads a line of input from a file and print it to the screen.fromoven.stdioimport*set_io(FILE,'text.in','r')set_io(CONSOLE,None,'w')text=cin()cout(text)Calling cin/coutDespite the name, the cin/cout also applies to file IO.cin()# Read one line.cout('Hello World')# Write. Note that \n is not added at the end.PointerThePointerpackage includes a pointer object.fromoven.pointerimportPointerInstantiating PointersAPointeris created as such:ptr_1=Pointer()ptr_2=Pointer(INIT_VALUE)Calling PointersGetting value from pointer:value=ptr()Assigning value to pointer:ptr(value)StructsThestructspackage features some data structures.fromoven.structsimport*Stackfromoven.structsimportStackTheStackclass is a subclass of python's list, its methods are:push: Push a value to the stackpop: Remove and return the ending element of the stackQueuefromoven.structsimportQueueTheQueueclass is a subclass of python's list, its methods are:push: Push a value to the queuepop: Remove and return the starting element of the queueTreefromoven.structsimportTreeTheTreeclass is a recursive node of a tree. The value of the node can be assigned either in the constructor or by calling thevalueattribute (variable).# Both nodes are tree nodes with the value 42node_1=Tree(42)node_2=Tree()node_2.value=42A child can be added to a node by calling theaddChildmethod and passing the child as the parameter. The children of a node can be accessed by calling thechildrenattribute (variable).Other methods include:dfs: Returns a list of the tree's nodes indepth first searchorder.bfs: Returns a list of the tree's nodes inbreadth first searchorder.BinaryTreefromoven.structsimportBinaryTreeTheBinaryTreeclass is a subclass of theTreeclass. Theleftandrightchildren can be accessed by:node.left()# Returns the left nodenode.right()# Returns the right nodenode.left(newNode)# Sets the left nodenode.right(newNode)# Sets the right nodeDue to this, theaddChildmethod is removed.All other methods are identical to that of theTreeclass except thedfsmethod, which is tweaked so that the calling of it becomes:node.dfs(MODE)TheMODEcan be any of:PREORDERINORDERPOSTORDERAll of the above modes are imported as such:fromoven.structsimportPREORDER,INORDER,POSTORDER