Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Scaevolus posted:

Is this a (known?) bug?

I don't know or have any insight...but I would assume a tuple holds references to objects. You can't change the references, but if you can change the pointed-to objects, there's nothing stopping you.

Adbot
ADBOT LOVES YOU

Rad ROM Max
Mar 10, 2011

by XyloJW
Can anyone tell me what's going on with the last line in this function? I'm only really interested in the last line.

pre:
def multiplicationTable(n):
    lim=n
    x=1
    y=0
    table=[]
    for m in range(1,lim+1):
        y+=1
        x=y
        for n in range(1,lim+1):
            table.append(x)
            x+=y
    return [table [i::lim] for i in range(lim)]
I wrote this to make a times table, but I stole the last line from somewhere to split the table into lists, but I'm not sure how it works.

Here's the final value of table for multiplicationTable(4)
pre:
[1, 2, 3, 4, 2, 4, 6, 8, 3, 6, 9, 12, 4, 8, 12, 16]
Here's what the function returns due to the last line.
pre:
[[1, 2, 3, 4], [2, 4, 6, 8], [3, 6, 9, 12], [4, 8, 12, 16]]

king_kilr
May 25, 2007

Scaevolus posted:

Is this a (known?) bug?

It was filed in the CPython bugtracker yes. It's basically unfixable though, short of implementing STM in Python.

tripwire
Nov 19, 2004

        ghost flow

baku posted:

Can anyone tell me what's going on with the last line in this function? I'm only really interested in the last line.

pre:
def multiplicationTable(n):
    lim=n
    x=1
    y=0
    table=[]
    for m in range(1,lim+1):
        y+=1
        x=y
        for n in range(1,lim+1):
            table.append(x)
            x+=y
    return [table [i::lim] for i in range(lim)]
I wrote this to make a times table, but I stole the last line from somewhere to split the table into lists, but I'm not sure how it works.

Here's the final value of table for multiplicationTable(4)
pre:
[1, 2, 3, 4, 2, 4, 6, 8, 3, 6, 9, 12, 4, 8, 12, 16]
Here's what the function returns due to the last line.
pre:
[[1, 2, 3, 4], [2, 4, 6, 8], [3, 6, 9, 12], [4, 8, 12, 16]]
Its a list comprehension.

code:
[table [i::lim] for i in range(lim)]
Is equivalent to
code:
result = []
for i in xrange(lim):
    row = table[i::lim]
    result.append( row )

return result
If youre wondering about the table[i::lim], the way to interpret that is as slicing. The syntax is like this:

sequence[ start_index : stop_index : step_size ]

Except you can omit any of the terms there and they get filled to a default (default start is zero, default end is the length of the sequence, default step size is one element at a time). In this case its slicing from i to the end of the sequence, stepping lim units at a time.

Rad ROM Max
Mar 10, 2011

by XyloJW
okay I get it I was thinking 0 in range(lim) somehow corresponded to the nonexistent 0 element in my list, instead of being the index 0.

Hughmoris
Apr 21, 2007
Let's go to the abyss!
As mentioned above, I'm new to programming and just doing this as a hobby.

Here is my problem: I have a text file with about 1200 lines of garbage characters, with random letters spread throughout it. I want to read in a line, take any letters and put them in a string and then toss the rest of the trash characters, then move onto the next line.

I've found a way to do that for a text file with one or two lines but I don't think its efficient and 1200 lines is a lot to read into memory.

Any advice?

*This is my first real experience with learning read/write

*Ok, I'm making progress but I need to write a loop that will continue to read each line and do something with that line, until there are no more lines. What is a good way to write a loop to break when it reaches the end of the text file?

Hughmoris fucked around with this message at 03:05 on Mar 29, 2011

brosmike
Jun 26, 2009

Hughmoris posted:

Ok, I'm making progress but I need to write a loop that will continue to read each line and do something with that line, until there are no more lines. What is a good way to write a loop to break when it reaches the end of the text file?

Python makes this very easy for you:
code:
with open("my_file.txt") as myfile:
    for line in myfile:
        #do whatever

Hughmoris
Apr 21, 2007
Let's go to the abyss!
Thanks for the reply. Here is what I settled on:
code:

f = open('C:/Python32/programs/test_file.txt', 'r')
msg = f.read()
for c in msg:
    if ord(c) >= 97 and ord (c) <= 122:
        print(c)
It scans the text file and spits out any letters it sees. I thought 1200 lines was a lot but it took it about .5 seconds.

