|
JetsGuy posted:Quick (stupid) question. I want to make a bunch of lists before going into a loop (that will append data into these lists). I would consider using a dictionary. code:
|
# ? Dec 6, 2012 00:21 |
|
|
# ? May 8, 2024 14:50 |
|
or even a two-level dictionary: data['oct']['x'] data['oct']['xerr'] If you're ever string-concatenating to generate dict keys, chances are you should just add another level of dict.
|
# ? Dec 6, 2012 00:25 |
|
JetsGuy posted:Quick (stupid) question. I want to make a bunch of lists before going into a loop (that will append data into these lists). Python code:
Python code:
Some better dict structure would probably be better, as posted above. evilentity fucked around with this message at 00:32 on Dec 6, 2012 |
# ? Dec 6, 2012 00:30 |
|
No Safe Word posted:or even a two-level dictionary: This is the approach I would recommend.
|
# ? Dec 6, 2012 01:12 |
|
Thanks for all the suggestions guys, I really need to learn how to (better) use dictionaries! At one point or another I did a few exercises with them, but just haven't used them since. As you can see with the snippet I gave earlier, I generally just make arrays with obvious names. Clearly, the best idea is to start using dictionaries more often! Thanks again!
|
# ? Dec 6, 2012 19:51 |
|
I am really enjoying loving around with pyside/qt. I'm sure I'd be loving around with pyqt instead but including binary installers that are designed for the native python interpreter on mac os , is an instant thumbs up from me.
|
# ? Dec 7, 2012 06:15 |
|
Hi, ultra noob here. I got about 90% of the way through "Learn Python the Hard Way" and want to finish the journey to becoming a beginner doing something more interesting than Zork clones. Specifically, I want to get started in web dev. What are some good tutorials for simple python web apps? I think the term I'm looking for is CRUD. I'd be happy learning how to build a comment system from the ground up, or a web forum, or a twitter clone, anything like that. One project that I would be really motivated for is a really simple recipe/weekly menu app. Stuff like click a button, and it selects seven dinners from recipes in the MySQL database or whatever it ends up being and poops out a grocery list of the ingredients. I would use that all the time, and I could play around with extending it by putting nutrition info in for each of the ingredients or w/e. I need help with the real basic stuff, like what language features I should be aware of. I've only worked with stuff outputting to a terminal, not creating html with it. Does anyone have any good examples of (preferably small) code for web apps? Any git hub repos I should check out--git hub is not built for browsing and my searches aren't fruitful. I'm aware of Django, and know it is something called a web framework, but I would rather build something myself and know how it works before I start having a framework build things for me. I'd like to learn it at a more fundamental level without going so far as to write it C or something. My goal is to learn for the fun of learning, so I'm relatively language agnostic, but I'd prefer to stay with python because I've got a start in it. Would I be better off learning PHP to start with?
|
# ? Dec 8, 2012 08:39 |
|
Consider starting with a framework like Flask, which makes you do a bit more low-level plumbing rather than having your hand being held all throughout. Bottom-up learning doesn't really go well for something as broad and general as "the web". Once you've mastered the basics, then you can dig deeper.
|
# ? Dec 8, 2012 09:05 |
|
Delicate Stranger I would suggest to start with Django, make your recipe site, and then dig into the source and see how Django does what it does. You could write your own drop in replacements for the Django engine piece by piece if you wanted to. IMHO it's not very realistic to try to build something as complex as twitter from the ground up as your first web project.
|
# ? Dec 8, 2012 15:03 |
|
Delicate Stranger posted:Would I be better off learning PHP to start with? Speaking as someone who learned PHP before taking up Python, I would say no - especially since you are already comfortable with Python, you are better off checking out the Python options for the web. PHP tries to make certain things easy for you when you are starting out with web programming, but it also makes it very easy for you to do things poorly, and it has a lot of "gotchas" and poor design decisions that you don't really get fully acquainted with until you have got far enough in that it would be a pain in the rear end to drop PHP and transition to something else. Go with something more well-thought-out, like a mature Python framework. You will be glad you did, even if it holds your hand slightly less at the start.
|
# ? Dec 8, 2012 16:34 |
|
Check out the Udacity's Web Development course if you got the time. I've been watching it and it's a real joy. It's a great roundup on the subject, it uses Python and Google App Engine. With the solid foundations you get here I'd guess jumping into any framework like Flask or Django would be much easier.
|
# ? Dec 8, 2012 17:28 |
|
Sounds like the consensus is to learn a framework like Django or Flask and then, piece by piece, learn the magic it's doing under the hood. As necessary. My impression had been that web frameworks were more for reducing basic, highly common stuff like creating a blog to extremely few lines, rather than helping build arbitrary, random web apps. I thought for something like that I'd have to learn the plumbing, since my recipe app idea wasn't a cms or comment box or something common. I'd been googling down blind alleys with search terms like cgi etc. I wasn't really interested in learning PHP if I didn't have to after skimming http://me.veekun.com/blog/2012/04/09/php-a-fractal-of-bad-design/, but I was hoping that it would lead to more pro-python responses by mentioning it, haha. Delicate Stranger fucked around with this message at 19:58 on Dec 8, 2012 |
# ? Dec 8, 2012 19:54 |
|
Delicate Stranger posted:Sounds like the consensus is to learn a framework like Django or Flask and then, piece by piece, learn the magic it's doing under the hood. As necessary. PHP was invented so HTML authors could easily add backend functionality to their pages using a style that was convenient and comfortable. This is fine for Joe's Homepage (the "H" in "PHP/FI"), but this approach obviously started to have problems as more entire websites started to be built as dynamic applications. Vulture Culture fucked around with this message at 20:17 on Dec 8, 2012 |
# ? Dec 8, 2012 20:15 |
|
I would start with Flask. Django is for big projects and if you're just starting out you're going to spend more time learning Django than you are learning web development.
|
# ? Dec 8, 2012 20:33 |
|
Django also does too much hand-holding and magic, I think (I still don't understand the broken logic behind making your entire application need a magic envvar). Flask is a nice middle-ground, since it has lots of helpers, but you don't have to use them. So you don't have to use the templating layer in Flask and just write raw HTML or something, but then you're vulnerable to XSS. That's probably a valuable learning experience for you. Pretty much the only non-optional thing is the routing layer, which is so boring and bland you don't want to write one yourself. Werkzeug, Flask's plumbing layer, is even more low-level, but I wouldn't recommend starting there until you have an understanding in the fundamentals.
|
# ? Dec 8, 2012 20:48 |
|
I'm making a little headway on my first real programming thing but I've hit something of a wall. I'm working on a little website/desktop app combo that will allow you to easily copy/paste between machines so long as both are on the internet. The site will also contain a searchable record of your clipboard and such. I have most of the website done and am starting to work on the desktop part. Ideally, I'd like to make this thing available on mac/windows/ubuntu at a minimum since that is what I bounce between from day to day. My idea was to just have this thing run as a tray app that allows you to authenticate and set up two keypress combinations. One would copy some text and then send that text to the database, the other would retrieve the most recently "copied" item and set that data to the clipboard and then paste it. I've got the whole copying/pasting thing down for the most part but I'm struggling to figure out the best or even a good way to have a python script run in the background and listen for a specific keypress combination. I know this is going to be different for each of the operating systems, so any advice for any of them is more than welcome.
|
# ? Dec 9, 2012 05:15 |
|
The March Hare posted:I'm making a little headway on my first real programming thing but I've hit something of a wall. I'm working on a little website/desktop app combo that will allow you to easily copy/paste between machines so long as both are on the internet. The site will also contain a searchable record of your clipboard and such. I have most of the website done and am starting to work on the desktop part. Ideally, I'd like to make this thing available on mac/windows/ubuntu at a minimum since that is what I bounce between from day to day. It may be a lot of work, but if Qt can do it, it shouldn't be that difficult to abstract.
|
# ? Dec 9, 2012 08:41 |
|
I'm almost done with LPTHW myself, but there's something I can't seem to get a grasp of mainly, when and when not to use self in functions. I'm also wondering if I'm missing something about scope. My only experience is Javascript before this, which was a lot simpler about this... For example, a very simple dice game: Python code:
However, if I want to call DiceGame.roll() somewhere, it tells me: TypeError: unbound method roll() must be called with DiceGame instances as first argument (got nothing instead) Now I've tried every combination of self and not using self and nothing works. What am I missing? Daynab fucked around with this message at 10:51 on Dec 9, 2012 |
# ? Dec 9, 2012 10:37 |
|
If the indenting in your quote is correct, start and roll aren't members of the DiceGame class. Indent them in line with def __init__ and you should be sweet. edit; err well I guess you've got some mix-ups with other things too, but the fixes depend on how you actually want to do things. Usually you'd do something like Python code:
If you want to do it the way you have it now (which will still work), you'll need to indent as I mentioned above and change calls from roll(self)/start(self) to self.start() and self.roll(). Rather than calling DiceGame.roll() you'd be doing: Python code:
The Gripper fucked around with this message at 10:56 on Dec 9, 2012 |
# ? Dec 9, 2012 10:40 |
|
Woops, I had actually fixed the indentation but forgot to fix it in my post - still made that error though. I changed it how you said but now it calls the whole thing just with the last line, which I don't understand. What if I just want to call that roll method without calling the whole thing? I get the same error as before. Python code:
Daynab fucked around with this message at 11:07 on Dec 9, 2012 |
# ? Dec 9, 2012 11:01 |
|
The __init__ is run whenever you create an instance of that class, so you can use it to set up any local variables (or start things, like you're doing). It won't run every time you call members of the class, just when it's created/initialized. Because you're calling start from inside __init__, you don't get a chance to actually call game.roll() separately. Honestly I'd remove self.start() from __init__ (you can remove __init__ altogether), and then you can actually start the game by calling game.start() after game = DiceGame().
|
# ? Dec 9, 2012 11:07 |
|
The Gripper posted:The __init__ is run whenever you create an instance of that class, so you can use it to set up any local variables (or start things, like you're doing). It won't run every time you call members of the class, just when it's created/initialized. Okay, this explanation makes a lot of sense, thanks a lot! Now I've only to figure out when to use self and when to not.
|
# ? Dec 9, 2012 11:10 |
|
Daynab posted:Okay I looked at your code and it works great, but now if I simply call DiceGame or an instance of it, nothing happens. Do I have to choose between being able to call methods of a class or running the whole class, I can't have both options at once? So I guess yeah the choice is whether you want the game to start automatically as soon as the class is created by calling self.start() in __init__, or being able to start the game whenever you want by creating an instance of the class then calling start() afterwards on your own. Is there a particular thing you want to do but aren't able to, this way?
|
# ? Dec 9, 2012 11:15 |
|
Whenever you use self, you're storing that value with the object. At a real simple level, do you need to store a value to be used by other methods? In your roll method, you use self.dice_result yet you don't need to access self.dice_result anywhere else. You'd be fine with just dice_result. On the other hand if you wanted to keep a history of all the results in the current session, you could do this: edited: __init__() was in the wrong order Python code:
Maluco Marinero fucked around with this message at 11:34 on Dec 9, 2012 |
# ? Dec 9, 2012 11:18 |
|
Thank you both for the explanation, that clears a lot of things up. However Maluco Marinero when running your code I get a AttributeError: 'DiceGame' object has no attribute 'dice_results'
|
# ? Dec 9, 2012 11:32 |
|
Ah woops, put the init line 'self.dice_results = []' before 'self.start()'
|
# ? Dec 9, 2012 11:33 |
|
One of the things new people to Python find confusing with self is that it's an argument that you don't specifically have to pass to the method (so they have no idea what it is or where it comes from). Consider this example: Python code:
|
# ? Dec 9, 2012 11:36 |
|
The Gripper posted:Basically instance.method_name() is shorthand for ClassName.method_name(instance), instance is actually passed to the method as argument 1, self. Okay, mind blown.
|
# ? Dec 9, 2012 11:45 |
|
Suspicious Dish posted:Django also does too much hand-holding and magic, I think (I still don't understand the broken logic behind making your entire application need a magic envvar).
|
# ? Dec 9, 2012 17:58 |
|
My Rhythmic Crotch posted:What magic variable are you referring to, the CSRF token? DJANGO_SETTINGS_MODULE
|
# ? Dec 9, 2012 18:46 |
|
Suspicious Dish posted:DJANGO_SETTINGS_MODULE Thats needed so the contrib apps (as well as the rest of the framework) can access your database settings (among other things). How else should the framework go about achieving this?
|
# ? Dec 9, 2012 19:32 |
|
Why can't one part of the app access one database, and another the other? Global settings prevent this. I'd love to tell each part of my code which database it should be using, by passing it in as a parameter, not as a global. The same go for all global settings. There might be instances where I want one setting over here, another setting over here. The hacks that you do to get a one-file Django project are atrocious.
|
# ? Dec 9, 2012 19:40 |
|
I think it's a carry-over from Django's days of carrying multiple news sites on the same codebase. It's also needed by mod_wsgi or its equivalent.
|
# ? Dec 9, 2012 21:13 |
|
After dicking around with Django as my first foray into web development I'm beginning to come more and more around to Suspicious Dish's thoughts about it. I mean, it's nice for what it is, and I want to be freed from as much tedium as possible, but...every time I try to do something a little out of the ordinary it feels like I'm fighting with the framework and using magic incantations. So, if I want to do print website, its cool, but if I want to print website + be_a_little_crazy() it just seems like I'm torturing the framework. I mean, maybe that's the tradeoff you have to make between something that covers all the tedium and something that doesn't but lets you do things the designers didn't plan for. I dunno, I'm still too new at web dev to say for sure. Googlin' leads me to believe theres some plugins (or whatever you call them) for Flask to automate creating admin pages...are any of those any good?
|
# ? Dec 9, 2012 21:30 |
|
Yeah, I'm installing Django and preparing to play with it. I do eventually want to learn it. But bottom line is I'm trying to just put links/buttons on a page, be able to click them, and have stuff from a database appear on a new page load. My googling isn't bringing up anything on that level. I'm thinking I'll continue to play with Django and see what kind of code pops out of it, and reverse engineer, but that seems a little bass-achwards for what I really want to accomplish short term. It won't be wasted time but delayed gratification. Slightly different topic. I hate LPTHW. I don't even want to finish it. What books would you recommend? Head First into Python looks interesting, but it's working with Python 3 and Django doesn't support that yet.
|
# ? Dec 9, 2012 23:53 |
|
Delicate Stranger posted:Slightly different topic. I hate LPTHW. I don't even want to finish it. Any specific reason? Delicate Stranger posted:What books would you recommend? Head First into Python looks interesting, but it's working with Python 3 and Django doesn't support that yet. I liked and will recommend Think Python.
|
# ? Dec 10, 2012 00:03 |
|
Suspicious Dish posted:Any specific reason? Eh, that came off kind of petulant. He's a great guy to put it out there for free. But honestly every lesson seemed to leave me with more questions than answers. That's to be expected in the beginning, but a good deal of the style seemed to boil down to: 1. Here, type in the this code VERY CAREFULLY (DON'T copy and paste). 2. Well isn't that neat! What do you think it did? 3. Next lesson! ...I get that programming is hard, and involves a lot of googling even after becoming proficient, but the mark of a good book, to me, seems to be anticipating the most frequently asked questions and addressing them. Zed seems to find that the biggest sticking point for people is accuracy in typing? Boggles my mind a little. I mean I don't doubt it but it never was the question I was having. Another mark of a good book is providing the vocabulary to make for effective googling/stack overflow questions. One of the reoccurring extra credit questions is to comment above every line and write out in english what it's doing. I got stuck almost immediately because I couldn't tell what to call things other than "thinger" or "this bit here." That might sound like a minor gripe but not saying what a thing is, it's name and the category of things it belongs to, really frustrates my ability to reason about it and internalize it. My ideal book would have tons of examples of individual concepts and a handful of fully baked tiny projects with source code I can take apart, rebuild, modify, and learn from to assist in building the first from-scratch programs I attempt. Preferably for web apps, but I've discovered learning to program is going to be two steps forward one step back for a much longer time than I thought. The actual python documentation seems to be pretty good about giving code examples, so I have that as a resource. I'm not enthused to finish the book because I anticipate that I'll be in much the same place as I am now--I think of a project I want to do and don't even know what parts of the language I'll be using to implement a solution. I'll check out the book you recommend. edit--I like this author's style a lot better, there's many small code examples and they seem pretty well explained. Delicate Stranger fucked around with this message at 00:42 on Dec 10, 2012 |
# ? Dec 10, 2012 00:17 |
|
I get what you mean about LPTHW and it's pretty much the only thing I didn't like about it (it's a lot more fun to learn codecademy.com-style, but obviously a lot less complete so that's why I've went through this) but I find that it usually answered questions on the next lesson for some reason. Like he'll introduce a new concept, have you type it, not explain it and then on the next one explain it.
|
# ? Dec 10, 2012 00:51 |
|
Delicate Stranger posted:edit--I like this author's style a lot better, there's many small code examples and they seem pretty well explained. Zed's right, to a point. Most of your programming days are going to be reading API documentation and figuring out how to glue pieces together, and building that broad understanding and terminology yourself, but maybe diving right in without a lot more hand holding the first time is too sudden. Glad you're liking Think Python, though! I've read through it multiple times, and I still think it's a great piece of book.
|
# ? Dec 10, 2012 01:13 |
|
|
# ? May 8, 2024 14:50 |
|
Delicate Stranger posted:Yeah, I'm installing Django and preparing to play with it. I do eventually want to learn it. But bottom line is I'm trying to just put links/buttons on a page, be able to click them, and have stuff from a database appear on a new page load. My googling isn't bringing up anything on that level. edit: Dive Into Python is not very highly recommended, for reasons stated on the next page. WorldIndustries fucked around with this message at 01:40 on Dec 10, 2012 |
# ? Dec 10, 2012 01:20 |