Code Monkey home page Code Monkey logo

packtab's Issues

Set up Travis CI

  • Run minimal tests
  • build and upload distribution packages to PyPI on tagged commits

Avoid Type/Object Identity Comparisons

It is preferable to use isinstance rather than comparing type objects for identity. Also better to compare integers for equality rather than object identity โ€” the latter may work for small-magnitude integers, but it can give surprising results sometimes.

diff --git a/Lib/packTab/__init__.py b/Lib/packTab/__init__.py
index bbf1e7f..f687485 100644
--- a/Lib/packTab/__init__.py
+++ b/Lib/packTab/__init__.py
@@ -25,7 +25,7 @@ except ImportError:
 class AutoMapping(collections.defaultdict):
    _next = 0
    def __missing__(self, key):
-		assert type(key) is not int
+		assert not isinstance(key, int)
        v = self._next
        self._next = self._next + 1
        self[key] = v
@@ -60,8 +60,8 @@ def pack_table(data, mapping=None, default=0):

    # Set up mapping.  See docstring.
    if mapping is not None:
-		assert (all(type(k) is int and type(v) is str for k,v in mapping.items()) or
-			all(type(k) is str and type(v) is int for k,v in mapping.items()))
+		assert (all(isinstance(k, int) and isinstance(v, str) for k,v in mapping.items()) or
+			all(isinstance(k, str) and isinstance(v, int) for k,v in mapping.items()))
        mapping2 = mapping.copy()
        for k,v in mapping.items():
            mapping2[v] = k
@@ -72,7 +72,7 @@ def pack_table(data, mapping=None, default=0):

    # Set up data as a list.
    if isinstance(data, dict):
-		assert(all(type(k) is int and type(v) in (int, str) for k,v in data.items()))
+		assert(all(isinstance(k, int) and isinstance(v, (int, str)) for k,v in data.items()))
        minK = min(dict.keys())
        maxK = max(dict.keys())
        assert minK >= 0
@@ -83,11 +83,11 @@ def pack_table(data, mapping=None, default=0):
        del data2

    # Convert all to integers
-	assert (all(type(v) is int for v in data) or
-		all(type(v) is str for v in data))
-	if type(data[0]) is str:
+	assert (all(isinstance(v, int) for v in data) or
+		all(isinstance(v, str) for v in data))
+	if isinstance(data[0], str):
        data = [mapping[v] for v in data]
-	if type(default) is str:
+	if isinstance(default, str):
        default = mapping[default]

    return solve(data, default)
@@ -111,7 +111,7 @@ def binaryBitsFor(n):
    >>> binaryBitsFor(100)
    8
    """
-	if n is 1: return 0
+	if n == 1: return 0
    return 1 << ceil(log2(log2(n)))

 bytesPerOp = 4

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.