Skip to main content

Старается конвертировать один тип данных в другой.

Project description

adaptyper

Старается конвертировать один тип данных в другой.

Обработка пользовательских данных из не строго типизированных значений.

from adaptyper import convert, ValueType

# to
convert('FaLsE').to(ValueType.BOOL)  # False
convert('any_str_value').to('bool')  # True
convert(0).to('bool')  # False
convert(1.0).to('bool')  # True

convert(True).to(ValueType.STR)  # "TRUE"
convert(-1).to('str')  # "-1"

convert(True).to(ValueType.FLOAT)  # 1.0
convert('FaLsE').to('float')  # 0.0
convert('.5').to('float')  # 0.5
convert(-1).to('float')  # -1.0

convert(True).to(ValueType.INT)  # 1
convert('1.5').to('int')  # 1
convert('1.5').to('int', True)  # 2

convert('2011-11-04').to(ValueType.DATETIME)  # datetime(2011, 11, 4, 0, 0)
convert('4.11.2011').to('datetime', '%d.%m.%Y')  # datetime(2011, 11, 4, 0, 0)

convert('2011-11-04').to(ValueType.DATE)  # date(2011, 11, 4)
convert('4.11.2011').to('date', '%d.%m.%Y')  # date(2011, 11, 4)

convert('.00').to(ValueType.DECIMAL)  # Decimal('0.00')
convert('.5').to('decimal')  # Decimal('0.5')
convert(-1).to('decimal')  # Decimal('-1')
convert("- 123 \xa0 456").to('decimal')  # Decimal('-123456')

# bool
convert('').to_bool()  # False
convert('0').to_bool()  # False
convert('FaLsE').to_bool()  # False
convert('any_str_value').to_bool()  # True

# str
convert(True).to_str()  # "TRUE"
convert('any_str_value').to_str()  # "any_str_value"
convert(1).to_str()  # "1"
convert(-1).to_str()  # "-1"
convert(123.456).to_str()  # "123.456"
convert(-123.456).to_str()  # "-123.456"

# float
convert(None).to_float()  # None
convert('').to_float()  # None
convert('123,456').to_float()  # 123.456
convert(' 123\xa0456\xa0').to_float()  # 123456.0
convert('tRuE').to_float()  # 1.0
convert(True).to_float()  # 1.0
convert(1).to_float()  # 1.0
convert('.3').to_float()  # 0.3

# int
# работает через float,
# банковское округление (по-умолчанию False)
convert('.6').to_int()  # 0
convert('.6').to_int(bankers_rounding=True)  # 1
convert('1.5').to_int(False)  # 1
convert('1.5').to_int(True)  # 2

# datetime
convert(None).to_datetime()  # None
convert('').to_datetime()  # None
convert('2011-11-04').to_datetime()  # datetime(2011, 11, 4, 0, 0)
convert('4.11.2011').to_datetime('%d.%m.%Y')  # datetime(2011, 11, 4, 0, 0)

# date
# работает через datetime
convert(None).to_date()  # None
convert('').to_date()  # None
convert('2011-11-04').to_date()  # date(2011, 11, 4)
convert('4.11.2011').to_date('%d.%m.%Y')  # date(2011, 11, 4)

# decimal
convert('.00').to(ValueType.DECIMAL)  # Decimal('0.00')
convert('.5').to('decimal')  # Decimal('0.5')
convert(-1).to('decimal')  # Decimal('-1')
convert(123456).to('decimal')  # Decimal('123456')
convert("- 123 456").to('decimal')  # Decimal('-123456')
convert("123\xa0456").to('decimal')  # Decimal('123456')

PyPI Downloads PyPI Downloads PyPI Downloads

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

adaptyper-0.4.2.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

adaptyper-0.4.2-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file adaptyper-0.4.2.tar.gz.

File metadata

  • Download URL: adaptyper-0.4.2.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.12.7 Windows/10

File hashes

Hashes for adaptyper-0.4.2.tar.gz
Algorithm Hash digest
SHA256 33b60343fc39d44104e67c48f1d23c208beeb6ce52f618e74c577aa96da96174
MD5 20473acb35e441af67fb2e6143378557
BLAKE2b-256 f3809dc6b568b77f9f837b03cd341f7cee196e56e7f121bcdfc012fc173cb84a

See more details on using hashes here.

File details

Details for the file adaptyper-0.4.2-py3-none-any.whl.

File metadata

  • Download URL: adaptyper-0.4.2-py3-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.12.7 Windows/10

File hashes

Hashes for adaptyper-0.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4ea901de9a12dd9548828b0c8499fb8eae0e43b9b801bc1268c168cb30118464
MD5 df16c7e08e217d92ae1f0cd277ea54e4
BLAKE2b-256 ad85d123cc6f6f2ffd766b91f38a2e683e6a603ad4632e4ba00d5bf10c3f4e4b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page