mdpython


Namemdpython JSON
Version 0.0.23 PyPI version JSON
download
home_pageNone
SummaryMD Python Library - High level library for common use cases
upload_time2025-02-16 10:37:29
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseNone
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # MD Python Library

This package contains high level functions for common programming use cases.

## What is it?

mdpython is a Python package that makes it easy to work with common programming use cases.
It aims to provide ready-to-use functions for solving practical, real world problems in Python.

## Table of Contents

1. File Utilities
    1. [Compare Directories](#compare-directories)
    2. [Compare Files](#compare-files)
    3. [Disk cleanup](#disk-cleanup)
    4. [Extract URLs from excel file](#extract-urls-from-excel-file)
2. UI Utilities
    1. [Menu based app](#menu-based-app)
3. Debug Utilities
    1. [Getting execution duration](#getting-execution-duration)
    2. [Print current position](#print-current-position)
    3. [Track package changes](#track-package-changes)
    4. [Search for functions](#search-for-functions)
4. Data Utilities
    1. [Flatten JSON](#flatten-json)
    2. [List of JSON object to CSV file](#list-of-json-object-to-csv-file)
    3. [Search for text inside JSON data](#search-for-text-inside-json-data)
    4. [Get all objects for a particular key in JSON](#get-all-objects-for-a-particular-key-in-json)
5. Account Utilities
    1. [Reconcile bills and payments](#reconcile-bills-and-payments)

## Compare Directories

This function will compare two directories. The comparison results can be dumped out as a html report.
The gen_html_report takes an additional list of extensions. If file names match this extension, then
detailed difference for these files will be included in report.
The returned cmp object can also be used to access file differences as list. 4 lists are provided -

1. files_only_in_left
2. dirs_only_in_left
3. files_only_in_right
4. dirs_only_in_right

Directories will be recursively scanned to report the differences.

    from mdpython.fileutils import compare  
      
    cmp = compare.compare_dirs(r"C:\Users\Dell\OneDrive\Desktop\result_9th",  
                               r"C:\Users\Dell\OneDrive\Desktop\result_9th_v2")  
      
    cmp.gen_html_report(r"C:\Users\Dell\OneDrive\Desktop\out.html", ["py", "txt",  
                                                                     "json"])  
      
    for fl in cmp.files_only_in_right:  
        if fl.name.endswith("py"):  
            print(fl.absolute())

[Back to contents](#table-of-contents)

## Compare Files

This function is used to compare files. it is a convenient wrapper around difflib library. It takes
as input file1, file2 and the output file where html report will be saved.

    from mdpython.fileutils import compare
    
    compare.compare_files(r"C:\Users\Dell\OneDrive\Desktop\spark\b.py",
                          r"C:\Users\Dell\OneDrive\Desktop\spark\c.py",
                          r"C:\Users\Dell\OneDrive\Desktop\spark\out.html")

[Back to contents](#table-of-contents)

## Disk cleanup

This function is a helper to assist in clearing up space on disk. The retrieve_info is to be called
on directory that needs to be cleaned. This will scan all files recursively. Post this, the object returned
by retrieve_info can be used to perform additional operations.

1. sort_by_time - Gets the files sorted by modified time ascending
2. sort_by_size - Gets the files sorted by filesize in descending order
3. modified_within - Gets the files modified within provided minutes
4. modified_before - Gets the files modified before provided minutes
5. sort_by_file_count - Gets directories sorted by number of files within the directory in descending order

All the files are returned as Path objects

    from mdpython.fileutils import cleanup
    
    dr = r"C:\Users\Dell\OneDrive\Desktop\result_9th"
    
    info = cleanup.retrieve_info(dr)
    
    print("sorted by time")
    for dtl in info.sort_by_time()[:5]:
        print(dtl)
    
    print("\nsorted by size")
    for dtl in info.sort_by_size()[:5]:
        print(dtl)
    
    print("\nmodified in last 30 mins")
    for dtl in info.modified_within(mins=30)[:5]:
        print(dtl)
    
    print("\nmodified more than 1 day ago")
    for dtl in info.modified_before(mins=24 * 60)[:5]:
        print(dtl)
    
    print("\nsorted by number of files in directory")
    for dtl in info.sort_by_file_count()[:5]:
        print(dtl)

[Back to contents](#table-of-contents)

## Extract URLs from excel file

This function generates an extract of all hyperlinks present in an excel file. It extracts
both explicit ( where hyperlink is attached to cell ) and implicit ( where text content is
a http or https link ). Report contains file name, sheet name, row number, column name, type ( explicit
or implicit ), hyperlink text and hyperlink URL.

    import mdpython.fileutils.excel as mdexcel
    import json
    from mdpython.datautils import jsonutil
    
    # Get URLs
    xl = mdexcel.Excel(r"c:\users\dell\onedrive\desktop\dummy_data.xlsx")
    urls = xl.extract_urls(["A", "B"])
    print(json.dumps(urls['data'], indent=2))
    
    # Save as CSV
    jsonutil.list_to_csv(urls['data'], r"c:\users\dell\onedrive\desktop\out.csv",
                         colkey=urls['keys'])

[Back to contents](#table-of-contents)

## Menu based app

Once you have implemented different functions for an application, you can use this function
as a quick and easy wrapper to bind all functions into a menu based application. It uses tkinter
menu. It takes as input a list of 3 value tuples - menu name, sub menu name, function to be called
when user clicks on the sub menu item.

    from datetime import datetime
    from random import randint, choice
    from mdpython.uiutils import menu_based_app
    
    def show_date():
        print(datetime.now().strftime("%Y-%m-%d"))
    
    
    def show_time():
        print(datetime.now().strftime("%H:%M:%S"))
    
    
    def show_date_and_time():
        print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
    
    
    def show_random_number():
        print(randint(1, 100))
    
    
    def show_random_color():
        print(choice(['red', 'blue', 'green']))
    
    
    ar = [
        ["Random", "Random Integer", show_random_number],
        ["Random", "Random Color", show_random_color],
        ["Date", "Show Date", show_date],
        ["Date", "Show Time", show_time],
        ["Date", "Show Date and Time", show_date_and_time]
    ]
    
    menu_based_app.start(ar)

[Back to contents](#table-of-contents)

## Getting execution duration

A 'timer' object is provided to set it on or off for different user defined names. This can be used to
get a report of execution times of a function or block of statements. call timer.start('somename') when
you want to start timer and timer.stop('somename') when you want to stop timer. call timer.show() to get a
report of execution times. More than one timer can be set in the same program by passing different names.
If the timer.start is called within a loop body, then execution times will be reported as different iterations.

    from mdpython.debugutils import timer
    from random import randint
    from math import factorial
    
    timer.start("main")
    
    def count_elements_in_array():
        ar = list(range(randint(1000000,10000000)))
        print(len(ar))
    
    def get_factorial():
        for i in range(5):
            timer.start("looptest")
            num = randint(900,1000)
            print(num, factorial(num))
            timer.stop("looptest")
    
    timer.start("func1")
    count_elements_in_array()
    timer.stop("func1")
    
    get_factorial()
    
    timer.stop("main")
    
    timer.show()

    -- Output
    Name                           Duration             Start Time           End Time            
    ============================== ==================== ==================== ====================
    main                           0:00:00.046207       2024-04-08 21:24:59  2024-04-08 21:24:59 
    func1                          0:00:00.033129       2024-04-08 21:24:59  2024-04-08 21:24:59 
    looptest.4                     0:00:00.010020       2024-04-08 21:24:59  2024-04-08 21:24:59 
    looptest.2                     0:00:00.003058       2024-04-08 21:24:59  2024-04-08 21:24:59 
    looptest.1                     0:00:00              2024-04-08 21:24:59  2024-04-08 21:24:59 
    looptest.3                     0:00:00              2024-04-08 21:24:59  2024-04-08 21:24:59 
    looptest.5                     0:00:00              2024-04-08 21:24:59  2024-04-08 21:24:59 

[Back to contents](#table-of-contents)

## Print current position

This function comes in handy while debugging using print. Instead of manually writting print statements
and thinking what to write after print to pinpoint to the position, we can simply write curpos.show(). This
makes it easy to delete the lines later as print statements can be part of programs or debug. But curpos will
only be part of debug. To temporarily disable debug, write curpos.disable_show() at the beginning of program.
This will suppress all curpos.show() messages.

    from mdpython.debugutils import curpos
    from random import randint
    from math import factorial
    
    
    def count_elements_in_array():
        ar = list(range(randint(1000000, 10000000)))
        print(len(ar))
    
    
    def get_factorial():
        for i in range(5):
            num = randint(900, 1000)
            print(num, factorial(num))
            curpos.show()
    
    
    count_elements_in_array()
    curpos.show()
    
    get_factorial()

[Back to contents](#table-of-contents)

## Track package changes

This function is used to identify changes to installed python library versions over a period
of time. Periodically call version.save to save a dump of current version details for all
installed packages in a JSON file. Pass the directory where a time stamped JSON file will be created.
If any version issue occurs in future or if we want to check if there are any changes to installed
versions, call version.timeline to get timeline of what was changed and when. Call version.compare to see
what has changed after the latest snapshot and now.

    from mdpython.debugutils import version
    
    version.save("app_dev", r"D:\data\version")
    version.timeline("app_dev", r"D:\data\version")
    version.compare("app_dev", r"D:\data\version")

[Back to contents](#table-of-contents)

## Search for functions

This functions scans through docstrings to search for text within them. To get of elements, call
list_elements function and pass object, search string. To also see relevant documentation, pass
showdoc=True

    import pandas as pd
    from mdpython.debugutils import find
    
    a = [1,2]
    b = {"k": 1}
    s = pd.Series(range(10))
    
    find.search_function(pd, "")
    find.list_elements(s, "truncate", showdoc=True)

[Back to contents](#table-of-contents)

## Flatten JSON

This function is used to flatten a nested JSON into a single key value pair flat JSON. Nested keys are
flattened to . notation. So {"car":{"color":"red"}} will be flattened to {"car.color": "red"}. Nested
arrays are flattened to position. So {"car": [{"color": "red"}, {"color": "blue"}]} will be flattened to
{"car.0.color": "red", "car.1.color": "blue"}

    from mdpython.datautils import jsonutil
    import json
    
    json_data = {
        "name": "John",
        "age": 30,
        "car": {
            "make": "Toyota",
            "model": "Camry"
        },
        "colors": ["red", "blue", "green"],
        "nested_list": [
            [1, 2, 3],
            {"hello": "world"},
            [[7, 8], [9, 10]],
            [[[11, 12], [13, 14]], [[], [17, 18]]]
        ],
        "nested_dict": {
            "info1": {"key1": "value1"},
            "info2": {"key2": "value2"}
        },
        "list_of_dicts": [
            {"item1": "value1"},
            {"item2": "value2"}
        ]
    }
    
    flattened_data = jsonutil.flatten_json(json_data)
    print(json.dumps(flattened_data, indent=2))

[Back to contents](#table-of-contents)

## List of JSON object to CSV file

This functions flattens a nested JSON and dumps it into a csv file. Flattening happens in same fashion
as described in [Flatten JSON](#flatten-json). Each unique key forms a column in the CSV file.

    from mdpython.datautils import jsonutil
    
    out_fl = r"C:\Users\Dell\Onedrive\Desktop\out.csv"
    
    json_data = [
        {
            "name": "John",
            "age": 30,
            "car": {
                "make": "Toyota",
                "model": "Camry"
            },
            "colors": ["red", "blue", "green"]
        }, {
            "name": "Sheema",
            "age": 25,
            "car": {
                "make": "Audi",
                "model": "a4",
                "dimension": [5000, 1850, 1433]
            },
            "colors": ["blue", "yellow"]
        }, {
            "name": "Bruce",
            "car": {
                "make": "Ford"
            }
        }
    ]
    
    jsonutil.list_to_csv(json_data, out_fl)

[Back to contents](#table-of-contents)

## Search for text inside JSON data

This function searches for text within a nested JSON. Both keys and values are searched for the
provided text. The output is returned as a list of flattened JSON for matched values. flattening
rules are same as applied in [Flatten JSON](#flatten-json)

    from mdpython.datautils import jsonutil
    
    json_data = {
        "data": [
            {
                "name": "John",
                "age": 30,
                "car": {
                    "make": "Toyota",
                    "model": "Camry"
                },
                "colors": ["red", "blue", "green"]
            }, {
                "name": "Sheema",
                "age": 25,
                "car": {
                    "make": "Audi",
                    "model": "a4",
                    "dimension": [5000, 1850, 1433]
                },
                "colors": ["blue", "yellow"]
            }, {
                "name": "Bruce",
                "car": {
                    "make": "Ford"
                }
            }
        ]
    }
    
    print(jsonutil.search(json_data, "blue"))

    # Output
    [['data.0.colors.1', 'blue'], ['data.1.colors.0', 'blue']]

[Back to contents](#table-of-contents)

## Get all objects for a particular key in JSON

This function recursively searches through a nested JSON and returns a list of all values
corresponding to provided key.

    from mdpython.datautils import jsonutil

    json_data = {
        "data": [
            {
                "name": "John",
                "age": 30,
                "car": {
                    "make": "Toyota",
                    "model": "Camry"
                },
                "colors": ["red", "blue", "green"]
            }, {
                "name": "Sheema",
                "age": 25,
                "car": {
                    "make": "Audi",
                    "model": "a4",
                    "dimension": [5000, 1850, 1433]
                },
                "colors": ["blue", "yellow"]
            }, {
                "name": "Bruce",
                "car": {
                    "make": "Ford"
                }
            }
        ]
    }
    
    print(jsonutil.find_values_by_key(json_data, "colors"))

    # Output
    [['red', 'blue', 'green'], ['blue', 'yellow']]

[Back to contents](#table-of-contents)

## Reconcile bills and payments

This function reconciles payments against bills. It uses a 4 step approach to match bills and payments

1. First payments with exact amount with date greater than or equal to bill date are mapped
2. Discount percentages can be passed as a list to the function to provide discount on final bill amount
3. If addition of more than one payment with date same or after bill amount matches value, then it is mapped.
   Similarly if addition of more than one bill amount matches a payment amount on or after bill date, then it
   is mapped.
4. Remaining payments are distributed across bills on a first come first serve basis where payment date is
   greater than or equal to bill date

The function requires 2 dataframes, one for payment and one for bill. 4 columns are required - bill date, bill amount
, payment date and payment amount.

    import pandas as pd
    from mdpython.account import reconcile
    
    inp_fl = r"C:\Users\Dell\Onedrive\Desktop\input.xlsx"
    out_fl = r"C:\Users\Dell\Onedrive\Desktop\output.xlsx"
    
    disc_ar = [2, 2.5, 5, 10]
    
    bill_df = pd.read_excel(inp_fl, usecols="B:C").dropna()
    pymt_df = pd.read_excel(inp_fl, usecols="G:H").dropna()
    
    recon = reconcile.reconcile_payment(
        bill_df=bill_df
        , pymt_df=pymt_df
        , bill_dt_col="Bill Date"
        , bill_amt_col="Bill Amount"
        , pymt_dt_col="Payment Date"
        , pymt_amt_col="Payment Amount"
        , disc_ar=disc_ar
    )
    
    print(recon.bill_dtl_df)
    print(recon.pymt_dtl_df)
    
    recon.to_excel(out_fl)

[Back to contents](#table-of-contents)

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "mdpython",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": null,
    "author": null,
    "author_email": "Dhaval <seedhaval@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/fe/d4/7a45d43a1900c02c3062be17bee43e63019ce4eb2cbef7e632ffd97f5c9c/mdpython-0.0.23.tar.gz",
    "platform": null,
    "description": "# MD Python Library\n\nThis package contains high level functions for common programming use cases.\n\n## What is it?\n\nmdpython is a Python package that makes it easy to work with common programming use cases.\nIt aims to provide ready-to-use functions for solving practical, real world problems in Python.\n\n## Table of Contents\n\n1. File Utilities\n    1. [Compare Directories](#compare-directories)\n    2. [Compare Files](#compare-files)\n    3. [Disk cleanup](#disk-cleanup)\n    4. [Extract URLs from excel file](#extract-urls-from-excel-file)\n2. UI Utilities\n    1. [Menu based app](#menu-based-app)\n3. Debug Utilities\n    1. [Getting execution duration](#getting-execution-duration)\n    2. [Print current position](#print-current-position)\n    3. [Track package changes](#track-package-changes)\n    4. [Search for functions](#search-for-functions)\n4. Data Utilities\n    1. [Flatten JSON](#flatten-json)\n    2. [List of JSON object to CSV file](#list-of-json-object-to-csv-file)\n    3. [Search for text inside JSON data](#search-for-text-inside-json-data)\n    4. [Get all objects for a particular key in JSON](#get-all-objects-for-a-particular-key-in-json)\n5. Account Utilities\n    1. [Reconcile bills and payments](#reconcile-bills-and-payments)\n\n## Compare Directories\n\nThis function will compare two directories. The comparison results can be dumped out as a html report.\nThe gen_html_report takes an additional list of extensions. If file names match this extension, then\ndetailed difference for these files will be included in report.\nThe returned cmp object can also be used to access file differences as list. 4 lists are provided -\n\n1. files_only_in_left\n2. dirs_only_in_left\n3. files_only_in_right\n4. dirs_only_in_right\n\nDirectories will be recursively scanned to report the differences.\n\n    from mdpython.fileutils import compare  \n      \n    cmp = compare.compare_dirs(r\"C:\\Users\\Dell\\OneDrive\\Desktop\\result_9th\",  \n                               r\"C:\\Users\\Dell\\OneDrive\\Desktop\\result_9th_v2\")  \n      \n    cmp.gen_html_report(r\"C:\\Users\\Dell\\OneDrive\\Desktop\\out.html\", [\"py\", \"txt\",  \n                                                                     \"json\"])  \n      \n    for fl in cmp.files_only_in_right:  \n        if fl.name.endswith(\"py\"):  \n            print(fl.absolute())\n\n[Back to contents](#table-of-contents)\n\n## Compare Files\n\nThis function is used to compare files. it is a convenient wrapper around difflib library. It takes\nas input file1, file2 and the output file where html report will be saved.\n\n    from mdpython.fileutils import compare\n    \n    compare.compare_files(r\"C:\\Users\\Dell\\OneDrive\\Desktop\\spark\\b.py\",\n                          r\"C:\\Users\\Dell\\OneDrive\\Desktop\\spark\\c.py\",\n                          r\"C:\\Users\\Dell\\OneDrive\\Desktop\\spark\\out.html\")\n\n[Back to contents](#table-of-contents)\n\n## Disk cleanup\n\nThis function is a helper to assist in clearing up space on disk. The retrieve_info is to be called\non directory that needs to be cleaned. This will scan all files recursively. Post this, the object returned\nby retrieve_info can be used to perform additional operations.\n\n1. sort_by_time - Gets the files sorted by modified time ascending\n2. sort_by_size - Gets the files sorted by filesize in descending order\n3. modified_within - Gets the files modified within provided minutes\n4. modified_before - Gets the files modified before provided minutes\n5. sort_by_file_count - Gets directories sorted by number of files within the directory in descending order\n\nAll the files are returned as Path objects\n\n    from mdpython.fileutils import cleanup\n    \n    dr = r\"C:\\Users\\Dell\\OneDrive\\Desktop\\result_9th\"\n    \n    info = cleanup.retrieve_info(dr)\n    \n    print(\"sorted by time\")\n    for dtl in info.sort_by_time()[:5]:\n        print(dtl)\n    \n    print(\"\\nsorted by size\")\n    for dtl in info.sort_by_size()[:5]:\n        print(dtl)\n    \n    print(\"\\nmodified in last 30 mins\")\n    for dtl in info.modified_within(mins=30)[:5]:\n        print(dtl)\n    \n    print(\"\\nmodified more than 1 day ago\")\n    for dtl in info.modified_before(mins=24 * 60)[:5]:\n        print(dtl)\n    \n    print(\"\\nsorted by number of files in directory\")\n    for dtl in info.sort_by_file_count()[:5]:\n        print(dtl)\n\n[Back to contents](#table-of-contents)\n\n## Extract URLs from excel file\n\nThis function generates an extract of all hyperlinks present in an excel file. It extracts\nboth explicit ( where hyperlink is attached to cell ) and implicit ( where text content is\na http or https link ). Report contains file name, sheet name, row number, column name, type ( explicit\nor implicit ), hyperlink text and hyperlink URL.\n\n    import mdpython.fileutils.excel as mdexcel\n    import json\n    from mdpython.datautils import jsonutil\n    \n    # Get URLs\n    xl = mdexcel.Excel(r\"c:\\users\\dell\\onedrive\\desktop\\dummy_data.xlsx\")\n    urls = xl.extract_urls([\"A\", \"B\"])\n    print(json.dumps(urls['data'], indent=2))\n    \n    # Save as CSV\n    jsonutil.list_to_csv(urls['data'], r\"c:\\users\\dell\\onedrive\\desktop\\out.csv\",\n                         colkey=urls['keys'])\n\n[Back to contents](#table-of-contents)\n\n## Menu based app\n\nOnce you have implemented different functions for an application, you can use this function\nas a quick and easy wrapper to bind all functions into a menu based application. It uses tkinter\nmenu. It takes as input a list of 3 value tuples - menu name, sub menu name, function to be called\nwhen user clicks on the sub menu item.\n\n    from datetime import datetime\n    from random import randint, choice\n    from mdpython.uiutils import menu_based_app\n    \n    def show_date():\n        print(datetime.now().strftime(\"%Y-%m-%d\"))\n    \n    \n    def show_time():\n        print(datetime.now().strftime(\"%H:%M:%S\"))\n    \n    \n    def show_date_and_time():\n        print(datetime.now().strftime(\"%Y-%m-%d %H:%M:%S\"))\n    \n    \n    def show_random_number():\n        print(randint(1, 100))\n    \n    \n    def show_random_color():\n        print(choice(['red', 'blue', 'green']))\n    \n    \n    ar = [\n        [\"Random\", \"Random Integer\", show_random_number],\n        [\"Random\", \"Random Color\", show_random_color],\n        [\"Date\", \"Show Date\", show_date],\n        [\"Date\", \"Show Time\", show_time],\n        [\"Date\", \"Show Date and Time\", show_date_and_time]\n    ]\n    \n    menu_based_app.start(ar)\n\n[Back to contents](#table-of-contents)\n\n## Getting execution duration\n\nA 'timer' object is provided to set it on or off for different user defined names. This can be used to\nget a report of execution times of a function or block of statements. call timer.start('somename') when\nyou want to start timer and timer.stop('somename') when you want to stop timer. call timer.show() to get a\nreport of execution times. More than one timer can be set in the same program by passing different names.\nIf the timer.start is called within a loop body, then execution times will be reported as different iterations.\n\n    from mdpython.debugutils import timer\n    from random import randint\n    from math import factorial\n    \n    timer.start(\"main\")\n    \n    def count_elements_in_array():\n        ar = list(range(randint(1000000,10000000)))\n        print(len(ar))\n    \n    def get_factorial():\n        for i in range(5):\n            timer.start(\"looptest\")\n            num = randint(900,1000)\n            print(num, factorial(num))\n            timer.stop(\"looptest\")\n    \n    timer.start(\"func1\")\n    count_elements_in_array()\n    timer.stop(\"func1\")\n    \n    get_factorial()\n    \n    timer.stop(\"main\")\n    \n    timer.show()\n\n    -- Output\n    Name                           Duration             Start Time           End Time            \n    ============================== ==================== ==================== ====================\n    main                           0:00:00.046207       2024-04-08 21:24:59  2024-04-08 21:24:59 \n    func1                          0:00:00.033129       2024-04-08 21:24:59  2024-04-08 21:24:59 \n    looptest.4                     0:00:00.010020       2024-04-08 21:24:59  2024-04-08 21:24:59 \n    looptest.2                     0:00:00.003058       2024-04-08 21:24:59  2024-04-08 21:24:59 \n    looptest.1                     0:00:00              2024-04-08 21:24:59  2024-04-08 21:24:59 \n    looptest.3                     0:00:00              2024-04-08 21:24:59  2024-04-08 21:24:59 \n    looptest.5                     0:00:00              2024-04-08 21:24:59  2024-04-08 21:24:59 \n\n[Back to contents](#table-of-contents)\n\n## Print current position\n\nThis function comes in handy while debugging using print. Instead of manually writting print statements\nand thinking what to write after print to pinpoint to the position, we can simply write curpos.show(). This\nmakes it easy to delete the lines later as print statements can be part of programs or debug. But curpos will\nonly be part of debug. To temporarily disable debug, write curpos.disable_show() at the beginning of program.\nThis will suppress all curpos.show() messages.\n\n    from mdpython.debugutils import curpos\n    from random import randint\n    from math import factorial\n    \n    \n    def count_elements_in_array():\n        ar = list(range(randint(1000000, 10000000)))\n        print(len(ar))\n    \n    \n    def get_factorial():\n        for i in range(5):\n            num = randint(900, 1000)\n            print(num, factorial(num))\n            curpos.show()\n    \n    \n    count_elements_in_array()\n    curpos.show()\n    \n    get_factorial()\n\n[Back to contents](#table-of-contents)\n\n## Track package changes\n\nThis function is used to identify changes to installed python library versions over a period\nof time. Periodically call version.save to save a dump of current version details for all\ninstalled packages in a JSON file. Pass the directory where a time stamped JSON file will be created.\nIf any version issue occurs in future or if we want to check if there are any changes to installed\nversions, call version.timeline to get timeline of what was changed and when. Call version.compare to see\nwhat has changed after the latest snapshot and now.\n\n    from mdpython.debugutils import version\n    \n    version.save(\"app_dev\", r\"D:\\data\\version\")\n    version.timeline(\"app_dev\", r\"D:\\data\\version\")\n    version.compare(\"app_dev\", r\"D:\\data\\version\")\n\n[Back to contents](#table-of-contents)\n\n## Search for functions\n\nThis functions scans through docstrings to search for text within them. To get of elements, call\nlist_elements function and pass object, search string. To also see relevant documentation, pass\nshowdoc=True\n\n    import pandas as pd\n    from mdpython.debugutils import find\n    \n    a = [1,2]\n    b = {\"k\": 1}\n    s = pd.Series(range(10))\n    \n    find.search_function(pd, \"\")\n    find.list_elements(s, \"truncate\", showdoc=True)\n\n[Back to contents](#table-of-contents)\n\n## Flatten JSON\n\nThis function is used to flatten a nested JSON into a single key value pair flat JSON. Nested keys are\nflattened to . notation. So {\"car\":{\"color\":\"red\"}} will be flattened to {\"car.color\": \"red\"}. Nested\narrays are flattened to position. So {\"car\": [{\"color\": \"red\"}, {\"color\": \"blue\"}]} will be flattened to\n{\"car.0.color\": \"red\", \"car.1.color\": \"blue\"}\n\n    from mdpython.datautils import jsonutil\n    import json\n    \n    json_data = {\n        \"name\": \"John\",\n        \"age\": 30,\n        \"car\": {\n            \"make\": \"Toyota\",\n            \"model\": \"Camry\"\n        },\n        \"colors\": [\"red\", \"blue\", \"green\"],\n        \"nested_list\": [\n            [1, 2, 3],\n            {\"hello\": \"world\"},\n            [[7, 8], [9, 10]],\n            [[[11, 12], [13, 14]], [[], [17, 18]]]\n        ],\n        \"nested_dict\": {\n            \"info1\": {\"key1\": \"value1\"},\n            \"info2\": {\"key2\": \"value2\"}\n        },\n        \"list_of_dicts\": [\n            {\"item1\": \"value1\"},\n            {\"item2\": \"value2\"}\n        ]\n    }\n    \n    flattened_data = jsonutil.flatten_json(json_data)\n    print(json.dumps(flattened_data, indent=2))\n\n[Back to contents](#table-of-contents)\n\n## List of JSON object to CSV file\n\nThis functions flattens a nested JSON and dumps it into a csv file. Flattening happens in same fashion\nas described in [Flatten JSON](#flatten-json). Each unique key forms a column in the CSV file.\n\n    from mdpython.datautils import jsonutil\n    \n    out_fl = r\"C:\\Users\\Dell\\Onedrive\\Desktop\\out.csv\"\n    \n    json_data = [\n        {\n            \"name\": \"John\",\n            \"age\": 30,\n            \"car\": {\n                \"make\": \"Toyota\",\n                \"model\": \"Camry\"\n            },\n            \"colors\": [\"red\", \"blue\", \"green\"]\n        }, {\n            \"name\": \"Sheema\",\n            \"age\": 25,\n            \"car\": {\n                \"make\": \"Audi\",\n                \"model\": \"a4\",\n                \"dimension\": [5000, 1850, 1433]\n            },\n            \"colors\": [\"blue\", \"yellow\"]\n        }, {\n            \"name\": \"Bruce\",\n            \"car\": {\n                \"make\": \"Ford\"\n            }\n        }\n    ]\n    \n    jsonutil.list_to_csv(json_data, out_fl)\n\n[Back to contents](#table-of-contents)\n\n## Search for text inside JSON data\n\nThis function searches for text within a nested JSON. Both keys and values are searched for the\nprovided text. The output is returned as a list of flattened JSON for matched values. flattening\nrules are same as applied in [Flatten JSON](#flatten-json)\n\n    from mdpython.datautils import jsonutil\n    \n    json_data = {\n        \"data\": [\n            {\n                \"name\": \"John\",\n                \"age\": 30,\n                \"car\": {\n                    \"make\": \"Toyota\",\n                    \"model\": \"Camry\"\n                },\n                \"colors\": [\"red\", \"blue\", \"green\"]\n            }, {\n                \"name\": \"Sheema\",\n                \"age\": 25,\n                \"car\": {\n                    \"make\": \"Audi\",\n                    \"model\": \"a4\",\n                    \"dimension\": [5000, 1850, 1433]\n                },\n                \"colors\": [\"blue\", \"yellow\"]\n            }, {\n                \"name\": \"Bruce\",\n                \"car\": {\n                    \"make\": \"Ford\"\n                }\n            }\n        ]\n    }\n    \n    print(jsonutil.search(json_data, \"blue\"))\n\n    # Output\n    [['data.0.colors.1', 'blue'], ['data.1.colors.0', 'blue']]\n\n[Back to contents](#table-of-contents)\n\n## Get all objects for a particular key in JSON\n\nThis function recursively searches through a nested JSON and returns a list of all values\ncorresponding to provided key.\n\n    from mdpython.datautils import jsonutil\n\n    json_data = {\n        \"data\": [\n            {\n                \"name\": \"John\",\n                \"age\": 30,\n                \"car\": {\n                    \"make\": \"Toyota\",\n                    \"model\": \"Camry\"\n                },\n                \"colors\": [\"red\", \"blue\", \"green\"]\n            }, {\n                \"name\": \"Sheema\",\n                \"age\": 25,\n                \"car\": {\n                    \"make\": \"Audi\",\n                    \"model\": \"a4\",\n                    \"dimension\": [5000, 1850, 1433]\n                },\n                \"colors\": [\"blue\", \"yellow\"]\n            }, {\n                \"name\": \"Bruce\",\n                \"car\": {\n                    \"make\": \"Ford\"\n                }\n            }\n        ]\n    }\n    \n    print(jsonutil.find_values_by_key(json_data, \"colors\"))\n\n    # Output\n    [['red', 'blue', 'green'], ['blue', 'yellow']]\n\n[Back to contents](#table-of-contents)\n\n## Reconcile bills and payments\n\nThis function reconciles payments against bills. It uses a 4 step approach to match bills and payments\n\n1. First payments with exact amount with date greater than or equal to bill date are mapped\n2. Discount percentages can be passed as a list to the function to provide discount on final bill amount\n3. If addition of more than one payment with date same or after bill amount matches value, then it is mapped.\n   Similarly if addition of more than one bill amount matches a payment amount on or after bill date, then it\n   is mapped.\n4. Remaining payments are distributed across bills on a first come first serve basis where payment date is\n   greater than or equal to bill date\n\nThe function requires 2 dataframes, one for payment and one for bill. 4 columns are required - bill date, bill amount\n, payment date and payment amount.\n\n    import pandas as pd\n    from mdpython.account import reconcile\n    \n    inp_fl = r\"C:\\Users\\Dell\\Onedrive\\Desktop\\input.xlsx\"\n    out_fl = r\"C:\\Users\\Dell\\Onedrive\\Desktop\\output.xlsx\"\n    \n    disc_ar = [2, 2.5, 5, 10]\n    \n    bill_df = pd.read_excel(inp_fl, usecols=\"B:C\").dropna()\n    pymt_df = pd.read_excel(inp_fl, usecols=\"G:H\").dropna()\n    \n    recon = reconcile.reconcile_payment(\n        bill_df=bill_df\n        , pymt_df=pymt_df\n        , bill_dt_col=\"Bill Date\"\n        , bill_amt_col=\"Bill Amount\"\n        , pymt_dt_col=\"Payment Date\"\n        , pymt_amt_col=\"Payment Amount\"\n        , disc_ar=disc_ar\n    )\n    \n    print(recon.bill_dtl_df)\n    print(recon.pymt_dtl_df)\n    \n    recon.to_excel(out_fl)\n\n[Back to contents](#table-of-contents)\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "MD Python Library - High level library for common use cases",
    "version": "0.0.23",
    "project_urls": null,
    "split_keywords": [],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "e6a9225484762094a5a9ded2aab92c89ab94cd84af517f24dcb3721a98443d08",
                "md5": "818cf43267744e4c5f3a0d6218f760f0",
                "sha256": "98ff8586a82c6fce51c97c236f0f9d71d2c46d81c2f87dcf34d78ba3d61015d7"
            },
            "downloads": -1,
            "filename": "mdpython-0.0.23-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "818cf43267744e4c5f3a0d6218f760f0",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 17661,
            "upload_time": "2025-02-16T10:37:27",
            "upload_time_iso_8601": "2025-02-16T10:37:27.395991Z",
            "url": "https://files.pythonhosted.org/packages/e6/a9/225484762094a5a9ded2aab92c89ab94cd84af517f24dcb3721a98443d08/mdpython-0.0.23-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "fed47a45d43a1900c02c3062be17bee43e63019ce4eb2cbef7e632ffd97f5c9c",
                "md5": "a7d8a35edfbcd4d4fd51f7bd33c2284c",
                "sha256": "6908f0428236a025259c876d32a6d396a30b81694ce969d1c1089d934287340e"
            },
            "downloads": -1,
            "filename": "mdpython-0.0.23.tar.gz",
            "has_sig": false,
            "md5_digest": "a7d8a35edfbcd4d4fd51f7bd33c2284c",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 13332,
            "upload_time": "2025-02-16T10:37:29",
            "upload_time_iso_8601": "2025-02-16T10:37:29.058882Z",
            "url": "https://files.pythonhosted.org/packages/fe/d4/7a45d43a1900c02c3062be17bee43e63019ce4eb2cbef7e632ffd97f5c9c/mdpython-0.0.23.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-02-16 10:37:29",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "mdpython"
}
        
Elapsed time: 1.44701s