Re: [Tutor] using shelve

2007-06-24 Thread chrispython
 
Sorry it took me so long to get back - from your posts and my experimentation I 
can see that when you access one item in the shelve dictionary, it only gets 
the one item, not all of them. I am going to use shelve, and only refactor or 
change if performance becomes an issue - which I don't see happening for a long 
time.
On Thursday, June 21, 2007, at 10:27PM, John Fouhy [EMAIL PROTECTED] wrote:
On 22/06/07, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote:
 I created a shelf called 'myshelf' and put two objects in it, a string and a 
 list. When I open the shelf I get:

  d=shelve.open('/Users/development/Desktop/myshelf')
  d.keys()
 ['dir1', 'dir2']
  d
 {'dir2': '/Users/development/Desktop/RSSReaderApp/RSS.db', 'dir1': 
 ['.DS_Store', '.localized', 'access the latest version', 'Python Quick 
 Reference.webloc', 'rssdb', 'workspace']}

 It seems that when you use shelve.open(), it actually brings the entire 
 shelf dictionary into memory, accessible through d.

Well ... all that tells me is that when you ask python for a string
representation of a shelf, it reads the entire thing.

 What if you had 100 objects in myshelf, or 1000 or 100,000? Wouldn't it get 
 bogged down?

If you try to print out the whole thing, probably.  Let's try some test:

 import shelve
 d = shelve.open('test.shelf')
 for i in range(1000):
...  d[str(i)] = 'x'*i
...
 d.close()

Morpork:~/tmp repton$ ls -l test.shelf
-rw-r--r--   1 repton  repton   1M Jun 22 14:23 test.shelf

First, we'll measure how long it takes python to open the shelf using
shelve.open:

Morpork:~/tmp repton$ python -m timeit -s 'import shelve' 'd =
shelve.open(test.shelf)'
1000 loops, best of 3: 1.95 msec per loop

Now we'll measure how long it takes python to open a shelf and turn it
into a string:

Morpork:~/tmp repton$ python -m timeit -s 'import shelve' 'd =
shelve.open(test.shelf)' 'str(d)'
10 loops, best of 3: 51.5 msec per loop

So, that's about a factor of 25 difference.

HTH!

-- 
John.


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] using shelve

2007-06-21 Thread chrispython
I created a shelf called 'myshelf' and put two objects in it, a string and a 
list. When I open the shelf I get:

 d=shelve.open('/Users/development/Desktop/myshelf')
 d.keys()
['dir1', 'dir2']
 d
{'dir2': '/Users/development/Desktop/RSSReaderApp/RSS.db', 'dir1': 
['.DS_Store', '.localized', 'access the latest version', 'Python Quick 
Reference.webloc', 'rssdb', 'workspace']}

It seems that when you use shelve.open(), it actually brings the entire shelf 
dictionary into memory, accessible through d.

What if you had 100 objects in myshelf, or 1000 or 100,000? Wouldn't it get 
bogged down?

If so, what is it good for and how would you store a large number of objects? 
(Storing strings and lists in an sql database is probably the way to go for 
this simple example, but what if  you had more complex objects?)

Thanks
Chris V.


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Subclassing vs. stand alone functions

2007-06-15 Thread chrispython
Hi there,

I am new to Python and trying to get my head around the OO stuff. I guess my 
question is - when do you go with subclassing vs. making a standalone function? 

Let's say you want to load a dictionary. Do I create a function that accepts 
some argument (say a file name) and returns a dictionary, or do I subclass dict 
and override the __init__  and __setitem__ functions to make 'self-loading' 
dictionary? It seems the end result is the same.

Here is a follow-up if you will indulge me...

I created a class called WebPage which is a stand-alone class (I get that). It 
loads a web page template, and has a function to update the replacement vars 
with your data (updHtmlVar), and another to spit out the html to a file 
(wrtHtml). Do you subclass WebPage for each particular page you want (because 
you can customize it with load functions for each piece of data) or do you just 
use it as is, and create separate functions outside the class that load the 
data and you just use updHtmlVar to load it into your WebPage object? Again, 
the end result is the same.

(I can send code samples if it will help).

Thanks,

Chris
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Subclassing vs. stand alone functions

2007-06-15 Thread chrispython

I am new to Python and trying to get my head around 
the OO stuff. I guess my question is - when do you go 
with subclassing vs. making a standalone function? 

OK, I'll take a slightly different approach than the other 
answers so far.

First: procedural and OO styles of programming are diffrent 
ways of thinking about a problem. Any programming problem 
can be solved using either approach and both approaches 
are equally good, neither is intrinsically better than the other.

Second: Some problems are more amenable to an OO
aproach than a procedural and vice versa. And the majority 
can be done either way with very little to choose between 
them.

I will now assume that you understand the procedural way 
and already know how to apply good procedural design, 
including modularity featuring loose coupling and tight 
cohesion. In addition data structure design and its relationship 
to your procedural design should be a concept familiar to you.
( In a perfect world you'll also be familiar with the princuiples of 
functional programming and the lambda calculus, but that's 
possibly asking too much.)

Not sure about the lambda calculus, but I have been doing procedural 
programming 
for about 10 years. (I try my best for  modularity and all that good stuff :)
That leaves the question of why and wjen should we use OOP?

OOP suits programs that feature a high level of correspondence 
between the real world and the software modfel we are building.
For example simulation software (including many games) usually 
involves the representation and control of a number of objects. 
It is a natural link to model these objects as classes and create 
corresponding objects in our solution. Similarly GUIs are made 
up of windows, widgets, etc. Again these have a fairtly clear 
translation into objects.

When we get into problems primarily of algorithms, or of 
transforms to fixed data then an OOP style is not always such 
an obvious fit. Similarly when modelling complex state 
machines the applicability of OOP can be less obvious and 
a traditional table driven procedural style may seem better suited.

In those cases the decision to use OOP is likely to be driven 
by the desire to create a reusable component. Something that 
can be utilised across multiple projects. or it may be driven by 
the desire to abstract away a complex process or data structure.
Hiding it behind a simplere API.  This can be done using 
traditional approaches but usually only at the cost od writing 
an awful lot of code or by exposing the data structure at least 
for initialisation purposes.

Thanks, this is just what I needed! A way to think about which to use.

Now to your examples:

Let's say you want to load a dictionary. 

Why would anyone ever want to load a dictionary?

I just want to create dictionary with some data in it. The data
comes from a file, let's say. I would then go on to do something with
the dictionary - like use it as input to another function. (Sorry, I am 
thinking
procedurally, or are dictionaries typically populated for you by 
the functions you call... maybe it's just a bad example.

What is the higher level goal you are trying to achieve?
Is the dictionary part of the solution or the problem?
If it is part of the problem a dictionary object may be appropriate. 
If its part of the solution, and you are already using a non 
OOP approach why would you want an object? Unless its 
for the reasons above - reuse or abstraction that is hard using 
procedures. But you should very rarely be making decisions
at this level unless you have alrwady decided on amn overall 
approach and you are considering an exception to the overall 
style. ie Should I create a function in an OOP design or should 
I create a class in a procedural design. (Mixing styles is OK 
but will normally involve some compromises)

Do I create a function that accepts some argument 
or do I subclass dict and override 
It seems the end result is the same.

Quite so and the andswer will depend on what you are 
trying to achieve. There is no definitive right answer.

I created a class called WebPage which is a stand-alone 
class (I get that). 

Sorry, I don't get it! :-).
Do you mean you only have a class and never create any instances?
No.
Or do you mean you don;t subclass anything in defining it?
Yes.
Or do you mean you only create a single instance?
You could have multiple instances. 

It loads a web page template, and has a function to update 
the replacement vars with your data (updHtmlVar), and another 
to spit out the html to a file (wrtHtml). 

The data that this page holds is an html template.
Does it also hold the data displayed by the html? 
in 
which case its not updating with 'your' data but with 
*its own* data. But it may allow you to pass some data 
to it. Or is it that it renders the html only when given 
some data? Which it doesn't store?
It stores the data and the template. When it is instantiated, 
you just have the template and the variables. You