Monday, September 13, 2021

Recent Questions - Stack Overflow

Recent Questions - Stack Overflow


Will this HTTP Web API call cause deadlock?

Posted: 13 Sep 2021 07:55 AM PDT

I would like to know your feedback to optimize the code. Will this cause deadlock or performance problems? This is calling a Web API method and using .Net 4.7

Some posts suggest to use async and await when using GetAsync to avoid deadlocks. Should HttpRequestException handling, TaskCanceledException and CancelPendingRequests be used?

public ItemsDTO GetItems(int itemId)   {      var result = new ItemsDTO();      try      {          var client = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true });          var response = client.GetAsync(String.Format(apiUrl + "store/GetItems/{0}", itemId)).Result;            if (response.IsSuccessStatusCode)              result = response.Content.ReadAsAsync<ItemsDTO>().Result;          else              throw new Exception(response.StatusCode + " - " + response.ReasonPhrase);      }      catch (Exception ex)      {          Store.ManageException.HandleException(ex);          throw new GetItemsException(ex.Message);      }      return result;   }  

How does excel interpret an array

Posted: 13 Sep 2021 07:55 AM PDT

Is an array stored as a string ?

Websocket connection failed in Django and user matching query does not exist error

Posted: 13 Sep 2021 07:55 AM PDT

