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
LuckySevens
Feb 16, 2004

fear not failure, fear only the limitations of our dreams

Hey guys, I've started learning Python as my start to beginning to learn programming, hopefully I'll have the staying power to learn a thing or two. Is it alright if I post really newbie questions here?

Adbot
ADBOT LOVES YOU

LuckySevens
Feb 16, 2004

fear not failure, fear only the limitations of our dreams

Hey, I'm a python and programming newbie, I've been going through some newbie tutorials, just getting a feel for whats going on. I'm just wondering if someone could explain to me how calling a function and using it is working here:

code:
import time
# Create daily menu based on MENU.TXT
# First open the files to read(r) and write(w)
inp = open("menu.txt","r")
outp = open("menu.prn","w")


# Create todays date string 
today = time.localtime(time.time())
theDate = time.strftime("%A %B %d", today)

# Add Banner text and a blank line
outp.write("Menu for %s\n\n" % theDate) 

# copy each line of menu.txt to new file
for line in inp:
    outp.write(line)

print "Menu created for %s..." % theDate

# Now close the files
inp.close()
outp.close()
I apologise for the newbieness, when we're creating todays date string, one line says "today = time.localtime(time.time())". I understand we're creating a variable and we're using the localtime function, what I don't get exactly is the time. before it and the time.time() inside the function. The tutorial didn't really explain what's going on here, can somebody help me with just a quick human explanation. I want to make sure I understand everything fully instead of glossing over this and missing something key :)

LuckySevens
Feb 16, 2004

fear not failure, fear only the limitations of our dreams

Thanks guys, Grey Area that's an awesome explanation and exactly what I was looking for, much thanks for taking the time to type it :)

LuckySevens
Feb 16, 2004

fear not failure, fear only the limitations of our dreams

[st]Newb question, whats the logic behind if __name__ == '__main__'? It seems like some kind of way to test code but I can't quite work it out :([/st]

cancel that, I've worked it out!

LuckySevens fucked around with this message at 09:22 on Oct 17, 2008

LuckySevens
Feb 16, 2004

fear not failure, fear only the limitations of our dreams

noob question time again, since the last answers I got were pretty darn good :)

I'm trying to write a quick function to sort and return filenames in a directory by order of file extension. I tinkered myself for a little while but couldn't get anything to work cleanly, so I did some google cheating and found this:

code:
def cmp_extension(path0, path1):
     return cmp(os.path.splitext(path0)[1], os.path.splitext(path1)[1])

def print_dir_by_ext(dir_path):
     for name in sorted(os.listdir(dir_path), cmp_extension):
           print os.path.join(dir_path, name)
Of course, when inputting the dir I want to evaluate, it returns a perfect output. The only problem is, I don't quite understand exactly how this is work, I usually like to chart it in my mind how the input data is bouncing and for some reason this simple sorter function is doing it to me. I see that for every path/file we pull from an os.listdir on the dir_path we provide is being sorted, by how does cmp_extension fit into what is going on? os.path.splitext is obviously spliting the dir and file extension, and then it gets rebuilt before printing, but what is the path0, path1 values and where are they coming from? Such a noob :(

LuckySevens
Feb 16, 2004

fear not failure, fear only the limitations of our dreams

At first I read the explanation and I was like "wow programmer guys love talking about how they'd do it", but by breaking the problem down into a different method its helped me think and understand whats going on. I was a little bit clueless on the sorted() function parameters and its starting to click now. Very cool how much versatility (and shortcuts) inbuilt functions can have.

Thanks guys, and once again, such a good explanation there may be another q soon :q:

LuckySevens
Feb 16, 2004

fear not failure, fear only the limitations of our dreams

Pie Colony posted:

This may be a stupid question but the one thing I don't like about teaching myself a language vs. taking a class is that you never have an opportunity to make sample programs, something that I feel really helps me in reinforcing the education. Is there like a collection or list or something of small programming assignments designed to test your knowledge?

Check out the python forums: http://python-forum.org/pythonforum/index.php

