From: Chris Rebert on
On Tue, Mar 23, 2010 at 5:05 PM, Jimbo <nilly16(a)yahoo.com> wrote:
> I have made a Python App(really script) that will check a stocks
> current values from a website & save that data to a SQLite 3 database.
>
> I am looking for any suggestions & criticisms on what I should do
> better or anything at all but mainly in these areas:
<snip>
> Any advice criticism would be really helpful.

Complying with Python naming conventions would be one place to start:
* Class names should be in StudlyCaps (so class "Stock", not class "stock").
* Constants should be in UPPERCASE_WITH_UNDERSCORES.
* Most other names should be words_separated_by_underscores, not
camelCaseLikeThis; FWIW, I'm not a fan of this part of the guideline
personally.

Also, conditions don't need parentheses around them. So:
if (decision == 1): #WRONG! harder to read, extra syntactic noise
if decision == 1: #RIGHT

For many more style details, see PEP 8 -- Style Guide for Python Code:
http://www.python.org/dev/peps/pep-0008/

Additionally, the "return 0" in main() serves no purpose; the return
value isn't used as the exit code for the program. You can either
eliminate the line entirely or use plain "return" if you want the
extra clarity.

Cheers,
Chris
--
http://blog.rebertia.com
From: Tim Roberts on
Jimbo <nilly16(a)yahoo.com> wrote:
>
>class stock:
> code = ""
> purchasePrice = 0
> purchaseQuantity = 0
> price = [] # list of recent prices
> recentBid = [] # list of recent bids for stock
> recentOffer = [] # list of recent offers for stock
> stockVol = [] # list of stock quantity available on
>market

Using lists as class variables is a very good way to create very surprising
bugs. Consider the following:

C:\Dev>type x.py

class test:
check = []
def __init__(self):
self.check.append(1)

x = test()
y = test()
z = test()
print x.check
print y.check
print z.check

C:\Dev>x.py
[1, 1, 1]
[1, 1, 1]
[1, 1, 1]

C:\Dev>

> def __init__(self):
> """ Default Constructor """
> self.code = ""
> self.purchasePrice = 0
> self.purchaseQuantity = 0
>
> def constructor(self, stockCode, purPrice, purQuant):
> """ Constructor """
> self.code = stockCode
> self.purchasePrice = purPrice
> self.purchaseQuantity = purQuant

The "constructor" concept here is not very Pythonic. Why not replace those
both with:

def __init__(self, stockCode="", purPrice=0, purQuant=0):
self.code = stockCode
self.purchasePrice = purPrice
self.purchaseQuantity = purQuant
--
Tim Roberts, timr(a)probo.com
Providenza & Boekelheide, Inc.
From: Steven D'Aprano on
On Wed, 24 Mar 2010 21:14:23 -0700, Tim Roberts wrote:

> Jimbo <nilly16(a)yahoo.com> wrote:
>>
>>class stock:
>> code = ""
>> purchasePrice = 0
>> purchaseQuantity = 0
>> price = [] # list of recent prices
>> recentBid = [] # list of recent bids for stock
>> recentOffer = [] # list of recent offers for stock
>> stockVol = [] # list of stock quantity available on market
>
> Using lists as class variables is a very good way to create very
> surprising bugs. Consider the following:
[snip]


Don't you think that the class attributes are *supposed* to be shared by
all instances? In that case the behaviour you show is not a bug at all.


--
Steven
From: Bruno Desthuilliers on
Steven D'Aprano a écrit :
> On Wed, 24 Mar 2010 21:14:23 -0700, Tim Roberts wrote:
>
>> Jimbo <nilly16(a)yahoo.com> wrote:
>>> class stock:
>>> code = ""
>>> purchasePrice = 0
>>> purchaseQuantity = 0
>>> price = [] # list of recent prices
>>> recentBid = [] # list of recent bids for stock
>>> recentOffer = [] # list of recent offers for stock
>>> stockVol = [] # list of stock quantity available on market
>> Using lists as class variables is a very good way to create very
>> surprising bugs. Consider the following:
> [snip]
>
>
> Don't you think that the class attributes are *supposed* to be shared by
> all instances? In that case the behaviour you show is not a bug at all.

Python's class attributes are indeed supposed to be shared - that's even
the whole point of having class attributes.

But this feature has proven to be confusing for newcomers that more
often than not have previous exposure to OOPLs where you do "declare"
your class "schema" at the class level (where Python defines class
attributes).

Now reread the OP's code, and you'll find out he's indeed yet another
victim of this gotcha:

"""
for row in cur.fetchall():
newStock = stock()
newStock.code = row[0]
newStock.purchasePrice = row[1]
newStock.purchaseQuantity = row[2]
cur.execute(stockQuery,[newStock.code])
for rw in cur.fetchall():
newStock.price.append(rw[0])
newStock.recentOffer.append(rw[1])
newStock.recentBid.append(rw[2])
newStock.stockVol.append(rw[3])
stockList.append(newStock)
"""
From: Tim Roberts on
Steven D'Aprano <steven(a)REMOVE.THIS.cybersource.com.au> wrote:

>On Wed, 24 Mar 2010 21:14:23 -0700, Tim Roberts wrote:
>
>> Jimbo <nilly16(a)yahoo.com> wrote:
>>>
>>>class stock:
>>> code = ""
>>> purchasePrice = 0
>>> purchaseQuantity = 0
>>> price = [] # list of recent prices
>>> recentBid = [] # list of recent bids for stock
>>> recentOffer = [] # list of recent offers for stock
>>> stockVol = [] # list of stock quantity available on market
>>
>> Using lists as class variables is a very good way to create very
>> surprising bugs. Consider the following:
>
>Don't you think that the class attributes are *supposed* to be shared by
>all instances? In that case the behaviour you show is not a bug at all.

Yes, it occurred to me just after I sent that pot that I had answered a
different question from the one he asked. I SUSPECT that he is using class
variables as a way to initialize members, not as true class variables.

Thanks for clarifying this.
--
Tim Roberts, timr(a)probo.com
Providenza & Boekelheide, Inc.