Open Side Menu Go to the Top

03-30-2016 , 11:09 AM
Quote:
Originally Posted by Mr.mmmKay
In that case it would be interesting if timeit also returned the standard deviation so you could get an idea if the difference in time between two functions is significant
You can do it yourself using the "repeat" method of timeit. However, they have this note:

Quote:
Note It’s tempting to calculate mean and standard deviation from the result vector and report these. However, this is not very useful. In a typical case, the lowest value gives a lower bound for how fast your machine can run the given code snippet; higher values in the result vector are typically not caused by variability in Python’s speed, but by other processes interfering with your timing accuracy. So the min() of the result is probably the only number you should be interested in. After that, you should look at the entire vector and apply common sense rather than statistics.
Programming homework and newbie help thread Quote
Programming homework and newbie help thread
$25m Guaranteed WPM on CoinPoker
Join the action now
Daily Rewards • Splash Pots • CoinRaces
Programming homework and newbie help thread
03-30-2016 , 12:14 PM
Quote:
Originally Posted by mackeleven
Hi guys. basic SQL question

Look a the same id with different colors. This is a composite key correct? It's ok to repeat the id if the corresponding colors are different?

I don't like this design.

I'd prefer to see an entry for each color item in the stock table. If you want to show that each shirt, pants, etc, are the same collection, then create a separate look up table and a view that adds up the total stock by item type.
Programming homework and newbie help thread Quote
03-30-2016 , 01:37 PM
if you consider:

shirts come in a selection of black, white, yellow, red
and there are 50 of them in stock

10 black, 20 red etc

you mean like this? i don't understand what you mean by same collection,

item color qty price
shirt black 10 12.50
shirt red 20 12.50

and sum qty where item = shirt etc?

Last edited by mackeleven; 03-30-2016 at 01:44 PM.
Programming homework and newbie help thread Quote
03-30-2016 , 01:55 PM
yeah, that's what I mean. I also tend to thing of the world in skus and upcs. If you registered all of this with GS1, you'd end up having to create a new sku / upc for each product. So, more specifically, something like this:

Code:
create table products (
upc bigint primary key,
sku varchar unique,
description varchar
);

create table valid_product_type (
product_type varchar primary key  --- shirt, pants, etc
);

create table master_products (
master_id [could be serial, varchar] primary key,
master_name varchar unique,
product_type varchar references valid_product_type (product_type),
);

create table product_master_lookup (
master_id references master_products (master_id)
upc bigint references products (upc)
primary_key (master_id, upc)
);
The reason is that the master product isn't a real product. You can't, for example, sell a generic shirt, so it doesn't make sense to put it on sale. The above schema will give you the flexibility to represent each individual item and allow you to add up the inventory of the the main "type." Also, if you end up with two collections of shirts, you have no issues.
Programming homework and newbie help thread Quote
03-30-2016 , 02:01 PM
Funny that's the way I was doing it. The spreadsheet there was my instructor's solution. He's throwing me off lol.
Programming homework and newbie help thread Quote
03-30-2016 , 02:25 PM
Quote:
Originally Posted by JSLigon
There might be a bigger issue with h(), though (which I was not previously aware of):

Code:
>>> 5%5 == 0
True
>>> 5%5 == 0 is True
False
>>> (5%5 == 0) is True
True
Edit: And indeed at the end of the loop in h(), n remains empty.
It looks like the 'is' operator takes priority over the '==' operator.
Programming homework and newbie help thread Quote
03-30-2016 , 03:29 PM
Quote:
Originally Posted by mackeleven
Funny that's the way I was doing it. The spreadsheet there was my instructor's solution. He's throwing me off lol.
For purely instructional purposes, I don't mind his / her idea, but I don't think it is well-executed. The main issue is that there is a total stock and then an item stock column. This is going to cause massive headaches, regardless if you are using the processing language or not.

