Commits

Atsushi Odagiri  committed b79829d Draft

first commit

  • Participants

Comments (0)

Files changed (6)

+syntax: glob
+*.egg-info
+*~
+.*.swp
+*.pyc
+*.pyo
+==================
+standardenum
+==================
+
+USAGE
+-----------------
+
+>>> from standardenum import StandardEnum
+>>> class SampleEnum(StandardEnum):
+...     A = 1
+...     B = 2
+...     C = 3
+...
+>>> SampleEnum.A
+<SampleEnum.A value=1>
+>>> for e in SampleEnum:
+...     print e
+...
+<SampleEnum.A value=1>
+<SampleEnum.B value=2>
+<SampleEnum.C value=3>
+>>> SampleEnum.A == SampleEnum.A
+True
+>>> SampleEnum.A == SampleEnum.B
+False

File setup.cfg

Empty file added.
+from setuptools import setup, find_packages
+import sys, os
+
+here = os.path.dirname(__file__)
+
+def _read(name):
+    try:
+        return open(os.path.join(here, name)).read()
+    except:
+        return ""
+
+version = '0.0'
+
+readme = _read("README.txt")
+setup(name='standardenum',
+      version=version,
+      description="",
+      long_description=readme,
+      classifiers=[], # Get strings from http://pypi.python.org/pypi?%3Aaction=list_classifiers
+      keywords='',
+      author='Atsushi Odagiri',
+      author_email='aodagx@gmail.com',
+      url='https://bitbucket.org/aodag/standardenum',
+      license='MIT',
+      package_dir={"": "src"},
+      packages=find_packages("src"),
+      include_package_data=True,
+      zip_safe=False,
+      test_suite="standardenum",
+      install_requires=[
+          # -*- Extra requirements: -*-
+      ],
+      entry_points="""
+      # -*- Entry points: -*-
+      """,
+      )

File src/standardenum/__init__.py

+#
+# -*- coding:utf-8 -*-
+"""
+>>> class SampleEnum(StandardEnum):
+...     A = 1
+...     B = 2
+...     C = 3
+...
+>>> SampleEnum.A
+<SampleEnum.A value=1>
+>>> for e in SampleEnum:
+...     print e
+...
+<SampleEnum.A value=1>
+<SampleEnum.B value=2>
+<SampleEnum.C value=3>
+>>> SampleEnum.A == SampleEnum.A
+True
+>>> SampleEnum.A == SampleEnum.B
+False
+"""
+
+
+class EnumType(type):
+
+    def __init__(cls, name, bases, dct):
+        super(EnumType, cls).__init__(name, bases, dct)
+        cls._values = []
+
+        for key, value in dct.iteritems():
+            if not key.startswith('_'):
+                v = cls(key, value)
+                setattr(cls, key, v)
+                cls._values.append(v)
+
+    def __iter__(cls):
+        return iter(cls._values)
+
+
+class StandardEnum(object):
+
+    __metaclass__ = EnumType
+
+    def __init__(self, k, v):
+        self.v = v
+        self.k = k
+
+    def __repr__(self):
+        return "<%s.%s value=%s>" % (self.__class__.__name__, self.k, self.v)
+
+    def __str__(self):
+        return str(self.v)
+
+    def __int__(self):
+        return int(self.v)
+
+    def __long__(self):
+        return long(self.v)
+
+    def __float__(self):
+        return float(self.v)
+
+    def __complex__(self):
+        return complex(self.v)
+
+
+
+if __name__ == '__main__':
+    import doctest
+    doctest.testmod()

File src/standardenum/test_it.py

+import unittest
+
+class Tests(unittest.TestCase):
+    def _getTarget(self):
+        from standardenum import StandardEnum
+        return StandardEnum
+
+    def test_it(self):
+        target = self._getTarget()
+        dummy = type('dummy', (target,), 
+            dict(
+                A=1,
+                B=2,
+                C=3,
+            ))
+        self.assertTrue(hasattr(dummy, 'A'))
+        self.assertTrue(hasattr(dummy, 'B'))
+        self.assertTrue(hasattr(dummy, 'C'))
+
+
+    def test_int(self):
+        target = self._getTarget()
+        dummy = type('dummy', (target,), 
+            dict(
+                A=1,
+                B=2,
+                C=3,
+            ))
+        self.assertEqual(int(getattr(dummy, 'A')), 1)
+