Nine Tails 1 Newbie Poster

There is a large body of literature available for embedding Python code in a C program. The official reference for embedding in C can be found at http://www.python.org/doc/current/c-api/. This may be of interest to you if you are looking to use Python code from a C program.

Nine Tails 1 Newbie Poster

It would appear to be a threading problem. Your calls using the .after function from the Label class are basically opening up new threads. The errors are very strange, which is indicative of thread issues. For instance, you will get the string 'zzzz' from the string 'aaa' with a 25 letter shift and 1000 ms wait time. You really need to write your code so that your scrolling is completed before another call to update_func is made.

My suggestion for this would be to just use for loops instead of .after function calls. This will avoid opening up new threads which can cause all sorts of problems (which appear to be very random and difficult to explain) if you are not very careful with them. If you think that the output of the code to your GUI is moving too quickly then you can include time.sleep calls in your code to make the program wait before showing more output.

Nine Tails 1 Newbie Poster

When you split by commas you leave in the newline characters if there are any. So you the third entry in the first line isn't really '-4500', it is '-4500\n'. In the remainder of your program you must be stripping off the '\n' somehow. If you didn't then the float conversion that you have in your code would give an error.

Nine Tails 1 Newbie Poster

If you want a really simple solution you can loop through and find the line numbers of each of the tags that you are looking for. You then know that the data you are looking for occurs on line numbers in between the tag line numbers.

Nine Tails 1 Newbie Poster

I think that you are asking for help in seeing that your function actually goes through all the steps of binary search on a list. Why don't you create a small test list and place a print statement in your code that displays the sublist that is sent back to the function at each recursive call. Since the list is small you can easily tell what the proper search path should be. If the output is what you should be seeing then it is likely that your code works.

Nine Tails 1 Newbie Poster

You could read everything in as a single string using read() and then use find() on the resulting string to determine the locations of the substrings that you are looking for. Once you know the locations you can easily do editing on any text in between. I don't see why you would necessarily need to know the line numbers in order to accomplish this task. You could read it in line by line and test each line to find the line numbers or you could split the read in string by end line characters and use index() on the resulting list if you can not live without knowing the line numbers.

ning2009 commented: He help me a lot +1
Nine Tails 1 Newbie Poster

Sorry that I forgot to mention this before, but if you choose to outsource work load to matlab you would also want to use the -wait argument in the command line call. Just to be safe!

Nine Tails 1 Newbie Poster

When you enter the word something without quotation marks or apostrophes python thinks that you are referencing a variable. This is why you are getting the error. In order to pass the function the string something you need to call the function as follows:

backwards('something')

Nine Tails 1 Newbie Poster

In python variable types are not important. You should just assign x to your string and strike the line word = "x". This assigns word to the string x not to the string of the value stored in the variable x as you were probably intending. You also want to start with index zero since python starts iterable objects at position zero (this would also cause you to remove the +1 in your loop statement).

Nine Tails 1 Newbie Poster

You could use os.system calls in your wxPython GUI to run Matlab from the command line. You can dump data into a text file (making sure to put an exit command at the end of the file otherwise the Matlab automation window won't close) and then call

os.system('matlab -automation -r myMfile')

If the commands are written as a string of commands separated by semicolons you can simply replace myMFile with the string of commands.

You also have to make sure that myMfile is on the Matlab path (If you are on windows the easiest way is to add it to the MATLAB folder in My Documents). Any matlab call will unfortunately open a hidden matlab automation window. You can not easily get around Matlab opening up in some form, but at least this way it won't pop up on the screen.

Nine Tails 1 Newbie Poster

The thing that you are doing wrong is that you are not putting everything together into a single string.

It is easiest to use string formatting in order to create the system command that you are looking for. For instance you can do the following:

program_str = 'python'
args = '-V -3'
file_to_run = 'tempPy.py'
cmd_str = ''.join([program_str, ' ', args, ' ', file_to_run])
os.system(cmd_str)

If you have your arguments stored as a list it might be easiest to write a helper function to create the argument string

args_list = ['-V', '-3']
def mk_args_str(args_list):
    print ('%s '*len(args_list) % tuple(args_list).rstrip()

formatted_args = mk_args_str(args_list)
Nine Tails 1 Newbie Poster

To gain access to hello.py in another module you could always use an import statement. In order to do this you will have to make sure that the module that you have saved is on the python path.

The python path can be viewed by importing the sys module and then typing sys.path. If your module is not in one of the folders listed you can either move the module to one of the folders that is already on the path or append a new folder to the python path. After you have accomplished this you can simply use an import statement to gain access to the main() function in hello.py. In general import will give you access to all functions defined in the module that is imported.

Nine Tails 1 Newbie Poster

It would make your code more elegant if you first defined a function (which in this case would mean using a def keyword) that would take as input the list of entries for a single employee and would give as output a true or false statement. Your code would then check whether or not the output is true or false using an if statement. If true you would add the employee information to your records and if false you would not add it.

Also, I would use the print statements from your code and paste them into your function. That way the user can have some sort of intelligent feedback. So when the function runs it will signal your code in the while loop to either keep the data or not. If not then you will have hit one of the print statements inside the function that will notify the user of the error.

Nine Tails 1 Newbie Poster

Functions are nice! You could write a function to check an employee's entries. At the end of your while loop you could construct the list of values for the employee and then run it through the function to check it. If it doesn't pass you can then print something saying that there was an error and not keep the data, otherwise you could append the employee's information list to a list. This would create a list of lists where each entry is a list of information for a single employee.

Once that is accomplished you can perform sorting and file I/O on the entries of the list of lists.

Nine Tails 1 Newbie Poster

I am slightly confused by your description of the assignment. You are using raw input to get values, so it is very difficult to get a large number of entries with such a scheme. In a real world setting you would have a file somewhere that would contain this information in some predefined format. From that you could read the data in and do whatever sorting or file writing that you desired.

As far as your code is concerned, you can save the information gathered into a list by doing the following after you exit the while loop:

employee = [eName, pRate, hWork]

You can then sort this list using built in methods which are well documented in the python help files. File I/O is also well documented and straightforward.

P.S. The first four lines of your code make your work look sloppy. The first line is never used and the next three are immediately overwritten so there is no point in assigning them. Python will retain references to eName, pRate, and hWork even though they are assigned in a code block (the while loop) unlike some other languages such as C++. That means that you will have access to these variables in any subsequent lines of code.

Nine Tails 1 Newbie Poster

I am trying to create a widget that will display information for a large number of items. This widget needs to have the following properties:

1.) Highlights a line on a left mouse click
2.) Has an accessor function that can be used to retrieve the line number that is currently highlighted
3.) Fields are aligned into columns

The problem is that the Listbox widget does not provide text formatting (which means that it solves 1 and 2 but not 3). I also can’t get away with creating a class of linked Listboxes which would be displayed side by side because I can’t find any way to right justify text in a Listbox consistenty (right justification is needed because dollar amounts look sloppy when left justified).

I came up with the idea of creating a Text widget that would contain lines of labels where each label corresponded to a field. Basically for each line window_create is used to add a Text widget of height one that acts as a line. Each entry is then properly formatted by using window_create on this new line to add each field individually (6 fields in all). This worked fine at first when I tried it on 73 lines, but when I tried it on the full list of 1,444 lines the computer completes the code but does not display anything. How can this be fixed? Just in case seeing the code is helpful I have attached it below (it …

Nine Tails 1 Newbie Poster

This is by no means the cleanest way to implement this code. In order to complete what you are asking continue and break statements would mostly do the trick. If you include a continue statement after you print out an error message you will stop the next question from being asked. For instance:

employee=[]
eName="Employee"
pRate="Payrate"
hWork="Weekly Hours"
while True:
	eName=raw_input("\nEnter the employees' first and last name. ")
	pRate=int (raw_input("What is their hourly wages? " ))
	if pRate < 6 or pRate > 20:
        	print ("Employee has to make at least $6 and no more than $20!")
		continue
	hWork=int(raw_input("How many hours did they work this week? "))
    	if hWork < 1 or hWork > 60:
        	print ("Employee has to work at least 1 hour and no more than 60 hours!")
		continue
	break

When a continue is reached the code will restart from the top of the while loop. The only way that the code can reach the final break statement is if all entered values were valid.

You are not actually saving any of the inputs that the user is giving you. Just before the last break statement you need to update employee with the values that the user entered.

Also please make sure not to put spaces between function names and parentheses such as in 'int ('. I fixed this in the code that is supplied above. Python can be funny about such things. The code above works perfectly when run from the command line without …

Nine Tails 1 Newbie Poster

It would be helpful to know how your resource lines are entering your program. Are they entered by the user interactively, read in from a file, created from other variables that exist in your program code, etc.

Also, in your last post you stated that your goal is to perform integer addition

10000 + 56 = 10056

Instead of the string concatenation

'10000' + '56' = '1000056'

that is described in your original post. Integer addition won't work on your SEC values because they contain non-numeric characters.

If for some reason that you did not describe you are also looking to add strings as if they were integers you can accomplish this as follows:

str1 = '10000'
str2 = '56'
#Integer addition solution as an integer
int_total = int(str1) + int(str2)
#Integer addition solution as a string
str_total = str(int_total)
Nine Tails 1 Newbie Poster

I'm assuming that the data in:

1 J=2,7183 SEC=CON450X450 NSEG=2 ANG=0
56 J=7224,164 SEC=CON450X450 NSEG=2 ANG=0

is a string that you have read in from somewhere. If you are using code to generate these strings then you can vastly simplify things. I'm assuming that you also aren't too familiar with python, so I figure that some simple code using a for loop would be the most useful to you.

Under these circumstances you can accomplish your goal as follows:

#Define input variable
in1 = '1 J=2,7183 SEC=CON450X450 NSEG=2 ANG=0\n56 J=7224,164 SEC=CON450X450 NSEG=2 ANG=0' 

#Define constants
OUTPUT_PREFIX = 'elset='

#For each line add the output for the line to output list
lines = in1.split('\n')
output_list = []
for line in lines:
	#I assume that your fields are delimited by spaces
	fields = line.split(' ')
	elset_suffix = fields[0]
	#Slice off the 'Sec=' prefix
	sec_field = fields[2]
	elset_prefix = sec_field[4:]
	
	#join all three strings together
	elset_str = ''.join([OUTPUT_PREFIX, elset_prefix, elset_suffix])
	output_list.append(elset_str)

The variable output_list contains the desired results. This is by no means the best way to handle such a situation, but it should help you understand the logic behind how the solution can be obtained.

Nine Tails 1 Newbie Poster

If you are talking about the source code that they mentioned as being at www.wrox.com, I was able to find it at the following website:

http://www.wrox.com/WileyCDA/WroxTitle/Beginning-Python.productCd-0764596543,descCd-DOWNLOAD.html