[program-l] Some thoughts on objects was ...Re: Re: Python, best way to return a bunch of data from a function?

  • From: "R Dinger" <rrdinger@xxxxxxxxxx>
  • To: "programming" <program-l@xxxxxxxxxxxxx>
  • Date: Tue, 2 Oct 2012 13:11:58 -0700

I think the best way to organize a system is through objects.  You may not
be familiar with object oriented programming, though.  I have a few thoughts 
below that explain how I approach such a problem, but these are just my 
quick thoughts.

Think of having a 'stock' class of objects.  The class is a template
for making stocks and has various attributes like a ticker symbol, shares 
owned, some historical data and whatever
else you want to include in a stock.  Note this can grow and evolve over 
time it does not need to be complete in order to start.

Then an instance of a stock object is created for a specific company, say BA
(Boeing) and the historical data is loaded from yahoo or wherever.  Try 
thinking of a class as a data type and an object as a variable of that type. 
You can then save that specific object in a file, database or something 
else.  The class
should also include functions to update the historical data, compute the
average volume and so forth.  The functions of an object are sometimes
called methods.

OO is really just an organizing scheme for the code there is really nothing
magic about it.  Your code is organized around data abstractions rather
than procedural details.  One advantage is that problem of computing the
average volume is done by simply asking the stock to compute it (all the 
data is right there).  The
intermediate data is not passed all over the place exposing it to
inadvertent corruption.  Most of the stock data is encapsulated in the stock
objects where it is handy to access.

Next imagine a dialog box for the stock class of objects.  The various 
functions of the class are invoked when the various buttons or other 
controls are pushed on the dialog.  GUI interfaces go hand and hand with OO 
system design and that notion of encapsulation.

The individual stocks in your system could be organized in a container 
object of some sort with general functions like updating the historical 
data, saving changes and showing current gain or loss.  To update the 
container object would loop through the stocks and tell each one to update 
its internal historical data.  This could be done automatically at startup 
or via a menu item or whatever.

HTH

Richard

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


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

** 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: