StrTokenizer
A Python module that mimics the functionality of the Java StringTokenizer class. This class splits a given string into tokens based on a specified delimiter and offers methods to iterate over the tokens, count them, and manipulate the tokenizer's state.
Installation
To install the StrTokenizer package globally, you can use pip. Here are the steps to install:
- Ensure you have
pipinstalled on your system. - Open your command line interface (CLI) and run:
pip install StrTokenizer
If you want to use it locally without installing, simply download or copy the tokenizer.py file and import it into your project.
Usage
Import the Module
If the module is installed via pip, import the class from your module:
from StrTokenizer import StrTokenizer
If the module (tokenizer.py) is downloaded from GitHub, import it like this:
from tokenizer import StrTokenizer
Creating a StrTokenizer Object
To create an instance of StrTokenizer, provide the input string, the delimiter (optional, defaults to a space " "), and whether to return the delimiters as tokens (optional, defaults to False).
# Example with default delimiter (space)
tokenizer = StrTokenizer("This is a test string")
# Example with custom delimiter
tokenizer = StrTokenizer("This,is,a,test,string", ",")
# Example with custom delimiter and returning the delimiter as tokens
tokenizer = StrTokenizer("This,is,a,test,string", ",", return_delims=True)
Methods
countTokens() -> int
Returns the total number of tokens in the string.
token_count = tokenizer.countTokens()
print("Number of tokens:", token_count)
countTokensLeft() -> int
Returns the number of tokens left to be iterated.
tokens_left = tokenizer.countTokensLeft()
print("Tokens left:", tokens_left)
hasMoreTokens() -> bool
Checks if there are more tokens to iterate over.
if tokenizer.hasMoreTokens():
print("There are more tokens available.")
nextToken() -> str
Returns the next token. Raises an IndexError if no more tokens are available.
while tokenizer.hasMoreTokens():
print(tokenizer.nextToken())
rewind(steps: int = None) -> None
Resets the tokenizer's index either completely or by a specified number of steps:
- Without arguments: Resets the tokenizer back to the first token.
- With
steps: Moves the tokenizer back by the given number of steps.
# Rewind completely
tokenizer.rewind()
# Rewind by 2 tokens
tokenizer.rewind(2)
Example
from tokenizer import StrTokenizer
# Create a tokenizer with a custom delimiter
tokenizer = StrTokenizer("apple,orange,banana,grape", ",")
# Get the number of tokens
print("Number of tokens:", tokenizer.countTokens())
# Iterate over the tokens
while tokenizer.hasMoreTokens():
print("Token:", tokenizer.nextToken())
# Rewind the tokenizer and iterate again
tokenizer.rewind()
print("After rewinding:")
while tokenizer.hasMoreTokens():
print("Token:", tokenizer.nextToken())
Output:
Number of tokens: 4
Token: apple
Token: orange
Token: banana
Token: grape
After rewinding:
Token: apple
Token: orange
Token: banana
Token: grape
Methods Overview
-
__init__(self, inputstring: str, delimiter: str = " ", return_delims: bool = False):- Initializes the
StrTokenizerwith the given string, delimiter, and whether to return delimiters as tokens.
- Initializes the
-
__create_token(self) -> None:- Splits the input string into tokens based on the delimiter.
-
countTokens(self) -> int:- Returns the total number of tokens.
-
countTokensLeft(self) -> int:- Returns the number of tokens left for iteration.
-
hasMoreTokens(self) -> bool:- Checks if there are more tokens to be retrieved.
-
nextToken(self) -> str:- Returns the next available token or raises an
IndexErrorif no tokens are left.
- Returns the next available token or raises an
-
rewind(self, steps: int = None) -> None:- Resets the tokenizer's index either completely or by a given number of steps.
You can install the StrTokenizer package from PyPI:
Install StrTokenizer from PyPI
Source Code:
License
This project is open-source and available for modification or distribution.
Metadata
Release files for StrTokenizer 1.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| strtokenizer-1.1.0.tar.gz | 4.1 kB | Without provenance | Details for strtokenizer-1.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| StrTokenizer-1.1.0-py3-none-any.whl | Python 3 | none | any | Details for StrTokenizer-1.1.0-py3-none-any.whl |
Total release size: 8.7 kB
Release files / strtokenizer-1.1.0.tar.gz
| Download URL | strtokenizer-1.1.0.tar.gz |
|---|---|
| Size | 4.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5bdccbf31c4f7956850344772d0d6949f148faa56ffe3b03517e623c4e24eb6b
|
|
BLAKE2b-256 checksum How to use checksums |
bc8733987fde57d456599f18b245a24017f9d120e5dc4119673099411ec2a5d5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.12.6
|
Release files / StrTokenizer-1.1.0-py3-none-any.whl
| Download URL | StrTokenizer-1.1.0-py3-none-any.whl |
|---|---|
| Size | 4.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2971e2572b9d92a455e83eeba501e4ccb03dc9cde7ecad2a8cdcf74bd6706879
|
|
BLAKE2b-256 checksum How to use checksums |
446cd11d591189820fbd04df66e961130185bb3ad9448decd94d61d2698c2a71
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.12.6
|
Security for StrTokenizer 1.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.