save class

N

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
 
G

Gabriel Genellina

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.
 
N

nik

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)?






You just create the .py file with any text editor, containing the source
code for all your classes.



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.

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
 
G

Gabriel Genellina

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*.
 
J

Josiah Carlson

Gabriel said:
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
 
N

nik

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.
 
G

Gabriel Genellina

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.
 
J

Josiah Carlson

nik said:
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
 
D

Diez B. Roggisch

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


Diez
 
N

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top