Commit 201f3133 authored by Jim Fulton's avatar Jim Fulton

initial

parent c1ce70b0
from iclass import Base
class Mutable(Base):
"""Mutable objects
"""
class Comparable(Base):
"""Objects that can be tested for equality
"""
class Orderable(Comparable):
"""Objects that can be compared with < > == <= >= !="""
class Hashable(Base):
"""Objects that support hash"""
class Key(Comparable, Hashable):
"""Objects that are immutable with respect to state that
influences comparisons or hash values"""
import Basic, Util
class Mapping(Basic.Base):
"anything supporting __getitem__"
class Sized(Basic.Base):
"anything supporting __len"
class MutableMapping(Basic.Mutable):
"Has __setitem__ and __delitem__"
class Sequence(Mapping):
"Keys must be integers in a sequence starting at 0."
class Sequential(Sequence):
"Keys must be used in order"
Util.assertTypeImplements(type(()), (Sequence, Sized, Basic.Key))
Util.assertTypeImplements(type([]), (Sequence, Sized, MutableMapping))
Util.assertTypeImplements(type({}), (Mapping, Sized, MutableMapping))
import Basic, Util
class BitNumber(Basic.Base):
"""Numbers that can be interpreted as strings of bits
they support & | ~ ^ << >>
"""
class Number(Basic.Base):
"""Numeric interface
it is assumed that numeric types are embedded in each other in some
way (this is the Scheme numerical tower I think); they support
operators + - *, usually / and %, perhaps **
"""
class Offsetable(Basic.Base):
"""Things that you can subtract and add numbers too
e.g. Date-Time values.
"""
class Real(Number):
"""any subset of the real numbers (this includes ints and rationals)
"""
class Complex(Number):
"""has re and im fields (which are themselves real)
"""
class Exact(Number):
"""e.g. integers and rationals; also fixed point
"""
class AbritraryPrecision(Exact):
"""e.g. long, rationals
"""
class FixedPrecisionInt(Exact):
"""Fixed precision integers
"""
class FP64(FixedPrecisionInt):
"""int() applied to these returns an integer that fits in 32 bits
"""
class FP32(FP64):
"""int() applied to these returns an integer that fits in 32 bits
"""
class FP16(FP32):
"""int() applied to these returns an integer that fits in 16 bits
"""
class FP8(FP16):
"""int() applied to these returns an integer that fits in 16 bits
"""
class FP1(FP8):
"""int() applied to these returns an integer that fits in 16 bits
"""
class Signed(FixedPrecisionInt):
"""These can be < 0
"""
class Unsigned(FixedPrecisionInt):
"""These can not be < 0
"""
class CheckedOverflow(FixedPrecisionInt):
"""raises OverflowError when results don't fit
"""
class UncheckedOverflow(FixedPrecisionInt):
"""take results module 2**N (for example)
"""
class FD(Exact):
"""fixed_decimal<n+m>
a signed fixed decimal number with n digits
before and m digits after the decimal point
"""
class Inexact(Number):
"""floating point arithmetic
"""
Util.assertTypeImplements(type(1), (FP32, BitNumber, Signed))
Util.assertTypeImplements(type(1L), (AbritraryPrecision, BitNumber, Signed))
Util.assertTypeImplements(type(1.0), (Inexact, BitNumber, Signed))
Scarecrow interface implementation
import iclass, Util
Interface=Util.impliedInterface(
iclass.Interface, "Interface",
"""Interface of Interface objects
""")
iclass.Interface.__implements__=Interface
from Basic import *
class Class(Base):
"""Implement shared instance behavior and create instances
Classes can be called to create an instance. This interface does
not specify what if any arguments are required.
"""
Util.assertTypeImplements(iclass.ClassType, Class)
from Number import *
from Mapping import *
from iclass import Interface, ClassType, Base, assertTypeImplements
def impliedInterface(klass, __name__=None, __doc__=None):
"""Create an interface object from a class
The interface will contain only objects with doc strings and with names
that begin and end with '__' or names that don't begin with '_'.
"""
if __name__ is None: name="%sInterface" % klass.__name__
return Interface(__name__, (), _ii(klass, {}), __doc__)
def _ii(klass, items):
for k, v in klass.__dict__.items():
if k[:1]=='_' and not (len(k) > 4 and k[:2]=='__' and k[-2:]=='__'):
continue
items[k]=v
for b in klass.__bases__: _ii(b)
return items
def implementedBy(object):
"""Return the interfaces implemented by the object
"""
r=[]
implements=tiget(type(object), None)
if implements is None:
if hasattr(object, '__implements__'):
implements=object.__implements__
else: return r
if isinstance(implements,Interface): r.append(i)
else: _wi(implements, r.append)
return r
def implementedByInstances(object):
"""Return the interfaces that instanced implement (by default)
"""
r=[]
if type(klass) is ClassType:
if hasattr(klass, '__implements__'):
implements=klass.__implements__
else: return r
elif hasattr(klass, 'instancesImplements'):
# Hook for ExtensionClass. :)
implements=klass.instancesImplements()
else:
implements=tiget(klass,None)
if implements is not None:
if isinstance(implements,Interface): r.append(i)
else: _wi(implements, r.append)
return r
def _wi(interfaces, append):
for i in interfaces:
if isinstance(i,Interface): append(i)
else: _wi(i, append)
from Standard import Base
from Util import impliedInterface
from Util import assertTypeImplements, implementedBy, implementedByInstances
"""Python Interfaces
Scarecrow version:
Classes or objects can implement an __implements__ attribute that
names a sequence of interface objects.
An interface is defined using the class statement and one or more base
interfaces.
"""
_typeImplements={}
class Interface:
"""Prototype (scarecrow) Interfaces Implementation
"""
def __init__(self, name, bases=(), attrs=None, __doc__=None):
"""Create a new interface
"""
for b in bases:
if not isinstance(b, Interface):
raise TypeError, 'Expected base interfaces'
self.__bases__=bases
self.__name__=name
if attrs is None: attrs={}
if attrs.has_key('__doc__'):
if __doc__ is None: __doc__=attrs['__doc__']
del attrs['__doc__']
self.__attrs=attrs
if __doc__ is not None: self.__doc__=__doc__
def extends(self, other):
"""Does an interface extend another?
"""
for b in self.__bases__:
if b is other: return 1
if b.extends(other): return 1
return 0
def implementedBy(self, object,
tiget=_typeImplements.get):
"""Does the given object implement the interface?
"""
implements=tiget(type(object), None)
if implements is None:
if hasattr(object, '__implements__'):
implements=object.__implements__
else: return 0
if isinstance(implements,Interface):
return implements is self or implements.extends(self)
else:
return self.__any(implements)
def implementedByInstancesOf(self, klass,
tiget=_typeImplements.get):
"""Do instances of the given class implement the interface?
"""
if type(klass) is ClassType:
if hasattr(klass, '__implements__'):
implements=klass.__implements__
else: return 0
elif hasattr(klass, 'instancesImplements'):
# Hook for ExtensionClass. :)
implements=klass.instancesImplements()
else:
implements=tiget(klass,None)
if implements is None: return 0
if isinstance(implements,Interface):
return implements is self or implements.extends(self)
else:
return self.__any(implements)
def names(self):
"""Return the attribute names defined by the interface
"""
return self.__attrs.keys()
def namesAndDescriptions(self):
"""Return the attribute names and descriptions defined by the interface
"""
return self.__attrs.items()
def getDescriptionFor(self, name, default=None):
"""Return the attribute description for the given name
"""
return self.__attrs.get(name, default)
def __any(self, interfaces):
for i in interfaces:
if isinstance(i,Interface):
return i is self or i.extends(self)
else:
return self.__any(i)
ClassType=type(Interface)
Base=Interface("Interface")
def assertTypeImplements(type, interfaces):
"""Return the interfaces implemented by objects of the given type
"""
_typeImplements[type]=interfaces
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment