[NEWB] Dictionary instantiation?

2007-12-07 Thread Matt_D
Hello there, this is my first post to the list. Only been working with
Python for a few days. Basically a complete newbie to programming.

I'm working with csv module as an exercise to parse out a spreadsheet
I use for work.(I am an editor for a military journalism unit) Not
trying to do anything useful, just trying to manipulate the data.
Anyway, here's the code I've got so far:

import csv
import string
import os

#Open the appropriate .csv file
csv_file = csv.reader(open("D:\\Python25\\BNSR.csv"))

#Create blank dictionary to hold {[author]:[no. of stories]} data
story_per_author = {}

def author_to_dict(): #Function to add each author to the dictionary
once to get initial entry for that author
for row in csv_file:
author_count = row[-1]
story_per_author[author_count] = 1

#Fetch author names
def rem_blank_authors(): #Function to remove entries with '' in the
AUTHOR field of the .csv
csv_list = list(csv_file) #Convert the open file to list format
for e-z mode editing
for row in csv_list:
author_name = row[-1]
if author_name == '': #Find entries where no author is listed
csv_list.remove(row) #Remove those entries from the list

def assign_author_to_title(): #Assign an author to every title
author_of_title = {}
for row in csv_file:
title = row[3]
author = row[-1]
author_of_title[title] = author


assign_author_to_title()
print author_of_title

--

Ok, the last two lines are kind of my "test the last function" test.
Now when I run these two lines I get the error:

Traceback (most recent call last):
  File "D:\Python25\Lib\SITE-P~1\PYTHON~1\pywin\framework
\scriptutils.py", line 310, in RunScript
exec codeObject in __main__.__dict__
  File "D:\Python25\csv_read.py", line 33, in 
print author_of_title
NameError: name 'author_of_title' is not defined

I am guessing that the author_of_title dict does not exist outside of
the function in which it is created? The concept of instantiation is
sort of foreign to me so I'm having some trouble predicting when it
happens.

If I call the assign_author_to_title function later, am I going to be
able to work with the author_of_title dictionary? Or is it best if I
create author_of_title outside of my function definitions?

Clearly I'm just stepping through my thought process right now,
creating functions as I see a need for them. I'm sure the code is
sloppy and terrible but please be gentle!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dictionary instantiation?

2007-12-07 Thread Matt_D
On Dec 7, 12:33 pm, Matt_D <[EMAIL PROTECTED]> wrote:
> On Dec 7, 12:27 pm, Matt_D <[EMAIL PROTECTED]> wrote:
>
>
>
> > On Dec 7, 11:42 am, Virgil Dupras <[EMAIL PROTECTED]>
> > wrote:
>
> > > On Dec 7, 9:05 am, Matt_D <[EMAIL PROTECTED]> wrote:
>
> > > > Hello there, this is my first post to the list. Only been working with
> > > > Python for a few days. Basically a complete newbie to programming.
>
> > > > I'm working with csv module as an exercise to parse out a spreadsheet
> > > > I use for work.(I am an editor for a military journalism unit) Not
> > > > trying to do anything useful, just trying to manipulate the data.
> > > > Anyway, here's the code I've got so far:
>
> > > > import csv
> > > > import string
> > > > import os
>
> > > > #Open the appropriate .csv file
> > > > csv_file = csv.reader(open("D:\\Python25\\BNSR.csv"))
>
> > > > #Create blank dictionary to hold {[author]:[no. of stories]} data
> > > > story_per_author = {}
>
> > > > def author_to_dict(): #Function to add each author to the dictionary
> > > > once to get initial entry for that author
> > > > for row in csv_file:
> > > > author_count = row[-1]
> > > > story_per_author[author_count] = 1
>
> > > > #Fetch author names
> > > > def rem_blank_authors(): #Function to remove entries with '' in the
> > > > AUTHOR field of the .csv
> > > > csv_list = list(csv_file) #Convert the open file to list format
> > > > for e-z mode editing
> > > > for row in csv_list:
> > > > author_name = row[-1]
> > > > if author_name == '': #Find entries where no author is listed
> > > > csv_list.remove(row) #Remove those entries from the list
>
> > > > def assign_author_to_title(): #Assign an author to every title
> > > > author_of_title = {}
> > > > for row in csv_file:
> > > > title = row[3]
> > > > author = row[-1]
> > > > author_of_title[title] = author
>
> > > > assign_author_to_title()
> > > > print author_of_title
>
> > > > --
>
> > > > Ok, the last two lines are kind of my "test the last function" test.
> > > > Now when I run these two lines I get the error:
>
> > > > Traceback (most recent call last):
> > > >   File "D:\Python25\Lib\SITE-P~1\PYTHON~1\pywin\framework
> > > > \scriptutils.py", line 310, in RunScript
> > > > exec codeObject in __main__.__dict__
> > > >   File "D:\Python25\csv_read.py", line 33, in 
> > > > print author_of_title
> > > > NameError: name 'author_of_title' is not defined
>
> > > > I am guessing that the author_of_title dict does not exist outside of
> > > > the function in which it is created? The concept of instantiation is
> > > > sort of foreign to me so I'm having some trouble predicting when it
> > > > happens.
>
> > > > If I call the assign_author_to_title function later, am I going to be
> > > > able to work with the author_of_title dictionary? Or is it best if I
> > > > create author_of_title outside of my function definitions?
>
> > > > Clearly I'm just stepping through my thought process right now,
> > > > creating functions as I see a need for them. I'm sure the code is
> > > > sloppy and terrible but please be gentle!
>
> > > As you said, author_of_title doesn't exist outside of
> > > assign_author_to_title() because it has been instantiated in the
> > > function, and thus belong to the local scope. You could instantiate
> > > your dictionary outside of the function, but the nicest way to handle
> > > this would be to add a line "return author_of_title" at the end of
> > > assign_author_to_title() and have "print assign_author_to_title()"
> > > instead of the 2 last lines.
>
> > Another newb question, same project:
>
> > #Fetch author names
> > def rem_blank_authors(): #Function to remove entries with '' in the
> > AUTHOR field of the .csv
> > csv_list = list(csv_file) #Convert the open file to list format
> > for e-z mode editing
> > for row in csv_list:
> > author_name = row[-1]
> >  

Re: Dictionary instantiation?

2007-12-07 Thread Matt_D
On Dec 7, 11:42 am, Virgil Dupras <[EMAIL PROTECTED]>
wrote:
> On Dec 7, 9:05 am, Matt_D <[EMAIL PROTECTED]> wrote:
>
>
>
> > Hello there, this is my first post to the list. Only been working with
> > Python for a few days. Basically a complete newbie to programming.
>
> > I'm working with csv module as an exercise to parse out a spreadsheet
> > I use for work.(I am an editor for a military journalism unit) Not
> > trying to do anything useful, just trying to manipulate the data.
> > Anyway, here's the code I've got so far:
>
> > import csv
> > import string
> > import os
>
> > #Open the appropriate .csv file
> > csv_file = csv.reader(open("D:\\Python25\\BNSR.csv"))
>
> > #Create blank dictionary to hold {[author]:[no. of stories]} data
> > story_per_author = {}
>
> > def author_to_dict(): #Function to add each author to the dictionary
> > once to get initial entry for that author
> > for row in csv_file:
> > author_count = row[-1]
> > story_per_author[author_count] = 1
>
> > #Fetch author names
> > def rem_blank_authors(): #Function to remove entries with '' in the
> > AUTHOR field of the .csv
> > csv_list = list(csv_file) #Convert the open file to list format
> > for e-z mode editing
> > for row in csv_list:
> > author_name = row[-1]
> > if author_name == '': #Find entries where no author is listed
> > csv_list.remove(row) #Remove those entries from the list
>
> > def assign_author_to_title(): #Assign an author to every title
> > author_of_title = {}
> > for row in csv_file:
> > title = row[3]
> > author = row[-1]
> > author_of_title[title] = author
>
> > assign_author_to_title()
> > print author_of_title
>
> > --
>
> > Ok, the last two lines are kind of my "test the last function" test.
> > Now when I run these two lines I get the error:
>
> > Traceback (most recent call last):
> >   File "D:\Python25\Lib\SITE-P~1\PYTHON~1\pywin\framework
> > \scriptutils.py", line 310, in RunScript
> > exec codeObject in __main__.__dict__
> >   File "D:\Python25\csv_read.py", line 33, in 
> > print author_of_title
> > NameError: name 'author_of_title' is not defined
>
> > I am guessing that the author_of_title dict does not exist outside of
> > the function in which it is created? The concept of instantiation is
> > sort of foreign to me so I'm having some trouble predicting when it
> > happens.
>
> > If I call the assign_author_to_title function later, am I going to be
> > able to work with the author_of_title dictionary? Or is it best if I
> > create author_of_title outside of my function definitions?
>
> > Clearly I'm just stepping through my thought process right now,
> > creating functions as I see a need for them. I'm sure the code is
> > sloppy and terrible but please be gentle!
>
> As you said, author_of_title doesn't exist outside of
> assign_author_to_title() because it has been instantiated in the
> function, and thus belong to the local scope. You could instantiate
> your dictionary outside of the function, but the nicest way to handle
> this would be to add a line "return author_of_title" at the end of
> assign_author_to_title() and have "print assign_author_to_title()"
> instead of the 2 last lines.

