example code Flashcards

(12 cards)

1
Q

switching between asynchronous tasks

A

import asyncio

async def my.task(n):
> print(f”my.task: starting{n}”)
> await asyncio.sleep(n) # non-blocking delay

sync def sequential_main( ) :
> await my task ( 3 )
> await my task ( 1 )

async def gather_main( ) :
> await asyncio.gather( my_task ( 3 ) , my_task ( 1 ) )

  • import asyncio
  • async is a keyword for this function telling it to wait for other tasks to run for n seconds
  • “await” to run both tasks in order, wait for each to finish before moving on
  • call several tasks at once, not synchronous
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

await asyncio.sleep(n)
is what?

A

non-blocking delay; run something else, then check if this finished execution

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

declarative react example: return JSX display

A

import { useState } from “react”;

export default function App() {
const [count, setCount] = useState(0);

return (
> <div>

<div>
> <span>Count: {count}</span>
> {count > 5 ? <b> Yeah!</b> : <i> Click away...</i>}
</div>

> <button onClick={() => setCount(c => c + 1)}>Increment</button> </div>
);
}

  • import useState hook
  • define App, which can be exported from the file to move elsewhere
  • const [count, setCount] = useState(0);
  • return JSX display
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

ES6 class, model of car

A

class Model extends Car {
constructor(name, mod) {
super(name);
this.model = mod;
}
show() {
return this.present() + ‘, it is a ‘ + this.model
}
}
const mycar = new Model(“Ford”, “Model T”);
mycar.show();

  • class Model extends Car
  • constructor with all args needed
  • super; get name from extending Car class
  • this.model for instance of class Model
  • define and call show()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

find element with the id root, store in the variable “container”

A

const container = document.getElementById(‘root’);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

implement runnable

A

public class MyRunnable implements Runnable {

@Override
public void run() {
    System.out.println("Thread is running...");
}

public static void main(String[] args) {
    Thread thread = new Thread(new MyRunnable());
    thread.start();  // starts the new thread
} }

  • public class myrunnable implements the built-in interface runnable
  • override run method, output message
  • public static void main(String[] args)
  • Thread thread to instantiate new thread
  • thread.start to begin executing a new thread
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

extend thread

A

class MyThread extends Thread{

@Override
public void run(){
    
    System.out.println("Thread is running");
} }

public class Main {

public static void main(String[] args){
    
    MyThread t1 = new MyThread();
    MyThread t2 = new MyThread();
    
    t1.start();  
    t2.start();  
} }

  • MyThread extends the class Thread
  • extend (public void) run() to execute a message when it runs
  • create thread objects in Main class main(String[] args)
  • run() or t.start();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

value of container (root) becomes React object in DOM

A

const root = ReactDOM.createRoot(container);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

render the root React object

A

root.render(<p>Hello</p>);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

imperative javascript

A

for (let i = 0; i < numbers.length; i++) {
if (numbers[i] > 10) {
result.push(numbers[i]);
}
}

tells the system how to do something step by step

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

functional test

A

class LoginFunctionalTest {

private WebDriver driver;

@BeforeEach
void setUp() {
    driver = new ChromeDriver();
    driver.get("https://example.com/login");
}

@Test
void testLoginSuccess() {
    WebElement usernameField = driver.findElement(By.id("username"));
    WebElement passwordField = driver.findElement(By.id("password"));
    WebElement loginButton   = driver.findElement(By.id("login-btn"));

    usernameField.sendKeys("user1");
    passwordField.sendKeys("secret123");
    loginButton.click();

    WebElement welcomeMessage = driver.findElement(By.id("welcome"));
    Assertions.assertTrue(welcomeMessage.isDisplayed());
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

unit test

A

class CalculatorTest {

@Test
void testAdd() {
    Calculator calc = new Calculator();
    int result = calc.add(2, 3);
    assertEquals(expectedresult, result);
} }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly