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
mystes
May 31, 2006

Oh, I didn't realize that ElementTree was part of the standard library now. I guess it's bad to use regular expressions to parse xml like this, but I figured it wasn't worth installing a new module just for this. Never mind then.

Edit:
vvvvv Personally, that's the point where I would start to just use regular expressions and hope nobody notices.

mystes fucked around with this message at 23:50 on Apr 14, 2009

Adbot
ADBOT LOVES YOU

TOO SCSI FOR MY CAT
Oct 12, 2008

this is what happens when you take UI design away from engineers and give it to a bunch of hipster art student "designers"

mystes posted:

Oh, I didn't realize that ElementTree was part of the standard library now. I guess it's bad to use regular expressions to parse xml like this, but I figured it wasn't worth installing a new module just for this. Never mind then.

If elementtree isn't available, then use the DOM implementation -- it's really ugly, since it's basically just a port of the Java API, but still usable.

blitrig
Jul 2, 2003

Janin posted:

http://docs.python.org/library/xml.etree.elementtree.html

Parse the text into a document node, then use:

set(e.text for e in doc.findall('name'))

I don't have Python installed on this system, so this is untested :shobon:


No, never do this

Awesome, thanks. This worked perfectly!

Kibbles n Shits
Apr 8, 2006

burgerpug.png


Fun Shoe
Let me preface this post by saying I am a complete nooblet when it comes to programming, so don't laugh too hard. I am looking for some feedback on this little bit of code I wrote. It creates a 2-D grid that I am going to use with a battleship game I am programming for practice.

code:
Grid = [] #clear grid
        row = 0
        column = 0
        iRow = int(raw_input("\nEnter the width of the grid: "))
        iCol = int(raw_input("\nEnter the height of the grid: "))
        iMax = iRow * iCol #Calculated total number of cells
    
        for i in range(iMax): #Repeat this block until the total number of grid cells have been created
        
            new = grid(row, column, 'Blank')
            Grid.append(new)
            column += 1
        
            if column > iCol - 1: #When we are done with the columns, move to next row and start back from column 0
            
                column = 0
                row += 1
here is my grid class

code:
class grid(object):

    def __init__(self, x, y, Type):
        self.x = x
        self.y = y
        self.Type = Type
This works but I am wondering if there is a more practical way to do it that I am missing? I don't want to develop a habit of writing bloated, inefficient code so if anyone can school me up it would be appreciated. :) If this isn't the right place for this post, apologies all around.

hlfrk414
Dec 31, 2008
You seem to be using lists, why not just have a 2D list?

code:
#from pprint import pprint

rows, columns = 5, 6

grid = []
for row in range(rows):
        #for each row add a list of column values
	grid.append([])
	for column in range(columns):
                #for each column, add the str 'blank'
		grid[row].append('blank')

#pprint(grid)
##[['blank', 'blank', 'blank', 'blank', 'blank', 'blank'],
## ['blank', 'blank', 'blank', 'blank', 'blank', 'blank'],
## ['blank', 'blank', 'blank', 'blank', 'blank', 'blank'],
## ['blank', 'blank', 'blank', 'blank', 'blank', 'blank'],
## ['blank', 'blank', 'blank', 'blank', 'blank', 'blank']]

##or if you want to know about list comprehensions
##this will do the same thing
#grid = [ ['blank'] * columns for row in range(rows)]

arow, acol = 0,1

grid[arow][acol] = 'red peg'

#pprint(grid)
##[['blank', 'red peg', 'blank', 'blank', 'blank', 'blank'],
## ['blank', 'blank', 'blank', 'blank', 'blank', 'blank'],
## ['blank', 'blank', 'blank', 'blank', 'blank', 'blank'],
## ['blank', 'blank', 'blank', 'blank', 'blank', 'blank'],
## ['blank', 'blank', 'blank', 'blank', 'blank', 'blank']]

print grid[arow][acol] #will print 'red peg'
You can optionally put your own objects into the grid instead of strs, but there is no need for the pieces in the grid to know their position now. Moreover, you shouldn't call your class grid when they are pieces in a grid, not a grid themselves.

BeefofAges
Jun 5, 2004

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

I don't know if anyone here is interested, but I've been working on a benchmark written entirely in Python, and I need people to mess with it and find bugs.

http://forums.somethingawful.com/showthread.php?threadid=3119056

Shaocaholica
Oct 29, 2002

