sábado, 4 de enero de 2025

Python Range

 >>> 

>>> [a for a in range(2,11,2) ]

[2, 4, 6, 8, 10]

>>> [a for a in range(2,11,2) if a>4]

[6, 8, 10]

>>> [a**2 for a in range(2,11,2) if a>4]

[36, 64, 100]

>>> 


filtering a list with other list

>>> l=[a for a in l if a not in ['Casa',0]]

>>> l

['Television', 10, 'Pistola']

 

>>> l=[a for a in l if a not in ['Casa',0]]

>>> l

['Television', 10, 'Pistola']


miércoles, 25 de diciembre de 2024

Switch Match

 >>> def http(e):

...    match e:

...       case 400:

...          print('Error 400')

...       case 401:

...          print('Auth request')

...       case _:

...          print('Unknow error')

... 

>>> http(12)

Unknow error

>>> http(500)

Unknow error

>>> http(400)

Error 400


martes, 24 de diciembre de 2024

OpenAI API Realtime TTS and Speech to Text

 https://platform.openai.com/docs/guides/realtime-websocket?websocket-examples=python

martes, 17 de diciembre de 2024

getting key from a dict based on a value

 >>> users

{'Hans': 'active', 'Éléonore': 'inactive', '景太郎': 'active'}

>>> [a for a in users.keys() if users.get(a)=='active']

['Hans', '景太郎']


jueves, 12 de diciembre de 2024

getting only str o int values from a dict

 >>> r

{'Name': 'Ambiorix', 'age': 34}

>>> {r[a]:a for a in r if type(r[a])==int}

{34: 'age'}

>>> {r[a]:a for a in r if type(r[a])==str}

{'Ambiorix': 'Name'}


martes, 10 de diciembre de 2024

dict practice

 >>> 

>>> 

>>> answer

'Ambiorix Rodriguez Placencio'

>>> chr(max({a:ord(a) for a in answer}.values()))

'z'

>>> chr(max({a:ord(a) for a in answer}.values()))