2,646 Posted Topics
Re: if D is a dictionary a code like [code=python] for x in D: print x [/code] will print only the keys of the dictionary. If you wnat the keys and values, you should write [code=python] for k, v in D.iteritems(): print k, v [/code] | |
Re: I suggest a debugging technique: you comment out large parts of your program until it runs, then you uncomment more and more until you find the problem. You can use multiline strings to comment easily ;) | |
Re: I think the easiest way is that you run python's built-in IDE which is called [icode]idle[/icode]. The modules of idle should be a folder like [icode]/usr/lib/python2.4/idlelib[/icode]. On my mandriva system, I do this by putting a small executable called [icode]$HOME/bin/idle[/icode] and which contains the following (I found this somewhere on … | |
Re: replace eval by print repr(...) so that we can see the string which was eval'ed | |
Re: I think a simple way to do it is give [icode]AddData[/icode] objects a pointer to the main window like this [code=python] classAddData(...): def __init__(self, parent, id, title, main_window, size = (200, 150)): self.main_window = main_window .... def OnCancel(self): ... self.main_window.Show(True) [/code] As a constructive criticism of your code, I think … | |
Re: A no-name-moose, rather than writing template looking code with [icode]<NameOfDictionary>[/icode] things, whihc are not in the syntax of python, you should better write a python function with local variables, like this [code=python] def crypt(input_sequence, dictionary): L = '' for i in input_sequence: if i in dictionary: L += dictionary[i] return … | |
Re: I think, replace [icode]"\n"[/icode] by [icode]", "[/icode] in the method [icode]__str__[/icode] of the [icode]Matrix[/icode] class. | |
Re: In my opinion, the problem when "it takes too long" in this context, is that a certain function call blocks. The first question is to find the innermost function which blocks. Often, low level functions which attempt to obtain data through a connection have a time delay argument, or a … | |
Re: I think it would be easier if you told us how you run your programs, and may be which code they contain ! | |
Re: I think this would be better written in a non recursive way like this [code=python] # adding to christ o'leary's fight function import random import sys global monster_HP1 monster_HP1 = 5 global hp hp = 7 def main(): global monster_HP1, hp print """you are in a fight! Type 'h' to … | |
Re: Use the [icode]reduce[/icode] built-in function like this [code=python] number = reduce(lambda x, y: x + y, num) [/code] | |
Re: Here is a way to open an object file and make it a string [code=python] text = open('data.xrc').read() [/code] :) | |
Re: An alternative to using a password to communicate with your friend is to send them a picture of your favorite pet and to use the sha128 digest of the picture as a key, together with a standard encryption algorithm (RSA or a better one). This way, only the people with … | |
Re: A faster way [code=python] wordlistfile = open('wordlist.txt', 'r') possiblefile = open('possible.txt', 'r') D = {} for line in wordlistfile: word = line.strip() s = "".join(sorted(word)) if not s in D: D[s] = [] D[s].append(word) for line in possiblefile: scrambled = line.strip() s = "".join(sorted(scrambled)) if s in D: print scrambled, … | |
Re: For simple dictionaries, you can avoid using pickle and write to a file. Try this in a console [code] >>> from pickle import load >>> D = load(open("old.txt")) >>> out = open("foo.txt", "w") >>> out.write(repr(D)) >>> out.close() >>> E = eval(open("foo.txt").read()) >>> D == E True >>> [/code] My advice … | |
Re: Its strange that you can't run another evince. You could get the results of the commands like this [code=python] from commands import getstatusoutput def open_pdf(self): status, output = getstatusoutput('evince '%s''%self.ebook) if status: print "evince failed with status %d" % status print output else: print "evince terminated successfully" [/code] It may … | |
Re: I factored your code a while, and added a car [code=python] import Tkinter as tk import time root=tk.Tk() root.title("Traffic Simulation") canvas = tk.Canvas(root, width=1000, height=400, bg="#FFFFFF") canvas.pack() # make roads road_data = [ (140,150,200,150,200,150,200,50), (250,150,250,50,250,150,374,150,374,150,374,50), (424,150,424,50,424,150,542,150,542,150,542,50), (592,150,592,50,592,150,674,150,674,150,674,50), (724,150,724,50,724,150,850,150), (140,200,200,200,200,200,200,300), (250,200,250,300,250,200,344,200,344,200,344,300), (394,200,394,300,394,200,542,200,542,200,542,300), (592,200,592,300,592,200,674,200,674,200,674,300), (724,200,724,300,724,200,850,200), ] for t in road_data: canvas.create_line(t, width=5) … | |
Re: I think the problem is that you always use the same [icode]image_sonic[/icode]. For example, each time you go through the double [icode]for i ... for j ...[/icode], the value of [icode]image_sonic[0][0][/Icode] is modified, but since all [icode]self.TileSnc[stat][pos][/icode] are this same [icode]image_sonic[/icode] object (or array), evrey loop erases the previous one. … | |
Re: Try this :) [code=python] menu = """You can (1) Enter the coefficients (2) Check solvability (3) Solve the system using the Gauss-Seidel method (4) Solve the system using the Gauss-Jacobi method """ while True: choice = raw_input(menu) print "you chose", choice if not choice in "1234": break [/code] | |
Re: Another way to build the lists [code=python] words = """ aero aesthet andr arch arch ast baro biblio bio cardi chron cosm crat cycl dem dont dogma dox esth """.strip().split() [/code] | |
Re: [url]http://docs.python.org/lib/string-methods.html#l2h-265[/url] | |
![]() | Re: Shoudn't you open the file with mode [icode]"r"[/icode] in [icode]load_takings[/icode] and with mode [icode]"w"[/icode] in [icode]"dump_takings"[/icode] ? ![]() |
Re: hm, I tried this on the python console [code] >>> def P(p_0, t, i): ... return p_0 *(1+i) ** t ... >>> P(10000, 5, 7) 327680000 >>> P(10000, 7, 5) 2799360000L >>> [/code] so I can't see your problem ? | |
Re: There is a nice article here [url]http://www.onlamp.com/pub/a/python/2004/12/02/tdd_pyunit.html[/url] about the [icode]unittest[/icode] module, with examples. Personnaly, I prefer to use [icode]doctest[/icode] for test driven development. I'll try to give an example later with doctest. | |
Re: I don't know if this is your problem, but pyro proxies can't be shared between different threads. I had this problem once and I was able to solve it by creating a thread devoted to driving a pyro proxy and calling it's methods. Other threads could use the proxy by … | |
Re: why not [icode]"%.3f"[/icode] ? | |
Re: In the server program, I would write [code=python] while 1: data =clientsock.recv(BUFSIZ) if not data: break clientsock.sendall('echoed ' + data) [/code] and in the client program [code=python] tcpCliSock.sendall(data) data = tcpCliSock.recv(1024) if not data: break print data [/code] | |
Re: Since your TestTable objects are instances of this class [url]http://wxpython.org/docs/api/wx.grid.GridTableBase-class.html[/url], I suppose you can use directly it's methods on your objects (like [icode]SetValue[/icode]). Also your method redefinitions hide the original methods, so may be you should drop, or rewrite your methods so that they call the methods of the parent … | |
Re: I'd suggest something like this [code=python] import os def writeSizes(folder, tmpfile): temp = open(tmpfile, "w") for name in sorted(os.walk(folder).next()[2]): p = os.path.join(folder, name) temp.write("%s %d\n)" % (name, os.getsize(p)) temp.close() writeSizes("C:\path\to\my\folder", "C:\path\to\my\temp\file") [/code] | |
Re: 1/ if you type "statistics module python" in a google search engine, you'll find easily some modules with functions for statistics, so don't say you've searched the web ! 2/ The list of all modules which come by default with the python distribution can be browsed in the documentation here … | |
Re: [icode]raw_input[/icode] returns a string, so your test should be [code=python] if choice == "military": ... [/code] With your code, python thinks that military is a variable's name and complains because there is no such variable. | |
Re: [url]http://en.wikipedia.org/wiki/Nine_Men's_Morris[/url] | |
Re: You should explain what you mean by "closing your python script, then reopen it". Do you mean ending the current program execution and start a new execution of the same program ? In that case, you could use functions like [icode]os.exec[/icode] which replaces the current process by another one, or … | |
Re: I think the indentation is wrong. May be it should be this [code=python] def LoadImage(image_name): file_name = self.clssSnc.sprites[slf.clssSnc.stat] if self.clssSnc.stat == 0: file_name = self.clssSnc.spritesi[slf.clssSnc.stati] #carrega a imagem do cenrio ou do personagem fl = None image_path = "sprites/" + image_name fl = open(image_path, 'r') if (image == "sonic"): file_path … | |
Re: This regular expression searches for "debug" not followed by an alphanumeric character or underscore [code=python] import re pat = re.compile(r"debug(?!\w)") sentence = "I'm trying to debug a problem but the debugger is not working" print pat.sub("fix", sentence) [/code] However it wouldn't work for "I'm trying to debug a problem but … | |
Re: A general solution is to use a regular expression like this [code=python] import re pattern = re.compile("cheese") data = 'I love cheese; cheese is my favourite besides cheese.' count = -1 def f(mo): global count all_cheeses = ["shredded gorgonzola","bree","camenbert"] count += 1 return all_cheeses[count] print pattern.sub(f, data) [/code] The call … | |
Re: First there are many very complete clients for instant messenging, like psi [url]http://psi-im.org/[/url] for example. Within minutes, you can be able to talk to your buddy privately on the internet, so the first question is do you really need to write your own instant messenger in python (knowing that you … | |
Re: The problem is that your line is a character string, so [icode]line[5][/icode] is the fifth character. You must split the lines to obtain arrays of 7 items, and get the fifth item like this [code=python] data_list.sort(key= lambda line: float(line.split(",")[5])) [/code] also you don't nedd to write a loop to get … | |
![]() | Re: I have 2 links [url]http://effbot.org/tkinterbook/[/url] and [url]http://www.astro.washington.edu/owen/TkinterSummary.html[/url], however if you're a new to gui programming in python, you should consider starting with wx python rather than tkinter, mastering a gui toolkit is a lot of work, so the initial choice is important. |
Re: May be you can replace the call to [icode]os.system[/icode] by a [icode]subprocess.Popen[/icode] object. See [url]http://docs.python.org/lib/node536.html[/url]. I don't know if this will solve your cmd window problem however. | |
![]() | Re: If you want to store the notebook in a readable form, a good solution could be pretty-print the dictionary to the file without using the cPickle module. You would have two functions [code=python] from pprint import pprint TXTFILE = "notebook.txt" def dump_notebook(notebook): fout = open(TXTFILE, "w") pprint(notebook, fout) fout.close() def … |
Re: Here is a good tutorial for a beginner [url]http://www.openbookproject.net/thinkCSpy/[/url] | |
Re: The problem here is that you're adding information to the file: you must give your program a way to know that "ndoe" is a "name", "swimming" a "hobby", etc. This depends essentially on your file: you may have a succession of records with the same sequence name-home-hobby-gender repeated in this … | |
Re: I suggest you type [icode]python vs java[/icode] or [icode]python vs c++[/icode] in the google search engine, there are many comparisons online. However, I think the universally recognized strength of python is that there is much less programmer's effort involved to get something running in python than in other languages :) … | |
Re: When you want to transform the elements of a list one by one, use the [icode]map[/icode] function like this [code=python] def unstrip(theStr): return " %s " % theStr theArray = ["red", "blue"] # prints [" red ", " blue "] print map(unstrip, theArray) [/code] The list comprehension is posible too … | |
Re: I looks like homework, it's almost the same problem as JA5ONS's. You should try to work with him, or tell us more about your problem. | |
Re: Have a look at this thread may be [url]http://www.daniweb.com/forums/thread139376.html[/url] | |
Re: No, it should have the same indentation as [icode]def __init__()[/icode]. As it is, your function [icode]OnPrint[/icode] is defined in the scope of the [icode]__init__[/icode] function, and does not exist outside this scope. | |
Re: Instead of a [icode]print[/icode] statement, you could use my snippet [url]http://www.daniweb.com/code/snippet928.html[/url] to write a log file of what is happening during execution | |
I'm on linux, and I'm using a command called 'espeak' which reads sentences on stdin and writes phonemes on stdout. Whe I run it in a console, the output is the following [code] bash$ espeak -q -v mb-fr4 -s160 bonjour tout le monde # <-- I type this input, the … |
The End.