473,387 Members | 1,590 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,387 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 3065
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.