问:
Nodejs global variables change unpredictably in response to the requests
I am working on an app that needs to count the number of users that are viewing a webpage at the same time. To do that, I created a React component that is responsible for the page display, and every time when it loads it sends a request to the Node.JS backend whose sole purpose is to keep track of the amount of users. Every time the page loads, it sends the get request to Node to get the amount of users and displays it as a state, and every time the user leaves the page the event listener must send a post request to decrement the value of variable by one. This is what I have so far:

server.js (back-end)

//This is a small backend that keeps track of how many people are on each page.
const express = require('express')
const cors = require('cors')
const app = express()

const whiteList = ['http://localhost:3000', 'http://127.0.0.1:3000']
const corsOptions = {
    origin: function (origin, callback) {
      if (!origin || whiteList.indexOf(origin) !== -1)
          callback(null, true)
      else
        callback(new Error("Not allowed by CORS"))
    },
    credentials: true,
  }

app.use(cors(corsOptions))
let googleCounter = 0, discordCounter = 0, linuxCounter = 0
console.log(googleCounter, discordCounter, linuxCounter)
app.get("/google", (request, response) => {
    console.log(googleCounter, discordCounter, linuxCounter)
    console.log(`Google counter ${googleCounter} increased to ${googleCounter + 1}`)
    googleCounter++
    response.send(`Google Counter: ${googleCounter}`)
})

app.get("/discord", (request, response) => {
    discordCounter++
    response.send(discordCounter)
})

app.get("/linux", (request, response) => {
    linuxCounter++
    response.send(linuxCounter)
})

//These methods will fire when users leave the pages.
app.post("/google", () => {
    console.log(`Google counter ${googleCounter} decreased to ${googleCounter - 1}`)
    googleCounter--})
app.post("/discord", () => discordCounter--)
app.post("/linux", () => linuxCounter--)

app.listen(5000, () => console.log(`Server started on port 5000: ${googleCounter}, ${discordCounter}, ${linuxCounter}`))
```
<p><strong>Google.js</strong> (the front-end)</p>
<pre class="lang-js prettyprint-override"><code>//This is a generic page to view. Source: https://uk.wikipedia.org/wiki/Google
import React, {useState, useEffect} from "react"
import Base from "./Base"

export default function Google(props) {
    const url = "http://localhost:5000/google"
    const [counter, setCounter] = useState(1)
    //Increment user counter when the page is loaded:
    useEffect(() => {
        if (!props.user) return;
        fetch(url, {method: "GET"})
            .then(response => {return response.text()})
            .then(response => console.log(response))
            .catch(error => console.log(error))
         //Decrement user counter when the user leaves the page:
         window.addEventListener("unload", () => fetch(url, {method: "POST"})
            .then(response => {return response.text()})
            .then(response => console.log(response))
            .then(() => console.log("User left the page")))
            .catch(error => console.log(error))
    }, [props.user])

    if (!props.user) return (<div><Base/></div>)
    return (<div>
        <h1>Google</h1>
        <h2>Currently active users: {counter}</h2>
    </div>)
}
</code>```

My issue is that the googleCounter variable behaves unpredictably: when I run the server, it immediately falls to -3 to -5, and every time I send the get request it increments the variable but also decrements. I suspect the unload event could run more often than when user leaves the page, useEffect could have run more often or the get request could overlap with the post request in some way. When I tried to log the post request from the client-side, I've got the event listener is undefined:

How can I make sure that the googleCounter variable only decreases when the user leaves the page?

Body:
I am working on an app that needs to count the number of users that are viewing a webpage at the same time. To do that, I created a React component that is responsible for the page display, and every time when it loads it sends a request to the Node.JS backend whose sole purpose is to keep track of the amount of users. Every time the page loads, it sends the get request to Node to get the amount of users and displays it as a state, and every time the user leaves the page the event listener must send a post request to decrement the value of variable by one. This is what I have so far:

server.js (back-end)

//This is a small backend that keeps track of how many people are on each page.
const express = require('express')
const cors = require('cors')
const app = express()

const whiteList = ['http://localhost:3000', 'http://127.0.0.1:3000']
const corsOptions = {
    origin: function (origin, callback) {
      if (!origin || whiteList.indexOf(origin) !== -1)
          callback(null, true)
      else
        callback(new Error("Not allowed by CORS"))
    },
    credentials: true,
  }

app.use(cors(corsOptions))
let googleCounter = 0, discordCounter = 0, linuxCounter = 0
console.log(googleCounter, discordCounter, linuxCounter)
app.get("/google", (request, response) => {
    console.log(googleCounter, discordCounter, linuxCounter)
    console.log(`Google counter ${googleCounter} increased to ${googleCounter + 1}`)
    googleCounter++
    response.send(`Google Counter: ${googleCounter}`)
})

app.get("/discord", (request, response) => {
    discordCounter++
    response.send(discordCounter)
})

app.get("/linux", (request, response) => {
    linuxCounter++
    response.send(linuxCounter)
})

//These methods will fire when users leave the pages.
app.post("/google", () => {
    console.log(`Google counter ${googleCounter} decreased to ${googleCounter - 1}`)
    googleCounter--})
app.post("/discord", () => discordCounter--)
app.post("/linux", () => linuxCounter--)

app.listen(5000, () => console.log(`Server started on port 5000: ${googleCounter}, ${discordCounter}, ${linuxCounter}`))

Google.js (the front-end)

//This is a generic page to view. Source: https://uk.wikipedia.org/wiki/Google
import React, {useState, useEffect} from "react"
import Base from "./Base"

export default function Google(props) {
    const url = "http://localhost:5000/google"
    const [counter, setCounter] = useState(1)
    //Increment user counter when the page is loaded:
    useEffect(() => {
        if (!props.user) return;
        fetch(url, {method: "GET"})
            .then(response => {return response.text()})
            .then(response => console.log(response))
            .catch(error => console.log(error))
         //Decrement user counter when the user leaves the page:
         window.addEventListener("unload", () => fetch(url, {method: "POST"})
            .then(response => {return response.text()})
            .then(response => console.log(response))
            .then(() => console.log("User left the page")))
            .catch(error => console.log(error))
    }, [props.user])

    if (!props.user) return (
<Base/>
)
    return (

Google

Currently active users: {counter}

)
}

My issue is that the googleCounter variable behaves unpredictably: when I run the server, it immediately falls to -3 to -5, and every time I send the get request it increments the variable but also decrements. I suspect the unload event could run more often than when user leaves the page, useEffect could have run more often or the get request could overlap with the post request in some way. When I tried to log the post request from the client-side, I've got the event listener is undefined:

How can I make sure that the googleCounter variable only decreases when the user leaves the page?


来源: stackexchange

问:
const err = new MongooseError(message);
i was making an api but i got here an error

the error:

```<code>const err = new MongooseError(message);
</code>```

full error:

```<code>MongooseError: Operation `users.insertOne()` buffering timed out after 10000ms
    at Timeout.<anonymous> (C:\path%\path%\path%\path%\path\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:158:23)
</code>```

Please help me i cant solve this error

ContentLicense: CC BY-SA 4.0

Title: const err = new MongooseError(message);

Body:
i was making an api but i got here an error

the error:

const err = new MongooseError(message);

full error:

MongooseError: Operation `users.insertOne()` buffering timed out after 10000ms
    at Timeout.<anonymous> (C:\path%\path%\path%\path%\path\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:158:23)

Please help me i cant solve this error


问:
server shows "page not found-404" error while verifying password
I am trying to create a password verification project but when I try to run that, it keeps getting 404 error while it should be redirect on my login page or password do not match error. Although i get the verification email and get the link, when i click the link and try to reset my password, this error comes

```<code>Not Found: /change-password/263d4b70-c6db-4341-9b56-924d3c9000e6/change-password/263d4b70-c6db-4341-9b56-924d3c9000e6/
</code>```

can anyone tell me what I am missing here? It would be very helpful

Here's a snippet of my code, thanks in advance

```<code>urls.py

from django.urls import path
from .views import *

urlpatterns = [
   path('', home, name='home' ),
   path('login/', login_attempt, name='login' ),
   path('register',register_attempt, name='register'),
   path('logout_user', logout_attempt, name='logout_user'),
   path('forgetpassword', forgetPassword, name='forgetpassword'),
   path('change-password/<token>/', changePassword, name='changepassword'),
]
</code>```
```<code>views.py

from django.shortcuts import render,redirect
from django.contrib.auth import authenticate
from django.contrib import auth
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from epayapp.helpers import send_forget_password_mail
from .models import *
import uuid

# Create your views here.

def forgetPassword(request):
    try:
        if request.method == 'POST':
            username= request.POST.get('username')

            curr_user= User.objects.filter(username= username).first()

            if curr_user is None:
                messages.success(request, 'Username not found')
                return redirect('forgetpassword')

            user_obj= User.objects.get(username=username)
            token= str(uuid.uuid4())
            user_obj.forget_password_token= token
            user_obj.save()
            send_forget_password_mail(user_obj, token)
            messages.success(request, 'An email is sent')
            return redirect('forgetpassword')

    except Exception as e:
        print(e)
    return render(request, 'index/forget_password.html')

def changePassword(request, token):
    context= {}

    try:
        user_obj= User.objects.filter(forget_password_token=token).first()
        context={'user_id': user_obj.id}

        if request.method == 'POST':
            new_password= request.POST.get('password')
            confirm_password= request.POST.get('confirmpassword')
            user_id= request.POST.get('user_id')

            if user_id is None:
                messages.success(request, 'No user found')
                return redirect(f'change-password/{token}/')

            if new_password != confirm_password :
                messages.success(request, 'Password does not match')
                return redirect(f'change-password/{token}/')

            curr_user = User.objects.get(id= user_id)
            curr_user.set_password(new_password)
            curr_user.save()
            return redirect('login/')

    except Exception as e:
        print (e)
    return render(request, 'index/change-password.html',context)
</code>```
```<code>helps.py

from django.core.mail import send_mail
import uuid
from django.conf import settings

def send_forget_password_mail(email, token):
    subject= 'Reset Your Password'
    message= f'click this link to reset your password http://127.0.0.1:8000/change-password/{token}/'
    email_from= settings.EMAIL_HOST_USER
    recipient_list= [email]
    send_mail(subject, message, email_from, recipient_list)

    return True

</code>```

Body:
I am trying to create a password verification project but when I try to run that, it keeps getting 404 error while it should be redirect on my login page or password do not match error. Although i get the verification email and get the link, when i click the link and try to reset my password, this error comes

Not Found: /change-password/263d4b70-c6db-4341-9b56-924d3c9000e6/change-password/263d4b70-c6db-4341-9b56-924d3c9000e6/

can anyone tell me what I am missing here? It would be very helpful

Here's a snippet of my code, thanks in advance

urls.py

from django.urls import path
from .views import *

urlpatterns = [
   path('', home, name='home' ),
   path('login/', login_attempt, name='login' ),
   path('register',register_attempt, name='register'),
   path('logout_user', logout_attempt, name='logout_user'),
   path('forgetpassword', forgetPassword, name='forgetpassword'),
   path('change-password/<token>/', changePassword, name='changepassword'),
]

views.py

from django.shortcuts import render,redirect
from django.contrib.auth import authenticate
from django.contrib import auth
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from epayapp.helpers import send_forget_password_mail
from .models import *
import uuid

# Create your views here.

def forgetPassword(request):
    try:
        if request.method == 'POST':
            username= request.POST.get('username')

            curr_user= User.objects.filter(username= username).first()

            if curr_user is None:
                messages.success(request, 'Username not found')
                return redirect('forgetpassword')

            user_obj= User.objects.get(username=username)
            token= str(uuid.uuid4())
            user_obj.forget_password_token= token
            user_obj.save()
            send_forget_password_mail(user_obj, token)
            messages.success(request, 'An email is sent')
            return redirect('forgetpassword')

    except Exception as e:
        print(e)
    return render(request, 'index/forget_password.html')

def changePassword(request, token):
    context= {}

    try:
        user_obj= User.objects.filter(forget_password_token=token).first()
        context={'user_id': user_obj.id}

        if request.method == 'POST':
            new_password= request.POST.get('password')
            confirm_password= request.POST.get('confirmpassword')
            user_id= request.POST.get('user_id')

            if user_id is None:
                messages.success(request, 'No user found')
                return redirect(f'change-password/{token}/')

            if new_password != confirm_password :
                messages.success(request, 'Password does not match')
                return redirect(f'change-password/{token}/')

            curr_user = User.objects.get(id= user_id)
            curr_user.set_password(new_password)
            curr_user.save()
            return redirect('login/')

    except Exception as e:
        print (e)
    return render(request, 'index/change-password.html',context)

helps.py

from django.core.mail import send_mail
import uuid
from django.conf import settings

def send_forget_password_mail(email, token):
    subject= 'Reset Your Password'
    message= f'click this link to reset your password http://127.0.0.1:8000/change-password/{token}/'
    email_from= settings.EMAIL_HOST_USER
    recipient_list= [email]
    send_mail(subject, message, email_from, recipient_list)

    return True


问:
How catboost overfitting detector works with multiple eval datasets?
Catboost model can use several eval_sets for detecting overfit.

But how overfitting-detector works when multiple datasets are passed?

Read docs, googled it. I thought I'd find something, but I didn't...


问:
Call Nested Function
This might have already been covered before but I am having issues calling a nested function for my discord bot. It is meant to count down from 3 to 0 & display outputs to the user at each stage. I have the Async commands working so figured I would just use print to test that's why none of that is visible.

If anyone can see an issue with my code could you please let me know as I'm hitting a brick wall.

```<code>def count(countfunc):

def wrapper(*args, **kwargs):

    wrapper.counter = 3
    wrapper.start = 3

    wrapper.counter -= 1
    if wrapper.counter == 0:
        print(f"has ran out of warnings & is now banned on the server for 1 day")
    else:
        print(f"has", wrapper.counter ,"warnings left")
    return countfunc(*args, **kwargs)

return wrapper("*args, **kwargs")

@count def countfunc(wrapper):
    return

print (countfunc)
</code>```

I am trying to call the print values from the wrapper  if statement. This works when called within the count function/wrapper function but when called outside of it just says "None". I'm gathering that this is due to it not being able to read the function values but not 100% sure how to fix it.

Many Thanks In Advance,
Ben

Title: Call Nested Function

Body:
This might have already been covered before but I am having issues calling a nested function for my discord bot. It is meant to count down from 3 to 0 & display outputs to the user at each stage. I have the Async commands working so figured I would just use print to test that's why none of that is visible.

If anyone can see an issue with my code could you please let me know as I'm hitting a brick wall.

def count(countfunc):

def wrapper(*args, **kwargs):

    wrapper.counter = 3
    wrapper.start = 3

    wrapper.counter -= 1
    if wrapper.counter == 0:
        print(f"has ran out of warnings & is now banned on the server for 1 day")
    else:
        print(f"has", wrapper.counter ,"warnings left")
    return countfunc(*args, **kwargs)

return wrapper("*args, **kwargs")

@count def countfunc(wrapper):
    return

print (countfunc)

I am trying to call the print values from the wrapper  if statement. This works when called within the count function/wrapper function but when called outside of it just says "None". I'm gathering that this is due to it not being able to read the function values but not 100% sure how to fix it.

Many Thanks In Advance,
Ben


问:
Playing recorded audio results in ACMP4AACBaseDecoder failing
I'm playing around with a really simple audio use case. I'm recording via AVAudioRecorder and playing the recorded audio via AVAudioPlayer. The way I configured it is to set up the AVAudioRecorder once when the view controller is presented and record over the same file whenever record is initiated. Playing will play the url used by the recorder. The first recording and play back work fine. However if I try to record and play again I get the following:

```<code>2022-12-30 10:39:09.874066-0800 [40936:5138438]  Error deserializing gain control data
2022-12-30 10:39:09.874246-0800 [40936:5138438]  Error deserializing right channel stream
2022-12-30 10:39:09.874443-0800 [40936:5138438]  Error in deserializing element
2022-12-30 10:39:09.874545-0800 [40936:5138438]  Error deserializing packet
2022-12-30 10:39:09.874675-0800 [40936:5138438] [ac]   ACMP4AACBaseDecoder.cpp:1438  (0x127850040) Error decoding packet 1: err = -1, packet length: 198
2022-12-30 10:39:09.874781-0800 [40936:5138438] [ac]   ACMP4AACBaseDecoder.cpp:1447  '21 4E E5 44 87 E2 FC 62 0A 9B 64 42 80 D5 4E 01 17 D0 9D 47 CF FA E0 91 D2 5C FD 28 8B 3E 04 3A FD 71 F0 43 6D 20 48 52 EB 6C 73 7D 30 98 41 CC 12 F9 27 C3 10 60 4F 67 41 F6 4F 59 F1 A6 45 A4 B4 87 6A 4F 34 9C 26 CF 03 9A 9C 8D 20 14 6E 72 E6 C5 08 A6 BC D7 3B DE 3D 7F 61 1A 23 38 EC 95 1E 71 C8 1A D4 F1 B0 6E ED 9C 53 D3 FD 0E E1 5B AA CD 83 8F D3 0D 40 D7 D1 94 B4 F5 A8 1A 13 4D CE E2 37 69 14 FA E9 01 A2 50 2C AE 9C 75 49 04 06 8B 6F 01 23 20 0D 3E F1 5B 6F 47 D3 F3 F3 8A 06 E9 C5 AB 34 84 28 21 C7 4D DA 47 BE 9E 1A D5 6A B8 74 B2 EA 21 1D 51 0B 2D CE 1C 3F 01 F8 0F C0 7E 10 EB F5 C7'
2022-12-30 10:39:09.876272-0800 [40936:5138438]  Too few bits left in input buffer
</code>```

I'm not very knowledgable about how audio works beneath the hood but I figured this is pretty basic audio stuff.

How I set up the recorder (only done once when VC is loaded):

```<code>func setupAudioRecorder() {
    // Set the audio file
    let audioFileURL = getFileUrl()

    // Setup audio session
    let audioSession = AVAudioSession.sharedInstance()
    do {
        try audioSession.setCategory(.playAndRecord, mode: .default, options: [])
    } catch _ {
    }

    // Define the recorder setting
    let recorderSetting = [AVFormatIDKey: NSNumber(value: kAudioFormatMPEG4AAC as UInt32),
                           AVSampleRateKey: 44100.0,
                           AVNumberOfChannelsKey: 2 ]

    audioRecorder = try? AVAudioRecorder(url: audioFileURL, settings: recorderSetting)
    audioRecorder?.delegate = self
    audioRecorder?.isMeteringEnabled = true
    audioRecorder?.prepareToRecord()
}

func getDocumentsDirectory() -> URL
{
    let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
    let documentsDirectory = paths[0]
    return documentsDirectory
}

func getFileUrl() -> URL
{
    let timeInterval = Int(NSDate().timeIntervalSince1970*1000)
    let audioFileName = "version_" + String(timeInterval) + ".m4a"
    let filePath = getDocumentsDirectory().appendingPathComponent(audioFileName)
    return filePath
}
</code>```

Start recording:

```<code>func startRecording() {
    if let recorder = audioRecorder {
        if !recorder.isRecording {
            let audioSession = AVAudioSession.sharedInstance()

            do {
                try audioSession.setActive(true)
            } catch _ {
            }

            // Start recording
            recorder.record()
            print("RECORD " + recorder.url.absoluteString)
        }
    }
}
</code>```

How I set up the player (called every time user hits play):

```<code>func setUpPlayer() {
    guard let _ = self.audioPlayer  else {
        self.audioPlayer = try? AVAudioPlayer(contentsOf: self.audioRecorder!.url)
        self.audioPlayer?.delegate = self
        setAudioPlayerToUseSpeaker()
        return
    }
}
</code>```

Toggle player:

```<code>func toggleAudioPlayer() {
    if let audioPlayer = self.audioPlayer {
        if (audioPlayer.isPlaying) {
            audioPlayer.stop()
        } else {
            audioPlayer.play()
        }
    }
}
</code>```

Body:
I'm playing around with a really simple audio use case. I'm recording via AVAudioRecorder and playing the recorded audio via AVAudioPlayer. The way I configured it is to set up the AVAudioRecorder once when the view controller is presented and record over the same file whenever record is initiated. Playing will play the url used by the recorder. The first recording and play back work fine. However if I try to record and play again I get the following:

2022-12-30 10:39:09.874066-0800 [40936:5138438]  Error deserializing gain control data
2022-12-30 10:39:09.874246-0800 [40936:5138438]  Error deserializing right channel stream
2022-12-30 10:39:09.874443-0800 [40936:5138438]  Error in deserializing element
2022-12-30 10:39:09.874545-0800 [40936:5138438]  Error deserializing packet
2022-12-30 10:39:09.874675-0800 [40936:5138438] [ac]   ACMP4AACBaseDecoder.cpp:1438  (0x127850040) Error decoding packet 1: err = -1, packet length: 198
2022-12-30 10:39:09.874781-0800 [40936:5138438] [ac]   ACMP4AACBaseDecoder.cpp:1447  '21 4E E5 44 87 E2 FC 62 0A 9B 64 42 80 D5 4E 01 17 D0 9D 47 CF FA E0 91 D2 5C FD 28 8B 3E 04 3A FD 71 F0 43 6D 20 48 52 EB 6C 73 7D 30 98 41 CC 12 F9 27 C3 10 60 4F 67 41 F6 4F 59 F1 A6 45 A4 B4 87 6A 4F 34 9C 26 CF 03 9A 9C 8D 20 14 6E 72 E6 C5 08 A6 BC D7 3B DE 3D 7F 61 1A 23 38 EC 95 1E 71 C8 1A D4 F1 B0 6E ED 9C 53 D3 FD 0E E1 5B AA CD 83 8F D3 0D 40 D7 D1 94 B4 F5 A8 1A 13 4D CE E2 37 69 14 FA E9 01 A2 50 2C AE 9C 75 49 04 06 8B 6F 01 23 20 0D 3E F1 5B 6F 47 D3 F3 F3 8A 06 E9 C5 AB 34 84 28 21 C7 4D DA 47 BE 9E 1A D5 6A B8 74 B2 EA 21 1D 51 0B 2D CE 1C 3F 01 F8 0F C0 7E 10 EB F5 C7'
2022-12-30 10:39:09.876272-0800 [40936:5138438]  Too few bits left in input buffer

I'm not very knowledgable about how audio works beneath the hood but I figured this is pretty basic audio stuff.

How I set up the recorder (only done once when VC is loaded):

func setupAudioRecorder() {
    // Set the audio file
    let audioFileURL = getFileUrl()

    // Setup audio session
    let audioSession = AVAudioSession.sharedInstance()
    do {
        try audioSession.setCategory(.playAndRecord, mode: .default, options: [])
    } catch _ {
    }

    // Define the recorder setting
    let recorderSetting = [AVFormatIDKey: NSNumber(value: kAudioFormatMPEG4AAC as UInt32),
                           AVSampleRateKey: 44100.0,
                           AVNumberOfChannelsKey: 2 ]

    audioRecorder = try? AVAudioRecorder(url: audioFileURL, settings: recorderSetting)
    audioRecorder?.delegate = self
    audioRecorder?.isMeteringEnabled = true
    audioRecorder?.prepareToRecord()
}

func getDocumentsDirectory() -> URL
{
    let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
    let documentsDirectory = paths[0]
    return documentsDirectory
}

func getFileUrl() -> URL
{
    let timeInterval = Int(NSDate().timeIntervalSince1970*1000)
    let audioFileName = "version_" + String(timeInterval) + ".m4a"
    let filePath = getDocumentsDirectory().appendingPathComponent(audioFileName)
    return filePath
}

Start recording:

func startRecording() {
    if let recorder = audioRecorder {
        if !recorder.isRecording {
            let audioSession = AVAudioSession.sharedInstance()

            do {
                try audioSession.setActive(true)
            } catch _ {
            }

            // Start recording
            recorder.record()
            print("RECORD " + recorder.url.absoluteString)
        }
    }
}

How I set up the player (called every time user hits play):

func setUpPlayer() {
    guard let _ = self.audioPlayer  else {
        self.audioPlayer = try? AVAudioPlayer(contentsOf: self.audioRecorder!.url)
        self.audioPlayer?.delegate = self
        setAudioPlayerToUseSpeaker()
        return
    }
}

Toggle player:

func toggleAudioPlayer() {
    if let audioPlayer = self.audioPlayer {
        if (audioPlayer.isPlaying) {
            audioPlayer.stop()
        } else {
            audioPlayer.play()
        }
    }
}


问:
I'm trying to declare constants with a dynamically generated name
So, I wanted to automate some of my coding process. I figured maybe I don't have to manually name all the constants that refer to classes, and have a function do that. Here's my code, which, I feel, should work. Of course it doesn't. Should it? If not, why not?</p>
```<code>function definitions() {
    var allElem = document.querySelectorAll("*"); //Get every HTML element.

    for(a = 0; a < allElem.length; a++) { //Iterate through every HTML element.

        if (allElem[a].classList.length == 0) { //If an element has no class...

            console.log("no var declared, " + a); //...ignore it.

        } else { //If it has one or more classes...

            for(b = 0; b < allElem[a].classList.length; b++) { //...iterate through the classList.
                eval("const " + allElem[a].classList[b] + " = " + "document.getElementsByClassName('" + allElem[a].classList[b] + "');"); //Declare a constant named for the class if refers to
                console.log("const " + allElem[a].classList[b] + " = " + "document.getElementsByClassName('" + allElem[a].classList[b] + "');");
            }
        }
    }
    console.log(document.getElementsByClassName('clone').length); //This logs 10, which is correct.
    console.log(clone.length); // 'Uncaught ReferenceError: clone is not defined'. But... but... I thought I did... :'(
}
</code>```

Body:
So, I wanted to automate some of my coding process. I figured maybe I don't have to manually name all the constants that refer to classes, and have a function do that. Here's my code, which, I feel, should work. Of course it doesn't. Should it? If not, why not?

function definitions() {
    var allElem = document.querySelectorAll("*"); //Get every HTML element.

    for(a = 0; a < allElem.length; a++) { //Iterate through every HTML element.

        if (allElem[a].classList.length == 0) { //If an element has no class...

            console.log("no var declared, " + a); //...ignore it.

        } else { //If it has one or more classes...

            for(b = 0; b < allElem[a].classList.length; b++) { //...iterate through the classList.
                eval("const " + allElem[a].classList[b] + " = " + "document.getElementsByClassName('" + allElem[a].classList[b] + "');"); //Declare a constant named for the class if refers to
                console.log("const " + allElem[a].classList[b] + " = " + "document.getElementsByClassName('" + allElem[a].classList[b] + "');");
            }
        }
    }
    console.log(document.getElementsByClassName('clone').length); //This logs 10, which is correct.
    console.log(clone.length); // 'Uncaught ReferenceError: clone is not defined'. But... but... I thought I did... :'(
}


问:
Can I access Faceid in android in React native ? I have tried already "react-native-touch-id": "^4.4.1",
I am trying to access faceId in react native in android 11
and I already have tried  "react-native-touch-id": "^4.4.1", but I could not acheive this. This library gives only touchId true or false promise</p>


问:
Use the ConvNext classifier layer
Inside the source code of the Convnext:

self.classifier = nn.Sequential(
    norm_layer(lastconv_output_channels), nn.Flatten(1), nn.Linear(lastconv_output_channels, num_classes)
)
```
<p>When I call the pretrained model and I want to change in the classifier layer <code>norm_layer</code> is not defined and I don't know how to use it from the source code</p>
<pre class="lang-py prettyprint-override"><code>model = torchvision.models.convnext_base(prtrained=True, stochastic_depth_prob=0.1,
                                         layer_scale=1e-4)

model.classifier = nn.Sequential(
   ** norm_layer**(1024), nn.Flatten(1), nn.Linear(1024, 7)
)
</code>```

Please anyone can help to write it correctly ?

Title: Use the ConvNext classifier layer

Body:
Inside the source code of the Convnext:

self.classifier = nn.Sequential(
    norm_layer(lastconv_output_channels), nn.Flatten(1), nn.Linear(lastconv_output_channels, num_classes)
)

When I call the pretrained model and I want to change in the classifier layer norm_layer is not defined and I don't know how to use it from the source code

model = torchvision.models.convnext_base(prtrained=True, stochastic_depth_prob=0.1,
                                         layer_scale=1e-4)

model.classifier = nn.Sequential(
   ** norm_layer**(1024), nn.Flatten(1), nn.Linear(1024, 7)
)

Please anyone can help to write it correctly ?


问:
How can I send a GET request in Akka (Java) and obtain the JSON body of the response?
I have been struggling to simply perform a GET request to the Spotify API and parse the results of the request. I haven't been able to successfully do it despite trying to follow the docs. I am trying to achieve this within an Actor. I have tried making the HttpEntity a strict HttpEntity but this returns an error. I am not sure if this is due to dependency issues or if it is simply the incorrect approach. What I would like to do is obtain the entire payload of the response and parse it to then return it to another actor. I am unsure what should go inside of the try block to achieve this. Thanks very much.

```<code>
ActorSystem actorSystem = getContext().getSystem();
Http http = Http.get(actorSystem);
String endpoint = "https://api.spotify.com/v1/artists/1moxjboGR7GNWYIMWsRjgG/top-tracks?market=IE";
Authorization authorization = Authorization.oauth2("REDACTED");
HttpRequest request = HttpRequest.create().withUri(endpoint).addHeader(authorization);
CompletionStage<HttpResponse> responseFuture = http.singleRequest(request)
responseFuture.thenAccept(response -> {
    try {
}

</code>```

Have tried casting entity to a strict entity but this results in an error.

Body:
I have been struggling to simply perform a GET request to the Spotify API and parse the results of the request. I haven't been able to successfully do it despite trying to follow the docs. I am trying to achieve this within an Actor. I have tried making the HttpEntity a strict HttpEntity but this returns an error. I am not sure if this is due to dependency issues or if it is simply the incorrect approach. What I would like to do is obtain the entire payload of the response and parse it to then return it to another actor. I am unsure what should go inside of the try block to achieve this. Thanks very much.

