The problem was mixing bytes and str values. Ensuring that everything is in bytes makes it work because urandom returns bytes.
# Encode the concatenated strings into a byte string
chars = (string.ascii_letters + string.digits + string.punctuation).encode()
char_gen = (c for c in map(urandom, repeat(1)) if c in chars)
# Use an empty byte string to join the generated bytes
pswd_text = b''.join(islice(char_gen, None, string_len))
You can always call pswd_text.decode() to get a unicode string (str).
I want to convert map
host1: attr1: val1 attr2: val2
into a string attr1=val1,attr2=val2. It is creazy difficult!
I can operate with a key / value separately but I cannot work with both to combine them into x=y:
- vars:
host:
attr1: val1
attr2: val2
block:
- debug:
msg: "{{ host|join(',') }}"Hi,
I am trying to write an Icinga check in Python which checks the status of my Keepalived setup. It has several status codes.
I'd like to convert those status codes to something human readable which makes sense. So I am thinking about a dict, something like: state_codes = {0: "init", 1: "backup", 2: "master", 3: "fault", 4: "goto master", 98: "goto fault"} which contains all the possible codes.
I have a JSON object which has the data:
[{
"data": {
"iname": "VRRP_1",
"state": 1,
"wantstate": 1,
}
}]
So the thing is I want to print something like VRRP_1 has state <STATE> and wants state <WANTSTATE>, in stead of the numbers.
How would one do that?
Thank you!
>>> T1 = ['13', '17', '18', '21', '32']
>>> T3 = list(map(int, T1))
>>> T3
[13, 17, 18, 21, 32]
This does the same thing as:
>>> T3 = [int(x) for x in T1]
>>> T3
[13, 17, 18, 21, 32]
so what you are doing is
>>> T3 = [[int(letter) for letter in x] for x in T1]
>>> T3
[[1, 3], [1, 7], [1, 8], [2, 1], [3, 2]]
Hope that clears up the confusion.
>>> T1 = ['13', '17', '18', '21', '32']
>>> print [int(x) for x in T1]
[13, 17, 18, 21, 32]
You don't need map inside your list comprehension. Map creates another list so you end up with a list of list.
Caveat: This will work if the strings are granted to be numbers otherwise it will raise exception.
Using list/generator comprehensions is preferable (more Pythonic) to map():
def rot_13(string):
alph = 'abcdefghijklmnopqrstuwxyz'
return ''.join(alph[(alph.find(i)+13) % len(alph)] for i in string)
python3 map returns a generator, so you have to consume it, using str.join is the easiest way:
"".join(map(lambda i: alph[(alph.find(i)+13) % len(alph)], string))
It is similar to this code:
my_string = ""
for l in map(lambda i: alph[(alph.find(i)+13) % len(alph)], string):
my_string += l