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
duck monster
Dec 15, 2004

Centipeed posted:

I was looking at GUI programming with Python, but I figured I'd just ask here instead of wading around the internet trying to find an answer: Do any of the Python GUI doohickies work in the same way the visual programming languages work, like VB.net and C#? As in, you can just drag and drop GUI components and then put your code in behind the scenes, as it were?

Boa constructor is a full blown delphi/vb type system for python. Its a bit undermaintained and has some quirks however.

Its a very impressive piece of code and if the guy behind it wasnt so bad at letting other people work with his code, it'd be *the* IDE for python work bar none.

Adbot
ADBOT LOVES YOU

UnNethertrash
Jun 14, 2007
The Trashman Cometh

No Safe Word posted:

That's because:

code:
    numbers2 = numbers
    numbers2.remove(f)
Everything that does this actually removes stuff from numbers as well. Assignment on lists and other complex datatypes pretty much always makes a reference instead of a copy. Luckily, python ships with the copy module to do what you want!

It spit out the right answer in a split second. Welcome to Costco, I love you.

ATLbeer
Sep 26, 2004
Über nerd

nbv4 posted:

In Django, you can format dates in the template system by using PHP-style date format strings like so: "{{ my_date|"n-j-Y" }}" but how do you do the same thing from within python itself? I know about datetime.date.strftime() and strptime(), but those functions take a completely different date formatting "language"...

When in doubt... browse source :v:

http://code.djangoproject.com/browser/django/trunk/django/utils/dateformat.py

code:
"""
2	PHP date() style date formatting
3	See [url]http://www.php.net/date[/url] for format strings
4	
5	Usage:
6	>>> import datetime
7	>>> d = datetime.datetime.now()
8	>>> df = DateFormat(d)
9	>>> print df.format('jS F Y H:i')
10	7th October 2003 11:39
11	>>>
12	"""

a cat
Aug 8, 2003

meow.
Ok so I'm trying to write a program which basically allows me to input questions and their answers, organized by chapter/book/whatever so I can study them. Like flashcards. The problem is I'm not sure how I should be storing them. I'm guessing that using a database is the way to go instead of reading/writing from a text file, but I'm extremely new at all this programming stuff and I can't figure out how to use databases with python. I have installed MySQL but the one module I can find that supposedly lets use work with MySQL doesn't seem to work with Python 2.6. I guess I could obviously downgrade the version I'm using, but I really have no idea if I'm even walking down the right path.

Any tips?

Jo
Jan 24, 2005

:allears:
Soiled Meat

jstirrell posted:

Ok so I'm trying to write a program which basically allows me to input questions and their answers, organized by chapter/book/whatever so I can study them. Like flashcards. The problem is I'm not sure how I should be storing them. I'm guessing that using a database is the way to go instead of reading/writing from a text file, but I'm extremely new at all this programming stuff and I can't figure out how to use databases with python. I have installed MySQL but the one module I can find that supposedly lets use work with MySQL doesn't seem to work with Python 2.6. I guess I could obviously downgrade the version I'm using, but I really have no idea if I'm even walking down the right path.

Any tips?

If you have a metric fuckton of questions and answers that are being updated and changed constantly and actively, a database might be the way to go. Here, however, it seems like a better idea to go the simple route and just have a .txt file with a few special tags in it. Especially if you're just learning how to program.

Since you're just starting to program, I'd recommend building the program iteratively, rather than designing all the little chunks and putting them together. First, write something that can take input and dump it back to the screen. Second, write something that can take input and dump back everything that has been entered so far. Next write something that will take input and write it to the disk. Afterwards, write something that will take input and dump to console everything that has been written to disk and the console. When you're comfortable enough that you understand these parts, build a data structure that can hold your question and answer. Find a way to write it to and read it from the disk. When all that's done, build the interface. ( A)dd question R)eview chapter, etc )

Don't hesitate to write pseudocode on a napkin.

Startup:
Load q/a list
while( !quit )
- show menu
end while
save q/a list

Jo fucked around with this message at 18:55 on Jan 1, 2009

Scaevolus
Apr 16, 2007

jstirrell posted:

I have installed MySQL but the one module I can find that supposedly lets use work with MySQL doesn't seem to work with Python 2.6. I guess I could obviously downgrade the version I'm using, but I really have no idea if I'm even walking down the right path.

Any tips?
Look into sqlite3, it's the perfect database module for this.

chemosh6969
Jul 3, 2004

code:
cat /dev/null > /etc/professionalism

I am in fact a massive asswagon.
Do not let me touch computer.
Would using a dictionary work?

hey mom its 420
May 12, 2007

It would, but you'd have to pickle and unpickle it all the time, but sqlite3 is perfect for this kind of stuff.

chemosh6969
Jul 3, 2004

code:
cat /dev/null > /etc/professionalism

I am in fact a massive asswagon.
Do not let me touch computer.

Bonus posted:

It would, but you'd have to pickle and unpickle it all the time, but sqlite3 is perfect for this kind of stuff.

You wouldn't have to pickle if you didn't want to.

Either way the stuff has to be entered and it could just be done in the python file. You wouldn't need to save answers to a separate file.

If he didn't want everything in one file, the program and the answers together, he could have a separate python file for each book or chapter or whatever that has the questions and answers, makes a dictionary and whatever other functions he wants, and just import it from the main program.

I'd just do it all in python instead of installing a database for it.

Scaevolus
Apr 16, 2007

chemosh6969 posted:

You wouldn't have to pickle if you didn't want to.

Either way the stuff has to be entered and it could just be done in the python file. You wouldn't need to save answers to a separate file.

If he didn't want everything in one file, the program and the answers together, he could have a separate python file for each book or chapter or whatever that has the questions and answers, makes a dictionary and whatever other functions he wants, and just import it from the main program.

I'd just do it all in python instead of installing a database for it.
Writing the data into python files by hand is more painful than having a nice interface to input the pairs and then pickling or using a database. It's also not a very good learning experience, and not as extensible.

Sqlite3 is packaged by default with Python, and does not require an external database program.

Scaevolus fucked around with this message at 20:56 on Jan 2, 2009

chemosh6969
Jul 3, 2004

code:
cat /dev/null > /etc/professionalism

I am in fact a massive asswagon.
Do not let me touch computer.

Scaevolus posted:

Writing the data into python files by hand is more painful than having a nice interface to input the pairs and then pickling or using a database.

But is the data going to magic itself into a database or pickles?

One could also write a prompt that asks for the input.

Just saying.

Side note: Does Sqlite3 work with mysql or is it it's own db?

Scaevolus
Apr 16, 2007

chemosh6969 posted:

But is the data going to magic itself into a database or pickles?
It's not exactly difficult to pickle something.
code:
>>> import pickle
>>> pickle.dump({"a":"b", 1:2}, open("file.pickle", "w"))
>>> pickle.load(open("file.pickle"))
{"a":"b":, 1: 2}

quote:

Side note: Does Sqlite3 work with mysql or is it it's own db?
No, it's a different DB.

SQLite

quote:

SQLite is a software library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine. SQLite is the most widely deployed SQL database engine in the world. The source code for SQLite is in the public domain.
The developers are very active as well.

Scaevolus fucked around with this message at 21:36 on Jan 2, 2009

hey mom its 420
May 12, 2007

Basically sqlite is just a database stored within a single file and you can then run queries on that file and everything. Pretty much one of the best pieces of software around today.

chemosh6969
Jul 3, 2004

code:
cat /dev/null > /etc/professionalism

I am in fact a massive asswagon.
Do not let me touch computer.

Bonus posted:

Basically sqlite is just a database stored within a single file and you can then run queries on that file and everything. Pretty much one of the best pieces of software around today.

Looks cool for small stuff.

No Safe Word
Feb 26, 2005

chemosh6969 posted:

Looks cool for small stuff.

Works surprisingly well for larger stuff too.

SQLite owns.

Jo
Jan 24, 2005

