Skip to main content
Chemistry LibreTexts

8.7: User Created Package

  • Page ID
    470273
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    This activity is correlated to Assignment 3.  In your class folder we want you to create a subfolder Sandbox and in it we want you to create a file called my_script.py and subfolder called My_toolbox. and 

    So the directory hierarchy looks like this, where the folders start with a captial letter and the files all end with .py

    • Sandbox
      • my_script.py
      • Tools
        • __init.py__
        • my_tool_box.py

    Python files

    my_script.py

    This file is in the Sandbox and located next to the Tools directory (folder).  It will call functions from a module within the Tools package.  We will use the follow code for this.

    #Script 1: my_script.py
    from Tools import my_toolbox as mtb
    num1=float(input("Enter first number: "))
    num2=float(input("Enter second number: "))
    value=mtb.addition(num1,num2)
    print(f"The sum of {num1} and {num2} is {value}.")
    print(f"the type for the variable now is {type(value)}.\n")
    
    now = mtb.current_date()
    print(f"The current date is {now}.")
    print(f"the type for the variable now is {type(now)}.")
    

     

    __init__.py

    Place this file in the Tools directory. The __init__.py (and those are two underscores) is used to mark a directory as a Python Package and when present in a folder, python treats the folder as a package, which means it can be imported into another program as a module.  Right now we are going to keep the __init__.py file empty, but for example, if the files in the package had dependencies on other packages, like the time module, they could be set up in the __init__.py  file

     

    my_toolbox.py

    Place this file in the tools directory.  It will serve as a module where you can create functions (tools) that can be called from other programs. 

    #Script 2: my_toolbox.py
    '''This package contains functions that can be called by other programs'''
    
    def addition(num1,num2):
        the_sum=num1+num2
        return(the_sum)
    
    def current_date():
        import datetime
        today=datetime.date.today()
        return(today)
    
    

    Note, there are two functions in the second script; addition and current_time. The addition has two arguments that must be submitted when the first script calls it (num1 and num2 are passed from script 1 to script 2 as arguments) and it returns one value, the sum.  The second function (current_time) has no parameters, and so you pass it nothing when you call it, but it returns the variable "today", which you assign to the variable "now" in the calling program

     

     

     


    8.7: User Created Package is shared under a not declared license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?