Fig. 5E
So I need to search through a few hundred (500-1000) text files for a simple string (4-20 chars). I'm not counting, its basically true once I find the first occurance. This is on windows so no access to native grep. What can I do to make it somewhat fast as opposed to scanning through the file starting at the beginning?

deimos
Nov 30, 2006

Forget it man this bat is whack, it's got poobrain!

Shaocaholica posted:

So I need to search through a few hundred (500-1000) text files for a simple string (4-20 chars). I'm not counting, its basically true once I find the first occurance. This is on windows so no access to native grep. What can I do to make it somewhat fast as opposed to scanning through the file starting at the beginning?

download and install unxutils!

otherwise read and adapt effbot's widefinder

deimos fucked around with this message at 21:32 on Apr 16, 2009

nonathlon
Jul 9, 2004
And yet, somehow, now it's my fault ...

outlier posted:

I have a webpage with a form (on a Plone site for what it matters), that when submitted calls an external program to do some calculations, grabs the results and renders them on the page. Simple. (For interest, it's matching submitted biosequences to an established corpus via various specialised commandline programs.)

The problem has come about that sometimes the server is overloaded or the external program fails to complete, leaving my webserver waiting for an answer that never comes. So: what's the appropriate Pythonic idiom for handling this sort of situation? It seems reasonable to launch the external app, wait for x seconds and then conclude it has failed and return a "sorry" message to the user. Threads, job control, multiprocessing - what's the best way to it?

Odd to follow myself up but ...

The situation is that I have some 3rd party code calling a commandline program, waiting for the results, then munging them in various ways before returning them to a webservice. And when the user gets bored and terminates the request, the called commandline process seems to hang around. Curious.

Dwelling through the code, I see they have called the commandline with the subprocess module. I would reason that when owning object (that calls subprocess and creates the Popen) gets deleted, then the Popen should get deleted and delete the process - yet it's still hanging around. What gives? How can I ensure that a called process is disposed of correctly?

Sylink
Apr 17, 2004

Can anyone explain XML parsing to me? I look at XML and think its pretty simple but all the modules and tutorials I read seem extremely esoteric.

I just want to grab values from certain tags which shouldn't seem this complicated with all kinds nodes and poo poo.

nonathlon
Jul 9, 2004
And yet, somehow, now it's my fault ...

Sylink posted:

Can anyone explain XML parsing to me? I look at XML and think its pretty simple but all the modules and tutorials I read seem extremely esoteric.

I just want to grab values from certain tags which shouldn't seem this complicated with all kinds nodes and poo poo.

That's really a general programming question not a Python one but look: an XML document is a set of nested tags that can be represented as a tree. So you _have_ to think about nodes, because to get to the node you want, you need to walk to the right point in the tree. But that's no so hard, because you just start from the root and move to children, and their children as you need.

Additional free advice: use xml.etree (in Python 2.5+) or Elementtree (before that). It make handling XML easy.

Scaevolus
Apr 16, 2007

Sylink posted:

Can anyone explain XML parsing to me? I look at XML and think its pretty simple but all the modules and tutorials I read seem extremely esoteric.

I just want to grab values from certain tags which shouldn't seem this complicated with all kinds nodes and poo poo.
I felt the same a few weeks ago, but it's pretty simple once you use lxml.etree (documentation).

Here's how you read the temperature from Google's API:

The XML document is here: http://www.google.com/ig/api?weather=Denver

To print the current temperature, you can do:
code:
from lxml import etree

xml = etree.parse("http://www.google.com/ig/api?weather=Denver") 
# takes a filename or URL

print xml.find('weather/current_conditions/temp_f').get('data') 
# gets the 'data' attribute of the specified node
If you could be more specific we could give more detailed help.

Scaevolus fucked around with this message at 19:33 on Apr 17, 2009

Ferg
May 6, 2007

Lipstick Apathy
So coming from PHP I'm spoiled with the most excellent online documentation ever. Is there a wiki-like documentation site for Python anywhere that is nearly as comprehensive? I'm finding plenty of samples here and there, and the python site walks through a lot of ideas, but I'd like something that works more like reference material than step-by-step walkthroughs.

tef
May 30, 2004

-> some l-system crap ->
I know this will sound facile, but have you looked at the standard documentation for python?

http://docs.python.org/index.html

It has the complete language reference:

http://docs.python.org/reference/index.html

And the complete library reference:

http://docs.python.org/library/index.html

Ferg
May 6, 2007

Lipstick Apathy

tef posted:

I know this will sound facile, but have you looked at the standard documentation for python?

http://docs.python.org/index.html

It has the complete language reference:

http://docs.python.org/reference/index.html

And the complete library reference:

http://docs.python.org/library/index.html

Actually, somehow I missed the larger spectrum of information there. That will be a help, but I'm really more curious to know if anything community-driven (ala PHP's documentation) exists. I love the ability to look up a function on php.net then glance down to the user comments to see some developer who's done exactly what I'm looking to do and provides a code sample.

m0nk3yz
Mar 13, 2002

Behold the power of cheese!

Ferg posted:

Actually, somehow I missed the larger spectrum of information there. That will be a help, but I'm really more curious to know if anything community-driven (ala PHP's documentation) exists. I love the ability to look up a function on php.net then glance down to the user comments to see some developer who's done exactly what I'm looking to do and provides a code sample.

We're adding comments to the docs soon, I don't know when though :(

Sylink
Apr 17, 2004

So I have an interesting bug.

My programs were working fine until all of a sudden they decide not to find the module they require anymore.

If I move the source files to my desktop lets say they run fine. However, If I run one in the original directory I get an error as if all of the other py files in the directory ran too and they complain the module doesn't exist.

:wtf:

For example:

code:
Traceback (most recent call last):
  File "C:\Documents and Settings\Sylink\Desktop\Py\diggmagic\xml.py", line 1, in <module>
    from digg import *
  File "C:\Documents and Settings\Sylink\Desktop\Py\diggmagic\digg.py", line 14, in <module>
    from xml.dom import minidom
  File "C:\Documents and Settings\Sylink\Desktop\Py\diggmagic\xml.py", line 4, in <module>
    from xml.dom import minidom
ImportError: No module named dom
That is the error I get if I run xml.py, it decides to open other random poo poo then leaves pyc files in the directory.

I get the same poo poo if I run another py file diggtest.py which for some reason decides to call xml.py

code:
Traceback (most recent call last):
  File "C:\Documents and Settings\Sylink\Desktop\Py\diggmagic\xml.py", line 1, in <module>
    from digg import *
  File "C:\Documents and Settings\Sylink\Desktop\Py\diggmagic\digg.py", line 14, in <module>
    from xml.dom import minidom
  File "C:\Documents and Settings\Sylink\Desktop\Py\diggmagic\xml.py", line 4, in <module>
    from xml.dom import minidom
ImportError: No module named dom
This makes no loving sense and is annoying. If I move diggtest.py and my library digg.py to the desktop they run fine. If I open up a new command line and do from xml.dom import minidom the command works

This really is pissing me off. And these files all worked 5 minutes ago and as far as I know nothing changed. This is under python 2.5

No Safe Word
Feb 26, 2005

Sylink posted:

So I have an interesting bug.

My programs were working fine until all of a sudden they decide not to find the module they require anymore.

If I move the source files to my desktop lets say they run fine. However, If I run one in the original directory I get an error as if all of the other py files in the directory ran too and they complain the module doesn't exist.

:wtf:

For example:

code:
Traceback (most recent call last):
  File "C:\Documents and Settings\Sylink\Desktop\Py\diggmagic\xml.py", line 1, in <module>
    from digg import *
  File "C:\Documents and Settings\Sylink\Desktop\Py\diggmagic\digg.py", line 14, in <module>
    from xml.dom import minidom
  File "C:\Documents and Settings\Sylink\Desktop\Py\diggmagic\xml.py", line 4, in <module>
    from xml.dom import minidom
ImportError: No module named dom
That is the error I get if I run xml.py, it decides to open other random poo poo then leaves pyc files in the directory.

I get the same poo poo if I run another py file diggtest.py which for some reason decides to call xml.py

code:
Traceback (most recent call last):
  File "C:\Documents and Settings\Sylink\Desktop\Py\diggmagic\xml.py", line 1, in <module>
    from digg import *
  File "C:\Documents and Settings\Sylink\Desktop\Py\diggmagic\digg.py", line 14, in <module>
    from xml.dom import minidom
  File "C:\Documents and Settings\Sylink\Desktop\Py\diggmagic\xml.py", line 4, in <module>
    from xml.dom import minidom
ImportError: No module named dom
This makes no loving sense and is annoying. If I move diggtest.py and my library digg.py to the desktop they run fine. If I open up a new command line and do from xml.dom import minidom the command works

This really is pissing me off. And these files all worked 5 minutes ago and as far as I know nothing changed. This is under python 2.5
Don't name the file xml.py :)

in digg.py, xml.dom will find xml.py in it first and look for a dom module in it, the current directory is searched first in the python path

mister_gosh
May 24, 2002

I have a loop which adds entries to a dictionary like so:

code:
userDictionary[id] = [name,addr]
I then want to cycle through it later on.

What I'm expecting is to iterate through this DICTIONARY, get its LIST value, iterate through the LIST and extract the values based on their index.

Would I be better off with a multi-dimensional list here instead of a dictionary? The following code is just giving me the first and second letter of the name.

code:
for id in userDictionary.keys():
      
    name,addr = "null","null" # do these need to be instantiated?
    for item in userDictionary[id]:
        name = item[0]
        addr = item[1]
    
    print id,'\t',name,'\t',addr

Sylink
Apr 17, 2004

No Safe Word posted:

Don't name the file xml.py :)

in digg.py, xml.dom will find xml.py in it first and look for a dom module in it, the current directory is searched first in the python path

Oh god I love you, I've been swearing at my computer for the last ten minutes. My aneurysm subsided.

I'm a decent programmer but 99% of my problems turn out to be stupid/simple.

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

Sylink posted:

This makes no loving sense and is annoying. If I move diggtest.py and my library digg.py to the desktop they run fine. If I open up a new command line and do from xml.dom import minidom the command works

Look at what's happening here with the relationship between module names and filenames:

code:
[pythonbox ~] echo "from xml.dom import minidom" > x.py
[pythonbox ~] python x.py
[pythonbox ~] mv x.py xml.py
[pythonbox ~] python xml.py 
Traceback (most recent call last):
  File "xml.py", line 1, in <module>
    from xml.dom import minidom
  File "/root/xml.py", line 1, in <module>
    from xml.dom import minidom
ImportError: No module named dom

No Safe Word
Feb 26, 2005

mister_gosh posted:

I have a loop which adds entries to a dictionary like so:

code:
userDictionary[id] = [name,addr]
I then want to cycle through it later on.

What I'm expecting is to iterate through this DICTIONARY, get its LIST value, iterate through the LIST and extract the values based on their index.

Would I be better off with a multi-dimensional list here instead of a dictionary? The following code is just giving me the first and second letter of the name.

code:
for id in userDictionary.keys():
      
    name,addr = "null","null" # do these need to be instantiated?
    for item in userDictionary[id]:
        name = item[0]
        addr = item[1]
    
    print id,'\t',name,'\t',addr
1) no, you don't need to instantiate variables
2) you don't need that inner loop once you have the id, just do:

code:
name, addr = userDictionary[id]
print id,'\t',name,'\t',addr

mister_gosh
May 24, 2002

No Safe Word posted:

1) no, you don't need to instantiate variables
2) you don't need that inner loop once you have the id, just do:

code:
name, addr = userDictionary[id]
print id,'\t',name,'\t',addr

Love for python rising!

Java would never allow you to do this sort of thing, but it makes complete sense.

So if you call another .py program, would that program be aware of the variable names (labels) you initially assigned the indexes as?

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

mister_gosh posted:

Love for python rising!

Java would never allow you to do this sort of thing, but it makes complete sense.

So if you call another .py program, would that program be aware of the variable names (labels) you initially assigned the indexes as?

You would probably want to pass them as parameters to a function, not make them global to another module.

code:
>>> d = {'a':(1,2), 'b':(3,4)}    
>>> for key,val in d.items():
...     print key,val[0]     
...     # somecall(key,val)
... 
a 1
b 3

hlfrk414
Dec 31, 2008

mister_gosh posted:

Love for python rising!

Java would never allow you to do this sort of thing, but it makes complete sense.

So if you call another .py program, would that program be aware of the variable names (labels) you initially assigned the indexes as?

I'm not sure I understand what you are asking, what do you mean by call a .py program, you mean load the module? When you load a module, it's code is run, and whoever imports the module gets a module object which contains whatever the module defined.

Also, make sure you understand python scope, it is different from Java and the C family of languages. When a variable is declared in a function, it is accessible to the whole function.

code:
for i in range(3):
    j = i + 1
    print i, j

#i and j are accessible outside the loop
#they hold their last values
print i, j
If you are used to C style scoping, this feels odd. But it is no better or worse, just different. It can be quite useful, allowing you to define variables inside if statements and use them elsewhere or to be able to check the last value of a loop.

king_kilr
May 25, 2007
Screw that:

code:
var = {
    'key1': ['val1', 'val2'],
    'key2': ['val3', 'val4'],
}

for key, (val1, val2) in var.iteritems():
    print "%s: %s, %s" % (key, val1, val2)
Unpacking is amazing.

Hughmoris
Apr 21, 2007
Let's go to the abyss!
I'll start by saying I am very new to Python and programming in general, and know nothing about making a GUI.

I've decided to start a very simple project of building a ledger of some sort to keep track of my spending and balances. I would like to eventually learn how to put it into a very simple GUI. My question is if I write the program to work in a console window, is it typically easy to come back later on and write a GUI interface to interact with the already established program? Or should I just start writing the program with a GUI interface from ground zero because trying to incorporate it later would be a pain.

king_kilr
May 25, 2007
If you pay a lot of attention to the architecture of your program it should be fairly easy to add a GUI later(this is, strictly speaking, how an app with a GUI *should* be). However in practice this it isn't going to be totally easy for someone new to programming to know what to watch out for. So my advice would be write it for the shell first, and try to divide your functions into 2 groups, those that interact with the user, and those that interact with the actual representaiton of the data. If you do that you should be in a good position.

BeefofAges
Jun 5, 2004

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

I would read up on how GUIs work first, so that you have an idea of how to structure your program in a way that would be easy to add a GUI.

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

BeefofAges posted:

I would read up on how GUIs work first, so that you have an idea of how to structure your program in a way that would be easy to add a GUI.

Thanks guys. Any recommended readings?

Tomed2000
Jun 24, 2002

What's a quick way to simulate probability? If I'm given a probability of say, 0.7, I want to do something 70% of the time (and not do said thing 30% of the time)

wrok
Mar 24, 2006

by angerbotSD

Tomed2000 posted:

What's a quick way to simulate probability? If I'm given a probability of say, 0.7, I want to do something 70% of the time (and not do said thing 30% of the time)

code:
import random
myprob = 0.7
if random.random() <= myprob:
    do_the_thing()          # 70% of the time
else
    dont_do_the_thing()     # 30% of the time

Sylink
Apr 17, 2004

what is popular for python GUI usage these days? Is it still tkinter or whatever?

I'd like to make something quick and easy for windows but cross platform is fine. I just want it easy to make.

awesomepanda
Dec 26, 2005

The good life, as i concieve it, is a happy life.
hi everyone, im trying to install numpy 1.3.0 into my python version 3.1 but its not working. i have no idea what to do with the contents of the zip file. where do i put them? do they go into the python directory? but where? inside the library folder? when i tried importing numpy after copying them into the lib folder i got the following error

File "<pyshell#0>", line 1, in <module>
import numpy
File "C:\Python31\lib\numpy\__init__.py", line 117
except ImportError, e:

any ideas on what i may be able to do?

king_kilr
May 25, 2007
I'm fairly certain numpy, like most major python libraries, hasn't ported to 3k yet.

m0nk3yz
Mar 13, 2002

Behold the power of cheese!

king_kilr posted:

I'm fairly certain numpy, like most major python libraries, hasn't ported to 3k yet.

king_kilr is correct. There's a GSoC project to port it, I think

awesomepanda
Dec 26, 2005

The good life, as i concieve it, is a happy life.
hi everyone, im a hobbyist programmer and i really like what i have seen of python so far. i really want to understand the language but i am not getting that through reading through the documentation. i learn best through solving problems and working on projects. are there any challenge puzzles or problem sets out there to help facilitate one's understanding of python?

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

CrazyPanda posted:

hi everyone, im a hobbyist programmer and i really like what i have seen of python so far. i really want to understand the language but i am not getting that through reading through the documentation. i learn best through solving problems and working on projects. are there any challenge puzzles or problem sets out there to help facilitate one's understanding of python?

http://projecteuler.net/

ahobday
Apr 19, 2007

A A 2 3 5 8 K posted:

http://projecteuler.net/

I tried Project Euler when I was first getting into a programming language, but the problem is that most of the thinking you have to do (Or I had to do, at least) was mathematical rather than "How do I do this with the language I've got in front of me?". Did anyone else find this?

Adbot
ADBOT LOVES YOU

nonathlon
Jul 9, 2004
And yet, somehow, now it's my fault ...

Centipeed posted:

I tried Project Euler when I was first getting into a programming language, but the problem is that most of the thinking you have to do (Or I had to do, at least) was mathematical rather than "How do I do this with the language I've got in front of me?". Did anyone else find this?

That's fair comment - a lot of the problems are mathematical or algorithmic in nature. It's a good source of exercises, but don't feel that you have to do every problem.

  • Locked thread