ActorSystem actorSystem = getContext().getSystem();
Http http = Http.get(actorSystem);
String endpoint = "https://api.spotify.com/v1/artists/1moxjboGR7GNWYIMWsRjgG/top-tracks?market=IE";
Authorization authorization = Authorization.oauth2("REDACTED");
HttpRequest request = HttpRequest.create().withUri(endpoint).addHeader(authorization);
CompletionStage<HttpResponse> responseFuture = http.singleRequest(request)
responseFuture.thenAccept(response -> {
    try {
}

Have tried casting entity to a strict entity but this results in an error.


问:
pymongo - Upsert collection to add object with nested objects and nested data
I have spent weeks researching, finally asking the question....

Using python/pymongo I am stuck at the simple step of just adding a nested object with nested data "to an existing object or nested object"...so I have not even made it to the upsert yet.  I "have" code that confirms if the collection "serial_number+ exists, if not then create it -- however I am stuck at adding nested objects to an existing or newly created collection.  Looking for an example to build from, not for this forum to solve the full solution :-)

Attached is a screenshot of the desired layout.  Use case, several cars are available to be rented

MongoDB Database with a Collection that is a car's serial number and all activities related to that car
a.  Need to capture details of each rental activity - nested data under a each serial number.
b.  Need to capture maintenance details for each car - nested data under each serial number.

When either "a" or "b" occur, need to check that the record is not already there, if not then add a new nested object that contains nested data.

I have tried for loops, numerous postings referring to collections and sub-collections -- all seem to require highly detailed information within code to parse the dictionary (which comes from semi-structed sources, not consistent) and add to a collection....but cannot find a starting point to add to an existing object or nested object within an existing collection?

```<code>#Assume mongodb is structured like this:
#rent_activity(DB)>vehicle(document)>serial_number(object)>car_details(object under serial_number)
#rent_activity(DB)>vehicle(document)>serial_number(object)>rental_activity(object under serial_number)
#rent_activity(DB)>vehicle(document)>serial_number(object)>maintenance(object under serial_number)
#The example below is attempting to insert new objects with nested arrays to the car_details object
import os,sys
import pymongo
import csv
import boto3
import time
import psycopg2
import pandas as pd
import pandas.io.sql as sqlio
from datetime import datetime
from pymongo import errors

my_connection = psycopg2.connect(dbname='xxx', host='xxx', port=xxx, user='xxx', password='xxx')

client = pymongo.MongoClient("xxxxxxx")
mydb = client["rent_activity"] #This is the mongodb database
collection = mydb["vehicle"]

car_details_sql = "SELECT serial_number, mileage, fuel_level other from car_details"
data = sqlio.read_sql_query(car_details_sql, my_connection) #Create dataframe from query results

try:
    #Goal is to add each line from the dataframe result returned from the SQL queary, as a record under collection "vehicle", object "serial_number", nested object "car_details"
    for item in data.to_dict(orient="records"): #For each row, transform to dict
        mycarkey = str(item.get("purchase_date")) + "_" + str(item.get("serial_number")) #Create unique string
        car_number = "car_" + mycarkey
        finaldata = {}#Create empty
        finaldata[mycarkey] = item #Add outer unique key to existing dict for this set of data
        finaldata.update({"_id": car_number}) #Define custom object _id for this set of data
        collection.insert_one(finaldata)
            #This inserts to the collection "vehicle"
            #What is needed is to nest this to:
            #   rent_activity(DB)>vehicle(document)>serial_number(object)>car_details(object under serial_number)
            #NOTE: If I simply add another outer key "serial_number" I receive error key already exists
            #..so the issue is how to add an object with nested values beneath an existing object??
except Exception as e:
    raise e
</code>```

Body:
I have spent weeks researching, finally asking the question....

Using python/pymongo I am stuck at the simple step of just adding a nested object with nested data "to an existing object or nested object"...so I have not even made it to the upsert yet.  I "have" code that confirms if the collection "serial_number+ exists, if not then create it -- however I am stuck at adding nested objects to an existing or newly created collection.  Looking for an example to build from, not for this forum to solve the full solution :-)

Attached is a screenshot of the desired layout.  Use case, several cars are available to be rented

MongoDB Database with a Collection that is a car's serial number and all activities related to that car
a.  Need to capture details of each rental activity - nested data under a each serial number.
b.  Need to capture maintenance details for each car - nested data under each serial number.

When either "a" or "b" occur, need to check that the record is not already there, if not then add a new nested object that contains nested data.

I have tried for loops, numerous postings referring to collections and sub-collections -- all seem to require highly detailed information within code to parse the dictionary (which comes from semi-structed sources, not consistent) and add to a collection....but cannot find a starting point to add to an existing object or nested object within an existing collection?

#Assume mongodb is structured like this:
#rent_activity(DB)>vehicle(document)>serial_number(object)>car_details(object under serial_number)
#rent_activity(DB)>vehicle(document)>serial_number(object)>rental_activity(object under serial_number)
#rent_activity(DB)>vehicle(document)>serial_number(object)>maintenance(object under serial_number)
#The example below is attempting to insert new objects with nested arrays to the car_details object
import os,sys
import pymongo
import csv
import boto3
import time
import psycopg2
import pandas as pd
import pandas.io.sql as sqlio
from datetime import datetime
from pymongo import errors

my_connection = psycopg2.connect(dbname='xxx', host='xxx', port=xxx, user='xxx', password='xxx')

client = pymongo.MongoClient("xxxxxxx")
mydb = client["rent_activity"] #This is the mongodb database
collection = mydb["vehicle"]

car_details_sql = "SELECT serial_number, mileage, fuel_level other from car_details"
data = sqlio.read_sql_query(car_details_sql, my_connection) #Create dataframe from query results

try:
    #Goal is to add each line from the dataframe result returned from the SQL queary, as a record under collection "vehicle", object "serial_number", nested object "car_details"
    for item in data.to_dict(orient="records"): #For each row, transform to dict
        mycarkey = str(item.get("purchase_date")) + "_" + str(item.get("serial_number")) #Create unique string
        car_number = "car_" + mycarkey
        finaldata = {}#Create empty
        finaldata[mycarkey] = item #Add outer unique key to existing dict for this set of data
        finaldata.update({"_id": car_number}) #Define custom object _id for this set of data
        collection.insert_one(finaldata)
            #This inserts to the collection "vehicle"
            #What is needed is to nest this to:
            #   rent_activity(DB)>vehicle(document)>serial_number(object)>car_details(object under serial_number)
            #NOTE: If I simply add another outer key "serial_number" I receive error key already exists
            #..so the issue is how to add an object with nested values beneath an existing object??
except Exception as e:
    raise e


问:
How to extract a base64 server cert from a p7b file in python
I have a p7b file I need to extract the base64 server certificate from. I have an openssl command I can do this on in my terminal, but the problem is I have a whole folder full of p7b certs I need to extract the server certs from.

Here is my openSSL command:
openssl pkcs7 -inform DER -outform PEM -in p7bfile.p7b -print_certs > base64_server_cert.cer

I did some googling and found a post saying to use the call command to run this in python, but when I try that it runs with no errors but there's no output in folder, so I'm not sure if I'm running they python syntax incorrectly or this is the wrong module to use. If I try printing the assigned "decrypted" variable I just get a 1 (assuming it's 0 or 1 output only).

Example of my python code:

```<code>
from subprocess import call

def main():
    decrypted = call(['openssl', 'pkcs7', '-inform', 'DER', '-outform', 'PEM', '-in', 'p7bfile.p7b', '-print_certs', '>', 'base64_server_cert.cer'])

if __name__ == '__main__':
    main()

</code>```

Is there a better way to run an open SSL command like this to extract the server cert? I don't care about extracting the CA or intermediate certs in the file, just the server cert is all that matters to me.

Included in above post, but attempted to run code using call from subprocess module and formatting my openssl command I can successfully run from my terminal, but it doesn't appear to take any action when run in python (no cert is output like when I run the command locally).

Body:
I have a p7b file I need to extract the base64 server certificate from. I have an openssl command I can do this on in my terminal, but the problem is I have a whole folder full of p7b certs I need to extract the server certs from.

Here is my openSSL command:
openssl pkcs7 -inform DER -outform PEM -in p7bfile.p7b -print_certs > base64_server_cert.cer

I did some googling and found a post saying to use the call command to run this in python, but when I try that it runs with no errors but there's no output in folder, so I'm not sure if I'm running they python syntax incorrectly or this is the wrong module to use. If I try printing the assigned "decrypted" variable I just get a 1 (assuming it's 0 or 1 output only).

Example of my python code:

from subprocess import call

def main():
    decrypted = call(['openssl', 'pkcs7', '-inform', 'DER', '-outform', 'PEM', '-in', 'p7bfile.p7b', '-print_certs', '>', 'base64_server_cert.cer'])

if __name__ == '__main__':
    main()

Is there a better way to run an open SSL command like this to extract the server cert? I don't care about extracting the CA or intermediate certs in the file, just the server cert is all that matters to me.

Included in above post, but attempted to run code using call from subprocess module and formatting my openssl command I can successfully run from my terminal, but it doesn't appear to take any action when run in python (no cert is output like when I run the command locally).


问:
Docker compose jupyter pyspark notebook NameError: name 'TerminalsExtensionApp' is not defined
```<code>version: "3.7"

services:

   pyspark:

    image: jupyter/pyspark-notebook:latest

     environment:
      JUPYTER_ENABLE_LAB: "yes"
    ports:
      - "8888:8888"
    volumes:
      [C:\spark\shared_data:/home/jovyan/work/data]
    requirements:
      image: jupyter/pyspark-notebook:latest
      volumes:
        [C:\spark\shared_data:/home/jovyan/work/data]
      command: pip install JayDeBeApi
</code>```

I run the docker-compose yaml file of jupyter pyspark notebook & it has other Python modules also installed via requirements.txt. While composing up the docker I get UserWarning: Could not import submodules warnings.warn("Could not import submodules") and NameError: name 'TerminalsExtensionApp' is not defined.

Body:
version: "3.7"

services:

   pyspark:

    image: jupyter/pyspark-notebook:latest

     environment:
      JUPYTER_ENABLE_LAB: "yes"
    ports:
      - "8888:8888"
    volumes:
      [C:\spark\shared_data:/home/jovyan/work/data]
    requirements:
      image: jupyter/pyspark-notebook:latest
      volumes:
        [C:\spark\shared_data:/home/jovyan/work/data]
      command: pip install JayDeBeApi

I run the docker-compose yaml file of jupyter pyspark notebook & it has other Python modules also installed via requirements.txt. While composing up the docker I get UserWarning: Could not import submodules warnings.warn("Could not import submodules") and NameError: name 'TerminalsExtensionApp' is not defined.


问:
php file containing script tag with php variables
Hopefully there is a cleaner way of achieving what I am trying to do here.

I have a php page that loads a table which is sortable. I want to update my database with the new sort order once the user reorders the rows in a table.

Here is my html:

```<code>    <table id="table" data-pagination="true" data-reorderable-rows="true" class="table table-bordered table-hover" role="presentation">
        <thead>
          <tr class="form-field form-required">
             <th data-field="id" data-visible="false">Id</th>
             <th data-sortable="true">Display Order</th>
             <th data-sortable="true">Name</th>
             <th data-sortable="true">Age</th>
             <th data-sortable="true">Status</th>
          </tr>
         </thead>
    .....
 </table>
</code>```

This is my script that i have inside my php file:

```<code><script type="text/javascript">
   jQuery(document).ready(function($) {
       $('#table').bootstrapTable()

       $('#button').click(function () {

         alert(JSON.stringify($('#table').bootstrapTable('getData').map(function (row) {
             <?php
               global $wpdb;
               $key = 1;
               $wpdb->update("wp_table_name", array("display_order"=>$key), array("id"=> row.id // javascript here ));
             $key++;
             ?>
            console.log(row.id);

         })));
         return "Display order updated!";
       })
     })
</script>
</code>```

How can i properly reference 'row.id' in the php code? The row.id has the correct value of the id of the row i want to update in the database. The row also contains all the table rows in the sort order i dragged them to so i know that part is working - it is passing the id from javascript to php is where i am having trouble.

Body:
Hopefully there is a cleaner way of achieving what I am trying to do here.

I have a php page that loads a table which is sortable. I want to update my database with the new sort order once the user reorders the rows in a table.

Here is my html:

             Id
             Display Order
             Name
             Age
             Status

    .....

This is my script that i have inside my php file:

How can i properly reference 'row.id' in the php code? The row.id has the correct value of the id of the row i want to update in the database. The row also contains all the table rows in the sort order i dragged them to so i know that part is working - it is passing the id from javascript to php is where i am having trouble.


问:
Apache POI VLookup substiture
I have a excel in which a column A has values with ~ characters 'Total Assets (~)'
When Column A is selected, excel will automatically populate column b with corresponding id
using formula VLOOKUP(SUBSTITUTE(E2,"~","~~"),Sheet2!A:B,2,FALSE)

