Basic Types

Basic types in SNAP are TInt, TFlt, and TStr, which in Snap.py are automatically converted to Python types int, float, and str, respectively. In general, there is no need to explicitly work with SNAP types in Snap.py because of the automatic conversion.

TInt

class TInt
class TInt(val)
class TInt(SIn)

Returns a new TInt initialized with the value specified by optional parameter val. If no value is given, the TInt object is initialized with the default value 0. If SIn is provided, the value is loaded from the binary stream SIn. In Snap.py, TInt is automatically converted to Python type int.

Below is a list of functions supported by the TInt class:

Load(SIn)

Loads the int from a binary stream SIn.

Save(SOut)

Saves the int to a binary stream SOut.

Abs(val)

A static method that returns the absolute value of val, an int.

GetHexStr(val)

A static method that returns a string with the hexidecimal representation of int val.

GetInRng(val, min, max)

A static method that returns int val if it is between min and max. If val is smaller than min, it returns min. If val is larger than max, it returns max.

GetKiloStr(val)

A static method that returns the int val as a kilo-formatted string. If val is less than 1000, it returns val as a string. If val is greater than or equal to 1000, it returns a string in form of ‘x.yK’, where x is some digit from 1-9 and y from 0-9.

GetMegaStr(val)

A static method that returns the int val as a mega-formatted string. If val is less than 1000000, it returns the equivalent of GetKiloStr(val). If val is greater than or equal to 1000000, it returns a string in the form of ‘x.yM’, where x is some digit from 1-9 and y from 0-9.

GetMemUsed()

Returns the size in bytes.

GetMn(val1, val2)
GetMn(val1, val2, val3)
GetMn(val1, val2, val3, val4)

A static method that returns the minimum of the ints passed in as parameters.

GetMx(val1, val2)
GetMx(val1, val2, val3)
GetMx(val1, val2, val3, val4)

A static method that returns the maximum of the ints passed in as parameters.

GetPrimHashCd()

Returns the value stored in the int.

GetRnd(range=0)

A static method that returns a random int between 0 and range-1, inclusive. If a range value of 0 is specified, it returns a random int between 0 and INT_MAX. The default value of range is 0.

GetSecHashCd()

Returns the value stored in the int divided by 0x10.

IsEven(val)

A static method that returns a bool indicating whether val is even.

IsOdd(val)

A static method that returns a bool indicating whether val is odd.

Sign(val)

A static method that returns 1 if val > 0, -1 if val < 0, and 0 if val == 0.

A single public attribute is offered by the TInt class:

Val

A member of the TInt object of type int that gives the value the int holds.

A few static public attributes are offered by the TInt class:

Mn

The minimum value of an signed int, equivalent to INT_MIN in C++.

Mx

The maximum value of an signed int, equivalent to INT_MAX in C++.

Kilo

Equal to 1024.

Mega

Equal to 1024*1024.

Giga

Equal to 1024*1024*1024.

Rnd

The TRnd object used in methods such as GetRnd().

Below is some code demonstrating the use of the TInt type:

>>> i = snap.TInt(10)
>>> print(i.Val)
10
>>> i.Val = 21
>>> snap.TInt.IsEven(5)
False
>>> snap.TInt.GetMegaStr(1234567)
'1.2M'

TFlt

class TFlt
class TFlt(val)
class TFlt(SIn)

Returns a new TFlt initialized with the value specified by optional parameter val. If no value is given, the TFlt object is initialized with the default value 0. If SIn is provided, the value is loaded from the binary stream SIn. In Snap.py, TFlt is automatically converted to Python type float.

Below is a list of functions supported by the TFlt class:

Load(SIn)

Loads the float from a binary stream SIn.

Save(SOut)

Saves the float to a binary stream SOut.

Abs(val)

A static method that returns the absolute value of val, a float.

GetInRng(val, min, max)

A static method that returns float val if it is between min and max. If val is smaller than min, it returns min. If val is larger than max, it returns max.

GetKiloStr(val)

A static method that returns the float val as a kilo-formatted string. If val is less than 1000, it rounds val to the nearest int, and returns it as a string. If val is greater than or equal to 1000, it returns a string in form of ‘x.yK’, where x is some digit from 1-9 and y from 0-9.

GetMegaStr(val)

A static method that returns the float val as a mega-formatted string. If val is less than 1000000, it returns the equivalent of GetKiloStr(val). If val is greater than or equal to 1000000, it returns a string in the form of ‘x.yM’, where x is some digit from 1-9 and y from 0-9.

GetGigaStr(val)

A static method that returns the float val as a giga-formatted string. If val is less than 1000000000, it returns the equivalent of GetMegaStr(val). If val is greater than or equal to 1000000000, it returns a string in the form of ‘x.yG’, where x is some digit from 1-9 and y from 0-9.

GetMemUsed()

Returns the size in bytes.

GetMn(val1, val2)
GetMn(val1, val2, val3)
GetMn(val1, val2, val3, val4)

A static method that returns the minimum of the floats passed in as parameters.

GetMx(val1, val2)
GetMx(val1, val2, val3)
GetMx(val1, val2, val3, val4)

A static method that returns the maximum of the floats passed in as parameters.

GetPrimHashCd()

Returns the primary hash code for the float object.

GetRnd()

A static method that returns a random int between 0 and 1.

GetSecHashCd()

Returns the secondary hash code for the float object.

IsNum()
IsNum(val)

A method that returns a bool indicating whether val is a valid numner. If val is not provided, it returns a bool indicating whether this float is a valid number.

IsNaN()
IsNaN(val)

A static method that returns a bool indicating whether val is NaN, not a number. If val is not provided, it returns a bool indicating whether this float is NaN.

Sign(val)

A static method that returns 1 if val > 0, -1 if val < 0, and 0 if val == 0.

Round(val)

A static method that returns val rounded to the nearest int.

Eq6(val1, val2)

A static method that returns whether val1 and val2 are equal to 6 decimal places.

A single public attribute is offered by the TFlt class:

Val

A member of the TFlt object of type int that gives the value.

A few static public attributes are offered by the TFlt class:

Mn

The minimum value of a TFlt, equivalent to -DBL_MAX in C++.

Mx

The maximum value of a TFlt, equivalent to DBL_MAX in C++.

NInf

The value used to represent negative infinity, which is equivalent to Mn.

PInf

The value used to represent positive infinity, which is equivalent to Mx.

Eps

The epsilon value for the TFlt, equal to 1e-16.

EpsHalf

Equal to 1e-7.

Rnd

The TRnd object used in methods such as GetRnd().

Below is some code demonstrating the use of the TFlt type:

>>> f = snap.TFlt(9.874)
>>> print(f.Val)
9.874
>>> f.Val = 2.1
>>> f.IsNum()
True
>>> snap.TFlt.Round(1.234567)
1

TStr

class TStr
class TStr(str)
class TStr(SIn)

Returns a new TStr initialized with the value specified by optional parameter str. If no value is given, the TStr object is initialized with the empty string. If SIn is provided, the value is loaded from the binary stream SIn. In Snap.py, TStr is automatically converted to Python type str.

Below is a list of functions supported by the TStr class:

Load(SIn)

Loads the string from a binary stream SIn.

Save(SOut)

Saves the string to a binary stream SOut.

ChangeCh(orig, repl, start)

Looks for the first instance of the character orig starting at index start and replaces it with the character repl. Returns the index of the character replaced.

ChangeChAll(orig, repl, start)

Looks for the all instances of the character orig starting at index start and replaces them with the character repl. Returns the number of character replaced.

ChangeStr(orig, repl, start)

Looks for the first instance of the string orig starting at index start and replaces it with the string repl. Returns the starting index of the string replaced.

ChangeStrAll(orig, repl, start)

Looks for the all instances of the string orig starting at index start and replaces them with the string repl. Returns the number of replacements done.

Clr()

Sets the string to the empty string.

CmpI(str)

Compares the string to the parameter str, of type TStr, character by character. Returns a positive number if the string is greater than str and vice versa.

CountCh(ch, start=0)

Returns the number of times ch appears in the string, starting at position start.

CStr()

Returns the string as a c-string, which is converted to a python str.

DelChAll(ch)

Deletes all instances of the char ch from the string.

DelStr(str)

Deletes the first instance of str found in the string. Returns a bool indicating whether anything was deleted.

DelSubStr(start, end)

Deletes the substring starting at position start and ending at position end from the string.

Empty()

Returns a bool indicating whether the string is empty.

Eql(str)

Returns a bool indicating whether the string is equal to the TStr str.

FromHex()

Converts the string from hex to the original string and returns the resulting value.

GetCap()

Capitalizes the first letter of the contents of the string and returns the resulting Python str.

GetCh(ChN)

Returns the character at position ChN.

GetFlt()

Returns the contents of the string converted to a float.

GetFromHex()

Returns the string converted from hex as a Python str. The contents of the original string are left unchanged.

GetHex()

Returns the string converted to hex as a Python str. The contents of the original string are left unchanged.

GetHexInt()

Returns the contents of the string converted to an int, which is in decimal, not hexadecimal format.

GetHexInt64()

Returns the contents of the string converted to a 64-bit int, which is in decimal, not hexadecimal format.

GetInt()

Returns the contents of the string converted to an int.

GetInt64()

Returns the contents of the string converted to a 64-bit int.

GetLc()

Returns a Python str with the contents of the string converted to lowercase. The contents of the original string are left unchanged.

GetMemUsed()

Returns the size in bytes.

GetPrimHashCd()

Returns the primary hash code for the string.

GetSecHashCd()

Returns the secondary hash code for the string.

GetSubStr(start)
GetSubStr(start, end)

Returns a substring starting at position start and ending at position end, inclusive. If end is not specified, the end position is assumed to be the last character in the string.

GetTrunc()

Returns a Python str with all the whitespace removed from the end of the contents of the string.

GetUc()

Returns a Python str with the contents of the string converted to uppercase. The contents of the original string are left unchanged.

GetUInt()

Returns the contents of the string converted to an unsigned int.

GetUInt64()

Returns the contents of the string converted to an unsigned 64-bit int.

InsStr(pos, str)

Inserts the contents of str (either a Python str or a TStr) into the string at position pos.

IsChIn(ch)

Returns a bool indicating whether the character ch is in the string.

IsFlt()

Returns a bool indicating whether the contents of string is a valid float.

IsHexInt()

Returns a bool indicating whether the string is a valid hexadecimal int.

IsHexInt64()

Returns a bool indicating whether the string is a valid 64-bit hexadecimal int.

IsInt()

Returns a bool indicating whether the string is an int.

IsInt64()

Returns a bool indicating whether teh string is a 64-bit int.

IsLc()

Returns a bool indicating whether the string is lowercase.

IsPrefix(prefix)

Returns a bool indicating whether prefix is a prefix of the string.

IsSuffix(suffix)

Returns a bool indicating whether suffix is a suffix of the string.

IsUc()

Returns a bool indicating whether the string is uppercase.

IsUInt()

Returns a bool indicating whether the string is an unsigned int.

IsUInt64()

Returns a bool indicating whether the string is an unsigned 64-bit int.

IsWord()

Returns a bool indicating whether the contents of the string is a single word, which is defined as a collection of letters and digits, starting with a letter.

IsWs()

Returns a bool indicating whether the content of the string is just whitespace.

LastCh()

Returns the last character in the string.

Left(start)

Returns the substring starting at position 0 to start-1.

LeftOf(ch)

Returns the substring left of the first instance of char ch in the string.

LeftOfLast(ch)

Returns the substring left of the last instance of char ch in the string.

Len()

Returns the length of the string.

Mid(start)
Mid(start, numChars)

Returns the Python str starting at position start containing at most numChars characters. If numChars is not specified, it returns the substring starting at position start to the end of the string.

PutCh(pos, ch)

Replaces the character at position pos with character ch.

Reverse()

Returns a Python str with the string reversed.

Right(start)

Returns the substring starting at position start to the end of the string.

RightOf(ch)

Returns the substring right of the first instance of char ch in the string.

RightOfLast(ch)

Returns the substring right of the last instance of char ch in the string.

SearchCh(ch, start=0)

Searches the string for the character ch starting at position start and returns the index at which ch was found or -1 if it was not found.

SearchChBack(ch, start=-1)

Searches the string for the character ch starting at position start and going backward. Returns the index at which the character was found or -1. A start value of -1 indicates that the method should start searching at the end of the string.

SearchStr(str, start=0)

Searches the string for the substring str starting at position start and returns the index at which str was found or -1 if it was not found.

Slice(start, numChars)

Returns a substring of the string starting at position start containing numChars characters.

ToCap()

Returns a Python str with the first letter of the contents of the string capitalized.

ToHex()

Converts the string to hex and returns the resulting value.

ToLc()

Coverts the contents of the string to lowercase and returns the resulting string.

ToTrunc()

Removes the trailing whitespace from the contents of the string and returns the resulting Python str.

ToUc()

Coverts the contents of the string to uppercase and returns the resulting string.

Below is some code demonstrating the use of the TStr type:

>>> s = snap.TStr('Welcome to Snap.py!')
>>> print(s.CStr())
'Welcome to Snap.py!'
>>> s.GetSubStr(0,6)
'Welcome'

Note

Do not use an empty string literal “” in Python, if a Snap.py function parameter is of type TStr. SNAP handling of TStr(“”) is not compatible with Python, so an empty string literal will cause an error.