Skip to main content

A shameless library for auto-correcting variable types.

Project description

shaml

Shaml (short for Shameless) is a multi-language library that simplifies data handling by automatically converting input values to more appropriate types. It aims to reduce boilerplate code for type checking and conversion across various programming languages, providing a consistent and intuitive API.

Static Badge

Static Badge

Supported Languages

  • Python
  • JavaScript (Node.js)

Installation

Python

pip install shaml

JavaScript (Node.js)

npm install shaml

Usage

Python

from shaml import sl

# String to int/float
number = sl("123")  # Converts to integer 123
price = sl("45.67") # Converts to float 45.67

# String to boolean
flag = sl("True")   # Converts to boolean True
state = sl("False")  # Converts to boolean False

# String to list/dict
data = sl("[1, 2, '3']")  # Converts to list [1, 2, '3']
config = sl("{'name': 'Alice', 'age': '30'}")  # Converts to dict {'name': 'Alice', 'age': '30'}

# Accessing value
print(sl(123))
print(sl(123.45))
print(sl(True))
print(sl(False))
print(sl([1, 2, "3"]))
print(sl({"name": "Alice", "age": "30"}))
print(sl(None))
print(sl("hello"))

Dot Notation (Python)

If a JSON or dictionary is passed, then you can access each key using dot notation:

from shaml import sl

config = sl("{'name': 'Alice', 'age': '30'}")
print(config.name)  # prints Alice
print(config.age)  # prints 30 as integer

JavaScript (Node.js)

const sl = require('shaml');

// String to number
const number = sl("123"); // Converts to number 123
const price = sl("45.67"); // Converts to number 45.67

// String to boolean
const flag = sl("true"); // Converts to boolean true
const state = sl("false"); // Converts to boolean false

// String to array/object
const data = sl("[1, 2, '3']"); // Converts to array [1, 2, '3']
const config = sl("{'name': 'Alice', 'age': '30'}"); // Converts to object {'name': 'Alice', 'age': '30'}

// Accessing value
console.log(sl(123));
console.log(sl(123.45));
console.log(sl(true));
console.log(sl(false));
console.log(sl([1, 2, "3"]));
console.log(sl({"name": "Alice", "age": "30"}));
console.log(sl(null));
console.log(sl("hello"));

Dot Notation (JavaScript)

If a JSON or object is passed, then you can access each key using dot notation:

const sl = require('shaml');

const config = sl("{'name': 'Alice', 'age': '30'}");
console.log(config.name); // prints Alice
console.log(config.age); // prints 30 as number

Error Handling

Shaml will try to convert to the best of its ability; otherwise, it will return the original object.

Python

from shaml import sl

value = sl("hello")
print(value)  # prints hello

For debugging purposes, you can enable debug mode:

from shaml import sl

value = sl("hello", debug=True)

Then it will return a ShamelessError object, which can be used to determine the error:

from shaml import sl

value = sl("hello", debug=True)
if value:
    print("this will not be printed")
else:
    print(value, value.message, value.original_value)  # prints Shameless Error:  with original value hello   with original value hello

JavaScript (Node.js)

const sl = require('shaml');

const value = sl("hello");
console.log(value); // prints hello

Configuration Options

Python

Option Description Default Value
debug If set to True, the function will throw ShamelessError exceptions when a conversion fails. False

Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository.
  2. Create a new branch for your feature or bug fix.
  3. Implement your changes.
  4. Write tests to verify your changes.
  5. Submit a pull request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

shaml-0.0.2.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

shaml-0.0.2-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file shaml-0.0.2.tar.gz.

File metadata

  • Download URL: shaml-0.0.2.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for shaml-0.0.2.tar.gz
Algorithm Hash digest
SHA256 1f79ae928776b5ac9e74d4f966e1bb6d0187bc242d351aad175bad711d51c33a
MD5 b930ec504dda467531f1ce563077e3e2
BLAKE2b-256 3a7f3b96e3235ede3eb6807d91044bcbc9ed7a0729df076941e8a4422252cc96

See more details on using hashes here.

File details

Details for the file shaml-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: shaml-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for shaml-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 6c07e2b163866350f42b641aeb97e2d27b6bc9335da4a4d968482993719f03ac
MD5 10259195211e031e2f08c6aabbc963ef
BLAKE2b-256 8e91875df35a65de0d8f4967fe5ff93ce517adbd219daeb846b893d51cf61a0e

See more details on using hashes here.

Supported by

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