I have written substitute to overcome ~ character issue.
But if I try to upload the above excel for processing using Apache POI to process
getting ERROR as cellType. Wrote below code to process

```<code>XSSFCell vCell = vRow.getCell (2);

  String vCellValue = vCell.getCellFormula ();
  FormulaEvaluator evaluator = vWorkbook.getCreationHelper().createFormulaEvaluator();
  CellType cellType = evaluator.evaluateFormulaCell(vCell);
</code>```

But unfortunately when evaluator process the cell, its returing cellType as ERROR.

Any ideas.

Title: Apache POI VLookup substiture

Body:
I have a excel in which a column A has values with ~ characters 'Total Assets (~)'
When Column A is selected, excel will automatically populate column b with corresponding id
using formula VLOOKUP(SUBSTITUTE(E2,"~","~~"),Sheet2!A:B,2,FALSE)

I have written substitute to overcome ~ character issue.
But if I try to upload the above excel for processing using Apache POI to process
getting ERROR as cellType. Wrote below code to process

XSSFCell vCell = vRow.getCell (2);

  String vCellValue = vCell.getCellFormula ();
  FormulaEvaluator evaluator = vWorkbook.getCreationHelper().createFormulaEvaluator();
  CellType cellType = evaluator.evaluateFormulaCell(vCell);

But unfortunately when evaluator process the cell, its returing cellType as ERROR.

Any ideas.


问:
CentOS 7 (running as a VM under Hyper-V on Windows 11 Pro) - http port forwarding NOT working
I have an older dedicated PC running on my home network as a webserver. Trying to retire it by replacing it with a VM on a brand new workstation Santa brought me. Simple home hobbyist network consists of router, 10Gb switch and of course computing devices off of that.

The new machine is running Windows11 Pro, and via Hyper-V I have a CentOS 7 VM. I've configured the firewall to enable http service (and port 80) being accessible from outside my network. I'm running httpd. From behind my router/switch I can access the web server with no problem, from the host machine and other machines on my network). Alas - I'm unable to access this web server remotely/externally - even after turning of the VM's firewall and ensuring port forwarding was properly pointing to IP:80 from my router. I have been scouring the web/forums/etc. for days now - nothing I've tried seems to work.

Also, I was careful in ensuring the Hyper-V settings for a virtual switch are pointing to my actual hardware NIC and set accordingly as found here and other forums (see attached image for details).

From all the "experimentations" I've tried - it's seems like the port is just not being forwarded from my router properly. So it's really pointing towards my router at this point. BUT - I can and have configured real hardware before (over decades) with no problem. Since I'm NEWB to Hyper-V and VM's - I'm worried some setting may not be correct.

Thus - reaching out to anyone with similar experience who's solved this problem. Thanks in advance.

Here's a graphic in which I captured some of the many things I've tried to no avail.
settings, etc.


问:
Toggle offline mode button is not accessible in android studio
I want to disable toggle offline mode. I found the option in right corner inside gradle, but I am not able to access that button.

I have check other's solution on stackoverflow but the solution won't work in my version of android studio.


问:
Can't move files based on file name with any extension
I have created this program, and everything works until I try to specify moving the files based on the name. I can transfer the files individually if I include the file extension. However, I'd like to move the file no matter the extension and nothing I've done will fix it. I am relatively new to programming only taken one semester class on python so some of this is still new to me.

```<code>  # list of extensions
  #extensions_dir = {".jpg", ".pdf", ".docx", ".txt", ".xlsx",
  #".gif", ".png"}

  # lists the files in folder
  files = os.listdir(downloads)

 #splits up extenstions
 extensions = {files.split('.') [-1] for files in files if
 os.path.isfile(os.path.join(downloads, files))}

 #combines all the file extensions in the list
 all_extensions = [extensions.split('.') for extensions in files]

 for file in files:
    if file == "4410." + str(all_extensions):
      shutil.move(os.path.join(downloads, file),
      control_systems)
    if file == "4470.txt":
      shutil.move(os.path.join(downloads, file), signal_processing)
    if file == "4451":
      shutil.move(os.path.join(downloads, file), embedded)
    if file == "3320":
      shutil.move(os.path.join(downloads, file), electronics)
</code>```

I have tried the using the str(all_extensions) but have not been able to implement that.

Body:
I have created this program, and everything works until I try to specify moving the files based on the name. I can transfer the files individually if I include the file extension. However, I'd like to move the file no matter the extension and nothing I've done will fix it. I am relatively new to programming only taken one semester class on python so some of this is still new to me.

  # list of extensions
  #extensions_dir = {".jpg", ".pdf", ".docx", ".txt", ".xlsx",
  #".gif", ".png"}

  # lists the files in folder
  files = os.listdir(downloads)

 #splits up extenstions
 extensions = {files.split('.') [-1] for files in files if
 os.path.isfile(os.path.join(downloads, files))}

 #combines all the file extensions in the list
 all_extensions = [extensions.split('.') for extensions in files]

 for file in files:
    if file == "4410." + str(all_extensions):
      shutil.move(os.path.join(downloads, file),
      control_systems)
    if file == "4470.txt":
      shutil.move(os.path.join(downloads, file), signal_processing)
    if file == "4451":
      shutil.move(os.path.join(downloads, file), embedded)
    if file == "3320":
      shutil.move(os.path.join(downloads, file), electronics)

I have tried the using the str(all_extensions) but have not been able to implement that.


问:
How do I test BERT with my own input instead of using a database?
So I am new to BERT and language classification and I wrote this code following an online course, but they use a database in order to test and train the model:

```<code># imports and setup for BERT
!pip install transformers
import os
import gdown
import torch
import numpy as np
import transformers
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from keras.utils import pad_sequences

from transformers import BertTokenizer
from transformers import get_linear_schedule_with_warmup
from transformers import BertForSequenceClassification, AdamW, BertConfig
from torch.utils.data import TensorDataset, DataLoader, RandomSampler, SequentialSampler

%matplotlib inline

# gdown.download('https://drive.google.com/uc?id=1q4U2gVY9tWEPdT6W-pdQpKmo152QqWLE', 'finance_train.csv', True)
# gdown.download('https://drive.google.com/uc?id=1nIBqAsItwVEGVayYTgvybz7HeK0asom0', 'finance_test.csv', True)

!wget 'https://storage.googleapis.com/inspirit-ai-data-bucket-1/Data/AI%20Scholars/Sessions%206%20-%2010%20(Projects)/Project%20-%20NLP%2BFinance/finance_test.csv'
!wget 'https://storage.googleapis.com/inspirit-ai-data-bucket-1/Data/AI%20Scholars/Sessions%206%20-%2010%20(Projects)/Project%20-%20NLP%2BFinance/finance_train.csv'

def get_finance_train():
  df_train = pd.read_csv("finance_train.csv")
  return df_train
def get_finance_test():
  df_test = pd.read_csv("finance_test.csv")
  return df_test

def flat_accuracy(preds, labels):
    pred_flat = np.argmax(preds, axis=1).flatten()
    labels_flat = labels.flatten()
    return np.sum(pred_flat == labels_flat) / len(labels_flat)
tokenizer = BertTokenizer.from_pretrained("bert-base-uncased", do_lower_case = True)
print ("Train and Test Files Loaded as train.csv and test.csv")

LABEL_MAP = {0 : "negative", 1 : "neutral", 2 : "positive"}
NONE = 4 * [None]
RND_SEED=2020
</code>```
```<code>df_train = get_finance_train()

sentences = df_train["Sentence"].values
labels = df_train["Label"].values

input_ids = []
sentences_with_special_tokens = []
tokenized_texts = []
attention_masks = []

for sentence in sentences:
  new_sentence = "[CLS] " + sentence + " [SEP]"
  sentences_with_special_tokens.append(new_sentence)

for sentence in sentences_with_special_tokens:
  tokenized_sentence = tokenizer.tokenize(sentence)
  tokenized_texts.append(tokenized_sentence)

for token in tokenized_texts:
  ids = tokenizer.convert_tokens_to_ids(token)
  input_ids.append(ids)

input_ids = pad_sequences(input_ids,
                          maxlen=128,
                          dtype="long",
                          truncating="post",
                          padding="post")

for id in input_ids:
  mask=[float(i>0) for i in id]
  attention_masks.append(mask)

X_train, X_val, y_train, y_val =train_test_split(input_ids, labels, test_size=0.15, random_state=RND_SEED)
train_masks, validation_masks, _, _ = train_test_split(attention_masks, input_ids, test_size=0.15,random_state=RND_SEED)

train_inputs = torch.tensor(np.array(X_train));
validation_inputs = torch.tensor(np.array(X_val));
train_masks = torch.tensor(np.array(train_masks));
validation_masks = torch.tensor(np.array(validation_masks));
train_labels = torch.tensor(np.array(y_train));
validation_labels = torch.tensor(np.array(y_val));

batch_size = 32
train_data = TensorDataset(train_inputs, train_masks, train_labels);
train_sampler = RandomSampler(train_data); # Samples data randonly for training
train_dataloader = DataLoader(train_data, sampler=train_sampler, batch_size=batch_size);
validation_data = TensorDataset(validation_inputs, validation_masks, validation_labels);
validation_sampler = SequentialSampler(validation_data); # Samples data sequentially
validation_dataloader = DataLoader(validation_data, sampler=validation_sampler, batch_size=batch_size);

#Load BertForSequenceClassification, the pretrained BERT model with a single linear classification layer on top.
model = BertForSequenceClassification.from_pretrained(
    "bert-base-uncased", # Use the 12-layer BERT small model, with an uncased vocab.
    num_labels = 3,
    output_attentions = False, # Whether the model returns attentions weights.
    output_hidden_states = False, # Whether the model returns all hidden-states.
);

# Given that this a huge neural network, we need to explicity specify
# in pytorch to run this model on the GPU.
model.cuda();

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
n_gpu = torch.cuda.device_count()
torch.cuda.get_device_name(0)

optimizer = AdamW(model.parameters(),
                  lr = 2e-5,
                  eps = 1e-8
                )
epochs = 4
</code>```
```<code># Total number of training steps is [number of batches] x [number of epochs].
# (Note that this is not the same as the number of training samples).
total_steps = len(train_dataloader) * epochs

# Create the learning rate scheduler.
scheduler = get_linear_schedule_with_warmup(optimizer,
                                            num_warmup_steps = 0, # Default value in run_glue.py
                                            num_training_steps = total_steps)

# We'll store training and validation loss,
# validation accuracy, and timings.
training_loss = []
validation_loss = []
training_stats = []
for epoch_i in range(0, epochs):
    # Training
    print('Epoch {:} / {:} ========'.format(epoch_i + 1, epochs))
    print('Training the model')
    # Reset the total loss for  epoch.
    total_train_loss = 0
    # Put the model into training mode.
    model.train()
    # For each batch of training data
    for step, batch in enumerate(train_dataloader):
        # Progress update every 40 batches.
        if step % 20 == 0 and not step == 0:
            # Report progress.
            print('  Batch {:>5,}  of  {:>5,}. '.format(step, len(train_dataloader)))

        # STEP 1 & 2: Unpack this training batch from our dataloader.
        # As we unpack the batch, we'll also copy each tensor to the GPU using the
        # `to` method.
        # `batch` contains three pytorch tensors:
        #   [0]: input ids
        #   [1]: attention masks
        #   [2]: labels
        b_input_ids = batch[0].to(device)
        b_input_mask = batch[1].to(device)
        b_labels = batch[2].to(device)

        # STEP 3
        # Always clear any previously calculated gradients before performing a
        # backward pass.
        model.zero_grad()

        # STEP 4
        # Perform a forward pass (evaluate the model on this training batch).
        # It returns the loss (because we provided labels) and
        # the "logits"--the model outputs prior to activation.
        outputs = model(b_input_ids,
                             token_type_ids=None,
                             attention_mask=b_input_mask,
                             labels=b_labels)
        loss = outputs[0]
        logits = outputs[1]
        # Accumulate the training loss over all of the batches so that we can
        # calculate the average loss at the end. `loss` is a Tensor containing a
        # single value; the `.item()` function just returns the Python value
        # from the tensor.
        total_train_loss += loss.item()

        # STEP 5
        # Perform a backward pass to calculate the gradients.
        loss.backward()
        # Clip the norm of the gradients to 1.0.
        # This is to help prevent the "exploding gradients" problem.
        torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)

        # STEP 6
        # Update parameters and take a step using the computed gradient
        optimizer.step()
        # Update the learning rate.
        scheduler.step()

    # Calculate the average loss over all of the batches.
    avg_train_loss = total_train_loss / len(train_dataloader)

    print("  Average training loss: {0:.2f}".format(avg_train_loss))
    # Validation
    # After the completion of each training epoch, measure our performance on
    # our validation set.

    print("Evaluating on Validation Set")
    # Put the model in evaluation mode
    model.eval()
    # Tracking variables
    total_eval_accuracy = 0
    total_eval_loss = 0
    nb_eval_steps = 0
    # Evaluate data for one epoch
    for batch in validation_dataloader:
        #Step 1 and Step 2
        # Unpack this validation batch from our dataloader.

        b_input_ids = batch[0].to(device)
        b_input_mask = batch[1].to(device)
        b_labels = batch[2].to(device)

        # Tell pytorch not to bother with constructing the compute graph during
        # the forward pass, since this is only needed for backprop (training).
        with torch.no_grad():

            # Forward pass, calculate logit predictions.
            # The "logits" are the output
            # values prior to applying an activation function like the softmax.
            outputs = model(b_input_ids,
                                   token_type_ids=None,
                                   attention_mask=b_input_mask,
                                   labels=b_labels)
            loss = outputs[0]
            logits = outputs[1]

        # Accumulate the validation loss.
        total_eval_loss += loss.item()

        # Move logits and labels to CPU
        logits = logits.detach().cpu().numpy()
        label_ids = b_labels.to('cpu').numpy()

        # Calculate the accuracy for this batch of test sentences, and
        # accumulate it over all batches.
        total_eval_accuracy += flat_accuracy(logits, label_ids)

    # Report the final accuracy for this validation run.
    avg_val_accuracy = total_eval_accuracy / len(validation_dataloader)
    print("Validation Accuracy: {0:.2f}".format(avg_val_accuracy))

    # Calculate the average loss over all of the batches.
    avg_val_loss = total_eval_loss / len(validation_dataloader)

    print("Validation Loss: {0:.2f}".format(avg_val_loss))

    training_loss.append(avg_train_loss)
    validation_loss.append(avg_val_loss)
    # Record all statistics from this epoch.

    training_stats.append(
        {
            'epoch': epoch_i + 1,
            'Training Loss': avg_train_loss,
            'Valid. Loss': avg_val_loss,
            'Valid. Accur.': avg_val_accuracy

        }
    )

print("Training complete!")
</code>```