A A 2 3 5 8 K
Nov 24, 2003
Illiteracy... what does that word even mean?

Hughmoris posted:

Thanks for the reply. Here is what I settled on:
code:

f = open('C:/Python32/programs/test_file.txt', 'r')
msg = f.read()
for c in msg:
    if ord(c) >= 97 and ord (c) <= 122:
        print(c)
It scans the text file and spits out any letters it sees. I thought 1200 lines was a lot but it took it about .5 seconds.

You don't want to read the file that way. You're reading the whole file into memory at once. What if the size of the file is greater than the amount of free memory? The example you were given doesn't have that problem. Read this for more info: http://docs.python.org/tutorial/inputoutput.html

VirtuaSpy
Jul 26, 2002

OMG WTF FIREEEE!!
For you folks using or interested in using Python for scientific computing.

A great lecture by Fernando Perez on Scientific Computing Using Python. Covers NumPy, SciPy, SAGE, matplotlib, Mayavi, and more:

http://www.youtube.com/watch?v=1j_HxD4iLn8

Dren
Jan 5, 2001

Pillbug

Hughmoris posted:

code:

f = open('C:/Python32/programs/test_file.txt', 'r')
msg = f.read()
for c in msg:
    if ord(c) >= 97 and ord (c) <= 122:
        print(c)

The point that you should avoid reading the entire file into memory has been made several times. I would like to point out that python allows you the following syntactical nicety:
code:
if 97 <= ord(c) <= 127:
    print(c)
Also, you can directly compare the chars
code:
if 'a' <= c <= 'z':
    print(c)
And you may want uppercase letters too so try one of these two forms.
code:
if 'A' <= c <= 'Z' or 'a' <= c <= 'z':
    print(c)

if 'A' <= c.upper() <= 'Z':
    print(c)

brosmike
Jun 26, 2009
Python also lets you just do

code:
'a'.isalpha()

Rad ROM Max
Mar 10, 2011

by XyloJW
here is another question. i wrote this function to remove negative numbers from a list.

pre:
def removeneg(L):
    for i in L:
        if i<0:
            L.remove(i)
    print L
if i give it
[10, -2, 11, -9, -7, 0, 1, -12, 13]

it returns
[10, 11, -7, 0, 1, 13]

why doesn't it remove the -7?

Haystack
Jan 23, 2005





You're mutating the list that you're iterating over, which plays merry hell with your for loop.

Haystack fucked around with this message at 23:00 on Mar 29, 2011

Sneftel
Jan 28, 2009
This is where list comprehensions or filter() will be useful.

FoiledAgain
May 6, 2007

It seems like if you have two negatives in a row, it removes the first and keeps the second one. For example if you feed your function the list [-1,-2,-3] it returns [-2]. My guess is that when Python removes one number, it moves the next one into the deleted number's position in the list. When the loop continues, it appears to "skip over" one number, although it's just that the number you wanted is now at an index that the loop has already passed through. (or something like that, I don't know the right technical terms for what I'm trying to say)

MaberMK
Feb 1, 2008

BFFs

Sneftel posted:

This is where list comprehensions or filter() will be useful.

code:
>>> a = [1, 2, 3, -4, -5, 6, -7, 8, 9, -10]
>>> b = [x for x in a if x >= 0]
>>> b
[1, 2, 3, 6, 8, 9]

FISHMANPET
Mar 3, 2007

Sweet 'N Sour
Can't
Melt
Steel Beams
The lazy way would be to run that loop until it finds no negatives, but that's not very clean or elegant or sane.
E: Or construct a new list.

Rad ROM Max
Mar 10, 2011

by XyloJW

Haystack posted:

You're mutating a the list that you're iterating over, which plays merry hell with your for loop.

I see. I tried changing the IF to WHILE but that just created an error.

MaberMK posted:

code:
>>> a = [1, 2, 3, -4, -5, 6, -7, 8, 9, -10]
>>> b = [x for x in a if x >= 0]
>>> b
[1, 2, 3, 6, 8, 9]

Thanks, that's much better than whatever I was trying to do. I guess I still need to get the hang of assigning loops to variables like that.

code:
def removeneg(L):
    print [i for i in L if i >=0]

good jovi
Dec 11, 2000

'm pro-dickgirl, and I VOTE!

baku posted:

code:
def removeneg(L):
    print [i for i in L if i >=0]

Note that this doesn't actually modify L, or even return the modified list. If you're actually using that as written, its name is somewhat misleading.

