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
tripwire
Nov 19, 2004

        ghost flow

Centipeed posted:

I am trying to wrap my head around how to make this more elegant. I can't think of anything:

code:
for line in currentmap:
        for char in line:
            c.text(x, y, char)
            x = x + 1
        x = 0
        y = y + 1
c.test is a Console method, which is similar to curses but for Windows. currentmap is a 2D list containing a 5x5 grid of characters:

code:
#####
#...#
#...#
#...#
#####
It works, currently, but is there any way of doing this more elegantly? x and y are needed by c.text as co-ordinates.

Edit: Also I'm currently using

code:
currentmap = [[char for char in line] for line in file]
to get the text file into the 2D list. This, however, includes newline characters, which I don't want in the list. Is there anyway to stop these getting in?
I'm a little unclear about what you are asking, but if you are looking for a more idiomatic way of doing a 2d nested loop like that consider this instead:

code:
for y, line in enumerate( currentmap ):
        for x, char in enumerate( line ):
            c.text(x, y, char)
For your second question, python list comprehensions allow you to put a trailing if statement in to filter elements, so if you want to prevent line breaks from getting into your list, you can do so like this:

code:
characters_we_want_to_ignore = [ '\n', '\r' ]
currentmap = [[char for char in line if char not in characters_we_want_to_ignore ] for line in file]

Adbot
ADBOT LOVES YOU

ahobday
Apr 19, 2007

tripwire posted:

I'm a little unclear about what you are asking, but if you are looking for a more idiomatic way of doing a 2d nested loop like that consider this instead:

code:
for y, line in enumerate( currentmap ):
        for x, char in enumerate( line ):
            c.text(x, y, char)
For your second question, python list comprehensions allow you to put a trailing if statement in to filter elements, so if you want to prevent line breaks from getting into your list, you can do so like this:

code:
characters_we_want_to_ignore = [ '\n', '\r' ]
currentmap = [[char for char in line if char not in characters_we_want_to_ignore ] for line in file]

Perfect. Ever since I started using Python's self-iterating for loops I've wanted to avoid creating loop variables that I iterate myself. Also, the newline solution is great.

I've run into another problem. What would make Python (Under Windows) instantly quit back to the command line without showing the compiler errors it produces? If I repeatedly run the code I can see flashes of a compiler error, but it instantly takes me back to the command prompt as if nothing had happened.

tripwire
Nov 19, 2004

        ghost flow
I'm not too sure, maybe you could try running your script a line a time with pdb until you see whats being printed out. The documentation is here: http://docs.python.org/library/pdb.html

ahobday
Apr 19, 2007

tripwire posted:

I'm not too sure, maybe you could try running your script a line a time with pdb until you see whats being printed out. The documentation is here: http://docs.python.org/library/pdb.html

I've been trying that, but as soon as my program creates a new instance of Console, pdb stops doing anything. This console module will be the death of me, I'm sure.

bitprophet
Jul 22, 2004
Taco Defender

Centipeed posted:

I've been trying that, but as soon as my program creates a new instance of Console, pdb stops doing anything. This console module will be the death of me, I'm sure.

Yea, anything mucking with the terminal window's likely to mess you up.

Maybe wrap your whole deal in a try/except and log to file?
code:
try:
    do_shit_with_Console()
except KeyboardInterrupt:
    raise
except Exception, e:
    with open('/path/to/log', 'a') as log:
        log.write("%s\n" % e)
    raise
Something like that, anyway. I have KeyboardInterrupt in its own clause because it's generally a bad idea to do anything but allow it to raise to the top, else you can't Ctrl-C out of your script at all.

Lonely Wolf
Jan 20, 2003

Will hawk false idols for heaps and heaps of dough.
Maybe you could try something, which I normally wouldn't suggest, like this:
code:
def error_trap():
  try:
    mainLoop() #all the Console stuff
  except: #this is the part I'd normally howl at
    #print stacktrace and exit
I'd recommend wrapping it in a function like that so you don't have to strip that nonsense off when you're done debugging.

edit: Beaten. That's what I get for typing this while watching TV. Good point about ctrl+c, that extra clause gets rid of my concerns.

Lonely Wolf fucked around with this message at 18:34 on Mar 9, 2009

king_kilr
May 25, 2007
For those doing concurrency stuff I implemented futures in Python earlier today for those who may be interested: http://dpaste.com/11007/

m0nk3yz
Mar 13, 2002

Behold the power of cheese!

