Loading a datalog program in a file into pyDatalog - logic-programming

I am trying to use the pyDatalog.load() method to load a small pyDatalog program. For example, I am loading the factorial sample from https://sites.google.com/site/pydatalog/
from pyDatalog import pyDatalog
pyDatalog.create_atoms('factorial, N, F') # gives datalog capability to these words
def run_program():
N = pyDatalog.Variable()
F = pyDatalog.Variable()
file_in = open("sample_datalog_program.dl", 'r')
mc = file_in.read()
print mc
#pyDatalog.program()
def _(): # the function name is ignored
pyDatalog.load(mc)
#pyDatalog.load("""
#+ (factorial[1]==1)
#(factorial[N] == F) <= (N > 1) & (F == N*factorial[N-1])
#""")
print(pyDatalog.ask('factorial[4]==F'))
file_in.close()
pass
if __name__ == "__main__":
run_program()
the file sample_datalog_program.dl contains the following:
"""
+ (factorial[1]==1)
(factorial[N] == F) <= (N > 1) & (F == N*factorial[N-1])
"""
What am I doing wrong? When I replace the line pyDatalog.load(mc) by the next 4 commented lines it works fine.
The error I get is:
/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/bin/python2.7 run_datalog_program.py
pyDatalog version 0.12.0
Traceback (most recent call last):
File "run_datalog_program.py", line 25, in <module>
run_program()
File "run_datalog_program.py", line 11, in run_program
#pyDatalog.program()
File "/usr/local/lib/python2.7/site-packages/pyDatalog/pyParser.py", line 191, in add_program
load(source_code, newglobals, defined, function=func_name)
File "/usr/local/lib/python2.7/site-packages/pyDatalog/pyParser.py", line 154, in load
six.exec_(code, newglobals)
File "/usr/local/lib/python2.7/site-packages/six.py", line 308, in exec_
exec("""exec code in globs, locs""")
File "<string>", line 1, in <module>
File "_", line 1, in <module>
File "/usr/local/lib/python2.7/site-packages/pyDatalog/pyDatalog.py", line 115, in load
return pyParser.load(code)
File "/usr/local/lib/python2.7/site-packages/pyDatalog/pyParser.py", line 133, in load
spaces = r.match(line).group()
TypeError: expected string or buffer
"""
+ (factorial[1]==1)
(factorial[N] == F) <= (N > 1) & (F == N*factorial[N-1])
"""
Process finished with exit code 1
Thank you!

You should not mix the pyDatalog.load() API and the in-line API. See Dynamic Datalog statements (at bottom of page)
Here is how you could write your program:
from pyDatalog import pyDatalog
def run_program():
file_in = open("sample_datalog_program.dl", 'r')
mc = file_in.read()
print mc
pyDatalog.load(mc)
file_in.close()
print(pyDatalog.ask('factorial[3]==N'))
if __name__ == "__main__":
run_program()
The code in sample_datalog_program.dl should be (without triple quotes):
+ (factorial[1]==1)
(factorial[N] == F) <= (N > 1) & (F == N*factorial[N-1])

Related

UnicodeDecodeError with processing a csv

Suddently a "UnicodeDecodeError" arises in a code of mine which worked yesterday.
File
"D:\Anaconda\lib\site-packages\IPython\core\interactiveshell.py", line
3284, in run_code
self.showtraceback(running_compiled_code=True)
File
"D:\Anaconda\lib\site-packages\IPython\core\interactiveshell.py", line
2021, in showtraceback
value, tb, tb_offset=tb_offset)
File "D:\Anaconda\lib\site-packages\IPython\core\ultratb.py", line
1379, in structured_traceback
self, etype, value, tb, tb_offset, number_of_lines_of_context)
File "D:\Anaconda\lib\site-packages\IPython\core\ultratb.py", line
1291, in structured_traceback
elist = self._extract_tb(tb)
File "D:\Anaconda\lib\site-packages\IPython\core\ultratb.py", line
1272, in _extract_tb
return traceback.extract_tb(tb)
File "D:\Anaconda\lib\traceback.py", line 72, in extract_tb
return StackSummary.extract(walk_tb(tb), limit=limit)
File "D:\Anaconda\lib\traceback.py", line 364, in extract
f.line
File "D:\Anaconda\lib\traceback.py", line 286, in line
self._line = linecache.getline(self.filename, self.lineno).strip()
File "D:\Anaconda\lib\linecache.py", line 16, in getline
lines = getlines(filename, module_globals)
File "D:\Anaconda\lib\linecache.py", line 47, in getlines
return updatecache(filename, module_globals)
File "D:\Anaconda\lib\linecache.py", line 137, in updatecache
lines = fp.readlines()
File "D:\Anaconda\lib\codecs.py", line 321, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf6 in position
2441: invalid start byte
import csv
import logging
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)
dateiname_TDM = "./TDM_example_small.csv"
dateiname_corpus = "./Topic_Modeling/Input_Data/corpus.mm"
dateiname_dictionary = "./Topic_Modeling/Input_Data/dictionary.dict"
ids = {}
corpus = []
with open(dateiname_TDM, newline='') as csvfile:
reader = csv.reader(csvfile, delimiter=';', quotechar='|')
documente = next(reader, None)[1:]
for rownumber, row in enumerate(reader):
for index, field in enumerate(row):
if index == 0:
if rownumber > 0:
ids[rownumber-1] = field
else:
if rownumber == 0:
corpus.append([])
else:
try:
if field > 0:
corpus[index-1].append((rownumber-1, int(field)))
except ValueError:
corpus[index-1].append((rownumber-1, 0))
Without seeing the what's at position 2441 I'm not entirely sure, but it is probably one of the following:
A special, non-ascii/extended ascii character, in which case do the_string.encode("UTF-8") or when opening do encoding = "UTF-8" in the open function
You have \u or \U somewhere and this makes the next characters read as part of a Unicode sequence so do repr(the_string) to add backslashes to nullify backslashes after (Probably not this one)
You are reading a bytes object not a str object. Try opening it with r+b (read & write, bytes) in the open function
I've more or less thrown spaghetti at a wall but I hope this helps!

