pycocoa.pytypes

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

# Module pytypes

Conversions from `NS...` ObjC instances to Python.

Version: 21.11.04

Functions
bool2NS(py) Create an `NSBoolean` instance from a Python `bool`. |
bytes2NS(py) Create an `NSData` instance from Python `bytes`. |
dict2NS(py, frozen=False) Create an `NS[Mutable]Dictionary` instance from a Python `dict`. |
float2NS(py) Create an `NSNumber` instance from a Python `float` or `int`. |
frozenset2NS(py) Create an (immutable) `NSSet` instance from a Python `frozenset`. |
generator2NS(py) Create an `NSArray` instance from a Python `generator`. |
int2NS(py) Create an `NSNumber` instance from a Python `int` or `long`. |
list2NS(py) Create an `NSMutableArray` instance from a Python `list`. |
map2NS(py) Create an `NSArray` instance from a Python `map`. |
None2NS(py) Return the `NSNull` singleton for Python's `None`. |
range2NS(py) Create an `NSArray` instance from a Python `range`. |
set2NS(py) Create an `NSMutableSet` instance from a Python `set`. |
str2NS(py) Create an `NSStr` instance 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`. |
unicode2NS(py) Create an `NSStr` instance from a Python `unicode` string. |
url2NS(py, url2=None) Create an `NSURL` instance from a Python string. |
py2NS(py) Convert (an instance of) a Python object into an instance of the equivalent `NS...` ObjC class: |
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` instance from a Python `bool`. Parameters: `py` - The value (`int` or `bool`). Returns: The `NSMain.BooleanYES` or `.BooleanNO` singleton (`NSBoolean`).

### bytes2NS(py) |
Create an `NSData` instance from Python `bytes`. Parameters: `py` - The value (`bytes`). Returns: The ObjC instance (`NSData`). Raises: `RuntimeError` - If `len` vs `count` assertion failed.

### dict2NS(py, frozen=False) |
Create an `NS[Mutable]Dictionary` instance from a Python `dict`. Parameters: `py` - The value (`dict`). `frozen` - Immutable (`bool`), mutable otherwise. Returns: The ObjC instance (`NSDictionary` or `NSMutableDictionary`). Raises: `RuntimeError` - If `len` vs `count` assertion failed.

### float2NS(py) |
Create an `NSNumber` 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`.

### frozenset2NS(py) |
Create an (immutable) `NSSet` instance from a Python `frozenset`. Parameters: `py` - The value (`frozenset`). Returns: The ObjC instance (`NSSet`). Raises: `RuntimeError` - If `len` vs `count` assertion failed.

### generator2NS(py) |
Create an `NSArray` instance from a Python `generator`. Parameters: `py` - The value (`generator`). Returns: The ObjC instance (`NSArray`). Raises: `RuntimeError` - If `len` vs `count` assertion failed.

### int2NS(py) |
Create an `NSNumber` instance from a Python `int` or `long`. Parameters: `py` - The value (`int` or `long`). Returns: The ObjC instance (`NSInt`, `NSLong`, or `NSLongLong`). Raises: `TypeError` - If `py` not an `int` or `long`.

### list2NS(py) |
Create an `NSMutableArray` instance from a Python `list`. Parameters: `py` - The value (`list`). Returns: The ObjC instance (`NSMutableArray`). Raises: `RuntimeError` - If `len` vs `count` assertion failed.

### map2NS(py) |
Create an `NSArray` instance from a Python `map`. Parameters: `py` - The value (`map`). Returns: The ObjC instance (`NSArray`). Raises: `RuntimeError` - If `len` vs `count` assertion failed.

### 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`.

### range2NS(py) |
Create an `NSArray` instance from a Python `range`. Parameters: `py` - The value (`range`). Returns: The ObjC instance (`NSArray`). Raises: `RuntimeError` - If `len` vs `count` assertion failed.

### set2NS(py) |
Create an `NSMutableSet` instance from a Python `set`. Parameters: `py` - The value (`set`). Returns: The ObjC instance (`NSMutableSet`). Raises: `RuntimeError` - If `len` vs `count` assertion failed.

### str2NS(py) |
Create an `NSStr` instance from a Python `str`. Parameters: `py` - The value (`str`). Returns: The ObjC instance (`NSStr`).

### 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`. Parameters: `py` - The value (`tuple`). Returns: The ObjC instance (`NSArray`). Raises: `RuntimeError` - If `len` vs `count` assertion failed.

### 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`.

### 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 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

### 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.