Another newb question, same project:

#Fetch author names
def rem_blank_authors(): #Function to remove entries with '' in the
AUTHOR field of the .csv
csv_list = list(csv_file) #Convert the open file to list format
for e-z mode editing
for row in csv_list:
author_name = row[-1]
if author_name == '': #Find entries where no author is listed
csv_list.remove(row) #Remove those entries from the list
return csv_list

def author_to_dict(): #Function to add each author to the dictionary
once to get initial entry for that author
#rem_blank_authors() #Call this function to remove blank author
fields before building the main dictionary
for row in csv_file:
author_count = row[-1]
if author_count in story_per_author:
story_per_author[author_count] += 1
else:
story_per_author[author_count] = 1
return story_per_author

def assign_author_to_title(): #Assign an author to every title
author_of_title = {}
for row in csv_file:
title = row[3]
author = row[-1]
author_of_title[title] = author

author_to_dict()
print story_per_author

--

The solu

Re: Dictionary instantiation?

2007-12-07 Thread Matt_D
On Dec 7, 12:27 pm, Matt_D <[EMAIL PROTECTED]> wrote:
> On Dec 7, 11:42 am, Virgil Dupras <[EMAIL PROTECTED]>
> wrote:
>
>
>
> > On Dec 7, 9:05 am, Matt_D <[EMAIL PROTECTED]> wrote:
>
> > > Hello there, this is my first post to the list. Only been working with
> > > Python for a few days. Basically a complete newbie to programming.
>
> > > I'm working with csv module as an exercise to parse out a spreadsheet
> > > I use for work.(I am an editor for a military journalism unit) Not
> > > trying to do anything useful, just trying to manipulate the data.
> > > Anyway, here's the code I've got so far:
>
> > > import csv
> > > import string
> > > import os
>
> > > #Open the appropriate .csv file
> > > csv_file = csv.reader(open("D:\\Python25\\BNSR.csv"))
>
> > > #Create blank dictionary to hold {[author]:[no. of stories]} data
> > > story_per_author = {}
>
> > > def author_to_dict(): #Function to add each author to the dictionary
> > > once to get initial entry for that author
> > > for row in csv_file:
> > > author_count = row[-1]
> > > story_per_author[author_count] = 1
>
> > > #Fetch author names
> > > def rem_blank_authors(): #Function to remove entries with '' in the
> > > AUTHOR field of the .csv
> > > csv_list = list(csv_file) #Convert the open file to list format
> > > for e-z mode editing
> > > for row in csv_list:
> > > author_name = row[-1]
> > > if author_name == '': #Find entries where no author is listed
> > > csv_list.remove(row) #Remove those entries from the list
>
> > > def assign_author_to_title(): #Assign an author to every title
> > > author_of_title = {}
> > > for row in csv_file:
> > > title = row[3]
> > > author = row[-1]
> > > author_of_title[title] = author
>
> > > assign_author_to_title()
> > > print author_of_title
>
> > > --
>
> > > Ok, the last two lines are kind of my "test the last function" test.
> > > Now when I run these two lines I get the error:
>
> > > Traceback (most recent call last):
> > >   File "D:\Python25\Lib\SITE-P~1\PYTHON~1\pywin\framework
> > > \scriptutils.py", line 310, in RunScript
> > > exec codeObject in __main__.__dict__
> > >   File "D:\Python25\csv_read.py", line 33, in 
> > > print author_of_title
> > > NameError: name 'author_of_title' is not defined
>
> > > I am guessing that the author_of_title dict does not exist outside of
> > > the function in which it is created? The concept of instantiation is
> > > sort of foreign to me so I'm having some trouble predicting when it
> > > happens.
>
> > > If I call the assign_author_to_title function later, am I going to be
> > > able to work with the author_of_title dictionary? Or is it best if I
> > > create author_of_title outside of my function definitions?
>
> > > Clearly I'm just stepping through my thought process right now,
> > > creating functions as I see a need for them. I'm sure the code is
> > > sloppy and terrible but please be gentle!
>
> > As you said, author_of_title doesn't exist outside of
> > assign_author_to_title() because it has been instantiated in the
> > function, and thus belong to the local scope. You could instantiate
> > your dictionary outside of the function, but the nicest way to handle
> > this would be to add a line "return author_of_title" at the end of
> > assign_author_to_title() and have "print assign_author_to_title()"
> > instead of the 2 last lines.
>
> Another newb question, same project:
>
> #Fetch author names
> def rem_blank_authors(): #Function to remove entries with '' in the
> AUTHOR field of the .csv
> csv_list = list(csv_file) #Convert the open file to list format
> for e-z mode editing
> for row in csv_list:
> author_name = row[-1]
> if author_name == '': #Find entries where no author is listed
> csv_list.remove(row) #Remove those entries from the list
> return csv_list
>
> def author_to_dict(): #Function to add each author to the dictionary
> once to get initial entry for that author
> #rem_blank_authors() #Call this function to remove blank author
> fields before building the main dictionary
>   

