how to call a function before its defination

This is the place for queries that don't fit in any of the other categories.

how to call a function before its defination

Postby vishwas » Mon Mar 25, 2013 5:28 am

hello everybody here in my code i am facing some problem in this ....after clicking the button it calls the function new_proj after that a the end it calls jojo() function but it this jojo function this is a variable y which is taking the return of value of the new proj...i am not able to solve it....so pls sort it with an ease....as i am kind of begginer...

Code: Select all
from Tkinter import *
import os
import tkMessageBox
import shutil
import re

prr=Tk()
prr.title("RCS TOOL")
prr.geometry('700x700+700+700')
var = StringVar()
var.set("Project Name")
label=Label( prr,textvariable=var,height=3)
label.config(font=('arial',11))
label.place(x=100,y=10)

name_entry=Entry(prr)
name_entry.place(x=200,y=30)


def new_proj():
    #newpath = None
    x= (r"C:\Documents and Settings\KSR\Desktop\%s")%name_entry.get()
    if not os.path.exists(x): os.makedirs(x)

    shutil.copy2('C:\Documents and Settings\KSR\Desktop\myproj\soth.tcl',x)

    x=x+"\soth.tcl"
    print x
    return x
y=new_proj()
jojo()

def jojo(mat,dic = {'virtex4':'virtex5'}):
    return dic[mat.group()]
pat = re.compile('virtex4')
print y
with open(y,'rb+')as f:
     content= f.read()
     f.seek(0,0)
     f.write(pat.sub(jojo,content))
     f.truncate()
     
b1= Button(prr,text="create new project",padx=10,pady=3,command=new_proj)
b1.place(x=330,y=25)

vishwas
 
Posts: 14
Joined: Thu Mar 14, 2013 9:57 am

Re: how to call a function before its defination

Postby stranac » Mon Mar 25, 2013 10:51 am

You can't call a function before it's defined.
But simply moving the definition before the call could solve that particular problem.

Having said that, what you're doing is really weird, for a few reasons:
  • you don't need to use regex to do simple substitutions like this. str.replace() should be powerful enough
  • opening a file in 'rb+' mode is rarely a good thing to do. It is better to open the original file for reading, and the new one for writing
  • everything you want to happen when the button is clicked should be inside of the new_proj() function(so don't return x, create the new file inside the function)
  • that jojo() call makes no sense
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1097
Joined: Thu Feb 07, 2013 3:42 pm

Re: how to call a function before its defination

Postby vishwas » Mon Mar 25, 2013 11:35 am

sir basically i tried str.replace thing but its not working ....basically i want to edit a .tcl file ....not a txt file ....def new_proj(): is creating a folder and copying the original file ...and will edit it (as if it is not the whole program but it is one portion init)and jojo() function will handle the edit portion.....and you said to create a function within function i am srry i cant able to do that...can you tell me where to make the edits.... ......for more understanding i am also giving the file code in which i hav to make the edits....it is .tcl file which controlls the whole xilinx ise....and my gui app will control evrything by taking entries from the user....in this x=x+"\soth.tcl"....is i am doing to point the address where i want to make edits...thanks a lot in advance

Code: Select all

set vishwas [lindex $argv 0]

if { [file exists $vishwas.xise] != 1} then {
   project new $vishwas
   puts "creating ise project :$vishwas "
}
project open $vishwas

project set family "virtex4"
project set device "xc4vfx60"
project set package "ff1152"
project set speed "-11"
project set top_level_module_type "HDL"
project set synthesis_tool "XST (VHDL/Verilog)"
##project set simulator "ISim (VHDL/Verilog)"
project set "Preferred Language" "Verilog"
project set "Enable Message Filtering" "false"

xfile add *.vhd

#project set top "Behavioral" "add"

process run "Implement Design"

project close

vishwas
 
Posts: 14
Joined: Thu Mar 14, 2013 9:57 am

Re: how to call a function before its defination

Postby ichabod801 » Mon Mar 25, 2013 12:10 pm

That is a text file. Just because the extension is tcl doesn't mean the content of the file isn't stored as text. It is also a small text file. If all you want to do is replace 'vitrix4' with 'vitrix5', I would just read() the file into a variable, use variable.replace('vitrix4', 'vitrix5'), and then write the variable back to the file.
Craig "Ichabod" O'Brien
Minimalist, buddhist, theist, and programmer
Current languages: Python, SAS, and C++
Previous serious languages: R, Java, VBA, Lisp, HyperTalk, BASIC
ichabod801
 
Posts: 84
Joined: Sat Feb 09, 2013 12:54 pm
Location: Outside Washington DC

Re: how to call a function before its defination

Postby vishwas » Mon Mar 25, 2013 12:17 pm

sir i have tried that one but its not working ....i know it is also a text file but its not changing it.....otherwise why i would do this coding....can you help me by just editing the program i hav posted than it will be more easy to undertand it...pls :cry:
vishwas
 
Posts: 14
Joined: Thu Mar 14, 2013 9:57 am

Re: how to call a function before its defination

Postby ichabod801 » Mon Mar 25, 2013 12:55 pm

No, you help us help you by giving use the error you are getting. Try it the way I suggested, its much simpler. If it doesn't work, show us the code you wrote and show us the error you got or exactly how the output was wrong.
Craig "Ichabod" O'Brien
Minimalist, buddhist, theist, and programmer
Current languages: Python, SAS, and C++
Previous serious languages: R, Java, VBA, Lisp, HyperTalk, BASIC
ichabod801
 
Posts: 84
Joined: Sat Feb 09, 2013 12:54 pm
Location: Outside Washington DC


Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot] and 2 guests