Tuesday, June 8, 2021

Recent Questions - Stack Overflow

Recent Questions - Stack Overflow


jQuery .get Function Happening After page load (HAML)

Posted: 08 Jun 2021 12:19 PM PDT

So I'm using HAML and I'm looping through a set of URLs [url1, url2, url3] in order to pull an image from its page and put its src in another page. My code is as follows:

urls.each do |url|     .img{:id => #url_{url}     :javascript          var urlID = "#url_" + "#{url};          $.get('#{url}').then(function(res){                var src = $(res).find('.image').attr('src');                $(`{urlID}`).attr('src', src);              });  

However, this doesn't seem to work as whenever I console.log(urlID) within the $.get function, it only will log the last url 3 times instead of url1 url2 url3, and I'm not really sure why? This means it replaces the same image src 3 times instead of displaying 3 separate images in each of the image classes. Does anyone know why?

Why resize: both does not work on fieldset?

Posted: 08 Jun 2021 12:19 PM PDT

Why does it not enable resize control on the fieldset?

Docs only say that

resize does not apply to the following:

  • Inline elements
  • Block elements for which the overflow property is set to visible

fieldset {    resize: both;    overflow: hidden;  }
<fieldset>    <legend>Controls</legend>    <label>Input: </label><input type="number" value="20"/>  </fieldset>

I tried wrapping fieldset in a div and also wrapping everything in fieldset with a div and made that resizeable. That kind of works but the drag handle is many pixels off the fieldset border. Also when resizing it looks weird and sometimes hides the fieldset borders.

vulnerabilities error while installing via NPM, ( npm audit fix ) also not working

Posted: 08 Jun 2021 12:19 PM PDT

I was working on a react project and wanted to install react-icons so run npm install react-icons in the command prompt. but after a while it throws these errors,

F:\Areebs\React JS\areeburrub>npm install react-icons  npm WARN @babel/plugin-bugfix-v8-spread-parameters-in-optional-chaining@7.13.12 requires a peer of @babel/core@^7.13.0 but none is installed. You must install peer dependencies yourself.  npm WARN tsutils@3.21.0 requires a peer of typescript@>=2.8.0 || >= 3.2.0-dev || >= 3.3.0-dev || >= 3.4.0-dev || >= 3.5.0-dev || >= 3.6.0-dev || >= 3.6.0-beta || >= 3.7.0-dev || >= 3.7.0-beta but none is installed. You must install peer dependencies yourself.  npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@2.3.2 (node_modules\fsevents):  npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.3.2: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})  npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.13 (node_modules\webpack-dev-server\node_modules\fsevents):  npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.13: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})  npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.13 (node_modules\watchpack-chokidar2\node_modules\fsevents):  npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.13: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})    + react-icons@4.2.0  updated 1 package and audited 1949 packages in 122.206s    138 packages are looking for funding    run `npm fund` for details    found 85 vulnerabilities (82 moderate, 3 high)    run `npm audit fix` to fix them, or `npm audit` for details    

So as you can see it says run npm audit fix so I did that also but,

F:\Areebs\React JS\areeburrub>npm audit fix  npm WARN @babel/plugin-bugfix-v8-spread-parameters-in-optional-chaining@7.13.12 requires a peer of @babel/core@^7.13.0 but none is installed. You must install peer dependencies yourself.  npm WARN tsutils@3.21.0 requires a peer of typescript@>=2.8.0 || >= 3.2.0-dev || >= 3.3.0-dev || >= 3.4.0-dev || >= 3.5.0-dev || >= 3.6.0-dev || >= 3.6.0-beta || >= 3.7.0-dev || >= 3.7.0-beta but none is installed. You must install peer dependencies yourself.  npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@2.3.2 (node_modules\fsevents):  npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.3.2: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})  npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.13 (node_modules\watchpack-chokidar2\node_modules\fsevents):  npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.13: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})  npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.13 (node_modules\webpack-dev-server\node_modules\fsevents):  npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.13: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})    added 10 packages from 2 contributors, removed 3 packages and updated 3 packages in 22.668s    145 packages are looking for funding    run `npm fund` for details    fixed 1 of 85 vulnerabilities in 1949 scanned packages    84 vulnerabilities required manual review and could not be updated    

I have tried these things:

after this error I searched why this is happening, and checked these.

  • My NPM version is 6.14.12
  • My React Version is 17.0.2
  • My React Script Version is 4.0.3

How can I use DPDK with GridFTP?

Posted: 08 Jun 2021 12:19 PM PDT

I am new to Data Plane Development Kit (DPDK). I have the following questions.

  1. Is it possible to use DPDK with GridFTP. If possible, then how? Please provide me an example.
  2. Is it possible to use DPDK to transfer data between two host computer nodes? If possible, then how? Please provide me an example.

Please reply to me. I really appreciate any help you can provide.

intellij System.out.println() selecting with a trailing comma

Posted: 08 Jun 2021 12:19 PM PDT

i'm very new to intellij so i watchedup some of the shortcuts on their website and i'm quite suffering to use the System.out.println() in the public static void main(string[] args {} i want to select the System with a trailing comma but idk how xD

Error inserting data into Django database field with a OneToOnefield

Posted: 08 Jun 2021 12:18 PM PDT

I've asked this question before and tried to Google it but I've had no luck, so I have simplified my question. I have two very simple models: one holds some shift numbers and the other holds some data related to the sale of gift cards during a shift. In this case, we have an employee who worked shift "1234" and sold $200.45 and $43.67 worth of gift card from each of two terminals. The models are below:

class Data_Shifts(models.Model):      shift_id = models.CharField(max_length=25, primary_key=True, db_column="shift_id", verbose_name="Shift ID")        def __str__(self):          return str(self.shift_id)    class Data_GiftCards(models.Model):      shift_id = models.OneToOneField('Data_Shifts', on_delete=models.CASCADE, primary_key=True, db_column="shift_id", verbose_name="Shift ID")      net_sales_terminal_1 = models.DecimalField(max_digits=8, decimal_places=2, default=0)      net_sales_terminal_2 = models.DecimalField(max_digits=8, decimal_places=2, default=0)        def __str__(self):          return str(self.shift_id)  

I then try to insert some test data into the table using the following command:

Data_GiftCards.objects.create(shift_id="1234", net_sales_terminal_1="200.45", net_sales_terminal_2="43.67")  

Upon submitting the web form, I get the following error:

Cannot assign "'1234'": "Data_GiftCards.shift_id" must be a "Data_Shifts" instance.

I am boggled by this. I have a workaround that bypasses django and inserts directly into the table successfully, but this is dirty and I'd prefer to use the proper Pythonic Django way. What am I doing wrong here?

Many thanks in advance.

Terraform Change Tags Only If There Is Any Other Change

Posted: 08 Jun 2021 12:18 PM PDT

I'd like to use something as below to create/manage common tags for all resources in projects. For the common_var_tag, I'd like it to be applied only there are any other changes. So the sources are tagged with last-modified by who and when.

Is there any way to do it?

Thanks in advance!

locals {       common_var_tags = {           ChangedBy = data.aws_caller_identity.current.arn          ChangedAt = timestamp()       }         common_fix_tags = {           Project.  = "Project"           Owner     = "Tiger Peng"           Team      = "DevOps"           CreatedAt = "2021-06-08"        }   }   

Changing values in a column in a Pandas dataFrame by using another dataFramen and two conditions

Posted: 08 Jun 2021 12:18 PM PDT

I have two dataFrames:

The first dataframe df contains the data:

df = pd.DataFrame({'ISIN': ['US0378331005', 'CH0038863350', 'DE0007164600'],              'Country': ['United States', 'Switzerland', 'Germany'], 'ETF': ['A', 'B', 'C']})  

The second dataframe Lookup_Category is a lookup table to link the column 'Country' in df to column 'Country' in Lookup_Category and to link columns 'ETF' to each other:

Lookup_Category = pd.DataFrame({'ETF': ['A', 'B', C'],              'Country': ['United States', 'Switzerland', 'Belgium'],              'Category': ['cat1','cat2', 'cat3']})  

The result of the new dataframe shall be:

enter image description here

Hot to do this?

Solidity: How to represent bytes32 as string

Posted: 08 Jun 2021 12:18 PM PDT

This may be simple in other languages but I can't figure out how to do it in Solidity.
I have a bytes32 like this 0x05416460deb76d57af601be17e777b93592d8d4d4a4096c57876a91c84f4a712.

I don't want to convert the bytes to a string, rather I just want to represent the whole thing as a string, like "0x05416460deb76d57af601be17e777b93592d8d4d4a4096c57876a91c84f4a712".
How can this be done in Solidity?

How to handle browser close event through Java script

Posted: 08 Jun 2021 12:18 PM PDT

I have an application which logs users session once he logins. Say i have view case page and when user click a case it would be locked by that user and the action is tracked by inserting an entry into lock table with the java Session, caseid and userId. Now when the user click any other tab within the application, including logout an action is called to delete the session id in the lock table for that user and case. I have a requirement to release this lock even when the user closes the browser close button. My backend code will handle the scenario when the elapsedTime of the lock is greater than 10mins. However when the user closes the browser and some other user logins to view the same case within this span of 10mins it still shows the lock by the previous user. So i have to catpure the browser close event and do the same action as i do for logout and other tab click. My below piexe of java script code works well in IE browser for browser close but doesnt work in chrome or firefox. Can someone suggest which event to use in chrome/firefox please?

<script type="text/javascript">  document.getElementById('logoff').onclick = function(){      sessionStorage.setItem('accept', '0');      location.href="/abc/logout/";  };  $(document).ready(function(){             var validNavigation = false;    // Attach the event keypress to exclude the F5 refresh (includes normal refresh)  $(document).bind('keypress', function(e) {      if (e.keyCode == 116){          validNavigation = true;      }  });    // Attach the event click for all links in the page  $("a").bind("click", function() {      validNavigation = true;  });    // Attach the event submit for all forms in the page  $("form").bind("submit", function() {    validNavigation = true;  });    // Attach the event click for all inputs in the page  $("input[type=submit]").bind("click", function() {    validNavigation = true;  });     $("input[type=button]").bind("click", function() {    validNavigation = true;  });     window.onbeforeunload = function() {   if (!validNavigation) {               sessionStorage.setItem('accept', '0');          location.href="/abc/logout/";      }  };    });    </script>  

Chrome Extension Dev. (typescript) - "TypeError: o is not a function" ONLY when opening new tab and setting storage.local to "true"

Posted: 08 Jun 2021 12:17 PM PDT

I've been developing a scraper-type chrome extension for internal/personal use to scrape course data from a university's website.

The high-level algorithm is as follows:

  1. Open up the main page where the user can input the class data they want to find. (The point is to use the data in this page to generate every possible url for every unique course page)
  2. Generate the first endpoint and open a new tab with that url. (This is what I'm calling the "second degree scrape")
  3. Begin the second degree scrape and when it's done, set the chrome.storage.local to true. Then close the tab.
  4. The content script from the main page reads the local storage and sees that the state is true so it resolves the promise. It resets the local storage to false.
  5. It generates the new url and recursively repeats the process until every possible url is created.

The extension works well when I set the storage true and never modify it and simply console.log every possible url. The error arrises when I let the program open up a new tab and let it update local.storage. Before using local.storage I tried a similar implementation using messaging (simple and long-lived) and background but I had the same issue then.

Any ideas of what I could try?

Here's my code:

background/index.ts

chrome.storage.local.set({ secondDegreeState: false });  

content/index.ts

const un:string = "***";  const pw:string = "***"  const levels:Array<string> = ['L', 'U', 'G'];  let ccyys:string = `20212`;  let search_type_main:string = `FIELD`      let signInSequence:Function = () => {    if(document.getElementById("login-button")){      let signInButton:HTMLInputElement = document.getElementById("login-button")?.children[0] as HTMLInputElement;      let username: HTMLInputElement = document.getElementById("username") as HTMLInputElement;      let password: HTMLInputElement = document.getElementById("password") as HTMLInputElement;      username.value = un;      password.value = pw;      signInButton.value = "Begin Scraping";      setTimeout(() => {        signInButton.click();        console.log('Sign in button pressed');      }, 2000);    }  }  let scrapingSeqence:Function = () => {    if(window.location.href === "https://utdirect.utexas.edu/apps/registrar/course_schedule/20212/"){ // If we are in the main registration page      firstDegreeScrape(0, 1);    }    if(window.location.hostname == "utdirect.utexas.edu"){ // Make sure that we're on a proper hostname      secondDegreeScrape();    }  }      function secondDegreePromise(url:string) : Promise<any> {    /// Open up a new tab with the generated URL    window.open(url, '_blank');    return new Promise (function callback(resolve:Function, reject:Function) {      chrome.storage.local.get(['secondDegreeState'], (response) => {        if(chrome.runtime.lastError) {          console.error(chrome.runtime.lastError.message);          reject("Chrome error");        }else if (response.secondDegreeState === false){ // If the second degree state is still not done          console.log('Still waiting for 2nd degree scrape to finish...'+' Its state is '+response.secondDegreeState);          setTimeout(callback, 5000); //    repeat promise after n-seconds until state is true.        }else if(response.secondDegreeState === true){ // If the promise is complete          resolve("2nd degree scrape was complete!");        }else {          reject("Oopsie...");        }      })       });  }          // Two base cases, 1: it reaches the end of the levels array, 2: it reaches the end of the FOS list.  let firstDegreeScrape:Function = (levelNum: number, fosNum: number) => {    // Reset the scrape state (Turns it false)    chrome.storage.local.set({ secondDegreeState: false });    if (levelNum < levels.length){ // If not base case #1      const fosParent:HTMLElement|null = document.getElementById("fos_fl"); // Define the FOS parent element.      if(fosParent){  // If the fosParent is present. (Will most likely return true... just for extra safety)        let fosChildren = fosParent.children;        if(fosNum < fosChildren.length){ // If not base case #2          let fos:HTMLOptionElement = fosChildren[fosNum] as HTMLOptionElement; // The individual field of study.          let fosValue:string = fos.value.split(' ').join('+'); // Format FOS          const url:string = `https://utdirect.utexas.edu/apps/registrar/course_schedule/20212/results/?ccyys=${ccyys}&search_type_main=${search_type_main}&fos_fl=${fosValue}&level=${levels[levelNum]}`;          secondDegreePromise(url)            .then((res)=>{ // If the second degree scrape promise is resolved              console.log(res+"Now moving along to next URL.");              firstDegreeScrape(levelNum, fosNum+1); // Generate the next URL and scrape it            })            .catch(res=>{console.log(res)});                  }else {           firstDegreeScrape(levelNum+1, 1);        }      }    }  }    let secondDegreeScrape:Function = () => {    // make sure that there is something to scrape    let table: HTMLTableElement = document.getElementsByClassName('rwd-table')[0] as HTMLTableElement;      if(table){      let t_rows:HTMLCollection = table.children[1].children as HTMLCollection;      let t_rows_arr:Element[] = Array.from(t_rows);      for(let i=0; i < t_rows_arr.length; i++){        // console.log(t_rows_arr[i].childElementCount);        if(t_rows_arr[i].childElementCount == 1){ // If the row is a title          let course_title:any = t_rows_arr[i].childNodes[1].firstChild?.firstChild?.textContent;          let divisionRegex = /^[a-z\s]{0,3}/gi;          let courseNumRegex = /\d*\w/m;            console.log("Division: "+course_title.match(divisionRegex)[0]);          course_title = course_title.replace(divisionRegex, "");          console.log("Course Number: "+course_title.match(courseNumRegex)[0]);          course_title = course_title.replace(courseNumRegex, "");          console.log("Course Name: "+course_title);                  }else { // If it's a sub-column          let row = t_rows_arr[i];          let rowChildren = row.childNodes;          let unique = rowChildren[1].childNodes[0].childNodes[0].textContent; //          console.log("Unique: "+unique);            let days = rowChildren[3].textContent;          console.log("Days: "+days);                     let hour = rowChildren[5].textContent;           console.log("Hour: "+hour);                     // let room;                    let instruction_mode = rowChildren[9].textContent;          console.log("Instruction Mode: "+instruction_mode);                     let instructor = rowChildren[11].textContent;          console.log("Instructor: "+instructor);                    let status = rowChildren[13].textContent;          console.log("Status: "+status);            let flags = rowChildren[15].textContent;          console.log("Flags: "+flags);                    let core = rowChildren[17].textContent;          console.log("Core: "+core);          console.log("\n");         }      }      if(document.getElementById("next_nav_link")){ // If there is a next page        setTimeout(()=>{          document.getElementById("next_nav_link")?.click(); // Click the next button        }, 5000)      }else {        setTimeout(()=>{          // Let's complete the 2nd degree scrape (Sets true) & update the local variable          chrome.storage.local.set({ secondDegreeState: true });          //close the tab          window.close();        }, 1000)      }    }   }    let main:Function = () => {    signInSequence();    scrapingSeqence();  }    main();  

manifest.json permissions:

  • tabs
  • declarativeContent
  • storage
  • activeTab

Thanks for the help!

Declare and initialize unique_ptr inside a structure

Posted: 08 Jun 2021 12:18 PM PDT

I was wondering how I can declare a unique ptr inside a structure and initialize it later. Right now I have

struct CacheObject {      std::string _cache_type;       std::unique_ptr<faiss::Cache> cache;         uint64_t _cache_size;      // Real time data       int64_t rt_byte_req;      int64_t rt_byte_miss;      int64_t rt_obj_req;      int64_t rt_obj_miss;        CacheObject();  };  

and

CacheObject::CacheObject() : _cache_type("LRU"), cache(nullptr),          _cache_size(5000), rt_byte_req(0), rt_byte_miss(0),          rt_obj_req(0), rt_obj_miss(0) {}  

But I got an error message saying that

error: use of deleted function 'std::unique_ptr<_Tp, _Dp>& std::unique_ptr<_Tp, _Dp>::operator=(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = faiss::Cache; _Dp = std::default_delete<faiss::Cache>]'         arg2 = *temp;  

Why would this happen and how can I revise it? Thanks!

Splitting array into multiple ones

Posted: 08 Jun 2021 12:18 PM PDT

I'd like to split my array into multiple ones: The original array looks as follows:

array([[228.6311346 , 228.6311346 , 228.6311346 ],         [418.57914851,   0.        , 228.321311  ],         [416.83133465,   0.        , 723.25171282]])  

The resulting arrays should look like this:

array1([228.6311346, 418.57914851, 416.83133465])  array2([228.6311346, 0., 0.])  array3([228.6311346, 228.321311, 723.25171282])  

Avoiding copypasting subqueries twice inside the SELECT portion of the MySQL query

Posted: 08 Jun 2021 12:18 PM PDT

SELECT SQL_CALC_FOUND_ROWS wp_posts.*,                             ( 3959 * Acos(Cos(Radians((SELECT latitude                                                        FROM   wp_product_city                                                        WHERE                                               city_slug = 'toronto'))) * Cos(                                           Radians(                                           tbl_product_location.latitude)) * Cos(                                           Radians(                                           tbl_product_location.longitude)                                               - Radians((SELECT longitude                                               FROM   wp_product_city                                               WHERE                                               city_slug = 'toronto')                                                   )) +                                           Sin(                                                         Radians((SELECT latitude                                                                  FROM                                                         wp_product_city                                                                  WHERE                                                         city_slug = 'toronto')))                                           * Sin(                                           Radians                                           (                                           tbl_product_location.latitude))) ) AS                             distance,                             wp.product_location  FROM   wp_posts         INNER JOIN wp_product wp                 ON wp.post_id = wp_posts.id         INNER JOIN wp_product_product_location tbl_product_location                 ON tbl_product_location.product_location = wp.product_location  WHERE  1 = 1         AND ( wp_posts.post_status = 'publish' )  ORDER  BY distance ASC   

So I have this query, the problem is that I am copy pasting a subquery twice, and I can't find another way to do this. I tried joining the table, but since the select elements are done before the joins, it doesn't seem to work. Is there a way to avoid copy-pasting the subquery twice?

SELECT latitude  FROM   wp_product_city  WHERE  city_slug = 'toronto'   

This is the one I am copy-pasting twice.

How can i redirect /?lang=en|ru to /?lang=en#googtrans(en|ru)?

Posted: 08 Jun 2021 12:18 PM PDT

How can I add the Google translate parameter #googtrans(en|de) or other language, so the translation happens automatically?

Basically, when the user goes to https://example.com/page/?lang=de they are redirected to https://example.com/page/?lang=en#googtrans(en|de)

I use this .htaccess rule, but it's not working:

RewriteCond %{QUERY_STRING} ^lang=([a-z]{2})$  RewriteRule ^/?lang=en#googtrans(en|[a-z]{2}) [R=301,L]  

how to modify a data multiple times of in ObservableCollection from api response Xamarin Forms

Posted: 08 Jun 2021 12:17 PM PDT

I am filling a ObservableCollection with a response an api of reports, with the following data from an array in Json format:

[   {     "date": "2021-06-08 11:46:55",     "product": "EXCHANGE",     "amount": 1,     "total": "90",     "points": "90",     "id": "5581"   },   {    "date": "2021-06-03 17:33:00",    "product": "GNV",    "amount": "80",    "total": "270",    "points": "27",    "id": 102959   },  ]  

"There are many more similar data but I put those 2 examples because I receive them all in that format"

So I add the data to ObservableCollection

   public class WebApiClientService      {          private HttpClient _client;            public WebApiClientService()          {              _client = new HttpClient();          }          public async Task<T> CallPostTransactionsTask<T>(object objectParams)          {              Uri uri = new Uri(string.Format(Constants.urlTransactions));                try              {                  var jsonData = JsonConvert.SerializeObject(objectParams);                  var contentJson = new StringContent(jsonData, Encoding.UTF8, "application/json");                  HttpResponseMessage response = await _client.PostAsync(uri, contentJson);                    if (response.IsSuccessStatusCode)                  {                      var jsonResult = await response.Content.ReadAsStringAsync();                      return JsonConvert.DeserializeObject<T>(jsonResult);                  }                  else                  {                      return default(T);                  }              }              catch (Exception e)              {                  Console.WriteLine(e);                  throw;              }          }      }    public class TransaccionesResponseModel      {          public DateTime date{ get; set; }          public string product{ get; set; }          public double amount{ get; set; }          public int total { get; set; }          public double points{ get; set; }          public int id { get; set; }      }    public class TransactionsPageViewModel : BaseViewModel  {      private WebApiClientService webApi = new WebApiClientService();        private ObservableCollection<TransaccionesResponseModel> listTransactions;        public ObservableCollection<TransaccionesResponseModel> ListTransactions      {          get          {              return this.listTransactions;          }            set          {              if (this.listTransactions == value)              {                  return;              }                this.SetProperty(ref this.listTransactions, value);          }      }      public TransactionsPageViewModel()      {          CheckTransactions();      }      public async void CheckTransactions()      {          var transactionConsult = new          {            codeClient = "909090",            dateStart= "2021-02-01 00:00:00",            dateEnd= "2021-06-10 23:59:59"          };          ListTransactions= await webApi.CallPostTransactionsTask<ObservableCollection<TransaccionesResponseModel>>(transactionConsult);      }  }  

And in the XAML

<ContentPage.Content>      <StackLayout Padding="5">          <CollectionView ItemsSource="{Binding ListTransacciones}">              <CollectionView.ItemTemplate>                  <DataTemplate>                      <Grid Padding="10,0" RowDefinitions="Auto, Auto">                          <Frame                              Padding="5"                              BackgroundColor="White"                              BorderColor="#C5740C"                              CornerRadius="5"                              IsClippedToBounds="False">                              <Grid ColumnDefinitions="*,*">                                    <StackLayout Grid.Column="0">                                      <Label Text="Date:" />                                      <Label Text="Product:"/>                                      <Label Text="Amount:"/>                                      <Label Text="Points:" />                                      <Label Text="Id:" />                                  </StackLayout>                                    <StackLayout Grid.Column="1">                                      <Label                                          BackgroundColor="Transparent"                                          HorizontalTextAlignment="End"                                          Text="{Binding date, StringFormat='{0:dd/MM/yyyy HH:mm}'}" />                                      <Label                                          BackgroundColor="Transparent"                                          HorizontalTextAlignment="End"                                          Text="{Binding product}" />                                      <Label                                          BackgroundColor="Transparent"                                          HorizontalTextAlignment="End"                                          Text="{Binding amount, StringFormat='{0:#,0.#0}'}" />                                      <Label                                          BackgroundColor="Transparent"                                          HorizontalTextAlignment="End"                                          Text="{Binding total, StringFormat='{0:#.00}'}" />                                      <Label                                          BackgroundColor="Transparent"                                          HorizontalTextAlignment="End"                                          Text="{Binding points, StringFormat='{0:#,0.#0}'}" />                                      <Label                                          BackgroundColor="Transparent"                                          HorizontalTextAlignment="End"                                          Text="{Binding id}" />                                  </StackLayout>                              </Grid>                          </Frame>                      </Grid>                  </DataTemplate>              </CollectionView.ItemTemplate>          </CollectionView>      </StackLayout>  </ContentPage.Content>  

And it paints me this way:

date:          2021-06-08 11:46  product:               EXCHANGE  amount:                       1  total:                       90  points:                      90  id:                        5581    date:          2021-06-03 17:33  product:                    GNV  amount:                      80  total:                      270  points:                      27  id:                      102959  

Now I want to increase a sign of -

to points: 90 whenever: product: EXCHANGE

I want to get the result in this way when each product is equal to EXCHANGE:

date:          2021-06-08 11:46  product:               EXCHANGE  amount:                       1  total:                       90  points:                     -90  id:                        5581  

Short Time Fourier Transform (spectrum analysis) in parallel & lazily using Xarray (and/or Dask)

Posted: 08 Jun 2021 12:19 PM PDT

Question:

I am trying to do spectrum analysis on long time series data (see example for data structure, it is basically 1d data with a time index). To save time and memory etc. I want to do this parallel and lazily (using xarray and / or dask).
What would be the best way to do this?

What I tried:

(see below for examples & code)

  1. using scipy.signal.stft with xr.apply_ufunc:
    Problem: ValueError, only works if input data is 1 chunk, which does not work with large data.

  2. using using scipy.signal.stft with dask.array.from_delayed:
    Problem: Output data is always 1 chunk which makes it hard to further work with the data. (rechunking afterwards overloads RAM)

  3. Intermediate (lazily) 2d transformation using xr.Dataset.rolling.construct. Here 1 dimension is time and the rows are the short time windows over which I do the fft ("rolling window").

    with data: [1, 2, 3, 4, 5] and a rolling window of 3 this would become:

    time index rolling window
    00:00:00 NaN, NaN, 1
    00:00:01 NaN, 1, 2
    00:00:02 1, 2, 3
    00:00:03 2, 3, 4
    00:00:04 3, 4, 5

    Then using xr.apply ufunc to calculate the fft over each time point row (vectorized) over this new array, also lazily (see example below for specifics). This works and because it is lazily computed it works with big data sets too.

    Issues:

    • seems slower than scipy.signal.stft (see benchmarks below)
    • overlap/stepsize can not be changed (as with noverlap in scipy stft).
    • is the intermediate step really needed or is there a way to immediately calculate the rolling window fft?
    • Still overloads RAM in some cases

Other: I also tried other methods such as optimizing the function with numba (but that doesn't work well with np.fft.fft) but to keep this already long post short I only included the most promising methods I tried.

What can I do to do this, and / or increase performance of method 3 or use method 1 or 2, or something else to lazily and parallel using xarray / Dask?

Thanks!

Code / Examples:

imports & test dataset:

import xarray as xr  import dask.array as da  import dask  import numpy as np  from scipy import signal as ss    # variables  fs= 128           # rec frequency  _l = int(1e3)     # data length  window = fs * 10  # window over which to do stft      # Create data   data = np.random.random(_l * fs)  data = xr.DataArray(da.from_array(data, name="x")).assign_coords({"dim_0":np.arange(_l * fs)})  data = data.chunk(chunks=_l/10 * fs)   # create multiple chunks to simulate bigger data  data = data.to_dataset()  

Method 1:

def stft(data,):      f, t, Zxx = ss.stft(data,                          fs=fs,                           nperseg=window,                          noverlap= window - 1                          )          return np.abs(Zxx)    data['fft'] = xr.apply_ufunc(stft,                 data.x,                input_core_dims=[['dim_0']],                output_core_dims=[["f", "t"]],   # define newly created output dims                dask="parallelized",                output_dtypes=['f8'],                output_sizes={"f": window / 2 + 1,                               "t": data.x.size - 1},                )  

output:

ValueError: dimension 'dim_0' on 0th function argument to apply_ufunc with dask='parallelized' consists of multiple chunks, but is also a core dimension. To fix, rechunk into a single dask array chunk along this dimension, i.e., ``.chunk({'dim_0': -1})``, but beware that this may significantly increase memory usage.  

Method 2:

def stft1(x):      f, t, Zxx = ss.stft(x,                          fs=fs,                           nperseg=window,                          noverlap=window - 1                          )          return np.abs(Zxx)        da.from_delayed(dask.delayed(stft1)(data.x), shape=(window / 2 + 1, data.x.size - 1), meta="f8")  

output:
Method 2 output

Method 3: (working but not optimal?)

def xr_make_fft(ds, par):      ds['roll_window'] = np.arange(window)  # create dimension for rolling window            # FFT function to apply vectorized:      def xr_fft(x):          fft = np.fft.fft(x)[xr_fft.idx]          return np.abs(fft)            # make new parameter with rolling windows stacked      ds['FFT_window'] = ds[par].rolling(dim_0=window).construct("roll_window",)            # Calc FFT Freq domain      fftfreq = np.fft.fftfreq(window, 1 / fs)      idx = (0 < fftfreq)      freq = fftfreq[idx]      ds['Frequency'] = freq      xr_fft.idx = idx            ds[f'FFT'] = xr.apply_ufunc(xr_fft,                                  ds[f"FFT_window"],                                  vectorize=True,                                  input_core_dims=[["roll_window"]],  # define input dim over which to vectorize (this dim in inserted completely)                                  output_core_dims=[["Frequency"]],   # define newly created output dims                                  dask="parallelized",                                  output_dtypes=['f8'],                                  output_sizes={"Frequency": len(freq)},                                  )            ds = ds.drop(f'FFT_window').drop_dims('roll_window')      return ds    data = xr_make_fft(data, "x")  

output: method 3 output

performance test on small dataset: 128e3 length data (largest dataset which did not overload RAM):

Method 2:

%timeit da.from_delayed(dask.delayed(stft1)(data.x), shape=(window / 2 + 1, data.x.size - 1), meta="f8").compute()  3.53 s ± 28.8 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)  

Method 3:

%timeit data.FFT.compute()   6.04 s ± 219 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)  

scipy.signal.stft

%timeit ss.stft(data.x, fs, nperseg=window, noverlap=window-1)[2]  1.87 s ± 8.28 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)  

generating artifacts in gitlab: Allure reporter for WDIO

Posted: 08 Jun 2021 12:19 PM PDT

I encounter an issue at last step of my CI/CD setup using gitlab. My testing framework consists from webdriverIO + Typescript + allure-reporter. I get an error when I want to generate the report artifacts as follows: enter image description here

I have 2 folders one called allure-results which is in artifacts archive, and allure-report which cannot be found. I rechecked the path, but seems should be ok. Any idea or example regarding to allure reporter in gitlab?

Bellow I can put my config file for webdriverIO

 reporters: ['spec', ['cucumberjs-json', {          jsonFolder: 'artifacts/jsonReports',          language: 'en'      }],      ['allure', {          outputDir: 'allure-results',          useCucumberStepReporter: true,          disableWebdriverStepsReporting: false,          disableWebdriverScreenshotsReporting: false,      }]],
gitlab-ci.yml

enter image description here

Locally works as expected. Thanks

location pathname removing icon at wrong pathway in react

Posted: 08 Jun 2021 12:17 PM PDT

I currently have a project laid out like this:

enter image description here

I'm trying to make it so that when you click the shopping cart icon and go to the cart, the cart icon disappears. However when I try and write the logic for location.pathname it ends up removing the shopping cart icon on the products page itself like so:

enter image description here

How do I fix this so that the shopping cart item disappears only when I am actually in the cart section (which I have linked up using react router)?

My Navbar component:

import React from "react";  import {    AppBar,    Toolbar,    IconButton,    Badge,    MenuItem,    Menu,    Typography,  } from "@material-ui/core";  import { ShoppingCart } from "@material-ui/icons";  import { Link, useLocation } from "react-router-dom";  import logo from "../../assets/commerce.png";  import useStyles from "./styles";    const Navbar = ({ totalItems }) => {    const classes = useStyles();    const location = useLocation;      return (      <>        <AppBar position="fixed" className={classes.appBar} color="inherit">          <Toolbar>            <Typography              component={Link}              to="/"              variant="h6"              className={classes.title}              color="inherit"            >              <img                src={logo}                alt="Generic commerce template"                height="25px"                className={classes.image}              />              Generic Commerce Template            </Typography>            <div className={classes.grow} />              {location.pathname === '/' && (            <div className={classes.button}>              <IconButton component={Link} to="/cart" aria-label="Show cart items" color="inherit">                <Badge badgeContent={totalItems} color="secondary">                  <ShoppingCart />                </Badge>              </IconButton>            </div>            )}          </Toolbar>        </AppBar>      </>    );  };    export default Navbar;  

Also where I used react router in App JS:

return (      <Router>        <div>          <Navbar totalItems={cart.total_items} />          <Switch>            <Route exact path="/">              <Products products={products} onAddToCart={handleAddToCart} />            </Route>              <Route exact path="/cart">              <Cart cart={cart} />            </Route>          </Switch>        </div>      </Router>    );  };  

MongoDB Function not returning array correctly

Posted: 08 Jun 2021 12:17 PM PDT

I am attempting to find out if each string in an array exists on MongoDB in a simple and efficient way. The field to be searched is known (i.e. name)

The issue that I am running into is that when calling the below function it returns undefined.

const generateNaughtyList = (listtoCheck: Array<string>): Array<string> => {    let doesntExist = []    listtoCheck.forEach(async (e) => {      const docCount = await Article.countDocuments({ name: e }).exec();      if (docCount != 1) doesntExist.push(e)    })    return doesntExist  }    

calling function

  const hello = await generateNaughtyList(Stringlist)    console.log(hello)  

If I try to console.log out during the loop (i.e.)

   if (docCount != 1) console.log('It doesn't exist')    

It works fine?

If I use another loop method

  for (let i = 0; i <= listtoCheck.length-1; i++) {      const docCount: number = await Article.countDocuments({ name: listtoCheck[i] }).exec();      if (docCount != 1) doesntExist.push(listtoCheck[i])    }  

It works fine. The forEach doesn't seem to want to work!

Defining a string literal

Posted: 08 Jun 2021 12:18 PM PDT

What's the difference between this two literal string definitions?:

const char *message1 = "message1";  const char message2[] = "message2";  

If both of them are null-terminated it's so strange - when i pass the first one to some text printing function I get an "memory could not be written" error but when pass the second everything is OK!?

enter image description here

The function of question is some 3-rd party function from Fallout 2 game mod source called sfall. The function as far as i know calls the native Fallout 2 engine defined function which the sfall code calls by it's own wrapper.

How to convert large exponential values to integer/decimal format in Marklogic?

Posted: 08 Jun 2021 12:18 PM PDT

I have an exponential value for e.g. 3.22122E+23

In Marklogic when I try- xs:decimal(3.22122E+23) I get this error:

[1.0-ml] XDMP-CAST: (err:FORG0001) xs:decimal(xs:double("3.22122E23")) -- Invalid cast: xs:double("3.22122E23") cast as xs:decimal

A lower value for e.g. xs:decimal(3.22122E+18) gives me the correct result i.e. 3221220000000000000.

I see that this is because of decimal overflow and cannot be represented as a decimal data type but is there any way in Marklogic to handle and calculate such huge values?

Same question applies for the negative values(3.22122E-23) where I can handle and display data above 20 decimal places.

Calling Trimble Connect APIs from a C# App

Posted: 08 Jun 2021 12:19 PM PDT

I am trying to use a C# application to interact with a Trimble Connect Project, to do this i'm trying to follow this link: APIs tutorial but the problem is that after adding a lot of missed dependencies I'm stuck with this: Could not load type 'System.ServiceModel.Description.DispatcherSynchronizationBehavior' from assembly 'System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

Powershell - Parse windows events and extract xml data information

Posted: 08 Jun 2021 12:19 PM PDT

I required your help regarding this subject.

Context:

I am looking for a way to parse evt (or evtx) files based on id (example: 302) and extract the data available in the xml field of the event.

Event xml format is:

- <Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">  - <System>    <Provider Name="MyProgr" />     <EventID Qualifiers="49154">302</EventID>     <Level>2</Level>     <Task>0</Task>     <Keywords>0x80000000000000</Keywords>     <TimeCreated SystemTime="2021-05-31T14:55:59.040708600Z" />     <EventRecordID>772</EventRecordID>     <Channel>Application</Channel>     <Computer>Server.lan</Computer>     <Security />     </System>  - <EventData>    <Data>A text relating this event</Data>     </EventData>    </Event>  

My goal is to get a similar output (based on sample above):

Event: 302     The 2021-05-31T14:55:59 on Server.lan, MyProgr raise the following message:   A text relating this event  

Do you have any good idea to address my needs?

Solution

$events = Get-WinEvent  -FilterHashTable @{LogName = "Application"; id=302} -MaxEvents 50 | where {$_.Message -like '*'}  foreach($event in $events){  "-"*50                                      write-host "Date:" $event.TimeCreated              write-host "Source:"$event.ProviderName              $event.Message               "-"*50  }  

Access VBA: Calculating Median or data using GROUP BY on two columns

Posted: 08 Jun 2021 12:19 PM PDT

I am trying to find a way to calculate the median of a dataset in access, that is grouped by two columns, typeA, typeB.

This is a sample of the table:

ID (autonumber) typeA (large number) typeB (large number) total (large number)
1 1 1 15
2 2 1 15
3 1 1 45
4 2 1 44
5 1 2 19
6 1 2 4
7 1 2 34
8 2 2 19
9 2 2 18

Using Access 2016

Currently I am using the following code snippet:

Function fMedian(SQLOrTable, GroupFieldName, GroupFieldValue, GroupFieldName2, GroupFieldValue2, MedianFieldName)  DoCmd.SetWarnings False    Dim rs As DAO.Recordset    Set db = CurrentDb  Set rs1 = db.OpenRecordset(SQLOrTable, dbOpenDynaset)    If IsDate(GroupFieldValue) Then      GroupFieldValue = "#" & GroupFieldValue & "#"  ElseIf Not IsNumeric(GroupFieldValue) Then      GroupFieldValue = "'" & Replace(GroupFieldValue, "'", "''") & "'"  End If    If IsDate(GroupFieldValue2) Then      GroupFieldValue2 = "#" & GroupFieldValue2 & "#"  ElseIf Not IsNumeric(GroupFieldValue) Then      GroupFieldValue2 = "'" & Replace(GroupFieldValue2, "'", "''") & "'"  End If    rs1.Filter = GroupFieldName & "=" & GroupFieldValue  rs1.Sort = MedianFieldName    Set rs = rs1.OpenRecordset()  rs.Move (rs.RecordCount / 2)    If rs.RecordCount Mod 2 = 0 Then      varMedian1 = rs.Fields(MedianFieldName)      rs.MoveNext      fMedian = varMedian1 + rs.Fields(MedianFieldName) / 2  Else      fMedian = rs.Fields(MedianFieldName)  End If    End Function  

As it stands, this works great for grouping by one column, but I cannot figure out how to allow it to group by on both typeA and typeB. I have by editing the rs1.filter line but to no avail.

Any help with the code, or a better approach would be appreciated.

Thank you!

Trying to understand this code: map((data: SearchResponse) => data && data.items || [])

Posted: 08 Jun 2021 12:18 PM PDT

I am new to Angular and am trying to learn by examining code samples. I pulled some sample code for ngx-Typeahead and am trying to understand the code.

interface SearchResponse {    total_count: number;    incomplete_results: boolean;    items: Owner[];  }  

then

this.suggestions$ = new Observable((observer: Observer<string>) => {        observer.next(this.search);      }).pipe(        switchMap((query: string) => {          if (query) {            return this.http.get<SearchResponse>('https://localhost:5001/api/GetOwners', {              params: { q: query }            }).pipe(              map((data: SearchResponse) => data && data.items || []),              tap(() => noop, err => { this.errorMessage = err && err.message || 'Something goes wrong'; })            );          }            return of([]);        })      );  

The line of code that says:

map((data: SearchResponse) => data && data.items || [])  

Reads something like "map the response into this function and call it "data". Then with this, AND it with the items (an element of SearchResponse). If that AND is nothing, then return an empty array." So what if it isn't nothing? What does "data && data.items yield? Or am I overthinking this and data && data.items will return true so the whole data item is returned?

Saying words in a given language in Python

Posted: 08 Jun 2021 12:18 PM PDT

How can Python to say words in a given language? I tried searching but it gave me results for detecting/processing human voice.

After that, I searched on stackoverflow. I found a few results, however, none of them worked out for me. I am on Windows 10 and my computer is an Mac OS from Apple.

When I tried this:

import os    os.system("say 'hi'")  

Python displayed:

sh: 1: say: not found  

When I tried this:

import pyttsx3;  engine = pyttsx3.init();  engine.say("I will speak this text");  engine.runAndWait();  

Python said that there was no such file/directory as libespeak.so.1 while running the init().

At last, I tried:

import win32com.client as wincl  speak = wincl.Dispatch("SAPI.SpVoice")  speak.Speak("This is the pc voice speaking")  

Python told me that the was no such thing as win32com.

What is the correct syntax for running a bash script as a step in EMR?

Posted: 08 Jun 2021 12:18 PM PDT

I am trying to run a bash script as a step after EMR completes bootstrapping. Following is my terraform code:

step { action_on_failure = "CONTINUE"   name = "Setup Hadoop configuration"   hadoop_jar_step {   jar = "command-runner.jar"   args = ["bash,-c,'cd /mnt; chmod +x ./userdata.sh; ./userdata.sh'"]   }}  

This isn't working and keeps on failing mentioning that No file found. Are args correctly provided ? What about script-runner.jar? Any help is highly appreciated. :(

Elasticsearch, Failed to obtain node lock, is the following location writable

Posted: 08 Jun 2021 12:18 PM PDT

Elasticsearch won't start using ./bin/elasticsearch. It raises the following exception:

- ElasticsearchIllegalStateException[Failed to obtain node lock, is the following location writable?: [/home/user1/elasticsearch-1.4.4/data/elasticsearch]  

I checked the permissions on the same location and the location has 777 permissions on it and is owned by user1.

ls -al /home/user1/elasticsearch-1.4.4/data/elasticsearch  
drwxrwxrwx  3 user1 wheel 4096 Mar  8 13:24 .  drwxrwxrwx  3 user1 wheel 4096 Mar  8 13:00 ..  drwxrwxrwx 52 user1 wheel 4096 Mar  8 13:51 nodes  

What is the problem?

Trying to run elasticsearch 1.4.4 on linux without root access.

Multiple Query Condition in Android using ORMLITE

Posted: 08 Jun 2021 12:18 PM PDT

i want to make a simple query, with multiple conditions

I use OrmLite to map entity object.

Now I want to search for an object into my table.

Supposing i have a Person entity that maps PERSON table, what I want to do is to initialize an object with some parameters and search it.

Suppose a function searchPerson(Person oPerson)

If i pass an object OPerson like this

Id = null  Name = John  Age = null  Sex = male  

Is possible to write a query to reach that goal? Something like this pseudo-code

pers = (from p in db.Table<Person>()                        where (if OPerson.Id !=null) p.Id==OPerson.Id}                       AND {(if OPerson.Name !=null) p.Name.Contains(OPerson.Name)}                       AND {(if condition) where-contion}                       select p).ToList();  

I know that i can do multiple query in this way

list=PersonDao.queryBuilder().where().eq("name",OPerson.name).and().eq("sex",OPerson.sex").query();  

but I want also to check if the value exists

No comments:

Post a Comment