Semi-Newbie needs a little help

N

Nile

I am trying to write a simple little program to do some elementary
stock market analysis. I read lines, send each line to a function and
then the function returns a date which serves as a key to a
dictionary. Each time a date is returned I want to increment the value
associated with that date. The function seems to be working properly.
By means of a print statement I have inserted just before the return
value I can see there are three dates that are returned which is
correct. The dictionary only seems to capture the last date. My test
data consists of five stocks, each stock with five days. The correct
answer would be a count of 5 for the second day, the third day, and
the last day -- 11/14/2008.

Here is the a code, followed by a portion of the output. I know
enough to write simple little programs like this with no problems up
until now but I don't know enough to figure out what I am doing
wrong.

Code

for x in range(len(file_list)):
d = open(file_list[x] , "r")
data = d.readlines()
k = above_or_below(data) # This
function seems to work correctly
print "here is the value that was returned " , k
dict[k] = dict.get(k,0) + 1

dict_list = dict.values()
print "here is a list of the dictionary values ", dict_list
print "the length of the dictionary is ", len(dict)

And here is some output
Function will return k which = 11/11/2008 # These 3 lines are
printed from the function just before the return
Function will return k which = 11/12/2008 # This sample shows
stocks 4 and 5 but 1,2,3 are the same.
Function will return k which = 11/14/2008
here is the value that was returned 11/14/2008 # printed from
code above - only the last day seems to be
Function will return k which = 11/11/2008 #
recognized.
Function will return k which = 11/12/2008
Function will return k which = 11/14/2008
here is the value that was returned 11/14/2008
here is a list of the dictionary values [5] # dict has
counted only the last day for 5 stocks
the length of the dictionary is 1
 
C

Chris Rebert

I am trying to write a simple little program to do some elementary
stock market analysis.  I read lines, send each line to a function and
then the function returns a date which serves as a key to a
dictionary. Each time a date is returned I want to increment the value
associated with that date. The function seems to be working properly.
By means of a print statement I have inserted just before the return
value I can see there are three dates that are returned which is
correct.  The dictionary only seems to capture the last date. My test
data consists of five stocks, each stock with five days. The correct
answer would be a count of 5 for the second day, the third day, and
the last day -- 11/14/2008.

Here is the a code, followed by a portion of the output.  I know
enough to write simple little programs like this with no problems up
until now but I don't know enough to figure out what I am doing
wrong.
   for x in range(len(file_list)):

for filename in file_list:
#I'm assuming the lack of indentation on the subsequent lines is a
mere transcription error...
   d = open(file_list[x] , "r")

d = open(filename , "r")
   data = d.readlines()
   k = above_or_below(data)                                # This
function seems to work correctly
   print "here is the value that was returned " , k
   dict[k] = dict.get(k,0) + 1

`dict` is the name of a builtin type. Please rename this variable to
avoid shadowing the type.
Also, where is this variable even initialized? It's not in this code
snippet you gave.
Further, I would recommend using a defaultdict
(http://docs.python.org/dev/library/collections.html#collections.defaultdict)
rather than a regular dictionary; this would make the
count-incrementing part nicer.

Taking these changes into account, your code becomes:

from collections import defaultdict

counts = defaultdict(lambda: 0)

for filename in file_list:
d = open(filename , "r")
data = d.readlines()
k = above_or_below(data) # This function seems to work correctly
print "here is the value that was returned " , k
counts[k] += 1

values = counts.values()
print "here is a list of the dictionary values ", values
print "the length of the dictionary is ", len(counts)


I don't immediately see what's causing your problem, but guess that it
might've be related to the initialization of the `dict` variable.

Cheers,
Chris
 
P

Pablo Torres N.

Code

   for x in range(len(file_list)):
   d = open(file_list[x] , "r")
   data = d.readlines()
   k = above_or_below(data)                                # This
function seems to work correctly
   print "here is the value that was returned " , k
   dict[k] = dict.get(k,0) + 1

   dict_list = dict.values()
   print "here is a list of the dictionary values ", dict_list
   print "the length of the dictionary is ", len(dict)

Correcting your indentation errors and moving your comments above the
line they reference will attract more help from others in this list
;-)

