Package for string maths
Project description
String Arithmetic Library
This documentation was translated and compiled with the help of AI. Examples may contain minor errors that do not affect the understanding of how it works.
Overview
The library provides tools for performing arithmetic operations on strings by converting characters to their numeric codes (ASCII/Unicode) and back. It allows adding, subtracting, multiplying, and dividing strings with both numbers and other strings.
Quick start
Write in cli:
pip install pystringmath
And in Python script:
from pystringmath import *
for i in range(1000):
print(nmath.sum('Gabe Newell\'s mom very thin', i)) # Caesar cipher with shift i. Just look at output!`
to.non_safe_array(data) (Recomendated for use)
Converts a string to an array of character numeric codes. You can pass an array, and the method will return the same array.
Parameters:
data(str, list) - input string
Returns:
- list[int] - array of character codes
Exceptions:
TypeError- only if input data is not integer list
Example:
>>> to.non_safe_array('Hello')
[72, 101, 108, 108, 111]
>>> to.non_safe_array([72, 101, 108, 108, 111])
[72, 101, 108, 108, 111]
Class to
A helper class for converting strings to code arrays and back.
Methods
to.array(data)
Converts a string to an array of character numeric codes.
Parameters:
data(str) - input string
Returns:
- list[int] - array of character codes
Exceptions:
TypeError- if input data is not a string
Example:
>>> to.array('Hello')
[72, 101, 108, 108, 111]
to.chars(data)
Converts an array of numeric codes back to a string.
Parameters:
data(list[int]) - array of character codes
Returns:
- str - resulting string
Exceptions:
TypeError- if input data is invalid
Example:
>>> to.chars([72, 101, 108, 108, 111])
'Hello'
Class nmath
Performs arithmetic operations on a string using a single number.
General Features
- All methods accept a string and a number
- Operations are applied to each character in the string
- Result is returned as a string
nmath.sum(data, plus)
Adds a number to each character in the string.
Parameters:
data(str) - source stringplus(int) - number to add
Returns:
- str - new string
Example:
>>> nmath.sum('ABC', 1)
'BCD' # A(65)+1=66('B'), B(66)+1=67('C'), C(67)+1=68('D')
nmath.min(data, minus)
Subtracts a number from each character in the string.
Parameters:
data(str) - source stringminus(int) - number to subtract
Returns:
- str - new string
Example:
>>> nmath.min('DEF', 1)
'CDE' # D(68)-1=67('C'), E(69)-1=68('D'), F(70)-1=69('E')
nmath.mul(data, multiplier)
Multiplies each character's code by a number.
Parameters:
data(str) - source stringmultiplier(int) - multiplier
Returns:
- str - new string
Example:
>>> nmath.mul('AB', 2)
'ÆÊ' # A(65)*2=130('Æ'), B(66)*2=132('Ê')
nmath.div(data, divisor)
Performs integer division of each character's code by a number.
Parameters:
data(str) - source stringdivisor(int) - divisor
Returns:
- str - new string
Exceptions:
ZeroDivisionError- when attempting division by zero
Example:
>>> nmath.div('d', 2)
'2' # d(100)//2 = 50('2')
Class armath
Performs element-wise arithmetic operations between two strings with cyclic repetition.
General Features
- Accepts two strings
- Operations are performed on corresponding character codes
- If the second string is shorter, it's cyclically repeated (via
itertools.cycle) - Result is returned as a string
armath.sum(data, plus)
Element-wise addition of character codes from two strings.
Parameters:
data(str) - first stringplus(str) - second string (addend)
Returns:
- str - resulting string
Example:
>>> armath.sum('ABC', '12')
'rtt' # A(65)+'1'(49)=114('r')
# B(66)+'2'(50)=116('t')
# C(67)+'1'(49)=116('t') - cyclic repetition of '12'
armath.min(data, subtrahend)
Element-wise subtraction of second string character codes from the first.
Parameters:
data(str) - first stringsubtrahend(str) - second string (subtrahend)
Returns:
- str - resulting string
Example:
>>> armath.min('ABC', '12')
'\x10\x11\x12' # A(65)-'1'(49)=16 (DLE character, non-printable)
# Results may be in non-printable range
armath.mul(data, multiplier)
Element-wise multiplication of character codes from two strings.
Parameters:
data(str) - first stringmultiplier(str) - second string (multipliers)
Returns:
- str - resulting string
Example:
>>> armath.mul('AB', '12')
'1¡' # A(65)*'1'(49)=3185 - may exceed ASCII range
armath.div(data, divisor)
Element-wise integer division of first string character codes by second string codes.
Parameters:
data(str) - first string (dividend)divisor(str) - second string (divisor)
Returns:
- str - resulting string
Exceptions:
ZeroDivisionError- if the second string contains a character with code 0
Example:
>>> armath.div('abcd', '12')
'\x00\x00\x00\x00' # Results may be non-printable
Class crypto
Basic encrypt and decrypt functions for prototyping and testing
General Features
- Basic encrypt and decrypt functions
.xor(data, key)
XOR enctypting and decrypting
Parameters:
data(str or list) - first stringkey(str or list) - second string (addend)
Returns:
- str - resulting string
Example:
>>> crypto.xor('Test text', 'key')
?
E
>>> crypto.xor(crypto.xor('Test text', 'key'), 'key')
Test text
Usage Examples
Basic Operations
# Simple numeric operations
text = "Hello"
encoded = nmath.sum(text, 5) # "Mjqqt" (shift by 5)
decoded = nmath.min(encoded, 5) # "Hello"
# String operations
result = armath.sum("Hello", "123") # Element-wise addition with cyclic repetition of "123"
Encryption/Decryption
# Simple shift cipher
def encrypt(text, key):
return nmath.sum(text, key)
def decrypt(text, key):
return nmath.min(text, key)
message = "Secret message"
encrypted = encrypt(message, 10)
decrypted = decrypt(encrypted, 10)
print(decrypted) # "Secret message"
Variant Generation
base = "Test"
variants = [nmath.mul(base, i) for i in range(1, 6)]
for i, variant in enumerate(variants, 1):
print(f"Variant {i}: {variant}")
Notes and Limitations
- Encoding: Works with Unicode characters, but results may be non-printable
- Integer Division: Uses
//, so fractional results are truncated - Value Range: Operation results may fall outside the printable character range
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pystringmath-1.3.1.tar.gz.
File metadata
- Download URL: pystringmath-1.3.1.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
211d6c7a137dfd6da6b18c0fe365a2bb9860232e85deb72aade6b40955e1dc6d
|
|
| MD5 |
f3043cd339ce517b8d80379068608fb6
|
|
| BLAKE2b-256 |
7ed7d6b66625cd740393fbacb3241ae31e30fe37d5637d286bf912f6927dc22f
|
File details
Details for the file pystringmath-1.3.1-py3-none-any.whl.
File metadata
- Download URL: pystringmath-1.3.1-py3-none-any.whl
- Upload date:
- Size: 5.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e94370547156a36d4b44798c5b2232fea3527a6a00d058dd55b05dfd2dee961e
|
|
| MD5 |
c972da4c872cced6a3b14bfd0a95ae25
|
|
| BLAKE2b-256 |
53eecc266a8040a28183ace8eda747816fd849b83d06a562f4a03cbded58d8df
|