Importing functions that require parameters

2007-12-10 Thread Matt_D
Good afternoon.

As a self-tutoring project I am writing a one-time-pad encrypt/decrypt
script. I have completed the encryption portion and am working
currently on the decryption algorithm. My goal is to have the encrypt
and decrypt be individual modules vice two parts of the same.

My problem, or perhaps more accurately, question, lies in importing a
function from the otp_encrypt script. Here is the function I am
attempting to call:

def get_key(ptext):
"""Convert one-time-pad to uppercase, and strip spaces. On final
line slice pad to match length of plain text. (OTP will not work if
len(pad) != len(plaintext)"""
ptext = upper_case(ptext)
otp = # key removed just due to sheer length
otp = string.upper(otp)
new = ""
for letter in otp:
if letter in string.uppercase:
new += letter
return new[:len(ptext)]

The parameter of get_key is sys.argv[1]. Now I understand why I'm
getting the errors I'm getting (invalid syntax if I include () or
([parameter], or an IndexError if I don't include those), but my
question is, is it feasible to import a function from a module when
that function requires a parameter from elsewhere in the imported
module? Or is it just better to just import * in all cases?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Importing functions that require parameters

2007-12-10 Thread Matt_D
On Dec 10, 2:46 pm, John Machin <[EMAIL PROTECTED]> wrote:
> "requires a parameter from elsewhere in the imported module" is a
> concept I don't understand.
>
> Here is what I think that you need to do in your main script:
>
> import sys
> import otp_encrypt
> the_key = opt_encrypt.get_key(sys.argv[1])
>
> If that isn't what you want, you'll need to explain the sentence that
> starts "Now I understand", with examples of what you have tried.

When I try:

from otp_encrypt import get_key

I get:

---
IndexErrorTrace

C:\WINDOWS\system32\ in ()
 62 cipher += letter
 63 return cipher
 64
---> 65 print final(sys.argv[1])
 66

IndexError: list index out of range

In [13]: from otp_encrypt import get_key()

I know why I'm getting the error -- I'm importing a function from a
module in iPython with a sys.argv parameter. No big mystery there.

> BTW, how is the uppercase function different from string.upper, and
> why aren't you using string methods e.g. otp = otp.upper()
> ?

To be honest, I think I tried it once, but probably left off the ().
When I got an error I more than likely changed it to string.upper(otp)
and since it worked I didn't worry about it. This is like the second
full script I've actually finished so I'm trying to get all my
functionality in first before I start optimizing the script. While I'm
sure things like this are obvious to you, I've only been coding for a
week so any questions like, "Why did you do x when y is much better?"
can probably be answered with, "Stupid newb."

Thanks again.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Importing functions that require parameters

2007-12-10 Thread Matt_D
On Dec 10, 4:49 pm, Peter Otten <[EMAIL PROTECTED]> wrote:

>
> Peter

Thanks, Peter. You answered my question precisely. I'm successfully
encrypting and decrypting now. Thank you again.

R,

Matt
-- 
http://mail.python.org/mailman/listinfo/python-list