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
tef
May 30, 2004

-> some l-system crap ->

Lonely Wolf posted:

But if you have Python 2.6, do what Tef said. In fact, all of you do what Tef says or my crew will jump you.

If you need to sanitize settings or validate them, then getters & setters are ok (for example a phone number property that checks for valid area codes, etc). Properties are excellent for this.

The problem is that for some classes, getters and setters are examples of things that break encapsulation, as logic often ends up in the classes calling them, rather than the classes themselves.

The idea is that it is better to tell, don't ask. Pass the information to the class needed to do the work, rather than pull the information out.

I don't know which applies for the classes the guy above is writings, so I'll avoid trying to apply dogma here, but I don't want to encourage some pitfalls in OO design.

Adbot
ADBOT LOVES YOU

pankus
Jul 30, 2003

Kire posted:

Gosh, I've heard people say to use Netbeans or Eclipse, but I'm trying to load some simple scripts that refuse to work in IDLE (pygame's event.QUIT has never worked when I create and run things in IDLE, and I've been told it might be a Tkinter issue) and holy cow is this impossible. Both Netbeans and Eclipse adamantly refuse to let me run a script. In Netbeans, it just says "Hello" in a sub-window (not part of the program I'm trying to run) and eclipse just tells me it can't do it: "The selection cannot be launched, and there are no recent launches."

I don't care if using IDLE means that I have to repeatedly force-quit every time I use pygame...at least I can edit/run things.

netbeans defaults to having something like this line in new projects, maybe that's your problem?

code:
if __name__ == "__main__":
    print "Hello World!"
In netbeans related questions, is anyone having trouble debugging with 6.7 and python 2.6.2? If I set a breakpoint the debugger immediately exits.

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

spankweasel posted:

(Metaclasses and "automatic property creation" Has anybody explored this? I'm tearing my hair out trying to understand metaclasses.

Some nifty, clever stuff has been done with metaclasses. However, I've begun to avoid using them because (a) they can be tricky to understand, (b) your code starts to be shaped by 'clever tricks' and odd metaclass idioms and (c) they're going away in Python 3000.

I think "automatic property creation" is probably referring to stuff like Django uses in its models.

ten_twentyfour
Jan 24, 2008

After teaching myself some Python, I finally finished my A* algorithm program. Yes, it's basic, but I only have a few CS classes under by belt, so I'm proud. Added mobility to the start/stop nodes, but I broke it so no pictures of that. The app scales to any grid size, but after about 50x50 it begins to noticeably slow down. Haven't done any optimization yet, but thus far I'm proud with how much I've learned. I really do love Python so much.


(in case it needs saying, black = open space, dark blue = walls, red = goal, green = start, light blue = best path)
((Yes, it looks like a swastika, no I'm not racist))
(((In my implementation I chose to disallow cutting corners of walls, which is why the path doesn't cut them to save distance)))


My eventual goal is to take this module and implement it in a 2-d top-scroller of some sort.

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"

outlier posted:

[metaclasses are] going away in Python 3000.
What? No they're not. Metaclasses, especially abstract base classes, are a fundamental and useful feature of Python 3.

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

Janin posted:

What? No they're not. Metaclasses, especially abstract base classes, are a fundamental and useful feature of Python 3.

Mea culpa - I mispoke. The metaclass _syntax_ and machinery is changing in P3000. Thus any fancy metaclass you do now will have to change.

(There is a metaclass solution that works in Python 2/3 here, but it looks a bit clumsy to my eye.)

nonathlon fucked around with this message at 09:00 on Aug 7, 2009

ShizCakes
Jul 16, 2001
BANNED
I'm still trying to figure out the barebones of python web programming. I've only ever coded PHP for the web (although I have written a number of apps in python), and I am still having trouble breaking my brain from how easy PHP is.

My latest frustration: How the gently caress do I access the query string? The closest I can find online is os.environ['QUERY_STRING'] - but that doesn't exist?

What I've got:
Lighttpd with mod_fcgi and flup.

My little app looks like this:

code:
#!/usr/bin/python
def myapp(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/plain')])
    
    string = ""
    for param in os.environ.keys():
        string += param + ": " + os.environ[param] + "\n"

    return [string]

if __name__ == '__main__':
    import os, cgi, flup.server.fcgi
    flup.server.fcgi.WSGIServer(myapp).run()
and that outputs the following:

code:
LANG: en_US.UTF-8
TERM: xterm
SHLVL: 2
PHP_FCGI_CHILDREN: 1
PWD: /
PATH: /sbin:/usr/sbin:/bin:/usr/bin
_: /usr/sbin/lighttpd
I can't find this seemingly basic information documented anywhere I look. And I don't even know who's responsible for providing the query string - is it lighttpd? mod_fastcgi? flup?

Please help, I've frustrated myself into a corner yet again...

[EDIT] - I'm happy to learn on my own, but the documentation that I've found thus far doesn't actually explain anything. The actual python docs are great, but they don't seem to have anything to do with this. If you know where I can find documentation that has examples, I would be indebted to you. Also, please don't link me to this: http://www.wsgi.org/wsgi/ - I can't find anything of use in there (I don't even know if it's the right place).

ShizCakes fucked around with this message at 14:31 on Aug 7, 2009

Xenos
Jun 17, 2005

ShizCakes posted:

My little app looks like this:

...

I can't find this seemingly basic information documented anywhere I look. And I don't even know who's responsible for providing the query string - is it lighttpd? mod_fastcgi? flup?

Please help, I've frustrated myself into a corner yet again...

Take a look at myapp()'s argument list. You should notice that it takes an environ parameter. Instead of printing out os.environ.keys(), try printing environ.keys().

tef
May 30, 2004

-> some l-system crap ->
Use a web framework rather than a collection of wsgi utilities?

For example: http://webpy.org/

ShizCakes
Jul 16, 2001
BANNED

Xenos posted:

Take a look at myapp()'s argument list. You should notice that it takes an environ parameter. Instead of printing out os.environ.keys(), try printing environ.keys().

Thank you! I don't quite understand why that works, but it does. (Specifically, I am not passing an environ or start_response variable to myapp when I call it, so where is it getting them?)

tef posted:

Use a web framework rather than a collection of wsgi utilities?

For example: http://webpy.org/

I'm trying this now. I don't get the concept of how this works. (the hello world example just puts out "not found"?). God, I swear I am not this retarded usually - I just wish there was something that explained the underlying concepts simply, or a guide to switching from php, or something.

Threep
Apr 1, 2006

It's kind of a long story.

ShizCakes posted:

I'm trying this now. I don't get the concept of how this works. (the hello world example just puts out "not found"?). God, I swear I am not this retarded usually - I just wish there was something that explained the underlying concepts simply, or a guide to switching from php, or something.
Explain what you've done/not done and what the actual error is, it's hard to help you when we don't know what isn't found.

Anyway, the idea is, with a traditional CGI script, and also with PHP, you call the script directly from the url - http://url/hello.py or http://url/showthread.php

The way web.py and django and similar frameworks are supposed to be used, your script gets called with every url requested from the server - http://url/hello or http://forums.somethingawful.com/thread/2675400-python-information-and-short-questions-megathread. If you need static files served from the server, you make those exceptions (like /static/ in the lighttpd config on the web.py site)

After that you're free to use Python concepts to decide how to deal with the URL - for example web.py uses its url rules to decide what class to will render the page. In the Hello World example on the web.py front page, http://url/hello is a valid url and everything else is a 404.

This is a good excuse to link this.

Threep fucked around with this message at 15:47 on Aug 7, 2009

Profane Obituary!
May 19, 2009

This Motherfucker is Dead
Django is another webframework that is pretty popular and has an amazing tutorial

ShizCakes
Jul 16, 2001
BANNED

Threep posted:

Explain what you've done/not done and what the actual error is, it's hard to help you when we don't know what isn't found.

First, thanks for your quick explanation. I am on board with that concept (mostly - I wish I had a choice, because sometimes I don't need things to be that complicated, or I want to run multiple apps easily, or a million other things)

Anyways, the problem that I am having now seems to be the way that web.py does it's URL matching or whatever.

With this simple code:

code:
#!/usr/bin/env python
import web

urls = ( '/test.py', 'index' )

app = web.application(urls, globals())

class index:
        def GET(self):
                return "Hello, world!"

if __name__ == "__main__":
        app.run()
It outputs "not found" - but it's not my server's 404. It's coming from test.py.

If I change '/test.py' to '.*', it works - but I can't figure out what actual string it wants to match against. "test.py", "/", "./test.py" all output "not found". Going to a different path like https://www.example.com/BONK returns the proper server 404.

Note that I am not routing all requests to test.py at the moment - I just want to test some stuff and play around.

Threep
Apr 1, 2006

It's kind of a long story.
code:
class index:
        def GET(self, path):
                return path
I've never actually used web.py myself (spot the error in my previous reply), but try this to see what it matches against.

Also, if it actually was /test.py you should do this:
code:
urls = ( r'/test\.py', 'index' )
since . is gonna match any character else.

ShizCakes
Jul 16, 2001
BANNED

Threep posted:

code:
class index:
        def GET(self, path):
                return path
I've never actually used web.py myself (spot the error in my previous reply), but try this to see what it matches against.

Also, if it actually was /test.py you should do this:
code:
urls = ( r'/test\.py', 'index' )
since . is gonna match any character else.

The answer, after much loving around, is an empty string. My theory was that it's not matching any URL - it's just itself. I really wish there was good documentation on this stuff.

I prefer not to use django, yet, because - as painful as it is for me right now, I want to learn some of the 'lower level' of this before it's all abstracted away.

Allie
Jan 17, 2004

ShizCakes posted:

Thank you! I don't quite understand why that works, but it does. (Specifically, I am not passing an environ or start_response variable to myapp when I call it, so where is it getting them?)

I see you've already moved on from this, but I feel compelled to respond:

You never called myapp, you passed it to WSGIServer(). It sets up the environ and calls your function for each request.

If you're just coming to Python, I suggest you do yourself a favor and stay away from the low level machinations of WSGI and use a framework. Not that WSGI is bad - it's a great way to host applications - but developing on it directly can be tedious.

Once you get to the point of deploying your app, regardless of the framework, I recommend taking a look at mod_wsgi.

Also, I personally recommend using Django. :)

ShizCakes
Jul 16, 2001
BANNED

Milde posted:

I see you've already moved on from this, but I feel compelled to respond:

You never called myapp, you passed it to WSGIServer(). It sets up the environ and calls your function for each request.

If you're just coming to Python, I suggest you do yourself a favor and stay away from the low level machinations of WSGI and use a framework. Not that WSGI is bad - it's a great way to host applications - but developing on it directly can be tedious.

Once you get to the point of deploying your app, regardless of the framework, I recommend taking a look at mod_wsgi.

Also, I personally recommend using Django. :)

I'm glad you did, and I appreciate it. The idea of what's going is much clearer - right now, I am really just interested in understanding even more than being productive. Unfortunately, I am in a place where I can't trace out what's going on so little things are throwing me for a loop. I'm not a professional coder or anything, but I do like it anyways.

Habnabit
Dec 30, 2007

lift your skinny fists like
antennas in germany.

king_kilr posted:

No, it's not. raise NotImplementerError is the correct idiom.

A little late, but no, that's not the correct idiom. Raising NotImplementedError breaks correct use of super(). This is what abc.abstractmethod is for, if you're using python 2.6+. Otherwise, just make your method do nothing.

Mata
Dec 23, 2003
I feel really stupid but I'm not really used to this language... how do you change parts of a string?

What I want to do is basically like: mystring[40] = 'i' at a few select places.
what's the best way to go about it? the whole string can get up to a few megabytes so ideally i wouldn't want to copy it around too much. Do i have to split it up then merge it again?

Thermopyle
Jul 1, 2003

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

Mata posted:

I feel really stupid but I'm not really used to this language... how do you change parts of a string?

What I want to do is basically like: mystring[40] = 'i' at a few select places.
what's the best way to go about it? the whole string can get up to a few megabytes so ideally i wouldn't want to copy it around too much. Do i have to split it up then merge it again?

Strings in python are immutable. The best you can do is create a new string that is a variation on the original.

Maybe something like this:

code:
mystring = mystring[:39] + 'i' + mystring[41:]

Mata
Dec 23, 2003

Thermopyle posted:

Strings in python are immutable. The best you can do is create a new string that is a variation on the original.

Maybe something like this:

code:
mystring = mystring[:39] + 'i' + mystring[41:]

Yeah that's what I ended up doing, trying not to think too much about the efficiency

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"

Mata posted:

Yeah that's what I ended up doing, trying not to think too much about the efficiency
If you're manipulating bytes, rather than characters, use the bytearray object (which is mutable):

code:
>>> obj = bytearray(b"abc")
>>> obj[0:1] = b"d"
>>> obj
bytearray(b'dbc')
Available in 2.6+

Threep
Apr 1, 2006

It's kind of a long story.
I'm working on something that involves downloading a queue from a small number of different connections (say, 5 or so) to different files

What's the best way to go about this? I've looked at:

  • Python threads - because I doubt the GIL will hurt much when I'm mostly bandwidth-bound anyway.
  • multiprocessing module's queues - I'm not sure the best way to go about detecting a finished job though

And for the sake of learning - if I was downloading from 50/100 connections and/or to one file, what would the answer be?

m0nk3yz
Mar 13, 2002

Behold the power of cheese!

Threep posted:

I'm working on something that involves downloading a queue from a small number of different connections (say, 5 or so) to different files

What's the best way to go about this? I've looked at:

  • Python threads - because I doubt the GIL will hurt much when I'm mostly bandwidth-bound anyway.
  • multiprocessing module's queues - I'm not sure the best way to go about detecting a finished job though

And for the sake of learning - if I was downloading from 50/100 connections and/or to one file, what would the answer be?

Use threading - you're I/O/Socket bound.

Casey Finnigan
Apr 30, 2009

Dumb ✔
So goddamn crazy ✔
This is probably a really stupid question, but I'm in the middle of learning Python as my first programming language with A Byte of Python and I'm stuck on this lesson

According to the lesson when I import the using_sys.py script I should get a result that looks like this:

code:
The command line arguments are:
using_sys.py
we
are
arguments


The PYTHONPATH is ['/home/swaroop/byte/code', '/usr/lib/python23.zip',
'/usr/lib/python2.3', '/usr/lib/python2.3/plat-linux2',
'/usr/lib/python2.3/lib-tk', '/usr/lib/python2.3/lib-dynload',
'/usr/lib/python2.3/site-packages', '/usr/lib/python2.3/site-packages/gtk-2.0']
but when I import it I get this (sorry they're not coded, it breaks the tables for some reason):


The command line arguments are:



The PYTHONPATH is ['', 'C:\\Windows\\system32\\python26.zip', 'C:\\Python26\\DLLs', 'C:\\Python26\\lib', 'C:\\Python26\\lib\\plat-win', 'C:\\Python26\\lib\\lib-tk', 'C:\\Python26\\Lib\\site-packages\\pythonwin', 'C:\\Python26', 'C:\\Python26\\lib\\site-packages', 'C:\\Python26\\lib\\site-packages\\win32', 'C:\\Python26\\lib\\site-packages\\win32\\lib', 'C:\\Users\\Jordan\\Desktop\\diveintopython-5.4\\py']



and when I run it I get this:

The command line arguments are:
C:\Users\Jordan\Desktop\diveintopython-5.4\py\using_sys.py


The PYTHONPATH is ['C:\\Users\\Jordan\\Desktop\\diveintopython-5.4\\py', 'C:\\Windows\\system32\\python26.zip', 'C:\\Python26\\DLLs', 'C:\\Python26\\lib', 'C:\\Python26\\lib\\plat-win', 'C:\\Python26\\lib\\lib-tk', 'C:\\Python26\\Lib\\site-packages\\pythonwin', 'C:\\Python26', 'C:\\Python26\\lib\\site-packages', 'C:\\Python26\\lib\\site-packages\\win32', 'C:\\Python26\\lib\\site-packages\\win32\\lib', 'C:\\Users\\Jordan\\Desktop\\diveintopython-5.4\\py']



I'm using PythonWin to code and run everything, if it matters.

Lonely Wolf
Jan 20, 2003

Will hawk false idols for heaps and heaps of dough.
It looks like it's running fine except you didn't pass it any command line arguments and you're running it on windows instead of a *nix. There does not appear to be a problem. What do you think is wrong?

Try running it from the command line like
code:
python using_sys.py We are arguments

Sparta
Aug 14, 2003

the other white meat
I want to develop a small ad network system, I've worked out the logic behind how it will work and have mapped out functions, checks, etc. Problem is I am not really a programmer. I'm the opposite of an average programmer: I don't know poo poo about syntax but I know how to keep good practices (writing up specs, keeping view, model, and data seperate, making sure user input is cleansed, etc.

I have heard some pretty great things about python and specifically Django.

What would be a good book to read regarding this? People keep telling me to read the Python Docs, but I can't really learn by reading docs on my monitor, I need printed word.

Any recommendations for good, hands on, learn-by-doing books on Python and Django for someone who knows how to code well but doesn't know how to code?

deedee megadoodoo
Sep 28, 2000
Two roads diverged in a wood, and I, I took the one to Flavortown, and that has made all the difference.


Sparta posted:

I want to develop a small ad network system, I've worked out the logic behind how it will work and have mapped out functions, checks, etc. Problem is I am not really a programmer. I'm the opposite of an average programmer: I don't know poo poo about syntax but I know how to keep good practices (writing up specs, keeping view, model, and data seperate, making sure user input is cleansed, etc.

I have heard some pretty great things about python and specifically Django.

What would be a good book to read regarding this? People keep telling me to read the Python Docs, but I can't really learn by reading docs on my monitor, I need printed word.

Any recommendations for good, hands on, learn-by-doing books on Python and Django for someone who knows how to code well but doesn't know how to code?

I've never used django but it might make sense to pick up a basic python book before diving into a framework. I started with "Learning Python" but have heard good things about "Dive into Python." http://www.pythonchallenge.com is worth a look to get some hands on experience. Once you have the basic understanding you should pick up one of the many django books. Someone can call me out on this if they feel it's bad advice.

bitprophet
Jul 22, 2004
Taco Defender

Sparta posted:

Any recommendations for good, hands on, learn-by-doing books on Python and Django for someone who knows how to code well but doesn't know how to code?

I and two other gentlemen wrote a Django book which includes a large early chapter on Python, so that you can learn enough Python to get off the ground and handle the rest of the book: Python Web Development with Django

Not entirely sure if your situation is the exact target audience (which is to say, people who know how to code but not necessarily well *cough*average PHP coder*cough*) but as long as you know enough about programming to know what a loop is and what a variable is, you should be fine.


Otherwise, aforementioned Dive into Python is good stuff for learning Python, and the Django team has an official book out in print for that topic (and online for free, but if you want a book-book, obviously get the hard copy.)

TheMaskedUgly
Sep 21, 2008

Let's play a different game.
I'm new to python, but I've written a long program and it has an issue.
Basically it copies a template web page, and makes 10 copies of it with different first paragraphs. Because I'm new, this has ended up being a fairly long program. When I run it, it gets through each copy fine until it get to the last one. It only manages to copy the last page half way. If I swap the programming for the 9th and 10th copies, then the 9th only gets halfway, so I know its not the actual code that's the problem.
Is there a maximum length for a python program? That seems highly unlikely. (The code's a couple of hundred lines long max) Any ideas?

E: This was written in IDLE 2.6, by the by. Also, I've got a punctuation checker that only actually works if I open it and hit f5 in the IDLE editor, but not if I just double click on it, running it through command prompt. It's only 4 seconds of effort, but it still seems like that shouldn't be right?

TheMaskedUgly fucked around with this message at 23:58 on Aug 11, 2009

tripwire
Nov 19, 2004

        ghost flow

TheMaskedUgly posted:

I'm new to python, but I've written a long program and it has an issue.
Basically it copies a template web page, and makes 10 copies of it with different first paragraphs. Because I'm new, this has ended up being a fairly long program. When I run it, it gets through each copy fine until it get to the last one. It only manages to copy the last page half way. If I swap the programming for the 9th and 10th copies, then the 9th only gets halfway, so I know its not the actual code that's the problem.
Is there a maximum length for a python program? That seems highly unlikely. (The code's a couple of hundred lines long max) Any ideas?

Post the code.

Thermopyle
Jul 1, 2003

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

TheMaskedUgly posted:

I'm new to python, but I've written a long program and it has an issue.
Basically it copies a template web page, and makes 10 copies of it with different first paragraphs. Because I'm new, this has ended up being a fairly long program. When I run it, it gets through each copy fine until it get to the last one. It only manages to copy the last page half way. If I swap the programming for the 9th and 10th copies, then the 9th only gets halfway, so I know its not the actual code that's the problem.
Is there a maximum length for a python program? That seems highly unlikely. (The code's a couple of hundred lines long max) Any ideas?

E: This was written in IDLE 2.6, by the by. Also, I've got a punctuation checker that only actually works if I open it and hit f5 in the IDLE editor, but not if I just double click on it, running it through command prompt. It's only 4 seconds of effort, but it still seems like that shouldn't be right?

Not enough info. Post the code. Also, a couple of hundred lines isn't long at all, so don't worry about that.

TheMaskedUgly
Sep 21, 2008

Let's play a different game.
This is my first non 'hello world' program so go easy.

There are 10 copies of this, with the A replaced with B etc, preceeded by a chunk that copies the template to the temp file. If you need anything else say.

Didn't make this clear, but it reads the template from a text file, as well as the different first paragraph, title, and reference numbers, each from seperate text files. Temp01 = the files locations etc.
code:
# Copy  Title A
Initialise()
inp = open(Temp01, "r")
outp = open(Temp02, "w")
text = inp.read()
outp.write(text.replace("@@@@@TITLE@@@@@", Titles[0]))
inp.close()
outp.close()

Temp02to01()
print "Title A: replaced"

# Copy Paragraph A
inp = open(Temp01, "r")
outp = open(Temp02, "w")
text = inp.read()
outp.write(text.replace("@@@@@PARAGRAPH@@@@@", Paragraphs[0]))
inp.close()
outp.close()
Temp02to01()
print "Paragraph A: replaced"

# Copy Tracer A
inp = open(Temp01, "r")
outp = open(Temp02, "w")
text = inp.read()
outp.write(text.replace("@@@@@TRACERIMAGE@@@@@", Tracer[0]))
inp.close()
outp.close()
Temp02to01()
print "Tracer Image A: replaced"

# Copy ReffNo A
inp = open(Temp01, "r")
outp = open(Temp02, "w")
text = inp.read()
outp.write(text.replace("@@@@@REFERENCENUMBER@@@@@", ReffNo[0]))
inp.close()
outp.close()
Temp02to01()
print "ReffNo A: replaced"

# Save Advertorial A
inp = open(Temp01, "r")
outp = open(OutputA, "w")
for line in inp:
    outp.write(line)
print "Advertorial A: 100% Complete"
print "----------------------------"
print " "

TheMaskedUgly fucked around with this message at 00:07 on Aug 12, 2009

tripwire
Nov 19, 2004

        ghost flow
Just throw the whole thing on pastebin or something and link us.

Also if you catch yourself getting ready to copy and paste code slam your hand in a door until you learn not to do it. If only a couple things vary between all those different blocks opening a file and closing it, make a list and go through them with a loop.

TheMaskedUgly
Sep 21, 2008

Let's play a different game.
http://pastebin.com/m7eea8442

There's the first program. The original program was identical except it went all the way to the 10th copy and finished. To make the two programs, I copied the beginning section and initialising crap, and then cut out the last 5. It works now in two sections.

king_kilr
May 25, 2007

bitprophet posted:

Otherwise, aforementioned Dive into Python is good stuff for learning Python, and the Django team has an official book out in print for that topic (and online for free, but if you want a book-book, obviously get the hard copy.)

Small correction. The Django Book is not by the Django team, it is by Adrian Holovaty and Jacob Kaplan-Moss, it is a private enterprise and is unaffiliated with either the Django Project or the Django Software Foundation.

Scaevolus
Apr 16, 2007

TheMaskedUgly posted:

http://pastebin.com/m7eea8442
Did a function kill your mother?

deedee megadoodoo
Sep 28, 2000
Two roads diverged in a wood, and I, I took the one to Flavortown, and that has made all the difference.


TheMaskedUgly posted:

http://pastebin.com/m7eea8442

There's the first program. The original program was identical except it went all the way to the 10th copy and finished. To make the two programs, I copied the beginning section and initialising crap, and then cut out the last 5. It works now in two sections.

I'm pretty sure this replaces that entire monstrosity:

code:
## Contains the File locations for all necessary files
Template = r"c:/Documents and Settings/Name/Desktop/HTMLiser/Input/Template.txt"
TitlesFile = r"c:/Documents and Settings/Name/Desktop/HTMLiser/Input/Titles.txt"
ParagraphsFile = r"c:/Documents and Settings/Name/Desktop/HTMLiser/Input/Paragraphs.txt"
TracerFile = r"c:/Documents and Settings/Name/Desktop/HTMLiser/Input/TracerImages.txt"
ReffNoFile = r"c:/Documents and Settings/Name/Desktop/HTMLiser/Input/ReffNo.txt"
OutputFiles ["C:/Documents and Settings/Name/Desktop/HTMLiser/Output/12-000211-A-Zuneta1.htm",
"C:/Documents and Settings/Name/Desktop/HTMLiser/Output/12-000211-B-Zuneta1.htm",
"C:/Documents and Settings/Name/Desktop/HTMLiser/Output/12-000211-C-Zuneta1.htm",
"C:/Documents and Settings/Name/Desktop/HTMLiser/Output/12-000211-D-Zuneta1.htm",
"C:/Documents and Settings/Name/Desktop/HTMLiser/Output/12-000211-E-Zuneta1.htm",
"C:/Documents and Settings/Name/Desktop/HTMLiser/Output/12-000211-F-Zuneta1.htm",
"C:/Documents and Settings/Name/Desktop/HTMLiser/Output/12-000211-G-Zuneta1.htm",
"C:/Documents and Settings/Name/Desktop/HTMLiser/Output/12-000211-H-Zuneta1.htm",
"C:/Documents and Settings/Name/Desktop/HTMLiser/Output/12-000211-I-Zuneta1.htm",
"C:/Documents and Settings/Name/Desktop/HTMLiser/Output/12-000211-J-Zuneta1.htm"]

template = open(Template).read()
title_file = open(TitlesFile)
paragraph_file = open(ParagraphsFile)
tracer_file = open(TracerFile)
reffno_File = open(ReffNoFile)


for file in OutputFiles: 
    title = title_file.readline()
    paragraph = paragraph_file.readline()
    tracer = tracer_file.readline()
    reffno = reffno_file.readline()

    
    output_file = open(file, 'w')
    output_file.write(template.replace("@@@@@TITLE@@@@@", title).replace("@@@@@PARAGRAPH@@@@@", paragraph).replace("@@@@@TRACERIMAGE@@@@@", tracer).replace("@@@@@REFERENCENUMBER@@@@@", reffno))
    output_file.close()

title_file.close()
paragraph_file.close()
tracer_file.close()
reffno_file.close()
And this could even be cleaned up a bit.

deedee megadoodoo fucked around with this message at 19:47 on Aug 12, 2009

Matlock, BRB
Jun 23, 2004

by T. Finn
If I write
code:
for v in t:
  # bla bla
where t is a list, is there a way to get the index of my current variable v in t? Something like
code:
for v in t:
  print(t.index(v))
or whatever, which would print out
0
1
2
3
4
if my list has 5 elements.

Adbot
ADBOT LOVES YOU

Lonely Wolf
Jan 20, 2003

Will hawk false idols for heaps and heaps of dough.
code:
for i, v in enumerate(t):
    print i, "; ", v
outputs
0: . . .
1: . . .
2: . . .

and so on

  • Locked thread