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
m0nk3yz
Mar 13, 2002

Behold the power of cheese!

Mido posted:

Django!! :woop:

Django.

Adbot
ADBOT LOVES YOU

Stabby McDamage
Dec 11, 2005

Doctor Rope

Thermopyle posted:

I hate web design with a passion.

What's the best library or framework for abstracting away as much of it as possible?

Something useful for getting input from a user via forms or whatever web magic is possible and displaying results to them.

I'm not horribly concerned with making it beautiful, just functional.

Everyone posted:

Django

I'm not sure this is the right answer. He said he hates web design. Django doesn't remove any web design...you still have to write all the HTML and CSS yourself; the templating just makes it easier.

If you don't want to touch HTML, I think you'd want something higher level. I'm not sure what that is, but I'd be very surprised if there wasn't some framework for making CRUD apps without HTML.

bitprophet
Jul 22, 2004
Taco Defender

Stabby McDamage posted:

I'm not sure this is the right answer. He said he hates web design. Django doesn't remove any web design...you still have to write all the HTML and CSS yourself; the templating just makes it easier.

If you don't want to touch HTML, I think you'd want something higher level. I'm not sure what that is, but I'd be very surprised if there wasn't some framework for making CRUD apps without HTML.

Thermopyle posted:

Something useful for getting input from a user via forms or whatever web magic is possible and displaying results to them.

I'm not horribly concerned with making it beautiful, just functional.

That says to me he's doing the usual conflation of "web design" with "web development" and doesn't actually mean the visual design aspect of things.

So he probably does want Django :shobon:

Regarding the HTML/CSS crap one does have to deal with either way, something like Blueprint CSS can help make a simple site look significantly better than CSS-less HTML will, with almost no work necessary. Certainly from a typography and layout perspective.

MaberMK
Feb 1, 2008

BFFs
Having done quite a bit of Django recently, I do not understand the appeal at all. In fact, I hate it.

