I think that this is the correct place to ask, so I'll give a brief answer.

In python2 you had "strings" and "unicode strings". Python2 "strings" were 
1-byte sequences, so it was impossibile to represent UNICODE code points beyond 
the few ASCII ones; therefore the "unicode string" was introduced. Strings 
could be used both for binary data, as well as for text.

This confusion was deprecated, and in python3 there is a strict distinction 
between text and binary data. Strings (see 
<https://docs.python.org/3/library/stdtypes.html#text-sequence-type-str>) are 
sequences of UNICODE code points, and therefore are multi-byte sequences; 
1-byte (8 bit) sequences are called "bytes" (see 
<https://docs.python.org/3/library/stdtypes.html#bytes-objects>) and are used 
for binary data.

A very common mapping from python2 to python3 datatypes is 

str() → byte()
unicode() → str()

or if you prefer

"abc" → b"abc"
u"àèì" → "àèì"

I regard pyownet as a low-level library, so I like to speak binary to the 
owserver, i.e. read and writes are bytes in python3 and str in python2. It is 
responsibility of the user to decode/encode the messages sent and received from 
owserver. OWFS node names (similarly to path names on a file system) instead 
are considered "non binary", and therefore are represented by strings.

As what regards the practicality of using pyownet.

- some nodes contain binary data: e.g. '/26.xxxxxxxxxxxx/pages/page.ALL', no 
decoding needed

- numeric values can be converted directly, without the need of decoding: if 
owp is a proxy object you have e.g.

>>> owp.read('/26.xxxxxxxxxxxx/temperature')
b'     24.4688'
>>> float(owp.read('/26.xxxxxxxxxxxx/temperature'))
24.4688

(this is because the float() class accepts both strings and bytes as input.)

- text values should be decoded, but usually you can omit the encoding (which 
should be 'utf-8' or better 'ascii'):

>>> owp.read("/structure/26/CA")
b'y,000000,000001,rw,000001,s,'
>>> owp.read("/structure/26/CA").decode()
'y,000000,000001,rw,000001,s,'

Regards,

Stefano


> On 9 Oct 2019, at 22:44, Mick Sulley <m...@sulley.info> wrote:
> 
> I am updating my python code from 2.7 to 3.7, using pyownet to communicate 
> with 1-wire.
> 
> Reads and writes were strings in 2.7 but it seems they are binary in 3.7.  I 
> can get around this by appending .decode('utf-8) and .encode('utf-8) to the 
> read and write functions, but I feel that I am making hard work of this.  Is 
> there a better way to move reads and writes to Python3?
> 
> I don't understand why the change has occurred, but I guess that is not a 
> question for this group.
> 
> Thanks
> 
> Mick
> 
> 
> 
> _______________________________________________
> Owfs-developers mailing list
> Owfs-developers@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/owfs-developers

_______________________________________________
Owfs-developers mailing list
Owfs-developers@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/owfs-developers

Reply via email to