Open Side Menu Go to the Top
Register
Programming homework and newbie help thread Programming homework and newbie help thread

05-23-2018 , 09:15 PM
it sounds like ajax is exactly what you want, not sure what you mean about it being deprecated

for your purposes, vanilla JS with ajax should work.

and asynchronous request should be fine, but it depends on what exactly what you need
Programming homework and newbie help thread Quote
05-23-2018 , 09:19 PM
"asynchronous" does not mean it blocks until it receives a response, just fyi
Programming homework and newbie help thread Quote
05-23-2018 , 10:16 PM
He wants it to block until the response, how to make it do that is the question. I think?

Last edited by _dave_; 05-23-2018 at 10:32 PM.
Programming homework and newbie help thread Quote
05-23-2018 , 11:02 PM
Quote:
Originally Posted by RustyBrooks
Sounds like he has to, because the consumer/called of foo is expecting a value, not a promise etc.
Yep exactly. The setup was under the assumption that foo would be written entirely in JavaScript, but some people like myself prefer python.
Programming homework and newbie help thread Quote
06-14-2018 , 05:29 PM
I think I'm pretty well prepared to just ignore sonarqube now, unless I can get whoever admins it at our company to make some pretty broad exceptions.

It's on my ass about the following (all of which it calls "bugs")
* using python2 style prints (i.e. print "hi" instead of print("hi") - this is a python2 project)
* using <i> instead of <em> and <b> instead of <strong>

There are hundreds of each of these and it's giving my project a "C" for "Bugs" because of it.

I mean, ok, changing these is fine but it seems kind of silly to make 1000 lines of changes across hundreds of files for this.
Programming homework and newbie help thread Quote
06-14-2018 , 07:15 PM
This is at work right? Probably couldn't hurt to get some standards in place about what is acceptable and what isn't so you don't have to go through this wak-a-mole every time to submit something.
Programming homework and newbie help thread Quote
06-14-2018 , 07:20 PM
Heh, I don't know why I posted this in the homework thread. Misclick I guess.

Yeah, this is at work. I'm the first adopter for my language so I think I'm going to have to see if we can get the rules re-written a bit. The only number that matters in the immediate future is vulnerabilities per LOC, of which so far I have none. Uh, after I marked some false positives. For example it considers any "hard-coded IP" to be a security vulnerability, even if it's like 127.0.0.1 (like in the case of a default value for the "list of IPs allowed to connect") or something that looks like an IP but isn't (some version numbers are like 1.1.0.12 which looks vaguely like an IP) or even something that is just in the middle of a string (like a SQL query that is looking for results for some specific IPs)

I looked over the Java projects and some of it looks OK and some looks crazy. Like any function that reads from a file seems to get flagged with "might be reading from user supplied filename" and I don't think it's actually inspecting the code to see if that's a possibility or not (and maybe you don't care - a lot of our Java stuff runs on machines that are essentially owned by the customer, they can read wtf ever they want)
Programming homework and newbie help thread Quote
06-15-2018 , 06:25 PM
I wonder how much of it was written for the author's own use cases which are not being generalized in a sane manner?
Programming homework and newbie help thread Quote
06-15-2018 , 07:59 PM
Quote:
Originally Posted by kerowo
I wonder how much of it was written for the author's own use cases which are not being generalized in a sane manner?
I guess? I don't know a lot about it but it's a prominent piece of software in the field I think. We're paying them a boatload of money.

It reminds me of almost all the linting software I've ever used - mostly good ideas but a few terrible ones, and the terrible ones are so noisy as to drown out the rest. If I can't get the terrible ones nixed then essentially it'll be useless to me because either I'll have to fix 1000 "bugs" that aren't broken, or I'll have to wade through 1000 noisy bug reports to get to ones that might be useful. I'm probably not going to do either of those.

These linters and syntax checkers and static analysis tools sort of remind me of vegetarianism or veganism. It would be easier to get, say, 50% of people to eat 50% less meat than it would be to get 10% of people to eat no meat. The net benefit would be a lot greater, but you'd have to relax your idealism somewhat.

Something I find Super ****ing Annoying about linters is the cross-language problems. Like... python really prefers that class attributes be lower_case_words_with_underscores but javascript linters prefer camelCase. So you either have to do automated conversion at some layer in your API, or one of you has to have the linter be pissed at you all the time.

I use pep8, with the most annoying 10% of rules off.
Programming homework and newbie help thread Quote
07-29-2018 , 08:46 PM
2 sql questions:

If I have a table with 10 results, and I run a query that returns 5, and left join another query that would return 10, do I get all 10 or just the five from the first query?

Follow up, if you do multiple left joins string together, does each successive query only return the results matching the consecutive queries before it, or just the query immediately preceding it?
Programming homework and newbie help thread Quote
07-29-2018 , 09:23 PM
There is not a pat answer to your question, it depends on the join conditions and the data in the joined tables. If you left join on a table that had 3 rows for every id from the join clause, then left joining would make the query set 3x larger. Left join just means "include the left row even if there are no matching rows in the right" otherwise normal join rules apply
Programming homework and newbie help thread Quote
07-30-2018 , 12:58 AM
Yeah, left joins return at least one row for every row in the first query, but may return more if there are more matching rows in the second set. For example, if it's a list of men and a list of women and the query is a left join based on who has slept with whom, there will be more than one row returned for me (thinly veiled brag) but even virgin men will get one row returned. Rusty's last line above is the same thing restated differently.

By the way, you can return at least one row for every row in BOTH subqueries if you want, via FULL OUTER JOIN. LEFT JOIN is shorthand for LEFT OUTER JOIN.
Programming homework and newbie help thread Quote
07-30-2018 , 01:23 PM
Using your example, if I wanted only non-virgin men, plus the first woman they had slept with, plus the first car they owned (which some of them may not have owned cars), I’d just do two inner joins? And the men who hadn’t owned cars would just get a NULL?
Programming homework and newbie help thread Quote
07-30-2018 , 04:19 PM
You might be tempted to do this, but it probably won't work
Code:
select * 
from men m
join women_slept_with w using (man_id)
left join car_owned co using (man_id)
where w.slept_rank=1 and co.owned_rank=1
Because the co.owned_rank is outside of the left join, so it will filter rows *after* the join, and exclude anyone who doesn't have a car. You could probably do it like this

Code:
select * 
from men m
join women_slept_with w using (man_id)
left join car_owned co using (man_id)
where w.slept_rank=1 and (co.owned_rank=1 or co.owned_rank is null)
Because when the dude doesn't have a car, all the fields from co will be null.

You could also put the logic into the join filter

Code:
select * 
from men m
join women_slept_with w using (man_id)
left join car_owned co on (m.man_id=co.man_id and co.owned_rank=1)
where w.slept_rank=1
This works because the left join guarantees at least one row, regardless of whether the filter criteria is met.

There are *lots* of other ways you could do it to, and depending on your database type and your indexes some might work better than others.

