switching between asynchronous tasks
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 ) )
–
await asyncio.sleep(n)
is what?
non-blocking delay; run something else, then check if this finished execution
declarative react example: return JSX display
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>
);
}
–
ES6 class, model of car
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();
–
find element with the id root, store in the variable “container”
const container = document.getElementById(‘root’);
implement runnable
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
} }–
extend thread
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();
} }–
value of container (root) becomes React object in DOM
const root = ReactDOM.createRoot(container);
render the root React object
root.render(<p>Hello</p>);
imperative javascript
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
functional test
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());
}unit test
class CalculatorTest {
@Test
void testAdd() {
Calculator calc = new Calculator();
int result = calc.add(2, 3);
assertEquals(expectedresult, result);
} }