Skip to main content
Help us improve Python packaging – donate today!

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

Convert the string representation of a Python type to its native type.

https://travis-ci.org/geowurster/str2type.svg?branch=master https://coveralls.io/repos/geowurster/str2type/badge.svg?branch=master

Why?

When building commandline utilities the syntax --arg key=val is useful, especially when key and val will be used as Class(key=val) internally. This module was built specifically to be used with the CLI framework click, and the included extensions play nicely, but the str2type.str2type() function can be used elsewhere.

Examples

See the examples directory for click integration examples. Everything else is primarily handled by a single function:

>>> from str2type import str2type
>>> print(str2type("1.23"))
1.23
>>> print(str2type("1.")
1.0
>>> print(str2type(".2"))
0.2
>>> print(str2type("None"))
None
>>> print(str2type('String'))
'String'

Supported Types

Only the standard builtin Python types plus JSON strings are supported:

  • int
  • float
  • None
  • True
  • False
  • JSON

Installing

Via pip:

$ pip install str2type

From master branch:

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

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 -e .
$ nosetests --with-coverage
$ pep8 --max-line-length=95 str2type

Release history Release notifications

This version
History Node

0.4.1

History Node

0.4.0

History Node

0.3.1

History Node

0.3.0

History Node

0.2.0

History Node

0.1.3

History Node

0.1.2

History Node

0.1.1

History Node

0.1.0

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
str2type-0.4.1.tar.gz (7.2 kB) Copy SHA256 hash SHA256 Source None Apr 28, 2015

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page