sneekula 969 Nearly a Posting Maven

This re stuff makes my head spin! I can see that it is very powerful for text processing, but also seemingly very complex! Almost another language within Python.

sneekula 969 Nearly a Posting Maven

In python if I divide 6/3 I get 2, that's fine. But I divide 6/4 and get 1...

Why aren't I getting a decimal? I would like to know because I want to make something that can detect wether or not a number is whole. (I need help with that too). But I thought I would give it a shot myself and found that it wont give me a decimal when it should.

Thanks

Here is one way to do this:

a = 6
b = 4
# assure a float division
print a/float(b)  # 1.5
# test for integer (whole number)
print type(a) == int  # True
sneekula 969 Nearly a Posting Maven

I keep reading threads here, some use the Tkinter GUI toolkit and others use the wxPython GUI toolkit. Why would one use one or the other?

sneekula 969 Nearly a Posting Maven

The department's computer has Python 2.4 and on my home computer (really my dad's computer) I installed Python 2.5.
Is it possible to have Python 2.4 and Python 2.5 on the same computer?

sneekula 969 Nearly a Posting Maven

Another question, can zipfile archive several files like tarfile? How would you save the decompresed files?

sneekula 969 Nearly a Posting Maven

Looks like Ghostdog is right ...

# test sample from user input
months = ['Dec', 'Jan']
# test sample from os.path.walk()
files = ['FebWork1,dat', 'JanWork3.dat', 'DecWork7.dat']  # test
dates = []
for i in files:
    for j in months:
         if j in i:
             dates.append(i)
             break
print dates
# ... or list comprehension ...
dates2 = [i for i in files for j in months if j in i  ]
print dates2
"""
my output -->
['JanWork3.dat', 'DecWork7.dat']
['JanWork3.dat', 'DecWork7.dat']
"""

I sort of can see how list comprehension evolves from the individual for loops. Is there anything list comprehension can do that one could not write out with individual for loops?

sneekula 969 Nearly a Posting Maven

Python offers a number of modules providing easy file compression and decompression. Here are code snippets for two of the popular compression formats ...
zipfile -->
http://www.daniweb.com/code/snippet629.html
tarfile -->
http://www.daniweb.com/code/snippet630.html

Now that is fairly easy code to work with. I didn't know it was that simple. Does anyone have a particular preference of a compression format?

sneekula 969 Nearly a Posting Maven

What is wxGlade?

sneekula 969 Nearly a Posting Maven

Since there is so much discussion on one of the threads on zipping/unzipping files and all the associated incompatibilities, is there a way to do this with Python and cut out the middleman?

sneekula 969 Nearly a Posting Maven

I your interest should be in chemistry, write a chemical structure drawing program, that displays bondenergies and charge intensities of the molecule.

sneekula 969 Nearly a Posting Maven

Thanks mawe,

originally I thought it was by ascii value, but then 11 would have been the min value since ascii 1 is lower than ascii 7. So the order in the list was more like: [7, 9, 11, 12, 700, 777, 13456789, 'Dick', 'Mary', 'Paul', 'Zoe'] Is there a way to get a min/max of each type?

sneekula 969 Nearly a Posting Maven

I took a mixed type list and set out to find the min and max values. The results are very surprising to me:

mixed_list = [11, 'Dick', 12, 'Mary', 7, 'Zoe', 9, 700, 777, 'Paul', 13456789]
mn = min(mixed_list)
mx = max(mixed_list)
print mn, type(mn)  # 7 <type 'int'>
print mx, type(mx)  # Zoe <type 'str'>

How does Python handle mixed type lists to get such a result?

sneekula 969 Nearly a Posting Maven

Thank you,
what would happen to a swap if I had a dictionary with duplicate values like this:

d = {'a': 1, 'b': 1, 'c':1, 'd':99}
sneekula 969 Nearly a Posting Maven

Thanks,
I noticed the argument 'event'. What does it contain? What can it be used for? Right now it seems to be just a dummy argument that is not used for anything.

sneekula 969 Nearly a Posting Maven

Vegaseat's explanation will take a while to sink in, but I am seeing the light. Changed my sample code to:

def add_item(list2):
    # make a true copy first
    list2 = list(list2)
    list2.append(99)
    return list2
list1 = [1, 2, 3]
list3 = add_item(list1)
print list1  # [1, 2, 3]  works now!
print list3  # [1, 2, 3, 99]  expected

The thing with counter4() I don't quite get. It works in an interesting way. I am not passing a list to it, but I guess the default does. Does that mean that a default argument only gets evaluated on the first call to the function? Otherwise it would reset to zero all the time.

sneekula 969 Nearly a Posting Maven

Thanks mawe,
looks like root.destroy() would be the safer way to exit Tkinter. I don't use IDLE very often, I am a DrPython addict, but managed to freeze up IDLE a few times in the past. I checked back on that, and indeed it's associated with the use of root.quit(). Now it makes a lot more sense!

sneekula 969 Nearly a Posting Maven

Thank you fellow Pythonians. I learned from mawe's explanation that in order to find an item or find the maximum item you have to un-nest the list as shown by vegaseat's code.

Converting the list to a string, as Ghostdog74 suggested, works well for the search, but can't find the maximum.

sneekula 969 Nearly a Posting Maven

I have used DrPython for a while now and really like it! Comes in Windows and Linux versions. It has clean displays and a minimum of quirks (does not like foreign characters). There are lots of configuration options. The results are displayed in an output window, and you can select and copy items from there. You can also install the code completion plugin from the internet from within the DrPython. The code completion option is great help for a beginner.

sneekula 969 Nearly a Posting Maven

I asked that in another thread, but it got lost:

When do you use root.quit() and when do you use root.destroy() to exit a Tkinter program?

Also, can you intercept an exit when you use the little x in the title bar, just to affirm that you really want to exit, or you in case you should save data as a file before you exit.

sneekula 969 Nearly a Posting Maven

I know that one has to be careful with mutable arguments like lists applied to function calls, so that things like this won't accidentally happen:

def add_item(list2):
    list2.append(99)
    return list2
 
list1 = [1, 2, 3]
list3 = add_item(list1)
 
print list1  # [1, 2, 3, 99]  oops!
print list3  # [1, 2, 3, 99]  expected

Can somebody explain to me why exactly why this happens? Also, can this be usefull?

sneekula 969 Nearly a Posting Maven

I was experimenting with the nested list example in thread:
http://www.daniweb.com/techtalkforums/post246791-72.html
and was trying to search a nested list like that:

nested_list = [1 ,'Dick', 2, ['Mary', 7, 9, [700, 777, 'Paul']], 13]
 
if 'Paul' in nested_list:
    print 'found Paul'
else:
    print 'Paul not found'

It always tells me that 'Paul' is not found! How come? Is there an error in Python?

sneekula 969 Nearly a Posting Maven

To exit, you can also use a key-binding. Look:

import Tkinter as tk
 
def exit(event):
    root.quit()
 
root = tk.Tk()
tk.Label(text="Press <ESC> to exit").pack()
root.bind("<Escape>", exit)
root.mainloop()

Just press the Esc-Key and ... that's it ;)

Thanks, that works well.

Just a question, when do you use root.quit() and root.destroy()? I have seen them both.

sneekula 969 Nearly a Posting Maven

Finally, it works perfectly. Thanks for all your help. What do you suggest I do now? I'm hoping to learn how to use images in my forms.

Unless you haven't done it already, I would a let the user select cities from departure and arrival listboxes, so there is no typing (and possible misspelling) needed. I have done something similar in one of my chemistry programs.

sneekula 969 Nearly a Posting Maven

What do you do to re-enable the button?

sneekula 969 Nearly a Posting Maven

In Ene's example if you code print dic['michigan'] your result would be the value 'lansing'. That is the way you access dictionaries. In other words, the key is the index.

sneekula 969 Nearly a Posting Maven

So, how did you solve the problem?

BTW, I really enjoyed reading your blogs!

sneekula 969 Nearly a Posting Maven

How do you best swap the key:value pair of a dictionary?

sneekula 969 Nearly a Posting Maven

As far as I know in the US all the states and capitals are unique. Since your are entering the state and want to find that capital, Ene's solution will be the easiest.

sneekula 969 Nearly a Posting Maven

I want to make a Tkinter button respond to a left and right mouse click differently. How can I do this?

sneekula 969 Nearly a Posting Maven

How can I best center a Tkinter window on my display screen?

sneekula 969 Nearly a Posting Maven

Thanks mawe, that works well.

Just a question, if get rid of the titlebar, how can I exit the program?

sneekula 969 Nearly a Posting Maven

I found and played a pretty sophisticated Tic Tac Toe game written in Python at an earlier posting here:
http://www.daniweb.com/techtalkforums/thread26658.html

sneekula 969 Nearly a Posting Maven

How can I make a Tkinter window take up all my display screen area?

sneekula 969 Nearly a Posting Maven

Is there a way to read Adobe PDF files with Python?

sneekula 969 Nearly a Posting Maven

This came up on Chris99's bus ticket program thread. Would like to see any simple examples on how to pass variables between classes without using global variables.

sneekula 969 Nearly a Posting Maven

Also a word of advice:
1) start your class names with a capital letter
2) create class instances like:
main = Main(root)
child = Child()
3) now you can get rid of the globals by associating the variables properly to the class instance via self. or the instance name.

Maybe somebody could give an example of how to pass variables between classes without using global variables.

Sorry, didn't want to highjack the thread!

sneekula 969 Nearly a Posting Maven

Thanks for the advice, I will try to discover the fun of experimenting with the code. After all, it can't do more than melt down my computer!?

sneekula 969 Nearly a Posting Maven

Thank you Mr. Uran! I am still trying to think my way through the details of the sort_chem function.

sneekula 969 Nearly a Posting Maven

Your code works vegaseat, but I have a problem with it. If you know anything about chemistry, there are several chlorobenzoic acids, ortho, meta and para, indicating the position of the chlorine in relation to the carboxylic acid. I would like the sort to keep the chlorobenzoic acids together, like the original table in the expanded data below:

# table of chemicals in stockroom as list of lists
# order --> stock number, chemical name, quantity (grams)
from operator import itemgetter
def sort_table(table, column_num):
    """
    sort a list of sublists that form a table by column number
    note that first column in table starts with column_num zero
    """
    return sorted(table, key=itemgetter(column_num))
def print_table(table):
    """
    pretty print the table
    """
    print '-'*66
    print "%-10s   %-40s  %s" % ('stock', 'chemical', 'quantity(g)')
    print '-'*66
    for item in table:
        print "%-10s   %-40s  %8d" % (item[0], item[1], item[2])
    print '-'*66
# assign constants
SN = 0  # index of stock number
CN = 1  # index of chemical name
QY = 2  # index of quantity
# short version of the table
table = [
['ud-99137', 'm-chlorobenzoic acid', 750],
['ud-99133', 'o-chlorobenzoic acid', 1500],
['ud-99139', 'p-chlorobenzoic acid', 600],
['ud-99672', 'potassium carbonate', 5000],
['ud-99083', 'aluminum chloride anh', 3500],
['ud-99412', 'nickel sulfate', 100],
['ud-99463', 'oleic acid', 250]
]
print "original table:"
print_table(table)
print
print "table sorted by chemical name:"
table1 = sort_table(table, CN)
print_table(table1)

Does anyone have an idea how to do this?

sneekula 969 Nearly a Posting Maven

What do I do if I want two buttons and the NW corner is already taken by the first button?

sneekula 969 Nearly a Posting Maven

I am writing a small Python program for the use in our departments stockroom. This is what I have come up with (just starting):

# table of chemicals in stockroom
# order --> stock number, chemical name, quantity (grams)
# short version of the table
table = [
['ud-99137', 'm-chlorobenzoic acid', 750],
['ud-99672', 'potassium carbonate', 5000],
['ud-99083', 'aluminum chloride anh', 3500]
]
# pretty print the table
print '-'*66
print "%-10s   %-40s  %s" % ('stock', 'chemical', 'quantity(g)')
print '-'*66
for item in table:
    print "%-10s   %-40s  %8d" % (item[0], item[1], item[2])
print '-'*66

For right now I would like to sort this list of lists by stock number, chemical name and quantity on hand. Any ideas?

sneekula 969 Nearly a Posting Maven

Ah, that seems to work for me, thanks!

sneekula 969 Nearly a Posting Maven

I am confused, why does this nice sticky continue somewhere else as csgal suggested. I went there and it sort of dead ends!
http://www.daniweb.com/techtalkforums/thread63753.html

sneekula 969 Nearly a Posting Maven

As you can see, I am playing around with the Tkinter GUI toolkit. How can I keep a number of widgets fixed in a location even when the user expands the window?

sneekula 969 Nearly a Posting Maven

According to my engineering professor, the reason there is a variety of computer languages is that each language (at least the popular once) was written for a purpose. Python was not written to make Operating Systems, but C was (for Unix). So it would be a little silly to use Python.

sneekula 969 Nearly a Posting Maven

Is there a way to make a Tkinter GUI window that the user can not change the size of?

sneekula 969 Nearly a Posting Maven

I agree with Jeff!
I have seen other computer language code like C, where the writer did not not use any indentation. Now you get lost in an ocean of { and } and ; which makes large code almost unreadable!

sneekula 969 Nearly a Posting Maven

I like to create a database of common chemicals with Python. How would I go about that? Any help welcome!

sneekula 969 Nearly a Posting Maven

Wow, I am learning a lot. Your code was written like a tutorial, that helps a beginner like me! Thanks!

How do you copy the files in listbox2 to a different directory?

sneekula 969 Nearly a Posting Maven

I have started to program with windows concepts. It is different then console and that is were the learning curve sets in. Once you realize how to approach a GUI with its frames, windows, dialog boxes, buttons, labels and so on, you will enjoy it. I use Tkinter right now, it is part of the Python installation, and seems to be the easiest GUI toolkit to learn with.