Re: [Tutor] Python .decode issue

2014-02-11 Thread Peter Otten
eryksun wrote:

> On Tue, Feb 11, 2014 at 3:42 AM, Peter Otten <__pete...@web.de> wrote:
>>
>> Unfortunately the bytes --> bytes conversion codecs in Python 2 have no
>> convenient analog in Python 3 yet.
>>
>> This will change in Python 3.4, where you can use
>>
> import codecs
> codecs.decode(b"ff10", "hex")
>> b'\xff\x10'
> codecs.encode(b"\xff\x10", "hex")
>> b'ff10'
> 
> 3.4 restores the "hex" alias to encodings.aliases.aliases. You can use
> "hex_codec" in earlier versions.
> 
> >>> codecs.encode(b"\xff\x10", "hex_codec")
> b'ff10'
> >>> codecs.decode(b"ff10", "hex_codec")
> b'\xff\x10'

Thanks for the correction.

>> But for now you are stuck with binascii.b2a_hex() etc.
> 
> The alternate names are binascii.hexlify and binascii.unhexlify.
> 
> >>> binascii.hexlify(b'\xff\x10') # b2a_hex
> b'ff10'
> >>> binascii.unhexlify(b'ff10')   # a2b_hex
> b'\xff\x10'


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python .decode issue

2014-02-11 Thread eryksun
On Tue, Feb 11, 2014 at 3:42 AM, Peter Otten <__pete...@web.de> wrote:
>
> Unfortunately the bytes --> bytes conversion codecs in Python 2 have no
> convenient analog in Python 3 yet.
>
> This will change in Python 3.4, where you can use
>
 import codecs
 codecs.decode(b"ff10", "hex")
> b'\xff\x10'
 codecs.encode(b"\xff\x10", "hex")
> b'ff10'

3.4 restores the "hex" alias to encodings.aliases.aliases. You can use
"hex_codec" in earlier versions.

>>> codecs.encode(b"\xff\x10", "hex_codec")
b'ff10'
>>> codecs.decode(b"ff10", "hex_codec")
b'\xff\x10'

> But for now you are stuck with binascii.b2a_hex() etc.

The alternate names are binascii.hexlify and binascii.unhexlify.

>>> binascii.hexlify(b'\xff\x10') # b2a_hex
b'ff10'
>>> binascii.unhexlify(b'ff10')   # a2b_hex
b'\xff\x10'
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python .decode issue

2014-02-11 Thread Peter Otten
james campbell wrote:

> I have been currently trying to get a small piece of code to work, but
> keep getting an error:
> 
> header_bin = header_hex.decode('hex')
> AttributeError: 'str' object has no attribute 'decode'
> 
> 
> The source of this code is from:
> https://en.bitcoin.it/wiki/Block_hashing_algorithm Here is the the code:
> >>> import hashlib >>> header_hex = ("0100" +
> "81cd02ab7e569e8bcd9317e2fe99f2de44d49ab2b8851ba4a308" +
> "e320b6c2fffc8d750423db8b1eb942ae710e951ed797f7affc8892b0f1fc122b" +
> "c7f5d74d" + "f2b9441a" + "42a14695") >>> header_bin =
> header_hex.decode('hex') >>> hash =
> hashlib.sha256(hashlib.sha256(header_bin).digest()).digest() >>>
> hash.encode('hex_codec')
> '1dbd981fe6985776b644b173a4d0385ddc1aa2a829688d1e' >>>
> hash[::-1].encode('hex_codec')
> '1e8d6829a8a21adc5d38d0a473b144b6765798e61f98bd1d'
> 
> I get the above error when I input this line by line into Win7 IDLE 3.3.3.
> Hopefully somebody can point me in the right direction. Thanks.

With the move from Python 2 to Python 3 string handling was changed

py2 --> py3
-
str --> bytes
unicode --> str

Also the encode decode methods have changed. 
bytes.decode() always gives you a str and str.encode() always returns bytes; 
there are no bytes.encode or str.decode methods.

