404 Posted Topics
Re: Well, here's the basic idea. You have a button in your GUI. The button has a callback function. That function can then call your program. Here's a typical instance: [code="Python"] def mycallback(self, event): os.system("C:\Program Files\Myprog.exe") [/code] Jeff | |
Re: Lists of lists work fine as container arrays (not mathematical arrays, but you don't seem to be wanting that). So: [code="Python"] nlat=10 mylist = [] for x in range(0,nlat): tmp = [] for y in range(0,nlat): disk=sphere(pos=(x*s,y*s), radius = r, color=(0,1,9)) tmp.append(disk) mylist.append(tmp) [/code] et voila! An array of spheres. … | |
![]() | Re: For the file choosing, you should check out the filedialog module. Google for references on it. For the endless looping, take a look at the code and think about how to remove the element of choice from it. Jeff |
Re: As well he should :) Here's what wooee means: [code="Python"] def SD(): b = [] for n in range(r-1): if r[n] > a: b.append((r[n] - a)**2) if r[n] < m: b.append((a - r[n])**2) SD = (float(b)/r)**0.5 #float because the data includes decimal values return SD print "The standard deviation is", … | |
Re: Yeah, that's interesting ... None in Python is a special type, whereas Nothing is equivalent to NULL of whatever datatype one is working with: Dim b as Integer: b = Nothing -- this makes b a Nothing value of type Integer (as opposed to a None type). In short, Nothing … | |
Re: Well, if you think about it, the problem does not have a unique solution. [code="Python"] x = Class() x.name = 'the_name' x.value = 10 y = Class() y.name = 'the_name' y.value = 20 [/code] So now ... do you want x.value or y.value? Maybe both? Jeff | |
Re: Two thoughts. (1) You're writing code as if it were C or Pascal. A better way is to write a class: [code="Python"] class Bird(object): def __init__(self, nID=0, ter=0, year=0, fled=0, age=0.0): self.nID = nID self.ter = ter self.year = year self.fled = fled self.age = age def from_line(self, string): items … | |
Re: Well, start with a thought. How can you explain the process to someone else in English? Make it general: given data in rows of [i]n[/i], how would you rewrite it in rows of [i]m[/i]? Once you have that explanation in English, it will lead to the thought that gives the … | |
Re: I really like the text we used for my high-school level class: Python Programming for the Absolute Beginner. Two caveats -- it's game-oriented, and it doesn't use Python 3.0. That said, it contains clear explanations and models reasonably good programming practices. Jeff | |
Re: [quote]My question is what is the order in which instantiations and allocations occur in python when instantiating a class? The author's code below is under the assumption that 'DefaultDomainName' will exist when an instance of the class is created (e.g. __init__() is called), but this does not seem to be … | |
Re: I'm confused. Here's the help on shutil.copy: [quote="TFM"] >>> help(shutil.copy) Help on function copy in module shutil: copy(src, dst) Copy data and mode bits ("cp src dst"). The destination may be a directory. [/quote] Can you be specific with your question? Thanks, Jeff | |
Re: One more thought: lists of lists in Python function reasonably well as arrays: [code="Python"] mylist = [[1,2,3],[4,5,6],[7,8,9]] print mylist[0][2] 3 [/code] I say "reasonably well" -- there aren't any linear algebra methods that can be used on lists of lists (like det(), inv(), or eigenvalues()), but the LinearAlgebra module covers … | |
Re: Just to add to scru's point: Being able to re-use code is an extremely important but sometimes overlooked feature of programming. Its main value is not saving time, but eliminating bugs. That is, if the code for CD works provably correctly (or testably correct even), then a DVD class built … | |
Re: I fully agree, Vega. The coding purists frown on while True loops, but I find them really useful. Glad to know someone else out there shares my guilty secret. :lol: Jeff | |
Re: Without contradicting the posters above, I would add that Python makes OOP really easy. And in the end, coding with objects is easier to wrap one's mind around than coding without objects -- once OOP is learned, of course. So ... yes, you could ... but you probably shouldn't. ;) … | |
Re: [code="Python"] >>> class Thing(object): pass >>> t = Thing() >>> dir(t) ['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__'] >>> print t.__class__ <class '__main__.Thing'> >>> print t.__module__ __main__ [/code] Jeff | |
Re: [QUOTE=scru;798764]Now I want to take it a step further and make myself a small GUI library for python 3. What I'm interested in is how I could go about designing the object hierarchy among other things. As a community of python users, this would be an ideal place to start … | |
![]() | Re: In Python < 3.0, [b]print[/b] is a keyword and as such cannot be deleted or clobbered, as far as I can tell. So for example, it's not a part of __builtins__ because it isn't a function. Interesting that on this point, Python is bowing to the wisdom of C. Jeff ![]() |
Re: The style is good too. In general, functions should do one thing and do it well. Yours pass that criterion. Although ... I don't usually have a "main()" for the simple reason that I like to examine variables if the program crashes, and stuffing the main code into main() makes … | |
Re: I'm not getting your results: [code="Python"] class test(object): a = 55 def dispmenu(self): print self.a >>> >>> instance = test() >>> >>> instance.dispmenu() 55 >>> action1 = instance.dispmenu() 55 >>> print action1 None [/code] Here's what I think you want to do: [code="Python"] >>> action1 = instance.dispmenu >>> action1() 55 … | |
Re: If you have two genuinely separate programs, then they will have to signal each other. This would likely be accomplished with a pipe, and you'll need some kind of signal handling. How badly do you want to pursue this solution? What are you trying to accomplish? Jeff | |
Re: The newline character is '\n' (including the quotes), not \n\. Hope it helps, Jeff | |
Re: This appears to work: >>> im = Image.open("24bit test.bmp") >>> im2 = im.convert("P") >>> im2.save("256bit test.bmp") The bit-depth of the output file is 8, so I assume that's what you were after? Jeff | |
Re: Ah. Here's a stab: [code=Python] def change_time(record, new_time): name,id,time = record.split('|') time = str(new_time) record = '|'.join((name,id,time)) + '|' return record [/code] Jeff | |
Re: Nice code! Line 19 in the second file is (one of?) your troubles: def __init__(self, filename = "", NodeList = [], ModelInfo = [""], EdgeList = []) This is tricky, and it gets even experienced Python users several times until they learn by banging their heads against walls. Here's what … | |
Re: Yeah, that's a challenging newbie project. Start by solving your problem in English -- write a plan that closely imitates how you would do this by hand. Then we can help turn your plan into code. Jeff | |
Re: Search this forum for posts by Seagull One. She has gotten a lot of this code working. Jeff | |
Re: It's good code, seagull (or should I say "Lore-enn"?). You're asking a question that has more to do with the speech recognition module than Python, so I can't directly help. But when I'm stuck on such, I usually try out the help files. In this case, it looks as if … | |
Re: Surely this is too hard? What are you trying to do, Matt? Also, Vega ... how old is 'too old', exactly? :) Jeff | |
Re: I would probably take a completely different approach: [code="Python"] class MyFloat(float): def __new__(self, name, value): return float.__new__(self, value) def __init__(self, name, value): self._name = name name = property(lambda self: self._name) x=MyFloat("name", 1.5) print x print x._name print x.name [/code] The call to __new__ is necessary when subclassing immutable types like … | |
Re: Or this: [code="Python"] try: val = int(item) if val > 0: do stuff for positive numbers elif val < 0: do stuff for negative numbers else: # don't omit zero! do stuff for zero except: pass [/code] If you want to allow floating point numbers, you can change the int() … | |
Re: The function "unpack" is not a built-in function. Does your code import some module at the top? Jeff | |
Re: Well, a quick tangential note: your method for extracting the extension name[-4:] is buggy. What if the file extension is ".jpeg"? Better: [code="Python"] filename, ext = os.path.splitext(name) if ext == ".tif": do stuff ... savedfile = filename+".jpg" do more stuff... [/code] | |
Re: Good news: the convention for scientific notation 2.72e-2 (which is the correct rendering) is so well-recognized that superscripting is not required. Jeff | |
Re: Just to jlm's good points above, the getter and setter functions allow the object to control access to its own data members. For instance, suppose you have a sprite that can rotate. Clearly, if the rotation is greater than 360 degrees, then you want to reduce the rotation to its … | |
Re: There are a couple of bugs that jump out at me. First, notice that you remove the "serverlist.txt" file each time a server message is received. So naturally, only the last server that broadcasts will be in your list -- which I don't think was the desired behavior. Second, your … | |
![]() | Re: Well, that depends. Do you want to end up coding games? If so, then start! [url]http://www.pygame.org/news.html[/url] The "tutorials" link on the left should be helpful Jeff |
Re: I would try to contact Tim Golden. At first glance, it looks like his AD module doesn't handle Unicode properly (though I might be mistaken about this) Jeff | |
Re: Absolutely. You need ... dictionaries. [code="Python"] store_dict = {} f1 = open("ITEMS.txt") for line in f1: store, item = line.strip("/n").split(" ") store_dict[store] = item f1.close() items_dict = {} f2 = open("IDs.txt") for line in f2: item, ID = line.strip("\n").strip() items_dict[item] = ID f2.close() [/code] At this point, you now have … | |
Re: It worked for me. What result are you getting? Jeff P.S. eliminate the spaces between "code = Python" to get your tag to work. | |
Re: Is there a chance that the sound is playing on the wrong sound device? I have two sound cards (built in and recording equipment), and sometimes my software gets confused. Jeff | |
Re: Well, that question may not have an answer if the values are not unique; for example, if book['key1'] = {'keya':valuea, 'keyb':valueb} and book['key2'] = {'keya':valuec, 'keyd':valued} then 'keya' cannot be mapped backwards. But suppose that your values [b]are[/b] unique. Then you could do this: [code="Python"] >>> import shelve ## set … | |
Re: Well, the usual procedure here is to start with your code. Post it here and then we can give pointers and such. Jeff | |
Re: If you are determined to iterate through the original list ... and there are times when that's appropriate ... then you must iterate through a [b]copy[/b] of the list: [code="Python"] >>> nums = [1.0,2.0,3.0,4.0,5.0] >>> for n in nums[:]: if n % 2 == 1: # better way to say … | |
Re: What do you mean by "increasing the look"? If you just want to make it more visible, you could set border colors to hot pink or something... Jeff | |
Re: Seems to me that this line ought to be an error... [code="Python"] c=Cursor() [/code] because you then try to execute commands on it ... but it's not assigned to any database, right? Maybe I'm misunderstanding. Jeff | |
Re: I would recommend re-ordering your class a bit, if you have the freedom to do so: [code="Python"] class Temperature(object): def __init__(self, temp): def toCelcius(self): def toRankine(self): def toFahrenheit(self): [/code] That way, all of your temperatures can be stored as objects of the same class and you can access the value … | |
Re: The appropriate code tags are [noparse][code="Python"] ## code here [/code][/noparse] What's happening? I'm guessing that the sound plays, but then the code hangs after that? Jeff | |
Re: Yes, the standard way of writing a shell (which is what you are doing) is like this: [code="pseudo"] while True: inputline = raw_input(myprompt) parse inputline into command, operands if command == "quit": break else: execute command using operands [/code] | |
Re: What you've done is to make the game itself into your main code. If you think about it, that's not what you want. The main loop here is [code="pseudo"] playerscore = housescore = 0 while user wants to play: winner = CrapsGame() # note the 's' -- a Crap Game … |
The End.