473,221 Members | 1,751 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,221 software developers and data experts.

save class

nik
Hi,

I would like to create a class and then save it for re-use later. I
have tried to use pickle, but am not sure if that is right. I am
sorry, but I am new to python.

Basically, I have a class, Map. I want to be able to create new maps:
MapA, MapB... that have Map as the base class.

start with-
class Map:
pass

and then get a different class

class MapA(Map):
pass

that can be saved in a .py file for re-use

so far I thought that -
cls = new.classobj('MapA', (Map, ), {})
file = open('somefile', mode='w')
pickle.dump(cls, file)

-might work, but it didn't.... can anybody point me in the right
direction? I know that classes must get saved from the interactive
console, so I would think that it would be a standard thing to do.

Thank you,
Nik

Jun 14 '07 #1
9 3056
En Wed, 13 Jun 2007 22:20:16 -0300, nik <ni*****@gmail.comescribió:
I would like to create a class and then save it for re-use later. I
have tried to use pickle, but am not sure if that is right. I am
sorry, but I am new to python.
Do you want to save the *source*code* of your class, or do you want to
save created *instances* -objects- of your classes to retrieve them later
(like a database)?
Basically, I have a class, Map. I want to be able to create new maps:
MapA, MapB... that have Map as the base class.

start with-
class Map:
pass

and then get a different class

class MapA(Map):
pass

that can be saved in a .py file for re-use
You just create the .py file with any text editor, containing the source
code for all your classes.
so far I thought that -
cls = new.classobj('MapA', (Map, ), {})
file = open('somefile', mode='w')
pickle.dump(cls, file)

-might work, but it didn't.... can anybody point me in the right
direction? I know that classes must get saved from the interactive
console, so I would think that it would be a standard thing to do.
This would try to save the *class* definition, which is usually not
required because they reside on your source files.
If this is actually what you really want to do, try to explain us exactly
why do you think so. Chances are that there is another solution for this.

--
Gabriel Genellina

Jun 14 '07 #2
nik
On Jun 13, 6:48 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Wed, 13 Jun 2007 22:20:16 -0300, nik <nikb...@gmail.comescribió:
I would like to create a class and then save it for re-use later. I
have tried to usepickle, but am not sure if that is right. I am
sorry, but I am new to python.

Do you want to save the *source*code* of your class, or do you want to
save created *instances* -objects- of your classes to retrieve them later
(like a database)?
Basically, I have a class, Map. I want to be able to create new maps:
MapA, MapB... that have Map as the base class.
start with-
class Map:
pass
and then get a different class
class MapA(Map):
pass
that can be saved in a .py file for re-use

You just create the .py file with any text editor, containing the source
code for all your classes.
so far I thought that -
cls = new.classobj('MapA', (Map, ), {})
file = open('somefile', mode='w')
pickle.dump(cls, file)
-might work, but it didn't.... can anybody point me in the right
direction? I know that classes must get saved from the interactive
console, so I would think that it would be a standard thing to do.

This would try to save the *class* definition, which is usually not
required because they reside on your source files.
If this is actually what you really want to do, try to explain us exactly
why do you think so. Chances are that there is another solution for this.

--
Gabriel Genellina
Thanks for the response.

It would seem that I want to actually save the source code for the
class. I know that I could of course open up an editor and just make
it, but my ideal would be to have the base class, Map, be able to make
the sub-classes. I don't want the class definition. What I want is an
actual class that I could later import and use somewhere else. I am
planning to have each one of these map objects contain a different
dictionary and then be able to import the map into the application,
but have certain methods defined in the Map super-class to draw data
out of the specific map's specific dictionary. I hope that makes
sense.

Something like,
class Map:
dict = {}
def DoSomething(self):
pass

def MakeNewMapSubClass(self, newclassname):
""" make a new file, newclassname.py that contains a new
class
newclassname(Map) that inherits from base-class Map.

Thanks,
Nik

Jun 14 '07 #3
En Wed, 13 Jun 2007 23:11:22 -0300, nik <ni*****@gmail.comescribió:
On Jun 13, 6:48 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
>En Wed, 13 Jun 2007 22:20:16 -0300, nik <nikb...@gmail.comescribió:
I would like to create a class and then save it for re-use later. I
have tried to usepickle, but am not sure if that is right. I am
sorry, but I am new to python.

Do you want to save the *source*code* of your class, or do you want to
save created *instances* -objects- of your classes to retrieve them
later (like a database)?
It would seem that I want to actually save the source code for the
class. I know that I could of course open up an editor and just make
it, but my ideal would be to have the base class, Map, be able to make
the sub-classes. I don't want the class definition. What I want is an
actual class that I could later import and use somewhere else. I am
planning to have each one of these map objects contain a different
dictionary and then be able to import the map into the application,
but have certain methods defined in the Map super-class to draw data
out of the specific map's specific dictionary. I hope that makes
sense.

Something like,
class Map:
dict = {}
def DoSomething(self):
pass

def MakeNewMapSubClass(self, newclassname):
""" make a new file, newclassname.py that contains a new
class
newclassname(Map) that inherits from base-class Map.
And are you sure you actually need different subclasses? Will you
construct them several instances of each subclass? From the above
description I feel you want just different Map *instances*, each with its
own dict, not different *subclasses*.

--
Gabriel Genellina

Jun 14 '07 #4
Gabriel Genellina wrote:
En Wed, 13 Jun 2007 23:11:22 -0300, nik <ni*****@gmail.comescribió:
>It would seem that I want to actually save the source code for the
class. I know that I could of course open up an editor and just make
it, but my ideal would be to have the base class, Map, be able to make
the sub-classes. I don't want the class definition. What I want is an
actual class that I could later import and use somewhere else. I am
planning to have each one of these map objects contain a different
dictionary and then be able to import the map into the application,
but have certain methods defined in the Map super-class to draw data
out of the specific map's specific dictionary. I hope that makes
sense.

Something like,
class Map:
dict = {}
def DoSomething(self):
pass

def MakeNewMapSubClass(self, newclassname):
""" make a new file, newclassname.py that contains a new
class
newclassname(Map) that inherits from base-class Map.

And are you sure you actually need different subclasses? Will you
construct them several instances of each subclass? From the above
description I feel you want just different Map *instances*, each with
its own dict, not different *subclasses*.
What you said, and that his solution sounds like a Java approach to the
problem (subclass an abstract base class that calls specific methods on
the subclass to "do the right thing").

To offer the OP source he can use...

class Map:
def __init__(self):
self.dict = {}
def DoSomething(self):
#do something with self.dict

Every instance gets a new dictionary. Now, if he actually wants to
change the behavior of the DoSomething method, of course then it would
make sense to subclass Map.
- Josiah
Jun 14 '07 #5
nik
On Jun 13, 10:04 pm, Josiah Carlson <josiah.carl...@sbcglobal.net>
wrote:
Gabriel Genellina wrote:
En Wed, 13 Jun 2007 23:11:22 -0300, nik <nikb...@gmail.comescribió:
It would seem that I want to actually save the source code for the
class. I know that I could of course open up an editor and just make
it, but my ideal would be to have the base class, Map, be able to make
the sub-classes. I don't want the class definition. What I want is an
actual class that I could later import and use somewhere else. I am
planning to have each one of these map objects contain a different
dictionary and then be able to import the map into the application,
but have certain methods defined in the Map super-class to draw data
out of the specific map's specific dictionary. I hope that makes
sense.
Something like,
class Map:
dict = {}
def DoSomething(self):
pass
def MakeNewMapSubClass(self, newclassname):
""" make a new file, newclassname.py that contains a new
class
newclassname(Map) that inherits from base-class Map.
And are you sure you actually need different subclasses? Will you
construct them several instances of each subclass? From the above
description I feel you want just different Map *instances*, each with
its own dict, not different *subclasses*.

What you said, and that his solution sounds like a Java approach to the
problem (subclass an abstract base class that calls specific methods on
the subclass to "do the right thing").

To offer the OP source he can use...

class Map:
def __init__(self):
self.dict = {}
def DoSomething(self):
#do something with self.dict

Every instance gets a new dictionary. Now, if he actually wants to
change the behavior of the DoSomething method, of course then it would
make sense to subclass Map.

- Josiah
I am hoping to change the self.dict for each subclass. I realize that
I could save self.dict to file and then load in different dicts each
time I get a new instance of class. But I want to be able to make
subclasses of map that each have different self.dict. Then when I need
to use them, just import the module and use the specific dict, instead
of having to keep track of a separate dictionary file. I am new to
this, but I thought that this would be a regular thing to do in
python, because people must make classes in the interactive console
and then export them somehow for later use.

Thank you for your responses.

Jun 14 '07 #6
En Thu, 14 Jun 2007 16:05:14 -0300, nik <ni*****@gmail.comescribió:
On Jun 13, 10:04 pm, Josiah Carlson <josiah.carl...@sbcglobal.net>
wrote:
>Gabriel Genellina wrote:
En Wed, 13 Jun 2007 23:11:22 -0300, nik <nikb...@gmail.comescribió:
>It would seem that I want to actually save the source code for the
class. I know that I could of course open up an editor and just make
it, but my ideal would be to have the base class, Map, be able to
make
>the sub-classes. I don't want the class definition. What I want is an
actual class that I could later import and use somewhere else. I am
planning to have each one of these map objects contain a different
dictionary and then be able to import the map into the application,
but have certain methods defined in the Map super-class to draw data
out of the specific map's specific dictionary. I hope that makes
sense.
And are you sure you actually need different subclasses? Will you
construct them several instances of each subclass? From the above
description I feel you want just different Map *instances*, each with
its own dict, not different *subclasses*.

What you said, and that his solution sounds like a Java approach to the
problem (subclass an abstract base class that calls specific methods on
the subclass to "do the right thing").

To offer the OP source he can use...

class Map:
def __init__(self):
self.dict = {}
def DoSomething(self):
#do something with self.dict

Every instance gets a new dictionary. Now, if he actually wants to
change the behavior of the DoSomething method, of course then it would
make sense to subclass Map.

- Josiah

I am hoping to change the self.dict for each subclass. I realize that
I could save self.dict to file and then load in different dicts each
time I get a new instance of class. But I want to be able to make
subclasses of map that each have different self.dict. Then when I need
to use them, just import the module and use the specific dict, instead
of having to keep track of a separate dictionary file. I am new to
As Josiah said, I still don't see why do you want a *subclass*. If the
only difference between your "subclasses" is their dict, they're not
subclasses but just Map *instances*.
Let's say, have a class Person, with attributes "name" and "email". If I
want to represent two different persons, I would create two Person
*instances*: Person(name="Gabriel", email="gagsl-py2@...") and
Person(name="nik", email="nikbaer@...")
Classes try to capture behavior and structure; instances contain state
(very roughly said). One *could* use two subclasses here, and in certain
circumstances it may be useful, but it's not the most common case.
this, but I thought that this would be a regular thing to do in
python, because people must make classes in the interactive console
and then export them somehow for later use.
I've never done that. I only use the interactive interpreter for testing
purposes, I never "develop" code inside the interpreter.

--
Gabriel Genellina

Jun 14 '07 #7
nik wrote:
of having to keep track of a separate dictionary file. I am new to
this, but I thought that this would be a regular thing to do in
python, because people must make classes in the interactive console
and then export them somehow for later use.
Create a file. Put your code in it. Run your code. Occasionally
copy/paste your code into the console for testing and/or learning about
how Python works. If you write something you want to keep in the
console, copy it out of the console and paste it into your source file(s).

- Josiah
Jun 15 '07 #8
of having to keep track of a separate dictionary file. I am new to
this, but I thought that this would be a regular thing to do in
python, because people must make classes in the interactive console
and then export them somehow for later use.
No. That's not how things work. One does dabble with the interpreter a
bit, sometimes even creating a tiny class.

But except from the occasional expression tested in the interpreter,
it's not common to use that code and save it.

Write python-files from the start. Then either execute them

python myfile.py

or if you insist, do
python
>>import myfile
myfile.some_function()

Diez
Jun 15 '07 #9
nik
Thank you for all the responses. In light of what you've told me I
have gone back to storing my specific dictionaries in text files and
then reading them in to the class.

Thank you,
Nik
Jun 19 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: George | last post by:
This program need to draw the some triangles into a 512 × 512 buffer (in memory). Or save it to a file. #include "project3.h" Image::Image(int xres, int yres): xres(xres), yres(yres) {...
0
by: prakash | last post by:
Dear Friends I am new guy to Visual C++.NET I've program to save website as a image vc++.net . It have a function "SaveSnapshot" to save the webpage as an image On that function ifor saving...
4
by: Glenn M | last post by:
I have a shared XML file on a server . i also have one xslt file that performs a simple transform on in to view the data. now i want to have another page that lets users modify the shared xml...
1
by: Irene | last post by:
Hello all! I'm creating a web site in ASP.NET (VB.NET). One of the requirements was to allow users to create orders going through several steps. A must have is to have an option to save the work...
2
by: mpreisdorf | last post by:
I want to save the raw data of a class (without the .NET object overhead) to a binary file. For example: ref class Test { public: String^ name; Int32 number; ..... }
2
by: mast2as | last post by:
Hi there, for a long time I've been trying to think of way of saving different data of different types using one single class (well 2 in reality, a class for the data, and 1 class for a list of...
1
by: Stedak | last post by:
I have the following class I use to save Tiff's. The problem I have with it is that the final size of the images are very large. If we scan directly to a file the final tiff may be 600-900 kb.s but...
0
by: amrhi | last post by:
Hy Guys , Can anybody help me ? I try to make small web database in my unit. Some of fields have on change behaviour to get other data that automatically filled other text field. But when i try to...
3
by: Angus | last post by:
I have a web page with a toolbar containing a Save button. The Save button can change contextually to be a Search button in some cases. Hence the button name searchsavechanges. The snippet of...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.