Unfortunately the bytes --> bytes conversion codecs in Python 2 have no 
convenient analog in Python 3 yet. 

This will change in Python 3.4, where you can use

>>> import codecs
>>> codecs.decode(b"ff10", "hex")
b'\xff\x10'
>>> codecs.encode(b"\xff\x10", "hex")
b'ff10'

But for now you are stuck with binascii.b2a_hex() etc. 
Converting the 2x code of your example to 3x gives

>>> import hashlib
>>> import binascii
>>> header_hex = (
... b"0100"
... b"81cd02ab7e569e8bcd9317e2fe99f2de44d49ab2b8851ba4a308"
... b"e320b6c2fffc8d750423db8b1eb942ae710e951ed797f7affc8892b0f1fc122b"
... b"c7f5d74d"
... b"f2b9441a"
... b"42a14695")
>>> header_bin = binascii.a2b_hex(header_hex)
>>> hash = hashlib.sha256(hashlib.sha256(header_bin).digest()).digest()
>>> binascii.b2a_hex(hash)
b'1dbd981fe6985776b644b173a4d0385ddc1aa2a829688d1e'
>>> binascii.b2a_hex(hash[::-1])
b'1e8d6829a8a21adc5d38d0a473b144b6765798e61f98bd1d'


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python .decode issue

2014-02-10 Thread Mark Lawrence

On 10/02/2014 23:16, Pierre Dagenais wrote:


On 14-02-08 12:55 AM, james campbell wrote:

header_bin = header_hex.decode('hex')
AttributeError: 'str' object has no attribute 'decode'


What I understand is that you are trying to decode 'hex', which is a
string. The interpreter is telling you that strings cannot be decoded.
I do not know header_hex.decode(), but I suspect it's expecting an hex
number, check it out.

PierreD.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor



"AttributeError: 'str' object has no attribute 'decode'" is saying that 
header_hex has no attribute decode, the string 'hex' doesn't enter into 
the equation.


--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection 
is active.
http://www.avast.com


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python .decode issue

2014-02-10 Thread Pierre Dagenais


On 14-02-08 12:55 AM, james campbell wrote:
> header_bin = header_hex.decode('hex')
> AttributeError: 'str' object has no attribute 'decode'

What I understand is that you are trying to decode 'hex', which is a
string. The interpreter is telling you that strings cannot be decoded.
I do not know header_hex.decode(), but I suspect it's expecting an hex
number, check it out.

PierreD.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python .decode issue

2014-02-09 Thread Dave Angel
 james campbell  Wrote in message:
> 

(Context lost because message was erroneously posted in html)

strings cannot be decoded, so there's naturally no such method.
 Why not post a two line example of what you're trying, and
 explain what you were expecting it to do.


-- 
DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Python .decode issue

2014-02-09 Thread james campbell
I have been currently trying to get a small piece of code to work, but keep 
getting an error:

header_bin = header_hex.decode('hex')
AttributeError: 'str' object has no attribute 'decode'


The source of this code is from: 
https://en.bitcoin.it/wiki/Block_hashing_algorithm
 Here is the the code:
  >>> import hashlib >>> header_hex = ("0100" + 
"81cd02ab7e569e8bcd9317e2fe99f2de44d49ab2b8851ba4a308" + 
"e320b6c2fffc8d750423db8b1eb942ae710e951ed797f7affc8892b0f1fc122b" + "c7f5d74d" 
+ "f2b9441a" + "42a14695") >>> header_bin = header_hex.decode('hex') >>> hash = 
hashlib.sha256(hashlib.sha256(header_bin).digest()).digest() >>> 
hash.encode('hex_codec') 
'1dbd981fe6985776b644b173a4d0385ddc1aa2a829688d1e' >>> 
hash[::-1].encode('hex_codec') 
'1e8d6829a8a21adc5d38d0a473b144b6765798e61f98bd1d'

I get the above error when I input this line by line into Win7 IDLE 3.3.3. 
Hopefully somebody can point me in the right direction. Thanks.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor