gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gnunet-python] 03/12: Simplify initiation of `_Key` and subclasses.


From: gnunet
Subject: [gnunet-python] 03/12: Simplify initiation of `_Key` and subclasses.
Date: Sun, 12 Apr 2020 23:22:21 +0200

This is an automated email from the git hooks/post-receive script.

htgoebel pushed a commit to branch master
in repository gnunet-python.

commit 41724f12889455473a83fba459a709791005d5c2
Author: Hartmut Goebel <address@hidden>
AuthorDate: Sat Jan 19 19:50:56 2019 +0100

    Simplify initiation of `_Key` and subclasses.
    
    Also take the chance and break some long lines.
---
 gnunet/__init__.py | 16 +++++++++-------
 gnunet/crypto.py   |  3 +--
 2 files changed, 10 insertions(+), 9 deletions(-)

diff --git a/gnunet/__init__.py b/gnunet/__init__.py
index dc4e701..c4c5648 100644
--- a/gnunet/__init__.py
+++ b/gnunet/__init__.py
@@ -6,8 +6,8 @@ class GNUNetDaemonError(Exception):
 
 
 class _Key:
-    def __init__(self, arg, subtype, bits):
-        if isinstance(arg, subtype):
+    def __init__(self, arg):
+        if isinstance(arg, type(self)):
             self._data = arg._data
         elif isinstance(arg, str):
             self._data = strings.string_to_data(arg)
@@ -15,18 +15,20 @@ class _Key:
             try:
                 self._data = bytearray(arg)
             except:
-                raise TypeError("'arg' must be a " + type(subtype).__name__ + 
", a string or an array of bytes. Not a '" + type(arg).__name__ + "'.")
+                raise TypeError("'arg' must be a %s, a string or an array "
+                                "of bytes. Not a '%s'." %
+                                (type(self).__name__, type(arg).__name__),)
 
-        if len(self._data) * 8 != bits:
-            raise ValueError("'arg' must be a " + bits + " bit hash. Got " + 
len(self._data) + " bits.")
+        if len(self._data) * 8 != self.__bits__:
+            raise ValueError("'arg' must be a %s bit hash. Got %s bits."
+                             % (self.__bits__, len(self._data) * 8))
 
     def __str__(self):
         return strings.data_to_string(self._data)
 
 
 class HashCode(_Key):
-    def __init__(self, arg):
-        _Key.__init__(self, arg, HashCode, 512)
+    __bits__ = 512
 
     def __repr__(self):
         return "gnunet.HashCode('" + str(self) + "')"
diff --git a/gnunet/crypto.py b/gnunet/crypto.py
index 461b0cc..253f46e 100644
--- a/gnunet/crypto.py
+++ b/gnunet/crypto.py
@@ -3,8 +3,7 @@ import gnunet.strings as strings
 
 
 class EcdsaPublicKey(_Key):
-    def __init__(self, arg):
-        _Key.__init__(self, arg, EcdsaPublicKey, 256)
+    __bits__ = 256
 
     def __repr__(self):
         return "gnunet.crypto.EcdsaPublicKey('" + str(self) + "')"

-- 
To stop receiving notification emails like this one, please contact
address@hidden.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]