I am trying to create a one on one chat application using django channels,websockets and Redis.Tried implementing django channels from (https://youtu.be/RVH05S1qab8) but getting errors like this (from javaScript console).The line 253 is from views.py,the code for which is mentioned below.Also error in line 95 of models.py which is also mentioned in the code below.

(from terminal)

Also the message is not been sent to the other user in real time and the page has to be refreshed to get the message.

Code for thread.html:

      {% extends "bookexchange/base.html" %}            {% block content %}      <h3>Thread for {% if user != object.first %}{{ object.first }}{% else %}{{ object.second }}{% endif %}</h3>      <ul id='chat-items'>      {% for chat in object.chatmessage_set.all %}            <li>{{ chat.message }} via {{ chat.user }}</li>            {% endfor %}      </ul>            <form id='form' method='POST'> {% csrf_token %}          <input type="hidden" id="myUsername" value="{{ user.username }}">      {{form.as_p }}      <input type='submit' class='btn btn-primary'/>      </form>            {% endblock %}            {% block script %}      <script src="https://cdnjs.cloudflare.com/ajax/libs/reconnecting-websocket/1.0.0/reconnecting-websocket.min.js" integrity="sha512-B4skI5FiLurS86aioJx9VfozI1wjqrn6aTdJH+YQUmCZum/ZibPBTX55k5d9XM6EsKePDInkLVrN7vPmJxc1qA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>            <script>            var loc=window.location      var formData=$("#form")      var msgInput=$("#id_message")      var chatHolder=$("#chat-items")      var me=$("#myUsername").val()      var wsStart='ws://'       if(loc.protocol=='https:'){          wsStart='wss://'      }      var endpoint =wsStart + loc.host + loc.pathname       var socket= new WebSocket(endpoint)            socket.onmessage=function(e){          console.log("message",e)          var chatDataMsg=JSON.parse(e.data)          chatHolder.append("<li>"+ chatDataMsg.message +"via"+ chatDataMsg.username +"</li>")      }      socket.onopen=function(e){          console.log("open",e)          formData.submit(function(event){              event.preventDefault()              var msgText=msgInput.val()              // chatHolder.append("<li>"+ msgText + " via "+me  + "</li>")              // var formDataSerialized=formData.serialize()              var finalData={                  'message': msgText              }              socket.send(JSON.stringify(finalData))              // msgInput.val('')              formData[0].reset()          })      }      socket.onerror=function(e){          console.log("error",e)      }      socket.onclose=function(e){          console.log("close",e)      }            </script>      {% endblock %}    

Code for consumers.py:

  import asyncio  import json  from django.contrib.auth import get_user_model  from channels.consumer import AsyncConsumer  from channels.db import database_sync_to_async  from .models import Thread,ChatMessage      class ChatConsumer(AsyncConsumer):      async def websocket_connect(self,event):          print("connected",event)                    other_user=self.scope['url_route']['kwargs']['username']          me = self.scope['user']          # print(other_user,me)          thread_obj=await self.get_thread(me,other_user)          print(me,thread_obj.id)          self.thread_obj=thread_obj          chat_room=f"thread_{thread_obj.id}"          self.chat_room=chat_room          await self.channel_layer.group_add(              chat_room,              self.channel_name          )            await self.send({              'type':"websocket.accept"          })          # await asyncio.sleep(10)               async def websocket_receive(self,event):          print("receive",event)          front_text=event.get('text',None)          if front_text is not None:              loaded_dict_data=json.loads(front_text)              msg=loaded_dict_data.get('message')                          user=self.scope['user']              username='default'              if user.is_authenticated:                  username=user.username              myResponse={                  'message':msg,                  'username':username              }              await self.create_chat_message(user,msg)              await self.channel_layer.group_send(                  self.chat_room,                  # new_event                  {                      'type':"chat_message",                      "text":json.dumps(myResponse)                  }              )      async def chat_message(self,event):          print('message',event)          await self.send({              'type':"chat_message",              "text":event['text']          })                        async def websocket_disconnect(self,event):          print("disconnected",event)            @database_sync_to_async      def get_thread(self,user,other_username):          return Thread.objects.get_or_new(user,other_username)[0]        @database_sync_to_async      def create_chat_message(self,me,msg):          thread_obj=self.thread_obj          me=self.scope['user']          return Thread.objects.create(thread=thread_obj,user=me,message=msg)    

Code for views.py:

  class ThreadView(LoginRequiredMixin, FormMixin, DetailView):      template_name = 'bookexchange/thread.html'      form_class = ComposeForm      success_url = './'        def get_queryset(self):          return Thread.objects.by_user(self.request.user)        def get_object(self):          other_username  = self.kwargs.get("username")          obj, created  = Thread.objects.get_or_new(self.request.user,other_username) #line 253          if obj == None:              raise Http404          return obj        def get_context_data(self, **kwargs):          context = super().get_context_data(**kwargs)          context['form'] = self.get_form()          return context    

Models.py contains User model created using AbstractUser.Also it contains 'broadcast_msg_to_chat' function which is not defined anywhere in the source code.The code is

  class ThreadManager(models.Manager):          def by_user(self, user):              qlookup = Q(first=user) | Q(second=user)              qlookup2 = Q(first=user) & Q(second=user)              qs = self.get_queryset().filter(qlookup).exclude(qlookup2).distinct()              return qs                def get_or_new(self, user, other_username): # get_or_create              username = user.username              if username == other_username:                  return None              qlookup1 = Q(first__username=username) & Q(second__username=other_username)              qlookup2 = Q(first__username=other_username) & Q(second__username=username)              qs = self.get_queryset().filter(qlookup1 | qlookup2).distinct()              if qs.count() == 1:                  return qs.first(), False              elif qs.count() > 1:                  return qs.order_by('timestamp').first(), False              else:                  Klass = user.__class__                  user2 = Klass.objects.get(username=other_username) #line 95                  if user != user2:                      obj = self.model(                              first=user,                               second=user2                          )                      obj.save()                      return obj, True                  return None, False                  class Thread(models.Model):          first        = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='chat_thread_first')          second       = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='chat_thread_second')          updated      = models.DateTimeField(auto_now=True)          timestamp    = models.DateTimeField(auto_now_add=True)                    objects      = ThreadManager()                @property          def room_group_name(self):              return f'chat_{self.id}'                def broadcast(self, msg=None):              if msg is not None:                  broadcast_msg_to_chat(msg, group_name=self.room_group_name, user='admin')                  return True              return False                  class ChatMessage(models.Model):          thread      = models.ForeignKey(Thread, null=True, blank=True, on_delete=models.SET_NULL)          user        = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name='sender', on_delete=models.CASCADE)          message     = models.TextField()          timestamp   = models.DateTimeField(auto_now_add=True)    

Code for routing.py:

      application= ProtocolTypeRouter({          'websocket':AllowedHostsOriginValidator(              AuthMiddlewareStack(                  URLRouter(                      [                          re_path(r"^messages/(?P<username>[\w.@+-]+)/$", ChatConsumer),                      ]                  )              )          )      })    

aspx Gridview conditional format

Posted: 13 Sep 2021 07:55 AM PDT

I have a simple gridview and code behind to conditionally format some rows. It runs but nothing gets colored. I've tried various autoformatting and finally removed all in the hope that there was some magic but to no avail

protected void GridviewRowDataBound(object sender, GridViewRowEventArgs e)  {      if (e.Row.RowType == DataControlRowType.DataRow)      {          int CellValue = Convert.ToInt32(e.Row.Cells[2].Text);          if (CellValue >= 0)          {              e.Row.Cells[2].BackColor = System.Drawing.Color.Green;          }          if (CellValue < 0)          {              e.Row.Cells[2].BackColor = System.Drawing.Color.Red;          }      }  }        

<%@ Page Language="VB" CodeBehind="TrainPurch.aspx.vb" %>

<!DOCTYPE html>  <html lang="en" xmlns=http://www.w3.org/1999/xhtml>         <head runat="server">           <title></title>      </head>  <body>      <form id="form1" runat="server">             <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="D:\Inetpub/wwwroot\dealerinfo\oracledealerinfo.mdb" SelectCommand=" SELECT tblDealerMifTrained.R12Account, tblDealerMifTrained.GroupOwner, tblDealerMifTrained.Units AS Units, tblDealerMifTrained.ProductCode, ProdToEDP.Model, qry6aTrainedPurchase.Trained FROM (ProdToEDP INNER JOIN tblDealerMifTrained ON ProdToEDP.ProductCode = tblDealerMifTrained.ProductCode) INNER JOIN qry6aTrainedPurchase ON (tblDealerMifTrained.ProductCode = qry6aTrainedPurchase.ProductCode) AND (tblDealerMifTrained.R12Account=qry6aTrainedPurchase.R12Account) WHERE (((tblDealerMifTrained.Type)='Purchase')) GROUP BY tblDealerMifTrained.R12Account, tblDealerMifTrained.GroupOwner, tblDealerMifTrained.Units, tblDealerMifTrained.ProductCode, ProdToEDP.Model, qry6aTrainedPurchase.Trained;"></asp:AccessDataSource>     <div style="height:750px; overflow:auto">          <asp:GridView            id="GridView2"            runat="server"            AllowSorting="True"                         DataSourceID="AccessDataSource1" Caption="Trained Purchased" PageSize="25" AutoGenerateColumns="False">              <Columns>                  <asp:BoundField DataField="R12Account" HeaderText="R12Account" SortExpression="R12Account" />                  <asp:BoundField DataField="GroupOwner" HeaderText="GroupOwner" SortExpression="GroupOwner" />                  <asp:BoundField DataField="Units" HeaderText="Units" SortExpression="Units" />                  <asp:BoundField DataField="ProductCode" HeaderText="ProductCode" SortExpression="ProductCode" />                  <asp:BoundField DataField="Model" HeaderText="Model" SortExpression="Model" />                  <asp:BoundField DataField="Trained" HeaderText="Trained" SortExpression="Trained" />              </Columns>        </asp:GridView>  </div>      </form>  </body>  </html>         

fill NaN with values of a similar row

Posted: 13 Sep 2021 07:55 AM PDT

My dataframe has these 2 columns: "reach" and "height". the column "reach" has a lot of missing value. But the column 'height' have all the value needed. What I see is that reach is often a function of height. Therefore, for the rows with NaN, I want to look at the height, then find another row with the same "height" and that has "reach" available, then copy this value to the 1 with missing value

enter image description here

Postgres XSLT generate uuid

Posted: 13 Sep 2021 07:54 AM PDT

In Postgresql I want to assign ids to nodes inside a xml document.

Here the original xml

<elements>    <element name="one" />    <element name="two" />    <element name="three" />  </elements>  

Expected result:

<elements>    <element name="one" id="33204d05-263a-462b-bb84-67318924c109"/>    <element name="two" id="c42956c9-e603-4442-9172-7b3f8c377674"/>    <element name="three" id="41070972-4401-4d5e-8ec0-ffce9e5b217e"/>  </elements>  

Script to generate base data:

create table data(id serial, name text, xml xml);  insert into data(name, xml)    values('a', '  <elements>    <element name="one" />    <element name="two" />    <element name="three" />  </elements>');  

So far I have this

select xslt_process(xml::text, '      <xsl:stylesheet version="1.0"                           xmlns:xsl="http://www.w3.org/1999/XSL/Transform"                           xmlns:xs="http://www.w3.org/2001/XMLSchema"     >                                <xsl:template match="@*|node()">              <xsl:copy>                  <xsl:apply-templates select="@*|node()"/>              </xsl:copy>          </xsl:template>                                 <xsl:template match="//element">        <xsl:copy>               <xsl:attribute name="Id"><xsl:value-of select="generate-id()"/></xsl:attribute>               <xsl:apply-templates select="@*|node()"/>             </xsl:copy>       </xsl:template>                                   </xsl:stylesheet>',   'text paramlist') from data;  

which gives me

<?xml version="1.0"?>  <elements>    <element Id="idp1383723609" name="one"/>    <element Id="idp1383723225" name="two"/>    <element Id="idp1383723353" name="three"/>  </elements>  

How can I get uuids instead of hash ids?

Grouping flat data to create a hierarchical tree using LINQ for JSON

Posted: 13 Sep 2021 07:54 AM PDT

I am trying to project a flat data source into an object that can be serialized directly to JSON using Newtonsoft.Json. I've created a small program in Linqpad with an imagined inventory overview as a test. The requested output is as follows:

  • Site name
    • Inventory name
      • Product name
      • Weight
      • Units
    • Inventory name
      • Product (etc)

For the life of me I can't get it to only have a single "Site name" as the only root object. I want an to list the contents inside an inventory inside a site, but it always ends up looking like: enter image description here

How can I make the "Site" distinct having a collection of "inventory" which each has a collection of "products"?

My actual data source is a database table and it resembles the structure of my test object - and it is what it is.

The test code in Linqpad: (note that it references Newtonsoft.Json)

void Main()  {      var contents = new List<DatabaseRecord>()      {          new DatabaseRecord{Product="Autoblaster", Inventory="Hull 5", Site="Death star", Units=20,Weight=500},          new DatabaseRecord{Product="E11 Blaster Rifle", Inventory="Hull 5", Site="Death star", Units=512,Weight=4096},          new DatabaseRecord{Product="SWE/2 Sonic Rifle", Inventory="Hull 1", Site="Death star", Units=20,Weight=500},          new DatabaseRecord{Product="Relby v10 Micro Grenade Launcher", Inventory="Hull 5", Site="Death star", Units=20,Weight=500},          new DatabaseRecord{Product="T-8 Disruptor", Inventory="Hull 1", Site="Death star", Units=20,Weight=500},          new DatabaseRecord{Product="E11 Blaster Rifle", Inventory="Hull 2", Site="Death star", Units=50,Weight=1200}      };            var inventorycontent = from row in contents                      group row by row.Site into sites                      orderby sites.Key                      select from inventory in sites                          group inventory by inventory.Inventory into inventories                             orderby inventories.Key                             select new                             {                                 site = sites.Key,                                 inventory = inventories.Key,                                 lines = inventories.Select(i => new { i.Product, i.Weight, i.Units })                             };        contents.Dump();      inventorycontent.Dump();            JsonConvert.SerializeObject(inventorycontent, Newtonsoft.Json.Formatting.Indented).Dump();  }    // Define other methods and classes here  class DatabaseRecord  {      public string Product { get; set; }      public string Inventory { get; set; }      public string Site { get; set; }      public int Units { get; set; }      public double Weight { get; set; }        }  

JSON output:

[    [      {        "site": "Death star",        "inventory": "Hull 1",        "lines": [          {            "Product": "SWE/2 Sonic Rifle",            "Weight": 500.0,            "Units": 20          },          {            "Product": "T-8 Disruptor",            "Weight": 500.0,            "Units": 20          }        ]      },      {        "site": "Death star",        "inventory": "Hull 2",        "lines": [          {            "Product": "E11 Blaster Rifle",            "Weight": 1200.0,            "Units": 50          }        ]      },      {        "site": "Death star",        "inventory": "Hull 5",        "lines": [          {            "Product": "Autoblaster",            "Weight": 500.0,            "Units": 20          },          {            "Product": "E11 Blaster Rifle",            "Weight": 4096.0,            "Units": 512          },          {            "Product": "Relby v10 Micro Grenade Launcher",            "Weight": 500.0,            "Units": 20          }        ]      }    ]  ]  

How to set pseudo elements properties dynamically?

Posted: 13 Sep 2021 07:54 AM PDT

I am trying to overlay a watermark on a HTML5 video, and depending on the user selection, set the position of the watermark on the video.

CSS

/*Video.module.css*/    .wrapper {      position: relative;      height: 100%;  }    .wrapper::before {      display: block;      position: absolute;      top: 2%;      left: 2%;      height: 18%;      width: 18%;      content: '';      background: 'red';  }  

JSX

import styles from 'styles/Video.module.css';    const Video = ({ src }) => {      return (          <div class={styles.wrapper}>              <video src={src}>                  <track type='captions' />              </video>          </div>      );  };    

With the current styles, the watermark is positioned at the top-left of the video. I want to allow the user to select the position.

Currently, my approach is to create a css class selector for each position like below and use useState hook to set the class based on user selection.

CSS

.wrapper-top-right {      position: relative;      height: 100%;  }    .wrapper-top-right::before {      top: 2%      right: 2%      ....  }    .wrapper-bottom-right {      ....  }    .wrapper-bottom-right::before {      bottom: 2%;      right: 2%;      ....  }  

JSX

import styles from 'styles/Video.module.css';  import React, {useState} from 'react';    const Video = ({ src }) => {      //Values: 'top-left', 'bottom-left', 'top-right', 'bottom-right'      const [position, setPosition] = useState('top-left');         return (          <div class={`styles[${position}`}>              <video src={src}>                  <track type='captions' />              </video>              /** Button with code to set position.... **/          </div>      );  };    

But this is definitely not ideal due to the repeated code within the CSS file. Would like to know if there is way to set the properties (in this case, top, bottom, left, right) dynamically so that I only need a single class selector.

Transferring data from multiple PDF Documents into one Excel

Posted: 13 Sep 2021 07:54 AM PDT

currently I am using Python and Tabula to transfer a huge amount of pdfs documents into one excel sheet. My Code looks like this:

import tabula.io  import os  from tabula.io import read_pdf      tabula.convert_into_by_batch("Z:\EU-Daten\Auswertung ESR\Projects", output_format="csv", pages="all")  

But this created only single documents that were almost empty. But i really want to have one excel document, in which specific numbers out of the documents are combined. Is there someone with the same problem that has a good idea for me?

Thanks!

Splice and for loops

Posted: 13 Sep 2021 07:54 AM PDT

I am trying to splice an element from an array the first condition gets executed but the second is not. The thing I am trying to achieve is, removing the element at index 2 which is "c" and then adding "a" but I need to do it with splice. And the problem is the first condition works but second does not.

function rotate(arr) {      for (el of arr) {          if (arr.indexOf(el) === 0) {              arr.splice(0, 1, "c")          }          else if (arr.indexOf(el) === 2) {              arr.splice(2, 1, "a")          }          return arr      }  }    console.log(rotate(["a", "b", "c"]))  

How to check whether all number elements in an array are even numbers with recursion?

Posted: 13 Sep 2021 07:55 AM PDT

I'm currently learning as much as I can about recursion but I'm really stuck on how to use it to check whether all the number elements in an array are even numbers. With ES6, I can use "every" to achieve the desire outcome as follow:

const isEven = (arr) => arr.every((item) => item % 2 === 0);  console.log(isEven([2,4,6,8])); // true  console.log(isEven([2,4,6,9])); // false  

The following is my attempt as recursion:

const isEven = (arr) => (arr.length === 0) ? true : isEven(arr.slice(1)) % 2 === 0;  console.log(isEven([2,4,6,8])); // true  console.log(isEven([2,4,6,9])); // true  

As you can see, the result is incorrect for the 2nd example and I have a feeling that I'm not on the right path either. Can someone kindly show me what I'm doing wrong?

Error in python: 'bitwise_and' not supported for the input types,

Posted: 13 Sep 2021 07:55 AM PDT

I am getting an error of - TypeError: ufunc 'bitwise_and' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe when trying to run a simple loop. How can it be corrected?

for i in range(0,df.shape[0]+1):  if(df.iloc[i,8] >= 90):      df.iloc[i,9]="Ex"  elif (df.iloc[i,8] >= 80 & df.iloc[i,8] <90):      df.iloc[i,9]="A"  elif(df.iloc[i,8] >= 70 & df.iloc[i,8] <80):      df.iloc[i,9]="B"  elif(df.iloc[i,8] >= 60 & df.iloc[i,8] <70):      df.iloc[i,9]="C"  elif(df.iloc[i,8] >= 50 & df.iloc[i,8] <60):      df.iloc[i,9]="D"  else:      df.iloc[i,9]="E"        

PowerShell : Changing the value of an object in an array

Posted: 13 Sep 2021 07:56 AM PDT

I often consult this site but today is a big day as I am asking my first question!

I regularly code in PowerShell but I'm stuck on one point. To explain it to you I will take an example.

Let's imagine that I create a table in which I enter the result of 2 Get-ADComputer:

$tab = @()  $tab += Get-ADComputer PORT-CAEN-15 | Select-Object Name, Test  $tab += Get-ADComputer PORT-CAEN-24 | Select-Object Name, Test  

The result of the table is the following :

Name         Test  ----         ----  PORT-CAEN-15 {}    PORT-CAEN-24 {}   

How can I modify the "Test" property for each computer ?

Thank you for your answer and don't hesitate to tell me if I'm not using the right terms.

antlr.MismatchedTokenException: expecting "set", found 'INNER'

Posted: 13 Sep 2021 07:55 AM PDT

How to convert base64 to file without saving to directory? [duplicate]

Posted: 13 Sep 2021 07:54 AM PDT

I transformed an image and a PDF to base64. Is it possible to transform base64 to a File without having to save to a computer location? Do this conversion directly?

I have a template to convert base64 to byte[]. I needed convert to file to load a PDF

import javax.xml.bind.DatatypeConverter;  import java.io.File;  import org.apache.pdfbox.pdmodel.PDDocument;    public class Base64Convert {        public static void main(String[] args) {          byte[] bytes = convertBase64ToBytes("SAwIG9iago8PAovVHlw....");          File file = new File(bytes); // <-- that's not possible          PDDocument document = PDDocument.load(file);      }        public static byte[] convertBase64ToBytes(String base64){          return DatatypeConverter.parseBase64Binary(base64);      }  }  

The variable file would be the final result I expected, a file obtained through a byte[].

So in summary:

base64 > byte[] > file

How do I make multiple sequential calls with axios?

Posted: 13 Sep 2021 07:54 AM PDT

I'm getting a headache from trying to come up with a solution, and I hope that someone here can solve my problem.

I am trying to create a program that will do a loop for a number of audio files, using NodeJS:

  1. Download audio file from source.
  2. Save audio in local folder.
  3. Send audio file to outside API for treatment (this part is already handled).
  4. Obtain the answer to the audio file treatment
  5. Display the answer

The API cannot work on 2 or more audios at the same time because it mixes them up and returns incorrect results, so the audio list has to be handled sequentially (so, it does steps 1-5 for audio_1, then steps 1-5 for audio_2, etcetera).

I can do the loop just fine, but due to how Promises work, what the program actually does is this:

  1. Do steps 1-2 for audio_1.
  2. Get a Promise to do step 3 for audio_1.
  3. Do steps 1-2 for audio_2.
  4. Get a Promise to do step 3 for audio_2...
  5. Once it gets all the Promises, it listens in until all the the Promises are resolved.

And this messes things up due to the above mentioned problem.

Here's the code I have right now:

async function transcriptor(val){    const vars = {"variables":"my_variables"}    const url = "my_url"    const fileContent = fs.readFileSync(val.file_path)    let form = new FormData()    Object.keys(vars).forEach(key=>form.append(key,vars[key]))    form.append("file",fileContent,val.file_name)    return new Promise(async function(resolve,reject){      axios.post(url,form,{headers:form.getHeaders(),maxBodyLength:Infinity})      .then((response)=>{        resolve(response.data)      }).catch((error)=>{        reject(error)      });    })  }  async function transcribirWBTA(client){    ...    await collection.find({}).forEach(async (f)=>{      let file_path = `local_file_path`      await https.get(f.file_location,function(res){        let file = fs.createWriteStream(file_path)        var stream = res.pipe(file)        stream.on('finish',async function(){          let result = await transcriptor({"file_path":file_path,"file_name":f.file_name})          nAudios += 1          console.log(nAudios,result.transcription)          ...        })      })    })  }  

My only obstacle is ensuring that the forEach loop will not move to one cycle until the previous one has finished - once I get that, then my full program should work.

Any ideas?

Bigquery - How to filter records with certain condition

Posted: 13 Sep 2021 07:55 AM PDT

I'm trying to write SQL (in BigQuery) the following in order to get a result that satisfies following condition. for ex: my table contains following data

id         | value  ___________|_____________  1          | p  1          | oo  2          | p  4          | p  4          | lop  5          | AA  5          | p  6          | p  6          | p  

I want to filter out records where it contains only value as "p" from the table. Expected result would be

2          | p  6          | p  

I have tried with following query but it returns other rows as well (1,p and 1,oo)

SELECT id,value  FROM `ggg.tt.table` where id in   (     select id from (SELECT id,COUNT(distinct value )as cnt     from (select * FROM `ggg.tt.table` where trim(value) = 'p'        )group by 1 having cnt = 1))  

can someone help how to achieve this using bigquery ?

How to retrieve count objects faster on Django?

Posted: 13 Sep 2021 07:54 AM PDT

My goal is to optimize the retrieval of the count of objects I have in my Django model.

I have two models:

  • Users
  • Prospects

It's a one-to-many relationship. One User can create many Prospects. One Prospect can only be created by one User.

I'm trying to get the Prospects created by the user in the last 24 hours.

Prospects model has roughly 7 millions rows on my PostgreSQL database. Users only 2000.

My current code is taking to much time to get the desired results.

I tried to use filter() and count():

import datetime    # get the date but 24 hours earlier  date_example = datetime.datetime.now() - datetime.timedelta(days = 1)    # Filter Prospects that are created by user_id_example  # and filter Prospects that got a date greater than date_example (so equal or sooner)  today_prospects = Prospect.objects.filter(user_id = 'user_id_example', create_date__gte = date_example)    # get the count of prospects that got created in the past 24 hours by user_id_example  # this is the problematic call that takes too long to process  count_total_today_prospects = today_prospects.count()  

I works, but it takes too much time (5 minutes). Because it's checking the entire database instead of just checking, what I though it would: only the prospects that got created in the last 24 hours by the user.

I also tried using annotate but it's equally slow, because it's ultimately doing the same thing than the regular .count():

today_prospects.annotate(Count('id'))

How can I get the count in a more optimized way?

Changing the v-if in the same element to computed with it being reference to be used for other properties

Posted: 13 Sep 2021 07:54 AM PDT

I have been working on a sorting system lately with VueJs and Laravel but sort of got stuck on a piece of code that actually works, tho I don't think it's efficient because I'm using a v-if and a v-for in the same element. It is as following:

<tbody class="bg-white divide-y divide-gray-200" >                                  <tr v-for="data in Medewerkers" :key="data" class="bg-white" v-if="data.timesheets.length !== 0">  <!-- This is the timesheet check function v-if="data.timesheets.length !== 0" -->  <td class="px-6 py-3 text-xs font-medium leading-4 tracking-wider text-left text-gray-500 uppercase bg-gray-50" {{ data.mw_naam }}</td>   <td class="px-6 py-3 text-xs font-medium leading-4 tracking-wider text-left text-gray-500 uppercase bg-gray-50"> {{ data.timesheets[0].datum }}   </td>  </tr>  </tbody>  

Yes, I'm using Tailwind with it if you ever wondered.

So the code is self-explanatory I think. I'm requesting data from the database and inserting it into a table (Not the whole code is shown, only the part where I get data from the timesheet table). The thing is, I wanna know how do I use the computed properly to be getting the same data and then referencing it so the (datum) property can read it as well. I did try it but no luck so far and this is what I came up with:

    computed: {      timeMedewerkers: function () {           this.data.timesheets.filter(Medewerkers => Medewerkers.timesheets.length !== 0);  }}  

I am still a junior dev so I might be missing something, I would appreciate it if you don't criticize me for whatever reason. I'm open to tips and suggestions tho. Thanks in advance!

This is what it looks like when fully compiled: This is the compiled version, it returns the names requested from the timesheet table

NSIS not displaying new control after change ui

Posted: 13 Sep 2021 07:54 AM PDT

I took ui.cpp and default.rc from the svn, compiled it, used changeui ---> works!

I can't display new controls. This is a section from the new default.rc

IDD_LICENSE DIALOGEX 0, 0, 266, 130 STYLE DS_FIXEDSYS | DS_CONTROL | WS_CHILD  FONT 8, "MS Shell Dlg", 0, 0, 0x1  BEGIN      ICON            IDR_MAINFRAME,IDR_MAINFRAME,0,0,22,20      LTEXT           "pppppppppppppppppppppp",507,25,0,241,23      LTEXT           "email user",508, 172, 106, 233, 31      CONTROL         "",IDC_EDIT1,RICHEDIT_CLASS,WS_BORDER | WS_VSCROLL |                   WS_TABSTOP | 0x804,0,24,266,105  END  

In the nsh script, on create function of a page I tried:

GetDlgItem $MyHandle $HWNDPARENT507  MessageBox MB_OK $MyHandle //this is always 0 no matter what I tried in the previous line!!!  EnableWindow $MyHandle 1  

I also tried editing the original defalt.exe with resource hacker but nsis wouldn't display new text/control.

How can I display the new text and control???

AddEVentListener not working after updated html button

Posted: 13 Sep 2021 07:54 AM PDT

<!-- Any html blocks -->  {% for n in loop %}  <div class="update">      <div>          <button type="button" class="update--btn">-</button>      </div>  </div>  {% endfor %}      let updateBtn = document.getElementsByClassName('update--btn');    [...updateBtn].forEach(e => {      e.addEventListener('click', clickBtn);  })    function clickBtn() {      const xhr = new XMLHttpRequest();      xhr.open('POST', url);      xhr.onload = () => {          let cartUpdate = this.closest('.update');          cartUpdate.innerHTML ='';            let cartUpdateInner = document.createElement('div');          cartUpdateInner.innerHTML = '<button type="button" class="update--btn">+</button>'          cartUpdate.appendChild(cartUpdateInner);      }      xhr.send();  }  

When the page is loaded for the first time, all working. But second click on button, after change HTML in not working.

PS. I need change html, not only text

How to Add Color to Static Mapbox Image Gotten from API

Posted: 13 Sep 2021 07:54 AM PDT

Below is the URL I pass to fetch API to get the image. It works great but the location pin is grey and I need it to be #800080 (purple). I get there's a marker-color parameter but not sure how to add it to the baseUrl string below:

const baseUrl = `https://api.mapbox.com/styles/v1/mapbox/streets-v11/static/geojson(%7B%22type%22%3A%22Point%22%2C%22coordinates%22%3A%5B${geoLocation.longitude}%2C${geoLocation.latitude}%5D%7D)/${geoLocation.longitude},${geoLocation.latitude},15/500x300?access_token=${MAPBOX_API_KEY}`;

Relevant documentation which gives additional parameters but no clear examples: https://docs.mapbox.com/api/maps/static-images/#overlay-options

Code:

  const getUrlExtension = (url) => {    return url.split(/[#?]/)[0].split('.').pop().trim();  };    const onImageEdit = async (imgUrl) => {    const imgExt = getUrlExtension(imgUrl);    const response = await fetch(imgUrl);    const blob = await response.blob();    const file = new File([blob], 'locationImage.' + imgExt, {        type: blob.type,    });      setImage(file);    setPreviewSrc(response.url);  };    function previewLocation(event) {    event.preventDefault();    const MAPBOX_API_KEY ='xyz';      // baseUrl based on https://docs.mapbox.com/api/maps/static-images/#overlay-options    const baseUrl = `https://api.mapbox.com/styles/v1/mapbox/streets-v11/static/geojson(%7B%22type%22%3A%22Point%22%2C%22coordinates%22%3A%5B${geoLocation.longitude}%2C${geoLocation.latitude}%5D%7D)/${geoLocation.longitude},${geoLocation.latitude},15/500x300?access_token=${MAPBOX_API_KEY}`;      onImageEdit(baseUrl);  }  

making the listcolumns search dynamic

Posted: 13 Sep 2021 07:54 AM PDT

I am following upn on a tute on datatables and found a code which basically iterates over the columns for a order clause but in the case, i do not have a list of order columns hardcoded, its a dynamic list

how can I change this

<cfif form["order[0][column]"] gt 0>      ORDER BY       <cfif form["order[0][column]"] eq '1'>      empname <cfif form["order[0][dir]"] eq 'desc'>desc</cfif>      </cfif>      <cfif form["order[0][column]"] eq '2'>      empno <cfif form["order[0][dir]"] eq 'desc'>desc</cfif>      </cfif>      <cfif form["order[0][column]"] eq '3'>      ic <cfif form["order[0][dir]"] eq 'desc'>desc</cfif>      </cfif>     </cfif>  

to be dynamic as my list variable is a comma separated as id,name,email and lots more

How to ask the user from for a single word in Alexa Skill

Posted: 13 Sep 2021 07:55 AM PDT

I am trying to develop an Alexa skill where Alexa asks the user for a single word (and than uses the word in sentence). The user should be able to response with just the word, without any phrase around it. The word can be any word found in the dictionary.

So I am trying to create Intent with an Utterance like this:

{word}  

The first question is: What to use for the {word} slot. There is AMAZON.SearchQuery which is for phrases, not for words, but maybe that is good enough.

Unfortunately, when I try to build the model I get:

Sample utterance "{word}" in intent "GetNextWordIntent" must include a carrier phrase. Sample intent utterances with phrase types cannot consist of only slots.  

So I really need a phrase around the slot, which is not what I want.

How can I create an Intent (or do it some other way) to ask the user for a single word?

I found this project: https://github.com/rubyrocks/alexa-backwardsword which claims to be a skill, that asks the user for a word and says it backward. Unfortunately the project does not really explain how it deploys itself and how it works in detail.

Android sensors return the same values for every app opening

Posted: 13 Sep 2021 07:54 AM PDT

Every time I open my app, I get the device's orientation values. If I turn the device 90 degrees to the right at the X-axis, the values change as well. But every time I reopen my app, it shows that the X value is 348. Even if I rotated the phone 90 degrees before opening my app. Why do the sensors return the same value at the start?

Whu every time I entry the app, the starting points are always the same?

I rotated the device before opening the app to 0 degrees and got:

enter image description here

I rotated the device before opening the app to 90 degrees and got:

enter image description here

I'm rotating the correct axis, this is not the problem

My code:

    protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.activity_main);            sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);          sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);            sensorManager.registerListener(MainActivity.this, sensor, SensorManager.SENSOR_DELAY_FASTEST);      }        @Override      public void onSensorChanged(SensorEvent event) { // Activated every changes in sensors          Log.i(TAG, event.values[0] + " | " + event.values[1] + " | " + event.values[2]); // Prit the sensors values      }        @Override      public void onAccuracyChanged(Sensor sensor, int accuracy) {        }  

"symbol" and "markers" don't seem to work anymore in the line function of plotly-express

Posted: 13 Sep 2021 07:55 AM PDT

In the website of plotly express, there is an example to use "symbol" and "markers" as an argument for the line function :

import plotly.express as px  df = px.data.gapminder().query("continent == 'Oceania'")  fig = px.line(df, x='year', y='lifeExp', color='country', markers=True)  fig.show()    import plotly.express as px  df = px.data.gapminder().query("continent == 'Oceania'")  fig = px.line(df, x='year', y='lifeExp', color='country', symbol="country")  fig.show()  

These two examples don't work for me :

line() got an unexpected keyword argument 'markers'    line() got an unexpected keyword argument 'symbol'  

Would you know why ? Is there an alternative ?

Copy contents from host OS into Docker image without rebuilding image

Posted: 13 Sep 2021 07:54 AM PDT

I'm building a new image and copy contents from host OS folder D:\Programs\scrapy into it like so: docker build . -t scrapy

Dockerfile

FROM mcr.microsoft.com/windows/servercore:ltsc2019  SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]    RUN mkdir root  RUN cd root  WORKDIR /root  RUN mkdir scrapy  COPY scrapy to /root/scrapy          

Now when I add new contents to the host OS folder "D:\Programs\scrapy" I want to also add it to image folder "root/scrapy", but I DON'T want to build a completely new image (it takes quite a while). So how can I keep the existing image and just overwrite the contents of the image folder "root/scrapy".
Also: I don't want to copy the new contents EACH time I run the container (so NOT at run-time), I just want to have a SEPARATE command to add more files to an existing image and then run a new container based on that image at another time.

I checked here: How to update source code without rebuilding image (but not sure if OP tries to do the same as me)

UPDATE 1

Checking What is the purpose of VOLUME in Dockerfile and docker --volume format for Windows

I tried the commands below, all resulting in error:

docker: Error response from daemon: invalid volume specification: ''. See 'docker run --help'.

Where <pathiused> is for example D:/Programs/scrapy:/root/scrapy

docker run -v //D/Programs/scrapy:/root/scrapy scrapy    docker run -v scrapy:/root/scrapy scrapy    docker run -it -v //D/Programs/scrapy:/root/scrapy scrapy    docker run -it -v scrapy:/root/scrapy scrapy  

UPDATE WITH cp command based on @Makariy's feedback

docker images -a gives:

REPOSITORY                             TAG        IMAGE ID       CREATED        SIZE  scrapy                                 latest     e35e03c8cbbd   29 hours ago   5.71GB  <none>                                 <none>     2089ad178feb   29 hours ago   5.71GB  <none>                                 <none>     6162a0bec2fc   29 hours ago   5.7GB  <none>                                 <none>     116a0c593544   29 hours ago   5.7GB  mcr.microsoft.com/windows/servercore   ltsc2019   d1724c2d9a84   5 weeks ago    5.7GB  

I run docker run -it scrapy and then docker container ls which gives:

CONTAINER ID   IMAGE     COMMAND                    CREATED              STATUS              PORTS     NAMES  1fcda458a14c   scrapy    "c:\\windows\\system32…"   About a minute ago   Up About a minute             thirsty_bassi  

If I run docker cp D:\Programs\scrapy scrapy:/root/scrapy I get:

Error: No such container:path: scrapy:\root

So in a separate PowerShell instance I then run docker cp D:\Programs\scrapy thirsty_bassi:/root/scrapy whichs show no output in PowerShell whatsoever, so I think it should've done something.

But then in my container instance when I goto /root/scrapy folder I only see the files that were already added when the image was built, not the new ones I wanted to add.

Also, I think I'm adding files to the container here, but is there no way to add it to the image instead? Without rebuilding the whole image?

Add CSP header to Google Cloud Storage

Posted: 13 Sep 2021 07:54 AM PDT

I'm serving a SPA (Vue-app) from a Google Cloud Storage bucket. And I'm trying to configure Google Cloud Storage bucket to add a CSP Response header. (Content-Security-Policy: default ...)

I've tried the following, but without success:

1. Adding header with gsutil

Running the following:

gsutil setmeta -h "Content-Security-Policy:${CSP}" gs://{BUCKET_NAME}/index.html  

But this returns the following response:

CommandException: Invalid or disallowed header (Content-Security-Policy).  Only these fields (plus x-goog-meta-* fields) can be set or unset:  [u'cache-control', u'content-disposition', u'content-encoding', u'content-language', u'content-type']  

It seems this header is not standardly allowed to add.

2. Adding Custom header with prefix with gsutil

I then proceeded to follow their advice and prepend x-goog-meta-* in the hopes they convert it back to Content-Security-Policy themselves. Running the following:

gsutil setmeta -h "x-goog-meta-Content-Security-Policy:${CSP}" gs://{BUCKET_NAME}/index.html  

..gives the following response:

Setting metadata on gs://{BUCKET_NAME}/index.html...  / [1 objects]                                                                     Operation completed over 1 objects.   

So this works. But upon checking the response headers, they did not alter it to Content-Security-Policy header: enter image description here

So now I'm a bit clueless how to enable this CSP-header for Google Storage buckets. What am I missing? Or is this simply not possible?

Thanks in advance.

How to use pg_dump with a connection uri / url?

Posted: 13 Sep 2021 07:55 AM PDT

I can invoke psql like this:

psql postgres://...  

How can I use pg_dump with a connection string in the format postgres://...?
Would be more convenient than breaking URI's into host, post, username, password.

Is there syntax for this?

When do I call my class controller, manager or service?

Posted: 13 Sep 2021 07:55 AM PDT

Maybe it is obvious to you. I'm new to java (half year work) and I had a discussion with my collegues. I have troubles naming my classes according to their responsibilities. For that, my classes gain responsibilities they should never have.

Can you help me?

BTW: I'm working current at a project where I have to use a persistance-layer from service classes. I have split my packages to model, service and persistance.

No comments:

Post a Comment