To answer the other question you had, a composite key means a primary, foreign, or unique key over two or more columns.
Programming homework and newbie help thread Quote
03-30-2016 , 04:56 PM
Quote:
Originally Posted by econophile
It looks like the 'is' operator takes priority over the '==' operator.
Code:
>>> 5%5 == 0 is True
False
>>> 5%5 == (0 is True)
True
Same issue without the modulus:
Code:
>>> 0 == 0 is True
False
>>> (0 == 0) is True
True
>>> 0 == (0 is True)
True
More fun:
Code:
>>> False is False
True
>>> False is False is False
True
>>> (False is False) is False
False
>>> False is (False is False)
False
What I think is happening: "is" tests if the two operands point to the same object, not just if they evaluate to the same thing. Putting an expression in parentheses forces the expression to be evaluated, somehow? So with the expression "5%5 == 0 is True", the left side of the "is" which is "5%5 == 0" as an unevaluated expression (like a tree with == at the root, 5%5 left branch, 0 right branch - or however Python represents expressions that haven't been evaluated yet) is not the same object as "True", even though it will evaluate to True.

Last edited by JSLigon; 03-30-2016 at 05:05 PM. Reason: added example
Programming homework and newbie help thread Quote
03-30-2016 , 05:14 PM
Some instructive results

Code:
>>> 0 == False
True
>>> 0 is False
False
Programming homework and newbie help thread Quote
03-30-2016 , 05:15 PM
Quote:
Originally Posted by econophile
Some instructive results

Code:
>>> 0 == False
True
>>> 0 is False
False
"is" is completely literal. Which becomes required when implicit conversion happens, such as with == etc.
Programming homework and newbie help thread Quote
03-30-2016 , 05:18 PM
I think I have a plausible explanation for all of the above, except:
Code:
>>> False is False is False
True
No idea what's happening there.
Programming homework and newbie help thread Quote
03-30-2016 , 05:24 PM
Yeah that's weird. Also

Code:
>>> None is None is None
True
Programming homework and newbie help thread Quote
03-30-2016 , 05:31 PM
Thanks, that explains it, along with similar behavior with triple == that I plan to never use in my code:

Code:
>>> False == False == False
True
>>> (False == False) == False
False
>>> False == (False == False)
False
Actually I take back the part about never using multiple == like that. Could be useful as long as I'm clear on the semantics, less to type when testing whether several expressions are all equal.

Last edited by JSLigon; 03-30-2016 at 05:41 PM.
Programming homework and newbie help thread Quote
03-30-2016 , 07:31 PM
Quote:
Originally Posted by daveT
For purely instructional purposes, I don't mind his / her idea, but I don't think it is well-executed. The main issue is that there is a total stock and then an item stock column. This is going to cause massive headaches, regardless if you are using the processing language or not.

To answer the other question you had, a composite key means a primary, foreign, or unique key over two or more columns.
That confusion is my fault. I believe he greyed out the column named stock in the stock table instead of deleting it. Cheers though.
Programming homework and newbie help thread Quote
03-31-2016 , 07:49 PM
Code:
course_id   | courseName | courseCost | staff_id |
+-----------+------------+------------+----------+
|       401 | Boxing     |         30 |      100 |
|       402 | TKD        |         30 |      101 |
|       403 | BJJ        |         30 |      102 |
|       404 | Wrestling  |         30 |      103 |
+-----------+------------+------------+----------+
Hi. How do I sum two rows of a column. I need to sum the coursecost of boxing
and wrestling.........

Code:
select coursecost from course 
where coursename='boxing' or courseName = 'wrestling';


edit: i can create a new table:

Code:
create table cost as
(
select coursecost from course 
where coursename='boxing' or courseName = 'wrestling'
);
and then sum coursecost from that new table but that's necessary surely...?

Last edited by _dave_; 03-31-2016 at 10:36 PM. Reason: aligned your table :)
Programming homework and newbie help thread Quote
03-31-2016 , 08:50 PM
Code:
select SUM(coursecost) from course 
where coursename='boxing' or courseName = 'wrestling';
.
Programming homework and newbie help thread Quote
03-31-2016 , 09:00 PM
Jesus. I swear I tried that like 3 times and got an error each time.
Thanks
Programming homework and newbie help thread Quote
04-04-2016 , 02:02 AM
JS question - say I have an object that inherits/uses (not sure of my terminology) a given prototype, which has functions, and I stash it in a mongo database. Upon later retrieval, it's just a plain old Object and doesn't have the prototype it did before, which makes me sad.

So, what I want to do is take that object and either give it the former glory it had as an instance of a class (am I allowed to say that in JS?) before being reduced to a pile of data fields. The only thing I could figure out how to do so far is make a new object and copy the data fields from the old object over:

Code:
var newUser = Object.assign(Object.create(user.prototype), userData);
but I'm not sure if that's ideal?
Programming homework and newbie help thread Quote
04-04-2016 , 08:19 AM
Question regarding databases (SQLite) -

I'm doing a school project that requires us to create a program that works with a database. I picked SQLite as the DBMS because it seemed simple to set up and work with. Disclaimer, I don't know a whole lot about databases, I'm familiar with SQL syntax but don't quite understand everything about databases, like what is the difference between say SQLite and MySQL. So bonus points for anyone who can explain that

Anyway, to create and initialize the database (with 1 table) I simply executed the following code in IntelliJ:

Code:
 Connection c = null;
    try {
      Class.forName("org.sqlite.JDBC");
      c = DriverManager.getConnection("jdbc:sqlite:MyDatabase");
    } catch ( Exception e ) {
      System.err.println( e.getClass().getName() + ": " + e.getMessage() );
      System.exit(0);
    }
This tries to connect with a database called MyDatabase, and if it doesn't exits then it "creates" a new database and connects with it.

Then I executed SQL statements on that database connection, and was able to create tables, insert into tables, retrieve from tables etc etc. Everything you could do with SQL.
And the database got stored in a file called MyDatabase, and had the file extension of .file (??)

