Native integer and floating-point type emulation
Project description
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 |
|||||
---|---|---|---|---|---|
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
Built Distribution
Hashes for nativetypes-1.0.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cac295d3d61d7cbcf9ce83f27770bcbe38cab427eeeb0ca989018d76be9c70da |
|
MD5 | d06322aa0a5f8a081cd754c7aa3cb48b |
|
BLAKE2b-256 | d96e30560fcd71d6d3b95d003d79e610441e5a91beb8a53250f89084a84ffd2b |