noteed / python-pickle Goto Github PK
View Code? Open in Web Editor NEWSerialization/deserialization using Python Pickle format
License: Other
Serialization/deserialization using Python Pickle format
License: Other
Hi, I am running this code on a protocol 2 file:
main :: IO ()
main = do putStrLn "Start"
byteString <- S.readFile worldFileName :: IO S.ByteString
let res = unpickle byteString
case res of
Left err -> putStrLn $ "Can't unpickle .\nUnpickling error:\n " ++ err
Right v -> putStrLn "Well done!"
putStrLn "Done"
And I keep getting:
Start
civs: Prelude.undefined
where civs is the name of my app. Do you know what I am doing wrong?
We had written code as -
import os
import builtins
import pickle
import sys
sys.tracebacklimit=0
import traceback
import io
from logging import Logger
safe_builtins = {
'range',
'complex',
'set',
'frozenset'
}
class RestrictedUnpickler(pickle.Unpickler):
def find_class(self, module, name):
# Only allow safe classes from builtins.
if module == "builtins" and name in safe_builtins:
return getattr(builtins, name)
# Forbid everything else.
raise pickle.UnpicklingError("global '%s.%s' is forbidden" %
(module, name))
def restricted_loads(s):
"""Helper function analogous to pickle.loads()."""
return RestrictedUnpickler(io.BytesIO(s)).load()
def func1(a):
try:
x= restricted_loads(pickle.dumps(a))
return x
except pickle.UnpicklingError :
s= pickle.UnpicklingError("unsupported persistent object")
return s
def func2(s):
try:
x=restricted_loads(pickle.dumps(s))
return s[x]
except pickle.UnpicklingError :
s=traceback.format_exc()
return s
if __name__ == "__main__":
a=range(int(input()))
b=func1(a)
print(b)
y=tuple(input())
z=func2(y)
print(z)
Getting
TypeError: tuple indices must be integers or slices, not tuple
Input (stdin)
50
"a", "b", "c", "d", "e", "f", "g", "h"
Your Output (stdout)
range(0, 50)
Expected Output
range(0, 50)
Traceback (most recent call last):
_pickle.UnpicklingError: global 'builtins.slice' is forbidden
Trying to save Random Forest model. (sklearn, python 2.7x)
All the ways fail :
def save_model(self, fname):
with open(fname,'wb') as f :
pickle.dump(self.model, f)
pickle: TypeError: can't pickle instancemethod objects
joblib : PicklingError: Can't pickle <type 'instancemethod'>: it's not found as __builtin__.instancemethod
cPickle : TypeError: can't pickle instancemethod objects
dill : ValueError: pickle protocol must be <= 2
....... /usr/lib/python2.7/pickle.pyc in save(self, obj) 284 f = self.dispatch.get(t) 285 if f: --> 286 f(self, obj) # Call unbound method with explicit self 287 return 288 /usr/lib/python2.7/pickle.pyc in save_dict(self, obj) 653 654 self.memoize(obj) --> 655 self._batch_setitems(obj.iteritems()) 656 657 dispatch[DictionaryType] = save_dict /usr/lib/python2.7/pickle.pyc in _batch_setitems(self, items) 667 for k, v in items: 668 save(k) --> 669 save(v) 670 write(SETITEM) 671 return /usr/lib/python2.7/pickle.pyc in save(self, obj) 304 reduce = getattr(obj, "__reduce_ex__", None) 305 if reduce: --> 306 rv = reduce(self.proto) 307 else: 308 reduce = getattr(obj, "__reduce__", None) /usr/lib/python2.7/copy_reg.pyc in _reduce_ex(self, proto) 68 else: 69 if base is self.__class__: ---> 70 raise TypeError, "can't pickle %s objects" % base.__name__ 71 state = base(self) 72 args = (self.__class__, base, state) TypeError: can't pickle instancemethod objects
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.