Package pycocoa :: Module pytypes
[frames] | no frames]

Module pytypes

Conversions from NS... ObjC instances to Python.


Version: 25.04.07

Functions
 
bool2NS(py)
Create an NSBoolean from a Python bool.
 
bytes2NS(py)
Create an NSData from Python bytes.
 
decimal2NS(py)
Create an NSDecimal from a Python Decimal.
 
dict2NS(py)
Create an NSMutableDictionary from a Python dict or frozendict.
 
dicts2NS(py)
Create an NS[Mutable]Dictionary from a Python dict or frozendict.
 
float2NS(py)
Create an NSDouble instance from a Python float or int.
 
frozendict2NS(py)
Create an NSDictionary from a Python frozendict or dict.
 
frozenset2NS(py)
Create an NSSet from a Python frozenset or set.
 
generator2NS(py, frozen=True)
Create an NS[Mutable]Array from a Python generator.
 
int2NS(py, NS=None)
Create an NSNumber from a Python int or long.
 
iterable2NS(py, frozen=True)
Create an NS[Mutable]Array from a Python iterable.
 
list2NS(py)
Create an NSMutableArray from a Python list or tuple.
 
listuple2NS(py)
Create an NS[Mutable]Array from a Python list or tuple.
 
map2NS(py, frozen=True)
Create an NS[Mutable]Array from a Python map.
 
None2NS(py)
Return the NSNull singleton for Python's None.
 
py2NS(py)
Convert (an instance of) a Python object into an instance of the equivalent NS... ObjC class:
 
range2NS(py, frozen=True)
Create an NS[Mutable]Array from a Python range.
 
set2NS(py)
Create an NSMutableSet from a Python set or frozenset.
 
sets2NS(py)
Create an NS[Mutable]Set instance from a Python set or frozenset.
 
str2NS(py)
Create an NSStr instance from a Python str.
 
strs2NS(py, frozen=True)
Create an NS[Mutable]String from a Python str.
 
time2NS(py=None, since=1970)
Create an NSDate instance from a timestamp.
 
tuple2NS(py)
Create an immutable NSArray instance from a Python tuple or list.
 
unicode2NS(py)
Create an NSStr instance from a Python unicode string.
 
url2NS(py, url2=None)
Create an NSURL instance from a Python string.
 
type2NS(py)
Create the NS... ObjC object for a Python Type (or wrapper) instance.
Variables
  __all__ = _ALL_LAZY.pytypes
Function Details

bool2NS(py)

 

Create an NSBoolean from a Python bool.

Parameters:
  • py - The value (int or bool).
Returns:
The NSMain.BooleanYES or .BooleanNO singleton (NSBoolean).

bytes2NS(py)

 

Create an NSData from Python bytes.

Parameters:
  • py - The value (bytes).
Returns:
The ObjC instance (NSData).
Raises:
  • TypeError - If py not bytes or str.
  • RuntimeError - If len vs count assertion failed.

decimal2NS(py)

 

Create an NSDecimal from a Python Decimal.

Parameters:
  • py - The value (Decimal, float, int or str).
Returns:
The value (NSDecimal).
Raises:
  • TypeError - If py not a Decimal, float, int or str.
  • ValueError - Invalid decimal py.

dict2NS(py)

 

Create an NSMutableDictionary from a Python dict or frozendict.

Returns:
The ObjC instance (NSMutableDictionary).

See Also: Function dicts2NS for further details.

dicts2NS(py)

 

Create an NS[Mutable]Dictionary from a Python dict or frozendict.

Parameters:
  • py - The value (dict or frozendict).
Returns:
The ObjC instance (NSMutableDictionary or immutable NSDictionary).
Raises:
  • RuntimeError - If len vs count assertion failed.
  • TypeError - If py is not a dict or frozendict.

See Also: Functions dict2NS and frozendict2NS.

float2NS(py)

 

Create an NSDouble instance from a Python float or int.

Parameters:
  • py - The value (float or int).
Returns:
The ObjC instance (NSDouble).
Raises:
  • TypeError - If py not a float or int.

frozendict2NS(py)

 

Create an NSDictionary from a Python frozendict or dict.

Returns:
The ObjC instance (NSDictionary, immutable).

See Also: Function dicts2NS for further details.

frozenset2NS(py)

 

Create an NSSet from a Python frozenset or set.

Returns:
The ObjC instance (NSSet, immutable).

See Also: Function sets2NS for further details.

generator2NS(py, frozen=True)

 

Create an NS[Mutable]Array from a Python generator.

Parameters:
  • py - The value (generator).
  • frozen - If True, return an immutable NSArray, otherwise an NSMutableArray (bool).
Returns:
The ObjC instance (NSMutableArray or immutable NSArray).
Raises:
  • TypeError - If py not a generator.

int2NS(py, NS=None)

 

Create an NSNumber from a Python int or long.

Parameters:
  • py - The value (int or long).
  • NS - Alternate NSNumber ObjC class to use when py exceeds the NSLong or NSLongLong range, for example NS=NSDecimal.
Returns:
The ObjC instance (NSNumber or NS alternate).
Raises:
  • TypeError - If py not an int or long.
  • ValueError - If py exceeds NSLong range and if no NS alternate is specified.

iterable2NS(py, frozen=True)

 

Create an NS[Mutable]Array from a Python iterable.

Parameters:
  • py - The value (iterable).
  • frozen - If True, return an immutable NSArray, otherwise an NSMutableArray (bool).
Returns:
The ObjC instance (NSMutableArray or immutable NSArray).
Raises:
  • TypeError - If py is not iterable.

list2NS(py)

 

Create an NSMutableArray from a Python list or tuple.

Returns:
The ObjC instance (NSMutableArray).

See Also: Function listuple2NS for further details.

listuple2NS(py)

 

Create an NS[Mutable]Array from a Python list or tuple.

Parameters:
  • py - The value (list or tuple).
Returns:
The ObjC instance (NSMutableArray or immutable NSArray).
Raises:
  • RuntimeError - If len vs count assertion failed.

See Also: Functions list2NS and tuple2NS.

map2NS(py, frozen=True)

 

Create an NS[Mutable]Array from a Python map.

Parameters:
  • py - The value (map).
  • frozen - If True, return an immutable NSArray, otherwise an NSMutableArray (bool).
Returns:
The ObjC instance (NSArray or NSMutableArray).

None2NS(py)

 

Return the NSNull singleton for Python's None.

Parameters:
  • py - The value (None).
Returns:
The singleton (NSNull).
Raises:
  • ValueError - If py is not None.

py2NS(py)

 

Convert (an instance of) a Python object into an instance of the equivalent NS... ObjC class:

Parameters:
  • py - The value (Python type).
Returns:
The ObjC instance (ObjCInstance).
Raises:
  • TypeError - Unhandled, unexpected Python type.

Note: Conversion map:

  • bool -> NSBoolean/NSNumber
  • bytes -> NSData (Python 3+ only)
  • bytearray -> NSData
  • Decimal -> NSDecimal
  • dict -> NSMutableDictionary
  • float -> NSNumber
  • frozendict -> NSDictionary, immutable
  • frozenset -> NSSet, immutable
  • generator -> NSArray, immutable (like tuple)
  • int -> NSNumber
  • list -> NSMutableArray
  • map -> NSArray, immutable (like tuple, Python 3+ only)
  • None -> NSNull
  • range -> NSArray, immutable (like tuple)
  • set -> NSMutableSet
  • str -> NSStr, immutable
  • tuple -> NSArray, immutable
  • unicode -> NSStr, immutable

See Also: Converting values between Python and Objective-C

range2NS(py, frozen=True)

 

Create an NS[Mutable]Array from a Python range.

Parameters:
  • py - The value (range).
  • frozen - If True, return an immutable NSArray, otherwise an NSMutableArray (bool).
Returns:
The ObjC instance (NSMutableArray or immutable NSArray).

set2NS(py)

 

Create an NSMutableSet from a Python set or frozenset.

Returns:
The ObjC instance (NSMutableSet).

See Also: Function sets2NS for further details.

sets2NS(py)

 

Create an NS[Mutable]Set instance from a Python set or frozenset.

Parameters:
  • py - The value (set or frozenset).
Returns:
The ObjC instance (NSMutableSet or immutable NSSet).
Raises:
  • RuntimeError - If len vs count assertion failed.
  • TypeError - If py is not set nor frozenset.

str2NS(py)

 

Create an NSStr instance from a Python str.

Parameters:
  • py - The value (str).
Returns:
The ObjC instance (NSStr).

See Also: Function strs2NS.

strs2NS(py, frozen=True)

 

Create an NS[Mutable]String from a Python str.

Parameters:
  • py - The value (str).
  • frozen - If True, return an immutable NSString, otherwise an NSMutableString (bool).
Returns:
The ObjC instance (NSMutableString or immutable NSString).

See Also: Function str2NS.

time2NS(py=None, since=1970)

 

Create an NSDate instance from a timestamp.

Parameters:
  • py - The timestamp in seconds (float, int).
  • since - Epoch start (1970, 2001) otherwise now.
Returns:
The ObjC instance (NSDate).

Note: Using py=None means py=0, since=None or py=time.time(), since=1970.

tuple2NS(py)

 

Create an immutable NSArray instance from a Python tuple or list.

Returns:
The ObjC instance (NSArray, immutable).

See Also: Function listuple2NS for further details.

unicode2NS(py)

 

Create an NSStr instance from a Python unicode string.

Parameters:
  • py - The value (unicode).
Returns:
The ObjC instance (NSStr).

url2NS(py, url2=None)

 

Create an NSURL instance from a Python string.

Parameters:
  • py - The URL (str or unicode).
  • url2 - Optionally, relative to this URL (str or unicode).
Returns:
The ObjC instance (NSURL).

See Also: URL for parsing an NSURL.

type2NS(py)

 

Create the NS... ObjC object for a Python Type (or wrapper) instance.

Parameters:
  • py - The value (Type).
Returns:
The ObjC instance (ObjCInstance).
Raises:
  • TypeError - Unhandled, unexpected Type.

See Also: Function py2NS.