:allears:
Soiled Meat
:shobon: At what point does SQLite not scale well? I have a webspider that throws off some 40,000+ (small) entries per page. I'd like to use SQLite, but if it chokes on that many entries...

m0nk3yz
Mar 13, 2002

Behold the power of cheese!

Jo posted:

:shobon: At what point does SQLite not scale well? I have a webspider that throws off some 40,000+ (small) entries per page. I'd like to use SQLite, but if it chokes on that many entries...

It wouldn't be too hard to write a quick little test to see if it scales the way you want it too...

hey mom its 420
May 12, 2007

Why are you storing 40k entries per page?

Jo
Jan 24, 2005

:allears:
Soiled Meat

Bonus posted:

Why are you storing 40k entries per page?

Perceptual image hashing. ;)

40k might be a bit of an exaggeration, as several entries will be discarded, but it's entirely possible for a single image to generate 30-3000 entries.

tayl0r
Oct 3, 2002
This post brought to you by SOE
I've been searching on google for a bit and I can't find any python methods or libraries for doing XML encoding.

Does anything like that exist?

tayl0r
Oct 3, 2002
This post brought to you by SOE

tayl0r posted:

I've been searching on google for a bit and I can't find any python methods or libraries for doing XML encoding.

Does anything like that exist?

Well of course after I posted to this thread I found what I was looking for. http://docs.python.org/library/markup.html


Edit: Hmm.. still trying to find something that *writes* XML

Edit 2: ahh ha! http://docs.python.org/library/xml.sax.utils.html

tayl0r fucked around with this message at 09:50 on Jan 3, 2009

m0nk3yz
Mar 13, 2002

Behold the power of cheese!

tayl0r posted:

Well of course after I posted to this thread I found what I was looking for. http://docs.python.org/library/markup.html


Edit: Hmm.. still trying to find something that *writes* XML

Edit 2: ahh ha! http://docs.python.org/library/xml.sax.utils.html

elementtree does writing as well

dwayne_dibbley
Nov 26, 2005

jstirrell posted:

Ok so I'm trying to write a program which basically allows me to input questions and their answers, organized by chapter/book/whatever so I can study them.

My first Python program was pretty much this. I used it to learn German vocabulary. I stored everything in text files like so:

english:german
english:german

A different text file for each topic and made a menu in Python to load whichever file I wanted. Then it would show a random English word and then after a keypress the corresponding German word.

functional
Feb 12, 2008

Hi, softball question here. Looking to learn Python this week. Would like a personal recommendation on what to work through. I may be working in a Python shop soon.

I was contemplating working through some of the Euler challenges. I am slightly worried that might be a waste of time, and I don't want to focus too much on mathy things.

I've worked with just about every other well-known programming language besides Python. From what I've seen I much prefer Python to Ruby, a language which leaves a bad taste in my mouth. Python seems to be designed by someone who understands powerful programming, whereas Ruby appears to be simply a mountain of syntax sugar.

functional fucked around with this message at 20:26 on Jan 5, 2009

tef
May 30, 2004

-> some l-system crap ->
The python tutorial is pretty good, and the euler problems are also pretty good to make sure you have the fundamentals sorted.

A classic exercise is to re-write unix utilities like grep, wc, cat, tail, rev and sort in python.

functional
Feb 12, 2008

php:
<?
a=[0,1]
for x in a:
  a.append(x)
#infinite loop
?>
but

php:
<?
a=[0,1]
for x in range(len(a)):
  a.append(x)

>>> a
[0, 1, 0, 1]
?>
Seems inconsistent to me. What's the rule for evaluating the argument to a for statement?

Current guess: The for loop takes as its argument a pointer to a [mutable?] list. The list is evaluated only when the code enters the loop. Bounds checking and element retrieval is done at run time.

functional fucked around with this message at 02:38 on Jan 6, 2009

bitprophet
Jul 22, 2004
Taco Defender
The expression after the 'in' is evaluated only once, and the result is then iterated over (via a call to __iter__, I believe, but that's not important here). This expression is NOT re-evaluated every time, AFAIK.

In the first example, the expression evaluates to 'a', which is the name of a list object. Thus, the for loop iterates over "the object known as 'a'", and since that object keeps changing in the loop body, you get the infinite loop.

In the second example, the expression evaluates to [0,1], because that's what range(len(a)) evaluates to when the loop is first entered. This is NOT a name of a list, but is instead just a list. The fact that it was initially generated based on the length of 'a' doesn't matter, because range(len(a)) was only called one time, in order to get the object-to-iterate-over.

Thus, the second example works normally, because [0,1] never changes -- it's not tied to the value of 'a' in any way.

Hope that made sense...it sounds like you sort-of figured it out in your guess, if I read you correctly.

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"

bitprophet posted:

In the first example, the expression evaluates to 'a', which is the name of a list object. Thus, the for loop iterates over "the object known as 'a'", and since that object keeps changing in the loop body, you get the infinite loop.

Not quite. The name is only evaluated once, but it's used to retrieve an iterator. Since the list is being expanded within the loop, the iterator will never run out of items.

code:
>>> a = []
>>> i = iter(a)
>>> a.append(1)
>>> i.next()
1
>>> a.append(2)
>>> i.next()
2
# and so on

necrobobsledder
Mar 21, 2005
Lay down your soul to the gods rock 'n roll
Nap Ghost
So just to be clear, it is perfectly fine in Python to mutate the collection being iterated over? Because in some languages (albeit, with some quirks), this is considered an error at either runtime or compile time. Is there any particular philosophical reason for Python to allow this, or is this more of a quirk rather than explicit language semantic?

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"

necrobobsledder posted:

So just to be clear, it is perfectly fine in Python to mutate the collection being iterated over? Because in some languages (albeit, with some quirks), this is considered an error at either runtime or compile time. Is there any particular philosophical reason for Python to allow this, or is this more of a quirk rather than explicit language semantic?

It's frowned upon, but particular semantics will depend on the type of the container being mutated. For example, mutating a dictionary will raise RuntimeError. I don't know if this behavior is documented anywhere except to say "don't do this".

sw-itch
Apr 6, 2008
I seem to be getting a lot of memory not being cleared up after parsing XML using cElementTree.
code:
from xml.etree.cElementTree import XML

f = open('w:\\f.xml', 'rb')
data = f.read()
xml_data = XML(data)

data = None
xml_data = None


If I run the same code (well, with: from lxml.etree import XML) using the lxml parser, I get all my space back.


Is this a memory leak with cElementTree, or am I not understanding something?

Also I would prefer not to even load the xml into memory, is there a good low memory way to read xml with python?

tef
May 30, 2004

-> some l-system crap ->
I would highly recommend using lxml, I have found it a joy to use compared to elementtree

http://codespeak.net/lxml/parsing.html

quote:

>>> tree = etree.parse("doc/test.xml")

jonypawks
Dec 1, 2003

"Why didn't you tell me you were the real Snake?" -- Ken

necrobobsledder posted:

So just to be clear, it is perfectly fine in Python to mutate the collection being iterated over? Because in some languages (albeit, with some quirks), this is considered an error at either runtime or compile time. Is there any particular philosophical reason for Python to allow this, or is this more of a quirk rather than explicit language semantic?

If you need to modify a list you're iterating over just slice the whole list:

code:
>>> a = [0, 1]
>>> for x in a[:]:
...     a.append(x)
... 
>>> a
[0, 1, 0, 1]
This will have no side effects as it will be iterating over a copy. This is how the python tutorial recommends you do this: http://docs.python.org/tutorial/controlflow.html#for-statements.

a cat
Aug 8, 2003

meow.

jstirrell posted:

Ok so I'm trying to write a program which basically allows me to input questions and their answers, organized by chapter/book/whatever so I can study them.

Alright I'm still working on this, the tips you guys gave me were great but I'm running into problems. I'm finding teaching myself to program to be extremely hard, especially since I don't really have anyone besides you guys in this thread to consult. When I read the tutorials, even doing typing out all the examples, I don't really feel I retain nearly as much knowledge as when I just take a simple seeming idea and try to program it, using google and the documentation mainly to tackle hurdles as they come up.

So anyways, I'm currently just trying to to write code that can have questions inputted and sorted by subject and chapter, without worrying yet about having a hard copy saved somewhere for future use. Here's what I have so far:

code:
class Subject:
    def __init__(self, name):
        self.name = name
        self.chapters = []
        self.currentchapter = 0

    def enterChapters(self):
        while True:
            num = raw_input("What is the section or chapter's number? ")
            title = raw_input("What is the section or chapter's title? ")
            self.chapters.append(Chapter(num,title))
            prompt = raw_input("Do you want to enter questions for this chapter now? ")
            if prompt == "yes":
                self.chapters[self.currentchapter].enterQuestions()
                self.currentchapter += 1
            else:
                break
            prompt = raw_input("Are you done entering chapters? ")
            if prompt == "yes":
                self.currentchapter += 1
                break
                
    
class Chapter:
    def __init__(self, num, title):
        self.num = num
        self.title = title
        self.questions = []
        self.answers = []
    
    def enterQuestions(self):
        temp = 0
        n = len(self.questions) + 1
        while True:       #is there a better way to do this???
            self.questions.append(raw_input("%s%i%s" % ("Enter question #", n, ": ")))
            print "%s%i%s%s%s" % ("Question #", n, " is: '", self.questions[n-1], "'")
            self.answers.append(raw_input("%s%i%s" % ("What is the answer to question #", n, "? ")))
            if self.answers[n-1] == 'done':        #temporary
                break
            n += 1

class Main:
    def __init__(self):
        self.subjects = []

    def newSubject(name):
        name = raw_input("What is the name of the subject or class? ")
        self.subjects.append(Subject(name))


prompt = raw_input("What would you like to do? ")
if prompt == "N":
    action = Main()
    action.newSubject()
The problem is that it doesn't compile currently which I think is due to some syntax error at the end of the code which I can't figure out, but what worries me more is that I still feel like I have no clue if I even have a decent structure for the program I'm trying to write, or where I'm really heading with this. Anyone have any suggestions? What am I doing right? What is wrong? Any general tips for struggling self-teaching noobs like myself? Also, I'm trying to get the general structure of the code down so I realize some of the non-critical details (ie elifs) are missing.

Sorry for the wall of code, should I not be posting huge general questions here? I'm just kind of really lost.

a cat fucked around with this message at 21:08 on Jan 6, 2009

functional
Feb 12, 2008

What does a tuple do that a list won't? Is it useful for anything? (You seem to be able to 'tuple pack' just fine with a list. Why would I switch over?)

functional fucked around with this message at 00:20 on Jan 7, 2009

tef
May 30, 2004

-> some l-system crap ->
You can use a tuple as a hash key.

No Safe Word
Feb 26, 2005

functional posted:

What does a tuple do that a list won't? Is it useful for anything? (You seem to be able to 'tuple pack' just fine with a list. Why would I switch over?)

http://jtauber.com/blog/2006/04/15/python_tuples_are_not_just_constant_lists/

Summary: Tuples are more like "records", and lists are "collections"

Kilometers Davis
Jul 9, 2007

They begin again

Is there anything wrong with learning Python in 3.0 as opposed to 2.6?

functional
Feb 12, 2008

tef posted:

You can use a tuple as a hash key.

Five star answer. Thanks.

Adbot
ADBOT LOVES YOU

tef
May 30, 2004

-> some l-system crap ->

IntoTheNihil posted:

Is there anything wrong with learning Python in 3.0 as opposed to 2.6?

Yes, none of the libraries or third party things will support it yet. The leap to 3.0 from 2.6 isn't really that much - it's only to allow backwards incomatible changes.

(Personally, at work, we're still using 2.5 at the moment).

3.0 isn't going to happen overnight, and I imagine most people won't switch until 3.1 at least - there are still changes that need to be refined, and the perfomance of 3.0 is slighly less than 2.6 at the moment.

Learn the version of python you're most likely to encounter, but you can do future imports to bring most of the parts of 3.0 you want in.

  • Locked thread