Here is what I have:

```<code>import tensorflow as tf

df_test = "The stock is growing, the company is doing well. The company is close to bankruptcy and the stock price is falling"
sentences_with_special_tokens = []
tokenized_texts = []
input_ids = []
attention_masks = []

for sentence in df_test:
  new_sentence = "[CLS] " + sentence + " [SEP]"
  sentences_with_special_tokens.append(new_sentence)
print(sentences_with_special_tokens)
tokenized_texts = []

for sentence in sentences_with_special_tokens:
  tokenized_sentence = tokenizer.tokenize(sentence)
  tokenized_texts.append(tokenized_sentence)
print(tokenized_texts)

for token in tokenized_texts:
  ids = tokenizer.convert_tokens_to_ids(token)
  input_ids.append(ids)
print(input_ids)

input_ids = pad_sequences(input_ids,
                          maxlen=128,
                          dtype="long",
                          truncating="post",
                          padding="post")

for id in input_ids:
  mask=[float(i>0) for i in id]
  attention_masks.append(mask)
print(attention_masks)
</code>```

I want to input df_test into the model and for it to return the percentages for each of the labels

```<code>LABEL_MAP = {0 : "negative", 1 : "neutral", 2 : "positive"}
</code>```

I looked online and I found this on hugging face for single label classification:

```<code>import torch
from transformers import BertTokenizer, BertForSequenceClassification

model = BertForSequenceClassification.from_pretrained("textattack/bert-base-uncased-yelp-polarity")

inputs = tokenizer("Hello, my dog is cute", return_tensors="pt")

with torch.no_grad():
    logits = model(**inputs).logits

predicted_class_id = logits.argmax().item()
model.config.id2label[predicted_class_id]
</code>```

But when I tried implementing this into my code, it tells me that it needs all of the tensors on the same device and I have at least two devices.

RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu! (when checking argument for argument index in method wrapper__index_select)

Body:
So I am new to BERT and language classification and I wrote this code following an online course, but they use a database in order to test and train the model:

# imports and setup for BERT
!pip install transformers
import os
import gdown
import torch
import numpy as np
import transformers
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from keras.utils import pad_sequences

from transformers import BertTokenizer
from transformers import get_linear_schedule_with_warmup
from transformers import BertForSequenceClassification, AdamW, BertConfig
from torch.utils.data import TensorDataset, DataLoader, RandomSampler, SequentialSampler

%matplotlib inline

# gdown.download('https://drive.google.com/uc?id=1q4U2gVY9tWEPdT6W-pdQpKmo152QqWLE', 'finance_train.csv', True)
# gdown.download('https://drive.google.com/uc?id=1nIBqAsItwVEGVayYTgvybz7HeK0asom0', 'finance_test.csv', True)

!wget 'https://storage.googleapis.com/inspirit-ai-data-bucket-1/Data/AI%20Scholars/Sessions%206%20-%2010%20(Projects)/Project%20-%20NLP%2BFinance/finance_test.csv'
!wget 'https://storage.googleapis.com/inspirit-ai-data-bucket-1/Data/AI%20Scholars/Sessions%206%20-%2010%20(Projects)/Project%20-%20NLP%2BFinance/finance_train.csv'

def get_finance_train():
  df_train = pd.read_csv("finance_train.csv")
  return df_train
def get_finance_test():
  df_test = pd.read_csv("finance_test.csv")
  return df_test

def flat_accuracy(preds, labels):
    pred_flat = np.argmax(preds, axis=1).flatten()
    labels_flat = labels.flatten()
    return np.sum(pred_flat == labels_flat) / len(labels_flat)
tokenizer = BertTokenizer.from_pretrained("bert-base-uncased", do_lower_case = True)
print ("Train and Test Files Loaded as train.csv and test.csv")

LABEL_MAP = {0 : "negative", 1 : "neutral", 2 : "positive"}
NONE = 4 * [None]
RND_SEED=2020

df_train = get_finance_train()

sentences = df_train["Sentence"].values
labels = df_train["Label"].values

input_ids = []
sentences_with_special_tokens = []
tokenized_texts = []
attention_masks = []

for sentence in sentences:
  new_sentence = "[CLS] " + sentence + " [SEP]"
  sentences_with_special_tokens.append(new_sentence)

for sentence in sentences_with_special_tokens:
  tokenized_sentence = tokenizer.tokenize(sentence)
  tokenized_texts.append(tokenized_sentence)

for token in tokenized_texts:
  ids = tokenizer.convert_tokens_to_ids(token)
  input_ids.append(ids)

input_ids = pad_sequences(input_ids,
                          maxlen=128,
                          dtype="long",
                          truncating="post",
                          padding="post")

for id in input_ids:
  mask=[float(i>0) for i in id]
  attention_masks.append(mask)

X_train, X_val, y_train, y_val =train_test_split(input_ids, labels, test_size=0.15, random_state=RND_SEED)
train_masks, validation_masks, _, _ = train_test_split(attention_masks, input_ids, test_size=0.15,random_state=RND_SEED)

train_inputs = torch.tensor(np.array(X_train));
validation_inputs = torch.tensor(np.array(X_val));
train_masks = torch.tensor(np.array(train_masks));
validation_masks = torch.tensor(np.array(validation_masks));
train_labels = torch.tensor(np.array(y_train));
validation_labels = torch.tensor(np.array(y_val));

batch_size = 32
train_data = TensorDataset(train_inputs, train_masks, train_labels);
train_sampler = RandomSampler(train_data); # Samples data randonly for training
train_dataloader = DataLoader(train_data, sampler=train_sampler, batch_size=batch_size);
validation_data = TensorDataset(validation_inputs, validation_masks, validation_labels);
validation_sampler = SequentialSampler(validation_data); # Samples data sequentially
validation_dataloader = DataLoader(validation_data, sampler=validation_sampler, batch_size=batch_size);

#Load BertForSequenceClassification, the pretrained BERT model with a single linear classification layer on top.
model = BertForSequenceClassification.from_pretrained(
    "bert-base-uncased", # Use the 12-layer BERT small model, with an uncased vocab.
    num_labels = 3,
    output_attentions = False, # Whether the model returns attentions weights.
    output_hidden_states = False, # Whether the model returns all hidden-states.
);

# Given that this a huge neural network, we need to explicity specify
# in pytorch to run this model on the GPU.
model.cuda();

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
n_gpu = torch.cuda.device_count()
torch.cuda.get_device_name(0)

optimizer = AdamW(model.parameters(),
                  lr = 2e-5,
                  eps = 1e-8
                )
epochs = 4

# Total number of training steps is [number of batches] x [number of epochs].
# (Note that this is not the same as the number of training samples).
total_steps = len(train_dataloader) * epochs

# Create the learning rate scheduler.
scheduler = get_linear_schedule_with_warmup(optimizer,
                                            num_warmup_steps = 0, # Default value in run_glue.py
                                            num_training_steps = total_steps)

# We'll store training and validation loss,
# validation accuracy, and timings.
training_loss = []
validation_loss = []
training_stats = []
for epoch_i in range(0, epochs):
    # Training
    print('Epoch {:} / {:} ========'.format(epoch_i + 1, epochs))
    print('Training the model')
    # Reset the total loss for  epoch.
    total_train_loss = 0
    # Put the model into training mode.
    model.train()
    # For each batch of training data
    for step, batch in enumerate(train_dataloader):
        # Progress update every 40 batches.
        if step % 20 == 0 and not step == 0:
            # Report progress.
            print('  Batch {:>5,}  of  {:>5,}. '.format(step, len(train_dataloader)))

        # STEP 1 & 2: Unpack this training batch from our dataloader.
        # As we unpack the batch, we'll also copy each tensor to the GPU using the
        # `to` method.
        # `batch` contains three pytorch tensors:
        #   [0]: input ids
        #   [1]: attention masks
        #   [2]: labels
        b_input_ids = batch[0].to(device)
        b_input_mask = batch[1].to(device)
        b_labels = batch[2].to(device)

        # STEP 3
        # Always clear any previously calculated gradients before performing a
        # backward pass.
        model.zero_grad()

        # STEP 4
        # Perform a forward pass (evaluate the model on this training batch).
        # It returns the loss (because we provided labels) and
        # the "logits"--the model outputs prior to activation.
        outputs = model(b_input_ids,
                             token_type_ids=None,
                             attention_mask=b_input_mask,
                             labels=b_labels)
        loss = outputs[0]
        logits = outputs[1]
        # Accumulate the training loss over all of the batches so that we can
        # calculate the average loss at the end. `loss` is a Tensor containing a
        # single value; the `.item()` function just returns the Python value
        # from the tensor.
        total_train_loss += loss.item()

        # STEP 5
        # Perform a backward pass to calculate the gradients.
        loss.backward()
        # Clip the norm of the gradients to 1.0.
        # This is to help prevent the "exploding gradients" problem.
        torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)

        # STEP 6
        # Update parameters and take a step using the computed gradient
        optimizer.step()
        # Update the learning rate.
        scheduler.step()

    # Calculate the average loss over all of the batches.
    avg_train_loss = total_train_loss / len(train_dataloader)

    print("  Average training loss: {0:.2f}".format(avg_train_loss))
    # Validation
    # After the completion of each training epoch, measure our performance on
    # our validation set.

    print("Evaluating on Validation Set")
    # Put the model in evaluation mode
    model.eval()
    # Tracking variables
    total_eval_accuracy = 0
    total_eval_loss = 0
    nb_eval_steps = 0
    # Evaluate data for one epoch
    for batch in validation_dataloader:
        #Step 1 and Step 2
        # Unpack this validation batch from our dataloader.

        b_input_ids = batch[0].to(device)
        b_input_mask = batch[1].to(device)
        b_labels = batch[2].to(device)

        # Tell pytorch not to bother with constructing the compute graph during
        # the forward pass, since this is only needed for backprop (training).
        with torch.no_grad():

            # Forward pass, calculate logit predictions.
            # The "logits" are the output
            # values prior to applying an activation function like the softmax.
            outputs = model(b_input_ids,
                                   token_type_ids=None,
                                   attention_mask=b_input_mask,
                                   labels=b_labels)
            loss = outputs[0]
            logits = outputs[1]

        # Accumulate the validation loss.
        total_eval_loss += loss.item()

        # Move logits and labels to CPU
        logits = logits.detach().cpu().numpy()
        label_ids = b_labels.to('cpu').numpy()

        # Calculate the accuracy for this batch of test sentences, and
        # accumulate it over all batches.
        total_eval_accuracy += flat_accuracy(logits, label_ids)

    # Report the final accuracy for this validation run.
    avg_val_accuracy = total_eval_accuracy / len(validation_dataloader)
    print("Validation Accuracy: {0:.2f}".format(avg_val_accuracy))

    # Calculate the average loss over all of the batches.
    avg_val_loss = total_eval_loss / len(validation_dataloader)

    print("Validation Loss: {0:.2f}".format(avg_val_loss))

    training_loss.append(avg_train_loss)
    validation_loss.append(avg_val_loss)
    # Record all statistics from this epoch.

    training_stats.append(
        {
            'epoch': epoch_i + 1,
            'Training Loss': avg_train_loss,
            'Valid. Loss': avg_val_loss,
            'Valid. Accur.': avg_val_accuracy

        }
    )

print("Training complete!")

Here is what I have:

import tensorflow as tf

df_test = "The stock is growing, the company is doing well. The company is close to bankruptcy and the stock price is falling"
sentences_with_special_tokens = []
tokenized_texts = []
input_ids = []
attention_masks = []

for sentence in df_test:
  new_sentence = "[CLS] " + sentence + " [SEP]"
  sentences_with_special_tokens.append(new_sentence)
print(sentences_with_special_tokens)
tokenized_texts = []

for sentence in sentences_with_special_tokens:
  tokenized_sentence = tokenizer.tokenize(sentence)
  tokenized_texts.append(tokenized_sentence)
print(tokenized_texts)

for token in tokenized_texts:
  ids = tokenizer.convert_tokens_to_ids(token)
  input_ids.append(ids)
print(input_ids)

input_ids = pad_sequences(input_ids,
                          maxlen=128,
                          dtype="long",
                          truncating="post",
                          padding="post")

for id in input_ids:
  mask=[float(i>0) for i in id]
  attention_masks.append(mask)
print(attention_masks)

I want to input df_test into the model and for it to return the percentages for each of the labels

LABEL_MAP = {0 : "negative", 1 : "neutral", 2 : "positive"}

I looked online and I found this on hugging face for single label classification:

import torch
from transformers import BertTokenizer, BertForSequenceClassification

model = BertForSequenceClassification.from_pretrained("textattack/bert-base-uncased-yelp-polarity")

inputs = tokenizer("Hello, my dog is cute", return_tensors="pt")

with torch.no_grad():
    logits = model(**inputs).logits

predicted_class_id = logits.argmax().item()
model.config.id2label[predicted_class_id]

But when I tried implementing this into my code, it tells me that it needs all of the tensors on the same device and I have at least two devices.

RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu! (when checking argument for argument index in method wrapper__index_select)


问:
Unexpected token when trying to build an export a new URL instance using webpack
I have this file that was originally in my react app that was created with create-react-app

SocketWorker.ts

export const SocketWorker = new Worker(
  new URL('./SocketService.worker.ts', import.meta.url)
);
```
<p>But I have to move this file to a library package now and when I build the library package with webpack I get this error</p>
```ERROR in ./src/socket/SocketWorker.ts 1:82
Module parse failed: Unexpected token (1:82)
File was processed with these loaders:
* ./node_modules/ts-loader/index.js
You may need an additional loader to handle the result of these loaders.
> export const SocketWorker = new Worker(new URL('./SocketService.worker.ts', import.meta.url));
```
<p>Based on that error message, I'm wondering if the CRA app has a loader in the core <code>webpack.config.js</code> that I need to include in my library package. But that config file is obscured and I'm not entirely sure which loader would be the relevant one and how I should configure it. How do I set up the test condition to say it should handle the results of the <code>ts-loader</code> for this one file?</p>
<p>I have also tried pulling out the assignment of <code>new URL(args)</code> to its own variable to see where the issue happens, and it does indeed happen on that line and not on the <code>new Worker</code> line.</p>
<p><strong>EDIT</strong></p>
<p>I found this clue in the webpack documentation.</p>
<p>https://webpack.js.org/guides/asset-modules/#url-assets</p>
```When using new URL('./path/to/asset', import.meta.url), webpack creates an asset module too.

src/index.js
`const logo = new URL('./logo.svg', import.meta.url);`

Depending on the target in your configuration, webpack would compile the above code into a different result:
```

So it seems like what is happening is webpack is creating a new asset module when it hits my new URL line, only what's being loaded is a ts file and in this chain of events, there is no loader set up for it? How do I fix my config to account for that?

Body:
I have this file that was originally in my react app that was created with create-react-app

SocketWorker.ts

export const SocketWorker = new Worker(
  new URL('./SocketService.worker.ts', import.meta.url)
);

But I have to move this file to a library package now and when I build the library package with webpack I get this error

ERROR in ./src/socket/SocketWorker.ts 1:82
Module parse failed: Unexpected token (1:82)
File was processed with these loaders:
* ./node_modules/ts-loader/index.js
You may need an additional loader to handle the result of these loaders.
> export const SocketWorker = new Worker(new URL('./SocketService.worker.ts', import.meta.url));

Based on that error message, I'm wondering if the CRA app has a loader in the core webpack.config.js that I need to include in my library package. But that config file is obscured and I'm not entirely sure which loader would be the relevant one and how I should configure it. How do I set up the test condition to say it should handle the results of the ts-loader for this one file?

I have also tried pulling out the assignment of new URL(args) to its own variable to see where the issue happens, and it does indeed happen on that line and not on the new Worker line.

EDIT

I found this clue in the webpack documentation.

https://webpack.js.org/guides/asset-modules/#url-assets

When using new URL('./path/to/asset', import.meta.url), webpack creates an asset module too.

src/index.js
`const logo = new URL('./logo.svg', import.meta.url);`

Depending on the target in your configuration, webpack would compile the above code into a different result:

So it seems like what is happening is webpack is creating a new asset module when it hits my new URL line, only what's being loaded is a ts file and in this chain of events, there is no loader set up for it? How do I fix my config to account for that?


问:
How to call external c file from assembly?
I'm tried this below tools, to call 2 files

My idea was to execute the function from c using assembler and create a bin file

```<code>nasm -f elf32 boot3.s -o boot3.o // convert asm file to object
gcc -c bootloader3.c -o bootloader3.o // convert c file to object
gcc boot3.o bootloader3.o -o boot3.bin // make bin file
</code>```

boot3.s

```<code>section .text
bits 16

global _start              ; Declare the entry point symbol
extern main                ; Declare the main function as external

_start:
    cli                     ; Disable the interrupts
    call main               ; Call the main function
    hlt                     ; Halt the CPU
