Navigation Menu

Skip to content

Commit

Permalink
Add Python 3 compatibility (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
reclosedev committed Dec 21, 2015
1 parent 5b64847 commit b437f4c
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 23 deletions.
1 change: 1 addition & 0 deletions pyautocad/api.py
Expand Up @@ -32,6 +32,7 @@
ACAD = None

import pyautocad.types
from pyautocad.compat import basestring, xrange

logger = logging.getLogger(__name__)

Expand Down
7 changes: 3 additions & 4 deletions pyautocad/cache.py
Expand Up @@ -11,6 +11,7 @@
:copyright: (c) 2012 by Roman Haritonov.
:license: BSD, see LICENSE.txt for more details.
"""
from __future__ import print_function


class Cached(object):
Expand Down Expand Up @@ -90,19 +91,17 @@ def __delattr__(self, key):
object.__delattr__(self._instance, key)




if __name__ == "__main__":
import time

class Foo(object):
@property
def x(self):
print 'consuming time'
print('consuming time')
time.sleep(1)
return 42

foo = Foo()
cached_foo = Cached(foo)
for i in range(5):
print cached_foo.x
print(cached_foo.x)
15 changes: 15 additions & 0 deletions pyautocad/compat.py
@@ -0,0 +1,15 @@
import sys

IS_PY3 = sys.version_info[0] == 3

if IS_PY3:
unicode = str
bytes = bytes
basestring = str
xrange = range
else:
unicode = unicode
_orig_bytes = bytes
bytes = lambda s, *a: _orig_bytes(s)
basestring = basestring
xrange = xrange
14 changes: 12 additions & 2 deletions pyautocad/types.py
Expand Up @@ -13,6 +13,8 @@
import operator
import math

from pyautocad.compat import IS_PY3


class APoint(array.array):
""" 3D point with basic geometric operations and support for passing as a
Expand Down Expand Up @@ -100,13 +102,21 @@ def __sub__(self, other):
def __mul__(self, other):
return self.__left_op(self, other, operator.mul)

def __div__(self, other):
return self.__left_op(self, other, operator.div)
if IS_PY3:
def __div__(self, other):
return self.__left_op(self, other, operator.truediv)
else:
def __div__(self, other):
return self.__left_op(self, other, operator.div)

__radd__ = __add__
__rsub__ = __sub__
__rmul__ = __mul__
__rdiv__ = __div__
__floordiv__ = __div__
__rfloordiv__ = __div__
__truediv__ = __div__
_r_truediv__ = __div__

def __neg__(self):
return self.__left_op(self, -1, operator.mul)
Expand Down
5 changes: 2 additions & 3 deletions pyautocad/utils.py
Expand Up @@ -9,6 +9,7 @@
:copyright: (c) 2012 by Roman Haritonov.
:license: BSD, see LICENSE.txt for more details.
"""
from __future__ import print_function

import sys
import re
Expand Down Expand Up @@ -108,9 +109,7 @@ def timing(message=u'Elapsed'):
yield begin
finally:
elapsed = (time.time() - begin)
print u'%s: %.3f s' % (message, elapsed)


print(u'%s: %.3f s' % (message, elapsed))


def dynamic_print(text):
Expand Down
6 changes: 5 additions & 1 deletion tests/test_api.py
@@ -1,10 +1,14 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# date: 16.01.12
from __future__ import print_function
import os, sys
sys.path.insert(0, os.path.abspath('..'))
from _ctypes import COMError
import unittest

from pyautocad import Autocad, aDouble, APoint, ACAD
from pyautocad.compat import unicode


NPASS = 3000
Expand All @@ -14,7 +18,7 @@ class ApiTestCase(unittest.TestCase):
def setUp(self):
self.acad = Autocad(True)
self.doc = self.acad.app.Documents.Add()
print 'Current', self.doc.Name
print('Current', self.doc.Name)

def tearDown(self):
self.doc.Close(False)
Expand Down
10 changes: 6 additions & 4 deletions tests/test_cached.py
@@ -1,6 +1,8 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#date: 16.01.12
from __future__ import print_function
import os, sys
sys.path.insert(0, os.path.abspath('..'))
import unittest
import mock

Expand All @@ -12,17 +14,17 @@ def __init__(self):
self._x = None

def getx(self):
print "get x"
print("get x")
return self._x

def setx(self, value):
print "set x", value
print("set x", value)
self._x = value

x = property(lambda self: self.getx(), lambda self, x: self.setx(x))

def modify(self, val):
print "modify", val
print("modify", val)
self._x = val


Expand Down
4 changes: 2 additions & 2 deletions tests/test_contrib_tables.py
@@ -1,7 +1,7 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#date: 13.02.12
import os
import os, sys
sys.path.insert(0, os.path.abspath('..'))
import unittest

from pyautocad.contrib import tables
Expand Down
2 changes: 2 additions & 0 deletions tests/test_some_ideas.py
@@ -1,6 +1,8 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#date: 13.02.12
import os, sys
sys.path.insert(0, os.path.abspath('..'))
import unittest

from pyautocad import Autocad
Expand Down
3 changes: 2 additions & 1 deletion tests/test_types.py
@@ -1,6 +1,8 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#date: 17.01.12
import os, sys
sys.path.insert(0, os.path.abspath('..'))
import unittest

from pyautocad.types import APoint, distance
Expand Down Expand Up @@ -54,7 +56,6 @@ def test_args(self):
for arg in wrong_args:
with self.assertRaises(TypeError):
p = APoint(arg)
print arg

p = APoint(0, 0, 0)
for arg in wrong_args:
Expand Down
14 changes: 8 additions & 6 deletions tests/test_utils.py
@@ -1,15 +1,17 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#date: 16.01.12
from __future__ import unicode_literals
import os, sys
sys.path.insert(0, os.path.abspath('..'))
import unittest

from pyautocad.utils import unformat_mtext, mtext_to_string, string_to_mtext

class UtilsTestCase(unittest.TestCase):
def test_unformat(self):
texts = [ur'{\fGOST type A|b0|i0|c204|p34;ЩО\fGOST type A|b0|i0|c0|p34;2-8}',
ur'text\Pwith {\fWide Latin|b0|i0|c0|p18;multi} {\fVerdana|b0|i0|c0|p34;format}',
ur'test\P\pxi-3,l3,t3;1. list1\P2. list2\P\pi0,l0,tz;\P\pi-3,l3,t3;{\fSymbol|b0|i0|c2|p18;· }bullet1\P{\fSymbol|b0|i0|c2|p18;· }bullet2\P\pi0,l0,tz;\P{\fVani|b0|i0|c0|p34;another font} {\fVerdana|b1|i0|c0|p34;and bold}',
texts = [r'{\fGOST type A|b0|i0|c204|p34;ЩО\fGOST type A|b0|i0|c0|p34;2-8}',
r'text\Pwith {\fWide Latin|b0|i0|c0|p18;multi} {\fVerdana|b0|i0|c0|p34;format}',
r'test\P\pxi-3,l3,t3;1. list1\P2. list2\P\pi0,l0,tz;\P\pi-3,l3,t3;{\fSymbol|b0|i0|c2|p18;· }bullet1\P{\fSymbol|b0|i0|c2|p18;· }bullet2\P\pi0,l0,tz;\P{\fVani|b0|i0|c0|p34;another font} {\fVerdana|b1|i0|c0|p34;and bold}',
]
desired1 = [u'ЩО2-8', u'text\Pwith multi format', u'test\P1. list1\P2. list2\P\P· bullet1\P· bullet2\P\Panother font and bold']
desired2 = [u'ЩО2-8', u'text\nwith multi format',
Expand All @@ -21,8 +23,8 @@ def test_unformat(self):
· bullet2
another font and bold"""]
result1 = map(unformat_mtext, texts)
result2 = map(mtext_to_string, texts)
result1 = list(map(unformat_mtext, texts))
result2 = list(map(mtext_to_string, texts))

self.assertEqual(result1, desired1)
self.assertEqual(result2, desired2)
Expand Down

0 comments on commit b437f4c

Please sign in to comment.