169 Posted Topics

Member Avatar for esmooov

Does it really just hang, or perhaps return the wrong error code? Usually when I run into this kind of problem I telnet to port 21 and type in the USER and PASS commands manually. Try this; you may find the server giving an unexpected message. That might require you …

Member Avatar for BearofNH
0
88
Member Avatar for sb3700

I don't know the answer but suggest two things to consider: [LIST=1] [*]Sending all four lines in one write() may burst data faster than the receiver can actually handle. [*]You might check on the newlines in the string sent by the write(), they could be different from what you type …

Member Avatar for sb3700
0
3K
Member Avatar for Norbert X

... you should not use [B]random[/B] as your variable name. Pick something different, say [B]rand[/B] or [B]rnum[/B] or whatever. Otherwise you overwrite your imported [B]random[/B] module the first time you assign something to variable [B]random[/B]. Things will work the first time and fail subsequently.

Member Avatar for Norbert X
0
133
Member Avatar for artmansoft

Good explanation, Jeff. I don't really have a good handle on Python's [B]type()[/B] function, but you can use it as ff:[code=Python]>>> str(type(56)) "<type 'int'>" >>> str(type('ABC')) "<type 'str'>" >>> str(type(False)) "<type 'bool'>" >>>[/code]Perhaps others can come up with a better way, but you'd basically do something like:[code=Python]NothingTypes = {"<type 'int'>": …

Member Avatar for jrcagle
0
1K
Member Avatar for Ene Uran

Thanks for the example, VE. I have a .tar.bz2 file I need to read within Python and was able to take your code and use it. I've put together a little .tar.bz2 extraction example in case others follow the same path and wind up here. This is for Python 2.5 …

Member Avatar for BearofNH
0
154
Member Avatar for amit1_x

The [B]datetime[/B] object takes a series of integers, not a single string:[code=Python]>>> t1 = datetime.datetime(2009,12,22,22,22) >>> t2 = datetime.datetime(2009,12,22,20,22) >>> (t1-t2).days 0 >>> (t1-t2).seconds 7200 >>>[/code]The difference between two [B]datetime[/B]s is a [B]timedelta[/B], an object containing days, seconds and microseconds. If you really have to work with strings then you …

Member Avatar for BearofNH
0
161
Member Avatar for chebude

The jargon for this is "transpose a matrix". I did this interactively (pay careful attention to the brackets):[code=Python]>>> import numpy >>> M = numpy.matrix([['A', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10'], ... ['B', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20'], ... ['C', '21', '22', …

Member Avatar for BearofNH
0
99
Member Avatar for breatheasier

[code=Python]... p = choice(array) #CHOOSE A PARTICLE,P, AT RANDOM FROM THE ARRAY pnewx=p.x + 0.05*(random() -0.5) #GENERATE A STEP IN X pnewy=p.y + 0.05*(random() -0.5) #GENERATE A STEP IN Y for disk in array:#ANY OTHER DISK IN THE ARRAY ??might be wrong? ...[/code] I don't see where you handle the …

Member Avatar for BearofNH
0
158
Member Avatar for eyewirejets

The statement [ICODE]if win_loss_code == 'W' or 'w':[/ICODE] doesn't parse the way you want. Try [ICODE]if win_loss_code == 'W' or win_loss_code == 'w':[/ICODE] and do the corresponding for Ll. The way it is currently written the test is [LIST=1] [*]Is win_loss_code equal to 'W'? *OR* [*]Is the string 'w' a …

Member Avatar for jrcagle
0
118
Member Avatar for mahela007

For even more fun, learn some SQL. There's even Python code to access SQL databases, but I would suggest first some interactive basics with (free) mySQL. (You quickly learn why there's a software interface :-) Like programming, familiarity with SQL creeps up in job interviews, and it's nice to be …

Member Avatar for jrcagle
0
80
Member Avatar for curiouskitten

Possibly not the easiest to explain, but a completely different approach would be [ICODE]"".join([ [letr.lower(),letr.upper()][letr in 'aeiou'] for letr in list("whatever was input") ])[/ICODE]

Member Avatar for Ene Uran
0
11K
Member Avatar for thehivetyrant

No, things don't group the way your code expects. In particular, [ICODE]elif i1 and i2== False:[/ICODE] parses as [ICODE]elif (is==True) and (i2==False):[/ICODE]. You would need parentheses to change the grouping from the default. But I suggest you go even further. For example your initial test can read simply [ICODE]if i1 …

Member Avatar for BearofNH
0
106
Member Avatar for drew7py

[QUOTE]a = array('f',[bird[0],bird[2],bird[3],bird[5],bird[9],bird[7]])[/QUOTE]Doesn't the 'f' come second in the [ICODE]numpy.array()[/ICODE] call?

Member Avatar for Ene Uran
0
3K
Member Avatar for Ghostenshell

For spacing, try [ICODE]print ' ' * 15 + 'CRAPS'[/ICODE]. Also, the word is "lose" not "loose". Where you say [ICODE]while roll == roll:[/ICODE] you probably should just say [ICODE]while True:[/ICODE] since that is the canonical way of setting up an infinite loop. Now to get out of the loop …

Member Avatar for oldSoftDev
0
128
Member Avatar for thehivetyrant

Not to be a blanket-wetter, but I noticed you have [ICODE]pythag=(a**2+b**2)**0.5[/ICODE] before the statements that give values to [B]a[/B] and [B]b[/B]. The program as presented will not run without minor edits. Still, it looks much nicer than the original code!

Member Avatar for thehivetyrant
0
493
Member Avatar for Aue

Use a tuple of the two words as dictionary index. Piece of cake. Of course you have to figure out how to separate the words from the HTML, but there's plenty of libraries out there.

Member Avatar for leegeorg07
0
84
Member Avatar for gsingh2011

I don't know what that "it" is you are trying to write to the file. You seem to want to write the output of the program being run, but I don't think you can do that with [B].call()[/B] which just returns the final status of the program run, which is …

Member Avatar for BearofNH
0
469
Member Avatar for acoxia

I don't see how this can work, since [B]dashes[/B] is initially undefined:[code=Python] while y != g: dashes += " _" y += 1[/code] Perhaps replace that code with the simpler [icode]dashes = g * " _"[/icode]?

Member Avatar for ashutosa
0
171
Member Avatar for damo87

Using the secret [noparse][code=Python]...[/code][/noparse] tags, we get: [code=Python]def main(): def Parent(i): return i/2 def Left(i): return 2*i def Right(i): return 2*i+1 def Heapify(A, parent, last): left = Left(parent) right = Right(parent) if left <= last and A < A[parent]: smallest = left else: smallest = parent if right <= last …

Member Avatar for damo87
0
434
Member Avatar for bnarav

Doesn't look like a Python problem -- looks like a network problem. Are you sure your new server's network configuration is set up like the old one's? (Both hardware connections and software parameters).

Member Avatar for mn_kthompson
0
122
Member Avatar for Rejinacm

Probably only Guido knows for sure. This topic has come up a lot in the past. An [B]if/elif[/B] chain is as concise as [B]switch/case[/B] and allows for tests other than equality, so in some sense is more powerful. But it does not have the "semantic familiarity" of a [B]switch[/B] statement, …

Member Avatar for sneekula
0
137
Member Avatar for Littlerocks

Nice code. Almost there. Here's a hint: [ICODE]evolved_array[iy,ix]==1[/ICODE] does NOT assign any value to [ICODE]evolved_array[iy,ix][/ICODE]. But you seem to be trying to do that. Blame it on your editor...

Member Avatar for Littlerocks
0
119
Member Avatar for Gribouillis

As I understand it, the 2.x print statement syntax is illegal in 2.6 when the new print function is desired, and always illegal syntax in 3.0. (I think that's what #1 meant when he said "breaks old code"). So logic along the lines of[code=Python]if sys.version_info[0] >= 3: print(a,b,c) else: print …

Member Avatar for Gribouillis
0
169
Member Avatar for happymadman

Problem is you're naming your output variable [B]random[/B]. Use another name! After you do your [ICODE]import random[/ICODE] you've defined an object named [B]random[/B] that basically contains a bunch of procedures such as [B]randint()[/B]. You're overwriting all that with some random integer and that's why you get an error the second …

Member Avatar for happymadman
0
232
Member Avatar for StarZ

Yes, it's simple enough. The example given started out with [ICODE]mypocket = ("gum", "wallet", "old kleenex", "bus ticket")[/ICODE]. Was there something unusual about this that prevented you from extending that from 4 items to 10?

Member Avatar for vegaseat
0
162
Member Avatar for axn

Shouldn't [ICODE]zip_fh = 'C:\Documents and Settings\file.cfg'[/ICODE] read [ICODE]zip_fh = 'C:\\Documents and Settings\\file.cfg'[/ICODE]?

Member Avatar for woooee
0
218
Member Avatar for Fractilion

I concur with woooee's suggestion to look into classes. But it would be remiss not to mention global variables, which are useful in certain simple cases.[code=Python] A = 602 B = 47 def this2(): global A,B A += B def this(): this2() def main(): global A this() print A # …

Member Avatar for bvdet
0
111
Member Avatar for chebude

Try something along these lines: [code=Python]while line: Total = 0 fields = line.split() N = len(fields) print fields[0], # Print name. Notice trailing comma #sum each 5 fields over the raw: for step in range (6, N, 5): Total2 = 0 for i in range (step-5, step, 1): Total2 += …

Member Avatar for chebude
0
88
Member Avatar for Paramecium

One problem is with the statement [ICODE]def queueReverse(q):[/ICODE] because, if you want [B]queueReverse()[/B] to be a method of [B]Class Queue[/B] then the first argument shouldn't be named [B]q[/B], it should be named [B]self[/B]. When you later write [ICODE]test.queueReverse()[/ICODE] the value of [B]test[/B] is assigned to [B]self[/B] when you get to …

Member Avatar for bvdet
0
109
Member Avatar for ashain.boy

You might try adapting something like this, coded off the cuff:[code=Python] >>> line 'San Diego 5,673 7,062 24.50%' >>> L = line.split(" ") >>> for i in range(len(L)): ... if L[i][0].isdigit(): ... break ... else: ... print "Bad imput line: %s" % s ... # Whatever else you do to …

Member Avatar for bvdet
0
91
Member Avatar for myself2211

If I understand the problem, you want an [B]else[/B] on the [B]for[/B] loop, and do a [B]break[/B] when you don't want it executed. Try something along these lines:[code=Python] for line in text_file: # if the line does not contain the typed word # then continue to the next line if …

Member Avatar for myself2211
0
238
Member Avatar for Bouzy210

At the very least, your lines probably end with \n so any [ICODE]endswith()[/ICODE] will probably fail unless you account for that.

Member Avatar for jlm699
0
153
Member Avatar for sandmonkey

Are you sure you're actually executing that statement? I would have written your code as ff:[code=Python] elif choice == 5: hisin = raw_input("Please enter the customers unique ID again... ") fname = "C:/Users/Dhyan/Documents/" + hisin + ".txt" h = open(fname) if not h: print "Unable to open input file %s, …

Member Avatar for sandmonkey
0
120
Member Avatar for Stefano Mtangoo

I use Emacs for everything, mostly under Windows but also those rare occasions I find myself running Linux. I had to learn it -- it was the only decent screen editor available to me in 1978.

Member Avatar for mathijs
0
240
Member Avatar for bugmenot

In C you can set up a variable with a memory address (say, some PCI-mapped memory) and use pointer constructs to Read/Write it. Python does not naturally allow for this kind of direct access to memory, so you will need to work around this restriction (sometimes called a "feature" by …

Member Avatar for BearofNH
0
888
Member Avatar for doeman

Here's another recursive way although it is kind of cheating: [code=Python]>>> sum_cube = lambda N: eval(["0","(N*N*N) + sum_cube(N-1)"][N>0]) >>> sum_cube(10) 3025[/code]

Member Avatar for ZZucker
0
100
Member Avatar for Chemist

After [ICODE]t = time.strftime('%H:%M')[/ICODE], t is a string, not a number. Instead of [ICODE]time.localtime(t + i * 60)[/ICODE] you probably want something like [ICODE]time.strftime("%H:%M", time.gmtime(time.time()+i*60))[/ICODE], possibly corrected for timezone. [code]>>> time.strftime("%H:%M", time.gmtime(time.time()+i*60)) '22:38'[/code]

Member Avatar for Gribouillis
0
139
Member Avatar for Dart82

Yes, I'd probably start breaking the sentence into a list of words. Two problems come to mind: (1) Punctuation would need to be trimmed from the end of each word (but possibly need to be remembered in case the punctuated word(s) are part of the output); (2) Individual words can …

Member Avatar for Dart82
0
110
Member Avatar for Stefano Mtangoo

I've done SQL access from Python but only to standard servers. Perhaps you could use the [B]Search[/B] box (upper right part of the page, well on my page anyway) and see if there's any help already here.

Member Avatar for Stefano Mtangoo
0
118
Member Avatar for trihaitran

You didn't mention the easiest (though not too secure) approach: connect to [ICODE]http://greenman:greenman@schoolfinder.com/[/ICODE]and you're in without needing any new magic. Or did you want something more secure?

Member Avatar for trihaitran
0
213
Member Avatar for Stefano Mtangoo

It looks like the [ICODE]def OnPrint()[/ICODE] code is indented too far. It's usually at the same indentation level as the[ICODE] __init__()[/ICODE] method.

Member Avatar for Stefano Mtangoo
0
116
Member Avatar for dr_kac

Duplicate keys? E.g., 123 34 0.45 707 55 0.45 621 38 0.45 Would leave you with {0.45:'621_38'}

Member Avatar for dr_kac
0
68
Member Avatar for nuaris

This is approximately what I would do in your position. You really should read the appropriate documentation as there are tricky conditions that can arise when the program you run is looking for input, but this should get you started. [CODE=Python]>>> import popen2 >>> cmdout , cmdin = popen2.popen2("dir") >>> …

Member Avatar for BearofNH
0
202
Member Avatar for Seagull One

The normal way to handle this is in the [B]init[/B] function inside class MyApp, you would write [ICODE]self.topic=None[/ICODE] or something similar.

Member Avatar for Seagull One
0
106
Member Avatar for mayapower

I don't think there's a built-in way to do this. Of course you can roll your own with a dictionary, viz: [CODE=Python]class a(object): IDmap = {} def __init__(self): a.IDmap[id(self)] = self b = a() print id(b) # 13975472 print a.IDmap[id(b)] # <__main__.a object at 0x00D53FB0> [/CODE] Of course, 0x00D53FB0 == …

Member Avatar for tomleo
0
74
Member Avatar for Shadow14l

Windows calls this the "Foreground Window". It might not be the one your program is running. [ICODE]HWND GetForegroundWindow(VOID); [/ICODE] Once you have the HWND you can call [ICODE]GetWindowText(hWnd,outbuf,outlen)[/ICODE] to get the title bar contents. I assume pywin32 supports these calls, but haven't looked them up.

Member Avatar for Shadow14l
1
16K
Member Avatar for R.V.
Member Avatar for ms_melagiri

If you use [B]popen2[/B] you can get the subprocess output and inspect it yourself, then write it to a file.

Member Avatar for woooee
0
499
Member Avatar for ronnix

In the past I have used global variables to pass messages to/from threads. In order to avoid disaster I only allow one thread to write to any given variable. There are operating-system-specific message passing primitives you can also use, but then they tend to be operating system specific, or relatively …

Member Avatar for BearofNH
0
89
Member Avatar for FreezeBlink

Try a list comprehension: [code=Python]>>> nums = [1.0,2.0,3.0,4.0,5.0] >>> evens = [x for x in nums if x%2==0] >>> evens [2.0, 4.0] >>>[/code]

Member Avatar for jrcagle
0
118

The End.