Skip to main content

Convert a string representation of an int, float, None, True, False, or JSON to its native type. For None, True, and False, case is irrelevant.

Project description

[![Build Status](https://travis-ci.org/geowurster/str2type.svg?branch=master)](https://travis-ci.org/geowurster/str2type)

Convert a string representation of an int, float, None, True, False, or JSON to its native type. For None, True, and False, case is irrelevant. The primary use-case of this function is automatically parsing user-input from the commandline into Python types. Argument parsers usually handle this no problem but if a flag can take multiple types then str2type() can serve as the decoder.

>>> from str2type import str2type
>>> print(str2type("1.23"))
1.23
>>> print(str2type("1.")
1.0
>>> print(str2type(".2"))
2.0
>>> print(str2type("NonE"))
None

Installing

Via pip:

$ pip install str2type

From master branch:

$ git clone https://github.com/geowurster/str2type $ pip install ./str2type/

Developing

Install:

$ pip install virtualenv $ git clone https://github.com/geowurster/str2type $ cd str2type $ virtualenv venv $ source venv/bin/activate $ pip install -r requirements-dev.txt $ pip install -e .

Test:

$ nosetests

Lint:

$ pep8 –max-line-length=120 str2type.py

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

str2type-0.1.3.tar.gz (4.0 kB view hashes)

Uploaded Source

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