My goal is to serialize a dictionary object to a specific file location, and read it back each time the program is run. The following works in Python2.7, but throws an error in Python3.4. What confuses me is that this works the first time an object is saved to disk, but not on subsequent executions.
The problem seems to be that in setitem an error is thrown that says that 'ConfigDict' object has no attribute '_config_file'. Why does it not have a value any time except the first time I run the script??
import os
import pickle
class ConfigDict(dict):
def __init__(self, config_name): # Name of a pickle file within configs directory
self._config_directory = 'C:\\Users\\myfilepath\\configs'
self._config_file = self._config_directory + '\\' + config_name + '.pickle'
# If file does not exist, write a blank pickle file.
if not os.path.isfile(self._config_file):
with open(self._config_file, 'wb') as fh:
pickle.dump({}, fh)
# Read the pickle file from disk.
with open(self._config_file, 'rb') as fh:
pkl = pickle.load(fh)
self.update(pkl)
def __setitem__(self, key, value):
dict.__setitem__(self, key, value)
with open(self._config_file, 'wb') as fh:
pickle.dump(self, fh)
cc = ConfigDict('DBConfig')
print()
print()
cc['config_val_1'] = '1'
cc['config_val_3'] = '3'
print(cc['config_val_3'])
Here's the full traceback:
Traceback (most recent call last):
File "C:/Users/filepath/test.py", line 25, in <module>
cc = ConfigDict('DBConfig')
File "C:/Users/filepath/test.py", line 16, in __init__
pkl = pickle.load(fh)
File "C:/Users/filepath/test.py", line 21, in __setitem__
with open(self._config_file, 'wb') as fh:
AttributeError: 'ConfigDict' object has no attribute '_config_file'
Related
I am trying to read my dictionary(in my file) using ast
import ast
import os
class just_a_class():
def __init__(self, file,):
self.file = file()
self.read_file = open(self.file,'r+')
def read_file(self):
dict = ast.literal_eval(self.read_file.read())
return 'Input: \n\n {}'.format(dict)
the_class = just_a_class("dict.txt")
print(the_class.self.read_file())
Error:
Traceback (most recent call last):
File "c:/Users/Barış/Desktop/Onemli_Programlarim/Connection_PL/conn.py", line 13, in <module>
the_class = just_a_class("dict.txt")
File "c:/Users/Barış/Desktop/Onemli_Programlarim/Connection_PL/conn.py", line 6, in __init__
self.file = file()
TypeError: 'str' object is not callable
The code below is similar to what is used in the question. I've tried
to improve the readability by using common techniques and conventions,
such as using fname for the filename, instead of file, using a
context manager to open (and auto-close) the file, and capitalization
for class names. Also changed the flags to the open() call to be "r"
instead of "r+", as suggested by ShadowRanger.
import ast
class Just_a_class():
def __init__(self, fname):
self.fname = fname
def read_file(self):
with open(self.fname, 'r') as f:
dict = ast.literal_eval(f.read())
return 'Input: \n\n {}'.format(dict)
the_class = Just_a_class("dict.txt")
print(the_class.read_file())
# {'a': 5, 'b': 'hello'}
I have most of the program done. The last part of this program needs to open the file in append mode> Add 2 names > close file. Then, it has to open file in read mode> print contents> close file.
The file path has been assigned to a variable.
I keep getting the below error. (code is below that)
I don't know what to do to fix this
Traceback (most recent call last):
File "C:\Users\gorri\Desktop\College Work\CPT180_ShellScripting\Assignments\Programs\workWithFiles2.py", line 34, in
cat_files = open(cat_files, 'a')
TypeError: expected str, bytes or os.PathLike object, not TextIOWrapper
from pathlib import Path
import os
import os.path
path_E = Path('E:/CPT180Stuff')
os.chdir(path_E)
cat_files = (path_E / 'pets' / 'cats' / 'catnames.txt')
#opens catnames file to append end and add names.
cat_files = open(cat_files, 'a')
cat_files.write('Princess\nFreya\n')
cat_files.close()
cat_files = open(cat_files, 'r')
cat_files = cat_files.read()
print(cat_files)
cat_files.close()
In your current code, you are first assigning cat_files to the file name, but then in this line:
cat_files = open(cat_files, 'r')
You are now assigning cat_files to a file handle, which is not a string. This is why the next statement fails: it is expecting the filename string, not the file handle. You should use a different variable name for the handle, e.g.:
#opens catnames file to append end and add names.
f = open(cat_files, 'a')
f.write('Princess\nFreya\n')
f.close()
f = open(cat_files, 'r')
f = f.read()
print(f)
f.close()
So, I have class that I use in a Flask app. I use this class in multiple pages, which is why I would like to save the creates class object in a pickle, and unpack it when I need it again. It just keeps on giving me errors.. I have a class that looks similar to this:
class files(name):
def __init__(self, name):
self.name = name
self.settings = Settings()
self.files_directory = self.settings.files_directory
self.files = self.create_list()
def store_files_from_folder(self):
loaded_files = []
files = list_files()
for file in files:
file_path = os.path.join(self.files_directory, file)
print('Loading file: {}'.format(file))
loaded_file = function_reads_in_files_from_folder(file_path, self.name)
loaded_files.append(loaded_file)
print('Loaded {} files'.format(len(loaded_files)))
and I'm trying to create the jsonpickle like this:
creates_class = files("Mario")
jsonpickle_test = jsonpickle.encode(creates_class, unpicklable=False)
result = jsonpickle.decode(jsonpickle_test, files)
But I get the following error:
Traceback (most recent call last):
File "C:\Users\lib\site-packages\IPython\core\interactiveshell.py", line 3343, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-8-23e9b5d176ac>", line 1, in <module>
result = jsonpickle.decode(jsonpickle_test, files)
File "C:\Users\lib\site-packages\jsonpickle\unpickler.py", line 41, in decode
data = backend.decode(string)
AttributeError: type object 'files' has no attribute 'decode'
And I can't get to resolve it. Could someone help me?
The problem is in the passed argument unpickable=False
unpicklable – If set to False then the output will not contain the information necessary to turn the JSON data back into Python objects, but a simpler JSON stream is produced.
You can avoid unpickable=False or load the produced data with json.loads to a dict and then use de kwargs arguments for the object creation
creates_class = files("Mario")
jsonpickle_test = jsonpickle.encode(creates_class, unpicklable=False)
result_dict = json.loads(jsonpickle_test)
create_class = files(**result_dict)
I'm trying to make a config file manager that stores dictionaries in a pickle file, in a config folder in the same directory. I'm having issues with a pesky AttributeError that claims I don't have an attribute '_name', which I defined in the first line of init().
The main program has 2 classes, one inheriting from Exceptions (the error class) and the other inheriting from dict (the main class). The init takes a name and finds if the file exists. If it doesn't, then an empty dictionary is written to the given file.
I receive the error when I:
1. open an existing file to print the keys
2. write a key and value pair to an existing file
3. try and get a key from the dictionary
I've tried not calling dict.setitem in init and nothing changed, I still get the error. I tried just loading the file while doing absolutely nothing else to it, and couldn't get it to work.
import os
import pickle
class ConfigDict(dict):
'''
This class is responsible for all main functions.
Pass a string as an argument without a file type.
Keywords such as 'database' and 'aiconstructer' work well.
'''
def __init__(self, name):
self._name = name+'.pickle'
if not os.path.isfile(self._name):
with open(self._name, 'wb') as f:
pickle.dump('{}', f)
with open(self._name,'rb') as f:
obj = pickle.load(f)
if len(obj) > 2:
for k in obj.keys():
dict.__setitem__(self, k, obj[k])
def __getitem__(self, key):
if not key in self.keys():
raise ConfigKeyError(self, key)
return dict.__getitem__(key)
def __setitem__(self, key, val):
dict.__setitem__(self, key, val)
with open(self._name, 'wb') as f:
pickle.dump(self, f)
The Traceback is as follows:
Traceback (most recent call last):
File "interface.py", line 12, in <module>
test = ConfigDict('alpha')
File "/home/bear/Desktop/Config/confdict.py", line 35, in __init__
obj = pickle.load(f)
File "/home/bear/Desktop/Config/confdict.py", line 47, in __setitem__
with open(self._name, 'wb') as f:
AttributeError: 'ConfigDict' object has no attribute '_name'
The code for interface.py that initiates this is:
import sys
from confdict import ConfigDict, ConfigKeyError
test = ConfigDict('alpha')
if len(sys.argv) == 3:
key = sys.argv[1]
val = sys.argv[2]
print('Writing')
test[key] = val
print('Done')
elif len(sys.argv) == 2:
key = sys.argv[1]
print('{}:{}'.format(key,test[key]))
else:
print('Keys : Values')
print('-----------------')
for k in test.keys():
print('{} : {}'.format(k,test[k]))
I expect to be able to load the contents of the pickle file into self, but instead I get the AttributeError. Is it something I'm doing grammatically wrong, or is there a rule that I forgot to follow? Thank you very much for any help in advance.
Ok well i have another question. I implemented the error checking but for some reason it still isn't working. I still get a python error instead of the error i just wrote in the program.
Traceback (most recent call last):
File "E:/python/copyfile.py", line 31, in <module>
copyFile()
File "E:/python/copyfile.py", line 8, in copyFile
file1 = open(source,"r")
IOError: [Errno 2] No such file or directory: 'C:/Users/Public/asdf.txt'
check out the shutil module in standard library:
shutil.copyfile(src, dst)
http://docs.python.org/2/library/shutil.html#shutil.copyfile
I would rather ask you to write your own:
import os
import hashlib
def md5ChkSum(_file): # Calculates MD5 CheckSum
with open(_file, 'rb') as fp:
hash_obj = hashlib.md5()
line = fp.readline()
while line:
hash_obj.update(line)
line = fp.readline()
return hash_obj.hexdigest()
def copier(_src, _dst):
if not os.path.exists(_src):
return False
_src_fp = open(_src, "r")
_dst_fp = open(_dst, "w")
line = _src_fp.readline()
while line:
_dst_fp.write(line)
line = _src_fp.readline()
_src_fp.close()
_dst_fp.close()
if md5ChkSum(_src) == md5ChkSum(_dst):
return "Copy: SUCCESSFUL"
return "Copy: FAILED"
res = copier(r"/home/cnsiva/6.jpg", r"/home/cnsiva/6_copied.jpg")
if not res:
print "FILE Does not Exists !!!"
else: print res
OUTPUT:
Copy: SUCCESSFUL