Maestro Stefano, questo sembra bellissimo! :-)

On 14.10.20 16:13, Stefano Miccoli via Owfs-developers wrote:
> A more succinct way would be:
>
> str(x).encode()
>
> In fact the outer call to ‘bytes’ in 'bytes(str.encode(str(x)))' is a
> no-op, and python strings are objects which have an ‘encode' method,
> so no need call the class method ’str.encode’.
>
> Another possible way is 
>
> f"{x:d}".encode()
>
> or 
>
> "{:d}".format(x).encode()
>
> which is more “defensive”, in the sense that the conversion fails if x
> is not an integer. In fact “str(x)” is defined for almost any
> imaginable object in python, and could return anything. Therefore it
> is better to be a little more verbose, and be explicit on the fact
> that here we are interested in a decimal integer.
>
> Another variant, in which we accept a float value could be
>
> f"{round(x):d}".encode()
>
> or
>
> f"{x:.0f}"
>
> but possibilities are endless.
>
> Bye and thank you for sharing.
>
>
> Stefano
>
>
>> On 14 Oct 2020, at 15:08, Mick Sulley <m...@sulley.info
>> <mailto:m...@sulley.info>> wrote:
>>
>> I had a bit of trouble with this and thought it worth sharing the
>> solution.
>>
>> With Python2 and pyownet I could use
>>
>>     owp.write('/settings/timeout/directory', 60)
>>
>> but with Python3 that throws an error, TypeError: 'data' argument
>> must be binary.  I can use
>>
>>     owp.write('/settings/timeout/directory', b'60')
>>
>> but if I want to use a variable for the data the format which works is -
>>
>>     x = 60
>>     owp.write('/settings/timeout/directory', bytes(str.encode(str(x))))
>>
>> There may be other ways to do it but that works for me.
>>
>> Mick
>>
>>
>>
>> _______________________________________________
>> Owfs-developers mailing list
>> Owfs-developers@lists.sourceforge.net
>> <mailto: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

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

Reply via email to