2,646 Posted Topics

Member Avatar for Kezoor

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]

Member Avatar for Kezoor
0
182
Member Avatar for TheNational22

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 ;)

Member Avatar for TheNational22
0
219
Member Avatar for The_Bear

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 …

Member Avatar for Stefano Mtangoo
0
182
Member Avatar for ihatehippies

replace eval by print repr(...) so that we can see the string which was eval'ed

Member Avatar for Gribouillis
0
132
Member Avatar for Stefano Mtangoo

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 …

Member Avatar for Stefano Mtangoo
0
182
Member Avatar for danizzil14

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 …

Member Avatar for Gribouillis
0
212
Member Avatar for defience

I think, replace [icode]"\n"[/icode] by [icode]", "[/icode] in the method [icode]__str__[/icode] of the [icode]Matrix[/icode] class.

Member Avatar for defience
0
98
Member Avatar for implor

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 …

Member Avatar for Gribouillis
0
107
Member Avatar for gracc

I think it would be easier if you told us how you run your programs, and may be which code they contain !

Member Avatar for gracc
0
83
Member Avatar for mruane

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 …

Member Avatar for Gribouillis
0
189
Member Avatar for jworld2

Use the [icode]reduce[/icode] built-in function like this [code=python] number = reduce(lambda x, y: x + y, num) [/code]

Member Avatar for jworld2
0
488
Member Avatar for lllllIllIlllI

Here is a way to open an object file and make it a string [code=python] text = open('data.xrc').read() [/code] :)

Member Avatar for lllllIllIlllI
0
177
Member Avatar for iamgame

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 …

Member Avatar for iamgame
0
759
Member Avatar for predator78

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, …

Member Avatar for ZZucker
0
212
Member Avatar for aot

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 …

Member Avatar for woooee
0
982
Member Avatar for keripix

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 …

Member Avatar for woooee
0
95
Member Avatar for Luckymeera

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) …

Member Avatar for Gribouillis
0
1K
Member Avatar for lumeniel

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. …

Member Avatar for lumeniel
0
100
Member Avatar for Feenix45

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]

Member Avatar for woooee
0
727
Member Avatar for SoulMazer

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]

Member Avatar for Dunganb
0
2K
Member Avatar for predator78
Member Avatar for predator78
0
164
Member Avatar for leegeorg07

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] ?

Member Avatar for leegeorg07
0
112
Member Avatar for bman923

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 ?

Member Avatar for Chewie-DK
0
90
Member Avatar for ganil123

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.

Member Avatar for ganil123
0
284
Member Avatar for Chewie-DK

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 …

Member Avatar for Chewie-DK
0
118
Member Avatar for Mr_Grieves
Member Avatar for predator78

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]

Member Avatar for predator78
0
143
Member Avatar for tillaart36

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 …

Member Avatar for tillaart36
0
258
Member Avatar for Th3_uN1Qu3

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]

Member Avatar for Th3_uN1Qu3
0
199
Member Avatar for db_ozbecool

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 …

Member Avatar for woooee
0
163
Member Avatar for tomtetlaw

[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.

Member Avatar for tomtetlaw
0
108
Member Avatar for LexiK
Member Avatar for jlm699
0
1,000
Member Avatar for Dekudude

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 …

Member Avatar for mathijs
0
3K
Member Avatar for lumeniel

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 …

Member Avatar for lumeniel
0
729
Member Avatar for Hazey

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 …

Member Avatar for bvdet
0
74
Member Avatar for damyt01

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 …

Member Avatar for bvdet
0
96
Member Avatar for skate6566

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 …

Member Avatar for EAnder
0
136
Member Avatar for g_e_young

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 …

Member Avatar for g_e_young
0
1K
Member Avatar for leegeorg07

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.

Member Avatar for Gribouillis
0
97
Member Avatar for Dekudude

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.

Member Avatar for Gribouillis
0
177
Member Avatar for leegeorg07

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 …

Member Avatar for Gribouillis
0
148
Member Avatar for baba786

Here is a good tutorial for a beginner [url]http://www.openbookproject.net/thinkCSpy/[/url]

Member Avatar for sneekula
0
282
Member Avatar for ndoe

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 …

Member Avatar for ndoe
0
114
Member Avatar for baba786

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 :) …

Member Avatar for Gribouillis
0
105
Member Avatar for Dekudude

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 …

Member Avatar for Dekudude
0
132
Member Avatar for J3STER

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.

Member Avatar for J3STER
0
106
Member Avatar for cmac1212

Have a look at this thread may be [url]http://www.daniweb.com/forums/thread139376.html[/url]

Member Avatar for cmac1212
0
80
Member Avatar for Stefano Mtangoo

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.

Member Avatar for Stefano Mtangoo
0
116
Member Avatar for besktrap

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

Member Avatar for besktrap
0
235
Member Avatar for Gribouillis

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 …

Member Avatar for woooee
0
167

The End.