how to use the column in temporary table for the following update in MSQL Posted: 10 Oct 2021 07:58 AM PDT I have a MySQL database. I want to update a column in a table using the values from concat columns in other tables. SELECT * FROM(SELECT distinct t.id, t.title as Textbook, GROUP_CONCAT(concat(ci.discipline_code, ci.code, " (" , ci.type , ")") SEPARATOR ', ') as CourseCode FROM tms_local.bms_material m, tms_local.bms_title t, tms_local.bms_course c, tms_local.bms_courseinfo ci where t.id > 1 AND t.id = m.book_id and c.id = m.course_id and ci.id = c.id and isbn != 'NA' GROUP BY t.id) AS temporary_table; UPDATE tms_local.bms_title SET tms_local.bms_title.thumbnail = temporary_table.CourseCode WHERE tms_local.bms_title.title=temporary_table.Textbook; But I got the error: Unknow temporary_table.Textbook in where clause. How could I use the update the tms_local.bms_title.thumbnail column using CourseCode column from the selected table? enter image description here |
join string by comma and put around aech element quotes (python) Posted: 10 Oct 2021 07:58 AM PDT I have a following string: s_tring = 'abcd,efgh,igkl,mnop,qrst,uvwx,yz' I want to create a list and split it by commas and put quotes around each elemnt like this: some_list = '"abcd", "efgh", "igkl", "mnop", "qrst", "uvwx", "yz"' |
Required regex in javascript to fetch token value of 6 digits Posted: 10 Oct 2021 07:57 AM PDT I have a response from an API and the overall response is as follows: {"account":"12345678","lastUsed":"2021-10-07T16:55:27.558+09:00","lastUsedBy":"Abcdef Qwerty","token":"378953","user":"Abcdef Qwerty"} I have to extract below numeric value using regex(i.e. I have to extract the token value which is dynamic ) 378953 I tried number of approaches but couldn't find the correct regex. Request to you all to help with above requirement. Thanks in advance. Note: Regex is required in Javascript |
How to bring Date Range picker to the front in Shiny Posted: 10 Oct 2021 07:57 AM PDT I have updated the latest version of R and Shiny. In this latest version the Date Range picker goes behind the heading on the page making it difficult to get to the year (see pick). I need to bring the widger in frontenter image description here I think some css code needs to be added. Any help will be appreciated Ganesh |
How to create slide show in Uno WASM? Posted: 10 Oct 2021 07:57 AM PDT I am attempting to create a simple slideshow in my Uno app. What I have works in UWP, but fails in WASM. My code looks like this: public void SlideShowPageLoaded(object sender, RoutedEventArgs args) { vm.URL = vm.SlideList[CurrentIndex].URL; timer = new DispatcherTimer(); timer.Tick += dispatcherTimer_Tick; timer.Interval = new TimeSpan(0, 0, this.Interval); timer.Start(); } private void dispatcherTimer_Tick(object sender, object e) { this.GetNextIndex(); try { vm.URL = vm.SlideList[CurrentIndex].URL; } catch (Exception ex) { Debug.WriteLine(ex.Message); } } When the page loads, I set the URL for my image, then create a DispatchTimer. The Tick event just changes the URL. The image xaml looks like: <Image x:Name="imageControl" Source="{Binding URL, Mode=OneWay}" /> This works fine in UWP, nothing happens in WASM. I'm guessing that the DispatchTimer is not supported? Are there other options that I can use? |
Proper way of modeling multiple tables with different relationships in Flask SQL Alchemy Posted: 10 Oct 2021 07:57 AM PDT I am new to flask and SQL-alchemy. I am trying to create a database model that supports three tables with different relationship types. I am trying to create an Restful-API. Below is what I imagined. Here is my proposed code. Should I use db.relationship here? I was used to MySQL and creating tables does not require that code. class UserModel(db.Model): __tablename__ = "UserModel" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), nullable=False) password = db.Column(db.String(100), nullable=False) class ProductModel(db.Model): __tablename__ = "ProductModel" product_id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), nullable=False) description = db.Column(db.String(200), nullable=False) price = db.Column(db.Integer, nullable=False) stock = db.Column(db.Integer, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('UserModel.id'), nullable=False) class OrderModel(db.Model): order_id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('UserModel.id'), nullable=False) product_id = db.Column(db.Integer, db.ForeignKey('ProductModel.product_id'), nullable=False) I am not sure if this is correct because I just based it on what I understood in MySQL and when I tried adding products with ID referring to a non-existent user to "Put" is successful. Is this the proper way of modeling multiple tables with different relationships? |
node-rsa errors when trying to decrypt message Posted: 10 Oct 2021 07:57 AM PDT I have been trying to create a website for an assignment where I use node with node-rsa and javascript with jsencrypt where among other things the javascript client gets the generated by node rsa server , encrypts the message (jsencrypt) that the user has entered, sends it to the server and gets the server to decrypt it (node-rsa). Im getting the following errror Error: Error during decryption (probably incorrect key). Original error: Error: Incorrect data or key at NodeRSA.module.exports.NodeRSA.$$decryptKey (C:\Users\Ananya Krishnan\node_modules\node-rsa\src\NodeRSA.js:301:19) at NodeRSA.module.exports.NodeRSA.decrypt (C:\Users\Ananya Krishnan\node_modules\node-rsa\src\NodeRSA.js:249:21) at C:\Users\Ananya Krishnan\OneDrive\Desktop\fall 2021\infosec project vs\app.js:28:24 at Layer.handle [as handle_request] (C:\Users\Ananya Krishnan\OneDrive\Desktop\fall 2021\infosec project vs\node_modules\express\lib\router\layer.js:95:5) at next (C:\Users\Ananya Krishnan\OneDrive\Desktop\fall 2021\infosec project vs\node_modules\express\lib\router\route.js:137:13) at Route.dispatch (C:\Users\Ananya Krishnan\OneDrive\Desktop\fall 2021\infosec project vs\node_modules\express\lib\router\route.js:112:3) at Layer.handle [as handle_request] (C:\Users\Ananya Krishnan\OneDrive\Desktop\fall 2021\infosec project vs\node_modules\express\lib\router\layer.js:95:5) at C:\Users\Ananya Krishnan\OneDrive\Desktop\fall 2021\infosec project vs\node_modules\express\lib\router\index.js:281:22 at Function.process_params (C:\Users\Ananya Krishnan\OneDrive\Desktop\fall 2021\infosec project vs\node_modules\express\lib\router\index.js:335:12) at next (C:\Users\Ananya Krishnan\OneDrive\Desktop\fall 2021\infosec project vs\node_modules\express\lib\router\index.js:275:10) This is the code that gets data from html form and encrypts it function encrypted(){ var encrypt1 = new JSEncrypt(); var password = "jrjdhbefinrsivisniefnisenfiunsifniesnfi"; var a = document.getElementById("cardNumber").value; //generating hash(message digest) var md = CryptoJS.MD5(a).toString(); console.log(md); //encrypting meseage digest with the sender's private key encrypt1.setPublicKey("MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANwHBPVb3nSuO+nCtEKsRL4Wo75WAd1CC7SxUgylkV5vJgBfrjqCYKyuCPJwUwbikFpGMJE7Xi7zzYThhyGAKEsCAwEAAQ=="); md = encrypt1.encrypt(md); console.log(md); //appending message digest with the message a = a+md console.log(a); //encrypting the message a = CryptoJS.TripleDES.encrypt(a, password).toString(); console.log(a); //encrypting with the banks public key encrypt1.setPublicKey("MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANwHBPVb3nSuO+nCtEKsRL4Wo75WAd1CC7SxUgylkV5vJgBfrjqCYKyuCPJwUwbikFpGMJE7Xi7zzYThhyGAKEsCAwEAAQ=="); password = encrypt1.encrypt(password); //appending the private key a = a+password; console.log(a); document.getElementById("cardNumber").value = a; //encrypting the pin var d = document.getElementById("pin").value; d = CryptoJS.TripleDES.encrypt(d, password).toString(); document.getElementById("pin").value = d; console.log(a); console.log(d); return true; } This is the node js code const express = require('express'); const path = require("path"); const bodyParser = require("body-parser"); var CryptoJS = require("crypto-js"); const NodeRSA = require('node-rsa'); const key = new NodeRSA('-----BEGIN RSA PRIVATE KEY-----MIIBPAIBAAJBANwHBPVb3nSuO+nCtEKsRL4Wo75WAd1CC7SxUgylkV5vJgBfrjqCYKyuCPJwUwbikFpGMJE7Xi7zzYThhyGAKEsCAwEAAQJAcCrb6kFVXOJeBMOPpdWwjb945lVuSd+bnBgxneTiAONyP3KYf8nxFAVah1t260IQd6jLrXaUZgY3OSCP9s4hUQIhAP70LsceT8WKFcbxXXj16baGfZ6hiHF3eqamir/YyTeHAiEA3O4l5mb6pg/dVm04cPqbEMTFV3SAUspNe2emgTlfsh0CIQDyZG1k2ii1gocZ0bgwnHxLEKq1+pWiaxms/4HCy/TiWQIhAJJRdvBdC2un3iM7OsI4dzJn33rIQaSTLzWns/KpmONRAiEA5rq90Zb4zExtiNMH0925ZDuguiukHVgAlocaHelXwSE=-----END RSA PRIVATE KEY-----'); const key1 = new NodeRSA('-----BEGIN PUBLIC KEY-----MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANwHBPVb3nSuO+nCtEKsRL4Wo75WAd1CC7SxUgylkV5vJgBfrjqCYKyuCPJwUwbikFpGMJE7Xi7zzYThhyGAKEsCAwEAAQ==-----END PUBLIC KEY-----'); key.setOptions({encryptionScheme: 'pkcs1'}); var app = express(); app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.json()); app.use(express.static('./public')); app.get("/",(req,res)=>{ res.sendFile(__dirname + "/index.html") }) app.post('/pay',function(req,res){ console.log("DONE"); console.log(req.body); var a = req.body.cardNumber; var totLen = a.length; var pwd = a.substr(totLen-88); var msg = a.substr(0,totLen-88); const decrypted = key.decrypt(pwd).toString(); // console.log('decrypted: ', decrypted); msg = CryptoJS.TripleDES.decrypt(msg, decrypted); var plaintext = msg.toString(CryptoJS.enc.Utf8); // console.log(plaintext); var md = plaintext.substr(plaintext.length-88); var card = plaintext.substr(0,plaintext.length-88); md = key.decrypt(md).toString(); var t1 = CryptoJS.MD5(card).toString(); var md1 = t1; console.log(md); console.log(md1); console.log(md==md1); res.send("<h1><center>Details Received</center></h1><p>CARD NUMBER -"+req.body.cardNumber+"</p><p>EXPIRY DATE -"+req.body.expiryDate+"</p><p>CVV - "+req.body.cvv+"</p><p>PIN -"+req.body.pin+"</p>"); }); app.listen(5000,function(err){ console.log("Server running on PORT 3000"); }); I have tried whatever solution was already available online so any help would be greatly appreciated because I haven't been able to figure it out after spending a lot of time on it |
Roles auth in ASP .NET ends up with a HTTP400 response Posted: 10 Oct 2021 07:56 AM PDT I've tried to make simple roles authentication in ASP .NET Core. Here's how it made: [HttpPost] [ValidateAntiForgeryToken] public async Task<IActionResult> Login(LoginModel model) { if (ModelState.IsValid) { User user = await _db.Users.Include(u => u.Role).FirstOrDefaultAsync(u => u.Code == model.Code && u.Password == model.Password); if (user != null) { await Authenticate(user); return RedirectToAction("Cabinet", "Cabinet"); } ModelState.AddModelError("", "Incorrect login or password"); } return View(model); } private async Task Authenticate(User user) { var claims = new List<Claim> { new Claim(ClaimsIdentity.DefaultNameClaimType, user.Code), new Claim(ClaimsIdentity.DefaultRoleClaimType, user.Role?.Name) }; ClaimsIdentity id = new ClaimsIdentity(claims, "ApplicationCookie", ClaimsIdentity.DefaultNameClaimType, ClaimsIdentity.DefaultRoleClaimType); await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(id)); } And it works just fine when I have such a method in the CabinetController : [Authorize(Roles = "admin,user")] [HttpGet] public async Task<IActionResult> Cabinet() { //some actions here } But when I add [ValidateAntiForgeryToken] and my method looks like this: [Authorize(Roles = "admin,user")] [ValidateAntiForgeryToken] [HttpGet] public async Task<IActionResult> Cabinet() { //some actions here } After a successfull authentication I end up with HTTP 400. I think that RedirectToAction("Cabinet", "Cabinet"); throws error 400. Am I right? And if I am, why does it behave like this? |
Haskell - given list, break it into lists of identical items next to each other: [1,1,2,4,5,5] [[1,1], [2], [4], [5,5]] Posted: 10 Oct 2021 07:57 AM PDT Given list, break it into lists of identical items next to each other: [1,1,2,4,5,5] 🡪 [[1,1], [2], [4], [5,5]] And i need to do that using recursion and without higher order functions. Here what i've got so far breakList :: [Int] -> [[Int]] breakList [] = [[]] breakList [x] = [[x]] breakList (x:y:rest) | x == y = [x] : breakList (y:rest) | otherwise = [x] : breakList rest` but it does not work. |
i want to print a number greater then or equal to a specific number [closed] Posted: 10 Oct 2021 07:59 AM PDT new to coding i have this code i want it to pick a random number and i want that number to be greater then or equal to 5 and if it picked a number below it i want it to repeat the execution import java.util.Random; public class vege { public static void main(String[] args) { Random random = new Random(); System.out.println(random.nextInt(10)); } } |
Find big O notation of nested loop which have if statement inside it Posted: 10 Oct 2021 07:57 AM PDT Here a part of an algorithm to rotate an array by 180 deg System.out.println("\nHasil:"); for (i= arr.length-1; i>=0; i--) { System.out.print("["); for (j= arr.length-1; j>=0; j--){ System.out.print(arr[i][j]); if (j != 0) { System.out.print(","); } }System.out.println("]"); } I confusing determine the big o notation that have nested loop with if statement inside it. |
Why this promisified JWT.verify isn't working? Posted: 10 Oct 2021 07:58 AM PDT So I'm trying to promisify jwt.verify manually but for some reason it is not passing the decoded_token to the resolve function. This is working fine... This isn't.. PromisifiedJWTVerify.. Can someone please mention the cause of this behavior? |
Return json data from express to ajax post request Posted: 10 Oct 2021 07:57 AM PDT I'm having issues with ajax and response from express. Basically, I'm trying to send an error response to ajax post: // authentication failed return res.json({ msg: 'Incorrect OTP...' }); $.post({ type: 'POST', url: 'http://localhost:8080/verifyOTP', data: JSON.stringify({ phone: phone, m: m, otp: otp, withCredentials: true }), success: function(err) { // hack to get error msg console.log(err); }, dataType: "json", contentType: "application/json" }); I've tried the error method with $.post (no luck)...and the success function isn't working either... I've been at this for 2 days :/ I keep getting redirected to my previous page in my workflow. One thing to note, is that this current route has query parameters: http://localhost:8080/verifyOTP?phone=8191291382&m=11fe12adb039dd2abfa254291c985c1a9cc860d29c4c970594f086fd75500360.1633875932641. This may be the cause for redirect??? |
How to shuffle the order of if statements in a function in Python? Posted: 10 Oct 2021 07:57 AM PDT I have a functions in Python which has a series of if statements. def some_func(): if condition_1: return result_1 if condition_2: return result_2 ... #other if statements But I want that the order of these if statements is changed every time I call the function. Like if I call the function there can be a case when condition_1 and condition_2 both are true but since condition_1 is checked first thus I will get result_1 but I want that when I call the function second time some other random condition to be checked. (since I have only five if statements in the function therefore any one of the five conditions). So is there any way to do this like storing the conditions in a list or something. Any help will be appreciated. |
Return true/false [closed] Posted: 10 Oct 2021 07:57 AM PDT The input arrays: Stockholm, Södertälje, Uppsala, Norrtälje Karolinska Hospital, Södersjukhuset, Danderyds hospital, Södertälje Hospital, Uppsala Hospital, Norrtälje Hospital 250, 100, 50, 95, 150, 120 I am trying to deliver the below output: There is at least one hospital that has 100 doctors: true There is at least one hospital that has 300 doctors: false public static boolean searchForValue(int[] numberOfDoctors, int value1) { if (value1 == 100); { return true;} } I can get the first statement to respond as 'true' but cant utilise the return false'. I have tried using else, or repeating the input with the value as either == 300 or != 100 but neither works. How should this be done? |
Pandas compare Object columns Posted: 10 Oct 2021 07:56 AM PDT |
ruamel.yaml weird behavior with datetime like values Posted: 10 Oct 2021 07:57 AM PDT check.py import os from pathlib import Path import sys import json from ruamel.yaml import YAML yaml = YAML(pure=True) yaml.sort_base_mapping_type_on_output = None yaml.indent(mapping=2, sequence=4, offset=2) def yamlCheck(file: os.PathLike): with open(file, 'r', encoding='utf8') as yr: data = yaml.load(yr) print(data) check = json.dumps(data, indent=4, ensure_ascii=False) print(check) if __name__ == "__main__": yamlCheck('test.yaml') test1.yaml, with error. a: - b: 2 - 123 - 2000-12-26 test2.yaml, runs ok a: - b: 2 - 123 - 111-12-26 test2.yaml, runs ok a: - b: 2 - 123 - 2000-12-26111 runs ok means the code runs without error, json.dumps load the OrderedDict data well. with the output of print(data) I think ruamel.yaml has parse that xxxx-xx-xx formatted data as datetime and contruct a datetime object with it, which causes the json.dumps function's error. So what is the standard of the ruamel.yaml to decide to parse the value as datetime? |
Giving more priority to later capture group in date regex Posted: 10 Oct 2021 07:57 AM PDT I am preparing a Python regex expression to match a idiosyncratic format for timedeltas. Here are some examples of strings I will feed into the regex: 1:42.15 (1) 19.78 (1) 2-24:04 8:03.280 (1) So the overall format is hour-minute:second.second_fractions , sometimes padded with zeroes. The number in parenthesis that appears at the end of some strings must be ignored. I would like to match each line using three capture groups so that the 1st group is always the hour, the 2nd is always the minute and the 3rd is always the seconds and second fractions together. I wrote this regex: (\d{0,2})-?(\d{0,2}):?(\d{0,2}\.?\d*) This succesfully matches all examples I have tried it on, but there is an issue. Because of the greedy way regex matched, when eg the hour is missing the minute is captured by the first capture group instead of the second as I intended. That is, with the input 1:42.15 (1) I get the output ('1', '', '42.15') . What I actually wanted is the output ('', '1', '42.15') - the minute always corresponding to the second capture group. How can I modify the priorities of the capture groups to achieve this behaviour? |
Value of i in loop keeps getting overridden even after changing explicitly Posted: 10 Oct 2021 07:58 AM PDT This code is for reference. Working on a code with a similar implementation. I wanted to override the value of i inside the loop: def function(): num = 10 for i in range(num): print(i) if (i % 2 == 0): i = i + 2 else: i = i + 1 The actual output fro the above code : 0 1 2 3 4 5 6 7 8 9 I expected that the value of i will be updated inside the loop, since I have different values to add to i depending on the condition, whereas it was getting overridden at the start of the loop to be in sequence. Can anyone explain how can i stop this? I wanted to update the value of i inside the loop logic. |
Unable to locate xpath on Selenium.htmlunit.HtmlUnitDriver Posted: 10 Oct 2021 07:57 AM PDT I'm trying to click the "PLAY DEMO" button and and then click the "CONFIRM AGE" button on a pop-up screen on this website https://www.spribe.co/games/aviator This will then allow me to enter this page: https://aviator-next.spribegaming.com/?user=1180421&token=3152cf089ba8b4c2d08591154848e680a0f299e6462199ea6b9aa6de3613dbb2〈=EN¤cy=USD&return_url=https://spribe.co/games&operator=demo But it is not happening. I tried relative xpath and absolute expath. On my code below I'm able to open a website and find the title and also click the cookies confirm button. Can anyone help? import java.util.logging.Level; import java.util.logging.Logger; import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.StdErrLog; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.htmlunit.HtmlUnitDriver; import com.gargoylesoftware.htmlunit.BrowserVersion; public class CraigTRYtesting { public static void main(String[] args) { WebDriver driver = new HtmlUnitDriver(BrowserVersion.CHROME); driver.manage().timeouts().implicitlyWait(60,TimeUnit.SECONDS); Logger logger = Logger.getLogger(""); logger.setLevel(Level.OFF); StdErrLog logG = new StdErrLog(); logG.setLevel(StdErrLog.LEVEL_OFF); Log.setLog(logG); ((HtmlUnitDriver) driver).setJavascriptEnabled(true); driver.get("https://www.spribe.co/games/aviator"); System.out.println("WEBSITE TITTLE " + driver.getTitle()); System.out.println("WEBSITE FOUND"); //COOKIE driver.findElement(By.xpath("//html[1]/body[1]/app-root[1]/app-layout[1]/app-cookie-accept[1]/div[1]/div[1]/button[1]")).click(); System.out.println("COOKIE BUTTON CLICKED"); //CLICK BUTTON driver.findElement(By.xpath("//html[1]/body[1]/app-root[1]/app-layout[1]/app-game-details[1]/div[1]/section[1]/div[1]/div[1]/div[1]/a[1]/button[1]")).click(); System.out.println("DEMO BUTTON CLICKED"); //CONFIRM AGE BUTTON driver.findElement(By.xpath("//html[1]/body[1]/ngb-modal-window[1]/div[1]/div[1]/app-age-permission-modal[1]/div[3]/button[1]")).click(); System.out.println("CONFIRM AGE BUTTON CLICKED"); //ROUND COUNTER String counter = driver.findElement(By.xpath("///div[@class='coefficient coefficient-sm']")).getText(); System.out.println("ROUND COUNTER FOUND " + counter); driver.quit(); } } THE ROUND COUNTER WILL BE ON THIS PAGE: https://aviator-next.spribegaming.com/?user=1180421&token=3152cf089ba8b4c2d08591154848e680a0f299e6462199ea6b9aa6de3613dbb2〈=EN¤cy=USD&return_url=https://spribe.co/games&operator=demo AFTER CONFIRMING THE AGE. |
python list referred as true and false in the same time [duplicate] Posted: 10 Oct 2021 07:58 AM PDT i was coding a simple view in django when the result is always wrong then i noticed the if statement is triggering when it should not i inspected the code to realize it was correct here is the code i tested >>> li = [""] >>> li == True False >>> if li: ... print("li is true") ... li is true >>> li == False False >>> li == None False >>> li == True False im so confused?? li == False is false should it not return true? |
how to enable hotspot in the action center of win10 Posted: 10 Oct 2021 07:58 AM PDT for some reasons, I dont want to run a batch file to start a wifi hotspot. so yeah, I choose to enable hotspot in the action center with Click command in autohotkey. (I want to press Numpad0 and NumpadEnter to enable it) (codes below ain't working) ~Numpad0 & NumpadEnter:: CoordMode MouseGetPos , Screen MouseGetPos, X, Y Send #a MouseClick , Left, 1496, 796, 1, 10, U Send #a MouseClick , L, X, Y, 0, 10, , Return The problem is, autohotkey can't neither move the mouse cursor onto the button of action center for unknown reasons nor then click it. Any help is appreciated. |
LEFT OUTER JOIN with 'field IS NULL' in WHERE works as INNER JOIN Posted: 10 Oct 2021 07:58 AM PDT Today I've faced some unexplainable (for me) behavior in PostgreSQL — LEFT OUTER JOIN does not return records for main table (with nulls for joined one fields) in case the joined table fields are used in WHERE expression. To make it easier to grasp the case details, I'll provide an example. So, let's say we have 2 tables: item with some goods, and price , referring item , with prices for the goods in different years: CREATE TABLE item( id INTEGER PRIMARY KEY, name VARCHAR(50) ); CREATE TABLE price( id INTEGER PRIMARY KEY, item_id INTEGER NOT NULL, year INTEGER NOT NULL, value INTEGER NOT NULL, CONSTRAINT goods_fk FOREIGN KEY (item_id) REFERENCES item(id) ); The table item has 2 records (TV set and VCR items), and the table price has 3 records, a price for TV set in years 2000 and 2010, and a price for VCR for year 2000 only: INSERT INTO item(id, name) VALUES (1, 'TV set'), (2, 'VCR'); INSERT INTO price(id, item_id, year, value) VALUES (1, 1, 2000, 290), (2, 1, 2010, 270), (3, 2, 2000, 770); -- no price of VCR for 2010 Now let's make a LEFT OUTER JOIN query, to get prices for all items for year 2010: SELECT i.*, p.year, p.value FROM item i LEFT OUTER JOIN price p ON i.id = p.item_id WHERE p.year = 2010 OR p.year IS NULL; For some reason, this query will return a results only for TV set, which has a price for this year. Record for VCR is absent in results: id | name | year | value ----+--------+------+------- 1 | TV set | 2010 | 270 (1 row) After some experimenting, I've found a way to make the query to return results I need (all records for item table, with nulls in the fields of joined table in case there are no mathing records for the year. It was achieved by moving year filtering into a JOIN condition: SELECT i.*, p.year, p.value FROM item i LEFT OUTER JOIN ( SELECT * FROM price WHERE year = 2010 -- <= here I filter a year ) p ON i.id = p.item_id; And now the result is: id | name | year | value ----+--------+------+------- 1 | TV set | 2010 | 270 2 | VCR | | (2 rows) My main question is — why the first query (with year filtering in WHERE ) does not work as expected, and turns instead into something like INNER JOIN ? I'm severely blocked by this issue on my current project, so I'll be thankful about tips/hints on the next related questions too: - Are there any other options to achieve the proper results?
- ... especially — easily translatable to Django's ORM queryset?
Upd: @astentx suggested to move filtering condition directly into JOIN (and it works too): SELECT i.*, p.year, p.value FROM item i LEFT OUTER JOIN price p ON i.id = p.item_id AND p.year = 2010; Though, the same as my first solution, I don't see how to express it in terms of Django ORM querysets. Are there any other suggestions? |
MapStruct. Map from List<Integer> to List<Object> Posted: 10 Oct 2021 07:58 AM PDT Source sourceDto { List<Integer> ids; } Target targetDto { List<CustomObject> myObjects; } Custom Object CustomObject { Integer id, String name, String slug } Note I am using an external API that always need name and slug initialised to an empty string. I mean, sending only the ID will no work. That's why I am trying so with Mapper |
Execution of simple MAGMA inversion matrix distributed on 2 GPU cards Posted: 10 Oct 2021 07:57 AM PDT I have below a simple program from MAGMA doc which inverses a large matrix on a GPU card. #include <stdio.h> #include <cuda.h> #include "magma_v2.h" #include "magma_lapack.h" int main() { //double *d_lA[MagmaMaxGPUs]; magma_init (); // initialize Magma magma_queue_t queue=NULL; magma_int_t dev=0; magma_queue_create(dev ,&queue ); double gpu_time , *dwork; // dwork - workspace magma_int_t ldwork; // size of dwork magma_int_t *piv , info; // piv - array of indices of inter - magma_int_t m = 20000; // changed rows; a - mxm matrix magma_int_t mm=m*m; // size of a, r, c double *a; // a- mxm matrix on the host double *d_a; // d_a - mxm matrix a on the device double *d_r; // d_r - mxm matrix r on the device double *d_c; // d_c - mxm matrix c on the device magma_int_t ione = 1; magma_int_t ISEED [4] = { 0,0,0,1 }; // seed magma_int_t err; const double alpha = 1.0; // alpha =1 const double beta = 0.0; // beta=0 ldwork = m * magma_get_dgetri_nb( m ); // optimal block size // allocate matrices err = magma_dmalloc_cpu( &a , mm ); // host memory for a err = magma_dmalloc( &d_a , mm ); // device memory for a err = magma_dmalloc( &d_r , mm ); // device memory for r err = magma_dmalloc( &d_c , mm ); // device memory for c err = magma_dmalloc( &dwork , ldwork );// dev. mem. for ldwork piv=( magma_int_t *) malloc(m*sizeof(magma_int_t ));// host mem. // generate random matrix a // for piv lapackf77_dlarnv (&ione ,ISEED ,&mm ,a); // randomize a printf("here1\n"); magma_dsetmatrix( m, m, a, m, d_a , m, queue); // copy a -> d_a magmablas_dlacpy(MagmaFull , m, m, d_a , m, d_r ,m,queue);//d_a ->d_r // find the inverse matrix: d_a*X=I using the LU factorization // with partial pivoting and row interchanges computed by // magma_dgetrf_gpu; row i is interchanged with row piv(i); // d_a -mxm matrix; d_a is overwritten by the inverse gpu_time = magma_sync_wtime(NULL); printf("here2\n"); //magma_dgetrf_mgpu(2, m, m, d_lA, m, piv, &info); magma_dgetrf_gpu(m, m, d_a, m, piv, &info); magma_dgetri_gpu(m, d_a, m, piv, dwork, ldwork, &info); printf("here3\n"); gpu_time = magma_sync_wtime(NULL)-gpu_time; magma_dgemm(MagmaNoTrans ,MagmaNoTrans ,m,m,m,alpha ,d_a ,m, d_r ,m,beta ,d_c ,m,queue); // multiply a^-1*a printf("magma_dgetrf_gpu + magma_dgetri_gpu time: %7.5f sec.\ \n",gpu_time ); magma_dgetmatrix( m, m, d_c , m, a, m, queue); // copy d_c ->a printf("upper left corner of a^-1*a:\n"); magma_dprint( 4, 4, a, m ); // part of a^-1*a free(a); // free host memory free(piv); // free host memory magma_free(d_a); // free device memory magma_free(d_r); // free device memory magma_free(d_c); // free device memory magma_queue_destroy(queue); // destroy queue magma_finalize (); // finalize Magma return 0; } Everything works fine, the short runtime expected is checked : Now if you look at the code above, you will see that I have commented 2 lines : //double *d_lA[MagmaMaxGPUs]; and //magma_dgetrf_mgpu(2, m, m, d_lA, m, piv, &info); Indeed, uncommenting these 2 lines and replacing magma_dgetrf_gpu(m, m, d_a, m, piv, &info); by magma_dgetrf_mgpu(2, m, m, d_lA, m, piv, &info); are done to make run this code distributed on 2 GPU cards (on 2 GPU cards RTX A6000). I have set MAGMA_NUM_GPUS=2 from MAGMA doc. So I have the code : #include <stdio.h> #include <cuda.h> #include "magma_v2.h" #include "magma_lapack.h" int main() { double *d_lA[MagmaMaxGPUs]; magma_init (); // initialize Magma magma_queue_t queue=NULL; magma_int_t dev=0; magma_queue_create(dev ,&queue ); double gpu_time , *dwork; // dwork - workspace magma_int_t ldwork; // size of dwork magma_int_t *piv , info; // piv - array of indices of inter - magma_int_t m = 20000; // changed rows; a - mxm matrix magma_int_t mm=m*m; // size of a, r, c double *a; // a- mxm matrix on the host double *d_a; // d_a - mxm matrix a on the device double *d_r; // d_r - mxm matrix r on the device double *d_c; // d_c - mxm matrix c on the device magma_int_t ione = 1; magma_int_t ISEED [4] = { 0,0,0,1 }; // seed magma_int_t err; const double alpha = 1.0; // alpha =1 const double beta = 0.0; // beta=0 ldwork = m * magma_get_dgetri_nb( m ); // optimal block size // allocate matrices err = magma_dmalloc_cpu( &a , mm ); // host memory for a err = magma_dmalloc( &d_a , mm ); // device memory for a err = magma_dmalloc( &d_r , mm ); // device memory for r err = magma_dmalloc( &d_c , mm ); // device memory for c err = magma_dmalloc( &dwork , ldwork );// dev. mem. for ldwork piv=( magma_int_t *) malloc(m*sizeof(magma_int_t ));// host mem. // generate random matrix a // for piv lapackf77_dlarnv (&ione ,ISEED ,&mm ,a); // randomize a printf("here1\n"); magma_dsetmatrix( m, m, a, m, d_a , m, queue); // copy a -> d_a magmablas_dlacpy(MagmaFull , m, m, d_a , m, d_r ,m,queue);//d_a ->d_r // find the inverse matrix: d_a*X=I using the LU factorization // with partial pivoting and row interchanges computed by // magma_dgetrf_gpu; row i is interchanged with row piv(i); // d_a -mxm matrix; d_a is overwritten by the inverse gpu_time = magma_sync_wtime(NULL); printf("here2\n"); magma_dgetrf_mgpu(2, m, m, d_lA, m, piv, &info); magma_dgetri_gpu(m, d_a, m, piv, dwork, ldwork, &info); printf("here3\n"); gpu_time = magma_sync_wtime(NULL)-gpu_time; magma_dgemm(MagmaNoTrans ,MagmaNoTrans ,m,m,m,alpha ,d_a ,m, d_r ,m,beta ,d_c ,m,queue); // multiply a^-1*a printf("magma_dgetrf_gpu + magma_dgetri_gpu time: %7.5f sec.\ \n",gpu_time ); magma_dgetmatrix( m, m, d_c , m, a, m, queue); // copy d_c ->a printf("upper left corner of a^-1*a:\n"); magma_dprint( 4, 4, a, m ); // part of a^-1*a free(a); // free host memory free(piv); // free host memory magma_free(d_a); // free device memory magma_free(d_r); // free device memory magma_free(d_c); // free device memory magma_queue_destroy(queue); // destroy queue magma_finalize (); // finalize Magma return 0; } At the begin of execution, both GPU seem to be running but I get quickly the following errors : $ ./main_magma_double_example.exe here1 here2 CUDA runtime error: an illegal memory access was encountered (700) in magma_dgetrf_mgpu at /home/fab/magma-2.6.1/src/dgetrf_mgpu.cpp:183 CUDA runtime error: an illegal memory access was encountered (700) in magma_dgetrf_mgpu at /home/fab/magma-2.6.1/src/dgetrf_mgpu.cpp:183 CUDA runtime error: an illegal memory access was encountered (700) in magma_dgetrf_mgpu at /home/fab/magma-2.6.1/src/dgetrf_mgpu.cpp:192 CUDA runtime error: an illegal memory access was encountered (700) in magma_dgetrf_mgpu at /home/fab/magma-2.6.1/src/dgetrf_mgpu.cpp:193 CUDA runtime error: an illegal memory access was encountered (700) in magma_dgetrf_mgpu at /home/fab/magma-2.6.1/src/dgetrf_mgpu.cpp:192 CUDA runtime error: an illegal memory access was encountered (700) in magma_dgetrf_mgpu at /home/fab/magma-2.6.1/src/dgetrf_mgpu.cpp:193 here3 CUDA runtime error: an illegal memory access was encountered (700) in magma_sync_wtime at /home/fab/magma-2.6.1/control/magma_timer.cpp:98 magma_dgetrf_gpu + magma_dgetri_gpu time: 1.07300 sec. CUBLAS error: memory mapping error (11) in main at example_double_MAGMA_NVIDIA.cpp:57 upper left corner of a^-1*a: [ 0.1206 0.4128 0.9920 0.4738 0.6438 0.1080 0.1855 0.9998 0.0623 0.0777 0.2275 0.1513 0.4903 0.1876 0.8492 0.3984 ]; CUDA runtime error: an illegal memory access was encountered (700) in main at example_double_MAGMA_NVIDIA.cpp:62 CUDA runtime error: an illegal memory access was encountered (700) in main at example_double_MAGMA_NVIDIA.cpp:63 CUDA runtime error: an illegal memory access was encountered (700) in main at example_double_MAGMA_NVIDIA.cpp:64 CUDA runtime error: an illegal memory access was encountered (700) in main at example_double_MAGMA_NVIDIA.cpp:65 We can see that inversion is wrong. I don't know the origin of these error messages. Compilation done by Makefile.inc : CXX = nvcc -O3 LAPACK = /opt/intel/oneapi/mkl/latest/lib/intel64 MAGMA = /usr/local/magma INCLUDE_CUDA=/usr/local/cuda/include LIBCUDA=/usr/local/cuda/lib64 CXXFLAGS = -c -I${MAGMA}/include -I${INCLUDE_CUDA} -lpthread LDFLAGS = -L${LAPACK} -lmkl_intel_lp64 -L${LIBCUDA} -lcuda -lcudart -lcublas -L${MAGMA}/lib -lmagma -lpthread SOURCES = example_double_MAGMA_NVIDIA.cpp EXECUTABLE = main_magma_double_example.exe What can I try to exploit both GPU cards at the same time? PS: I must mention there is a NVLink hardware component between the 2 GPU cards. |
Angular: Run ChangeDetection after element is in DOM? Posted: 10 Oct 2021 07:57 AM PDT I have a component inside a modal dialog. I notice the component's @Inputs fire before the element is actually in the DOM! This causes undesirable layout behavior. How can I ensure the element is in the DOM before running change-detection? Notes: I am currently using this hack. I assume there is a more elegant way: class MyDialog implements AfterViewInit { @ViewChild(ChartElement) ChartElement chart; @override void ngAfterViewInit() async { while(document.getElementById('#myElement') == null) { await Future.delayed(Duration(milliseconds: 100)); } chart.draw(); } } |
Linux Evdev Poll Lag Posted: 10 Oct 2021 07:58 AM PDT I'm on a laptop with 2 connected keyboards (built-in and USB). I'm obtaining these connected keyboards with libudev and using epoll to poll them for input via the evdev interface: // Compile with $(gcc udev.c -ludev) #include <stdbool.h> #include <stdio.h> #include <string.h> #include <sys/epoll.h> #include <sys/poll.h> #include <sys/types.h> #include <fcntl.h> #include <unistd.h> #include <linux/input.h> #include <time.h> #include <libudev.h> #define BILLION 1000000000L long timespec_diff(struct timespec *start, struct timespec *end) { return (BILLION * (end->tv_sec - start->tv_sec)) + (end->tv_nsec - start->tv_nsec); } bool want_to_run = true; int main(int argc, char *argv[]) { int epoll_fd = epoll_create1(0); struct udev *udev_obj = udev_new(); struct udev_enumerate *udev_enum = udev_enumerate_new(udev_obj); udev_enumerate_add_match_subsystem(udev_enum, "input"); udev_enumerate_scan_devices(udev_enum); struct udev_list_entry *udev_entries = udev_enumerate_get_list_entry(udev_enum); struct udev_list_entry *udev_entry = NULL; udev_list_entry_foreach(udev_entry, udev_entries) { char const *udev_entry_syspath = udev_list_entry_get_name(udev_entry); struct udev_device *device = udev_device_new_from_syspath(udev_obj, udev_entry_syspath); char const *dev_prop = \ udev_device_get_property_value(device, "ID_INPUT_KEYBOARD"); if (dev_prop != NULL && strcmp(dev_prop, "1") == 0) { const char *dev_path = udev_device_get_devnode(device); if (dev_path != NULL) { int dev_fd = open(dev_path, O_RDWR | O_NONBLOCK); struct epoll_event event = {}; event.events = EPOLLIN; event.data.fd = dev_fd; epoll_ctl(epoll_fd, EPOLL_CTL_ADD, dev_fd, &event); } } udev_device_unref(device); } udev_enumerate_unref(udev_enum); struct timespec prev_timespec = {}; clock_gettime(CLOCK_MONOTONIC_RAW, &prev_timespec); while (want_to_run) { struct epoll_event epoll_events[5] = {0}; int num_epoll_events = epoll_wait(epoll_fd, epoll_events, 5, 0); for (int epoll_event_i = 0; epoll_event_i < num_epoll_events; ++epoll_event_i) { int dev_fd = epoll_events[epoll_event_i].data.fd; struct input_event dev_events[4] = {0}; int dev_event_bytes_read = read(dev_fd, dev_events, sizeof(dev_events)); int num_dev_events = dev_event_bytes_read / sizeof(dev_events[0]); for (int dev_event_i = 0; dev_event_i < num_dev_events; ++dev_event_i) { int dev_event_type = dev_events[dev_event_i].type; int dev_event_code = dev_events[dev_event_i].code; int dev_event_value = dev_events[dev_event_i].value; bool is_released = (dev_event_type == EV_KEY ? dev_event_value == 0 : false); bool is_down = (dev_event_type == EV_KEY ? dev_event_value == 1 : false); bool was_down = (dev_event_type == EV_KEY ? dev_event_value == 2 : false); bool w = (dev_event_code == KEY_W); bool a = (dev_event_code == KEY_A); bool s = (dev_event_code == KEY_S); bool d = (dev_event_code == KEY_D); bool q = (dev_event_code == KEY_Q); bool e = (dev_event_code == KEY_E); bool up = (dev_event_code == KEY_UP); bool down = (dev_event_code == KEY_DOWN); bool left = (dev_event_code == KEY_LEFT); bool right = (dev_event_code == KEY_RIGHT); bool escape = (dev_event_code == KEY_ESC); bool space = (dev_event_code == KEY_SPACE); bool enter = (dev_event_code == KEY_ENTER); bool ctrl = (dev_event_code == KEY_LEFTCTRL); if (q) want_to_run = false; } } struct timespec end_timespec = {}; clock_gettime(CLOCK_MONOTONIC_RAW, &end_timespec); printf("ns per frame: %lu\n", timespec_diff(&prev_timespec, &end_timespec)); prev_timespec = end_timespec; } return 0; } Experimenting by entering keys on each keyboard I experience some serious lag/stall in the following circumstances (I encourage you to compile and try yourself): - If I start entering keys on one keyboard and then switch to the other, the program stalls briefly.
- If I simultaneously enter keys on each keyboard the program stalls indefinitely until I stop entering keys.
I have tested with different keyboards and experience the same result. What is going on here? UPDATE I think it may be specific to my host environment (Ubuntu 20.04) as when I spam keys on each keyboard inside another program, e.g. gnome terminal, firefox, etc. the same stalling and delay happens. If I wail on the keys it's some serious stall (the cpu fan goes off and everything) It only happens for keyboards it seems as moving an external mouse and trackpad together causes no issues. Running under Intel VTune hotspots analysis shows that epoll_wait() is the source of the stall (no surprises there) |
Python type-checking Protocols and Descriptors Posted: 10 Oct 2021 07:57 AM PDT I observe a behavior about typing.Protocol when Descriptors are involved which I do not quite fully understand. Consider the following code: import typing as t T = t.TypeVar('T') class MyDescriptor(t.Generic[T]): def __set_name__(self, owner, name): self.name = name def __set__(self, instance, value: T): instance.__dict__[self.name] = value def __get__(self, instance, owner) -> T: return instance.__dict__[self.name] class Named(t.Protocol): first_name: str class Person: first_name = MyDescriptor[str]() age: int def __init__(self): self.first_name = 'John' def greet(obj: Named): print(f'Hello {obj.first_name}') person = Person() greet(person) Is the class Person implicitly implementing the Named protocol? According to mypy, it isn't: error: Argument 1 to "greet" has incompatible type "Person"; expected "Named" note: Following member(s) of "Person" have conflicts: note: first_name: expected "str", got "MyDescriptor[str]" I guess that's because mypy quickly concludes that str and MyDescriptor[str] are simply 2 different types. Fair enough. However, using a plain str for first_name or wrapping it in a descriptor that gets and sets a str is just an implementation detail. Duck-typing here tells me that the way we will use first_name (the interface) won't change. In other words, Person implements Named . As a side note, PyCharm's type-checker does not complain in this particular case (though I am not sure if it's by design or by chance). According to the intended use of typing.Protocol , is my understanding wrong? |
S3 bucket policy to deny all except a particular AWS service role and IAM role Posted: 10 Oct 2021 07:57 AM PDT Can you write an s3 bucket policy that will deny access to all principals except a particular IAM role and AWS service role (e.g. billingreports.amazonaws.com). I have tried using 'Deny' with 'NotPrincipal', but none of the below examples work as I don't think the ability to have multiple types of principals is supported by AWS? This allows you to save the policy but locks out the bucket (warning: only root user can then update policy to unlock) "Effect": "Deny", "NotPrincipal": { "AWS": [ "arn:aws:iam::<account_id>:root", "arn:aws:iam::<account_id>:role/specialBillingRole" ], "Service": "billingreports.amazonaws.com" } Therefore I am trying to use conditions but can't find the right combinations that will work. Here is an example policy. { "Version": "2008-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": "billingreports.amazonaws.com" }, "Action": "s3:*", "Resource": "arn:aws:s3:::my-bucket/*" }, { "Sid": "", "Effect": "Deny", "Principal": "*", "Action": "s3:*", "Resource": [ "arn:aws:s3:::my-bucket/*", "arn:aws:s3:::my-bucket" ], "Condition": { "StringNotLike": { "aws:PrincipalArn": [ "arn:aws:iam::<account_id>:role/specialBillingRole", "billingreports.amazonaws.com", "<account_id>" ] } } } ] } UPDATED the question as per some comment suggestions. 2nd UPDATE Also tried the below, which still gives access to all roles/users in the account (can't use wildcards in the Principal ). { "Effect": "Deny", "Principal": { "AWS": "arn:aws:iam::<account_id>:root" }, "Action": "s3:*", "Resource": [ "arn:aws:s3:::my-bucket/*", "arn:aws:s3:::my-bucket" ], "Condition": { "ArnNotEquals": { "aws:PrincipalArn": [ "arn:aws:iam::<account_id>:role/specialBillingRole", "<account_id>" ] } } } |
Add client certificate to .NET Core HttpClient Posted: 10 Oct 2021 07:59 AM PDT I was playing around with .NET Core and building an API that utilizes payment APIs. There's a client certificate that needs to be added to the request for two-way SSL authentication. How can I achieve this in .NET Core using HttpClient ? I have looked at various articles and found that HttpClientHandler doesn't provide any option to add client certificates. |
No comments:
Post a Comment