Fourth-week blog post for PSF, GSoC 2021
Tags: python, gsoc
Published: 2021-06-28 01:32:07

Once again, I'm writing this as part of my *Google Summer of Code* (GSoC) for *Python Software Foundation* (PSF). The third week has ended, and along with it I finished my [first milestone](https://github.com/LeoCumpli21/PyZombis/milestone/1)... Or at least that's what I thought. [Last post](https://dev.to/leocumpli21/third-week-check-in-for-gsoc-2021-4jon) I told you I was ready to tackle my [second milestone](https://github.com/LeoCumpli21/PyZombis/milestone/2), however, I forgot some lectures need to be checked as well. So, I did start my second milestone, but I had to pause it. Let the questions begin.

## What am I working on?

I worked on 2 different things. First, regarding my second milestone, I worked on two tickets. Before ellaborating on them, let me introduce you to what this milestone is about:
*It consists of refactoring Facebook's API lesson. The expected outcome is to have created new exercises with other APIs, public ones so that no special requirements are asked for.* 

* The [1st](https://github.com/PyAr/PyZombis/pull/113) ticket was a piece of cake. I just had to debug a block of code that involved using Reddit API. 
* The [2nd](https://github.com/LeoCumpli21/PyZombis/issues/11) one wasn't as easy. Here's why:
I had to create 3 exercises from scratch involving [TestDive API](https://tastedive.com/api/similar). Here's one of them:
```python
import requests
import json

api_url = "https://tastedive.com/api/similar"
proxy = "https://cors.bridged.cc/"
parametros = {"q": "Coco", "limit": 5, "info": 1}

solicitud = requests.get(proxy + api_url, params=parametros)
datos = json.loads(solicitud.text)

resultados = len(datos["Similar"]["Results"])
print(f"resultados: {resultados}")

peliculas_similares = []
for peli in datos["Similar"]["Results"]:
    peliculas_similares.append(peli["Name"])

print(f"Pelis: {peliculas_similares} len: {len(peliculas_similares)}")

pixar = 0
for peli in datos["Similar"]["Results"]:
    for pal in peli["wTeaser"].split():
        if pal == "Pixar":
            pixar += 1

print(f"Pixar: {pixar}")
```
I also added `unittest` for automatic grading to it. Apart from that, I made a web test with `playwright` for this same exercise. I learned about `page.keyboard` from [Playwright](https://playwright.dev/python/docs/api/class-keyboard/).
> Keyboard provides an api for managing a virtual keyboard. The high level api is keyboard.type(text, **kwargs), which takes raw characters and generates proper keydown, keypress/input, and keyup events on your page.

Before this, to press a key or type something I used:
``` python
page.press("text=def remplazar_primer_caracter(s):", "Tab")
page.type(
    "text=def remplazar_primer_caracter(s):",
    "return s[0] + s[1:].replace(s[0], '*')",
)
```
Notice the first argument, which is the "[selector](https://playwright.dev/docs/core-concepts/#selectors)", an element to tell playwright where to apply an event. Now, I just have to place the cursor where I want to start typing and, with the `keyboard` class, tell it what to do. For example:
```python
page.click("#ac_l45_5 >> text=parametros = {}")
# Clear all code
page.keyboard.press("Control+A")
page.keyboard.press("Backspace")
# Type something
page.keyboard.type("Hello world")
``` 

The second thing I worked on was fixing 2 lectures. 

## What have I struggled with this week?

Once again, git gave me trouble. I don't know why, but `git push` and all its variants decided to not work anymore. Regarding python, my major problem was when I wanted to clear all code with playwright using the `ctrl+A` & `backspace` keys. 

## What solutions have I found?

For the git problem, I had to install GitHub Desktop to push my local commits to my remote repo. For playwright doubts, I checked its documentation and found the `keyboard` class.

This is all for now :)