I have this class, and I've been pickling it's objects as a file format
for my program, which works great. The problems are a.) how to handle
things when the user tries to load a non-pickled file, and b.) when they
load a pickled file of the wrong class.
a. I can handle with a general exception I guess. I just tried to
pickle.load() a .pyc and it failed with a 'KeyError' exception - however
that works. It might do that every time, it might not.
Regarding b., if I type check I simply get the 'instance' type, which
doesn't get me all of the way there because I might have an instance of
the wrong class. I can't find any sort of __type__ attribute to set in
the objects. Perhaps I should use __str__? 6 1229
Gordon Airporte wrote: I have this class, and I've been pickling it's objects as a file format for my program, which works great. The problems are a.) how to handle things when the user tries to load a non-pickled file, and b.) when they load a pickled file of the wrong class.
a. I can handle with a general exception I guess. I just tried to pickle.load() a .pyc and it failed with a 'KeyError' exception - however that works. It might do that every time, it might not.
I hope you're aware of the fact that pickle is not suitable for applications
where you cannot trust the source; see e.g. http://www.livejournal.com/users/jcalderone/15864.html
(one of the comments show how to place restrictions on what can be loaded
from a given pickle)
Regarding b., if I type check I simply get the 'instance' type, which doesn't get me all of the way there because I might have an instance of the wrong class. I can't find any sort of __type__ attribute to set in the objects. Perhaps I should use __str__?
how about a plain
obj = cPickle.load(...)
if not isinstance(obj, Class):
print "expected a Class instance"
</F>
Gordon Airporte wrote: I have this class, and I've been pickling it's objects as a file format for my program, which works great. The problems are a.) how to handle things when the user tries to load a non-pickled file, and b.) when they load a pickled file of the wrong class.
a. I can handle with a general exception I guess. I just tried to pickle.load() a .pyc and it failed with a 'KeyError' exception - however that works. It might do that every time, it might not.
I hope you're aware of the fact that pickle is not suitable for applications
where you cannot trust the source; see e.g. http://www.livejournal.com/users/jcalderone/15864.html
(one of the comments show how to place restrictions on what can be loaded
from a given pickle)
Regarding b., if I type check I simply get the 'instance' type, which doesn't get me all of the way there because I might have an instance of the wrong class. I can't find any sort of __type__ attribute to set in the objects. Perhaps I should use __str__?
how about a plain
obj = cPickle.load(...)
if not isinstance(obj, Class):
print "expected a Class instance"
</F>
isinstance! Now why didn't I know about that? Thanks you. I guess I'll
need a throwaway instance of the class to run type() on to get a usable
type object for comparison, but I'll work something out.
As to the security considerations...this is a small enough program with
a limited enough release that I'm not going to sweat it, although I will
keep this in mind.
isinstance! Now why didn't I know about that? Thanks you. I guess I'll
need a throwaway instance of the class to run type() on to get a usable
type object for comparison, but I'll work something out.
As to the security considerations...this is a small enough program with
a limited enough release that I'm not going to sweat it, although I will
keep this in mind.
> I guess I'll need a throwaway instance of the class to run type() on to get a usable type object for comparison, but I'll work something out.
Never mind - I can just pass the name of the class, as it should be.
> I guess I'll need a throwaway instance of the class to run type() on to get a usable type object for comparison, but I'll work something out.
Never mind - I can just pass the name of the class, as it should be. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: dgaucher |
last post by:
Hi,
I want to consume a Web Service that returns a choice, but my C++ client
always receives the same returned type. On the other hand, when I am using a
Java client, it is working fine (of...
|
by: H. S. |
last post by:
Hi,
I am trying to compile these set of C++ files and trying out class
inheritence and function pointers. Can anybody shed some light why my
compiler is not compiling them and where I am going...
|
by: Jason Cartwright |
last post by:
I have an abstract base class and two derived classes that I want to
serialize and deserialize with schema validation. When I serialize
instances of the derived classes the XmlSerializer adds the...
|
by: Ced |
last post by:
Hi,
i'm not an expert in C but i try to compile BTNG software under linux
kernel 2.4.2-2. I get these errors at the very first stage. Does
someone could have a rapid look on this and tell me...
|
by: Chris Fink |
last post by:
When I am consuming a webservice, an object has an undefined value
(inq3Type.Call3Data). I do not completely understand why this is happening
and apologize for the vague question. My assumption...
|
by: Xah Lee |
last post by:
in March, i posted a essay “What is Expressiveness in a Computer
Language”, archived at:
http://xahlee.org/perl-python/what_is_expresiveness.html
I was informed then that there is a academic...
|
by: john |
last post by:
Hi to All
To demonstrate:
public class MyBaseGenericClass<T>
{
}
public class MyGenericClass1<T: MyBaseGenericClass<T>
{
|
by: Sky |
last post by:
I have been looking for a more powerful version of GetType(string) that will
find the Type no matter what, and will work even if only supplied
"{TypeName}", not the full "{TypeName},{AssemblyName}"...
|
by: weirdwoolly |
last post by:
Hopefully someone will be able to help.
I have written a stored procedure in C++ called from a Java test
harness to validate the graphic data types in C++ and their use.
I have declared the...
|
by: amanjsingh |
last post by:
Hi, I am trying to implement Java Web Service using Apache Axis2 and Eclipse as a tool. I have created the basic code and deployed the service using various eclipse plugin but when I try to invoke...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |