bharatk 0 Light Poster

My version was this, I kind of like the simplicity especially if you utilice gprolog min_list, it is short and simple

corresponding(A, B, [A|_], [B|_]).
corresponding(A, B, [_|C], [_|D]) :- corresponding(A, B, C, D).
min2(A, B, C, D) :- min_list(D, B), corresponding(A, B, C, D).

For min_list implementation, as I posted to StackOverflow (don't double post other time), my smallest with cuts as yours was copy from internet:

% length 1 is base case, all  minimums are equal, so cuts
smallest([A], A).
% shorten by 1 by dropping bigger or equal
smallest([A, B|C], D) :-
	A > B,
	smallest([B|C], D), !.
% cut means this is else branch and need not condition
smallest([A, _|C], D) :-
	smallest([A|C], D).

This is elegant & efficient and as you said, the solution I had got didn't find all the pairs.


It does take a while to get used to Prolog's way of doing stuff coming from the imperative world. And sorry for the double posting. I thought I could have more views & discussions by doing so.

bharatk 0 Light Poster

Did you solve it? With smallest or gprolog min_list, the min2 rule is only one line and very obvious logical statement.

I actually found a way to solve it & get exactly what I wanted.

Here I check if B is H2 then A is H1. Here is the code:

min2(A,B,[A],[B]).  
min2(A,B,[X|T1],[Y|T2]) :- smallest(W,[Y|T2]), B is W, min2(A,B,T1,T2), !. 
min2(A,B,[H1|T1],[H2|T2]):- B=:=H2 -> A = H1 ; min2(A,B,T1,T2).
bharatk 0 Light Poster

I am trying to find the index position of the minimum element in a list and print the element at the corresponding index position in another list.

For example:

?- min2(X,Y,[a,b,c],[5,3,7]).
    X= b
    y= 3

Code:

min2(A,B,[A|_],[B|_]).
    min2(A,B,[X|T1],[Y|T2]) :- smallest(W,[Y|T2]),  % using a predicate to find the min element in teh list
                               B is W,              % setting B to the result of above(i.e the min element)
                               min2(A,B,T1,T2).     % looking up position corresponding to min element in list1

the predicate for finding the min element in the list is:

smallest(Head, [Head]).
    smallest(Element, [Head|Tail]) :- smallest(E, Tail), Head =< E, Element is Head.
    smallest(Element, [Head|Tail]) :- smallest(E, Tail), E < Head , Element is E.

The result I am getting is :

X = a,
    Y = 5 ;
    X = b,
    Y = 3 ;
    false.

It somehow picks the first element also. My base case could be wrong? I tried altering the base case to min2(A,B,[A|_],[B|_]). and it breaks.

Please show me where I am going wrong.

Thanks.

bharatk 0 Light Poster

I did a few changes & achieved what I wanted.

This is the changes I made to CPUSh.py:

print "CPU Scheduling Algorithm Simulation\n"
print "Which Algorithm do you want to run? "
print "1.FCFS\n2.SJF\n3.Priority Scheduling\n4.Round Robin Scheduling\n"
choice=input()

if choice==1:
	from files.fcfs import FCFS
	fc=FCFS()
	fc
elif choice==3:
	from files.priority import Priori
	pr=Priori()
	pr

elif choice==2:
	import files.sjf
	
elif choice==4:
	from files.rr import RRobin
	ro=RRobin()
	ro
else:
	print "Wrong choice"

raw_input()

But is this the right way to create modular code?

bharatk 0 Light Poster

Hello

I have 4 .py files along with __init__.py in a folder.
I want to create a file which would act as a menu and load classes from the files in that folder.

I have that file outside the folder.

The contents of that file:

from files import *

print "CPU Scheduling Algorithm Simulation\n"
print "Which Algorithm do you want to run? "
print "1.FCFS\n2.SJF\n3.Priority Scheduling\n4.Round Robin Scheduling\n"
choice=input()

if choice==1:
	fc=FCFS()
	fc
elif choice==3:
	pr=Priori
	pr
elif choice==2:
	import sjf #as this file does not have a class
elif choice==4:
	ro=RRobin()
	ro
else:
	print "Wrong choice"
raw_input()

So the folder structure is like this:

CPUSh.py
files [ this is a folder]
|
->__init__.py
->fcfs.py
->priority.py
-> sjf.py
->rr.py

each .py files mentioned above has all the code and functions inside single class(which I am instantiating in the CPUSh.py file)

So how do I make it in such a way that when I enter a particular choice the corresponding file executes?

Thank you

bharatk 0 Light Poster

Thank you tonyjv!!
It worked perfectly.
I guess I should maximize using Python idioms where ever possible...makes it simpler.

bharatk 0 Light Poster

I have 2 lists:
a=[0,1,5,7]
b=[2,10,6,3]

I need to get this list :

c=[(b[0]+a[0]),(b[0]+b[1]),(b[0]+b[1]+b[2]),(b[0]+b[1]+b[2]+b[3])]

I tried doing this using a for loop:

c=[(b[0]+a[0])]
for i in range(0,(len(b)-1)):
    for p in range(i+1,len(b)):
        if i==0:
            c.append((b[i]+b[p]))
            
        else:
            c.append((c[i]+b[p]))

I am supposed to get [2,12,18,21] but I am getting [2, 12, 12, 12, 18, 15, 15]

Where am I going wrong in the for loops?

Thank you :)

bharatk 0 Light Poster

I have a form in which I can input text through text boxes.
How do I make these data go into the db on clicking submit.

this is the code of the form in the template. What do I give in form action?

<form method="post" action="[B]what do I put here??[/B]">
<p>
Title:<input type="text" name="title"/>
</p>
<p>
Name:<input type="text" name="name"/>
</p>
<p>
Phone:<input type="text" name="phone"/>
</p>
<p>
Email:<input type="text" name="email"/>
</p>
<p>
<textarea name="description" rows=20 cols=60>
</textarea><br>
</p>
<input type="submit" value="Submit"/>
</form>

I have a function in the views.py for saving the data in the page. But I dont know how to impliment it properly:

def save_page(request):
   title = request.POST["title"]
   name = request.POST["name"]
   phone = request.POST["phone"]
   email = request.POST["email"]
   description = request.POST["description"]

Now how do I send these into the db?

I went through forms in django docs but couldnt understand anything.

Thank you.

bharatk 0 Light Poster

when I perform the above I get the output printed on the webpage as

061.1919444389641(julianday(resolutions.created_at)-julianday(tickets.created_at))*2461.1919444389641

only 61.1919444389641 is supposed to be printed but the query statement is also getting printed.

bharatk 0 Light Poster

Hello

I have a rails application where I have to subtract 2 time stamps and display the result on the webpage

I have used the following sql statement and I am getting the results.

select (julianday(resolutions.created_at)-julianday(tickets.created_at))*24 from tickets,resolutions

the value of tickets.created_at is 2010-04-05 18:59:02 which is a time stamp and value of resolutions.created_at is 2010-04-08 08:10:33

Now where do I put this sql so that I can be seen in my webpage.
I tired it in the views page with the following but nothing showed up:

<% @sql = "select (julianday(resolutions.created_at)-julianday(tickets.created_at))*24 from tickets,resolutions" %>
<% @t=(ActiveRecord::Base.connection.execute(@sql)) %>

So how do I display it on my webpage?

Thank you.

bharatk 0 Light Poster

Thank you, I got it now
I can see you have used .lift method .. I tend to overlook minute details like this while coding... :D

And I didn't import with a namespace as this code is a part of a bigger code and I'd to make changes everywhere then..

bharatk 0 Light Poster

I have function in my program calls a class which opens a new window

class TopLevel3:
     def __init__(self):
        self.root2 = Toplevel()
        self.canvas3 = Canvas(self.root2, width=400, height=200)
        self.canvas3.pack()
        self.root2.title("ImageDB")
        r = 0
        for c in range(6):
            Label(relief=RIDGE,  width=55).grid(row=r, column=0)
            Entry(relief=SUNKEN, width=20).grid(row=r, column=1)
            Button(text='View Image').grid(row=r, column=2)
            Button(text='Update Tag').grid(row=r, column=3)
            r = r+1
        self.root2.mainloop()

This is called by a function in another class:

def img_db(self):
            TopLevel3()

But instead of the labels & buttons appearing in the new child window they appear in the parent window itself.
(Image attached)
Where am I going wrong?

bharatk 0 Light Poster

I tried doing a similar thing.

A function in my program calls a class which opens a new window

class TopLevel3:
     def __init__(self):
        self.root2 = Toplevel()
        self.canvas3 = Canvas(self.root2, width=400, height=200)
        self.canvas3.pack()
        self.root2.title("ImageDB")
        r = 0
        for c in range(6):
            Label(relief=RIDGE,  width=55).grid(row=r, column=0)
            Entry(relief=SUNKEN, width=20).grid(row=r, column=1)
            Button(text='View Image').grid(row=r, column=2)
            Button(text='Update Tag').grid(row=r, column=3)
            r = r+1
        self.root2.mainloop()

This is called by a function in another class:

def img_db(self):
            TopLevel3()

But instead of the labels & buttons appearing in the new child window they appear in the parent window itself.
(Image attached)
Where am I going wrong?

Editor's note:
Please do not hijack an old solved thread for your question. Start your own thread. Many people that could help you will not look in a solved thread.

bharatk 0 Light Poster

Thank you yet again!!!
That was really clear & helpful. :)

bharatk 0 Light Poster

Thank you vegaseat, I created a PIL as u told and it works.

So to avoid namespace issues I should be doing something like
import Tkinter as tkin
instead of from Tkinter import *

bharatk 0 Light Poster

How do I create the namespace?

I use some other name instead of self.image?

like
pic = ImageTk.PhotoImage(Image.open(filename))
instead of self.image= ImageTk.PhotoImage(Image.open(filename))

and use pic everywhere instead of using self.image?
I tried out this but I'm getting the same error.
How exactly do I create a namespace?

bharatk 0 Light Poster

I thought that looked familiar.

Looks like
def img_zoom(self, image1):
also receives an event from the menu click, so try
def img_zoom(self, image1, event):
where event is simply a dummy.

Hello
It looks like the variables are automatically passed by the 'self' attribute which is passed to img_zoom()

But a new problem has cropped up. When i try to use resize I get the following error:

image_resized = self.image.resize((int(width*scale), int(height*scale)), Image.ANTIALIAS)
AttributeError: PhotoImage instance has no attribute 'resize'

But PhotoImage does have that attribute.

resize doesnt work even if I have it inside the file_open(self).

bharatk 0 Light Poster

Thank you macroasm. I'll check it out.

bharatk 0 Light Poster

forgot to mention: the above code to resize image was got by googling and seems to have been written by vegaseat in some other forum.

bharatk 0 Light Poster

Hello

I'm making an GUI app to open a image and zoom it.

This function is used to open the image & display it:

def file_open(self):
                filename =askopenfilename(filetypes=imageTypes)
                image1 = ImageTk.PhotoImage(Image.open(filename))
                self.canvas.config(height=image1.height(), width=image1.width())
                self.canvas.create_image(2, 2, image=image1, anchor=NW)
                self.image = image1
                self.img_zoom(image1) # passing image1 to img_zoom function

The image is zoomed when I click on zoom in on the menu

viewmenu.add_command(label="Zoom In" ,command=self.img_zoom)

This is the function for zoom:

def img_zoom(self,image1):
            width, height = image1.size
            image1.resize((width, height), type)
            scale = 2
            image_resized = image1.resize((int(width*scale), int(height*scale)), Image.ANTIALIAS)
            image1 = ImageTk.PhotoImage(image=image_resized)
            self.canvas.config(height=image1.height(), width=image1.width())
            self.canvas.create_image(2, 2, image=image1, anchor=NW)
            self.image = image1

But I am getting an error saying

return self.func(*args)
TypeError: img_zoom() takes exactly 2 arguments (1 given)

So how exactly do I make image1 available to the zoom function

thank you.

bharatk 0 Light Poster

vegaseat,
thank you! I used the canvas & I'm able to get what I wanted.
Your quick answers have been a gr8 help for newbies like me.

bharatk 0 Light Poster

I have opened an image using the PhotoImage method of ImageTk
How do I close the image.

This is the scenario is I am using it in. I have 2 buttons, one each to open an image from disk & image from url. When I open either of them the image is displayed. But when I open a new image, the old image remains on the screen & the new image is displayed below it.

How do I close the old image so that only the new image is seen.

Code snippet of for opening the images:

def file_open(self):
                filename =askopenfilename(filetypes=imageTypes)
                image1 = ImageTk.PhotoImage(Image.open(filename))
                panel1 = Label(self, image=image1)
                panel1.pack(side='top', fill='both', expand='yes')
                panel1.image = image1
                
        def openurl(self):
                
                url= tkSimpleDialog.askstring("Open URL","Enter URL")
                file =urllib.urlopen(url)
                im = cStringIO.StringIO(file.read()) 
                img = Image.open(im)
                image1 = ImageTk.PhotoImage(img)
                panel1 = Label(self, image=image1)
                panel1.pack(side='top', fill='both', expand='yes')
                panel1.image = image1
bharatk 0 Light Poster

I am right now using Netbeans IDE & Wing IDE. These appear more friednly than IDLE.
I wanted an IDE where I could just drag and drop buttons & text boxes like in visual basic,instead of coding them.
I guess I'll have to only write the code for the GUI elements.

bharatk 0 Light Poster

IMHO, Tkinter GUI code is too simple to write to warrant a visual approach. It's so much easier to build yourself a few Tkinter code templates and go with that.

BTW, code produced by Visual Tkinter doesn't seem to follow any Python syntax guidelines.

