sneekula 969 Nearly a Posting Maven

Thanks vegaseat, I looked at the code and its output. Having the full path name for each file would be kind of ugly to select through. Isn't there a way to show just the file names without the directory in my listbox?

I put a few temporary print statements into your code to see how it all works. I think I am getting that part.

sneekula 969 Nearly a Posting Maven

I have a directory with many subdirectories. Each subdirectory contains a small number of image files (.jpg). I would like to accumulate all these image files in a Tkinter listbox, sort them and be able to select blocks of files and send them to another listbox. Ultimately I would like to use the files in the selected file listbox and make a copy of those images in a separate directory.

I hope I made myself clear, any help?

sneekula 969 Nearly a Posting Maven

Yeah, that works great. Is there a way to edit a listbox line?

sneekula 969 Nearly a Posting Maven

I downloaded and installed Pygame, got some of my soundfiles into the working directory and adjusted the sound file names in the program. Now it works great! Thank you!

sneekula 969 Nearly a Posting Maven

Oh thank you, that will be a good start into my next project. Is there a way to sort the items in a listbox?

sneekula 969 Nearly a Posting Maven

Nice program! I ran it and for factorial 13 I got 1932053504 when it should be 6227020800.

sneekula 969 Nearly a Posting Maven

I have a data file with chemical names (one name per line) and want to load that into a Tkinter GUI listbox and then be able to select it by clicking on the line. Need some help.

sneekula 969 Nearly a Posting Maven

I am experimenting (playing around) with the Python Tkinter Gui, and wondered if there is a way to play a sound like from a .wav or .au file?

sneekula 969 Nearly a Posting Maven

I know that Pyhon compiles source code to a Byte Code before the interpreter works on it. What does this Byte Code look/behave like?

sneekula 969 Nearly a Posting Maven

I see, so using:

while True:
    pass

would be an endless loop, and you better give it a condition to break out of this loop. Something like:

while True:
    quit = raw_input("press q to quit ")
    if quit == 'q':
        break
sneekula 969 Nearly a Posting Maven

I will check Tkinter programming first. Any examples that show the difference between console and GUI programming?

sneekula 969 Nearly a Posting Maven

Jeff, thanks. I had to change the split separator from ' RGB ' to ' RGB' to make it work.

sneekula 969 Nearly a Posting Maven

The module shutil has functions for deleting whole directory trees.

sneekula 969 Nearly a Posting Maven

Import the module shutil, it has functions for copying and moving files, copying whole directory trees, deleting directory trees and more.

sneekula 969 Nearly a Posting Maven

I have a color data file that gives the color name and its RGB values, for instance like this:

red RGB(255,0,0)
green RGB(0,128,0)
blue RGB(0,0,255)
brown RGB(165,42,42)
gold RGB(255,215,0)
maroon RGB(128,0,0)

I like to create a color dictionary from this file that looks like this:

{'red': (255,0,0), 'green': (0,128,0), 'blue': (0,0,255), ...}

How can I best do that?

sneekula 969 Nearly a Posting Maven

Thanks a lot for the class examples, now using a class makes a little more sense. I guess you really have to think ahead where a class would come in handy!

Are there any guidelines when to use and not to use a class?

sneekula 969 Nearly a Posting Maven

Which Graphics User Interface (GUI) would you recommend for a beginner? I have seen some examples of Tkinter, wxPython and GTK.

sneekula 969 Nearly a Posting Maven

I keep reading about endless loops, is that something you want or do you always have to control an endless loop situation?

sneekula 969 Nearly a Posting Maven

Look what can happen if you spend a lot of time on DaniWeb!
The government will probably legislate a warning sticker soon!

sneekula 969 Nearly a Posting Maven

I tried to transpose mawe's horizontal simple bargraph into a vertical simple bargraph, but with no success. Has anybody tried this before?

Make something like this:

##########
#####
##
#

Look like this:

#
#
#
#
#
# #
# #
# #
# # #
# # # #
sneekula 969 Nearly a Posting Maven

So, if the fallthrough is due to a 'break' statement, the 'else' associated with a 'while' or 'for' will not kick in? Might be useful sometimes.

sneekula 969 Nearly a Posting Maven

Looks like I have to find myself some good Python class examples. Are there any around?

sneekula 969 Nearly a Posting Maven

If you don't know then you wont be able to help me.:confused:

Thanks for your kind answer. I took the liberty to check
http://www.pygtk.org/pygtk2reference/class-gtktreeview.html
all I can say is "ouch!", that is one complex thing! No wonder you are lost!

sneekula 969 Nearly a Posting Maven

LaMouche, nice game!

What would a dictionary do to improve it?

Would it be hard to dress the game up in a GUI like Tkinter?

sneekula 969 Nearly a Posting Maven

Looks like this code comes in handy too:

[I]# make elements in the list all_uppers unique and sorted[/I]
all_uppers = sorted(list(set(all_uppers)))

Thanks to all who helped!

sneekula 969 Nearly a Posting Maven

Wow LaMouche, this is sweet Python code. I learned a lot about file reading/writing too. Here is what I have, showing it with my test file:

"""
file sentences.txt looks like that:
Tom ate his Apple.
Frank shoveled Snow.
Henry drove a Truck.
"""
f_in = open("sentences.txt","r")
lines = f_in.readlines()
f_in.close()
lines_new = []
for line in lines:
    lines_new.append(line[0].upper() + line[1:].lower())
f_out = open("sentences_fixed.txt", "w")
for line in lines_new:
    f_out.write(line)
f_out.close()
"""
file sentences_fixed.txt looks like that:
Tom ate his apple.
Frank shoveled snow.
Henry drove a truck.
"""

My actual sentence file works fine too. I think I understand your code. Each line is a sentence and line[0].upper() simply makes certain that the first letter in the sentence is capitalized. To this you concatenate with the + the remainder of the sentence converted to lower case. Nice example of string slicing!

Thanks!

sneekula 969 Nearly a Posting Maven

What is pygtk/gtk and what is is it used for?
Also, what is a treeview?

sneekula 969 Nearly a Posting Maven

I have a file of sentences similar to this:

Tom ate his Apple.
Frank shoveled Snow.
Henry drove a Truck.

I would like to prosess each sentence so it only retains the capital letter in the first word, every other word in the senetnce should be lower case. The result would look like this:

Tom ate his apple.
Frank shoveled snow.
Henry drove a truck.
sneekula 969 Nearly a Posting Maven

Thanks Jeff, that is even fancier than I needed. Will come in handy though!

The re module seems to be nice, really don't have much trouble seeing the syntax, but I have a little problem understanding the one liner about making a list unique and sort.

sneekula 969 Nearly a Posting Maven

I think I am getting a feeling in which case to use 'for' or 'while'. Never looked at a recursive fuction as a loop, but I guess it is and can be used for that purpose. Almost looks like a limited goto!

With the loops there is also continue, break and else. Why and how are they used in a loop?

sneekula 969 Nearly a Posting Maven

I have a text I want to search for all it's upper case letters, then present these letters unique and sorted. I welcome any suggestions.

sneekula 969 Nearly a Posting Maven

When do you use a for loop and when to use a while loop. Are there other loop types in Python? Some of the slicing examples almost look like loops.

sneekula 969 Nearly a Posting Maven

Thanks mawe, that will come in handy! Also thanks to vegaseat for the beauty of a bar graph made with Tkinter, that is my first intro to Tkinter. Looks like GUI programming is much simpler than with other languages.

sneekula 969 Nearly a Posting Maven

How would you make a simple bar graph from a list of data?

data = [20, 15, 10, 7, 5, 4, 3, 2, 1, 1, 0]
sneekula 969 Nearly a Posting Maven

Sounds like hard program to play around with. You could really do some damage to files in your hard drive!

sneekula 969 Nearly a Posting Maven

Yeah, thanks a lot Ene! I can really use this in my Chemistry class. I also found your more elaborate example in the Code Snippets. Are you taking chemistry classes too?

Now I can see why people might want to use OOP with Python.

sneekula 969 Nearly a Posting Maven

Thank you! I am still trying to digest the one liners, particularly:

x_reversed = int(str(x)[::-1])

Can anybody break that down for me so I can see the flow?

sneekula 969 Nearly a Posting Maven

I like to create a structure in Python, something like:

solvent_name
boiling_point
melting_point
flash_point

I need to search and sort this structure. How can I best do that?

sneekula 969 Nearly a Posting Maven

Now I am looking for the best way to reverse the digits in an integer. For instance x = 12345 should become y = 54321.

sneekula 969 Nearly a Posting Maven

Works great! I have to play around with Python slicing. Thanks LaMouche!

sneekula 969 Nearly a Posting Maven

Python does make it look easy! Thank you!

sneekula 969 Nearly a Posting Maven

What is the most efficient way to spell a string in reverse, for instance s = "banana".

sneekula 969 Nearly a Posting Maven

If I have an integer like x = 12345, how do I separate that into individual integers 1, 2, 3, 4, 5?

sneekula 969 Nearly a Posting Maven

In the last couple of threads people have used the Python class, but really don't seem to know how to use them properly, or why to use them. Hence my question: "Why would you use a class in Python?"

I though only Java forces you to use OOP.

sneekula 969 Nearly a Posting Maven

I think I will stay away from using "class" until I learn how to use OOP at least rudimentary.

sneekula 969 Nearly a Posting Maven

Just an exercise in passing arguments to functions:

def increment(a, b, c):
    a += 1
    b += 1
    c += 1
    return a, b, c

def main():
    c = d = e = 0
    c, d, e = increment(c, d, e)
    # test the result
    print c, d, e  # 1 1 1
main()
sneekula 969 Nearly a Posting Maven

Thanks Jeff, so multiplying the sublist [0, 0, 0] by three makes three of exactly the same sublist having the same reference. Appending the list properly won't do that! Wanted to be tricky and paid the price!

sneekula 969 Nearly a Posting Maven

I am new to Python, but it seems to me that using main(), or whatever you want to call it, forces you to write cleaner code. Here is my argument, with no main() you create automatically global variables:

def show_x():
    """possible since x is global and is not changed"""
    print x
def double_x():
    return 2 * x
x = 3
# allows for somewhat sloppy global use of x
x = double_x()
show_x()  # 6

Wrapping the last few lines into main() makes x now local:

def show_x(x):
    """there is no global x, so it has to be passed in"""
    print x
def double_x(x):
    return 2 * x
def main():
    """main makes x local and forces you to pass it properly"""
    x = 3
    x = double_x(x)
    show_x(x)  # 6
main()

It also organizes your code a little, so you don't end up with code like this, where the lines that belong into main() are scattered all over (still in sequence though):

x = 3
 
def double_x():
    return 2 * x
 
# allows for somewhat sloppy global use of x
x = double_x()
 
def show_x():
    """possible since x is global and is not changed"""
    print x
 
show_x()  # 6
sneekula 969 Nearly a Posting Maven

I was trying to create a 3x3 list of lists, and came up with this surprising behaviour:

# creating a 2D list with an overloaded * operator
mlist3 = [[0]*3]*3
print mlist3  # [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
mlist3[0][0] = 1
print mlist3  # [[1, 0, 0], [1, 0, 0], [1, 0, 0]]  oops!
# populate it properly, now it works!
mlist3[0] = [7, 12, 23]
mlist3[1] = [22, 31, 9]
mlist3[2] = [4, 17, 31]
print mlist3  # [[7, 12, 23], [22, 31, 9], [4, 17, 31]]
mlist3[0][0] = 1
print mlist3  # [[1, 12, 23], [22, 31, 9], [4, 17, 31]]

Any explanation?

sneekula 969 Nearly a Posting Maven

Thanks LaMouche, I thought so too! At least you know what args looks like, since it is in () and separated by commas, it must be a tuple.

One thing that bothers me, vegaseat used tuple for a variable name, isn't that a keyword in Python and therefore off limits!?