mdpython


Namemdpython JSON
Version 0.0.16 PyPI version JSON
download
home_pageNone
SummaryMD Python Library
upload_time2024-10-02 06:58:36
maintainerNone
docs_urlNone
authorNone
requires_python>=3.0
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 Python use cases.

# Compare Directories

    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())


# Menu based app

    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)

# Disk cleanup

    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)

# Getting execution duration

    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 

# Print current position

    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()

# Compare Files

    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")

# Track package changes

    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")

# Search for functions

    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)
    
# Flatten JSON

    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))

# List of JSON object to 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)

# Extract URLs from excel file

    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'])

# Search for text inside JSON data

    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']]
 
# Get all objects for a particular key in 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.find_values_by_key(json_data, "colors"))

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

# Reconcile bills and payments

    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)

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "mdpython",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.0",
    "maintainer_email": null,
    "keywords": null,
    "author": null,
    "author_email": "Dhaval <seedhaval@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/e3/22/13529cf22c7dc1ab7eb02a02619aabf568a5d3893230112bee765659dbc3/mdpython-0.0.16.tar.gz",
    "platform": null,
    "description": "# MD Python Library\n\nThis package contains high level functions for common Python use cases.\n\n# Compare Directories\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\n# Menu based app\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# Disk cleanup\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# Getting execution duration\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# Print current position\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# Compare Files\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# Track package changes\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# Search for functions\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# Flatten JSON\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# List of JSON object to 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# Extract URLs from excel file\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# Search for text inside JSON data\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# Get all objects for a particular key in 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.find_values_by_key(json_data, \"colors\"))\n\n    # Output\n    [['red', 'blue', 'green'], ['blue', 'yellow']]\n\n# Reconcile bills and payments\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",
    "bugtrack_url": null,
    "license": null,
    "summary": "MD Python Library",
    "version": "0.0.16",
    "project_urls": null,
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "1e36153003b100240e0e12c42f1bcf68414519d49f4b471d21c57481d0c99232",
                "md5": "e26147d589e5d0ac9168f7933891e2d7",
                "sha256": "0b0d37bd515835ae1e99949cd4d0410b8f51c0793c164c1df7f209d58c38276b"
            },
            "downloads": -1,
            "filename": "mdpython-0.0.16-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "e26147d589e5d0ac9168f7933891e2d7",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.0",
            "size": 14389,
            "upload_time": "2024-10-02T06:58:35",
            "upload_time_iso_8601": "2024-10-02T06:58:35.283259Z",
            "url": "https://files.pythonhosted.org/packages/1e/36/153003b100240e0e12c42f1bcf68414519d49f4b471d21c57481d0c99232/mdpython-0.0.16-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e32213529cf22c7dc1ab7eb02a02619aabf568a5d3893230112bee765659dbc3",
                "md5": "0ad4e0276da15d0c84be320b15e964c0",
                "sha256": "e2ba2b1ae50d1f3ee259275d5ac31fe3f3b977de5f5cebc6b1f262252578c4b9"
            },
            "downloads": -1,
            "filename": "mdpython-0.0.16.tar.gz",
            "has_sig": false,
            "md5_digest": "0ad4e0276da15d0c84be320b15e964c0",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.0",
            "size": 10144,
            "upload_time": "2024-10-02T06:58:36",
            "upload_time_iso_8601": "2024-10-02T06:58:36.829976Z",
            "url": "https://files.pythonhosted.org/packages/e3/22/13529cf22c7dc1ab7eb02a02619aabf568a5d3893230112bee765659dbc3/mdpython-0.0.16.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-10-02 06:58:36",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "mdpython"
}
        
Elapsed time: 0.34953s