Rad ROM Max
Mar 10, 2011

by XyloJW

Sailor_Spoon posted:

Note that this doesn't actually modify L, or even return the modified list. If you're actually using that as written, its name is somewhat misleading.

You're right, I was just making a list. It's pretty pointless

Rad ROM Max fucked around with this message at 19:59 on Mar 29, 2011

Hughmoris
Apr 21, 2007
Let's go to the abyss!

Dren posted:

The point that you should avoid reading the entire file into memory has been made several times. I would like to point out that python allows you the following syntactical nicety:
code:
if 97 <= ord(c) <= 127:
    print(c)
Also, you can directly compare the chars
code:
if 'a' <= c <= 'z':
    print(c)
And you may want uppercase letters too so try one of these two forms.
code:
if 'A' <= c <= 'Z' or 'a' <= c <= 'z':
    print(c)

if 'A' <= c.upper() <= 'Z':
    print(c)

Thanks for this.

FoiledAgain
May 6, 2007

How do you use linecache? I get the following error with it:

edit: n/m, I realized I was giving it a file instead of a file name. duh.

FoiledAgain fucked around with this message at 22:39 on Mar 29, 2011

Profane Obituary!
May 19, 2009

This Motherfucker is Dead

king_kilr posted:

Fun times:

code:
>>> a = ([], 2)

>>> a[0] += ["boy oh boy"]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

/home/alex/<ipython console> in <module>()

TypeError: 'tuple' object does not support item assignment

>>> a
[3] (['boy oh boy'], 2)

This basically works because
code:
a[0] += ["boy oh boy"]
is essentially
code:
a[0] = a[0].__iadd__(["boy oh boy"])
__iadd__ modifies self, and then returns self. The end result is that the __iadd__ call succeeds, but the assignment back to a[0] fails. In this case the assignment back to a[0] is extraneous because list is mutable, but if the list was not a mutable data structure then you would need the assignment to succeed as well.

Gothmog1065
May 14, 2009
Is there a way to run a python program in some sort of debugger step by step to see what exactly is executing?

Reformed Pissboy
Nov 6, 2003

Gothmog1065 posted:

Is there a way to run a python program in some sort of debugger step by step to see what exactly is executing?

If you're still using IDLE, it has a nice little built-in debugger in the Debug menu.

Gothmog1065
May 14, 2009
How do you check for another version of python on your computer? Add/remove program only shows 3.2, and I only see the folder for 3.2 on the root directory. Here's what's going on:

pre:
#Hangman Word Game

import random

HANGMAN = ( #The Hanged Man.
"""
<<insert 8 hangman pictures here as a tuple>>
""")

WRONGLIMIT = len(HANGMAN)
WORDS = ("OVERUSED", "CLAM", "GUAM", "TAFFETA", "PYTHON", "COMPUTER")

print("""
Welcome to the Hangman Game!

You have""", WRONGLIMIT, """tries to guess the letters in the word!
Please only use letters in your guesses!
""")

while True:
    word = random.choice(WORDS)
    tries = 0
    guess = input("Please give your first guess: ")
    guess = guess.upper()
    used = []
    guessedWord = "-" * len(word)

    while tries < WRONGLIMIT and guessedWord != word:

        while guess in used:
            print("You have already used the letter", guess)
            guess = input("Please enter another letter: ")
            guess = guess.upper()

        used.append(guess)

        if guess in word:
            print("Yes,", guess," is in the word!")

            new = ""
            for i in range(len(word)):
                if guess == word[i]:
                    new += guess
                else:
                    new += guessedWord[i]

            guessedWord = new
            print("Please enter a new letter (Current Word:", guessedWord,"): ")
            guess = input("")
            guess = guess.upper()
            
            
        else:
            print("That is an incorrect guess!\n")
            print(HANGMAN[tries])
            print("\n\nThe word so far:", guessedWord, "\nYou have used" \
                  " these letters so far:\n", used)
            tries += 1

    if guessedWord == word:
        print("Congratulations! You have guessed the word, ", word, "!")
    else:
        print("You were hanged and lose! The word was", word, "\n")

    cont = input("Would you like to contune? Y/N:")

    if cont.lower() == "n" or cont.lower() == "no":
        break
Being run through the console comes up with this:

pre:
Welcome to the Hangman Game!

You have 8 tries to guess the letters in the word!
Please only use letters in your guesses!

Please give your first guess: h
That is an incorrect guess!


 ------
 |    |
 |
 |
 |
 |
 |
 |
 |
----------



The word so far: --------
You have used these letters so far:
 ['H\r']
You have already used the letter H
Please enter another letter:
It runs perfectly in IDlE, as I said, I've never run another version of Python on this computer. This is probably related to the problem I was having earlier, now I have some actual poo poo I can copy.

fischtick
Jul 9, 2001

CORGO, THE DESTROYER

Fun Shoe

Gothmog1065 posted:

['H\r']

Looks like it's a known bug in python 3.2: http://bugs.python.org/issue11272
In the meantime you can clean up the guess on your own:
code:
    guess = input("Please give your first guess: ")
    guess = guess.strip().upper()

Gothmog1065 posted:

Is there a way to run a python program in some sort of debugger step by step to see what exactly is executing?

Have you looked at pdb? Put this near where you want to break into debug mode:
code:
import pdb; pdb.set_trace()
It's an awful lot like gdb once you're in the debug shell.

Gothmog1065
May 14, 2009

fischtick posted:

Looks like it's a known bug in python 3.2: http://bugs.python.org/issue11272
Yeah, I was going to random rear end forums and I finally ran across a guy who said that, I was going to test today when I got to work to be sure. I'll roll back to 3.1.3 in the meantime.

quote:

In the meantime you can clean up the guess on your own:
code:
    guess = input("Please give your first guess: ")
    guess = guess.strip().upper()
I'm assuming this will simply strip any carriage returns or newlines from the input?


quote:

Have you looked at pdb? Put this near where you want to break into debug mode:
code:
import pdb; pdb.set_trace()
It's an awful lot like gdb once you're in the debug shell.
I'll look into that when I get to work. That will really help when I'm making some really retarded mistakes that I can't normally see. I finally figured out the IDLE debugger as well.

Captain Capacitor
Jan 21, 2008

The code you say?
I know a lot of folks don't like it, but I've always been a fan of Pydev's debugger.

Rohaq
Aug 11, 2006

VirtuaSpy posted:

For you folks using or interested in using Python for scientific computing.

A great lecture by Fernando Perez on Scientific Computing Using Python. Covers NumPy, SciPy, SAGE, matplotlib, Mayavi, and more:

http://www.youtube.com/watch?v=1j_HxD4iLn8
Fantastic, I was just looking for something about Python/NumPy/SciPy/matplotlib for my dissertation. Thank you!

Mopp
Oct 29, 2004

I'm new to Python, and I'm trying to use pyserial to get data from a serial port and display it on a realtime graph. The graph works fine, but I'm having trouble with this code I tried adjusting for my purposes. It's supposed to read numeric data from the serial port and save it, but I can't even start it. The only thing that happens is that it writes "100" repeated 500 times.

Here is the code:
code:
"""
Listen to serial, return most recent numeric values
"""
from threading import Thread
import time
import serial

last_received = ''
def receiving(ser):
    global last_received
    buffer = ''
    while True:
        buffer = buffer + ser.read(ser.inWaiting())
        if '\n' in buffer:
            lines = buffer.split('\n') # Guaranteed to have at least 2 entries
            last_received = lines[-2]
            #If the Arduino sends lots of empty lines, you'll lose the
            #last filled line, so you could make the above statement conditional
            #like so: if lines[-2]: last_received = lines[-2]
            buffer = lines[-1]


class SerialData(object):
    def __init__(self, init=50):
        try:
            self.ser = ser = serial.Serial(
                port='com4',
                baudrate=9600,
                bytesize=serial.EIGHTBITS,
                parity=serial.PARITY_NONE,
                stopbits=serial.STOPBITS_ONE,
                timeout=0.1,
                xonxoff=0,
                rtscts=0,
                interCharTimeout=None
            )
        except serial.serialutil.SerialException:
            #no serial connection
            self.ser = None
        else:
            Thread(target=receiving, args=(self.ser,)).start()
        
    def next(self):
        if not self.ser:
            return 100 #return anything so we can test when Arduino isn't connected
        #return a float value or try a few times until we get one
        for i in range(40):
            raw_line = last_received
            try:
                return float(raw_line.strip())
            except ValueError:
                print 'bogus data',raw_line
                time.sleep(.005)
        return 0.
    def __del__(self):
        if self.ser:
            self.ser.close()

if __name__=='__main__':
    s = SerialData()
    for i in range(500):
        time.sleep(.015)
        print s.next()
I don't understand everything in this code, and can't find where it fails. Could anyone help me?

BeefofAges
Jun 5, 2004

Cry 'Havoc!', and let slip the cows of war.

Sounds like it's just running

code:
def next(self):
        if not self.ser:
            return 100 #return anything so we can test when Arduino isn't connected
Are you sure your serial port connection is working? I'm suspicious of port='com4'. I'll have to check my own pyserial code, but I think you need to give it an integer port number, not a string.

Mopp
Oct 29, 2004

BeefofAges posted:

Sounds like it's just running

code:
def next(self):
        if not self.ser:
            return 100 #return anything so we can test when Arduino isn't connected
Are you sure your serial port connection is working? I'm suspicious of port='com4'. I'll have to check my own pyserial code, but I think you need to give it an integer port number, not a string.

the port I'm actually trying to access is
code:
        try:
            self.ser = ser = serial.Serial(
			port='/dev/tty.FireFly-16CB-SPP',
			baudrate=115200,
			stopbits=serial.STOPBITS_ONE,
			bytesize=serial.EIGHTBITS
			)
which works just fine in another program, so I don't think that's the error. Besides, the other program takes some time to open the port in the beginning of the program (it's a bluetooth modem), while this just spews out "100". I can change the value of "return 100" to something else but that doesn't change the printed value, so I don't think it's that either.

FoiledAgain
May 6, 2007

I'm using Pylab to draw some figures, and I'm having trouble, mostly because I hardly ever deal with graphics and I don't really understand everything that's going on.
The y-axis goes from 0-2500 for each of my plots, but it gets squashed to fit within a screen and I can't see the details very well. How do I extend the size of the plot/change the scaling on the y-axis? This is probably really easy, and I tried reading though documentation, but as I said I'm not familiar with pylab or graphic at all and I'm quickly lost.

This is the relevant code I'm working with:

code:
for j in range(num_files+1):
    filename = ''.join([path,'node_',str(j),'output_test.txt'])
    with open(filename,'r') as f:
        lines = [line.rstrip('\n') for line in f]
   
    pylab.cla()
    pylab.plot([y for y in range(len(lines))],lines,'b')
edit: I'm also not committed to using pylab, if someone has a better suggestion for drawing figures

vvvv Thanks! That's perfect.

FoiledAgain fucked around with this message at 20:29 on Mar 30, 2011

Modern Pragmatist
Aug 20, 2008

FoiledAgain posted:

I'm using Pylab to draw some figures, and I'm having trouble, mostly because I hardly ever deal with graphics and I don't really understand everything that's going on.

pylab.ylim

N.Z.'s Champion
Jun 8, 2003

Yam Slacker
Python projects go in this thread right? For the last few months I've been busy porting Docvert from PHP to Python and what the software does is convert Office files to Docbook and clean HTML, lists are made hierarchical , any vector diagrams are converted to SVG/PNG, and so on. It needs pyuno/libreoffice but the conversion from OpenDocument to DocBook and HTML is done in Docvert.

N.Z.'s Champion fucked around with this message at 23:48 on Mar 30, 2011

Hughmoris
Apr 21, 2007
Let's go to the abyss!
Ok, I am stumped. I want this function to take a value, search through a text file of dictionary words and tell me if the value is a word.
code:
def dictLook(word):
        f = open('C:/Python32/programs/dictionary.txt', 'r')
        for line in f:
                if word in line:
                        print(word + ' is a word.')
                        break
        f.close()
If i run: dictLook('oklo')
it spits out that it is a word. What am I missing?

Here is the list of words I use:
http://download.oracle.com/javase/tutorial/collections/interfaces/examples/dictionary.txt

*I think I might have stumbled upon the issue. 'oklo' is in the word 'booklore', so maybe it registers that. Is there a way to make it match word specifically and not register false words like that?

Hughmoris fucked around with this message at 05:02 on Mar 31, 2011

FoiledAgain
May 6, 2007

Hughmoris posted:

*I think I might have stumbled upon the issue. 'oklo' is in the word 'booklore', so maybe it registers that. Is there a way to make it match word specifically and not register false words like that?

Since each line is only a single word, you could change your if statement to "if word == line". You could also try something more complicated with regular expressions.

Adbot
ADBOT LOVES YOU

Hughmoris
Apr 21, 2007
Let's go to the abyss!

FoiledAgain posted:

Since each line is only a single word, you could change your if statement to "if word == line". You could also try something more complicated with regular expressions.

Hmm. I tried that but it doesn't want to work. Nothing registers as a word. I've looked at regular expressions but it confuses the hell out of me.

  • Locked thread