My education was just opening up already built programs, trying to understand them, then coding my own additions and seeing if I could get it to work. Its fun, you get to see how other people code, as well as code yourself, and you even get something useful at the end of it.

LuckySevens
Feb 16, 2004

fear not failure, fear only the limitations of our dreams

tef posted:

andLinux? you can run it within windows and it plays reasonably nicely

32bit only though.

LuckySevens
Feb 16, 2004

fear not failure, fear only the limitations of our dreams

Is there an alternative to mod_python, since I'm running 2.6? I found some vague info from google on how to alter the install, but I can't get it to work.

LuckySevens
Feb 16, 2004

fear not failure, fear only the limitations of our dreams

Why are people pissed over Tornado?

LuckySevens
Feb 16, 2004

fear not failure, fear only the limitations of our dreams

Write a .bat file?

LuckySevens
Feb 16, 2004

fear not failure, fear only the limitations of our dreams

GregNorc, you should try out the python forums beginner section, when I first started programming that's what I did, it allowed me to ask dumber questions and there's a lot of people willing to slowly explain stuff to you.

They don't mind if you ask 10 questions a day.

LuckySevens
Feb 16, 2004

fear not failure, fear only the limitations of our dreams

tef posted:

Python by default doesn't care about the death of children:

:rimshot:

LuckySevens
Feb 16, 2004

fear not failure, fear only the limitations of our dreams

I'm using urllib2 to download a rar file, but its coming up as corrupt despite it being the correct size. What's something special I need to do?

LuckySevens
Feb 16, 2004

fear not failure, fear only the limitations of our dreams

tripwire posted:

Are you writing it to a file in binary mode or ascii?

Binary, using wb mode.

LuckySevens
Feb 16, 2004

fear not failure, fear only the limitations of our dreams

yeah good idea, i'll give that a whirl

LuckySevens
Feb 16, 2004

fear not failure, fear only the limitations of our dreams

if in doubt, just use 2.6 for now, its better supported.

LuckySevens
Feb 16, 2004

fear not failure, fear only the limitations of our dreams

I'm creating a range of dates for a stock market script, I need a tuple with a date, then another date 10 days back from the original excluding weekends (and ideally any time when the market is closed), another 10 days back from the last date in the tuple, etc. I've been looking at the docs and the only way I can think of to do it is to extract the name of the days between the date ranges and adjusting how many days I go back depending on whether a saturday or sunday turns up, but in my coding so far it doesn't seem very elegant nor would it take into account public holidays (which would probably require a preloaded list of excluded dates to be checked against which make my little method even more cluttered).

Is there an easy way to do this or am I doing this the hard way?

LuckySevens
Feb 16, 2004

fear not failure, fear only the limitations of our dreams

ChiralCondensate posted:

The datetime module has some helpful stuff for counting back and forth on the calendar, if you haven't found it already. You could make a date object out of the starting date, and decrement it by a timedelta(days=1) instance, counting off those days that aren't weekends (date.weekday() not in (5,6)) and aren't in a list_of_public_holidays.

Nice, what I was looking for, thank you

LuckySevens
Feb 16, 2004

fear not failure, fear only the limitations of our dreams

Stupid python question, I'm sorting a 2x20 list with a string and a % integer, and I want to sort by the absolute value and put the highest 5 entries into a list. I suck at this but this is what I came up with:

code:
def bigmovers():
    list = __request('ST')
    change = []
    for m in list:
        change.append([m[2], m[20]])
    sorted(change, key=lambda stocks: stocks[1])
    return change
Now this sorts by value, negative and positive, but not absolute value. It won't let me just add abs(stocks[1]) to the lambda like I thought it might in my head, obviously for a good reason. Any ideas?

LuckySevens
Feb 16, 2004

fear not failure, fear only the limitations of our dreams

ah, i think its screwing up because my values have % symbols, ill just parse those. duh :P

LuckySevens
Feb 16, 2004

fear not failure, fear only the limitations of our dreams

