Changeset - 23ab3e996c07
[Not reviewed]
0 2 0
Dionisio E Alonso (Baco) <dalonso _at_ grulic.org.ar> - 10 years ago 2014-07-14 21:26:11

PYD-5: Added support for Python 3.x. Minor corrections to documentation included.
2 files changed with 24 insertions and 17 deletions:
0 comments (0 inline, 0 general)
docs/usage.rst
Show inline comments
 
@@ -83,7 +83,7 @@ Finally, the resulting identicons can be in different formats::
 
                                     output_format="png")
 
  # Create identicon in ASCII format.
 
  identicon_ascii = generator.generate("john.doe@example.com", 200, 200,
 
                                       output_format="png")
 
                                       output_format="ascii")
 

	
 
Using the generated identicons
 
------------------------------
 
@@ -97,10 +97,10 @@ to achieve::
 
  identicon_png = generator.generate("john.doe@example.com", 200, 200,
 
                                     output_format="png")
 
  identicon_ascii = generator.generate("john.doe@example.com", 200, 200,
 
                                       output_format="png")
 
                                       output_format="ascii")
 

	
 
  # Identicon can be easily saved to a file.
 
  f = open("sample.png", "w")
 
  f = open("sample.png", "wb")
 
  f.write(identicon_png)
 
  f.close()
 

	
 
@@ -147,6 +147,6 @@ output them in PNG format to local directory::
 
                                   output_format="png")
 

	
 
    filename = user + ".png"
 
    with open(filename, "w") as f:
 
    with open(filename, "wb") as f:
 
        f.write(identicon)
 

	
pydenticon/__init__.py
Show inline comments
 
@@ -7,6 +7,9 @@ from io import BytesIO
 
# Pillow for Image processing.
 
from PIL import Image, ImageDraw
 

	
 
# For decoding hex values (works both for Python 2.7.x and Python 3.x).
 
import binascii
 

	
 

	
 
class Generator(object):
 
    """
 
@@ -64,8 +67,8 @@ class Generator(object):
 

	
 
        # Check if the digest produces sufficient entropy for identicon
 
        # generation.
 
        entropy_provided = len(digest("test").hexdigest()) / 2 * 8
 
        entropy_required = (columns / 2 + columns % 2) * rows + 8
 
        entropy_provided = len(digest(b"test").hexdigest()) // 2 * 8
 
        entropy_required = (columns // 2 + columns % 2) * rows + 8
 

	
 
        if entropy_provided < entropy_required:
 
            raise ValueError("Passed digest '%s' is not capable of providing %d bits of entropy" % (str(digest), entropy_required))
 
@@ -97,7 +100,7 @@ class Generator(object):
 
          True if the bit is 1. False if the bit is 0.
 
        """
 

	
 
        if hash_bytes[n / 8] >> int(8 - ((n % 8) + 1)) & 1 == 1:
 
        if hash_bytes[n // 8] >> int(8 - ((n % 8) + 1)) & 1 == 1:
 
            return True
 

	
 
        return False
 
@@ -119,7 +122,7 @@ class Generator(object):
 

	
 
        # Since the identicon needs to be symmetric, we'll need to work on half
 
        # the columns (rounded-up), and reflect where necessary.
 
        half_columns = self.columns / 2 + self.columns % 2
 
        half_columns = self.columns // 2 + self.columns % 2
 
        cells = self.rows * half_columns
 

	
 
        # Initialise the matrix (list of rows) that will be returned.
 
@@ -134,7 +137,7 @@ class Generator(object):
 
            if self._get_bit(cell, hash_bytes[1:]):
 

	
 
                # Determine the cell coordinates in matrix.
 
                column = cell / self.columns
 
                column = cell // self.columns
 
                row = cell % self.rows
 

	
 
                # Mark the cell and its reflection. Central column may get
 
@@ -166,14 +169,18 @@ class Generator(object):
 

	
 
        # If data seems to provide identical amount of entropy as digest, it
 
        # could be a hex digest already.
 
        if len(data) / 2 == self.digest_entropy / 8:
 
        if len(data) // 2 == self.digest_entropy // 8:
 
            try:
 
                data.decode("hex")
 
                digest = data
 
            except TypeError:
 
                digest = self.digest(data).hexdigest()
 
                binascii.unhexlify(data.encode('utf-8'))
 
                digest = data.encode('utf-8')
 
            # Handle Python 2.x exception.
 
            except (TypeError):
 
                digest = self.digest(data.encode('utf-8')).hexdigest()
 
            # Handle Python 3.x exception.
 
            except (binascii.Error):
 
                digest = self.digest(data.encode('utf-8')).hexdigest()
 
        else:
 
            digest = self.digest(data).hexdigest()
 
            digest = self.digest(data.encode('utf-8')).hexdigest()
 

	
 
        return [int(digest[i * 2:i * 2 + 2], 16) for i in range(16)]
 

	
 
@@ -217,8 +224,8 @@ class Generator(object):
 
        draw = ImageDraw.Draw(image)
 

	
 
        # Calculate the block widht and height.
 
        block_width = width / self.columns
 
        block_height = height / self.rows
 
        block_width = width // self.columns
 
        block_height = height // self.rows
 

	
 
        # Go through all the elements of a matrix, and draw the rectangles.
 
        for row, row_columns in enumerate(matrix):
0 comments (0 inline, 0 general)