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.0.tar.gz (11.3 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: morvba-2.0.0.tar.gz
  • Upload date:
  • Size: 11.3 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.0.tar.gz
Algorithm Hash digest
SHA256 6c91287e94489340fabdf6c6d1eb9c8f399ebbf1fb1d74449a90ff51dfd9fa23
MD5 0087b41d6663aa44ed58af830141eed2
BLAKE2b-256 75c23b2eefad5686115415bdae3dc50a9c5ca4c3370d636c0df74b72adf3927b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: morvba-2.0.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c5437823b8f25510f5082a789764339a443f60dfa0069b190b03d8886cdd46c4
MD5 47b900ba235ca4bb28e2b2f5c99ddf14
BLAKE2b-256 f4e157b25d5f8cb14655151b723a4ca22375c2bfcb823a1ec20ae8f1ab88e3ca

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