|
BigRedDot posted:Every feature of a language should be used judiciously. Python dictionaries are wonderful, but their over-allocation growth is extremely aggressive. If you don't understand this you can easily write extremely suboptimal code. quote:Can you expand or provide some examples for why decorators in particular are different? Right now this admonishment sounds like a "just-so" story. They aren't fundamentally different, like I said, it's a matter of "some more than others". It's based on their potential of abuse and how much damage that abuse can cause. Dicts are not very easy to abuse (unless you start using them in lieu of classes or something ridiculous like that). I think decorator abuse is more common. I have both seen people do it and been guilty of it myself. (since you asked for specifics, this is going to get long) Something about decorators just sort of makes them look like good fits for solving certain kinds of problems but there are significant drawbacks associated with them which are not really obvious (until you've ran into them). Authorization frameworks and caching are some usual suspects for making you fall into these traps IMO. I guess it's because at first glance it looks like a neat separation of concerns - e.g. "oh this method is actually all about doing this one specific thing, and figuring out authorization/caching/logging is just incidental to that! I know, I'll break those out in a decorator, leaving the method body with just a single responsibility and encouraging code-reuse by having a generic auth/cache/logging framework! How wonderful!" This may well be the case except whereas before you had two simple problems (do this one thing, and also cache that one thing), you now have one simple problem (do the one thing) and one much more complex one (a general, re-usable caching framework that has to adapt to currently unknowable requirements). You'll notice this as you find your decorators more and more complicated to configure. What started out as @memoize quickly becomes @memoize(expiration=123) which then becomes @memoize(expiration=123, invalidate_on=signal) which then becomes @memoize(expiration_strategy=(memoize.rule('user-cache-invalidation-signals @ invalidated') && memoize.rule('oh god what I done'))). You also tend to run into fact that these little ancillary concerns actually depend on some runtime state that you easily have access to inside your method body, but not outside of it (at compile time), so now you have to decide between tightly coupling your fancy decorators to specific method signatures or doing something generic but really really complicated. So... you could do that. Or you could just put an if statement inside your method body. You can of course also go off the rails into architecture-space-journey inside your method body as well, but I really think you are a lot less likely to do so since you have so much more freedom to go with a much simpler situation-specific solution. You can also use decorators in some places and forgo them for simple local solution as needed. And of course there are always going to be applications where decorators really are the best solution, e.g. because you specifically need access to some metadata outside the method body execution. The separation of concerns they can bring isn't imagined either - it really can be a much better way to write your app, you just need to be aware of what you're committing to. Like I said, it's just a matter of judicious use and "some more than others". The most egregious abuse of decorators I've seen was this auto-html-admin type builder for an API where the average ratio of decorators to method bodies was easily 5:1. You configured the whole page in the decorator - the widgets used, the tables being generated, the validation and data marshaling rules. It was loving nuts. The actual method body would be some piss-trivial operation like adding the given item to some list. It is way past the point of practical unfucking, the only way forward is to add even more generic configurability to it. I'd bet my right hand that thing started with something like "hmm, I could probably use decorators for this, it will be so neat". This may be why I err on the side of being conservative when it comes to decorators . Also just in general I've been burned by "time saving" magic far more often than I've been noticeably helped by it, so I tend to be skeptical (though hopefully not outright dismissive) about cool language tricks as a rule. Mrs. Wynand fucked around with this message at 06:45 on Dec 3, 2013 |
# ? Dec 3, 2013 06:42 |
|
|
# ? May 30, 2024 14:12 |
That was a great post to read, well done I think I'll stick with the if statement for now.
|
|
# ? Dec 3, 2013 07:24 |
|
PyCharm Community Edition I hate an IDE war as much as everybody else, but I just used PyCharm for the first time today. I didn't want to fork $ for it back when it was closed for development at home. Now at work I am so fed up with not have ReSharper-like power with Python source that I was about to go start a trial license. Then I found out the community edition had happened. I had to merge 2 months of garbage code into a branch manually and make some tactful decisions along the way, and it just ate it right up. I was assuming it was going to take me over two hours just to get that first pass done, but I had it done in 40 minutes, starting with opening up PyCharm for the first time. I imagine it's overkill for some smaller stuff, but for doing code archaeology, I couldn't imagine anything else. Okay, I have to see what happens when I try to use the debugger . . .
|
# ? Dec 3, 2013 07:28 |
|
My Rhythmic Crotch posted:I think the simplest solution would be one table to hold the symbols, then another table that has a column for date, one column for each kind of attribute, and then a foreign key to the symbol table. Dominoes fucked around with this message at 12:06 on Dec 3, 2013 |
# ? Dec 3, 2013 12:02 |
|
Dominoes, I don't have a class to recommend but this is one of those times where you could probably get a huge benefit from taking a step back from your problem to run through a quick intro to relational databases class or tutorial.
|
# ? Dec 3, 2013 13:36 |
|
Is there any way of calling an object by a parameter value? (For example, if I needed to find a 'baseball' object with the highest 'size' attribute.)
|
# ? Dec 3, 2013 17:38 |
|
Rocko Bonaparte posted:PyCharm Community Edition
|
# ? Dec 3, 2013 17:41 |
|
I just wish PyCharm didn't look like poo poo on Linux. Apparently to get anti-aliased fonts, you have to patch the Java VM. You also have to use Oracle Java. The completer is amazing though, really wish I could get the same results in Sublime.
|
# ? Dec 3, 2013 17:55 |
|
Eggie posted:Is there any way of calling an object by a parameter value? (For example, if I needed to find a 'baseball' object with the highest 'size' attribute.) Just like, finding the largest value of a property from a list of objects? max(baseball.size from baseball in baseballs) is probably more idiomatic. You can also do max(baseballs, key=lambda baseball: baseball.size) or max(baseballs, key=operators.attrgetter('size') if you want something slightly more like ruby's crazy baseballs.max(&:size).
|
# ? Dec 3, 2013 18:06 |
|
evensevenone posted:I just wish PyCharm didn't look like poo poo on Linux. Apparently to get anti-aliased fonts, you have to patch the Java VM. You also have to use Oracle Java. I exclusively use PyCharm on Linux, and I've never had any complaints about its appearance, nor have I had to patch the JVM to have antialiased fonts. In my experience, JetBrains' IDEs work perfectly fine with OpenJDK 7 -- I mainly use the Oracle JVM to avoid having to remove the OpenJDK check from the launcher shell script every time I update the IDE.
|
# ? Dec 3, 2013 18:10 |
|
Eggie posted:Is there any way of calling an object by a parameter value? (For example, if I needed to find a 'baseball' object with the highest 'size' attribute.) Uh, could you restate that, perhaps with a small bit of example code? I wrote something like Mr. Wynand's reply, but on re-reading I'm still not sure if that's what you're asking for.
|
# ? Dec 3, 2013 18:13 |
|
Lysidas posted:I exclusively use PyCharm on Linux, and I've never had any complaints about its appearance, nor have I had to patch the JVM to have antialiased fonts. Weird. I'll post a screenshot when I get to work, but for me only the editor is antialiased; the menus and even the tree on the left are not (and very ugly). I'm using xubuntu but I can't really imagine that's a factor. This indicates that I'm not alone in this: http://youtrack.jetbrains.com/issue/IDEA-57233 . What distro are you using?
|
# ? Dec 3, 2013 18:26 |
|
Kubuntu 13.10 on all of my machines, and PyCharm has always looked great even as far back as 11.04.
|
# ? Dec 3, 2013 18:29 |
|
If you're running the Infinality freetype patches you need to use the Oracle java package + need to patch a file or edit a config or something. I said this last page, but Jetbrains are very lenient w/r/t handing out full licenses for open-source projects.
|
# ? Dec 3, 2013 18:46 |
|
Holy poo poo PyCharm is awesome, don't know how I didn't hear about the community edition until now
|
# ? Dec 3, 2013 19:48 |
|
Does the community edition do that remote interpeter vagrant magic? I sort of discounted using pycharm out of hand since I assumed it needed to run on the local python env and I always always always run the actual servers in a headless VM. But turns out it can absolutely do that that workflow, so now I am interested.
|
# ? Dec 3, 2013 20:09 |
|
Also I had no idea python 3 introduces advisory type annotations (and some people, notably jetbrains, are actually trying to build tools on top of that). I'd love me a type-inferred python with generics and poo poo. I now care about python 3.
|
# ? Dec 3, 2013 20:11 |
|
Mr. Wynand posted:Does the community edition do that remote interpeter vagrant magic? I sort of discounted using pycharm out of hand since I assumed it needed to run on the local python env and I always always always run the actual servers in a headless VM. But turns out it can absolutely do that that workflow, so now I am interested. According to this, it doesn't: http://www.jetbrains.com/pycharm/features/editions_comparison_matrix.html Although the VM integration is pretty sweet as it allows you to even init, start, stop, and destroy vagrant instances. The one problem I have is whenever I install a new package to the remote interpreter, the inspections doesn't pick up on the new package at all. The only way I've been able to fix it is by restarting PyCharm.
|
# ? Dec 3, 2013 21:21 |
|
Mr. Wynand posted:Also I had no idea python 3 introduces advisory type annotations (and some people, notably jetbrains, are actually trying to build tools on top of that). You can also get pycharm to prompt you about type issues on python 2.x projects by including type hints in docstrings. http://www.jetbrains.com/pycharm/webhelp/type-hinting-in-pycharm.html
|
# ? Dec 3, 2013 21:24 |
|
Thermopyle posted:You can also get pycharm to prompt you about type issues on python 2.x projects by including type hints in docstrings. Neat-o! Does their JS editor do this sort of thing as well? (I actually use jsdoc somewhat consistently) I assume they also make stand-alone fancy js/node editors.
|
# ? Dec 3, 2013 21:27 |
|
Computer viking posted:Uh, could you restate that, perhaps with a small bit of example code? I wrote something like Mr. Wynand's reply, but on re-reading I'm still not sure if that's what you're asking for. If you'll ignore my baseball example earlier, I'm trying to code a pathfinding program using the A* method. So far my program can take a starting node and calculate the FGH values of all adjacent nodes. The step I'm stuck at is calling the node with the lowest F value. At the risk of letting everyone see my ugly code, here's the for-loop that takes all nodes in the Open List and calculates their FGH values by comparing their x, y positions to an ending node. code:
|
# ? Dec 4, 2013 01:41 |
|
Ok then yes, that thing I said (except using min instead of max if you want lowest obvs).
|
# ? Dec 4, 2013 01:50 |
fletcher posted:
After reading about this a bit more, I'm a little confused. I was thinking cached_expensive_stuff = None was initializing an instance variable, not a class variable. I don't want different instances of Thingy using the same cached_expensive_stuff. But from my test, it doesn't seem like they are using the same cached_expensive_stuff, so it works like I intended? Python code:
code:
|
|
# ? Dec 4, 2013 02:45 |
|
"Python 3 to Javascript translator written in Python that produces fast portable javascript code." Correct me if I'm wrong, but if it's possible to translate between languages, then why can't you just write an entire project in one language and then translate it into another? Although it's prolly cause 1. machine translation isn't that great and 2. working backwards from what a language creates is a pain, especially if it's compiled. Right?
|
# ? Dec 4, 2013 02:52 |
|
fletcher posted:Is it both an instance and a class variable or something? I'm coming from Java land if that matters. the tl;dr version, is that: - there's a class attribute set to None, and new instances of that class will have None as the value. - instances of this class can read the value of class attribute given a name using "self.name", "self.__class__.name" or "cls.name" (in a class method) - a particular instance has the ability to "shadow" the class attribute by setting an instance attribute of the same name using "self.name = new_value. -- conceptually, this is similar to "monkey patching" (http://stackoverflow.com/a/5626250/171094) Lurchington fucked around with this message at 03:03 on Dec 4, 2013 |
# ? Dec 4, 2013 02:59 |
|
Pollyanna posted:"Python 3 to Javascript translator written in Python that produces fast portable javascript code." Google is doing something very similar to get their Dart scripting language going - it can optionally output javascript so that browsers that don't understand Dart can still run Dart apps.
|
# ? Dec 4, 2013 05:08 |
|
My Rhythmic Crotch posted:Writing in one language and executing in another is perfectly possible. Normally, compilers are used to build machine code from high level code. Ohh. Well...that's not so bad really. Maybe I'll check it out. Also, I have a problem with virtualenv. I'm trying to install packaged from a requirements.txt, which includes numpy and matplotlib, and I get this error: code:
|
# ? Dec 4, 2013 06:13 |
|
Pollyanna posted:Ohh. Well...that's not so bad really. Maybe I'll check it out. Quite a lot of languages used to compile down to C, since there are C compilers available for ca. everything ever. Some still do, I think. Javascript fills a similar "available everywhere"-niche, so it's not surprising that there's a bunch of "compile to JS" projects. As for the error, does this look vaguely relevant? https://github.com/ContinuumIO/anaconda-issues/issues/33 If I understand it right, the problem is that is runs code from /Users/rebecca/anaconda/ , but the python interpreter isn't the anaconda one. Somewhere in their code they're parsing the python version string, and the one produced by whichever python version you use doesn't quite fit their assumptions. Computer viking fucked around with this message at 12:20 on Dec 4, 2013 |
# ? Dec 4, 2013 12:14 |
|
Well, being able to compile to JS from Python makes writing a simple game in JS a lot easier at the very least. Computer viking posted:As for the error, does this look vaguely relevant? Dang. How do I change my current Python version, again? I tried editing .bash_profile but it didn't change whether or not I used Anaconda. As for the current interpreter, calling sys.version tells me that it's the Anaconda interpreter. vv
|
# ? Dec 4, 2013 18:37 |
|
Pollyanna posted:Well, being able to compile to JS from Python makes writing a simple game in JS a lot easier at the very least. You mean when you type 'python somemodule'? Try changing your PATH. If you're using a shebang ('#!/usr/bin/env python') and typing './somemodule' then you can just specify the specific Python you want directly
|
# ? Dec 4, 2013 19:03 |
|
I built a basic web app using BaseHTTPServer that does simple formatting based on an sqlite database. It works totally fine as is, and importantly automatically launches my browser when I run the script:Python code:
|
# ? Dec 4, 2013 22:55 |
|
Use multiprocess or threading.
|
# ? Dec 4, 2013 23:17 |
|
I want to make a browser interface for an invoicing thing I've made. I know nothing about web development. The interface being a way to edit old / add new invoices to an sql database and to run invoice program on the selected invoice to spit out the PDF. I'm not going to be running it actually over any sort of network, I just want a browser interface on the local machine. Is bottle/flask the sort of thing I want for this? Is using an sql database to store the invoice information overkill?
|
# ? Dec 4, 2013 23:58 |
|
ArcticZombie posted:I want to make a browser interface for an invoicing thing I've made. I know nothing about web development. The interface being a way to edit old / add new invoices to an sql database and to run invoice program on the selected invoice to spit out the PDF. I'm not going to be running it actually over any sort of network, I just want a browser interface on the local machine. Is bottle/flask the sort of thing I want for this? Is using an sql database to store the invoice information overkill? Serious question: Have you considered just using an existing invoicing app? It is quite possibly one of the most oft-developed types of app ever. I like harvestapp myself, and unless your company is Deloitte & Touche or something, I assure you there will be one for your specific needs as well. Barring that, you could use one of those magic & wizards graphical DYI relational database builders - like good ol' MS Access, or FileMaker (there's a bunch of hip new web-based ones as well, but I couldn't recommend you a specific one). It's a very very very very common problem to solve. Barring that as well, you probably want something that can auto-generate an admin UI, probably django, but good luck packaging that up to run painlessly on, say, a windows desktop. I would just host it on heroku or something, tis not expensive (can even be free if you barely ever use it).
|
# ? Dec 5, 2013 00:08 |
|
Pollyanna posted:Well, being able to compile to JS from Python makes writing a simple game in JS a lot easier at the very least. How so? You're still going to need to understand whatever JS library you're using thoroughly in order to build any kind of game. You're just adding another layer of difficulty on top of it imo. If you want to make a game in JS, make a game in JS. http://www.antonoffplus.com/coding-an-html5-game-for-30-minutes-or-an-introduction-to-the-phaser-framework/
|
# ? Dec 5, 2013 00:56 |
|
onionradish posted:I built a basic web app using BaseHTTPServer that does simple formatting based on an sqlite database. It works totally fine as is, and importantly automatically launches my browser when I run the script: Something like this should do what you need. Python code:
|
# ? Dec 5, 2013 07:09 |
|
PyCharm PEP 8 flagger issue:PEP8 posted:If operators with different priorities are used, consider adding whitespace around the operators with the lowest priority(ies). Use your own judgment; however, never use more than one space, and always have the same amount of whitespace on both sides of a binary operator. PyCharm's PEP8 analyzer flags not using whitespace in the example above. Is there a way to correct this behaviour without disabling the "missing whitespace around arithmetic operator" entirely? Seems like a bug.
|
# ? Dec 5, 2013 12:31 |
|
Dominoes posted:PyCharm PEP 8 flagger issue: PyCharm doesn't flag those for me. I'm running version 131.618.
|
# ? Dec 5, 2013 16:26 |
|
Mr. Wynand posted:Serious question: Have you considered just using an existing invoicing app? It is quite possibly one of the most oft-developed types of app ever. I like harvestapp myself, and unless your company is Deloitte & Touche or something, I assure you there will be one for your specific needs as well. The primary reason I started this was a learning experience, not because there's any pressing need for a poo poo hot invoicing application. Making my work life a little easier whilst learning seems like a win-win to me. VV Thanks. ArcticZombie fucked around with this message at 17:57 on Dec 5, 2013 |
# ? Dec 5, 2013 17:30 |
|
|
# ? May 30, 2024 14:12 |
|
ArcticZombie posted:I want to make a browser interface for an invoicing thing I've made. I know nothing about web development. The interface being a way to edit old / add new invoices to an sql database and to run invoice program on the selected invoice to spit out the PDF. I'm not going to be running it actually over any sort of network, I just want a browser interface on the local machine. Is bottle/flask the sort of thing I want for this? Is using an sql database to store the invoice information overkill?
|
# ? Dec 5, 2013 17:43 |