I'm having some stupid problem where I installed psycopg2, it said I need mx.datetime which I thought I had, so I re-installed that and now python can't see the module psycopg2 and won't import it. Other modules in my python2.6 folder import, what could cause this?

edit: pythonpaths are correct, module is where all the others are

LuckySevens fucked around with this message at 00:11 on May 4, 2010

LuckySevens
Feb 16, 2004

fear not failure, fear only the limitations of our dreams

Yeah I did that and now it works. Why I don't know :(

LuckySevens
Feb 16, 2004

fear not failure, fear only the limitations of our dreams

uh, don't you have to call a method in Round(), with P1Round() instead of P1Round?

edit: you're not assigning the values properly in Round() at all, how does it know what P1Action is?

LuckySevens
Feb 16, 2004

fear not failure, fear only the limitations of our dreams

systran posted:

Thanks guys.. I tried everything you were saying and it still wasn't working, then I noticed my attempt to execute "Round" was written as "Round" instead of "Round()" :)

You're trying to return a value I presume, but that's not what's happening here:

code:
def Round():
    P1Health = 5
    P2Health = 5
    while P1Health > 0 and P2Health > 0:
        P1Turn
        P2Turn
        if P1Action == 1 and P2Action == 1:
            P1Roll = random.randint(1,12)
            P2Roll = random.randint(1,12)
            if P1Roll > 6 and P2Roll > 6:
P1Turn doesn't mean anything, and even if it was P1Turn() you're just executing that method which asks the user for a value but that value isn't going to be inside of Round() for you to call it as a variable.

LuckySevens
Feb 16, 2004

fear not failure, fear only the limitations of our dreams

Dicionaries are just lists that are not ordered and accessible by a key, ala { key : value }. Just play with them, they're fairly simple to use and you can do some python tricks to extract the data into lists or get values using a string name etc etc.

LuckySevens
Feb 16, 2004

fear not failure, fear only the limitations of our dreams

haha I never thought of that

LuckySevens
Feb 16, 2004

fear not failure, fear only the limitations of our dreams

FredMSloniker posted:

I just decided to take a look at this thread because I'm pondering learning a programming language besides BASIC and writing a game with it. However, the OP hasn't been updated since April 2010, and the guy who started the thread hasn't been around September 2010. Anyone willing to volunteer to start a new thread so we can get an up-to-date OP?

What exactly needs to be updated? Looks like everything it needs is there.

LuckySevens
Feb 16, 2004

fear not failure, fear only the limitations of our dreams

FredMSloniker posted:

I think not posting since September 29, 2010 constitutes a bit more than 'not posting a lot'. Even if nothing in the OP needs to be changed (and there is at least one change that even my newbie eyes can see: Python's now on versions 2.7 and 3.2), it'd be good to have it in the hands of someone who can make any necessary changes.

That said, if nobody wants to volunteer to do it, welp.

Programming languages and resources don't change radically, by design. They don't want to break methods/compatibility concerns without fair warning. They try to remain as constant as they can, with meaningful changes taking many years to come through.

If you're looking for the most 'up to date' resources, they're all their in the OP; there's nothing that needs changing. The best things for a newbie are all there, download python and start working through it. Even if you looked at a python thread OP from 4-5 years ago, it would still be useful.

If you wanted more guides or something, well, us programmers have a phrase: DRY. Don't Repeat Yourself. All the best stuff is in the OP and that's not going to change.

LuckySevens
Feb 16, 2004

fear not failure, fear only the limitations of our dreams

I stumbled onto m0nk3yz homepage, and lost 3 hours of my life reading his python good to great collection.

Exactly the types of topics I was looking to take my python knowledge to the next level.

Adbot
ADBOT LOVES YOU

LuckySevens
Feb 16, 2004

fear not failure, fear only the limitations of our dreams

tef posted:

Cool links

Really good stuff, the sort was particular interesting, I like the way all the considerations are framed, in a 'see: its not so complicated when we break down the problem into smaller, logically separated chunks' kind of way, enlightening to see the thinking itself.

  • Locked thread