MCPcopy Create free account
hub / github.com/pydata/numexpr / CastFactory

Function CastFactory

code_generators/interp_generator.py:848–927  ·  view source on GitHub ↗

Builds all the possible cast operations. Currently all casts are based on the numpy framework. I'm unaware of any potential competiting standards for Python. The main issue would seem to be if we want to support anything other than 'safe' casting? Cast styles are 'no', '

(opsList: List[Operation], casting: str='safe')

Source from the content-addressed store, hash-verified

846 return ''.join( [str(self.py_Name),'_',str(self.libs)])
847
848def CastFactory(opsList: List[Operation], casting: str='safe') -> None:
849 """
850 Builds all the possible cast operations. Currently all casts are based on
851 the numpy framework. I'm unaware of any potential competiting standards
852 for Python.
853
854 The main issue would seem to be if we want to support anything other than
855 'safe' casting? Cast styles are 'no', 'equiv', 'safe', 'same_kind', and
856 'unsafe'.
857
858 Numpy dtype.chars are used for tuple lookup, as it's faster than parsing
859 through .descr:
860 bool = '?'
861 uint8 = 'B'
862 int8 = 'b'
863 uint16 = 'H'
864 int16 = 'h'
865 uint32 = 'I'
866 int32 = 'i'
867 uint64 = 'L'
868 int64 = 'l'
869 float32 = 'f'
870 float64 = 'd'
871 longfloat = 'g'
872 complex64 = 'F'
873 complex128 = 'D'
874 bytes = 'S'
875 str/unicode = 'U'
876
877 NOTE: Windows is different for ints.
878
879 Note
880 ----
881 ``longfloat`` and ``clongfloat`` are not implemented at present, as they
882 do not have C-equivalents.
883 """
884 global OP_COUNT
885
886 for dChar in ALL_FAM:
887 for castChar in ALL_FAM:
888 if dChar == castChar:
889 continue
890
891
892 # TEMPORARY: remove strings
893 if 'S' in [dChar,castChar] or 'U' in [dChar,castChar]:
894 continue
895
896 if not np.can_cast( dChar, castChar, casting=casting ):
897 if dChar == 'F' or dChar == 'D' or castChar == 'F' or castChar == 'D':
898 # Unsafe casts for complex aren't provided, users should use the complex(a,b) functions
899 continue
900 # Unsafe casts
901 opsList += [Operation( 'unsafe_cast',
902 '$DEST = ($DTYPE0)($ARG1)',
903 CAST_SAFE, castChar, [dChar], aliases=np.dtype(castChar).name )]
904
905 # NumPy doesn't provide casts for real types to complex types so

Callers 1

generateFunction · 0.85

Calls 1

OperationClass · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…