|
Thermopyle posted:Class When you're just starting with classes, think small. Don't think about all the crazy stuff they can do and just go with a basic yet functional struct replacement. Typical example you see in a textbook: code:
In order to use this class, you would simply make a new variable: code:
code:
Once you get the hang of it, you can augment the class very very quickly to do fun things.
|
# ? May 25, 2009 17:51 |
|
|
# ? May 9, 2024 07:04 |
|
spankweasel posted:Typical example you see in a textbook: It's an example of exactly how not to use object orientation in python. Don't re-invent existing collection structures (list/hash), and getters and setters are pretty much the root of all evil. If you are doing stuff in an OO style, you're meant to encapsulate the process entirely and expose it through actions, rather than poking around with the internals.
|
# ? May 25, 2009 19:17 |
|
Yea I thought getters/setters were bad form in Python. Keep that java nonsense outta here son!
|
# ? May 25, 2009 19:41 |
|
spankweasel posted:Ignore this post newbies. Coding like this will cause you brain damage and make you think like a java programmer. Before long you will be editing XML based configuration files and your wife will leave you and your dog will bite you. Heres the better way of doing it. Class definition. code:
code:
code:
Sorry spankweazle. Dont teach other people bad habits. Also rampant public property useage is sometimes considered bad OO by (non pythonic) puritans (since how do you concieve of it as 'message exchanges' without complete brainhurt), but OO purism is for smalltalk users but this is python and python programmers specialize in silly walks. Essentially the idea is that you (ab)use the class as a implicit struct. Its not true OO, but its a perfectly acceptable use of objects in python world. duck monster fucked around with this message at 19:59 on May 25, 2009 |
# ? May 25, 2009 19:48 |
|
code:
|
# ? May 25, 2009 19:49 |
|
Janin posted:
It seems the phrase "There's only one way to do it" has more to do with style than anything with python. That's kind of nice, still. I find the syntax for it slightly bizarre, but maybe that's just me.
|
# ? May 25, 2009 20:35 |
|
MononcQc posted:It seems the phrase "There's only one way to do it" has more to do with style than anything with python. code:
|
# ? May 25, 2009 20:50 |
|
tef posted:
Weell, except that __slots__ is a sequence that has to be present when the class is created. You could make a function that creates a new class with __slots__ and returns that, though. code:
MononcQc posted:It seems the phrase "There's only one way to do it" has more to do with style than anything with python. Not really. namedtuples are immutable, so you can't use the two interchangeably. Habnabit fucked around with this message at 21:18 on May 25, 2009 |
# ? May 25, 2009 21:16 |
|
There's never any reason to create a generic struct type. Python already has a type for unstructured associated data -- it's called `dict`.
|
# ? May 25, 2009 21:28 |
|
Ignore me. I read the flow of the thread wrong.
tbradshaw fucked around with this message at 23:40 on May 25, 2009 |
# ? May 25, 2009 22:52 |
|
duck monster posted:I agree with what you wrote 100%. I don't use getters and setters in my own code, however if the guys is asking about some basic class poo poo, I thought it would be mildly useful to see a lovely java implementation in python -for reference-. As people said, there's a million ways to do what he wants and since we don't know exactly what he wanted (other than "Classes are hard. hurr."), I went with a simple, stupid, java-based text book bullshit class as a silly example. One that might show up online when looking for class information. My mistake. I apologize for stepping out of line. Won't do any of that silly java poo poo again. (I even work for Sun^H^H^HOracle and I won't do it again ) Go with what Duck said. Be simple. It'll get the job done a thousand times faster than my bullshit java example. duck monster posted:Heres the better way of doing it.
|
# ? May 25, 2009 23:04 |
|
Thermopyle posted:I'm kind of a beginner, and this is my story. From what you've said it's safe to assume that the kind of things you're programming are fairly small in scale. For simple scripts that are smaller than a few hundred lines of code the benefits of classes really aren't obvious. Once you start getting into some more meatier projects the benefits classes will provide in structuring your data in a modular way will become much more apparent. Some OO zealots will apply classes to *everything* when really it's about using the right tool for the right job, classes aren't always the answer. What might also be beneficial to your understand is trawling through the source code for the standard library and seeing how OO is applied to various tasks there.
|
# ? May 25, 2009 23:49 |
|
jupo posted:From what you've said it's safe to assume that the kind of things you're programming are fairly small in scale. For simple scripts that are smaller than a few hundred lines of code the benefits of classes really aren't obvious. Once you start getting into some more meatier projects the benefits classes will provide in structuring your data in a modular way will become much more apparent. This is the correct answer. Code examples aren't needed to make this point.
|
# ? May 26, 2009 00:42 |
|
jupo posted:From what you've said it's safe to assume that the kind of things you're programming are fairly small in scale. For simple scripts that are smaller than a few hundred lines of code the benefits of classes really aren't obvious. Once you start getting into some more meatier projects the benefits classes will provide in structuring your data in a modular way will become much more apparent. This is true. Largest of my scripts are around 150 lines. Most of them are automation scripts like scraping something from a website and doing something with that, or organizing files, etc... I guess I'll have to think of a "meatier" projects.
|
# ? May 26, 2009 00:48 |
|
Habnabit posted:Not really. namedtuples are immutable, so you can't use the two interchangeably. It's more about one string used to name 3 fields: code:
code:
It was a bit harsh to go against the "there's only one way to do it", but the collections module is one kind of big contradiction to the motto. From the docs: http://docs.python.org/library/collections.html posted:The specialized containers provided in this module provide alternatives to Python’s general purpose built-in containers, dict, list, set, and tuple. Can't deviate more explicitely from "there's one way to do it" than that. Maybe it's just me.
|
# ? May 26, 2009 01:00 |
|
MononcQc posted:It's more about one string used to name 3 fields: You can use any iterable for the second parameter, if it's not a string it'll be treated as an iterable yielding strings. MononcQc posted:It was a bit harsh to go against the "there's only one way to do it", but the collections module is one kind of big contradiction to the motto. From the docs:
|
# ? May 26, 2009 01:46 |
|
Janin posted:You can use any iterable for the second parameter, if it's not a string it'll be treated as an iterable yielding strings.
|
# ? May 26, 2009 02:04 |
|
MononcQc posted:Makes more sense. I still don't get why anyone would or should use a string as the second parameter. That sounds like something even PHP wouldn't do. Is there any advantage to that?
|
# ? May 26, 2009 02:11 |
|
Janin posted:You type "" instead of ["", "", "", ""]. Since the set of characters that field names may contain is restricted to Python identifiers, the string can be unambiguously converted to a list. PHP uses strings even where they're inappropriate and cause information loss (dynamic function calling), so that's not an appropriate comparison. Seems to go against the idea of least astonishment or wanting to be explicit as much as possible. If you want a list, ask for a list. Anyway, I won't push this any further, I got the answers I asked for. The rest is more or less opinions on my end. Thanks for your time.
|
# ? May 26, 2009 03:45 |
|
I think of classes as simply collections of functions and variables that are sort of centered around a common purpose , or a library. Say you want to a do a lot of different math thingies, rather than writing a bunch of individual functions outside of a class you put them in a class so you can keep them in a certain collection of names. So you have a normal function get_dongs() and a class Dong with its own function get_dongs() called like so code:
|
# ? May 26, 2009 06:24 |
|
Sylink posted:So you have a normal function get_dongs() and a class Dong with its own function get_dongs() called like so There is an important thing to be learned from K&R and that is that motivating examples should be as un-contrived as possible so as to illustrate why something is necessary (or useful, etc) instead of just illustrating what it is.
|
# ? May 26, 2009 06:33 |
|
Sylink posted:Say you want to a do a lot of different math thingies, rather than writing a bunch of individual functions outside of a class you put them in a class so you can keep them in a certain collection of names. edit: generally.
|
# ? May 26, 2009 07:00 |
|
Something so obvious that I'm sure there must be an obvious solution that I'm missing: lexicographic sorting in Python. I have a set of menu options that should be sorted, which look like Set 1 Set 2 Set 3 ... Set 10 Set 11 And of course the standard Python source is giving me: Set 1 Set 10 Set 11 Set 2 ... Any favoured solutions?
|
# ? May 26, 2009 15:14 |
|
outlier posted:Something so obvious that I'm sure there must be an obvious solution that I'm missing: lexicographic sorting in Python. http://code.activestate.com/recipes/285264/ I don't know of any "standard" solution but that was the first hit for "python natural sort" and it looks okay enough
|
# ? May 26, 2009 15:56 |
|
outlier posted:code:
code:
code:
Scaevolus fucked around with this message at 16:33 on May 26, 2009 |
# ? May 26, 2009 16:25 |
|
No Safe Word posted:Actually what Python does is lexicographic sorting, you want "natural sort" Mea culpa, you are right. Thanks to both of you - that simplification looks like just the ticket.
|
# ? May 26, 2009 22:48 |
|
Sylink posted:I think of classes as simply collections of functions and variables that are sort of centered around a common purpose , or a library. Kinda. but not quite. This is more a name space than a class, and you can use class singletons as name spaces, kinda. Really the best way to think of objects is as little black box's that represent a particular thing or concept. In effect a tiny self contained program with multiple entry points. The program is defined in the class, and is made active as an object. So a class might be "Employee", and it holds data for a single employee, can save or retrieve itself from a database, and has functions like "Set pay rate". You might then make 20 employee objects, one for each employee, then have another object called "Payroll" which is a singleton that encapsulates a bunch of data about banks and money and poo poo, and perhaps an instance of a "ledger" object. When you want to pay an employee, you might send a message to that employee object something like john.pay_employee(john.weekly_rate) , note that this is a method call, but its best to think of methods as messages sent to that object. This becomes even more explicit (as message sending) in languages like smalltalk and Objective C. So after you send john.pay_employee(john.weekly_rate), the pay_employee method then sends a message to the payroll object, something like payroll.transfer(450.00 , self.account_number , "Weekly pay for John") which means pay john $450 via the enclosed account_number [extracted from my own properties] and put "Weekly pay for john" into the methods. The payrol might then poo poo out a bunch of data over the net to the bank , then send a message to the ledger recording the transaction. Basically this is modelling the ways a real payroll department might do it IRL. The "john" object is a record in a filing cabinet, and the methods are things you can do with the 'john' object. There might also be a payroll clerk, who you ask (call a method) to do the payroll for john, and the payroll clerk then interacts with both the bank and the general ledger (or whatever the gently caress payroll clerks do). So by converting the entities and processes into objects with methods and properties, you convert a physical process into a computer program. Have a study of the way UML works, its a loving great tool for thrashing out OO designs. Once you start programming OO, you can never turn back. Its a loving great methodology.
|
# ? May 27, 2009 09:50 |
|
I wanted to contribute to this thread because I use python a ton. I was reading a few pages back and saw there was some confusion about how to implement decorators so I figured I'd post two that I've used fairly recently: The first example wraps a function in some log messages that will tell you when a function starts and when it ends and what values it took as arguments. I've used this quite a bit in a threaded application to track down some bugs I encountered and changed the "print" statements to logging calls when it went into production. Just add "@log" above your function declaration to make it work. code:
code:
|
# ? May 27, 2009 14:05 |
|
dustgun posted:If I want to make a site that's basically only index.py, what's the best way to do that? I'm honestly not quite sure what magic words I should be searching for. Little bit late to the party, but I just discovered this project not long ago. It's essentially a filesystem based blog written in Python, with a mess of plugins and "flavours" (style and look, etc). Fairly easy to set up. http://pyblosxom.sourceforge.net/ Also, I love having Python on my iPhone so I can script my way around anything, even apps. Plus it's handy to be able to run Django on my phone. Not that it's practical in any way, but was really, really fun to get working.
|
# ? May 28, 2009 02:55 |
|
Captain Capacitor posted:Little bit late to the party, but I just discovered this project not long ago. It's essentially a filesystem based blog written in Python, with a mess of plugins and "flavours" (style and look, etc). Fairly easy to set up. I used this a number of years ago before moving on to a fully blown CMS. Pros: it's just like the original Blosxom, except saner and written better. The plugins are a nice idea. Cons: it's just like the original Blosxom, which - despite any number of plugins - always acts just like and only a blog.
|
# ? May 28, 2009 08:28 |
|
Mechanize is giving me fits. Basically, it doesn't recognize that there are any forms on this page. Here's some demonstration code: code:
code:
Other Browser methods will follow links on that page, but forms just won't work. I can get forms on other sites to work fine. I'm trying to write a script that will invite to our Steam Group anyone who ranks above a certain rank on our Psychostats. This requires logging in and maintaining cookies and whatnot...thus the use of mechanize.
|
# ? May 29, 2009 21:01 |
|
Thermopyle posted:Mechanize is giving me fits. I ran this on OSX and had no problems (did install mechanize using easy_install, tho). I would suggest skipping the whole browser parsing nonsense, since this module seems to choke on certain HTML (try running br.forms() ). Try using urllib2 and just making the HTTP requests yourself. Use Firebug to monitor the request/response while you send out an invite manually and just replicate the process.
|
# ? May 30, 2009 02:07 |
|
Captain Capacitor posted:I ran this on OSX and had no problems (did install mechanize using easy_install, tho). I would suggest skipping the whole browser parsing nonsense, since this module seems to choke on certain HTML (try running br.forms() ). Try using urllib2 and just making the HTTP requests yourself. Use Firebug to monitor the request/response while you send out an invite manually and just replicate the process. I ran it on OSX, python2.6, mechanize 0.1.11 and got some weird poo poo. I just called br.open(), and then br.forms() and got: code:
|
# ? May 30, 2009 02:38 |
|
Sailor_Spoon posted:I ran it on OSX, python2.6, mechanize 0.1.11 and got some weird poo poo. I just called br.open(), and then br.forms() and got: I vaguely remember twill using an old and modified version of mechanize, I don't remember though
|
# ? May 30, 2009 03:33 |
|
m0nk3yz posted:I vaguely remember twill using an old and modified version of mechanize, I don't remember though This was using trunk, which has 0.1.11
|
# ? May 30, 2009 04:05 |
|
Sailor_Spoon posted:I ran it on OSX, python2.6, mechanize 0.1.11 and got some weird poo poo. I just called br.open(), and then br.forms() and got: This is odd. I get the same errors I mention in my post on linux with python 2.5 and windows with python 2.6. Twill also gives me the same errors. Guess I'll dig in to it more.
|
# ? May 31, 2009 01:56 |
|
It's throwing a string as an exception and the exception handler is trying to access the msg attribute.
|
# ? May 31, 2009 02:04 |
|
Anyone having problems using distutils install under Windows 7 64bit? I am getting the following when running "python setup.py install" on the Twisted source.code:
|
# ? May 31, 2009 02:33 |
|
supster posted:Anyone having problems using distutils install under Windows 7 64bit? I am getting the following when running "python setup.py install" on the Twisted source. Windows Server 2008 x64 shits itself on virtualenv, easy_install and pip.
|
# ? May 31, 2009 05:59 |
|
|
# ? May 9, 2024 07:04 |
|
What is the "accepted" way of implementing a simple substring search for plain text? I think it's called that. Basically a user types in a search term and it searches for those exact characters, so it also shows them if they're within a word. I've thought about it, and I figured this might be the way to do it: 1. Get input 2. Find length of input 3. Step through text in these lengths, one character at a time 4. Return when string is found But there might be a better way? Also, if I wanted to highlight all instances of the found string, would I have to store the locations in a list or something? I want to highlight them in the text, but on a command line, so I need the locations of the specific characters.
|
# ? May 31, 2009 20:05 |