input output in prolog - list

i want to read characters from a file in prolog and place them in a list.
could some one help me out with it?
thanks

SWI-Prolog offers read_file_to_codes/3. Usage example:
?- read_file_to_codes('/etc/passwd', Codes, []).
Codes = [114, 111, 111, 116, 58, 120, 58, 48, 58|...].

Related

How to solve error Expected singleton: purchase.order.line (57, 58, 59, 60, 61, 62, 63, 64)

I'm using odoo version 9 and I've created a module to customize the reports of purchase order. Among the fields that I want displayed in the reports is the supplier reference for article but when I add the code that displays this field <span> <t t-esc="', '.join([str(x.product_code) for x in o.order_line.product_id.product_tmpl_id.seller_ids])"/>
but it displays an error when I want to start printing the report
QWebException: "Expected singleton: purchase.order.line(57, 58, 59, 60, 61, 62, 63, 64)" while evaluating
"', '.join([str(x.product_code) for x in o.order_line.product_id.product_tmpl_id.seller_ids])"
PS: I don't change anything in the module purchase.
I don't know how to fix this problem any idea for help please ?
It is because your purchase order got several orderlines and you are hoping that the order will have only one orderline.
o.orderline.product_id.product_tmpl_id.seller_ids
will work only if there is one orderline otherwise you have loop through each orderline. Here o.orderline will have multiple orderlines and you can get product_id from multiple orderline. If you try o.orderline[0].product_id.product_tmpl_id.seller_ids it will work but will get only first orderline details. Inorder to get all the orderline details you need to loop through it.
There are more than one seller ids found . That's why you are getting number of ids here .i.e. purchase.order.line(57, 58, 59, 60, 61, 62, 63, 64). You have to select one of the id among them . To see the result just try this :
o.order_line[0].product_id.product_tmpl_id.seller_ids
If you want to show all of these seller ids on report apply for loop in to the xml.

Remove duplicates based on specific column

I do have a big text file in the following format on my Linux CentOS 7.
430004, 331108, 075, 11, 19, Chunsuttiwat Nattika
431272, 331108, 075, 11, 19, Chunsuttiwat Nattika
435979, 335086, 803, 6, 19, ANNI BRENDA
436143, 335151, 545, 4, 23, Agrawal Abhishek
436723, 335387, 386, 2, 19, Bhati Naintara
438141, 325426, 145, 11, 19, Teh Joshua
I would like to remove duplicate lines including the origin if it matches on second column.
Expected Output:
435979, 335086, 803, 6, 19, ANNI BRENDA
436143, 335151, 545, 4, 23, Agrawal Abhishek
436723, 335387, 386, 2, 19, Bhati Naintara
438141, 325426, 145, 11, 19, Teh Joshua
Update:
sort + uniq + awk pipeline:
sort -k2,2 file | uniq -f1 -c -w7 | awk '$1==1{ sub(/[[:space:]]*[0-9]+[[:space:]]*/,"",$0); print}'
sort -k2 -n file - sort the file by the 2nd field numerically
uniq -f1 -c - output lines with the number of occurrences (-f1 - skips the 1st field in the file)
awk '$1==1{ $1=""; print}' - print the lines which occur only once ($1==1 - check for count value from uniq command)
Using awk
#Input
awk '{R[i++]=$0;f=$1;$1="";N[$0]++;}
END{for(i=0;i<NR;i++){
temp=R[i];sub(/^[[:digit:]]*\, /,"",R[i]);
if(N[" "R[i]]==1){print temp}}}' filename
#Output
435979, 335086, 803, 6, 19, ANNI BRENDA
436143, 335151, 545, 4, 23, Agrawal Abhishek
436723, 335387, 386, 2, 19, Bhati Naintara
438141, 325426, 145, 11, 19, Teh Joshua
This is all you need:
$ awk 'NR==FNR{c[$2]++;next} c[$2]==1' file file
435979, 335086, 803, 6, 19, ANNI BRENDA
436143, 335151, 545, 4, 23, Agrawal Abhishek
436723, 335387, 386, 2, 19, Bhati Naintara
438141, 325426, 145, 11, 19, Teh Joshua

Django order by queryset by requests values (id__in)

I query a Django table by a list of IDs
hclistofcases = testcase.objects.filter(id__in="[182, 180, 184, 179, 178, 181, 183"))
That words an returns a queryset, however the queryset is not in the list order (i.e. record 182 first and 183 last). Is there a way to ensure that the quesryset is returned in the list order? I am currently using sqlite as the database
Any hep would be appreciated
Thanks
Grant
empty = testcase.objects.none()
_ = []
for i in [182, 180, 184, 179, 178, 181, 183]:
_.append(testcase.fiter(id=i))
return empty.union(*_)
According to the docs (https://docs.djangoproject.com/en/1.10/ref/models/querysets/#order-by) you can append .order_by(id) to get ascending order.

checking if two lists are equal in Maple

I've got the following lists :
list1:=[1, 5, 14, 30, 55, 91, 140, 204, 285, 385, 506, 650, 819, 1015,
1240, 1496, 1785, 2109, 2470, 2870]
list2:=[1, 5, 14, 30, 55, 91, 140, 204, 285, 385, 506, 650, 819, 1015,
1240, 1496, 1785, 2109, 2470, 2870]
each generated by a procedure I defined. I need to verify that they are equal, which is the case. However, when I tried to use the evalb function as well as a flag that I was updating during a loop, in both cases, I got 'false' as the answer along with the error message:
"error, final value in a for loop must be numeric or a character"
What I am doing wrong?
Maple will automatically resolve multiple copies of lists with identical entries to the same object. So to test equality, you don't even need to traverse the lists programmatically. You can just do:
evalb(list1=list2);
If however you'd like to do a more sophisticated comparison, you can use the verify command. For example, this will verify that the first list has the second list as a sublist:
verify([1, 2, 3, 4, 5], [2, 3, 4], superlist);
Calling verify with no second argument is equivalent to the first evalb test, e.g.:
verify(list1, list2);

I want to append a list of numbers to another list; using another list for the range of appending

Just to be clear there are 3 different lists involved. la is a list of integers and the posfinList is a list of numbers where each integer from la should be appended until it reaches the first number in the list then moves to the next in the posfinList. The numbers in posfinList will change everytime I use different data.
posfinList=[83, 81, 83, 82, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 86, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 85, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83]
la is a list of 6,932 integers
rosen = 0
blos=0
lafin = []
lafins=[]
while rosen<len(la):
while rosen<(posfinList[blos]):
lafin.append(la[rosen])
if rosen >=(posfinList[blos]):
lafins.append(lafin)
blos+=1
rosen+=1
print lafins
Well it's not entirely clear what you're trying to do. Could you clarify please? Or even better, what output would you expect for this input?:
la=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
posfinList=[2, 4, 6]
(StackOverflow people: Yes, I know that should be a comment, but I don't have the reputation for it yet!)
Even without a clarification, however, I can confirm that you have an infinite loop. If posfinList[0]<6932, as it is in your example, then the inner loop terminates with rosen=posfinList[0] and rosen has no way to increase or reset. The outer loop continues and never terminates, causing the infinite loop you're seeing.
I'm assuming you want to chop up the list la into several pieces, so that the n:th piece has a length equal to posfinList[n] for all n. Please confirm/deny.
The following code is based on that assumption, see if it works the way you want:
lafins = []
for p in postfinList:
lafins.append( la[rosen:rosen+p] )
rosen += p
(If la[rosen:rosen+p] is unfamiliar notation to you, you can read about it (it's called 'slice' notation) in the Python Documentation. You'll find the relevant bits a little way down under the 'Strings' header.)
I have mostly used the same variable names as you, though they're not all that descriptive. Taking the time to choose variable names that actually document what the variables do generally pays off handsomely when testing and debugging, not to mention helps to make sure that you yourself know what those variables are meant to do.
As for your original code, there are at least three problems:
the stuff inside if rosen >=(posfinList[blos]) will never execute. The if itself is contained in while rosen<(posfinList[blos]), which actually makes sure that rosen>=(postfinList[blos]) will never be true inside it.
when while rosen<(posfinList[blos]) exits (unless by coincidence rosen>=len(la)), while rosen<len(la) will enter an infinite loop: rosen will always be less than len(la). Neither of those values will ever change again, so the while condition will always be true.
this one is difficult to explain, so I'm just going to say this: for your loop to work, in the example case you gave, postfinList should be [83, 164, 247, 329, ...] instead of [83, 81, 83, 82, ...], that is, it should be [83, 83+81, 83+81+83, 83+81+83+82, ...]. Think about why that is.