# Groups

See also

Notifications:

  The Groups object uses notifications to notify observers of changes.

## Tasks

### Notifications

- `dispatcher`

- `addObserver()`

- `removeObserver()`

- `hasObserver()`

### Parent

- `getParent()`

- `setParent()`

#### Groups

class defcon.Groups(font=None)

  This object contains all of the groups in a font.

  This object posts the following notifications:

  - Groups.Changed

  - Groups.BeginUndo

  - Groups.EndUndo

  - Groups.BeginRedo

  - Groups.EndRedo

  - Groups.GroupSet

  - Groups.GroupDeleted

  - Groups.Cleared

  - Groups.Updated

  This object behaves like a dict. The keys are group names and the values are lists of glyph names:

      {
          "myGroup" : ["a", "b"],
          "myOtherGroup" : ["a.alt", "g.alt"],
      }

  The API for interacting with the data is the same as a standard dict. For example, to get a list of all group names:

      groupNames = groups.keys()

  To get all groups including the glyph lists:

      for groupName, glyphList in groups.items():

  To get the glyph list for a particular group name:

      glyphList = groups["myGroup"]

  To set the glyph list for a particular group name:

      groups["myGroup"] = ["x", "y", "z"]

  And so on.

  Note: You should not modify the group list and expect the object to know about it. For example, this could cause your changes to be lost:

      glyphList = groups["myGroups"]
      glyphList.append("n")

  To make sure the change is noticed, reset the list into the object:

      glyphList = groups["myGroups"]
      glyphList.append("n")
      groups["myGroups"] = glyphList

  This may change in the future.

  addObserver(observer, methodName, notification, identifier=None)

    Add an observer to this object’s notification dispatcher.

    - observer An object that can be referenced with weakref.

    - methodName A string representing the method to be called when the notification is posted.

    - notification The notification that the observer should be notified of.

    - identifier None or a string identifying the observation. There is no requirement that the string be unique. A reverse domain naming scheme is recommended, but there are no requirements for the structure of the string.

    The method that will be called as a result of the action must accept a single notification argument. This will be a `defcon.tools.notifications.Notification` object.

    This is a convenience method that does the same thing as:

        dispatcher = anObject.dispatcher
        dispatcher.addObserver(observer=observer, methodName=methodName,
            notification=notification, observable=anObject, identifier=identifier)

  canRedo()

    Returns a boolean indicating whether the undo manager is able to perform a redo.

  canUndo()

    Returns a boolean indicating whether the undo manager is able to perform an undo.

  clear()

    Remove all items from the dict.

  copy()

    Return a shallow copy of the dict.

  destroyAllRepresentations(notification=None)

    Destroy all representations.

  destroyRepresentation(name, **kwargs)

    Destroy the stored representation for name and **kwargs. If no kwargs are given, any representation with name will be destroyed regardless of the kwargs passed when the representation was created.

  property dirty

    The dirty state of the object. True if the object has been changed. False if not. Setting this to True will cause the base changed notification to be posted. The object will automatically maintain this attribute and update it as you change the object.

  disableNotifications(notification=None, observer=None)

    Disable this object’s notifications until told to resume them.

    - notification The specific notification to disable. This is optional. If no notification is given, all notifications will be disabled.

    This is a convenience method that does the same thing as:

        dispatcher = anObject.dispatcher
        dispatcher.disableNotifications(
            observable=anObject, notification=notification, observer=observer)

  property dispatcher

    The `defcon.tools.notifications.NotificationCenter` assigned to the parent of this object.

  enableNotifications(notification=None, observer=None)

    Enable this object’s notifications.

    - notification The specific notification to enable. This is optional.

    This is a convenience method that does the same thing as:

        dispatcher = anObject.dispatcher
        dispatcher.enableNotifications(
            observable=anObject, notification=notification, observer=observer)

  findObservations(observer=None, notification=None, observable=None, identifier=None)

    Find observations of this object matching the given arguments based on the values that were passed during addObserver. A value of None for any of these indicates that all should be considered to match the value. In the case of identifier, strings will be matched using fnmatch.fnmatchcase. The returned value will be a list of dictionaries with this format:

    > [
    >   {

    >     observer=<…> observable=<…> methodName=”…” notification=”…” identifier=”…”

    >   }

    > ]

    This is a convenience method that does the same thing as:

        dispatcher = anObject.dispatcher
        dispatcher.findObservations(
            observer=observer, observable=anObject,
            notification=notification, identifier=identifier
        )

  property font

    The `Font` that this object belongs to.

  classmethod fromkeys(iterable, value=None, /)

    Create a new dictionary with keys from iterable and values set to value.

  get(key, default=None, /)

    Return the value for key if key is in the dictionary, else default.

  getDataForSerialization(**kwargs)

    Return a dict of data that can be pickled.

  getRepresentation(name, **kwargs)

    Get a representation. name must be a registered representation name. **kwargs will be passed to the appropriate representation factory.

  hasCachedRepresentation(name, **kwargs)

    Returns a boolean indicating if a representation for name and **kwargs is cached in the object.

  hasObserver(observer, notification)

    Returns a boolean indicating is the observer is registered for notification.

    This is a convenience method that does the same thing as:

        dispatcher = anObject.dispatcher
        dispatcher.hasObserver(observer=observer,
            notification=notification, observable=anObject)

  holdNotifications(notification=None, note=None)

    Hold this object’s notifications until told to release them.

    - notification The specific notification to hold. This is optional. If no notification is given, all notifications will be held.

    - note An arbitrary string containing information about why the hold has been requested, the requester, etc. This is used for reference only.

    This is a convenience method that does the same thing as:

        dispatcher = anObject.dispatcher
        dispatcher.holdNotifications(
            observable=anObject, notification=notification, note=note)

  items()

    Return a set-like object providing a view on the dict’s items.

  keys()

    Return a set-like object providing a view on the dict’s keys.

  pop(k[, d]) → v, remove specified key and return the corresponding value.

    If the key is not found, return the default if given; otherwise, raise a KeyError.

  popitem()

    Remove and return a (key, value) pair as a 2-tuple.

    Pairs are returned in LIFO (last-in, first-out) order. Raises KeyError if the dict is empty.

  postNotification(notification, data=None)

    Post a notification through this object’s notification dispatcher.

    > - notification The name of the notification.

    > - data Arbitrary data that will be stored in the `Notification` object.

    This is a convenience method that does the same thing as:

        dispatcher = anObject.dispatcher
        dispatcher.postNotification(
            notification=notification, observable=anObject, data=data)

  redo()

    Perform a redo if possible, or return. If redo is performed, this will post BaseObject.BeginRedo and BaseObject.EndRedo notifications.

  releaseHeldNotifications(notification=None)

    Release this object’s held notifications.

    - notification The specific notification to hold. This is optional.

    This is a convenience method that does the same thing as:

        dispatcher = anObject.dispatcher
        dispatcher.releaseHeldNotifications(
            observable=anObject, notification=notification)

  removeObserver(observer, notification)

    Remove an observer from this object’s notification dispatcher.

    - observer A registered object.

    - notification The notification that the observer was registered to be notified of.

    This is a convenience method that does the same thing as:

        dispatcher = anObject.dispatcher
        dispatcher.removeObserver(observer=observer,
            notification=notification, observable=anObject)

  representationKeys()

    Get a list of all representation keys that are currently cached.

  setDataFromSerialization(data)

    Restore state from the provided data-dict.

  setdefault(key, default=None, /)

    Insert key with a value of default if key is not in the dictionary.

    Return the value for key if key is in the dictionary, else default.

  undo()

    Perform an undo if possible, or return. If undo is performed, this will post BaseObject.BeginUndo and BaseObject.EndUndo notifications.

  property undoManager

    The undo manager assigned to this object.

  update([E, ]**F) → None. Update D from mapping/iterable E and F.

    If E is present and has a .keys() method, then does: for k in E.keys(): D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]

  values()

    Return an object providing a view on the dict’s values.