|
Ugg boots posted:Thanks for the help, but it's not quite that applicable for my problem, because I'm dealing with collisions between two different objects (of different types.) Google for "multimethods in python". You probably won't want as many indirections and searches as most of the implementations would likely have, but it should give you an idea of how to structure some well-thought out look up tables or whatever solution you decide upon.
|
# ¿ Nov 8, 2007 09:02 |
|
|
# ¿ Apr 28, 2024 19:17 |
|
code:
|
# ¿ Jan 9, 2008 08:29 |
|
code:
e: the decorator with args decorator is the first hit from google, an old ASPN classic. Quite useful. e2: of course this means you can modify these function properties outside of the function call foo.lemons = "soup";foo(), which may or may not be desirable, but just because you have a scab doesn't mean you have to pick at it. Lonely Wolf fucked around with this message at 20:34 on Jan 9, 2008 |
# ¿ Jan 9, 2008 20:21 |
|
There are few cases I can think of where the decorator would be useful. If you need a C-style static variable or a simple singleton, it's your man. Otherwise a class is certainly more useful. I don't think it seems too magical or hackish, but then again half the fun I have in python (or any language) is just playing around with the internals and making it do things it wasn't meant to, so my point of view is a bit skewed. I couldn't recommend using it in a large project where its behavior may come as a surprise, or a multithreaded one where its behavior could degrade randomly, but for something small or one-off it's definitely less verbose; and, at any rate, easily refactored into a proper class come maintenance time. At the very least, it's something we can drop into our stupid python tricks bag.
|
# ¿ Jan 9, 2008 22:06 |
|
(Seperate issue, so seperate post) Has anyone had any experience with Pylons? I haven't done any web work, and I have some time off and lessened work load, so I'd like to play around with something. Django et al seem too straight-jackety. I don't really want to do anything with it, and am happy to just play around in its undocumented internals for fun, but if anyone has comments, criticisms, or tips about it I'd be happy to hear them.
|
# ¿ Jan 9, 2008 22:14 |
|
Bonus is right. To modify a global in a local namespace you need to use the global statement.
|
# ¿ Feb 11, 2008 20:55 |
|
Scaevolus posted:(I'm not yet sure how I feel about the [expr] if [cond] else [expr] syntax). I prefer it because it's easier and more natural to read, which is one of the major reasons I prefer to python.
|
# ¿ May 23, 2008 06:44 |
|
I'm working on a metaclass that builds a lot of repetitious code from a class attribute. No problems there. But I also want it to generate some automatic documentation for the class and the class's __init__. It seems wasteful and error prone to do this repetious documentation by hand. Unfortunately __doc__ is immutable. There seem to be a number of messy ways around this. This is for a hobby library and I'm trying to simplify it for people who want to extend the library as well as people who just want to use code by them or me. What I want to know is if there's a 'proper' way to deal with this, or at least a clean way. I'd rather stick to the standard library. I can post the code if necessary, but it's a dirty sketch as I play around with possible designs, filled with numerous stanzas irrelevant to the discussion, and I basically just want to know how to use a metaclsass to alter docstrings.
|
# ¿ May 28, 2008 02:53 |
|
I'm sorry. I was unclear. I am using __new__, the problems is updating the docstring of the __init__ method of the class being created. Note that I'm using attrs where Milde used dct and that the gross assumptions my code makes about how the class's __init__ method operated are valid in context, and that I've tried to cutaway code and comments that don't apply to the questions at hand.code:
|
# ¿ May 28, 2008 04:01 |
|
Awesome! Thanks for the help. Where in the documentation would I find more about these abstrusely named properties?
|
# ¿ May 28, 2008 04:58 |
|
ATLbeer posted:
You're throwing all your parameters away and even with the scope issuse resolved it's not going to do anything. It should be self.Text = Text, etc. Also, the official and idiomatic style is to only start names with capitals iff they are the names of classes.
|
# ¿ Jul 4, 2008 06:41 |
|
I often find something like this useful:code:
|
# ¿ Oct 23, 2008 08:09 |
|
You're not instantiating your room objects. It needs to be room1 = room(), etc. However, you're going to have far more problems than that. You should probably read up on classes and objects in the Python tutorial.
|
# ¿ Nov 2, 2008 20:05 |
|
chemosh6969 posted:I do it when I expect an error to happen but don't care about logging it because I know what it is, or don't care, and want to program to do whatever it's doing. Even if you don't care about logging it do something like except OSError: pass so you can be like wtf TypeError? in the rare case that something truly, you know, exceptional happens.
|
# ¿ Nov 14, 2008 01:05 |
|
Right, why have a computer do something automatically that you can do manually
|
# ¿ Nov 14, 2008 01:13 |
|
I don't think we're disagreeing as much as you think we are. I'm okay with ignoring exceptions, even not logging them. (In very simple shell script like programs, of course). I'm just saying only ignore the exceptions that you know will happen and don't care about so that when something subtle or uncommon happens your script terminates with a stack trace saying hey I'm broken, broken right here, and broken in this way. It beats what the gently caress why isn't this piece of poo poo working?
|
# ¿ Nov 14, 2008 01:36 |
|
The whole logging thing seems to be leading to tangents so here's the core summation: except: pass is always troublesome, even if not immediately so or if not apparently so. However, ignoring exceptions that you want to safely ignore is fine, just use except IgnorableException: pass instead, so that you are in fact always safely ignoring them. That said, feel free to ignore the above advice and any exceptions if you realize that it is a bad practice and a potential source of bugs but you guage the likelihood of such bugs to be close enough to never to not give a drat.
|
# ¿ Nov 15, 2008 02:38 |
|
Well, I'm glad he showed us that, since it means we can discount his opinions and arguments wholesale and move on with our lives.
Lonely Wolf fucked around with this message at 03:55 on Nov 15, 2008 |
# ¿ Nov 15, 2008 03:52 |
|
duck monster posted:
code:
|
# ¿ Nov 15, 2008 19:55 |
|
chemosh6969 posted:I'm interested it learning how to use python to search for and download movie info from places like imdb.com and allmovie.com. I know you mean using Python to collect the information, but http://www.imdb.com/interfaces#plain It's free for personal use, but you have to work it out with them if you want to use it on a web site. I assume that means a lot of money. As far as using Python google "python screen scraping"
|
# ¿ Nov 29, 2008 03:38 |
|
I don't know anything about macs, but it looks like you don't have libreadline installed.
|
# ¿ Nov 29, 2008 04:22 |
|
host=website, post=80 I assume that should be port
|
# ¿ Dec 22, 2008 19:46 |
|
Are you asking how to bang him? . . . If not, do you have a question?
|
# ¿ Jan 14, 2009 02:12 |
|
that's because type, et al, are class variables not instance variables. Also, help(str.split). e: et all? Am I retarded? Lonely Wolf fucked around with this message at 02:47 on Jan 15, 2009 |
# ¿ Jan 15, 2009 02:34 |
|
You need to set them in the body of init not the class body.
|
# ¿ Jan 15, 2009 02:47 |
|
I think for his purposes something more likecode:
|
# ¿ Jan 30, 2009 20:54 |
|
But us doing your homework will help you on the midterm?
|
# ¿ Feb 10, 2009 16:18 |
|
fnordcircle posted:
You think that's cool? code:
|
# ¿ Feb 23, 2009 16:00 |
|
Maybe you could try something, which I normally wouldn't suggest, like this:code:
edit: Beaten. That's what I get for typing this while watching TV. Good point about ctrl+c, that extra clause gets rid of my concerns. Lonely Wolf fucked around with this message at 18:34 on Mar 9, 2009 |
# ¿ Mar 9, 2009 18:29 |
|
the new matrix has a different id but check the ids of its elements vs the ids of the old matrix's elements to see what what was said.
|
# ¿ Mar 13, 2009 04:23 |
|
I hope this helps or whatever:code:
|
# ¿ Apr 6, 2009 00:56 |
|
He's clearly simplified it, for pedagogic reasons.
|
# ¿ Apr 6, 2009 03:57 |
|
The only time I've seen a decorator return a non-callable is to return a property type in a class body.
|
# ¿ May 1, 2009 22:39 |
|
Clanpot Shake posted:
also you can loop over a file with code:
code:
|
# ¿ May 10, 2009 00:07 |
|
spankweasel posted:Why would you cast i to a string? I was just keeping it close to his code there. If you'll notice the second time I didn't, though I did keep it as %s in the format string, implicitly casting it to repr, because I was far too lazy to remember that it was supposed to be %d.
|
# ¿ May 10, 2009 03:25 |
|
Since the almost univeral answer to "I want to learn how to program what should I learn?" on these forums is Python, it might not be a bad idea to at least have a section of first-time friendly links.
|
# ¿ May 14, 2009 05:52 |
|
dancavallaro posted:I used py2exe to make an executable of a Python script I wrote. It works great on the Windows XP computer that I created it on, and it works great on my Windows Vista laptop at home. But it doesn't work on my boss's Vista computer, and fails with the error "the application has failed to start because its side-by-side configuration is incorrect. Please see application event log for more details". Any ideas what could be wrong? Did you check the log?
|
# ¿ May 14, 2009 23:41 |
|
It's throwing a string as an exception and the exception handler is trying to access the msg attribute.
|
# ¿ May 31, 2009 02:04 |
|
Likewise I don't see why auto-registering a class is so problematic and impossible. It is semi-magicy but looks reasonably straightforward and less work than defining a plugin system that feels less Pythonic than the metaclass approach to me. What problems have you had with such things? To answer the original question, re metaclasses, the metaclass is called when the new classes are instantiated (versus when the class's objects are instantiated) and just adds each plugin class to a list that you can get to easily.
|
# ¿ Jun 6, 2009 21:59 |
|
|
# ¿ Apr 28, 2024 19:17 |
|
Janin posted:There's no way to separate the definition and registration of a plugin. This makes testing difficult -- do you want all your test classes showing up as plugins? Both of these seem like implementation details that could be handled by the metaclass either letting you put a sentienel to determine a test case or catching errors and passing them to the application to handle. quote:Violates "explicit is better than implicit" -- registration is hidden in a magic class, and is difficult to discover except through grepping through the source for "__metaclass__". This is definitely a valid concern. I see it as a tradeoff. You're providing a little magic to make it easier for someone to worry about writing a plugin for your app without having to do a lot of boilerplate config files. quote:Violates the principles of duck-typing -- merely implementing a prescribed interface is no longer sufficient, as only classes inheriting from the registration class will be found. This is the worst part of the metaclass scheme, but the simplicity is worth it in my opinion. Maybe I've spent too much time playing in metaprogramming land to have a sane perspective though. quote:Multiple applications can't share plugins without a separate, shared registration library. I don't see how that isn't true for any plugin system.
|
# ¿ Jun 6, 2009 23:15 |