Re: Concatenating a Hash to a String
"Ivan \"Rambius\" Ivanov" wrote at 2020-12-1 00:32 -0500:
> ...
>This code throws
>
>$ ./hashinstr.py
>Traceback (most recent call last):
> File "./hashinstr.py", line 16, in
>gen_sql(s)
> File "./hashinstr.py", line 13, in gen_sql
>sql = "insert into HASHES value ('" + ehash + "')"
>TypeError: can only concatenate str (not "bytes") to str
With Python 3, you must carefully distinquish `bytes` (a
sequence of numbers between 0 and 255) and `str` (a sequence of
(unicode) characters).
`base64` operates on `bytes`; `"..."` constructs an `str`;
Python 3 has made `bytes` and `str` (mostly) incompatible.
You can use `.decode([charset, error])` to convert `bytes` to `str`
and `.encode([charset, error])` to convert `str` to `bytes`.
Because `base64.encode` gives you a sequence of values between 0 and 127,
you can omit the optional parameters for `.decode`.
--
https://mail.python.org/mailman/listinfo/python-list
Re: Concatenating a Hash to a String
On 2020-12-01 05:32, Ivan "Rambius" Ivanov wrote:
Hello,
I want to store the hashes of strings in a database and I have
problems generating the sql statements. I generate the hashes using
hashlib and then convert it to base64 and I put the base64
representation in the sql. Here is the code:
#!/usr/bin/env python3.8
import base64
import hashlib
def gen_sql(s):
sha = hashlib.sha256()
# need to encode s as binary, otherwise the next line throws
# TypeError: Unicode-objects must be encoded before hashing
sha.update(b"{s}")
ehash = base64.b64encode(sha.digest())
sql = "insert into HASHES value ('" + ehash + "')"
s = "test"
gen_sql(s)
This code throws
$ ./hashinstr.py
Traceback (most recent call last):
File "./hashinstr.py", line 16, in
gen_sql(s)
File "./hashinstr.py", line 13, in gen_sql
sql = "insert into HASHES value ('" + ehash + "')"
TypeError: can only concatenate str (not "bytes") to str
Any help on how to concatenate ehash to the sql?
The bytes are all in the ASCII range, so you can convert it into a
string using .decode('ascii').
And, of course, use parametrised queries, as ChrisA said.
--
https://mail.python.org/mailman/listinfo/python-list
Re: Best-practice for formatted string literals and localization?
Hartmut Goebel wrote at 2020-11-30 19:30 +0100:
>formatted string literals are great, but can't be used together with
>localization:
>
>_(f"These are {count} stones")
>
>will crash babel ("NameError: name 'count' is not defined".
Translations are kept in external files (without any runtime
association).
As a consequence, they cannot contain runtime entities;
and therefore, you should not use f-strings for internationalizations.
Usually, the translation machinery has special ways to
provide parameters for translations.
For example with `zope.i18nmessageid`, you can use
`_(msg, mapping=)` to provide parameters
to the translations -- as in your case `count`).
Check, what parameter support your translation machinery support.
--
https://mail.python.org/mailman/listinfo/python-list
IDLE error
The IDLE seems to be malfunctioning, I just re-installed Python and used the reapir function but I can’t open the IDLE, please help. -- Nestares D. Álvaro -- https://mail.python.org/mailman/listinfo/python-list
Bot
Hi guys, I'm new here, can anyone help me built a bot than can input data in a website? This is not for spam purposes, I just need to reserve a place in the library at the university but they are completed in a matter of minutes and I can't waste time "camping" the website. Thank you Nestares D. Álvaro -- https://mail.python.org/mailman/listinfo/python-list
Re: IDLE error
Hi, Alvaro, On Tue, Dec 1, 2020 at 9:11 PM Álvaro d'Ors wrote: > > The IDLE seems to be malfunctioning, I just re-installed Python and used > the reapir function but I can’t open the IDLE, please help. What OS do you use? Are you trying to run it by double-clicking on the icon? What happens? Does it give you any error? Thank you. > > > > -- > Nestares D. Álvaro > -- > https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: Bot
On 01/12/2020 21:53, Álvaro d'Ors wrote: Hi guys, I'm new here, can anyone help me built a bot than can input data in a website? This is not for spam purposes, I just need to reserve a place in the library at the university but they are completed in a matter of minutes and I can't waste time "camping" the website. Thank you https://duckduckgo.com/?q=python+web+form+fill - the third or fourth response seemed to handle a log-on screen. Although if you're prepared to pay for their time, others may be able to save your time... -- Regards =dn -- https://mail.python.org/mailman/listinfo/python-list
Re: Concatenating a Hash to a String
Hello,
Thank you all for your help.
On Tue, Dec 1, 2020 at 1:38 PM MRAB wrote:
> The bytes are all in the ASCII range, so you can convert it into a
> string using .decode('ascii').
I utilized encode and decode string methods to convert from bytes to strings
> And, of course, use parametrised queries, as ChrisA said.
I am using sqlite3 and it supports parameterized queries. Overall they
are better than constructing the sql statements manually.
Regards and thanks
rambius
--
Tangra Mega Rock: http://www.radiotangra.com
--
https://mail.python.org/mailman/listinfo/python-list