ok.. I wanted an IDE like that coz I am a absolute beginner in python programming and was too used to using Visual Studio.
Guess I'd find coding in tkinter easier as I go.

Thank you for the replies.

bharatk 0 Light Poster

I tired out all their versions listed here: http://code.google.com/p/visualtkinter/downloads/list

But every version gives the same error. I think there is no stable version yet.

I just wanted a IDE in which I could develop like developing in Visual Basic.

Is there anything like that for tkinter?

bharatk 0 Light Poster

and also I am not able to select any items other than button & the arrow:

http://i47.tinypic.com/wlyxxw.jpg

bharatk 0 Light Poster

Hello

I am trying to use Visual Tkinter but it's giving an "Invalid procedure call" error each time I try to place something on the form and the program closes.

Screenshot of what I'm getting:
http://i48.tinypic.com/10zy5br.jpg

I'm not even able to add a simple button to the form & when I try to add it gives this error & closes.

How do I get this to work?

Thank you

bharatk 0 Light Poster

Thank you for ur reply. :)

I turned on the Command Extensions.

But I figured out that I need not use the %CD% at all as long as the batch file & the .py file are going to remain in the same directory.

So I used the following code to get the output as well as get it saved to a text file.

echo off
App1.py
App1.py > App1_OUTPUT.txt
bharatk 0 Light Poster

Hello

I have a .py file. I am creating a .bat file which when clicked runs the python script & saves the output as a text file in the same directory.

I gave this inside the batch file

%CD%\App1.py > %CD%\op.txt

but I am not able to see the output nor the saved text file.

So how do I go about doing this?

Thanks

bharatk 0 Light Poster

U need to study tuples, have a look here:

http://www.tutorialspoint.com/python/python_tuples.htm

I can slice it by giving something like L[1:10], but I want to know if I could do something like sort all values which begins with a & copy it to another list.

bharatk 0 Light Poster

Hello

I have generated a list of words & their occurrences.
Now, how to I filter them based to the starting alphabet. I.e. all words starting with 'a' should be filtered from thos list & put into 1 list along with their occurrences, similarly b,c & d.

This is the list I've generated.It shows the words & the number of occurrences of the words.

[('a', 4574), ('abandon', 3), ('abandoned', 8), ('abandoning', 1), ('abide', 1), ('ability', 2), ('abjectly', 1), ('abjure', 1), ('able', 45), ('abnormal', 4), ('abnormality', 1), ('abolition', 1), ('about', 487), ('above', 27), ('babbled', 1), ('baby', 1), ('back', 131), ('backed', 1), ('backing', 1), ('backs', 1), ('backwards', 2), ('bad', 25), ('bade', 2), ('badly', 10), ('bags', 2), ('baked', 1), ('baking', 1), ('balance', 1), ('balancing', 1), ('balcony', 1), ('bald', 1), ('ball', 3), ('balloon', 3), ('balls', 1), ('bandage', 1), ('banging', 2), ('bank', 9), ('bankrupt', 1), ('banks', 4), ('banquets', 1), ('bantering', 1), ('bar', 3), ('bare', 9), ('bared', 1), ('barefoot', 1), ('bareheaded', 1), ('barely', 1), ('bargain', 1), ('bargained', 1), ('baring', 1), ('barking', 1), ('barracks', 1), ('barred', 1), ('barrel', 1), ('barricade', 1), ('barrier', 1), ('barriers', 2), ('barter', 2), ('base', 6), ('based', 2),  ('cease', 4), ('ceased', 13), ('ceasing', 1), ('ceiling', 1), ('ceilings', 1), ('celebrate', 1), ('cellar', 1), ('censorship', 1), ('censure', 1), ('centered', 1), ('central', 1), ('centre', 1), ('centres', 1), ('century', 2), ('ceremoniously', 1), ('ceremony', 3), ('certain', 66), ('certainly', 63), ('certainty', 2), ('certificate', 7), ('cesspool', 1), ('chain', 3), ('chair', 26), ('chairs', 5), ('chalk', 1), ('challenge', 1), ('challenged', 1), ('challenging', 2), ('chamois', 2), ('champagne', 6), ('champion', 1), …
bharatk 0 Light Poster

Thank you, it works. :)

bharatk 0 Light Poster

Hello

I have created a multidimensional list in python

I used the following code:

r =[(300, 4), (5, 6), (100, 2)]

I tried sorting it in ascending order using r.sort()
and I get [(5, 6), (100, 2), (300, 4)]

I want it to get sorted based on each on the 2nd element instead of the first. That is the result should be [(100,2), (300,4), (5,6)]

How do I go about doing this?

Thank you.