Formatter

This example shows how to implement an NSFormatter subclass in Python.

Sources

main.py

from AppKit import NSFormatter, NSString
from PyObjCTools import AppHelper

try:
    unicode
except NameError:
    unicode = str

class MyFormatter(NSFormatter):
    def stringForObjectValue_(self, product):
        if isinstance(product, (str, unicode)):
            return product
        return str(product)

    def getObjectValue_forString_errorDescription_(self, value, upc, error):
        print(self, upc)

        if not upc:
            print("No data")
            return True, None, None

        print("Have data")
        return False, None, NSString.stringWithString_("Foo the %s"%("bar",))

AppHelper.runEventLoop()

setup.py

"""
Script for building the example.

Usage:
    python3 setup.py py2app
"""
from setuptools import setup

setup(
    name="Formatter",
    app=["main.py"],
    data_files=["MainMenu.nib"],
    setup_requires=[
        "py2app",
        "pyobjc-framework-Cocoa",
    ]
)

Resources