Skip to main content

Native integer and floating-point type emulation

Project description

https://api.travis-ci.org/AlexAltea/ntypes.svg?branch=master https://coveralls.io/repos/github/AlexAltea/ntypes/badge.svg?branch=master https://img.shields.io/pypi/v/ntypes.svg

Emulate native integer and floating-point types in Python 2.x and 3.x.

More information at: https://github.com/AlexAltea/ntypes

Install the package via:

pip install nativetypes

Comparison

There are several alternatives to ntypes, specifically: ctypes, numpy, fixedint, cinc. However, ntypes also offers some features not present across all these packages.

ntypes

*ctypes*

*numpy*

*fixedint*

*cinc*

Floating-point

Yes

Yes

Yes

Implicit casts

Yes

Yes

Custom aliases

Yes

Slicing

Yes

Yes

High-performance

Yes

Yes

Other reasons might include that numpy is way too large dependency to be imported just for the sake of fixed-size integers. Note that high-performance is not a goal for this library.

FAQ

What’s the point of this library?

This library is syntactic sugar for developers and reverse-engineers that want to port code from C/C++/ASM into Python and need all this low-level quirks: Overflows, underflows, casts, etc.

Although Python prevents many headaches with its arbitrarily large integers, writing code equivalent to functions written in C/C++/ASM means masking every operation, and doing dozens of conversions manually between distinct types. This library does that work for you.

Where can read about the API?

The documentation is quite incomplete at this moment. Check the examples below or check the tests.

Why is this package nativetypes?

I’m not good with naming. Ideally, I would have registered ntypes instead, but that one was apparently taken.

Examples

  • Fast inverse square root (see [1])

def rsqrt(number: float32):
    i = reinterpret_cast(int32, number)
    i = 0x5F3759DF - (i >> 1)
    y = reinterpret_cast(float32, i)
    y *= (1.5 - (0.5 * number * y * y))
    return y
  • Ranbyus DGA (see [2]):

    def ranbyus_dga(timestamp):
    s = uint32(self.seed)
    t1 = uint32(t.day)
    t2 = uint32(t.month)
    t3 = uint32(t.year)
    
    name = ""
    for i in xrange(12):
        t1 = (t1 >> 15) ^ (16 * (t1 & 0x1FFF ^ 4 * (t1 ^ s)))
        t2 = ((t2 ^ (4 * t2)) >>  8) ^ ((t2 & 0xFFFFFFFE) * 14)
        t3 = ((t3 ^ (7 * t3)) >> 11) | ((t3 & 0xFFFFFFF0) << 17)
        s = (s >> 6) ^ (((t1 + 8 * s) << 8) & 0x3FFFF00)
        name += string.ascii_lowercase[int(t1 ^ t2 ^ t3) % 25]
    
    # TLD omitted
    return name

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

nativetypes-1.0.1-py2.py3-none-any.whl (9.6 kB view hashes)

Uploaded Python 2 Python 3

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