A package for unpacking int, float, string and bool objects from a text string.
Project description
strup - string unpack
This Python package is for unpacking basic objects from a text string. The standard data types string, int, float and bool are supported.
Goals
A major goal with strup is to provide an intuitive interface. If the standard string methods are too low level and the re-module adds too much complexity to your task, then strup might be your compromise.
Backward compatibility of this API is strongly emphasized.
Usage
We apply the utility function unpack(fmt, text) for extracting strings, ints, floats and bools from a text string text. Each character in the string fmt defines the data type for the corresponding object.
>>> from strup import unpack
>>> i, x, s, ok = unpack("ifs?", "5 2.3 ole True")
>>> i, x, s, ok
(5, 2.3, 'ole', True)
Similar syntax is applied in the standard struct module for handling of binary data. However, dots in the fmt string indicates that the corresponding item in text should be ignored.
>>> unpack("f..s", " 2.3 ,ole,55, dole", sep=',') # sep as defined in string.split()
(2.3, ' dole')
Strings confined by quotes are supported
>>> unpack("isf", "100 'Donald Duck' 125.6", quote="'")
(100, 'Donald Duck', 125.6)
Zero-sized string might be interpreted as None objects.
>>> unpack("fissi", "2.3,,, ,12", sep=',', none=True)
(2.3, None, None, ' ', 12)
In loops you may benefit from splitting the grammar and the actual decoding using the class Unpack:
>>> from strup import Unpack
>>> mydecode = Unpack('.s..f', quote='"') # Preprocess the pattern
>>> for line in ['5.3 "Donald Duck" 2 yes 5.4',
'-2.2 "Uncle Sam" 4 no 1.5',
'3.3 "Clint Eastwood" 7 yes 6.5']:
... mydecode(line)
("Donald Duck", 5.4)
("Uncle Sam", 1.5)
("Clint Eastwood", 6.5)
Documentation
Complete documentation and more examples are hosted on ReadTheDocs.
Source code
The source code for this package is located on GitHub.
Installation
To install strup from PyPI:
pip install strup # For end users
pip install -e .[dev] # For package development (from the root of your strup Git repo)
or Anaconda:
conda install -c jeblohe strup
strup is continuously tested on Python 2.7, 3.4 and above.
License
This software is licensed open-source under the MIT License.
Project details
Release history Release notifications | RSS feed
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
File details
Details for the file strup-1.0.0.tar.gz
.
File metadata
- Download URL: strup-1.0.0.tar.gz
- Upload date:
- Size: 7.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.0 requests/2.24.0 setuptools/50.3.0.post20201006 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.7.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3340bc7c07ccaf9c70215d3339b26d0377a010463110ef338406eb2719083786 |
|
MD5 | 95ae01e7ac3ef0d368dd28f5a7479ce3 |
|
BLAKE2b-256 | 72931e182f04e890e17339d59028c409231ca261a4d58e2b745e12aa0cc6647c |
File details
Details for the file strup-1.0.0-py2.py3-none-any.whl
.
File metadata
- Download URL: strup-1.0.0-py2.py3-none-any.whl
- Upload date:
- Size: 7.5 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.0 requests/2.24.0 setuptools/50.3.0.post20201006 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.7.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 07701f10d57d7559bb32d1d28ab7ee18770f01e3feb9c0a0923cd1610e80f48d |
|
MD5 | a820c35f242ec9aad1349d2e7c65cfb0 |
|
BLAKE2b-256 | 133fe0144afa35e8f6b2926ed42e47f9ebe6de79f48a89fb9a8e3efac49ca5b9 |