|
KICK BAMA KICK, I think that you should be able to do what you want to do using SQLalchemy's events. The following (untested, incomplete, possibly fundamentally unsound) modification to your example code might do it.Python code:
I've never actually personally used sqlalchemy's event API, so I have no idea if this will actually work, but it was fun figuring it out
|
# ? Nov 15, 2014 22:14 |
|
|
# ? May 26, 2024 10:13 |
|
Thanks to both of you; Thermopyle's post introduced me to the term "generic relationship" and I found sqlalchemy-utils, which provides a simple implementation. I can make a collection of those easily enough with an intermediary table but unfortunately generic relationships break automatic cascading, which complicates things. Haystack's solution seems really close, but there's one snag. A roles dictionary looks like this after the classes are defined: code:
|
# ? Nov 15, 2014 23:12 |
|
Oops. I love SQLA, but man does it ever have a lot of abstraction layers. Try:Python code:
|
# ? Nov 16, 2014 01:17 |
|
That turns out to be an InstrumentedAttribute object. I scrounged the docs and nothing is jumping out at me. Tried poking around in mapper.attrs but couldn't make sense of that either.
|
# ? Nov 16, 2014 02:11 |
|
Huh. I know what the problem is but I'm a bit stumped as how to circumvent it. Basically, those InstrumentedAttributes are the proxy objects that sqla uses to do things like making employee.name = "bob" work with all the side-effects that it needs (like knowing what's been changed or not). So it's close, but not quite right. I'll keep poking at it, but at this point I'd recommend taking your question to the sqla mailing list where actual experts can help you out.
|
# ? Nov 16, 2014 03:28 |
|
Appreciate all your help; learned a lot even if we didn't solve it. I tried some different ideas in that event handler but I'm not even sure it fires consistently. I think I saw some really odd behavior -- different results based on whether a simple print statement I was using to inspect a variable was commented out, I could swear I even saw different results just running the exact same code consecutively.
|
# ? Nov 16, 2014 16:08 |
|
Goddamnit that was so simple it's worth a double post. Mapping Class Inheritance; the example is even nearly the exact case I was using as my example.code:
|
# ? Nov 16, 2014 21:30 |
|
Hey, any pitfalls in using an abbreviation like sf in place of self? Seems like an easy way to improve readability by reducing clutter, and should be obvious to other users reading the code. I'm surprised I haven't read about this.
|
# ? Nov 17, 2014 21:01 |
|
Dominoes posted:Hey, any pitfalls in using an abbreviation like sf in place of self? Seems like an easy way to improve readability by reducing clutter, and should be obvious to other users reading the code. I'm surprised I haven't read about this. Please don't change a word everyone understands to save a measly two characters. The reason you haven't read about it that it is a patently bad idea and against the spririt of PEP-8
|
# ? Nov 17, 2014 21:34 |
|
If someone did that in my team I would revert it, take them to one side, and ask why they did something in the name of readability and yet broke convention and idiom.
|
# ? Nov 17, 2014 21:35 |
|
tef posted:Please don't change a word everyone understands to save a measly two characters. two * a_lot of characters. self cluttering is an issue when using complex expressions/comprehensions, especially with long variable names. Dominoes fucked around with this message at 22:20 on Nov 17, 2014 |
# ? Nov 17, 2014 22:13 |
|
Dominoes posted:two * a_lot of characters. Hi, I write Python all day every day. Nobody cares about sf. If I looked at your code without being familiar with "sf", I'd be looking all over the place to find out where you imported that or where you set it as a global or something else. Everyone knows what "self" means. Explicit is better than implicit. Don't try to golf two characters with 'self' -> 'sf'. pep8 and pyflakes exist for a reason. Your code should cleanly pass linters.
|
# ? Nov 17, 2014 22:56 |
|
Dominoes posted:two * a_lot of characters. Use simpler expressions. Find the balance point between long and short variable names. Consider using comments?
|
# ? Nov 17, 2014 23:34 |
|
Dominoes posted:two * a_lot of characters. Split your complex expressions/comprehensions over several lines to improve readability. Dropping two letters from "self" breaks a convention that every Python user uses, and while it's true that you can do it you're not really gaining anything by doing so
|
# ? Nov 18, 2014 00:11 |
|
evol262 posted:Hi, I write Python all day every day. Speaking of linters, this code does not pass pylint: http://stackoverflow.com/a/6117124 Python code:
|
# ? Nov 18, 2014 00:27 |
|
Dominoes posted:Hey, any pitfalls in using an abbreviation like sf in place of self? Seems like an easy way to improve readability by reducing clutter, and should be obvious to other users reading the code. I'm surprised I haven't read about this. Don't.
|
# ? Nov 18, 2014 02:20 |
|
pmchem posted:Speaking of linters, this code does not pass pylint: http://stackoverflow.com/a/6117124
|
# ? Nov 18, 2014 02:34 |
|
Dominoes posted:two * a_lot of characters. Make a reference with a nice name to self or the attribute you're trying to access before the expression/comprehension. salisbury shake fucked around with this message at 03:38 on Nov 18, 2014 |
# ? Nov 18, 2014 03:19 |
|
"self cluttering is a problem in big expressions or comprehensions" is code smells. Why do you have expressions or comprehensions so complex that you think it would be worthwhile to use a shorter alias for "self"? Are you trying to write Haskell in Python? I'm into functional programming and all, but very few parts of your code should need enough class variables or methods to make this a problem. If there's one method ruling it all and doing everything, you should break it into smaller pieces. Maybe in smaller, private methods. Maybe those methods are generators which handle whatever state your expression or comprehension is currently trying to do. Adding a reference to shorten the way it looks to the programmer is lipstick on a pig. It eliminates clarity and adds complexity for no gain. Is there an example you can show of code that you think would benefit from renaming self?
|
# ? Nov 18, 2014 06:22 |
|
pmchem posted:Speaking of linters, this code does not pass pylint: http://stackoverflow.com/a/6117124 Python code:
|
# ? Nov 18, 2014 07:09 |
|
Dominoes posted:two * a_lot of characters. You have wasted more characters writing these terrible posts than you ever will writing sf instead of self. quote:self cluttering is an issue when using complex expressions/comprehensions, especially with long variable names. Four letters is not a long variable name by any reasonable standard. Grow up. I can only assume you are so bored at your job that you are inventing problems to solve. self is not a long variable name. I'm going to assume poe's law because I want to live in a world where no-one is this stupid to suggest it, or so stupid as to defend it.
|
# ? Nov 18, 2014 16:01 |
|
lol, give the poor guy a break! How many posts do we need to tell him this?
|
# ? Nov 18, 2014 16:19 |
|
I kind of understand Dominoes because writing self over and over is kind of frustrating when you don't need to do that as much in other languages like C++, Java and Ruby.
|
# ? Nov 18, 2014 17:20 |
|
Symbolic Butt posted:I kind of understand Dominoes because writing self over and over is kind of frustrating when you don't need to do that as much in other languages like C++, Java and Ruby. How the heck is writing 'sf' over and over less annoying than writing 'self'?
|
# ? Nov 18, 2014 17:31 |
|
Symbolic Butt posted:I kind of understand Dominoes because writing self over and over is kind of frustrating when you don't need to do that as much in other languages like C++, Java and Ruby. Yeah, because "this" is way less annoying in Java.
|
# ? Nov 18, 2014 18:02 |
|
Symbolic Butt posted:I kind of understand Dominoes because writing self over and over is kind of frustrating Should I punch a hole in the window because i'm not sure how to open it? I think it will be simple and effective
|
# ? Nov 18, 2014 18:35 |
|
You know you can save an extra 50% of characters by using "s" instead of "sf".
|
# ? Nov 18, 2014 19:13 |
|
EAT THE EGGS RICOLA posted:How the heck is writing 'sf' over and over less annoying than writing 'self'? It doesn't, idk I just said I kinda get why he's frustrated with self
|
# ? Nov 18, 2014 19:20 |
|
Holy poo poo!
|
# ? Nov 18, 2014 19:26 |
|
Thermopyle posted:lol, give the poor guy a break! Maybe I have no patience because it's been seven years in this goddamn thread quote:How many posts do we need to tell him this? One didn't seem to be enough. Symbolic Butt posted:I kind of understand Dominoes because writing self over and over is kind of frustrating when you don't need to do that as much in other languages like C++, Java and Ruby. When I was a younger, petulant mess of a programmer, I too waltzed into python and the explicit self. I'd done Java, JavaScript, and a handful of terrible languages not even worth mentioning. I didn't like it much, but I'd often used /this.foo/ instead of /foo/ in Java. I liked to be able to see what I was doing. I wasn't so keen on what seemed to be self sneaking into my method arguments. Now I've been using python for a bit, it's probably the best feature of python. Let's take a step back and look why it's there and what it does for us: Python doesn't have methods: It has functions in classes. This means you can call a method explicitly on an object, ClassName.method(object, args). This also means you can pull the function out and assign it to other classes, ClassOne.foo = ClassTwo.foo. It also means that obj.foo() is actually two operations, method = obj.foo; method(). The choice of making methods out functions gives us unique ways to build classes, and also lets us take a method and pass it around as a function. This is incredibly useful, and it's obvious to see why when we compare it to other languages with objects. In Ruby, you don't even have functions, there isn't really a notion of them. A top level def foo ... end is actually defining a private method on Object. I'll let that sink in: there are no functions in ruby. defining a top level method is monkey patching object. You also have to clumsily call method = object.method(:name_of_method), method.call(), to get the same results. Ruby ends up with a plethora of things that look like functions but all behave differently. Lambdas, Blocks, Procs, Methods. In javascript, when do obj.foo(), and you do method = obj.foo; method() they do two entirely different things. JavaScript programmers must manually bind methods to objects if they want to capture them to pass around. Let us not forget var that = this — this isn't like a variable, nor acts like one, it doesn't obey the scoping rules that other languages have. In python: methods are functions, and when you lookup obj.foo, it partially applies the function, setting the first argument to obj. self is just like any other argument, and can be captured inside nested function and class definitions. In many ways, it's not explicit self, but lexical self. it's just another variable, which is why you can call it anything you like. Python could chose to have both method and function types, and special self keyword, but we'd be doing this = self to lexically bind it, and also fun doing bind and unbind to play about with methods. It gives you all of this expressive power and how do you thank it? Trying to rename it from something *every* python programmer calls it, and *every python programmer* has a habit of writing it. Why do you claim to do it? Readability? Optimization? Time saving. You have already saved yourself so much time by using lexical self that one or two keystrokes isn't going to make a project late. Whining about lexical self and arguing to remove or shorten it for the very reasons lexical self exists. Lexical self is the best goddam thing in python, take a seat, shut the gently caress up, and learn why it's there. I have no loving patience for such foolish behaviour.
|
# ? Nov 18, 2014 19:27 |
|
The absolute broken nature of ruby's ersatz functions is that *adding methods to Object* is considered routine, and *returning a class from inside a method, which captures method arguments* is considered *wtf*. They are living in a world where mashing everything into one namespace makes sense and Foo = namedtuple('Foo', 'x y') is crazy talk. If you want to go over to the world of implict self you are free to go but don't bring any of those trash ideas into python. We have already suffered enough.
|
# ? Nov 18, 2014 19:32 |
|
Guido talks about the importance of self here: http://neopythonic.blogspot.com/2008/10/why-explicit-self-has-to-stay.html although he doesn't address the convention of why self is called "self". For me it simply comes down to having respect and consideration for the other programmers that will have to understand my code later (including myself). I'm not much of a philosopher but Kant's categorical imperative made a strong impression on me and I think it applies to programming: let's all write code such that if everyone everywhere wrote code in that style we'd all be able to understand it. Taking it upon yourself to rename "self", for any reason, obviously breaks convention and is planting little time bombs of misunderstanding in your code that will bite you or, worse, someone who has to maintain your code after you're gone.
|
# ? Nov 18, 2014 20:14 |
|
Most IDEs will put it there for you, anyway.
|
# ? Nov 18, 2014 20:44 |
|
Thanks qntm, Quark, salisbury, evol, and Symb for the explanations; Going to keep using self.
|
# ? Nov 18, 2014 20:58 |
|
I pray that the next bad idea you have doesn't take five people repeating the same thing to you for you to listen
|
# ? Nov 18, 2014 21:10 |
JetBrains recently released free PyCharm Educational Edition, I thought someone may find it interesting.
|
|
# ? Nov 18, 2014 21:15 |
|
tef posted:Maybe I have no patience because it's been seven years in this goddamn thread tef, you're a treasure
|
# ? Nov 19, 2014 01:33 |
|
Shy posted:JetBrains recently released free PyCharm Educational Edition, I thought someone may find it interesting. Is there somewhere that compares this and the community edition?
|
# ? Nov 19, 2014 01:50 |
hooah posted:Is there somewhere that compares this and the community edition? I didn't know there's also a community edition. http://www.jetbrains.com/pycharm-educational/concepts/ posted:PyCharm Educational Edition is based on the Community Edition and comprises all of its functionality. Additionally, it installs and detects Python during installation. It has a simpler UI (adjustable in settings) and adds a new "Educational" project type.
|
|
# ? Nov 19, 2014 02:31 |
|
|
# ? May 26, 2024 10:13 |
|
Yeah, I think I understand why python is the way it is when it comes to self and the object system (mainly because you've been posting on yospos about this subject for so long). It doesn't bother me too much because I don't like to design code around everything being classes and methods, I like functions better most of time so python suits my needs. But I do think it's the consequence of a fundamental tradeoff (intentional or not) that made attributes be the kings of python. Consequently if you're a fan of writing methods you'll get a bumpy ride... And I guess it's just not possible to create a perfect balance for both approaches.
|
# ? Nov 19, 2014 04:39 |