But when I shipped the project out to another member of my team (it's a group project for school), he got an error which was something like "SQL_ERROR: No such table Trips exists". It was like the database had been corrupted/wiped out upon sending it over to him. I just .zipped the whole folder containing all the source code and the database. And he extracted it and ran it. So I'm wondering what might be the cause of this? Like, is there something fundamental thing about databases that I'm missing that could explain why you can't send a database to someone else like this? Also, is it expected that a database is a file with .file extension?
Perhaps this was just a problem with his computer/compiler/whatever, and we should try it on another computer?
Programming homework and newbie help thread Quote
04-04-2016 , 09:56 AM
Quote:
Originally Posted by goofyballer
JS question - say I have an object that inherits/uses (not sure of my terminology) a given prototype, which has functions, and I stash it in a mongo database. Upon later retrieval, it's just a plain old Object and doesn't have the prototype it did before, which makes me sad.

So, what I want to do is take that object and either give it the former glory it had as an instance of a class (am I allowed to say that in JS?) before being reduced to a pile of data fields. The only thing I could figure out how to do so far is make a new object and copy the data fields from the old object over:

Code:
var newUser = Object.assign(Object.create(user.prototype), userData);
There's this too but I wouldn't make an argument either way as to which is better:

https://developer.mozilla.org/en-US/...setPrototypeOf
Programming homework and newbie help thread Quote
04-05-2016 , 10:27 AM
Quote:
Originally Posted by goofyballer
JS question - say I have an object that inherits/uses (not sure of my terminology) a given prototype, which has functions, and I stash it in a mongo database. Upon later retrieval, it's just a plain old Object and doesn't have the prototype it did before, which makes me sad.

So, what I want to do is take that object and either give it the former glory it had as an instance of a class (am I allowed to say that in JS?) before being reduced to a pile of data fields. The only thing I could figure out how to do so far is make a new object and copy the data fields from the old object over:

Code:
var newUser = Object.assign(Object.create(user.prototype), userData);
but I'm not sure if that's ideal?
That's a perfectly good solution, and in fact is philosophically in line with Crockford's latest object creation recommendations:

http://stackoverflow.com/questions/2...glas-crockford

I think calling it an instance of a class... isn't necessarily wrong per se, but there a better perspectives for thinking about JS. You really don't want to bring in Java/C++ baggage. Better to think of there being nothing but objects -- no classes -- but with the ability to compose and create new objects from the old ones. Crockford has said, and I agree, that the "class" keyword was ES6's biggest mistake.

Directly relevant to your question: http://raganwald.com/2015/05/11/javascript-classes.html
Programming homework and newbie help thread Quote
04-05-2016 , 03:04 PM
Thanks, I'll look those over. re: terminology, I'm just not sure what the proper way to describe these things in JS parlance is - is there a term for an object with its own prototype methods, as opposed to an object that uses Object's prototype?

I've found the discussion of constructors somewhat confusing from the things I've read online so far, mostly because
- constructor functions look like regular functions in every way
- but magic things happen when they're called with 'new'!

Is there any meaningful difference between these two pieces of code?

Code:
var asdf = new constructorFunc();

var asdf = (function (obj) { constructorFunc.call(obj); return obj; })({});
Programming homework and newbie help thread Quote
04-05-2016 , 03:44 PM
Quote:
Originally Posted by goofyballer
Is there any meaningful difference between these two pieces of code?

Code:
var asdf = new constructorFunc();

var asdf = (function (obj) { constructorFunc.call(obj); return obj; })({});
Two differences as far as I can tell are that they don't have the same prototype; and if there's a return statement in the constructor, the first example will return that instead (only if it's an object though I think) whereas in the second example, it will still return {}.
Programming homework and newbie help thread Quote
04-05-2016 , 09:00 PM
Quote:
Originally Posted by goofyballer
Thanks, I'll look those over. re: terminology, I'm just not sure what the proper way to describe these things in JS parlance is - is there a term for an object with its own prototype methods, as opposed to an object that uses Object's prototype?

I've found the discussion of constructors somewhat confusing from the things I've read online so far, mostly because
- constructor functions look like regular functions in every way
- but magic things happen when they're called with 'new'!

Is there any meaningful difference between these two pieces of code?

Code:
var asdf = new constructorFunc();

var asdf = (function (obj) { constructorFunc.call(obj); return obj; })({});
I don't think there's any special parlance. If you create an object with a "Dog" constructor, you'd just say it's an object of type Dog.

candy answered your other question. As far as actually writing code, you're best off not using "new" altogether. And definitely don't create a hierarchy of prototype chains.
Programming homework and newbie help thread Quote
Programming homework and newbie help thread
$25m Guaranteed WPM on CoinPoker
Join the action now
Daily Rewards • Splash Pots • CoinRaces
Programming homework and newbie help thread

      
m