[program-l] Re: Python, best way to return a bunch of data from a function?

  • From: Al Puzzuoli <alpuzz@xxxxxxxxx>
  • To: program-l@xxxxxxxxxxxxx
  • Date: Mon, 01 Oct 2012 23:13:11 -0400

Hi Rich,
I guess at this point, I'm still in the hacking around faze. Learning the language, and adapting my solution to fit the knowledge I've gleaned thus far. So it sounds as if I may not need to take apart my CSV? If I don't take it apart, how would I parse it? For example, I might want to read the volume column, and calculate its average over the past 90 days. I'll definitely begin familiarizing myself with the other storage alternatives you mentioned.
Thanks a lot for the assist,

Al

On 10/1/2012 7:50 PM, R Dinger wrote:
Using global variables is a bad idea.  And answering this question is full
of
pitfalls, but I will try.

I am unclear on why you are taking apart your CSV files in the first place,
but if you really want six lists you can simply return them from your
"reading-function" as a tuple e.g.:
return (list1, list2, ... list6)

You can also convert the CSV tuple to a named tuple so you only need one
list with named attributes like l.high or l.low.  That seems more direct
than having six lists.

Or you can use a dictionary with the date as the key giving you direct
access rather than sequential access.

Or you can put the data into a custom designed object and return it.

Everything in Python is an object.

Or Python comes with the sqlite database and the data could be stored in a
suitable DB table.

Or Python comes with several other data persistence features like shelves
that you could use if you do not like databases.

You may change your mind several times as your system evolves.  Maybe you
should think about how you will be accessing and using this data as that may
lead to how to structure it.  Object oriented systems are more structured
around the data abstractions used than being structured around processing
procedures.

Keep tinkering and you will eventually get where you want to go.

Richard
----- Original Message -----
From: "Al Puzzuoli" <alpuzz@xxxxxxxxx>
To: <program-l@xxxxxxxxxxxxx>
Sent: Monday, October 01, 2012 8:23 AM
Subject: [program-l] Python, best way to return a bunch of data from a
function?


Hi everyone,
For my current project, I'm envisioning having a number of different
data collection functions. Currently, I'm working with Yahoo. I'm using
Urllib to download a CSv file. I then zip the data into appropriate
columns, convert it from tuples to  lists. Next, I pop the header rows
off my lists and convert them to floating point values. Right now, this
code is running as part of my main program, but I ultimately want to
modularize it and later add data collectors for other sources. So in the
case of Yahoo, the above process does the job, but another source might
offer the data in JSON format or whatever. Ultimately, how I parse the
data should be transparent to the program. The important thing is that
the main program gets back 6 lists, date, opening, high, low, closing,
volume. The most obvious way to do this would seem to be to declare
these list as global variables in my data collection functions, but I'm
afraid that if I use global variables, I shall incur the wrath of the
programming GODS and be afflicted forever after by vengeful gremlins
from on high.
So is this instance an acceptable use case for global variables? If not,
what would be the best way to formulate  a function that would do all
the necessary parsing and processing, and then return all the data I
need in a single pass?
Thanks,

Al

** To leave the list, click on the immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=unsubscribe]
** If this link doesn't work then send a message to:
** program-l-request@xxxxxxxxxxxxx
** and in the Subject line type
** unsubscribe
** For other list commands such as vacation mode, click on the
** immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=faq]
** or send a message, to
** program-l-request@xxxxxxxxxxxxx with the Subject:- faq

** To leave the list, click on the immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=unsubscribe]
** If this link doesn't work then send a message to:
** program-l-request@xxxxxxxxxxxxx
** and in the Subject line type
** unsubscribe
** For other list commands such as vacation mode, click on the
** immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=faq]
** or send a message, to
** program-l-request@xxxxxxxxxxxxx with the Subject:- faq

** To leave the list, click on the immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=unsubscribe]
** If this link doesn't work then send a message to:
** program-l-request@xxxxxxxxxxxxx
** and in the Subject line type
** unsubscribe
** For other list commands such as vacation mode, click on the
** immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=faq]
** or send a message, to
** program-l-request@xxxxxxxxxxxxx with the Subject:- faq

Other related posts: