Skip to main content

An enhancement of str.isnumeric()

This project has been archived.

The maintainers of this project have marked this project as archived. No new releases are expected.

Project description

numerical

Python library I made to learn regex and to have a better version of str.isnumeric()

PyPI rejected the name without telling me why (too similar to numerics?), so its called better-is-numeric there.

Supports decimals and negative numbers, managed by flags.

Even if you don't need to support decimals or negative numbers, this is still better than str.isnumeric(), since that function for some reason counts exponents (⅐, for example) as numeric.

Should work down to Python 3.9.

Importing is kind of ugly, maybe I'll fix it one day. In the meantime, you can avoid this and the PyPI name by just downloading the code from the Codeberg repository and putting it in the same directory as your program, for a simple from numerical import *

Here's some example code:

from is_numeric.numerical import *

print(is_numeric("1", {NM_ALLOW_NEGATIVE, NM_ALLOW_DECIMALS, NM_ALLOW_LEADING_ZERO}))

This will print True, since the NM_ALLOW flags do not require, they only allow. If you want to REQUIRE a negative number or decimals, simply check:

if "-" in whatever_string_you_are_checking

if "." in whatever_string_you_are_checking

I've copied this from the docstring(?) of the function, since it describes the flags:

This function uses a "flag" system to control what's allowed and what isn't.
You can pass these in a set called "flags" in the arguments.
Certain flags switch the function to dictionary output, to include whatever data you requested.
The simple boolean output is still included in the dictionary output, in the "numeric" field.
The flags are in variables, but you can also use their string values.
1: NM_ALLOW_NEGATIVE - Set to the string "AN" and enabled by default, this flag allows negative numbers.
2: NM_ALLOW_DECIMALS - Set to the string "AD", this flag allows numbers with decimals.
3: NM_ALLOW_LEADING_ZERO - Set to the string "AZ", this flag allows "invalid" numbers like 01.
4: NM_ALLOW_PLUS_SIGN - Set to the string "AP", this flag allows explicitly positive numbers, such as +2.
5: NM_RETURN_MATCH - Set to the string "RM", this flag uses dictionary output and returns the raw output of the match function inside the "match" field.
6: NM_RETURN_REGEX - Set to the string "RX", this flag uses dictionary output and returns the constructed regex inside the "regex" field.

If you want no flags, try passing set() (a blank set), or [] (an empty list)

The latter works because the function converts your list to a set. The former can't just be {}, because Python will see that as an empty dictionary.

If you want the default flags, don't pass your own set.

That is, JUST give the string. is_numeric(string)

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

better_is_numeric-1.2.1.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

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

better_is_numeric-1.2.1-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file better_is_numeric-1.2.1.tar.gz.

File metadata

  • Download URL: better_is_numeric-1.2.1.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.4

File hashes

Hashes for better_is_numeric-1.2.1.tar.gz
Algorithm Hash digest
SHA256 97af15f2e35b5a70765fba0c4fd95736adabee9b5e23ff9305db025e8e9b7061
MD5 2b442eb82653d70b902572168dd0473a
BLAKE2b-256 0b8f33aa90d82b30b7b258686e3f3660d85c0eee8136f8d01b9ef13dfe29540e

See more details on using hashes here.

File details

Details for the file better_is_numeric-1.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for better_is_numeric-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7b5234df0acadab2646e0a6dbe81b6e27884ddb7b3c6783ed3dfbb360b990c22
MD5 5dcd1a8c13b67737ae429914f11283d8
BLAKE2b-256 dd5ef79119806da261722a17d1967464c29dfe22487c5dde6d638097adcdb9cb

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