Also, I'd recommend limiting your line length to 80 chars, since lines
are wrapped anyway.
 
M

MRAB

Chris said:
I am trying to write a simple little program to do some elementary
stock market analysis. I read lines, send each line to a function and
then the function returns a date which serves as a key to a
dictionary. Each time a date is returned I want to increment the value
associated with that date. The function seems to be working properly.
By means of a print statement I have inserted just before the return
value I can see there are three dates that are returned which is
correct. The dictionary only seems to capture the last date. My test
data consists of five stocks, each stock with five days. The correct
answer would be a count of 5 for the second day, the third day, and
the last day -- 11/14/2008.

Here is the a code, followed by a portion of the output. I know
enough to write simple little programs like this with no problems up
until now but I don't know enough to figure out what I am doing
wrong.
for x in range(len(file_list)):

for filename in file_list:
#I'm assuming the lack of indentation on the subsequent lines is a
mere transcription error...
d = open(file_list[x] , "r")

d = open(filename , "r")
data = d.readlines()
k = above_or_below(data) # This
function seems to work correctly
print "here is the value that was returned " , k
dict[k] = dict.get(k,0) + 1

`dict` is the name of a builtin type. Please rename this variable to
avoid shadowing the type.
Also, where is this variable even initialized? It's not in this code
snippet you gave.
Further, I would recommend using a defaultdict
(http://docs.python.org/dev/library/collections.html#collections.defaultdict)
rather than a regular dictionary; this would make the
count-incrementing part nicer.

Taking these changes into account, your code becomes:

from collections import defaultdict

counts = defaultdict(lambda: 0)
Better is:

counts = defaultdict(int)
for filename in file_list:
d = open(filename , "r")
data = d.readlines()
k = above_or_below(data) # This function seems to work correctly
print "here is the value that was returned " , k
counts[k] += 1

values = counts.values()
print "here is a list of the dictionary values ", values
print "the length of the dictionary is ", len(counts)


I don't immediately see what's causing your problem, but guess that it
might've be related to the initialization of the `dict` variable.
It might be that the indentation was wrong where the count is
incremented, but I can't tell because none of the lines were shown
indented.
 
N

Nile

   for x in range(len(file_list)):
   d = open(file_list[x] , "r")
   data = d.readlines()
   k = above_or_below(data)                                # This
function seems to work correctly
   print "here is the value that was returned " , k
   dict[k] = dict.get(k,0) + 1
   dict_list = dict.values()
   print "here is a list of the dictionary values ", dict_list
   print "the length of the dictionary is ", len(dict)

Correcting your indentation errors and moving your comments above the
line they reference will attract more help from others in this list
;-)

Also, I'd recommend limiting your line length to 80 chars, since lines
are wrapped anyway.

Yup - Sorry, first post ever - next ones will be better formatted
 
G

Guest

Dear Python gurus,

If I'd like to set dielectric constant for the certain material, is it possible to do such in Python environment? If yes, how to do or what syntax can be used?

Also, I'd like to get a simulation result, like voltage, is it possible to get this value in Python environment?

Please let me know,
nacim
 
R

Rhodri James

Dear Python gurus,

If I'd like to set dielectric constant for the certain material, is it
possible to do such in Python environment? If yes, how to do or what
syntax can be used?

Also, I'd like to get a simulation result, like voltage, is it possible
to get this value in Python environment?

Quite possibly, however you're going to have to give us a *lot* more
information before the answers you get will be worth anything at all.
How is "the certain material" represented? What simulator are you
using? Which Python environment? Which Python version for that matter?

We may appear to be mind-readers, but we usually need a bit more
than this to work on.
 
N

Nile

I am trying to write a simple little program to do some elementary
stock market analysis.  I read lines, send each line to a function and
then the function returns a date which serves as a key to a
dictionary. Each time a date is returned I want to increment the value
associated with that date. The function seems to be working properly.
By means of a print statement I have inserted just before the return
value I can see there are three dates that are returned which is
correct.  The dictionary only seems to capture the last date. My test
data consists of five stocks, each stock with five days. The correct
answer would be a count of 5 for the second day, the third day, and
the last day -- 11/14/2008.
Here is the a code, followed by a portion of the output.  I know
enough to write simple little programs like this with no problems up
until now but I don't know enough to figure out what I am doing
wrong.
   for x in range(len(file_list)):

