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
SmirkingJack
Nov 27, 2002
What is the standard for documenting your code? I get a kick out of javadoc/phpdoc. Is there a pythondoc or some equivalent way of generating HTML documentation of your API?

bitprophet posted:

...
Also, the official Django book is done! Should be on shelves by mid/late December, if not earlier, I'm told :)

Is this book you speak of The Definitive Guide to Django: Web Development Done Right or a different one?

SmirkingJack fucked around with this message at 14:50 on Nov 16, 2007

Adbot
ADBOT LOVES YOU

SmirkingJack
Nov 27, 2002
Ok, I just don't get it.
code:
class test(object):
    
    var1 = ""
    var2 = []
    
    def change(self, newString):
        self.var1 = newString
    
    def add(self, newItem):
        self.var2.append(newItem)

If I do something like
code:
a = test()
b = test()

a.change("foo")
b.change("bar")

a.add("pillow")
b.add("talk")

print a.var1
print b.var1

print a.var2
print b.var2
The output is
code:
foo
bar
['pillow', 'talk']
['pillow', 'talk']
Why is var2 shared between instances but var1 is not? And why is var2 shared to begin with? So far Python strikes me as an odd language.

SmirkingJack
Nov 27, 2002

JoeNotCharles posted:

Try "print test.var1" and "print test.var2". (It'll be more clear if you initialize var1 to "default" instead of "".)

Here's what's happening:
...

Whoops, I forgot to say thanks for this. That's an interesting behavior, one that I obviously did not expect. So, thank you for the help.

SmirkingJack
Nov 27, 2002

Mulozon Empuri posted:

The Django Book 1.0 is finally online! :woop:

Wow, that really surprises me. I would have thought that they would have waited until after Christmas at least before releasing it online, to push book sales for the season.

SmirkingJack
Nov 27, 2002
Yesterday Reddit pointed me to "How not to write Python code" and one of the things that caught my eye was the following:

quote:

Use Pythonesque coding pattern
This is very related to the previous item, obviously. Python has some well-known constructs to handle some situations. Get to know and understand them.
An example: as you might know, Python has no switch/case construct. There’s a very neat way to implement this though by simply using a dict and function objects (or lambda functions). An example:
code:
def handle_one():
    return 'one'
def handle_two():
    return 'two'
def handle_default():
    return 'unknown'
cases = {
    'one': handle_one,
    'two': handle_two,
    'three': lambda: 'three',
}
for i in ('one', 'two', 'three', 'four', ):
    handler = cases.get(i, handle_default)
    print handler()
We’re using the dict.get method here, which can take an optional ‘default’ argument. Pretty neat, huh?

I am pretty new to this Python thing and it made me realize that I do not know much about coding in a manner consistent with other Python programmers. Could anyone point out examples of common Python techniques ("this is the way we do switch")?

SmirkingJack
Nov 27, 2002

outlier posted:

* Django is the officially blessed framework. My one (and major) quibble is that it's using it's own database layer and ORM, which defeats the point if you have your own database layer already laid out (as I do). And Django's efforts at integrating SQLALchemy appear to have gone nowhere in 2 years.


I thought part of Django's charm was that it didn't force you to use anything you didn't want to. So if you wanted to use a different db layer/ORM then you could.

SmirkingJack
Nov 27, 2002

SkyLander posted:

I have a couple Python questions if anyone could help.

I have to write a command line interface type program, basically what are considered simple things and I decided to mess around with Python to do it instead of C++. So far things seem a lot simpler and working in python is a lot easier than in C++. Just takes some getting used to in that everything is a little more loose.

Anyway basically I have to do basic commands and one of them is to do a dir command which just lists what is in a directory. So I found listdirectory() in the OS module. So I basically just do input from the command line into a string and I just put that into listdirectory. Now does that just create a list of strings? Or how do I then output that? Sorry if that seems confusing.

Basically what I have is

def directory(command)
directoryOut = os.listdirectory(command)
print directoryOut

And that doesn't do anything so I assume I am missing something.

Thanks in advanced.

First I would enter the interactive editor and type "import os" followed by "dir(os)" to print a list of methods in the os module.

SmirkingJack
Nov 27, 2002

SkyLander posted:

...
Basically what I have is

def directory(command)
directoryOut = os.listdirectory(command)
print directoryOut

And that doesn't do anything so I assume I am missing something.

Thanks in advanced.

Are you calling your function? What does the following do?

code:
def directory(command):
  directoryOut = os.listdir(command)
  print directoryOut

directory("c:")
Replace "c:" with "/" if you are not running Windows.

SmirkingJack fucked around with this message at 23:08 on Mar 3, 2008

SmirkingJack
Nov 27, 2002

Milde posted:

That's silly, it'll a list a whole bunch of useless poo poo he doesn't need or care about and won't tell him what any of it means. help(os) is much more helpful (that's why it's called help, after all).
...

The idea was to quickly help him realize that he was not calling the right method.

SmirkingJack
Nov 27, 2002


I remember that when this one came out I was beginning to visit Python and it inspired a little project. I did not have the time to follow through on it but things have been getting a little monotonous for me lately and I needed to take a break and do something different.

So I finally got around to writing this. It grabs xkcd's RSS feed and looks for an update. If it finds one then it will download the new strip to a pictures directory and set it as the wallpaper. I was going to make it cross platform but I ended up not feeling like it and hey, it was only supposed to be a small break. So sorry, OS X only.

Download the script to wherever you want and save this plist to ~/Library/LaunchAgents and change the path appropriately. The plist has the script running every three hours so if you want it to be different then change TimeInterval to however many seconds.

Perhaps in 2011, when I hope to have some more free time, I will update it to work with other operating systems (not hard) and other comics.

A couple caveats - Prior to running this the background preferences must be set to a centered non-rotating picture. And it might freak out if ~/Pictures/xkcd doesn't already exist, I can't remember.

Somebody fucked around with this message at 05:10 on May 28, 2008

Adbot
ADBOT LOVES YOU

SmirkingJack
Nov 27, 2002
Oh, so this is what Python is all about.

  • Locked thread