-
-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Counted mixin class and refactor form signature computation (#178)
* Add Counted mixin class and refactor form signature computation * fixup * fixup --------- Co-authored-by: Matthew Scroggs <[email protected]>
- Loading branch information
1 parent
83f3408
commit b0d635a
Showing
8 changed files
with
91 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,40 @@ | ||
# -*- coding: utf-8 -*- | ||
"Utilites for types with a global unique counter attached to each object." | ||
"Mixin class for types with a global unique counter attached to each object." | ||
|
||
# Copyright (C) 2008-2016 Martin Sandve Alnæs | ||
# | ||
# This file is part of UFL (https://www.fenicsproject.org) | ||
# | ||
# SPDX-License-Identifier: LGPL-3.0-or-later | ||
|
||
import itertools | ||
|
||
def counted_init(self, count=None, countedclass=None): | ||
"Initialize a counted object, see ExampleCounted below for how to use." | ||
|
||
if countedclass is None: | ||
countedclass = type(self) | ||
class Counted: | ||
"""Mixin class for globally counted objects.""" | ||
|
||
if count is None: | ||
count = countedclass._globalcount | ||
# Mixin classes do not work well with __slots__ so _count must be | ||
# added to the __slots__ of the inheriting class | ||
__slots__ = () | ||
|
||
self._count = count | ||
_counter = None | ||
|
||
if self._count >= countedclass._globalcount: | ||
countedclass._globalcount = self._count + 1 | ||
def __init__(self, count=None, counted_class=None): | ||
"""Initialize the Counted instance. | ||
:arg count: The object count, if ``None`` defaults to the next value | ||
according to the global counter (per type). | ||
:arg counted_class: Class to attach the global counter too. If ``None`` | ||
then ``type(self)`` will be used. | ||
class ExampleCounted(object): | ||
"""An example class for classes of objects identified by a global counter. | ||
""" | ||
# create a new counter for each subclass | ||
counted_class = counted_class or type(self) | ||
if counted_class._counter is None: | ||
counted_class._counter = itertools.count() | ||
|
||
Mimic this class to create globally counted objects within a single type. | ||
""" | ||
# Store the count for each object | ||
__slots__ = ("_count",) | ||
self._count = count if count is not None else next(counted_class._counter) | ||
self._counted_class = counted_class | ||
|
||
# Store a global counter with the class | ||
_globalcount = 0 | ||
|
||
# Call counted_init with an optional constructor argument and the class | ||
def __init__(self, count=None): | ||
counted_init(self, count, ExampleCounted) | ||
|
||
# Make the count accessible | ||
def count(self): | ||
return self._count |
Oops, something went wrong.