Among my many complaints:

  • No request dispatch... you're responsible for dictating user-accessible functions "up-front" by defining routes manually
  • Corollary to above, requests aren't rigorously tied to function prototypes because you access the request parameters directly
  • Doesn't leverage the object-oriented-ness of Python, uses separate so-called "applications" for distinct parts of what is actually a single application
  • User model is not intended to be extended directly, resulting in multiple tables for storing user information
  • ORM pales in comparison to SQLAlchemy, yet they continue to develop it
  • Template language doesn't evaluate Python
  • Controllers are called "views" (yes, I'm aware of the reasoning behind this)

Use TurboGears. It does a lot more work for you.

MaberMK fucked around with this message at 17:19 on Jun 18, 2010

king_kilr
May 25, 2007
I'm sorry, was that a list of complaints, or a list of the best features of Django?

Thermopyle
Jul 1, 2003

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

Stabby McDamage posted:

If you don't want to touch HTML, I think you'd want something higher level. I'm not sure what that is, but I'd be very surprised if there wasn't some framework for making CRUD apps without HTML.

This is more along the lines of what I'm looking for.

In my ideal world, I'm looking for "print" and "raw_input" for the web.

bitprophet posted:

That says to me he's doing the usual conflation of "web design" with "web development" and doesn't actually mean the visual design aspect of things.

So he probably does want Django :shobon:

Regarding the HTML/CSS crap one does have to deal with either way, something like Blueprint CSS can help make a simple site look significantly better than CSS-less HTML will, with almost no work necessary. Certainly from a typography and layout perspective.

I don't want to do web development or design.

If that's not possible, then I'm looking for the thing that gets me the closest.

angel opportunity
Sep 7, 2004

Total Eclipse of the Heart
This is really pissing me off, I will only post the parts of the code I feel are relevant to the problem, but let me know if you need to see more. I have:

code:
def P1Status():
    print(P1 +'\'s Health: ' +str(P1Health)
                  
def P2Status():
    print(P2 +'\'s Health: ' +str(P2Health)
When I try to run this, it highlights the SECOND def and says "syntax error", how is it possible that there is no syntax error in the first one but there is in the second one? What could the syntax error even be?

wins32767
Mar 16, 2007

You need another ) at the end of the first (and second) print lines.

angel opportunity
Sep 7, 2004

Total Eclipse of the Heart
Thanks, I got rid of all my syntax error but now I can't get it to run.

code:
import random

print('Player 1, enter your name.')
P1 = input()

print('Player 2, enter your name.')
P2 = input()

def P1Status():
    print(P1 + '\'s Health: ' +str(P1Health))
                  
def P2Status():
    print(P2 + '\'s Health: ' +str(P2Health))

def P1Turn():
    #"Turn" is a standard turn for the player with all four options
    print(P1 + ' ,it\'s your turn.  Input your action.  1 = attack, 2 = wait, 3 = charge shot, 4 = cheap shot')
    P1Action = input()
                  
def P2Turn():
    #"Turn" is a standard turn for the player with all four options
    print(P1 + ' ,it\'s your turn.  Input your action.  1 = attack, 2 = wait, 3 = charge shot, 4 = cheap shot')
    P2Action = input()
    
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:
etc. etc.

It goes on for the combinations of all four possible actions, then after I finish defining Round, I just try to run it (so I define everything as seen above including the really long "Round", then after that I just have "Round" at the bottom in an attempt to run what I just defined). When I run the program it asks for the players' names and stores them, then it defines everything up to round, defines round, then it does nothing when it runs round. I've tried setting it as "while True" and it still won't run. I even took it out of the def and just tried to run it by itself and it won't run. Is there anything obviously wrong here that is preventing it from running?

angel opportunity fucked around with this message at 23:43 on Jun 18, 2010

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?

angel opportunity
Sep 7, 2004

Total Eclipse of the Heart
Sorry, I've only been learning Python for a few days. I'm reading tutorials on localscope/namespaces etc. and I'm trying to figure out how to apply this to Round(). I'm not sure how to apply what you just said, do I need to put parameters into Round() and P1Turn(), if so which ones?

Fayk
Aug 2, 2006

Sorry, my brain doesn't work so good...

systran posted:

Sorry, I've only been learning Python for a few days. I'm reading tutorials on localscope/namespaces etc. and I'm trying to figure out how to apply this to Round(). I'm not sure how to apply what you just said, do I need to put parameters into Round() and P1Turn(), if so which ones?

I believe he's saying that here:

quote:

def Round():
P1Health = 5
P2Health = 5
while P1Health > 0 and P2Health > 0:
P1Turn
P2Turn

You want "P1Turn()" not "P1Turn"

angel opportunity
Sep 7, 2004

Total Eclipse of the Heart
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()" :)

m0nk3yz
Mar 13, 2002

Behold the power of cheese!

king_kilr posted:

I'm sorry, was that a list of complaints, or a list of the best features of Django?

Seconding this. I do not like the routing stuff, and I despise templates (meaning, code in HTML) that evaluates real, unrestricted python. Everything you listed, modulo the ORM (which I am completely ambivalent about), seems like a feature.

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.

angel opportunity
Sep 7, 2004

Total Eclipse of the Heart
I finished the program finally after going through a lot of bugs.

This was the first program I ever did, and I (obviously) am having a lot of trouble with namespaces.

The original way I planned out the program was to define a lot of functions ahead of time and then execute them on the global scope. The problem I kept running into was that I wanted the players' health to be accessible and modifiable; I couldn't figure out how to do this unless I just made everything inside Round().

My original program (that didn't work) had a bunch of functions defined ahead of time and then I called on them as needed. My final product threw everything into Round(). The game was basically a complicated Rock/Paper/Scissors style thing: There were four choices and you rolled dice. I had it where if you did "attack" and rolled an 11 or 12 you knocked the other player down. This resulted in the other player having very limited/poor options while you had several good options. I tried to just define a function P1Knockdown() and P2Knockdown(), then basically

code:
if P1Roll >=11:
    P1Knockdown()
I needed to insert this code for the knockdowns in several places, it ended up working but I couldn't find out how to make the damage that took place during a knockdown get back into Round(), so I ended up having to have the same giant block of text in several places at once. Assuming my explanation makes sense, would I have been able to do this within a function and still modify the players' health some how?

Lonely Wolf
Jan 20, 2003

Will hawk false idols for heaps and heaps of dough.
Read about the global keyword

tripwire
Nov 19, 2004

        ghost flow

Lonely Wolf posted:

Read about the global keyword

Better yet, don't, and just learn proper OO practices instead.

Lonely Wolf
Jan 20, 2003

Will hawk false idols for heaps and heaps of dough.
Teaching OO anything to someone at that level is useless at best and destructive at worst. Let him get to the point where he has a program that could benefit from OO, not just scream BEST PRACTICE! and make him throw a bunch of stuff that makes no sense in his program.

tripwire
Nov 19, 2004

        ghost flow
I disagree completely. What you learn first is inevitably going to be what you are most comfortable with and using globals all over the place without an appreciation for why its generally a bad idea is likely to instill bad habits in you.

It's not like I'm nitpicking about best practices, or even saying OO is necessary for small programs. It would be perfectly acceptable to use standard types like ints and strings rather than touching the class system, so long as you simply pass them around explicitly and learn not to rely on the global keyword.

I'm just saying that establishing a good foundation will pay off in the long run. systran mentioned that one of their goals was to be able to access/manipulate the values of each players life. While its technically possible to do this by writing procedural code and using global variables, it will probably end up being making for more work in the long run as soon as the complexity of your game rises past the bare minimum.


To systran:
Functions which don't have a "return" or "yield" keyword somewhere in their body will return None. If you want to set the value of a variable to the result of a function, you have to return that value in the function. Also, you should try to avoid the "input" function as it evals the data it receives. Instead, get in the habit of using "raw_input", and explicitly try to parse the data.

Finally, rather than copy-pasting player1's code and changing the 1 to a 2, try using generalized functions.

code:
#instead of this
def P1Turn():
    #"Turn" is a standard turn for the player with all four options
    print(P1 + ' ,it\'s your turn.  Input your action.  1 = attack, 2 = wait, 3 = charge shot, 4 = cheap shot')
    P1Action = input()


#you want this
def turn(player_name):
    #Takes a string of a players name, and returns their 
    #choice of move as an integer between 1 and 4 inclusive.
    message = player_name + ' ,it\'s your turn.  ' + \
        'Input your action.  1 = attack, 2 = wait, ' + \
        '3 = charge shot, 4 = cheap shot\n'
    action = raw_input(message)
    action_as_int = int(action)
    return action_as_int

king_kilr
May 25, 2007

Lonely Wolf posted:

Teaching OO anything to someone at that level is useless at best and destructive at worst. Let him get to the point where he has a program that could benefit from OO, not just scream BEST PRACTICE! and make him throw a bunch of stuff that makes no sense in his program.

So the solution is to get them using obscene hacks like globals() instead?

Lonely Wolf
Jan 20, 2003

Will hawk false idols for heaps and heaps of dough.
global not globals()

Having a few global variables in an incredibly small program that runs once and then dies is hardly a hack.

Better ways to structure code can be explored after he has the satisfaction of getting something working, but baby steps.

Systran, get something to work. Let it be ugly. When you're sure it works, figure out ways to clean it up and keep asking questions.

rawstorm
May 10, 2008

by Ozma
Is it possible to do:

code:
int[][] fun = {{2,4,5,6},
               {4,5,2,5},
               {2,8,3,1}};

In python so it looks pretty and isn't on one line?

king_kilr
May 25, 2007
Uhh, you mean:

code:
fun = [
    [1, 2, 3],
    [1, 2, 3],
    [1, 2, 3],    
]

rawstorm
May 10, 2008

by Ozma
^ Thanks.

I know that there is a hex(number) command in python to convert and integer into its hexadecimal representation. Is there a method that does this but converts the number into its binary representation?

RichardA
Sep 1, 2006
.
Dinosaur Gum
In python 3 bin(number) or bin(number)[2:] if you do not want 0b in front of it.

In older versions the python wiki suggests:
code:
def bin(a):
  s=''
  t={'0':'000','1':'001','2':'010','3':'011',
     '4':'100','5':'101','6':'110','7':'111'}
  for c in oct(a)[1:]:
    s+=t[c]
  return s

RichardA fucked around with this message at 09:58 on Jun 20, 2010

Scaevolus
Apr 16, 2007

RichardA posted:

In python 3 bin(number) or bin(number)[2:] if you do not want 0b in front of it.
Also in Python 2.6+

rawstorm
May 10, 2008

by Ozma
Thanks, just what I needed.

Scaevolus
Apr 16, 2007

rawstorm posted:

Thanks, just what I needed.

Also, int(string, base) will let you convert back to an integer, e.g. int('101001', 2).

angel opportunity
Sep 7, 2004

Total Eclipse of the Heart
Thanks guys, I did get the program to work without using global or without using return. I guess it's good to know that global exists, but I'm going to end up using this program as the combat engine to a much larger program, so I'm guessing I'll want to avoid it. Thanks for the suggestion to not copy/paste and change the numbers, as I was doing that I knew it wasn't the most efficient method, I've been understanding parameters better lately and will incorporate that into my next revision.

If I want to work on making an isometric grid should I use pygame?

Modern Pragmatist
Aug 20, 2008

tripwire posted:

I disagree completely. What you learn first is inevitably going to be what you are most comfortable with and using globals all over the place without an appreciation for why its generally a bad idea is likely to instill bad habits in you.

It's not like I'm nitpicking about best practices, or even saying OO is necessary for small programs. It would be perfectly acceptable to use standard types like ints and strings rather than touching the class system, so long as you simply pass them around explicitly and learn not to rely on the global keyword.

Completely agree with this. I am still waiting on an acceptable reason to use global variables.

Blotto Skorzany
Nov 7, 2008

He's a PSoC, loose and runnin'
came the whisper from each lip
And he's here to do some business with
the bad ADC on his chip
bad ADC on his chiiiiip
Are you familiar with singletons? Those are just globals in disguise

Modern Pragmatist
Aug 20, 2008

Otto Skorzeny posted:

Are you familiar with singletons? Those are just globals in disguise

Although a singleton is a safer and more OO approach to a global variable, I still feel that there are very few situations that would warrant the use of either of these.

Its just that I have yet to run into a situation where I can't refactor my code to avoid using globals. Conversely, I have run into many headaches when dealing with others' code that relies upon globals.

Lonely Wolf
Jan 20, 2003

Will hawk false idols for heaps and heaps of dough.

Modern Pragmatist posted:

Although a singleton is a safer and more OO approach to a global variable, I still feel that there are very few situations that would warrant the use of either of these.

Its just that I have yet to run into a situation where I can't refactor my code to avoid using globals. Conversely, I have run into many headaches when dealing with others' code that relies upon globals.

While a singleton is certainly more OO it is not inherently more or less safe. It's the same thing with a different name.

I gave a use for globals: short programs that run one and then quit, which I had thought systran's code was.

You can certainly refactor globals away. The question is, is it worth the added complexity? On the other hand, globals are like gotos. Generally to be avoided but if it's simpler to use it, use it. Furthermore, in the context of someone new to programming, refactoring code and data flows to avoid global state is hardly justifiable. If you show them how to do it without showing them why or letting them learn why, then you get a coder who codes by dogma not experience and reason.

The reason code with gotos and globals get a bad rap is because most of it is written by people who leap to them unreflexively without weighing the consequences or alternatives first. But bad code is bad code. That doesn't mean that the ingridients of bad code are themselves inherently bad. Bad code has plenty of if statements as well, after all.

Lurchington
Jan 2, 2003

Forums Dragoon
I have a program that has around 10-15 modules, and I want most of them to have access to command line options. I ended up having a global singleton instance of the (subclassed) OptionsParser that everything just imported.

I don't know if it's a good use-case for that kind of thing, but I tried setting up a whole bunch of __init__ statements passing in an OptionsParser instance, and it sucked.

angel opportunity
Sep 7, 2004

Total Eclipse of the Heart
Nevermind, fixed the problem

angel opportunity fucked around with this message at 05:02 on Jun 21, 2010

Captain Capacitor
Jan 21, 2008

The code you say?

Lurchington posted:

I have a program that has around 10-15 modules, and I want most of them to have access to command line options. I ended up having a global singleton instance of the (subclassed) OptionsParser that everything just imported.

I don't know if it's a good use-case for that kind of thing, but I tried setting up a whole bunch of __init__ statements passing in an OptionsParser instance, and it sucked.

Could you not, as part of __init__, pass sys.argv into each module's OptionParser?

Lurchington
Jan 2, 2003

Forums Dragoon

Captain Capacitor posted:

Could you not, as part of __init__, pass sys.argv into each module's OptionParser?

That's an alternative, and assuming you mean each module has an OptionParser with only the options relevant to it, wouldn't that wreck output on -h/--help at my program's point of entry?

If each module has its very own instance of the exact same OptionParser, then I didn't necessarily see how it was better unless I was slavishly "NO GLOBALS OR SINGLETONS"

Captain Capacitor
Jan 21, 2008

The code you say?

Lurchington posted:

That's an alternative, and assuming you mean each module has an OptionParser with only the options relevant to it, wouldn't that wreck output on -h/--help at my program's point of entry?

If each module has its very own instance of the exact same OptionParser, then I didn't necessarily see how it was better unless I was slavishly "NO GLOBALS OR SINGLETONS"

I'm not entirely familiar with the structure of your app, but if each sub module maintained its own Parser, the parent package could combine all of the options into one big OptionParser object.

Or, you could do like what some other projects do, have a specific "help" command that spits out the options for a module.

Adbot
ADBOT LOVES YOU

unixbeard
Dec 29, 2004

sometimes i use a global variable for a database connection

yet to suffer any material downsides to it

  • Locked thread