</code>```

bootloader3.c

#include <stdio.h>

void main() {
    printf("Hello, World!\n");
    return;
}
```
<p>My error: Error Image</p>
<p><strong>Update 05.01.23</strong></p>
<p>I tried to use a similar method, but with other commands, unfortunately another attempt also failed.</p>
<p><strong>boot.s</strong></p>
```bits 16

extern main

start:
    cli                     ; Disable the interrupts
    mov si, msg             ; SI now points to our message
    mov ah, 0x0E            ; Indicate BIOS we're going to print chars
.loop  lodsb                   ; Loads SI into AL and increments SI [next char]
    or al, al               ; Checks if the end of the string
    jz halt                 ; Jump to halt if the end
    int 0x10                ; Otherwise, call interrupt for printing the char
    jmp .loop               ; Next iteration of the loop

halt:  hlt                     ; CPU command to halt the execution
msg:   db "Hello, World!", 0   ; Our actual message to print

section .text
global puts

puts:
    ; Insert code for the puts function here
times 510 - ($ - $$) db 0

; Add the boot signature
dw 0xAA55
```
<p><strong>bootloader.c</strong></p>
```
//extern void puts(const char* str);

int main(void)
{
    puts("Hello user");
    return 0;
}
```

Compile using tools

First Step

nasm -f elf64 boot.s -o boot.o

gcc -c -m32 -o bootloader.o bootloader.c

ld -Ttext=0x7C00 -o boot.elf boot.o bootloader.o

Second Step

nasm -f bin boot.s -o boot.o

gcc -c -m32 -o bootloader.o bootloader.c

gcc -m32 -nostdlib -nostartfiles -Wl,-Ttext,0x7C00 boot.o bootloader.c -o boot.elf

Third Step

nasm -f elf32 boot.s -o boot.o

gcc -c -m32 -o bootloader.o bootloader.c

ld -Ttext=0x7C00 -o boot.elf boot.o bootloader.o

My Error:

On First & Third step

ld: bootloader.o:bootloader.c:(.text+0xa): undefined reference to `__main' ld: bootloader.o:bootloader.c:(.text+0x16): undefined reference to `puts'

On Seconds step

c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\Klubuntu\AppData\Local\Temp\ccojaMyU.o:bootloader.c:(.text+0xa): undefined reference to `__main' c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\Klubuntu\AppData\Local\Temp\ccojaMyU.o:bootloader.c:(.text+0x16): undefined reference to `puts' collect2.exe: error: ld returned 1 exit status

Sorry for all grammar mistakes
Thanks from above help

Body:
I'm tried this below tools, to call 2 files

My idea was to execute the function from c using assembler and create a bin file

nasm -f elf32 boot3.s -o boot3.o // convert asm file to object
gcc -c bootloader3.c -o bootloader3.o // convert c file to object
gcc boot3.o bootloader3.o -o boot3.bin // make bin file

boot3.s

section .text
bits 16

global _start              ; Declare the entry point symbol
extern main                ; Declare the main function as external

_start:
    cli                     ; Disable the interrupts
    call main               ; Call the main function
    hlt                     ; Halt the CPU

bootloader3.c

#include <stdio.h>

void main() {
    printf("Hello, World!\n");
    return;
}

My error: Error Image

Update 05.01.23

I tried to use a similar method, but with other commands, unfortunately another attempt also failed.

boot.s

bits 16

extern main

start:
    cli                     ; Disable the interrupts
    mov si, msg             ; SI now points to our message
    mov ah, 0x0E            ; Indicate BIOS we're going to print chars
.loop  lodsb                   ; Loads SI into AL and increments SI [next char]
    or al, al               ; Checks if the end of the string
    jz halt                 ; Jump to halt if the end
    int 0x10                ; Otherwise, call interrupt for printing the char
    jmp .loop               ; Next iteration of the loop

halt:  hlt                     ; CPU command to halt the execution
msg:   db "Hello, World!", 0   ; Our actual message to print

section .text
global puts

puts:
    ; Insert code for the puts function here
times 510 - ($ - $$) db 0

; Add the boot signature
dw 0xAA55

bootloader.c

//extern void puts(const char* str);

int main(void)
{
    puts("Hello user");
    return 0;
}

Compile using tools

First Step

nasm -f elf64 boot.s -o boot.o

gcc -c -m32 -o bootloader.o bootloader.c

ld -Ttext=0x7C00 -o boot.elf boot.o bootloader.o

Second Step

nasm -f bin boot.s -o boot.o

gcc -c -m32 -o bootloader.o bootloader.c

gcc -m32 -nostdlib -nostartfiles -Wl,-Ttext,0x7C00 boot.o bootloader.c -o boot.elf

Third Step

nasm -f elf32 boot.s -o boot.o

gcc -c -m32 -o bootloader.o bootloader.c

ld -Ttext=0x7C00 -o boot.elf boot.o bootloader.o

My Error:

On First & Third step

ld: bootloader.o:bootloader.c:(.text+0xa): undefined reference to `__main' ld: bootloader.o:bootloader.c:(.text+0x16): undefined reference to `puts'

On Seconds step

c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\Klubuntu\AppData\Local\Temp\ccojaMyU.o:bootloader.c:(.text+0xa): undefined reference to `__main' c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\Klubuntu\AppData\Local\Temp\ccojaMyU.o:bootloader.c:(.text+0x16): undefined reference to `puts' collect2.exe: error: ld returned 1 exit status

Sorry for all grammar mistakes
Thanks from above help


问:
How could I solve this error? [400] validation.error.contacts.no.contact.lists: Campaign Email Contact List is null or empty
I have created a Webhook scenario. The Webhook payload is received then created as a contact in Constant Contact, next an Email Campaign is created for Constant Contact, then an Email Activity is Scheduled for Constant Contact. In the Create a Contact options I save the contact to a contact list which is in the Contact List ID 1 input box. The new contact should be saving to the specified contact list. When I execute the scenario, I get this error below. The error bundle shows on the Schedule an Email Activity. For some reason the contact is not saving to the specified contact list. Any thoughts on how to solve this issue?

[400] validation.error.contacts.no.contact.lists: Campaign Email Contact List is null or empty, please include a valid contact list with this request


问:
fastapi + sqlalchemy + pydantic → how to process relations
Using FastAPI I can't fetch all the data. For example I have this output :

Response body Download (but the items list is always empty):

[
    {
         "email": "mmm@gmail.com",
         "id": 1,
         "is_active": true,
         "items": []
    },
    {
         "email": "ee@gmail.com",
         "id": 2,
         "is_active": true,
         "items": []
    },
]
```
<p>Endpoint are for example:</p>
<pre class="lang-py prettyprint-override"><code>@app.get("/users/", response_model=list[schemas.User])
async def read_users(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
    users = crud.get_users(db, skip=skip, limit=limit)
    return users

@app.get("/users/{user_id}", response_model=schemas.User)
async def read_user(user_id: int, db: Session = Depends(get_db)):
    db_user = crud.get_user(db, user_id=user_id)
    if db_user is None:
        raise HTTPException(status_code=404, detail="User not found")
    return db_user
</code>```

I have the following schemas (database is PostgreSQL):

from pydantic import BaseModel

class ItemBase(BaseModel):
    title: str
    description: str | None = None

class ItemCreate(ItemBase):
    pass

class Item(ItemBase):
    id: int
    owner: int

    class Config:
        orm_mode = True

class UserBase(BaseModel):
    email: str

class UserCreate(UserBase):
    password: str

class User(UserBase):
    id: int
    is_active: bool
    items: list[Item] = []

    class Config:
        orm_mode = True
```
<p>Database is postgresql</p>
<p>Models are (table users and table items):</p>
<pre class="lang-py prettyprint-override"><code>from sqlalchemy import Boolean, Column, ForeignKey, Integer, String
from sqlalchemy.orm import relationship

from .database import Base

class User(Base):
    __tablename__ = "users"
    __table_args__ = {'schema': 'socialcampaigns'}

    id = Column(Integer, primary_key=True, index=True)
    email = Column(String, unique=True, index=True)
    hashed_password = Column(String)
    is_active = Column(Boolean, default=True)

    items_rel = relationship("Item", back_populates="owner_id_rel")

class Item(Base):
    __tablename__ = "items"
    __table_args__ = {'schema': 'socialcampaigns'}

    id = Column(Integer, primary_key=True, index=True)
    title = Column(String, index=True)
    description = Column(String, index=True)
    owner = Column(Integer, ForeignKey("socialcampaigns.users.id"))

    owner_id_rel = relationship("User",  back_populates="items_rel")
</code>```

Crud code is:

from sqlalchemy.orm import Session
from . import models, schemas

def get_user(db: Session, user_id: int):
    return db.query(models.User).filter(models.User.id == user_id).first()

def get_users (db: Session, skip: int = 0, limit: int = 100):
    return db.query(models.User).offset(skip).limit(limit).all()
```

How to solve it? How do I view the list of Items when viewing users?

Body:
Using FastAPI I can't fetch all the data. For example I have this output :

Response body Download (but the items list is always empty):

[
    {
         "email": "mmm@gmail.com",
         "id": 1,
         "is_active": true,
         "items": []
    },
    {
         "email": "ee@gmail.com",
         "id": 2,
         "is_active": true,
         "items": []
    },
]

Endpoint are for example:

@app.get("/users/", response_model=list[schemas.User])
async def read_users(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
    users = crud.get_users(db, skip=skip, limit=limit)
    return users

@app.get("/users/{user_id}", response_model=schemas.User)
async def read_user(user_id: int, db: Session = Depends(get_db)):
    db_user = crud.get_user(db, user_id=user_id)
    if db_user is None:
        raise HTTPException(status_code=404, detail="User not found")
    return db_user

I have the following schemas (database is PostgreSQL):

from pydantic import BaseModel

class ItemBase(BaseModel):
    title: str
    description: str | None = None

class ItemCreate(ItemBase):
    pass

class Item(ItemBase):
    id: int
    owner: int

    class Config:
        orm_mode = True

class UserBase(BaseModel):
    email: str

class UserCreate(UserBase):
    password: str

class User(UserBase):
    id: int
    is_active: bool
    items: list[Item] = []

    class Config:
        orm_mode = True

Database is postgresql

Models are (table users and table items):

from sqlalchemy import Boolean, Column, ForeignKey, Integer, String
from sqlalchemy.orm import relationship

from .database import Base

class User(Base):
    __tablename__ = "users"
    __table_args__ = {'schema': 'socialcampaigns'}

    id = Column(Integer, primary_key=True, index=True)
    email = Column(String, unique=True, index=True)
    hashed_password = Column(String)
    is_active = Column(Boolean, default=True)

    items_rel = relationship("Item", back_populates="owner_id_rel")

class Item(Base):
    __tablename__ = "items"
    __table_args__ = {'schema': 'socialcampaigns'}

    id = Column(Integer, primary_key=True, index=True)
    title = Column(String, index=True)
    description = Column(String, index=True)
    owner = Column(Integer, ForeignKey("socialcampaigns.users.id"))

    owner_id_rel = relationship("User",  back_populates="items_rel")

Crud code is:

from sqlalchemy.orm import Session
from . import models, schemas

def get_user(db: Session, user_id: int):
    return db.query(models.User).filter(models.User.id == user_id).first()

def get_users (db: Session, skip: int = 0, limit: int = 100):
    return db.query(models.User).offset(skip).limit(limit).all()

How to solve it? How do I view the list of Items when viewing users?