Skip to main content

MD Python Library

Project description

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)

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

mdpython-0.0.16.tar.gz (10.1 kB view details)

Uploaded Source

Built Distribution

mdpython-0.0.16-py3-none-any.whl (14.4 kB view details)

Uploaded Python 3

File details

Details for the file mdpython-0.0.16.tar.gz.

File metadata

  • Download URL: mdpython-0.0.16.tar.gz
  • Upload date:
  • Size: 10.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.2

File hashes

Hashes for mdpython-0.0.16.tar.gz
Algorithm Hash digest
SHA256 e2ba2b1ae50d1f3ee259275d5ac31fe3f3b977de5f5cebc6b1f262252578c4b9
MD5 0ad4e0276da15d0c84be320b15e964c0
BLAKE2b-256 e32213529cf22c7dc1ab7eb02a02619aabf568a5d3893230112bee765659dbc3

See more details on using hashes here.

File details

Details for the file mdpython-0.0.16-py3-none-any.whl.

File metadata

  • Download URL: mdpython-0.0.16-py3-none-any.whl
  • Upload date:
  • Size: 14.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.2

File hashes

Hashes for mdpython-0.0.16-py3-none-any.whl
Algorithm Hash digest
SHA256 0b0d37bd515835ae1e99949cd4d0410b8f51c0793c164c1df7f209d58c38276b
MD5 e26147d589e5d0ac9168f7933891e2d7
BLAKE2b-256 1e36153003b100240e0e12c42f1bcf68414519d49f4b471d21c57481d0c99232

See more details on using hashes here.

Supported by

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