How to make an array of words? Python

Good afternoon, I wanted to know how to make an array of letters, for example I have a list that is the alphabet:
alphabet=[chr(i) for i in range(ord('a'),ord('z')+1)]
And I want to do with this list a matrix of 5 * 5, which I have done, it has been this, but python gives me error
dimension=5
A= np.zeros((dimension,dimension))
n=0
for j in range(dimension):
for i in range(dimension):
A[i][j] = alphabet[n]
n=n+1
The error that gives me is this:
Traceback (most recent call last):
File "Cuestionario 4.py", line 217, in <module>
A[i][j] = alphabet[n]
ValueError: could not convert string to float: 'a'
Thank you for your attention
replace this
A= np.zeros((dimension,dimension))
with
A= np.chararray((dimension, dimension))
Full code should look like this
dimension=5
A= np.chararray((dimension,dimension))
n=0
for j in range(dimension):
for i in range(dimension):
A[i][j] = alphabet[n]
n=n+1

'enumerate' function breaks loop? python3

def main():
x = open("textfile.txt", "r")
#o = enumerate(x.readlines())
for i in x:
print(i, end="")
x.close
if __name__ == "__main__": main()
If I uncomment the 'o' object this script will not run.
Could someone please tell me why that is?
:python3.3
you mean you don't get output, right?
that's because x.readlines() isn't a generator - it actually reads all the data out of x. and then gives that to o, wrapped with an enumerator.
so when you do
for i in x:
there's no more data to be read - nothing to do.
you could do:
for i,text in o:
print '%d: %s'%(i, text)
and that would work...

Ways to validate converted code from FORTRAN to C

I have converted around 90+ fortran files into C files using a tool and I need to validate that the conversion is good or not.
Can you give me some ideas on how best to ensure that the functionality has been preserved through the translation?
You need verification tests that exercise those fortran functions. Then you run those tests against the c code.
You can use unit test technology/methodology. In fact I can't see how else you would prove that the conversion is correct.
In lots of unit test methodologies you would write the tests in the same language as the code, but in this case I recommend very very strongly to pick one language and one code base to exercise both sets of functions. Also don't worry about be trying to create pure unit tests rather use the techniques to give you coverage of all the use that the fortran code was supposed to handle.
Use unit tests.
First write your unit tests on the Fortran code and check whether they all run correctly, then rewrite them in C and run those.
The problem in this approach is that you also need to rewrite your unit test, which you normally don't do when refactoring code (except for API changes). This means that you might end up debugging your ported unit testing code as well, beside the actual code.
Therefore, it might be better to write testing code that contains minimal logic and only write the results of the functions to a file. Then you can rewrite this minimal testing code in C, generate the same files and compare the files.
Here is what I did for a "similar" task (comparing fortran 90 to fortran 90 + OpenACC GPU accelerated code):
Analyze what's the output of each Fortran module.
Write these output arrays to .dat files.
Copy the .dat files into a reference folder.
Write the output of the converted modules to files (either CSV or binary). Use the same filename for convenience.
Make a python script that compares the two versions.
I used convenience functions like these in fortran (analogous for 1D, 2D case):
subroutine write3DToFile(path, array, n1, n2, n3)
use pp_vardef
use pp_service, only: find_new_mt
implicit none
!input arguments
real(kind = r_size), intent(in) :: array(n1,n2,n3)
character(len=*), intent(in) :: path
integer(4) :: n1
integer(4) :: n2
integer(4) :: n3
!temporary
integer(4) :: imt
call find_new_mt(imt)
open(imt, file = path, form = 'unformatted', status = 'replace')
write(imt) array
close(imt)
end subroutine write3DToFile
In python I used the following script for reading binary Fortran data and comparing it. Note: Since you want to convert to C you would have to adapt it such that you can read the data produced by C instead of Fortran.
from optparse import OptionParser
import struct
import sys
import math
def unpackNextRecord(file, readEndianFormat, numOfBytesPerValue):
header = file.read(4)
if (len(header) != 4):
#we have reached the end of the file
return None
headerFormat = '%si' %(readEndianFormat)
headerUnpacked = struct.unpack(headerFormat, header)
recordByteLength = headerUnpacked[0]
if (recordByteLength % numOfBytesPerValue != 0):
raise Exception, "Odd record length."
return None
recordLength = recordByteLength / numOfBytesPerValue
data = file.read(recordByteLength)
if (len(data) != recordByteLength):
raise Exception, "Could not read %i bytes as expected. Only %i bytes read." %(recordByteLength, len(data))
return None
trailer = file.read(4)
if (len(trailer) != 4):
raise Exception, "Could not read trailer."
return None
trailerUnpacked = struct.unpack(headerFormat, trailer)
redundantRecordLength = trailerUnpacked[0]
if (recordByteLength != redundantRecordLength):
raise Exception, "Header and trailer do not match."
return None
dataFormat = '%s%i%s' %(readEndianFormat, recordLength, typeSpecifier)
return struct.unpack(dataFormat, data)
def rootMeanSquareDeviation(tup, tupRef):
err = 0.0
i = 0
for val in tup:
err = err + (val - tupRef[i])**2
i = i + 1
return math.sqrt(err)
##################### MAIN ##############################
#get all program arguments
parser = OptionParser()
parser.add_option("-f", "--file", dest="inFile",
help="read from FILE", metavar="FILE", default="in.dat")
parser.add_option("--reference", dest="refFile",
help="reference FILE", metavar="FILE", default="ref.dat")
parser.add_option("-b", "--bytesPerValue", dest="bytes", default="4")
parser.add_option("-r", "--readEndian", dest="readEndian", default="big")
parser.add_option("-v", action="store_true", dest="verbose")
(options, args) = parser.parse_args()
numOfBytesPerValue = int(options.bytes)
if (numOfBytesPerValue != 4 and numOfBytesPerValue != 8):
print "Unsupported number of bytes per value specified."
sys.exit()
typeSpecifier = 'f'
if (numOfBytesPerValue == 8):
typeSpecifier = 'd'
readEndianFormat = '>'
if (options.readEndian == "little"):
readEndianFormat = '<'
inFile = None
refFile = None
try:
#prepare files
inFile = open(str(options.inFile),'r')
refFile = open(str(options.refFile),'r')
i = 0
while True:
passedStr = "pass"
i = i + 1
unpackedRef = None
try:
unpackedRef = unpackNextRecord(refFile, readEndianFormat, numOfBytesPerValue)
except(Exception), e:
print "Error reading record %i from %s: %s" %(i, str(options.refFile), e)
sys.exit()
if (unpackedRef == None):
break;
unpacked = None
try:
unpacked = unpackNextRecord(inFile, readEndianFormat, numOfBytesPerValue)
except(Exception), e:
print "Error reading record %i from %s: %s" %(i, str(options.inFile), e)
sys.exit()
if (unpacked == None):
print "Error in %s: Record expected, could not load record it" %(str(options.inFile))
sys.exit()
if (len(unpacked) != len(unpackedRef)):
print "Error in %s: Record %i does not have same length as reference" %(str(options.inFile), i)
sys.exit()
#analyse unpacked data
err = rootMeanSquareDeviation(unpacked, unpackedRef)
if (abs(err) > 1E-08):
passedStr = "FAIL <-------"
print "%s, record %i: Mean square error: %e; %s" %(options.inFile, i, err, passedStr)
if (options.verbose):
print unpacked
except(Exception), e:
print "Error: %s" %(e)
finally:
#cleanup
if inFile != None:
inFile.close()
if refFile != None:
refFile.close()

Trying to store Utf-8 data in datastore getting UnicodeEncodeError

Trying to store utf-8 into datastore and getting error :
Traceback (most recent call last):
File "/sinfo/google_appengine/google/appengine/ext/webapp/__init__.py", line 511, in __call__
handler.get(*groups)
File "/sinfo/siteinfo/siteinfo.py", line 1911, in get
seoEntity.put()
File "/sinfo/google_appengine/google/appengine/ext/db/__init__.py", line 833, in put
return datastore.Put(self._entity, rpc=rpc)
File "/sinfo/google_appengine/google/appengine/api/datastore.py", line 275, in Put
req.entity_list().extend([e._ToPb() for e in entities])
File "/sinfo/google_appengine/google/appengine/api/datastore.py", line 680, in _ToPb
properties = datastore_types.ToPropertyPb(name, values)
File "/sinfo/google_appengine/google/appengine/api/datastore_types.py", line 1499, in ToPropertyPb
pbvalue = pack_prop(name, v, pb.mutable_value())
File "/sinfo/google_appengine/google/appengine/api/datastore_types.py", line 1322, in PackString
pbvalue.set_stringvalue(unicode(value).encode('utf-8'))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 1: ordinal not in range(128)
How do i solve this? The data is already utf-8 encoded and when I enter it into the datastore it uses the ascii codec and fails?
I use following helper in my projects
def force_utf8(string):
if type(string) == str:
return string
return string.encode('utf-8')
Use it to escape all your unicode data before passing to GAE. Also you can find useful the following snippet:
def force_unicode(string):
if type(string) == unicode:
return string
return string.decode('utf-8')

Resources