Re: What is a class method?

2010-08-23 Thread Denis Gomes
John,

   I agree with you and I also think the definition given on the official
python site is somewhat confusing, at least for an engineer like myself.
But I'll take a stab at explaning it using what I know thus far.

  I think to understand what a class method is you have to first understand
what a class variable is.  Take this code snippet for example.

class foo(object):
 x=10
 def __init__(self):
  self.x=20

In this example if you create an instance of foo and call it f.  You can
access the instance attribute x by using f.x or you can access the class
variable using the notation foo.x.  These two will give you two different
results.  Now lets do something a bit more interesting.
Say you have the following snippet.

class foo(object):
 x=0
 def __init__(self):
  self.x=10
  foo.x+=1

>>>f=foo()
>>>g=goo()
>>>f.x
10
>>>g.x
10
>>>foo.x
2

What happened here is that the class variable foo.x is used to keep a count
of the total number of instances created.  So we see that a class variable
can be looked at as what "connects" the two instances in a way, so that data
can be shared between instances of the same class.  This defintion may very
well only apply to this case, but I think the mechanics is fundamentally the
same.
Keeping this in mind, lets make the jump to class methods.  When an
instance of a class is created, the methods are just functions that "work
on" the attributes (the variables).  Similarly, a class method is a method
that works on a class variable.  For example,

class foo(object):
 x=10
 def __init__(self):
  self.x=20
 @classmethod
 def change(self):
   self.x=15

>>>f=foo()
>>>f.x
20
>>>foo.x
10
>>>f.change()
>>>f.x
20
>>>foo.x
15

So this is my explanation for what a classmethod is.  Hope it helps.  Good
luck.

Denis

On Mon, Aug 23, 2010 at 2:24 AM, John Nagle  wrote:

> On 8/22/2010 9:16 PM, James Mills wrote:
>
>> On Mon, Aug 23, 2010 at 1:53 PM, Paulo da Silva
>>
>>   wrote:
>>
>>> I did it before posting ...
>>>
>>> The "explanation" is not very clear. It is more like "how to use it".
>>>
>>
>> Without going into the semantics of languages basically the
>> differences are quite clear:
>>
>> @classmethod is a decorator that warps a function with
>> passes the class as it's first argument.
>>
>> @staticmethod (much like C++/Java) is also a decorator that
>> wraps a function but does not pass a class or instance as
>> it's first argument.
>>
>
>That reads like something the C++ standards revision committee
> would dream up as they add unnecessary template gimmicks to the
> language.
>
>John Nagle
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


python+sqlite3 in memory database

2010-08-30 Thread Denis Gomes
Hi Everyone,

I am using sqlite3 with python2.5 and the pysqlite wrapper. I am trying
to copy tables from one database (in memory) to another database (file)
using ATTACH. I looked on the internet and found a couple of sites that show
how to do this but the table schema is not copied.

def SaveToFile(self,filename):
# Attach external db file - give it handle filename
# Attaching automatically creates a database by default
self.__curs.execute("ATTACH %s AS %s" % (filename,filename))
table_names=self.__curs.execute("SELECT name FROM main.sqlite_master
WHERE type='table'").fetchall()
for table_name, in table_names:
#copy in mem db to persistent db
self.__curs.execute("CREATE TABLE %s.%s AS SELECT * FROM
main.%s" % (filename,table_name,table_name))
self.__curs.execute("DETACH %s" % filename)  # Detach external db"""


Is there a way to copy the schema from the sqlite_master table.  I know we
can used iter.dump from within python to dump to text based SQL but I would
prefer not to do this.  Eventually my goal is to dynamically load and unload
sections of a file based database (could be tables or rows) in and out of
memory for effeciency purposes.  Any help is appreciated. Thanks in advance.

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


Re: python+sqlite3 in memory database

2010-08-30 Thread Denis Gomes
Hey Benjamin,

 Take a look at this website I found about cached and in-memory databases.
I think the gist of the article is that caching is good if you are doing
SELECTs on data that is frequently used whereas in-memory speeds up
writes, (inserts and updates) to the db as well as querying. Maybe I am
missing something?

http://www.mcobject.com/in_memory_database

Denis
 <http://www.mcobject.com/in_memory_database>


On Mon, Aug 30, 2010 at 3:00 PM, Benjamin Peterson wrote:

> Denis Gomes  gmail.com> writes:
>
> >
> > Eventually my goal is to dynamically load and unload sections of a file
> based
> database (could be tables or rows) in and out of memory for effeciency
> purposes.
>
> Have you actually found this to be an useful optimization? SQLite already
> internally caches database information in memory.
>
>
>
>
> --
>  http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python+sqlite3 in memory database

2010-08-30 Thread Denis Gomes
Yep, I see what you are saying.  I am going to do a bit more research to see
how sqlite3 works internally, ie. cache size, page size, etc, and then
decide if I will need to mess with in-memory databases.

Thanks for your insight, appreciate it.

Denis

On Mon, Aug 30, 2010 at 3:51 PM, Benjamin Peterson wrote:

> Denis Gomes  gmail.com> writes:
>
> >
> >
> > Hey Benjamin,
> >
> >  Take a look at this website I found about cached and in-memory
> databases.  I
> think the gist of the article is that caching is good if you are doing
> SELECTs
> on data that is frequently used whereas in-memory speeds up
> writes, (inserts and
> updates) to the db as well as querying. Maybe I am missing something?
>
> Well, of course, but there's little point to doing INSERTs and UPDATEs if
> you
> don't write them to disk at some point. You could just have a long running
> transaction which will not write to the database file (though depending on
> how
> you have sqlite setup it may write to a journal file) until you commit it.
>
>
>
>
> --
>  http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: GUibuilder evaluation

2010-09-06 Thread Denis Gomes
Hello,

   There are really three you can choose from if you are serious about
making GUI applications.  PyQt as already suggested.  wxPython and pyGTK.
 Tkinter is good in my opinion if you are making smaller gui based programs.
 PyQt has an extensive number of additional features, including networking,
OpenGL, Multimedia, Database etc.  It is used for the KDE desktop
environment.   PyQt has a very powerful Designer which you can used to
generate python code very fast for complex guis.  wxPython is only GUI
centric, but it does have a host of widgets you can play with and pre-made
examples you can use to speed up your programming.  It also has a gui
building called Boa Constructor which is functional but not in the same
class as the qt designer (this is my opinion, try it).  As for pyGTK, it is
used for Gnome.  I am not to familiar with it but it looks like a good gui
toolkit as well.  So my advice is to try all three and see which one you
like the best and go from there.

Denis

On Mon, Sep 6, 2010 at 6:15 AM, wissem belguidoum wrote:

> On 6 sep, 09:54, Niklasro  wrote:
> > Hello
> > Making a GUI, could you recommend tkinter or any other proposal?
> > Thanks
>
> Hi,
>
> I am considering to learn Qt, which is a multi-platform widget
> liberary and a RAD IDE...,
> basically for C++ programing but there is a binding called PyQt for
> python.
>
> Good luck.
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list