Skip to main content

A package providing VBA-like and other useful functions for Python.

Project description

VBA-like and Useful Functions in Python

This module provides a set of VBA-like and other useful functions that you can use directly in your Python projects. Save the cC.py file to your project folder and import the functions using the following import statement:

from cC import *

Alternatively you can import it with pip.

pip install -U morvba

Functions


savesetting(a='', b='', c='', d='')

Save a setting to an INI file.

Arguments:

  • a: Group name
  • b: Subgroup name
  • c: Item name
  • d: Value

Example:

savesetting("app", "window", "width", "1024")

getsetting(a='', b='', c='', d='')

Retrieve a setting from an INI file.

Arguments:

  • a: Group name
  • b: Subgroup name
  • c: Item name
  • d: Default value (optional)

Example:

window_width = getsetting("app", "window", "width", "")

savesettingreg(a='', b='', c='', d='')

Save a setting to the Windows registry.

Arguments:

  • a: Main key name
  • b: Subkey name
  • c: Value name
  • d: Data to set

Example:

savesettingreg("Software", "MyApp", "Version", "1.0")

getsettingreg(a='', b='', c='', d='')

Retrieve a setting from the Windows registry.

Arguments:

  • a: Main key name
  • b: Subkey name
  • c: Value name
  • d: Default value (optional)

Example:

version = getsettingreg("Software", "MyApp", "Version", "1.0")

left(str_text='', int_len=1)

Return the left part of a string.

Arguments:

  • str_text: Input string
  • int_len: Number of characters to return (default is 1)

Example:

result = left("Hello, world!", 5)

right(str_text='', int_len=1)

Return the right part of a string.

Arguments:

  • str_text: Input string
  • int_len: Number of characters to return (default is 1)

Example:

result = right("Hello, world!", 6)

mid(str_text='', int_start=1, int_len=1)

Return a substring from the middle of a string.

Arguments:

  • str_text: Input string
  • int_start: Starting position
  • int_len: Number of characters to return

Example:

result = mid("Hello, world!", 8, 5)

pricap(str_text='')

Capitalize the first letter of each word in a string.

Arguments:

  • str_text: Input string

Example:

result = pricap("hello, world!")

instr(int_start=1, str_text='', str_what='')

Return the position of a substring in a string.

Arguments:

  • int_start: Starting position
  • str_text: Input string
  • str_what: Substring to search for

Example:

position = instr(1, "Hello, world!", "world")

trim(str_text='', str_char=' ')

Trim spaces or a specific character from a string.

Arguments:

  • str_text: Input string
  • str_char: Character to trim (default is space)

Example:

result = trim("   Hello, world!   ")

pkey(str_text='', str_ini='', str_end='', boo_trim=True)

Extract a string between two delimiters.

Arguments:

  • str_text: Input string
  • str_ini: Starting delimiter
  • str_end: Ending delimiter
  • boo_trim: Whether to trim spaces (default is True)

Example:

result = pkey("Example: [Hello, world!]", "[", "]")

err(e='')

Print a formatted error message.

Arguments:

  • e: The error message or exception

Example:

try:
    # some code that might raise an exception
except Exception as e:
    err(e)

text_to_list(text='')

Convert a string representation of a list back to a list.

Arguments:

  • text: Input string representing a list

Example:

my_list = text_to_list("[1, 2, 3]")

remove_first_part_of_domain(url='')

Remove the subdomain from a given URL.

Arguments:

  • url: The input URL

Example:

domain = remove_first_part_of_domain("sub.example.com")

get_base_url(url='')

Extract the base URL (portion before the "?").

Arguments:

  • url: The input URL

Example:

base = get_base_url("https://example.com/page?query=value")

remove_html(text='')

Remove HTML, script, and style tags from a given text.

Arguments:

  • text: The input text with HTML content

Example:

clean_text = remove_html("<div>Hello <span>World</span></div>")

extract_hrefs(html='')

Extract all href values from anchor tags in an HTML string.

Arguments:

  • html: The input HTML content

Example:

links = extract_hrefs("<a href='link1.html'>Link 1</a> <a href='link2.html'>Link 2</a>")

remove_extra_whitespace(t='')

Remove extra whitespace from a given text.

Arguments:

  • t: The input text

Example:

cleaned_text = remove_extra_whitespace("This   is   a    text.")

remove_html_tags(text='')

Remove all HTML tags from a given text.

Arguments:

  • text: The input text with HTML content

Example:

clean_text = remove_html_tags("<div>Hello <span>World</span></div>")

remove_html_scripts(text='')

Remove script and style tags from a given HTML text.

Arguments:

  • text: The input text with HTML content

Example:

clean_text = remove_html_scripts("<script>alert('Hi');</script><div>Hello</div>")

clean_space_line(text='')

Clean spaces and line breaks from a given text.

Arguments:

  • text: The input text

Example:

cleaned_text = clean_space_line("This   is   a    text.")

convert_url_to_filename(url='')

Convert a URL to a filename by removing the protocol and replacing invalid characters.

Arguments:

  • url: The input URL

Example:

filename = convert_url_to_filename("https://example.com/page?query=value")

md5(value='')

Generate an MD5 hash for a given value.

Arguments:

  • value: The input value

Example:

hash_value = md5("Hello World")

jsGetUTCTime()

Get the current UTC time in a specific format.

Example:

current_time = jsGetUTCTime()

jsURLenc(u='')

URL encode a given string.

Arguments:

  • u: The input string

Example:

encoded = jsURLenc("Hello World!")

jsURLdec(u='')

URL decode a given string.

Arguments:

  • u: The encoded string

Example:

decoded = jsURLdec("Hello%20World%21")

jsGetDomainName(hostName='')

Get the domain name from a given URL or hostname.

Arguments:

  • hostName: The input URL or hostname

Example:

domain = jsGetDomainName("https://sub.example.com/page")

is_url(str='')

Check if a given string is a valid URL.

Arguments:

  • str: The input string

Example:

is_valid = is_url("https://example.com")

is_email(email_addr='')

Check if a given string is a valid email address.

Arguments:

  • email_addr: The input email address

Example:

is_valid_email = is_email("test@example.com")

is_date(string='', fuzzy=False)

Check if a given string can be interpreted as a date.

Arguments:

  • string: The input string
  • fuzzy: Whether to ignore unknown tokens in the string

Example:

is_valid_date = is_date("2023-09-07")

num_tokens(messages='', model="gpt-4", stat_adjust=1.55)

Calculate the number of tokens used by a list of messages.

Arguments:

  • messages: The input messages
  • model: The model name (default is "gpt-4")
  • stat_adjust: Statistical adjustment factor

Example:

tokens = num_tokens(["Hello", "World"])

open_encoding(file='')

Detect and open a file with its correct encoding.

Arguments:

  • file: The path to the file

Example:

content = open_encoding("sample.txt")

You can use these functions in your Python code without referencing the class name, just like you would in VBA.

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

morvba-2.0.1.tar.gz (11.4 kB view details)

Uploaded Source

Built Distribution

morvba-2.0.1-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

Details for the file morvba-2.0.1.tar.gz.

File metadata

  • Download URL: morvba-2.0.1.tar.gz
  • Upload date:
  • Size: 11.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for morvba-2.0.1.tar.gz
Algorithm Hash digest
SHA256 91fceef3cf0dd6ddbaae9346d9519124de12a1426e26381fc188ff0dc10093dc
MD5 37dc9cf833c010952b764e69abfe5baf
BLAKE2b-256 6e0f78ea09e7fa3ca136652dba8a5794aa0c70ad608f9f5fba65e68437421c84

See more details on using hashes here.

File details

Details for the file morvba-2.0.1-py3-none-any.whl.

File metadata

  • Download URL: morvba-2.0.1-py3-none-any.whl
  • Upload date:
  • Size: 10.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for morvba-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a3810eb8f785e139f96c9ceca2225e40c7736d30ed4c31510b187d5b5318605e
MD5 688dd5ab158ac563d41dd34f8fe4befb
BLAKE2b-256 e899e90e38ea654b3150ec13536b1aa9682624d09c8acf107f0355bc02898c05

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