for filename in file_list:
#I'm assuming the lack of indentation on the subsequent lines is a
mere transcription error...
   d = open(file_list[x] , "r")

    d = open(filename , "r")
   data = d.readlines()
   k = above_or_below(data)                                # This
function seems to work correctly
   print "here is the value that was returned " , k
   dict[k] = dict.get(k,0) + 1

`dict` is the name of a builtin type. Please rename this variable to
avoid shadowing the type.
Also, where is this variable even initialized? It's not in this code
snippet you gave.
Further, I would recommend using a defaultdict
(http://docs.python.org/dev/library/collections.html#collections.defau...)
rather than a regular dictionary; this would make the
count-incrementing part nicer.

Taking these changes into account, your code becomes:

from collections import defaultdict

counts = defaultdict(lambda: 0)

for filename in file_list:
    d = open(filename , "r")
    data = d.readlines()
    k = above_or_below(data) # This function seems to work correctly
    print "here is the value that was returned " , k
    counts[k] += 1

    values = counts.values()
    print "here is a list of the dictionary values ", values
    print "the length of the dictionary is ", len(counts)

I don't immediately see what's causing your problem, but guess that it
might've be related to the initialization of the `dict` variable.

Cheers,
Chris
--http://blog.rebertia.com- Hide quoted text -

- Show quoted text -

I initialized the dictionary earlier in the program like this -

hashtable = {}

I changed the "dict" to hashtable but I still get the same result
I will try to learn about the defaultdict but I'm just trying to keep
it as simple as I can for now

Revised code

for x in range(len(file_list)):
d = open(file_list[x] , "r")
data = d.readlines()
k = 0
k = above_or_below(data)
print "here is the value that was returned ",k
hashtable[k] = hashtable.get(k,0) + 1


hashtable_list = hashtable.values()
print "here is a list of the dictionary values ", hashtable_list
print "the length of the dictionary is ", len(hashtable)

Output
# The first 3 lines are printed from the function
# right before the return statement. This output
# snippet shows the last two stocks. The function
# SAYS it is returning the correct value but only
# the last date seems to make it to the hashtable
Function will return k which = 11/11/2008
Function will return k which = 11/12/2008
Function will return k which = 11/14/2008

# this line is printed from the code above
# I don't understand why all three dates don't
# seem to make it to the main program. Only
# the last date seems to be recognized
here is the value that was returned 11/14/2008

Function will return k which = 11/11/2008
Function will return k which = 11/12/2008
Function will return k which = 11/14/2008
here is the value that was returned 11/14/2008
here is a list of the dictionary values [5]
the length of the dictionary is 1
 
M

MRAB

Nile wrote:
[snip]
I initialized the dictionary earlier in the program like this -

hashtable = {}

I changed the "dict" to hashtable but I still get the same result
I will try to learn about the defaultdict but I'm just trying to keep
it as simple as I can for now

Revised code

for x in range(len(file_list)):
d = open(file_list[x] , "r")
data = d.readlines()

What's the point of the following line?
k = 0
k = above_or_below(data)
print "here is the value that was returned ",k
hashtable[k] = hashtable.get(k,0) + 1


hashtable_list = hashtable.values()
print "here is a list of the dictionary values ", hashtable_list
print "the length of the dictionary is ", len(hashtable)

Output
# The first 3 lines are printed from the function
# right before the return statement. This output
# snippet shows the last two stocks. The function
# SAYS it is returning the correct value but only
# the last date seems to make it to the hashtable
Function will return k which = 11/11/2008
Function will return k which = 11/12/2008
Function will return k which = 11/14/2008

# this line is printed from the code above
# I don't understand why all three dates don't
# seem to make it to the main program. Only
# the last date seems to be recognized
here is the value that was returned 11/14/2008

Function will return k which = 11/11/2008
Function will return k which = 11/12/2008
Function will return k which = 11/14/2008
here is the value that was returned 11/14/2008
here is a list of the dictionary values [5]
the length of the dictionary is 1
Exit code: 0

I think there's a bug in 'above_or_below' which you haven't noticed.
 
R

Rhodri James

Revised code

for x in range(len(file_list)):
d = open(file_list[x] , "r")
data = d.readlines()
k = 0
k = above_or_below(data)
print "here is the value that was returned ",k
hashtable[k] = hashtable.get(k,0) + 1


hashtable_list = hashtable.values()
print "here is a list of the dictionary values ", hashtable_list
print "the length of the dictionary is ", len(hashtable)

Output
# The first 3 lines are printed from the function
# right before the return statement. This output
# snippet shows the last two stocks. The function
# SAYS it is returning the correct value but only
# the last date seems to make it to the hashtable
Function will return k which = 11/11/2008
Function will return k which = 11/12/2008
Function will return k which = 11/14/2008

Have you checked the indentation of the print statement
that produces this? Is it perhaps inside a loop still?
 
D

Dave Angel

MRAB said:
I initialized the dictionary earlier in the program like this -

hashtable = {}

I changed the "dict" to hashtable but I still get the same result
I will try to learn about the defaultdict but I'm just trying to keep
it as simple as I can for now

Revised code

for x in range(len(file_list)):
d = open(file_list[x] , "r")
data = d.readlines()

What's the point of the following line?
k = 0
k = above_or_below(data)
print "here is the value that was returned ",k
hashtable[k] = hashtable.get(k,0) + 1


hashtable_list = hashtable.values()
print "here is a list of the dictionary values ", hashtable_list
print "the length of the dictionary is ", len(hashtable)

Output
# The first 3 lines are printed from the function
# right before the return statement. This output
# snippet shows the last two stocks. The function
# SAYS it is returning the correct value but only
# the last date seems to make it to the hashtable
Function will return k which = 11/11/2008
Function will return k which = 11/12/2008
Function will return k which = 11/14/2008

# this line is printed from the code above
# I don't understand why all three dates don't
# seem to make it to the main program. Only
# the last date seems to be recognized
here is the value that was returned 11/14/2008

Function will return k which = 11/11/2008
Function will return k which = 11/12/2008
Function will return k which = 11/14/2008
here is the value that was returned 11/14/2008
here is a list of the dictionary values [5]
the length of the dictionary is 1
Exit code: 0

I think there's a bug in 'above_or_below' which you haven't noticed.

</div>
The code supplied doesn't match the output supplied. It'd probably help
if the output was actually pasted from the command window, instead of
retyped with more comments than data. And of course it'd help if you
actually showed us the function above_or_below(), which is probably
where the bug is. If it prints three values, but returns a string, then
why would you be surprised? Maybe you intended it to return a list?

Each time above_or_below() it's called, it prints three lines before
returning, but only returns the final value. How big is file_list? I
suspect it's of length 5.

And the output is shown as repeated twice, but it probably was actually
five sets of data.

You do know you can print hashtable, don't you? You're extracting and
printing the values, but not bothering with the keys.

I suggest you add a print to the entry point of above_or_below(), to
match the one you have for its return. And all of these print lines
should be indented. That might make it easier to interpret the output,
without lots of inserted comments.

DaveA
 
S

Steven D'Aprano

Dear Python gurus,

If I'd like to set dielectric constant for the certain material, is it
possible to do such in Python environment? If yes, how to do or what
syntax can be used?

certain_material.dielectric_constant = 1.234

Also, I'd like to get a simulation result, like voltage, is it possible
to get this value in Python environment?

Yes.
 
G

Gary Herron

Dear Python gurus,

If I'd like to set dielectric constant for the certain material, is it possible to do such in Python environment? If yes, how to do or what syntax can be used?

Also, I'd like to get a simulation result, like voltage, is it possible to get this value in Python environment?

Please let me know,
nacim
This would be a good place for you to start:
http://www.catb.org/~esr/faqs/smart-questions.html
 
P

Piet van Oostrum

Nile said:
N> I initialized the dictionary earlier in the program like this -
N> hashtable = {}
N> I changed the "dict" to hashtable but I still get the same result
N> I will try to learn about the defaultdict but I'm just trying to keep
N> it as simple as I can for now
N> Revised code
N> for x in range(len(file_list)):
N> d = open(file_list[x] , "r")
N> data = d.readlines()
N> k = 0
N> k = above_or_below(data)
N> print "here is the value that was returned ",k
N> hashtable[k] = hashtable.get(k,0) + 1

N> hashtable_list = hashtable.values()
N> print "here is a list of the dictionary values ", hashtable_list
N> print "the length of the dictionary is ", len(hashtable)
N> Output
N> # The first 3 lines are printed from the function
N> # right before the return statement. This output
N> # snippet shows the last two stocks. The function
N> # SAYS it is returning the correct value but only
N> # the last date seems to make it to the hashtable
N> Function will return k which = 11/11/2008
N> Function will return k which = 11/12/2008
N> Function will return k which = 11/14/2008
N> # this line is printed from the code above
N> # I don't understand why all three dates don't
N> # seem to make it to the main program. Only
N> # the last date seems to be recognized
N> here is the value that was returned 11/14/2008
N> Function will return k which = 11/11/2008
N> Function will return k which = 11/12/2008
N> Function will return k which = 11/14/2008
N> here is the value that was returned 11/14/2008
N> here is a list of the dictionary values [5]
N> the length of the dictionary is 1

Now in your code there is a 1-1 relation between printing
"here is the value that was returned" and incrementing the hashtable
entry. In your log there are only 2 prints of "here is the value that
was returned" so how can the count be 5? Are you hiding something from
us?
 
N

Nile

Thanks all for your help. I appreciate it. The problem was in the
function. A simple bug which I should have caught but I had my mental
blinders on and was sure the problem was outside the function. The
answers have given me a lot to learn so thanks for that as well.
 
S

Simon Forman

Dear Python gurus,

If I'd like to set dielectric constant for the certain material, is it possible to do such in Python environment? If yes, how to do or what syntax can be used?

Also, I'd like to get a simulation result, like voltage, is it possible to get this value in Python environment?

Please let me know,
nacim

The answers to your first and third questions are, "yes" and "yes". :]
(Generally speaking if something can be done by a computer it can be
done with python.)

As for your second question check out the "magnitude" package:

http://pypi.python.org/pypi/magnitude/ and
http://juanreyero.com/magnitude/

(That second link also has links to three other packages that deal
with units of measurement.)

Ii has units for the SI measurements, including volts and coulombs, so
you should be able to accomplish your goals with it.

The tricky thing is, as far as I can tell from the wikipedia entry
(http://en.wikipedia.org/wiki/Relative_static_permittivity),
"dielectric constant" seems to be a dimensionless number, i.e. C/C...
I could be totally daft though.

HTH,
~Simon
 
G

Guest

Hello Gurus,

Thank you for trying to help to my initial and not well written questions. I will compile more detailed information and ask again. Btw, I am giving a glimpse to: "How To Ask Questions The Smart Way".

nacim


-----Original Message-----
From: Simon Forman [mailto:[email protected]]
Sent: Tuesday, July 07, 2009 7:19 AM
To: BRAVO,NACIM (A-Sonoma,ex1)
Cc: (e-mail address removed)
Subject: Re: Newbie needs help

Dear Python gurus,

If I'd like to set dielectric constant for the certain material, is it possible to do such in Python environment? If yes, how to do or what syntax can be used?

Also, I'd like to get a simulation result, like voltage, is it possible to get this value in Python environment?

Please let me know,
nacim

The answers to your first and third questions are, "yes" and "yes". :]
(Generally speaking if something can be done by a computer it can be
done with python.)

As for your second question check out the "magnitude" package:

http://pypi.python.org/pypi/magnitude/ and
http://juanreyero.com/magnitude/

(That second link also has links to three other packages that deal
with units of measurement.)

Ii has units for the SI measurements, including volts and coulombs, so
you should be able to accomplish your goals with it.

The tricky thing is, as far as I can tell from the wikipedia entry
(http://en.wikipedia.org/wiki/Relative_static_permittivity),
"dielectric constant" seems to be a dimensionless number, i.e. C/C...
I could be totally daft though.

HTH,
~Simon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top