(Note that this filters out the virgins because they won't have any records in women_slept_with at all, much less any with rank=1 and we aren't using a left join on that table)
Programming homework and newbie help thread Quote
07-30-2018 , 04:19 PM
(Note that it's really easy to make mistakes when writing SQL queries so I could easily have some above)
Programming homework and newbie help thread Quote
07-30-2018 , 05:26 PM
Yeah, so the trouble is the request to get only the first row that matches the condition (first woman slept with, first car owned), which requires some extra complication beyond just left joins. So Rusty added columns to track rank explicitly (slept_rank, owned_rank), which is one way to solve that problem.

In Postgres (and I think Oracle and MS SQL with some slight syntax changes, but not Mysql?) you can also use window functions, without needing to manually set the value of a column:

Code:
twoplustwo=> select * from person;
 id |  name   | gender 
----+---------+--------
  1 | Bob     | M
  2 | Alice   | F
  3 | Mallory | M
  4 | Carol   | F

twoplustwo=> select * from partners;
 id | person_id | partner_id |        date         
----+-----------+------------+---------------------
  1 |         1 |          2 | 2018-08-01 00:00:00
  2 |         1 |          4 | 2018-08-04 00:00:00
  3 |         2 |          4 | 2018-07-23 00:00:00

twoplustwo=> select * from cars;
 id | person_id |  make  |   model   |    purchase_date    
----+-----------+--------+-----------+---------------------
  1 |         1 | Subaru | Impreza   | 2018-01-01 00:00:00
  2 |         1 | Subaru | Forrester | 2014-09-01 00:00:00
  3 |         2 | Ford   | F-150     | 2003-06-14 00:00:00
  4 |         3 | Toyota | Camry     | 2013-05-05 00:00:00
Code:
WITH ranked_partners AS (
    SELECT *,  RANK() OVER (PARTITION BY person_id ORDER BY date) as rank
    FROM partners
),

ranked_cars AS (
    SELECT *,  RANK() OVER (PARTITION BY person_id ORDER BY purchase_date) as rank
    FROM cars
)

SELECT p.id, p.name, p.gender,
    rp.partner_id as partner_id, sp.name as partner_name, 
    rp.date as partner_date, 
    rc.make, rc.model, rc.purchase_date
FROM
    person p
JOIN ranked_partners rp ON (rp.person_id=p.id AND rp.rank=1)
JOIN person sp ON (sp.id=rp.partner_id)
LEFT JOIN ranked_cars rc ON (rc.person_id=p.id AND rc.rank=1)
WHERE p.gender='M';

 id | name | gender | partner_id | partner_name |    partner_date     |  make  |   model   |    purchase_date    
----+------+--------+------------+--------------+---------------------+--------+-----------+---------------------
  1 | Bob  | M      |          2 | Alice        | 2018-08-01 00:00:00 | Subaru | Forrester | 2014-09-01 00:00:00
I think there's probably some kind of subquery or temporary table that would make this doable in MySQL but I'm not sure off the top of my head what the best approach is.
Programming homework and newbie help thread Quote
07-30-2018 , 05:29 PM
Yeah I did not really want to get into things like partition by, so I punted and assumed we had them enumerated.

There are techniques for this for nearly every SQL database but some of them are really heinous.
Programming homework and newbie help thread Quote
07-30-2018 , 05:35 PM
Mysql 8 has window functions, and it looks like so do newer versions of MariaDB. Not sure about amazon aurora. So it seems like that's probably a good way to learn to do it?
Programming homework and newbie help thread Quote
07-30-2018 , 05:41 PM
LOL version "8"

Never change, software guys. (The previous version was 5.7, I am guessing they decided to just call 5.8 version "8" to make it not seem behind other databases or something. )
Programming homework and newbie help thread Quote
07-30-2018 , 05:46 PM
Yeah, I didn't know about it and was like wtf at first. "Damn, we're on an old MySQL version!" Then I saw that it was the next version after 5.7.
Programming homework and newbie help thread Quote
07-30-2018 , 05:56 PM
Completely randomly, I just realized I need to solve more or less this exact problem for MySQL, without window functions, for a task I just started on. Except I want the last row that matches some set of conditions. :P
Programming homework and newbie help thread Quote
07-30-2018 , 07:29 PM
Quote:
Originally Posted by RustyBrooks
Yeah I did not really want to get into things like partition by, so I punted and assumed we had them enumerated.
Yeah, I intentionally ignored that part of the problem because 1) I didn’t want to over complicate the question, and 2) I already solved it for my case.
Programming homework and newbie help thread Quote
07-31-2018 , 02:57 PM
What version of SQL you using?
Programming homework and newbie help thread Quote
07-31-2018 , 04:26 PM
I assume there are some decent SQL tutorials online somewhere, any recommendations? Mostly at the moment I do all the query stuff I need to do for our Power BI reports in M, but when we step into the 20th century and stop running a bunch of reports from our case management system to CSV and hook it up to SQL I can do stuff directly on the database instead
Programming homework and newbie help thread Quote
08-01-2018 , 04:01 PM
I feel like this should exist, but I don't actually know of any that I really like. I learned the basics a long time ago, and since then I usually just cobble together whatever refresher I need from random StackOverflow questions and db engine documentation.
Programming homework and newbie help thread Quote

      
m