king_kilr posted:

For those doing concurrency stuff I implemented futures in Python earlier today for those who may be interested: http://dpaste.com/11007/

I still think this is neat :)

ATLbeer
Sep 26, 2004
Über nerd

king_kilr posted:

For those doing concurrency stuff I implemented futures in Python earlier today for those who may be interested: http://dpaste.com/11007/

So quick question on program flow here

code:
g = Thread(f, args=(1,)).run()
h = Thread(f, args=(2,)).run()
print h + 3
print h
Obviously thread g and h both are running concurrently but, why doesn't Python throw a name error when referencing h in line 3. It obviously is waiting for thread h to return the value to h but, how does Python know to "wait" for h to have a value before evaluating that statement. There doesn't appear to be a control structure to stop that evaluation from happening?

edit: It's late and I'm tired... So in case my question doesn't make sense... Why isn't it written like this
code:
h = None
h = Thread(f, args=(2,)).run()
while h is None:
 pass
print h+3
Obviously this example defeats the purpose of concurrency but, I'm confused as to why (or how) Python knows to wait or ignore the next statement till the previous thread is finished without the "while h is None"

ATLbeer fucked around with this message at 06:25 on Mar 10, 2009

Pooball
Sep 21, 2005
Warm and squishy.
ATLbeer, Thread.run() returns a Future object (see line 44). This is an object that proxies the real value.

I think line 43 should use "start" rather than "run" - run() just calls the handler synchronously.

king_kilr
May 25, 2007

Pooball posted:

ATLbeer, Thread.run() returns a Future object (see line 44). This is an object that proxies the real value.

I think line 43 should use "start" rather than "run" - run() just calls the handler synchronously.

To expand on this(because this was my revelation into how to do this), in Python everything about an object is in it's attributes. What does it mean for an object to be an integer? It means it acts like a number, we can display it, we can add it to other numbers, etc. Well in Python all it means to display something is for it to have an __str__ method, or to add an __add__ method. All being an integer means is that it walks, talks, and quacks like one.

ndrz
Oct 31, 2003

I am quite new to programming with Python, and even more so with Qt, so I figure I should ask this question.

I'm writing a program that scans a directory, and queries all of the folder names in that directory against IMDB (with imdbpy), stores the information in a sqlite database, and lets the user browse that database.

Here's a screenshot, for example:


All the functionality works, and the program is very fast - the problem is memory usage. For a database with 200 movies, the program uses about 120mb. I've narrowed down the problem: if I don't display the cover in the movie list, memory usage shoots down to about 40mb. For reference, the size of the folder that contains the cover images is 6mb.

Here's how I'm currently implementing this:
code:
for row, value in enumerate(tableInfo):
    pic = QtGui.QPixmap("covers\\" + value[3] + ".jpg")
    self.lblCover = QtGui.QLabel()            
    self.lblCover.setScaledContents(True)
    self.lblCover.setPixmap(pic)
    self.ui.tblMovies.setCellWidget(row, 0, self.lblCover)
I know that instead of using a QLabel, I can use an icon, like so:

code:
self.ui.tblMovies.setItem(row, 1, QtGui.QTableWidgetItem(icon, value[0]))
However, besides still having to generate the QPixmap and then a QIcon from that, this doesn't scale the contents of the image, and as they have different x/y ratios, the titles don't line up. (I'm fine with the distortion of the aspect ratio since the picture is so small)

So, without berating me too badly, how can I improve this?

bollig
Apr 7, 2006

Never Forget.
real noob question here:
I'm doing the exercises in "How to think like a computer scientist", and I'm stuck on one of the exercises for matrices. Here's what i have:

[code]
def add_column(matrix):
"""
>>> m = [[0, 0], [0, 0]]
>>> add_column(m)
[[0, 0, 0], [0, 0, 0]]
>>> n = [[3, 2], [5, 1], [4, 7]]
>>> add_column(n)
[[3, 2, 0], [5, 1, 0], [4, 7, 0]]
>>> n
[[3, 2], [5, 1], [4, 7]]
"""
last_matrix = matrix[:]
for index, list in enumerate(last_matrix):
list[:len(list)] += [0]
print last_matrix
print matrix
return last_matrix
[code]

there's also a doctest at the end. Basically it fails the last test, where I have to keep it a pure function. I thought I had done this by declaring last_matrix by splitting it. I've been loving around with this for like 3 hours now, could you guys help me out. The indents are all correct, i cant get it to translate with the BB code.

ChiralCondensate
Nov 13, 2007

what is that man doing to his colour palette?
Grimey Drawer

bollig posted:

last_matrix = matrix[:]
The copy-by-slice trick only copies the top list. You need to do a deep copy; copy.deepcopy will do it for you, or you can do it in the same go as your column-adding:
code:
new_matrix = []
for row in matrix:
   new_matrix.append(row + [0])

bollig
Apr 7, 2006

Never Forget.

ChiralCondensate posted:

The copy-by-slice trick only copies the top list. You need to do a deep copy; copy.deepcopy will do it for you, or you can do it in the same go as your column-adding:
code:
new_matrix = []
for row in matrix:
   new_matrix.append(row + [0])

From what I've gathered, the slice does work. The only problem is that it doesn't pass the last test. Which doesn't make sense to me. Also, I'm really trying to do this as explicitly as possible, and I don't really get what's going on with the .append deal. I could very well be mistaken, but the new_matrix prints out the same as the matrix, but has a different id, which to me means that the slice worked.

Lonely Wolf
Jan 20, 2003

Will hawk false idols for heaps and heaps of dough.
the new matrix has a different id but check the ids of its elements vs the ids of the old matrix's elements to see what what was said.

ChiralCondensate
Nov 13, 2007

what is that man doing to his colour palette?
Grimey Drawer

bollig posted:

From what I've gathered, the slice does work. The only problem is that it doesn't pass the last test. Which doesn't make sense to me. Also, I'm really trying to do this as explicitly as possible, and I don't really get what's going on with the .append deal. I could very well be mistaken, but the new_matrix prints out the same as the matrix, but has a different id, which to me means that the slice worked.
Yes, you do indeed have a new list object, but its elements (your row lists) are the exact same lists as the old:
code:
>>> x = [[1],[2]]
>>> x
[[1], [2]]
>>> y = x[:]
>>> y
[[1], [2]]
>>> y[0][0]=55
>>> y
[[55], [2]]
>>> x
[[55], [2]]
>>> id(y)
11092536
>>> id(x)
11087784
>>> id(y[0])
11073296
>>> id(x[0])
11073296

Zombywuf
Mar 29, 2008

I'm trying to talk to an MS SQL database from python on a Linux box. I was using python-mssql which worked fine until the DB tried to return nVarChar (unicode) data, whereupon I saw this error:

quote:

internal error: SQL Server message 4004, severity 16, state 1, procedure uspWebPub_GetTrackingInfo, line 19:
Unicode data in a Unicode-only collation or ntext data cannot be sent to clients using DB-Library (such as ISQL) or ODBC version 3.7 or earlier.
DB-Lib error message 4004, severity 16:
General SQL Server error: Check messages from the SQL Server
The author of the mssql library appears to be Japanese, does everyone use ASCII in Japan?

Anyway, anyone got any clues as to how to get unicode data out of MS SQL Server on a Linux box?

EDIT: Found the solution, as usual immediately after posting, http://use.perl.org/~jdavidb/journal/37102. Perl to the rescue once again.

Zombywuf fucked around with this message at 11:17 on Mar 13, 2009

tef
May 30, 2004

-> some l-system crap ->
I used pyodbc last time instead of pymssql, with a little more success.

http://code.google.com/p/pyodbc/


I dunno maybe there's still some working examples from the last time.

bollig
Apr 7, 2006

Never Forget.

ChiralCondensate posted:

Yes, you do indeed have a new list object, but its elements (your row lists) are the exact same lists as the old:
code:
>>> x = [[1],[2]]
>>> x
[[1], [2]]
>>> y = x[:]
>>> y
[[1], [2]]
>>> y[0][0]=55
>>> y
[[55], [2]]
>>> x
[[55], [2]]
>>> id(y)
11092536
>>> id(x)
11087784
>>> id(y[0])
11073296
>>> id(x[0])
11073296

Aha, thanks. I got it now.

king_kilr
May 25, 2007

tef posted:

I used pyodbc last time instead of pymssql, with a little more success.

http://code.google.com/p/pyodbc/


I dunno maybe there's still some working examples from the last time.

I think that's more likely to work, I know the django-pyodbc guys prefer it to -mssql and django uses unicode everywhere so I'm sure it does as well.

ATLbeer
Sep 26, 2004
Über nerd
On the hunt for a Python package before I go reinvent the wheel

I'm looking for a PubSub style messaging system that I can pass objects (like in Pyro) for the messages

I basically have a process that is monitoring a data feed (IRC channel) and I need the resulting parsed data to be sent to any application that "subscribes" to the data feed. This data is real time data so a polling system is unusable and I'd like to be able to attach different applications to this data stream.

m0nk3yz
Mar 13, 2002

Behold the power of cheese!

ATLbeer posted:

On the hunt for a Python package before I go reinvent the wheel

I'm looking for a PubSub style messaging system that I can pass objects (like in Pyro) for the messages

I basically have a process that is monitoring a data feed (IRC channel) and I need the resulting parsed data to be sent to any application that "subscribes" to the data feed. This data is real time data so a polling system is unusable and I'd like to be able to attach different applications to this data stream.

This is similar to a JavaSpace/JMS (for Java) - Off the top of my head, does then Event server stuff in pyro get you close to what you want? http://pyro.sourceforge.net/manual/6-eventserver.html

Otherwise, I got nothin - all the message-queue stuff out lately is cross language and polling based. I just had some thoughts about writing one in cython that just shot pickled objects around.

ATLbeer
Sep 26, 2004
Über nerd

m0nk3yz posted:

This is similar to a JavaSpace/JMS (for Java) - Off the top of my head, does then Event server stuff in pyro get you close to what you want? http://pyro.sourceforge.net/manual/6-eventserver.html

Otherwise, I got nothin - all the message-queue stuff out lately is cross language and polling based. I just had some thoughts about writing one in cython that just shot pickled objects around.

Eh... I haven't touched Java in years and wanted a pure Python solution. I have a bit more work on some other stuff. If there isn't anything out there I just write my own. Publishing pickled objects is basically what I'm looking for.

Now that I read your link... I might play with that for a while this weekend

m0nk3yz
Mar 13, 2002

Behold the power of cheese!

ATLbeer posted:

Eh... I haven't touched Java in years and wanted a pure Python solution. I have a bit more work on some other stuff. If there isn't anything out there I just write my own. Publishing pickled objects is basically what I'm looking for.

Now that I read your link... I might play with that for a while this weekend

Let me know if you want to put one together, I might be able to help out/contribute.

Scaevolus
Apr 16, 2007

ATLbeer posted:

On the hunt for a Python package before I go reinvent the wheel

I'm looking for a PubSub style messaging system that I can pass objects (like in Pyro) for the messages

I basically have a process that is monitoring a data feed (IRC channel) and I need the resulting parsed data to be sent to any application that "subscribes" to the data feed. This data is real time data so a polling system is unusable and I'd like to be able to attach different applications to this data stream.

Can't Twisted do something like this?

tef
May 30, 2004

-> some l-system crap ->
Does it have to be in python? There are good messaging systems that python can use.

For example: http://www.rabbitmq.com/

Kire
Aug 25, 2006

king_kilr posted:

For those doing concurrency stuff I implemented futures in Python earlier today for those who may be interested: http://dpaste.com/11007/

I have a question about the test() function:

code:
def test():
    def f(i):
        time.sleep(i)
        print "%s -- done" % i
        return i
    g = Thread(f, args=(1,)).run()
    h = Thread(f, args=(2,)).run()
    print h + 3
    print h
Where does it get i from? Doesn't one need to use def test(i) to be able to give f the i value by calling test(i)?

bitprophet
Jul 22, 2004
Taco Defender

Kire posted:

I have a question about the test() function:

code:
def test():
    def f(i):
        time.sleep(i)
        print "%s -- done" % i
        return i
    g = Thread(f, args=(1,)).run()
    h = Thread(f, args=(2,)).run()
    print h + 3
    print h
Where does it get i from? Doesn't one need to use def test(i) to be able to give f the i value by calling test(i)?

No, because with the def statement (wherever it's used, regardless of scope) the argument names aren't existing variables -- you're declaring them right there for reference inside the def block.

Just think about it for a second :) it's defining a function, not calling it!

Kire
Aug 25, 2006
I need to install wxPython on my winxp machine at work, but I cannot get administrative access which is required for the normal .exe installs. I thought wxPython would just be a package or module I could put into my Lib folder, but it looks more complicated than that. Or is it? Can I just pull all the .py files from the .tar and put them into the correct subdirectories, all of it inside /Lib? Will that let me import wxpython?

tef
May 30, 2004

-> some l-system crap ->
Can anyone explain this?

code:
>>> def foo(x):
...     return not(x)+1
... 
>>> foo(1)
False
>>> 
>>> def foo(x):
...     return (not(x))+1
... 
>>> foo(1)
1
>>> 
A little look at the bytecode is a little more revealing:

code:
>>> dis.dis(foo)
  2           0 LOAD_FAST                0 (x)
              3 LOAD_CONST               1 (1)
              6 BINARY_ADD          
              7 UNARY_NOT           
              8 RETURN_VALUE    

>>> dis.dis(foo)
  2           0 LOAD_FAST                0 (x)
              3 UNARY_NOT           
              4 LOAD_CONST               1 (1)
              7 BINARY_ADD          
              8 RETURN_VALUE        
It seems this is translating not(x)+1 to not(x+1)

tef
May 30, 2004

-> some l-system crap ->
Ooooor I should be using not x and not not(x)

tef fucked around with this message at 01:27 on Mar 21, 2009

bitprophet
Jul 22, 2004
Taco Defender

tef posted:

Ooooor I should be using not x and not not(x)

I wasn't aware not() was even valid syntax :psyduck: It's not listed as a builtin function; the grammar for it in http://docs.python.org/reference/expressions.html#boolean-operations doesn't mention any parentheses; but yet the paragraphs below the grammar in that link, describe 'not' as if it were a function.

Oh god, it's even worse, you can do 1.and(2). What is this poo poo, Ruby?

World turning upside down...:gonk:

VVVV I understand now, that makes a little sense. Still disturbed that I went so long not realizing how much one could twist Python's syntax :v:

bitprophet fucked around with this message at 22:32 on Mar 21, 2009

tef
May 30, 2004

-> some l-system crap ->
not(x) is parsed as not (x)

so 1.and(2) is 1.0 and (2)

fnordcircle
Jul 7, 2004

PTUI
I'm working out of a python book that I burned about 75% of the way through, but I'm still feeling really, really hazy on some of the concepts and as many times as I've reread the big about classes my eyes still cross. I think I'd learn better from doing than reading, but powering through and moving on, trying to decipher the source code in this book it isn't getting any easier.

Does anyone have a suggestion for a book/online tutorial about python's implementation of classes so I can try to get a better handle on this?

king_kilr
May 25, 2007

fnordcircle posted:

I'm working out of a python book that I burned about 75% of the way through, but I'm still feeling really, really hazy on some of the concepts and as many times as I've reread the big about classes my eyes still cross. I think I'd learn better from doing than reading, but powering through and moving on, trying to decipher the source code in this book it isn't getting any easier.

Does anyone have a suggestion for a book/online tutorial about python's implementation of classes so I can try to get a better handle on this?

checkout dive into python, it's a free online python book

Scaevolus
Apr 16, 2007

fnordcircle posted:

I'm working out of a python book that I burned about 75% of the way through, but I'm still feeling really, really hazy on some of the concepts and as many times as I've reread the big about classes my eyes still cross. I think I'd learn better from doing than reading, but powering through and moving on, trying to decipher the source code in this book it isn't getting any easier.

Does anyone have a suggestion for a book/online tutorial about python's implementation of classes so I can try to get a better handle on this?

The official tutorial tends to be both more concise and more thorough than most Python books.

http://docs.python.org/tutorial/classes.html

Scaevolus fucked around with this message at 09:58 on Mar 22, 2009

Kire
Aug 25, 2006
I'm using IDLE as my IDE, and I'm not sure if that is related to a problem I have with Tkinter. Any time I use self.quit to create a button which will "quit" the Tkinter window/program, it freezes. If I close Tkinter windows by clicking on the X in the top right they go away just fine, but I always have to force-quit a self-quitted application. What is going on?

Also, is there any preference among you gurus between wxPython and Tkinter? (I can't use wxPython at work because I can't find a way to install the source files without needing admin rights on my winxp box.)

Kire fucked around with this message at 20:15 on Mar 25, 2009

king_kilr
May 25, 2007
For those who are interested in VM work or just want their Python faster this seems to be an intersting project: http://code.google.com/p/unladen-swallow/

Adbot
ADBOT LOVES YOU

m0nk3yz
Mar 13, 2002

Behold the power of cheese!

king_kilr posted:

For those who are interested in VM work or just want their Python faster this seems to be an intersting project: http://code.google.com/p/unladen-swallow/

Interesting? It's pure awesome and win. I spoked to a few people about it today, and it's very very real. I could see this supplanting CPython-trunk quickly should they hit the goals for this year.

  • Locked thread